diff --git a/.gitignore b/.gitignore index d45eb1b..c0dee2f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ !*.cpp !TODO.md !problemlist.md +*sync-conflict* diff --git a/2019 China Collegiate Programming Contest Qinhuangdao Onsite/A. Angle Beats.cpp b/2019 China Collegiate Programming Contest Qinhuangdao Onsite/A. Angle Beats.cpp new file mode 100644 index 0000000..6e47492 --- /dev/null +++ b/2019 China Collegiate Programming Contest Qinhuangdao Onsite/A. Angle Beats.cpp @@ -0,0 +1,302 @@ +/* Problem URL: https://codeforces.com/gym/102361/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; + +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.y - y * b.x; + } + + ll operator * (pt b) { + return x * b.x + y * b.y; + } + + pt operator - (pt b) { + return pt(x - b.x, y - b.y); + } +}; + +int ccw(pt a, pt b, pt c) +{ + ll r = (b - a) ^ (c - a); + return (r > 0) - (r < 0); +} + +ll cdot(pt a, pt b, pt c) +{ + return (b - a) * (c - a); +} + +int quad(pt a) +{ + if (a.x == 0 && a.y > 0) { + return 0; + } + if (a.x > 0 && a.y == 0) { + return 1; + } + if (a.x == 0 && a.y < 0) { + return 2; + } + if (a.x < 0 && a.y == 0) { + return 3; + } + static const int q[][2] = {{0, 1}, {3, 2}}; + return q[a.x < 0][a.y < 0]; +} + +bool polar_cmp(pt a, pt b) +{ + if (quad(a) != quad(b)) { + return quad(a) != quad(b); + } + return ccw(pt(0, 0), a, b) < 0; +} + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n, q; + cin >> n >> q; + V pts(n + q); + cin >> pts; + + vi p(n + q); + iota(all(p), 0); + + auto cmpr = [&](vi &p, int i) { + if (p.empty()) { + return vector>(); + } + sort(all(p), [&](int j, int k){return polar_cmp(pts[j] - pts[i], pts[k] - pts[i]);}); + V> v; + int c = 1; + nrep(j, 1, p.size()) { + if (ccw(pts[i], pts[p[j]], pts[p[j - 1]]) == 0) { + c++; + continue; + } + v.emplace_back(p[j - 1], c); + c = 1; + } + v.emplace_back(p.back(), c); + return v; + }; + + vl ans(q); + + rep(i, n) { + vvi qp(4); + nrep(j, n, n + q) { + qp[quad(pts[j] - pts[i])].push_back(j); + } + + vvi np(4); + rep(j, n) { + if (i == j) { + continue; + } + np[quad(pts[j] - pts[i])].push_back(j); + } + + V>> cp = {cmpr(np[0], i), cmpr(np[1], i), cmpr(np[2], i), cmpr(np[3], i)}; + auto cmp = [&](int j, int k) { + return polar_cmp(pts[j] - pts[i], pts[k] - pts[i]); + }; + sort(all(qp[0]), cmp); + sort(all(qp[1]), cmp); + sort(all(qp[2]), cmp); + sort(all(qp[3]), cmp); + + auto getcw = [&](vi &p, V> &c) { + int j = 0; + int k = 0; + while (j < p.size() && k < c.size()) { + ll dot = cdot(pts[i], pts[p[j]], pts[c[k].first]); + if (dot == 0) { + ans[p[j] - n] += c[k].second; + j++; + continue; + } + + if (dot > 0) { + k++; + continue; + } + + j++; + } + }; + + auto getccw = [&](vi &p, V> &c) { + int j = (int)p.size() - 1; + int k = (int)c.size() - 1; + while (j >= 0 && k >= 0) { + ll dot = cdot(pts[i], pts[p[j]], pts[c[k].first]); + if (dot == 0) { + ans[p[j] - n] += c[k].second; + j--; + continue; + } + + if (dot > 0) { + k--; + continue; + } + + j--; + } + }; + + rep(j, 4) { + getcw(qp[j], cp[(j + 1) % 4]); + getccw(qp[j], cp[((j - 1) % 4 + 4) % 4]); + } + } + + nrep(i, n, n + q) { + vvi np(4); + rep(j, n) { + np[quad(pts[j] - pts[i])].push_back(j); + } + + V>> cp = {cmpr(np[0], i), cmpr(np[1], i), cmpr(np[2], i), cmpr(np[3], i)}; + + auto getcw = [&](V> &p1, V> &p2) { + int j = 0; + int k = 0; + + while (j < p1.size() && k < p2.size()) { + ll dot = cdot(pts[i], pts[p1[j].first], pts[p2[k].first]); + + if (dot == 0) { + ans[i - n] += p1[j].second * p2[k].second; + j++; + k++; + continue; + } + + if (dot < 0) { + j++; + continue; + } + + k++; + } + }; + + rep(j, 4) { + getcw(cp[j], cp[(j + 1) % 4]); + } + } + + repv(i, ans) { + cout << i << '\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, Southern and Volga Russian Regional Contest (Unrated, Online Mirror, ICPC Rules, Preferably Teams)/A. Bonus Project.cpp b/2024-2025 ICPC, NERC, Southern and Volga Russian Regional Contest (Unrated, Online Mirror, ICPC Rules, Preferably Teams)/A. Bonus Project.cpp new file mode 100644 index 0000000..a26e96f --- /dev/null +++ b/2024-2025 ICPC, NERC, Southern and Volga Russian Regional Contest (Unrated, Online Mirror, ICPC Rules, Preferably Teams)/A. Bonus Project.cpp @@ -0,0 +1,140 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2038/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, k; + cin >> n >> k; + + vl a(n); + vl b(n); + cin >> a >> b; + + ll total = 0; + vl ans(n); + rep(i, n) { + ans[i] = a[i] / b[i]; + total += ans[i]; + } + + if (total < k) { + rep(i, n) { + cout << "0" << " \n"[i == n - 1]; + } + return; + } + + int inc = total - k; + + rep(i, n) { + if (ans[i] < inc) { + inc -= ans[i]; + ans[i] = 0; + continue; + } + + ans[i] -= inc; + break; + } + + cout << ans; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/2025-2026 ICPC, NERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/M. Medical Parity.cpp b/2025-2026 ICPC, NERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/M. Medical Parity.cpp new file mode 100644 index 0000000..f3e868a --- /dev/null +++ b/2025-2026 ICPC, NERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/M. Medical Parity.cpp @@ -0,0 +1,119 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2181/M */ + +#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() +{ + string a, b; + cin >> a >> b; + int n = a.size(); + + vvi dp(n, vi(2)); + dp[n - 1][0] = b[n - 1] != '0'; + dp[n - 1][1] = b[n - 1] != '1'; + + for (int i = n - 2; i >= 0; i--) { + dp[i][0] = (b[i] != '0') + min(dp[i + 1][1] + (a[i + 1] != '1'), dp[i + 1][0] + (a[i + 1] != '0')); + dp[i][1] = (b[i] != '1') + min(dp[i + 1][0] + (a[i + 1] != '1'), dp[i + 1][1] + (a[i + 1] != '0')); + } + + cout << min(dp[0][0] + (a[0] != '0'), dp[0][1] + (a[0] != '1')) << '\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 Global Round 26/C2. Magnitude (Hard Version).cpp b/Codeforces Global Round 26/C2. Magnitude (Hard Version).cpp new file mode 100644 index 0000000..fec4263 --- /dev/null +++ b/Codeforces Global Round 26/C2. Magnitude (Hard Version).cpp @@ -0,0 +1,144 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1984/C2 */ + +#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; + + const ll mod = 998244353; + + map c1; + map c2; + + c1[0] = 1; + rep(i, n) { + if (c1.size() == 1) { + auto itr = c1.begin(); + c2[itr->first + a[i]] += itr->second; + c2[itr->first + a[i]] %= mod; + c2[abs(itr->first + a[i])] += itr->second; + c2[abs(itr->first + a[i])] %= mod; + swap(c1, c2); + c2.clear(); + continue; + } + + auto itr1 = c1.begin(); + auto itr2 = prev(c1.end()); + + c2[itr1->first + a[i]] += itr1->second; + c2[itr1->first + a[i]] %= mod; + c2[abs(itr1->first + a[i])] += itr1->second; + c2[abs(itr1->first + a[i])] %= mod; + c2[itr2->first + a[i]] += itr2->second; + c2[itr2->first + a[i]] %= mod; + c2[abs(itr2->first + a[i])] += itr2->second; + c2[abs(itr2->first + a[i])] %= mod; + swap(c1, c2); + c2.clear(); + } + + cout << prev(c1.end())->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 1007 (Div. 2)/C. Trapmigiano Reggiano.cpp b/Codeforces Round 1007 (Div. 2)/C. Trapmigiano Reggiano.cpp new file mode 100644 index 0000000..9a8a25e --- /dev/null +++ b/Codeforces Round 1007 (Div. 2)/C. Trapmigiano Reggiano.cpp @@ -0,0 +1,172 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2071/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, st, en; + cin >> n >> st >> en; + st--, en--; + + 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 ne(n, -1); + function find_p = [&](int i, int p) { + if (i == en) { + return true; + } + + repv(j, graph[i]) { + if (j == p) { + continue; + } + + if (find_p(j, i)) { + ne[i] = j; + return true; + } + } + + return false; + }; + + find_p(st, -1); + + vi ans; + + function rem = [&](int i, int p) { + repv(j, graph[i]) { + if (j == p) { + continue; + } + + rem(j, i); + } + + ans.push_back(i + 1); + }; + + function getans = [&](int i, int p) { + repv(j, graph[i]) { + if (j == p || j == ne[i]) { + continue; + } + + rem(j, i); + } + + ans.push_back(i + 1); + if (i != en) { + getans(ne[i], i); + } + }; + + getans(st, -1); + + cout << ans; +} + +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 1026 (Div. 2)/C. Racing.cpp b/Codeforces Round 1026 (Div. 2)/C. Racing.cpp new file mode 100644 index 0000000..1970a1a --- /dev/null +++ b/Codeforces Round 1026 (Div. 2)/C. Racing.cpp @@ -0,0 +1,151 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2110/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 d(n); + cin >> d; + + V> v(n); + repv(i, v) { + cin >> i.first >> i.second; + } + + V> p(n + 1); + nrep(i, 1, n + 1) { + int a = d[i - 1]; + if (a == -1) { + p[i] = {p[i - 1].first, p[i - 1].second + 1}; + rmin(p[i].second, v[i - 1].second); + rmax(p[i].first, v[i - 1].first); + if (p[i].second < p[i].first) { + cout << "-1\n"; + return; + } + continue; + } + + p[i] = {p[i - 1].first + a, p[i - 1].second + a}; + rmin(p[i].second, v[i - 1].second); + rmax(p[i].first, v[i - 1].first); + if (p[i].second < p[i].first) { + cout << "-1\n"; + return; + } + } + + int cur = p.back().first; + for (int i = n - 1; i >= 0; i--) { + if (d[i] == -1) { + d[i] = cur > p[i].first; + cur -= cur > p[i].first; + continue; + } + + cur -= d[i]; + } + + cout << d; +} + +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 1065 (Div. 3)/D. Rae Taylor and Trees (easy version).cpp b/Codeforces Round 1065 (Div. 3)/D. Rae Taylor and Trees (easy version).cpp new file mode 100644 index 0000000..3bf201d --- /dev/null +++ b/Codeforces Round 1065 (Div. 3)/D. Rae Taylor and Trees (easy version).cpp @@ -0,0 +1,143 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2171/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; + vi p(n); + cin >> p; + + vi suf(n + 1); + vi pos(n); + for (int i = n - 1; i >= 0; i--) { + suf[i] = max(suf[i + 1], p[i]); + pos[p[i] - 1] = i; + } + + int now = 1; + int ma = 1; + int lim = n; + while (now <= n) { + int po = pos[now - 1]; + if (po >= lim) { + now++; + continue; + } + + if (now > ma) { + cout << "No\n"; + return; + } + + rmax(ma, suf[po]); + rmin(lim, po); + now++; + } + + if (lim != 0) { + 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 1065 (Div. 3)/F. Rae Taylor and Trees (hard version).cpp b/Codeforces Round 1065 (Div. 3)/F. Rae Taylor and Trees (hard version).cpp index e9fa9ad..1e55953 100644 --- a/Codeforces Round 1065 (Div. 3)/F. Rae Taylor and Trees (hard version).cpp +++ b/Codeforces Round 1065 (Div. 3)/F. Rae Taylor and Trees (hard version).cpp @@ -93,42 +93,52 @@ void solve() vi p(n); cin >> p; + vi suf(n + 1); vi pos(n); - rep(i, n) { + for (int i = n - 1; i >= 0; i--) { + suf[i] = max(suf[i + 1], p[i]); pos[p[i] - 1] = i; } - stack> s; + V> ans; - pair big = {0, 0}; - - V> edges; - - for (int i = n - 1; i >= 0; i--) { - while (!s.empty() && s.top().first > pos[i]) { - edges.emplace_back(s.top().second + 1, i + 1); - s.pop(); + int now = 1; + int lim = n; + int ma = 1; + while (now < n) { + int ps = pos[now - 1]; + if (now > ma) { + cout << "No\n"; + return; } - if (big.first > pos[i]) { - edges.emplace_back(big.second + 1, i + 1); + if (ps >= lim) { + now++; continue; } - big = {pos[i], i}; - s.emplace(pos[i], i); + nrep(i, ps + 1, n) { + if (p[i] < now) { + break; + } + + ans.emplace_back(p[i], now); + } + if (now != ma) { + ans.emplace_back(ma, now); + } + + lim = ps; + rmax(ma, suf[ps]); } - sortv(edges); - edges.erase(unique(all(edges)), edges.end()); - - if (edges.size() != n - 1) { + if (ans.size() < n - 1) { cout << "No\n"; return; } cout << "Yes\n"; - repv(i, edges) { + repv(i, ans) { cout << i.first << ' ' << i.second << '\n'; } } diff --git a/Codeforces Round 1073 (Div. 1)/B1. Sub-RBS (Easy Version).cpp b/Codeforces Round 1073 (Div. 1)/B1. Sub-RBS (Easy Version).cpp new file mode 100644 index 0000000..13cb31a --- /dev/null +++ b/Codeforces Round 1073 (Div. 1)/B1. Sub-RBS (Easy Version).cpp @@ -0,0 +1,126 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2190/B1 */ + +#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 s; + cin >> s; + + int ind = 0; + while (ind < s.size() - 1 && (s[ind] == '(' || s[ind + 1] == ')')) { + ind++; + } + + int ne = ind + 2; + while (ne < s.size() && s[ne] != '(') { + ne++; + } + + if (ne >= s.size()) { + cout << "-1\n"; + return; + } + + cout << n - 2 << '\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 1079 (Div. 2)/A. Friendly Numbers.cpp b/Codeforces Round 1079 (Div. 2)/A. Friendly Numbers.cpp new file mode 100644 index 0000000..bb99d52 --- /dev/null +++ b/Codeforces Round 1079 (Div. 2)/A. Friendly Numbers.cpp @@ -0,0 +1,127 @@ +/* Problem URL: https://codeforces.com/contest/2197/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() +{ + ll x; + cin >> x; + + auto calc = [&](ll a) { + ll sum = 0; + ll tmp = a; + while (tmp > 0) { + sum += tmp % 10; + tmp /= 10; + } + + return a - sum; + }; + + int ans = 0; + rep(i, 1000) { + if (calc(x + i) == x) { + ans++; + } + } + + 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 1079 (Div. 2)/B. Array and Permutation.cpp b/Codeforces Round 1079 (Div. 2)/B. Array and Permutation.cpp new file mode 100644 index 0000000..86c5e45 --- /dev/null +++ b/Codeforces Round 1079 (Div. 2)/B. Array and Permutation.cpp @@ -0,0 +1,132 @@ +/* Problem URL: https://codeforces.com/contest/2197/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; + vi a(n); + vi b(n); + cin >> a >> b; + + vi pos(n); + rep(i, n) { + pos[a[i] - 1] = i; + } + + vi need(n); + rep(i, n) { + need[i] = pos[b[i] - 1]; + } + + int mi = oo; + for (int i = n - 1; i >= 0; i--) { + if (need[i] > mi) { + cout << "NO\n"; + return; + } + + rmin(mi, need[i]); + } + + 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 1079 (Div. 2)/C. Game with a Fraction.cpp b/Codeforces Round 1079 (Div. 2)/C. Game with a Fraction.cpp new file mode 100644 index 0000000..7867017 --- /dev/null +++ b/Codeforces Round 1079 (Div. 2)/C. Game with a Fraction.cpp @@ -0,0 +1,120 @@ +/* Problem URL: https://codeforces.com/contest/2197/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() +{ + ll p, q; + cin >> p >> q; + + if (p >= q) { + cout << "Alice\n"; + return; + } + + ll diff = q - p; + if (p >= 2 * diff && q >= 3 * diff) { + cout << "Bob\n"; + return; + } + + cout << "Alice\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 1082 (Div. 2)/A. Parkour Design.cpp b/Codeforces Round 1082 (Div. 2)/A. Parkour Design.cpp new file mode 100644 index 0000000..0c95dee --- /dev/null +++ b/Codeforces Round 1082 (Div. 2)/A. Parkour Design.cpp @@ -0,0 +1,123 @@ +/* Problem URL: https://codeforces.com/contest/2202/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() +{ + ll x, y; + cin >> x >> y; + + ll xlim = 0; + if (y > 0) { + xlim += y * 2; + } + + if (y < 0) { + xlim += -y * 4; + } + + if (xlim > x || (x - xlim) % 3) { + 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 1082 (Div. 2)/B. ABAB Construction.cpp b/Codeforces Round 1082 (Div. 2)/B. ABAB Construction.cpp new file mode 100644 index 0000000..ce1cdbf --- /dev/null +++ b/Codeforces Round 1082 (Div. 2)/B. ABAB Construction.cpp @@ -0,0 +1,143 @@ +/* Problem URL: https://codeforces.com/contest/2202/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; + string a; + cin >> a; + + vvvi dp(n, vvi(2, vi(2))); + + if (a.back() == 'a') { + dp.back()[0][0] = 1; + } else if (a.back() == 'b') { + dp.back()[1][1] = 1; + } else { + dp.back()[0][0] = 1; + dp.back()[1][1] = 1; + } + + for (int i = n - 2; i >= 0; i--) { + rep(j, 4) { + int b1 = (j >> 1) & 1; + int b2 = j & 1; + + if (a[i] == '?') { + dp[i][b1][b2] |= dp[i + 1][b1^1][b2]; + dp[i][b1][b2] |= dp[i + 1][b1][b2^1]; + continue; + } + + int c = a[i] - 'a'; + if (b1 == c) { + dp[i][b1][b2] |= dp[i + 1][b1^1][b2]; + } + if (b2 == c) { + dp[i][b1][b2] |= dp[i + 1][b1][b2^1]; + } + } + } + + cout << (dp[0][0][~n & 1] ? "YES\n" : "NO\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 1082 (Div. 2)/C1. Lost Civilization (Easy Version).cpp b/Codeforces Round 1082 (Div. 2)/C1. Lost Civilization (Easy Version).cpp new file mode 100644 index 0000000..a1a2936 --- /dev/null +++ b/Codeforces Round 1082 (Div. 2)/C1. Lost Civilization (Easy Version).cpp @@ -0,0 +1,126 @@ +/* Problem URL: https://codeforces.com/contest/2202/problem/C1 */ + +#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; + + set t; + int c = 0; + + rep(i, n) { + if (t.count(a[i])) { + c++; + while (*prev(t.end()) > a[i] + 1) { + t.erase(prev(t.end())); + } + } else { + t.clear(); + } + t.insert(a[i] + 1); + } + + cout << n - 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 1083 (Div. 2)/A. Simons and Making It Beautiful.cpp b/Codeforces Round 1083 (Div. 2)/A. Simons and Making It Beautiful.cpp new file mode 100644 index 0000000..9e12969 --- /dev/null +++ b/Codeforces Round 1083 (Div. 2)/A. Simons and Making It Beautiful.cpp @@ -0,0 +1,118 @@ +/* Problem URL: https://codeforces.com/contest/2205/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 p(n); + cin >> p; + + rep(i, n) { + if (p[i] == n) { + swap(p[i], p[0]); + break; + } + } + + 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 1083 (Div. 2)/B. Simons and Cakes for Success.cpp b/Codeforces Round 1083 (Div. 2)/B. Simons and Cakes for Success.cpp new file mode 100644 index 0000000..edfc20f --- /dev/null +++ b/Codeforces Round 1083 (Div. 2)/B. Simons and Cakes for Success.cpp @@ -0,0 +1,140 @@ +/* Problem URL: https://codeforces.com/contest/2205/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; + +constexpr int MAXN = 1e5; +bool prime[MAXN]; +vl primes; + + +void pre() +{ + fill(prime, prime + MAXN, true); + prime[1] = false; + + nrep(i, 2, MAXN) { + if (!prime[i]) { + continue; + } + + primes.push_back(i); + for (int j = i * 2; j < MAXN; j += i) { + prime[j] = false; + } + } +} + +#define TEST 1 + +void solve() +{ + ll n; + cin >> n; + + vl ans; + for (int i = 0; primes[i] * primes[i] <= n; i++) { + while (n % primes[i] == 0) { + n /= primes[i]; + ans.push_back(primes[i]); + } + } + + ans.push_back(n); + ans.erase(unique(all(ans)), ans.end()); + + ll act = 1; + repv(i, ans) { + act *= i; + } + cout << act << '\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 1083 (Div. 2)/C. Simons and Posting Blogs.cpp b/Codeforces Round 1083 (Div. 2)/C. Simons and Posting Blogs.cpp new file mode 100644 index 0000000..2441d0b --- /dev/null +++ b/Codeforces Round 1083 (Div. 2)/C. Simons and Posting Blogs.cpp @@ -0,0 +1,197 @@ +/* Problem URL: https://codeforces.com/contest/2205/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; + + vvi act(n); + rep(i, n) { + int m; + cin >> m; + act[i].resize(m); + cin >> act[i]; + } + + V used(1e6 + 1); + + vi t(n); + iota(all(t), 0); + vi ans; + while (true) { + if (t.empty()) { + break; + } + + vi ch = t; + vi tmp; + while (ch.size() > 1) { + int a = oo; + repv(j, ch) { + while (!act[j].empty() && used[act[j].back()]) { + act[j].pop_back(); + } + + if (act[j].empty()) { + a = oo; + break; + } + + if (act[j].back() < a) { + tmp.clear(); + tmp.push_back(j); + a = act[j].back(); + continue; + } + + if (act[j].back() == a) { + tmp.push_back(j); + } + } + + if (a == oo) { + break; + } + + used[a] = true; + ans.push_back(a); + swap(ch, tmp); + tmp.clear(); + } + + bool rem = false; + int cur = 0; + rep(i, t.size()) { + while (!act[t[i]].empty() && used[act[t[i]].back()]) { + act[t[i]].pop_back(); + } + + if (act[t[i]].empty()) { + rem = true; + continue; + } + + t[cur] = t[i]; + cur++; + } + while (t.size() > cur) { + t.pop_back(); + } + + if (rem) { + continue; + } + + while (!act[ch[0]].empty()) { + int j = act[ch[0]].back(); + act[ch[0]].pop_back(); + if (used[j]) { + continue; + } + used[j] = true; + ans.push_back(j); + } + } + + cout << ans; +} + +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 1083 (Div. 2)/D. Simons and Beating Peaks.cpp b/Codeforces Round 1083 (Div. 2)/D. Simons and Beating Peaks.cpp new file mode 100644 index 0000000..4bdd4d3 --- /dev/null +++ b/Codeforces Round 1083 (Div. 2)/D. Simons and Beating Peaks.cpp @@ -0,0 +1,172 @@ +/* Problem URL: https://codeforces.com/contest/2205/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; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vi p(n); + cin >> p; + + vi pos(n); + rep(i, n) { + pos[p[i] - 1] = i; + } + + vi lf(n, -1); + vi rg(n, n); + + stack> s; + rep(i, n) { + while (!s.empty() && s.top().first < p[i]) { + s.pop(); + } + + if (!s.empty()) { + lf[i] = s.top().second; + } + + s.emplace(p[i], i); + } + + while (!s.empty()) { + s.pop(); + } + + for (int i = n - 1; i >= 0; i--) { + while (!s.empty() && s.top().first < p[i]) { + s.pop(); + } + + if (!s.empty()) { + rg[i] = s.top().second; + } + + s.emplace(p[i], i); + } + + vvi dp(n, vi(2)); + + int ans = oo; + + for (int i = n - 1; i >= 0; i--) { + int ps = pos[i]; + + if (lf[ps] == -1) { + dp[i][0] = ps; + } else { + int dis = ps - lf[ps] - 1; + dp[i][0] = dis + dp[p[lf[ps]] - 1][0]; + } + + if (rg[ps] == n) { + dp[i][1] = n - ps - 1; + } else { + int dis = rg[ps] - ps - 1; + dp[i][1] = dis + dp[p[rg[ps]] - 1][1]; + } + + rmin(ans, dp[i][0] + dp[i][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 1084 (Div. 3)/A. Eating Game.cpp b/Codeforces Round 1084 (Div. 3)/A. Eating Game.cpp new file mode 100644 index 0000000..836fbbc --- /dev/null +++ b/Codeforces Round 1084 (Div. 3)/A. Eating Game.cpp @@ -0,0 +1,111 @@ +/* Problem URL: https://codeforces.com/contest/2200/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 a(n); + cin >> a; + int b = *max_element(all(a)); + cout << count(all(a), b) << '\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 1084 (Div. 3)/B. Deletion Sort.cpp b/Codeforces Round 1084 (Div. 3)/B. Deletion Sort.cpp new file mode 100644 index 0000000..31104d5 --- /dev/null +++ b/Codeforces Round 1084 (Div. 3)/B. Deletion Sort.cpp @@ -0,0 +1,118 @@ +/* Problem URL: https://codeforces.com/contest/2200/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; + vi a(n); + cin >> a; + + nrep(i, 1, n) { + if (a[i] < a[i - 1]) { + cout << "1\n"; + return; + } + } + + cout << n << '\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 1084 (Div. 3)/C. Specialty String.cpp b/Codeforces Round 1084 (Div. 3)/C. Specialty String.cpp new file mode 100644 index 0000000..a6a8130 --- /dev/null +++ b/Codeforces Round 1084 (Div. 3)/C. Specialty String.cpp @@ -0,0 +1,131 @@ +/* Problem URL: https://codeforces.com/contest/2200/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; + string a; + cin >> n >> a; + + if (n & 1) { + cout << "NO\n"; + return; + } + + vi c(n, 1); + + for (int i = 1; i <= n; i += 2) { + vi pref = c; + nrep(j, 1, n) { + pref[j] += pref[j - 1]; + } + + for (int j = 0; j + i < n; j++) { + if (c[j] && c[j + i] && a[j] == a[j + i] && pref[j + i - 1] - pref[j] == 0) { + c[j] = 0; + c[j + i] = 0; + } + } + } + + cout << (find(all(c), 1) == c.end() ? "YES\n" : "NO\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 1084 (Div. 3)/D. Portal.cpp b/Codeforces Round 1084 (Div. 3)/D. Portal.cpp new file mode 100644 index 0000000..57e7371 --- /dev/null +++ b/Codeforces Round 1084 (Div. 3)/D. Portal.cpp @@ -0,0 +1,150 @@ +/* Problem URL: https://codeforces.com/contest/2200/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; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, x, y; + cin >> n >> x >> y; + + vi p(n); + cin >> p; + + vi act; + nrep(i, x, y) { + act.push_back(p[i]); + } + + int pos = min_element(all(act)) - act.begin(); + vi ans; + + nrep(i, pos, act.size()) { + ans.push_back(act[i]); + } + rep(i, pos) { + ans.push_back(act[i]); + } + + vi ot; + rep(i, x) { + ot.push_back(p[i]); + } + nrep(i, y, n) { + ot.push_back(p[i]); + } + + if (ot.empty()) { + cout << ans; + return; + } + + int i = 0; + for (; i < ot.size() && ot[i] < ans[0]; i++) { + cout << ot[i] << ' '; + } + repv(i, ans) { + cout << i << ' '; + } + for (; i < ot.size(); i++) { + cout << ot[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 1084 (Div. 3)/E. Divisive Battle.cpp b/Codeforces Round 1084 (Div. 3)/E. Divisive Battle.cpp new file mode 100644 index 0000000..9b7a5ee --- /dev/null +++ b/Codeforces Round 1084 (Div. 3)/E. Divisive Battle.cpp @@ -0,0 +1,156 @@ +/* Problem URL: https://codeforces.com/contest/2200/problem/E */ + +#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; + +constexpr int MAXN = 1e6 + 1; +int d[MAXN]; + +void pre() +{ + + fill(d, d + MAXN, 1); + + nrep(i, 1, MAXN) { + if (d[i] != 1) { + continue; + } + + for (int j = i; j < MAXN; j += i) { + d[j] = i; + } + } +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vi a(n); + cin >> a; + + bool pos = false; + nrep(i, 1, n) { + if (a[i] < a[i - 1]) { + pos = true; + break; + } + } + + if (!pos) { + cout << "Bob\n"; + return; + } + + vi act; + repv(i, a) { + if (i == 1) { + act.push_back(i); + continue; + } + while (i > 1) { + act.push_back(d[i]); + i /= d[i]; + } + } + + nrep(i, 1, act.size()) { + if (act[i] < act[i - 1]) { + cout << "Alice\n"; + return; + } + } + + cout << "Bob\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 1084 (Div. 3)/F. Mooclear Reactor 2.cpp b/Codeforces Round 1084 (Div. 3)/F. Mooclear Reactor 2.cpp new file mode 100644 index 0000000..eb36d88 --- /dev/null +++ b/Codeforces Round 1084 (Div. 3)/F. Mooclear Reactor 2.cpp @@ -0,0 +1,148 @@ +/* Problem URL: https://codeforces.com/contest/2200/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 1 + +void solve() +{ + int n, m; + cin >> n >> m; + + vvi par(n + 1); + rep(i, n) { + int x, y; + cin >> x >> y; + par[y].push_back(x); + } + + ll ans = 0; + + vvl b(n + 1, vl(2)); + multiset s; + ll tot = 0; + for (int i = n; i >= 0; i--) { + repv(j, par[i]) { + s.insert(j); + tot += j; + } + + while (s.size() > i + 1) { + tot -= *s.begin(); + s.erase(s.begin()); + } + + b[i][0] = tot; + b[i][1] = tot - *s.begin() * (s.size() == i + 1); + rmax(ans, b[i][0]); + } + + vl ma(n + 1); + ma[0] = 0; + nrep(i, 1, n + 1) { + ma[i] = max(ma[i - 1], b[i][1]); + } + + while (m--) { + ll x, y; + cin >> x >> y; + cout << max({b[y][0], ma[y] + x, ans}) << " \n"[m == 0]; + + } +} + +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 1085 (Div. 1 + Div. 2)/A. 1-1.cpp b/Codeforces Round 1085 (Div. 1 + Div. 2)/A. 1-1.cpp new file mode 100644 index 0000000..16321c4 --- /dev/null +++ b/Codeforces Round 1085 (Div. 1 + Div. 2)/A. 1-1.cpp @@ -0,0 +1,133 @@ +/* Problem URL: https://codeforces.com/contest/2207/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 s; + cin >> s; + + nrep(i, 1, n - 1) { + if (s[i - 1] == '1' && s[i + 1] == '1') { + s[i] = '1'; + } + } + + int ans1 = 0; + int ans2 = 0; + int c = 0; + rep(i, n) { + if (s[i] == '1') { + c++; + continue; + } + + ans1 += c - (c - 1) / 2; + ans2 += c; + c = 0; + } + ans1 += c - (c - 1) / 2; + ans2 += c; + + cout << ans1 << ' ' << ans2 << '\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 854 by cybercats (Div. 1 + Div. 2)/C. Double Lexicographically Minimum.cpp b/Codeforces Round 854 by cybercats (Div. 1 + Div. 2)/C. Double Lexicographically Minimum.cpp new file mode 100644 index 0000000..2f1748c --- /dev/null +++ b/Codeforces Round 854 by cybercats (Div. 1 + Div. 2)/C. Double Lexicographically Minimum.cpp @@ -0,0 +1,173 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1799/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() +{ + string a; + cin >> a; + + map c; + repv(i, a) { + c[i]++; + } + + int l = 0; + int r = a.size() - 1; + + auto itr = c.begin(); + while (itr != c.end()) { + auto [ch, co] = *itr; + itr = c.erase(itr); + + while (co > 1) { + a[l] = ch; + a[r] = ch; + l++; + r--; + co -= 2; + } + + if (co == 0) { + continue; + } + + if (c.size() >= 2) { + a[r] = ch; + r--; + break; + } + + if (c.empty()) { + a[l] = ch; + break; + } + + auto [ch2, co2] = *itr; + itr = c.erase(itr); + + int now = 1; + while (co2--) { + if (now) { + a[l] = ch2; + l++; + } else { + a[r] = ch2; + r--; + } + now ^= 1; + } + + a[r] = ch; + } + + while (itr != c.end()) { + auto [ch, co] = *itr; + itr++; + + while (co--) { + a[l] = ch; + l++; + } + } + + cout << a << '\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 855 (Div. 3)/E1. Unforgivable Curse (easy version).cpp b/Codeforces Round 855 (Div. 3)/E1. Unforgivable Curse (easy version).cpp new file mode 100644 index 0000000..1aabc94 --- /dev/null +++ b/Codeforces Round 855 (Div. 3)/E1. Unforgivable Curse (easy version).cpp @@ -0,0 +1,137 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1800/E1 */ + +#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; + cin >> n >> k; + string a, b; + cin >> a >> b; + + vi c1(26); + vi c2(26); + rep(i, n) { + c1[a[i] - 'a']++; + c2[b[i] - 'a']++; + } + + if (c1 != c2) { + cout << "NO\n"; + return; + } + + if (n <= 3 && a != b) { + cout << "NO\n"; + return; + } + + if (n >= 3 && n <= 5 && a[2] != b[2]) { + cout << "NO\n"; + return; + } + + if (n >= 3 && n <= 4 && a[1] != b[1]) { + 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 855 (Div. 3)/F. Dasha and Nightmares.cpp b/Codeforces Round 855 (Div. 3)/F. Dasha and Nightmares.cpp new file mode 100644 index 0000000..211363c --- /dev/null +++ b/Codeforces Round 855 (Div. 3)/F. Dasha and Nightmares.cpp @@ -0,0 +1,142 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1800/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; + + V> a(n); + rep(i, n) { + string s; + cin >> s; + vi c(26); + repv(i, s) { + c[i - 'a']++; + } + + rep(j, 26) { + a[i].first |= (c[j] != 0) << j; + a[i].second |= (c[j] & 1) << j; + } + } + + ll ans = 0; + rep(i, 26) { + vi all; + repv(j, a) { + if ((j.first >> i) & 1) { + continue; + } + all.push_back(j.second ^ ((1 << 26) - 1) ^ (1 << i)); + } + sortv(all); + repv(j, a) { + if ((j.first >> i) & 1) { + continue; + } + ans += upper_bound(all(all), j.second) - lower_bound(all(all), j.second); + } + } + + cout << (ans >> 1) << '\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 856 (Div. 2)/D. Counting Factorizations.cpp b/Codeforces Round 856 (Div. 2)/D. Counting Factorizations.cpp index 699a799..c9a6100 100644 --- a/Codeforces Round 856 (Div. 2)/D. Counting Factorizations.cpp +++ b/Codeforces Round 856 (Div. 2)/D. Counting Factorizations.cpp @@ -78,20 +78,17 @@ auto operator>>(istream &is, vector> &vec)->istream& { const int oo = INT32_MAX >> 1; const ll OO = INT64_MAX >> 1; -const ll mod = 998244353; - -constexpr MAXN = 1e6 + 10; +constexpr int MAXN = 1e6 + 10; bool prime[MAXN]; -bool vis[MAXN]; - -vl fact[MAXN]; -vl inv[MAXN]; +ll fact[MAXN]; +ll inv[MAXN]; +ll finv[MAXN]; +ll mod = 998244353; void pre() { fill(prime, prime + MAXN, true); - prime[0] = false; prime[1] = false; nrep(i, 2, MAXN) { @@ -105,50 +102,38 @@ void pre() } fact[0] = 1; - fact[1] = 1; - nrep(i, 2, MAXN) { + nrep(i, 1, MAXN) { fact[i] = fact[i - 1] * i % mod; } - inv[0] = 1; inv[1] = 1; nrep(i, 2, MAXN) { inv[i] = (mod - mod / i) * inv[mod % i] % mod; } - nrep(i, 2, MAXN) { - inv[i] = inv[i - 1] * inv[i] % mod; + finv[0] = 1; + nrep(i, 1, MAXN) { + finv[i] = finv[i - 1] * inv[i] % mod; } } -#define TEST 1 +#define TEST 0 void solve() { int n; cin >> n; + vi a(2 * n); + cin >> a; - vi act(n << 1); - cin >> act; - - set up; - - vi ot; vi primes; - - rep(i, n << 1) { - if (prime[act[i]]) { - if (vis[act[i]]) { - ot.push_back(act[i]); - } else { - up.insert(act[i]); - vis[act[i]] = true; - primes.push_back(act[i]); - } - continue; + map c; + rep(i, 2 * n) { + if (prime[a[i]] && !c.count(a[i])) { + primes.push_back(a[i]); } - ot.push_back(act[i]); + c[a[i]]++; } if (primes.size() < n) { @@ -156,38 +141,27 @@ void solve() return; } - auto comb = [&](int n, int k) { - return fact[n] * inv[k] % mod * inv[n - k] % mod; - }; - - ll ans = comb(primes.size(), n); - - if (ot.empty()) { - cout << ans << '\n'; - return; - } - - sortv(ot); - - int c = 1; - vi tmp; - - nrep(i, 1, ot.size()) { - if (ot[i] == ot[i - 1]) { - c++; - continue; + ll non = 1; + repv(i, c) { + if (!prime[i.first]) { + non = (non * finv[i.second]) % mod; } - - tmp.push_back(c); - c = 1; } - tmp.push_back(c); + vvl dp(primes.size() + 1, vl(n + 1)); + dp[0][0] = fact[n] * non % mod; + nrep(i, 1, primes.size() + 1) { + int cur = primes[i - 1]; + ll co = c[cur]; + dp[i][0] = dp[i - 1][0] * finv[co] % mod; - repv(i, up) { - vis[i] = false; + nrep(j, 1, n + 1) { + dp[i][j] = (dp[i - 1][j] * finv[co] % mod + dp[i - 1][j - 1] * finv[co - 1] % mod) % mod; + } } + + cout << dp[primes.size()][n] << '\n'; } int main() diff --git a/Codeforces Round 861 (Div. 2)/C. Unlucky Numbers.cpp b/Codeforces Round 861 (Div. 2)/C. Unlucky Numbers.cpp new file mode 100644 index 0000000..4ac0809 --- /dev/null +++ b/Codeforces Round 861 (Div. 2)/C. Unlucky Numbers.cpp @@ -0,0 +1,168 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1808/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 vvvvvi = vector; +using vvvvvvi = 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() +{ + string a, b; + cin >> a >> b; + + string zer(b.size() - a.size(), '0'); + a = zer + a; + int n = a.size(); + rep(i, n) { + a[i] -= '0'; + b[i] -= '0'; + } + + vvvvvvi memo(n, vvvvvi(2, vvvvi(2, vvvi(2, vvi(10, vi(10, -1)))))); + + function dp = [&](int i, int u, int o, int s, int mi, int ma){ + if (i >= n) { + return ma - mi; + } + + int &ans = memo[i][u][o][s][mi][ma]; + if (ans != -1) { + return ans; + } + ans = oo; + + for (int j = o ? 0 : a[i]; j <= (u ? 9 : b[i]); j++) { + rmin(ans, dp(i + 1, u || j < b[i], o || j > a[i], s || j != 0, s || j != 0 ? min(mi, j) : 9, s || j != 0 ? max(ma, j) : 0)); + } + return ans; + }; + + int target = dp(0, 0, 0, 0, 9, 0); + + string ans; + function rec = [&](int i, int u, int o, int s, int mi, int ma) { + for (int j = o ? 0 : a[i]; j <= (u ? 9 : b[i]); j++) { + bool nu = u || j < b[i]; + bool no = o || j > a[i]; + bool ns = s || j != 0; + + int nmi = ns ? min(mi, j) : 9; + int nma = ns ? max(ma, j) : 0; + + if (i == n - 1) { + if (nma - nmi == target) { + ans += j + '0'; + return; + } + continue; + } + + if (memo[i + 1][nu][no][ns][nmi][nma] == target) { + ans += j + '0'; + rec(i + 1, nu, no, ns, nmi, nma); + return; + } + } + }; + + rec(0, 0, 0, 0, 9, 0); + + cout << stoll(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 864 (Div. 2)/C. Li Hua and Chess.cpp b/Codeforces Round 864 (Div. 2)/C. Li Hua and Chess.cpp new file mode 100644 index 0000000..b178f18 --- /dev/null +++ b/Codeforces Round 864 (Div. 2)/C. Li Hua and Chess.cpp @@ -0,0 +1,143 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1797/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() +{ + ll n, m; + cin >> n >> m; + + auto ask = [&](int i, int j) { + cout << "? " << i << ' ' << j << endl; + ll ans; + cin >> ans; + return ans; + }; + + auto answer = [&](int i, int j) { + cout << "! " << i << ' ' << j << endl; + return; + }; + + ll c1 = ask(1, 1); + + if (c1 >= n) { + ll c2 = ask(1, c1 + 1); + answer(c2 + 1, c1 + 1); + return; + } + + ll c2 = ask(c1 + 1, 1); + + if (c1 != c2) { + ll c3 = ask(c1 + 1, c2 + 1); + if (c3 == 0) { + answer(c1 + 1, c2 + 1); + return; + } + + answer(n - c2, c1 + 1); + return; + } + + ll c3 = ask(1, c1 + 1); + answer(c3 + 1, c1 + 1); +} + +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 890 (Div. 2) supported by Constructor Institute/C. To Become Max.cpp b/Codeforces Round 890 (Div. 2) supported by Constructor Institute/C. To Become Max.cpp new file mode 100644 index 0000000..d1b42dc --- /dev/null +++ b/Codeforces Round 890 (Div. 2) supported by Constructor Institute/C. To Become Max.cpp @@ -0,0 +1,159 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1856/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 k; + cin >> n >> k; + + vl a(n); + cin >> a; + + ll ans = *max_element(all(a)); + rep(i, n - 1) { + ll co = 0; + nrep(j, i + 1, n) { + if (a[j] < a[i] - (j - i - 1)) { + co += a[i] - (j - i - 1) - a[j]; + if (co >= k) { + break; + } + continue; + } + + if (co == k) { + break; + } + + ll lk = k - co; + ll low = 0; + ll high = a[j] - a[i] + j - i - 1; + ll act = 0; + while (low <= high) { + ll mid = (low + high) >> 1; + + ll cost = mid * (j - i); + if (cost < lk) { + low = mid + 1; + act = mid; + continue; + } + + high = mid - 1; + } + + rmax(ans, a[i] + act + 1); + a[i] += act; + co += act * (j - i); + + if (a[j] < a[i] - (j - i - 1)) { + co += a[i] - (j - i - 1) - a[j]; + if (co >= k) { + break; + } + } + } + } + + 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 893 (Div. 2)/B. The Walkway.cpp b/Codeforces Round 893 (Div. 2)/B. The Walkway.cpp new file mode 100644 index 0000000..9708f56 --- /dev/null +++ b/Codeforces Round 893 (Div. 2)/B. The Walkway.cpp @@ -0,0 +1,158 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1858/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, m, d; + cin >> n >> m >> d; + + vi t(m); + cin >> t; + + vl suf(m); + suf[m - 1] = (n - t[m - 1]) / d + 1; + + for (int i = m - 2; i >= 0; i--) { + suf[i] = suf[i + 1] + (t[i + 1] - t[i] - 1) / d + 1; + } + + ll ans = OO; + int c = 1; + ll pref = 0; + + rep(i, m) { + if (i == 0) { + ll tot = (t[i + 1] - 2) / d + suf[1] + 1; + ans = tot; + pref += max((t[i] - 2) / d, 0) + 1 + (t[0] != 1); + continue; + } + + if (i == m - 1) { + ll tot = (n - t[i - 1]) / d + pref; + + if (tot < ans) { + ans = tot; + c = 1; + continue; + } + + if (tot == ans) { + c++; + } + + continue; + } + + ll tot = (t[i + 1] - t[i - 1] - 1) / d + suf[i + 1] + pref; + if (tot < ans) { + ans = tot; + c = 1; + } else if (tot == ans) { + c++; + } + + pref += (t[i] - t[i - 1] - 1) / d + 1; + } + + cout << ans << ' ' << 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 898 (Div. 4)/G. ABBC or BACB.cpp b/Codeforces Round 898 (Div. 4)/G. ABBC or BACB.cpp new file mode 100644 index 0000000..e50feb9 --- /dev/null +++ b/Codeforces Round 898 (Div. 4)/G. ABBC or BACB.cpp @@ -0,0 +1,135 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1873/G */ + +#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() +{ + string s; + cin >> s; + int n = s.size(); + + if (n == 1) { + cout << "0\n"; + return; + } + + vi pos = {0}; + rep(i, n) { + if (s[i] == 'B') { + pos.push_back(i + 1); + } + } + pos.push_back(n + 1); + + if (pos.size() == 2) { + cout << "0\n"; + return; + } + + vvi dp(pos.size(), vi(2)); + + nrep(i, 1, pos.size() - 1) { + dp[i][0] = pos[i] - pos[i - 1] - 1 + dp[i - 1][0]; + dp[i][1] = pos[i + 1] - pos[i] - 1 + max(dp[i - 1][0], dp[i - 1][1]); + } + + cout << max(dp.end()[-2][0], dp.end()[-2][1]) << '\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 904 (Div. 2)/C. Medium Design.cpp b/Codeforces Round 904 (Div. 2)/C. Medium Design.cpp new file mode 100644 index 0000000..d05833e --- /dev/null +++ b/Codeforces Round 904 (Div. 2)/C. Medium Design.cpp @@ -0,0 +1,160 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1884/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, m; + cin >> n >> m; + + V> a(n); + vector c; + repv(i, a) { + cin >> i.first >> i.second; + c.push_back(i.first); + c.push_back(i.second); + } + auto itr = find(all(a), make_pair(1, m)); + if (itr != a.end()) { + a.erase(itr); + } + + sortv(c); + c.erase(unique(all(c)), c.end()); + repv(i, a) { + i.first = lower_bound(all(c), i.first) - c.begin(); + i.second = lower_bound(all(c), i.second) - c.begin(); + } + + int sz = c.size() + 1; + + vi t(sz); + vi pref(sz); + vi suf(sz); + repv(i, a) { + t[i.first]++; + t[i.second + 1]--; + if (c[i.first] == 1) { + pref[0]++; + pref[i.second + 1]--; + } + + if (c[i.second] == m) { + suf[sz - 1]++; + if (i.first > 0) { + suf[i.first - 1]--; + } + } + } + + nrep(i, 1, sz) { + pref[i] += pref[i - 1]; + suf[sz - i - 1] += suf[sz - i]; + t[i] += t[i - 1]; + } + + int ans = 0; + rep(i, sz) { + rmax(ans, t[i] - min(pref[i], suf[i])); + } + + 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)/E. Good Triples.cpp b/Codeforces Round 913 (Div. 3)/E. Good Triples.cpp new file mode 100644 index 0000000..edb8674 --- /dev/null +++ b/Codeforces Round 913 (Div. 3)/E. Good Triples.cpp @@ -0,0 +1,116 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1907/E */ + +#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() +{ + ll n; + cin >> n; + + ll ans = 1; + while (n > 0) { + ll now = n % 10; + n /= 10; + + ans *= (now + 1) * (now + 2) / 2; + } + 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 921 (Div. 1)/A. Did We Get Everything Covered?.cpp b/Codeforces Round 921 (Div. 1)/A. Did We Get Everything Covered?.cpp new file mode 100644 index 0000000..90f4639 --- /dev/null +++ b/Codeforces Round 921 (Div. 1)/A. Did We Get Everything Covered?.cpp @@ -0,0 +1,171 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1924/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, k, m; + cin >> n >> k >> m; + string a; + cin >> a; + + vi pos(k, -1); + vvi ne(m + 1, vi(k, -1)); + + for (int i = m - 1; i >= 0; i--) { + ne[i + 1] = pos; + pos[a[i] - 'a'] = i + 1; + } + ne[0] = pos; + + vi memo(m + 1, -1); + + function dp = [&](int i) { + if (i == -1) { + return 0; + } + + int &ans = memo[i]; + if (ans != -1) { + return ans; + } + + ans = oo; + rep(j, k) { + rmin(ans, dp(ne[i][j]) + 1); + } + + return ans; + }; + + int c = dp(0) - 1; + if (c >= n) { + cout << "YES\n"; + return; + } + + cout << "NO\n"; + + int now = 0; + string ans; + while (ans.size() < n) { + int j = 0; + while (1) { + if (ne[now][j] == -1) { + while (ans.size() < n) { + ans += j + 'a'; + } + break; + } + + if (memo[ne[now][j]] == c) { + c--; + ans += j + 'a'; + now = ne[now][j]; + break; + } + + j++; + } + } + + 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 928 (Div. 4)/G. Vlad and Trouble at MIT.cpp b/Codeforces Round 928 (Div. 4)/G. Vlad and Trouble at MIT.cpp new file mode 100644 index 0000000..f767152 --- /dev/null +++ b/Codeforces Round 928 (Div. 4)/G. Vlad and Trouble at MIT.cpp @@ -0,0 +1,143 @@ +/* Problem URL: https://codeforces.com/contest/1926/problem/G */ + +#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 graph(n); + nrep(i, 1, n) { + int p; + cin >> p; + graph[p - 1].push_back(i); + } + + string a; + cin >> a; + + vvl dp(n, vl(3)); + + function dfs = [&](int i) { + if (a[i] == 'S') { + dp[i][0] = oo; + dp[i][2] = oo; + } + + if (a[i] == 'P') { + dp[i][1] = oo; + dp[i][2] = oo; + } + + repv(j, graph[i]) { + dfs(j); + + dp[i][0] += min({dp[j][0], dp[j][1] + 1, dp[j][2]}); + dp[i][1] += min({dp[j][0] + 1, dp[j][1], dp[j][2]}); + dp[i][2] += min({dp[j][0] + 1, dp[j][1] + 1, dp[j][2]}); + } + }; + + dfs(0); + + cout << *min_element(all(dp[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/Codeforces Round 954 (Div. 3)/E. Beautiful Array.cpp b/Codeforces Round 954 (Div. 3)/E. Beautiful Array.cpp new file mode 100644 index 0000000..89afbbf --- /dev/null +++ b/Codeforces Round 954 (Div. 3)/E. Beautiful Array.cpp @@ -0,0 +1,168 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1986/E */ + +#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 k; + cin >> n >> k; + vl a(n); + cin >> a; + + map c; + rep(i, n) { + c[a[i] % k].push_back(a[i]); + } + + int oc = 0; + repv(i, c) { + oc += i.second.size() & 1; + } + + if (oc > 1) { + cout << "-1\n"; + return; + } + + auto func = [&](vl &v) -> int { + if (v.size() <= 1) { + return 0; + } + int n = v.size(); + vl suf(n + 1); + suf[n - 2] = (v.end()[-1] - v.end()[-2]) / k; + for (int i = n - 4; i >= 0; i -= 2) { + suf[i] = suf[i + 2] + (v[i + 1] - v[i]) / k; + } + + ll ans = OO; + ll sum = 0; + rep(i, n) { + if (i & 1) { + rmin(ans, (v[i + 1] - v[i - 1]) / k + suf[i + 2] + sum); + sum += (v[i] - v[i - 1]) / k; + continue; + } + + rmin(ans, sum + suf[i + 1]); + } + + return ans; + }; + + ll ans = 0; + repv(i, c) { + auto &v = i.second; + sortv(v); + if (v.size() & 1) { + ans += func(v); + continue; + } + + nrep(j, 1, v.size()) { + ans += (v[j] - v[j - 1]) / k; + j++; + } + } + + 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 954 (Div. 3)/F. Non-academic Problem.cpp b/Codeforces Round 954 (Div. 3)/F. Non-academic Problem.cpp new file mode 100644 index 0000000..d5183ad --- /dev/null +++ b/Codeforces Round 954 (Div. 3)/F. Non-academic Problem.cpp @@ -0,0 +1,208 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1986/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, m; + cin >> n >> m; + + V>> graph(n); + V rem(m); + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + graph[a].emplace_back(b, m); + graph[b].emplace_back(a, m); + } + + V vis(n); + vi t(n); + vi mi(n); + int tim = 0; + int tt = 1; + function dfs = [&](int i, int p) { + vis[i] = true; + t[i] = tim++; + mi[i] = t[i]; + + for (auto [j, ind] : graph[i]) { + if (j == p) { + continue; + } + + if (vis[j]) { + rmin(mi[i], t[j]); + continue; + } + + rmin(mi[i], dfs(j, i)); + if (mi[j] > t[i]) { + rem[ind] = true; + tt++; + } + } + + return mi[i]; + }; + + dfs(0, 0); + + vl c(tt); + vi g(n); + vvi act(tt); + int cur = 0; + fill(all(vis), false); + + function func = [&](int i) { + vis[i] = true; + g[i] = cur; + c[cur]++; + for (auto [j, ind] : graph[i]) { + if (vis[j] || rem[ind]) { + continue; + } + + func(j); + } + }; + + rep(i, n) { + if (vis[i]) { + continue; + } + func(i); + cur++; + } + + rep(i, n) { + for (auto [j, ind] : graph[i]) { + if (rem[ind]) { + act[g[i]].push_back(g[j]); + act[g[j]].push_back(g[i]); + } + } + } + + repv(i, act) { + sortv(i); + i.erase(unique(all(i)), i.end()); + } + + ll ans = (ll)n * (n - 1) / 2; + function calc = [&](int i, int p) { + ll count = c[i]; + repv(j, act[i]) { + if (j == p) { + continue; + } + + ll tm = calc(j, i); + rmin(ans, tm * (tm - 1) / 2 + (n - tm) * (n - tm - 1) / 2); + count += tm; + } + return count; + }; + + calc(0, 0); + 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 962 (Div. 3)/F. Bomb.cpp b/Codeforces Round 962 (Div. 3)/F. Bomb.cpp new file mode 100644 index 0000000..213d040 --- /dev/null +++ b/Codeforces Round 962 (Div. 3)/F. Bomb.cpp @@ -0,0 +1,161 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1996/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; + ll k; + cin >> n >> k; + + vl a(n); + vl b(n); + cin >> a >> b; + + ll low = 0; + ll high = 1e9; + ll act = 1e9; + while (low <= high) { + ll mid = (low + high) >> 1; + + ll cos = 0; + rep(i, n) { + cos += max((a[i] - mid + b[i] - 1) / b[i], 0LL); + } + + if (cos <= k) { + act = mid; + high = mid - 1; + continue; + } + + low = mid + 1; + } + + ll ans = 0; + ll cos = 0; + priority_queue> pq; + rep(i, n) { + ll t = max((a[i] - act + b[i] - 1) / b[i], 0LL); + cos += t; + + ll mod = a[i] % b[i]; + ll add = t * (a[i] / b[i] * 2 - t + 1) / 2 * b[i] + mod * t; + a[i] -= b[i] * t; + ans += add; + pq.emplace(max(a[i], 0LL), b[i]); + } + + k -= cos; + + while (k > 0) { + auto [x, y] = pq.top(); + if (x == 0) { + break; + } + pq.pop(); + ans += x; + pq.emplace(max(x - y, 0LL), y); + k--; + } + + 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 963 (Div. 2)/C. Light Switches.cpp b/Codeforces Round 963 (Div. 2)/C. Light Switches.cpp new file mode 100644 index 0000000..98c92c7 --- /dev/null +++ b/Codeforces Round 963 (Div. 2)/C. Light Switches.cpp @@ -0,0 +1,142 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1993/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, k; + cin >> n >> k; + + vl a(n); + cin >> a; + + ll mod = k << 1; + vl pref((mod << 1) + 1); + + ll ma = 0; + + repv(i, a) { + pref[i % mod]++; + pref[i % mod + k]--; + rmax(ma, i); + } + + nrep(i, 1, (mod << 1) + 1) { + pref[i] += pref[i - 1]; + } + + int act = 0; + nrep(i, ma % mod, (mod << 1)) { + ll sum = 0; + if (i >= mod) { + sum = pref[i] + pref[i - mod]; + } else { + sum = pref[i] + pref[i + mod]; + } + + if (sum == n) { + cout << ma + i - ma % mod << '\n'; + return; + } + } + + cout << "-1\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 969 (Div. 2)/C. Dora and C++.cpp b/Codeforces Round 969 (Div. 2)/C. Dora and C++.cpp new file mode 100644 index 0000000..f60fa36 --- /dev/null +++ b/Codeforces Round 969 (Div. 2)/C. Dora and C++.cpp @@ -0,0 +1,130 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2007/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 a, b; + cin >> n >> a >> b; + ll g = gcd(a, b); + + vl v(n); + repv(i, v) { + cin >> i; + i %= g; + } + sortv(v); + v.erase(unique(all(v)), v.end()); + + if (v.size() == 1) { + cout << "0\n"; + return; + } + + ll ans = v.back() - v[0]; + + nrep(i, 1, v.size()) { + rmin(ans, v[i - 1] + g - v[i]); + } + + 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 971 (Div. 4)/E. Klee's SUPER DUPER LARGE Array!!!.cpp b/Codeforces Round 971 (Div. 4)/E. Klee's SUPER DUPER LARGE Array!!!.cpp new file mode 100644 index 0000000..57f4650 --- /dev/null +++ b/Codeforces Round 971 (Div. 4)/E. Klee's SUPER DUPER LARGE Array!!!.cpp @@ -0,0 +1,132 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2009/E */ + +#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() +{ + ll n, k; + cin >> n >> k; + + auto calc = [&](ll l, ll r) { + return (r - l + 1) * (l + r) / 2; + }; + + ll low = k; + ll high = k + n - 2; + ll ans = k; + while (low <= high) { + ll mid = (low + high) / 2; + + ll f = calc(k, mid); + ll s = calc(mid + 1, k + n - 1); + + ll a = f - s; + if (a <= 0) { + ans = mid; + low = mid + 1; + continue; + } + + high = mid - 1; + } + + cout << min(abs(calc(k, ans) - calc(ans + 1, k + n - 1)), abs(calc(k, ans + 1) - calc(ans + 2, k + n - 1))) << '\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 973 (Div. 2)/C. Password Cracking.cpp b/Codeforces Round 973 (Div. 2)/C. Password Cracking.cpp new file mode 100644 index 0000000..9685907 --- /dev/null +++ b/Codeforces Round 973 (Div. 2)/C. Password Cracking.cpp @@ -0,0 +1,163 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2013/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 = [&](string a) { + cout << "? " << a << endl; + int ans; + cin >> ans; + return ans; + }; + + auto answer = [&](string a) { + cout << "! " << a << endl; + }; + + if (n == 1) { + bool t = ask("0"); + if (t) { + answer("0"); + } else { + answer("1"); + } + return; + } + + string ans; + if (ask("0")) { + ans = "0"; + } else { + ans = "1"; + } + + bool back = false; + rep(i, n - 1) { + if (back) { + if (ask("0" + ans)) { + ans = "0" + ans; + continue; + } + + ans = "1" + ans; + continue; + } + + if (ask(ans + "0")) { + ans += "0"; + continue; + } + + if (ask(ans + "1")) { + ans += "1"; + continue; + } + + back = true; + i--; + } + + answer(ans); +} + +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 981 (Div. 3)/F. Kosuke's Sloth.cpp b/Codeforces Round 981 (Div. 3)/F. Kosuke's Sloth.cpp new file mode 100644 index 0000000..446773a --- /dev/null +++ b/Codeforces Round 981 (Div. 3)/F. Kosuke's Sloth.cpp @@ -0,0 +1,127 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2033/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() +{ + ll n, k; + cin >> n >> k; + const ll mod = 1e9 + 7; + n %= mod; + + if (k == 1) { + cout << n << '\n'; + return; + } + + ll cur = 2; + ll a = 1; + ll b = 1; + + while (b % k != 0) { + ll c = b; + b = (a + b) % k; + a = c; + cur++; + } + + cout << cur * n % mod << '\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 993 (Div. 4)/G1. Medium Demon Problem (easy version).cpp b/Codeforces Round 993 (Div. 4)/G1. Medium Demon Problem (easy version).cpp new file mode 100644 index 0000000..3f60991 --- /dev/null +++ b/Codeforces Round 993 (Div. 4)/G1. Medium Demon Problem (easy version).cpp @@ -0,0 +1,169 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2044/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 + +void solve() +{ + int n; + cin >> n; + vi p(n); + repv(i, p) { + cin >> i; + i--; + } + + V vis(n); + V loop(n); + + function getloop = [&](int i) { + vis[i] = true; + int t = p[i]; + int h = p[t]; + + while (t != h) { + if (vis[t]) { + return; + } + + vis[t] = true; + + t = p[t]; + h = p[p[h]]; + } + + vis[t] = true; + loop[t] = true; + h = p[h]; + while (h != t) { + loop[h] = true; + vis[h] = true; + h = p[h]; + } + }; + + rep(i, n) { + if (vis[i]) { + continue; + } + + getloop(i); + } + + vi ans(n, -1); + + function dfs = [&](int i) { + if (loop[i]) { + return ans[i] = 0; + } + + if (ans[i] != -1) { + return ans[i]; + } + + return ans[i] = dfs(p[i]) + 1; + }; + + rep(i, n) { + dfs(i); + } + + cout << *max_element(all(ans)) + 2 << '\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/Educational Codeforces Round 142 (Rated for Div. 2)/C. Min Max Sort.cpp b/Educational Codeforces Round 142 (Rated for Div. 2)/C. Min Max Sort.cpp new file mode 100644 index 0000000..47374d8 --- /dev/null +++ b/Educational Codeforces Round 142 (Rated for Div. 2)/C. Min Max Sort.cpp @@ -0,0 +1,127 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1792/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 p(n); + cin >> p; + vi pos(n); + rep(i, n) { + pos[p[i] - 1] = i; + } + + int ans = n - 1; + + int l = 0; + int ma = pos[0]; + nrep(i, 1, n) { + if (pos[i] < pos[i - 1]) { + l = i; + } + + rmin(ans, max(l, n - i - 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/Educational Codeforces Round 153 (Rated for Div. 2)/C. Game on Permutation.cpp b/Educational Codeforces Round 153 (Rated for Div. 2)/C. Game on Permutation.cpp new file mode 100644 index 0000000..80ceb2e --- /dev/null +++ b/Educational Codeforces Round 153 (Rated for Div. 2)/C. Game on Permutation.cpp @@ -0,0 +1,127 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1860/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 p(n); + cin >> p; + + int ans = 0; + vi lis; + rep(i, n) { + auto itr = lower_bound(all(lis), p[i]); + int pos; + if (itr == lis.end()) { + lis.push_back(p[i]); + pos = lis.size() - 1; + } else { + *itr = p[i]; + pos = itr - lis.begin(); + } + + ans += pos == 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/Educational Codeforces Round 157 (Rated for Div. 2)/C. Torn Lucky Ticket.cpp b/Educational Codeforces Round 157 (Rated for Div. 2)/C. Torn Lucky Ticket.cpp new file mode 100644 index 0000000..111e7e1 --- /dev/null +++ b/Educational Codeforces Round 157 (Rated for Div. 2)/C. Torn Lucky Ticket.cpp @@ -0,0 +1,163 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1895/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 0 + +void solve() +{ + int n; + cin >> n; + + V ev; + V od; + + rep(i, n) { + string a; + cin >> a; + if (a.size() & 1) { + od.emplace_back(a); + } else { + ev.emplace_back(a); + } + } + + auto scmp = [&](string &a, string &b) { + return a.size() < b.size(); + }; + + sort(all(ev), scmp); + sort(all(od), scmp); + + auto calc = [&](V &a) { + ll ans = 0; + V> c(6); + + repv(i, a) { + nrep(j, 1, min(6, (int)i.size() + 1)) { + int lim = (i.size() + j) >> 1; + ll cur = 0; + rep(k, lim - j) { + cur -= i[k] - '0'; + } + nrep(k, lim - j, i.size()) { + cur += i[k] - '0'; + } + + ans += c[j][cur]; + + cur = 0; + rep(k, lim) { + cur += i[k] - '0'; + } + nrep(k, lim, i.size()) { + cur -= i[k] - '0'; + } + + ans += c[j][cur]; + } + + c[i.size()][accumulate(all(i), 0) - '0' * i.size()]++; + } + + return ans; + }; + + cout << calc(ev) + calc(od) << '\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/Educational Codeforces Round 166 (Rated for Div. 2)/C. Job Interview.cpp b/Educational Codeforces Round 166 (Rated for Div. 2)/C. Job Interview.cpp new file mode 100644 index 0000000..e4e3dea --- /dev/null +++ b/Educational Codeforces Round 166 (Rated for Div. 2)/C. Job Interview.cpp @@ -0,0 +1,167 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1976/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, m; + cin >> n >> m; + + int lim[] = {n, m}; + + V> a(n + m + 1); + vi team(n + m + 1); + rep(i, n + m + 1) { + cin >> a[i][0]; + } + rep(i, n + m + 1) { + cin >> a[i][1]; + team[i] = a[i][1] > a[i][0]; + } + + vi pref0(n + m + 2); + vi pref1(n + m + 2); + + nrep(i, 1, n + m + 2) { + pref0[i] = pref0[i - 1] + (team[i - 1] == 0); + pref1[i] = pref1[i - 1] + (team[i - 1] == 1); + } + + int lim0 = (int)(upper_bound(all(pref0), n) - pref0.begin()) - 1; + int lim1 = (int)(upper_bound(all(pref1), m) - pref1.begin()) - 1; + + int lims[] = {lim0, lim1}; + ll cou[] = {0, 0}; + + ll ans = 0; + for (int i = n + m; i >= 0; i--) { + if (i > lims[team[i]]) { + ans += a[i][team[i]^1]; + continue; + } + + if (i == lims[team[i]]) { + cou[team[i]] = a[i][team[i]]; + cou[team[i]^1] = a[i][team[i]^1]; + continue; + } + + ans += a[i][team[i]]; + } + + rep(i, n + m + 1) { + if (i < lims[team[i]]) { + cout << ans - a[i][team[i]] + cou[team[i]] << ' '; + continue; + } + + if (i == lims[team[i]]) { + cout << ans << ' '; + ans += a[i][team[i]^1]; + cou[0] = 0; + cou[1] = 0; + continue; + } + + cout << ans - a[i][team[i]^1] << ' '; + } + 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/Educational Codeforces Round 168 (Rated for Div. 2)/D. Maximize the Root.cpp b/Educational Codeforces Round 168 (Rated for Div. 2)/D. Maximize the Root.cpp new file mode 100644 index 0000000..549db23 --- /dev/null +++ b/Educational Codeforces Round 168 (Rated for Div. 2)/D. Maximize the Root.cpp @@ -0,0 +1,142 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1997/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; + + vl val(n); + cin >> val; + + vl an(n); + + vvi graph(n); + nrep(i, 1, n) { + int p; + cin >> p; + graph[p - 1].push_back(i); + } + + function dfs = [&](int i) { + if (graph[i].empty()) { + return val[i]; + } + + ll mi = OO; + repv(j, graph[i]) { + rmin(mi, dfs(j)); + } + + an[i] = mi; + if (val[i] > mi) { + return mi; + } + + ll diff = mi - val[i]; + return mi - ((diff + 1) >> 1); + }; + + dfs(0); + + cout << val[0] + an[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/Educational Codeforces Round 177 (Rated for Div. 2)/D. Even String.cpp b/Educational Codeforces Round 177 (Rated for Div. 2)/D. Even String.cpp new file mode 100644 index 0000000..6ea3af6 --- /dev/null +++ b/Educational Codeforces Round 177 (Rated for Div. 2)/D. Even String.cpp @@ -0,0 +1,151 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2086/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; + + +const ll mod = 998244353; + +constexpr int MAXN = 1e6; +ll fact[MAXN]; + +void pre() +{ + fact[0] = 1; + nrep(i, 1, MAXN) { + fact[i] = (fact[i - 1] * i) % mod; + } +} + +#define TEST 1 + +void solve() +{ + vi c(26); + int s = 0; + ll mu = 1; + repv(i, c) { + cin >> i; + s += i; + mu *= fact[i]; + mu %= mod; + } + + int o = (s + 1) >> 1; + + vi dp(o + 1); + dp[0] = 1; + rep(i, 26) { + if (c[i] == 0) { + continue; + } + + for (int j = o; j >= c[i]; j--) { + dp[j] += dp[j - c[i]]; + dp[j] %= mod; + } + } + + auto fpow = [&](ll a, ll p) { + ll ans = 1; + rep(i, 61) { + if ((p >> i) & 1) { + ans = (ans * a) % mod; + } + a = (a * a) % mod; + } + + return ans; + }; + + cout << fact[o] * fact[s >> 1] % mod * fpow(mu, mod - 2) % mod * dp[o] % mod << '\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/Educational Codeforces Round 178 (Rated for Div. 2)/E. Unpleasant Strings.cpp b/Educational Codeforces Round 178 (Rated for Div. 2)/E. Unpleasant Strings.cpp new file mode 100644 index 0000000..6078dde --- /dev/null +++ b/Educational Codeforces Round 178 (Rated for Div. 2)/E. Unpleasant Strings.cpp @@ -0,0 +1,147 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2104/E */ + +#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, k; + cin >> n >> k; + + string a; + cin >> a; + + vi dp(n, oo); + vvi ne(n, vi(k, -1)); + + vi pr(k, -1); + + for (int i = n - 1; i >= 0; i--) { + ne[i] = pr; + pr[a[i] - 'a'] = i; + + rep(j, k) { + if (ne[i][j] == -1) { + dp[i] = 0; + break; + } + + rmin(dp[i], dp[ne[i][j]] + 1); + } + } + + int q; + cin >> q; + while (q--) { + string s; + cin >> s; + + int now = pr[s[0] - 'a']; + nrep(i, 1, s.size()) { + if (now == -1) { + break; + } + + now = ne[now][s[i] - 'a']; + } + + cout << (now == -1 ? 0 : dp[now] + 1) << '\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/Educational Codeforces Round 180 (Rated for Div. 2)/D. Reachability and Tree.cpp b/Educational Codeforces Round 180 (Rated for Div. 2)/D. Reachability and Tree.cpp new file mode 100644 index 0000000..81ac681 --- /dev/null +++ b/Educational Codeforces Round 180 (Rated for Div. 2)/D. Reachability and Tree.cpp @@ -0,0 +1,180 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2112/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; + + 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); + } + + V> edges; + pair c1; + pair c2; + int dc = -1; + function dfs = [&](int i, int p, int d) { + bool valid = false; + repv(j, graph[i]) { + if (j == p) { + continue; + } + + valid = valid || graph[i].size() >= 2; + + if (d & 1) { + edges.emplace_back(j + 1, i + 1); + } else { + edges.emplace_back(i + 1, j + 1); + } + + if (dfs(j, i, d + 1 - valid) && dc == -1) { + dc = d; + c1 = {i + 1, j + 1}; + c2 = {j + 1, graph[j][0] == i ? graph[j][1] + 1 : graph[j][0] + 1}; + // edges.pop_back(); + edges.pop_back(); + edges.pop_back(); + } + } + + return valid; + }; + + dfs(0, 0, 0); + if (dc == -1) { + edges.clear(); + dfs(n - 1, n - 1, 0); + if (dc == -1) { + edges.clear(); + dfs(n - 2, n - 2, 0); + if (dc == -1) { + edges.clear(); + dfs(1, 1, 0); + if (dc == -1) { + cout << "NO\n"; + return; + } + } + } + } + + cout << "YES\n"; + + if (dc & 1) { + repv(i, edges) { + swap(i.first, i.second); + } + } + + repv(i, edges) { + cout << i.first << ' ' << i.second << '\n'; + } + cout << c1.first << ' ' << c1.second << '\n'; + cout << c2.first << ' ' << c2.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/Educational Codeforces Round 187 (Rated for Div. 2)/A. Towers of Boxes.cpp b/Educational Codeforces Round 187 (Rated for Div. 2)/A. Towers of Boxes.cpp new file mode 100644 index 0000000..10c8b86 --- /dev/null +++ b/Educational Codeforces Round 187 (Rated for Div. 2)/A. Towers of Boxes.cpp @@ -0,0 +1,111 @@ +/* Problem URL: https://codeforces.com/contest/2203/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, m, d; + cin >> n >> m >> d; + + int f = d / m + 1; + + cout << (n + f - 1) / f << '\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/Educational Codeforces Round 187 (Rated for Div. 2)/B. Beautiful Numbers.cpp b/Educational Codeforces Round 187 (Rated for Div. 2)/B. Beautiful Numbers.cpp new file mode 100644 index 0000000..e855c90 --- /dev/null +++ b/Educational Codeforces Round 187 (Rated for Div. 2)/B. Beautiful Numbers.cpp @@ -0,0 +1,119 @@ +/* Problem URL: https://codeforces.com/contest/2203/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() +{ + string a; + cin >> a; + ll tmp = accumulate(all(a), 0) - '0' * a.size(); + a[0]--; + sortv(a); + + int ans = 0; + while (tmp >= 10) { + tmp -= a.back() - '0'; + a.pop_back(); + ans++; + } + + 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/Educational Codeforces Round 187 (Rated for Div. 2)/C. Test Generator.cpp b/Educational Codeforces Round 187 (Rated for Div. 2)/C. Test Generator.cpp new file mode 100644 index 0000000..4632d18 --- /dev/null +++ b/Educational Codeforces Round 187 (Rated for Div. 2)/C. Test Generator.cpp @@ -0,0 +1,133 @@ +/* Problem URL: https://codeforces.com/contest/2203/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() +{ + ll s, m; + cin >> s >> m; + + ll low = 1; + ll high = OO; + ll ans = -1; + while (low <= high) { + ll mid = (high - low) / 2 + low; + + ll n = s; + for (int i = 62; i >= 0; i--) { + if (((m >> i) & 1) == 0) { + continue; + } + + n -= min(n >> i, mid) << i; + } + + if (n == 0) { + 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/Educational Codeforces Round 187 (Rated for Div. 2)/D. Divisibility Game.cpp b/Educational Codeforces Round 187 (Rated for Div. 2)/D. Divisibility Game.cpp new file mode 100644 index 0000000..1f659be --- /dev/null +++ b/Educational Codeforces Round 187 (Rated for Div. 2)/D. Divisibility Game.cpp @@ -0,0 +1,176 @@ +/* Problem URL: https://codeforces.com/contest/2203/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; + +// constexpr int MAXN = 2e6 + 10; +// vi divs[MAXN]; + +void pre() +{ + // nrep(i, 1, MAXN) { + // for (int j = i; j < MAXN; j += i) { + // divs[j].push_back(i); + // } + // } +} + +#define TEST 1 + +void solve() +{ + int n, m; + cin >> n >> m; + + vi a(n); + cin >> a; + sortv(a); + a.erase(unique(all(a)), a.end()); + + vi c(n + m + 1); + + repv(i, a) { + for (int j = i; j <= n + m; j += i) { + c[j]++; + } + } + + + int p[3] = {0, 0, 0}; + vi t(n + m + 1, -1); + + rep(i, m) { + int now; + cin >> now; + if (t[now] != -1) { + p[t[now]]++; + continue; + } + + int co = c[now]; + + if (co > 0 && a.size() - co > 0) { + p[2]++; + t[now] = 2; + continue; + } + + if (co > 0) { + p[0]++; + t[now] = 0; + continue; + } + + p[1]++; + t[now] = 1; + } + + string win[] = {"Alice", "Bob"}; + + int now = 0; + while (true) { + if (p[2]) { + p[2]--; + now ^= 1; + continue; + } + + if (p[now]) { + p[now]--; + now ^= 1; + continue; + } + + cout << win[now^1] << '\n'; + return; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Pinely Round 3 (Div. 1 + Div. 2)/C. Heavy Intervals.cpp b/Pinely Round 3 (Div. 1 + Div. 2)/C. Heavy Intervals.cpp new file mode 100644 index 0000000..36b222e --- /dev/null +++ b/Pinely Round 3 (Div. 1 + Div. 2)/C. Heavy Intervals.cpp @@ -0,0 +1,135 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1909/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 l(n); + vi r(n); + vl c(n); + cin >> l >> r >> c; + + sortv(l); + set q; + + rep(i, n) { + q.insert(r[i]); + } + + priority_queue pq; + for (int i = n - 1; i >= 0; i--) { + auto itr = q.lower_bound(l[i]); + pq.push(*itr - l[i]); + q.erase(itr); + } + + sortv(c); + ll ans = 0; + rep(i, n) { + ans += c[i] * pq.top(); + pq.pop(); + } + + 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/Refact.ai Match 1 (Codeforces Round 985)/D. Cool Graph.cpp b/Refact.ai Match 1 (Codeforces Round 985)/D. Cool Graph.cpp new file mode 100644 index 0000000..e39aa53 --- /dev/null +++ b/Refact.ai Match 1 (Codeforces Round 985)/D. Cool Graph.cpp @@ -0,0 +1,256 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2029/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, m; + cin >> n >> m; + + vvi graph(n); + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + graph[a].push_back(b); + graph[b].push_back(a); + } + + V vis(n); + V frame(n); + vi cur(n); + + set> rem; + vvi loops; + int end = -1; + + function dfs = [&](int i, int p) { + vis[i] = true; + frame[i] = true; + + for (int &k = cur[i]; k < graph[i].size(); k++) { + int j = graph[i][k]; + + if (j == p) { + continue; + } + + if (rem.count({i, j})) { + continue; + } + + if (frame[j]) { + end = j; + loops.emplace_back(); + loops.back().push_back(i); + frame[i] = false; + vis[i] = false; + rem.emplace(i, j); + rem.emplace(j, i); + return true; + } + + if (vis[j]) { + continue; + } + + if (dfs(j, i)) { + loops.back().push_back(i); + rem.emplace(i, j); + rem.emplace(j, i); + if (end != i) { + frame[i] = false; + vis[i] = false; + return true; + } + } + } + + frame[i] = false; + return false; + }; + + rep(i, n) { + if (vis[i]) { + continue; + } + dfs(i, i); + } + + int c = 0; + + V> ans; + repv(j, loops) { + nrep(i, 2, j.size()) { + ans.emplace_back(j[0] + 1, j[i - 1] + 1, j[i] + 1); + } + } + + auto printans = [&]() { + cout << ans.size() << '\n'; + for (auto [i, j, k] : ans) { + cout << i << ' ' << j << ' ' << k << '\n'; + } + }; + + vvi act(n); + rep(i, n) { + repv(j, graph[i]) { + if (rem.count({i, j})) { + continue; + } + act[i].push_back(j); + c++; + } + } + + if (c == 0) { + printans(); + return; + } + + fill(all(vis), false); + vi g(n); + int gg = 0; + set ev; + + function func = [&](int i) { + vis[i] = true; + g[i] = gg; + + repv(j, act[i]) { + if (vis[j]) { + continue; + } + + func(j); + } + }; + + rep(i, n) { + if (vis[i]) { + continue; + } + func(i); + gg++; + } + + int a1 = -1; + int a2 = -1; + rep(i, n) { + if (act[i].size() > 0) { + a1 = i; + a2 = act[i][0]; + ev.insert(g[i]); + break; + } + } + + rep(i, n) { + if (!ev.count(g[i])) { + ans.emplace_back(a1 + 1, a2 + 1, i + 1); + ev.insert(g[i]); + a1 = i; + } + } + + printans(); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/TODO.md b/TODO.md index 70fece1..0970e3e 100644 --- a/TODO.md +++ b/TODO.md @@ -44,6 +44,14 @@ Official divs from codeforces - [ ] [D1. Removal of a Sequence (Easy Version)](https://codeforces.com/contest/2169/problem/D1) A josephus problem, I'm gonna need to remember how it works to maka this. + + - [ ] [D. Reachability and Tree](https://codeforces.com/problemset/problem/2112/D) + + I know how to solve it, but I got bored in the middle, I'll get back to it eventually. + + - [X] [D. Even String](https://codeforces.com/problemset/problem/2086/D) + + I think I got close, but it's harder than I thought. - Global diff --git a/problemlist.md b/problemlist.md index 1159135..3599701 100644 --- a/problemlist.md +++ b/problemlist.md @@ -28,7 +28,7 @@ A little collection of interesting problems that I randomly decided to do. Another interesting sparse table problem, I'm not sure it needs one, but I solved with one. -- [D. R2D2 and Droid Army](https://codeforces.com/problemset/problem/514/D) +- [D. R3D2 and Droid Army](https://codeforces.com/problemset/problem/514/D) Another sparse table problem. @@ -118,3 +118,9 @@ A little collection of interesting problems that I randomly decided to do. - [https://codeforces.com/problemset/problem/1985/H1](https://codeforces.com/problemset/problem/1985/H1) DSU with rollback problem, cool to think about and not really that difficult. + +## Bridge + +- [F. Non-academic Problem](https://codeforces.com/problemset/problem/1986/F) + + Bridge to compress the graph into a tree.