diff --git a/.gitignore b/.gitignore index 94d815d..5ea546f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ * !/*/ !*.cpp +!TODO.md diff --git a/2023-2024 ICPC Central Europe Regional Contest (CERC 23)/B. Ball Passing.cpp b/2023-2024 ICPC Central Europe Regional Contest (CERC 23)/B. Ball Passing.cpp new file mode 100644 index 0000000..25f8b52 --- /dev/null +++ b/2023-2024 ICPC Central Europe Regional Contest (CERC 23)/B. Ball Passing.cpp @@ -0,0 +1,119 @@ +/* Problem URL: https://codeforces.com/gym/104871/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; +} + +using ld = long double; + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + string a; + cin >> a; + + V> boys; + V> girls; + + rep(i, n) { + ld x, y; + cin >> x >> y; + + if (a[i] == 'B') { + boys.emplace_back(x, y); + } else { + girls.emplace_back(x, y); + } + } + + auto dis = [&](pair a, pair b) { + ld x = a.first - b.first; + ld y = a.second - b.second; + return sqrt(x * x + y * y); + }; + + ld ans = 0; + rep(i, boys.size() >> 1) { + ans += dis(boys[i], boys[i + (boys.size() >> 1)]); + } + rep(i, girls.size() >> 1) { + ans += dis(girls[i], girls[i + (girls.size() >> 1)]); + } + + cout << setprecision(12) << ans << '\n'; +} diff --git a/2023-2024 ICPC Central Europe Regional Contest (CERC 23)/C. Cakes.cpp b/2023-2024 ICPC Central Europe Regional Contest (CERC 23)/C. Cakes.cpp new file mode 100644 index 0000000..71ca962 --- /dev/null +++ b/2023-2024 ICPC Central Europe Regional Contest (CERC 23)/C. Cakes.cpp @@ -0,0 +1,189 @@ +/* Problem URL: https://codeforces.com/gym/104871/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; +} + +#define int ll +#define INF (INT64_MAX >> 1) +struct dinitz { + const bool scaling = false; // com scaling -> O(nm log(MAXCAP)), + int lim; // com constante alta + struct edge { + int to, cap, rev, flow; + bool res; + edge(int to_, int cap_, int rev_, bool res_) + : to(to_), cap(cap_), rev(rev_), flow(0), res(res_) {} + }; + + vector> g; + vector lev, beg; + ll F; + dinitz(int n) : g(n), F(0) {} + + void add(int a, int b, int c) { + g[a].emplace_back(b, c, g[b].size(), false); + g[b].emplace_back(a, 0, g[a].size()-1, true); + } + bool bfs(int s, int t) { + lev = vector(g.size(), -1); lev[s] = 0; + beg = vector(g.size(), 0); + queue q; q.push(s); + while (q.size()) { + int u = q.front(); q.pop(); + for (auto& i : g[u]) { + if (lev[i.to] != -1 or (i.flow == i.cap)) continue; + if (scaling and i.cap - i.flow < lim) continue; + lev[i.to] = lev[u] + 1; + q.push(i.to); + } + } + return lev[t] != -1; + } + int dfs(int v, int s, int f = INF) { + if (!f or v == s) return f; + for (int& i = beg[v]; i < g[v].size(); i++) { + auto& e = g[v][i]; + if (lev[e.to] != lev[v] + 1) continue; + int foi = dfs(e.to, s, min(f, e.cap - e.flow)); + if (!foi) continue; + e.flow += foi, g[e.to][e.rev].flow -= foi; + return foi; + } + return 0; + } + ll max_flow(int s, int t) { + for (lim = scaling ? (1<<30) : 1; lim; lim /= 2) + while (bfs(s, t)) while (int ff = dfs(s, t)) F += ff; + return F; + } +}; + +signed main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int g, c, t; + cin >> g >> c >> t; + + int size = g + c + t + 2; + + dinitz din(size); + int s = g + c + t; + int si = g + c + t + 1; + + ll total = 0; + + rep(i, c) { + ll c; + cin >> c; + din.add(s, i, c); + total += c; + } + + vl ccost(g); + rep(i, g) { + cin >> ccost[i]; + din.add(i + c, si, INF); + } + + rep(i, t) { + ll cost; + cin >> cost; + din.add(i + g + c, si, cost); + } + + rep(i, c) { + rep(j, g) { + ll act; + cin >> act; + din.add(i, j + c, ccost[j] * act); + } + } + + rep(i, c) { + int n; + cin >> n; + while (n--) { + int j; + cin >> j; + j--; + + din.add(i, j + g + c, INF); + } + } + + cout << total - din.max_flow(s, si) << '\n'; +} diff --git a/2023-2024 ICPC Central Europe Regional Contest (CERC 23)/E. Equal Schedules.cpp b/2023-2024 ICPC Central Europe Regional Contest (CERC 23)/E. Equal Schedules.cpp new file mode 100644 index 0000000..4e55a1d --- /dev/null +++ b/2023-2024 ICPC Central Europe Regional Contest (CERC 23)/E. Equal Schedules.cpp @@ -0,0 +1,128 @@ +/* Problem URL: https://codeforces.com/gym/104871/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + string op; + map guys; + set all; + while (getline(cin, op), op[0] != '-') { + int n, m; + char fds[21]; + sscanf(op.c_str(), "%d%d%s", &n, &m, fds); + + string act = fds; + guys[act] += m - n; + all.insert(act); + } + + map other; + while (getline(cin, op), op[0] != '=') { + int n, m; + char fds[21]; + sscanf(op.c_str(), "%d%d%s", &n, &m, fds); + + string act = fds; + other[act] += m - n; + all.insert(act); + } + + int s = 0; + + for (auto &i : all) { + int diff = other[i] - guys[i]; + if (diff == 0) { + s++; + continue; + } + + cout << i << ' '; + + if (diff > 0) { + cout << "+" << diff << '\n'; + continue; + } + cout << diff << '\n'; + } + + if (s == all.size()) { + cout << "No differences found.\n"; + } +} diff --git a/2023-2024 ICPC Central Europe Regional Contest (CERC 23)/G. Going to the Moon.cpp b/2023-2024 ICPC Central Europe Regional Contest (CERC 23)/G. Going to the Moon.cpp new file mode 100644 index 0000000..0424877 --- /dev/null +++ b/2023-2024 ICPC Central Europe Regional Contest (CERC 23)/G. Going to the Moon.cpp @@ -0,0 +1,263 @@ +/* Problem URL: https://codeforces.com/gym/104871/problem/G */ + +#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; +} + +using ld = long double; + +ld EPS = 1e-16L; + +bool eq(ld a, ld b) { + return abs(a - b) <= EPS; +} + +struct pt { + ld x, y; + pt(ld x = 0, ld y = 0): x(x), y(y) {} + + ld operator^(const pt p) const { + return x*p.y - y*p.x; + } + + pt operator-(const pt p) const { + return {x - p.x, y - p.y}; + } + + ld operator*(const pt p) const { + return x*p.x + y*p.y; + } + + friend istream& operator >> (istream& in, pt &p) { + return in >> p.x >> p.y; + } +}; + +ld dist(pt p, pt q) +{ + return hypot(p.y - q.y, p.x - q.x); +} + +ld sarea(pt p, pt q, pt r) +{ + return ((q-p)^(r-q)) / 2; +} + +struct line { + pt p, q; + line() {} + line (pt p, pt q): p(p), q(q) {} +}; + +bool ccw(pt p, pt q, pt r) +{ + return sarea(p, q, r) > EPS; +} + +ld disttoline(pt p, line r) +{ + return 2 * abs(sarea(p, r.p, r.q)) / dist(r.p, r.q); +} + +bool isinseg(pt p, line r) +{ + pt a = r.p - p, b = r.q - p; + return eq((a^b), 0) and (a * b) < EPS; +} + +bool interseg(line r, line s) +{ + if (isinseg(r.p, s) or isinseg(r.q, s) or isinseg(s.p, r) or isinseg(s.q, r)) { + return true; + } + + return ccw(r.p, r.q, s.p) != ccw(r.p, r.q, s.q) and ccw(s.p, s.q, r.p) != ccw(s.p, s.q, r.q); +} + +ld disttoseg(pt p, line r) +{ + if ((r.q - r.p) * (p - r.p) < 0) { + return dist(r.p, p); + } + + if ((r.p - r.q) * (p - r.q) < 0) { + return dist(r.q, p); + } + + return disttoline(p, r); +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + pt a, b; + pt cir; + ld r; + cin >> a >> b >> cir >> r; + + if (eq(a.x, b.x) && eq(a.y, b.y)) { + ld dista = dist(a, cir); + + if (dista <= r) { + cout << "0\n"; + continue; + } + + dista -= r; + cout << setprecision(12) << (dista - r) * 2 << '\n'; + continue; + } + + ld distret = disttoseg(cir, line(a, b)); + cout << distret << '\n'; + if (distret <= r) { + cout << setprecision(12) << dist(a, b) << '\n'; + continue; + } + + a.x -= cir.x; + a.y -= cir.y; + b.x -= cir.x; + b.y -= cir.y; + + ld low = -r; + ld high = r; + ld ans = 1e18; + ld eps = 1e-9; + while (high - low >= eps) { + ld third = (high - low) / 3; + ld mid1 = low + third; + ld mid2 = high - third; + + auto getans = [&](ld mid) { + ld one = (a.x - mid); + ld two = (a.y - sqrt(r * r - mid * mid)); + ld ac = sqrt(one * one + two * two); + one = (b.x - mid); + two = (b.y - sqrt(r * r - mid * mid)); + ac += sqrt(one * one + two * two); + return ac; + }; + + ld ans1 = getans(mid1); + ld ans2 = getans(mid2); + + if (ans1 <= ans2) { + high = mid2; + ans = ans1; + continue; + } + + low = mid1; + ans = ans2; + } + + low = -r; + high = r; + while (high - low >= eps) { + ld third = (high - low) / 3; + ld mid1 = low + third; + ld mid2 = high - third; + + auto getans = [&](ld mid) { + ld one = (a.x - mid); + ld two = (a.y + sqrt(r * r - mid * mid)); + ld ac = sqrt(one * one + two * two); + one = (b.x - mid); + two = (b.y + sqrt(r * r - mid * mid)); + ac += sqrt(one * one + two * two); + return ac; + }; + + ld ans1 = getans(mid1); + ld ans2 = getans(mid2); + + if (ans1 <= ans2) { + high = mid2; + ans = ans1; + continue; + } + + low = mid1; + ans = ans2; + } + + cout << setprecision(12) << ans << '\n'; + } +} diff --git a/2023-2024 ICPC Central Europe Regional Contest (CERC 23)/H. Human Resources.cpp b/2023-2024 ICPC Central Europe Regional Contest (CERC 23)/H. Human Resources.cpp new file mode 100644 index 0000000..1de2766 --- /dev/null +++ b/2023-2024 ICPC Central Europe Regional Contest (CERC 23)/H. Human Resources.cpp @@ -0,0 +1,207 @@ +/* Problem URL: https://codeforces.com/gym/104871/problem/H */ + +#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; +} + +void encode() +{ + map var; + V inv; + + int prev = -1; + string guy; + + vvi graph; + + V parentless; + + while (cin >> guy) { + if (guy.back() == ':') { + guy.pop_back(); + + if (var.find(guy) == var.end()) { + graph.emplace_back(); + inv.emplace_back(guy); + var[guy] = var.size(); + parentless.push_back(true); + } + + prev = var[guy]; + continue; + } + + if (var.find(guy) == var.end()) { + graph.emplace_back(); + inv.emplace_back(guy); + var[guy] = var.size(); + parentless.push_back(false); + } + + int act = var[guy]; + graph[prev].push_back(act); + parentless[act] = false; + } + + int root = 0; + while (!parentless[root]) { + root++; + } + + string bin; + vi ans; + + function dfs = [&](int i) { + // for (auto j : graph[i]) { + // ans.push_back(j); + // bin.push_back('0'); + // } + + for (auto j : graph[i]) { + dfs(j); + } + + for (auto j : graph[i]) { + bin.push_back('0'); + } + + bin.push_back('1'); + ans.push_back(i); + }; + + dfs(root); + + rep(i, ans.size()) { + cout << inv[ans[i]] << '\n'; + } + // reverse(all(bin)); + cout << bin << '\n'; +} + +void decode() +{ + map var; + V inv; + + string bin; + while (cin >> bin, !isdigit(bin[0])) { + var[bin] = var.size(); + inv.emplace_back(bin); + } + + vvi ans(var.size()); + + stack q; + int i = 0; + + rep(cur, bin.size()) { + if (bin[cur] == '1') { + q.push(i); + i++; + continue; + } + + ans[i].push_back(q.top()); + q.pop(); + } + + i = ans.size(); + while (i--) { + if (ans[i].empty()) { + continue; + } + + cout << inv[i] << ": "; + reverse(all(ans[i])); + repv(j, ans[i]) { + cout << inv[j] << ' '; + } + cout << '\n'; + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + string op; + cin >> op; + + if (op == "ENCODE") { + encode(); + } else { + decode(); + } +} diff --git a/2023-2024 ICPC Central Europe Regional Contest (CERC 23)/L. Labelled Paths.cpp b/2023-2024 ICPC Central Europe Regional Contest (CERC 23)/L. Labelled Paths.cpp new file mode 100644 index 0000000..5d3df0f --- /dev/null +++ b/2023-2024 ICPC Central Europe Regional Contest (CERC 23)/L. Labelled Paths.cpp @@ -0,0 +1,121 @@ +/* Problem URL: https://codeforces.com/gym/104871/problem/L */ + +#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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + string op; + map guys; + set all; + while (getline(cin, op), op[0] != '-') { + int n, m; + char fds[21]; + sscanf(op.c_str(), "%d%d%s", &n, &m, fds); + + string act = fds; + guys[act] += m - n; + all.insert(act); + } + + map other; + while (getline(cin, op), op[0] != '-') { + int n, m; + char fds[21]; + sscanf(op.c_str(), "%d%d%s", &n, &m, fds); + + string act = fds; + other[act] += n - m; + all.insert(act); + } + + for (auto &i : all) { + int diff = guys[i] - other[i]; + if (diff == 0) { + continue; + } + + cout << i << ' '; + + if (diff > 0) { + cout << "+" << diff << '\n'; + continue; + } + cout << diff << '\n'; + } +} diff --git a/2024 Argentinian Programming Tournament (TAP)/B. Period Search.cpp b/2024 Argentinian Programming Tournament (TAP)/B. Period Search.cpp new file mode 100644 index 0000000..345e972 --- /dev/null +++ b/2024 Argentinian Programming Tournament (TAP)/B. Period Search.cpp @@ -0,0 +1,123 @@ +/* Problem URL: https://codeforces.com/gym/105321/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + set divs; + + for (int i = 2; i * i <= n; i++) { + if (n % i == 0) { + if (i * i == n) { + divs.insert(i); + continue; + } + divs.insert(n / i); + divs.insert(i); + } + } + + if (divs.empty()) { + cout << "1\n1 1\n"; + return 0; + } + + vi ans; + while (!divs.empty()) { + int now = *divs.rbegin(); + divs.erase(prev(divs.end())); + + ans.push_back(now); + + for (int i = 2; i * i <= now; i++) { + if (now % i == 0) { + divs.erase(i); + divs.erase(now / i); + } + } + } + + cout << ans.size() << '\n'; + repv(i, ans) { + cout << "1 " << i << '\n'; + } +} diff --git a/2024 Argentinian Programming Tournament (TAP)/J. Never Add Up to X.cpp b/2024 Argentinian Programming Tournament (TAP)/J. Never Add Up to X.cpp new file mode 100644 index 0000000..9014544 --- /dev/null +++ b/2024 Argentinian Programming Tournament (TAP)/J. Never Add Up to X.cpp @@ -0,0 +1,149 @@ +/* Problem URL: https://codeforces.com/gym/105321/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + ll x; + cin >> n >> x; + int lim = x >> 1; + + vl fds(n); + cin >> fds; + + vl one; + vl two; + int c = 0; + + rep(i, n) { + if (fds[i] * 2 == x) { + c++; + continue; + } + + if (fds[i] <= lim) { + one.push_back(fds[i]); + continue; + } + + two.push_back(fds[i]); + } + + sortv(one); + sort(all(two), greater<>()); + + if (!one.empty() && !two.empty() && one.back() + two.front() == x) { + if (one.front() + two.front() != x) { + reverse(all(one)); + } else if (one.back() + two.back() != x) { + reverse(all(two)); + } + } + + if (c > (n + 1) / 2 || (!one.empty() && !two.empty() && one.back() + two.front() == x && c == 0)) { + cout << "*\n"; + return 0; + } + + rep(i, one.size()) { + if (c > 1) { + cout << lim << ' '; + c--; + } + + cout << one[i] << ' '; + } + + if (c) { + cout << lim << ' '; + c--; + } + + rep(i, two.size()) { + cout << two[i] << ' '; + + if (c) { + cout << lim << ' '; + c--; + } + } + + cout << '\n'; +} diff --git a/Codeforces Global Round 29 (Div. 1 + Div. 2)/C. Rabbits.cpp b/Codeforces Global Round 29 (Div. 1 + Div. 2)/C. Rabbits.cpp new file mode 100644 index 0000000..5132f67 --- /dev/null +++ b/Codeforces Global Round 29 (Div. 1 + Div. 2)/C. Rabbits.cpp @@ -0,0 +1,157 @@ +/* Problem URL: https://codeforces.com/contest/2147/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + string tmp; + cin >> n >> tmp; + + repv(i, tmp) { + i -= '0'; + } + + int prev = tmp[0]; + int cur; + for (cur = 0; cur < n; cur++) { + if (prev == 0 && tmp[cur] == 0) { + continue; + } + + if (prev == 0 && tmp[cur] == 1) { + prev = 1; + continue; + } + + if (prev == 1 && tmp[cur] == 0) { + prev = 0; + continue; + } + + break; + } + + if (cur == n) { + cout << "YES\n"; + continue; + } + + int c = 0; + + bool pos = true; + bool exc = false; + + for (; cur < n; cur++) { + if (prev == 0 && tmp[cur] == 0) { + exc = true; + continue; + } + + if (prev == 0 && tmp[cur] == 1) { + prev = 1; + continue; + } + + if (prev == 1 && tmp[cur] == 0) { + c++; + prev = 0; + continue; + } + + if ((c & 1) && !exc) { + pos = false; + break; + } + + prev = 1; + c = 0; + exc = false; + } + + if (prev == 1 && (c & 1) && !exc) { + pos = false; + } + + cout << (pos ? "YES\n" : "NO\n"); + } +} diff --git a/Codeforces Global Round 29 (Div. 1 + Div. 2)/D. Game on Array.cpp b/Codeforces Global Round 29 (Div. 1 + Div. 2)/D. Game on Array.cpp new file mode 100644 index 0000000..feac192 --- /dev/null +++ b/Codeforces Global Round 29 (Div. 1 + Div. 2)/D. Game on Array.cpp @@ -0,0 +1,140 @@ +/* Problem URL: https://codeforces.com/contest/2147/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vl fds(n); + cin >> fds; + + map c; + map> even; + + repv(i, fds) { + if (i & 1) { + c[i]++; + if (i != 1) { + even[i - 1]++; + } + } else { + even[i]++; + } + } + + vl freq; + freq.reserve(c.size()); + + repv(i, c) { + freq.push_back(i.second); + } + + sort(all(freq), greater<>()); + + ll play[2] = {}; + + rep(i, freq.size()) { + play[i & 1] += freq[i]; + } + + auto itr = even.begin(); + + while (itr != even.end()) { + auto ne = next(itr); + ll pro = ne == even.end() ? 0 : ne->first; + ll diff = itr->first - pro; + + play[0] += (diff >> 1) * itr->second; + play[1] += (diff >> 1) * itr->second; + + if (ne != even.end()) { + ne->second += itr->second; + } + + itr = ne; + } + + cout << play[0] << ' ' << play[1] << '\n'; + } +} diff --git a/Codeforces Round 1056 (Div. 2)/A. El fucho.cpp b/Codeforces Round 1056 (Div. 2)/A. El fucho.cpp new file mode 100644 index 0000000..e4062ba --- /dev/null +++ b/Codeforces Round 1056 (Div. 2)/A. El fucho.cpp @@ -0,0 +1,90 @@ +/* Problem URL: https://codeforces.com/contest/2155/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + cout << ((n - 1) << 1) << '\n'; + } +} diff --git a/Codeforces Round 1056 (Div. 2)/B. Abraham's Great Escape.cpp b/Codeforces Round 1056 (Div. 2)/B. Abraham's Great Escape.cpp new file mode 100644 index 0000000..e5031f0 --- /dev/null +++ b/Codeforces Round 1056 (Div. 2)/B. Abraham's Great Escape.cpp @@ -0,0 +1,122 @@ +/* Problem URL: https://codeforces.com/contest/2155/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, k; + cin >> n >> k; + + if (n * n - k == 1) { + cout << "NO\n"; + continue; + } + cout << "YES\n"; + + int lim = n * n - k; + + V ans(n, string(n, 'D')); + + if (lim == 0) { + repv(i, ans) { + cout << i << '\n'; + } + continue; + } + + int c = 1; + ans[0][0] = 'R'; + for (int i = 1; i < n && c < lim; i++, c++) { + ans[0][i] = 'L'; + } + + for (int i = 1; i < n && c < lim; i++) { + for (int j = 0; j < n && c < lim; j++, c++) { + ans[i][j] = 'U'; + } + } + + repv(i, ans) { + cout << i << '\n'; + } + } +} diff --git a/Codeforces Round 1056 (Div. 2)/C. The Ancient Wizards' Capes.cpp b/Codeforces Round 1056 (Div. 2)/C. The Ancient Wizards' Capes.cpp new file mode 100644 index 0000000..26e3d4a --- /dev/null +++ b/Codeforces Round 1056 (Div. 2)/C. The Ancient Wizards' Capes.cpp @@ -0,0 +1,154 @@ +/* Problem URL: https://codeforces.com/contest/2155/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vl fds(n); + cin >> fds; + + if (n == 1) { + cout << (fds[0] == 1 ? "2\n" : "0\n"); + continue; + } + + vi diffs(n - 1); + int maximal = 0; + rep(i, n - 1) { + diffs[i] = fds[i] - fds[i + 1]; + rmax(maximal, abs(diffs[i])); + } + + if (maximal >= 2) { + cout << "0\n"; + continue; + } + + auto findans = [&](int s) { + vi ans(n); + ans[0] = s; + + vvi act(n, vi(2)); + act[0][s] = 1; + + nrep(i, 1, n) { + if (diffs[i - 1] == 0) { + ans[i] = ans[i - 1] ^ 1; + act[i][ans[i]] = 1; + continue; + } + + if (diffs[i - 1] < 0) { + if (ans[i - 1] != 0) { + return 0; + } + act[i][0] = 1; + continue; + } + + if (ans[i - 1] != 1) { + return 0; + } + + ans[i] = 1; + act[i][1] = 1; + } + + nrep(i, 1, n) { + act[i][0] += act[i - 1][0]; + act[n - i - 1][1] += act[n - i][1]; + } + + rep(i, n) { + if (fds[i] != act[i][0] + act[i][1]) { + return 0; + } + } + + return 1; + }; + + cout << findans(0) + findans(1) << '\n'; + } +} diff --git a/Codeforces Round 1057 (Div. 2)/A. Circle of Apple Trees.cpp b/Codeforces Round 1057 (Div. 2)/A. Circle of Apple Trees.cpp new file mode 100644 index 0000000..391d023 --- /dev/null +++ b/Codeforces Round 1057 (Div. 2)/A. Circle of Apple Trees.cpp @@ -0,0 +1,104 @@ +/* Problem URL: https://codeforces.com/contest/2153/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vi fds(n); + cin >> fds; + + sortv(fds); + + int ans = 0; + int prev = -1; + repv(i, fds) { + if (i > prev) { + ans++; + prev = i; + } + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1057 (Div. 2)/B. Bitwise Reversion.cpp b/Codeforces Round 1057 (Div. 2)/B. Bitwise Reversion.cpp new file mode 100644 index 0000000..11485b0 --- /dev/null +++ b/Codeforces Round 1057 (Div. 2)/B. Bitwise Reversion.cpp @@ -0,0 +1,95 @@ +/* Problem URL: https://codeforces.com/contest/2153/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll x, y, z; + cin >> x >> y >> z; + + if ((x & z) == (x & y) && (x & z) == (y & z)) { + cout << "YES\n"; + } else { + cout << "NO\n"; + } + } +} diff --git a/Codeforces Round 1057 (Div. 2)/C. Symmetrical Polygons.cpp b/Codeforces Round 1057 (Div. 2)/C. Symmetrical Polygons.cpp new file mode 100644 index 0000000..3174201 --- /dev/null +++ b/Codeforces Round 1057 (Div. 2)/C. Symmetrical Polygons.cpp @@ -0,0 +1,135 @@ +/* Problem URL: https://codeforces.com/contest/2153/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vl fds(n); + cin >> fds; + + sort(all(fds), greater<>()); + + vl tmp = {fds[0]}; + + ll ans = 0; + + int c = 0; + + nrep(i, 1, n) { + if (!tmp.empty() && tmp.back() == fds[i]) { + ans += tmp.back() * 2; + tmp.pop_back(); + c++; + continue; + } + + tmp.push_back(fds[i]); + } + + if (c == 0) { + cout << "0\n"; + continue; + } + + reverse(all(tmp)); + + ll act = 0; + if (!tmp.empty() && ans > tmp[0]) { + act = tmp[0]; + } + + nrep(i, 1, tmp.size()) { + if (ans + tmp[i - 1] > tmp[i]) { + act = tmp[i - 1] + tmp[i]; + } + } + + if (act == 0 && c == 1) { + cout << "0\n"; + continue; + } + + cout << ans + act << '\n'; + } +} diff --git a/Codeforces Round 1057 (Div. 2)/D. Not Alone.cpp b/Codeforces Round 1057 (Div. 2)/D. Not Alone.cpp new file mode 100644 index 0000000..1f5d580 --- /dev/null +++ b/Codeforces Round 1057 (Div. 2)/D. Not Alone.cpp @@ -0,0 +1,149 @@ +/* Problem URL: https://codeforces.com/contest/2153/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vl fds(n); + cin >> fds; + + vl memo(n, -1); + ll oo = INT64_MAX >> 6; + + function dp = [&](int i) { + if (i < 0) { + return 0LL; + } + + if (i == 0) { + return oo; + } + + ll &ans = memo[i]; + if (ans != -1) { + return ans; + } + + ans = abs(fds[i] - fds[i - 1]) + dp(i - 2); + if (i == 1) { + return ans; + } + + rmin(ans, min({ans, abs(fds[i] - fds[i - 1]) + abs(fds[i - 1] - fds[i - 2]), + abs(fds[i] - fds[i - 2]) + abs(fds[i - 1] - fds[i - 2]), + abs(fds[i] - fds[i - 1]) + abs(fds[i] - fds[i - 2]) + }) + dp(i - 3)); + + return ans; + }; + + ll ans = dp(n - 1); + + fill(all(memo), -1); + + ll tmp = fds.back(); + fds.pop_back(); + fds.insert(fds.begin(), tmp); + + rmin(ans, dp(n - 1)); + + fill(all(memo), -1); + + tmp = fds.back(); + fds.pop_back(); + fds.insert(fds.begin(), tmp); + + rmin(ans, dp(n - 1)); + + fill(all(memo), -1); + + tmp = fds.back(); + fds.pop_back(); + fds.insert(fds.begin(), tmp); + + rmin(ans, dp(n - 1)); + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 1058 (Div. 2)/A. MEX Partition.cpp b/Codeforces Round 1058 (Div. 2)/A. MEX Partition.cpp new file mode 100644 index 0000000..0e8e279 --- /dev/null +++ b/Codeforces Round 1058 (Div. 2)/A. MEX Partition.cpp @@ -0,0 +1,103 @@ +/* Problem URL: https://codeforces.com/contest/2160/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vi a(n); + cin >> a; + + int mex = 0; + V fds(101); + repv(i, a) { + fds[i] = true; + } + + while (fds[mex]) { + mex++; + } + + cout << mex << '\n'; + } +} diff --git a/Codeforces Round 1058 (Div. 2)/B. Distinct Elements.cpp b/Codeforces Round 1058 (Div. 2)/B. Distinct Elements.cpp new file mode 100644 index 0000000..4ed230e --- /dev/null +++ b/Codeforces Round 1058 (Div. 2)/B. Distinct Elements.cpp @@ -0,0 +1,112 @@ +/* Problem URL: https://codeforces.com/contest/2160/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + vl fds(n); + cin >> fds; + + vi ans(n); + ans[0] = 1; + + nrep(i, 1, n) { + ll diff = fds[i] - fds[i - 1]; + if (diff == 1) { + ans[i] = ans[i - 1]; + continue; + } + + if (diff > i) { + ans[i] = i + 1; + continue; + } + + ans[i] = ans[i - diff]; + } + + cout << ans; + } +} diff --git a/Codeforces Round 1058 (Div. 2)/C. Reverse XOR.cpp b/Codeforces Round 1058 (Div. 2)/C. Reverse XOR.cpp new file mode 100644 index 0000000..627217d --- /dev/null +++ b/Codeforces Round 1058 (Div. 2)/C. Reverse XOR.cpp @@ -0,0 +1,142 @@ +/* Problem URL: https://codeforces.com/contest/2160/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + ll n; + cin >> n; + + if (n == 0) { + cout << "YES\n"; + continue; + } + + auto inv = [&](ll i) { + int log = 63 - __builtin_clzll(i); + ll num = 0; + rep(j, log + 1) { + ll bit = (i >> (log - j)) & 1; + num |= bit << j; + } + + return num; + }; + + auto getans = [&]() { + int log = 63 - __builtin_clzll(n); + + nrep(i, log, 61) { + ll now = 0; + + rep(j, (i + 1) >> 1) { + int bitn = (n >> j) & 1; + int bitr = (n >> (i - j)) & 1; + + if (bitn != bitr) { + break; + } + + if (bitn == 1) { + now |= 1LL << (i - j); + continue; + } + + now |= 1LL << (i - j); + now |= 1LL << j; + } + + if (now == 0) { + continue; + } + + if ((now ^ inv(now)) == n) { + return true; + } + } + + return false; + }; + + cout << (getans() ? "YES\n" : "NO\n"); + } +} diff --git a/Codeforces Round 1058 (Div. 2)/D. MAD Interactive Problem.cpp b/Codeforces Round 1058 (Div. 2)/D. MAD Interactive Problem.cpp new file mode 100644 index 0000000..d3f3aa3 --- /dev/null +++ b/Codeforces Round 1058 (Div. 2)/D. MAD Interactive Problem.cpp @@ -0,0 +1,134 @@ +/* Problem URL: https://codeforces.com/contest/2160/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + + int size = n << 1; + + vi ans(size); + + vi zero = {0}; + vi pos; + + auto query = [&](vi &query) { + cout << "? " << query.size() << ' '; + rep(i, query.size()) { + cout << query[i] + 1 << " \n"[i == query.size() - 1]; + } + cout.flush(); + }; + + nrep(i, 1, n << 1) { + zero.push_back(i); + query(zero); + int tmp; + cin >> tmp; + + if (tmp != 0) { + ans[i] = tmp; + zero.pop_back(); + pos.push_back(i); + } + } + + rep(i, n) { + pos.push_back(zero[i]); + query(pos); + + int tmp; + cin >> tmp; + ans[zero[i]] = tmp; + + pos.pop_back(); + } + + cout << "! "; + repv(i, ans) { + cout << i << ' '; + } + cout << endl; + } +} diff --git a/Codeforces Round 282 (Div. 2)/B. Modular Equations.cpp b/Codeforces Round 282 (Div. 2)/B. Modular Equations.cpp new file mode 100644 index 0000000..6128994 --- /dev/null +++ b/Codeforces Round 282 (Div. 2)/B. Modular Equations.cpp @@ -0,0 +1,114 @@ +/* Problem URL: https://codeforces.com/contest/495/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll a, b; + cin >> a >> b; + + if (b == a) { + cout << "infinity\n"; + return 0; + } + + if (b > a) { + cout << "0\n"; + return 0; + } + + ll ans = 0; + ll mod = a - b; + + auto getdivs = [&]() { + for (ll i = 1; i * i <= mod; i++) { + if (mod % i == 0) { + ans += a % i == b; + + if (i * i != mod) { + ans += a % (mod / i) == b; + } + } + } + }; + + getdivs(); + + cout << ans << '\n'; +} diff --git a/Codeforces Round 824 (Div. 2)/D. Meta-set.cpp b/Codeforces Round 824 (Div. 2)/D. Meta-set.cpp new file mode 100644 index 0000000..9a72c1a --- /dev/null +++ b/Codeforces Round 824 (Div. 2)/D. Meta-set.cpp @@ -0,0 +1,156 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1735/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + vvi fds(n, vi(k)); + cin >> fds; + + map pos; + + int prox[3][3]; + prox[0][0] = 0; + prox[0][1] = 2; + prox[0][2] = 1; + prox[1][0] = 2; + prox[1][1] = 1; + prox[1][2] = 0; + prox[2][0] = 1; + prox[2][1] = 0; + prox[2][2] = 2; + + auto convert = [&](vi &a) { + ll now = 0; + ll p = 1; + + repv(i, a) { + now += i * p; + p *= 3; + } + + return now; + }; + + auto getnext = [&](vi &a, vi &b) { + ll now = 0; + ll p = 1; + + rep(i, k) { + now += prox[a[i]][b[i]] * p; + p *= 3; + } + + return now; + }; + + rep(i, n) { + pos[convert(fds[i])] = i; + } + + vi next(k); + + ll ans = 0; + + rep(i, n) { + ll cnt = 0; + // ll prev = 0; + rep(j, n) { + if (j == i) { + continue; + } + auto itr = pos.find(getnext(fds[i], fds[j])); + if (itr == pos.end() || itr->second < j) { + continue; + } + cnt++; + } + + if (cnt < 2) { + continue; + } + + ans += cnt * (cnt - 1) / 2; + } + + cout << ans << '\n'; +} diff --git a/Codeforces Round 848 (Div. 2)/C. Flexible String.cpp b/Codeforces Round 848 (Div. 2)/C. Flexible String.cpp new file mode 100644 index 0000000..872f9aa --- /dev/null +++ b/Codeforces Round 848 (Div. 2)/C. Flexible String.cpp @@ -0,0 +1,131 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1778/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, k; + cin >> n >> k; + + string a, b; + cin >> a >> b; + + map var; + + rep(i, n) { + a[i] -= 'a'; + b[i] -= 'a'; + + if (!var.count(a[i])) { + var[a[i]] = var.size(); + } + } + + vi pos(26); + repv(i, var) { + pos[i.first] = i.second; + } + + ll ans = 0; + rep(mask, 1 << var.size()) { + if (mask > 0 && __builtin_popcount(mask) > k) { + continue; + } + + ll now = 0; + int prev = 0; + + rep(i, n) { + if ((a[i] != b[i]) && !((mask >> (pos[a[i]])) & 1)) { + prev = i + 1; + continue; + } + + now += i - prev + 1; + } + + rmax(ans, now); + } + + cout << ans << '\n'; + } +} diff --git a/Codeforces Round 862 (Div. 2)/C. Place for a Selfie.cpp b/Codeforces Round 862 (Div. 2)/C. Place for a Selfie.cpp new file mode 100644 index 0000000..40fafe4 --- /dev/null +++ b/Codeforces Round 862 (Div. 2)/C. Place for a Selfie.cpp @@ -0,0 +1,146 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1805/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, m; + cin >> n >> m; + + set lines; + while (n--) { + ll now; + cin >> now; + lines.insert(now); + } + + vl para(m); + repv(i, para) { + ll a, b, c; + cin >> a >> b >> c; + + ll lim = 4 * a * c; + auto itr = lines.lower_bound(b); + + ll minimal = INT32_MAX >> 1; + ll ans = 0; + + if (itr != lines.end()) { + if (abs(b - *itr) <= minimal) { + minimal = abs(b - *itr); + ans = *itr; + } + auto tmp = next(itr); + + if (tmp != lines.end()) { + if (abs(b - *tmp) <= minimal) { + minimal = abs(b - *tmp); + ans = *tmp; + } + } + } + + if (itr != lines.begin()) { + itr--; + if (abs(b - *itr) <= minimal) { + minimal = abs(b - *itr); + ans = *itr; + } + + if (itr != lines.begin()) { + itr--; + if (abs(b - *itr) <= minimal) { + minimal = abs(b - *itr); + ans = *itr; + } + } + } + + if (minimal * minimal < lim) { + cout << "YES\n"; + cout << ans << '\n'; + } else { + cout << "NO\n"; + } + } + } +} diff --git a/Codeforces Round 979 (Div. 2)/D. QED's Favorite Permutation.cpp b/Codeforces Round 979 (Div. 2)/D. QED's Favorite Permutation.cpp new file mode 100644 index 0000000..4ac484e --- /dev/null +++ b/Codeforces Round 979 (Div. 2)/D. QED's Favorite Permutation.cpp @@ -0,0 +1,143 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2030/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n, q; + cin >> n >> q; + + vi perm(n); + cin >> perm; + + string a; + cin >> a; + + repv(i, a) { + i = i == 'R'; + } + + vi ma(n); + vi mi(n); + + ma[0] = perm[0]; + mi[n - 1] = perm[n - 1]; + + nrep(i, 1, n) { + ma[i] = max(ma[i - 1], perm[i]); + mi[n - i - 1] = min(mi[n - i], perm[n - i - 1]); + } + + int bad = 0; + rep(i, n - 1) { + if (a[i] == 0 && a[i + 1] == 1) { + bad += ma[i] > mi[i + 1]; + } + } + + while (q--) { + int i; + cin >> i; + i--; + + if (a[i] == 1 && a[i - 1] == 0) { + bad -= ma[i - 1] > mi[i]; + } + + if (a[i] == 0 && a[i + 1] == 1) { + bad -= ma[i] > mi[i + 1]; + } + + a[i] ^= 1; + + if (a[i] == 1 && a[i - 1] == 0) { + bad += ma[i - 1] > mi[i]; + } + + if (a[i] == 0 && a[i + 1] == 1) { + bad += ma[i] > mi[i + 1]; + } + + cout << (bad == 0 ? "YES\n" : "NO\n"); + } + } +} diff --git a/Codeforces Round 983 (Div. 2)/C. Trinity.cpp b/Codeforces Round 983 (Div. 2)/C. Trinity.cpp new file mode 100644 index 0000000..67e5f7d --- /dev/null +++ b/Codeforces Round 983 (Div. 2)/C. Trinity.cpp @@ -0,0 +1,106 @@ +/* Problem URL: https://codeforces.com/problemset/problem/2032/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int t; + cin >> t; + while (t--) { + int n; + cin >> n; + vl a(n); + cin >> a; + + ordered_set> ord; + + sortv(a); + rep(i, n - 1) { + ord.insert({a[i] + a[i + 1], i}); + } + + ll ans = INT32_MAX >> 1; + for (int i = n - 1; i > 1; i--) { + rmin(ans, (ll)ord.order_of_key({a[i], INT32_MAX}) + n - i - 1); + ord.erase({a[i - 1] + a[i - 2], i - 2}); + } + + cout << ans << '\n'; + } +} diff --git a/Educational Codeforces Round 1/E. Chocolate Bar.cpp b/Educational Codeforces Round 1/E. Chocolate Bar.cpp new file mode 100644 index 0000000..a347855 --- /dev/null +++ b/Educational Codeforces Round 1/E. Chocolate Bar.cpp @@ -0,0 +1,124 @@ +/* Problem URL: https://codeforces.com/contest/598/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll dp[31][31][51]; + const ll oo = INT64_MAX >> 1; + + rep(n, 31) { + rep(m, 31) { + rep(k, 51) { + if (k > n * m) { + dp[n][m][k] = oo; + continue; + } + + if (k == 0 || k == n * m) { + dp[n][m][k] = 0; + continue; + } + + dp[n][m][k] = oo; + + nrep(i, 1, n) { + rep(j, k + 1) { + rmin(dp[n][m][k], dp[i][m][j] + dp[n - i][m][k - j] + m * m); + } + } + + nrep(i, 1, m) { + rep(j, k + 1) { + rmin(dp[n][m][k], dp[n][i][j] + dp[n][m - i][k - j] + n * n); + } + } + } + } + } + + int t; + cin >> t; + while (t--) { + int n, m, k; + cin >> n >> m >> k; + + cout << dp[n][m][k] << '\n'; + } +} diff --git a/Educational Codeforces Round 140 (Rated for Div. 2)/D. Playoff.cpp b/Educational Codeforces Round 140 (Rated for Div. 2)/D. Playoff.cpp new file mode 100644 index 0000000..810e326 --- /dev/null +++ b/Educational Codeforces Round 140 (Rated for Div. 2)/D. Playoff.cpp @@ -0,0 +1,110 @@ +/* Problem URL: https://codeforces.com/problemset/problem/1767/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + string s; + cin >> s; + + int d = 1; + int u = 1 << n; + + int cd = 1; + int ud = 1; + + repv(i, s) { + if (i == '1') { + d += cd; + // d += ud >> 1; + cd <<= 1; + continue; + } + + u -= ud; + // u -= cd >> 1; + ud <<= 1; + } + + nrep(i, d, u + 1) { + cout << i << " \n"[i == u]; + } +} diff --git a/Educational Codeforces Round 182 (Rated for Div. 2)/D. Price Tags.cpp b/Educational Codeforces Round 182 (Rated for Div. 2)/D. Price Tags.cpp index f19d75d..10ae351 100644 --- a/Educational Codeforces Round 182 (Rated for Div. 2)/D. Price Tags.cpp +++ b/Educational Codeforces Round 182 (Rated for Div. 2)/D. Price Tags.cpp @@ -87,29 +87,35 @@ int main() ll y; cin >> n >> y; - set divs; vl fds(n); cin >> fds; - auto getdivs = [&](ll v) { - for (ll i = 1; i * i <= v; i++) { - if (v % i == 0) { - divs.insert(i); - divs.insert(v / i); - } + vl count(2e5 + 1); + ll limit = 1; + rep(i, n) { + count[fds[i]]++; + rmax(limit, fds[i]); + } + + nrep(i, 1, (int)2e5 + 1) { + count[i] += count[i - 1]; + } + + ll ans = -INT64_MAX >> 1; + + nrep(x, 2, (int)2e5 + 1) { + ll now = 0; + + for (int i = 1; i <= (int)2e5; i += x) { + int upper = min(i + x - 1, (int)2e5); + int act = (i / x) + 1; + now += (count[upper] - count[i - 1]) * act; + now -= y * max(0LL, (count[upper] - count[i - 1]) - (count[act] - count[act - 1])); } - }; - repv(i, fds) { - getdivs(i); + rmax(ans, now); } - divs.erase(1); - - ll ans = -1e17; - - repv(j, divs) { - - } + cout << ans << '\n'; } } diff --git a/Educational Codeforces Round 3/A. USB Flash Drives.cpp b/Educational Codeforces Round 3/A. USB Flash Drives.cpp new file mode 100644 index 0000000..77acef1 --- /dev/null +++ b/Educational Codeforces Round 3/A. USB Flash Drives.cpp @@ -0,0 +1,98 @@ +/* Problem URL: https://codeforces.com/contest/609/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vl fds(n); + cin >> fds; + sort(all(fds), greater<>()); + + int i = 0; + ll total = 0; + while (total < m) { + total += fds[i]; + i++; + } + + cout << i << '\n'; +} diff --git a/Educational Codeforces Round 3/B. The Best Gift.cpp b/Educational Codeforces Round 3/B. The Best Gift.cpp new file mode 100644 index 0000000..6d401a7 --- /dev/null +++ b/Educational Codeforces Round 3/B. The Best Gift.cpp @@ -0,0 +1,103 @@ +/* Problem URL: https://codeforces.com/contest/609/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vl count(m); + + rep(i, n) { + int now; + cin >> now; + count[now - 1]++; + } + + ll ans = 0; + ll sum = count[0]; + + nrep(i, 1, m) { + ans += count[i] * sum; + sum += count[i]; + } + + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 3/C. Load Balancing.cpp b/Educational Codeforces Round 3/C. Load Balancing.cpp new file mode 100644 index 0000000..3508b2b --- /dev/null +++ b/Educational Codeforces Round 3/C. Load Balancing.cpp @@ -0,0 +1,109 @@ +/* Problem URL: https://codeforces.com/contest/609/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vl a(n); + cin >> a; + + sortv(a); + + ll total = accumulate(all(a), 0LL); + ll lim1 = total / n; + ll lim2 = (total + n - 1) / n; + + ll ans1 = 0; + ll ans2 = 0; + repv(i, a) { + if (i < lim1) { + ans1 += lim1 - i; + continue; + } + + if (i > lim2) { + ans2 += i - lim2; + } + } + + cout << max(ans1, ans2) << '\n'; +} diff --git a/Educational Codeforces Round 3/D. Gadgets for dollars and pounds.cpp b/Educational Codeforces Round 3/D. Gadgets for dollars and pounds.cpp new file mode 100644 index 0000000..d9b1ef7 --- /dev/null +++ b/Educational Codeforces Round 3/D. Gadgets for dollars and pounds.cpp @@ -0,0 +1,200 @@ +/* Problem URL: https://codeforces.com/contest/609/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m, k; + ll s; + cin >> n >> m >> k >> s; + + vl cost[2] = {vl(n), vl(n)}; + cin >> cost[0]; + cin >> cost[1]; + + nrep(i, 1, n) { + rmin(cost[0][i], cost[0][i - 1]); + rmin(cost[1][i], cost[1][i - 1]); + } + + V> gad[2]; + + rep(i, m) { + int t; + ll c; + cin >> t >> c; + + gad[t - 1].emplace_back(c, i); + } + + sortv(gad[0]); + sortv(gad[1]); + + ll sum1 = 0; + ll sum2 = 0; + + int i = 0; + int j = 0; + + for (; i < min((int)gad[0].size(), k); i++) { + sum1 += gad[0][i].first; + } + + for (; j < k - i; j++) { + sum2 += gad[1][j].first; + } + + int oo = INT32_MAX >> 1; + + int ansi = -1; + int ansj = -1; + int ans = oo; + + while (i >= 0 && j <= gad[1].size()) { + int low = 0; + int high = n - 1; + int act = -1; + + while (low <= high) { + int mid = (low + high) >> 1; + + ll now = sum1 * cost[0][mid] + sum2 * cost[1][mid]; + + if (now <= s) { + act = mid; + high = mid - 1; + continue; + } + + low = mid + 1; + } + + if (act == -1) { + if (j == gad[1].size()) { + break; + } + i--; + sum1 -= gad[0][i].first; + sum2 += gad[1][j].first; + j++; + continue; + } + + if (act < ans) { + ans = act; + ansi = i; + ansj = j; + } + + if (j == gad[1].size()) { + break; + } + i--; + sum1 -= gad[0][i].first; + sum2 += gad[1][j].first; + j++; + } + + if (ans == oo) { + cout << "-1\n"; + return 0; + } + + int choice1 = 0; + int choice2 = 0; + + nrep(i, 1, ans + 1) { + if (cost[0][i] < cost[0][i - 1]) { + choice1 = i; + } + + if (cost[1][i] < cost[1][i - 1]) { + choice2 = i; + } + } + + cout << ans + 1 << '\n'; + + rep(k, ansi) { + cout << gad[0][k].second + 1 << ' ' << choice1 + 1 << '\n'; + } + rep(k, ansj) { + cout << gad[1][k].second + 1 << ' ' << choice2 + 1 << '\n'; + } +} diff --git a/Educational Codeforces Round 3/E. Minimum spanning tree for each edge.cpp b/Educational Codeforces Round 3/E. Minimum spanning tree for each edge.cpp new file mode 100644 index 0000000..d6a4e75 --- /dev/null +++ b/Educational Codeforces Round 3/E. Minimum spanning tree for each edge.cpp @@ -0,0 +1,191 @@ +/* Problem URL: https://codeforces.com/contest/609/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + V> edges(m); + rep(i, m) { + auto &[c, u, v, j] = edges[i]; + cin >> u >> v >> c; + u--, v--; + j = i; + } + + sortv(edges); + + vi dsu(n); + rep(i, n) { + dsu[i] = i; + } + + function find_p = [&](int i) { + if (dsu[i] == i) { + return i; + } + return dsu[i] = find_p(dsu[i]); + }; + + auto join = [&](int a, int b) { + a = find_p(a); + b = find_p(b); + dsu[b] = a; + return a != b; + }; + + ll ans = 0; + V>> graph(n); + + for (auto [c, u, v, j] : edges) { + if (join(u, v)) { + ans += c; + graph[u].emplace_back(v, c); + graph[v].emplace_back(u, c); + } + } + + vvi parent(20, vi(n)); + vvl maximal(20, vl(n)); + vi depth(n); + + function dfs = [&](int i, int p, ll c) { + parent[0][i] = p; + maximal[0][i] = c; + depth[i] = depth[p] + 1; + + nrep(j, 1, 20) { + parent[j][i] = parent[j - 1][parent[j - 1][i]]; + maximal[j][i] = max(maximal[j - 1][i], maximal[j - 1][parent[j - 1][i]]); + } + + for (auto [j, c] : graph[i]) { + if (j == p) { + continue; + } + + dfs(j, i, c); + } + }; + + dfs(0, 0, 0); + + auto lca = [&](int a, int b) { + if (depth[a] > depth[b]) { + swap(a, b); + } + + ll ans = 0; + int diff = depth[b] - depth[a]; + + rep(i, 20) { + if ((diff >> i) & 1) { + rmax(ans, maximal[i][b]); + b = parent[i][b]; + } + } + + if (a == b) { + return ans; + } + + for (int i = 19; i >= 0; i--) { + if (parent[i][a] != parent[i][b]) { + rmax(ans, maximal[i][a]); + rmax(ans, maximal[i][b]); + a = parent[i][a]; + b = parent[i][b]; + } + } + + return max({ans, maximal[0][a], maximal[0][b]}); + }; + + vl act(m); + + for (auto [c, u, v, i] : edges) { + act[i] = ans - lca(u, v) + c; + } + + repv(i, act) { + cout << i << '\n'; + } +} diff --git a/Educational Codeforces Round 3/F. Frogs and mosquitoes.cpp b/Educational Codeforces Round 3/F. Frogs and mosquitoes.cpp new file mode 100644 index 0000000..e1ebc27 --- /dev/null +++ b/Educational Codeforces Round 3/F. Frogs and mosquitoes.cpp @@ -0,0 +1,124 @@ +/* Problem URL: https://codeforces.com/contest/609/problem/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + V> frog(n); + V> mosq(m); + + rep(i, n) { + auto &[x, s, j, c] = frog[i]; + cin >> x >> s; + j = i; + c = 0; + } + + for (auto &[x, s] : mosq) { + cin >> x >> s; + } + + sortv(frog); + sortv(mosq); + + V> ans(n); + + int j = 0; + rep(i, n) { + auto [x, s, k, c] = frog[i]; + while (j < m && mosq[j].first < x) { + j++; + } + + while (j < m && mosq[j].first - x <= s) { + s += mosq[j].second; + c++; + j++; + } + + ans[k] = {c, s}; + } + + for (auto [c, s] : ans) { + cout << c << ' ' << s << '\n'; + } +} diff --git a/Educational Codeforces Round 45 (Rated for Div. 2)/A. Commentary Boxes.cpp b/Educational Codeforces Round 45 (Rated for Div. 2)/A. Commentary Boxes.cpp new file mode 100644 index 0000000..2e87590 --- /dev/null +++ b/Educational Codeforces Round 45 (Rated for Div. 2)/A. Commentary Boxes.cpp @@ -0,0 +1,89 @@ +/* Problem URL: https://codeforces.com/contest/990/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + ll n, m, a, b; + cin >> n >> m >> a >> b; + + ll mod = n % m; + + cout << min(mod * b, (m - mod) * a) << '\n'; +} diff --git a/Educational Codeforces Round 45 (Rated for Div. 2)/B. Micro-World.cpp b/Educational Codeforces Round 45 (Rated for Div. 2)/B. Micro-World.cpp new file mode 100644 index 0000000..6b50e5d --- /dev/null +++ b/Educational Codeforces Round 45 (Rated for Div. 2)/B. Micro-World.cpp @@ -0,0 +1,104 @@ +/* Problem URL: https://codeforces.com/contest/990/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + vl fds(n); + cin >> fds; + + multiset pq; + rep(i, n) { + pq.insert(fds[i]); + } + + auto itr = pq.begin(); + while (itr != pq.end()) { + auto get = pq.lower_bound(*itr - k); + while (*get != *itr) { + get = pq.erase(get); + } + itr++; + } + + cout << pq.size() << '\n'; +} diff --git a/Educational Codeforces Round 45 (Rated for Div. 2)/C. Bracket Sequences Concatenation Problem.cpp b/Educational Codeforces Round 45 (Rated for Div. 2)/C. Bracket Sequences Concatenation Problem.cpp new file mode 100644 index 0000000..74cf9dc --- /dev/null +++ b/Educational Codeforces Round 45 (Rated for Div. 2)/C. Bracket Sequences Concatenation Problem.cpp @@ -0,0 +1,144 @@ +/* Problem URL: https://codeforces.com/contest/990/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + map count; + + int c = 0; + ll ans = 0; + while (n--) { + string a; + cin >> a; + + int suf = 0; + repv(i, a) { + if (i == '(') { + suf++; + continue; + } + + if (suf == 0) { + suf = -1; + break; + } + suf--; + } + + int pref = 0; + for (int i = a.size() - 1; i >= 0; i--) { + if (a[i] == ')') { + pref++; + continue; + } + + if (pref == 0) { + pref = -1; + break; + } + pref--; + } + + if (pref == -1 && suf == -1) { + continue; + } + + if (pref == 0 || suf == 0) { + c++; + continue; + } + + if (pref == -1) { + ans += count[-suf]; + count[suf]++; + continue; + } + + ans += count[pref]; + count[-pref]++; + } + + ans += (ll)c * c; + + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 45 (Rated for Div. 2)/D. Graph And Its Complement.cpp b/Educational Codeforces Round 45 (Rated for Div. 2)/D. Graph And Its Complement.cpp new file mode 100644 index 0000000..86e6007 --- /dev/null +++ b/Educational Codeforces Round 45 (Rated for Div. 2)/D. Graph And Its Complement.cpp @@ -0,0 +1,168 @@ +/* Problem URL: https://codeforces.com/contest/990/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, a, b; + cin >> n >> a >> b; + + if (n == 1) { + cout << "YES\n0\n"; + return 0; + } + + if (n == 2) { + if (a == b) { + cout << "NO\n"; + } else { + cout << "YES\n"; + if (a == 1) { + cout << "01\n10\n"; + } else { + cout << "00\n00\n"; + } + } + return 0; + } + + if (a != 1 && b != 1) { + cout << "NO\n"; + return 0; + } + + if (n == 3 && max(a, b) == 1) { + cout << "NO\n"; + return 0; + } + + bool inv = false; + if (b == 1) { + swap(a, b); + inv = true; + } + + cout << "YES\n"; + vvi adj(n, vi(n)); + + if (b == 1) { + nrep(i, 1, n - 1) { + adj[0][i] = 1; + adj[i][0] = 1; + } + + adj[n - 1][n - 2] = 1; + adj[n - 2][n - 1] = 1; + + repv(i, adj) { + repv(j, i) { + cout << j; + } + cout << '\n'; + } + + return 0; + } + + rep(i, b - 1) { + nrep(j, i + 1, n) { + adj[i][j] = 1; + adj[j][i] = 1; + } + } + + if (inv) { + rep(i, n) { + rep(j, n) { + if (i == j) { + continue; + } + + adj[i][j] ^= 1; + } + } + } + + repv(i, adj) { + repv(j, i) { + cout << j; + } + cout << '\n'; + } +} diff --git a/Educational Codeforces Round 45 (Rated for Div. 2)/E. Post Lamps.cpp b/Educational Codeforces Round 45 (Rated for Div. 2)/E. Post Lamps.cpp new file mode 100644 index 0000000..160fce8 --- /dev/null +++ b/Educational Codeforces Round 45 (Rated for Div. 2)/E. Post Lamps.cpp @@ -0,0 +1,138 @@ +/* Problem URL: https://codeforces.com/contest/990/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m, k; + cin >> n >> m >> k; + n += 2; + + V blocked(n); + + while (m--) { + int n; + cin >> n; + blocked[n] = true; + } + + if (blocked[0]) { + cout << "-1\n"; + return 0; + } + + int prev = 0; + vi pref(n); + int c = 1; + int minimal = 0; + nrep(i, 1, n) { + rmax(minimal, c); + c++; + if (!blocked[i]) { + prev = i; + c = 1; + } + + pref[i] = prev; + } + + if (minimal > k) { + cout << "-1\n"; + return 0; + } + + vl cost(k); + cin >> cost; + + ll ans = INT64_MAX >> 1; + + nrep(i, minimal - 1, k) { + ll now = 0; + + int cur = 0; + while (cur < n - 2) { + now += cost[i]; + cur = pref[min(cur + i + 1, n - 1)]; + } + + rmin(ans, now); + } + + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 46 (Rated for Div. 2)/A. Codehorses T-shirts.cpp b/Educational Codeforces Round 46 (Rated for Div. 2)/A. Codehorses T-shirts.cpp new file mode 100644 index 0000000..dae2244 --- /dev/null +++ b/Educational Codeforces Round 46 (Rated for Div. 2)/A. Codehorses T-shirts.cpp @@ -0,0 +1,119 @@ +/* Problem URL: https://codeforces.com/contest/1000/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + V one(n); + V two(n); + + cin >> one >> two; + + vvi count1(4, vi(3)); + vvi count2(4, vi(3)); + + auto pos = [&](char op) { + map var; + var['M'] = 0; + var['S'] = 1; + var['L'] = 2; + + return var[op]; + }; + + rep(i, n) { + count1[one[i].size() - 1][pos(one[i].back())]++; + count2[two[i].size() - 1][pos(two[i].back())]++; + } + + int ans = 0; + + rep(i, 4) { + count2[i][0] -= min(count1[i][0], count2[i][0]); + count2[i][1] -= min(count1[i][1], count2[i][1]); + count2[i][2] -= min(count1[i][2], count2[i][2]); + + ans += count2[i][0] + count2[i][1] + count2[i][2]; + } + + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 46 (Rated for Div. 2)/B. Light It Up.cpp b/Educational Codeforces Round 46 (Rated for Div. 2)/B. Light It Up.cpp new file mode 100644 index 0000000..6560d94 --- /dev/null +++ b/Educational Codeforces Round 46 (Rated for Div. 2)/B. Light It Up.cpp @@ -0,0 +1,121 @@ +/* Problem URL: https://codeforces.com/contest/1000/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vi a(n); + cin >> a; + + vl norm(n + 1); + vl inv(n + 1); + + norm[0] = a[0]; + + nrep(i, 1, n) { + norm[i] += norm[i - 1]; + inv[i] += inv[i - 1]; + + if (i & 1) { + inv[i] += a[i] - a[i - 1]; + } else { + norm[i] += a[i] - a[i - 1]; + } + } + + inv[n] += inv[n - 1]; + norm[n] += norm[n - 1]; + + if (n & 1) { + inv[n] += m - a.back(); + } else { + norm[n] += m - a.back(); + } + + ll ans = norm.back(); + + rep(i, n + 1) { + rmax(ans, norm[i] + inv[n] - inv[i] - 1); + } + + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 46 (Rated for Div. 2)/C. Covered Points Count.cpp b/Educational Codeforces Round 46 (Rated for Div. 2)/C. Covered Points Count.cpp new file mode 100644 index 0000000..2da370a --- /dev/null +++ b/Educational Codeforces Round 46 (Rated for Div. 2)/C. Covered Points Count.cpp @@ -0,0 +1,119 @@ +/* Problem URL: https://codeforces.com/contest/1000/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + V> act; + + rep(i, n) { + ll l, r; + cin >> l >> r; + act.emplace_back(l, 1); + act.emplace_back(r + 1, 0); + } + + sortv(act); + + vl ans(n + 1); + + ll now = 0; + ll count = 0; + + rep(i, act.size()) { + auto [pos, t] = act[i]; + + ans[count] += pos - now; + now = pos; + + if (t == 1) { + count++; + continue; + } + + count--; + } + + nrep(i, 1, n + 1) { + cout << ans[i] << " \n"[i == n]; + } +} diff --git a/Educational Codeforces Round 46 (Rated for Div. 2)/D. Yet Another Problem On a Subsequence.cpp b/Educational Codeforces Round 46 (Rated for Div. 2)/D. Yet Another Problem On a Subsequence.cpp new file mode 100644 index 0000000..1934799 --- /dev/null +++ b/Educational Codeforces Round 46 (Rated for Div. 2)/D. Yet Another Problem On a Subsequence.cpp @@ -0,0 +1,139 @@ +/* Problem URL: https://codeforces.com/contest/1000/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vl a(n); + cin >> a; + + ll mod = 998244353; + + vvl comb(n, vl(n)); + comb[0][0] = 1; + + nrep(i, 1, n) { + comb[i][0] = 1; + comb[i][i] = 1; + + nrep(k, 1, n) { + comb[i][k] = (comb[i - 1][k] + comb[i - 1][k - 1]) % mod; + } + } + + vl memo(n, -1); + + function dp = [&](int i) -> ll { + if (i >= n) { + return 1; + } + + ll &ans = memo[i]; + if (ans != -1) { + return ans; + } + + ans = 0; + + if (a[i] <= 0 || a[i] + i + 1 > n) { + return 0; + } + + ll now = 0; + + nrep(j, i + a[i] + 1, n + 1) { + ans += dp(j) * comb[a[i] + now][a[i]]; + ans %= mod; + now++; + } + + return ans; + }; + + ll ans = 0; + rep(i, n) { + ans += dp(i); + ans %= mod; + } + + cout << ans << '\n'; +} diff --git a/Educational Codeforces Round 46 (Rated for Div. 2)/E. We Need More Bosses.cpp b/Educational Codeforces Round 46 (Rated for Div. 2)/E. We Need More Bosses.cpp new file mode 100644 index 0000000..cdb9c71 --- /dev/null +++ b/Educational Codeforces Round 46 (Rated for Div. 2)/E. We Need More Bosses.cpp @@ -0,0 +1,210 @@ +/* Problem URL: https://codeforces.com/contest/1000/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; +} + +set> inv; +V> act; +int c = 0; + +void IS_BRIDGE(int v,int to) { + inv.emplace(v, to); + inv.emplace(to, v); + act.emplace_back(v, to); + c++; +}; // some function to process the found bridge +int n; // number of nodes +vector> adj; // adjacency list of graph + +vector visited; +vector tin, low; +int timer; + +void dfs(int v, int p = -1) { + visited[v] = true; + tin[v] = low[v] = timer++; + bool parent_skipped = false; + for (int to : adj[v]) { + if (to == p && !parent_skipped) { + parent_skipped = true; + continue; + } + if (visited[to]) { + low[v] = min(low[v], tin[to]); + } else { + dfs(to, v); + low[v] = min(low[v], low[to]); + if (low[to] > tin[v]) + IS_BRIDGE(v, to); + } + } +} + +void find_bridges() { + timer = 0; + visited.assign(n, false); + tin.assign(n, -1); + low.assign(n, -1); + for (int i = 0; i < n; ++i) { + if (!visited[i]) + dfs(i); + } +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int m; + cin >> n >> m; + adj.resize(n); + + while (m--) { + int a, b; + cin >> a >> b; + a--, b--; + adj[a].push_back(b); + adj[b].push_back(a); + } + + find_bridges(); + + int g = 0; + + V vis(n); + + vi group(n); + + function att = [&](int i) { + vis[i] = true; + group[i] = g; + + for (auto j : adj[i]) { + if (vis[j] || inv.find({i, j}) != inv.end()) { + continue; + } + + att(j); + } + }; + + rep(i, n) { + if (vis[i]) { + continue; + } + att(i); + g++; + } + + vvi graph(c + 1); + + for (auto [u, v] : act) { + graph[group[u]].push_back(group[v]); + graph[group[v]].push_back(group[u]); + } + + int oo = INT32_MAX >> 1; + + auto bfs = [&](int u) -> pair { + vi dis(c + 1, oo); + dis[u] = 0; + queue q; + q.push(u); + + int ans = u; + + while (!q.empty()) { + auto i = q.front(); + q.pop(); + + for (auto j : graph[i]) { + if (dis[j] > dis[i] + 1) { + dis[j] = dis[i] + 1; + q.push(j); + ans = j; + } + } + } + + return {ans, dis[ans]}; + }; + + auto [u, _] = bfs(0); + + auto [v, ans] = bfs(u); + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/J. First Non-Bipartite Edge.cpp b/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/J. First Non-Bipartite Edge.cpp new file mode 100644 index 0000000..48ac845 --- /dev/null +++ b/ITMO Academy: pilot course - Disjoint Sets Union - Step 2/J. First Non-Bipartite Edge.cpp @@ -0,0 +1,131 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/7/2/practice/contest/289391/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vi dsu(n); + rep(i, n) { + dsu[i] = i; + } + + vi depth(n); + + function(int)> find_p = [&](int i) -> pair { + if (dsu[i] == i) { + return {i, 0}; + } + + auto ans = find_p(dsu[i]); + dsu[i] = ans.first; + depth[i] ^= ans.second; + + return {dsu[i], depth[i]}; + }; + + auto join = [&](int a, int b) { + auto ap = find_p(a); + auto bp = find_p(b); + + if (ap.first == bp.first) { + return depth[a] == depth[b]; + } + + depth[bp.first] = (depth[b] ^ depth[a] ^ 1); + dsu[bp.first] = ap.first; + + return false; + }; + + rep(i, m) { + int a, b; + cin >> a >> b; + a--, b--; + + if (join(a, b)) { + cout << i + 1 << '\n'; + return 0; + } + } + + cout << "-1\n"; +} diff --git a/ITMO Academy: pilot course - Disjoint Sets Union - Step 3/A. DSU with rollback.cpp b/ITMO Academy: pilot course - Disjoint Sets Union - Step 3/A. DSU with rollback.cpp new file mode 100644 index 0000000..22d5238 --- /dev/null +++ b/ITMO Academy: pilot course - Disjoint Sets Union - Step 3/A. DSU with rollback.cpp @@ -0,0 +1,161 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/7/3/practice/contest/289392/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vi dsu(n); + vi size(n); + rep(i, n) { + dsu[i] = i; + size[i] = 1; + } + + stack> re; + + function find_p = [&](int i) { + if (dsu[i] == i) { + return i; + } + return find_p(dsu[i]); + }; + + int c = n; + + auto join = [&](int a, int b) { + a = find_p(a); + b = find_p(b); + + if (a == b) { + return; + } + + if (size[a] < size[b]) { + swap(a, b); + } + + re.emplace(a, b); + dsu[b] = a; + + if (size[a] == size[b]) { + size[a]++; + } + + c--; + }; + + auto rollback = [&]() { + while (re.top().first != -1) { + auto [a, b] = re.top(); + re.pop(); + + if (size[b] == size[a] - 1) { + size[a]--; + } + + dsu[b] = b; + c++; + } + re.pop(); + }; + + while (m--) { + string op; + cin >> op; + + if (op[0] == 'u') { + int a, b; + cin >> a >> b; + join(a - 1, b - 1); + cout << c << '\n'; + continue; + } + + if (op[0] == 'p') { + re.emplace(-1, -1); + continue; + } + + rollback(); + cout << c << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Disjoint Sets Union - Step 3/B. Number of Connected Components on Segments.cpp b/ITMO Academy: pilot course - Disjoint Sets Union - Step 3/B. Number of Connected Components on Segments.cpp new file mode 100644 index 0000000..8f34474 --- /dev/null +++ b/ITMO Academy: pilot course - Disjoint Sets Union - Step 3/B. Number of Connected Components on Segments.cpp @@ -0,0 +1,230 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/7/3/practice/contest/289392/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vi dsu(n); + vi size(n); + rep(i, n) { + dsu[i] = i; + size[i] = 1; + } + + stack> ops; + + int c = n; + + function find_p = [&](int i) { + if (dsu[i] == i) { + return i; + } + return find_p(dsu[i]); + }; + + auto join = [&](int a, int b) { + a = find_p(a); + b = find_p(b); + + if (a == b) { + return; + } + + if (size[a] < size[b]) { + swap(a, b); + } + + dsu[b] = a; + + if (size[a] == size[b]) { + size[a]++; + } + + ops.emplace(a, b); + c--; + }; + + auto save = [&]() { + ops.emplace(-1, -1); + }; + + auto rollback = [&]() { + while (ops.top().first != -1) { + auto [a, b] = ops.top(); + ops.pop(); + + if (size[b] == size[a] - 1) { + size[a]--; + } + + dsu[b] = b; + c++; + } + ops.pop(); + }; + + V> edges(m); + repv(i, edges) { + cin >> i.first >> i.second; + i.first--, i.second--; + } + + int q; + cin >> q; + + static int len = sqrt(m) + 1; + + struct query { + int l; + int r; + int i; + + bool operator<(query &b) { + if (l / len != b.l / len) { + return l / len < b.l / len; + } + return r < b.r; + } + }; + + V queries(q); + rep(i, q) { + cin >> queries[i].l >> queries[i].r; + queries[i].l--, queries[i].r--; + + queries[i].i = i; + } + sortv(queries); + vi ans(q); + + int now = 0; + int calc = 1; + repv(i, queries) { + int cur = i.l / len; + if (cur > now) { + now = cur; + calc = cur + 1; + while (!ops.empty()) { + ops.pop(); + } + rep(i, n) { + dsu[i] = i; + size[i] = 1; + } + c = n; + } + + int stop = i.r / len; + if (cur == stop) { + save(); + nrep(j, i.l, i.r + 1) { + join(edges[j].first, edges[j].second); + } + ans[i.i] = c; + rollback(); + continue; + } + + while (calc < stop) { + nrep(i, calc * len, (calc + 1) * len) { + join(edges[i].first, edges[i].second); + } + calc++; + } + + save(); + nrep(j, calc * len, i.r + 1) { + join(edges[j].first, edges[j].second); + } + + nrep(j, i.l, min((now + 1) * len, m)) { + join(edges[j].first, edges[j].second); + } + + ans[i.i] = c; + rollback(); + } + + repv(i, ans) { + cout << i << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Disjoint Sets Union - Step 3/C. Dynamic Connectivity Offline.cpp b/ITMO Academy: pilot course - Disjoint Sets Union - Step 3/C. Dynamic Connectivity Offline.cpp new file mode 100644 index 0000000..f75a678 --- /dev/null +++ b/ITMO Academy: pilot course - Disjoint Sets Union - Step 3/C. Dynamic Connectivity Offline.cpp @@ -0,0 +1,242 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/7/3/practice/contest/289392/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + struct query { + int type; + int l; + int r; + int u; + int v; + int i; + }; + + V queries; + vi ans; + + map, int> prev; + + int time = 0; + + rep(i, m) { + char op; + cin >> op; + + if (op == '?') { + queries.emplace_back(1, time, time, 0, 0, ans.size()); + ans.emplace_back(); + continue; + } + + int a, b; + cin >> a >> b; + a--, b--; + + if (a > b) { + swap(a, b); + } + + if (op == '-') { + queries[prev[{a, b}]].r = time; + time++; + continue; + } + + time++; + prev[{a, b}] = queries.size(); + queries.emplace_back(0, time, INT32_MAX >> 1, a, b, -1); + } + + vi dsu(n); + vi size(n); + stack> rev; + + int c; + + auto init = [&]() { + c = n; + rep(i, n) { + dsu[i] = i; + size[i] = 1; + } + }; + + init(); + + function find_p = [&](int i) { + if (dsu[i] == i) { + return i; + } + return find_p(dsu[i]); + }; + + auto join = [&](int a, int b) { + a = find_p(a); + b = find_p(b); + + if (a == b) { + return; + } + + c--; + + if (size[a] < size[b]) { + swap(a, b); + } + + if (size[a] == size[b]) { + size[a]++; + } + + dsu[b] = a; + + rev.emplace(a, b); + }; + + auto save = [&]() { + rev.emplace(-1, -1); + }; + + auto rollback = [&]() { + while (rev.top().first != -1) { + auto [a, b] = rev.top(); + rev.pop(); + + if (size[b] == size[a] - 1) { + size[a]--; + } + + dsu[b] = b; + c++; + } + rev.pop(); + }; + + function&)> func = [&](int l, int r, V &queries) { + if (l == r) { + save(); + repv(i, queries) { + if (i.type == 0 && l >= i.l && r <= i.r) { + join(i.u, i.v); + } + } + + repv(i, queries) { + if (i.type == 1 && i.l == l) { + ans[i.i] = c; + } + } + rollback(); + + return; + } + + V queries2; + save(); + + repv(i, queries) { + if (l >= i.l && r <= i.r) { + join(i.u, i.v); + continue; + } + + if (l <= i.r && i.l <= r) { + queries2.emplace_back(i); + } + } + + int mid = (l + r) >> 1; + + func(l, mid, queries2); + func(mid + 1, r, queries2); + + rollback(); + }; + + func(0, time, queries); + + repv(i, ans) { + cout << i << '\n'; + } +} diff --git a/ITMO Academy: pilot course - Two Pointers Method - Step 1/A. Merging Arrays.cpp b/ITMO Academy: pilot course - Two Pointers Method - Step 1/A. Merging Arrays.cpp new file mode 100644 index 0000000..978451d --- /dev/null +++ b/ITMO Academy: pilot course - Two Pointers Method - Step 1/A. Merging Arrays.cpp @@ -0,0 +1,121 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/9/1/practice/contest/307092/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vi a(n); + vi b(m); + cin >> a >> b; + + vi c(n + m); + + int i = 0; + int j = 0; + int k = 0; + + while (i < n && j < m) { + if (a[i] <= b[j]) { + c[k] = a[i]; + k++; + i++; + continue; + } + + c[k] = b[j]; + k++; + j++; + } + + while (i < n) { + c[k] = a[i]; + i++; + k++; + } + + while (j < m) { + c[k] = b[j]; + j++; + k++; + } + + cout << c; +} diff --git a/ITMO Academy: pilot course - Two Pointers Method - Step 1/B. Number of Smaller.cpp b/ITMO Academy: pilot course - Two Pointers Method - Step 1/B. Number of Smaller.cpp new file mode 100644 index 0000000..27c772c --- /dev/null +++ b/ITMO Academy: pilot course - Two Pointers Method - Step 1/B. Number of Smaller.cpp @@ -0,0 +1,98 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/9/1/practice/contest/307092/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + vi a(n); + vi b(m); + cin >> a >> b; + + int i = 0; + rep(j, m) { + while (i < n && a[i] < b[j]) { + i++; + } + + cout << i << " \n"[j == m - 1]; + } +} diff --git a/ITMO Academy: pilot course - Two Pointers Method - Step 1/C. Number of Equal.cpp b/ITMO Academy: pilot course - Two Pointers Method - Step 1/C. Number of Equal.cpp new file mode 100644 index 0000000..4cad613 --- /dev/null +++ b/ITMO Academy: pilot course - Two Pointers Method - Step 1/C. Number of Equal.cpp @@ -0,0 +1,107 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/9/1/practice/contest/307092/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + vi a(n); + vi b(m); + cin >> a >> b; + + int lower = 0; + int higher = 0; + + ll ans = 0; + + rep(i, m) { + while (lower < n && a[lower] < b[i]) { + lower++; + } + + while (higher < n && a[higher] <= b[i]) { + higher++; + } + + ans += higher - lower; + } + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Two Pointers Method - Step 2/A. Segment with Small Sum.cpp b/ITMO Academy: pilot course - Two Pointers Method - Step 2/A. Segment with Small Sum.cpp new file mode 100644 index 0000000..0246b3d --- /dev/null +++ b/ITMO Academy: pilot course - Two Pointers Method - Step 2/A. Segment with Small Sum.cpp @@ -0,0 +1,107 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/9/2/practice/contest/307093/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + ll s; + cin >> n >> s; + + vl a(n); + cin >> a; + + int ans = 0; + ll now = 0; + + int j = -1; + + rep(i, n) { + now += a[i]; + + while (now > s) { + j++; + now -= a[j]; + } + + rmax(ans, i - j); + } + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Two Pointers Method - Step 2/B. Segment with Big Sum.cpp b/ITMO Academy: pilot course - Two Pointers Method - Step 2/B. Segment with Big Sum.cpp new file mode 100644 index 0000000..75b0cf6 --- /dev/null +++ b/ITMO Academy: pilot course - Two Pointers Method - Step 2/B. Segment with Big Sum.cpp @@ -0,0 +1,110 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/9/2/practice/contest/307093/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + ll s; + cin >> n >> s; + + vl a(n); + cin >> a; + + int oo = INT32_MAX >> 1; + int ans = oo; + + ll now = 0; + + int j = -1; + rep(i, n) { + now += a[i]; + + while (now - a[j + 1] >= s) { + j++; + now -= a[j]; + } + + if (now >= s) { + rmin(ans, i - j); + } + } + + cout << (ans == oo ? -1 : ans) << '\n'; +} diff --git a/ITMO Academy: pilot course - Two Pointers Method - Step 2/C. Number of Segments with Small Sum.cpp b/ITMO Academy: pilot course - Two Pointers Method - Step 2/C. Number of Segments with Small Sum.cpp new file mode 100644 index 0000000..13a5768 --- /dev/null +++ b/ITMO Academy: pilot course - Two Pointers Method - Step 2/C. Number of Segments with Small Sum.cpp @@ -0,0 +1,106 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/9/2/practice/contest/307093/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + ll s; + cin >> n >> s; + + vl a(n); + cin >> a; + + ll ans = 0; + ll now = 0; + + int j = -1; + rep(i, n) { + now += a[i]; + + while (now > s) { + j++; + now -= a[j]; + } + + ans += i - j; + } + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Two Pointers Method - Step 2/D. Number of Segments with Big Sum.cpp b/ITMO Academy: pilot course - Two Pointers Method - Step 2/D. Number of Segments with Big Sum.cpp new file mode 100644 index 0000000..50aa5a0 --- /dev/null +++ b/ITMO Academy: pilot course - Two Pointers Method - Step 2/D. Number of Segments with Big Sum.cpp @@ -0,0 +1,108 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/9/2/practice/contest/307093/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + ll s; + cin >> n >> s; + + vl a(n); + cin >> a; + + ll ans = 0; + ll now = 0; + + int j = -1; + rep(i, n) { + now += a[i]; + + while (now - a[j + 1] >= s) { + j++; + now -= a[j]; + } + + if (now >= s) { + ans += j + 2; + } + } + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Two Pointers Method - Step 2/E. Segments with Small Set.cpp b/ITMO Academy: pilot course - Two Pointers Method - Step 2/E. Segments with Small Set.cpp new file mode 100644 index 0000000..6845b54 --- /dev/null +++ b/ITMO Academy: pilot course - Two Pointers Method - Step 2/E. Segments with Small Set.cpp @@ -0,0 +1,118 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/9/2/practice/contest/307093/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, k; + cin >> n >> k; + + vi a(n); + cin >> a; + + vi count(1e5 + 1); + + int c = 0; + + auto add = [&](int num) { + count[num]++; + c += count[num] == 1; + }; + + auto rem = [&](int num) { + count[num]--; + c -= count[num] == 0; + }; + + ll ans = 0; + + int j = -1; + rep(i, n) { + add(a[i]); + + while (c > k) { + j++; + rem(a[j]); + } + + ans += i - j; + } + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Two Pointers Method - Step 2/F. Segments with Small Spread.cpp b/ITMO Academy: pilot course - Two Pointers Method - Step 2/F. Segments with Small Spread.cpp new file mode 100644 index 0000000..882d990 --- /dev/null +++ b/ITMO Academy: pilot course - Two Pointers Method - Step 2/F. Segments with Small Spread.cpp @@ -0,0 +1,127 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/9/2/practice/contest/307093/problem/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + ll k; + cin >> n >> k; + + vl a(n); + cin >> a; + + vvl minsparse(20, vl(n)); + vvl maxsparse(20, vl(n)); + + rep(i, n) { + minsparse[0][i] = a[i]; + maxsparse[0][i] = a[i]; + } + + nrep(j, 1, 20) { + for (int i = 0; i + (1 << (j - 1)) < n; i++) { + minsparse[j][i] = min(minsparse[j - 1][i], minsparse[j - 1][i + (1 << (j - 1))]); + maxsparse[j][i] = max(maxsparse[j - 1][i], maxsparse[j - 1][i + (1 << (j - 1))]); + } + } + + auto querymin = [&](int l, int r) { + int log = 31 - __builtin_clz(r - l + 1); + return min(minsparse[log][l], minsparse[log][r - (1 << log) + 1]); + }; + + auto querymax = [&](int l, int r) { + int log = 31 - __builtin_clz(r - l + 1); + return max(maxsparse[log][l], maxsparse[log][r - (1 << log) + 1]); + }; + + ll ans = 0; + + int j = 0; + rep(i, n) { + while (querymax(j, i) - querymin(j, i) > k) { + j++; + } + + ans += i - j + 1; + } + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Two Pointers Method - Step 2/G. Coprime Segment.cpp b/ITMO Academy: pilot course - Two Pointers Method - Step 2/G. Coprime Segment.cpp new file mode 100644 index 0000000..3ebf6ce --- /dev/null +++ b/ITMO Academy: pilot course - Two Pointers Method - Step 2/G. Coprime Segment.cpp @@ -0,0 +1,129 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/9/2/practice/contest/307093/problem/G */ + +#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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + vl a(n); + cin >> a; + + vvl sparse(20, vl(n)); + + ll minimal = INT64_MAX >> 1; + + rep(i, n) { + sparse[0][i] = a[i]; + rmin(minimal, a[i]); + } + + if (minimal == 1) { + cout << "1\n"; + return 0; + } + + nrep(j, 1, 20) { + for (int i = 0; i + (1 << (j - 1)) < n; i++) { + sparse[j][i] = gcd(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]); + } + } + + auto query = [&](int l, int r) { + int log = 31 - __builtin_clz(r - l + 1); + return gcd(sparse[log][l], sparse[log][r - (1 << log) + 1]); + }; + + int oo = INT32_MAX >> 1; + int ans = oo; + + int j = 0; + rep(i, n) { + while (j < i && query(j + 1, i) == 1) { + j++; + } + + if (query(j, i) == 1) { + rmin(ans, i - j + 1); + } + } + + cout << (ans == oo ? -1 : ans) << '\n'; +} diff --git a/ITMO Academy: pilot course - Two Pointers Method - Step 3/A. Looped Playlist.cpp b/ITMO Academy: pilot course - Two Pointers Method - Step 3/A. Looped Playlist.cpp new file mode 100644 index 0000000..03f7406 --- /dev/null +++ b/ITMO Academy: pilot course - Two Pointers Method - Step 3/A. Looped Playlist.cpp @@ -0,0 +1,130 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/9/3/practice/contest/307094/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + ll p; + cin >> n >> p; + + int prev = n; + + vl a(n); + cin >> a; + + ll total = 0; + + rep(i, n) { + a.push_back(a[i]); + total += a[i]; + } + + ll tmp = p / total * n; + p %= total; + + if (p == 0) { + cout << 1 << ' ' << tmp << '\n'; + return 0; + } + + n <<= 1; + + vl pref(n + 1); + rep(i, n) { + pref[i + 1] = pref[i] + a[i]; + } + + int minimal = INT32_MAX >> 1; + int pos = 0; + + int j = 0; + rep(i, n) { + while (j < i && pref[i + 1] - pref[j + 1] >= p) { + j++; + } + + if (pref[i + 1] - pref[j] >= p && i - j + 1 < minimal) { + minimal = i - j + 1; + pos = j; + } + } + + cout << pos - prev * (pos > prev) + 1 << ' ' << minimal + tmp << '\n'; +} diff --git a/ITMO Academy: pilot course - Two Pointers Method - Step 3/B. Total Length.cpp b/ITMO Academy: pilot course - Two Pointers Method - Step 3/B. Total Length.cpp new file mode 100644 index 0000000..0fe401a --- /dev/null +++ b/ITMO Academy: pilot course - Two Pointers Method - Step 3/B. Total Length.cpp @@ -0,0 +1,107 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/9/3/practice/contest/307094/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + ll s; + cin >> n >> s; + + vl a(n); + cin >> a; + + ll now = 0; + ll ans = 0; + + int j = -1; + rep(i, n) { + now += a[i]; + + while (now > s) { + j++; + now -= a[j]; + } + + ll size = i - j; + ans += size * (size + 1) / 2; + } + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Two Pointers Method - Step 3/C. Che city.cpp b/ITMO Academy: pilot course - Two Pointers Method - Step 3/C. Che city.cpp new file mode 100644 index 0000000..5a60898 --- /dev/null +++ b/ITMO Academy: pilot course - Two Pointers Method - Step 3/C. Che city.cpp @@ -0,0 +1,102 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/9/3/practice/contest/307094/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + ll d; + cin >> n >> d; + + vl a(n); + cin >> a; + + ll ans = 0; + + int j = 0; + rep(i, n) { + while (a[i] - a[j] > d) { + j++; + } + + ans += j; + } + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Two Pointers Method - Step 3/D. Stylish clothes.cpp b/ITMO Academy: pilot course - Two Pointers Method - Step 3/D. Stylish clothes.cpp new file mode 100644 index 0000000..5743e95 --- /dev/null +++ b/ITMO Academy: pilot course - Two Pointers Method - Step 3/D. Stylish clothes.cpp @@ -0,0 +1,148 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/9/3/practice/contest/307094/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + vi a1(n); + cin >> a1; + + cin >> n; + vi a2(n); + cin >> a2; + + cin >> n; + vi a3(n); + cin >> a3; + + cin >> n; + vi a4(n); + cin >> a4; + + sortv(a1); + sortv(a2); + sortv(a3); + sortv(a4); + + int i = 0; + int j = 0; + int k = 0; + int l = 0; + + int ans = INT32_MAX >> 1; + int ans1; + int ans2; + int ans3; + int ans4; + + while (i < a1.size() && j < a2.size() && k < a3.size() && l < a4.size()) { + int tmp = max({a1[i], a2[j], a3[k], a4[l]}) - min({a1[i], a2[j], a3[k], a4[l]}); + + if (tmp <= ans) { + ans = tmp; + ans1 = a1[i]; + ans2 = a2[j]; + ans3 = a3[k]; + ans4 = a4[l]; + } + + int act = min({a1[i], a2[j], a3[k], a4[l]}); + + if (a1[i] == act) { + i++; + continue; + } + + if (a2[j] == act) { + j++; + continue; + } + + if (a3[k] == act) { + k++; + continue; + } + + l++; + } + + cout << ans1 << ' ' << ans2 << ' ' << ans3 << ' ' << ans4 << '\n'; +} diff --git a/ITMO Academy: pilot course - Two Pointers Method - Step 3/E. Knapsack on a Segment.cpp b/ITMO Academy: pilot course - Two Pointers Method - Step 3/E. Knapsack on a Segment.cpp new file mode 100644 index 0000000..29d8f91 --- /dev/null +++ b/ITMO Academy: pilot course - Two Pointers Method - Step 3/E. Knapsack on a Segment.cpp @@ -0,0 +1,109 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/9/3/practice/contest/307094/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + ll s; + cin >> n >> s; + + vl w(n); + vl c(n); + cin >> w >> c; + + int j = -1; + ll wnow = 0; + ll cnow = 0; + ll ans = 0; + rep(i, n) { + wnow += w[i]; + cnow += c[i]; + + while (wnow > s) { + j++; + wnow -= w[j]; + cnow -= c[j]; + } + + rmax(ans, cnow); + } + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Two Pointers Method - Step 3/F. Card Substrings.cpp b/ITMO Academy: pilot course - Two Pointers Method - Step 3/F. Card Substrings.cpp new file mode 100644 index 0000000..5959c1f --- /dev/null +++ b/ITMO Academy: pilot course - Two Pointers Method - Step 3/F. Card Substrings.cpp @@ -0,0 +1,117 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/9/3/practice/contest/307094/problem/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + cin >> n >> m; + + string a, b; + cin >> a >> b; + + repv(i, a) { + i -= 'a'; + } + + vi lim(26); + repv(i, b) { + lim[i - 'a']++; + } + + vi count(26); + + ll ans = 0; + + int j = -1; + rep(i, n) { + int now = a[i]; + count[now]++; + + while (count[now] > lim[now]) { + j++; + int tmp = a[j]; + count[tmp]--; + } + + ans += i - j; + } + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Two Pointers Method - Step 3/G. Not Very Rude Substring.cpp b/ITMO Academy: pilot course - Two Pointers Method - Step 3/G. Not Very Rude Substring.cpp new file mode 100644 index 0000000..42ded44 --- /dev/null +++ b/ITMO Academy: pilot course - Two Pointers Method - Step 3/G. Not Very Rude Substring.cpp @@ -0,0 +1,118 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/9/3/practice/contest/307094/problem/G */ + +#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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + ll c; + cin >> n >> c; + + string a; + cin >> a; + repv(i, a) { + i -= 'a'; + } + + vl count(26); + int ans = 0; + + ll now = 0; + + int j = -1; + rep(i, n) { + count[a[i]]++; + + if (a[i] == 1) { + now += count[0]; + } + + while (now > c) { + j++; + count[a[j]]--; + if (a[j] == 0) { + now -= count[1]; + } + } + + rmax(ans, i - j); + } + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Two Pointers Method - Step 3/H. A-B Knapsack.cpp b/ITMO Academy: pilot course - Two Pointers Method - Step 3/H. A-B Knapsack.cpp new file mode 100644 index 0000000..13943c9 --- /dev/null +++ b/ITMO Academy: pilot course - Two Pointers Method - Step 3/H. A-B Knapsack.cpp @@ -0,0 +1,125 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/9/3/practice/contest/307094/problem/H */ + +#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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, m; + ll s, ac, bc; + cin >> n >> m >> s >> ac >> bc; + + vl a(n); + vl b(m); + cin >> a >> b; + + sort(all(a), greater<>()); + sort(all(b), greater<>()); + + ll now = 0; + ll w = 0; + + int j = 0; + + while (w + bc <= s && j < m) { + now += b[j]; + w += bc; + j++; + } + + ll ans = now; + + rep(i, n) { + now += a[i]; + w += ac; + + while (w > s && j > 0) { + j--; + w -= bc; + now -= b[j]; + } + + if (w > s) { + break; + } + + rmax(ans, now); + } + + cout << ans << '\n'; +} diff --git a/ITMO Academy: pilot course - Two Pointers Method - Step 3/I. Segment with the Required Subset.cpp b/ITMO Academy: pilot course - Two Pointers Method - Step 3/I. Segment with the Required Subset.cpp new file mode 100644 index 0000000..c0a26ac --- /dev/null +++ b/ITMO Academy: pilot course - Two Pointers Method - Step 3/I. Segment with the Required Subset.cpp @@ -0,0 +1,119 @@ +/* Problem URL: https://codeforces.com/edu/course/2/lesson/9/3/practice/contest/307094/problem/I */ + +#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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n, s; + cin >> n >> s; + + vi a(n); + cin >> a; + + int oo = INT32_MAX >> 1; + int ans = oo; + + vi count(s + 1); + count[0] = 1; + + auto add = [&](int now) { + for (int i = s; i >= now; i--) { + count[i] += count[i - now]; + } + }; + + auto rem = [&](int now) { + nrep(i, now, s + 1) { + count[i] -= count[i - now]; + } + }; + + int j = 0; + rep(i, n) { + add(a[i]); + + while (count[s]) { + rmin(ans, i - j + 1); + rem(a[j]); + j++; + } + } + + cout << (ans == oo ? -1 : ans) << '\n'; +} diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..1dfdaeb --- /dev/null +++ b/TODO.md @@ -0,0 +1,114 @@ +# TODO list + +List of unsolved problems that I should try again later. Putting everything here to clean +my tabs + +*Contents* +* [Codeforces](#TODO list#Codeforces) + * [Divs](#TODO list#Codeforces#Divs) + * [Educational](#TODO list#Codeforces#Divs#Educational) + * [Div 1/2](#TODO list#Codeforces#Divs#Div 1/2) + * [Div 4](#TODO list#Codeforces#Divs#Div 4) + * [GYM](#TODO list#Codeforces#GYM) + * [ICPC Brazil first phase](#TODO list#Codeforces#GYM#ICPC Brazil first phase) +* [Atcoder](#TODO list#Atcoder) + +## Codeforces + +Problem from codeforces. + +### Divs + +Official divs from codeforces + +#### Educational + +1. [F. Frogs and Mosquitoes](https://codeforces.com/contest/609/problem/F) + + I have no clue where to even start. + +2. [E. Water Taps](https://codeforces.com/contest/954/problem/E) + + I have an idea, but it's really annoying to solve it. + +3. [F. Fibonacci String Subsequences](https://codeforces.com/contest/946/problem/F) + + Obviously DP, but not sure how to do it... + +4. [F. Imbalance Value of Tree](https://codeforces.com/contest/915/problem/F) + + Tree problem, looks interesting. + +5. [F. Clear The Matrix](https://codeforces.com/contest/903/problem/F) + + DP with optimization? + +#### Div 1/2 + +1. [D. Division Versus Addition](https://codeforces.com/contest/2152/problem/D) + + The idea is simple, but I have to think through it better. + +2. [D2. Inversion Graph Coloring](https://codeforces.com/contest/2143/problem/D2) + + DP with optimization. + +3. [E. Yet Another MEX Problem](https://codeforces.com/contest/2146/problem/E) + + Maybe MEX with two pointers? + +4. [D. A Cruel Segment's Thesis](https://codeforces.com/contest/2140/problem/D) + + Looks greedy. + +5. [F. Flint and Steel](https://codeforces.com/contest/2133/problem/F) + + I tried this a long time ago, don't remember what it's about, just remembered + that I tried a greedy solution and it didn't work. + +#### Div 3 + +1. [G. Cry Me a River](https://codeforces.com/contest/2137/problem/G) + + Graph problem, don't remember the details. + +#### Div 4 + +1. [F. Gravity Falls](https://codeforces.com/contest/2148/problem/F) + + I barely read it, not sure were I found it either, but looks pretty easy. + +### GYM + +Gym from simulations or contests that I did + +#### ICPC Brazil first phase + +1. [D. Dominoes](https://codeforces.com/gym/106073/problem/D) + + If I did this one, I would have passed into second phase, must try to solve it + ASAP. + +#### ICPC Northwestern European Regional + +[Link](https://codeforces.com/gym/104875) because it has no problem page in gym. + +1. L. Last Guess + + I think I can solve it with flow. + +#### USP Try-outs + +1. [B. Tuk-Tuk Express](https://codeforces.com/gym/103934/problem/B) + + I have NO clue why my code keeps getting WA, awful implementation problem. + +2. [J. Apep, the Lord of Chaos](https://codeforces.com/gym/103934/problem/J) + + I don't remember what the problem was about, I just know that I tried it before. + +## Atcoder + +1. [F. Operate K](https://atcoder.jp/contests/abc386/tasks/abc386_f) + + DP with optimization. diff --git a/The 2025 ICPC Latin America Championship/F. Festival Signs.cpp b/The 2025 ICPC Latin America Championship/F. Festival Signs.cpp new file mode 100644 index 0000000..d09e24f --- /dev/null +++ b/The 2025 ICPC Latin America Championship/F. Festival Signs.cpp @@ -0,0 +1,300 @@ +/* Problem URL: https://codeforces.com/gym/105789/problem/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; +} + +int main() +{ + ios::sync_with_stdio(false); + cin.tie(nullptr); + + int n; + cin >> n; + + int time = 0; + + struct queryt { + int a; + int b; + int l; + int r; + int type; + int h; + int i; + }; + + V queries; + vi rem; + map var; + vi ans; + + rep(i, n) { + char op; + cin >> op; + + if (op == '?') { + int a, b; + cin >> a >> b; + var[a] = 0; + var[b] = 0; + ans.emplace_back(INT32_MAX >> 1); + queries.emplace_back(a, b, time, time, 0, -1, ans.size() - 1); + continue; + } + + if (op == '+') { + int a, b, h; + cin >> a >> b >> h; + var[a] = 0; + var[b] = 0; + time++; + queries.emplace_back(a, b, time, INT32_MAX >> 1, 1, h, -1); + rem.push_back(queries.size() - 1); + continue; + } + + int j; + cin >> j; + + queries[rem[j - 1]].r = time; + time++; + } + + int v = 0; + repv(i, var) { + i.second = v; + v += 2; + } + + repv(i, queries) { + i.a = var[i.a]; + i.b = var[i.b]; + } + + if (v > 1) { + v = 1 << (32 - __builtin_clz(v - 1)); + } + + vi seg(v << 1); + vi lazy(v << 1); + V upd(v << 1); + + vector> redo; + redo.reserve(20 * v); + + auto persist = [&](int i) { + redo.emplace_back(i, seg[i], lazy[i], upd[i]); + }; + + auto propagate = [&](int i) { + if (!upd[i]) { + return; + } + + persist(i); + rmax(seg[i], lazy[i]); + upd[i] = false; + + if (i < v) { + int l = i << 1; + int r = (i << 1) + 1; + + if (lazy[l] < lazy[i]) { + persist(l); + lazy[l] = lazy[i]; + upd[l] = true; + } + + if (lazy[r] < lazy[i]) { + persist(r); + lazy[r] = lazy[i]; + upd[r] = true; + } + } + }; + + function update = [&](int i, int l, int r, int tl, int tr, int v) { + propagate(i); + + if (l > tr || r < tl) { + return; + } + + if (l >= tl && r <= tr) { + if (lazy[i] >= v) { + return; + } + persist(i); + lazy[i] = v; + upd[i] = true; + propagate(i); + return; + } + + int mid = (l + r) >> 1; + + update(i * 2, l, mid, tl, tr, v); + update(i * 2 + 1, mid + 1, r, tl, tr, v); + + int ans = min(seg[i * 2], seg[i * 2 + 1]); + if (ans == seg[i]) { + return; + } + + persist(i); + seg[i] = ans; + }; + + function query = [&](int i, int l, int r, int tl, int tr) { + propagate(i); + + if (l > tr || r < tl) { + return INT32_MAX >> 1; + } + + if (l >= tl && r <= tr) { + return seg[i]; + } + + int mid = (l + r) >> 1; + + return min(query(i * 2, l, mid, tl, tr), query(i * 2 + 1, mid + 1, r, tl, tr)); + }; + + auto save = [&]() { + redo.emplace_back(-1, -1, -1, -1); + }; + + auto rollback = [&]() { + while (get<0>(redo.back()) != -1) { + auto [a, b, c, d] = redo.back(); + redo.pop_back(); + seg[a] = b; + lazy[a] = c; + upd[a] = d; + } + redo.pop_back(); + }; + + function&)> func = [&](int l, int r, V queries) { + if (l == r) { + save(); + + repv(i, queries) { + if (i.type == 1 && l >= i.l && l <= i.r) { + update(1, 0, v - 1, i.a, i.b, i.h); + } + } + + repv(i, queries) { + if (i.type == 0 && l == i.l) { + rmin(ans[i.i], query(1, 0, v - 1, i.a, i.b)); + } + } + + rollback(); + + return; + } + + save(); + + V queries2; + + repv(i, queries) { + if (i.type == 1 && l >= i.l && r <= i.r) { + update(1, 0, v - 1, i.a, i.b, i.h); + continue; + } + + if (l <= i.r && i.l <= r) { + queries2.emplace_back(i); + } + } + + int mid = (l + r) >> 1; + + func(l, mid, queries2); + func(mid + 1, r, queries2); + + rollback(); + }; + + func(0, time, queries); + + repv(i, ans) { + cout << i << '\n'; + } +}