From 97658bef82f7735a67d979cdc773ec2f5f6dc7de Mon Sep 17 00:00:00 2001 From: Segcolt Date: Tue, 28 Apr 2026 13:14:58 -0300 Subject: [PATCH] More problems --- .../K. Kim's Quest.cpp | 182 ++++++++++++++++++ .../C. PolandBall and Forest.cpp | 121 ++++++++++++ .../E. The Robotic Rush.cpp | 160 +++++++++++++++ .../D1. Unique Values (Easy version).cpp | 164 ++++++++++++++++ .../E. Making Anti-Palindromes.cpp | 167 ++++++++++++++++ .../F. Sum and Product.cpp | 141 ++++++++++++++ .../F. Shift and Reverse.cpp | 159 +++++++++++++++ .../C. Beautiful Triple Pairs.cpp | 132 +++++++++++++ .../E1. The Game (Easy Version).cpp | 164 ++++++++++++++++ .../D. Wonderful Lightbulbs.cpp | 133 +++++++++++++ 10 files changed, 1523 insertions(+) create mode 100644 2023-2024 ICPC, NERC, Northern Eurasia Onsite (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/K. Kim's Quest.cpp create mode 100644 8VC Venture Cup 2017 - Elimination Round/C. PolandBall and Forest.cpp create mode 100644 Codeforces Round 1074 (Div. 4)/E. The Robotic Rush.cpp create mode 100644 Codeforces Round 1093 (Div. 2)/D1. Unique Values (Easy version).cpp create mode 100644 Codeforces Round 867 (Div. 3)/E. Making Anti-Palindromes.cpp create mode 100644 Codeforces Round 891 (Div. 3)/F. Sum and Product.cpp create mode 100644 Codeforces Round 913 (Div. 3)/F. Shift and Reverse.cpp create mode 100644 Codeforces Round 946 (Div. 3)/C. Beautiful Triple Pairs.cpp create mode 100644 Ethflow Round 1 (Codeforces Round 1001, Div. 1 + Div. 2)/E1. The Game (Easy Version).cpp create mode 100644 Neowise Labs Contest 1 (Codeforces Round 1018, Div. 1 + Div. 2)/D. Wonderful Lightbulbs.cpp diff --git a/2023-2024 ICPC, NERC, Northern Eurasia Onsite (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/K. Kim's Quest.cpp b/2023-2024 ICPC, NERC, Northern Eurasia Onsite (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/K. Kim's Quest.cpp new file mode 100644 index 0000000..c08dd60 --- /dev/null +++ b/2023-2024 ICPC, NERC, Northern Eurasia Onsite (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/K. Kim's Quest.cpp @@ -0,0 +1,182 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1912/K */ + +#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 = 2e5 + 10; +ll fact[MAXN]; +ll inv[MAXN]; +ll invf[MAXN]; + +ll comb(ll n, ll k) { + return fact[n] * invf[k] % mod * invf[n - k] % mod; +} + +void pre() +{ + fact[0] = 1; + nrep(i, 1, MAXN) { + fact[i] = fact[i - 1] * i % mod; + } + + inv[1] = 1; + nrep(i, 2, MAXN) { + inv[i] = (mod - mod / i) * inv[mod % i] % mod; + } + + invf[0] = 1; + nrep(i, 1, MAXN) { + invf[i] = invf[i - 1] * inv[i] % mod; + } +} + +#define TEST 0 + +void solve() +{ + int n; + cin >> n; + vl a(n); + ll c = 0; + repv(i, a) { + cin >> i; + c += ~i & 1; + } + + ll ans = 0; + nrep(i, 3, c + 1) { + (ans += comb(c, i)) %= mod; + } + int seq[] = {1, 0, 1}; + + vvvl memo(n, vvl(3, vl(2, -1))); + auto init = [&]() { + rep(i, n) { + rep(j, 3) { + rep(k, 2) { + memo[i][j][k] = -1; + } + } + } + }; + + function dp = [&](int i, int c, int val) { + if (i >= n) { + return 0LL; + } + + ll &ans = memo[i][c][val]; + if (ans != -1) { + return ans; + } + + ans = 0; + if (seq[c] == (a[i] & 1)) { + (ans += dp(i + 1, (c + 1) % 3, val || (c == 2)) + (val || (c == 2))) %= mod; + } + (ans += dp(i + 1, c, val)) %= mod; + return ans; + }; + + (ans += dp(0, 0, 0)) %= mod; + seq[0] = 0; + seq[1] = 1; + init(); + (ans += dp(0, 0, 0)) %= mod; + seq[0] = 1; + seq[2] = 0; + init(); + (ans += dp(0, 0, 0)) %= 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/8VC Venture Cup 2017 - Elimination Round/C. PolandBall and Forest.cpp b/8VC Venture Cup 2017 - Elimination Round/C. PolandBall and Forest.cpp new file mode 100644 index 0000000..e4c185f --- /dev/null +++ b/8VC Venture Cup 2017 - Elimination Round/C. PolandBall and Forest.cpp @@ -0,0 +1,121 @@ +/* Problem URL: https://codeforces.com/contest/755/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 0 + +void solve() +{ + int n; + cin >> n; + set s; + int ans = 0; + rep(i, n) { + int a; + cin >> a; + if (a == i + 1) { + ans++; + continue; + } + + s.insert(a); + } + + cout << ans + (s.size() + 1) / 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 1074 (Div. 4)/E. The Robotic Rush.cpp b/Codeforces Round 1074 (Div. 4)/E. The Robotic Rush.cpp new file mode 100644 index 0000000..5e2a780 --- /dev/null +++ b/Codeforces Round 1074 (Div. 4)/E. The Robotic Rush.cpp @@ -0,0 +1,160 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2185/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, m, q; + cin >> n >> m >> q; + + vl a(n); + vl b(m); + cin >> a >> b; + sortv(b); + + string c; + cin >> c; + + V> mov(q + 1); + int now = 0; + nrep(i, 1, q + 1) { + char cur = c[i - 1]; + mov[i] = mov[i - 1]; + now += (cur == 'R') - (cur == 'L'); + rmin(mov[i].first, now); + rmax(mov[i].second, now); + } + + vi ans(q + 1); + ans[0] = n; + rep(i, n) { + int low = 0; + int high = q + 1; + while (low < high) { + int mid = (low + high) >> 1; + + ll posl = a[i] + mov[mid].first; + ll posr = a[i] + mov[mid].second; + + auto itr = lower_bound(all(b), posl); + auto itr2 = upper_bound(all(b), posr); + + if (((itr == b.end()) || (*itr < posl || *itr > a[i])) && ((itr2 == b.begin()) || (*prev(itr2) > posr || *prev(itr2) < a[i]))) { + low = mid + 1; + continue; + } + + high = mid; + } + + if (low == q + 1) { + continue; + } + + ans[low]--; + } + + nrep(i, 1, q + 1) { + ans[i] += ans[i - 1]; + cout << ans[i] << ' '; + } + cout << endl; +} + +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 1093 (Div. 2)/D1. Unique Values (Easy version).cpp b/Codeforces Round 1093 (Div. 2)/D1. Unique Values (Easy version).cpp new file mode 100644 index 0000000..5a37891 --- /dev/null +++ b/Codeforces Round 1093 (Div. 2)/D1. Unique Values (Easy version).cpp @@ -0,0 +1,164 @@ +/* Problem URL: https://codeforces.com/contest/2220/problem/D1 */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + + int sz = n * 2 + 1; + + vi p(sz + 1, -1); + auto calc = [&](int mid) { + if (p[mid] != -1) { + return p[mid]; + } + cout << "? " << mid; + nrep(i, 1, mid + 1) { + cout << ' ' << i; + } + cout << endl; + int s; + cin >> s; + + cout << "? " << sz - mid; + nrep(i, mid + 1, sz + 1) { + cout << ' ' << i; + } + cout << endl; + int t; + cin >> t; + + if (s > t) { + return p[mid] = 1; + } + + if (t > s) { + return p[mid] = 2; + } + + return p[mid] = ((mid - s) & 1) * 3; + }; + + auto ans = [&](int i, int j, int k) { + cout << "! " << i << ' ' << j << ' ' << k << endl; + return; + }; + + auto s = [&](int t) { + int low = 1; + int high = sz; + while (low < high) { + int mid = (low + high) >> 1; + if (calc(mid) >= t) { + high = mid; + continue; + } + + low = mid + 1; + } + + return low; + }; + + ans(s(1), s(2), s(3)); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 867 (Div. 3)/E. Making Anti-Palindromes.cpp b/Codeforces Round 867 (Div. 3)/E. Making Anti-Palindromes.cpp new file mode 100644 index 0000000..04ec60f --- /dev/null +++ b/Codeforces Round 867 (Div. 3)/E. Making Anti-Palindromes.cpp @@ -0,0 +1,167 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1822/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 (n & 1) { + cout << "-1\n"; + return; + } + + V c(26); + repv(i, a) { + c[i - 'a']++; + } + + rep(i, 26) { + if (c[i] > (n >> 1)) { + cout << "-1\n"; + return; + } + } + + multiset> c2; + fill(all(c), 0); + rep(i, n >> 1) { + if (a[i] == a[n - i - 1]) { + c[a[i] - 'a']++; + } + } + rep(i, 26) { + if (c[i] > 0) { + c2.insert(c[i]); + } + } + + int ans = 0; + while (c2.size() > 1) { + auto itr = c2.begin(); + auto itr2 = next(itr); + + int ca = *itr; + int cb = *itr2; + c2.erase(itr); + c2.erase(itr2); + + ans++; + ca--; + cb--; + + if (ca > 0) { + c2.insert(ca); + } + + if (cb > 0) { + c2.insert(cb); + } + } + + if (!c2.empty()) { + ans += *c2.begin(); + } + + 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 891 (Div. 3)/F. Sum and Product.cpp b/Codeforces Round 891 (Div. 3)/F. Sum and Product.cpp new file mode 100644 index 0000000..59085ba --- /dev/null +++ b/Codeforces Round 891 (Div. 3)/F. Sum and Product.cpp @@ -0,0 +1,141 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1857/F */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + map c; + rep(i, n) { + ll a; + cin >> a; + c[a]++; + } + + int q; + cin >> q; + while (q--) { + ll x, y; + cin >> x >> y; + + ll d = x*x - 4 * y; + if (d < 0) { + cout << "0 "; + continue; + } + + ll r1 = ceill((x - sqrtl(d)) / 2); + ll r2 = (x + sqrtl(d)) / 2; + if (r1 + r2 != x || r1 * r2 != y) { + cout << "0 "; + continue; + } + + if (r1 == r2) { + cout << c[r1] * (c[r1] - 1) / 2 << ' '; + continue; + } + + cout << c[r1] * c[r2] << ' '; + } + 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 913 (Div. 3)/F. Shift and Reverse.cpp b/Codeforces Round 913 (Div. 3)/F. Shift and Reverse.cpp new file mode 100644 index 0000000..cd8c672 --- /dev/null +++ b/Codeforces Round 913 (Div. 3)/F. Shift and Reverse.cpp @@ -0,0 +1,159 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1907/F */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + map c; + vl a(n); + repv(i, a) { + cin >> i; + c[i]++; + } + + if (c.size() == 1) { + cout << "0\n"; + return; + } + + auto calcans = [&](vl &a, int val) { + ll b = prev(c.end())->first; + + int k = 0; + while (a[k] != b) { + k++; + } + + while (k < n && a[k] == b) { + k++; + } + + int c = min(n - k, k + val * 2); + vl act; + nrep(i, k, n) { + act.push_back(a[i]); + } + + rep(i, k) { + act.push_back(a[i]); + } + + rep(i, n - 1) { + if (act[i] > act[i + 1]) { + return oo; + } + } + + return c; + }; + + int cur = calcans(a, 1); + reverse(all(a)); + rmin(cur, calcans(a, 0) + 1); + + if (cur >= oo) { + cout << "-1\n"; + return; + } + cout << cur << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 946 (Div. 3)/C. Beautiful Triple Pairs.cpp b/Codeforces Round 946 (Div. 3)/C. Beautiful Triple Pairs.cpp new file mode 100644 index 0000000..19dd846 --- /dev/null +++ b/Codeforces Round 946 (Div. 3)/C. Beautiful Triple Pairs.cpp @@ -0,0 +1,132 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1974/C */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + vi a(n); + cin >> a; + + ll ans = 0; + map, ll> c1; + map, ll> c2; + map, ll> c3; + map, ll> c4; + rep(i, n - 2) { + int a1 = a[i]; + int a2 = a[i + 1]; + int a3 = a[i + 2]; + + ans += c1[{a1, a2}]; + ans += c2[{a1, a3}]; + ans += c3[{a2, a3}]; + ans -= c4[{a1, a2, a3}] * 3; + + c1[{a1, a2}]++; + c2[{a1, a3}]++; + c3[{a2, a3}]++; + c4[{a1, a2, a3}]++; + } + + 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/Ethflow Round 1 (Codeforces Round 1001, Div. 1 + Div. 2)/E1. The Game (Easy Version).cpp b/Ethflow Round 1 (Codeforces Round 1001, Div. 1 + Div. 2)/E1. The Game (Easy Version).cpp new file mode 100644 index 0000000..d950451 --- /dev/null +++ b/Ethflow Round 1 (Codeforces Round 1001, Div. 1 + Div. 2)/E1. The Game (Easy Version).cpp @@ -0,0 +1,164 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2062/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; + cin >> n; + vl w(n); + ordered_set> c; + priority_queue> pq; + rep(i, n) { + cin >> w[i]; + c.insert({w[i], i}); + pq.emplace(w[i], i); + } + + 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 ac(n); + V>> ev(n); + + function dfs = [&](int i, int p) { + repv(j, graph[i]) { + if (j == p) { + continue; + } + + dfs(j, i); + + if (ev[j].size() > ev[i].size()) { + ev[i].swap(ev[j]); + } + + repv(k, ev[j]) { + ev[i].insert(k); + } + } + + ac[i] = ev[i].size() - ev[i].order_of_key({w[i], oo}); + ev[i].insert({w[i], i}); + }; + + dfs(0, 0); + + while (!pq.empty()) { + auto [wi, i] = pq.top(); + pq.pop(); + + if (ac[i] == c.size() - c.order_of_key({wi, oo})) { + continue; + } + + cout << i + 1 << '\n'; + return; + } + + cout << "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/Neowise Labs Contest 1 (Codeforces Round 1018, Div. 1 + Div. 2)/D. Wonderful Lightbulbs.cpp b/Neowise Labs Contest 1 (Codeforces Round 1018, Div. 1 + Div. 2)/D. Wonderful Lightbulbs.cpp new file mode 100644 index 0000000..546e0c9 --- /dev/null +++ b/Neowise Labs Contest 1 (Codeforces Round 1018, Div. 1 + Div. 2)/D. Wonderful Lightbulbs.cpp @@ -0,0 +1,133 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2096/D */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n; + cin >> n; + V> a(n); + map cx; + map cy; + repv(i, a) { + cin >> i.first >> i.second; + cx[i.first]++; + cy[i.first + i.second]++; + } + + ll x; + repv(i, cx) { + if (i.second & 1) { + x = i.first; + break; + } + } + + ll y; + repv(i, cy) { + if (i.second & 1) { + y = i.first; + break; + } + } + + cout << x << ' ' << y - x << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +}