From 3174264022ca518501c8602e92a2317ab0164a74 Mon Sep 17 00:00:00 2001 From: Segcolt Date: Sun, 22 Feb 2026 22:39:00 -0300 Subject: [PATCH] More stuff --- ... Bessie's Birthday Cake (Hard Version).cpp | 164 ++++++++++++++++++ .../A. DBMB and the Array.cpp | 121 +++++++++++++ .../B. Reverse a Permutation.cpp | 130 ++++++++++++++ .../C. Replace and Sum.cpp | 127 ++++++++++++++ .../D. Monster Game.cpp | 138 +++++++++++++++ .../E. Alternating String.cpp | 157 +++++++++++++++++ .../D. Satyam and Counting.cpp | 135 ++++++++++++++ 7 files changed, 972 insertions(+) create mode 100644 CodeTON Round 8 (Div. 1 + Div. 2, Rated, Prizes!)/C2. Bessie's Birthday Cake (Hard Version).cpp create mode 100644 Codeforces Round 1076 (Div. 3)/A. DBMB and the Array.cpp create mode 100644 Codeforces Round 1076 (Div. 3)/B. Reverse a Permutation.cpp create mode 100644 Codeforces Round 1076 (Div. 3)/C. Replace and Sum.cpp create mode 100644 Codeforces Round 1076 (Div. 3)/D. Monster Game.cpp create mode 100644 Codeforces Round 970 (Div. 3)/E. Alternating String.cpp create mode 100644 Codeforces Round 971 (Div. 4)/D. Satyam and Counting.cpp diff --git a/CodeTON Round 8 (Div. 1 + Div. 2, Rated, Prizes!)/C2. Bessie's Birthday Cake (Hard Version).cpp b/CodeTON Round 8 (Div. 1 + Div. 2, Rated, Prizes!)/C2. Bessie's Birthday Cake (Hard Version).cpp new file mode 100644 index 0000000..387fe21 --- /dev/null +++ b/CodeTON Round 8 (Div. 1 + Div. 2, Rated, Prizes!)/C2. Bessie's Birthday Cake (Hard Version).cpp @@ -0,0 +1,164 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1942/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, x, y; + cin >> n >> x >> y; + + ll ini = y; + + vi a(x); + cin >> a; + sortv(a); + + ll ans = x - 2; + + ll even = 0; + vl odds; + nrep(i, 1, x) { + if (a[i] - a[i - 1] - 1 <= 1) { + ans += a[i] - a[i - 1] - 1; + continue; + } + + ll g = a[i] - a[i - 1] - 1; + if (g & 1) { + odds.push_back(g >> 1); + continue; + } + + even += g >> 1; + } + + ll g = n - (a.back() - a[0] + 1); + + if (g <= 1) { + ans += g; + } else if (g & 1) { + odds.push_back(g >> 1); + } else { + even += g >> 1; + } + + + sortv(odds); + + repv(i, odds) { + if (y >= i) { + y -= i; + ans += i + 1; + continue; + } + + ans += y; + y = 0; + break; + } + + ll ev = min(even, (ll)y); + y -= ev; + ans += ev; + ll us = ini - y; + ans += us; + 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 1076 (Div. 3)/A. DBMB and the Array.cpp b/Codeforces Round 1076 (Div. 3)/A. DBMB and the Array.cpp new file mode 100644 index 0000000..2e8b275 --- /dev/null +++ b/Codeforces Round 1076 (Div. 3)/A. DBMB and the Array.cpp @@ -0,0 +1,121 @@ +/* Problem URL: https://codeforces.com/contest/2193/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, s, x; + cin >> n >> s >> x; + + int cur = 0; + while (n--) { + int x; + cin >> x; + cur += x; + } + + if (cur > s || (cur - s) % x) { + 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 1076 (Div. 3)/B. Reverse a Permutation.cpp b/Codeforces Round 1076 (Div. 3)/B. Reverse a Permutation.cpp new file mode 100644 index 0000000..1e5ba96 --- /dev/null +++ b/Codeforces Round 1076 (Div. 3)/B. Reverse a Permutation.cpp @@ -0,0 +1,130 @@ +/* Problem URL: https://codeforces.com/contest/2193/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 p(n); + cin >> p; + int cur = n; + int inv = n; + rep(i, n) { + if (p[i] == cur) { + cur--; + continue; + } + + inv = i; + break; + } + + int l = inv; + while (l < n && p[l] != cur) { + l++; + } + + reverse(p.begin() + inv, p.begin() + l + 1); + + 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 1076 (Div. 3)/C. Replace and Sum.cpp b/Codeforces Round 1076 (Div. 3)/C. Replace and Sum.cpp new file mode 100644 index 0000000..2964513 --- /dev/null +++ b/Codeforces Round 1076 (Div. 3)/C. Replace and Sum.cpp @@ -0,0 +1,127 @@ +/* Problem URL: https://codeforces.com/contest/2193/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, q; + cin >> n >> q; + vl a(n); + vl b(n); + cin >> a >> b; + rmax(a[n - 1], b[n - 1]); + + for (int i = n - 2; i >= 0; i--) { + a[i] = max({a[i], a[i + 1], b[i]}); + } + + vl pref(n + 1); + nrep(i, 1, n + 1) { + pref[i] = pref[i - 1] + a[i - 1]; + } + + while (q--) { + int l, r; + cin >> l >> r; + cout << pref[r] - pref[l - 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/Codeforces Round 1076 (Div. 3)/D. Monster Game.cpp b/Codeforces Round 1076 (Div. 3)/D. Monster Game.cpp new file mode 100644 index 0000000..d049410 --- /dev/null +++ b/Codeforces Round 1076 (Div. 3)/D. Monster Game.cpp @@ -0,0 +1,138 @@ +/* Problem URL: https://codeforces.com/contest/2193/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; + vl a(n); + vi b(n); + cin >> a >> b; + + priority_queue s; + repv(i, a) { + s.push(i); + } + + ll best = OO; + + ll ans = 0; + rep(i, n) { + while (b[i]) { + if (s.empty()) { + break; + } + + rmin(best, s.top()); + s.pop(); + b[i]--; + } + + if (b[i]) { + break; + } + + rmax(ans, (i + 1) * best); + } + + 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 970 (Div. 3)/E. Alternating String.cpp b/Codeforces Round 970 (Div. 3)/E. Alternating String.cpp new file mode 100644 index 0000000..d117f0a --- /dev/null +++ b/Codeforces Round 970 (Div. 3)/E. Alternating String.cpp @@ -0,0 +1,157 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2008/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; + string a; + cin >> n >> a; + + if (a.size() & 1) { + vvvi suf(n + 1, vvi(26, vi(2))); + for (int i = n - 1; i >= 0; i--) { + suf[i][a[i] - 'a'][i & 1]++; + + rep(j, 26) { + suf[i][j][0] += suf[i + 1][j][0]; + suf[i][j][1] += suf[i + 1][j][1]; + } + } + + int c = n >> 1; + + int ans = oo; + + vvi pref(26, vi(2)); + rep(i, n) { + int mi0 = oo; + int mi1 = oo; + rep(j, 26) { + rmin(mi0, c - pref[j][0] - suf[i + 1][j][1]); + rmin(mi1, c - pref[j][1] - suf[i + 1][j][0]); + } + + rmin(ans, mi0 + mi1); + + pref[a[i] - 'a'][i & 1]++; + } + + cout << ans + 1 << '\n'; + return; + } + + vvi c(26, vi(2)); + rep(i, n) { + c[a[i] - 'a'][i & 1]++; + } + + int cc = n >> 1; + + int mi0 = oo; + int mi1 = oo; + rep(j, 26) { + rmin(mi0, cc - c[j][0]); + rmin(mi1, cc - c[j][1]); + } + + cout << mi0 + mi1 << '\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)/D. Satyam and Counting.cpp b/Codeforces Round 971 (Div. 4)/D. Satyam and Counting.cpp new file mode 100644 index 0000000..6a553d4 --- /dev/null +++ b/Codeforces Round 971 (Div. 4)/D. Satyam and Counting.cpp @@ -0,0 +1,135 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2009/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + V> p(2, V(n + 1)); + + rep(i, n) { + int x, y; + cin >> x >> y; + p[y][x] = true; + } + + ll ans = 0; + n++; + + rep(i, n) { + if (p[0][i] && p[1][i]) { + ans += n - 3; + } + + if (i > 0 && i < n - 1) { + if (p[0][i] && p[1][i - 1] && p[1][i + 1]) { + ans++; + } + + if (p[1][i] && p[0][i - 1] && p[0][i + 1]) { + 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(); + } +}