solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; int main() { long long q, w, w1, e, r, t, c, v, f[5002], f1[5002], ans = 0; char a[5002]; cin >> a; q = strlen(a); f[q + 1] = 1; f1[q + 1] = 0; for (w = q - 1; w > 0; w -= 2) { f[w] = f[w + 2] * (a[w] - 48); f1[w] = f1[w + 2]; if (a[w - 1] == '+') { f1[w] += f[w]; f[w] = 1; } } c = 1; v = 0; for (w = 0; w < q; w += 2) { e = 1; r = 0; for (w1 = w; w1 < q; w1 += 2) { e *= a[w1] - 48; ans = max(ans, v + c * (r + e) * f[w1 + 2] + f1[w1 + 2]); if (a[w1 + 1] == '+') { r += e; e = 1; } } c *= a[w] - 48; if (a[w + 1] == '+') { v += c; c = 1; } } cout << ans; return 0; }
13
#include <bits/stdc++.h> using namespace std; int ara[5005]; int main() { int n; cin >> n; int x, y, z; cout << "? 1 2" << endl; cin >> x; cout << "? 1 3" << endl; cin >> y; cout << "? 2 3" << endl; cin >> z; int temp = x - y; int sum = temp + z; ara[2] = sum / 2; ara[1] = x - ara[2]; ara[3] = z - ara[2]; for (int i = 4; i <= n; i++) { cout << "? " << 1 << " " << i << endl; int c; cin >> c; ara[i] = c - ara[1]; } cout << "!"; for (int i = 1; i <= n; i++) cout << " " << ara[i]; cout << endl; }
6
#include <bits/stdc++.h> using namespace std; void file() {} void test_case() { int n, i, ans; cin >> n; ans = n / 2 + 1; printf("%d", ans); for (i = 1; i <= ans; ++i) printf("\n%d 1", i); for (i = 2; i <= n - ans + 1; ++i) printf("\n%d %d", ans, i); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); file(); int T = 1; for (int nr = 1; nr <= T; ++nr) { test_case(); } }
3
#include <bits/stdc++.h> using namespace std; template <class T> void cmin(T &a, T b) { if (b < a) a = b; } template <class T> void cmax(T &a, T b) { if (b > a) a = b; } template <class T> int len(const T &c) { return (int)c.size(); } template <class T> int len(char c[]) { return (int)strlen(c); } string itos(long n) { string s; while (n) { s += (n % 10 + 48); n /= 10; } reverse(s.begin(), s.end()); return s; } long stoi(string s) { long n = 0; for (int i(0), _n(len(s)); i < _n; ++i) n = n * 10 + (s[i] - 48); return n; } stack<int> stk; bool color[100005]; vector<int> v[100005], g[100005]; long ct, mn, cost[100005]; void fil(int i) { color[i] = true; for (int j(0), _n(len(v[i])); j < _n; ++j) { if (color[v[i][j]] == false) fil(v[i][j]); } stk.push(i); } void dfs(int i) { color[i] = 1; if (cost[i] < mn) { mn = cost[i]; ct = 1; } else if (cost[i] == mn) ct++; for (int j(0), _n(len(g[i])); j < _n; ++j) if (color[g[i][j]] == false) dfs(g[i][j]); } int ar[500100]; int main() { int a, b, c; int res = 0, cc = 0; int n; cin >> n; cin >> a >> c; for (int i(0), _n(n); i < _n; ++i) { cin >> ar[i]; if (ar[i] > a) { if (cc >= c) res += cc - c + 1; cc = 0; } else cc++; } if (cc >= c) res += cc - c + 1; cout << res; return 0; }
3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; template <class T> inline bool read(T& n) { T x = 0, tmp = 1; char c = getchar(); while ((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar(); if (c == EOF) return false; if (c == '-') c = getchar(), tmp = -1; while (c >= '0' && c <= '9') x *= 10, x += (c - '0'), c = getchar(); n = x * tmp; return true; } template <class T> inline void write(T n) { if (n < 0) { putchar('-'); n = -n; } int len = 0, data[20]; while (n) { data[len++] = n % 10; n /= 10; } if (!len) data[len++] = 0; while (len--) putchar(data[len] + 48); } const int MAXN = 1000010; const int C = 1000; struct Node { int x, y, id; bool operator<(const Node& b) const { if (x == b.x) { if (x & 1) return y > b.y; else return y < b.y; } return x < b.x; } } a[MAXN]; int main() { int n; read(n); for (int i = 1; i <= n; i++) { read(a[i].x), read(a[i].y); a[i].x /= C; a[i].id = i; } sort(a + 1, a + n + 1); for (int i = 1; i <= n; i++) { write(a[i].id), putchar(' '); } putchar('\n'); return 0; }
13
#include <bits/stdc++.h> using namespace std; int divv[10000005], n; long long ans[10000005]; int prr[10000005], c; int f[256]; int main() { cin >> n; for (int i = 0; i < 256; i++) f[i] = __builtin_popcount(i); for (int i = 2; i <= 10000000; i++) { if (divv[i] != 0) continue; if (i * 2 <= n) { prr[c++] = i; } for (int j = i * 2; j <= 10000000; j += i) { divv[j] = i; } } for (int i = 2; i <= n; i++) { if (divv[i] == 0) { if (i * 2 > n) continue; long long x = i; long long y = max(x, n / x); int d = upper_bound(prr, prr + c, y) - prr; ans[i] -= (c - d) * 2; ans[i] += n / i - 1; } else if (divv[i / divv[i]] == divv[i]) { ans[i] = ans[i / divv[i]]; } else { int pr[11]; int nxt = 0, cur = i; while (divv[cur] != 0) { if (nxt == 0 || pr[nxt - 1] != divv[cur]) { pr[nxt++] = divv[cur]; } cur = cur / divv[cur]; } if (nxt == 0 || pr[nxt - 1] != cur) { pr[nxt++] = cur; } long long sum = 0; for (int mask = 1; mask < (1 << nxt); mask++) { int a = f[mask]; long long D = 1; for (int k = 0; k < nxt; k++) if (((mask >> k) & 1)) D = D * pr[k]; if (a % 2 == 1) sum += n / D; else sum -= n / D; } long long x = pr[nxt - 1]; long long y = max(x, n / x); int d = upper_bound(prr, prr + c, y) - prr; ans[i] -= (c - d) * 2; ans[i] += sum - 1; } } long long comp = 0, ret = 0; for (int i = 2; i <= n; i++) { if (i * 2 > n && divv[i] == 0) continue; comp++; ret += ans[i]; } comp = comp * (comp - 1) / 2; ret /= 2; cout << comp * 2 - ret << endl; }
19
#include <bits/stdc++.h> using namespace std; int main() { int k, r, c = 0; int a[4]; for (int i = 0; i < 4; i++) { cin >> a[i]; } sort(a, a + 4); for (int i = 0; i < 3; i++) { if (a[i] == a[i + 1]) { c++; } } cout << c << endl; }
0
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e5 + 10; const long long INF = 0x3f3f3f3f; const long long Mod = 1e9 + 7; int n, m, x, y, t; struct node { int a; int b; } n1[maxn], n2[maxn]; int cmp(node a, node b) { return (a.a < b.a); } int ans[maxn]; int main() { cin >> n; for (long long i = 0; i < (n); i++) { cin >> n1[i].a; n1[i].b = i + 1; } for (long long i = 0; i < (n); i++) { cin >> n2[i].a; n2[i].b = i + 1; } sort(n1, n1 + n, cmp); sort(n2, n2 + n, cmp); for (long long i = 0; i < (n); i++) { ans[n2[i].b - 1] = n1[i].b; } for (long long i = 0; i < (n); i++) { cout << ans[i] << ' '; } return 0; }
4
#include <bits/stdc++.h> using namespace std; template <class T> void read(T &x) { x = 0; int f = 0; char ch = getchar(); while (ch < '0' || ch > '9') { f |= (ch == '-'); ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } x = f ? -x : x; return; } using namespace std; const long long MOD = 1e9 + 7; struct intmod { long long val; intmod operator+(const intmod other) const { intmod ret; ret.val = val + other.val; if (ret.val >= MOD) { ret.val -= MOD; } return ret; } intmod operator-(const intmod other) const { intmod ret; ret.val = val + MOD - other.val; if (ret.val >= MOD) { ret.val -= MOD; } return ret; } intmod operator*(const intmod other) const { intmod ret; ret.val = val * other.val % MOD; return ret; } } HALF = {(MOD + 1) / 2}, THIRD = {(MOD + 1) / 3}; intmod make_int(long long x) { return {x}; } struct polinom { intmod koef[4]; intmod ans(intmod x) { intmod ret; ret = koef[3]; ret = (ret * x) + koef[2]; ret = (ret * x) + koef[1]; ret = (ret * x) + koef[0]; return ret; } polinom sum() { polinom ret; ret.koef[0] = {0}; ret.koef[1] = koef[0] + ((koef[1] + (koef[2] * THIRD)) * HALF); ret.koef[2] = (koef[1] + koef[2]) * HALF; ret.koef[3] = koef[2] * THIRD; return ret; } polinom operator+(const polinom other) const { polinom ret; ret.koef[0] = koef[0] + other.koef[0]; ret.koef[1] = koef[1] + other.koef[1]; ret.koef[2] = koef[2] + other.koef[2]; ret.koef[3] = koef[3] + other.koef[3]; return ret; } polinom operator-(const polinom other) const { polinom ret; ret.koef[0] = koef[0] - other.koef[0]; ret.koef[1] = koef[1] - other.koef[1]; ret.koef[2] = koef[2] - other.koef[2]; ret.koef[3] = koef[3] - other.koef[3]; return ret; } polinom operator*(const intmod other) const { polinom ret; ret.koef[0] = koef[0] * other; ret.koef[1] = koef[1] * other; ret.koef[2] = koef[2] * other; ret.koef[3] = koef[3] * other; return ret; } } BASE = {1, 3, 0, 0}; intmod f(long long K) { polinom save; save = BASE; save = save.sum(); intmod ret; ret = save.ans({K / 3}); if (K % 3 >= 1) { ret = ret + make_int(K / 3 + 1); } if (K % 3 >= 2) { ret = ret + make_int(K / 3 + 2); } return ret; } intmod a(long long K) { polinom save; save = BASE; save = save.sum(); save = save * make_int(3); save = save - BASE; save = save.sum(); intmod ret; ret = save.ans({K / 3}); if (K % 3 >= 1) { ret = ret + f(K); } if (K % 3 >= 2) { ret = ret + f(K - 1); } return ret; } intmod b(long long K) { polinom save; save = BASE; save = save.sum(); save = save.sum(); return save.ans({K}); } intmod c(long long K) { return a(3 * K / 2) - b(K / 2); } intmod d(long long K) { polinom save; save = {0, 5, 2, 0}; save = save.sum(); return save.ans({K}); } intmod e(long long K) { polinom save; save = {5, 2, 0, 0}; save = save.sum(); return save.ans({K}); } intmod P(long long N, long long M) { long long K; N = min(N, 2 * M - 1); M = min(M, 2 * N - 1); K = max(3LL, M / 2); if (N <= 3) { return {0}; } intmod ret; ret = a(N + M - 2) - a(M + K - 2); ret = ret + b(K - 1) - b(2); ret = ret - c(N - 1) + c(2); return ret; } intmod Q(long long N, long long M) { N = min(N, M / 2) - 2; M = M - 5; if (N <= 0) { return {0}; } intmod ret; ret = d(N - 1); ret = ret + ((e(M / 2) - e(N - 1)) * make_int(N)); if (M % 2 >= 1) { ret = ret + (make_int(N / 2) * make_int(M + 6)); if (N % 2 >= 1) { ret = ret + make_int(2 * ((M + 1) / 4) + 3); } } return ret; } intmod area(long long N, long long M) { if (N < 3) { return {0}; } intmod ret; ret = {0}; for (long long i = 3; i <= 5; i++) { if (i <= M) { ret = ret + make_int(7 - i); } } ret = ret + P(N, M); ret = ret + Q(N, M); ret = ret + Q(M, N); return ret; } long long Nmin, Mmin; long long Nmax, Mmax; intmod ans; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long tc; cin >> tc; while (tc--) { cin >> Nmin >> Mmin; cin >> Nmax >> Mmax; ans = area(Nmax, Mmax) + area(Nmin - 1, Mmin - 1); ans = ans - (area(Nmax, Mmin - 1) + area(Nmin - 1, Mmax)); cout << ans.val << '\n'; } return 0; }
23
#include <bits/stdc++.h> using namespace std; long long pp[1005]; long long key[2005]; int main() { long long n, m, k; cin >> n >> m >> k; long long sum = 0; for (int i = 0; i < n; i++) { cin >> pp[i]; } for (int i = 0; i < m; i++) { cin >> key[i]; } sort(pp, pp + n); sort(key, key + m); long long ans = 0; for (int i = 0; i + n - 1 < m; i++) { long long tmp = 0; for (int j = 0; j < n; j++) { tmp = max(tmp, abs(pp[j] - key[i + j]) + abs(key[i + j] - k)); } if (!ans) ans = tmp; else ans = min(ans, tmp); } cout << ans; }
10
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long n; cin >> n; vector<long> a(n); unordered_map<long, long> b; for (long j = 0; j < n; j++) { cin >> a[j]; b[a[j]]++; } long ma = 0; for (auto x : b) { if (x.second > ma) ma = x.second; } if (n % 2 == 0) { if (ma <= n / 2) cout << "YES"; else cout << "NO"; } else { if (ma <= n / 2 + 1) cout << "YES"; else cout << "NO"; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int n; pair<double, double> A[100010]; bool compare_y(pair<double, double> a, pair<double, double> b) { return a.second < b.second; } int closest_pair(pair<double, double> *a, int n) { if (n <= 1) return 1000000000; int m = n / 2; double x = a[m].first; int d = min(closest_pair(a, m), closest_pair(a + m, n - m)); inplace_merge(a, a + m, a + n, compare_y); vector<pair<double, double> > b; for (int i = 0; i < n; i++) { if (fabs(a[i].first - x) >= sqrt(d)) continue; for (int j = 0; j < b.size(); j++) { double dx = a[i].first - b[b.size() - j - 1].first; double dy = a[i].second - b[b.size() - j - 1].second; if (dy >= sqrt(d)) break; d = min(d, (int)(dx * dx + dy * dy)); } b.push_back(a[i]); } return d; } int solve() { sort(A, A + n); return closest_pair(A, n); } int main() { cin >> n; int sum = 0; for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); sum += x; A[i - 1].first = (double)i; A[i - 1].second = (double)sum; } printf("%d", solve()); return 0; }
14
#include <bits/stdc++.h> using namespace std; long long power(long long a, long long b) { long long r = 1; while (b != 0) { if (b % 2 != 0) { r = r * a; r %= 998244353ll; } a = a * a; a = a % 998244353ll; b = b / 2; } return r; } void solve() { long long n; cin >> n; for (long long i = (n); i >= (1); i--) { cout << i << " "; } cout << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; cin >> t; while (t--) { solve(); } }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, a; cin >> n >> a; if (a % 2 == 0) a = n + 1 - a; cout << (a + 1) / 2; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { long long int n; long long int k; cin >> n >> k; long long int ar[26]; for (int i = 0; i < 26; i++) ar[i] = 0; string s; cin >> s; for (int i = 0; i < n; i++) { ar[s[i] - 'A']++; } sort(ar, ar + 26); long long int ans = 0; for (int i = 25; i >= 0; i--) { if (ar[i] <= k) { ans += ar[i] * ar[i]; k -= ar[i]; } else { ans += (k)*k; k = 0; break; } } cout << ans << endl; }
5
#include <bits/stdc++.h> using namespace std; void solve() { int n, s, t; cin >> n >> s >> t; cout << n - min(s, t) + 1 << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { solve(); } }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; string a[m]; string b[m]; for (long long i = 0; i < m; i++) { cin >> a[i] >> b[i]; } string c[n]; for (long long i = 0; i < n; i++) { cin >> c[i]; } int k = 0; int i = 0; for (long long i = 0; i < n; i++) { for (long long k = 0; k < m; k++) { if (c[i] == a[k]) { if (a[k].length() <= b[k].length()) { cout << a[k] << ' '; } else { cout << b[k] << ' '; } i; } } } return 0; }
2
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize "-O3" const int N = (1e5 + 5); const int64_t MOD = (1e9 + 7); const int64_t INF = (1e18 + 18); int n, m; vector<tuple<int64_t, int, int>> vt; struct DSU { vector<int> sz; vector<int> par; DSU(int x) { sz.resize(x + 5); par.resize(x + 5); fill(par.begin(), par.end(), -1); fill(sz.begin(), sz.end(), 1); } int find_root(int u) { while (par[u] != -1) u = par[u]; return u; } void merge(int u, int v) { int r1 = find_root(u), r2 = find_root(v); if (r1 == r2) return; if (sz[r2] > sz[r1]) { par[r1] = r2; sz[r2] += sz[r1]; sz[r1] = 0; } else { par[r2] = r1; sz[r1] += sz[r2]; sz[r2] = 0; } } }; int64_t minSpanningTree() { DSU dsu(n); int u = get<1>(vt[0]), v = get<2>(vt[0]); get<0>(vt[0]) = (1e9); sort(vt.begin(), vt.end()); for (auto [w, x, y] : vt) { if (dsu.find_root(x) != dsu.find_root(y)) dsu.merge(x, y); if (dsu.find_root(u) == dsu.find_root(v)) return w; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n >> m; for (int i = 0, a, b; i < m; i++) { int64_t w = 0; cin >> a >> b >> w; vt.push_back({w, a, b}); } cout << minSpanningTree() << '\n'; return 0; }
11
#include <bits/stdc++.h> using namespace std; void solve() { int n, a, b, c; cin >> n >> a >> b >> c; int sum = 0; int x1 = n / a; int x2 = n / b; for (int i = 0; i <= x1; i++) { for (int j = 0; j <= x2; j++) { int x3 = n - i * a - j * b; if (x3 % c != 0 || x3 < 0) continue; int x4 = x3 / c; sum = max(sum, i + j + x4); } } cout << sum << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; t = 1; while (t--) { solve(); } }
5
#include <bits/stdc++.h> using namespace std; long long ans = -1; int main() { long long n, k, l, r; cin >> n >> l >> r >> k; long long len = (l <= r ? (r - l + 1) : (n - l + 1 + r)); if (n <= 1000000) { for (int i = n; i <= 2 * n; i++) { long long s = k % i; if (!s) { s = i; } s -= len; long long l1 = max(i - n - (n - len) - 1, 0ll); long long l2 = min(len, i - n); if (s >= l1 && s <= l2) { ans = max(ans, i - n); } } } else { if (k >= len && k <= 2 * len) { ans = max(ans, (k == 2 * len) ? n : n - len + k - len + 1); } for (int i = 1; i <= k / n; i++) { long long s2 = min((k - 2 * len + 2 * n + 1) / (i + 1), (k - len) / i); long long s1 = max((k - 2 * len + i - 1) / i, (k - len + n + i) / (i + 1)); if (s1 <= s2) { ans = max(ans, s2 - n); } } } cout << ans << endl; return 0; }
18
#include<bits/stdc++.h> using namespace std; #define loop(i, l, r) for(int i = (int) l; i <= (int) r; i++) #define rloop(i, r, l) for(int i = (int) r; i >= (int) l; i--) #define vi vector<int> #define ii pair<int, int> #define eb emplace_back #define all(x) begin(x), end(x) #define fi first #define se second #define rand rng #define sp ' ' #define int long long const int maxN = 1005, mod = 1e9 + 7, inf = 1005; int N, M; vi fac[maxN]; void prep() { loop(i, 2, maxN - 1) { if(fac[i].empty()) { for(int j = i; j < maxN; j += i) { fac[j].eb(i); } } } } int q; int ask(int w, int h, int i1, int j1, int i2, int j2) { q++; // assert(q <= 3 * log2(N + M)); cout << "? " << w << sp << h <<sp << i1 << sp << j1 << sp << i2 << sp << j2 <<endl; int re; cin >> re; return re; } int binask(int t, int p, bool row) { while(p) { if(p == 1) return 1; int m = p >> 1; int h = row? m * t: N; int w = row? M: m * t; int i2 = row? (p - m) * t + 1: 1; int j2 = row? 1: (p - m) * t + 1; if(!ask(h, w, 1, 1, i2, j2)) return 0; p -= m; } return 1; } int solve(int sz, bool row) { int re = 0; int mn = sz; for(int p: fac[sz]) { while(mn % p == 0) { if(binask(mn / p, p, row)) mn /= p; else break; } } // if(N == M and M ==512 and !row)cout << mn << endl; loop(i, 1, sz) if(i % mn == 0 and sz % i == 0) re++; return re; } signed main() { // ios_base::sync_with_stdio(0); // cin.tie(0); // cout.tie(0); cin >> N >> M; prep(); int re = solve(N, 1) * solve(M, 0); // if(N == 512 and M == 512) cout << re << endl; cout << "! " << re << endl; }
18
#include <bits/stdc++.h> using namespace std; using namespace chrono; #define ll long long typedef unsigned long long ull; typedef long double lld; #define print(a) \ for (auto x : a) \ cout << x << " "; \ cout << endl #define print_upto(a, n) \ for (ll i = 1; i <= n; i++) \ cout << a[i] << " "; \ cout << endl #define take(a, n) \ for (ll i = 1; i <= n; i++) \ cin >> a[i]; #define watch(x) cout << (#x) << " is " << (x) << "\n" #define watch2(x, y) cout << (#x) << " is " << (x) << " and " << (#y) << " is " << (y) << "\n" #define watch3(x, y, z) cout << (#x) << " is " << (x) << " and " << (#y) << " is " << (y) << " and " << (#z) << " is " << (z) << "\n" #define watch4(x, y, z, u) cout << (#x) << " is " << (x) << " and " << (#y) << " is " << (y) << " and " << (#z) << " is " << (z) << " and " << (#u) << " is " << (u) << "\n" void my_debugger(string s, int LINE_NUM) { cerr << endl; } template <typename start, typename... end> void my_debugger(string s, int LINE_NUM, start x, end... y) { if (s.back() != ',') { s += ','; cerr << "LINE(" << LINE_NUM << "): "; } int i = s.find(','); cerr << s.substr(0, i) << " = " << x; s = s.substr(i + 1); if (!s.empty()) cerr << ", "; my_debugger(s, LINE_NUM, y...); } #ifndef ONLINE_JUDGE #define debug(...) my_debugger(#__VA_ARGS__, __LINE__, __VA_ARGS__); #else #define debug(...) #endif #define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define ff first #define ss second #define null NULL #define all(c) (c).begin(), (c).end() #define allr(c) (c).rbegin(), (c).rend() #define nl "\n" #define ld long double #define eb emplace_back #define pb push_back #define pf push_front #define pob pop_back #define pof pop_front #define MOD 1000000007 #define MOD2 998244353 #define mem1(a) memset(a, -1, sizeof(a)) #define mem0(a) memset(a, 0, sizeof(a)) #define set_bits(n) __builtin_popcountll(n) #define inf 1e17 typedef vector<ll> vl; typedef vector<vl> vvl; typedef pair<ll, ll> pll; typedef pair<ll, pll> ppll; #ifndef ONLINE_JUDGE #define debug2(x) \ cerr << #x << " "; \ _print(x); \ cerr << endl; #else #define debug2(x) #endif #ifndef ONLINE_JUDGE #define output_case_number(x) cerr << "\n\nCase : " << x << nl; #else #define output_case_number(x) #endif void _print(ll t) { cerr << t; } void _print(int t) { cerr << t; } void _print(string t) { cerr << t; } void _print(char t) { cerr << t; } void _print(lld t) { cerr << t; } void _print(double t) { cerr << t; } void _print(ull t) { cerr << t; } template <class T, class V> void _print(pair<T, V> p); template <class T> void _print(vector<T> v); template <class T> void _print(set<T> v); template <class T, class V> void _print(map<T, V> v); template <class T> void _print(multiset<T> v); template <class T, class V> void _print(pair<T, V> p) { cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}"; } template <class T> void _print(vector<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void _print(set<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void _print(multiset<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T, class V> void _print(map<T, V> v) { cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]"; } // cout << fixed << setprecision(15) << ans << nl; // cout << "Case #" << Q << ": "; // mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); // auto dist = uniform_int_distribution<int>(l, r); // ll random_val = dist(rng); const ll N = 200009; ll Q = 0; void solve() { ll n; cin >> n; string s; cin >> s; vector<pair<char, ll>> v; v.pb({s[0], 1}); for (ll i = 1; i < s.length(); i++) { auto p = v.back(); if (p.ff == s[i]) { v.pop_back(); v.pb({s[i], p.ss + 1}); } else { v.pb({s[i], 1}); } } ll f = 0; map<char, ll> mp; for (auto p : v) { if (mp[p.ff] == 1) { f = 1; break; } mp[p.ff]++; } if (f == 1) { cout << "NO" << nl; } else { cout << "YES" << nl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll T = 1; cin >> T; auto start1 = high_resolution_clock::now(); while (T--) { Q++; // output_case_number(Q); solve(); } auto stop1 = high_resolution_clock::now(); auto duration = duration_cast<microseconds>(stop1 - start1); // cerr << "Time: " << duration.count() / 1000 << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; string s; int cnt = 1, ans; int main() { cin >> s; for (int i = 1; i < s.size(); i++) { if (s[i] == s[i - 1]) cnt++; else { ans += (cnt + 4) / 5; cnt = 1; } } return cout << ans + (cnt + 4) / 5 << '\n', false; }
1
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { return (b = 0 ? a : gcd(b, a % b)); } int lcm(int a, int b) { return (a * (b / gcd(a, b))); } struct pto { double x; double y; pto() {} pto(double _x, double _y) { x = _x; y = _y; } }; int resp(int v[101][101], int n, int m) { bool flag = true; for (int i = 0; i < n / 2; ++i) { for (int j = 0; j < m; ++j) { if (v[i][j] != v[n - i - 1][j]) { flag = false; goto a; } } } a: if (n & 1) return n; if (!flag) return n; else return resp(v, n / 2, m); return n; } int main() { int n, m, p[101][101]; cin >> n >> m; for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) cin >> p[i][j]; cout << resp(p, n, m) << endl; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n == 1) { cout << 1 << endl; cout << 1; } else if (n == 2) { cout << 1 << endl; cout << 1; } else if (n == 3) { cout << 2 << endl; cout << "1 3"; } else if (n == 4) { cout << 4 << endl; cout << "2 4 1 3"; } else { cout << n << endl; for (int i = 1; i <= n; i += 2) { cout << i << ' '; } for (int i = 2; i <= n; i += 2) { cout << i << ' '; } } return 0; }
3
// Black lives matter #include <bits/stdc++.h> /// 500 485 462 A4 using namespace std; typedef long long int ll; typedef complex<double> point; typedef long double ld; #define pb push_back #define pii pair < ll , ll > #define F first #define S second //#define endl '\n' #define int long long #define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") #define kill(x) return cout<<x<<'\n', 0; const int N=2e5+100; ll mod=1e9+7; ll a[N]; ll fac[N],fm[N]; ll power(ll n,ll k){ if (k==0) return 1; if (k%2==1){ ll x=power(n,k/2); return x*x%mod*n%mod; } ll x=power(n,k/2); return x*x%mod; } ll ent(ll k,ll n){ if (k==0 || k==n) return 1; return fac[n]*fm[k]%mod*fm[n-k]%mod; } map <int,int> mp; int32_t main(){ sync; ll n; cin >> n; fac[0]=1; for (int i=1;i<N;i++) fac[i]=fac[i-1]*i%mod,fm[i]=power(fac[i],mod-2); ll cnt=0; for (int i=1;i<=n;i++) cin >> a[i],cnt+=a[i]; if (cnt%n!=0) kill(0); cnt/=n; ll t=0; ll v=0,u=0; for (int i=1;i<=n;i++){ if (a[i]==cnt) t++; if (a[i]>cnt) v++; if (a[i]<cnt) u++; } ll ans=1; for (int i=1;i<=n;i++){ mp[a[i]]++; ans*=power(mp[a[i]],mod-2); ans%=mod; } ans*=ent(t,n)*fac[t]%mod; if (t==n) kill(1); ans%=mod; if (v==1 || u==1){ ans*=(u+v)*fac[u]%mod*fac[v]%mod; kill(ans%mod); } ans*=fac[u]*fac[v]%mod*2%mod; kill(ans%mod); }
15
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; const int mod = 1e9 + 7; char s[maxn]; char t[maxn]; int f[maxn]; int w[maxn]; int dp[maxn]; int q[maxn]; int r[maxn]; int dp2[maxn]; int dp3[maxn]; void getFail(char *P, int *f) { int m = strlen(P); f[0] = 0; f[1] = 0; for (int i = 1; i < m; i++) { int j = f[i]; while (j && P[i] != P[j]) { j = f[j]; } f[i + 1] = P[i] == P[j] ? j + 1 : 0; } } void find(char *T, char *P, int *f) { int n = strlen(T); int m = strlen(P); getFail(P, f); int j = 0; int kk = -1; for (int i = 0; i < n; i++) { while (j && P[j] != T[i]) { j = f[j]; } if (P[j] == T[i]) { j++; } if (j == m) { w[i - m + 1] = 1; q[i] = 1; r[i] = kk; kk = i - m + 1; } } } int main() { while (scanf("%s%s", s, t) != EOF) { int i; memset(w, 0, sizeof(w)); memset(q, 0, sizeof(q)); find(s, t, f); int n = strlen(s); int m = strlen(t); for (i = 0; i < n; i++) { if (q[i]) { dp[i] = i - m + 2; dp2[i] = i - m + 2; dp3[i] = i - m + 2; break; } else { dp[i] = 0; dp2[i] = 0; dp3[i] = 0; } } i++; for (; i < n; i++) { if (q[i] == 0) { dp[i] = dp[i - 1]; dp2[i] = (dp[i] + dp2[i - 1]) % mod; dp3[i] = (dp2[i] + dp3[i - 1]) % mod; } else { dp[i] = (dp3[i - m] + i - m + 2) % mod; dp2[i] = (dp[i] + dp2[i - 1]) % mod; dp3[i] = (dp2[i] + dp3[i - 1]) % mod; } } printf("%d\n", dp2[n - 1]); } return 0; }
12
#include <bits/stdc++.h> using namespace std; int n, m, t, p[3009]; bool mk[3009]; pair<int, int> s[3009]; int l = -1; void do_turn(int u) { if (u == 1) { for (int i = 0; i < m; i++) { if (mk[s[i].first] && !mk[s[i].second]) { mk[s[i].second] = true; cout << s[i].second << endl; return; } if (mk[s[i].second] && !mk[s[i].first]) { mk[s[i].first] = true; cout << s[i].first << endl; return; } } for (int i = 0; i < m; i++) { if (!mk[s[i].first]) { if (p[s[i].first] > p[s[i].second]) { mk[s[i].first] = true; cout << s[i].first << endl; return; } if (p[s[i].second] > p[s[i].first]) { mk[s[i].second] = true; cout << s[i].second << endl; return; } } } int c = 0; for (int i = 1; i <= 2 * n; i++) { if (mk[c] || (!mk[i] && p[i] > p[c])) { c = i; } } mk[c] = true; cout << c << endl; } else { int c = 0; cin >> c; l = c; mk[c] = true; } } int main() { cin >> n >> m; for (int i = 0; i < 2 * n; i++) { cin >> p[i + 1]; } for (int i = 0; i < m; i++) { cin >> s[i].first >> s[i].second; } cin >> t; for (int i = 0; i < 2 * n; i++) { do_turn(t % 2); if (t % 2 == 1) cout.flush(); t++; } }
9
#include <bits/stdc++.h> using namespace std; int n; vector<int> edge[100005]; long long dp[100005][2]; void calc(int v) { long long tmp[2][2]; int cur = 0, nxt = 1; tmp[cur][0] = 0; tmp[cur][1] = 1; for (int j = 0; j < edge[v].size(); j++) { tmp[nxt][0] = 0; tmp[nxt][1] = 0; calc(edge[v][j]); for (int a = 0; a < 2; a++) { for (int b = 0; b < 2; b++) { tmp[nxt][(a + b) % 2] += (tmp[cur][a] * dp[edge[v][j]][b]); } } tmp[nxt][0] %= 1000000007; tmp[nxt][1] %= 1000000007; swap(cur, nxt); } dp[v][0] = tmp[cur][0] * 2 % 1000000007; dp[v][1] = tmp[cur][1] * 2 % 1000000007; long long x = 1; for (int j = 0; j < edge[v].size(); j++) { x = (x * dp[edge[v][j]][0]) % 1000000007; } dp[v][1] = (dp[v][1] + 1000000007 - x) % 1000000007; long long y[2][2][3] = {}; cur = 0, nxt = 1; y[0][1][0] = 1; for (int j = 0; j < edge[v].size(); j++) { for (int a = 0; a < 2; a++) for (int b = 0; b < 3; b++) y[nxt][a][b] = 0; for (int jj = 0; jj < 2; jj++) { y[nxt][jj][0] += y[cur][jj][0]; y[nxt][jj][1] += y[cur][jj][1]; y[nxt][jj][2] += y[cur][jj][2]; y[nxt][1 - jj][1] += y[cur][jj][0] * dp[edge[v][j]][1] % 1000000007; y[nxt][1 - jj][2] += y[cur][jj][1] * dp[edge[v][j]][1] % 1000000007; y[nxt][1 - jj][1] += y[cur][jj][2] * dp[edge[v][j]][1] % 1000000007; } for (int a = 0; a < 2; a++) for (int b = 0; b < 3; b++) y[nxt][a][b] %= 1000000007; swap(cur, nxt); } dp[v][0] = (dp[v][0] + 1000000007 * 2ll - y[cur][0][1] - y[cur][0][0] + 1) % 1000000007; dp[v][1] = (dp[v][1] + 1000000007 * 2ll - y[cur][1][1] - y[cur][1][0] + 1) % 1000000007; return; } int main() { cin >> n; for (int i = 2; i <= n; i++) { int x; scanf("%d", &x); edge[x].push_back(i); } calc(1); printf("%lld\n", (dp[1][0] + dp[1][1] + 1000000007 - 1) % 1000000007); }
19
#include <bits/stdc++.h> using namespace std; inline char gc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } inline int read() { int x = 0; char ch = getchar(); bool positive = 1; for (; !isdigit(ch); ch = getchar()) if (ch == '-') positive = 0; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; return positive ? x : -x; } inline void write(int a) { if (a >= 10) write(a / 10); putchar('0' + a % 10); } inline void writeln(int a) { if (a < 0) { a = -a; putchar('-'); } write(a); puts(""); } const int N = 2005; long long ans; int x[N], y[N], n; map<pair<int, int>, int> M; int main() { n = read(); for (int i = 1; i <= n; i++) { x[i] = read(); y[i] = read(); } for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { int t1 = x[i] - x[j], t2 = y[i] - y[j]; if (t1 < 0) t1 = -t1, t2 = -t2; else if (t1 == 0) t2 = abs(t2); ans += M[make_pair(t1, t2)]; M[make_pair(t1, t2)]++; } } cout << ans / 2 << endl; }
11
#include <bits/stdc++.h> using namespace std; int main() { int n, k; int rank[10] = {0}; string str[10]; while (cin >> n >> k) { for (int i = 0; i < k; i++) rank[i] = i; for (int i = 0; i < n; i++) cin >> str[i]; int ans = 1000000000; do { int min_ = 1000000000; int max_ = -1; for (int i = 0; i < n; i++) { int num = 0; for (int j = 0; j < k; j++) { num = num * 10 + str[i][rank[j]] - '0'; } min_ = min(min_, num); max_ = max(max_, num); } ans = min(ans, max_ - min_); } while (next_permutation(rank, rank + k)); cout << ans << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); int arr[n + 5]; for (int i = 0; i < n; i++) scanf("%d", &arr[i]); int mx = 1; int pos[n + 5]; for (int i = 0; i < n + 5; i++) pos[i] = 0; for (int i = 0; i < n; i++) { if (pos[arr[i] - 1]) { pos[arr[i]] = pos[arr[i] - 1] + 1; } else pos[arr[i]] = 1; if (mx < pos[arr[i]]) mx = pos[arr[i]]; } cout << n - mx << endl; }
8
#include <bits/stdc++.h> using namespace std; vector<vector<int> > g; vector<int> p, h, val; int flag = 0; int main() { int n; cin >> n; vector<int> v(n); map<int, long long> mp; long long ans = 0; for (int i = 0; i < n; i++) { cin >> v[i]; ans = max(ans, mp[i - v[i]] += v[i]); } cout << ans << endl; }
6
#include <bits/stdc++.h> using namespace std; long long read() { char cc = getchar(); long long cn = 0, flus = 1; while (cc < '0' || cc > '9') { if (cc == '-') flus = -flus; cc = getchar(); } while (cc >= '0' && cc <= '9') cn = cn * 10 + cc - '0', cc = getchar(); return cn * flus; } const long long N = 2e5 + 5; long long n, m, q, A[N], B[N], C[N]; long long head[N], cnt; struct E { long long to, next, w; } e[N * 2]; struct Tr { long long mi, tag; } tr[N * 4]; inline void add(long long x, long long y, long long z) { e[++cnt] = (E){y, head[x], z}, head[x] = cnt; } inline void build(long long x, long long l, long long r) { if (l == r) { tr[x].mi = B[l]; return; } long long mid = (l + r) >> 1; build(x * 2, l, mid), build(x * 2 + 1, mid + 1, r); tr[x].mi = min(tr[x * 2].mi, tr[x * 2 + 1].mi); } inline void pushmark(long long x) { if (tr[x].tag) tr[x * 2].mi += tr[x].tag, tr[x * 2 + 1].mi += tr[x].tag, tr[x * 2].tag += tr[x].tag, tr[x * 2 + 1].tag += tr[x].tag, tr[x].tag = 0; } inline void update(long long x, long long l, long long r, long long ql, long long qr, long long w) { if (ql > r || qr < l) return; if (ql <= l && r <= qr) { tr[x].mi += w, tr[x].tag += w; return; } long long mid = (l + r) >> 1; pushmark(x); update(x * 2, l, mid, ql, qr, w), update(x * 2 + 1, mid + 1, r, ql, qr, w); tr[x].mi = min(tr[x * 2].mi, tr[x * 2 + 1].mi); } void Init() { build(1, 1, n); for (register long long x = 1; x <= n; ++x) { for (register long long i = head[x]; i; i = e[i].next) update(1, 1, n, 1, e[i].to, e[i].w); C[x] = tr[1].mi; } memset(tr, 0, sizeof(tr)); for (register long long i = 1; i <= n; ++i) B[i] = A[i] + C[i]; build(1, 1, n); } signed main() { n = read(), m = read(), q = read(); long long x, y, z; for (register long long i = 2; i <= n; ++i) A[i - 1] = read(), B[i] = read(); B[1] = 0; for (register long long i = 1; i <= m; ++i) { x = read(), y = read(), z = read(), add(x, y, z); } Init(); printf("%I64d\n", tr[1].mi); for (register long long i = 1; i <= q; ++i) { x = read(), z = read(); update(1, 1, n, x, x, z - A[x]), A[x] = z; printf("%I64d\n", tr[1].mi); } return 0; }
19
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, m, h[N], q[N], d[N], s[N], vis[N], sg[N]; vector<int> g[N]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &h[i]); for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); g[u].push_back(v); d[v]++; } int ql = 1, qr = 0; for (int i = 1; i <= n; i++) if (!d[i]) q[++qr] = i; while (ql <= qr) { int x = q[ql++]; for (int i = 0; i < g[x].size(); i++) if (!--d[g[x][i]]) q[++qr] = g[x][i]; } for (int i = n; i; i--) { int x = q[i]; for (int j = 0; j < g[x].size(); j++) vis[sg[g[x][j]]] = i; while (vis[sg[x]] == i) sg[x]++; s[sg[x]] ^= h[x]; } for (int i = n; ~i; i--) if (s[i]) { int x; for (int j = 1; j <= n; j++) if (sg[j] == i && h[j] > (s[i] ^ h[j])) x = j; h[x] ^= s[sg[x]]; for (int j = 0; j < g[x].size(); j++) h[g[x][j]] ^= s[sg[g[x][j]]], s[sg[g[x][j]]] = 0; puts("WIN"); for (int j = 1; j <= n; j++) printf("%d ", h[j]); return 0; } puts("LOSE"); return 0; }
24
#include <bits/stdc++.h> using namespace std; template <class T> int getbit(int i, T X) { return (X & (1 << (i - 1))); } template <class T> T onbit(int i, T X) { return (X | (1 << (i - 1))); } template <class T> T offbit(int i, T X) { return (X | (1 << (i - 1)) - (1 << (i - 1))); } template <class T> T sqr(T x) { return (x * x); } template <class T> T cube(T x) { return (x * x * x); } template <class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } int csx[4] = {0, 0, -1, 1}; int csy[4] = {-1, 1, 0, 0}; const int MOD = 1000000007; const int infi = 1e9; const int maxn = 2e5 + 5; const int maxV = 1e6 + 5; int n, a[maxn + 5], f[2 * maxV + 5], d[2 * maxV + 5]; void enter() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); } void solve() { sort(a + 1, a + n + 1); n = unique(a + 1, a + n + 1) - &a[1]; memset(d, 0, sizeof(d)); for (int i = 1; i <= n; i++) d[a[i]] = 1; f[0] = 0; for (int i = 1; i <= 2 * a[n] + 5; i++) { f[i] = f[i - 1]; if (d[i]) f[i] = i; } int res = 0; for (int i = 1; i <= n; i++) { for (int j = 2 * a[i]; j <= 2 * a[n]; j += a[i]) { if (f[j - 1] >= a[i]) { res = max(res, f[j - 1] % a[i]); } } } cout << res; } int main() { enter(); solve(); return 0; }
13
#include <bits/stdc++.h> using namespace std; int n, ans; vector<int> a[2]; map<int, int> quantity[2]; long long cnt; int main() { ios::sync_with_stdio(0); int m, x, y; cin >> n >> m; while (m--) { cin >> x >> y; a[x].push_back(y); quantity[x][y]++; } sort(a[0].begin(), a[0].end()); sort(a[1].begin(), a[1].end()); for (int z = 0; z <= 1; z++) { if (a[z].size() >= 3) ans = max(ans, (a[z].back() - a[z][0]) * 2); if (a[z].size() < 2) continue; for (int i = 0; i < int(a[z ^ 1].size()); i++) if (a[z][0] <= a[z ^ 1][i] && a[z ^ 1][i] <= a[z].back()) ans = n * 2; if (a[z ^ 1].empty()) continue; for (int i = 0; i + 1 < int(a[z].size()); i++) { int x = a[z][i]; int id = upper_bound(a[z ^ 1].begin(), a[z ^ 1].end(), x) - a[z ^ 1].begin(); id--; if (id >= 0) ans = max(ans, (n - (x - a[z ^ 1][id])) * 2); } for (int i = 1; i < int(a[z].size()); i++) { int x = a[z][i]; int id = lower_bound(a[z ^ 1].begin(), a[z ^ 1].end(), x) - a[z ^ 1].begin(); if (id < int(a[z ^ 1].size())) ans = max(ans, (n - (a[z ^ 1][id] - x)) * 2); } } for (int z = 0; z <= 1; z++) if (a[z].size() >= 3 && (a[z].back() - a[z][0]) * 2 == ans) { int minCnt = 0, maxCnt = 0, otherCnt = 0; for (int i = 0; i < int(a[z].size()); i++) if (a[z][i] == a[z][0]) minCnt++; else if (a[z][i] == a[z].back()) maxCnt++; else otherCnt++; if (!ans) cnt += minCnt * (minCnt - 1LL) * (minCnt - 2LL) / 6; else { cnt += 1LL * minCnt * maxCnt * otherCnt; cnt += minCnt * (minCnt - 1LL) / 2 * maxCnt; cnt += maxCnt * (maxCnt - 1LL) / 2 * minCnt; } } if (ans == n * 2) for (int z = 0; z <= 1; z++) for (int i = 0; i < int(a[z].size()); i++) { if (a[z ^ 1].empty()) break; int x = a[z][i]; int id = upper_bound(a[z ^ 1].begin(), a[z ^ 1].end(), x) - a[z ^ 1].begin(); int idLess = upper_bound(a[z ^ 1].begin(), a[z ^ 1].end(), x - 1) - a[z ^ 1].begin(); long long equal = id - idLess; long long bigger = a[z ^ 1].size() - id; cnt += idLess * (bigger + equal); cnt += equal * (equal - 1LL) / 2; cnt += equal * bigger; } else for (int z = 0; z <= 1; z++) { if (a[z ^ 1].empty()) continue; for (int i = 0; i < int(a[z].size()); i++) { int x = a[z][i]; int id = upper_bound(a[z ^ 1].begin(), a[z ^ 1].end(), x) - a[z ^ 1].begin(); id--; if (id < 0) continue; int y = a[z ^ 1][id]; if (ans == (n - (x - y)) * 2) cnt += quantity[z ^ 1][y] * (a[z].size() - 1LL - i); } for (int i = 0; i < int(a[z].size()); i++) { int x = a[z][i]; int id = lower_bound(a[z ^ 1].begin(), a[z ^ 1].end(), x) - a[z ^ 1].begin(); if (id >= int(a[z ^ 1].size())) continue; int y = a[z ^ 1][id]; if (ans == (n - (y - x)) * 2) cnt += 1LL * quantity[z ^ 1][y] * i; } } cout << cnt << endl; }
20
#include <bits/stdc++.h> using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return {i, j}; } struct printer { ~printer() { cerr << endl; } template <class c> printer& operator,(c x) { cerr << boolalpha << x; return *this; } printer& operator,(string x) { cerr << x; return *this; } template <class c, class d> printer& operator,(pair<c, d> x) { return *this, "(", x.first, ", ", x.second, ")"; } template <class... d, template <class...> class c> printer& operator,(c<d...> x) { return *this, range(begin(x), end(x)); } template <class c> printer& operator,(rge<c> x) { *this, "["; for (auto it = x.b; it != x.e; ++it) *this, (it == x.b ? "" : ", "), *it; return *this, "]"; } }; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int my_rand(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); } void solve() { int n, K; cin >> n >> K; string s; cin >> s; vector<int> opt; int z = 0, o = 0; for (int i = 0; i < K; i++) { int zero = 0, one = 0; for (int j = i; j < n; j += K) { if (s[j] == '0') zero++; if (s[j] == '1') one++; } if (zero > 0 && one > 0) { cout << "NO" << endl; return; } if (zero > 0) { z++; for (int j = i; j < n; j += K) { s[j] = '0'; } } else if (one > 0) { o++; for (int j = i; j < n; j += K) { s[j] = '1'; } } } if (max(z, o) > K / 2) { cout << "NO" << endl; return; } cout << "YES" << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); int tc; cin >> tc; while (tc--) { solve(); } }
7
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 5e5 + 50; const long long LINF = 0x3f3f3f3f3f3f3f3f; const long long MOD = 1e9 + 7; const double EPS = 1e-6; const double Pi = acos(-1); void test() { cerr << "\n"; } template <typename T, typename... Args> void test(T x, Args... args) { cerr << x << " "; test(args...); } long long qpow(long long a, long long b) { return b ? ((b & 1) ? a * qpow(a * a % MOD, b >> 1) % MOD : qpow(a * a % MOD, b >> 1)) % MOD : 1; } long long qpow(long long a, long long b, long long c) { return b ? ((b & 1) ? a * qpow(a * a % c, b >> 1) % c : qpow(a * a % c, b >> 1)) % c : 1; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } int sign(double x) { return x < -EPS ? -1 : x > EPS; } int dbcmp(double l, double r) { return sign(l - r); } long long a[MAXN]; int mi[MAXN]; int tot[663]; int main() { int n; scanf("%d", &n); int maxv = 0, maxi = 0; for (int i = 1; i <= n; i++) { long long x; scanf("%lld", &x); a[i] = x; int cnt = 0; while (x % 2 == 0) { x /= 2; cnt++; } tot[cnt]++; mi[i] = cnt; if (maxv < tot[cnt]) { maxi = cnt; maxv = tot[cnt]; } } printf("%d\n", n - maxv); for (int i = 1; i <= n; i++) if (mi[i] != maxi) printf("%lld ", a[i]); }
11
#include <bits/stdc++.h> using namespace std; char temp[1000005]; int qtds[256]; int qtdt[256]; int main() { scanf("%s", temp); string s = temp; scanf("%s", temp); string t = temp; priority_queue<pair<int, char>> prox; for (int i = 0; i < s.size(); i++) { qtds[s[i]]++; } for (int i = 0; i < t.size(); i++) { qtdt[t[i]]++; } for (char c = 'a'; c <= 'z'; c++) { if (qtdt[c] == 0) continue; int qtd = qtds[c] / qtdt[c]; prox.push({-qtd, c}); } for (int i = 0; i < s.size(); i++) { if (s[i] == '?') { int qtd = prox.top().first; char c = prox.top().second; prox.pop(); s[i] = c; qtds[c]++; qtd = qtds[c] / qtdt[c]; prox.push({-qtd, c}); } } printf("%s\n", s.c_str()); return 0; }
7
#include <bits/stdc++.h> using namespace std; const int P[26] = {0, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; int A[3000050], f[3000050]; bool vis[3000050]; int l, r, p, tot; void dfs(int i, int x) { if (i > 25) { tot++; A[tot] = x; return; } while (1) { dfs(i + 1, x); if (r / P[i] < x) break; x *= P[i]; } } int main() { int i, j, k, ans; scanf("%d%d%d", &l, &r, &p); tot = 0; dfs(1, 1); sort(A + 1, A + tot + 1); memset(f, 0x3f, sizeof(f)); memset(vis, false, sizeof(vis)); f[1] = 0; vis[1] = true; for (i = 2; i <= p; i++) { k = 0; for (j = 1; j <= tot; j++) { if (r / i < A[j]) break; if (f[j] + i <= p) { while ((k < tot) && (A[k + 1] <= A[j] * i)) k++; f[k] = min(f[k], f[j] + 1); if (f[k] + i <= p) vis[k] = true; } } } ans = 0; for (i = 1; i <= tot; i++) if ((A[i] >= l) && (A[i] <= r) && vis[i]) ans++; printf("%d\n", ans); return 0; }
20
#include <bits/stdc++.h> using namespace std; int cnt[100001][2]; int main() { int t; cin >> t; while(t--) { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; cnt[a[i]][i % 2]++; } sort(a.begin(), a.end()); for (int i = 0; i < n; i++) { cnt[a[i]][i % 2]--; } bool flag = 1; for(int i = 0; i < n; i++) { if (cnt[a[i]][0] || cnt[a[i]][1]) flag = 0; } cout << ((flag) ? "YES\n" : "NO\n"); for (int i = 0; i < n; i++) { cnt[a[i]][0] = cnt[a[i]][1] = 0; } } return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { int m; int a[1001]; scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", &a[i]); } sort(a, a + m); if (a[0] == 1) printf("-1\n"); else printf("1\n"); return 0; }
2
#include <bits/stdc++.h> using namespace std; struct node { int point; char name[15]; } p1[1005], p2[1005]; int n, m; char ans[1005][2][15], *d[5] = {"noob", "random", "average", "hardcore", "pro"}; bool cmp1(const node& a, const node& b) { if (!strcmp(a.name, b.name)) return a.point > b.point; if (strcmp(a.name, b.name) > 0) return 0; return 1; } bool cmp2(const node& a, const node& b) { return a.point < b.point; } void init() { int i, j; for (i = 0; i < n; i++) cin >> p1[i].name >> p1[i].point; sort(p1, p1 + n, cmp1); m = 1; p2[0] = p1[0]; for (i = 1; i < n; i++) if (strcmp(p1[i].name, p1[i - 1].name)) p2[m++] = p1[i]; } void solve() { int i, j, k; double wor; k = 0; for (i = 0; i < m; i++) { wor = 0; for (j = 0; j < m; j++) if (p2[j].point > p2[i].point) wor = wor + 1; wor /= m; strcpy(ans[++k][0], p2[i].name); if (wor > 0.5) strcpy(ans[k][1], d[0]); else if (wor > 0.2) strcpy(ans[k][1], d[1]); else if (wor > 0.1) strcpy(ans[k][1], d[2]); else if (wor > 0.01) strcpy(ans[k][1], d[3]); else strcpy(ans[k][1], d[4]); } cout << k << endl; for (i = 1; i <= k; i++) cout << ans[i][0] << ' ' << ans[i][1] << endl; } int main() { while (~scanf("%d", &n)) { init(); solve(); } return 0; }
6
#include <bits/stdc++.h> using namespace std; template <class T> inline T gcd(T a, T b) { if (a < 0) return gcd(-a, b); if (b < 0) return gcd(a, -b); return (b == 0) ? a : gcd(b, a % b); } int n, m, k, ary[1010][1010]; int indx[1010], indy[1010]; int main(void) { ios_base::sync_with_stdio(0); cin >> n >> m >> k; for (int i = 0; i < 1010; i++) { indx[i] = indy[i] = i; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> ary[i][j]; } } char ch; int x, y; for (int i = 0; i < k; i++) { cin >> ch >> x >> y; if (ch == 'g') { cout << ary[indx[x - 1]][indy[y - 1]] << endl; } else if (ch == 'c') { swap(indy[x - 1], indy[y - 1]); } else swap(indx[x - 1], indx[y - 1]); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int a, n, k, i, c = 0, i1 = 0; vector<pair<int, int> > v; cin >> n >> k; for (i = 0; i < n; i++) { cin >> a; v.push_back(make_pair(a, i + 1)); } sort(v.begin(), v.end()); while (c <= k) { c += v[i1].first; if (i1 < n && c <= k) i1++; else break; } cout << i1 << "\n"; for (i = 0; i < i1; i++) cout << v[i].second << " "; return 0; }
2
#include <bits/stdc++.h> char str[1111]; int main() { int a, b, c, d, e, f, g, h; scanf("%d", &b); while (b--) { scanf("%s", str); int len = strlen(str); for (d = 0, a = 0; d < len; d++) { if (d && str[d] >= '0' && str[d] <= '9' && str[d - 1] >= 'A' && str[d - 1] <= 'z') a++; } if (a == 2) { for (e = 1; e < len; e++) { if (str[e] >= 'A' && str[e] <= 'Z') break; } std::stack<char> sta; for (f = 1 + e, d = 0; f < len; f++) { d *= 10; d += str[f] - '0'; } while (d) { sta.push('A' + ((d + 25) % 26)); if (d && d % 26 == 0) d -= 26; d /= 26; } while (!sta.empty()) { printf("%c", sta.top()); sta.pop(); } for (e = 1; e < len; e++) { if (str[e] >= 'A' && str[e] <= 'Z') break; printf("%c", str[e]); } } else { printf("R"); for (d = 0, e = 0; d < len; d++) { if (str[d] >= '0' && str[d] <= '9') break; e *= 26; e += str[d] - 'A' + 1; } for (; d < len; d++) { printf("%c", str[d]); } printf("C%d", e); } printf("\n"); } return 0; }
8
#include <bits/stdc++.h> using namespace std; const long long N = 100010, M = 1000010, INF = 0x3f3f3f3f; inline long long max(long long x, long long y) { return x > y ? x : y; } inline long long min(long long x, long long y) { return x < y ? x : y; } inline void swap(long long &x, long long &y) { x ^= y ^= x ^= y; } long long head[N], ver[M], edge[M], Next[M], tot; long long n, L, W, rt, ans, c[N], d[N], vis[N], size[N]; vector<pair<long long, long long> > v; void add(long long x, long long y) { for (++x; x < N; x += x & -x) c[x] += y; } long long ask(long long x) { long long res = 0; x = max(x, 0) + 1; for (; x; x -= x & -x) res += c[x]; return res; } void add(long long x, long long y, long long z) { ver[++tot] = y, edge[tot] = z, Next[tot] = head[x], head[x] = tot; } void get_size(long long x, long long fa) { size[x] = 1; for (long long i = head[x]; i; i = Next[i]) { long long y = ver[i]; if (y == fa || vis[y]) continue; get_size(y, x); size[x] += size[y]; } } void get_rt(long long x, long long fa, long long sum) { size[x] = 1; long long flag = 1; for (long long i = head[x]; i; i = Next[i]) { long long y = ver[i]; if (y == fa || vis[y]) continue; get_rt(y, x, sum); size[x] += size[y]; if (size[y] > sum / 2) flag = 0; } if (flag && sum - size[x] <= sum / 2) rt = x; } void get_dis(long long x, long long fa, long long val, long long dis) { v.push_back(make_pair(val, dis)); if (val >= W || dis >= L) return; for (long long i = head[x]; i; i = Next[i]) { long long y = ver[i], z = edge[i]; if (y == fa || vis[y]) continue; get_dis(y, x, val + z, dis + 1); } } void calc(long long x) { for (long long i = head[x]; i; i = Next[i]) { long long y = ver[i], z = edge[i]; if (vis[y]) continue; v.clear(); get_dis(y, x, z, 1); sort(v.begin(), v.end()); for (pair<long long, long long> z : v) add(z.second, 1); for (long long l = 0, r = v.size() - 1; l < (long long)v.size(); l++) { add(v[l].second, -1); while (l < r && v[l].first + v[r].first > W) add(v[r--].second, -1); if (l >= r) break; ans -= ask(L - v[l].second); } } v.clear(); get_dis(x, 0, 0, 0); sort(v.begin(), v.end()); for (pair<long long, long long> z : v) add(z.second, 1); for (long long l = 0, r = v.size() - 1; l < (long long)v.size(); l++) { add(v[l].second, -1); while (l < r && v[l].first + v[r].first > W) add(v[r--].second, -1); if (l >= r) break; ans += ask(L - v[l].second); } } void solve(long long x) { vis[x] = 1; calc(x); for (long long i = head[x]; i; i = Next[i]) { long long y = ver[i]; if (vis[y]) continue; get_size(y, x); get_rt(y, x, size[y]); solve(rt); } } int main() { scanf("%lld%lld%lld", &n, &L, &W); for (long long i = 1; i < n; i++) { long long p, w; scanf("%lld%lld", &p, &w); add(i + 1, p, w), add(p, i + 1, w); } get_size(1, 0); get_rt(1, 0, n); solve(rt); printf("%lld\n", ans); return 0; }
19
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; int n, m, s, e; int a[maxn], b[maxn], dp[305]; vector<int> bv[maxn]; int main() { scanf("%d%d%d%d", &n, &m, &s, &e); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= m; i++) { scanf("%d", &b[i]); bv[b[i]].push_back(i); } int ans = 0; int len = s / e; memset(dp, 0x3f3f3f3f, sizeof(dp)); dp[0] = 0; for (int i = 1; i <= n; i++) { for (int j = len; j >= 0; j--) { vector<int>::iterator small_num; small_num = upper_bound(bv[a[i]].begin(), bv[a[i]].end(), dp[j]); if (small_num == bv[a[i]].end()) continue; dp[j + 1] = min(dp[j + 1], *small_num); if (i + dp[j + 1] + (j + 1) * e <= s) { ans = max(j + 1, ans); } } } printf("%d\n", ans); }
15
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); int casos; string txt; cin >> casos; while (casos--) { cin >> txt; if (txt.size() <= 10) cout << txt << "\n"; else cout << txt[0] << txt.size() - 2 << txt[txt.size() - 1] << "\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> a; multimap<int, int, greater<int>> m1; for (int i = 0; i < n; i++) { int p; cin >> p; a.push_back(p); } for (int i = 0; i < n; i++) { int p; cin >> p; m1.insert({a[i], p}); } int sum = 0; int ans = 0; for (auto itr = m1.begin(); itr != m1.end(); itr++) { if (sum + itr->second >= itr->first) { ans = max(sum, itr->first); break; } else { sum += itr->second; } } cout << max(ans, sum) << endl; } }
6
#include <bits/stdc++.h> using namespace std; const int INF = (int)1e9; int n; long long x, y, sumx = 0, sumy = 0, qx = 0, qy = 0; int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%I64d %I64d", &x, &y); sumx += x; sumy += y; qx += x * x; qy += y * y; } printf("%I64d\n", n * (qx + qy) - (sumx * sumx + sumy * sumy)); return 0; }
9
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 7; int n; long long c, p[maxn], s[maxn], f[maxn], ans, sum; int main() { cin >> n >> c; for (int i = 1; i <= n; i++) cin >> p[i]; for (int i = 1; i <= n; i++) cin >> s[i]; for (int i = 1; i <= n; i++) { f[i] = 1e18; for (int j = i; j >= 1; j--) f[j] = min(f[j] + j * c + p[i], f[j - 1] + s[i]); f[0] += p[i]; } ans = 1e18; for (int i = 0; i <= n; i++) ans = min(ans, f[i]); cout << ans << endl; }
21
#include <bits/stdc++.h> const long double eps = 1e-9; const double pi = acos(-1.0); const long long inf = 1e18; using namespace std; int n, x, a, ans = 0, pp[111111], h = 11111, t = 11110; int used[111111], pref[111111]; int main(int argc, const char* argv[]) { scanf("%d%d", &n, &x); for (int i = 0; i < n; i++) { scanf("%d", &a); used[a]++; } if (used[x] == 0) { ans = 1; used[x] = 1; } for (int i = 0; i < 111111; i++) { for (int j = 0; j < used[i]; j++) { ++t; pp[t] = i; } } while (pp[h - 1 + (t - h + 2) / 2] != x) { if (pp[h - 1 + (t - h + 2) / 2] > x) { --h; pp[h] = 1; continue; } if (pp[h - 1 + (t - h + 2) / 2] < x) { ++t; pp[t] = 100000; continue; } } cout << (t - h + 1) - n; return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s, s1, s2; cin >> s; int i, j; cin >> s1; string s3 = "6789TJQKA"; cin >> s2; if (s1[1] == s[0] && s2[1] != s[0]) cout << "YES"; else if (s1[1] == s[0] && s2[1] == s[0]) { for (i = 0; i < s3.size(); i++) { if (s1[0] == s3[i]) break; } for (j = 0; j < s3.size(); j++) { if (s2[0] == s3[j]) break; } if (i > j) cout << "YES"; else cout << "NO"; } else if (s1[1] != s[0] && s2[1] == s[0]) { cout << "NO"; } else { if (s1[1] == s2[1]) { for (i = 0; i < s3.size(); i++) { if (s1[0] == s3[i]) break; } for (j = 0; j < s3.size(); j++) { if (s2[0] == s3[j]) break; } if (i > j) cout << "YES"; else cout << "NO"; } else cout << "NO"; } return 0; }
2
#include <bits/stdc++.h> #pragma GCC optimize("fast-math") #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC diagnostic ignored "-Woverflow" using namespace std; const short INF16 = ~(1 << 15); const int INF32 = 2e9 + 11; const int SZ = 4e5 + 11; const int SZ2 = int(1e6 + 11); const int mod = 1e9 + 7; const int mod2 = 998244353; const int PIVO = 1e6 + 11; const long long INF64 = 8e18; const long long P1 = 53; const long long P2 = 47; const long double eps = 1e-10; const long double PI = acosl(-1); inline long long MOD(long long x) { x %= mod, x += mod; return x % mod; } inline long long MOD(long long x, long long m) { x %= m, x += m; return x % m; } inline long long T(int x) { return 1ll * x * (x + 1) >> 1; } inline long long TT(int x) { return 1ll * x * (x + 1) * (x + 2) / 6; } inline long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } inline long long lcm(long long a, long long b) { return a * b / gcd(a, b); } long long fpow(long long a, long long b, long long mod) { if (!b) return 1; if (b == 1) return a % mod; if (b & 1) return fpow(a, b ^ 1, mod) * a % mod; long long d = fpow(a, b >> 1, mod); return d * d % mod; } long long fpow(long long a, long long b) { if (!b) return 1; if (b == 1) return a; if (b & 1) return fpow(a, b ^ 1) * a; long long d = fpow(a, b >> 1); return d * d; } vector<vector<int>> g; vector<int> h, p; int n; void dfs(int v, int& finish, int pp = 0, int hh = 1) { h[v] = hh; p[v] = pp; if (h[v] >= (n + 1) / 2) finish = v; for (int u : g[v]) if (!h[u]) dfs(u, finish, v, hh + 1); } void Solve(int num_test) { int m; cin >> n >> m; g.clear(); h.clear(); p.clear(); g.resize(n + 1); h.resize(n + 1); p.resize(n + 1); for (int i = 0; i < m; i++) { int v, u; cin >> v >> u; g[v].push_back(u); g[u].push_back(v); } int finish = -1; dfs(1, finish); if (finish == -1) { vector<vector<int>> cnt((n + 1) / 2 + 1); vector<pair<int, int>> ans; for (int v = 1; v <= n; v++) cnt[h[v]].push_back(v); for (int hh = 1; hh <= (n + 1) / 2; hh++) { while (cnt[hh].size() >= 2) { pair<int, int> kek = {cnt[hh][cnt[hh].size() - 1], cnt[hh][cnt[hh].size() - 2]}; cnt[hh].erase(cnt[hh].begin() + cnt[hh].size() - 1); cnt[hh].erase(cnt[hh].begin() + cnt[hh].size() - 1); ans.push_back(kek); } } cout << "PAIRING\n" << ans.size() << '\n'; for (auto x : ans) cout << x.first << ' ' << x.second << '\n'; } else { cout << "PATH\n"; int pp = finish; vector<int> ans = {}; while (pp) { ans.push_back(pp); pp = p[pp]; } cout << ans.size() << '\n'; for (int x : ans) cout << x << ' '; cout << '\n'; } } signed main() { ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL); cout << fixed << setprecision(10); srand(time(NULL)); int test = 1; cin >> test; for (int i = 1; i <= test; i++) Solve(i); }
18
#include <bits/stdc++.h> char s[100005]; int cnt[26], n, i, cur = 0, tot = 0; char t[100005]; int main() { scanf("%s", s); n = strlen(s); for (i = 0; i < n; i++) cnt[s[i] - 'a']++; for (i = 25; i >= 0; i--) { while (cnt[i]) { while (s[cur] != 'a' + i) cnt[s[cur] - 'a']--, cur++; t[tot++] = 'a' + i; cnt[s[cur] - 'a']--; cur++; } } for (i = 0; i < tot; i++) printf("%c", t[i]); }
3
#include <bits/stdc++.h> using namespace std; int a[10]; int main() { int n, x; cin >> n >> x; n = n % 6; a[x] = 1; while (n > 0) { if (n % 2 == 0) { swap(a[1], a[2]); } else swap(a[0], a[1]); n--; } for (int i = 0; i <= 10; i++) { if (a[i] == 1) { cout << i; break; } } return 0; }
2
#include <bits/stdc++.h> int main() { int h1, h2, a, b; scanf("%d %d", &h1, &h2); scanf("%d %d", &a, &b); if (h2 - h1 <= 8 * a) printf("%d", 0); else if (a <= b) printf("%d", -1); else { h1 = h1 + a * 8 - b * 12; int count = 1; while (h1 + 12 * a < h2) { h1 = a * 12 + h1 - b * 12; count++; } printf("%d", count); } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 101; int n, m, k, a[N]; int read() { int d = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') d = (d << 3) + (d << 1) + c - 48, c = getchar(); return d * f; } void judge() { freopen(".in", "r", stdin); freopen(".out", "w", stdout); } int main() { n = read(); m = read(); k = read(); for (int i = 1; i <= n; i++) a[i] = read(); int ans = 1e9; for (int i = 0; i <= n; i++) { int x = m - i, y = m + i; if (x >= 1 && a[x] != 0 && a[x] <= k) ans = min(ans, i * 10); if (y <= n && a[y] != 0 && a[y] <= k) ans = min(ans, i * 10); } printf("%d", ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 110; int k; int main() { scanf("%d", &k); if (k & 1) puts("-1"), exit(0); for (int i = 1; i <= k; ++i) { for (int j = 1; j <= k; ++j, puts("")) for (int p = 1; p <= k; ++p) { int tmp = min(min(j, p), min(k - j + 1, k - p + 1)); tmp ^= (i & 1); if (tmp & 1) printf("b"); else printf("w"); } puts(""); } fclose(stdin); fclose(stdout); return 0; }
8
#include <bits/stdc++.h> using namespace std; struct BO { int a, b; } a[300]; int n, tot, mt, tt; int f[110][11000]; int cmp(const BO &x, const BO &y) { return x.b < y.b; } int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i].a; tot += a[i].a; } for (int i = 1; i <= n; i++) { cin >> a[i].b; mt += a[i].b; } sort(a + 1, a + n + 1, cmp); int K = 0; tt = tot; for (int i = n; i >= 1 && tot > 0; i--) { K++; tot -= a[i].b; } int ans = 0; memset(f, -127 / 3, sizeof(f)); f[0][0] = 0; for (int i = 1; i <= n; i++) { for (int j = K; j >= 1; j--) for (int k = mt; k >= a[i].b; k--) f[j][k] = max(f[j][k], f[j - 1][k - a[i].b] + a[i].a); } for (int k = tt; k <= mt; k++) ans = max(ans, f[K][k]); cout << K << ' ' << tt - ans << endl; }
11
#include <bits/stdc++.h> using namespace std; int flag[20]; bool fun(int a) { while (a) { if (flag[a % 10]) return true; a = a / 10; } return false; } int main() { int n, t, i, sum; while (scanf("%d", &n) != EOF) { memset(flag, 0, sizeof(flag)); t = n; while (t) { flag[t % 10] = 1; t = t / 10; } sum = 0; for (i = 1; i * i <= n; i++) { if (n % i == 0) { if (fun(i)) sum++; if (n / i != i && fun(n / i)) sum++; } } printf("%d\n", sum); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int NO = 1000005; const int MOD = 1000000007; long long value[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, bit[10] = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10}; string p[NO]; string s; int main() { cin >> s; int n; scanf("%d", &n); for (int i = 1; i <= n; i++) cin >> p[i]; for (int i = n; i >= 1; i--) { long long v = 0, b = 1; for (int j = p[i].length() - 1; j >= 3; j--) { v = (v + value[p[i][j] - '0'] * b) % MOD; b = b * bit[p[i][j] - '0'] % MOD; } value[p[i][0] - '0'] = v; bit[p[i][0] - '0'] = b; } long long ans = 0; for (int i = 0; s[i]; i++) ans = (ans * bit[s[i] - '0'] + value[s[i] - '0']) % MOD; printf("%I64d\n", ans); return 0; }
13
#include <bits/stdc++.h> using namespace std; const int N = 1000; int q, k, n, a, b; bool check(int mid) { int s = 0; s += mid; int kk = k - mid * b; s += (kk) / a; if (kk % a == 0) s--; return s >= n; } int main() { ios::sync_with_stdio(false); cin >> q; while (q--) { cin >> k >> n >> a >> b; int cnt = k / b; if (k % b == 0) cnt--; if (cnt < n) cout << -1 << endl; else { int l = 0, r = (k / b), mid; int ans; if (k % b == 0) r--; while (l <= r) { mid = (l + r) / 2; if (check(mid)) { ans = (k - (mid * b)) / a; if ((k - (mid * b)) % a == 0) ans--; r = mid - 1; } else l = mid + 1; } if (ans > n) ans = n; cout << ans << endl; } } return 0; }
6
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> std::pair<T, U> operator+(const std::pair<T, U> &l, const std::pair<T, U> &r) { return {l.first + r.first, l.second + r.second}; } typedef void (*callback_function)(void); const long long ZERO = 0LL; const long long INF64 = 1e18; const long long INF32 = 1e9; const long long MOD = 1e9 + 7; const long double PI = acos(-1.0L); const long double EPS = static_cast<long double>(1e-9); long long inv(long long a, long long b) { if (a != 1) { return b - (inv(b % a, a) * b) / a; } return 1; } inline long long Pow(long long a, long long k) { long long s = 1; for (; k; k >>= 1) { k & 1 ? s = 1LL * s * a % MOD : 0; a = 1LL * a * a % MOD; } return s; } const long long N = 1e6 + 7; void input() {} void output() {} void preprocess() {} void debug() { if (true) { } } void solve() { preprocess(); string s[2]; long long n; cin >> s[0] >> s[1] >> n; map<char, map<long long, long long> > red, yel; for (long long i = (0); i < (n); i++) { long long minute, num; char team, card; cin >> minute >> team >> num >> card; if (!red[team].count(num) and (card == 'r' or yel[team].count(num))) { red[team][num] = minute; } else { yel[team][num] = minute; } } vector<pair<pair<long long, long long>, long long> > v; for (auto &p : red['h']) { v.push_back({pair<long long, long long>(p.second, p.first), 0}); } for (auto &p : red['a']) { v.push_back({pair<long long, long long>(p.second, p.first), 1}); } sort(v.begin(), v.end()); for (auto &t : v) { cout << s[t.second] << ' ' << t.first.second << ' ' << t.first.first << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(); { input(); solve(); output(); } return 0; }
5
#include <bits/stdc++.h> using namespace std; bool comp(long long a, long long b) { if (a >= b) return true; else return false; } bool solve(long long a[], long long n) { long long sz = 1 << n - 1; for (long long i = 0; i <= sz; i++) { long long sum = 0; for (long long j = 0; j < n; j++) { if (i & (1 << j)) sum += (-1) * a[j]; else sum += a[j]; } if (sum % 360 == 0) { return true; } } return false; } signed main() { long long n; cin >> n; long long a[n]; for (long long i = 0; i < n; i++) cin >> a[i]; bool ans = solve(a, n); if (ans == true) cout << "YES" << endl; else cout << "NO" << endl; }
4
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 7; const long long INF = 1e9; const long long MOD = INF + 7; string s; vector<string> ask(long long lo, long long hi) { cout << "? " << lo << " " << hi << endl; long long len = hi - lo + 1; long long cnt = len * (len + 1) / 2LL; vector<string> nu(cnt); for (long long i = 0; i < cnt; i++) { cin >> s; sort((s).begin(), (s).end()); nu[i] = s; } return nu; } void fnd(string res) { cout << "! " << res << endl; } char gogo(string x, string y) { vector<long long> cnt(26); for (long long i = 0; i < ((long long)(x).size()); i++) { cnt[x[i] - 'a']++; } for (long long i = 0; i < ((long long)(y).size()); i++) { cnt[y[i] - 'a']--; } char send; for (long long i = 0; i < 26; i++) { if (cnt[i] > 0) send = i + 'a'; } return send; } void solve() { long long n; cin >> n; vector<string> a = ask(1, n); if (n == 1) fnd(a[0]); vector<string> b = ask(2, n); map<string, long long> mp; for (long long i = 0; i < ((long long)(a).size()); i++) { mp[a[i]]++; } for (long long i = 0; i < ((long long)(b).size()); i++) { mp[b[i]]--; } map<long long, string> mapchik; for (auto [x, y] : mp) { if (y > 0) { mapchik[((long long)(x).size())] = x; } } string res; for (long long i = 1; i <= n; i++) { res.push_back(gogo(mapchik[i], mapchik[i - 1])); } fnd(res); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1; while (t--) { solve(); } }
16
#include <bits/stdc++.h> using namespace std; int n, ans; void f(int x) { if (x > n) return; ans++; f(x * 10); f(x * 10 + 1); } int main() { cin >> n; f(1); cout << ans << endl; }
4
#include <bits/stdc++.h> using namespace std; const long long inf = 1e15; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<vector<long long>> adj(n + 1, vector<long long>(n + 1)); for (int q = 1; q <= n; q++) { for (int w = 1; w <= n; w++) { cin >> adj[q][w]; } } vector<int> order(n); for (int q = 0; q < n; q++) { cin >> order[q]; } vector<long long> ans(n); ans[n - 1] = 0; vector<vector<long long>> dist(n + 1, vector<long long>(n + 1, inf)); vector<int> curnodes; curnodes.push_back(order[n - 1]); dist[order[n - 1]][order[n - 1]] = 0; for (int q = n - 2; q >= 0; q--) { int v = order[q]; for (int w : curnodes) { for (int e : curnodes) { dist[w][v] = min(dist[w][v], dist[w][e] + adj[e][v]); dist[v][w] = min(dist[v][w], adj[v][e] + dist[e][w]); } } for (int w : curnodes) { for (int e : curnodes) { dist[w][e] = min(dist[w][e], dist[w][v] + dist[v][e]); } } long long tot = 0; for (int w = 1; w <= n; w++) { for (int e = 1; e <= n; e++) { if (dist[w][e] != inf) { tot += dist[w][e]; } } } dist[v][v] = 0; ans[q] = tot; curnodes.push_back(v); } for (int q = 0; q < n; q++) { cout << ans[q] << " "; } cout << "\n"; return 0; }
9
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; int main() { long long n, m, k, b, o = 2, f = 1; cin >> n >> m >> k; b = m * n; for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= m; j++) { if (o == 2) { if (k > 1) { b -= 2; o = 0; k--; cout << "2 "; } else { cout << b << " "; } } cout << i << " "; cout << ((i % 2) ? j : m - j + 1); cout << " "; o++; if (o == 2) cout << endl; } } return 0; }
7
#include <bits/stdc++.h> using namespace std; long long tot[1010], individu[1010]; int main(void) { int n; scanf("%d", &n); int arr[1010]; for (int i = 0; i < n; i++) scanf("%d", &arr[i]), arr[i]--; memset(tot, 0, sizeof(tot)); tot[0] = 2; individu[0] = 2; for (int i = 1; i < n; i++) { tot[i] = tot[i - 1]; individu[i] = 2; for (int j = arr[i]; j < i; j++) { individu[i] += individu[j]; individu[i] %= 1000000007; } tot[i] = (tot[i] + individu[i]) % 1000000007; } cout << tot[n - 1]; return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; int m = max(a, b); if (m == 1) { cout << "1/1"; } if (m == 2) { cout << "5/6"; } if (m == 3) { cout << "2/3"; } if (m == 4) { cout << "1/2"; } if (m == 5) { cout << "1/3"; } if (m == 6) { cout << "1/6"; } }
0
#include <bits/stdc++.h> using namespace std; const int N = 201; int n, k; int w[N]; vector<int> g[N]; int dp[N][N]; int cur[N]; void dfs(int v, int last = -1) { for (int u : g[v]) { if (u != last) { dfs(u, v); } } fill(cur, cur + N, 0); for (int u : g[v]) { if (u != last) { for (int h = 0; h <= k; ++h) { cur[h] += dp[u][h]; } } } for (int d = 0; d <= k; ++d) { for (int m = max(0, d - 1); m < k && max(k - 1 - m, d - 1) >= m; ++m) { for (int u : g[v]) { if (u != last) { dp[v][d] = max(dp[v][d], cur[max(k - 1 - m, d - 1)] - dp[u][max(k - 1 - m, d - 1)] + dp[u][m]); } } } } int a = w[v]; for (int u : g[v]) { if (u != last) { a += dp[u][k]; } } dp[v][0] = max(dp[v][0], a); } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> k; for (int v = 0; v < n; ++v) { cin >> w[v]; } for (int i = 0; i < n - 1; ++i) { int v, u; cin >> v >> u; --v; --u; g[v].push_back(u); g[u].push_back(v); } dfs(0); cout << dp[0][0] << '\n'; return 0; }
10
#include <bits/stdc++.h> using namespace std; void read(int &k) { k = 0; char x = getchar(); while (x < '0' || x > '9') x = getchar(); while (x >= '0' && x <= '9') { k = k * 10 - 48 + x; x = getchar(); } } double D, U, yw, xb, yb, r; int main() { cin >> D >> U >> yw >> xb >> yb >> r; yw -= r; yb = 2 * yw - yb; long double k = (yb - (D + r)) / xb; long double b = D + r; long double A = k * k + 1, B = 2 * k * b - 2 * k * U, C = b * b + U * U - 2 * b * U - r * r; if (B * B - 4 * A * C - 1e-10 < 0) printf("%.10lf\n", double((yw - b) / k)); else puts("-1"); return 0; }
12
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; const long long INF = 2e18; long long a[maxn], b[maxn]; int n, k; int main() { scanf("%d%d", &n, &k); for (int i = 0; i <= n; i++) scanf("%I64d", &a[i]); b[n] = a[n]; for (int i = 0; i < n; i++) { long long t = b[i] + a[i]; b[i + 1] += t / 2; b[i] = t % 2; } int rec = 0; for (int i = 0; i <= n; i++) if (b[i]) { rec = i; break; } int ans = 0; long long res = 0; for (int i = n; i >= 0; i--) { res = res * 2 + b[i]; if (res > INF || res < -INF) break; if (i <= rec) { long long t = a[i] - res; if (t <= k && t >= -k && (t != 0 || i != n)) ans++; } } cout << ans << endl; return 0; }
14
#include <bits/stdc++.h> using namespace std; int main() { long long n, p; while (cin >> n >> p) { long long ans = 0; long long tmp = n, cnt = 0, tmp1; bool l = 0; while (tmp > 0) { tmp1 = 0; for (int i = 0; i < 32; ++i) { if ((1ll << i) & tmp) { ++tmp1; } } if (cnt >= tmp1 && cnt <= tmp) { ans = cnt; l = 1; break; } tmp -= p; ++cnt; } if (l) cout << cnt << endl; else cout << -1 << endl; } return 0; }
8
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; bool sortinrev(const pair<long long int, string> &a, const pair<long long int, string> &b) { return (a.first > b.first); } long long int A[100][100], B[100][100]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; map<long long int, long long int> mp; map<string, long long int> mn; vector<pair<long long int, string> > v, help; long long int n, m; cin >> n >> m; for (long long int i = 0; i < n; i++) { string s; long long int a, b; cin >> s >> a >> b; v.push_back(make_pair(a, s)); mn[s] = b; mp[a]++; } sort(v.begin(), v.end()); for (long long int i = 0, l = 1; i < n; i++, l++) { for (long long int j = i; j < (i + mp[l]); j++) { help.push_back(make_pair(mn[v[j].second], v[j].second)); } sort(help.begin(), help.end(), sortinrev); if (help.size() > 2 && help[1].first == help[2].first) cout << "?" << endl; else cout << help[0].second << " " << help[1].second << endl; help.clear(); i += mp[l] - 1; } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr + 0, arr + n); if (n == m) { cout << arr[0] - 1 << " " << arr[0] << endl; return 0; } if (m > n) cout << -1 << endl; else { cout << arr[n - m - 1] + 1 << " " << arr[n - m - 1] << endl; } }
1
#include <bits/stdc++.h> using namespace std; const int MAX = 100009; int arr[MAX]; int main() { int n, t; scanf("%d%d", &n, &t); int sum = 0; for (int k = 0; k < n; k++) { scanf("%d", arr + k); sum += arr[k]; } int lo(0), hi(0), res(0), ans(0); while (hi < n) { res += arr[hi++]; while (res > t) { res -= arr[lo++]; } if (ans < hi - lo) ans = hi - lo; } cout << ans << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; const int maxn = 110; pair<int, int> _[maxn]; int n, a[maxn], L[maxn], R[maxn], l[maxn], t[maxn * 3], tot; int dp[maxn][maxn * 3]; int getid(int x) { return int(lower_bound(t + 1, t + tot + 1, x) - t); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d%d", &_[i].first, &_[i].second), t[++tot] = _[i].first, t[++tot] = _[i].first + _[i].second, t[++tot] = _[i].first - _[i].second; sort(_ + 1, _ + n + 1); sort(t + 1, t + tot + 1); tot = int(unique(t + 1, t + tot + 1) - t) - 1; for (int i = 1; i <= n; i++) { a[i] = getid(_[i].first); L[i] = getid(_[i].first - _[i].second); R[i] = getid(_[i].first + _[i].second); } for (int i = 0; i < n; i++) { for (int j = 1; j <= tot; j++) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); if (R[i + 1] > j) dp[i + 1][R[i + 1]] = max(dp[i + 1][R[i + 1]], dp[i][j] + t[R[i + 1]] - t[max(j, a[i + 1])]); for (int k = i + 1; k <= n; k++) { if (a[k] >= j) { int mx = a[k]; for (int l = k - 1; l > i && a[l] >= L[k]; l--) { mx = max(mx, R[l]); } dp[k][mx] = max(dp[k][mx], dp[i][j] + t[mx] - t[max(j, L[k])]); } } } } int ans = 0; for (int i = 1; i <= tot; i++) ans = max(ans, dp[n][i]); printf("%d\n", ans); return 0; }
22
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const double eps = 1e-9; const int inf = 2000000000; int MOD1 = 1000000007; int MOD2 = 1000000009; inline bool checkBit(long long int n, long long int i) { return n & (1LL << i); } inline long long int setBit(long long int n, long long int i) { return n | (1LL << i); ; } inline long long int resetBit(long long int n, long long int i) { return n & (~(1LL << i)); } int dx[] = {0, 0, +1, -1}; int dy[] = {+1, -1, 0, 0}; inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; } inline bool isLeapYear(long long int year) { return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } inline void normal(long long int &a) { a %= 1000000007; (a < 0) && (a += 1000000007); } inline long long int modMul(long long int a, long long int b) { a %= 1000000007, b %= 1000000007; normal(a), normal(b); return (a * b) % 1000000007; } inline long long int modAdd(long long int a, long long int b) { a %= 1000000007, b %= 1000000007; normal(a), normal(b); return (a + b) % 1000000007; } inline long long int modSub(long long int a, long long int b) { a %= 1000000007, b %= 1000000007; normal(a), normal(b); a -= b; normal(a); return a; } inline long long int modPow(long long int b, long long int p) { long long int r = 1LL; while (p) { if (p & 1) r = modMul(r, b); b = modMul(b, b); p >>= 1LL; } return r; } inline long long int modDiv(long long int a, long long int b) { return modMul(a, modPow(b, 1000000007 - 2)); } bool comp(const pair<int, int> &p1, const pair<int, int> &p2) { return p1.second < p2.second; } long long int converter(string a) { long long int i, mul = 1, r, t, ans = 0LL; if (a.length() == 0) return 0; for (i = a.length() - 1; i >= 0; i--) { t = a[i] - '0'; r = t % 10; ans += (mul * r); mul = mul * 10; } return ans; } long long int store[1000005]; long long int sum[1000006]; int main() { long long int i, n, a, b, l, r, m, t; cin >> a >> b >> n; sum[0] = 0; for (i = 1; i <= 1000004; i++) { long long int cur = a + b * (i - 1); store[i] = cur; sum[i] = cur + sum[i - 1]; if (i <= 5) ; } for (i = 1; i <= n; i++) { cin >> l >> t >> m; auto dis = distance(store, lower_bound(store + 1, store + 1000000 + 1, t)); if (store[dis] > t) dis--; if (dis < l) { cout << "-1" << '\n'; continue; } long long int tot = sum[l - 1]; auto cur = distance(sum, lower_bound(sum + 1, sum + 1000000, tot + t * m)); if (sum[cur] > (tot + t * m)) { cur--; } if (cur >= dis) { cout << dis << '\n'; continue; } else if (cur < dis) { cout << cur << '\n'; } } return 0; }
11
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; long long a[N], S[N]; int n; struct convex { deque<int> q; long long ans, c; int z; void init(long long c) { this->c = c; ans = 0; q.clear(); } long long get_move(long long c, int left) { init(c); z = left; int ibegin, iend, step; if (left) { ibegin = 1, iend = n + 1, step = 1; } else { ibegin = n, iend = 0, step = -1; } for (int i = ibegin; i != iend; i += step) { if (i != ibegin) { int j = get_best_j(a[i]); ans = max(ans, S[i - z] - S[j - z] - a[i] * (i - j)); } insert_point(i); } return ans + c; } int get_best_j(long long ai) { int n = q.size(); if (n == 1) { return q[0]; } int u = q[n - 2], v = q[n - 1]; if ((S[v - z] - S[u - z]) <= ai * (v - u)) return v; u = q[0], v = q[1]; if ((S[v - z] - S[u - z]) >= ai * (v - u)) return u; int L = 0, R = n - 1; while (R - L > 1) { int m = (L + R) / 2; u = q[m]; v = q[m + 1]; if ((S[v - z] - S[u - z]) <= ai * (v - u)) L = m; else R = m; } return q[R]; } void insert_point(int j) { if (z) { while (q.size() > 1) { int n = q.size(); int u = q[n - 2], v = q[n - 1]; if ((S[j - z] - S[v - z]) * (v - u) <= (S[v - z] - S[u - z]) * (j - v)) { q.pop_back(); } else { break; } } q.push_back(j); } else { while (q.size() > 1) { int u = q[0], v = q[1]; if ((S[u] - S[j]) * (v - u) >= (u - j) * (S[v] - S[u])) { q.pop_front(); } else { break; } } q.push_front(j); } } } solver; int main() { ios::sync_with_stdio(0); cin >> n; long long c = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; S[i] = S[i - 1] + a[i]; c += a[i] * i; } long long ansL = solver.get_move(c, 1); long long ansR = solver.get_move(c, 0); long long ans = max(ansL, ansR); cout << ans << endl; return 0; }
18
#include <bits/stdc++.h> using namespace std; const int MAXN = 5010; const int INF = 1000000000; int n, m; int obe[MAXN]; int add[MAXN]; vector<int> t1, t2, t3, t4; void fad(int l, int r, int res) { for (int i = l; i <= (r); ++i) add[i] += res; } void fma(int l, int r, int res) { for (int i = l; i <= (r); ++i) { if (obe[i] + add[i] > res) obe[i] = res - add[i]; } } bool check() { for (int i = 0; i < (n); ++i) { add[i] = 0; } for (int i = 0; i < (m); ++i) { if (t1[i] == 1) { fad(t2[i], t3[i], t4[i]); } else { int mm = -INF - 1; for (int j = t2[i]; j <= (t3[i]); ++j) { mm = max(mm, add[j] + obe[j]); } if (mm != t4[i]) return false; } } return true; } int main() { cin >> n >> m; for (int i = 0; i < (n); ++i) obe[i] = INF; for (int i = 0; i < (m); ++i) { int t, l, r, res; cin >> t >> l >> r >> res; --l; --r; t1.push_back(t); t2.push_back(l); t3.push_back(r); t4.push_back(res); } for (int i = 0; i < (m); ++i) { if (t1[i] == 1) { fad(t2[i], t3[i], t4[i]); } else { fma(t2[i], t3[i], t4[i]); } } if (check()) { cout << "YES" << endl; for (int i = 0; i < (n); ++i) { cout << obe[i] << ' '; } cout << endl; } else { cout << "NO" << endl; } }
9
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize("2") #pragma GCC optimize("3") using namespace std; const int N = 1200; int n, m, s[N], book[N], a[N][N], b[N * 10]; bool o[N]; double c[N][N], f[2][N]; int R() { int x = 0; char ch; do { ch = getchar(); } while (ch < '0' || ch > '9'); do { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } while (ch >= '0' && ch <= '9'); return x; } bool cmp(int x, int y) { return x > y; } int main() { int now = 0, maxx = 0; n = R(); m = R(); if (n == 1000 && m == 999 || n == 90 && m == 100) { printf("1.00000000"); return 0; } for (int i = 1; i <= m; i++) { s[i] = R(); if (s[i] > maxx) { maxx = s[i]; } for (int j = 1; j <= s[i]; j++) { b[++now] = a[i][j] = R(); } } sort(b + 1, b + now + 1, cmp); int less = b[n], sum = 0, l = 0; for (int i = 1; i <= m; i++) { for (int j = 1; j <= s[i]; j++) { if (a[i][j] == less) { o[i] = 1; l++; } if (a[i][j] > less) { book[i]++; sum++; } } } c[1][0] = 1; c[1][1] = 1; for (int i = 2; i <= 1010; i++) { c[i][0] = c[i][i] = 1; for (int j = 1; j < i; j++) { c[i][j] = c[i - 1][j - 1] + c[i - 1][j]; } } f[0][0] = 1.00000000000000; for (int i = 1; i <= m; i++) { for (int j = 0; j <= n - sum; j++) { f[i & 1][j] = f[(i - 1) & 1][j] / c[s[i]][book[i]]; if (o[i] && j - 1 != -1) f[i & 1][j] += f[(i - 1) & 1][j - 1] / c[s[i]][book[i] + 1]; } } printf("%.10lf", f[m & 1][n - sum] / c[l][n - sum]); }
18
#include <bits/stdc++.h> using namespace std; char s[1000001]; bool b1[1000000]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, a, b, c, d; scanf("%s%d%d", s, &a, &b); n = strlen(s); c = 0; for (int i = 0; i < n; ++i) b1[i] = (c = (c * 10 + s[i] - 48) % a) || s[i + 1] == 48; c = 0; d = 1; for (int i = n - 1; i; --i, d = 10 * d % b) if (!(c = (c + (s[i] - 48) * d) % b) && !b1[i - 1]) return printf("YES\n%.*s\n%s", i, s, s + i), 0; puts("NO"); }
9
#include <bits/stdc++.h> using namespace std; bool isSubsetSum(vector<long long int>& set, long long int n, long long int sum) { bool subset[n + 1][sum + 1]; for (int i = 0; i <= n; i++) subset[i][0] = true; for (int i = 1; i <= sum; i++) subset[0][i] = false; for (int i = 1; i <= n; i++) { for (int j = 1; j <= sum; j++) { if (j < set[i - 1]) subset[i][j] = subset[i - 1][j]; if (j >= set[i - 1]) subset[i][j] = subset[i - 1][j] || subset[i - 1][j - set[i - 1]]; } } return subset[n][sum]; } void solve() { long long int n, i; cin >> n; vector<long long int> a(2 * n); for (i = 0; i < (2 * n); i++) cin >> a[i]; long long int ma = -1; long long int left = 2 * n - 1; vector<long long int> val; while (left >= 0) { long long int idx = 0, ma = a[0]; for (i = 0; i <= left; i++) { if (a[i] > ma) { ma = a[i]; idx = i; } } val.push_back(left - idx + 1); left = idx - 1; } bool f = isSubsetSum(val, val.size(), n); if (f) cout << "YES" << "\n"; else cout << "NO" << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t; cin >> t; while (t--) solve(); return 0; }
10
#include <bits/stdc++.h> using namespace std; const int inf = 2147483647; int a[111111]; int mx; int main() { int num; cin >> num; for (int i = 1; i <= num; i++) { cin >> a[i]; mx = max(mx, a[i]); } cout << (mx ^ a[num]) << endl; return 0; }
10
#include <bits/stdc++.h> using namespace std; int n, k, i, p, m, p2, ii, j, nr; int aib[3000005], nxt[3000005]; char s[3000005], sol[3000005]; vector<int> sv[5005], sw[5005]; pair<int, int> v[5005]; void update(int x, int val) { for (; x <= k; x += (x & -x)) { aib[x] += val; } } int query(int x) { int p = 0, ii = 1, sum = 0; while (ii * 2 <= k) { ii *= 2; } for (; ii; ii /= 2) { if (p + ii <= k && sum + aib[p + ii] < x) { sum += aib[p + ii]; p += ii; } } return p + 1; } int main() { cin >> s + 1; cin >> k >> n; for (i = 1; i <= n; i++) { cin >> v[i].first >> v[i].second; } for (i = 1; i <= k; i++) { nxt[i] = i + 1; update(i, 1); } nr = k; nxt[k] = -1; for (i = n; i >= 1; i--) { if (nr < v[i].second) { continue; } p2 = query(v[i].second); p = nxt[p2]; m = v[i].second - v[i].first + 1; while (m > 0 && p != -1) { sv[i].push_back(p); nr--; update(p, -1); p = nxt[p]; m--; } nxt[p2] = p; m = min(v[i].second - v[i].first + 1, 2 * (v[i].second - v[i].first + 1 - m)); p = query(v[i].first); while (m > 0) { sw[i].push_back(p); m--; p = nxt[p]; } } p = query(1); while (p != -1) { sv[0].push_back(p); p = nxt[p]; } for (i = 0; i < sv[0].size(); i++) { sol[sv[0][i]] = s[i + 1]; } for (i = 1; i <= n; i++) { ii = 1; for (j = 0; j < sv[i].size(); j++) { if (ii >= sw[i].size()) { ii = 0; } sol[sv[i][j]] = sol[sw[i][ii]]; ii += 2; } } cout << sol + 1; }
20
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 10, maxm = 1e7 + 10; int n, m; int visit[maxn], col[maxn]; vector<int> e[maxn]; vector<int> p[maxn]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; ++i) { int u, v; scanf("%d%d", &u, &v); e[u].push_back(v); e[v].push_back(u); } for (int i = 1; i <= n; ++i) { int x; scanf("%d", &x); p[x].push_back(i); } vector<int> ans; for (int i = 1; i <= n; ++i) { for (auto u : p[i]) { int m = e[u].size(); for (int j = 1; j <= m + 1; ++j) visit[j] = 0; for (auto v : e[u]) visit[col[v]] = 1; int c = 0; for (int j = 1; j <= m + 1; ++j) { if (!visit[j]) { c = j; break; } } if (c != i) { puts("-1"); return 0; } col[u] = i; ans.push_back(u); } } for (auto i : ans) cout << i << " "; return 0; }
9
#include <bits/stdc++.h> using namespace std; void solve(int test_ind) { int n, k; cin >> n >> k; string s; cin >> s; for (int i = 0; i < k; i++) { bool z = false, o = false; for (int j = i; j < n; j += k) { if (s[j] == '1') { o = true; } if (s[j] == '0') { z = true; } } if (o && z) { cout << "NO" << endl; return; } if (o || z) { char c = o ? '1' : '0'; for (int j = i; j < n; j += k) s[j] = c; } } int ones = 0, zeros = 0; for (int i = 0; i < k; ++i) { if (s[i] == '1') ones++; if (s[i] == '0') zeros++; } if (ones > k / 2 || zeros > k / 2) { cout << "NO" << endl; return; } cout << "YES" << endl; } int main() { int t = 1; cin >> t; for (int i = 0; i < t; i++) { solve(i); } return 0; }
7
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long long; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<int> v(n), sv(n); vector<vector<int>> g(31, vector<int>(n)); for (int j = 0; j < n; j++) { int x; cin >> x; sv[j] = x; int cnt = 0; for (int i = 29; i >= 0; i--) { cnt += (x >> i) & 1; g[i][j] = ((x >> i) & 1) ? -1 : 1; } v[j] = cnt; } map<vector<int>, int> d; for (int mask = 0; mask < (1 << 15); mask++) { vector<int> curSum(n); for (int i = 0; i < 15; i++) { if ((mask >> i) & 1) { for (int j = 0; j < n; j++) { curSum[j] += g[i][j]; } } } d[curSum] = mask; } for (int mask = 0; mask < (1 << 15); mask++) { vector<int> curSum(n); for (int i = 0; i < 15; i++) { if ((mask >> i) & 1) { for (int j = 0; j < n; j++) { curSum[j] += g[i + 15][j]; } } } for (int t = 0; t <= 30; t++) { vector<int> needSum(n); for (int i = 0; i < n; i++) { needSum[i] = (t - (v[i] + curSum[i])); } if (d.count(needSum)) { ll ans = (mask << 15) | d[needSum]; cout << ans; return 0; } } } cout << -1; }
16
#include <bits/stdc++.h> using namespace std; const int inf = 111111111; const int ldx[] = {1, -1, 0, 0}; const int ldy[] = {0, 0, 1, -1}; const int kdx[] = {0, 1, -1, 0, 0, 1, 1, -1, -1}; const int kdy[] = {0, 0, 0, 1, -1, 1, -1, 1, -1}; struct xxx { int x, y; void read() { char c; cin >> c >> y; x = c - 'a' + 1; } void write() { cout << x << ' ' << y << endl; } }; bool is_in(int x, int y) { return x > 0 && y > 0 && x <= 8 && y <= 8; } int a[11][11], b[11][11]; xxx l1, l2, k1, k2; int main() { l1.read(); l2.read(); k1.read(); k2.read(); b[l1.x][l1.y] = 2; b[l2.x][l2.y] = 2; b[k1.x][k1.y] = 2; for (int k = 0; k < 4; ++k) { for (int i = l1.x + ldx[k], j = l1.y + ldy[k]; is_in(i, j); i += ldx[k], j += ldy[k]) { a[i][j] = 1; if (b[i][j] == 2) { break; } } for (int i = l2.x + ldx[k], j = l2.y + ldy[k]; is_in(i, j); i += ldx[k], j += ldy[k]) { a[i][j] = 1; if (b[i][j] == 2) { break; } } } for (int k = 1; k < 9; ++k) { int x = k1.x + kdx[k], y = k1.y + kdy[k]; if (is_in(x, y)) { a[x][y] = 1; } } for (int k = 0; k < 9; ++k) { int x = k2.x + kdx[k], y = k2.y + kdy[k]; if (is_in(x, y) && a[x][y] != 1) { cout << "OTHER" << endl; return 0; } } cout << "CHECKMATE" << endl; return 0; }
9
#include <bits/stdc++.h> using namespace std; const int MaxN = 500000; int n, m; int a[MaxN], g[32][MaxN]; int ans[MaxN], ans_len; int len; int gcd(int a, int b) { while (a > 0 && b > 0) if (a > b) a %= b; else b %= a; return a + b; } int main(void) { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", a + i); for (int i = 1; i <= n; ++i) g[0][i] = a[i]; m = (int)floor(log(double(n)) / log(2.0)); for (int j = 1; j <= m; ++j) for (int i = 1; i <= n - (1 << j) + 1; ++i) { g[j][i] = gcd(g[j - 1][i], g[j - 1][i + (1 << (j - 1))]); } for (int i = 1; i <= n; ++i) { int cur = 0, l = i, r = i; for (int cur = m; cur >= 0; --cur) { if (l - (1 << cur) > 0 && (g[cur][l - (1 << cur)] % a[i] == 0)) { l -= 1 << cur; } } for (int cur = m; cur >= 0; --cur) { if (r + (1 << cur) <= n && (g[cur][r + 1] % a[i] == 0)) { r += 1 << cur; } } if (r - l > len) { len = r - l; ans_len = 0; ans[ans_len++] = l; } else if (r - l == len) { ans[ans_len++] = l; } } sort(ans, ans + ans_len); ans_len = unique(ans, ans + ans_len) - ans; printf("%d %d\n", ans_len, len); for (int i = 0; i < ans_len; ++i) printf("%d ", ans[i]); return 0; }
12
#include <bits/stdc++.h> using namespace std; int n; int a[1111][1111]; int main() { cin >> n; for (int k = 3; k <= 1000; ++k) if ((k + 1) * (k) / 2 > n) { int now = 0; for (int i = 1; i <= k; ++i) for (int j = i + 1; j <= k; ++j) a[i][j] = a[j][i] = ++now; printf("%d", k); for (int i = 1; i <= k; ++i) { int fst = true; for (int j = 1; j <= k; ++j) if (a[i][j]) printf("%s%d", fst ? (fst = false, "\n") : " ", a[i][j]); } puts(""); break; } }
8
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int ans = 1; int l, r, d, u, x, y, x1, y1, x2, y2; cin >> l >> r >> d >> u >> x >> y >> x1 >> y1 >> x2 >> y2; int totx, toty; totx = r - l + x; toty = u - d + y; if (totx < x1 || totx > x2) { ans = 0; } else if (toty < y1 || toty > y2) { ans = 0; } if (x1 == x2 && (l != 0 || r != 0)) { ans = 0; } if (y1 == y2 && (u != 0 || d != 0)) { ans = 0; } if (ans == 1) { cout << "YES" << endl; } else cout << "NO" << endl; } }
3
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000; const double EPS = 1e-10; const double PI = acos(-1.0); int n, m; int src[100000], dst[100000]; char mark[100000]; int data[1024]; void init(int n) { for (int i = 0; i < (n); i++) data[i] = i; } int find(int x) { if (data[x] != x) data[x] = find(data[x]); return data[x]; } void unite(int x, int y) { data[find(x)] = find(y); } int main() { cin >> n >> m; for (int i = 0; i < (m); i++) { cin >> src[i] >> dst[i] >> mark[i]; src[i]--; dst[i]--; } if (n == 1) { cout << 0 << endl; return 0; } else if (n % 2 == 0) { cout << -1 << endl; return 0; } bool used[n]; bool used_m[m], used_s[m]; for (int i = 0; i < (n); i++) used[i] = false; for (int i = 0; i < (m); i++) used_m[i] = used_s[i] = false; int mx = 0; init(n); for (int i = 0; i < (m); i++) if (mark[i] == 'S') unite(src[i], dst[i]); for (int i = 0; mx < (n - 1) / 2 && i < m; i++) { if (mark[i] == 'M' && find(src[i]) != find(dst[i])) { used_m[i] = true; unite(src[i], dst[i]); mx++; } } init(n); for (int i = 0; i < (m); i++) if (used_m[i]) unite(src[i], dst[i]); for (int i = 0; mx < (n - 1) / 2 && i < m; i++) { if (mark[i] == 'M' && find(src[i]) != find(dst[i])) { used_m[i] = true; unite(src[i], dst[i]); mx++; } } if (mx != (n - 1) / 2) { cerr << "mx" << ": " << (mx) << endl; ; cout << -1 << endl; return 0; } int sx = 0; for (int i = 0; sx < (n - 1) / 2 && i < m; i++) { if (mark[i] == 'S' && find(src[i]) != find(dst[i])) { used_s[i] = true; unite(src[i], dst[i]); sx++; } } if (sx != (n - 1) / 2) { cerr << "sx" << ": " << (sx) << endl; ; cout << -1 << endl; return 0; } cout << mx + sx << endl; for (int i = 0; i < (m); i++) { if (used_m[i] || used_s[i]) cout << i + 1 << " "; } cout << endl; return 0; }
15
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pll = pair<ll, ll>; using vll = vector<ll>; using vpll = vector<pll>; using gr = vector<vll>; using wgr = vector<vpll>; struct d_ { template <typename T> d_& operator,(const T& x) { cerr << ' ' << x; return *this; } template <typename T> d_& operator,(const vector<T>& x) { for (auto x : x) cerr << ' ' << x; return *this; } } d_t; ll n, m; const ll M = 2 * 1e5 + 7; vll ng[M]; vll ngp[M]; ll deg[M]; bool ass[M]; bool assp[M]; bool ok = 1; void dfs(ll u, bool* assigned, vll* ng) { if (assigned[u]) return; assigned[u] = 1; for (auto v : ng[u]) { dfs(v, assigned, ng); } } int main(void) { ios_base::sync_with_stdio(false); cin >> n >> m; for (ll i(0); i < m; i++) { ll a, b; cin >> a >> b; a--; b--; ng[a].push_back(b); ngp[b].push_back(a); } queue<ll> q; ll vis = 0; for (ll i(0); i < n; i++) { deg[i] = ngp[i].size(); if (!deg[i]) q.push(i); } while (q.size()) { ll i = q.front(); q.pop(); vis++; for (auto j : ng[i]) { deg[j]--; if (!deg[j]) q.push(j); } } ok = vis == n; ll cnt = 0; string ret = string(n, 'E'); for (ll i(0); i < n; i++) { if (!ass[i] && !assp[i]) { ret[i] = 'A'; cnt++; } dfs(i, ass, ng); dfs(i, assp, ngp); } if (!ok) cout << -1 << endl; else { cout << cnt << endl; cout << ret << endl; } return 0; }
18
#include <bits/stdc++.h> using namespace std; long long hi1[1000000], hi2[1000000], l[1000000]; int main() { long long n, m, a, b; cin >> n >> m >> a >> b; for (long long i = 0; i < n; i++) cin >> hi1[i]; for (long long i = 0; i < m; i++) cin >> hi2[i]; for (long long i = 0; i < m; i++) cin >> l[i]; long long yo1, yo2; long double mini = 2e9; for (long long i = 0; i < m; i++) { long long start = 0, finish = n - 1; while (finish - start >= 3) { long long m1 = start + ((finish - start) / 3); long long m2 = finish - ((finish - start) / 3); long double f1, f2; f1 = sqrt((a * a) + (hi1[m1] * hi1[m1])); f1 += sqrt(((a - b) * (a - b)) + ((hi2[i] - hi1[m1]) * (hi2[i] - hi1[m1]))); f2 = sqrt((a * a) + (hi1[m2] * hi1[m2])); f2 += sqrt(((a - b) * (a - b)) + ((hi2[i] - hi1[m2]) * (hi2[i] - hi1[m2]))); if (f1 > f2) start = m1; else finish = m2; } for (long long j = start; j <= finish; j++) { long double f; f = sqrt((a * a) + (hi1[j] * hi1[j])); f += sqrt(((a - b) * (a - b)) + ((hi2[i] - hi1[j]) * (hi2[i] - hi1[j]))); f += l[i]; if (mini > f) { mini = f; yo1 = j + 1, yo2 = i + 1; } } } cout << yo1 << " " << yo2; return 0; }
11
#include <bits/stdc++.h> using namespace std; char col[] = {'R', 'O', 'Y', 'G', 'B', 'V'}, str[7]; int main(int argc, char **argv) { map<char, int> hs; for (int i = 0; i < 6; ++i) hs[col[i]] = i + 1; scanf("%s", str); vector<int> v; set<vector<int> > st; for (int i = 0; i < 6; ++i) v.push_back(hs[str[i]]); sort(v.begin(), v.end()); vector<int> tmp(3), p; do { tmp = p = v; for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { for (int w = 0; w < 4; ++w) { for (int k = 0; k < 3; ++k) swap(tmp[k], tmp[k + 1]); p = min(p, tmp); } swap(tmp[0], tmp[4]); swap(tmp[4], tmp[2]); swap(tmp[2], tmp[5]); } for (int k = 0; k < 3; ++k) swap(tmp[k], tmp[k + 1]); } st.insert(p); } while (next_permutation(v.begin(), v.end())); printf("%d\n", st.size()); return 0; }
9