From 9243e7ea0cff4ec172f9d51875089bdf1effca09 Mon Sep 17 00:00:00 2001 From: Segcolt Date: Thu, 26 Mar 2026 17:41:55 -0300 Subject: [PATCH] More problems. --- .../D. Divorce.cpp | 174 ++++++++++++ .../F. UN Finals.cpp | 229 ++++++++++++++++ .../I. Intense Bit Wheel.cpp | 128 +++++++++ .../A. Adrenaline Rush.cpp | 157 +++++++++++ .../B. Quartet Swapping.cpp | 141 ++++++++++ .../A. String Rotation Game.cpp | 118 ++++++++ .../B. Flipping Binary String.cpp | 129 +++++++++ .../C. All-in-one Gun.cpp | 160 +++++++++++ .../A. Bingo Candies.cpp | 132 +++++++++ .../B. Cyclists.cpp | 150 ++++++++++ .../C. Stamina and Tasks.cpp | 123 +++++++++ .../D1. Tree Orientation (Easy Version).cpp | 258 ++++++++++++++++++ .../A. Flip Flops.cpp | 126 +++++++++ Codeforces Round 1087 (Div. 2)/B. Array.cpp | 121 ++++++++ .../C. Find the Zero.cpp | 145 ++++++++++ .../F. Gardening Friends.cpp | 174 ++++++++++++ .../D. Jumping Through Segments.cpp | 154 +++++++++++ .../G1. Division + LCP (easy version).cpp | 163 +++++++++++ .../F. Expected Median.cpp | 149 ++++++++++ .../C. You Soared Afar With Grace.cpp | 163 +++++++++++ problemlist.md | 14 + 21 files changed, 3108 insertions(+) create mode 100644 2018 ACM-ICPC, Universidad Nacional de Colombia Programming Contest/D. Divorce.cpp create mode 100644 2018 ACM-ICPC, Universidad Nacional de Colombia Programming Contest/F. UN Finals.cpp create mode 100644 2018 ACM-ICPC, Universidad Nacional de Colombia Programming Contest/I. Intense Bit Wheel.cpp create mode 100644 2024-2025 ICPC, NERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/A. Adrenaline Rush.cpp create mode 100644 Codeforces Round 1024 (Div. 1)/B. Quartet Swapping.cpp create mode 100644 Codeforces Round 1081 (Div. 2)/A. String Rotation Game.cpp create mode 100644 Codeforces Round 1081 (Div. 2)/B. Flipping Binary String.cpp create mode 100644 Codeforces Round 1081 (Div. 2)/C. All-in-one Gun.cpp create mode 100644 Codeforces Round 1086 (Div. 2)/A. Bingo Candies.cpp create mode 100644 Codeforces Round 1086 (Div. 2)/B. Cyclists.cpp create mode 100644 Codeforces Round 1086 (Div. 2)/C. Stamina and Tasks.cpp create mode 100644 Codeforces Round 1086 (Div. 2)/D1. Tree Orientation (Easy Version).cpp create mode 100644 Codeforces Round 1087 (Div. 2)/A. Flip Flops.cpp create mode 100644 Codeforces Round 1087 (Div. 2)/B. Array.cpp create mode 100644 Codeforces Round 1087 (Div. 2)/C. Find the Zero.cpp create mode 100644 Codeforces Round 867 (Div. 3)/F. Gardening Friends.cpp create mode 100644 Codeforces Round 913 (Div. 3)/D. Jumping Through Segments.cpp create mode 100644 Codeforces Round 943 (Div. 3)/G1. Division + LCP (easy version).cpp create mode 100644 Codeforces Round 964 (Div. 4)/F. Expected Median.cpp create mode 100644 Teza Round 1 (Codeforces Round 1015, Div. 1 + Div. 2)/C. You Soared Afar With Grace.cpp diff --git a/2018 ACM-ICPC, Universidad Nacional de Colombia Programming Contest/D. Divorce.cpp b/2018 ACM-ICPC, Universidad Nacional de Colombia Programming Contest/D. Divorce.cpp new file mode 100644 index 0000000..b8078c5 --- /dev/null +++ b/2018 ACM-ICPC, Universidad Nacional de Colombia Programming Contest/D. Divorce.cpp @@ -0,0 +1,174 @@ +/* Problem URL: https://codeforces.com/gym/101845/problem/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + +struct pt { + ll x, y; + + pt() = default; + pt(ll x, ll y): x(x), y(y) {} + + friend istream &operator >> (istream &is, pt &a) { + is >> a.x >> a.y; + return is; + } + + ll operator * (pt b) { + return x * b.x + y * b.y; + } + + ll operator ^ (pt b) { + return x * b.y - y * b.x; + } + + pt operator - (pt b) { + return pt(x - b.x, y - b.y); + } +}; + +ll sarea2(pt a, pt b, pt c) +{ + return (b - a) ^ (c - a); +} + +int ccw(pt a, pt b, pt c) +{ + ll r = sarea2(a, b, c); + return (r > 0) - (r < 0); +} + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n, m; + cin >> n >> m; + + V pol(n); + cin >> pol; + + vl pref(n * 3); + nrep(i, 1, n * 3) { + pref[i] = pref[i - 1] + sarea2(pt(0, 0), pol[(i - 1) % n], pol[i % n]); + } + + ll ans = 0; + while (m--) { + int l, r; + cin >> l >> r; + l--, r--; + + if (l > r) { + swap(l, r); + } + + if ((l + 1) % n == r) { + continue; + } + + int l2 = r; + int r2 = l + n; + + ll ar1 = abs(sarea2(pt(0, 0), pol[r], pol[l]) + pref[r] - pref[l]); + ll ar2 = abs(pref[r2] - pref[l2] + sarea2(pt(0, 0), pol[r2 % n], pol[l2 % n])); + + rmax(ans, min(ar1, ar2)); + } + + cout << ans / 2.0 << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/2018 ACM-ICPC, Universidad Nacional de Colombia Programming Contest/F. UN Finals.cpp b/2018 ACM-ICPC, Universidad Nacional de Colombia Programming Contest/F. UN Finals.cpp new file mode 100644 index 0000000..79d9d27 --- /dev/null +++ b/2018 ACM-ICPC, Universidad Nacional de Colombia Programming Contest/F. UN Finals.cpp @@ -0,0 +1,229 @@ +/* Problem URL: https://codeforces.com/gym/101845/problem/F */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n; + cin >> n; + + auto count = [&](string &a, int c[26]) { + repv(i, a) { + c[i - 'A']++; + } + }; + + V> edges; + vvi graph(n + 28); + int sz = graph.size(); + + int s = 0; + int t = n + 27; + + auto add_edge = [&](int u, int v, ll c) { + graph[u].push_back(edges.size()); + edges.emplace_back(v, c, 0); + graph[v].push_back(edges.size()); + edges.emplace_back(u, 0, 0); + }; + + vl pos(n); + rep(i, n) { + string a, b, c; + cin >> a >> b >> c; + + add_edge(s, i + 1, 1); + + int cc[26] = {0}; + + count(a, cc); + count(b, cc); + count(c, cc); + + int ma = *max_element(cc, cc + 26); + + rep(j, 26) { + if (cc[j] == ma) { + add_edge(i + 1, j + n + 1, 1); + } + } + } + + int k; + cin >> k; + + rep(i, 26) { + add_edge(i + n + 1, t, k); + } + + vi lvl(sz, oo); + vi p(sz); + + auto bfs = [&]() { + lvl[s] = 0; + queue q; + q.push(s); + + while (!q.empty()) { + auto i = q.front(); + q.pop(); + + repv(id, graph[i]) { + auto [j, l, c] = edges[id]; + if (l != c && lvl[j] > lvl[i] + 1) { + lvl[j] = lvl[i] + 1; + q.push(j); + } + } + } + + return lvl[t] != oo; + }; + + function dfs = [&](int i, ll f) { + if (f == 0) { + return 0LL; + } + + if (i == t) { + return f; + } + + for (int &d = p[i]; d < graph[i].size(); d++) { + int id = graph[i][d]; + auto &[j, l, c] = edges[id]; + + if (l == c || lvl[j] <= lvl[i]) { + continue; + } + + ll fr = dfs(j, min(f, l - c)); + if (fr == 0) { + continue; + } + + c += fr; + auto &[_, __, c2] = edges[id^1]; + c2 -= fr; + + return fr; + } + + return 0LL; + }; + + ll ans = 0; + + while (1) { + fill(all(lvl), oo); + if (!bfs()) { + break; + } + + fill(all(p), 0); + ll add = 0; + while ((add = dfs(s, OO)) != 0) { + ans += add; + } + }; + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/2018 ACM-ICPC, Universidad Nacional de Colombia Programming Contest/I. Intense Bit Wheel.cpp b/2018 ACM-ICPC, Universidad Nacional de Colombia Programming Contest/I. Intense Bit Wheel.cpp new file mode 100644 index 0000000..ab08d10 --- /dev/null +++ b/2018 ACM-ICPC, Universidad Nacional de Colombia Programming Contest/I. Intense Bit Wheel.cpp @@ -0,0 +1,128 @@ +/* Problem URL: https://codeforces.com/gym/101845/problem/I */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n, m; + cin >> n >> m; + + while (m--) { + ll num, k; + cin >> num >> k; + + k %= n; + + ll ans = 0; + int cur = 0; + int lim = n - k; + nrep(i, lim, n) { + ans |= ((num >> i) & 1) << cur; + cur++; + } + rep(i, lim) { + ans |= ((num >> i) & 1) << cur; + cur++; + } + + cout << ans << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/2024-2025 ICPC, NERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/A. Adrenaline Rush.cpp b/2024-2025 ICPC, NERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/A. Adrenaline Rush.cpp new file mode 100644 index 0000000..ae8e5d3 --- /dev/null +++ b/2024-2025 ICPC, NERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/A. Adrenaline Rush.cpp @@ -0,0 +1,157 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2052/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n; + cin >> n; + vi p(n); + cin >> p; + reverse(all(p)); + + V> ans; + V us(n + 1); + + // rep(i, n) { + // int c = p[i]; + // for (int j = c - 1; j > 0; j--) { + // if (us[j - 1]) { + // continue; + // } + // + // ans.emplace_back(c, j); + // } + // + // for (int j = c + 1; j <= n; j++) { + // if (us[j - 1]) { + // continue; + // } + // + // ans.emplace_back(j, c); + // ans.emplace_back(c, j); + // } + // } + + rep(i, n) { + int c = p[i]; + us[c] = true; + + for (int j = c - 1; j > 0; j--) { + if (us[j]) { + continue; + } + ans.emplace_back(c, j); + } + + for (int j = 1; j <= n; j++) { + if (us[j]) { + continue; + } + ans.emplace_back(j, c); + } + } + + cout << ans.size() << '\n'; + repv(i, ans) { + cout << i.first << ' ' << i.second << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1024 (Div. 1)/B. Quartet Swapping.cpp b/Codeforces Round 1024 (Div. 1)/B. Quartet Swapping.cpp new file mode 100644 index 0000000..12c43ff --- /dev/null +++ b/Codeforces Round 1024 (Div. 1)/B. Quartet Swapping.cpp @@ -0,0 +1,141 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2101/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vi p(n); + cin >> p; + + vvi od(2); + + auto cnt = [&](vi &p) { + int ans = 0; + ordered_set ord; + rep(i, p.size()) { + ans += ord.size() - ord.order_of_key(p[i]); + ord.insert(p[i]); + } + return ans & 1; + }; + + rep(i, n) { + od[i & 1].push_back(p[i]); + } + + bool pos = cnt(od[0]) != cnt(od[1]); + + sort(all(od[0]), greater<>()); + sort(all(od[1]), greater<>()); + + rep(i, n) { + p[i] = od[i & 1].back(); + od[i & 1].pop_back(); + } + + if (pos) { + swap(p[n - 1], p[n - 3]); + } + + cout << p; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1081 (Div. 2)/A. String Rotation Game.cpp b/Codeforces Round 1081 (Div. 2)/A. String Rotation Game.cpp new file mode 100644 index 0000000..59e67e4 --- /dev/null +++ b/Codeforces Round 1081 (Div. 2)/A. String Rotation Game.cpp @@ -0,0 +1,118 @@ +/* Problem URL: https://codeforces.com/contest/2192/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + string a; + cin >> a; + + int ans = 1; + int rep = 0; + nrep(i, 1, n) { + ans += a[i] != a[i - 1]; + rep |= a[i] == a[i - 1]; + } + + cout << ans + rep - (rep && n > 1 && a.back() == a.front()) << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1081 (Div. 2)/B. Flipping Binary String.cpp b/Codeforces Round 1081 (Div. 2)/B. Flipping Binary String.cpp new file mode 100644 index 0000000..b442283 --- /dev/null +++ b/Codeforces Round 1081 (Div. 2)/B. Flipping Binary String.cpp @@ -0,0 +1,129 @@ +/* Problem URL: https://codeforces.com/contest/2192/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vvi pos(2); + rep(i, n) { + char b; + cin >> b; + pos[b == '1'].push_back(i + 1); + } + + int now = 0; + if (~pos[0].size() & 1) { + if (pos[1].size() & 1) { + cout << "-1\n"; + return; + } + now = 1; + } + + cout << pos[now].size() << '\n'; + repv(i, pos[now]) { + cout << i << ' '; + } + cout << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1081 (Div. 2)/C. All-in-one Gun.cpp b/Codeforces Round 1081 (Div. 2)/C. All-in-one Gun.cpp new file mode 100644 index 0000000..1940a2c --- /dev/null +++ b/Codeforces Round 1081 (Div. 2)/C. All-in-one Gun.cpp @@ -0,0 +1,160 @@ +/* Problem URL: https://codeforces.com/contest/2192/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + ll h, k; + cin >> n >> h >> k; + + vl a(n); + ll sum = 0; + repv(i, a) { + cin >> i; + sum += i; + } + + ll ans = h / sum * (k + n); + h %= sum; + + if (h == 0) { + cout << ans - k << '\n'; + return; + } + + int lim = 0; + vl pref(n + 1); + rep(i, n) { + pref[i + 1] = pref[i] + a[i]; + } + + while (h - pref[lim] > 0) { + lim++; + } + + ll ma = 0; + nrep(j, lim - 1, n) { + rmax(ma, a[j]); + } + + int cur = oo; + rep(i, lim) { + int low = i; + int high = lim - 1; + int ans = lim; + while (low <= high) { + int mid = (low + high) >> 1; + + if (pref[mid + 1] - a[i] + ma >= h) { + ans = mid + 1; + high = mid - 1; + continue; + } + + low = mid + 1; + } + + rmin(cur, ans); + } + + cout << ans + cur << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1086 (Div. 2)/A. Bingo Candies.cpp b/Codeforces Round 1086 (Div. 2)/A. Bingo Candies.cpp new file mode 100644 index 0000000..23178b6 --- /dev/null +++ b/Codeforces Round 1086 (Div. 2)/A. Bingo Candies.cpp @@ -0,0 +1,132 @@ +/* Problem URL: https://codeforces.com/contest/2208/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + vi c(n * n); + rep(i, n) { + rep(j, n) { + int a; + cin >> a; + c[a - 1]++; + } + } + + if (n == 1) { + cout << "NO\n"; + return; + } + + int sz = n * n - n; + + rep(i, n * n) { + if (c[i] > sz) { + cout << "NO\n"; + return; + } + } + + cout << "YES\n"; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1086 (Div. 2)/B. Cyclists.cpp b/Codeforces Round 1086 (Div. 2)/B. Cyclists.cpp new file mode 100644 index 0000000..988643f --- /dev/null +++ b/Codeforces Round 1086 (Div. 2)/B. Cyclists.cpp @@ -0,0 +1,150 @@ +/* Problem URL: https://codeforces.com/contest/2208/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, k, p, m; + cin >> n >> k >> p >> m; + + list> a; + rep(i, n) { + int c; + cin >> c; + a.emplace_back(c, i == p - 1); + } + + int ans = 0; + while (1) { + auto choice = a.begin(); + auto itr = next(choice); + + nrep(i, 1, k) { + if (choice->second) { + break; + } + + if (itr->second) { + choice = itr; + itr++; + continue; + } + + if (itr->first < choice->first) { + choice = itr; + } + + itr++; + } + + if (choice->first > m) { + break; + } + + auto now = *choice; + a.erase(choice); + ans += now.second; + m -= now.first; + a.emplace_back(now); + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1086 (Div. 2)/C. Stamina and Tasks.cpp b/Codeforces Round 1086 (Div. 2)/C. Stamina and Tasks.cpp new file mode 100644 index 0000000..5e4cebb --- /dev/null +++ b/Codeforces Round 1086 (Div. 2)/C. Stamina and Tasks.cpp @@ -0,0 +1,123 @@ +/* Problem URL: https://codeforces.com/contest/2208/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + V> a(n); + repv(i, a) { + cin >> i.first >> i.second; + } + + reverse(all(a)); + + long double ans = 0; + rep(i, n) { + auto [c, p] = a[i]; + + rmax(ans, ans * (1 - p / 100.0L) + c); + } + + cout << fixed << setprecision(10) << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1086 (Div. 2)/D1. Tree Orientation (Easy Version).cpp b/Codeforces Round 1086 (Div. 2)/D1. Tree Orientation (Easy Version).cpp new file mode 100644 index 0000000..ba143b3 --- /dev/null +++ b/Codeforces Round 1086 (Div. 2)/D1. Tree Orientation (Easy Version).cpp @@ -0,0 +1,258 @@ +/* Problem URL: https://codeforces.com/contest/2208/problem/D1 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + V a(n); + cin >> a; + + vvi graph(n); + vvi mult(n); + vvi inv(n); + + rep(i, n) { + if (a[i][i] != '1') { + cout << "No\n"; + return; + } + + rep(j, n) { + if (i == j) { + continue; + } + + if (a[i][j] == '0') { + continue; + } + + rep(k, n) { + if (a[i][k] == '0' && a[j][k] == '1') { + cout << "No\n"; + return; + } + } + + graph[i].push_back(j); + inv[j].push_back(i); + mult[i].push_back(j); + mult[j].push_back(i); + } + } + + vi st; + V frame(n); + V vis(n); + + function dfs = [&](int i) { + vis[i] = true; + frame[i] = true; + + repv(j, graph[i]) { + if (frame[j]) { + return false; + } + + if (vis[j]) { + continue; + } + + if (!dfs(j)) { + return false; + } + } + + st.push_back(i); + + frame[i] = false; + return true; + }; + + function dfs2 = [&](int i) { + vis[i] = true; + repv(j, mult[i]) { + if (vis[j]) { + continue; + } + + dfs2(j); + } + }; + + rep(i, n) { + if (vis[i]) { + continue; + } + + if (!dfs(i)) { + cout << "No\n"; + return; + } + } + + fill(all(vis), false); + dfs2(0); + + + rep(i, n) { + if (!vis[i]) { + cout << "No\n"; + return; + } + } + + V> rem(n, V(n)); + + vvi act(n); + V> ans; + + repv(i, st) { + repv(j, graph[i]) { + if (rem[i][j]) { + continue; + } + + act[i].push_back(j); + act[j].push_back(i); + ans.emplace_back(i + 1, j + 1); + rem[i][j] = true; + } + + repv(j, inv[i]) { + rep(k, n) { + rem[j][k] = rem[j][k] || rem[i][k]; + } + } + } + fill(all(vis), false); + + function test = [&](int i, int p) { + vis[i] = true; + repv(j, act[i]) { + if (j == p) { + continue; + } + + if (vis[j]) { + return false; + } + + if (!test(j, i)) { + return false; + } + } + + return true; + }; + + if (!test(0, 0)) { + cout << "No\n"; + return; + } + + cout << "Yes\n"; + repv(i, ans) { + cout << i.first << ' ' << i.second << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1087 (Div. 2)/A. Flip Flops.cpp b/Codeforces Round 1087 (Div. 2)/A. Flip Flops.cpp new file mode 100644 index 0000000..e794c53 --- /dev/null +++ b/Codeforces Round 1087 (Div. 2)/A. Flip Flops.cpp @@ -0,0 +1,126 @@ +/* Problem URL: https://codeforces.com/contest/2209/problem/A */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + ll c; + ll k; + cin >> n >> c >> k; + + vl a(n); + cin >> a; + + sortv(a); + repv(i, a) { + if (i > c) { + break; + } + + ll diff = c - i; + + c += i + min(k, diff); + k -= min(k, diff); + } + + cout << c << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1087 (Div. 2)/B. Array.cpp b/Codeforces Round 1087 (Div. 2)/B. Array.cpp new file mode 100644 index 0000000..dbb8430 --- /dev/null +++ b/Codeforces Round 1087 (Div. 2)/B. Array.cpp @@ -0,0 +1,121 @@ +/* Problem URL: https://codeforces.com/contest/2209/problem/B */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vl a(n); + cin >> a; + + rep(i, n) { + int c1 = 0; + int c2 = 0; + nrep(j, i + 1, n) { + c1 += a[j] > a[i]; + c2 += a[j] < a[i]; + } + + cout << max(c1, c2) << ' '; + } + cout << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1087 (Div. 2)/C. Find the Zero.cpp b/Codeforces Round 1087 (Div. 2)/C. Find the Zero.cpp new file mode 100644 index 0000000..e8251d8 --- /dev/null +++ b/Codeforces Round 1087 (Div. 2)/C. Find the Zero.cpp @@ -0,0 +1,145 @@ +/* Problem URL: https://codeforces.com/contest/2209/problem/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + auto ask = [&](int i, int j) { + cout << "? " << i << ' ' << j << endl; + int ans; + cin >> ans; + return ans; + }; + + auto answer = [&](int i) { + cout << "! " << i << endl; + }; + + for (int i = 1; i <= n * 2 - 2; i += 2) { + int ans = ask(i, i + 1); + if (ans == 1) { + answer(i); + return; + } + } + + int p1 = n * 2 - 3; + int p2 = n * 2 - 2; + int p3 = n * 2 - 1; + int p4 = n * 2; + + int ans = ask(p1, p3); + if (ans == 1) { + answer(p1); + return; + } + + ans = ask(p2, p3); + if (ans == 1) { + answer(p2); + return; + } + + answer(p4); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 867 (Div. 3)/F. Gardening Friends.cpp b/Codeforces Round 867 (Div. 3)/F. Gardening Friends.cpp new file mode 100644 index 0000000..b65eb69 --- /dev/null +++ b/Codeforces Round 867 (Div. 3)/F. Gardening Friends.cpp @@ -0,0 +1,174 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1822/F */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + ll k, c; + cin >> k >> c; + + vvi graph(n); + rep(i, n - 1) { + int a, b; + cin >> a >> b; + a--, b--; + graph[a].push_back(b); + graph[b].push_back(a); + } + + vi dis(n); + vi dp(n); + dis[0] = -1; + + function dfs = [&](int i, int p) { + dis[i] = dis[p] + 1; + repv(j, graph[i]) { + if (j == p) { + continue; + } + + rmax(dp[i], dfs(j, i) + 1); + } + + return dp[i]; + }; + + dfs(0, 0); + + function reroot = [&](int i, int p) { + ll c1 = -1; + ll c2 = -1; + repv(j, graph[i]) { + if (dp[j] + 1 > c1) { + c2 = c1; + c1 = dp[j] + 1; + continue; + } + + if (dp[j] + 1 > c2) { + c2 = dp[j] + 1; + } + } + + ll ans = c1; + + repv(j, graph[i]) { + if (j == p) { + continue; + } + dp[i] = dp[j] + 1 == ans ? c2 : c1; + reroot(j, i); + } + + dp[i] = ans; + }; + + reroot(0, 0); + + ll ans = 0; + rep(i, n) { + rmax(ans, dp[i] * k - dis[i] * c); + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 913 (Div. 3)/D. Jumping Through Segments.cpp b/Codeforces Round 913 (Div. 3)/D. Jumping Through Segments.cpp new file mode 100644 index 0000000..95cded0 --- /dev/null +++ b/Codeforces Round 913 (Div. 3)/D. Jumping Through Segments.cpp @@ -0,0 +1,154 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1907/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + V> a(n); + repv(i, a) { + cin >> i.first >> i.second; + } + + ll low = 0; + ll high = 1e9; + ll ans = 1e9; + while (low <= high) { + ll mid = (low + high) >> 1; + + ll l = 0; + ll r = 0; + + bool pos = true; + + repv(i, a) { + ll l1 = l - mid; + ll l2 = l + mid; + ll r1 = r - mid; + ll r2 = r + mid; + + rmax(l1, i.first); + rmin(l2, i.second); + rmax(r1, i.first); + rmin(r2, i.second); + + l = min(l1, r1); + r = max(l2, r2); + + if (l > r) { + pos = false; + break; + } + } + + if (pos) { + ans = mid; + high = mid - 1; + continue; + } + + low = mid + 1; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 943 (Div. 3)/G1. Division + LCP (easy version).cpp b/Codeforces Round 943 (Div. 3)/G1. Division + LCP (easy version).cpp new file mode 100644 index 0000000..cca39cb --- /dev/null +++ b/Codeforces Round 943 (Div. 3)/G1. Division + LCP (easy version).cpp @@ -0,0 +1,163 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1968/G1 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +struct hash_string { + static vl p; + static ll m; + static ll b; + vl hash; + + hash_string(string &a) { + while (p.size() <= a.size()) { + p.push_back(((__int128)p.back() * b) % m); + } + + hash.resize(a.size() + 1); + rep(i, a.size()) { + hash[i + 1] = ((__int128)hash[i] * b + a[i]) % m; + } + } + + ll gethash(int l, int r) { + return ((hash[r + 1] - (__int128)hash[l] * p[r - l + 1]) % m + m) % m; + } +}; + +vl hash_string::p = {1}; +ll hash_string:: m = (1LL << 61) - 1; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll hash_string:: b = uniform_int_distribution<>()(rng); + +void solve() +{ + int n, l, r; + cin >> n >> l >> r; + string a; + cin >> a; + + hash_string ah(a); + + int low = 1; + int high = n; + int ans = 0; + while (low <= high) { + int mid = (low + high) >> 1; + + int c = 1; + for (int i = mid; i + mid - 1 < n; i++) { + if (ah.gethash(0, mid - 1) == ah.gethash(i, i + mid - 1)) { + c++; + i += mid - 1; + } + } + + if (c >= l) { + ans = mid; + low = mid + 1; + continue; + } + + high = mid - 1; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 964 (Div. 4)/F. Expected Median.cpp b/Codeforces Round 964 (Div. 4)/F. Expected Median.cpp new file mode 100644 index 0000000..2591755 --- /dev/null +++ b/Codeforces Round 964 (Div. 4)/F. Expected Median.cpp @@ -0,0 +1,149 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1999/F */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + +const ll mod = 1e9 + 7; + +constexpr int MAXN = 2e5 + 10; +ll fact[MAXN]; +ll inv[MAXN]; +ll invf[MAXN]; + +void pre() +{ + fact[0] = 1; + nrep(i, 1, MAXN) { + fact[i] = fact[i - 1] * i % mod; + } + + inv[1] = 1; + nrep(i, 2, MAXN) { + inv[i] = (mod - mod / i) * inv[mod % i] % mod; + } + + invf[0] = 1; + nrep(i, 1, MAXN) { + invf[i] = invf[i - 1] * inv[i] % mod; + } +} + +ll comb(ll n, ll k) { + return fact[n] * invf[k] % mod * invf[n - k] % mod; +} + +#define TEST 1 + +void solve() +{ + int n, k; + cin >> n >> k; + vi a(n); + cin >> a; + + ll c0 = count(all(a), 0); + ll c1 = count(all(a), 1); + + ll ans = 0; + nrep(i, (k + 1) >> 1, k + 1) { + ll o = i; + ll z = k - i; + + if (c0 < z || c1 < o) { + continue; + } + + (ans += comb(c1, o) * comb(c0, z) % mod) %= mod; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Teza Round 1 (Codeforces Round 1015, Div. 1 + Div. 2)/C. You Soared Afar With Grace.cpp b/Teza Round 1 (Codeforces Round 1015, Div. 1 + Div. 2)/C. You Soared Afar With Grace.cpp new file mode 100644 index 0000000..b56372f --- /dev/null +++ b/Teza Round 1 (Codeforces Round 1015, Div. 1 + Div. 2)/C. You Soared Afar With Grace.cpp @@ -0,0 +1,163 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2084/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vi a(n); + vi b(n); + cin >> a >> b; + + int c = 0; + int p; + + map, int> s; + rep(i, n) { + s[{a[i], b[i]}] = i; + if (a[i] == b[i]) { + c++; + p = i; + } + } + + if ((c > 0 && (~n & 1)) || (c != 1 && (n & 1))) { + cout << "-1\n"; + return; + } + + rep(i, n) { + if (!s.count({b[i], a[i]})) { + cout << "-1\n"; + return; + } + } + + V> ans; + + if (c && p != (n >> 1)) { + ans.emplace_back(p + 1, (n >> 1) + 1); + swap(a[p], a[n >> 1]); + swap(b[p], b[n >> 1]); + swap(s[{a[p], b[p]}], s[{a[n >> 1], b[n >> 1]}]); + } + + rep(i, n >> 1) { + int rev = n - i - 1; + auto itr = s.find({b[i], a[i]}); + + if (itr->second == rev) { + continue; + } + + ans.emplace_back(itr->second + 1, rev + 1); + auto itr2 = s.find({a[rev], b[rev]}); + swap(a[rev], a[itr->second]); + swap(b[rev], b[itr->second]); + swap(itr->second, itr2->second); + } + + cout << ans.size() << '\n'; + repv(i, ans) { + cout << i.first << ' ' << i.second << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/problemlist.md b/problemlist.md index 98c1647..edb4e2f 100644 --- a/problemlist.md +++ b/problemlist.md @@ -47,6 +47,10 @@ A little collection of interesting problems that I randomly decided to do. Interesting reroot problem, there's not much for me to say about it. +- [F. Gardening Friends](https://codeforces.com/problemset/problem/1822/F) + + Very standard reroot problem, but interesting as an exercise. + ## DP - [D. World is Mine](https://codeforces.com/problemset/problem/1987/D) @@ -133,6 +137,10 @@ A little collection of interesting problems that I randomly decided to do. it's interesting to think about regardless, can also be solved with a binary search, but it's not required. +- [F. Expected Median](https://codeforces.com/problemset/problem/1999/F) + + Kinda introductory combinatory problem, but interesting as an exercise. + ## DSU - [https://codeforces.com/problemset/problem/1985/H1](https://codeforces.com/problemset/problem/1985/H1) @@ -151,3 +159,9 @@ A little collection of interesting problems that I randomly decided to do. Interesting problem to think about, good introduction to divide and conquer in my opinion and can be used as a bridge to segment trees. + +## String + +- [G1. Division + LCP (easy version)](https://codeforces.com/problemset/problem/1968/G1) + + Can be solved in a lot of ways, but I find hashing with binary search simpler.