func_code_string
stringlengths 59
71.4k
|
---|
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const double EPS = 1e-7; const long long BIG_PRIME7 = 1000000007; const long long BIG_PRIME9 = 1000000009; int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } char ch = a ; int j, n, m, L, i; int N, C; int x, y, z, k; string f, s; int a[101], b[101]; int main() { cin >> x; for (i = 0; i < x; i++) { cin >> k; if (k & 1) z++; } if (!(z & 1)) cout << (x - z); else cout << z; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 111111; int last[3 * N], pre[3 * N], v[3 * N], cnt[3 * N], cur = 0; inline void add(int a, int b) { pre[cur] = last[a]; v[cur] = b; last[a] = cur; cur++; } set<int>* who[N]; inline set<int>* merge(set<int>*& a, set<int>*& b) { if (a->size() < b->size()) { return merge(b, a); } else { for (set<int>::iterator it = b->begin(); it != b->end(); it++) { if (a->find(-*it) != a->end()) { a->erase(-*it); } else { a->insert(*it); } } b->clear(); return a; } } int used[N]; void dfs(int u) { used[u] = true; for (int i = last[u]; i >= 0; i = pre[i]) if (!used[v[i]]) { dfs(v[i]); cnt[i ^ 1] = cnt[i] = who[v[i]]->size(); who[u] = merge(who[u], who[v[i]]); } } set<int>* mem[N]; int main() { memset(last, -1, sizeof(last)); int n; scanf( %d , &n); for (int i = (0); i <= ((n - 1) - 1); i++) { int u, v; scanf( %d%d , &u, &v); u--; v--; add(u, v); add(v, u); } for (int i = (0); i <= ((n)-1); i++) mem[i] = who[i] = new set<int>(); int k; scanf( %d , &k); for (int i = (1); i <= ((k)); i++) { int u, v; scanf( %d%d , &u, &v); u--; v--; if (u != v) { who[u]->insert(i); who[v]->insert(-i); } } dfs(0); for (int i = 0; i < cur; i += 2) { if (i) printf( ); printf( %d , cnt[i]); } puts( ); for (int i = (0); i <= ((n)-1); i++) delete (mem[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int INF = 1e9 + 7; const int base = 1e9; const int MAX = 1e5; const double EPS = 1e-9; const double PI = acos(-1.); int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, *a, *res, j = 0; cin >> n; a = new int[n]; res = new int[n]; set<int> cnt; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = n - 1; i >= 0; i--) { if (!cnt.count(a[i])) { cnt.insert(a[i]); res[j] = a[i]; j++; } } cout << j << endl; for (int i = j - 1; i >= 0; i--) { cout << res[i] << ; } return 0; } |
#include <bits/stdc++.h> using namespace std; string s; int n, m; int f[150][150][250][2]; int main() { cin >> s; n = s.size(); scanf( %d , &m); f[0][m][n][1] = true; for (int i = 0; i < n; i++) for (int j = 0; j <= m; j++) for (int k = 0; k <= n * 2; k++) for (int l = 0; l < 2; l++) if (f[i][j][k][l]) if (s[i] == T ) { f[i + 1][j][k][1 - l] = true; if (j != 0 && k - 1 + 2 * l >= 0) f[i + 1][j - 1][k - 1 + 2 * l][l] = true; } else { if (k - 1 + 2 * l >= 0) f[i + 1][j][k - 1 + 2 * l][l] = true; if (j != 0) f[i + 1][j - 1][k][1 - l] = true; } int i = 0; for (int j = 0; j <= m; j++) if (j % 2 == 0) for (int k = 0; k <= n * 2; k++) for (int l = 0; l < 2; l++) if (f[n][j][k][l]) { if (k - n < 0 && n - k > i) i = n - k; if (k - n > 0 && k - n > i) i = k - n; } printf( %d n , i); return 0; } |
#include <bits/stdc++.h> using namespace std; struct cell { bool up, down, right, left; int degree; cell() { up = down = left = right = degree = 0; } void reset() { up = down = left = right = degree = 0; } }; struct qcell { int x, y, degree; qcell() { x = y = -1; degree = 0; } qcell(int xx, int yy, int deg) { x = xx; y = yy; degree = deg; } }; char pitch[2001][2001]; cell info[2001][2001]; int n, m; int empty_cells = 0; bool update(int x, int y) { int degree = 0; if (x >= 0 && x < n && y >= 0 && y < m) { degree = info[x][y].degree; if (pitch[x][y] != . ) info[x][y].reset(); else { info[x][y].up = (x - 1 >= 0 && pitch[x - 1][y] == . ); info[x][y].down = (x + 1 < n && pitch[x + 1][y] == . ); info[x][y].left = (y - 1 >= 0 && pitch[x][y - 1] == . ); info[x][y].right = (y + 1 < m && pitch[x][y + 1] == . ); info[x][y].degree = (info[x][y].up + info[x][y].down + info[x][y].left + info[x][y].right); } } return degree != info[x][y].degree; } void do_push(int x, int y, queue<qcell> &q) { if (x >= 0 && x < n && y >= 0 && y < m) if (pitch[x][y] == . ) if (info[x][y].degree == 1) q.push(qcell(x, y, 1)); } void occupy(int x, int y, queue<qcell> &q) { int xx = x, yy = y; char dir = U ; if (info[x][y].down) dir = D ; if (info[x][y].left) dir = L ; if (info[x][y].right) dir = R ; if (dir == U ) { pitch[x][y] = v ; pitch[x - 1][y] = ^ ; xx = x - 1; } else if (dir == D ) { pitch[x][y] = ^ ; pitch[x + 1][y] = v ; xx = x + 1; } else if (dir == L ) { pitch[x][y] = > ; pitch[x][y - 1] = < ; yy = y - 1; } else { pitch[x][y] = < ; pitch[x][y + 1] = > ; yy = y + 1; } update(x, y); update(xx, yy); if (do_push) { if (update(x - 1, y)) do_push(x - 1, y, q); if (update(x + 1, y)) do_push(x + 1, y, q); if (update(x, y - 1)) do_push(x, y - 1, q); if (update(x, y + 1)) do_push(x, y + 1, q); if (update(xx - 1, yy)) do_push(xx - 1, yy, q); if (update(xx + 1, yy)) do_push(xx + 1, yy, q); if (update(xx, yy - 1)) do_push(xx, yy - 1, q); if (update(xx, yy + 1)) do_push(xx, yy + 1, q); } empty_cells -= 2; } int main() { cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { cin >> pitch[i][j]; if (pitch[i][j] == . ) empty_cells++; } queue<qcell> q; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { update(i, j); if (info[i][j].degree == 1) q.push(qcell(i, j, info[i][j].degree)); } bool unique_sol = 1; while (1) { if (q.empty()) break; qcell f = q.front(); q.pop(); if (info[f.x][f.y].degree == 0) { if (pitch[f.x][f.y] == . ) { unique_sol = false; break; } } else occupy(f.x, f.y, q); } if (unique_sol) if (empty_cells != 0) unique_sol = 0; if (!unique_sol) cout << Not unique << endl; else for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cout << pitch[i][j]; cout << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; int main() { ios::sync_with_stdio(false); int t; cin >> t; while (t--) { string s, t; cin >> s >> t; vector<int> each[26]; int n, m; n = s.length(); m = t.length(); for (int i = 0; i < n; ++i) { each[s[i] - a ].push_back(i); } int las = INF, res = 0; for (int i = 0; i < m; ++i) { if (upper_bound(each[t[i] - a ].begin(), each[t[i] - a ].end(), las) != each[t[i] - a ].end()) { las = *upper_bound(each[t[i] - a ].begin(), each[t[i] - a ].end(), las); } else { if (each[t[i] - a ].empty()) { res = -1; break; } else { res++; las = each[t[i] - a ][0]; } } } cout << res << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, h, a, road_width = 0; cin >> n >> h; for (int i = 0; i < n; i++) { cin >> a; if (a > h) road_width += 2; else road_width++; } cout << road_width << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int abs(int a, int b) { if (a > b) return a - b; else return b - a; } int main() { int n, c, i, b, count = 0, index1 = 1, index2 = 1, flag = 0; cin >> n >> c; char *s; s = new char[n + 2]; for (i = 1; i <= n; i++) { cin >> s[i]; } int *d = new int[n + 1]; for (i = 1; i <= n / 2; i++) { b = abs((int)s[i], (int)s[n - i + 1]); if (b < 13) d[i] = d[n - i + 1] = b; else d[i] = d[n - i + 1] = 26 - b; } if (c > n / 2) c = 1 + n - c; for (i = 1; i <= n / 2; i++) { if (d[i] > 0) { index1 = i; break; } } for (i = n / 2; i >= 1; i--) { if (d[i] > 0) { index2 = i; flag = 1; break; } } if (c <= index1) { count = index2 - c; } else if (c <= index2) { if (c - index1 > index2 - c) count = 2 * (index2 - c) + c - index1; else count = 2 * (c - index1) + index2 - c; } else count = c - index1; for (i = index1; i <= index2; i++) { count += d[i]; } if (flag == 1) cout << count; else cout << 0; return 0; } |
#include <bits/stdc++.h> int any_table[11][11]; struct DammedPascalNUmber { int tb[13][13]; DammedPascalNUmber() { int i, j; for (i = 0; i <= 12; i++) for (j = 0; j <= i; j++) { int &r = tb[i][j]; if (j == 0 || i == 0) r = 1; else r = tb[i - 1][j - 1] + tb[i - 1][j]; } } int count(int n, int m) { return tb[n][m]; } } pas; int any(int dig, int amo) { if (dig < amo) return 0; if (amo < 0) return 0; if (dig == 0) return 1; int &r = any_table[dig][amo]; int i; if (r >= 0) return r; r = 1; for (i = 0; i < amo; i++) r *= 2; for (; i < dig; i++) r *= 8; r *= pas.count(dig, amo); return r; } int check(int m, int n) { int i, j, t, r = 0; for (i = 1; i <= m; i++) { char str[15]; t = 0; sprintf(str, %d , i); for (j = 0; str[j]; j++) if (str[j] == 4 || str[j] == 7 ) t++; if (t == n) r++; } return r; } int a[13]; int dfs(int party, int quta) { int r = 0, i; if (party == 6) { return 1; } for (i = quta; i >= 0; i--) if (a[i] > 0) { int t = a[i]; a[i]--; r = (1LL * r + 1LL * t * 1LL * dfs(party + 1, quta - i) + 1LL * 1000000007) % 1000000007; a[i]++; } return r; } void test(int n, int m) { printf( any(%d,%d)=%d n , n, m, any(n, m)); } void test2(int m, int n) { printf( (%d,%d)=%d a[%d]=%d n , m, n, check(m, n), n, a[n]); } int main() { memset(any_table, -1, sizeof(any_table)); int m, s, i, j, k, sn, lucked; while (~scanf( %d , &m)) { memset(a, 0, sizeof(a)); char str[15]; sprintf(str, %d , m); for (sn = 0; str[sn]; sn++) ; for (j = sn; j > 0; j--) { a[j] = 0; lucked = 0; for (i = 0; i < sn; i++) { int p = 0, q = 0; k = str[i] - 0 - 1; for (; k >= 0; k--) if (k == 4 || k == 7) q++; else p++; if (p) a[j] += p * any(sn - i - 1, j - lucked); if (q) a[j] += q * any(sn - i - 1, j - lucked - 1); if (str[i] == 4 || str[i] == 7 ) { lucked++; } if (i == sn - 1 && lucked == j) a[j]++; } } a[0] = m; for (i = 1; i <= sn; i++) a[0] -= a[i]; int ans = 0; for (i = 1; i <= sn; i++) if (a[i]) { ans = (1LL * ans + 1LL * a[i] * dfs(0, i - 1) + 1LL * 1000000007) % 1000000007; } printf( %d n , ans); } } |
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; const double pi = acos(-1); const int inf = 1 << 30; const int mod = 1e9 + 9; struct point { int x, flag; bool operator<(const point &a) const { return flag > a.flag; } } s, q; bool check(long long a[], int n, long long h, int x) { priority_queue<int, vector<int>, less<int> > qwe; for (int i = 1; i <= min(n, x); i++) qwe.push(a[i]); int f = 1, pos; while (!qwe.empty()) { pos = qwe.top(); qwe.pop(); if (f) h -= pos; f ^= 1; } if (h >= 0ll) return true; else return false; } int main() { int n; long long h, a[maxn]; scanf( %d %lld , &n, &h); for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); int l = 0, r = n, ans = 0; while (l <= r) { int mid = (l + r) >> 1; if (check(a, n, h, mid)) l = mid + 1, ans = mid; else r = mid - 1; } printf( %d n , ans); return 0; } |
#include <bits/stdc++.h> const int64_t mod = 998244353; int gcd(int64_t a, int64_t b) { while (a != 0 && b != 0) { if (a >= b) a = a % b; else b = b % a; } return a + b; } struct Fraction { int64_t numerator; int64_t devider; Fraction(int64_t n = 0, int64_t d = 1) : numerator(n), devider(d){}; const Fraction& operator+(const Fraction& rv) { if (rv.devider == devider) { numerator += rv.numerator; numerator %= mod; return *this; } else { numerator *= rv.devider; numerator %= mod; numerator += rv.numerator * devider; numerator %= mod; devider *= rv.devider; devider %= mod; return *this; } } const Fraction& operator*(const Fraction& rv) { numerator *= rv.numerator; devider *= rv.devider; numerator %= mod; devider %= mod; return *this; } }; struct Item { Item(Fraction f, int64_t s, int64_t v) : possibility(f), sum(s), val(v){}; Fraction possibility; int64_t sum; int64_t val; }; void extended_euclid(int64_t a, int64_t b, int64_t& x, int64_t& y, int64_t& d) { int64_t q, r, x1, x2, y1, y2; if (b == 0) { d = a, x = 1, y = 0; return; } x2 = 1, x1 = 0, y2 = 0, y1 = 1; while (b > 0) { q = a / b, r = a - q * b; x = x2 - q * x1, y = y2 - q * y1; a = b, b = r; x2 = x1, x1 = x, y2 = y1, y1 = y; } d = a, x = x2, y = y2; } int main() { int64_t n, m; std::cin >> n >> m; std::vector<std::pair<int64_t, bool>> data; int64_t posWeight = 0, negWeight = 0; for (int64_t i = 0; i < n; ++i) { int64_t a; std::cin >> a; if (a == 0) data.push_back(std::make_pair(-1, false)); else data.push_back(std::make_pair(-1, true)); } for (auto& i : data) { int64_t w; std::cin >> w; i.first = w; if (i.second) posWeight += w; else negWeight += w; } std::vector<Item> posVec{ Item(Fraction(1, 1), posWeight + negWeight, posWeight)}, negVec{Item(Fraction(1, 1), posWeight + negWeight, negWeight)}; for (int64_t i = 0; i < m; ++i) { std::vector<Item> newPos, newNeg; for (auto j : posVec) { if (!newPos.size()) { Fraction f(j.val, j.sum); f = f * j.possibility; newPos.push_back(Item(f, j.sum + 1, j.val + 1)); } else { Fraction f(j.val, j.sum); f = f * j.possibility; newPos.rbegin()->possibility = newPos.rbegin()->possibility + f; } if (j.sum > j.val) { Fraction f(j.sum - j.val, j.sum); f = f * j.possibility; newPos.push_back(Item(f, j.sum - 1, j.val)); } } for (auto j : negVec) { if (!newNeg.size() && j.val > 1) { Fraction f(j.val, j.sum); f = f * j.possibility; newNeg.push_back(Item(f, j.sum - 1, j.val - 1)); } else if (j.val > 1) { Fraction f(j.val, j.sum); f = f * j.possibility; newNeg.rbegin()->possibility = newNeg.rbegin()->possibility + f; } Fraction f(j.sum - j.val, j.sum); f = f * j.possibility; newNeg.push_back(Item(f, j.sum + 1, j.val)); } posVec = newPos; negVec = newNeg; } Fraction pos, neg; for (auto i : posVec) pos = pos + Fraction(i.val, 1) * i.possibility; for (auto i : negVec) neg = neg + Fraction(i.val, 1) * i.possibility; for (auto i : data) { Fraction res; if (i.second) res = Fraction(i.first, posWeight) * pos; else res = Fraction(i.first, negWeight) * neg; int64_t g = gcd(res.numerator, res.devider); res.numerator /= g; res.devider /= g; int64_t d, x, y; extended_euclid(res.devider, mod, x, y, d); std::cout << (((x % mod + mod) % mod) * res.numerator) % mod << std::endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, m, x, y, f[N], fa[N], ch[N][2]; char op[5]; long long a[N], siz[N], ssiz[N], ans[N], sans[N], sqsiz[N], all[N]; bool isroot(int u) { return ch[fa[u]][0] != u && ch[fa[u]][1] != u; } int which(int u) { return u == ch[fa[u]][1]; } void pushup(int o) { int lc = ch[o][0], rc = ch[o][1]; siz[o] = siz[lc] + siz[rc] + ssiz[o]; all[o] = all[lc] + all[rc] + a[o] * ssiz[o]; ans[o] = ans[lc] + ans[rc] + sans[o]; ans[o] += (ssiz[o] * ssiz[o] - sqsiz[o]) * a[o]; ans[o] += 2 * siz[rc] * ssiz[o] * a[o]; ans[o] += 2 * all[lc] * (siz[o] - siz[lc]); } void rotate(int x) { int y = fa[x], z = fa[y], md = which(x); if (!isroot(y)) { ch[z][which(y)] = x; } fa[x] = z; ch[y][md] = ch[x][!md]; fa[ch[y][md]] = y; ch[x][!md] = y; fa[y] = x; pushup(y); pushup(x); } void splay(int u) { while (!isroot(u)) { if (!isroot(fa[u])) { rotate(which(u) == which(fa[u]) ? fa[u] : u); } rotate(u); } } void access(int u) { for (int v = 0; u; v = u, u = fa[u]) { splay(u); int rc = ch[u][1]; ssiz[u] += siz[rc]; sans[u] += ans[rc]; sqsiz[u] += siz[rc] * siz[rc]; ch[u][1] = v; ssiz[u] -= siz[v]; sans[u] -= ans[v]; sqsiz[u] -= siz[v] * siz[v]; pushup(u); } } void link(int u, int v) { access(v); splay(v); access(u); splay(u); fa[u] = v; ssiz[v] += siz[u]; sans[v] += ans[u]; sqsiz[v] += siz[u] * siz[u]; pushup(v); } void cut(int u, int v) { access(u); access(v); splay(v); splay(u); fa[u] = 0; ssiz[v] -= siz[u]; sans[v] -= ans[u]; sqsiz[v] -= siz[u] * siz[u]; pushup(v); } bool check(int u, int v) { access(v); splay(v); splay(u); if (isroot(v)) { return false; } return true; } int main() { scanf( %d , &n); for (int i = 2; i <= n; i++) { scanf( %d , &f[i]); } for (int i = 1; i <= n; i++) { scanf( %lld , &a[i]); ans[i] = all[i] = a[i]; } for (int i = 1; i <= n; i++) { siz[i] = 1; ssiz[i] = 1; } for (int i = 2; i <= n; i++) { link(i, f[i]); } access(1); splay(1); printf( %.10lf n , 1.0 * ans[1] / n / n); scanf( %d , &m); while (m--) { scanf( %s%d%d , op, &x, &y); if (op[0] == P ) { if (check(x, y)) { swap(x, y); } cut(x, f[x]); f[x] = y; link(x, f[x]); access(1); splay(1); printf( %.10lf n , 1.0 * ans[1] / n / n); } else { access(x); splay(x); a[x] = y; pushup(x); printf( %.10lf n , 1.0 * ans[x] / n / n); } } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long x, y, n, m, t, q, a, b, count = 0; cin >> n >> m; long long arr[n]; for (x = 0; x < n; x++) cin >> arr[x]; a = 0; for (x = 0; x < n; x++) { a = a + arr[x]; cout << a / m << ; a = a % m; } } |
#include <bits/stdc++.h> using namespace std; int qread() { char c; int s = 0, f = 1; while ((c = getchar()) < 0 || c > 9 ) (c == - ) && (f = -1); do s = s * 10 + c - 0 ; while ((c = getchar()) >= 0 && c <= 9 ); return s * f; } int n, m; const int mod = 1e9 + 7; int a[100011], pos[100011], bin[100011], inv[100011], sum[100011], lp = 0, val[100011], vv[100011], svv[100011]; struct Ques { int l, r, id; bool operator<(const Ques &b) const { return r < b.r; } } q[100011]; int ans[100011]; int WWW(long long v) { return v > 2000000001 ? 2000000001 : v; } int main() { n = qread(); m = qread(); bin[0] = inv[0] = 1; for (int i = 1; i <= n; i++) a[i] = qread(), bin[i] = (bin[i - 1] << 1) % mod, inv[i] = 1ll * inv[i - 1] * ((mod + 1) >> 1) % mod, sum[i] = ((sum[i - 1] + 1ll * a[i] * bin[i]) % mod + mod) % mod; for (int i = 1; i <= m; i++) { q[i].l = qread(); q[q[i].id = i].r = qread(); } sort(q + 1, q + 1 + m); pos[lp = 1] = 1; int j = 1; val[1] = a[1] * 2; vv[1] = svv[1] = ((a[1] * 2) % mod + mod) % mod; while (j <= m && q[j].r == 1) ans[q[j].id] = (a[1] + mod) % mod, j++; for (int i = 2; i <= n; i++) { if (a[i] <= 0) pos[++lp] = i, val[lp] = 2 * a[i], vv[lp] = (a[i] * 2ll % mod + mod) % mod, svv[lp] = (svv[lp - 1] + vv[lp]) % mod; else { int cur = WWW(0ll + val[lp] + bin[pos[lp] - pos[lp - 1] + 1] * 1ll * a[i]); int cvv = (vv[lp] + bin[pos[lp] - pos[lp - 1] + 1] * 1ll * a[i]) % mod; lp--; while (lp && cur > 0) { cur = WWW(0ll + val[lp] + (pos[lp] - pos[lp - 1] > 31 ? 2000000001 : bin[pos[lp] - pos[lp - 1]]) * 1ll * cur); cvv = (vv[lp] + bin[pos[lp] - pos[lp - 1]] * 1ll * cvv) % mod; lp--; } val[++lp] = cur; pos[lp] = i; vv[lp] = cvv; svv[lp] = (svv[lp - 1] + vv[lp]) % mod; } while (j <= m && q[j].r == i) { if (q[j].l == q[j].r) { ans[q[j].id] = (a[i] + mod) % mod; j++; continue; } int l = q[j].l + 1, Ans = (a[q[j].l] + mod) % mod; int L = 1, R = lp; while (L < R) { int mid = (L + R) >> 1; if (pos[mid] >= l) R = mid; else L = mid + 1; } Ans = (Ans + svv[lp] - svv[L]) % mod; Ans = (Ans + mod) % mod; Ans = (Ans + (sum[pos[L]] - sum[l - 1] + mod) * 1ll * inv[l - 1]) % mod; ans[q[j].id] = Ans; j++; } } for (int i = 1; i <= m; i++) printf( %d n , ans[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; const long long inf = 1000000000000000000; int main() { long long t; cin >> t; while (t--) { long long n; cin >> n; vector<pair<long long, long long> > p(n); for (auto& x : p) cin >> x.first >> x.second; sort(p.begin(), p.end()); pair<long long, long long> now = {0, 0}; bool f = 1; string ans = ; for (int i = 0; i < n; i++) { if (p[i].first >= now.first && p[i].second >= now.second) { long long r = p[i].first - now.first, u = p[i].second - now.second; while (r--) ans += R ; while (u--) ans += U ; now = p[i]; } else { f = 0; break; } } if (f) cout << YES n << ans << endl; else cout << NO n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m; scanf( %d %d , &n, &m); for (int i = 0; i < n; i++) { if (i % 2 == 0) { for (int j = 1; j <= m; j++) { printf( # ); } } else if (i % 4 == 1) { for (int j = 1; j < m; j++) { printf( . ); } printf( # ); } else if (i % 4 == 3) { printf( # ); for (int j = 1; j < m; j++) { printf( . ); } } printf( n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 65, mod = 1e9 + 7; vector<int> g[N]; int w[N]; int col[N], pr[N]; int sz[N][2]; long long ans = 0; void dfs(int v, int par = -1, int c = 0) { pr[v] = par; ++sz[v][c]; col[v] = c; for (int u : g[v]) if (u != par) { dfs(u, v, c ^ 1); sz[v][0] += sz[u][0]; sz[v][1] += sz[u][1]; } } int main() { int n; scanf( %d , &n); for (int i = 0; i < n; ++i) scanf( %d , w + i); for (int i = 1; i < n; ++i) { int u, v; scanf( %d %d , &u, &v); --u, --v; g[u].push_back(v); g[v].push_back(u); } ans = 0; dfs(0); for (int v = 0; v < n; ++v) { vector<pair<int, int> > vs; for (int u : g[v]) if (u != pr[v]) vs.emplace_back(sz[u][0], sz[u][1]); if (pr[v] != -1) vs.emplace_back(sz[0][0] - sz[v][0], sz[0][1] - sz[v][1]); long long cnt = n; if (col[v]) { for (pair<int, int> e : vs) { cnt = (cnt + 1LL * e.second * (n - e.first - e.second)) % mod; cnt = (cnt - 1LL * e.first * (n - e.first - e.second)) % mod; } } else { for (pair<int, int> e : vs) { cnt = (cnt - 1LL * e.second * (n - e.first - e.second)) % mod; cnt = (cnt + 1LL * e.first * (n - e.first - e.second)) % mod; } } cnt %= mod; if (cnt < 0) cnt += mod; ans = (ans + cnt * w[v]) % mod; } ans %= mod; if (ans < 0) ans += mod; cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 28; const double INF = 1e12, EPS = 1e-9; int n, m, k; bool star[600][600]; int sum[600][600]; char in[600][600]; void run() { cin >> n >> m >> k; for (int i = 0; i < n; i++) cin >> in[i]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (in[i][j] == 1 && in[i + 1][j] == 1 && in[i + 2][j] == 1 && j && in[i + 1][j - 1] == 1 && in[i + 1][j + 1] == 1 ) star[i][j] = 1; } long long ans = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) sum[i + 1][j + 1] = star[i][j] + sum[i][j + 1] + sum[i + 1][j] - sum[i][j]; for (int a = 0; a < n; a++) for (int b = a + 3; b <= n; b++) { int l = 0, r = 2, cnt = 0; for (; l < m; l++) { while (r <= m && cnt < k) { r++; cnt += sum[b - 2][r - 1] - sum[a][r - 1] - (sum[b - 2][r - 2] - sum[a][r - 2]); } if (cnt < k || r > m) break; ans += m - r + 1; cnt -= sum[b - 2][l + 2] - sum[a][l + 2] - (sum[b - 2][l + 1] - sum[a][l + 1]); } } cout << ans << endl; } int main() { run(); } |
#include <bits/stdc++.h> using namespace std; void readi(int &x) { int v = 0, f = 1; char c = getchar(); while (!isdigit(c) && c != - ) c = getchar(); if (c == - ) f = -1; else v = v * 10 + c - 0 ; while (isdigit(c = getchar())) v = v * 10 + c - 0 ; x = v * f; } void readll(long long &x) { long long v = 0ll, f = 1ll; char c = getchar(); while (!isdigit(c) && c != - ) c = getchar(); if (c == - ) f = -1; else v = v * 10 + c - 0 ; while (isdigit(c = getchar())) v = v * 10 + c - 0 ; x = v * f; } void readc(char &x) { char c; while ((c = getchar()) == ) ; x = c; } void writes(string s) { puts(s.c_str()); } void writeln() { writes( ); } void writei(int x) { if (!x) putchar( 0 ); char a[25]; int top = 0; while (x) { a[++top] = (x % 10) + 0 ; x /= 10; } while (top) { putchar(a[top]); top--; } } void writell(long long x) { if (!x) putchar( 0 ); char a[25]; int top = 0; while (x) { a[++top] = (x % 10) + 0 ; x /= 10; } while (top) { putchar(a[top]); top--; } } inline long long inc(int &x) { return ++x; } inline long long inc(long long &x) { return ++x; } inline long long inc(int &x, long long y) { return x += y; } inline long long inc(long long &x, long long y) { return x += y; } inline double inc(double &x, double y) { return x += y; } inline long long dec(int &x) { return --x; } inline long long dec(long long &x) { return --x; } inline long long dec(int &x, long long y) { return x -= y; } inline long long dec(long long &x, long long y) { return x -= y; } inline double dec(double &x, double y) { return x -= y; } inline long long mul(int &x) { return x = ((long long)x) * x; } inline long long mul(long long &x) { return x = x * x; } inline long long mul(int &x, long long y) { return x *= y; } inline long long mul(long long &x, long long y) { return x *= y; } inline double mul(double &x, double y) { return x *= y; } inline long long divi(const int &x) { long long ans, l, r, mid; ans = 0; l = 0; r = 0x3fffffff; while (l < r) { mid = (l + r) / 2; if (mid * mid <= x) { ans = mid; l = mid + 1; } else r = mid; } return ans; } inline long long divi(const long long &x) { long long ans, l, r, mid; ans = 0; l = 0; r = 0x3fffffff; while (l < r) { mid = (l + r) / 2; if (mid * mid <= x) { ans = mid; l = mid + 1; } else r = mid; } return ans; } inline long long divi(int &x, long long y) { return x /= y; } inline long long divi(long long &x, long long y) { return x /= y; } inline double divi(double &x, double y) { return x /= y; } inline long long mod(int &x, long long y) { return x %= y; } inline long long mod(long long &x, long long y) { return x %= y; } int n, m, i, j, ct[100005], x, y, vis[100005]; vector<int> e[100005]; struct vt { int id, deg; } a[100005]; bool cmp(vt x, vt y) { return x.deg > y.deg; } bool dfs(int x) { if (vis[x] == 1) return 1; if (vis[x] == 2) return 0; vis[x] = 1; for (__typeof((e[x]).begin()) it = (e[x]).begin(); it != (e[x]).end(); it++) if (dfs(*it)) return 1; vis[x] = 2; return 0; } bool check(int x) { memset(vis, 0, sizeof(vis)); vis[x] = 2; int i; for (i = 1; i <= n; i++) { if (!vis[i] && dfs(i)) { for (i = 1; i <= n; i++) if (vis[i] != 1) ct[i] = 1; return 0; } } return 1; } int main() { double st = clock(); ios_base::sync_with_stdio(false); ; cin >> n >> m; if ((1) <= ((m))) for (((i)) = (1); ((i)) <= ((m)); ((i))++) { cin >> x >> y; e[x].push_back(y); } if ((1) <= ((n))) for (((i)) = (1); ((i)) <= ((n)); ((i))++) { a[i].id = i; a[i].deg = e[i].size(); } stable_sort(a + 1, a + n + 1, cmp); if ((1) <= ((n))) for (((i)) = (1); ((i)) <= ((n)); ((i))++) { if ((clock() - st) / CLOCKS_PER_SEC > 0.9) break; x = a[i].id; if (ct[x] || !check(x)) continue; cout << x; return 0; } cout << -1 ; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long mxn = 1003, inf = 1e18 + 18; int n, m, lcp[mxn][mxn], rk[mxn][mxn], ri[mxn], tot; long long k, f[mxn]; string s; struct S { int l, r; } a[mxn * mxn]; inline bool cmp(S a, S b) { int p = a.l + lcp[a.l][b.l], q = b.l + lcp[a.l][b.l]; if (p > a.r or q > b.r) return a.r - a.l < b.r - b.l; return s[p] < s[q]; } inline long long add(long long a, long long b) { return a + b > inf ? inf : a + b; } inline long long calc(int md) { for (int i = 1; i <= n; ++i) { ri[i] = i; for (; ri[i] <= n and rk[i][ri[i]] < md;) ++ri[i]; } memset(f, 0, sizeof(f)), f[n + 1] = 1; for (int i = 1; i <= m; ++i) { for (int j = n; j; --j) f[j] = add(f[j], f[j + 1]); for (int j = 1; j <= n; ++j) f[j] = ri[j] <= n ? f[ri[j] + 1] : 0; f[n + 1] = 0; } return f[1]; } int main() { cin >> n >> m >> k >> s, s = + s; for (int i = n; i; --i) for (int j = n; j; --j) if (s[i] == s[j]) lcp[i][j] = lcp[i + 1][j + 1] + 1; for (int i = 1; i <= n; ++i) for (int j = i; j <= n; ++j) a[++tot] = (S){i, j}; sort(a + 1, a + tot + 1, cmp); for (int i = 1; i <= tot; ++i) rk[a[i].l][a[i].r] = i; int l = 1, r = tot, md; for (; l <= r;) { md = l + r >> 1; if (calc(md) >= k) l = md + 1; else r = md - 1; } for (int i = a[r].l; i <= a[r].r; ++i) putchar(s[i]); } |
#include <algorithm> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <unordered_map> using namespace std; typedef long long LL; const int N = 3e5 + 10, mod = 998244353; string g[N]; int dp[N]; int fastPow(int a, int k) { int res = 1; while (k) { if (k & 1) res = (LL)res * a % mod; k >>= 1; a = (LL)a * a % mod; } return res; } int main() { ios::sync_with_stdio(false); int n, m, sum = 0, res = 0; n = 300000; for (int i = 2; i <= n; i++) { if (i & 1) sum = (sum + 1) % mod; dp[i] = ((LL)dp[i - 1] * 2 % mod + sum + !(i & 1)) % mod; sum = (sum << 1) % mod; } sum = 0; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> g[i]; for (auto c : g[i]) if (c == o ) sum++; } for (int i = 0; i < n; i++) { int cnt = 0, len = 0; while (cnt <= m) { if (cnt == m || g[i][cnt] == * ) { if (len) { res = (res + (LL)dp[len] * fastPow(2, sum - len) % mod) % mod; len = 0; } } else { len++; } cnt++; } } for (int i = 0; i < m; i++) { int cnt = 0, len = 0; while (cnt <= n) { if (cnt == n || g[cnt][i] == * ) { if (len) { res = (res + (LL)dp[len] * fastPow(2, sum - len) % mod) % mod; len = 0; } } else { len++; } cnt++; } } cout << res << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 100 + 10; char s[maxn]; int f(char ch) { if (ch == A ) return 1; if (ch == B ) return 2; if (ch == C ) return 4; return 0; } int main() { scanf( %s , s + 1); int l = strlen(s + 1); for (int i = 1; i <= l - 2; ++i) { int t = f(s[i]) | f(s[i + 1]) | f(s[i + 2]); if (t == 7) { cout << Yes << endl; return 0; } } cout << No ; return 0; } |
#include <bits/stdc++.h> using namespace std; vector<int> tree[500001]; bool intree[500001]; vector<int> tmp(0); bool isleaf(int node) { if (tree[node].size() == 1 && intree[(tree[node])[0]]) return true; return false; } void DFS(int lvl, int node) { intree[node] = true; if (isleaf(node)) { tmp.push_back(lvl); } for (int i = 0; i < tree[node].size(); i++) { if (!intree[(tree[node])[i]]) { DFS(lvl + 1, (tree[node])[i]); } } } int main() { ios_base::sync_with_stdio(false); int N; cin >> N; for (int i = 1; i <= N; i++) intree[i] = false; int x, y; for (int i = 0; i < N - 1; i++) { cin >> x >> y; tree[x].push_back(y); tree[y].push_back(x); } intree[1] = true; int ans = 0, c; for (int i = 0; i < (tree[1]).size(); i++) { tmp.clear(); DFS(0, (tree[1])[i]); sort(tmp.begin(), tmp.end()); c = tmp.size(); for (int j = 0; j < c; j++) { ans = max(ans, tmp[j] + c - j); } } cout << ans << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; vector<string> v(n); for (int i = 0; i < n; i++) cin >> v[i]; for (int i = 1; i < n - 1; i++) { for (int j = 1; j < n - 1; j++) { if (v[i][j] == # and v[i + 1][j] == # and v[i][j + 1] == # and v[i][j - 1] == # and v[i - 1][j] == # ) { v[i][j] = a ; v[i + 1][j] = a ; v[i][j + 1] = a ; v[i][j - 1] = a ; v[i - 1][j] = a ; } } } int c = 0; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (v[i][j] == # ) { c = 1; break; } if (c) cout << NO << endl; else cout << YES << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n; cin >> n; long long con = -1; long long a[n + 5]; for (long long i = 1; i <= n; i++) { cin >> a[i]; } long long i = 1; for (i = 1; i < n; i++) { if (a[i + 1] == a[i]) { con = i; break; } } if (con == -1) { for (con = 1; con < n; con++) { if (a[con] > a[con + 1]) { break; } } } for (i = 1; i < con; i++) { if (a[i + 1] < a[i]) { cout << NO << n ; return 0; } } while (a[con] == a[con + 1]) { con++; } for (i = con; i < n; i++) { if (a[i + 1] >= a[i]) { cout << NO << n ; return 0; } } cout << YES << n ; } |
#include <bits/stdc++.h> using ll = long long; using namespace std; int main() { ios::sync_with_stdio(false); int N, K; cin >> N >> K; ll total = N + 1; total += N; total += min(2 * (K - 1) + (N - K), 2 * (N - K) + K - 1); cout << total << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int k, n[20], a[20][5010], g[32768], f[32768], hsh[301781], ps[301781], ans1[20], ans2[20], vis[20], TMS; pair<int, int> md[32768]; long long sm[20]; int get(int x) { int y = (x + 1000000001) % 301781; while (hsh[y] && hsh[y] ^ x + 1000000001) y = (y + 1) % 301781; return y; } void dg(int x, int s1, int s2) { if (x == k) { f[s2 + s1] |= f[s1] * g[s2]; return; } dg(x + 1, s1, s2); dg(x + 1, s1, s2 + (1 << x)); dg(x + 1, s1 + (1 << x), s2); } int main() { scanf( %d , &k); long long S = 0; for (int i = 0; i < k; ++i) { scanf( %d , n + i); for (int j = 0; j < n[i]; ++j) { int x; scanf( %d , &x); a[i][j] = x; sm[i] += x; S += x; int w = get(x); ps[w] = i; hsh[w] = x + 1000000001; } } if (S % k != 0) { printf( No ); return 0; } S /= k; for (int i = 0; i < k; ++i) for (int j = 0; j < n[i]; ++j) { int st = 1 << i, nw = i; long long c = S - (sm[i] - a[i][j]); int P = 1; ++TMS; for (;;) { vis[nw] = TMS; if (abs(c) > 1000000000) { P = 0; break; } int w = get(c); if (!hsh[w]) { P = 0; break; } st |= 1 << ps[w]; if (ps[w] == i) { P = c == a[i][j]; break; } c = S - (sm[ps[w]] - c); nw = ps[w]; if (vis[nw] == TMS) { P = 0; break; } } if (P) { md[st] = make_pair(i, j); g[st] = 1; } } f[0] = 1; dg(0, 0, 0); if (f[(1 << k) - 1]) { printf( Yes n ); int ST = (1 << k) - 1; while (ST) { for (int _ = ST; _; _ = (_ - 1) & ST) if (g[_] * f[ST - _]) { int i = md[_].first, j = md[_].second, nw = i; long long c = S - (sm[i] - a[i][j]), ls = a[i][j]; for (;;) { int w = get(c); ans1[ps[w]] = c; ans2[ps[w]] = nw; if (ps[w] == i) break; ls = c; nw = ps[w]; c = S - (sm[ps[w]] - c); } ST -= _; break; } } for (int i = 0; i < k; ++i) printf( %d %d n , ans1[i], ans2[i] + 1); } else { printf( No ); } } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int test = 1; for (; test > 0; --test) { long long int n, a, b, c, d, c1 = 1, c2, c3, c4; cin >> n >> a >> b >> c >> d; c2 = c1 + b - c; c3 = c2 + a - d; c4 = c3 + c - b; long long int m = 1 - min(min(c1, c2), min(c3, c4)), m1 = max(max(c1, c2), max(c3, c4)); if (m1 + abs(m) > n) cout << 0 << endl; else cout << (n - (m1 + abs(m)) + 1) * n << endl; } return (0); } |
#include <bits/stdc++.h> using namespace std; const long long int MAX = 1000000007; const long long int MAXN = 100010; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t; cin >> t; while (t--) { long long int n, k; cin >> n >> k; if (n % 2 == 0 && k % 2 == 1) cout << NO << endl; else if (n % 2 == 1 && k % 2 == 0) cout << NO << endl; else { if (k * k <= n) cout << YES << endl; else cout << NO << endl; } } return 0; } |
#include <bits/stdc++.h> using namespace std; int d[250003]; int main() { int i, j, k, n, m, t, a[501][501], b[501][501]; cin >> t; while (t--) { cin >> n >> m; for (i = 0; i < n; i++) for (j = 0; j < m; j++) cin >> a[i][j], d[a[i][j]] = i; for (i = 0; i < m; i++) for (j = 0; j < n; j++) cin >> b[i][j]; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { cout << a[d[b[0][i]]][j] << ; } cout << endl; } } } |
#include <bits/stdc++.h> using namespace std; vector<pair<long long int, pair<long long int, long long int> > > P; int level[1007]; double ans = 0; bool contains(pair<long long int, pair<long long int, long long int> > A, pair<long long int, pair<long long int, long long int> > B) { return (A.second.first - B.second.first) * (A.second.first - B.second.first) + (A.second.second - B.second.second) * (A.second.second - B.second.second) < (A.first) * (A.first); } int main() { long long int i, j, k, l, m, n, x, y, z, a, b, r; scanf( %lld , &n); for (i = 0; i < n; i++) { scanf( %lld , &x); scanf( %lld , &y); scanf( %lld , &r); P.push_back(make_pair(r, make_pair(x, y))); } sort(P.begin(), P.end()); for (i = n - 1; i >= 0; i--) { for (j = n - 1; j > i; j--) { if (contains(P[j], P[i])) { level[i] = max(level[i], level[j] + 1); } } if (level[i] == 0) level[i] = 1; } for (i = 0; i < n; i++) { if (level[i] <= 2) ans += (P[i].first * P[i].first); else { if (level[i] % 2 == 1) ans -= (P[i].first * P[i].first); else ans += (P[i].first * P[i].first); } } printf( %.10lf n , ans * 3.14159265358979); return 0; } |
#include <bits/stdc++.h> using namespace std; inline long long power(long long a, long long b) { long long x = 1; a = a % 1000000007ULL; while (b) { if (b & 1) x = (x * a) % 1000000007ULL; a = (a * a) % 1000000007ULL; b >>= 1; } return x; } inline long long inv(long long a) { return power(a, 1000000007ULL - 2); } long long gcd(long long a, long long b) { return a ? gcd(b % a, a) : b; } const int N = 2e5 + 5; vector<int> adj[N]; int n, dis[N]; bool vis[N]; int cnt[N], p[N]; int tdis[N]; vector<int> d[N]; void dfs(int u, int dd, int par) { vis[u] = true; if (dd < 3) tdis[u] = dd; else tdis[u] = N; dis[u] = dd; d[dd].push_back(u); p[u] = par; for (auto v : adj[u]) { if (!vis[v]) dfs(v, dd + 1, u); } } int main() { ios_base::sync_with_stdio(false); cin >> n; int u, v; for (int i = 1; i < n; i++) { cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } dfs(1, 0, 0); int maxx = 0; for (int i = 0; i < n; i++) { if (d[i].size() == 0) break; maxx = i; } int ans = 0; for (int i = maxx; i > 1; i--) { for (auto v : d[i]) { if (dis[v] < 2 || tdis[v] == 2) continue; ans++; tdis[p[v]] = 2; for (auto j : adj[p[v]]) tdis[j] = 2; } } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int mod = 998244353; long long a[300001]; long long n, m, k; int b[300001]; void solve() { cin >> n; for (int i = 0; i < n; ++i) { int x; cin >> x; a[x]++; b[a[x]] += a[x]; k = (b[a[x]] >= i && b[a[x]] != n) ? b[a[x]] : k; } cout << k + 1; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) { solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { char str[2020]; long l; cin >> l; cin >> str; int i, j, hs = 0, ts = 0; for (i = 0; i < l; i++) { if (str[i] == H ) hs++; else ts++; str[l + i] = str[i]; } int res = 1000000000; int t; for (i = 0; i < l; i++) { t = 0; for (j = 0; j < hs; j++) { if (str[i + j] == T ) t++; } if (t < res) res = t; t = 0; for (j = 0; j < ts; j++) { if (str[i + j] == H ) t++; } if (t < res) res = t; } cout << res; return 0; } |
#include <bits/stdc++.h> using namespace std; long long a[1000000 + 100], f2, x1, x2, b[1000000 + 100], razl, sol, pok1, pok2; double f1; int main() { for (int i = 1; i <= 4; i++) cin >> a[i]; f1 = sqrt(1 + 8 * a[1]) * 1.0; f2 = sqrt(1 + 8 * a[1]); if (f1 != f2) { printf( Impossible n ); return 0; } f1 = sqrt(1 + 8 * a[4]) * 1.0; f2 = sqrt(1 + 8 * a[4]); if (f1 != f2) { printf( Impossible n ); return 0; } x1 = 1 + sqrt(1 + 8 * a[1]); x1 /= 2; x2 = 1 + sqrt(1 + 8 * a[4]); x2 /= 2; if (a[1] == 0 && a[2] == 0 && a[3] == 0) x1 = 0; if (a[4] == 0 && a[2] == 0 && a[3] == 0) x2 = 0; if (a[2] + a[3] != x1 * x2) { printf( Impossible n ); return 0; } for (int i = 1; i <= x1; i++) b[i] = 0; for (int i = x1 + 1; i <= x1 + x2; i++) b[i] = 1; sol = x1 * x2; pok1 = x1; pok2 = x1 + x2; while (sol != a[2]) { if (sol - x2 > a[2]) swap(b[pok1], b[pok2]), pok1--, pok2--, sol -= x2; else { swap(b[pok1], b[pok1 + sol - a[2]]); break; } } if (x1 + x2 == 0) printf( 0 ); for (int i = 1; i <= x1 + x2; i++) cout << b[i]; printf( n ); return 0; } |
#include <bits/stdc++.h> using namespace std; void mohammed() { ios::sync_with_stdio(0); ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); } int main() { mohammed(); int n, x, y; cin >> n; map<int, int> mp; map<int, int>::iterator it; for (int i = 0; i < n; i++) { cin >> x >> y; mp[x]++; mp[y + 1]--; } x = 0; for (it = mp.begin(); it != mp.end(); it++) { it->second += x; if (it->second > 2) { cout << NO ; return 0; } x = it->second; } cout << YES ; return 0; } |
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); template <typename T, typename U> inline void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> inline void amax(T &x, U y) { if (x < y) x = y; } template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << ( << P.first << , << P.second << ) n ; } template <class T> ostream &operator<<(ostream &os, set<T> V) { os << [ ; for (auto vv : V) os << vv << , ; return os << b b] n ; } template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << [ ; for (auto vv : V) os << vv << , ; return os << b b] n ; } template <class K, class X> ostream &operator<<(ostream &os, map<K, X> V) { os << [ ; for (auto vv : V) os << vv << , ; return os << b b] n ; } long int ceildiv(long int n, long int f) { if (n % f) { return n / f + 1; } else return n / f; } string checkTicket(vector<long> first, vector<long> second) { int n = first.size(); if (first[0] < second[0]) { for (int i = 0; i < n; i += 1) { if (first[i] >= second[i]) { return NO n ; } } } else if (first[0] > second[0]) { for (int i = 0; i < n; i += 1) { if (first[i] <= second[i]) { return NO n ; } } } else { return NO n ; } return YES n ; } int main() { long int n; cin >> n; string s; cin >> s; vector<long> first, second; for (int i = 0; i < n; i++) { first.push_back(s[i] - 0 ); } for (int i = n; i < 2 * n; i++) { second.push_back(s[i] - 0 ); } sort(first.begin(), first.end()); sort(second.begin(), second.end()); cerr << first << second; cout << checkTicket(first, second); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 200005; int t[N]; int p[N]; int p2[N]; int main() { std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; long long sum = 0; cin >> n; for (int i = 0; i <= n; i++) { cin >> t[i]; sum += t[i]; } bool amb = 0; for (int i = 1; i <= n; i++) { if (t[i] > 1 && t[i - 1] > 1) amb = 1; } if (!amb) { cout << perfect ; return 0; } cout << ambiguous << n ; long long par = 1, k = 2; p[1] = 0; p2[1] = 0; for (int i = 1; i <= n; i++) { long long temp = k; for (int j = 0; j < t[i]; j++) { p[k] = par; p2[k] = par; k++; } par = temp; } k = 2; for (int i = 1; i <= n; i++) { if (t[i] > 1 && t[i - 1] > 1) { p2[k]++; break; } else k += t[i]; } for (int i = 1; i <= sum; i++) cout << p[i] << ; cout << n ; for (int i = 1; i <= sum; i++) cout << p2[i] << ; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long sum1 = 0, sum2 = 0; long temp; for (int i = 0; i < n; i++) { cin >> temp; sum1 += temp; } for (int i = 0; i < n - 1; i++) { cin >> temp; sum2 += temp; } cout << sum1 - sum2 << n ; for (int i = 0; i < n - 2; i++) { cin >> temp; sum2 -= temp; } cout << sum2; return (0); } |
#include <bits/stdc++.h> using namespace std; const int N = (1 << 20) + 15; long long dp[N]; pair<int, int> id[100]; int bit[100], c[100], st[N]; int main() { int u, a, mi, n, m, i, j, b, x, p = 0, idd; long long k, temp; long long ret = 4e18; scanf( %d%d%d , &n, &m, &b); for (i = 0; i < n; ++i) { scanf( %d%d%d , &c[i], &id[i].first, &mi); a = 0; for (j = 0; j < mi; ++j) scanf( %d , &u), a |= 1 << (u - 1); bit[i] = a; id[i].second = i; } memset(dp, -1, sizeof(dp)); sort(id, id + n); dp[0] = 0; ++p; for (i = 0; i < n; ++i) { k = id[i].first, idd = id[i].second; if (dp[bit[idd]] != -1 && dp[bit[idd]] <= c[idd]) continue; x = p; for (j = 0; j < p; ++j) { u = st[j] | bit[idd]; if (dp[u] == -1 && u != (1 << m) - 1) st[x++] = u; if (dp[u] == -1 || dp[u] > dp[st[j]] + c[idd]) dp[u] = dp[st[j]] + c[idd]; } if (dp[(1 << m) - 1] != -1) ret = min(ret, k * b + dp[(1 << m) - 1]); p = x; } if (ret == 4e18) ret = -1; cout << ret << endl; } |
#include <bits/stdc++.h> using namespace std; int cnt[1000100], a[1000100]; int main() { int n, k, ans; scanf( %d , &n); scanf( %d , &k); for (long long i = 0; i < n; i++) scanf( %d , &a[i]); int l = 0, r = 0, al, ar; set<int> x; cnt[a[0]]++; x.insert(a[0]); ans = 1; al = 0, ar = 0; while (l < n && r < n) { r++; x.insert(a[r]); cnt[a[r]]++; while ((long long)x.size() > k && l <= r) { cnt[a[l]]--; if (cnt[a[l]] == 0) x.erase(a[l]); l++; } r = max(l, r); if (r >= n) break; if (r - l + 1 > ans) { al = l; ar = r; ans = r - l + 1; } } printf( %d %d n , al + 1, ar + 1); return 0; } |
#include <bits/stdc++.h> using namespace std; const int M = 105; int l[M], r[M], t[M], c[M], res[M], p[M]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; ++i) { cin >> l[i] >> r[i] >> t[i] >> c[i]; } for (int i = 1; i <= n; ++i) p[i] = INT_MAX; for (int i = 0; i < m; ++i) for (int j = l[i]; j <= r[i]; ++j) if (t[i] < p[j]) p[j] = t[i], res[j] = i; int sum = 0; for (int i = 1; i <= n; ++i) if (p[i] != INT_MAX) sum += c[res[i]]; cout << sum << endl; } |
#include <bits/stdc++.h> using namespace std; const long double EPS = 1e-9, pi = acos(-1); const long long N = 1e5 + 5; long long r[N], h[N], n, F[N], dp[N]; bool cmp(pair<long long, long long> a, pair<long long, long long> b) { if (a.first == b.first) { return a.second < b.second; } else return a.first < b.first; } void add(long long d, long long id) { while (id <= n) { F[id] = max(F[id], d); id += (id & -id); } } long long get(long long i) { long long res = 0; while (i) { res = max(res, F[i]); i -= (i & -i); } return res; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (long long i = 1; i <= n; ++i) cin >> r[i] >> h[i]; vector<pair<long long, long long> > A; A.clear(); for (long long i = 1; i <= n; ++i) { A.push_back({r[i] * r[i] * h[i], i}); } sort(A.begin(), A.end(), cmp); long long res = 0, id = 0, prev = A[0].first; for (long long i = 0; i < n; ++i) { if (prev != A[i].first) { while (id < i) { add(dp[id], A[id].second); id++; } } dp[i] = get(A[i].second) + A[i].first; res = max(res, dp[i]); prev = A[i].first; } cout << fixed << setprecision(9) << pi * res << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; string str; for (int i = 0; i < t; i++) { cin >> str; long long int res = 0; long long int arr[str.size()]; if (str[0] == + ) { arr[0] = 1; } else { arr[0] = -1; } for (int j = 1; j < str.size(); j++) { if (str[j] == + ) { arr[j] = arr[j - 1] + 1; } else { arr[j] = arr[j - 1] - 1; } } int x = -1; for (int j = 0; j < str.size(); j++) { if (arr[j] == x) { res = res + j + 1; x--; } } res = res + str.size(); cout << res << endl; } return 0; } |
#include <bits/stdc++.h> template <typename T> void MACRO_VAR_Scan(T& t) { std::cin >> t; } template <typename First, typename... Rest> void MACRO_VAR_Scan(First& first, Rest&... rest) { std::cin >> first; MACRO_VAR_Scan(rest...); } template <typename T> void MACRO_VEC_ROW_Init(int n, T& t) { t.resize(n); } template <typename First, typename... Rest> void MACRO_VEC_ROW_Init(int n, First& first, Rest&... rest) { first.resize(n); MACRO_VEC_ROW_Init(n, rest...); } template <typename T> void MACRO_VEC_ROW_Scan(int p, T& t) { std::cin >> t[p]; } template <typename First, typename... Rest> void MACRO_VEC_ROW_Scan(int p, First& first, Rest&... rest) { std::cin >> first[p]; MACRO_VEC_ROW_Scan(p, rest...); } template <typename T> inline T CHMAX(T& a, const T b) { return a = (a < b) ? b : a; } template <typename T> inline T CHMIN(T& a, const T b) { return a = (a > b) ? b : a; } void CHECKTIME(std::function<void()> f) { auto start = std::chrono::system_clock::now(); f(); auto end = std::chrono::system_clock::now(); auto res = std::chrono::duration_cast<std::chrono::nanoseconds>((end - start)) .count(); std::cerr << [Time: << res << ns ( << res / (1.0e9) << s)] n ; } template <class T> std::vector<std::vector<T>> VV(int n, int m, T init = T()) { return std::vector<std::vector<T>>(n, std::vector<T>(m, init)); } template <typename S, typename T> std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) { os << ( << p.first << , << p.second << ) ; return os; } using ll = long long; using ull = unsigned long long; using ld = long double; using PAIR = std::pair<ll, ll>; using PAIRLL = std::pair<ll, ll>; constexpr ll INFINT = 1 << 30; constexpr ll INFINT_LIM = (1LL << 31) - 1; constexpr ll INFLL = 1LL << 60; constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62); constexpr double EPS = 1e-9; constexpr ll MOD = 1000000007; constexpr double PI = 3.141592653589793238462643383279; template <class T, size_t N> void FILL(T (&a)[N], const T& val) { for (auto& x : a) x = val; } template <class ARY, size_t N, size_t M, class T> void FILL(ARY (&a)[N][M], const T& val) { for (auto& b : a) FILL(b, val); } template <class T> void FILL(std::vector<T>& a, const T& val) { for (auto& x : a) x = val; } template <class ARY, class T> void FILL(std::vector<std::vector<ARY>>& a, const T& val) { for (auto& b : a) FILL(b, val); } namespace FFT { const double pi = std::acos(-1); std::vector<std::complex<double>> tmp; size_t sz = 1; std::vector<std::complex<double>> fft(std::vector<std::complex<double>> a, bool inv = false) { size_t mask = sz - 1; size_t p = 0; for (size_t i = sz >> 1; i >= 1; i >>= 1) { auto& cur = (p & 1) ? tmp : a; auto& nex = (p & 1) ? a : tmp; std::complex<double> e = std::polar(1., 2 * pi * i * (inv ? -1 : 1) / sz); std::complex<double> w = 1; for (size_t j = 0; j < sz; j += i) { for (size_t k = 0; k < i; ++k) { nex[j + k] = cur[((j << 1) & mask) + k] + w * cur[(((j << 1) + i) & mask) + k]; } w *= e; } ++p; } if (p & 1) std::swap(a, tmp); if (inv) for (size_t i = 0; i < sz; ++i) a[i] /= sz; return a; } std::vector<ll> mul(std::vector<ll> a, std::vector<ll> b) { size_t m = a.size() + b.size() - 1; sz = 1; while (m > sz) sz <<= 1; tmp.resize(sz); std::vector<std::complex<double>> A(sz), B(sz); for (size_t i = 0; i < a.size(); ++i) A[i].real(a[i]); for (size_t i = 0; i < b.size(); ++i) B[i].real(b[i]); A = fft(A); B = fft(B); for (size_t i = 0; i < sz; ++i) A[i] *= B[i]; A = fft(A, true); a.resize(m); for (size_t i = 0; i < m; ++i) a[i] = std::round(A[i].real()); return a; } }; // namespace FFT signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); ; ll n, x; MACRO_VAR_Scan(n, x); ; std::vector<ll> a(n); for (auto& i : a) std::cin >> i; ; for (ll i = 0; i < ll(n); ++i) a[i] = a[i] < x; std::vector<ll> b; { ll cnt = 0; for (ll i = 0; i < ll(n + 1); ++i) { if (i == n || a[i]) { b.emplace_back(cnt); cnt = 0; } else { ++cnt; } } } ll m = b.size(); { ll ans = 0; for (ll i = 0; i < ll(m); ++i) { ans += (ll)b[i] * (b[i] + 1) / 2; } std::cout << (ans); ; } { for (ll i = 0; i < ll(m); ++i) ++b[i]; auto bb(b); std::reverse((bb).begin(), (bb).end()); auto c = FFT::mul(b, bb); std::vector<ll> ans(n, 0); ll p = 0; for (ll i = ll(m - 1) - 1; i >= 0; --i) { ans[p++] = c[i]; } for (ll i = 0; i < ll(n); ++i) { std::cout << ; std::cout << (ans[i]); ; } std::cout << n ; ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; for (int i = 0; i < k; i++) { if (n % 10 != 0) { n = n - 1; } else if (n % 10 == 0) { n = n / 10; } } cout << n; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 100; template <class nmsl> inline void read(nmsl &x) { x = 0; char ch = getchar(), w = 0; while (!isdigit(ch)) w = (ch == - ), ch = getchar(); while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); x = w ? -x : x; } long long n, f[N], size[N], root[N], hd[N], bianshu, ans; struct abc { int nxt, to; } qwq[N * 2]; inline void addedg(int lai, int qu) { qwq[++bianshu].nxt = hd[lai]; qwq[bianshu].to = qu; hd[lai] = bianshu; } inline void first_dp(int u, int father) { size[u] = 1; for (register int i = hd[u]; i; i = qwq[i].nxt) { int v = qwq[i].to; if (v == father) continue; first_dp(v, u); size[u] += size[v]; f[u] += f[v]; } f[u] += size[u]; return; } inline void dp(int u, int father) { f[u] = 0; for (register int i = hd[u]; i; i = qwq[i].nxt) { int v = qwq[i].to; f[u] += f[v]; } long long tot = f[u] + n; ans = max(ans, tot); for (register int i = hd[u]; i; i = qwq[i].nxt) { int v = qwq[i].to; if (v == father) continue; size[u] = n - size[v]; f[u] = tot - f[v] - size[v]; dp(v, u); } } int main() { read(n); for (register int i = 1, qa, qb; i < n; i++) read(qa), read(qb), addedg(qa, qb), addedg(qb, qa); first_dp(1, 0); ans = f[1]; dp(1, 0); cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; const int M = 3000; int n, m; pair<pair<int, int>, int> v[M]; map<pair<pair<int, int>, int>, vector<pair<pair<int, int>, int> > > Q; int gcd(int a, int b) { return !b ? a : gcd(b, a % b); } pair<pair<int, int>, int> get(int x1, int y1, int x2, int y2) { int x = x2 - x1, y = y2 - y1; if (x == 0) y = 1; else if (y == 0) x = 1; else { int d = gcd(x, y); x /= d, y /= d; } if (x < 0) x = -x, y = -y; return make_pair(make_pair(x, y), y1 * x - x1 * y); } int sign(double x) { if (fabs(x) < 1E-6) return 0; return (x > 0) ? 1 : -1; } double dist(int x1, int y1, int x2, int y2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) { if (a.first != b.first) return a.first < b.first; return a.second > b.second; } int main() { Q.clear(); scanf( %d%d , &n, &m); for (int i = 0; i < (n); i++) { int x1, y1, x2, y2; scanf( %d%d%d%d , &x1, &y1, &x2, &y2); x1 *= 2, y1 *= 2, x2 *= 2, y2 *= 2; if (make_pair(x1, y1) > make_pair(x2, y2)) { swap(x1, x2); swap(y1, y2); } pair<pair<int, int>, int> tmp = get(x1, y1, x2, y2); Q[tmp].push_back(make_pair(make_pair(x1, y1), 1)); Q[tmp].push_back(make_pair(make_pair(x2, y2), -1)); } for (int i = 0; i < (m); i++) { scanf( %d%d%d , &v[i].first.first, &v[i].first.second, &v[i].second); for (int j = 0; j < (i); j++) { if (v[i].second != v[j].second) continue; int x1 = v[i].first.first, y1 = v[i].first.second, r = v[i].second; int x2 = v[j].first.first, y2 = v[j].first.second; if (sign(2 * r - dist(x1, y1, x2, y2)) >= 0) continue; int x0 = x1 + x2, y0 = y1 + y2; int x = x2 * 2 - x0, y = y2 * 2 - y0; pair<pair<int, int>, int> tmp = get(x0, y0, x0 - y, y0 + x); if (Q.find(tmp) == Q.end()) continue; Q[tmp].push_back(make_pair(make_pair(x0, y0), 0)); } } int ret = 0; for (__typeof(Q.begin()) it = Q.begin(); it != Q.end(); it++) { sort(it->second.begin(), it->second.end(), cmp); int now = 0; for (__typeof(it->second.begin()) t = it->second.begin(); t != it->second.end(); t++) { if (t->second == 0) ret += now; now += t->second; } } printf( %d n , ret); return 0; } |
#include <bits/stdc++.h> using namespace std; int mpow(int base, int exp); void solve() { long double n; cin >> n; long double sum = 0; while (n) { long double d; d = (1) * 1.0 / n; sum += d; n--; } cout << sum << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; solve(); return 0; } int mpow(int base, int exp) { base %= 1000000007; int result = 1; while (exp > 0) { if (exp & 1) result = ((long long int)result * base) % 1000000007; base = ((long long int)base * base) % 1000000007; exp >>= 1; } return result; } |
#include <bits/stdc++.h> using namespace std; long long unsigned int countDivisors(long long unsigned int n) { long long unsigned int cnt = 0; for (long long unsigned int i = 1; i <= sqrt(n); i++) { if (n % i == 0) { if (n / i == i) ++cnt; else cnt += 2; } } return cnt; } int Search(string pat, string txt) { int n = txt.size(); int m = pat.size(); for (int i = 0; i <= n - m; i++) { int j; for (j = 0; j < m; j++) { if (txt[i + j] != pat[j]) { break; } } if (j == m) { return i; } } return -1; } int findFirst(const std::vector<long long int> &array, int key, int low, int high) { int ans = -1; while (low <= high) { int mid = low + (high - low) / 2; if (key == array[mid]) { ans = mid; high = mid - 1; } else if (key > array[mid]) low = mid + 1; else high = mid - 1; } return ans; } int findLast(const std::vector<long long int> &array, int key, int low, int high) { int ans = -1; while (low <= high) { int mid = low + (high - low) / 2; if (key == array[mid]) { ans = mid; low = mid + 1; } else if (key > array[mid]) low = mid + 1; else high = mid - 1; } return ans; } int d, x, y; void extendedEuclid(int a, int b) { if (b == 0) { d = 1; x = 1; y = 0; } else { extendedEuclid(b, a % b); int temp = x; x = y; y = temp - (a / b) * y; } } int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int t; t = 1; for (int tc = 0; tc < t; tc++) { int n; cin >> n; vector<int> arr(n), brr(n, 0), v; for (int i1 = 0; i1 < n; i1++) cin >> arr[i1]; int pos = 0; for (int i = 0; i < n; i++) { int indx = n - arr[i]; brr[indx] = 1; while (brr[pos] == 1 && pos < n) { cout << (n - pos) << ; pos++; } cout << endl; } } return 0; } |
#include <bits/stdc++.h> using namespace std; long long mp[1005][1005]; long long n, a, b; void output() { for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= n; j++) { if (i == j) printf( 0 ); else printf( %lld , a < b ? !mp[i][j] : mp[i][j]); } printf( n ); } } int main() { scanf( %lld%lld%lld , &n, &a, &b); long long root = 2; if ((a != 1 && b != 1)) { printf( NO n ); return 0; } if (n == 2 && a == 1 && b == 1) { printf( NO n ); return 0; } if (n == 3 && a == 1 && b == 1) { printf( NO n ); return 0; } printf( YES n ); if (a == 1 && b == 1) for (long long i = 1; i < n; i++) mp[i][i + 1] = mp[i + 1][i] = true; else for (long long i = 0; i < n - max(a, b); i++, root++) mp[1][root] = mp[root][1] = true; output(); } |
#include <bits/stdc++.h> using namespace std; long long power(long long a, long long b) { long long ans = 1; while (b > 0) { if (b % 2 != 0) { ans = (ans * a) % 1000000007; } a = ((a % 1000000007) * (a % 1000000007)) % 1000000007; b >>= 1; } return ans; } void sieve(int n) { bool prime[n + 1]; memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { if (prime[p]) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int p = 2; p <= n; p++) { if (prime[p]) cout << p << ; } } int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int t = 1; cin >> t; while (t--) { int n, k; cin >> n >> k; vector<vector<int>> v; for (int i = 0; i < k; i++) { int c; cin >> c; vector<int> temp; for (int j = 0; j < c; j++) { int z; cin >> z; temp.push_back(z); } v.push_back(temp); } int l = 1, h = n; int ma; cout << ? << << n << ; for (int i = 1; i <= n; i++) cout << i << ; cout << endl; cin >> ma; for (int i = 0; i < 10; i++) { int mid = (h + l) / 2; int temp; cout << ? << << mid - l + 1 << ; for (int kha = l; kha <= mid; kha++) cout << kha << ; cout << endl; cin >> temp; if (temp == ma) h = mid; else l = mid; } int ind = h; bool flag = false; vector<int> ans; for (int i = 0; i < v.size(); i++) { sort(v[i].begin(), v[i].end()); for (int j = 0; j < v[i].size(); j++) { if (v[i][j] == ind) { flag = true; cout << ? << ; vector<int> loa; for (int lo = 1; lo <= n; lo++) { if (binary_search(v[i].begin(), v[i].end(), lo) == false) { loa.push_back(lo); } } cout << loa.size() << ; for (auto lo : loa) cout << lo << ; cout << endl; int kha; cin >> kha; ans.push_back(kha); } } if (flag == false) { ans.push_back(ma); } else { flag = false; } } cout << ! << ; for (auto i : ans) cout << i << ; cout << endl; string sa; cin >> sa; if (sa == Correct ) { continue; } else { return 0; } } return 0; } |
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const int maxn = 2e6 + 10; int a[maxn], cnt[maxn]; long long sum[maxn]; int main(void) { int n, x, y; scanf( %d %d %d , &n, &x, &y); int z = x / y; for (int i = 1; i <= n; i++) scanf( %d , a + i), cnt[a[i]]++, sum[a[i]] += a[i]; for (int i = 1; i <= 2000000; i++) cnt[i] += cnt[i - 1], sum[i] += sum[i - 1]; long long ans = INF; for (int i = 2; i <= 1000000; i++) { long long tmp = 0; int zz = min(z, i - 1); for (int j = 1; (j - 1) * i < 1000000; j++) { if (i - zz - 2 >= 0) tmp += (long long)(cnt[j * i - zz - 1] - cnt[j * i - i]) * x; if (zz) tmp += ((long long)i * j * (cnt[j * i - 1] - cnt[j * i - zz - 1]) - (sum[j * i - 1] - sum[j * i - zz - 1])) * y; } ans = min(ans, tmp); } printf( %I64d n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; const int md = 1e9 + 7; int n; char s[111]; int f[111][3333]; int main() { int t; f[0][0] = 1; for (int i = 1; i <= 100; ++i) { for (int j = 0; j <= 3000; ++j) { for (int c = 0; c <= min(j, 25); ++c) { f[i][j] += f[i - 1][j - c]; f[i][j] %= md; } } } scanf( %d , &t); while (t--) { scanf( %s , s); int sum = 0; for (int i = 0; i < strlen(s); ++i) sum += s[i] - a ; printf( %d n , (f[strlen(s)][sum] + md - 1) % md); } return 0; } |
#include <bits/stdc++.h> #pragma GCC optimize ( O3 ) #pragma GCC target ( sse4 ) using namespace std; typedef long long ll; typedef long double ld; typedef complex<ld> cd; typedef pair<int, int> pi; typedef pair<ll,ll> pl; typedef pair<ld,ld> pd; typedef vector<int> vi; typedef vector<ld> vd; typedef vector<ll> vl; typedef vector<pi> vpi; typedef vector<pl> vpl; typedef vector<cd> vcd; template<class T> using pq = priority_queue<T>; template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>; #define FOR(i, a, b) for (int i=a; i<(b); i++) #define F0R(i, a) for (int i=0; i<(a); i++) #define FORd(i,a,b) for (int i = (b)-1; i >= a; i--) #define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--) #define trav(a,x) for (auto& a : x) #define uid(a, b) uniform_int_distribution<int>(a, b)(rng) #define sz(x) (int)(x).size() #define mp make_pair #define pb push_back #define f first #define s second #define lb lower_bound #define ub upper_bound #define all(x) x.begin(), x.end() #define ins insert template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << << x << ;} void __print(const char *x) {cerr << << x << ;} void __print(const string &x) {cerr << << x << ;} void __print(bool x) {cerr << (x ? true : false );} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << { ; __print(x.first); cerr << , ; __print(x.second); cerr << } ;} template<typename T> void __print(const T &x) {int f = 0; cerr << { ; for (auto &i: x) cerr << (f++ ? , : ), __print(i); cerr << } ;} void _print() {cerr << ] n ;} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << , ; _print(v...);} #ifdef DEBUG #define dbg(x...) cerr << [91m <<__func__<< : <<__LINE__<< [ << #x << ] = [ ; _print(x); cerr << [39m << endl; #else #define dbg(x...) #endif const int MOD = 1000000007; const char nl = n ; const int MX = 200001; int dist[MX]; int A[MX]; set<pi> rem; pqg<pi> q; void add(int p, int pre, bool ty) { auto it1 = rem.lb({p, -1}); if (it1 != rem.end()) { int v = it1->s; int d = max(pre, abs(A[v]-p)); if (ty) { d = pre + abs(A[v]-p); } if (d < dist[v]) { dist[v] = d; q.push({d, v}); } } if (it1 != rem.begin()) { it1--; int v = it1->s; int d = max(pre, abs(A[v]-p)); if (ty) { d = pre + abs(A[v]-p); } if (d < dist[v]) { dist[v] = d; q.push({d, v}); } } } void push(int p, int K) { while (rem.lb({p, -1}) != rem.end() && rem.lb({p, -1})->f <= p+K) { int x = rem.lb({p, -1})->s; rem.erase({A[x], x}); q.push({K, x}); dist[x] = K; } auto it = rem.lb({p, -1}); while (it != rem.begin()) { it--; if (it->f < p-K) break; int x = it->s; rem.erase({A[x], x}); q.push({K, x}); dist[x] = K; it = rem.lb({p, -1}); } } void solve() { int N, Q; cin >> N >> Q; F0R(i, N) dist[i] = 100000000; int S; cin >> S; S--; int D; cin >> D; F0R(i, N) { cin >> A[i]; if (i != S) rem.ins({A[i], i}); } dist[S] = 0; add(A[S]+D, 0, true); add(A[S]-D, 0, true); bool vis[MX]; F0R(i, N) vis[i] = false; while (!q.empty()) { int v = q.top().s; int d = q.top().f; q.pop(); if (vis[v]) continue; vis[v] = true; //dbg(v, d); rem.erase({A[v], v}); add(A[v], d, true); push(A[v]+D, d); push(A[v]-D, d); add(A[v]+D, 0, true); add(A[v]-D, 0, true); } /*F0R(i, N) { cout << dist[i] << ; } cout << nl;*/ while(Q--) { int X, V; cin >> X >> V; X--; cout << (dist[X]<=V? Yes : No ) << nl; } } int main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); int T = 1; // cin >> T; while(T--) { solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; long long k, b, n, t; int main() { ios_base ::sync_with_stdio(0); cin.tie(0); cin >> k >> b >> n >> t; long long num = 1; long long step = n; while (t >= num) { num = num * k + b; step--; } if (step < 0) { cout << 0; return 0; } cout << step + 1; return 0; } |
#include <bits/stdc++.h> const long double pi = 3.14159265359; const long long N = 1e9 + 7; using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m, arr[200005]; cin >> n >> m; long long cntne = 0, cntpo = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; if (arr[i] == -1) cntne++; else cntpo++; } vector<long long> v; for (int i = 0; i < m; i++) { long long l, r; cin >> l >> r; r++; long long ans = abs(l - r); if ((ans % 2 == 0) && (cntne >= ans / 2) && (cntpo >= ans / 2)) v.push_back(1); else v.push_back(0); } for (int i = 0; i < ((long long)((v).size())); i++) cout << v[i] << endl; return 0; } |
#include <iostream> #include <vector> #include <algorithm> #include <string> #define ll long long using namespace std; int main() { ll qua; cin >> qua; for (int i = 0; i < qua; ++i) { ll n; cin >> n; vector<int> vec(n); for (int j = 0; j < n; ++j) { cin >> vec[j]; } cout << 0 << ; for (int j = 1; j < n; ++j) { ll r = (vec[j] | vec[j - 1]) - vec[j]; cout << r << ; vec[j] += r; } cout << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int MaxN = 100005; const int MaxM = MaxN * 2; const int oo = 0x3F3F3F3F; int ax[MaxM], K; int ret[MaxN]; set<pair<int, int> > tr[MaxM]; struct event { int l, r, v, tp; event() {} event(int l, int r, int v, int tp) : l(l), r(r), v(v), tp(tp) {} bool operator<(const event& e) const { if (r != e.r) return r > e.r; else return tp < e.tp; } } e[MaxM]; int getId(int v) { return lower_bound(ax, ax + K, v) - ax + 1; } void add(int k, pair<int, int> v) { while (k <= K) { tr[k].insert(v); k += k & -k; } } int read(int k, int v) { pair<int, int> ret = make_pair(oo, -1); while (k) { set<pair<int, int> >::iterator it = tr[k].lower_bound(make_pair(v, 0)); if (it != tr[k].end()) ret = min(ret, *it); k -= k & -k; } return ret.second; } int main() { int N, M, en; int l, r, t; scanf( %d%d , &N, &M); K = en = 0; for (int i = 0; i < N; i++) { scanf( %d%d%d , &l, &r, &t); e[en++] = event(l, r, t, -i - 1); ax[K++] = l; } for (int i = 0; i < M; i++) { scanf( %d%d%d , &l, &r, &t); e[en++] = event(l, r, t, i); ax[K++] = l; } sort(ax, ax + K); K = unique(ax, ax + K) - ax; sort(e, e + en); for (int i = 0; i < en; i++) { if (e[i].tp < 0) { add(getId(e[i].l), make_pair(e[i].v, -e[i].tp)); } else { ret[e[i].tp] = read(getId(e[i].l), e[i].v); } } for (int i = 0; i < M; i++) { printf( %d , ret[i]); } puts( ); return 0; } |
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> std::istream& operator>>(std::istream& i, pair<T, U>& p) { i >> p.first >> p.second; return i; } template <typename T> std::istream& operator>>(std::istream& i, vector<T>& t) { for (auto& v : t) { i >> v; } return i; } template <typename T, typename U> std::ostream& operator<<(std::ostream& o, const pair<T, U>& p) { o << p.first << << p.second; return o; } template <typename T> std::ostream& operator<<(std::ostream& o, const vector<T>& t) { if (t.empty()) o << n ; for (size_t i = 0; i < t.size(); ++i) { o << t[i] << n [i == t.size() - 1]; } return o; } template <typename T> using minheap = priority_queue<T, vector<T>, greater<T>>; template <typename T> using maxheap = priority_queue<T, vector<T>, less<T>>; unsigned int logceil(long long first) { return first ? 8 * sizeof(long long) - __builtin_clzll(first) : 0; } namespace std { template <typename T, typename U> struct hash<pair<T, U>> { hash<T> t; hash<U> u; size_t operator()(const pair<T, U>& p) const { return t(p.first) ^ (u(p.second) << 7); } }; } // namespace std template <typename T, typename F> T bsh(T l, T h, const F& f) { T r = -1, m; while (l <= h) { m = (l + h) / 2; if (f(m)) { l = m + 1; r = m; } else { h = m - 1; } } return r; } template <typename F> double bshd(double l, double h, const F& f, double p = 1e-9) { unsigned int r = 3 + (unsigned int)log2((h - l) / p); while (r--) { double m = (l + h) / 2; if (f(m)) { l = m; } else { h = m; } } return (l + h) / 2; } template <typename T, typename F> T bsl(T l, T h, const F& f) { T r = -1, m; while (l <= h) { m = (l + h) / 2; if (f(m)) { h = m - 1; r = m; } else { l = m + 1; } } return r; } template <typename F> double bsld(double l, double h, const F& f) { unsigned int r = 200; while (r--) { double m = (l + h) / 2; if (f(m)) { h = m; } else { l = m; } } return (l + h) / 2; } template <typename T> T gcd(T a, T b) { if (a < b) swap(a, b); return b ? gcd(b, a % b) : a; } template <typename T> class vector2 : public vector<vector<T>> { public: vector2() {} vector2(size_t a, size_t b, T t = T()) : vector<vector<T>>(a, vector<T>(b, t)) {} }; template <typename T> class vector3 : public vector<vector2<T>> { public: vector3() {} vector3(size_t a, size_t b, size_t c, T t = T()) : vector<vector2<T>>(a, vector2<T>(b, c, t)) {} }; template <typename T> class vector4 : public vector<vector3<T>> { public: vector4() {} vector4(size_t a, size_t b, size_t c, size_t d, T t = T()) : vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {} }; template <typename T> class vector5 : public vector<vector4<T>> { public: vector5() {} vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T()) : vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {} }; class GOmkarAndPies { public: vector<std::pair<int, int>> D; int N, M, K; void fix(int p) { for (int i = 0; i < K; ++i) { int s = p ^ 1 << i; if (D[s].first > D[p].first + 1) { D[s] = {D[p].first + 1, D[p].second}; fix(s); } } }; void solve(istream& cin, ostream& cout) { cin >> N >> M >> K; string S, T; cin >> S >> T; vector<std::pair<int, int>> P(N); cin >> P; for (std::pair<int, int>& p : P) { p.first--; p.second--; } vector<int> X(K); iota(X.begin(), X.end(), 0); vector<vector<int>> W{X}; for (int i = 0; i < N; ++i) { swap(X[P[i].first], X[P[i].second]); vector<int> I(K); for (int j = 0; j < K; ++j) I[X[j]] = j; W.push_back(X); } auto apply = [&](int first, string S) { int p = 0; for (int i = 0; i < K; ++i) { p |= (S[i] - 0 ) << W[first][i]; } return p; }; D.assign(1 << K, {K + 1, -1}); pair<int, std::pair<int, int>> ans{K + 1, {-1, -1}}; for (int i = N - M; i >= 0; --i) { int TX = apply(i + M, T); D[TX] = {0, i + M}; fix(TX); int SX = apply(i, S); ans = min(ans, {D[SX].first, {i + 1, D[SX].second}}); } cout << K - ans.first << n << ans.second << n ; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); GOmkarAndPies solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; cout << (regex_match(s, regex( (1|14|144)* )) ? YES : NO ); } |
#include <bits/stdc++.h> using namespace std; const int N = 3e3 + 5; int dw[N], up[N], E[N][2], Mn; vector<pair<int, int> > g[N]; void dfs_down(int v, int p) { dw[v] = 0; for (pair<int, int> u : g[v]) if (u.first != p) dfs_down(u.first, v), dw[v] += dw[u.first] + u.second; } void dfs_up(int v, int p, int f = -1) { if (f != -1) { up[v] = up[p] + dw[p] - dw[v] + f - (f == 0); } else up[v] = 0; Mn = min(Mn, dw[v] + up[v]); for (pair<int, int> u : g[v]) if (u.first != p) dfs_up(u.first, v, u.second ^ 1); } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin >> n; if (n == 1) return cout << 0 n , 0; for (int i = 0; i < n - 1; i++) { cin >> E[i][0] >> E[i][1]; g[E[i][0]].push_back({E[i][1], 0}); g[E[i][1]].push_back({E[i][0], 1}); } int ans = n; for (int i = 0; i < n - 1; i++) { Mn = n, dfs_down(E[i][0], E[i][1]), dfs_up(E[i][0], E[i][1]); int cur = Mn; Mn = n, dfs_down(E[i][1], E[i][0]), dfs_up(E[i][1], E[i][0]); cur += Mn; ans = min(ans, cur); } cout << ans << n ; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << ceil(n / 2.0) - 1; } |
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int y[N], pos[N]; int main() { int n, m, t = 0, ans = 0, cnt = 0; scanf( %d %d , &n, &m); for (int i = 1; i <= n; i++) scanf( %d , &y[i]); for (int i = 1; i <= m; i++) { int y1, y2, x; scanf( %d %d %d , &y1, &y2, &x); if (y1 == 1) { pos[++t] = y2; } if (y2 == 1e9) { ans++; } } sort(y + 1, y + 1 + n); sort(pos + 1, pos + 1 + t); int i = 1, j = 1; cnt = t, ans += n; while (i <= n) { while (j <= t) { if (pos[j] < y[i]) { j++, cnt--; } else break; } ans = min(ans, cnt + i - 1); i++; } cout << ans; } |
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while (c < 0 || c > 9 ) { if (c == - ) f = -1; c = getchar(); } while (c >= 0 && c <= 9 ) x = (x << 3) + (x << 1) + (c ^ 0 ), c = getchar(); return x * f; } const int N = 505, mod = 1e9 + 7; inline int inc(int x, int y) { x += y - mod; return x + (x >> 31 & mod); } inline int mul(int x, int y) { return 1ll * x * y % mod; } inline void Inc(int &x, int y) { x = inc(x, y); } int n, a[N], f[N][N]; int main() { n = read(); for (int i = 1; i <= n; i++) a[i] = read(), f[i][i] = 1; for (int i = 1; i <= n + 1; i++) f[i][i - 1] = 1; for (int len = 2; len < n; len++) for (int l = 1; l <= n - len + 1; l++) { int r = l + len - 1; f[l][r] = f[l + 1][r]; for (int k = l + 1; k <= r; k++) if (a[k] > a[l]) Inc(f[l][r], mul(f[l + 1][k - 1], f[k][r])); } printf( %d n , f[2][n]); return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 2001; char a[MAXN][MAXN]; int n, v[MAXN], r[MAXN], c[MAXN]; int gao() { int ret = 0; memset(r, 0, sizeof(int) * n); memset(c, 0, sizeof(int) * n); for (int i = 0; i < n - 1; ++i) { for (int j = n - 1; j > i; --j) { if (a[i][j] ^ r[i] ^ c[j]) { ++ret; r[i] ^= 1; c[j] ^= 1; } } } for (int i = 0; i < n; ++i) v[i] ^= (r[i] ^ c[i]); memset(r, 0, sizeof(int) * n); memset(c, 0, sizeof(int) * n); for (int i = n - 1; i > 0; --i) { for (int j = 0; j < i; ++j) { if (a[i][j] ^ r[i] ^ c[j]) { ++ret; r[i] ^= 1; c[j] ^= 1; } } } for (int i = 0; i < n; ++i) v[i] ^= (r[i] ^ c[i]); for (int i = 0; i < n; ++i) ret += v[i]; return ret; } int main() { while (scanf( %d , &n) != EOF) { memset(v, 0, sizeof(int) * n); for (int i = 0; i < n; ++i) { scanf( %s , a[i]); for (int j = 0; j < n; ++j) a[i][j] -= 0 ; v[i] ^= (int)a[i][i]; } printf( %d n , gao()); } return 0; } |
#include<bits/stdc++.h> using namespace std; typedef double db; typedef uint32_t u32; typedef uint64_t u64; typedef pair<int, int> pii; typedef pair<int, int> point; #define fi first #define se second #define pb(x) push_back(x) #define SZ(x) ((int) (x).size()) #define all(x) (x).begin(), (x).end() #define rep(i, l, r) for(int i = l; i < r; i++) typedef long long ll; const int mod = 998244353; int main() { #ifdef local freopen( in.txt , r , stdin); #endif ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); const int mod = 1e9 + 7; int n; cin >> n; vector<int> a(n); ll tot = 0; for(auto &e : a) { cin >> e; tot += e; } if(tot % n) { cout << 0 n ; return 0; } vector<int> fac(n + 1, 1); vector<int> ifac(n + 1, 1); for(int i = 2; i <= n; i++) { fac[i] = (ll) fac[i - 1] * i % mod; ifac[i] = (ll) ifac[mod % i] * (mod - mod / i) % mod; } for(int i = 2; i <= n; i++) { ifac[i] = (ll) ifac[i] * ifac[i - 1] % mod; } auto C = [&](int n, int m) { if(n < m || m < 0) return 0ll; return (ll) fac[n] * ifac[m] % mod * ifac[n - m] % mod; }; ll avg = tot / n; vector<int> l, r; int eq = 0; for(auto &e : a) { if(e > avg) { r.push_back(e); } else if(e < avg) { l.push_back(e); } else { eq++; } } int ans = 1; if(l.size() > 1 && r.size() > 1) { ans = 2; } if(l.size() == 1) { r.push_back(l[0]); l.clear(); } if(r.size() == 1) { l.push_back(r[0]); r.clear(); } sort(all(l)); sort(all(r)); int a1 = fac[l.size()]; int a2 = fac[r.size()]; for(int i = 0, j = 0; i < (int) l.size(); i = j) { while(j < (int) l.size() && l[j] == l[i]) { j++; } a1 = (ll) a1 * ifac[j - i] % mod; } for(int i = 0, j = 0; i < (int) r.size(); i = j) { while(j < (int) r.size() && r[j] == r[i]) { j++; } a2 = (ll) a2 * ifac[j - i] % mod; } int a3 = C(n, eq); ans = (ll) ans * a1 % mod; ans = (ll) ans * a2 % mod; ans = (ll) ans * a3 % mod; cout << ans << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int main(void) { int n; cin >> n; set<int> a, b; vector<pair<int, int> > ans(n); for (int i = 0; i < n; ++i) { cin >> ans[i].first >> ans[i].second; --ans[i].first; --ans[i].second; if (ans[i].first == -1) a.insert(i); if (ans[i].second == -1) b.insert(i); } while (a.size() > 1) { int node = *a.begin(); a.erase(a.begin()); int last = node; while (ans[last].second != -1) last = ans[last].second; auto nxt = b.begin(); if (*nxt == last) ++nxt; ans[*nxt].second = node; ans[node].first = *nxt; b.erase(nxt); } for (int i = 0; i < n; ++i) cout << ans[i].first + 1 << << ans[i].second + 1 << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n], sm = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sm += a[i]; } vector<int> ind, r; int need = sm / 2; if (a[0] > need) { cout << 1 << endl; cout << 1; return 0; } for (int i = 1; i < n; i++) { if ((2 * a[i]) <= a[0]) { ind.push_back(i + 1); r.push_back(a[i]); } } bool ok; int u = a[0]; for (auto x : r) { u += x; } if (u > need) { ok = true; } else { ok = false; } if (ok) { cout << ind.size() + 1 << endl; cout << 1 << ; for (auto x : ind) { cout << x << ; } } else { cout << 0; } } |
#include <bits/stdc++.h> using namespace std; int nbVoyages, coutSimple, coutLiaison, maxCartes, coutCarte; map<pair<string, string>, int> routes; int main() { ios_base::sync_with_stdio(false); cin >> nbVoyages >> coutSimple >> coutLiaison >> maxCartes >> coutCarte; long long total = 0; string derFin = ; for (int iVoyage = 0; iVoyage < nbVoyages; iVoyage++) { string a, b; cin >> a >> b; string der = b, pre = a; if (b < a) swap(a, b); if (routes.find({a, b}) == routes.end()) routes[{a, b}] = 0; if (derFin == pre) { routes[{a, b}] += coutLiaison; total += coutLiaison; } else { routes[{a, b}] += coutSimple; total += coutSimple; } derFin = der; } vector<int> couts; for (pair<pair<string, string>, int> route : routes) { couts.push_back(route.second); } sort(couts.begin(), couts.end()); for (int iFois = 0; iFois < maxCartes; iFois++) { if (!couts.empty() && couts.back() > coutCarte) { total += coutCarte - couts.back(); couts.pop_back(); } } cout << total << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const double eps = 1e-6; const int mod = 1000000007; int modpow(int x, int y, int md) { if (y == 0) return 1; int ret = modpow(x, y >> 1, md); ret = (long long)ret * ret % md; if (y & 1) ret = (long long)ret * x % md; return ret; } int n; int sum[5555]; vector<int> g[5555]; set<pair<int, int> > ans; void dfs(int x, int par = 0) { sum[x] = 1; for (auto to : g[x]) if (to != par) dfs(to, x); for (auto to : g[x]) if (to != par) sum[x] += sum[to]; vector<bool> ok(5555, false); ok[0] = true; for (auto to : g[x]) if (to != par) { for (int i = 5005; i >= 0; i--) if (ok[i] && i + sum[to] <= 5005) ok[i + sum[to]] = true; } for (int i = 1; i <= 5005; i++) if (ok[i] && n - i - 1 >= 1) ans.insert(make_pair(i, n - i - 1)), ans.insert(make_pair(n - i - 1, i)); } int main() { cin >> n; for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } dfs(1); cout << ans.size() << endl; for (auto p : ans) cout << p.first << << p.second << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; string s[3]; int N, M, p[3][1000005], a[3][1000005], b[1000005]; void genPrefix(int pat) { int i, k; p[pat][0] = 0; k = 0; i = 1; while (i < M) { if (s[pat][i] == s[pat][k]) { k++; p[pat][i] = k; i++; } else { if (k != 0) k = p[pat][k - 1]; else { p[pat][i] = 0; i++; } } } } void KMPSearch(int txt, int pat) { int i, k; genPrefix(pat); i = k = 0; while (i < N) { if (s[txt][i] == s[pat][k]) { i++; k++; a[txt][i - k] = i - 1; } if (k == M) { k = p[pat][k - 1]; } else if (i < N && s[txt][i] != s[pat][k]) { if (k != 0) { if (s[txt][i - 1] == s[pat][k]) a[txt][i - k] = max(i - 1, a[txt][k - 1]); k = p[pat][k - 1]; if (k > 0 && s[txt][i - 1] == s[pat][k]) a[txt][i - k] = max(i - 1, a[txt][k - 1]); } else i++; } } } int bs(int low, int high, int v) { int mid, x = -1; while (low <= high) { mid = (low + high) >> 1; if (b[mid] >= v) { x = mid; high = mid - 1; } else low = mid + 1; } return x; } pair<int, int> solve() { s[2] = s[0]; reverse(s[2].begin(), s[2].end()); memset(a, -1, sizeof(a)); KMPSearch(1, 2); KMPSearch(0, 1); b[0] = a[1][0]; int i, j = N, len, k, x; int ansx = -1, ansy = -1; for (i = 1; i <= N - 1; i++) b[i] = max(b[i - 1], a[1][i]); for (i = 0; i <= N - 2; i++) { j--; if (s[0][i] != s[1][j]) break; if (b[0] >= j - 1) { ansx = i; ansy = i + 1; } if (a[0][i + 1] <= 0 || i + 1 == N - 1) continue; len = min(N - 2, a[0][i + 1]) - i; k = bs(0, len, j - 1); if (k >= 0 && ansx < i) { ansx = i; ansy = i + 1 + k; } } return make_pair(ansx, ansy); } int main() { std::ios_base::sync_with_stdio(0); int cases, caseno = 0, n, i, j, k, cnt, m; getline(cin, s[0]); getline(cin, s[1]); N = s[0].size(); M = s[1].size(); if (N != M) { cout << -1 -1 << n ; return 0; } pair<int, int> ans = solve(); cout << ans.first << << ans.second << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, x; scanf( %d%d , &n, &x); if (n == 1) { printf( YES n ); printf( %d n , x); } else if (n == 2) { if (x == 0) printf( NO n ); else { printf( YES n ); printf( 0 %d n , x); } } else { printf( YES n ); int tmp = 0; int a = (1 << 17); int b = (1 << 18); for (int i = 0; i < n - 3; i++) { printf( %d , i); tmp ^= i; } printf( %d %d %d n , a, b, a ^ b ^ tmp ^ x); } return 0; } |
#include <bits/stdc++.h> using namespace std; bool psort(std::vector<long long int> p1, std::vector<long long int> p2) { return p1[0] < p2[0]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, m; cin >> n; std::vector<vector<long long int> > v(n, vector<long long int>(3)); for (int i = 0; i < n; ++i) { cin >> v[i][0]; } for (int i = 0; i < n; ++i) { cin >> v[i][1]; } for (int i = 0; i < n; ++i) { cin >> v[i][2]; } sort(v.begin(), v.end(), psort); cin >> m; long long int start[4] = {0}; for (int i = 0; i < m; ++i) { long long int k, t, flag = 0; cin >> t; while (start[t] < n) { if (v[start[t]][0] != -1) { if (v[start[t]][1] == t || v[start[t]][2] == t) { cout << v[start[t]][0] << ; v[start[t]][0] = -1; start[t]++; flag = 1; break; } } start[t]++; } if (flag == 0) cout << -1 << ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string fld; cin >> fld; int l = 1, r = 2 * n; int ans = -1; while (l <= r) { int mid = (l + r) >> 1; int posbl = 1; int back = -1, flag = 0; for (int i = 0; i < n; i++) { if (fld[i] == * ) if (i > back && flag == 0) back = i, flag = 1; if (fld[i] == P ) { if (back == -1) { back = i + mid; } else { if (i <= back) { back = max(back, i + mid); } else { if (flag == 1) { if (i - back > mid) { posbl = 0; break; } else if (i - back <= mid && mid <= (2 * (i - back))) back = max(i, (mid - (i - back)) / 2 + i); else { back = max(mid - (2 * (i - back)) + i, (mid - (i - back)) / 2 + i); } } else { back = i + mid; } } } flag = 0; } } if (flag == 1) posbl = 0; if (posbl == 1) { r = mid - 1; ans = mid; } else l = mid + 1; } cout << ans << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; char s[N]; char t1[7] = twone ; char t2[4] = one ; char t3[4] = two ; int main() { int T; scanf( %d , &T); while (T--) { scanf( %s , s); int ans = 0; int len = strlen(s); for (int i = 0; i < len; i++) { if (s[i] == t ) { int f = 0; for (int j = i; j <= i + 4; j++) { if (s[j] != t1[j - i]) { f = 1; break; } } if (f == 0) { s[i + 2] = - ; ans++; } } } for (int i = 0; i < len; i++) { if (s[i] == t ) { int f = 0; for (int j = i; j <= i + 2; j++) { if (s[j] != t3[j - i]) { f = 1; break; } } if (f == 0) { s[i + 1] = - ; ans++; } } } for (int i = 0; i < len; i++) { if (s[i] == o ) { int f = 0; for (int j = i; j <= i + 2; j++) { if (s[j] != t2[j - i]) { f = 1; break; } } if (f == 0) { s[i + 1] = - ; ans++; } } } printf( %d n , ans); for (int i = 0; i < len; i++) if (s[i] == - ) printf( %d , i + 1); printf( n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); long long int n, m = 0, p = 0, cnt = 0, f = 0; cin >> n; long long int a[n]; for (long long int i = 0; i < n; i++) { cin >> a[i]; if (a[i] < 0) { m++; } if (a[i] > 0) p++; if (a[i] < 0) { cnt = cnt - a[i] - 1; } if (a[i] > 0) { cnt = cnt + a[i] - 1; } if (a[i] == 0) { cnt = cnt + 1; f++; } } if (f and m % 2 == 0) cout << cnt << endl; if (f and m % 2 != 0) cout << cnt << endl; if (!f and m % 2 == 0) cout << cnt << endl; if (!f and m % 2 != 0) cout << cnt + 2 << endl; } |
#include <bits/stdc++.h> using namespace std; int n, m; int A[600006]; vector<int> G[600006]; int dfn[600006], bc[600006], clo; int g[600006][20], dep[600006]; void dfs(int u) { dfn[u] = ++clo, bc[clo] = u; if (u == 1) dep[u] = 1; for (int v : G[u]) { g[v][0] = u; dep[v] = dep[u] + 1; for (int k = (1), kend = (18); k <= kend; ++k) if (g[g[v][k - 1]][k - 1]) g[v][k] = g[g[v][k - 1]][k - 1]; else break; dfs(v); } } int lca(int u, int v) { if (dep[u] < dep[v]) swap(u, v); if (dep[u] != dep[v]) for (int k = (18), kend = (0); k >= kend; --k) if (dep[g[u][k]] >= dep[v]) u = g[u][k]; if (u == v) return u; for (int k = (18), kend = (0); k >= kend; --k) if (g[u][k] != g[v][k]) u = g[u][k], v = g[v][k]; return g[u][0]; } int dis(int u, int v) { return dep[u] + dep[v] - 2 * dep[lca(u, v)]; } set<int> S; void solve() { cin >> n; ++n; for (int i = (2), iend = (n); i <= iend; ++i) { int p; scanf( %d , &p); G[p].push_back(i); } dfs(1); int s = 1, t = 1, lt = 0; auto wkr = [&](int x) { return max(dis(x, s), dis(x, t)); }; S.insert(1); for (int i = (2), iend = (n); i <= iend; ++i) { int is = dis(i, s), it = dis(i, t), flg = 0; if (is >= lt && is >= it) t = i, lt = is, flg = 1; else if (it >= lt && it >= is) s = i, lt = it, flg = 1; if (!flg) { printf( %d n , (int)S.size()); continue; } auto tr = S.lower_bound(dfn[i]), st = tr; while (tr != S.end()) { if (wkr(bc[*tr]) < lt) ++tr; else break; } if (tr == S.end()) { auto sr = S.begin(); while (sr != st) { if (wkr(bc[*sr]) < lt) ++sr; else break; } S.erase(S.begin(), sr); } S.erase(st, tr); tr = S.lower_bound(dfn[i]), st = tr; while (tr != S.begin()) { --tr; if (wkr(bc[*tr]) >= lt) { ++tr; break; } } if (tr == S.begin()) { auto sr = S.end(); while (sr != st) { --sr; if (wkr(bc[*sr]) >= lt) { ++sr; break; } } S.erase(sr, S.end()); } S.erase(tr, st); S.insert(dfn[i]); printf( %d n , (int)S.size()); } } signed main() { solve(); } |
#include <bits/stdc++.h> using namespace std; struct fenwick { long long n, fen[100005]; void resize(int _n) { n = _n + 1; memset(fen, 0, sizeof fen); } int query(int ind) { ind++; int ans = 0; while (ind >= 1) { ans += fen[ind]; ind -= ind & -ind; } return ans; } void update(int ind, int val) { ind++; while (ind <= n) { fen[ind] += val; ind += ind & -ind; } } int query(int l, int r) { return query(r) - query(l - 1); } }; const int maxn = 100001; int n, l, w, x, y, sz[maxn]; long long res; bool blocked[maxn]; vector<pair<int, int> > c, v[maxn]; fenwick tree; void dfs(int node, int pnode, int curw, int curl) { c.push_back(make_pair(curw, curl)); for (auto &i : v[node]) if (i.first != pnode && !blocked[i.first]) dfs(i.first, node, curw + i.second, curl + 1); } long long solve(int node, int curw, int curl) { long long ret = 0; c.clear(); dfs(node, 0, curw, curl); sort(c.begin(), c.end()); for (auto &i : c) tree.update(i.second, 1); for (int i = 0, j = c.size() - 1; i < c.size(); i++) { while (i <= j && c[i].first + c[j].first > w) tree.update(c[j--].second, -1); if (j < i) break; tree.update(c[i].second, -1); ret += tree.query(l - c[i].second); } return ret; } int dfsz(int node, int pnode) { sz[node] = 1; for (auto &i : v[node]) if (i.first != pnode && !blocked[i.first]) sz[node] += dfsz(i.first, node); return sz[node]; } int find(int node, int pnode, int n) { for (auto &i : v[node]) if (i.first != pnode && !blocked[i.first] && 2 * sz[i.first] > n) return find(i.first, node, n); return node; } void decompose(int node) { int centroid = find(node, 0, dfsz(node, 0)); blocked[centroid] = 1; res += solve(centroid, 0, 0); for (auto &i : v[centroid]) if (!blocked[i.first]) res -= solve(i.first, i.second, 1); for (auto &i : v[centroid]) if (!blocked[i.first]) decompose(i.first); } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n >> l >> w; tree.resize(n + 1); for (int i = 2; i <= n && cin >> x >> y; i++) { v[i].push_back(make_pair(x, y)); v[x].push_back(make_pair(i, y)); } decompose(1); cout << res << endl; return 0; } |
#include <bits/stdc++.h> const int maxn = 1e5 + 10; const int N = 5e2 + 10; using namespace std; long long gcd(long long p, long long q) { return q == 0 ? p : gcd(q, p % q); } long long qpow(long long p, long long q) { long long f = 1; while (q) { if (q & 1) f = f * p % 1000000007; p = p * p % 1000000007; q >>= 1; } return f; } int n, m, k, t; vector<int> d[4][11][10]; char a[maxn], b[maxn]; int gao(char p) { if (p == A ) return 0; else if (p == T ) return 1; else if (p == G ) return 2; else return 3; } void add(vector<int>& d, int x, int y) { while (x < d.size()) d[x] += y, x += x & (-x); } int get(vector<int>& d, int x) { int ret = 0; while (x) ret += d[x], x -= x & (-x); return ret; } int main() { int i, j; scanf( %s , a + 1); n = strlen(a + 1); for (i = 0; i <= (int)3; i++) for (j = 1; j <= (int)10; j++) for (k = 0; k <= (int)j - 1; k++) d[i][j][k].resize((n + j - 1) / j + 1); for (i = 1; i <= n; i++) { for (j = 1; j <= 10; j++) { add(d[gao(a[i])][j][(i - 1) % j], (i - 1) / j + 1, 1); } } int q; scanf( %d , &q); while (q--) { int op, x, y; scanf( %d%d , &op, &x); if (op == 1) { scanf( %s , b); for (i = 1; i <= (int)10; i++) add(d[gao(a[x])][i][(x - 1) % i], (x - 1) / i + 1, -1); for (i = 1; i <= (int)10; i++) add(d[gao(b[0])][i][(x - 1) % i], (x - 1) / i + 1, 1); a[x] = b[0]; } else { scanf( %d%s , &y, b); int len = strlen(b), ret = 0; for (i = 0; i < len && i < y - x + 1; i++) { int mo = (i + x - 1) % len; ret += get(d[gao(b[i])][len][mo], (y - 1 - mo) / len + 1) - get(d[gao(b[i])][len][mo], (x - 1 + len - 1 - mo) / len); } printf( %d n , ret); } } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long int i, j, t; cin >> t; for (long long int aa = 0; aa < t; aa++) { long long int n, k, d; cin >> n >> k >> d; long long int mini = k; vector<long long int> v(n); map<long long int, long long int> m; for (i = 0; i < n; i++) { cin >> v[i]; if (i < d) { long long int ss = m.size(); m[v[i]]++; if (ss < m.size()) m[v[i]] = 1; } } m.size() < mini ? mini = m.size() : mini = mini; for (i = 1; i < n - d + 1; i++) { m[v[i - 1]]--; if (m[v[i - 1]] == 0) m.erase(v[i - 1]); long long int ss = m.size(); m[v[i + d - 1]]++; if (ss < m.size()) m[v[i + d - 1]] = 1; m.size() < mini ? mini = m.size() : mini = mini; } m.size() < mini ? mini = m.size() : mini = mini; cout << mini << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int *p = new int[n + 1], c = 0; int *m = new int[n + 1]; for (int i = 0; i <= n; i++) { p[i] = m[i] = 0; c ^= i; m[i] = c; } int f = 0; for (int i = 1; i <= n; i++) { cin >> p[i]; f ^= p[i]; } int r = 0; for (int i = 2; i <= n; i++) { int grps = (n / i); int rem = n % i; if (grps & 1) r ^= m[i - 1]; r ^= m[rem]; } cout << (r ^ f); return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; long long a[maxn], b[maxn], dp[maxn]; int q[maxn]; int head = 0, tail = 0; double A(int i, int j) { return dp[i] - dp[j]; } double B(int i, int j) { return b[i] - b[j]; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> b[i]; dp[1] = 0; int head = 1, tail = 1; q[tail] = 1; for (int i = 2; i <= n; i++) { while (head < tail && A(q[head + 1], q[head]) / B(q[head + 1], q[head]) >= -a[i]) head++; dp[i] = dp[q[head]] + b[q[head]] * a[i]; while (head < tail && A(i, q[tail]) / B(i, q[tail]) >= A(q[tail], q[tail - 1]) / B(q[tail], q[tail - 1])) tail--; q[++tail] = i; } cout << dp[n] << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAX_INT = numeric_limits<int>::max(); const int MAX_UINT = numeric_limits<unsigned int>::max(); void get_acum(const vector<int>& v, vector<int>& acum) { int sum = 0; for (unsigned int i = 0; i < v.size(); ++i) { acum.push_back(sum); sum += v[i]; } acum.push_back(sum); } int solve(vector<int>& A, vector<int>& B, vector<int>& X, unsigned int k) { if (A.size() + X.size() < k || B.size() + X.size() < k) { return -1; } else { sort(A.begin(), A.end()); sort(B.begin(), B.end()); sort(X.begin(), X.end()); vector<int> A_acum, B_acum, X_acum; get_acum(A, A_acum); get_acum(B, B_acum); get_acum(X, X_acum); int res = MAX_INT; unsigned int max_i = min((unsigned int)X.size(), k); for (unsigned int i = 0; i < max_i + 1; ++i) { if (k - i < A_acum.size() && k - i < B_acum.size()) { int total = A_acum[k - i] + B_acum[k - i] + X_acum[i]; if (total < res) res = total; } } return res; } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); unsigned int n, k; cin >> n >> k; vector<int> A; vector<int> B; vector<int> X; for (unsigned int i = 0; i < n; ++i) { unsigned int ti; bool ai, bi; cin >> ti >> ai >> bi; if (ai && bi) X.push_back(ti); else if (ai) A.push_back(ti); else if (bi) B.push_back(ti); } cout << solve(A, B, X, k); return 0; } |
#include <bits/stdc++.h> using namespace std; set<long long int> adj[3005]; map<long long int, long long int> mq; long long int done[3005][3005]; int main() { ios_base::sync_with_stdio(false); long long int n, m, i, ans = 0; cin >> n >> m; for (i = 0; i < m; i++) { long long int x, y; cin >> x >> y; adj[x].insert(y); } for (i = 1; i <= n; i++) { for (auto it : adj[i]) { for (auto it2 : adj[it]) { if (mq.find(it2) != mq.end() && it2 != i) { ans += mq[it2]; } mq[it2]++; } } mq.clear(); } cout << ans << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main() { int a, b, h; cin >> a >> b >> h; int c = 0; while (h != 0) { if (c % 2 == 0) { h = h - gcd(a, h); } else { h = h - gcd(b, h); } c++; } if (c % 2 == 0) { cout << 1 << endl; } else cout << 0 << endl; } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 100000 + 5; const double EPS = 1e-3; int n; int t[MAXN]; int g(double q) { bool ok = true; for (int i = 1; i < n; i++) if (fabs(double(t[i] - t[i - 1] * q)) > EPS) ok = false; if (ok) return 0; ok = true; for (int i = 2; i < n; i++) if (fabs(double(t[i] - t[i - 1] * q)) > EPS) ok = false; if (ok) return 1; ok = true; int prev = t[0]; for (int i = 1; i < n; i++) if (fabs(double(t[i] - prev * q)) > EPS) { if (!ok) return 2; ok = false; } else prev = t[i]; return 1; } int f() { if (n == 1) return 0; double q1 = 0, q2 = 0; if (t[0] != 0) q1 = double(t[1]) / t[0]; if (t[n - 2] != 0) q2 = double(t[n - 1]) / t[n - 2]; return min(g(q1), g(q2)); } int main() { scanf( %d , &n); for (int i = 0; i < n; i++) scanf( %d , &t[i]); printf( %d n , f()); } |
#include <bits/stdc++.h> using namespace std; const int MAX = 1e6 + 5; int a[MAX], run[MAX], ans[MAX]; char s[MAX]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; scanf( %d , &n); scanf( %s , s); for (int i = 0; i < n; ++i) { if (s[i] == 0 ) a[i] = 0; else if (s[i] == 1 ) a[i] = 1; else a[i] = -1; } vector<pair<int, int>> blocks; int it1 = 0, it2 = 0; while (it1 != n) { while (it2 < n - 1 && a[it1] == a[it2 + 1]) ++it2; blocks.emplace_back(it2 - it1 + 1, a[it1]); ++it2; it1 = it2; } for (int i = 0; i < (int)blocks.size(); ++i) { if (blocks[i].second != -1) continue; if (i == 0) blocks[i].second = blocks[i + 1].second; else if (i == (int)blocks.size() - 1) blocks[i].second = blocks[i - 1].second; else if (blocks[i - 1].second == blocks[i + 1].second) blocks[i].second = blocks[i - 1].second; } vector<pair<int, int>> compress; it1 = 0; it2 = 0; while (it1 != (int)blocks.size()) { int len = blocks[it1].first; while (it2 < (int)blocks.size() - 1 && blocks[it1].second == blocks[it2 + 1].second) it2++, len += blocks[it2].first; compress.emplace_back(len, blocks[it1].second); ++it2; it1 = it2; } for (auto block : compress) { if (block.second == -1) continue; for (int len = 1; len <= block.first; len++) ans[len] += block.first / len; } int max_size = -1, bef = -2; int size = compress.size(); for (int i = 0; i < size; i++) { int len = compress[i].first; int type = compress[i].second; if (bef != -1 && type != -1) { max_size = len; if (i != size - 1) max_size += compress[i + 1].first; for (int l = 1; l <= max_size; l++) run[l] = len % l; } else if (type == -1) { for (int l = 1; l <= max_size; l++) { if (run[l] + len >= l) run[l] += len, ans[l] += run[l] / l, run[l] %= l; else run[l] = len; } int add = 0; if (i != (int)size - 1) add += compress[i + 1].first; if (i != (int)size - 2) add += compress[i + 2].first; for (int l = max_size + 1; l <= len + add; ++l) run[l] = (len % l); max_size = len + add; } else { for (int l = 1; l <= max_size; ++l) { if (run[l] + (len % l) >= l) run[l] += ((len % l) - l), ans[l]++; else run[l] += (len % l); } } bef = type; } for (int i = 1; i <= n; ++i) printf( %d , ans[i]); printf( n ); return 0; } |
#include <bits/stdc++.h> using namespace std; inline int read() { int f = 1, x = 0; char ch = getchar(); while (ch < 0 || ch > 9 ) { if (ch == - ) { f = -1; } ch = getchar(); } while (ch >= 0 && ch <= 9 ) { x = x * 10 + ch - 0 ; ch = getchar(); } return x * f; } const int N = 55; struct W { int x, y, tx, ty, id; } up[N], down[N]; bool cmp_down(W a, W b) { return a.tx == b.tx ? a.ty < b.ty : a.tx > b.tx; } bool cmp_up(W a, W b) { return a.x == b.x ? a.y < b.y : a.x < b.x; } vector<pair<int, int> > ans_fr, ans_to; bool has[N][N]; void move_up(int& x, int y) { int dx = x; has[x][y] = 0; has[x - 1][y] = 1; ans_fr.push_back(make_pair(dx, y)); ans_to.push_back(make_pair(dx - 1, y)); --x; } void move_down(int& x, int y) { int dx = x; has[x][y] = 0; has[x + 1][y] = 1; ans_fr.push_back(make_pair(dx, y)); ans_to.push_back(make_pair(dx + 1, y)); ++x; } void move_left(int x, int& y) { int dy = y; has[x][y] = 0; has[x][y - 1] = 1; ans_fr.push_back(make_pair(x, dy)); ans_to.push_back(make_pair(x, dy - 1)); --y; } void move_right(int x, int& y) { int dy = y; has[x][y] = 0; has[x][y + 1] = 1; ans_fr.push_back(make_pair(x, dy)); ans_to.push_back(make_pair(x, dy + 1)); ++y; } void move_row(int& x, int& y, int tx, int ty) { while (y < ty && has[x][y + 1] == 0) { move_right(x, y); } while (y > ty && has[x][y - 1] == 0) { move_left(x, y); } } void move(int& x, int& y, int tx, int ty) { while (x < tx) { move_down(x, y); } while (x > tx) { move_up(x, y); } while (y < ty) { move_right(x, y); } while (y > ty) { move_left(x, y); } } int check(W a) { if (a.x == a.tx) { return a.y == a.ty ? 0 : 1; } else { return a.y == a.ty ? 2 : 3; } } int nx[N], ny[N]; char mp[N][N]; int vis[N]; void write(int n) { for (int i = 1; i <= n; i++) { cout << mp[i] + 1 << endl; } puts( ---------------------- ); } void deal(int n, int m) { if (n == 1) { puts( 0 ); } else { if (m == 1) { move(up[1].x, up[1].y, up[1].tx, up[1].ty); } else { if (cmp_up(up[1], up[2]) == 0) { swap(up[1], up[2]); } if (up[1].x == 2) { move_up(up[1].x, up[1].y); move_up(up[2].x, up[2].y); } else if (up[2].x == 2) { move_up(up[2].x, up[2].y); } switch (check(up[1])) { case 0: break; case 1: move(up[2].x, up[2].y, 2, 1); move_right(up[1].x, up[1].y); break; case 2: move(up[1].x, up[1].y, up[1].tx, up[1].ty); break; case 3: move(up[1].x, up[1].y, up[1].tx, up[1].ty); move_left(up[2].x, up[2].y); break; } move(up[2].x, up[2].y, up[2].tx, up[2].ty); } cout << ans_fr.size() << endl; for (int i = 0; i < ans_fr.size(); i++) { int xx1 = ans_fr[i].first, xx2 = ans_to[i].first; int yy1 = ans_fr[i].second, yy2 = ans_to[i].second; printf( %d %d %d %d n , xx1, yy1, xx2, yy2); } } } int main() { int n = read(), m = read(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { mp[i][j] = . ; } } for (int i = 1; i <= m; i++) { up[i].id = i; nx[i] = up[i].x = read(); ny[i] = up[i].y = read(); has[nx[i]][ny[i]] = 1; mp[nx[i]][ny[i]] = i + 0 ; } for (int i = 1; i <= m; i++) { up[i].tx = read(); up[i].ty = read(); } if (n <= 2) { deal(n, m); return 0; } copy(up + 1, up + 1 + m, down + 1); sort(up + 1, up + 1 + m, cmp_up); int cnt = m; while (cnt) { for (int i = 1; i <= m; i++) { if (vis[i] == 0) { int id = up[i].id; move_row(nx[id], ny[id], 1, i); if (ny[id] == i) { vis[i] = 1; cnt--; } } } } for (int i = 1; i <= m; i++) { int id = up[i].id; while (nx[id] > 1) { move_up(nx[id], ny[id]); } } for (int i = 1; i <= m; i++) { move_down(nx[i], ny[i]); move_down(nx[i], ny[i]); } sort(down + 1, down + 1 + m, cmp_down); for (int i = 1; i <= m; i++) { int id = down[i].id; move_up(nx[id], ny[id]); move_row(nx[id], ny[id], 1, i); move_up(nx[id], ny[id]); } for (int i = 1; i <= m; i++) { int id = down[i].id; while (nx[id] < down[i].tx) { move_down(nx[id], ny[id]); } } cnt = m; while (cnt) { for (int i = 1; i <= m; i++) { if (vis[i] == 1) { int id = down[i].id; move_row(nx[id], ny[id], nx[id], down[i].ty); if (ny[id] == down[i].ty) { vis[i] = 2; cnt--; } } } } assert(ans_fr.size() == ans_to.size()); assert(ans_fr.size() <= 10800); cout << ans_fr.size() << endl; for (int i = 0; i < ans_fr.size(); i++) { int xx1 = ans_fr[i].first, xx2 = ans_to[i].first; int yy1 = ans_fr[i].second, yy2 = ans_to[i].second; printf( %d %d %d %d n , xx1, yy1, xx2, yy2); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int a[m], b[m]; int ndup = n; for (int i = 0; i < m; i++) { cin >> a[i]; b[i] = a[i]; } sort(a, a + m); sort(b, b + m, greater<int>()); int mn = 0, mx = 0; for (int i = 0; i < m; i++) { while (a[i] > 0 && n > 0) { mn += a[i]; a[i]--; n--; } } while (ndup > 0) { mx += b[0]; b[0]--; ndup--; sort(b, b + m, greater<int>()); } cout << mx << << mn; } |
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c, d, e, f, g, h, i, j, k, l, m, n, t; cin >> t; while (t--) { cin >> n; cout << (n + 1) / 2 << endl; } } |
//...START BY DOING WHAT IS NECESSARY, THEN WHAT IS POSSIBLE AND SUDDENLY YOU ARE DOING THE IMPOSSIBLE... #include <bits/stdc++.h> using namespace std; #define FAST_FURIER ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define pb push_back #define mk make_pair #define rep(i,a,N) for(ll i=a;i<N;i++) #define rrep(i,a,N) for(ll i=a;i>N;i--) typedef long long ll; #define M 1000000007 bool comp(int x,int y) { return x > y; } /*..............................code starts here........................*/ // C is first won in M int main() { FAST_FURIER; int tt=1; cin >> tt; ll m,d,k; //ll a,b,c; while(tt--) { cin >> d >> k; ll x = 0,y = 0; int f = 0; while(true){ ll d1 = pow(x+k,2)+pow(y,2), d2 = pow(x,2)+pow(y+k,2); f = abs(f-1); if(d1 < d2 and d1 <= d*d){ x += k; } else if(d2 <= d1 and d2 <= d*d) y += k; else break; } if(f) cout << Utkarsh ; else cout << Ashish ; cout << endl; } } // Author : shivam_123 // g++ -std=c++17 |
#include <bits/stdc++.h> using namespace std; long long a, b, c, e[200000], mix, l, r, o, d[200000]; vector<long long> v[100000], v1; set<long long> st; map<long long, long long> mp, mp1; string s; pair<int, int> p[100000], p1[100000]; char ch[10]; deque<char> dq; int main() { cin >> a; for (int i = 0; i < a / 4; i++) if ((a - 2 * (i + 1)) % 2 == 0) c++; if (a % 4 == 0) { cout << c - 1; } else cout << c; } |
#include <bits/stdc++.h> using namespace std; char s[5000005]; int len[1000005]; int idx[1000005]; int sum[1000005]; int sz = 0; int dp[1000005][20]; int p[1000005]; int main() { int n, r, c; scanf( %d%d%d , &n, &r, &c); gets(s); gets(s); int cnt = 0, slen = 0; for (int i = 0; s[i]; i++, slen++) { if (s[i] == && cnt != 0) { len[sz] = cnt; idx[sz++] = i - cnt; cnt = 0; } else cnt++; } if (cnt > 0) { len[sz] = cnt; idx[sz++] = slen - cnt; cnt = 0; } sum[0] = len[0]; for (int i = 1; i < n; i++) sum[i] = sum[i - 1] + 1 + len[i]; for (int i = n - 1; i >= 0; i--) { int id = upper_bound(sum, sum + n, (i > 0 ? sum[i - 1] + 1 : 0) + c) - sum; if (id > n) id = n; dp[i][0] = id - i; } for (int j = 2, t = 1; j <= r; j *= 2, t++) { for (int i = n - 1; i >= 0; i--) dp[i][t] = dp[i][t - 1] + dp[i + dp[i][t - 1]][t - 1]; } int maid = -1, ma = 0; for (int i = 0; i < n; i++) { int idx = i, rr = r, cur = 0; for (int j = 20; j >= 0; j--) { if (rr >= (1 << j)) { rr -= (1 << j); cur += dp[idx][j]; idx += dp[idx][j]; } } if (cur > ma) maid = i, ma = cur; } int cn = 1, cs = dp[maid][0]; for (int i = maid; i < maid + ma; i++) { s[idx[i] + len[i]] = 0; if (cn < cs) { printf( %s , s + idx[i]); cn++; } else { printf( %s n , s + idx[i]); cs = dp[i + 1][0]; cn = 1; } } return 0; } |
#include <bits/stdc++.h> using namespace std; char change(char curranswer) { if (curranswer == z ) curranswer = A ; else if (curranswer == Z ) curranswer = 0 ; else if (curranswer == 9 ) ; else curranswer++; return curranswer; } int main() { long long i, j, t; cin >> t; while (t--) { long long r, c, k; long long rice = 0; char a[100][100]; cin >> r >> c >> k; for (i = 0; i < r; i++) { for (j = 0; j < c; j++) { cin >> a[i][j]; if (a[i][j] == R ) rice++; } } long long ans = rice / k; long long maxx = rice - k * ans; long long minn = k - maxx; char curranswer = a ; long long currcount = 0; bool flag = true; for (i = 0; i < r; i++) { for (flag ? (j = 0) : (j = c - 1); flag ? (j < c) : j >= 0; flag ? j++ : j--) { bool local = false; if (a[i][j] == R ) local = true; a[i][j] = curranswer; if (local) { currcount++; if (maxx > 0) { if (currcount == ans + 1) { maxx--; currcount = 0; curranswer = change(curranswer); } } else if (minn > 0) { if (currcount == ans) { minn--; currcount = 0; if (minn) curranswer = change(curranswer); } } } } flag = !flag; } for (i = 0; i < r; i++) { for (j = 0; j < c; j++) cout << a[i][j]; cout << endl; } } } |
#include <bits/stdc++.h> using namespace std; long long gcd(long long x, long long y) { if (y == 0) return x; else return gcd(y, x % y); } long long expo(long long n, long long m) { long long r = 1; while (m > 0) { if (m % 2) r = (r * n) % 1000000009; n = (n * n) % 1000000009; m = m / 2; } return r % 1000000009; } long long lx = 0, ly = 0; bool check(long long x, long long y) {} int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long a, b; cin >> a >> b; string s; cin >> s; long long p = 0, q = 0; if (a == 0 && b == 0) { cout << Yes ; return 0; } for (long long i = 0; i < s.size(); i++) { if (s[i] == U ) { ly++; } else if (s[i] == D ) ly--; else if (s[i] == L ) lx--; else lx++; if (lx == a && ly == b) { cout << Yes ; return 0; } } if (lx == 0 && ly == 0) { cout << No ; return 0; } for (long long i = 0; i < s.size(); i++) { if (s[i] == U ) { q++; } else if (s[i] == D ) q--; else if (s[i] == L ) p--; else p++; long long x = a - p, y = b - q, X = lx, Y = ly; if (lx == 0) { if (ly > 0 && y > 0 && x == 0 && y % ly == 0) { cout << Yes ; return 0; } else if (ly < 0 && y < 0 && x == 0) { ly = -1 * ly, y = -1 * y; if (y % ly == 0) { cout << Yes ; return 0; } } } else if (ly == 0) { if (lx > 0 && x > 0 && y == 0 && x % lx == 0) { cout << Yes ; return 0; } else if (lx < 0 && x < 0 && y == 0) { lx = -1 * lx, x = -1 * x; if (x % lx == 0) { cout << Yes ; return 0; } } } else { if (lx > 0 && x > 0) { if (ly > 0 && y > 0) { if (x / lx == y / ly && x % lx == 0 && y % ly == 0) { cout << Yes ; return 0; } } else if (ly < 0 && y < 0) { ly = -1 * ly, y = -1 * y; if (x / lx == y / ly && x % lx == 0 && y % ly == 0) { cout << Yes ; return 0; } } } else if (lx < 0 && x < 0) { lx = -1 * lx, x = -1 * x; if (ly > 0 && y > 0) { if (x / lx == y / ly && x % lx == 0 && y % ly == 0) { cout << Yes ; return 0; } } else if (ly < 0 && y < 0) { ly = -1 * ly, y = -1 * y; if (x / lx == y / ly && x % lx == 0 && y % ly == 0) { cout << Yes ; return 0; } } } } lx = X, ly = Y; } cout << No ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = int(3e5), mod = int(1e9) + 7; int n; int a[N], c[N]; long long t[N]; long long x[N], pref[N]; long double p[N]; long long suf[N]; void upd(int v, int val) { v += n - 1; t[v] += val; c[v]++; while (v > 1) { v >>= 1; t[v] = t[v + v] + t[v + v + 1]; c[v] = c[v + v] + c[v + v + 1]; } } long long get(int l, int r) { l += n - 1; r += n - 1; long long res = 0; while (l <= r) { if (l & 1) res += t[l]; if (!(r & 1)) res += t[r]; l = (l + 1) >> 1; r = (r - 1) >> 1; } return res; } int cnt(int l, int r) { l += n - 1; r += n - 1; int res = 0; while (l <= r) { if (l & 1) res += c[l]; if (!(r & 1)) res += c[r]; l = (l + 1) >> 1; r = (r - 1) >> 1; } return res; } int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %d , &a[i]); } long long all = 1ll * n * (n + 1) / 2; long double res = 0; for (int i = 1; i <= n; i++) { long long cur = get(1, a[i] - 1); long double p = (cur + 0.0) * (n - i + 1) / all; res += p * 0.5; cur = get(a[i] + 1, n); p = (cur + 0.0) * (n - i + 1) / all; res += cnt(a[i] + 1, n); res -= p * 0.5; upd(a[i], i); } printf( %.12lf , double(res)); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long a, b; cin >> a >> b; long long ans = 0; while (true) { long long x = max(a, b); long long y = min(a, b); if (x == 0 || y == 0) break; long long n = x / y; ans += n; x -= n * y; a = x; b = y; } cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int x, test; cin >> test; while (test--) { int n, c0, c1, h; string s; cin >> n >> c0 >> c1 >> h >> s; int cnt0 = 0, cnt1 = 0; for (auto &z : s) { cnt0 += z == 0 ; cnt1 += z == 1 ; } int cost = (cnt0 * c0) + (cnt1 * c1); int cost0 = (c0 * n) + (cnt1 * h); int cost1 = (c1 * n) + (cnt0 * h); cout << min({cost, cost0, cost1}) << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int inf = int(1e9) + 123; int main() { int n, v; cin >> n >> v; vector<int> a; for (int i = 1; i <= n; i++) { int x; cin >> x; a.push_back(x); } sort(a.begin(), a.end()); int sum = 0; for (int i = 0; i < a.size() - 1; i++) { sum += a[i]; } if (sum <= v) { cout << YES ; } else { cout << NO ; } return 0; } |