From bfbe19b7d4dad856599199f62279adffe3686810 Mon Sep 17 00:00:00 2001 From: Segcolt Date: Tue, 24 Mar 2026 17:04:11 -0300 Subject: [PATCH] Add more problems --- .../A1. Dual (Easy Version).cpp | 177 +++++++++++++ .../B. Effects of Anti Pimples.cpp | 144 ++++++++++ ...2. Medium Demon Problem (hard version).cpp | 181 +++++++++++++ .../C. Wonderful City.cpp | 247 ++++++++++++++++++ problemlist.md | 9 + 5 files changed, 758 insertions(+) create mode 100644 Codeforces Round 889 (Div. 1)/A1. Dual (Easy Version).cpp create mode 100644 Codeforces Round 902 (Div. 1, based on COMPFEST 15 - Final Round)/B. Effects of Anti Pimples.cpp create mode 100644 Codeforces Round 993 (Div. 4)/G2. Medium Demon Problem (hard version).cpp create mode 100644 Neowise Labs Contest 1 (Codeforces Round 1018, Div. 1 + Div. 2)/C. Wonderful City.cpp diff --git a/Codeforces Round 889 (Div. 1)/A1. Dual (Easy Version).cpp b/Codeforces Round 889 (Div. 1)/A1. Dual (Easy Version).cpp new file mode 100644 index 0000000..c109874 --- /dev/null +++ b/Codeforces Round 889 (Div. 1)/A1. Dual (Easy Version).cpp @@ -0,0 +1,177 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1854/A1 */ + +#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; + + int mi = oo; + int mii = -1; + int ma = -oo; + int mai = -1; + rep(i, n) { + if (a[i] < mi) { + mi = a[i]; + mii = i; + } + + if (a[i] >= ma) { + ma = a[i]; + mai = i; + } + } + + if (ma == mi) { + cout << "0\n"; + return; + } + + V> ans; + + auto printans = [&]() { + cout << ans.size() << '\n'; + repv(i, ans) { + cout << i.first << ' ' << i.second << '\n'; + } + }; + + if (ma > 0) { + ans.emplace_back(mai + 1, mai + 1); + ans.emplace_back(mai + 1, mai + 1); + ans.emplace_back(mai + 1, mai + 1); + ans.emplace_back(mai + 1, mai + 1); + ans.emplace_back(mai + 1, mai + 1); + ans.emplace_back(n, mai + 1); + ans.emplace_back(n, n); + ans.emplace_back(n, n); + ans.emplace_back(n, n); + + rep(i, n) { + ans.emplace_back(i + 1, n); + ans.emplace_back(n, n); + } + + printans(); + return; + } + + ans.emplace_back(mii + 1, mii + 1); + ans.emplace_back(mii + 1, mii + 1); + ans.emplace_back(mii + 1, mii + 1); + ans.emplace_back(mii + 1, mii + 1); + ans.emplace_back(mii + 1, mii + 1); + ans.emplace_back(mii + 1, mii + 1); + ans.emplace_back(1, mii + 1); + ans.emplace_back(1, 1); + ans.emplace_back(1, 1); + ans.emplace_back(1, 1); + + for (int i = n - 1; i >= 0; i--) { + ans.emplace_back(i + 1, 1); + ans.emplace_back(1, 1); + } + + 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/Codeforces Round 902 (Div. 1, based on COMPFEST 15 - Final Round)/B. Effects of Anti Pimples.cpp b/Codeforces Round 902 (Div. 1, based on COMPFEST 15 - Final Round)/B. Effects of Anti Pimples.cpp new file mode 100644 index 0000000..8ffcb6a --- /dev/null +++ b/Codeforces Round 902 (Div. 1, based on COMPFEST 15 - Final Round)/B. Effects of Anti Pimples.cpp @@ -0,0 +1,144 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1876/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 0 + +void solve() +{ + int n; + cin >> n; + + vi a(n); + cin >> a; + + vl c(2e5); + + rep(i, n) { + for (int j = i + 1; j <= n; j += i + 1) { + rmax(a[i], a[j - 1]); + } + c[a[i]]++; + } + + const ll mod = 998244353; + + auto fpow = [&](ll a, ll p) { + ll ans = 1; + rep(i, 61) { + if ((p >> i) & 1) { + (ans *= a) %= mod; + } + (a *= a) %= mod; + } + return ans; + }; + + ll ans = 0; + for (int i = 2e5 - 1; i >= 0; i--) { + if (c[i] == 0) { + continue; + } + + n -= c[i]; + (ans += ((fpow(2, c[i]) - 1) % mod + mod) % mod * fpow(2, n) % mod * i % mod) %= mod; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 993 (Div. 4)/G2. Medium Demon Problem (hard version).cpp b/Codeforces Round 993 (Div. 4)/G2. Medium Demon Problem (hard version).cpp new file mode 100644 index 0000000..b1bda8c --- /dev/null +++ b/Codeforces Round 993 (Div. 4)/G2. Medium Demon Problem (hard version).cpp @@ -0,0 +1,181 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2044/G2 */ + +#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); + vvi rev(n); + rep(i, n) { + cin >> p[i]; + p[i]--; + rev[p[i]].push_back(i); + } + + V vis(n); + V loop(n); + vvi ne(20, vi(n, -1)); + + rep(i, n) { + ne[0][i] = p[i]; + } + + 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); + } + + ll ans = 0; + function dfs = [&](int i) { + ll ans = 1; + repv(j, rev[i]) { + ans += dfs(j); + } + return ans; + }; + + rep(i, n) { + if (!loop[i]) { + continue; + } + + repv(j, rev[i]) { + if (loop[j]) { + continue; + } + + rmax(ans, dfs(j)); + } + } + + cout << 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/Neowise Labs Contest 1 (Codeforces Round 1018, Div. 1 + Div. 2)/C. Wonderful City.cpp b/Neowise Labs Contest 1 (Codeforces Round 1018, Div. 1 + Div. 2)/C. Wonderful City.cpp new file mode 100644 index 0000000..75495ab --- /dev/null +++ b/Neowise Labs Contest 1 (Codeforces Round 1018, Div. 1 + Div. 2)/C. Wonderful City.cpp @@ -0,0 +1,247 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2096/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; + + vvl h(n, vl(n)); + cin >> h; + + V>> graph(n * 2); + + auto addedge = [&](int u, int v, ll c) + { + graph[u].emplace_back(v, c); + }; + + vl a(n); + vl b(n); + cin >> a >> b; + + vl memo(n * 2, -1); + + function dfs = [&](int i) { + if (i >= (n - 1) * 2) { + return 0LL; + } + + ll &ans = memo[i]; + if (ans != -1) { + return ans; + } + + ans = OO; + + repv(j, graph[i]) { + ll tmp = dfs(j.first); + if (tmp == OO) { + continue; + } + + rmin(ans, tmp + j.second); + } + + return ans; + }; + + rep(j, n - 1) { + int pos = (1 << 4) - 1; + int msk = (1 << 5) - 1; + rep(i, n) { + if (h[i][j] == h[i][j + 1]) { + pos &= msk ^ 1; + pos &= msk ^ (1 << 3); + continue; + } + + if (h[i][j] == h[i][j + 1] - 1) { + pos &= msk ^ (1 << 2); + continue; + } + + if (h[i][j] - 1 == h[i][j + 1]) { + pos &= msk ^ (1 << 1); + } + } + + if (pos == 0) { + cout << "-1\n"; + return; + } + + if (pos & 1) { + addedge(j * 2, (j + 1) * 2, 0); + } + + if (pos & 2) { + addedge(j * 2, (j + 1) * 2 + 1, b[j + 1]); + } + + if (pos & 4) { + addedge(j * 2 + 1, (j + 1) * 2, 0); + } + + if (pos & 8) { + addedge(j * 2 + 1, (j + 1) * 2 + 1, b[j + 1]); + } + } + + ll ans = min(dfs(0), dfs(1) + b[0]); + if (ans >= OO) { + cout << "-1\n"; + return; + } + graph.assign(n * 2, V>()); + fill(all(memo), -1); + + rep(i, n - 1) { + int pos = (1 << 4) - 1; + int msk = (1 << 5) - 1; + rep(j, n) { + if (h[i][j] == h[i + 1][j]) { + pos &= msk ^ 1; + pos &= msk ^ (1 << 3); + continue; + } + + if (h[i][j] == h[i + 1][j] - 1) { + pos &= msk ^ (1 << 2); + continue; + } + + if (h[i][j] - 1 == h[i + 1][j]) { + pos &= msk ^ (1 << 1); + } + } + + if (pos == 0) { + cout << "-1\n"; + return; + } + + if (pos & 1) { + addedge(i * 2, (i + 1) * 2, 0); + } + + if (pos & 2) { + addedge(i * 2, (i + 1) * 2 + 1, a[i + 1]); + } + + if (pos & 4) { + addedge(i * 2 + 1, (i + 1) * 2, 0); + } + + if (pos & 8) { + addedge(i * 2 + 1, (i + 1) * 2 + 1, a[i + 1]); + } + } + + ll tmp = min(dfs(0), dfs(1) + a[0]); + if (tmp >= OO) { + cout << "-1\n"; + return; + } + + cout << ans + tmp << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/problemlist.md b/problemlist.md index 99389b8..98c1647 100644 --- a/problemlist.md +++ b/problemlist.md @@ -116,6 +116,15 @@ A little collection of interesting problems that I randomly decided to do. Cool graph problem, not exactly difficult, but interesting to think about it. +- [C. Wonderful City](https://codeforces.com/problemset/problem/2096/C) + + Amazing graph/dp problem, actually interesting to think about it. + +- [G2. Medium Demon Problem (hard version)](https://codeforces.com/problemset/problem/2044/G2) + + A very simple problem once you finally understand it, interesting to learn about dfs + and all that. + ## Math - [E2. Rudolf and Snowflakes (hard version)](https://codeforces.com/problemset/problem/1846/E2)