func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 7; string to_string(string s) { return + s + ; } string to_string(char s) { return string(1, s); } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? true : false ); } template <typename A> string to_string(A); template <typename A, typename B> string to_string(pair<A, B> p) { return ( + to_string(p.first) + , + to_string(p.second) + ) ; } template <typename A> string to_string(A v) { bool f = 1; string r = { ; for (const auto &x : v) { if (!f) r += , ; f = 0; r += to_string(x); } return r + } ; } void debug_out() { cout << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cout << << to_string(H); debug_out(T...); } const int N = 1e5 + 10; int n, k; vector<pair<int, int>> g[N]; vector<pair<long long, long long>> A1[N]; pair<long long, long long> f(int u, int p) { auto &a1 = A1[u]; for (auto it : g[u]) if (it.first != p) { int v = it.first, c = it.second; auto now = f(v, u); a1.emplace_back(now.first + c, max(now.first, now.second) + c); } sort((a1).begin(), (a1).end()); reverse((a1).begin(), (a1).end()); pair<long long, long long> ans(0, 0); for (auto i = (0); i <= (min(int(a1.size()), k - 1) - 1); ++i) { ans.first += a1[i].first; ans.second += a1[i].first; } long long cur = ans.second; for (auto i = (0); i <= (int(a1.size()) - 1); ++i) { if (i <= k - 2) { ans.second = max(ans.second, cur - a1[i].first + a1[i].second + (k - 1 < int(a1.size()) ? a1[k - 1].first : 0)); } else { ans.second = max(ans.second, cur + a1[i].second); } } return ans; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k; for (auto i = (1); i <= (n - 1); ++i) { int x, y, c; cin >> x >> y >> c; g[x].emplace_back(y, c); g[y].emplace_back(x, c); } pair<int, int> ans = f(0, -1); cout << max(ans.first, ans.second) << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n % 2 != 0) cout << -1 << endl; else { for (int i = 1; i <= n; i++) { if (i % 2 == 0) cout << i - 1 << ; else cout << i + 1 << ; } } return 0; }
#include <bits/stdc++.h> using namespace std; int N; int arr[51][51]; bool checkSum(int num, int row, int col) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (arr[row][i] + (long long int)arr[j][col] == num) { return true; } } } return false; } int main() { scanf( %d , &N); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { scanf( %d , &arr[i][j]); } } bool flag = true; for (int i = 0; i < N && flag; i++) { for (int j = 0; j < N && flag; j++) { if (arr[i][j] != 1) { flag = checkSum(arr[i][j], i, j); } } } if (flag) { printf( Yes n ); } else { printf( No n ); } return 0; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; double e[100010], r[100010], p[100010]; int main() { int n; { scanf( %d , &n); for (int i = 0; i < n; i++) { scanf( %lf , &p[i]); } e[0] = p[0]; r[0] = p[0]; for (int i = 1; i < n; i++) { e[i] = e[i - 1] + p[i] + 2.0 * p[i] * r[i - 1]; r[i] = (r[i - 1] + 1.0) * p[i]; } printf( %.8lf n , e[n - 1]); } return 0; }
#include <bits/stdc++.h> using namespace std; bool cmp(pair<char, int> &a, pair<char, int> &b) { return a.second < b.second; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s[3]; int i; bool imp = false; unordered_map<char, int> m; vector<pair<char, int>> v; for (i = 0; i < 3; i++) { cin >> s[i]; } for (i = 0; i < 3; i++) { if (s[i][1] == > ) { m[s[i][0]]++; m[s[i][2]]--; } else { m[s[i][0]]--; m[s[i][2]]++; } } for (auto it = m.begin(); it != m.end(); it++) { v.push_back(make_pair(it->first, it->second)); } if (v[0].second == v[1].second || v[1].second == v[2].second || v[0].second == v[2].second) { imp = true; } if (imp) { cout << Impossible n ; } else { sort(v.begin(), v.end(), cmp); for (i = 0; i < v.size(); i++) { cout << v[i].first; } cout << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; void _dbg(string) { cout << endl; } template <class H, class... T> void _dbg(string s, H h, T... t) { int l = s.find( , ); cout << s.substr(0, l) << = << h << , ; _dbg(s.substr(l + 1), t...); } template <class T, class U> ostream &operator<<(ostream &o, const pair<T, U> &p) { o << ( << p.first << , << p.second << ) ; return o; } template <class T> ostream &operator<<(ostream &o, const vector<T> &v) { o << [ ; for (T t : v) { o << t << , ; } o << ] ; return o; } class UnionFind { public: vector<int> par, rank; UnionFind(int sz) : par(sz, -1), rank(sz, 0) {} int find(int x) { if (par[x] < 0) return x; else return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) { par[y] += par[x]; par[x] = y; } else { par[x] += par[y]; par[y] = x; if (rank[x] == rank[y]) rank[x]++; } } inline bool same(int x, int y) { return find(x) == find(y); } inline int size(int x) { return -par[find(x)]; } }; int main() { int n; cin >> n; vector<int> v(n); for (int i = (int)(0); i < (int)(n); i++) scanf( %d , &v[i]); for (int i = (int)(0); i < (int)(n); i++) v[i]--; UnionFind uf(n + 1); vector<bool> appeared(n + 1, false); appeared[n] = true; cout << 1 ; for (int i = (int)(0); i < (int)(n); i++) { if (v[i] > 0 && appeared[v[i] - 1]) uf.unite(v[i] - 1, v[i]); if (appeared[v[i] + 1]) uf.unite(v[i], v[i] + 1); appeared[v[i]] = true; int sz = uf.size(n) - 1; printf( %d%c , (i + 2) - sz, n [i == n - 1]); } return 0; }
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); int main() { double s, f, r, v; int n; scanf( %d%lf%lf , &n, &r, &v); while (n--) { scanf( %lf%lf , &s, &f); double ans = 0, d = f - s; int cnt = (int)floor(d / (2 * pi * r)); ans += cnt * 2 * pi * r / v; d -= cnt * (2 * pi * r); d /= 2; double L = 0, R = d; for (int _i = 0; _i < 60; _i++) { double mid = (L + R) / 2; if (mid + sin(mid / r) * r > d) R = mid; else L = mid; } ans += 2 * L / v; printf( %f n , ans); } return 0; }
#include <bits/stdc++.h> using namespace std; long long d1[] = {0, 2, 3, 1}; long long fn1(long long start, long long pos, long long len) { long long zzz = len / 4; if (len == 1) return start; for (long long i = 0; i < 4; i++) { long long l = i * zzz; long long r = l + (zzz - 1); if (l <= pos && pos <= r) return fn1(start + d1[i] * zzz, pos - l, zzz); } return -1; } long long d2[] = {0, 3, 1, 2}; long long fn2(long long start, long long pos, long long len) { long long zzz = len / 4; if (len == 1) return start; for (long long i = 0; i < 4; i++) { long long l = i * zzz; long long r = l + (zzz - 1); if (l <= pos && pos <= r) return fn2(start + d2[i] * zzz, pos - l, zzz); } return -1; } long long solve() { long long n; cin >> n; n--; long long row = n / 3; long long col = n % 3; if (col == 0) { long long pos = row; long long temp = 1; long long start = 1; while (true) { if (pos - temp >= 0) { pos -= temp; temp *= 4; start *= 4; continue; } break; } cout << start + pos << n ; } if (col == 1) { long long pos = row; long long temp = 1; long long start = 2; while (true) { if (pos - temp >= 0) { pos -= temp; temp *= 4; start *= 4; continue; } break; } cout << fn1(start, pos, temp) << n ; } if (col == 2) { long long pos = row; long long temp = 1; long long start = 3; while (true) { if (pos - temp >= 0) { pos -= temp; temp *= 4; start *= 4; continue; } break; } cout << fn2(start, pos, temp) << n ; } return 0; } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); ; long long t = 1; cin >> t; while (t-- != 0) { long long stat = solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; vector<long long> g[100009], df; long long n, v[100009]; long long used[100009], x, sum, plu[100009], minu[100009], br; void dfs(int node) { used[node] = 1; long long mxpl = 0, mxmi = 0; for (int i = 0; i < g[node].size(); i++) { if (used[g[node][i]] == 0) { dfs(g[node][i]); mxpl = max(mxpl, plu[g[node][i]]); mxmi = max(mxmi, minu[g[node][i]]); } } if (v[node] + mxpl > mxmi) { plu[node] = mxpl; minu[node] = mxpl + v[node]; } else { minu[node] = mxmi; plu[node] = minu[node] - v[node]; } } int main() { cin >> n; for (int i = 1; i < n; i++) { int a, b; cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } for (int i = 1; i <= n; i++) { cin >> v[i]; } dfs(1); for (int i = 1; i <= n; i++) { } sum = minu[1] + plu[1]; cout << sum << endl; }
#include <bits/stdc++.h> using namespace std; long long num[100500]; long long d1[15], d2[15]; int main() { long long n; long long ans = 0; scanf( %lld , &n); for (int i = 1; i <= n; i++) { scanf( %lld , &num[i]); } for (int i = 1; i <= n; i++) { long long digit = 0; long long a = num[i]; long long di = 1; while (a) { d1[++digit] = a % 10; a /= 10; } for (int k = 1; k <= digit; k++) { ans += d1[k] * di + d1[k] * di * 10; di *= 100; di = di % 998244353; ans = ans % 998244353; } } ans *= n; ans = ans % 998244353; printf( %lld , ans); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> T sqr(T x) { return x * x; } template <typename T> T abs(T x) { return x > 0 ? x : -x; } int cur = 0; string get_next() { string ans = ; if (cur >= 26) { ans = B ; } else { ans = A ; } ans += (char)( a + cur % 26); cur++; return ans; } int main() { int n, k; cin >> n >> k; vector<bool> used(n - k + 1); for (int i = 0; i < n - k + 1; i++) { string s; cin >> s; if (s == NO ) { used[i] = true; } } vector<string> ans(n); for (int i = 0; i < n; i++) { if (i >= k - 1 && used[i - k + 1]) { ans[i] = ans[i - k + 1]; } else { ans[i] = get_next(); } } for (int i = 0; i < n; i++) { cout << ans[i] << ; } cout << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int N, M; long long ans, add[400009], sum[400009]; set<pair<int, pair<int, int> > > segm; int mod(int x) { if (x < 0) return -x; return x; } void split(int nod, int l1, int l2) { if (add[nod] == 0) return; int f1 = nod << 1, f2 = (nod << 1) + 1; sum[f1] += 1LL * add[nod] * l1, sum[f2] += 1LL * add[nod] * l2; add[f1] += add[nod], add[f2] += add[nod]; add[nod] = 0; } void U(int nod, int st, int dr, int x, int y, int val) { if (x <= st && dr <= y) { add[nod] += val; sum[nod] += 1LL * val * (dr - st + 1); return; } int mij = (st + dr) >> 1; split(nod, mij - st + 1, dr - mij); if (x <= mij) U(nod << 1, st, mij, x, y, val); if (y > mij) U((nod << 1) + 1, mij + 1, dr, x, y, val); sum[nod] = sum[nod << 1] + sum[(nod << 1) + 1]; } void Q(int nod, int st, int dr, int x, int y) { if (x <= st && dr <= y) { ans += sum[nod]; return; } int mij = (st + dr) >> 1; split(nod, mij - st + 1, dr - mij); if (x <= mij) Q(nod << 1, st, mij, x, y); if (y > mij) Q((nod << 1) + 1, mij + 1, dr, x, y); } void Update(int L, int R, int val) { set<pair<int, pair<int, int> > >::iterator it = segm.lower_bound( make_pair(L, make_pair(0, 0))), it2; if (it->second.first < L) { int A = it->second.first, B = it->first, C = it->second.second; if (B > R) { U(1, 1, N, L, R, mod(val - C)); segm.erase(it); segm.insert(make_pair(L - 1, make_pair(A, C))); segm.insert(make_pair(B, make_pair(R + 1, C))); segm.insert(make_pair(R, make_pair(L, val))); return; } U(1, 1, N, L, B, mod(val - C)); segm.erase(it); segm.insert(make_pair(L - 1, make_pair(A, C))); it = segm.lower_bound(make_pair(L, make_pair(0, 0))); } while (1) { if (it == segm.end()) break; if (it->second.first > R) break; if (it->first > R) { int A = it->second.first, B = it->first, C = it->second.second; U(1, 1, N, A, R, mod(val - C)); segm.erase(it); segm.insert(make_pair(B, make_pair(R + 1, C))); break; } it2 = it, it2++; U(1, 1, N, it->second.first, it->first, mod(val - it->second.second)); segm.erase(it); it = it2; } segm.insert(make_pair(R, make_pair(L, val))); } int main() { scanf( %d %d , &N, &M); for (int i = 1; i <= N; i++) segm.insert(make_pair(i, make_pair(i, i))); while (M--) { int tip, a, b, c; scanf( %d , &tip); if (tip == 1) { scanf( %d %d %d , &a, &b, &c); Update(a, b, c); } else { scanf( %d %d , &a, &b), ans = 0LL; Q(1, 1, N, a, b); printf( %I64d n , ans); } } return 0; }
#include <bits/stdc++.h> using namespace std; set<int> q; const int maxn = 360; int main() { int t, n; scanf( %d , &t); while (t--) { scanf( %d , &n); int num = 360 / (180 - n); int ans = 0; if (num * (180 - n) != 360) num++; for (int i = num; i <= 360; i++) { if ((n * i) % 180 != 0) continue; ans = i; if (ans != 0) break; } if (ans == 0) printf( -1 n ); else printf( %d n , ans); } return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9; const double INF = 1e12, EPS = 1e-9; const int B = 2000; const int dy[] = {-1, 0, 1, 0}, dx[] = {0, -1, 0, 1}; int h, w, q; int qs[100000][4]; char in[100010][12], dir[256]; int dp[100010 * 12], vis[100010][12]; inline int to(int y, int x) { int &res = dp[y * 12 + x]; if (res != -1) return res; if (y == 0 || x == 0 || x == w + 1) return res = y * 12 + x; res = -2; int d = dir[in[y][x]]; return res = to(y + dy[d], x + dx[d]); } int main() { dir[ < ] = 1; dir[ ^ ] = 0; dir[ > ] = 3; scanf( %d%d%d , &h, &w, &q); for (int i = 0; i < (int)h; i++) scanf( %s , in[i + 1] + 1); for (int i = 0; i < (int)q; i++) { char c, d; int y, x; scanf( %c%d%d , &c, &y, &x); qs[i][0] = c == A ; qs[i][1] = y; qs[i][2] = x; if (c == C ) { scanf( %c , &d); qs[i][3] = d; } } memset(vis, -1, sizeof(vis)); for (int it = 0; it < (int)q; it++) { if (it % B == 0) { memset(dp, -1, sizeof(dp)); for (int i = it; i < it + B && i < q; i++) if (qs[i][0] == 0) { int y = qs[i][1], x = qs[i][2]; dp[y * 12 + x] = y * 12 + x; } } int y = qs[it][1], x = qs[it][2]; if (!qs[it][0]) { in[y][x] = (char)qs[it][3]; continue; } while (1) { if (x == 0 || x == w + 1 || y == 0) { printf( %d %d n , y, x); break; } if (vis[y][x] == it) { puts( -1 -1 ); break; } vis[y][x] = it; int ny = to(y, x), nx; if (ny < 0) { puts( -1 -1 ); break; } nx = ny % 12; ny /= 12; if (nx == 0 || nx == w + 1 || ny == 0) { printf( %d %d n , ny, nx); break; } int d = dir[in[ny][nx]]; y = ny + dy[d]; x = nx + dx[d]; } } return 0; }
#include <bits/stdc++.h> using namespace std; const long long MAXN = 350; const long long PHI = (long long)998244352; const long long INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3f; const long long MOD = (long long)998244353; const long long OVER_FLOW = 0x7fffffff; const long long LOVER_FLOW = 0x7fffffffffffffff; long long n; struct op { long long x1, y1, x2, y2; }; long long vis[MAXN][MAXN]; vector<op> res; string str; void add(long long x1, long long y1, long long x2, long long y2) { res.push_back((op){x1, y1, x2, y2}); vis[x2][y2]++; } void move(long long x1, long long y1, bool x) { if (x) { if (x1 != 1) { add(x1, y1, 1, y1); } else { add(x1, y1, 1, y1 == 1 ? 2 : 1); } } else { if (x1 != 2) { add(x1, y1, 2, y1); } else { add(x1, y1, 2, y1 == 1 ? 2 : 1); } } } int main() { ios::sync_with_stdio(false); long long m; cin >> n >> m; for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= m; j++) { cin >> str; for (long long k = str.size() - 1; k >= 0; k--) { move(i, j, str[k] == 1 ); } } } for (long long i = 1; i <= 2; i++) { for (long long j = 2; j <= m; j++) { while (vis[i][j]) { add(i, j, i, 1); vis[i][j]--; } } } long long cnt = res.size(); memset(vis, 0, sizeof vis); for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= m; j++) { cin >> str; for (auto k : str) { move(i, j, k == 1 ); } } } for (long long i = 1; i <= 2; i++) { for (long long j = 2; j <= m; j++) { while (vis[i][j]) { add(i, j, i, 1); vis[i][j]--; } } } cout << res.size() << n ; for (long long i = 0; i < cnt; i++) { cout << res[i].x1 << << res[i].y1 << << res[i].x2 << << res[i].y2 << n ; } for (long long i = res.size() - 1; i >= cnt; i--) { cout << res[i].x2 << << res[i].y2 << << res[i].x1 << << res[i].y1 << n ; } return 0; }
#include <bits/stdc++.h> long long k, L, R, a[2222222], b[2222222], ans, sum[2222222]; int n, r; long long min(long long a, long long b) { return a < b ? a : b; } long long max(long long a, long long b) { return a > b ? a : b; } bool check(long long x) { long long ans = 0, now = 0; memset(b, 0, sizeof(b)); for (int i = 1; i <= n; i++) { now += b[i]; if (a[i] + now < x) ans += x - a[i] - now, b[i + 2 * r + 1] -= x - a[i] - now, now += x - a[i] - now; if (ans > k) return 0; } return 1; } int main() { scanf( %d%d%lld , &n, &r, &k); L = 2e18; for (int i = 1; i <= n; i++) scanf( %lld , &sum[i]), L = min(L, sum[i]), sum[i] += sum[i - 1]; for (int i = 1; i <= n; i++) a[i] = sum[min(i + r, n)] - sum[max(0, i - r - 1)]; R = 1e20; while (L <= R) { long long mid = L + R >> 1; if (check(mid)) L = mid + 1, ans = mid; else R = mid - 1; } printf( %lld n , ans); }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<long long> a(n); vector<int> nx(n); for (int i = 0; i < n; i++) { cin >> a[i]; } map<long long, int> mp; long long sig = 0; for (int i = 0; i < 3 * n; i++) { int ri = (i % n); nx[mp[sig]] = ri; mp[sig] = ri; sig += a[ri]; } int res = n; for (int i = 0; i < n; i++) { int len = 1, p = nx[i]; if (p == -1) { continue; } nx[i] = -1; while (p != i) { len++; int np = nx[p]; nx[p] = -1; p = np; } res = min(n - len, res); } cout << res << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int n, t; cin >> n >> t; long long int A[n]; for (long long int i = 0; i < n; i++) { cin >> A[i]; } long long int cnt = 0; long long int sum = 0; long long int i = 0; long long int j = 0; for (i = 0; i < n; i++) { sum += A[i]; if (sum <= t) { cnt++; } else { sum -= A[j]; j++; } } cout << cnt << endl; }
#include <bits/stdc++.h> using namespace std; int nnext[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; long long gcd(long long x, long long y) { return !y ? x : gcd(y, x % y); } int main() { int n, m, k, idx, x, y, now, have, array[350][350]; cin >> n >> m >> k, x = y = 1, idx = now = have = 0; vector<vector<pair<int, int> > > ans(k); while (k) { if (k == 1) { if (y > m) x++, y = m, now = 1; else if (!y) x++, y = 1, now = 0; ans[idx].push_back(make_pair(x, y)), have++; if (have == n * m) break; if (!now) y++; else y--; continue; } else { ans[idx].push_back(make_pair(x, y)), have++; if (!now) y++; else y--; if (y > m) x++, y = m, now = 1; else if (!y) x++, y = 1, now = 0; } if (ans[idx].size() == 2) idx++, k--; } for (int i = 0; i < int(ans.size()); i++) { cout << ans[i].size() << ; for (int j = 0; j < int(ans[i].size()); j++) cout << ans[i][j].first << << ans[i][j].second << ; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long BINF = 9e18, LINF = 2e9, mod = 998244353, P = 179, Q = 1791791791; const long long MAXN = 1e4 + 7; long long a[MAXN], b[MAXN], c[MAXN]; vector<pair<long long, long long>> g[MAXN]; bool cmp(vector<pair<long long, long long>> &a, vector<pair<long long, long long>> &b) { return a.size() < b.size(); } struct way { long long a, b, c; }; vector<way> ways; long long h[MAXN]; long long c1[MAXN], c2[MAXN]; long long f[MAXN]; long long tin[MAXN], tout[MAXN]; long long par[MAXN]; long long timer = 0; void dfs(long long v, long long p = -1, long long pc = -1) { par[v] = p; h[v] = (p == -1 ? 0 : h[p] + 1); tin[v] = timer++; if (g[v].size() == 1 && p != -1) { long long u, c; for (pair<long long, long long> e : g[v]) if (e.first != p) u = e.first, c = e.second; if (pc != c) { cout << NO ; exit(0); } dfs(u, p, pc); return; } for (pair<long long, long long> e : g[v]) { if (e.first == p) continue; dfs(e.first, v, e.second); if (c1[v] == -1) c1[v] = f[e.first]; else if (c2[v] == -1) c2[v] = f[e.first]; f[v] = f[e.first]; } tout[v] = timer++; if (p != -1) ways.push_back(way{p, v, pc}); if (g[v].size() == 0) f[v] = c1[v] = c2[v] = v; } void dfsd(long long v, long long p = -1) { long long ps = -1; for (long long i = 0; i < g[v].size(); ++i) { if (g[v][i].first == p) ps = i; else dfsd(g[v][i].first, v); } if (ps != -1) g[v].erase(g[v].begin() + ps); } vector<way> answ; void add(long long a, long long b, long long c) { if (a == b) return; answ.push_back({a, b, c}); } long long lca(long long a, long long b) { if (h[a] < h[b]) swap(a, b); while (h[a] > h[b]) a = par[a]; while (a != b) a = par[a], b = par[b]; return a; } long long dist(long long a, long long b) { return h[a] + h[b] - 2 * h[lca(a, b)]; } void solve() { long long n; cin >> n; fill(c1, c1 + n, -1); fill(c2, c2 + n, -1); for (long long i = 0; i < n - 1; ++i) { cin >> a[i] >> b[i] >> c[i]; --a[i]; --b[i]; g[a[i]].push_back({b[i], c[i]}); g[b[i]].push_back({a[i], c[i]}); } long long root = min_element(g, g + n, cmp) - g; dfsd(root, -1); dfs(root); for (way w : ways) { if (h[w.a] > h[w.b]) swap(w.a, w.b); long long lul = c1[w.a]; if (dist(lul, w.b) != dist(lul, w.a) + dist(w.a, w.b)) lul = c2[w.a]; if (lul == -1) lul = root; add(c1[w.b], c2[w.b], -(w.c / 2)); add(root, lul, -(w.c / 2)); add(root, c1[w.b], w.c / 2); add(lul, c2[w.b], w.c / 2); } cout << YES n ; cout << answ.size() << n ; for (way w : answ) cout << w.a + 1 << << w.b + 1 << << w.c << n ; return; } signed main() { ios_base::sync_with_stdio(false); cout.precision(40); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 202000; int n, m; struct edge { int u, v, w; } edges[maxn]; int pre[maxn]; vector<edge> vec; int found(int x) { int rx = x; while (pre[rx] != rx) rx = pre[rx]; while (pre[x] != rx) { int tmp = pre[x]; pre[x] = rx; x = tmp; } return rx; } int cmp(edge alpha, edge beta) { return alpha.w < beta.w; } void read() { scanf( %d%d , &n, &m); for (int i = 1; i <= m; i++) scanf( %d%d%d , &edges[i].u, &edges[i].v, &edges[i].w); sort(edges + 1, edges + m + 1, cmp); } void work() { int ans = 0; for (int i = 1; i <= n; i++) pre[i] = i; for (int i = 1; i <= m;) { int j = i; vec.clear(); while (edges[i].w == edges[j].w) { if (found(edges[j].u) == found(edges[j].v)) { j++; continue; } vec.push_back(edges[j]); j++; } for (int k = 0; k < vec.size(); k++) { if (found(vec[k].u) == found(vec[k].v)) ans++; else pre[found(vec[k].u)] = found(vec[k].v); } i = j; } printf( %d n , ans); } int main() { read(); work(); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = (1 << 18) + 100; const int maxnode = maxn << 2; int n, ai[maxn + 5]; int ls[maxnode + 5], rs[maxnode + 5]; int re[maxnode + 5], sp[maxnode + 5]; long long sum[maxnode + 5]; void Build(int x, int l, int r) { re[x] = sp[x] = 0; ls[x] = x << 1, rs[x] = x << 1 | 1; if (l == r) { sum[x] = ai[l]; return; } int &lson = ls[x], &rson = rs[x], mid = (l + r) >> 1; Build(lson, l, mid); Build(rson, mid + 1, r); sum[x] = sum[lson] + sum[rson]; } void Replace(int d, int x, int l, int r, int p, int v) { if (l == r) { sum[x] = v; return; } int &lson = ls[x], &rson = rs[x], mid = (l + r) >> 1; if (re[x] >> d & 1) { re[x] ^= (1 << d), sp[x] ^= (1 << d), re[lson] ^= (1 << (d + 1)), re[rson] ^= (1 << (d + 1)); } if (sp[x] >> d & 1) { sp[x] ^= (1 << d), swap(lson, rson); } re[lson] ^= re[x], re[rson] ^= re[x], re[x] = 0; sp[lson] ^= sp[x], sp[rson] ^= sp[x], sp[x] = 0; if (p <= mid) Replace(d + 1, lson, l, mid, p, v); else Replace(d + 1, rson, mid + 1, r, p, v); sum[x] = sum[lson] + sum[rson]; } long long Sum(int d, int x, int l, int r, int ll, int rr) { if (ll <= l && r <= rr) return sum[x]; int &lson = ls[x], &rson = rs[x], mid = (l + r) >> 1; long long res = 0; if (re[x] >> d & 1) { re[x] ^= (1 << d), sp[x] ^= (1 << d), re[lson] ^= (1 << (d + 1)), re[rson] ^= (1 << (d + 1)); } if (sp[x] >> d & 1) { sp[x] ^= (1 << d), swap(lson, rson); } re[lson] ^= re[x], re[rson] ^= re[x], re[x] = 0; sp[lson] ^= sp[x], sp[rson] ^= sp[x], sp[x] = 0; if (ll <= mid) res += Sum(d + 1, lson, l, mid, ll, rr); if (mid < rr) res += Sum(d + 1, rson, mid + 1, r, ll, rr); return res; } int main() { int _; scanf( %d%d , &n, &_); int pn = n; n = (1 << n); for (int i = 1; i <= n; ++i) scanf( %d , ai + i); Build(1, 1, n); while (_--) { int o, x, k, l, r; scanf( %d , &o); if (o == 1) { scanf( %d%d , &x, &k); Replace(0, 1, 1, n, x, k); } else if (o == 2) { scanf( %d , &k); k = pn - k; re[1] ^= (1 << k); } else if (o == 3) { scanf( %d , &k); k = pn - 1 - k; sp[1] ^= (1 << k); } else { scanf( %d%d , &l, &r); long long ans = Sum(0, 1, 1, n, l, r); printf( %lld n , ans); } } return 0; }
#include <bits/stdc++.h> using namespace std; struct deal { long num_person; long num_shoue; vector<deal> *d; public: deal(long _p, long _s, vector<deal> *_d) { num_person = _p; num_shoue = _s; d = _d; } deal(long _p, long _s) { num_person = _p; num_shoue = _s; d = NULL; } }; long n, m; vector<vector<long long>> d1; map<long, vector<long long>> people; map<long, vector<long>> num_people; map<long, long long> shoes; map<long, long> num_shoes; long long max(long long a, long long b) { return a > b ? a : b; } int main() { scanf( %d , &n); shoes = map<long, long long>(); num_shoes = map<long, long>(); long size, money; for (long i = 0; i < n; i++) { scanf( %d%d , &money, &size); shoes[size] = money; num_shoes[size] = i; } scanf( %d , &m); people = map<long, vector<long long>>(); for (long i = 0; i < m; i++) { scanf( %d%d , &money, &size); if (!people.count(size)) { people[size] = vector<long long>(2, 0); num_people[size] = vector<long>(2, -1); } if (people[size][0] < money) { people[size][1] = people[size][0]; num_people[size][1] = num_people[size][0]; people[size][0] = money; num_people[size][0] = i; } else if (people[size][1] < money) { people[size][1] = money; num_people[size][1] = i; } } map<long, vector<long long>>::iterator i = people.begin(); long money2; size = -10; d1 = vector<vector<long long>>(m, vector<long long>(2, 0)); long k = 0; vector<deal> *p10 = new vector<deal>(); vector<deal> *p11 = new vector<deal>(); vector<deal> *pp10; vector<deal> *pp11; long size10 = 0; long size11 = 0; long psize10; long psize11; long prev_size; for (i = people.begin(); i != people.end(); i++) { psize10 = size10; psize11 = size11; pp10 = p10; pp11 = p11; p10 = new vector<deal>(); p10->reserve(3); p11 = new vector<deal>(); p11->reserve(3); prev_size = size; size = i->first; money = i->second[0]; money2 = i->second[1]; if (prev_size + 1 < size) { p10->push_back(deal(-1, -1, pp11)); p11->push_back(deal(-1, -1, pp11)); size10 = psize11; size11 = psize11; if (shoes[size] > 0 && shoes[size] <= money) { d1[k][0] = shoes[size]; p10->push_back(deal(num_people[size][0], num_shoes[size])); size10++; } else d1[k][0] = 0; d1[k][1] = 0; if (shoes[size] > 0 && shoes[size + 1] > 0) { long cost, cost2; bool p = 0; if (shoes[size] > shoes[size + 1]) { cost = shoes[size]; cost2 = shoes[size + 1]; p = 0; } else { cost = shoes[size + 1]; cost2 = shoes[size]; p = 1; } if (money >= cost) { d1[k][1] += cost; if (!p) { p11->push_back(deal(num_people[size][0], num_shoes[size])); size11++; } else { p11->push_back(deal(num_people[size][0], num_shoes[size + 1])); size11++; } if (money2 >= cost2) { d1[k][1] += cost2; if (!p) { p11->push_back(deal(num_people[size][1], num_shoes[size + 1])); size11++; } else { p11->push_back(deal(num_people[size][1], num_shoes[size])); size11++; } } } else { if (money >= cost2) { d1[k][1] += cost2; if (!p) { p11->push_back(deal(num_people[size][0], num_shoes[size + 1])); size11++; } else { p11->push_back(deal(num_people[size][0], num_shoes[size])); size11++; } } } } else { if (shoes[size] > 0 && shoes[size] <= money) { d1[k][1] = shoes[size]; p11->push_back(deal(num_people[size][0], num_shoes[size])); size11++; } if (shoes[size + 1] > 0 && shoes[size + 1] <= money) { d1[k][1] = shoes[size + 1]; p11->push_back(deal(num_people[size][0], num_shoes[size + 1])); size11++; } } if (k != 0) { d1[k][0] += d1[k - 1][1]; d1[k][1] += d1[k - 1][1]; } } else { long cost = 0; if (shoes[size] > 0 && shoes[size] <= money) cost = shoes[size]; d1[k][0] = max(d1[k - 1][0] + cost, d1[k - 1][1]); if (d1[k - 1][0] + cost > d1[k - 1][1]) { p10->push_back(deal(-1, -1, pp10)); if (cost) { p10->push_back(deal(num_people[size][0], num_shoes[size])); size10++; } } else { p10->push_back(deal(-1, -1, pp11)); } cost = 0; long cost2 = 0; if (shoes[size] > 0 && shoes[size + 1] > 0) { if (shoes[size] > shoes[size + 1]) { if (shoes[size] <= money) { cost = shoes[size]; if (shoes[size + 1] <= money2) cost2 = shoes[size + 1]; } else { if (shoes[size + 1] <= money) { cost2 = shoes[size + 1]; } } } else { if (shoes[size + 1] <= money) { cost2 = shoes[size + 1]; if (shoes[size] <= money2) cost = shoes[size]; } else { if (shoes[size] <= money) { cost = shoes[size]; } } } } else { if (shoes[size] > 0 && shoes[size] <= money) cost = shoes[size]; if (shoes[size + 1] > 0 && shoes[size + 1] <= money) cost2 = shoes[size + 1]; } long cost3 = 0; if (shoes[size + 1] > 0 && shoes[size + 1] <= money) cost3 = shoes[size + 1]; d1[k][1] = 0; d1[k][1] = max(d1[k - 1][0] + cost + cost2, d1[k - 1][1] + cost3); if (d1[k - 1][0] + cost + cost2 > d1[k - 1][1] + cost3) { p11->push_back(deal(-1, -1, pp10)); if (cost != 0 && cost2 != 0) { if (cost > cost2) { p11->push_back(deal(num_people[size][0], num_shoes[size])); p11->push_back(deal(num_people[size][1], num_shoes[size + 1])); size11++; size11++; } else { p11->push_back(deal(num_people[size][0], num_shoes[size + 1])); p11->push_back(deal(num_people[size][1], num_shoes[size])); size11++; size11++; } } else { if (cost) { p11->push_back(deal(num_people[size][0], num_shoes[size])); size11++; } if (cost2) { p11->push_back(deal(num_people[size][0], num_shoes[size + 1])); size11++; } } } else { p11->push_back(deal(-1, -1, pp11)); if (cost3) { p11->push_back(deal(num_people[size][0], num_shoes[size + 1])); size11++; } } } k++; } printf( %I64d n , d1[k - 1][1]); printf( %d n , size11); vector<deal> *p; p = p11; while ((*p).size() != 0) { for (int j = 1; j < (*p).size(); j++) { if ((*p)[j].num_person + 1 < 0) break; printf( %d %d n , (*p)[j].num_person + 1, (*p)[j].num_shoue + 1); } p = (*p)[0].d; } return 0; }
#include <bits/stdc++.h> using namespace std; inline int ckmax(int &a, int b) { return a < b ? a = b, 1 : 0; } inline int ckmin(int &a, int b) { return a > b ? a = b, 1 : 0; } struct matrix { double x[128][128]; double *operator[](int a) { return x[a]; } void clear() { memset(x, 0, sizeof(x)); } void reset() { clear(); for (int i = 0; i < 128; ++i) x[i][i] = 1; } } p, a, ans; matrix operator*(matrix &a, matrix &b) { p.clear(); for (int i = 0; i < 128; i++) for (int j = 0; j < 128; j++) for (int k = 0; k < 128; k++) p[i][j] += a[i][k] * b[k][j]; return p; } int n, x; double pos[128]; int main() { scanf( %d%d , &n, &x); for (int i = 0; i <= x; ++i) scanf( %lf , &pos[i]); a.clear(); for (int i = 0; i < 128; ++i) for (int j = 0; j < 128; ++j) a[i][j] = pos[i ^ j]; ans.reset(); for (; n; n >>= 1, a = a * a) if (n & 1) ans = ans * a; printf( %.10f n , 1.0 - double(ans[0][0])); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); ; int n, value; char str[200]; cin >> n; vector<int> v; for (int i = 0; i < n; i++) { cin >> str; if (strcmp(str, add ) == 0) { cin >> value; v.insert(lower_bound(v.begin(), v.end(), value), value); } else if (strcmp(str, del ) == 0) { cin >> value; v.erase(lower_bound(v.begin(), v.end(), value)); } else if (strcmp(str, sum ) == 0) { long long sum = 0; for (int j = 2; j < v.size(); j += 5) { sum += v[j]; } cout << sum << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> inline bool read(T &x) { int c = getchar(); int sgn = 1; while ((~c && c < 0 ) || c > 9 ) { if (c == - ) sgn = -1; c = getchar(); } for (x = 0; ~c && 0 <= c && c <= 9 ; c = getchar()) x = x * 10 + c - 0 ; x *= sgn; return ~c; } int X[] = {-1, 0, 1, 0}; int Y[] = {0, 1, 0, -1}; int main() { int n; read(n); int win = 2; for (int i = 1; i <= n; i++) { int a; read(a); if (a % 2 == 0) { win = (win == 2) ? 1 : 2; } printf( %d n , win); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 200010; set<pair<long long, long long> > st; int resp[maxn]; struct island { long long ini, fim; } isl[maxn]; struct event { long long type, val, ini; event() { type = 0, val = 0, ini = 0; } event(long long a, long long b, long long c) { type = a; val = b; ini = c; } } e[2 * maxn]; bool cmp(event a, event b) { if (a.ini == b.ini && a.type == b.type) return a.val < b.val; if (a.ini == b.ini) return a.type < b.type; return a.ini < b.ini; } int main() { int n, m; scanf( %d %d , &n, &m); long long al, bl; int siz = 0; for (int i = 0; i < n; i++) { long long a, b; scanf( %I64d %I64d , &a, &b); if (i != 0) { isl[i - 1].fim = b - al; isl[i - 1].ini = a - bl; int atual = i - 1; e[siz++] = event(1, atual, isl[i - 1].ini); } al = a; bl = b; } for (int i = 0; i < m; i++) { long long q; scanf( %I64d , &q); e[siz++] = event(2, i, q); } sort(e, e + siz, cmp); for (int i = 0; i < siz; i++) { if (e[i].type == 1) { st.insert(make_pair(isl[e[i].val].fim, e[i].val)); } else { set<pair<long long, long long> >::iterator it = st.lower_bound(make_pair(e[i].ini, -1LL)); if (it != st.end()) { resp[it->second] = e[i].val; st.erase(it); } } } int ok = 1; if (!st.empty()) ok = 0; if (ok) { puts( Yes ); for (int i = 0; i < n - 1; i++) { printf( %d , resp[i] + 1); } printf( n ); } else { puts( No ); } }
#include <bits/stdc++.h> using namespace std; int main() { int n, a[10000], i, sum = 0; scanf( %d , &n); for (i = 0; i < n; i++) { scanf( %d , &a[i]); } int maxv = a[0]; int minv = a[0]; for (i = 1; i < n; ++i) { if (maxv < a[i]) { maxv = a[i]; sum++; } else if (minv > a[i]) { minv = a[i]; sum++; } } printf( %d n , sum); }
#include <bits/stdc++.h> using namespace std; int n, m; int s[505][505]; int main() { scanf( %d%d , &n, &m); char c = getchar(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { c = getchar(); if (c == S ) s[i][j] = 0; else if (c == W ) s[i][j] = 1; else s[i][j] = 2; } getchar(); } bool f = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (s[i][j] == 1) { if (i > 1 && s[i - 1][j] == 0) f = 1; if (j > 1 && s[i][j - 1] == 0) f = 1; if (i < n && s[i + 1][j] == 0) f = 1; if (j < m && s[i][j + 1] == 0) f = 1; } } } if (f) { printf( No n ); return 0; } printf( Yes n ); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (s[i][j] == 0) { printf( S ); } else if (s[i][j] == 1) printf( W ); else printf( D ); } printf( n ); } return 0; }
#include <bits/stdc++.h> int main() { int n, i, j, c = 0, p, q, f = 0; scanf( %d , &n); scanf( %d , &p); int a[p]; for (i = 0; i < p; i++) scanf( %d , &a[i]); scanf( %d , &q); int b[q]; for (i = 0; i < q; i++) scanf( %d , &b[i]); for (i = 1; i <= n; i++) { f = 0; for (j = 0; j < p; j++) { if (a[j] == i) { c++; f++; } } if (f == 0) { for (j = 0; j < q; j++) { if (b[j] == i) { c++; } } } } if (n == c) printf( I become the guy. n ); else printf( Oh, my keyboard! n ); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 1000018; char str[2][510000], text[1010000]; int n, ans[1010000], num_node, fail[1010000], nxt[1010000], len[1010000], to[1010000][26], path[1010000], pa[1010000]; int dl[1010000], last, save[1010000], ie[1010000]; void init() { num_node = 2; fail[0] = 1; len[1] = -1; len[0] = 0; nxt[0] = 1; last = 0; } int get_fail(int id, int pos) { while (pos - len[id] - 1 < 1 || text[pos - len[id] - 1] != text[pos]) id = fail[id]; return id; } void add_letter(int pos) { int cur = get_fail(last, pos); if (!to[cur][text[pos] - a ]) { len[num_node] = len[cur] + 2; fail[num_node] = to[get_fail(fail[cur], pos)][text[pos] - a ]; dl[num_node] = len[num_node] - len[fail[num_node]]; if (dl[num_node] == dl[fail[num_node]]) nxt[num_node] = nxt[fail[num_node]]; else nxt[num_node] = fail[num_node]; to[cur][text[pos] - a ] = num_node++; } last = to[cur][text[pos] - a ]; } int main() { scanf( %s%s , str[0], str[1]); n = strlen(str[0]); int i, j, s, p, q; for (i = 0; i < n; i++) { text[2 * i] = str[0][i]; text[2 * i + 1] = str[1][i]; } n *= 2; for (i = n - 1; i >= 0; i--) text[i + 1] = text[i]; init(); for (i = 1; i <= n; i++) { add_letter(i); ans[i] = inf; for (j = last; len[j] > 0; j = nxt[j]) { save[j] = ans[i - len[nxt[j]] - dl[j]]; path[j] = i - len[nxt[j]] - dl[j]; if (dl[j] == dl[fail[j]]) { if (save[j] > save[fail[j]]) { save[j] = save[fail[j]]; path[j] = path[fail[j]]; } } if (!(i & 1)) { if (ans[i] > save[j] + 1) { ans[i] = save[j] + 1; pa[i] = path[j]; } } } if (!(i & 1) && text[i] == text[i - 1]) { if (ans[i] > ans[i - 2]) { ans[i] = ans[i - 2]; pa[i] = i - 2; } } } if (ans[n] == inf) puts( -1 ); else { printf( %d n , ans[n]); int id = n; while (id > 0) { if (pa[id] + 2 != id) printf( %d %d n , pa[id] / 2 + 1, id / 2); id = pa[id]; } } return 0; }
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-9; const double INF = 1e6; const double PI = atan2(0, -1); const int maxn = 550; inline int sgn(double x) { if (fabs(x) < EPS) return 0; return x < 0 ? -1 : 1; } inline int iseq(double x, double y) { return sgn(x - y) == 0; } struct Point { double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} double Angle() { return atan2(y, x); } }; inline Point operator+(Point A, Point B) { return Point(A.x + B.x, A.y + B.y); } inline Point operator-(Point A, Point B) { return Point(A.x - B.x, A.y - B.y); } inline Point operator*(double x, Point A) { return Point(x * A.x, x * A.y); } inline double operator*(Point A, Point B) { return A.x * B.x + A.y * B.y; } inline double operator%(Point A, Point B) { return A.x * B.y - A.y * B.x; } inline double Length2(Point A) { return A * A; } inline double Length(Point A) { return sqrt(A * A); } inline double Distance2(Point A, Point B) { return Length2(A - B); } inline double Distance(Point A, Point B) { return Length(A - B); } inline bool operator==(Point A, Point B) { return iseq(Distance(A, B), 0); } inline bool operator<(Point A, Point B) { return iseq(A.x, B.x) ? sgn(A.y - B.y) == -1 : sgn(A.x - B.x) == -1; } struct Line { Point P, Q; Line() {} Line(Point A, Point B) { P = A; Q = B; } Point Direction() { return Q - P; } }; inline int IsOnLine(Point P, Line l) { return sgn((l.P - P) % (l.Q - P)) == 0; } inline int IsOnSegment(Point P, Line l) { if (P == l.P || P == l.Q) return 1; return sgn((l.P - P) % (l.Q - P)) == 0 && sgn((l.P - P) * (l.Q - P)) <= 0; } inline bool IsSegmentOverlap(Line A, Line B) { if (iseq(A.Direction() % B.Direction(), 0)) return IsOnSegment(A.P, B) + IsOnSegment(A.Q, B) + IsOnSegment(B.P, A) + IsOnSegment(B.Q, A) >= 2; else return false; } inline bool IsSegmentIntersected(Line A, Line B) { if (IsOnSegment(A.P, B) || IsOnSegment(A.Q, B)) return true; if (IsOnSegment(B.P, A) || IsOnSegment(B.Q, A)) return true; int sgn1 = sgn((A.P - B.P) % B.Direction()), sgn2 = sgn((A.Q - B.P) % B.Direction()); int sgn3 = sgn((B.P - A.P) % A.Direction()), sgn4 = sgn((B.Q - A.P) % A.Direction()); return sgn1 * sgn2 == -1 && sgn3 * sgn4 == -1; } inline Point getLineIntersection(Line A, Line B) { Point v = A.Direction(), w = B.Direction(); Point u = B.P - A.P; return A.P + ((w % u) / (w % v)) * v; } int n; vector<Point> Rect[maxn]; double area1, area2; inline double getPolygonArea(const vector<Point> &A) { double ans = 0; for (auto j = 1u; j + 1 < A.size(); ++j) ans += (A[j] - A[0]) % (A[j] - A[0]) * 0.5; return ans; } inline bool IsInPolygon(Point P, const vector<Point> &A) { double area1 = getPolygonArea(A), area2 = 0.0; for (auto j = 0u; j + 1 < A.size(); ++j) area2 += (A[j] - P) % (A[j + 1] - P) * 0.5; return iseq(area1, area2); } inline bool IsPolygonOverlap(vector<Point> P[], int i, int j) { int sgn = 1; for (auto k = 0u; k < P[i].size(); ++k) if (!IsInPolygon(P[i][k], P[j])) { sgn = 0; break; } if (!sgn) return false; if (iseq(getPolygonArea(P[i]), getPolygonArea(P[j]))) return i < j; return true; } struct info { Point P; double ang; int delta; info() {} info(Point x, double y, int z) { P = x; ang = y; delta = z; } }; inline int operator<(const info &A, const info &B) { if (iseq(A.ang, B.ang)) return A.delta > B.delta; else return A.ang < B.ang; } inline double getPosition(Point P, Line l) { return ((P - l.P) * l.Direction()) / (l.Direction() * l.Direction()); return (P.x - l.P.x) / (l.Q.x - l.P.x); } double ans[maxn]; double getAreaUnion(vector<Point> P[], int n, double *ans) { for (int i = 1; i <= n; ++i) ans[i] = 0.0; static vector<pair<double, int>> PointList; double aans = 0; for (int i = 1; i <= n; ++i) for (auto j = 0u; j + 1 < P[i].size(); ++j) { Line A(P[i][j], P[i][j + 1]); PointList.clear(); for (int k = 1; k <= n; ++k) if (i != k) for (auto w = 0u; w + 1 < P[k].size(); ++w) { Line B(P[k][w], P[k][w + 1]); int p1 = sgn((B.P - A.P) % A.Direction()); int p2 = sgn((B.Q - A.P) % A.Direction()); if (!p1 && !p2) { if (i < k && sgn(A.Direction() * B.Direction()) == 1) { PointList.push_back( make_pair(min(1.0, max(getPosition(B.P, A), 0.0)), -1)); PointList.push_back( make_pair(min(1.0, max(getPosition(B.Q, A), 0.0)), 1)); } } else { Point tp = getLineIntersection(A, B); if (p1 >= 0 && p2 < 0) PointList.push_back( make_pair(min(1.0, max(getPosition(tp, A), 0.0)), -1)); if (p1 < 0 && p2 >= 0) PointList.push_back( make_pair(min(1.0, max(getPosition(tp, A), 0.0)), 1)); } } PointList.push_back(make_pair(0.0, 1)); PointList.push_back(make_pair(1.0, -1)); sort(PointList.begin(), PointList.end()); double S0 = A.P % A.Q * 0.5; double lst = 0; int now = 0; for (auto l = 0u, r = 0u; l < PointList.size();) { ans[now] += S0 * (PointList[l].first - lst); lst = PointList[l].first; for (r = l + 1; r < PointList.size() && iseq(PointList[r].first, lst); ++r) ; for (auto w = l; w < r; ++w) now += PointList[w].second; l = r; } } return ans[1]; } int main() { scanf( %d , &n); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= 4; ++j) { Point P; scanf( %lf%lf , &P.x, &P.y); Rect[i].push_back(P); } if (sgn((Rect[i][1] - Rect[i][0]) % (Rect[i][2] - Rect[i][0])) == -1) reverse(Rect[i].begin(), Rect[i].end()); area1 += (Rect[i][1] - Rect[i][0]) % (Rect[i][3] - Rect[i][0]); Rect[i].push_back(Rect[i][0]); } area2 = getAreaUnion(Rect, n, ans); printf( %.10lf n , area1 / area2); return 0; }
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; vector<long long> arr(n); for (long long i = 0; i < n; i++) { cin >> arr[i]; } sort(arr.begin(), arr.end()); if (arr[n - 1] == 1) { arr[n - 1] = 2; } else { arr[n - 1] = 1; } sort(arr.begin(), arr.end()); for (long long i = 0; i < n; i++) { cout << arr[i] << ; } }
#include <bits/stdc++.h> using namespace std; long long int power(long long int x, long long int y) { if (y == 0) return 1; if (y & 1) return (x * power(x, y - 1)) % 1000000007; long long int t = power(x, y / 2); return (t * t) % 1000000007; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s; cin >> s; set<int> arr[26]; int n = (int)s.size(); for (int i = 0; i < n; i++) arr[s[i] - a ].insert(i); string mk = Mike n ; string ann = Ann n ; bool found = false; for (int i = 0; i < n; i++) { found = false; for (int j = 0; j < s[i] - a ; j++) { auto itr = arr[j].lower_bound(0); if (itr != arr[j].end() && *itr < i) { found = true; break; } } if (true == found) cout << ann; else cout << mk; } return 0; }
#include <bits/stdc++.h> const int N = 1000010; const int inf = 0x3f3f3f3f; using namespace std; int n, m; int c[N]; int main() { scanf( %d%d , &n, &m); for (int i = 0; i < m; i++) { int a, b; scanf( %d%d , &a, &b); c[a]++; c[b]++; } if (n < 3) return puts( 0 ), 0; long long ret = 0; for (int i = 1; i <= n; i++) ret += 1ll * c[i] * (n - 1 - c[i]); ret /= 2; cout << (1ll * n * (n - 1) * (n - 2) / 6 - ret) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long int; using dd = double; using pii = pair<int, int>; using pll = pair<ll, ll>; ll INFLL = (ll)2e18, MOD = 1e9 + 7; const int INF = 0x6f6f6f6f; vector<vector<ll>> adj; vector<int> vis; int dx8[] = {0, 1, 1, 1, 0, -1, -1, -1}, dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx4[] = {0, 1, 0, -1}, dy4[] = {1, 0, -1, 0}; inline ll mexp(ll x, ll n, ll m) { ll res = 1; while (n) { if (n & 1) res = (res * x) % m; n >>= 1; x = ((x % m) * (x % m)) % m; } return res; } inline bool ispow2(ll x) { return x && (!(x & (x - 1))); } inline ll gcd(ll x, ll y) { pll p{x, y}; while (p.second) p = {p.second, p.first % p.second}; return p.first; } int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed; cerr << fixed; cout << setprecision(10); cerr << setprecision(3); mt19937 genr(chrono::high_resolution_clock::now().time_since_epoch().count()); int n; cin >> n; vector<int> seq(n + 1); map<int, int> m; for (int i = 0; i < n; ++i) { int t; cin >> t; m[t] = i + 1; } int ct = 1; for (auto& e : m) seq[e.second] = ct++; m.clear(); vector<ll> tree(n + 1, 0); auto sum = [&](int pos) { ll ret = 0; while (pos) { ret += tree[pos]; pos -= pos & -pos; } return ret; }; auto upd = [&](int pos, ll val) { while (pos <= n) { tree[pos] += val; pos += pos & -pos; } }; ll ans = 0; for (int i = 1; i <= n; ++i) { ll gr = sum(n) - sum(seq[i]); ll le = seq[i] - 1 - sum(seq[i] - 1); ans += gr * le; upd(seq[i], 1); } cout << ans << n ; return 0; }
#include <bits/stdc++.h> using namespace std; vector<pair<long long, int> > H[1000007]; long long n, val[1000005], ans, cnt[1000005]; int S[1000005]; int m, up[1000005], p[1000005], len, b[1000005], mp[1000005]; void prepare() { int i, j; b[1] = 1; for (i = 2; i < 1000005; i++) { if (!b[i]) p[++len] = i; for (j = 1; j <= len; j++) { if (1LL * i * p[j] >= 1000005) break; b[1LL * i * p[j]] = 1; if (i % p[j] == 0) break; } } for (i = 1; i < 1000005; i++) S[i] = S[i - 1] + (!b[i]); for (i = 1; i <= len; i++) mp[p[i]] = i; for (i = 1000005 - 1; i >= 1; i--) mp[i] = mp[i] ? mp[i] : mp[i + 1]; } int SQRT(long long n) { int p = (int)(sqrt(n)); while (1LL * p * p <= n) p++; return p; } long long find(long long x, int t) { int i, u = x % 1000007, p; for (i = 0; i < H[u].size(); i++) if (H[u][i].first == x) break; p = H[u][i].second; return t > up[p] ? cnt[p] + up[p] - t : cnt[p]; } long long Pi(int t) { return cnt[t] + up[t] - 1; } void update(int t) { long long i, x = val[t], l = n / (x + 1) + 1, r = n / x; r = min(r, (long long)(sqrt(n))); for (i = l; i <= r; i++) if (!b[i]) ans += Pi(t) - S[i]; } int main() { long long i, j = 1, q = 1; long long x = 0; cin >> n; prepare(), m = 1; for (i = 1; 1LL * p[i] * p[i] * p[i] <= n; i++) ans++; for (i = 1; i <= n; i = n / (val[m++]) + 1) val[m] = n / i; m--, sort(val + 1, val + m + 1); for (i = 1; i <= m; i++) H[val[i] % 1000007].push_back(pair<long long, int>(val[i], i)); for (i = 1; i <= m; i++) up[i] = mp[SQRT(val[i])]; for (i = 1; i <= up[m]; i++) { for (j = m; j >= q; j--) { if (i != 1) { x = find(val[j] / p[i - 1], i - 1); cnt[j] = cnt[j] - x - 1; } else cnt[j] = val[j] - 1; } while (q <= m && i == up[q]) update(q++); } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); long long n, i, j, k, l, m, arr[100010], arr1[100010], x, y; double a, b, c, d, p, t; cin >> n; for (i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); reverse(arr, arr + n); arr1[0] = arr[0]; for (i = 1; i < n; i++) arr1[i] = arr1[i - 1] + arr[i]; m = 9999999999; for (i = 0; i < n - 1; i++) m = min(abs(abs(arr1[n - 1] - arr1[i]) - arr1[i]) + 1, m); cout << m; }
#include <bits/stdc++.h> using namespace std; int in() { int n; scanf( %d , &n); return n; } long long Lin() { long long n; scanf( %lld , &n); return n; } double Din() { double n; scanf( %lf , &n); return n; } const long long inf = (long long)1e17; const long long mod = (long long)1e9 + 7; const int N = 5e5 + 5; struct trie { trie* node[2]; int cnt; trie() { node[0] = node[1] = NULL; cnt = 0; } }; void add(trie* root, long long x, bool ck) { for (int i = 1; i <= 18; i++) { int p = (x % 2 == 1); x /= 10; if (root->node[p] == NULL) root->node[p] = new trie; root = root->node[p]; } ck ? root->cnt++ : root->cnt--; } int query(trie* root, string s) { reverse(s.begin(), s.end()); while (s.size() < 18) s.push_back( 0 ); for (int i = 0; i < 18; i++) { int p = s[i] - 0 ; if (root->node[p] == NULL) return 0; root = root->node[p]; } return root->cnt; } int solve() { int q = in(); trie* root = new trie; while (q--) { string ck; long long x; cin >> ck; if (ck == + ) { x = Lin(); add(root, x, true); } else if (ck == - ) { x = Lin(); add(root, x, false); } else { string s; cin >> s; printf( %d n , query(root, s)); } } return 0; } int main() { int test = 1, tc = 0; while (test--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main(void) { int n, k; cin >> n >> k; int r[257] = {}; for (int i = 0; i < 257; i++) { r[i] = -1; } int p; for (int i = 0; i < n; i++) { cin >> p; if (r[p] == -1) { for (int j = (p - k + 1 >= 0) ? p - k + 1 : 0; j <= p; j++) { if (r[j] == -1) { for (int x = j; x <= p; x++) { if (r[x] != -1) break; r[x] = j; } break; } else if (r[j + 1] == -1 && r[j] + k - 1 >= p) { for (int x = j + 1; x <= p; x++) { r[x] = r[j]; } break; } } } cout << r[p] << ; } return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; int i = n / 2 - 1, j = n / 2; sort(arr, arr + n); int x = 0; int ans[n]; if (n % 2) ans[0] = arr[n / 2], x++, j++; for (; i >= 0 && j < n; i--, j++) { ans[x++] = arr[i]; ans[x++] = arr[j]; } for (int i = 0; i < n; i++) cout << ans[i] << ; cout << endl; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); void test_case() { int n; cin >> n; cout << fixed << setprecision(10) << (1 / tan(PI / (2 * n))) << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int tc; cin >> tc; for (int tt = 1; tt <= tc; ++tt) { test_case(); } return 0; }
#include <bits/stdc++.h> struct node { char a; struct node *left; struct node *right; }; int main() { int t, len; char ch[205]; struct node *p, *q, *r[26]; scanf( %d , &t); getchar(); while (t--) { int flag = 0; scanf( %s , ch); len = strlen(ch); for (int i = 0; i < 26; i++) r[i] = NULL; for (int i = 0; i < len; i++) { if (r[ch[i] - a ] == NULL) { p = (struct node *)malloc(sizeof(struct node)); p->a = ch[i]; p->left = NULL; p->right = NULL; r[ch[i] - a ] = p; if (i > 0) { if (r[ch[i - 1] - a ]->left == NULL) { r[ch[i - 1] - a ]->left = p; p->right = r[ch[i - 1] - a ]; } else if (r[ch[i - 1] - a ]->right == NULL) { r[ch[i - 1] - a ]->right = p; p->left = r[ch[i - 1] - a ]; } else { printf( NO n ); flag = 1; break; } } } else { if (r[ch[i] - a ]->left != r[ch[i - 1] - a ] && r[ch[i] - a ]->right != r[ch[i - 1] - a ]) { printf( NO n ); flag = 1; break; } } } if (flag == 1) continue; printf( YES n ); for (int i = 0; i < 26; i++) { if (r[i] != NULL) { q = r[i]; while (q->left != NULL) q = q->left; while (q != NULL) { printf( %c , q->a); q = q->right; } break; } } for (int i = 0; i < 26; i++) if (r[i] == NULL) printf( %c , i + a ); printf( n ); } }
#include <bits/stdc++.h> using namespace std; const int N = int(2e6); int n; bool used[N + 7]; int main() { cin >> n; for (int i = 2; i * i <= N; ++i) { if (!used[i]) { for (int z = i * i; z <= N; z = z + i) used[z] = true; } } int cnt = 0; for (int i = 2; i <= N; ++i) if (!used[i]) { printf( %d , i); ++cnt; if (cnt == n) return 0; } return 0; }
#include <bits/stdc++.h> using namespace std; long long const M = 2e5 + 10, M2 = 1e5 + 10, mod = 1e9 + 7, inf = 1e9 + 10; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; long long tmp = 1; for (long long i = 1;; i++) { if (tmp - 1 >= n) return cout << i - 1, 0; tmp *= 2; } }
#include<bits/stdc++.h> using namespace std; #define pi acos(-1) #define printfl printf( %f n ,f) #define printd printf( %lf n ,d) #define printi printf( %d n ,p) #define printl printf( %ld n ,l) #define printll printf( %lld n ,ll) #define case cout<< Case <<TEST<< : #define forcase for(int TEST=1;TEST<=test;TEST++) #define max3(a,b,c) max((a,b),c) #define min3(a,b,c) min((a,b),c) #define newl printf( n ) #define printd1d2 printf( %lf %lf n ,d1, d2) #define ll long long int void solve() { ll n,q;cin>>n>>q; string s;cin>>s; map <ll,ll> mp; ll indx=1; for (ll i = 1; i <=26; i++) { mp[i-1]=i; } ll sum[n+1]; sum[0]=0; ll l=1; for (ll i = 0; i < s.size(); i++) { sum[l]=sum[l-1]+mp[s[i]- a ]; l++; } for (ll i = 0; i < q; i++) { ll c,d;cin>>c>>d; cout<<sum[d]-sum[c-1]<<endl; } // cout<<sum[13]<<endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int test; // cin>>test; //forcase solve(); return 0; }
#include <bits/stdc++.h> using namespace std; long long mod_factorial[100005]; long long mod_inv_factorial[100005]; long long binpow(long long a, long long b) { long long res = 1LL; while (b) { if (b & 1) { res = (res * a); } a = (a * a); b >>= 1; } return res; } long long binpowmod(long long a, long long b, long long p) { long long res = 1LL; while (b) { if (b & 1) { res = (res * 1LL * a) % 1000000007; } a = (a * 1LL * a) % 1000000007; b >>= 1; } return res; } long long mod_inverse(long long a) { return binpowmod(a, 1000000007 - 2, 1000000007); } void precomputefact() { mod_factorial[0] = 1; for (int i = 1; i <= 1e5; i++) { mod_factorial[i] = (i * 1LL * mod_factorial[i - 1]) % 1000000007; } mod_inv_factorial[0] = 1; for (int i = 1; i <= 1e5; i++) { mod_inv_factorial[i] = ((mod_inv_factorial[i - 1]) * 1LL * mod_inverse(i)) % 1000000007; } } long long modbinomial(long long n, long long k, long long p) { long long ans = mod_factorial[n] * mod_inv_factorial[n - k] % 1000000007 * mod_inv_factorial[k] % 1000000007; return ans; } void solve() { int n; cin >> n; vector<pair<int, int>> temp; function<bool(pair<int, int>, pair<int, int>)> comp = [](pair<int, int> a, pair<int, int> b) { if (a.first < b.first) { return true; } else if (a.first == b.first) { if (a.second > b.second) { return true; } } return false; }; for (int i = 0; i < n; i++) { int l, r; cin >> l >> r; temp.push_back(make_pair(l, r)); } sort(temp.begin(), temp.end(), comp); for (int i = 0; i < temp.size(); i++) { int f = temp[i].first; int s = temp[i].second; if (f == s) { cout << f << << s << << f << n ; } else { if (f == temp[i + 1].first) { cout << f << << s << << temp[i + 1].second + 1 << n ; } else if (s == temp[i + 1].second) { cout << f << << s << << temp[i + 1].first - 1 << n ; } } } cout << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long testcase; cin >> testcase; while (testcase--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { bool discount_flag = true; int num_of_session; cin >> num_of_session; vector<int> num_of_teams(200001); for (int i = 0; i < num_of_session; i++) cin >> num_of_teams[i]; for (int i = 0; i < num_of_session; i++) if (num_of_teams[i] % 2 == 1) if (i != num_of_session - 1 and num_of_teams[i + 1] > 0) { num_of_teams[i + 1] -= 1; num_of_teams[i] = 0; } else if (num_of_teams[i + 1] == 0) discount_flag = false; else if (i == num_of_session) discount_flag = false; if (discount_flag) cout << YES << endl; else cout << NO << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int fact(vector<int> x, long long t) { if (t == 1) return 1; vector<long long> num(x.begin(), x.end()); stringstream s; string tmp; s << t; s >> tmp; int count = 0; for (int i = 0; i < num.size(); i++) { stringstream ss; string temp; ss << num[i]; ss >> temp; bool flag = false; for (int j = 0; j < temp.size(); j++) { for (int k = 0; k < tmp.size(); k++) if (temp[j] == tmp[k]) { count++; flag = true; break; } if (flag) break; } } return count; } int main() { long long n; cin >> n; long long t = n; vector<int> factors; long long i; factors.push_back(1); for (i = 2; i < sqrt(n); i++) { if (n % i == 0) { factors.push_back(n / i); factors.push_back(i); } } if (i * i == n) { factors.push_back(i); } factors.push_back(n); cout << fact(factors, t); return 0; }
#include <bits/stdc++.h> using namespace std; void optimise() { ios_base::sync_with_stdio(false); cin.tie(NULL); } long long int mod = 998244353; long long int inf = 1e18 + 6; void normalize(long long int &a) { a = (a + mod) % mod; } long long int modmul(long long int a, long long int b) { a = a % mod; b = b % mod; normalize(a); normalize(b); return (a * b) % mod; } long long int modadd(long long int a, long long int b) { a = a % mod; b = b % mod; normalize(a); normalize(b); return (a + b) % mod; } long long int modsub(long long int a, long long int b) { a = a % mod; b = b % mod; normalize(a); normalize(b); return (a - b + mod) % mod; } long long int me(long long int x, long long int n) { x %= mod; if (n == 0) return 1; long long int u = me(x, n / 2) % mod; u = (u * u) % mod; if (n % 2) u = (u * x) % mod; return u; } long long int me1(long long int x, long long int n) { if (n == 0) return 1; long long int u = me1(x, n / 2); u = u * u; if (n % 2) u = u * x; return u; } inline long long int modInv(long long int a) { return me(a, mod - 2); } inline long long int modDiv(long long int a, long long int b) { return modmul(a, modInv(b)); } long long int __gcd(long long int a, long long int b) { if (b == 0) return a; else return __gcd(b, a % b); } const long long int N = 1e6 + 1; vector<long long int> G[N]; void solve() { long long int n; cin >> n; long long int a[n + 1]; long long int sum[n + 1]; sum[0] = 0; for (long long int i = 1; i <= n; ++i) { cin >> a[i]; sum[i] = sum[i - 1] + a[i]; } map<long long int, vector<pair<long long int, long long int> > > m; for (long long int i = 1; i <= n; i++) { for (long long int j = 1; j + i - 1 <= n; j++) { long long int sum1 = sum[i + j - 1] - sum[j - 1]; m[sum1].push_back({j + i - 1, j}); } } long long int max1 = 0; long long int ans = -1; for (auto i = m.begin(); i != m.end(); i++) { sort(m[i->first].begin(), m[i->first].end()); long long int j1 = 0; long long int r = 0; long long int sum1 = i->first; for (auto j = m[sum1].begin(); j != m[sum1].end(); j++) { if (j->second > r) { j1++; r = j->first; } } if (j1 > max1) { ans = sum1; max1 = j1; } } cout << max1 << n ; long long int r = 0; for (auto k1 = m[ans].begin(); k1 != m[ans].end(); k1++) { if (k1->second > r) { cout << k1->second << << k1->first << n ; r = k1->first; } } } signed main() { optimise(); long long int t; t = 1; while (t--) { solve(); cout << n ; } }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; long long int a[n + 2]; vector<long long int> pre(n + 2); for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + n + 1); for (int i = 1; i <= n; i++) { pre[i] = pre[i - 1] + a[i]; } int left = 1; int num = -1, occur = -1; for (int i = 1; i <= n; i++) { while ((a[i] * (i - left + 1) - (pre[i] - pre[left - 1])) > k) left++; if (occur < (i - left + 1)) { occur = i - left + 1; num = a[i]; } } cout << occur << << num << endl; }
#include <bits/stdc++.h> int main() { int t, a, b, i, temp; scanf( %d , &t); for (i = 1; i <= t; i++) { scanf( %d %d , &a, &b); if (b > a) { temp = a; a = b; b = temp; } while ((a * a) < 2 * a * b) { a++; } printf( %d n , a * a); } }
#include <bits/stdc++.h> using namespace std; long long a[222222], b[222222], c[222222], d[222222]; set<long long> st; set<long long>::iterator it; map<long long, long long> sp2; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, m, k, x, s, i; cin >> n >> m >> k; cin >> x >> s; for (i = 1; i <= m; i++) { cin >> a[i]; } for (i = 1; i <= m; i++) { cin >> b[i]; } for (i = 1; i <= k; i++) { cin >> c[i]; } for (i = 1; i <= k; i++) { cin >> d[i]; st.insert(d[i]); sp2[d[i]] = c[i]; } long long ans = n * x; long long rem; long long temp; for (i = 1; i <= m; i++) { if (b[i] <= s) { temp = 0; rem = s - b[i]; it = st.upper_bound(rem); if (it != st.begin()) { it--; temp += sp2[*it]; } if (temp >= n) ans = 0; else ans = min(ans, (n - temp) * a[i]); } } temp = 0; it = st.upper_bound(s); if (it != st.begin()) { it--; temp += sp2[*it]; } if (temp >= n) ans = 0; else ans = min(ans, (n - temp) * x); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF32 = 0x3f3f3f3f; const double EPS = 1.0e-8; const double PI = acos(-1.0); struct Point { double x, y; Point(double _x = 0, double _y = 0) { x = _x; y = _y; } friend Point operator+(const Point &a, const Point &b) { return Point(a.x + b.x, a.y + b.y); } friend Point operator-(const Point &a, const Point &b) { return Point(a.x - b.x, a.y - b.y); } friend double operator^(const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; } friend bool operator==(const Point &a, const Point &b) { return fabs(a.x - b.x) < EPS && fabs(a.y - b.y) < EPS; } }; struct V { Point start, end; V(Point _start = Point(0, 0), Point _end = Point(0, 0)) { start = _start; end = _end; } }; Point Basic; set<Point> Set; double Distance(Point a, Point b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } bool operator<(Point a, Point b) { a = a - Basic; b = b - Basic; double Ang1 = atan2(a.y, a.x), Ang2 = atan2(b.y, b.x); double Len1 = Distance(a, Point(0.0, 0.0)), Len2 = Distance(b, Point(0.0, 0.0)); if (fabs(Ang1 - Ang2) < EPS) return Len1 < Len2; return Ang1 < Ang2; } set<Point>::iterator Pre(set<Point>::iterator it) { if (it == Set.begin()) it = Set.end(); return --it; } set<Point>::iterator Nxt(set<Point>::iterator it) { ++it; return it == Set.end() ? Set.begin() : it; } int Query(Point p) { set<Point>::iterator it = Set.lower_bound(p); if (it == Set.end()) it = Set.begin(); return ((p - *(Pre(it))) ^ (*(it) - *(Pre(it)))) < EPS; } void Insert(Point p) { if (Query(p)) return; Set.insert(p); set<Point>::iterator it = Nxt(Set.find(p)); while (Set.size() > 3 && ((p - *(Nxt(it))) ^ (*(it) - *(Nxt(it)))) < EPS) { Set.erase(it); it = Nxt(Set.find(p)); } it = Pre(Set.find(p)); while (Set.size() > 3 && ((p - *(it)) ^ (*(it) - *(Pre(it)))) > -EPS) { Set.erase(it); it = Pre(Set.find(p)); } } int main() { int q; scanf( %d , &q); Basic = Point(0, 0); int oper; Point a, str[5]; for (int i = 1; i <= 3; ++i) { scanf( %d%lf%lf , &oper, &str[i].x, &str[i].y); Basic = Basic + str[i]; } Basic.x /= 3.0; Basic.y /= 3.0; q -= 3; for (int i = 1; i <= 3; ++i) { Set.insert(str[i]); } while (q--) { scanf( %d%lf%lf , &oper, &a.x, &a.y); if (oper == 1) { Insert(a); } else { if (Query(a)) printf( YES n ); else printf( NO n ); } } }
#include <bits/stdc++.h> using namespace std; int n; vector<array<int, 2>> v, good; long long f(int x, int y) { return abs(v[x][0] - v[y][0]) + abs(v[x][1] - v[y][1]); } int main() { ios_base::sync_with_stdio(0), cin.tie(0); cin >> n; v.resize(n); long long ans = 0; for (int i = 0; i < n; i++) { cin >> v[i][0] >> v[i][1]; } int min_x = -1, max_x = -1, min_y = -1, max_y = -1; for (int i = 0; i < n; i++) { if (min_x == -1 || v[i][0] < v[min_x][0]) { min_x = i; } if (max_x == -1 || v[i][0] > v[max_x][0]) { max_x = i; } if (min_y == -1 || v[i][1] < v[min_y][1]) { min_y = i; } if (max_y == -1 || v[i][1] > v[max_y][1]) { max_y = i; } ans += f(i, (i + 1) % n); } if (n == 3) { return cout << ans, 0; } set<int> st; st.insert(min_x), st.insert(max_x), st.insert(min_y), st.insert(max_y); for (int i : st) good.push_back(v[i]); long long ans3 = 0; if ((int)(good).size() < 4) { ans3 = ans; } else { for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { for (int k = j + 1; k < 4; k++) { long long cur = 0; cur += abs(good[j][0] - v[i][0]) + abs(good[j][1] - v[i][1]); cur += abs(good[j][0] - good[k][0]) + abs(good[j][1] - good[k][1]); cur += abs(good[k][0] - v[i][0]) + abs(good[k][1] - v[i][1]); ans3 = max(ans3, cur); } } } } cout << ans3 << ; for (int i = 4; i <= n; i++) { cout << ans << ; } }
#include <bits/stdc++.h> using namespace std; const long long base = 7; const long long maxn = 1e6 + 9; const long long inf = 1e9 + 7; queue<long long> l[30], r[30], l1, r1, lf, rf; vector<pair<long long, long long> > res; long long n, i; string a, b; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> a >> b; a = + a; b = + b; for (i = 1; i <= n; i++) { if (a[i] != ? ) l[a[i] - a ].push(i); else l1.push(i); } for (i = 1; i <= n; i++) { if (b[i] != ? ) r[b[i] - a ].push(i); else r1.push(i); } for (i = 0; i <= 27; i++) { while (true) { if (!l[i].empty() && !r[i].empty()) { res.push_back(make_pair(l[i].front(), r[i].front())); l[i].pop(); r[i].pop(); } else { while (!l[i].empty()) { lf.push(l[i].front()); l[i].pop(); } while (!r[i].empty()) { rf.push(r[i].front()); r[i].pop(); } break; } } } while (true) { if (!lf.empty() && !r1.empty()) { res.push_back(make_pair(lf.front(), r1.front())); lf.pop(); r1.pop(); } else break; } while (true) { if (!rf.empty() && !l1.empty()) { res.push_back(make_pair(l1.front(), rf.front())); rf.pop(); l1.pop(); } else break; } while (true) { if (!r1.empty() && !l1.empty()) { res.push_back(make_pair(l1.front(), r1.front())); r1.pop(); l1.pop(); } else break; } cout << res.size() << n ; for (i = 0; i < res.size(); i++) cout << res[i].first << << res[i].second << n ; }
#include <bits/stdc++.h> using namespace std; int main() { cin.sync_with_stdio(false); int ans, cnt, k; string str; while (cin >> str >> k) { ans = 0; cnt = 0; int len = str.length(); for (int i = len - 1; i >= 0; i--) { if (str[i] == 0 ) { ans++; } } if (ans < k) { cout << len - 1 << endl; } else { for (int i = len - 1; i >= 0; i--) { if (str[i] != 0 ) { cnt++; } else { k--; } if (k == 0) { break; } } cout << cnt << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; int b, d, ans, l, h; char a[105], c[105]; bool letter[30]; int lena, lenc; int ll[105], yy[105]; char aa[105 * 105]; inline int ok() { memset(letter, 0, sizeof(letter)); for (char *p = a; *p; ++p) letter[*p - a ] = 1; for (char *p = c; *p; ++p) if (letter[*p - a ] == 0) return 0; return 1; } inline int init() { for (int i = 0; i < 105; ++i) strcpy(aa + i * lena, a); for (int i = 0; i < lena; ++i) { char *p = aa + i, *q = c; while (*p && *q) { while (*p && *p != *q) ++p; if (*p != *q) return 0; ++p, ++q; } if (*q != 0) return 0; ll[i] = p - (aa + i); yy[i] = (p - aa) % lena; } return 1; } inline int test(int p) { p = p * d; long long pos = 0; long long vst[105], cnt[105]; memset(vst, -1, sizeof(vst)); for (int i = 0; i < p; ++i) { if (pos >= lena * b) return 0; if (vst[pos % lena] > -1) { long long tmp = pos - vst[pos % lena]; long long tem = i - cnt[pos % lena]; pos += tmp * ((p - i) / tem); i = (p - i) / tem * tem + i - 1; memset(vst, -1, sizeof(vst)); continue; } if (i) { vst[pos % lena] = pos; cnt[pos % lena] = i; } pos += ll[pos % lena]; } if (pos - 1 >= lena * b) return 0; return 1; } int main(int argc, char **argv) { scanf( %d%d , &b, &d); scanf( %s%s , a, c); lena = strlen(a), lenc = strlen(c); if (!ok() || !init()) puts( 0 ); else { ans = 0, l = 1, h = lena * b / (lenc * d); while (l <= h) { int mid = (l + h) >> 1; if (test(mid)) ans = mid, l = mid + 1; else h = mid - 1; } printf( %d n , ans); } return 0; }
#include<bits/stdc++.h> #define pb push_back #define mk make_pair #define ll long long #define ss second #define ff first #define pll pair<ll,ll> #define vll vector<ll> #define mll map<ll,ll> #define mod 1000000007 #define w(x) ll x; cin>>x; while(x--) #define ps(x,y) fixed<<setprecision(y)<<x; #define fo(i, j, k, in) for (ll i=j ; i<k ; i+=in) #define re(i, j) fo(i, 0, j, 1) #define pi 3.1415926535897932384626433832795 #define all(cont) cont.begin(), cont.end() #define countbit(x) __builtin_popcount(x) #define mod 1000000007 #define lo lower_bound #define de(n) ll n;cin>>n; #define def(a,n) ll n;cin>>n;ll a[n];re(i,n){cin>>a[i];} #define defi(a,n,k) ll n;cin>>n; ll k;cin>>k;ll a[n];re(i,n){cin>>a[i];} #define deb(x) cout<<#x<< = <<x<<endl; #define tr(it,a) for(auto it=a.begin();it!=a.end();it++) #define nl cout<<endl; using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; int x[2]={1,-1}; int y[2]={1,-1}; pair<int,int> p; p.first=0; p.second=0; int a1=n/2; int a2=n-n/2; if(n%2!=0){ int c=a2+1; int d=(a1+1); ll ans=1ll*2*(c*d); cout<<ans<<endl; } else{ int c=a2+1; int d=a1+1; ll ans=1ll*c*d; cout<<ans<<endl; } return 0; }
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << << x << ; } void __print(const char *x) { cerr << << x << ; } void __print(const string &x) { cerr << << x << ; } void __print(bool x) { cerr << (x ? true : false ); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << { ; __print(x.first); cerr << , ; __print(x.second); cerr << } ; } template <typename T> void __print(const T &x) { int f = 0; cerr << { ; for (auto &i : x) cerr << (f++ ? , : ), __print(i); cerr << } ; } void _print() { cerr << ] n ; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << , ; _print(v...); } inline long long int GCD(long long int x, long long int y) { if (x < y) swap(x, y); if (x == 0) return y; if (y == 0) return x; return GCD(x % y, y); } long long int phi(long long int n) { long long int result = n; for (long long int i = 2; i * i <= n; i++) { if (n % i == 0) { while (n % i == 0) n /= i; result -= result / i; } } if (n > 1) result -= result / n; return result; } long long int power(long long int x, long long int n, long long int mod) { long long int res = 1; x %= mod; while (n) { if (n & 1) { res = ((res * x) % mod + mod) % mod; } x = ((x * x) % mod + mod) % mod; n >>= 1; } return res; } long long int A[105][105]; long long int row[105], col[105], fr[105] = {0}, fc[105] = {0}, cr = 0, cc = 0; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long int n, m; cin >> n >> m; for (long long int i = 1; i <= n; i++) { for (long long int j = 1; j <= m; j++) { cin >> A[i][j]; row[i] += A[i][j]; col[j] += A[i][j]; } } while (1) { bool f = true; for (long long int i = 1; i <= n; i++) { if (row[i] < 0) { f = false; fr[i] ^= 1; cr--; if (fr[i]) cr += 2; for (long long int j = 1; j <= m; j++) { row[i] -= (2 * A[i][j]); col[j] -= (2 * A[i][j]); A[i][j] = -A[i][j]; } } } for (long long int j = 1; j <= m; j++) { if (col[j] < 0) { f = false; fc[j] ^= 1; cc--; if (fc[j]) cc += 2; for (long long int i = 1; i <= n; i++) { row[i] -= (2 * A[i][j]); col[j] -= (2 * A[i][j]); A[i][j] = -A[i][j]; } } } if (f) break; } cout << cr << ; for (long long int i = 1; i <= n; i++) { if (fr[i]) cout << i << ; } cout << endl; cout << cc << ; for (long long int j = 1; j <= m; j++) { if (fc[j]) cout << j << ; } cout << endl; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 100010; const int MAXV = 100010; const int MAXT = 333; int a[MAXN], n; int b[MAXN], m; int s, e; vector<int> ind[MAXV]; int p[MAXN][MAXT]; int main() { scanf( %d%d%d%d , &n, &m, &s, &e); for (int i = int(0); i <= int((MAXV)-1); ++i) ind[i].clear(); for (int i = int(1); i <= int(n); ++i) scanf( %d , a + i), ind[a[i]].push_back(i); for (int i = int(1); i <= int(m); ++i) scanf( %d , b + i); int mt = s / e; int ans = 0; for (int t = int(1); t <= int(mt); ++t) p[0][t] = n + 1; p[0][0] = 0; for (int j = int(1); j <= int(m); ++j) { for (int t = int(0); t <= int(mt); ++t) p[j][t] = p[j - 1][t]; for (int t = int(0); t <= int(mt - 1); ++t) { vector<int>::iterator it = lower_bound(ind[b[j]].begin(), ind[b[j]].end(), p[j - 1][t] + 1); if (it != ind[b[j]].end()) p[j][t + 1] = min(p[j][t + 1], *it); } for (int t = int(1); t <= int(mt); ++t) { int i = p[j][t]; if (i > n) break; int rest = s - (i + j); int cnt = rest / e; ans = max(ans, min(cnt, t)); } } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; struct rec { int x, y; } p; char a[2][100010]; queue<rec> q; int d[2][100010], n, k; bool bfs() { memset(d, -1, sizeof(d)); q.push((rec){0, 1}); d[0][1] = 0; while (q.size()) { p = q.front(); q.pop(); if (p.y + k > n) return 1; if (a[p.x][p.y + 1] == - && d[p.x][p.y + 1] == -1 && d[p.x][p.y] + 1 < p.y + 1) q.push((rec){p.x, p.y + 1}), d[p.x][p.y + 1] = d[p.x][p.y] + 1; if (a[p.x][p.y - 1] == - && d[p.x][p.y - 1] == -1 && d[p.x][p.y] + 1 < p.y - 1) q.push((rec){p.x, p.y - 1}), d[p.x][p.y - 1] = d[p.x][p.y] + 1; if (a[p.x ^ 1][p.y + k] == - && d[p.x ^ 1][p.y + k] == -1 && d[p.x][p.y] + 1 < p.y + k) q.push((rec){p.x ^ 1, p.y + k}), d[p.x ^ 1][p.y + k] = d[p.x][p.y] + 1; } return 0; } int main() { cin >> n >> k; scanf( %s%s , a[0] + 1, a[1] + 1); if (bfs()) puts( YES ); else puts( NO ); return 0; }
#include <bits/stdc++.h> using namespace std; struct node { int Tea; int Index; friend bool operator<(const node& a, const node& b) { return a.Tea > b.Tea; } } D[105]; int main() { int i, n, w, Ans[105]; scanf( %d%d , &n, &w); for (i = 0; i < n; i++) { scanf( %d , &D[i].Tea); D[i].Index = i; } sort(D, D + n); int Judge = 0; for (i = 0; i < n; i++) Judge += (D[i].Tea + 1) / 2; if (Judge > w) printf( -1 n ); else { w -= Judge; for (i = 0; i < n; i++) Ans[D[i].Index] = (D[i].Tea + 1) / 2; for (i = 0; i < n && w; i++) { if (w >= D[i].Tea - Ans[D[i].Index]) w -= D[i].Tea - Ans[D[i].Index], Ans[D[i].Index] = D[i].Tea; else Ans[D[i].Index] += w, w = 0; } for (i = 0; i < n; i++) { if (i) printf( ); printf( %d , Ans[i]); } printf( n ); } return 0; }
#include <bits/stdc++.h> int main() { int n, s, k = 0; scanf( %d %d , &n, &s); while (s > 0) { s -= n; k++; } printf( %d , k); return 0; }
#include <bits/stdc++.h> using namespace std; #define y1 temp_y1 #define all(x) x.begin(), x.end() const int N = 3e5 + 5; int n; int lst[N]; int fa[N]; vector<int> g[N]; int min_leaf = -1; int ext_cnt = 0; bool ok = true; void dfs1(int u, int k) { for (auto v : g[u]) { dfs1(v, k); } if (ext_cnt < k && lst[u] != ext_cnt) { ok = false; } if (ext_cnt == k) { min_leaf = u; } ++ext_cnt; } long long ans = 0; int seq[N]; int dfs_cnt = 0; int siz[N]; void dfs2(int u, int k, int dep) { seq[u] = dfs_cnt++; if (lst[u] < k) { ans += dep; siz[u]++; } for (auto v : g[u]) { dfs2(v, k, dep + 1); siz[u] += siz[v]; } if (lst[u] < k && lst[u] != ext_cnt) { ok = false; } if (lst[u] >= k && siz[u] + seq[u] != lst[u]) { ok = false; } ++ext_cnt; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 0; i < n; ++i) { cin >> lst[i]; --lst[i]; } for (int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v; --u; --v; fa[v] = u; g[u].push_back(v); } for (int i = 0; i < n; ++i) { sort(all(g[i]), [&](int x, int y) -> bool { return lst[x] < lst[y]; }); } if (lst[0] == 0) { dfs2(0, 0, 0); if (ok) { cout << YES << endl; cout << 0 << endl; for (int i = 0; i < n; ++i) { cout << lst[i] + 1 << ; } cout << endl; return 0; } else { cout << NO << endl; return 0; } } int v = lst[0] - 1; dfs1(0, v); int u = min_leaf; bool is_find = false; fa[0] = -1; //cerr << v + 1 << << u + 1 << << lst[u] + 1 <<endl; do { if (lst[u] == v) { is_find = true; } if (lst[u] == v && fa[u] != -1) { swap(lst[u], lst[fa[u]]); ++ans; } u = fa[u]; } while (u != -1); if (!is_find) { cout << NO << endl; return 0; } ext_cnt = 0; dfs2(0, v, 0); if (ok) { cout << YES << endl; cout << ans << endl; for (int i = 0; i < n; ++i) { cout << seq[i] + 1 << ; } cout << endl; } else { cout << NO << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; vector<pair<int, int>> flowers; vector<int> first_flowers; vector<long long> pref_sum; long long answer; void In() { flowers.clear(); pref_sum.clear(); first_flowers.clear(); answer = LLONG_MIN; cin >> n >> m; flowers.resize(m); for (int i = 0; i < m; i++) cin >> flowers[i].first >> flowers[i].second; } long long GetPrefSum(int start_index) { return pref_sum.back() - ((start_index - 1 >= 0) ? pref_sum[start_index - 1] : 0); } void Try(int index) { auto range = equal_range(first_flowers.begin(), first_flowers.end(), flowers[index].second); int less_or_equal = range.second - first_flowers.begin(); int remaining = m - less_or_equal; remaining = min(remaining, n); int start_index = m - remaining; if (index >= start_index) { long long result = GetPrefSum(start_index); long long left_take = n - remaining; result += left_take * flowers[index].second; answer = max(answer, result); } else { long long result = flowers[index].first; int really_remaining = m - less_or_equal; really_remaining = min(really_remaining, n - 1); start_index = m - really_remaining; result += GetPrefSum(start_index); long long left_take = n - 1 - really_remaining; result += left_take * flowers[index].second; answer = max(answer, result); } } void Solve() { sort(flowers.begin(), flowers.end()); pref_sum.resize(m); pref_sum[0] = flowers[0].first; for (int i = 1; i < m; i++) pref_sum[i] = flowers[i].first + pref_sum[i - 1]; first_flowers.resize(m); for (int i = 0; i < m; i++) first_flowers[i] = flowers[i].first; for (int i = 0; i < m; i++) Try(i); } void Out() { cout << answer << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; cin >> t; while (t--) { In(); Solve(); Out(); } }
#include <bits/stdc++.h> using namespace std; int digit(long long int n) { if (n > 0) return (digit(n / 10) + 1); return 0; } int main() { long long int l, h; long long int a[] = {0, 4, 49, 499, 4999, 49999, 499999, 4999999, 49999999, 499999999, 4999999999}; long long int b[] = {0, 9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999, 9999999999}; cin >> l >> h; long long int ans = max(a[digit(l)], l); ans = min(a[digit(h)], h); ans = max(ans, l); ans = (long long int)(ans) * (b[digit(ans)] - ans); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int mo = 1e9 + 7; int main() { long long a, b, n; while (cin >> a >> b) { n = b - 1; long long ans = 0; for (int k = 1; k <= a; ++k) { ans = (ans + (((((k * b) % mo) * ((n * (n + 1) / 2) % mo)) % mo + (n * (n + 1) / 2) % mo)) % mo) % mo; } cout << ans % mo << endl; } }
#include <bits/stdc++.h> using namespace std; template <class T> inline T BM(T p, T e, T M) { long long int ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T MDINV(T a, T M) { return BM(a, M - 2, M); } template <class T> inline T PW(T p, T e) { long long int ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p); p = (p * p); } return (T)ret; } template <class T> string NTS(T Number) { stringstream ss; ss << Number; return ss.str(); } template <class T> T stringtonumber(const string &Text) { istringstream ss(Text); T result; return ss >> result ? result : 0; } template <class T> bool ISLEFT(T a, T b, T c) { if (((a.first - b.first) * (b.second - c.second) - (b.first - c.first) * (a.second - b.second)) < 0.0) return 1; else return 0; } char a[10009]; int z[10009]; char ch[5000 + 2]; int ar[5000 + 2][5000 + 2]; int res[5000 + 2][5000 + 2]; void zalgo() { int l, r; l = r = 0; z[0] = strlen(a); for (int i = 1; i < z[0]; i++) { if (i > r) { l = r = i; for (int j = i; j < z[0]; j++) { if (a[j - i] != a[j]) break; else r = j; } if (a[i] != a[0]) z[i] = 0; else z[i] = r - l + 1; } else { int k = i - l; if (z[k] < r - i + 1) { z[i] = z[k]; } else { l = i; for (int j = r + 1; j < z[0]; j++) { if (a[j - l] != a[j]) break; else r = j; } z[i] = r - l + 1; } } } } int dp[5009][5009]; void update(int x1, int y1, int val) { while (y1 <= 5002) { dp[x1][y1] = (dp[x1][y1] + val) % 1000000007; y1 += (y1 & -y1); } } int query(int x1, int y1) { int sum1 = 0; while (y1 > 0) { sum1 = (sum1 + dp[x1][y1]) % 1000000007; y1 -= (y1 & -y1); } return sum1; } int main() { int n; scanf( %d , &n); scanf( %s , ch + 1); for (int i = 1; i <= n; i++) { int sz = 0; for (int j = i; j <= n; j++) { a[sz++] = ch[j]; } int len = sz; for (int j = i + 1; j <= n; j++) { a[sz++] = ch[j]; } a[sz] = 0 ; zalgo(); for (int j = i + 1; j <= n; j++) { ar[i][j] = z[len + (j - i - 1)]; } } res[0][0] = 1; for (int i = 1; i <= n; i++) { if (ch[i] == 0 ) continue; for (int j = i; j <= n; j++) { if (i == 1) res[i][j] = 1; else { int ga = j - i; int x = i - 1 - ga; int y = i - 1; x = max(1, x); if (y - x == ga) { int in = x; int mil = ar[x][i]; if (mil < (ga + 1)) { if (ch[x + mil] < ch[i + mil]) { in = x - 1; } } int sum = query(y, y) - query(y, in); if (sum < 0) sum += 1000000007; res[i][j] = sum; } else { int sum = query(y, y) - query(y, x - 1); if (sum < 0) sum += 1000000007; res[i][j] = sum; } } update(j, i, res[i][j]); } } int su = 0; for (int i = 1; i <= n; i++) { su = (su + res[i][n]) % 1000000007; } printf( %d n , su); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 110; int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); int al, ar, bl, br; cin >> al >> ar >> bl >> br; if ((al * 2 + 3 > br && br >= al - 1) || (ar * 2 + 3 > bl && bl >= ar - 1)) cout << YES << endl; else cout << NO << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long int c = 0; void bfs(map<long long int, vector<long long int> > &map, vector<bool> &visited, long long int x) { queue<long long int> q; q.push(x); visited[x] = true; while (!q.empty()) { long long int y = q.front(); q.pop(); for (long long int i : map[y]) { if (!visited[i]) { q.push(i); visited[i] = true; } } } } int main() { long long int x, y, n, m; cin >> n >> m; map<long long int, vector<long long int> > map; std::vector<bool> visited(n + 1, false), visitedf(n + 1, false); for (long long int i = 0; i < m; i++) { cin >> x >> y; map[x].push_back(y); map[y].push_back(x); } if (m != n - 1) { cout << no ; return 0; } bfs(map, visited, map.begin()->first); for (long long int i = 1; i <= n; i++) { if (!visited[i]) { cout << no ; return 0; } } cout << yes ; }
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) using namespace std; const int N = (int)2e5 + 100; const int M = (int)2e6 + 100; const int inf = (int)1e9 + 100; int a[N]; int b[N]; int t[4 * N]; map<int, int> m[4 * N]; void add(int x, int L, int R, int pos, int val) { t[x] = min(t[x], val); m[x][val]++; if (R - L == 1) { return; } int c = (L + R) >> 1; if (c > pos) { add(2 * x + 1, L, c, pos, val); } else { add(2 * x + 2, c, R, pos, val); } } int sum(int x, int L, int R, int l, int r) { if (R == r && L == l) { return t[x]; } int c = (L + R) >> 1; int o = inf; if (c > l) { o = sum(2 * x + 1, L, c, l, min(c, r)); } if (c < r) { o = min(o, sum(2 * x + 2, c, R, max(l, c), r)); } return o; } int del(int x, int L, int R, int l, int r, int val) { if (R - L == 1) { m[x][val]--; if (!m[x][val]) m[x].erase(val); if (m[x].size()) t[x] = m[x].begin()->first; else t[x] = inf; return L; } int c = (L + R) >> 1; int o = -1; m[x][val]--; if (!m[x][val]) m[x].erase(val); if (c > l && m[2 * x + 1].count(val)) { o = del(2 * x + 1, L, c, l, min(c, r), val); } if (c < r && m[2 * x + 2].count(val) && o == -1) { o = del(2 * x + 2, c, R, max(l, c), r, val); } t[x] = min(t[2 * x + 1], t[2 * x + 2]); return o; } void print(int x, int L, int R) { cout << x << << L << << R << : << t[x] << n ; for (auto j : m[x]) { cout << j.first << << j.second << n ; } cout << n ; if (R - L == 1) return; int c = (L + R) >> 1; print(2 * x + 1, L, c); print(2 * x + 2, c, R); } int main() { mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); srand(time(0)); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, M, i; cin >> n >> M; vector<int> v; for ((i) = 0; (i) < (n); (i)++) { cin >> a[i]; v.push_back(a[i]); } for ((i) = 0; (i) < (4 * N); (i)++) { t[i] = inf; } for ((i) = 0; (i) < (M); (i)++) { cin >> b[i]; v.push_back(b[i]); } sort((v).begin(), (v).end()), v.resize(unique((v).begin(), (v).end()) - v.begin()); map<int, int> m; for ((i) = 0; (i) < (v.size()); (i)++) { m[v[i]] = i; } int R = v.size(); for ((i) = 0; (i) < (M); (i)++) { int p; cin >> p; add(0, 0, R, m[b[i]], p); } int ma = 0; vector<pair<int, long long> > g; for ((i) = n - 1; (i) >= 0; (i)--) { if (a[i] > ma) { ma = a[i]; g.push_back({i, a[i]}); } else { g[g.size() - 1].second += a[i]; } } int ans = 0; for ((i) = 0; (i) < (g.size()); (i)++) { int x = sum(0, 0, R, 0, m[a[g[i].first]] + 1); int y; while (x != inf && x <= g[i].second) { y = del(0, 0, R, 0, m[a[g[i].first]] + 1, x); ans++; g[i].second -= x; x = sum(0, 0, R, 0, m[a[g[i].first]] + 1); } if (x != inf) { y = del(0, 0, R, 0, m[a[g[i].first]] + 1, x); add(0, 0, R, y, x - g[i].second); } } cout << ans; }
#include <bits/stdc++.h> using namespace std; bool custom(pair<int, int> a, pair<int, int> b) { if (a.first == b.first) { return a.second < b.second; } return a.first < b.first; } bool c1(string a) { int dec = 0; for (int i = 0; i < a.size() - 1; i++) { if (a[i] >= a[i + 1]) { dec += (a[i] > a[i + 1]); } else { dec = 0; } if (dec == 2) { return true; } } return false; } bool c2(string b) { char ant = b[0]; for (auto& first : b) { if (ant > first) { return true; } ant = first; } return false; } int main() { int k; cin >> k; int i = 1; string first = to_string(i); while (k > first.size()) { k -= first.size(); i++; first = to_string(i); } int c = 0; char an = first[0]; while (k > 0) { an = first[c]; c++; k--; } cout << an; }
#include <bits/stdc++.h> using namespace std; int n, a, b; vector<vector<int> > adj; vector<int> col; queue<int> x, y; void colorear(int u, int c) { col[u] = c; for (int v : adj[u]) if (col[v] == -1) colorear(v, 1 - c); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; adj = vector<vector<int> >(2 * n + 2); for (int i = 0; i < n; i++) { cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); x.push(a); y.push(b); if (a % 2) { adj[a].push_back(a + 1); adj[a + 1].push_back(a); } if (b % 2) { adj[b].push_back(b + 1); adj[b + 1].push_back(b); } } col = vector<int>(2 * n + 2, -1); for (int i = 1; i <= 2 * n; i++) { if (col[i] == -1) { colorear(i, 0); } } while (n--) { cout << col[x.front()] + 1 << << col[y.front()] + 1 << n ; x.pop(); y.pop(); } return 0; }
#include <bits/stdc++.h> using namespace std; pair<int, int> a[100000]; int main() { int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { int go; cin >> go; a[i].first = go; a[i].second = i; } sort(a, a + n); int an = 0; for (int i = 0; i < n; i++) { k -= a[i].first; if (k < 0) break; an++; } cout << an << endl; for (int i = 0; i < an; i++) { cout << a[i].second + 1 << ; } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #ifdef tabr #include library/debug.cpp #else #define debug(...) #endif template <long long mod> struct modular { long long value; modular(long long x = 0) { value = x % mod; if (value < 0) value += mod; } modular& operator+=(const modular& other) { if ((value += other.value) >= mod) value -= mod; return *this; } modular& operator-=(const modular& other) { if ((value -= other.value) < 0) value += mod; return *this; } modular& operator*=(const modular& other) { value = value * other.value % mod; return *this; } modular& operator/=(const modular& other) { long long a = 0, b = 1, c = other.value, m = mod; while (c != 0) { long long t = m / c; m -= t * c; swap(c, m); a -= t * b; swap(a, b); } a %= mod; if (a < 0) a += mod; value = value * a % mod; return *this; } modular operator+(const modular& rhs) const { return modular(*this) += rhs; } modular operator-(const modular& rhs) const { return modular(*this) -= rhs; } modular operator*(const modular& rhs) const { return modular(*this) *= rhs; } modular operator/(const modular& rhs) const { return modular(*this) /= rhs; } modular& operator++() { return *this += 1; } modular& operator--() { return *this -= 1; } modular operator++(int) { modular res(*this); *this += 1; return res; } modular operator--(int) { modular res(*this); *this -= 1; return res; } modular operator-() const { return modular(-value); } bool operator==(const modular& rhs) const { return value == rhs.value; } bool operator!=(const modular& rhs) const { return value != rhs.value; } bool operator<(const modular& rhs) const { return value < rhs.value; } }; template <long long mod> string to_string(const modular<mod>& x) { return to_string(x.value); } template <long long mod> ostream& operator<<(ostream& stream, const modular<mod>& x) { return stream << x.value; } template <long long mod> istream& operator>>(istream& stream, modular<mod>& x) { stream >> x.value; x.value %= mod; if (x.value < 0) x.value += mod; return stream; } constexpr long long mod = (long long) 1e9 + 7; using mint = modular<mod>; mint power(mint a, long long n) { mint res = 1; while (n > 0) { if (n & 1) { res *= a; } a *= a; n >>= 1; } return res; } vector<mint> fact(1, 1); vector<mint> finv(1, 1); mint C(int n, int k) { if (n < k || k < 0) { return mint(0); } while ((int) fact.size() < n + 1) { fact.emplace_back(fact.back() * (int) fact.size()); finv.emplace_back(mint(1) / fact.back()); } return fact[n] * finv[k] * finv[n - k]; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> c(n), b(n - 1); for (int i = 0; i < n; i++) { cin >> c[i]; } for (int i = 0; i < n - 1; i++) { cin >> b[i]; } vector<int> d(n); for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { d[i] += b[j]; } } int q; cin >> q; map<int, mint> mp; mp[(int) 2e9] = 0; mp[(int) -2e9] = 1; for (int i = 0; i < n; i++) { mp[(int) -2e9] *= c[i] + 1; } while (q--) { int x; cin >> x; if (mp.count(x)) { cout << mp[x] << n ; continue; } auto nxt = mp.lower_bound(x); auto pre = prev(nxt); if ((*pre).second == (*nxt).second) { cout << (*pre).second << n ; continue; } int m = 10100; vector<mint> dp(m); dp[0] = 1; int t = 0; for (int i = 0; i < n; i++) { vector<mint> new_dp(m); new_dp[0] = dp[0]; for (int j = 1; j < m; j++) { new_dp[j] = new_dp[j - 1] + dp[j]; if (j - c[i] - 1 >= 0) { new_dp[j] -= dp[j - c[i] - 1]; } } swap(dp, new_dp); t += x + d[i]; for (int j = 0; j < min(m, t); j++) { dp[j] = 0; } } mp[x] = accumulate(dp.begin(), dp.end(), mint(0)); cout << mp[x] << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; const int Day = 86400; const long long INF = 1e18; const int N = 4004; long long dp[N][N], t[N], d[N]; int main() { int n, k; scanf( %d%d , &n, &k); for (int(i) = 0; (i) < (n); (i)++) scanf( %d%d , &t[i], &d[i]), t[i]--; if (n == k) return printf( %d n , Day) & 0; memset(dp, INF, sizeof dp); dp[0][0] = 0; for (int(i) = (1); (i) <= (n); (i)++) for (int(j) = (0); (j) <= (k); (j)++) if (j <= i) { long long last = dp[i - 1][j]; if (last != INF) { dp[i][j] = max(last, t[i - 1]) + d[i - 1]; } if (j > 0) dp[i][j] = min(dp[i - 1][j - 1], dp[i][j]); } long long ans = 0; for (int(left) = (0); (left) <= (n); (left)++) { int right = min(k, left); long long start = dp[left][right]; long long finish = left < n ? t[left] : Day; if (start >= Day || finish > Day || start >= finish) continue; ans = max(ans, finish - start); } printf( %I64d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int limN = 1e5 + 5; vector<pair<long long, int> > adj[limN]; bool usd[limN]; long long minP[limN]; int main() { int ans = 0; int N, E1, E2; priority_queue<tuple<long long, int, int> > Q; scanf( %d%d%d , &N, &E1, &E2); for (int a, b, c; E1; E1--) { scanf( %d%d%d , &a, &b, &c); a--, b--; adj[a].push_back({c, b}); adj[b].push_back({c, a}); } for (int b, c; E2; E2--) { scanf( %d%d , &b, &c); b--; Q.push({-c, 0, b}); } Q.push({0, 1, 0}); while (!Q.empty()) { long long dst; int tip, pos; tie(dst, tip, pos) = Q.top(); Q.pop(); if (usd[pos]) { ans += 1 - tip; continue; } dst *= -1; usd[pos] = true; for (const auto &second : adj[pos]) { if (!usd[second.second] || minP[second.second] > dst + second.first) { minP[second.second] = dst + second.first; Q.push({-minP[second.second], 1, second.second}); } } } printf( %d n , ans); }
#include <bits/stdc++.h> using namespace std; int n, m; int p[30]; char A[30], ch[205]; int d[405][405]; int now[205], minv[205], minc[205]; int fan(int x) { return (x > n) ? (x - n) : (x + n); } int main() { scanf( %s , A); int tot = strlen(A); for (int i = 0; i < tot; ++i) { p[i] = A[i] == V ; } minv[0] = minc[0] = 26; for (int i = 0; i < tot; ++i) { if (p[i]) { minv[0] = i; break; } } for (int i = 0; i < tot; ++i) { if (p[i] == 0) { minc[0] = i; break; } } scanf( %d%d , &n, &m); int x, y; char chx[2], chy[2]; for (int i = 0; i < m; ++i) { scanf( %d%s%d%s , &x, chx, &y, chy); if (chx[0] == V ) x += n; if (chy[0] == V ) y += n; d[x][y] = 1; d[fan(y)][fan(x)] = 1; } for (int i = 1; i <= 2 * n; ++i) { d[i][i] = 1; } for (int k = 1; k <= 2 * n; ++k) { for (int i = 1; i <= 2 * n; ++i) { for (int j = 1; j <= 2 * n; ++j) { d[i][j] |= d[i][k] & d[k][j]; } } } for (int i = 1; i <= n; ++i) { minv[i] = minv[0]; minc[i] = minc[0]; for (int j = 1; j <= n; ++j) { if (d[i][j] && d[i][j + n]) { minc[i] = 26; } if (d[i + n][j] && d[i + n][j + n]) { minv[i] = 26; } } } scanf( %s , ch + 1); for (int i = n + 1; i >= 1; --i) { memset(now, -1, sizeof(now)); int iminv = 26, iminc = 26; if (minv[i] != 26) for (int j = ch[i] - a + 1; j < tot; ++j) { if (p[j]) { iminv = j; break; } } if (minc[i] != 26) for (int j = ch[i] - a + 1; j < tot; ++j) { if (p[j] == 0) { iminc = j; break; } } int flag = 1; for (int j = 1; j < i; ++j) { now[j] = ch[j] - a ; } for (int j = 1; j < i; ++j) { int type = p[now[j]]; if (type == 0 && minc[j] == 26) { flag = 0; break; } if (type == 1 && minv[j] == 26) { flag = 0; break; } for (int k = 1; k <= n; ++k) { if (d[j + type * n][k]) { if (now[k] != -1) { if (p[now[k]] != 0) { flag = 0; break; } } else { if (k != i) now[k] = minc[k]; else now[k] = iminc; } } if (d[j + type * n][k + n]) { if (now[k] != -1) { if (p[now[k]] != 1) { flag = 0; break; } } else { if (k != i) now[k] = minv[k]; else now[k] = iminv; } } } } for (int j = i; j <= n; ++j) { if (now[j] != -1) continue; if (j != i) { now[j] = min(minv[j], minc[j]); } else { now[j] = min(iminv, iminc); } int type = p[now[j]]; for (int k = 1; k <= n; ++k) { if (d[j + type * n][k]) { if (now[k] != -1) { if (p[now[k]] != 0) { flag = 0; break; } } else { if (k != i) now[k] = minc[k]; else now[k] = iminc; } } if (d[j + type * n][k + n]) { if (now[k] != -1) { if (p[now[k]] != 1) { flag = 0; break; } } else { if (k != i) now[k] = minv[k]; else now[k] = iminv; } } } } for (int i = 1; i <= n; ++i) { if (now[i] == 26) { flag = 0; break; } } if (flag) { for (int i = 1; i <= n; ++i) { printf( %c , now[i] + a ); } return 0; } } puts( -1 ); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int len = s.size(); bool flag = false; for (int i = 0; i < len - 1; i++) { if ((s[i] != a && s[i] != e && s[i] != i && s[i] != o && s[i] != u )) { if (s[i] == n ) continue; else { if ((s[i + 1] == a || s[i + 1] == e || s[i + 1] == i || s[i + 1] == o || s[i + 1] == u )) { continue; } else { flag = true; break; } } } } if ((s[len - 1] != a && s[len - 1] != e && s[len - 1] != i && s[len - 1] != o && s[len - 1] != u && s[len - 1] != n )) flag = true; if (!flag) { cout << YES n ; } else { cout << NO n ; } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1007; bool ans[MAXN]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); string s; cin >> s; int n = s.size(); int l = 0; for (int i = 0; i < n; ++i) { if (s[i] == a && (i == n - 1 || s[i + 1] == b )) { ans[l] = 1; ans[i] = 1; } if (s[i] == b ) l = i; } for (int i = 0; i < n; ++i) cout << ans[i] << ; cout << n ; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX_MEM = (int)1e8; int mpos = 0; char mem[MAX_MEM]; inline void *operator new(size_t n) { char *res = mem + mpos; mpos += n; return (void *)res; } inline void operator delete(void *) {} struct __io_dev { __io_dev(const bool &__fastio = false) { if (__fastio) ios_base::sync_with_stdio(false), cin.tie(nullptr); srand(time(nullptr)); if (!string( ).empty()) freopen( .in , r , stdin), freopen( .out , w , stdout); } ~__io_dev() { fprintf(stderr, %.6f ms n , 1e3 * clock() / CLOCKS_PER_SEC); } } __io(false); const long long inf = (long long)1e+9 + 7ll; const long long linf = (long long)1e+18 + 7ll; const long double eps = (long double)1e-9; const long double pi = acosl((long double)-1.0); const int alph = 26; const int maxs = 512l; static char buff[(int)2e6 + 17]; long long __p[3] = {29ll, 31ll, 33ll}; long long __mod[3] = {inf, inf + 2ll, 14881337ll}; const int maxn = (int)2e5 + 17; int n; string s; long long R, K; void print(int R, int K) { string r; int cnt = 0; for (; R > 0; R /= 10) { r.push_back(char( 0 + R % 10)); ++cnt; if (cnt % 3 == 0) r.push_back( . ); } if (r.back() == . ) r.pop_back(); if (((int)(r.size())) == 0) r.push_back( 0 ); reverse(r.begin(), r.end()); printf( %s , r.c_str()); if (K != 0) printf( .%.2d , K); } int main() { scanf( %s , buff); s = string(buff); n = ((int)(s.size())); for (int i = 0; i < n; ++i) if (!( a <= s[i] && s[i] <= z )) { int r = i; string t = ; for (; r < n && !( a <= s[r] && s[r] <= z ); ++r) t += s[r]; if (((int)(t.size())) >= 3 && t[((int)(t.size())) - 3] == . ) { K += 10 * (t[((int)(t.size())) - 2] - 0 ) + t[((int)(t.size())) - 1] - 0 ; int cursize = ((int)(t.size())); t.resize(cursize - 3); } long long cur = 0; for (auto c : t) if ( 0 <= c && c <= 9 ) cur = cur * 10ll + c - 0 ; R += cur; i = r - 1; } R += K / 100; K %= 100; print(R, K); return 0; }
#include <bits/stdc++.h> using namespace std; int t, n, p[100002]; bool id[100002]; int main() { scanf( %d , &t); while (t--) { scanf( %d , &n); for (int i = 1; i <= n; i++) { int a; scanf( %d , &a); p[a] = i; id[i] = true; } id[n + 1] = 0; bool succeed = true; int num = 1; int tp = p[1]; p[1] = 0; id[tp] = 0; for (int i = 2; i <= n; i++) { if (id[tp + 1] == false) { id[p[i]] = false; tp = p[i]; } else { if (tp + 1 != p[i]) { succeed = false; break; } else { id[tp + 1] = false; tp = tp + 1; } } } if (succeed) printf( Yes n ); else printf( No n ); } }
#include <bits/stdc++.h> using namespace std; const long long int MOD = 1e9 + 7; void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } unsigned long long int POW(unsigned long long int p, int b) { unsigned long long int ans = 1LL; while (b > 0) { if (b & 1) { ans = ans * 1LL * p; } b /= 2; p = p * 1LL * p; } return ans; } long long int power(long long int a, long long int b, long long int mod) { long long int x = 1; long long int y = a; while (b > 0) { if (b & 1) { x = x * y; x %= mod; } y = y * y; y %= mod; b /= 2; } return x; } long long int add(long long int a, long long int b, long long int m = MOD) { long long int x = a + b; while (x >= m) x -= m; while (x < 0) x += m; return x; } long long int sub(long long int a, long long int b, long long int m = MOD) { long long int x = a - b; while (x < 0) x += m; while (x >= m) x -= m; return x; } long long int mul(long long int a, long long int b, long long int m = MOD) { long long int x = a * 1ll * b; x %= m; if (x < 0) x += m; return x; } vector<char> g[1001]; int main() { string str; cin >> str; string temp = ; int idx = 0; bool ek = false; for (int i = 0; i < str.size();) { if (isdigit(str[i])) { int j = i; bool hai = false; for (j = i; j < str.size(); j++) { if (!isdigit(str[j]) and !(str[j] == . )) break; if (str[j] == . and (j + 2) < str.size() and (j + 3 >= (int)str.size() or !isdigit(str[j + 3]) ? 1 : 0)) { g[idx].push_back(str[j]); hai = true; ek = true; } if (isdigit(str[j])) g[idx].push_back(str[j]); } if (!hai) { g[idx].push_back( . ); g[idx].push_back( 0 ); g[idx].push_back( 0 ); } i = j; idx++; } else i++; } if (ek == false) { int s = 0; int cnt = 0; int carry = 0; vector<char> ans; for (int i = 0; i < idx; i++) { for (int j = 0; j < 3; j++) g[i].pop_back(); } while (true) { s = 0; bool mila = false; for (int i = 0; i < idx; i++) { if (g[i].empty()) continue; s += (g[i].back() - 0 ); g[i].pop_back(); mila = true; } if (mila == false) break; if (cnt == 3) { ans.push_back( . ); cnt = 0; } s += carry; ans.push_back(s % 10 + 0 ); carry = s / 10; cnt++; } while (carry != 0) { if (cnt == 3) { ans.push_back( . ); cnt = 0; } ans.push_back(carry % 10 + 0 ); carry /= 10; cnt++; } reverse(ans.begin(), ans.end()); for (int i = 0; i < ans.size(); i++) cout << ans[i]; } else { vector<char> ans; int s = 0; int cnt = 0; int carry = 0; for (int i = 0; i < idx; i++) { s += g[i].back() - 0 ; g[i].pop_back(); } s += carry; ans.push_back(s % 10 + 0 ); carry = s / 10; s = 0; for (int i = 0; i < idx; i++) { s += g[i].back() - 0 ; g[i].pop_back(); g[i].pop_back(); } s += carry; ans.push_back(s % 10 + 0 ); carry = s / 10; ans.push_back( . ); while (true) { s = 0; bool mila = false; for (int i = 0; i < idx; i++) { if (g[i].empty()) continue; s += (g[i].back() - 0 ); g[i].pop_back(); mila = true; } if (mila == false) break; if (cnt == 3) { ans.push_back( . ); cnt = 0; } s += carry; ans.push_back(s % 10 + 0 ); carry = s / 10; cnt++; } while (carry != 0) { if (cnt == 3) { ans.push_back( . ); cnt = 0; } ans.push_back(carry % 10 + 0 ); carry /= 10; cnt++; } int value = 0; if (ans[0] == 0 and ans[1] == 0 ) value = 3; reverse(ans.begin(), ans.end()); for (int i = 0; i < ans.size() - value; i++) cout << ans[i]; } }
#include <bits/stdc++.h> using namespace std; long long q, dp[105][100005], a[555555], b[555555], k, l, m, n, o, p; map<long long, long long> mp; vector<long long> adj[555555]; const long long mod = 1e9 + 7; void solve() { cin >> n; for (long long i = 1; i <= n; i++) { string s = ; cin >> s; long long op[26], po = 0; memset(op, 0, sizeof op); for (auto u : s) { op[u - a ]++; } for (long long i = 0; i < 26; i++) { po += (1 << i) * (op[i] % 2); } mp[po]++; a[i] = po; } for (long long i = 1; i <= n; i++) { mp[a[i]]--; k += mp[a[i]]; for (long long j = 0; j < 26; j++) { k += mp[a[i] ^ (1 << j)]; } } cout << k; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); q = 1; while (q--) { solve(); } }
#include <bits/stdc++.h> using namespace std; const bool debug = false; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); string s[8]; for (int i = 0; i < 8; i++) { cin >> s[i]; } int r = 0, col = 0; for (int i = 0; i < 8; i++) { int c = 0; for (int j = 0; j < 8; j++) { if (s[i][j] == B ) c++; } if (c == 8) r++; } for (int i = 0; i < 8; i++) { int c = 0; for (int j = 0; j < 8; j++) { if (s[j][i] == B ) c++; } if (c == 8) col++; } if (r == 0 and col == 0) { cout << 0; return 0; } if (r == 8 or col == 8) { cout << 8; return 0; } cout << r + col; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2010; const int mod = 1000000007; long long dp[maxn][maxn]; long long s[maxn][maxn], ss[maxn][maxn]; void upd(long long &t, long long k) { t += k; t %= mod; } int main() { int n, i, j, m; long long t; cin >> n >> m; for (i = 1; i <= n; i++) { for (j = 0; j + 1 < m; j++) { long long t = 1 + s[i - 1][j] * (j + 1) - ss[i - 1][j]; upd(dp[i][j], t); } s[i][0] = dp[i][0]; for (j = 1; j + 1 < m; j++) { s[i][j] = s[i][j - 1] + dp[i][j], ss[i][j] = ss[i][j - 1] + dp[i][j] * j; s[i][j] %= mod; ss[i][j] %= mod; } } long long ans = 0; for (i = 1; i <= n; i++) { for (j = 0; j + 1 < m; j++) { long long t1 = dp[i][j] - dp[i - 1][j]; long long t2 = dp[n - i + 1][j]; ans += t1 * t2 % mod * (m - j - 1); ans %= mod; } } cout << (ans + mod) % mod << endl; }
#include <bits/stdc++.h> using namespace std; long long a, b, w, x, c, ans; int main() { cin >> a >> b >> w >> x >> c; if (c <= a) { cout << 0 << endl; } else { ans = ceil(1.0 * (x * (c - a) - b) / (w - x)); cout << ans + c - a << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long n, d, b, l, r, need, ans1, ans2; long long a[500011], n1[500011], n2[500011]; int main() { scanf( %lld%lld%lld , &n, &d, &b); long long re = (n >> 1) * b; for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); for (int i = 1; i <= n; i++) n1[i] = min(a[i], re) + n1[i - 1], re -= min(a[i], re); re = (n >> 1) * b; for (int i = n; i; i--) n2[i] = min(a[i], re) + n2[i + 1], re -= min(a[i], re); for (int i = 1; i <= n / 2; i++) { r = min(r + d + 1, n); if (need + b <= n1[r]) need += b; else ans1++; } l = n + 1; need = 0; for (int i = n; i >= ((n + 1) / 2) + 1; i--) { l = max(1LL, l - d - 1); if (need + b <= n2[l]) need += b; else ans2++; } printf( %lld n , max(ans1, ans2)); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s, ans; cin >> s; ans = s; s += s; for (int k = 0; k < 10; k++) { for (int i = 0; i < s.length(); i++) { s[i]++; if (s[i] > 9 ) s[i] = 0 ; } for (int i = 0; i < n; i++) { ans = min(ans, s.substr(i, n)); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string str; cin >> str; str[0] = toupper(str[0]); cout << str; }
#include <bits/stdc++.h> using namespace std; const int N = 100010; int inf = 1e9; int mod = 1e9 + 7; const int M = 20; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; cin >> s; int n = (int)s.length(); int dp[(1 << M)]; memset(dp, 0, sizeof(dp)); int maxi = 0; for (int i = 0; i < n; i++) { int mask = 0; int cnt = 0; for (int j = i; j < n; j++) { int x = s[j] - a ; if ((mask >> x) & 1) { break; } mask ^= (1 << x); cnt++; dp[mask] = cnt; } maxi = max(maxi, cnt); } for (int i = 0; i < M; i++) { for (int j = 0; j < (1 << M); j++) { if (j & (1 << i)) { dp[j] = max(dp[j], dp[j ^ (1 << i)]); } } } for (int i = 0; i < (1 << M); i++) { int comp = (1 << M) - 1 - i; maxi = max(maxi, dp[i] + dp[comp]); } cout << maxi << n ; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxm = 2e6 + 5; struct task { int t, k, d; } tasks[maxm]; int times[110]; int main() { for (int i = 0; i < 110; ++i) { times[i] = 0; } int n, m; scanf( %d%d , &n, &m); for (int i = 0; i < m; ++i) { scanf( %d%d%d , &tasks[i].t, &tasks[i].k, &tasks[i].d); } for (int i = 0; i < m; ++i) { int num = 0, j = 1, res = 0; if (tasks[i].k > n) { printf( -1 n ); continue; } while (j <= n) { if (times[j] <= tasks[i].t) { num++; } if (num == tasks[i].k) break; j++; } j = 1; if (num == tasks[i].k) { int cnt = 0; while (j <= n) { if (times[j] <= tasks[i].t) { res += j; times[j] = tasks[i].t + tasks[i].d; cnt++; } if (cnt == tasks[i].k) break; j++; } printf( %d n , res); } else { printf( -1 n ); continue; } } }
#include <bits/stdc++.h> using namespace std; const int N = 510; int t, n, m, a, b, c; vector<pair<int, int> > g[N]; int dp[N][N], inDegree[N][N], source, ans[N][N]; bool vis[N]; void dfs(int u) { if (vis[u]) return; vis[u] = 1; for (int i = 0; i < g[u].size(); i++) { int v = g[u][i].first; int newCost = g[u][i].second + dp[source][u]; if (dp[source][v] == newCost) { inDegree[source][v]++; dfs(v); } } } int main() { scanf( %d%d , &n, &m); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { dp[i][j] = 1e9; } dp[i][i] = 0; } for (int i = 0; i < m; ++i) { scanf( %d%d%d , &a, &b, &c); g[a].push_back(make_pair(b, c)); g[b].push_back(make_pair(a, c)); dp[a][b] = min(dp[a][b], c); dp[b][a] = min(dp[b][a], c); } for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]); } } } for (source = 1; source <= n; source++) { memset(vis, 0, sizeof vis); dfs(source); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { for (int k = 1; k <= n; k++) { if (dp[i][k] + dp[k][j] == dp[i][j]) { ans[i][j] += inDegree[i][k]; } } } } for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { printf( %d , ans[i][j]); } } puts( ); return 0; }
#include <bits/stdc++.h> using namespace std; int a[200005]; bool got[200004], gave[200004]; queue<int> q; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; got[a[i]] = 1; if (a[i] != 0) { gave[i] = 1; } } for (int i = 1; i <= n; i++) { if (!got[i]) { q.push(i); } } for (int i = 1; i <= n; i++) { if (!got[i] && !gave[i]) { int x = q.front(); q.pop(); if (x == i) { q.push(x); x = q.front(); q.pop(); } a[i] = x; gave[i] = 1; got[x] = 1; } } for (int i = 1; i <= n; i++) { if (!gave[i]) { int x = q.front(); q.pop(); a[i] = x; gave[i] = 1; got[x] = 1; } } for (int i = 0; i < n; i++) { cout << a[i + 1] << ; } return 0; }
#include <bits/stdc++.h> using namespace std; #pragma GCC target( avx2 ) #pragma GCC optimization( O3 ) #pragma GCC optimization( unroll-loops ) bool check(string pattern, string text) { int j = 0; int i = 0; while (i < text.size()) { if (pattern[j] == text[i]) { j++; } i++; } return j == pattern.size(); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int d; cin >> d; while (d--) { string s, t, p; cin >> s >> t >> p; string k = s; s = s + p; map<char, long long int> m_s; map<char, long long int> m_t; for (int i = 0; i < s.size(); ++i) { m_s[s[i]]++; } for (int i = 0; i < t.size(); ++i) { m_t[t[i]]++; } bool checks = true; for (auto i : m_t) { if (m_s[i.first] < i.second) { checks = false; } } if (checks && check(k, t)) { cout << YES << n ; } else { cout << NO << n ; } } }
#include <bits/stdc++.h> using namespace std; vector<string> split(const string& s, char c) { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) v.emplace_back(x); return move(v); } void err(vector<string>::iterator it) {} template <typename T, typename... Args> void err(vector<string>::iterator it, T a, Args... args) { cerr << it->substr((*it)[0] == , it->length()) << = << a << t ; err(++it, args...); } const long double eps = 1e-9; const long long mod = 1e9 + 7; pair<long long, long long> dir[8] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {1, 1}, {-1, -1}, {-1, 1}, {1, -1}}; const long long nn = 3e5 + 5; vector<long long> g[nn]; long long par[nn]; long long sz[nn]; long long centroid[nn]; void dfs(long long v) { sz[v] = 1; for (long long to : g[v]) { dfs(to); sz[v] += sz[to]; } long long bad = -1; for (long long to : g[v]) { if (sz[to] * 2 > sz[v]) bad = to; } if (bad == -1) { centroid[v] = v; } else { centroid[v] = centroid[bad]; while (sz[centroid[v]] * 2 <= sz[v]) { centroid[v] = par[centroid[v]]; } } } void robin() { long long n, q; cin >> n >> q; for (long long i = 2; i <= n; i++) { long long v; cin >> v; par[i] = v; g[v].push_back(i); } dfs(1); while (q--) { long long v; cin >> v; cout << centroid[v] << n ; } } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long T = 1; for (long long tc = 1; tc <= T; tc++) { robin(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; int main() { ios::sync_with_stdio(false); cin.tie(0); int k; cin >> k; int sum = (1 << 18) - 1; cout << 3 3 n ; cout << sum << << (1 << 17) << << 0 << n ; cout << k << << sum << << 0 << n ; cout << 0 << << k << << k << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int arr[n + 1]; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); for (int i = n - 1; i >= 0; i--) { if (k % arr[i] == 0) { cout << (k / arr[i]) << endl; break; } } return 0; }
#include <bits/stdc++.h> using namespace std; int a[200000]; int b[200001]; int br[200001]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i <= m; i++) cin >> b[i]; int minimum = 0; for (int i = 1; i <= m; i++) minimum += b[i]; int ok = 0; for (int i = 1; i <= m; i++) ok += !b[i]; int left = 0; int ans = n + 1; for (int right = 0; right < n; right++) { br[a[right]]++; if (br[a[right]] == b[a[right]]) ok++; while (ok == m) { ans = min(ans, right - left + 1); if (br[a[left]] == b[a[left]]) ok--; br[a[left]]--; left++; } } if (ans == n + 1) cout << -1 << endl; else cout << ans - minimum << endl; return 0; }