diff --git a/2024-2025 ICPC Asia Jakarta Regional Contest (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/C. Saraga.cpp b/2024-2025 ICPC Asia Jakarta Regional Contest (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/C. Saraga.cpp new file mode 100644 index 0000000..28527da --- /dev/null +++ b/2024-2025 ICPC Asia Jakarta Regional Contest (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/C. Saraga.cpp @@ -0,0 +1,146 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2045/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() +{ + string s, t; + cin >> s >> t; + + int in = -1; + int jn = -1; + int ans = oo; + + vi pos(26, -1); + rep(i, t.size() - 1) { + pos[t[i] - 'a'] = i; + } + + nrep(i, 1, s.size()) { + int cur = s[i] - 'a'; + + if (pos[cur] == -1) { + continue; + } + + int c = i + t.size() - pos[cur] + 1; + if (c < ans) { + ans = c; + in = i; + jn = pos[cur]; + } + } + + if (ans == oo) { + cout << "-1\n"; + return; + } + + rep(i, in) { + cout << s[i]; + } + + nrep(j, jn, t.size()) { + cout << t[j]; + } + + 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/2024-2025 ICPC Asia Jakarta Regional Contest (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/M. Mirror Maze.cpp b/2024-2025 ICPC Asia Jakarta Regional Contest (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/M. Mirror Maze.cpp new file mode 100644 index 0000000..a430aff --- /dev/null +++ b/2024-2025 ICPC Asia Jakarta Regional Contest (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/M. Mirror Maze.cpp @@ -0,0 +1,248 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2045/M */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int r, c; + cin >> r >> c; + + V a(r); + cin >> a; + + auto getpos = [&](int i, int j, int k) { + return i * c * 4 + j * 4 + k; + }; + + int tot = 0; + + vvi graph(r * c * 4); + vi ty(r * c * 4, -1); + rep(i, r) { + rep(j, c) { + int n = getpos(i, j, 0); + int e = getpos(i, j, 1); + int s = getpos(i, j, 2); + int w = getpos(i, j, 3); + + if (i > 0) { + int ot = getpos(i - 1, j, 2); + graph[n].push_back(ot); + } + + if (j > 0) { + int ot = getpos(i, j - 1, 1); + graph[w].push_back(ot); + } + + if (i < r - 1) { + int ot = getpos(i + 1, j, 0); + graph[s].push_back(ot); + } + + if (j < c - 1) { + int ot = getpos(i, j + 1, 3); + graph[e].push_back(ot); + } + + if (a[i][j] == '.') { + graph[e].push_back(w); + graph[w].push_back(e); + graph[s].push_back(n); + graph[n].push_back(s); + continue; + } + tot++; + ty[n] = i * c + j; + ty[e] = i * c + j; + ty[s] = i * c + j; + ty[w] = i * c + j; + + if (a[i][j] == '/') { + graph[n].push_back(w); + graph[w].push_back(n); + graph[s].push_back(e); + graph[e].push_back(s); + continue; + } + + graph[n].push_back(e); + graph[e].push_back(n); + graph[w].push_back(s); + graph[s].push_back(w); + } + } + + V vis(r * c * 4); + vi sz(r * c * 4, -1); + + set s; + function dfs = [&](int i) { + vis[i] = true; + if (ty[i] != -1) { + s.insert(ty[i]); + } + + repv(j, graph[i]) { + if (vis[j]) { + continue; + } + + dfs(j); + } + }; + + function dfs2 = [&](int i) { + sz[i] = s.size(); + + repv(j, graph[i]) { + if (sz[j] != -1) { + continue; + } + + dfs2(j); + } + }; + + rep(i, r) { + rep(j, c) { + rep(k, 4) { + int ac = getpos(i, j, k); + if (vis[ac]) { + continue; + } + + dfs(ac); + dfs2(ac); + s.clear(); + } + } + } + + V ans; + rep(i, r) { + int w = getpos(i, 0, 3); + if (sz[w] == tot) { + ans.emplace_back("W" + to_string(i + 1)); + } + int e = getpos(i, c - 1, 1); + if (sz[e] == tot) { + ans.emplace_back("E" + to_string(i + 1)); + } + } + + rep(i, c) { + int n = getpos(0, i, 0); + if (sz[n] == tot) { + ans.emplace_back("N" + to_string(i + 1)); + } + int s = getpos(r - 1, i, 2); + if (sz[s] == tot) { + ans.emplace_back("S" + to_string(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/2025-2026 ICPC, NERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/F. Fragmented Nim.cpp b/2025-2026 ICPC, NERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/F. Fragmented Nim.cpp new file mode 100644 index 0000000..2a8d063 --- /dev/null +++ b/2025-2026 ICPC, NERC, Northern Eurasia Finals (Unrated, Online Mirror, ICPC Rules, Teams Preferred)/F. Fragmented Nim.cpp @@ -0,0 +1,118 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2181/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; + vl a(n); + cin >> a; + + int c = 0; + bool t = false; + rep(i, n) { + c += a[i] == 1; + t = t || a[i] != 1; + } + + cout << (((c & 1) && !t) || ((~c & 1) && t) ? "Alice\n" : "Bob\n"); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 1064 (Div. 1)/B. Marble Council.cpp b/Codeforces Round 1064 (Div. 1)/B. Marble Council.cpp new file mode 100644 index 0000000..91e717f --- /dev/null +++ b/Codeforces Round 1064 (Div. 1)/B. Marble Council.cpp @@ -0,0 +1,138 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2165/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; + + ll mod = 998244353; + + vl c(n); + ll ma = 0; + rep(i, n) { + int a; + cin >> a; + c[a - 1]++; + rmax(ma, c[a - 1]); + } + + vl dp(n + 1); + dp[0] = 1; + + rep(i, n) { + if (c[i] == 0) { + continue; + } + + for (int j = n; j >= c[i]; j--) { + (dp[j] += dp[j - c[i]] * c[i]) %= mod; + } + } + + ll ans = 0; + nrep(i, ma, n + 1) { + (ans += dp[i]) %= 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 966 (Div. 3)/E. Photoshoot for Gorillas.cpp b/Codeforces Round 966 (Div. 3)/E. Photoshoot for Gorillas.cpp new file mode 100644 index 0000000..4de9f7c --- /dev/null +++ b/Codeforces Round 966 (Div. 3)/E. Photoshoot for Gorillas.cpp @@ -0,0 +1,167 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2000/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; + + int w; + cin >> w; + vl a(w); + cin >> a; + + vvl c(n, vl(m)); + rep(i, n - k + 1) { + rep(j, m - k + 1) { + c[i][j]++; + int id = i + k; + int jd = j + k; + + if (id < n) { + c[id][j]--; + } + + if (jd < m) { + c[i][jd]--; + } + + if (id < n && jd < m) { + c[id][jd]++; + } + } + } + + rep(i, n) { + rep(j, m) { + if (i > 0) { + c[i][j] += c[i - 1][j]; + } + + if (j > 0) { + c[i][j] += c[i][j - 1]; + } + + if (i > 0 && j > 0) { + c[i][j] -= c[i - 1][j - 1]; + } + } + } + + vi t; + rep(i, n) { + rep(j, m) { + t.push_back(c[i][j]); + } + } + + sort(all(t), greater<>()); + sort(all(a), greater<>()); + + ll ans = 0; + + rep(i, w) { + ans += a[i] * t[i]; + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 972 (Div. 2)/C. Lazy Narek.cpp b/Codeforces Round 972 (Div. 2)/C. Lazy Narek.cpp new file mode 100644 index 0000000..0fe2247 --- /dev/null +++ b/Codeforces Round 972 (Div. 2)/C. Lazy Narek.cpp @@ -0,0 +1,156 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2005/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; + +bool inv[256]; + +void pre() +{ + inv['n'] = true; + inv['a'] = true; + inv['r'] = true; + inv['e'] = true; + inv['k'] = true; +} + +#define TEST 1 + +string fd = "narek"; + +void solve() +{ + int n, m; + cin >> n >> m; + + V a(n); + cin >> a; + + + vvl memo(n, vl(5, -OO)); + + function dp = [&](int i, int f) { + if (i >= n) { + return (ll)-f; + } + + ll &ans = memo[i][f]; + if (ans != -OO) { + return ans; + } + + int pr = f; + + ans = 0; + repv(j, a[i]) { + if (j == fd[f]) { + ans += 5 * (f == 4); + f = f + 1 - 5 * (f == 4); + continue; + } + + ans -= inv[j]; + } + + ll tmp = -OO; + rmax(tmp, dp(i + 1, f)); + + return ans = max({ans + tmp, ans - f, dp(i + 1, pr)}); + }; + + ll ans = 0; + rep(i, n) { + rmax(ans, dp(i, 0)); + } + + cout << ans << '\n'; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Codeforces Round 984 (Div. 3)/E. Reverse the Rivers.cpp b/Codeforces Round 984 (Div. 3)/E. Reverse the Rivers.cpp new file mode 100644 index 0000000..420a279 --- /dev/null +++ b/Codeforces Round 984 (Div. 3)/E. Reverse the Rivers.cpp @@ -0,0 +1,180 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2036/E */ + +#include +#include +#include + +using namespace std; +using namespace __gnu_pbds; + +template > +using ordered_set = tree; + +#define V vector + +#define rmin(a, b) a = min(a, b) +#define rmax(a, b) a = max(a, b) + +#define rep(i, lim) for (int i = 0; i < (lim); i++) +#define nrep(i, s, lim) for (int i = s; i < (lim); i++) + +#define repv(i, v) for (auto &i : (v)) +#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } +#define sortv(v) sort(v.begin(), v.end()) +#define all(v) (v).begin(), (v).end() + +using vi = vector; +using vvi = vector; +using vvvi = vector; +using vvvvi = vector; + +using ll = long long; + +using vl = vector; +using vvl = vector; +using vvvl = vector; +using vvvvl = vector; + +template +auto operator<<(ostream &os, const vector &vec)->ostream& { + os << vec[0]; + for (size_t i = 1; i < vec.size(); i++) { + os << ' ' << vec[i]; + } + os << '\n'; + return os; +} + +template +auto operator>>(istream &is, vector &vec)->istream& { + for (auto &i : vec) { + is >> i; + } + return is; +} + +template +auto operator<<(ostream &os, const vector> &vec)->ostream& { + for (auto &i : vec) { + os << i[0]; + for (size_t j = 1; j < i.size(); j++) { + os << ' ' << i[j]; + } + os << '\n'; + } + return os; +} + +template +auto operator>>(istream &is, vector> &vec)->istream& { + for (auto &i : vec) { + for (auto &j : i) { + is >> j; + } + } + return is; +} + +const int oo = INT32_MAX >> 1; +const ll OO = INT64_MAX >> 1; + + +void pre() +{ + +} + +#define TEST 0 + +void solve() +{ + int n, m, q; + cin >> n >> m >> q; + + vvl a(n, vl(m)); + cin >> a; + + rep(j, m) { + nrep(i, 1, n) { + a[i][j] |= a[i - 1][j]; + } + } + + while (q--) { + int l = 0; + int r = n - 1; + + int q; + cin >> q; + while (q--) { + int p; + char op; + ll c; + cin >> p >> op >> c; + p--; + + auto findl = [&]() { + int low = l; + int high = n - 1; + int ans = n; + while (low <= high) { + int mid = (low + high) >> 1; + + if (a[mid][p] > c) { + ans = mid; + high = mid - 1; + continue; + } + + low = mid + 1; + } + return ans; + }; + + auto findr = [&]() { + int low = 0; + int high = r; + int ans = -1; + while (low <= high) { + int mid = (low + high) >> 1; + + if (a[mid][p] < c) { + ans = mid; + low = mid + 1; + continue; + } + + high = mid - 1; + } + return ans; + }; + + if (op == '>') { + l = findl(); + } else { + r = findr(); + } + } + + if (l > r) { + cout << "-1\n"; + continue; + } + + cout << l + 1 << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + pre(); + + int t; + (TEST && cin >> t) || (t = 1); + while (t--) { + solve(); + } +} diff --git a/Educational Codeforces Round 144 (Rated for Div. 2)/D. Maximum Subarray.cpp b/Educational Codeforces Round 144 (Rated for Div. 2)/D. Maximum Subarray.cpp new file mode 100644 index 0000000..2a3c34c --- /dev/null +++ b/Educational Codeforces Round 144 (Rated for Div. 2)/D. Maximum Subarray.cpp @@ -0,0 +1,147 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1796/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 + +constexpr int MAXN = 2e5 + 10; +constexpr int MAXK = 30; +ll memo[MAXN][MAXK][4]; + +void solve() +{ + int n, k; + ll x; + cin >> n >> k >> x; + + vl a(n); + cin >> a; + + ll otinf = -OO / 4; + vvvl memo(n, vvl(k + 1, vl(3, -OO))); + + function dp = [&](int i, int j, int s) { + if (j > k) { + return otinf; + } + + if (i >= n) { + return j == k ? 0LL : otinf; + } + + ll &ans = memo[i][j][s]; + if (ans != -OO) { + return ans; + } + + if (s == 0) { + return ans = max({dp(i + 1, j, 0), dp(i + 1, j + 1, 0), + dp(i + 1, j + 1, 1) + a[i] + x, dp(i + 1, j, 1) + a[i] - x}); + } + + if (s == 1) { + return ans = max({dp(i + 1, j, 1) + a[i] - x, dp(i + 1, j + 1, 1) + a[i] + x, + dp(i + 1, j, 2), dp(i + 1, j + 1, 2)}); + } + + return ans = max(dp(i + 1, j + 1, 2), dp(i + 1, j, 2)); + }; + + cout << dp(0, 0, 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/problemlist.md b/problemlist.md index 06b29ce..99389b8 100644 --- a/problemlist.md +++ b/problemlist.md @@ -97,7 +97,18 @@ A little collection of interesting problems that I randomly decided to do. - [D. Jellyfish and Mex](https://codeforces.com/problemset/problem/1875/D) - An interesting problem to thinkm about. + An interesting problem to think about. + +- [C. Lazy Narek](https://codeforces.com/problemset/problem/2005/C) + + I just liked the problem, not particularly hard. + +- [D. Maximum Subarray](https://codeforces.com/problemset/problem/1796/D) + + Interesting to think about the transitions and the states, although figuring out + it's a dp problem is already a challenge by itself. + + ## Graph