From 3ac1071724fcc85f008ba4d1d43273b69c7c5f98 Mon Sep 17 00:00:00 2001 From: Segcolt Date: Sun, 26 Apr 2026 22:08:31 -0300 Subject: [PATCH] More stuff --- .../C. Dijkstra?.cpp | 153 ++++++++++++++ .../D. Blackslex and Penguin Civilization.cpp | 121 +++++++++++ .../A. Divisible Permutation.cpp | 119 +++++++++++ Codeforces Round 1077 (Div. 2)/B. Seats.cpp | 134 ++++++++++++ .../A. Lawn Mower.cpp | 108 ++++++++++ .../B. Offshores.cpp | 121 +++++++++++ .../C. Secret message.cpp | 165 +++++++++++++++ .../D. Table Cut.cpp | 155 ++++++++++++++ .../A. Course Wishes.cpp | 132 ++++++++++++ .../C. Interval Mod.cpp | 128 ++++++++++++ Codeforces Round 1093 (Div. 2)/A. Blocked.cpp | 119 +++++++++++ .../B. OIE Excursion.cpp | 127 ++++++++++++ Codeforces Round 1093 (Div. 2)/C. Grid L.cpp | 134 ++++++++++++ .../E. Not Escaping.cpp | 193 ++++++++++++++++++ .../A. A Number Between Two Others.cpp | 111 ++++++++++ .../B. Alternating String.cpp | 153 ++++++++++++++ .../C. Red-Black Pairs.cpp | 122 +++++++++++ .../D. Exceptional Segments.cpp | 133 ++++++++++++ .../J. Jagged Roads.cpp | 139 +++++++++++++ .../D. Valid BFS?.cpp | 151 ++++++++++++++ problemlist.md | 4 + 21 files changed, 2722 insertions(+) create mode 100644 Codeforces Alpha Round 20 (Codeforces format)/C. Dijkstra?.cpp create mode 100644 Codeforces Round 1071 (Div. 3)/D. Blackslex and Penguin Civilization.cpp create mode 100644 Codeforces Round 1077 (Div. 2)/A. Divisible Permutation.cpp create mode 100644 Codeforces Round 1077 (Div. 2)/B. Seats.cpp create mode 100644 Codeforces Round 1078 (Div. 2)/A. Lawn Mower.cpp create mode 100644 Codeforces Round 1078 (Div. 2)/B. Offshores.cpp create mode 100644 Codeforces Round 1078 (Div. 2)/C. Secret message.cpp create mode 100644 Codeforces Round 1078 (Div. 2)/D. Table Cut.cpp create mode 100644 Codeforces Round 1092 (Unrated, Div. 2, Based on THUPC 2026 — Finals)/A. Course Wishes.cpp create mode 100644 Codeforces Round 1092 (Unrated, Div. 2, Based on THUPC 2026 — Finals)/C. Interval Mod.cpp create mode 100644 Codeforces Round 1093 (Div. 2)/A. Blocked.cpp create mode 100644 Codeforces Round 1093 (Div. 2)/B. OIE Excursion.cpp create mode 100644 Codeforces Round 1093 (Div. 2)/C. Grid L.cpp create mode 100644 Codeforces Round 766 (Div. 2)/E. Not Escaping.cpp create mode 100644 Educational Codeforces Round 189 (Rated for Div. 2)/A. A Number Between Two Others.cpp create mode 100644 Educational Codeforces Round 189 (Rated for Div. 2)/B. Alternating String.cpp create mode 100644 Educational Codeforces Round 189 (Rated for Div. 2)/C. Red-Black Pairs.cpp create mode 100644 Educational Codeforces Round 189 (Rated for Div. 2)/D. Exceptional Segments.cpp create mode 100644 IME++ Starters Try-outs 2023/J. Jagged Roads.cpp create mode 100644 Manthan, Codefest 18 (rated, Div. 1 + Div. 2)/D. Valid BFS?.cpp diff --git a/Codeforces Alpha Round 20 (Codeforces format)/C. Dijkstra?.cpp b/Codeforces Alpha Round 20 (Codeforces format)/C. Dijkstra?.cpp new file mode 100644 index 0000000..2aba17e --- /dev/null +++ b/Codeforces Alpha Round 20 (Codeforces format)/C. Dijkstra?.cpp @@ -0,0 +1,153 @@ +/* Problem URL: https://codeforces.com/problemset/problem/20/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, m; + cin >> n >> m; + + V>> graph(n); + + while (m--) { + int a, b, c; + cin >> a >> b >> c; + a--, b--; + graph[a].emplace_back(b, c); + graph[b].emplace_back(a, c); + } + + vi p(n, -1); + vl dis(n, OO); + priority_queue, V>, greater<>> pq; + dis[0] = 0; + pq.emplace(0, 0); + while (!pq.empty()) { + auto [c, i] = pq.top(); + pq.pop(); + + if (c > dis[i]) { + continue; + } + + for (auto [j, w] : graph[i]) { + if (dis[j] > c + w) { + dis[j] = c + w; + p[j] = i; + pq.emplace(dis[j], j); + } + } + } + + if (dis[n - 1] == OO) { + cout << "-1\n"; + return; + } + + vi ans; + int now = n - 1; + while (now != -1) { + ans.push_back(now + 1); + now = p[now]; + } + reverse(all(ans)); + 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 1071 (Div. 3)/D. Blackslex and Penguin Civilization.cpp b/Codeforces Round 1071 (Div. 3)/D. Blackslex and Penguin Civilization.cpp new file mode 100644 index 0000000..efd5645 --- /dev/null +++ b/Codeforces Round 1071 (Div. 3)/D. Blackslex and Penguin Civilization.cpp @@ -0,0 +1,121 @@ +/* Problem URL: https://codeforces.com/contest/2179/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; + + ll cur = (1 << n) - 1; + int lim = __builtin_popcount(cur); + int mod = 0; + rep(i, 1 << n) { + cout << cur + (mod << (__builtin_popcount(cur) + 1)) << ' '; + if (cur > 0 && __builtin_popcount(cur) + __builtin_popcount(mod) >= lim - 1) { + mod = 0; + cur ^= 1 << (31 - __builtin_clz(cur)); + } else { + mod++; + } + } + 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 1077 (Div. 2)/A. Divisible Permutation.cpp b/Codeforces Round 1077 (Div. 2)/A. Divisible Permutation.cpp new file mode 100644 index 0000000..4552c33 --- /dev/null +++ b/Codeforces Round 1077 (Div. 2)/A. Divisible Permutation.cpp @@ -0,0 +1,119 @@ +/* Problem URL: https://codeforces.com/contest/2188/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); + + for (int i = n - 1; i >= 0; i--) { + if ((n - i) & 1) { + p[i] = (n - i + 1) >> 1; + continue; + } + + p[i] = n - ((n - i - 1) >> 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 1077 (Div. 2)/B. Seats.cpp b/Codeforces Round 1077 (Div. 2)/B. Seats.cpp new file mode 100644 index 0000000..a595344 --- /dev/null +++ b/Codeforces Round 1077 (Div. 2)/B. Seats.cpp @@ -0,0 +1,134 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2188/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; + + int i = 0; + while (i < n && a[i] == '0') { + i++; + } + + if (i == n) { + cout << (n + 2) / 3 << '\n'; + return; + } + + int ans = (i + 1) / 3; + int c = 0; + while (i < n) { + if (a[i] == '0') { + c++; + } else { + ans += c / 3; + c = 0; + } + i++; + } + + ans += (c + 1) / 3; + cout << ans + count(all(a), '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 1078 (Div. 2)/A. Lawn Mower.cpp b/Codeforces Round 1078 (Div. 2)/A. Lawn Mower.cpp new file mode 100644 index 0000000..19f30bb --- /dev/null +++ b/Codeforces Round 1078 (Div. 2)/A. Lawn Mower.cpp @@ -0,0 +1,108 @@ +/* Problem URL: https://codeforces.com/contest/2194/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 h, w; + cin >> h >> w; + cout << h - h / w << '\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 1078 (Div. 2)/B. Offshores.cpp b/Codeforces Round 1078 (Div. 2)/B. Offshores.cpp new file mode 100644 index 0000000..57d713f --- /dev/null +++ b/Codeforces Round 1078 (Div. 2)/B. Offshores.cpp @@ -0,0 +1,121 @@ +/* Problem URL: https://codeforces.com/contest/2194/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; + ll x, y; + cin >> n >> x >> y; + + vl a(n); + ll sum = 0; + repv(i, a) { + cin >> i; + sum += i / x * y; + } + + ll ans = 0; + repv(i, a) { + rmax(ans, i + sum - i / x * y); + } + 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 1078 (Div. 2)/C. Secret message.cpp b/Codeforces Round 1078 (Div. 2)/C. Secret message.cpp new file mode 100644 index 0000000..859cd3b --- /dev/null +++ b/Codeforces Round 1078 (Div. 2)/C. Secret message.cpp @@ -0,0 +1,165 @@ +/* Problem URL: https://codeforces.com/contest/2194/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, k; + cin >> n >> k; + + bitset<26> ev; + rep(i, 26) { + ev[i] = true; + } + + V a(k); + cin >> a; + + vi divs; + vi tp; + + for (int i = 1; i * i <= n; i++) { + if (n % i == 0) { + divs.push_back(i); + if (i * i != n) { + tp.push_back(n / i); + } + } + } + for (int i = tp.size() - 1; i >= 0; i--) { + divs.push_back(tp[i]); + } + + string ans(n, ' '); + + auto check = [&](int sz) { + rep(i, sz) { + bitset<26> val = ev; + + for (int j = i; j < n; j += sz) { + bitset<26> cur; + rep(l, k) { + cur[a[l][j] - 'a'] = true; + } + val &= cur; + if (val == 0) { + return false; + } + } + + char ch = 63 - __builtin_clzll(val.to_ullong()) + 'a'; + for (int j = i; j < n; j += sz) { + ans[j] = ch; + } + } + + return true; + }; + + repv(i, divs) { + if (check(i)) { + cout << ans << '\n'; + return; + } + } + + assert(false); +} + +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 1078 (Div. 2)/D. Table Cut.cpp b/Codeforces Round 1078 (Div. 2)/D. Table Cut.cpp new file mode 100644 index 0000000..6562261 --- /dev/null +++ b/Codeforces Round 1078 (Div. 2)/D. Table Cut.cpp @@ -0,0 +1,155 @@ +/* Problem URL: https://codeforces.com/contest/2194/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, m; + cin >> n >> m; + + vvi a(n, vi(m)); + ll c = 0; + repv(i, a) { + repv(j, i) { + cin >> j; + c += j == 1; + } + } + + ll lim = c >> 1; + ll ans = lim * ((c + 1) >> 1); + ll cur = 0; + + int i = n - 1; + int j = 0; + + while (cur < lim) { + cur += a[i][j] == 1; + j++; + if (j >= m) { + j = 0; + i--; + } + } + + string act; + rep(k, i) { + act.push_back('D'); + } + + rep(k, j) { + act.push_back('R'); + } + + act.push_back('D'); + i++; + + nrep(k, j, m) { + act.push_back('R'); + } + + nrep(k, i, n) { + act.push_back('D'); + } + + cout << ans << '\n'; + 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 1092 (Unrated, Div. 2, Based on THUPC 2026 — Finals)/A. Course Wishes.cpp b/Codeforces Round 1092 (Unrated, Div. 2, Based on THUPC 2026 — Finals)/A. Course Wishes.cpp new file mode 100644 index 0000000..5133c9f --- /dev/null +++ b/Codeforces Round 1092 (Unrated, Div. 2, Based on THUPC 2026 — Finals)/A. Course Wishes.cpp @@ -0,0 +1,132 @@ +/* Problem URL: https://codeforces.com/contest/2216/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, k; + cin >> n >> k; + + vi ans; + + vi lim(k); + cin >> lim; + + vi a(n); + cin >> a; + vi p(n); + iota(all(p), 0); + sort(all(p), [&](int i, int j){ + return a[i] > a[j]; + }); + + repv(i, p) { + nrep(j, a[i], k + 1) { + ans.push_back(i + 1); + } + } + + cout << ans.size() << '\n'; + repv(i, ans) { + cout << i << ' '; + } + cout << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1092 (Unrated, Div. 2, Based on THUPC 2026 — Finals)/C. Interval Mod.cpp b/Codeforces Round 1092 (Unrated, Div. 2, Based on THUPC 2026 — Finals)/C. Interval Mod.cpp new file mode 100644 index 0000000..d394338 --- /dev/null +++ b/Codeforces Round 1092 (Unrated, Div. 2, Based on THUPC 2026 — Finals)/C. Interval Mod.cpp @@ -0,0 +1,128 @@ +/* Problem URL: https://codeforces.com/contest/2216/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, k; + ll p, q; + cin >> n >> k >> p >> q; + + vl a(n); + cin >> a; + vvl pref(3, vl(n + 1)); + nrep(i, 1, n + 1) { + pref[0][i] = pref[0][i - 1] + a[i - 1] % p; + pref[1][i] = pref[1][i - 1] + a[i - 1] % q % p; + pref[2][i] = pref[2][i - 1] + min(a[i - 1] % p, a[i - 1] % q % p); + } + + ll ans = OO; + + nrep(i, k - 1, n) { + ll cur = min(pref[0][i + 1] - pref[0][i - k + 1], pref[1][i + 1] - pref[1][i - k + 1]); + ll bf = pref[2][i - k + 1] - pref[2][0]; + ll af = pref[2][n] - pref[2][i + 1]; + rmin(ans, cur + af + bf); + } + + 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 1093 (Div. 2)/A. Blocked.cpp b/Codeforces Round 1093 (Div. 2)/A. Blocked.cpp new file mode 100644 index 0000000..302e7f0 --- /dev/null +++ b/Codeforces Round 1093 (Div. 2)/A. Blocked.cpp @@ -0,0 +1,119 @@ +/* Problem URL: https://codeforces.com/contest/2220/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; + vl a(n); + cin >> a; + sort(all(a), greater<>()); + + nrep(i, 1, n) { + if (a[i] == a[i - 1]) { + cout << "-1\n"; + return; + } + } + + cout << a; +} + +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)/B. OIE Excursion.cpp b/Codeforces Round 1093 (Div. 2)/B. OIE Excursion.cpp new file mode 100644 index 0000000..c36ab17 --- /dev/null +++ b/Codeforces Round 1093 (Div. 2)/B. OIE Excursion.cpp @@ -0,0 +1,127 @@ +/* Problem URL: https://codeforces.com/contest/2220/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; + ll m; + cin >> n >> m; + + vl a(n); + cin >> a; + + int c = 1; + nrep(i, 1, n) { + if (a[i] == a[i - 1]) { + c++; + if (c >= m) { + cout << "NO\n"; + return; + } + continue; + } + + c = 1; + } + + 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 1093 (Div. 2)/C. Grid L.cpp b/Codeforces Round 1093 (Div. 2)/C. Grid L.cpp new file mode 100644 index 0000000..ba66cab --- /dev/null +++ b/Codeforces Round 1093 (Div. 2)/C. Grid L.cpp @@ -0,0 +1,134 @@ +/* Problem URL: https://codeforces.com/contest/2220/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; + + auto test = [&](ll p, ll q) -> ll { + if (p < 0 || q < 0) { + return -1; + } + + ll mi = min(p, q); + p -= mi; + q -= mi; + + if (q || p % 3) { + return -1; + } + + return mi + p / 3; + }; + + ll ans = -1; + rmax(ans, test(p - 4, q)); + rmax(ans, test(p, q - 2)); + rmax(ans, test(p - 2, q - 1)); + if (ans == -1) { + cout << "-1\n"; + return; + } + + cout << "1 " << 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 766 (Div. 2)/E. Not Escaping.cpp b/Codeforces Round 766 (Div. 2)/E. Not Escaping.cpp new file mode 100644 index 0000000..50cc734 --- /dev/null +++ b/Codeforces Round 766 (Div. 2)/E. Not Escaping.cpp @@ -0,0 +1,193 @@ +/* Problem URL: https://codeforces.com/contest/1627/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; + + +void pre() +{ + +} + +#define TEST 1 + +void solve() +{ + int n, m, k; + cin >> n >> m >> k; + + vl x(n); + cin >> x; + + vvi var(n); + + V> edges; + while (k--) { + int a, b, c, d, e; + cin >> a >> b >> c >> d >> e; + a--, b--, c--, d--; + edges.emplace_back(a, b, c, d, e); + var[a].push_back(b); + var[c].push_back(d); + } + + var[0].push_back(0); + + rep(i, n) { + sortv(var[i]); + var[i].erase(unique(all(var[i])), var[i].end()); + } + + vvl dis(n); + V>>> graph(n); + rep(i, n) { + graph[i].resize(var[i].size()); + dis[i].assign(var[i].size(), OO); + } + + for (auto [a, b, c, d, e] : edges) { + b = lower_bound(all(var[a]), b) - var[a].begin(); + d = lower_bound(all(var[c]), d) - var[c].begin(); + + graph[a][b].emplace_back(c, d, e); + } + + priority_queue, V>, greater<>> pq; + dis[0][0] = 0; + pq.emplace(0, 0, 0); + + while (!pq.empty()) { + auto [i, c, j] = pq.top(); + pq.pop(); + + if (c > dis[i][j]) { + continue; + } + + for (auto [u, v, w] : graph[i][j]) { + if (dis[u][v] > c - w) { + dis[u][v] = c - w; + pq.emplace(u, c - w, v); + } + } + + if (j > 0) { + ll dd = (var[i][j] - var[i][j - 1]) * x[i]; + if (dis[i][j - 1] > c + dd) { + dis[i][j - 1] = c + dd; + pq.emplace(i, c + dd, j - 1); + } + } + + if (j < var[i].size() - 1) { + ll dd = (var[i][j + 1] - var[i][j]) * x[i]; + if (dis[i][j + 1] > c + dd) { + dis[i][j + 1] = c + dd; + pq.emplace(i, c + dd, j + 1); + } + } + } + + ll ans = OO; + rep(i, dis[n - 1].size()) { + int v = var[n - 1][i]; + ll dd = (m - v - 1) * x[n - 1] + dis[n - 1][i]; + rmin(ans, dd); + } + + if (ans == OO) { + cout << "NO ESCAPE\n"; + return; + } + + 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 189 (Rated for Div. 2)/A. A Number Between Two Others.cpp b/Educational Codeforces Round 189 (Rated for Div. 2)/A. A Number Between Two Others.cpp new file mode 100644 index 0000000..c9fb05c --- /dev/null +++ b/Educational Codeforces Round 189 (Rated for Div. 2)/A. A Number Between Two Others.cpp @@ -0,0 +1,111 @@ +/* Problem URL: https://codeforces.com/contest/2225/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 mod = y / x; + + cout << (mod > 2 ? "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/Educational Codeforces Round 189 (Rated for Div. 2)/B. Alternating String.cpp b/Educational Codeforces Round 189 (Rated for Div. 2)/B. Alternating String.cpp new file mode 100644 index 0000000..fa2959d --- /dev/null +++ b/Educational Codeforces Round 189 (Rated for Div. 2)/B. Alternating String.cpp @@ -0,0 +1,153 @@ +/* Problem URL: https://codeforces.com/contest/2225/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; + repv(i, a) { + i -= 'a'; + } + + int n = a.size(); + vi one(n); + int now = 0; + rep(i, n) { + one[i] = a[i] == now; + now ^= 1; + } + + vi two(n); + now = 1; + rep(i, n) { + two[i] = a[i] == now; + now ^= 1; + } + + auto check = [&](vi &a) { + int i = 0; + while (i < n && a[i] == 0) { + i++; + } + + if (i >= n) { + return true; + } + + while (i < n && a[i] == 1) { + i++; + } + + while (i < n && a[i] == 0) { + i++; + } + + return i == n; + }; + + if (check(one) || check(two)) { + cout << "YES\n"; + return; + } + + cout << "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/Educational Codeforces Round 189 (Rated for Div. 2)/C. Red-Black Pairs.cpp b/Educational Codeforces Round 189 (Rated for Div. 2)/C. Red-Black Pairs.cpp new file mode 100644 index 0000000..00a41f0 --- /dev/null +++ b/Educational Codeforces Round 189 (Rated for Div. 2)/C. Red-Black Pairs.cpp @@ -0,0 +1,122 @@ +/* Problem URL: https://codeforces.com/contest/2225/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; + + string a[2]; + cin >> a[0] >> a[1]; + + vi dp(n + 1); + + nrep(i, 1, n + 1) { + int p = i - 1; + dp[i] = (a[0][p] != a[1][p]) + dp[i - 1]; + if (i > 1) { + rmin(dp[i], (a[0][p] != a[0][p - 1]) + (a[1][p] != a[1][p - 1]) + dp[i - 2]); + } + } + + cout << dp[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/Educational Codeforces Round 189 (Rated for Div. 2)/D. Exceptional Segments.cpp b/Educational Codeforces Round 189 (Rated for Div. 2)/D. Exceptional Segments.cpp new file mode 100644 index 0000000..292827b --- /dev/null +++ b/Educational Codeforces Round 189 (Rated for Div. 2)/D. Exceptional Segments.cpp @@ -0,0 +1,133 @@ +/* Problem URL: https://codeforces.com/contest/2225/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() +{ + ll n, x; + cin >> n >> x; + + const ll mod = 998244353; + + auto calc = [&](ll n, ll x) { + if (x < 0) { + return 0LL; + } + ll ans = 0; + ll cur = x / 4; + if (cur >= 0) { + ll bf = cur +1; + + ll m = x % 4; + ll up = x + 3 - m; + if (up <= n) { + ll diff = n - up; + diff = diff / 4 + 1; + bf %= mod; + diff %= mod; + ans = (bf * diff) % mod; + } + } + return ans; + }; + + cout << (calc(n, x) + calc(n - 2, x - 2)) % 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/IME++ Starters Try-outs 2023/J. Jagged Roads.cpp b/IME++ Starters Try-outs 2023/J. Jagged Roads.cpp new file mode 100644 index 0000000..a8e3cb6 --- /dev/null +++ b/IME++ Starters Try-outs 2023/J. Jagged Roads.cpp @@ -0,0 +1,139 @@ +/* Problem URL: https://codeforces.com/gym/104415/problem/J */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n, m; + cin >> n >> m; + + V>> graph(n); + while (m--) { + int a, b; + ll c; + cin >> a >> b >> c; + a--, b--; + graph[a].emplace_back(b, log(c) / log(7)); + graph[b].emplace_back(a, log(c) / log(7)); + } + + priority_queue, V>, greater<>> pq; + V dis(n, 1e18L); + dis[0] = 0; + pq.emplace(0, 0); + + while (!pq.empty()) { + auto [c, i] = pq.top(); + pq.pop(); + if (c > dis[i]) { + continue; + } + + for (auto [j, w] : graph[i]) { + if (dis[j] > c + w) { + dis[j] = c + w; + pq.emplace(dis[j], j); + } + } + } + + cout << fixed << setprecision(15) << dis[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/Manthan, Codefest 18 (rated, Div. 1 + Div. 2)/D. Valid BFS?.cpp b/Manthan, Codefest 18 (rated, Div. 1 + Div. 2)/D. Valid BFS?.cpp new file mode 100644 index 0000000..dfac0a0 --- /dev/null +++ b/Manthan, Codefest 18 (rated, Div. 1 + Div. 2)/D. Valid BFS?.cpp @@ -0,0 +1,151 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1037/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 0 + +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); + } + + vi ord(n); + cin >> ord; + + vvi dict(n + 1); + + function dfs = [&](int i, int p) { + repv(j, graph[i]) { + if (j == p) { + continue; + } + + dict[i].push_back(j + 1); + dfs(j, i); + } + sortv(dict[i]); + }; + + dfs(0, 0); + + int cur = 1; + rep(i, n) { + vi tmp; + rep(j, dict[ord[i] - 1].size()) { + tmp.push_back(ord[cur]); + cur++; + } + + sortv(tmp); + if (tmp != dict[ord[i] - 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/problemlist.md b/problemlist.md index 54dd92f..df36aa3 100644 --- a/problemlist.md +++ b/problemlist.md @@ -147,6 +147,10 @@ A little collection of interesting problems that I randomly decided to do. A really easy problem, at least it is once you realize that it's just a tree and the calculation is simple. +- [E. Not Escaping](https://codeforces.com/problemset/problem/1627/E) + + AMAZING graph problem, I actually had fun solving it. + ## Math - [E2. Rudolf and Snowflakes (hard version)](https://codeforces.com/problemset/problem/1846/E2)