func_code_string
stringlengths 59
71.4k
|
---|
// Codeforces Round #720 (Div. 2) // Problem link: https://codeforces.com/problemset/problem/1521/B // Problem tags: constructive algorithms math *1300 // CODE #include <iostream> #include <vector> #include <limits.h> using namespace std; int main() { int t; cin >> t; while(t--) { int n; cin >> n; vector<int> arr(n+1); int min = INT_MAX, pos = -1; for(int i = 1; i <= n; i++) { cin >> arr[i]; if(arr[i] < min) { min = arr[i]; pos = i; } } cout << n-1 << n ; int next = min+1; for(int i = pos-1; i > 0; i--) { arr[i] = next; cout << pos << << i << << min << << next << n ; next++; } next = min+1; for(int i = pos+1; i <= n; i++) { arr[i] = next; cout << pos << << i << << min << << next << n ; next++; } // cout << array: ; // for(int i=1; i<=n; i++) // cout << arr[i] << ; // cout << n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MX = (int)1e5 + 5; int test; vector<int> con[MX]; int dp[MX], sz[MX], prv[MX]; void dfs(int u) { sz[u] = 1; for (int v : con[u]) { dp[v] = dp[u] + 1; dfs(v); sz[u] += sz[v]; } } int fst[MX], st[MX], en[MX], pn; void hld(int u, int hd) { fst[u] = hd; st[u] = ++pn; int w = 0; for (int v : con[u]) if (sz[w] < sz[v]) w = v; if (w) hld(w, hd); for (int v : con[u]) if (v != w) hld(v, v); en[u] = pn; } struct Node { int a, b, clr, len; Node(int a = 0, int b = 0, int clr = 0) : a(a), b(b), clr(clr) {} Node operator+(const Node &d) const { if (b >= d.a) return Node(a, b - d.a + d.b); else return Node(a + d.a - b, d.b); } void clear() { a = len, b = 0, clr = 1; } } node[1 << 20]; void push_down(int id) { if (node[id].clr) { node[id + id].clear(); node[id + id + 1].clear(); node[id].clr = 0; } } void push_up(int id) { node[id] = node[id + id] + node[id + id + 1]; node[id].len = node[id + id].len + node[id + id + 1].len; } void build(int id, int st, int en) { if (st == en) { node[id] = Node(1, 0, 0); node[id].len = 1; return; } int mid = st + en >> 1; build(id + id, st, mid); build(id + id + 1, mid + 1, en); push_up(id); } void add(int id, int st, int en, int pos, int val) { if (pos <= st && en <= pos) { node[id].b += val; return; } push_down(id); int mid = st + en >> 1; if (pos <= mid) add(id + id, st, mid, pos, val); if (pos > mid) add(id + id + 1, mid + 1, en, pos, val); push_up(id); } Node get(int id, int st, int en, int l, int r) { if (l <= st && en <= r) { return node[id]; } push_down(id); Node res = Node(0, 0); int mid = st + en >> 1; if (l <= mid) res = res + get(id + id, st, mid, l, r); if (r > mid) res = res + get(id + id + 1, mid + 1, en, l, r); push_up(id); return res; } vector<pair<int, int> > cur; int get_val(int u, int v, int n) { int fu, fv; Node res = Node(0, 0); cur.clear(); while (true) { fu = fst[u], fv = fst[v]; if (fu == fv) break; if (dp[fu] > dp[fv]) swap(u, v), swap(fu, fv); cur.push_back(make_pair(st[fv], st[v])); v = prv[fv]; } if (st[u] > st[v]) swap(u, v); cur.push_back(make_pair(st[u], st[v])); for (int i = cur.size() - 1; i >= 0; i--) res = res + get(1, 1, n, cur[i].first, cur[i].second); return res.b; } void clear(int id, int st, int en, int l, int r) { if (l <= st && en <= r) { node[id].clear(); return; } push_down(id); int mid = st + en >> 1; if (l <= mid) clear(id + id, st, mid, l, r); if (r > mid) clear(id + id + 1, mid + 1, en, l, r); push_up(id); } int main() { ios_base::sync_with_stdio(false); int n, q; cin >> n >> q; for (int i = 2; i <= n; i++) { int u; cin >> u; con[u].push_back(i); prv[i] = u; } dp[1] = 1; dfs(1); hld(1, 1); build(1, 1, n); while (q--) { int type, v; cin >> type >> v; if (type == 1) add(1, 1, n, st[v], 1); else { int val = get_val(v, 1, n); if (type == 3) cout << (val > 0 ? black : white ) << endl; else add(1, 1, n, st[v], -val), clear(1, 1, n, st[v] + 1, en[v]); } } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { long long int t, n, i, s, a1, b1, m; cin >> t; while (t--) { cin >> n >> m; if (n < m) { cout << 0 n ; continue; } if (n == m) cout << n % 10 << n ; else { long long int a[10] = {}, b[11] = {}; for (i = 1; i <= 10; i++) { a[i - 1] = (m * i) % 10; b[i] += a[i - 1] + b[i - 1]; } a1 = n / m; b1 = a1 % 10; a1 = a1 / 10; s = a1 * b[10] + b[b1]; cout << s << n ; } } return 0; }
|
#include <bits/stdc++.h> using namespace std; int getInt(); int getNegInt(); int T, N; int get_nearest(int p, int a, vector<vector<int> >& path, unordered_set<int>& me) { if (me.find(a) != me.end()) return a; for (auto b : path[a]) { if (b == p) continue; int tar = get_nearest(a, b, path, me); if (tar != -1) return tar; } return -1; } int main(int argc, char** argv) { int a, b, k1, k2; T = getInt(); while (T--) { unordered_set<int> me, he; N = getInt(); vector<vector<int> > path(N + 1, vector<int>()); for (int i = 1; i < N; i++) { scanf( %d%d , &a, &b); path[a].push_back(b); path[b].push_back(a); } k1 = getInt(); int start; for (int i = 1; i <= k1; i++) { a = getInt(); me.insert(a); } k2 = getInt(); for (int i = 1; i <= k2; i++) { b = getInt(); start = b; he.insert(b); } int ans = -1; for (int i = 1; i <= 1; i++) { cout << B << start << endl; fflush(stdout); int index = getInt(); if (me.find(index) != me.end()) { ans = index; break; } int nearest = get_nearest(index, index, path, me); cout << A << nearest << endl; fflush(stdout); index = getInt(); if (he.find(index) != he.end()) { ans = nearest; break; } } cout << C << ans << endl; fflush(stdout); } return 0; } int getInt() { int ret = 0; char c = getchar(); while (c < 0 || c > 9 ) c = getchar(); while (c >= 0 && c <= 9 ) ret *= 10, ret += c - 0 , c = getchar(); return ret; } int getNegInt() { int ret = 0, sign = 1; char c = getchar(); while ((c < 0 && c != - ) || c > 9 ) c = getchar(); if (c == - ) sign = -1, c = getchar(); while (c >= 0 && c <= 9 ) ret *= 10, ret += c - 0 , c = getchar(); return ret * sign; }
|
#include <bits/stdc++.h> using namespace std; vector<long long> a; bool valido; vector<vector<long long>> adj; long long d; void dfs(long long u, long long p = 0) { if (!valido) return; for (long long v : adj[u]) if (v != p) dfs(v, u); if (a[u] % d == 0) { a[p]++; } else { a[u]++; if (p == 0 || a[u] % d != 0) valido = 0; } } int32_t main() { long long t; cin >> t; while (t--) { long long n; cin >> n; a.resize(n + 1); adj = vector<vector<long long>>(n + 1); for (long long i = 0; i < n - 1; i++) { long long a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } vector<long long> freq(n + 1); freq[1] = 2; for (long long i = 1; i < n - 1; i++) freq[1] = 2 * freq[1] % 998244353; for (d = 2; d <= n - 1; d++) { if ((n - 1) % d == 0) { valido = 1; fill(a.begin(), a.end(), 0); dfs(1); freq[d] = valido; } } vector<long long> res(n + 1); for (long long d = n; d >= 1; d--) { res[d] = freq[d]; for (long long mult = 2 * d; mult <= n; mult += d) { res[d] = res[d] - res[mult] + 998244353; res[d] %= 998244353; } } for (long long d = 1; d <= n; d++) { cout << res[d] << ; } cout << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { int n; cin >> n; vector<int> nums; for (long long i = 0; i < n; i++) { int num; cin >> num; nums.push_back(num); } sort(nums.begin(), nums.end()); long long c = 0; for (int i = 0; i < n; i++) { if (nums[i] == 0) { nums[i] += 1; c++; } } long long sum = 0; for (int i = 0; i < n; i++) { sum += nums[i]; } if (sum == 0) { c++; } cout << c << endl; } }
|
#include <bits/stdc++.h> using namespace std; struct node { int l, r; node() { l = r = -1; } } p[30]; int vis[30], sum[1000005]; int main() { int n, k; cin >> n >> k; string s; cin >> s; for (int i = 0; i < s.length(); i++) { int id = s[i] - A ; if (!vis[id]) { p[id].l = p[id].r = i; vis[id] = 1; } else p[id].r = i; } for (int i = 0; i < 26; i++) { if (vis[i]) { sum[p[i].l]++; sum[p[i].r + 1]--; } } int ans = sum[0]; if (ans > k) { cout << YES ; return 0; } for (int i = 1; i < n; i++) { sum[i] = sum[i - 1] + sum[i]; if (sum[i] > k) { cout << YES ; return 0; } } cout << NO ; }
|
#include <bits/stdc++.h> using namespace std; int n, q; int arr[1000010], lazy[1000010 << 2]; string str; struct data { int n4, n7, n47, n74; data() { n4 = n7 = n47 = n74 = 0; } } tree[1000010 << 2]; void build(int node, int l, int r) { if (l == r) { tree[node].n4 = (arr[l] == 4); tree[node].n7 = (arr[l] == 7); tree[node].n47 = 1; tree[node].n74 = 1; return; } int mid = (l + r) >> 1; build(node << 1, l, mid); build(node << 1 | 1, mid + 1, r); tree[node].n4 = tree[node << 1].n4 + tree[node << 1 | 1].n4; tree[node].n7 = tree[node << 1].n7 + tree[node << 1 | 1].n7; tree[node].n47 = max(max(tree[node << 1].n47 + tree[node << 1 | 1].n7, tree[node << 1].n4 + tree[node << 1 | 1].n47), tree[node << 1].n4 + tree[node << 1 | 1].n7); tree[node].n74 = max(max(tree[node << 1].n7 + tree[node << 1 | 1].n74, tree[node << 1].n74 + tree[node << 1 | 1].n4), tree[node << 1].n7 + tree[node << 1 | 1].n4); } void propagate(int node) { if (lazy[node]) { lazy[node << 1] ^= 1, lazy[node << 1 | 1] ^= 1; swap(tree[node << 1].n4, tree[node << 1].n7); swap(tree[node << 1 | 1].n4, tree[node << 1 | 1].n7); swap(tree[node << 1].n47, tree[node << 1].n74); swap(tree[node << 1 | 1].n47, tree[node << 1 | 1].n74); } lazy[node] = 0; } void update(int node, int l, int r, int i, int j) { if (r < i || l > j) return; if (i <= l && r <= j) { lazy[node] ^= 1; swap(tree[node].n4, tree[node].n7); swap(tree[node].n47, tree[node].n74); return; } int mid = (l + r) >> 1; propagate(node); update(node << 1, l, mid, i, j); update(node << 1 | 1, mid + 1, r, i, j); tree[node].n4 = tree[node << 1].n4 + tree[node << 1 | 1].n4; tree[node].n7 = tree[node << 1].n7 + tree[node << 1 | 1].n7; tree[node].n47 = max(max(tree[node << 1].n47 + tree[node << 1 | 1].n7, tree[node << 1].n4 + tree[node << 1 | 1].n47), tree[node << 1].n4 + tree[node << 1 | 1].n7); tree[node].n74 = max(max(tree[node << 1].n7 + tree[node << 1 | 1].n74, tree[node << 1].n74 + tree[node << 1 | 1].n4), tree[node << 1].n7 + tree[node << 1 | 1].n4); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> q; for (int i = 0; i < n; ++i) { char c; cin >> c; arr[i + 1] = c - 0 ; } build(1, 1, n); while (q--) { cin >> str; if (str == count ) { cout << tree[1].n47 << n ; } else { int l, r; cin >> l >> r; update(1, 1, n, l, r); } } }
|
#include <bits/stdc++.h> using namespace std; struct FenwickTree { int N; vector<int> A; FenwickTree(int N) : N(N), A(N) {} int rsq(int b) { int sum = 0; for (; b; b -= (b & (-b))) sum += A[b]; return sum; } int rsq(int a, int b) { return rsq(b) - (a == 1000000000 ? 0 : rsq(a - 1)); } void adjust(int k, int v) { for (; k < A.size(); k += k & -k) A[k] += v; } }; int n, k, a, b, q; int main() { cin >> n >> k >> a >> b >> q; FenwickTree bef(n + 5), aft(n + 5); vector<int> befa(n + 1), afta(n + 1); int i, j, k1; for (int c = 0; c < q; c++) { scanf( %d , &i); if (i == 1) { scanf( %d%d , &j, &k1); int befk = min(k1 + befa[j], b); int aftk = min(k1 + afta[j], a); bef.adjust(j, befk - befa[j]); befa[j] = befk; aft.adjust(j, aftk - afta[j]); afta[j] = aftk; } else { scanf( %d , &j); printf( %d n , bef.rsq(1, j - 1) + aft.rsq(k + j, n)); } } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int t, s, q; cin >> t >> s >> q; int ans = 0; while (s < t) { s = s * q; ans++; } cout << ans; return 0; }
|
#include <bits/stdc++.h> inline int read() { static char ch; while (ch = getchar(), ch < 0 || ch > 9 ) ; int res = ch - 48; while (ch = getchar(), ch >= 0 && ch <= 9 ) res = res * 10 + ch - 48; return res; } inline unsigned int Rand() { static unsigned int x = 998666131; return x += x << 2 | 1; } const int N = 5001, M = 1e5 + 5, T = 2e6; int n, m, Q, ans[M]; bool vis[M]; struct QueryT { int op, x, y, dir, len, lab; inline void Reset1(const int &_op, const int &_dir, const int &_x, const int &_y, const int &_len) { op = _op; dir = _dir; x = _x; y = _y; len = _len; } inline void Reset2(const int &_op, const int &_dir, const int &_x, const int &_y, const int &_lab) { op = _op; dir = _dir; x = _x; y = _y; lab = _lab; len = x + y; } inline bool operator<=(const QueryT &rhs) { if (x != rhs.x) return x < rhs.x; return op <= rhs.op; } } qry[M * 4], tmp[M * 4]; struct node { node *lc, *rc; int val, sze; unsigned int pri; inline void Update() { sze = (lc ? lc->sze : 0) + (rc ? rc->sze : 0) + 1; } } used[T], *pool = used, *unused[T], **top = unused; node *rt[5][N]; inline node *new_node() { node *res; if (top != unused) res = *--top; else res = pool++; res->lc = res->rc = NULL; return res; } inline void del_node(node *&u) { if (!u) return; del_node(u->lc); del_node(u->rc); *top++ = u; u = NULL; } inline void Zig(node *&u) { node *v = u->lc; u->lc = v->rc; v->rc = u; v->sze = u->sze; u->Update(); u = v; } inline void Zag(node *&u) { node *v = u->rc; u->rc = v->lc; v->lc = u; v->sze = u->sze; u->Update(); u = v; } inline void Insert(node *&u, const int &val) { if (!u) { u = new_node(); u->val = val; u->sze = 1; u->pri = Rand(); return; } ++u->sze; if (val <= u->val) { Insert(u->lc, val); if (u->lc->pri < u->pri) Zig(u); } else { Insert(u->rc, val); if (u->rc->pri < u->pri) Zag(u); } } inline int moreQuery(node *u, const int &val) { if (!u) return 0; if (u->val < val) return moreQuery(u->rc, val); return (u->rc ? u->rc->sze + 1 : 1) + moreQuery(u->lc, val); } inline void add(node **R, int x, const int &val) { for (; x <= n; x += x & -x) Insert(R[x], val); } inline int Query(node **R, int x, const int &val) { int res = 0; for (; x > 0; x -= x & -x) res += moreQuery(R[x], val); return res; } inline void Clear(node **R, int x) { for (; x <= n; x += x & -x) del_node(R[x]); } inline void cdqSolve(const int &l, const int &r) { if (l == r) return; int mid = l + r >> 1; cdqSolve(l, mid); cdqSolve(mid + 1, r); int ql = l, qr = mid + 1; for (int i = (l); i <= (r); ++i) { if (qr > r || (ql <= mid && qry[ql] <= qry[qr])) { tmp[i] = qry[ql++]; if (tmp[i].op == 1) add(rt[tmp[i].dir], tmp[i].y, tmp[i].len); } else { tmp[i] = qry[qr++]; if (tmp[i].op == 2) ans[tmp[i].lab] += Query(rt[tmp[i].dir], tmp[i].y, tmp[i].len); } } for (int i = (l); i <= (r); ++i) { qry[i] = tmp[i]; if (qry[i].op == 1) Clear(rt[qry[i].dir], qry[i].y); } } int main() { n = read(); Q = read(); int op, x, y, len, dir; for (int i = (1); i <= (Q); ++i) { op = read(); if (op == 1) { dir = read(), x = read(), y = read(), len = read(); if (dir == 2) y = n - y + 1; if (dir == 3) x = n - x + 1; if (dir == 4) x = n - x + 1, y = n - y + 1; len += x + y; qry[++m].Reset1(op, dir, x, y, len); } else { x = read(), y = read(); qry[++m].Reset2(op, 1, x, y, i); qry[++m].Reset2(op, 2, x, n - y + 1, i); qry[++m].Reset2(op, 3, n - x + 1, y, i); qry[++m].Reset2(op, 4, n - x + 1, n - y + 1, i); vis[i] = true; } } cdqSolve(1, m); for (int i = (1); i <= (Q); ++i) if (vis[i]) printf( %d n , ans[i]); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7, A = 1e5 + 2; bool v[20][20]; long long dp[20][1 << 20]; int n, m, start = 0; long long solve(int node, int mask) { if (~dp[node][mask]) return dp[node][mask]; long long ret = 0; if (v[node][start] && __builtin_popcount(mask) > 2) ret++; for (int i = start + 1; i < n; i++) { if (!(mask >> i & 1) && v[node][i]) ret += solve(i, mask | (1 << i)); } return dp[node][mask] = ret; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; x--, y--; v[x][y] = v[y][x] = 1; } long long ans = 0; memset(dp, -1, sizeof dp); for (int i = 0; i < n; i++) start = i, ans += solve(i, 1 << i); cout << ans / 2; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 105; int n, m, q, t, r, c, x; int R[N], C[N], ans[N][N]; pair<int, pair<int, int> > arr[N][N], temp[N]; int main() { cin >> n >> m >> q; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { arr[i][j].second.first = i; arr[i][j].second.second = j; } while (q--) { scanf( %d , &t); if (t == 1) { scanf( %d , &r); r--; for (int i = 0; i < m; i++) temp[(i - 1 + m) % m] = arr[r][i]; for (int i = 0; i < m; i++) arr[r][i] = temp[i]; } if (t == 2) { scanf( %d , &c); c--; for (int i = 0; i < n; i++) temp[(i - 1 + n) % n] = arr[i][c]; for (int i = 0; i < n; i++) arr[i][c] = temp[i]; } if (t == 3) { scanf( %d %d %d , &r, &c, &x); r--; c--; arr[r][c].first = x; } } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) ans[arr[i][j].second.first][arr[i][j].second.second] = arr[i][j].first; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) printf( %d , ans[i][j]); printf( n ); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 330, INF = 1000010000; int n, m, u, x, y, z, I, ans; int r[N], l[N], X[N], W[N], a[N], b[N], f[N], g[N][N], h[N][N]; int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %d%d , &X[i], &W[i]); b[++m] = X[i], b[++m] = X[i] + W[i]; b[++m] = X[i] - W[i]; } sort(b + 1, b + m + 1); m = unique(b + 1, b + m + 1) - b - 1; for (int i = 1; i <= n; i++) { r[i] = lower_bound(b + 1, b + m + 1, X[i] + W[i]) - b; l[i] = lower_bound(b + 1, b + m + 1, X[i] - W[i]) - b; a[X[i] = lower_bound(b + 1, b + m + 1, X[i]) - b] = i; } for (int i = 1; i <= m; i++) { int L = i; for (int j = i; j <= m; j++) { h[i][j] = L; g[i][j] = L == i ? i : g[L][j]; if (a[j]) L = min(L, l[a[j]]); } } for (int i = 1; i <= m; i++) { f[i] = f[i - 1]; for (int j = 1; j <= i - 1; j++) if (r[a[j]] >= i) { if (j < i - 1 && h[j + 1][i] < j) { int x = g[h[j + 1][i]][j]; f[i] = (f[i] > (f[x] + b[i] - b[x]) ? f[i] : (f[x] + b[i] - b[x])); } else f[i] = (f[i] > (f[j] + b[i] - b[j]) ? f[i] : (f[j] + b[i] - b[j])); } if (u = a[i - 1]) f[i] = (f[i] > (f[g[l[u]][i]] + b[i - 1] - b[g[l[u]][i]]) ? f[i] : (f[g[l[u]][i]] + b[i - 1] - b[g[l[u]][i]])); } cout << f[m] << n ; }
|
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 7; const int N = 1e5 + 10; vector<int> v[N]; void init() { int tot = 0; for (int i = 1; i < N; i++) { for (int j = i; j < N; j += i) { v[j].push_back(i); } tot = max(tot, (int)v[i].size()); } } int C(int n, int m) { int U = 1, D = 1; while (m) { U *= n++; D *= m--; } return U / D; } void solve(int n) { int ret = 0; for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) { ret++; } } cout << n << << ret << endl; } int main() { map<tuple<int, int, int>, int> mp; for (int i = 1; i < 8; i++) { if (((i >> 0) & 1) == 0) continue; for (int j = 1; j < 8; j++) { if (((j >> 1) & 1) == 0) continue; for (int k = 1; k < 8; k++) { if (((k >> 2) & 1) == 0) continue; int a = i, b = j, c = k; if (a > b) swap(a, b); if (b > c) swap(b, c); if (a > b) swap(a, b); mp[{a, b, c}] = 1; } } } init(); int ncase; scanf( %d , &ncase); while (ncase--) { map<int, int> mc; int a, b, c; scanf( %d%d%d , &a, &b, &c); vector<int> v1, v2; merge(v[a].begin(), v[a].end(), v[b].begin(), v[b].end(), back_inserter(v1)); merge(v[c].begin(), v[c].end(), v1.begin(), v1.end(), back_inserter(v2)); v2.erase(unique(v2.begin(), v2.end()), v2.end()); int sz[8]; memset((sz), (0), sizeof(sz)); for (auto &x : v2) { int f = 0; if (a % x == 0) f |= 1; if (b % x == 0) f |= 2; if (c % x == 0) f |= 4; sz[f]++; } int ans = 0; for (auto &t : mp) { int a, b, c; tie(a, b, c) = t.first; if (a == c) ans += C(sz[a], 3); else if (a == b) ans += C(sz[a], 2) * sz[c]; else if (b == c) ans += C(sz[b], 2) * sz[a]; else ans += sz[a] * sz[b] * sz[c]; } printf( %d n , ans); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MAX = 2e5 + 5; int n, a[MAX], bk[3]; int ans[MAX]; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i], bk[a[i]]++; sort(a + 1, a + n + 1, greater<int>()); int cur = 0, top = 0, tar; for (int i = 1; i <= n; i++) { cur += a[i]; bk[a[i]]--; ans[++top] = a[i]; if (cur == 2) { tar = i; break; } } if (bk[1] > 0) bk[1]--, cur += 1, ans[++top] = 1, n--; for (int i = tar + 1; i <= n; i++) { cur += a[i]; bk[a[i]]--; ans[++top] = a[i]; } for (int i = 1; i <= top; i++) printf( %d%c , ans[i], i == top ? n : ); return 0; }
|
#include <bits/stdc++.h> using namespace std; template <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << ( << p.first << , << p.second << ) ; return os; } template <class T> ostream& operator<<(ostream& os, const vector<T>& v) { os << { ; for (int i = 0; i < (int)v.size(); i++) { if (i) os << , ; os << v[i]; } os << } ; return os; } template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << : << arg1 << std::endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, , ); cerr.write(names, comma - names) << : << arg1 << | ; __f(comma + 1, args...); } const int mod = 1e9 + 7; const int N = 2005; inline int add(int x, int y) { x += y; if (x >= mod) x -= mod; return x; } inline int sub(int x, int y) { x -= y; if (x < 0) x += mod; return x; } inline int mul(int x, int y) { return (x * 1ll * y) % mod; } inline int powr(int a, long long b) { int x = 1 % mod; while (b) { if (b & 1) x = mul(x, a); a = mul(a, a); b >>= 1; } return x; } inline int inv(int a) { return powr(a, mod - 2); } char s[N][N]; int dp[2][N][N], dp2[2][N][N]; int num[2][N][N]; int main() { int n = 2000, m = 2000; scanf( %d , &(n)), scanf( %d , &(m)); for (int i = 1; i <= n; i++) { scanf( %s , s[i] + 1); } for (int i = n; i >= 1; i--) { for (int j = m; j >= 1; j--) { num[0][i][j] = num[0][i][j + 1] + (s[i][j] == R ); num[1][i][j] = num[1][i + 1][j] + (s[i][j] == R ); if (i == n) dp[0][i][j] = (num[0][i][j] == 0); if (j == m) dp[1][i][j] = (num[1][i][j] == 0); int mxj = m - num[0][i][j]; int mxi = n - num[1][i][j]; if (i != n) dp[0][i][j] = sub(dp2[1][i + 1][j], dp2[1][i + 1][mxj + 1]); if (j != m) dp[1][i][j] = sub(dp2[0][i][j + 1], dp2[0][mxi + 1][j + 1]); dp2[0][i][j] = add(dp[0][i][j], dp2[0][i + 1][j]); dp2[1][i][j] = add(dp[1][i][j], dp2[1][i][j + 1]); } } assert(dp[0][1][1] == dp[1][1][1]); printf( %d n , dp[0][1][1]); }
|
#include <bits/stdc++.h> using namespace std; int n, m; char mat[100][100]; char c; vector<int> us; int main() { cin >> n >> m; for (int i = 0; i < n; i++) cin >> mat[i]; for (int i = 0; i < n; i++) { us.clear(); for (int j = 0; j < m; j++) { if (mat[i][j] == # ) us.push_back(j); } if (us.size() > 0) { for (int j = 0; j < n; j++) { c = mat[j][us[0]]; for (int k = 1; k < (int)us.size(); k++) { if (mat[j][us[k]] != c) { cout << No ; return 0; } } } } } for (int i = 0; i < m; i++) { us.clear(); for (int j = 0; j < n; j++) { if (mat[j][i] == # ) us.push_back(j); } if (us.size() > 0) { for (int j = 0; j < m; j++) { c = mat[us[0]][j]; for (int k = 1; k < (int)us.size(); k++) { if (mat[us[k]][j] != c) { cout << No ; return 0; } } } } } cout << Yes ; return 0; }
|
#include <bits/stdc++.h> using namespace std; priority_queue<long long int> dif; vector<long long int> vec; long long int a[1000000], b[1000000]; int main() { long long int x, d, contador = 0, i; long long int pot, exp; cin >> x >> d; while (x > 0) { x++; pot = 1; exp = 0; while (pot <= x) { pot = pot * 2; exp++; } for (i = 0; i < exp - 1; i++) { vec.push_back(d * contador + 1); } contador++; x = x - pot / 2; } cout << vec.size() << n ; for (i = 0; i < vec.size(); i++) cout << vec[i] << ; }
|
#include <bits/stdc++.h> using namespace std; const long long pw10[] = {1, 10, 100, 1000, 10000, 100000}; long long K, F[7], Q, dp[7][2000000]; signed main() { cin >> K; for (long long i = 1; i <= 6; ++i) cin >> F[i]; memset(dp, -0x3f, sizeof(dp)); dp[0][0] = 0; for (long long i = 1; i <= 6; ++i) { for (long long j = 0; j < 3 * pw10[i - 1]; ++j) { deque<pair<long long, long long> > q; for (long long k = j, cnt = 0; k < 1e6; k += 3 * pw10[i - 1], ++cnt) { long long val = dp[i - 1][k] - cnt * F[i]; while (q.size() && q.back().first <= val) q.pop_back(); q.push_back(make_pair(val, cnt)); if (cnt - q.front().second > 3 * K) q.pop_front(); dp[i][k] = max(dp[i][k], cnt * F[i] + q.front().first); long long pos = 0; while (cnt - q[pos].second > 3 * (K - 1)) ++pos; long long v = cnt * F[i] + q[pos].first; for (long long t = 0; t < 10; ++t) dp[i][k + t * pw10[i - 1]] = max(dp[i][k + t * pw10[i - 1]], v); } } } for (cin >> Q; Q; --Q) { long long x; cin >> x; cout << max(0ll, dp[6][x]) << n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long maxn = 500000 + 5; long long a[maxn], tmp[maxn]; multiset<long long> save; vector<pair<long long, long long> > b; int main() { long long total = 0; long long n; scanf( %lld , &n); for (long long i = 0; i < n; i++) scanf( %lld , a + i), total += a[i]; sort(a, a + n, greater<long long>()); for (long long i = 0, cnt = 1; i < n; i++, cnt++) if (i == n - 1 || a[i] != a[i + 1]) b.push_back(make_pair(a[i], cnt)), cnt = 0; for (long long i = 0, sizesum = 0; i < (long long)b.size(); i++) { long long num = b[i].first, nowsize = b[i].second; long long save_size = min(sizesum, sizesum + nowsize >> 1); long long cnt = max(0LL, save_size - nowsize); for (long long j = save_size - 1; j >= cnt; j--) if (j < (long long)save.size()) tmp[j] = *save.begin(), save.erase(save.begin()); else tmp[j] = 0; for (long long j = cnt, k = sizesum - cnt; j < k && j < save_size; j++) if (tmp[j] < num) tmp[j] = num; else if (--k < save_size) tmp[k] = max(0LL, 2LL * num - tmp[j]); save.insert(tmp + cnt, tmp + save_size); sizesum += nowsize; } for (auto it = save.begin(); it != save.end(); it++) total -= (*it); printf( %lld n , total); }
|
#include <bits/stdc++.h> using namespace std; long long int b[101]; int main() { long long int n; cin >> n; long long int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; b[a[i]]++; } long long int ans = 0; for (int i = 1; i < 101; i++) { if (b[i] >= ans) { ans = b[i]; } } cout << ans << endl; return 0; }
|
#include <stdio.h> #include <vector> #include <algorithm> #include <set> #define pii pair<int,int> #define ll long long using namespace std; vector<int> edge[200001]; vector<pii > pt[200001]; int depth[200001]; ll d[200001]; int a[200001],v[200001]; void dfs(int x) { for (auto nxt : edge[x]) { depth[nxt] = depth[x] + 1; dfs(nxt); } } int main() { int t; for (scanf( %d , &t); t--;) { int i, n; for (scanf( %d , &n), i = 1; i <= n; ++i) { edge[i].clear(); pt[i].clear(); d[i] = 0; } for (i = 2; i <= n; ++i) { scanf( %d ,v+i); edge[v[i]].push_back(i); } depth[1] = 1; dfs(1); for (i = 2; i <= n; ++i) { scanf( %d ,a+i); pt[depth[i]].push_back({a[i],i}); } pt[1].push_back({0,1}); for (i = 1; i <= n; ++i) sort(pt[i].begin(), pt[i].end()); int lev = 2; while (pt[lev].size()) { multiset < ll > l, r; for (auto p : pt[lev]) { int x = p.second; r.insert(d[v[x]]+a[x]); } for (auto p : pt[lev]) { int x = p.second; auto it = r.lower_bound(d[v[x]]+a[x]); r.erase(it); if (r.size()) { auto mx = prev(r.end()); d[x] = max(d[x], *mx - a[x]); int rightmost = pt[lev].back().first; d[x] = max(d[x], d[v[x]] + rightmost - a[x]); } if (l.size()) { auto mx = prev(l.end()); d[x] = max(d[x], a[x] + *mx); int leftmost = pt[lev][0].first; d[x] = max(d[x], d[v[x]] + a[x] - leftmost); } l.insert({d[v[x]]-a[x]}); } ++lev; } ll ans = 0; for (i = 1; i <= n; ++i) { if (ans < d[i]) ans = d[i]; } printf( %I64d n ,ans); } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, res1, res2; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; res1 = a[0] ^ a[1]; vector<int> st; for (int i = 0; i < n; i++) { while (!st.empty() && st.back() < a[i]) st.pop_back(); st.push_back(a[i]); if (st.size() >= 2) res1 = max(res1, st[st.size() - 1] ^ st[st.size() - 2]); } res2 = a[n - 1] ^ a[n - 2]; vector<int> st1; for (int i = n - 1; i >= 0; i--) { while (!st1.empty() && st1.back() < a[i]) st1.pop_back(); st1.push_back(a[i]); if (st1.size() >= 2) res2 = max(res2, st1[st1.size() - 1] ^ st1[st1.size() - 2]); } cout << max(res1, res2); }
|
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) #pragma GCC optimize( unroll-loops ) #pragma GCC target( sse2 ) using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cout << name << : << arg1 << n ; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, , ); cout.write(names, comma - names) << : << arg1 << | ; __f(comma + 1, args...); } void solution() { int n; cin >> n; vector<long long> arr(n); map<long long, long long> cnt; map<long long, long long> freq; for (int i = 0; i < n; i++) cin >> arr[i]; long long ans = 1; for (int i = 1; i < n + 1; i++) { cnt[arr[i - 1]]++; freq[cnt[arr[i - 1]]]++; long long x = cnt[arr[i - 1]]; long long y = freq[cnt[arr[i - 1]]]; if (x * y == i && i < n) ans = i + 1; else if (x * y == i - 1) ans = i; } cout << ans << n ; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; for (int i = 1; i < t + 1; i++) { solution(); } }
|
#include <bits/stdc++.h> using namespace std; void solve() { int N, K; cin >> N >> K; set<int> s; for (int i = 0; i < N; i++) { int a; cin >> a; s.insert(a); } if (s.size() > K) { cout << -1 << endl; return; } cout << N * K << endl; for (int i = 0; i < N; i++) { for (int b : s) cout << b << ; for (int j = 0; j < K - (int)s.size(); j++) cout << 1 << ; } cout << endl; } int main() { int t; cin >> t; while (t--) solve(); }
|
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; int a[100005]; int l[1000], r[1000]; vector<int> v[100000]; int main() { int n, m; scanf( %d%d , &n, &m); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); for (int i = 1; i <= m; i++) { int s, t; scanf( %d%d , &s, &t); l[i] = s, r[i] = t; } int ans = -100000000, anss = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { int mi = a[i], ma = a[j]; for (int k = 1; k <= m; k++) { if (l[k] <= i && i <= r[k] && (l[k] > j || r[k] < j)) mi--, v[(i - 1) * 300 + j].push_back(k); } if (ans < ma - mi) { ans = ma - mi; anss = (i - 1) * 300 + j; } } } printf( %d n , ans); printf( %d n , v[anss].size()); for (int i = 0; i < v[anss].size(); i++) printf( %d , v[anss][i]); printf( n ); return 0; }
|
#include <bits/stdc++.h> using namespace std; long long power(long long b, long long e, long long m) { long long p = 1; while (e > 0) { if (e & 1) p = (p * b) % m; e = e >> 1; b = (b * b) % m; } return p; } long long cnt[16] = {1, 0, 0, 0, 1, 0, 1, 0, 2, 1, 1, 2, 0, 1, 0, 0}; long long getAns(long long n) { long long ans = 0; if (n == 0) return cnt[0]; while (n) { ans += cnt[n % 16]; n /= 16; } return ans; } int main() { long long n, i = 0, j, m, h, h1, h2, q, k, l, r, ans; scanf( %lld , &n); printf( %lld n , getAns(n)); return 0; }
|
#include <bits/stdc++.h> using namespace std; int sum[705][705], a[500100]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int q, t, x, y; memset(sum, 0, sizeof(sum)); cin >> q; while (q--) { cin >> t >> x >> y; if (t == 1) { a[x] += y; for (int i = 1; i < 705; ++i) { sum[i][x % i] += y; } } else { if (x < 705) { cout << sum[x][y] << endl; } else { int ans = 0; for (int i = y; i < 500100; i += x) ans += a[i]; cout << ans << endl; } } } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int N = 300001; int dp[82][82][82][82], n, k, m, u, v, c; vector<pair<int, int> > g[83]; int calc(int idx, int l, int r, int rem) { if (rem == 0) return 0; if (l > r) return 1e9; int &ret = dp[idx][l][r][rem]; if (ret != -1) return ret; ret = 1e9; for (int i = 0; i < g[idx].size(); ++i) { int v = g[idx][i].first; int cost = g[idx][i].second; if (v < l) continue; if (v > r) continue; ret = min(ret, cost + calc(v, v + 1, r, rem - 1)); ret = min(ret, cost + calc(v, l, v - 1, rem - 1)); } return ret; } int main() { memset(dp, -1, sizeof dp); scanf( %d%d , &n, &k); scanf( %d , &m); for (int i = 0; i < m; ++i) { scanf( %d%d%d , &u, &v, &c); if (u == v) continue; g[u].push_back(make_pair(v, c)); } int ret = 1e9; for (int i = 1; i <= n; ++i) { ret = min(ret, calc(i, i + 1, n, k - 1)); ret = min(ret, calc(i, 1, i - 1, k - 1)); } if (ret == 1e9) ret = -1; printf( %d n , ret); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<pair<string, int>> v(n); for (int i = 0; i < n; i++) { string s; cin >> s; v[i].first = s; v[i].second = i + 1; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (j % 2 == 1) { v[i].first[j] = (char)( Z - v[i].first[j]); } } } sort(v.begin(), v.end()); for (int i = 0; i < n; i++) { cout << v[i].second << ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; long long FastExp(long long a, long long b, long long c) { if (b == 1) return a; long long ans = FastExp(a, b / 2, c); if (b % 2 == 0) return ans % c * ans % c; if (b % 2 != 0) return ans % c * ans % c * a % c; } int main() { int n; cin >> n; int a[n + 5]; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { if (i == a[a[a[i]]]) { cout << YES << endl; return 0; } } cout << NO << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int mas[15] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}; int m, n; bool ans = false; cin >> n >> m; for (int i = 0; i < 15; i++) { if (mas[i] == n) { if (mas[i + 1] == m) { ans = true; break; } } } if (ans) cout << YES << endl; else cout << NO << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std::chrono; using namespace std; const int mod = 1e9 + 7; string s; int op[501], cl[501]; int sop[501], scl[501]; int n; void f() { memset(op, 0, sizeof(op)); memset(cl, 0, sizeof(cl)); if (s[0] == ( ) op[0]++; else cl[0]++; memset(sop, 0, sizeof(sop)); memset(scl, 0, sizeof(scl)); if (s[n - 1] == ( ) sop[n - 1]++; else scl[n - 1]++; for (int i = n - 2; i >= 0; --i) { if (s[i] == ) ) { sop[i] = sop[i + 1]; scl[i] = scl[i + 1] + 1; } else { if (scl[i + 1]) { sop[i] = sop[i + 1]; scl[i] = scl[i + 1] - 1; } else { sop[i] = sop[i + 1] + 1; scl[i] = scl[i + 1]; } } } for (int i = 1; i < n; ++i) { if (s[i] == ( ) { op[i] = op[i - 1] + 1; cl[i] = cl[i - 1]; } else { if (op[i - 1]) { op[i] = op[i - 1] - 1; cl[i] = cl[i - 1]; } else { op[i] = op[i - 1]; cl[i] = cl[i - 1] + 1; } } } } void solve() { cin >> n; cin >> s; int x1 = 1, x2 = 1, mx = 0, c = 0; for (int i = 0; i < n; ++i) { for (int j = i; j < n; ++j) { int c = 0; swap(s[i], s[j]); f(); for (int k = 0; k < n; ++k) { int open; open = 0; if (k + 1 < n) open = sop[k + 1]; if (cl[k] == open && op[k] == 0) { if (k + 1 < n) { if (scl[k + 1] == 0) c++; } else c++; } } swap(s[i], s[j]); if (mx < c) { mx = c; x1 = i + 1; x2 = j + 1; } } } cout << mx << endl; cout << x1 << << x2; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; t = 1; while (t--) solve(); }
|
#include <bits/stdc++.h> using namespace std; int n; int a[100100]; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); set<int> s; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) s.insert(a[i] - a[i - 1]); if (s.size() == 1) cout << a[n - 1] + *s.begin() << endl; else cout << a[n - 1] << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; vector<int> g[500000]; bool visited[500000]; int n; unordered_map<int, int> m, p; vector<pair<int, int> > v; void check(int u) { visited[u] = true; for (auto v : g[u]) { if (!visited[v]) check(v); } } int main() { cin >> n; set<int> s; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; m[a] = b; v.push_back({a, 0}); v.push_back({b, 1}); p[a] = i; p[b] = i; } sort(v.begin(), v.end()); int count = 0; for (int i = 0; i < v.size(); i++) { if (v[i].second == 0) { for (auto ele : s) { if (ele < m[v[i].first]) { count++; g[p[v[i].first]].push_back(p[ele]); g[p[ele]].push_back(p[v[i].first]); } else break; } if (count > n - 1) { break; } s.insert(m[v[i].first]); } else { s.erase(v[i].first); } } if (count == n - 1) { check(0); int i; for (i = 0; i < n; i++) { if (!visited[i]) break; } if (i == n) cout << YES << endl; else cout << NO << endl; } else cout << NO << endl; }
|
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; struct ppi { long long max; int id; int w; } pp1; struct pi { int le; int ri; int max; int id; } pp[maxn << 2]; void build(int tot, int le, int ri) { pp[tot].le = le; pp[tot].ri = ri; pp[tot].max = -1000000001; if (le == ri) return; build(2 * tot, le, (le + ri) / 2); build(2 * tot + 1, (le + ri) / 2 + 1, ri); } void merg(int tot, int p, int k, int id) { if (pp[tot].le == pp[tot].ri) { if (pp[tot].max < k) { pp[tot].max = k; pp[tot].id = id; } return; } int mid = (pp[tot].le + pp[tot].ri) / 2; if (p <= mid) { merg(2 * tot, p, k, id); } else merg(2 * tot + 1, p, k, id); if (pp[2 * tot].max > pp[2 * tot + 1].max) { pp[tot].max = pp[2 * tot].max; pp[tot].id = pp[2 * tot].id; } else { pp[tot].max = pp[2 * tot + 1].max; pp[tot].id = pp[2 * tot + 1].id; } } ppi query(int tot, int l, int r) { if (pp[tot].le >= l && pp[tot].ri <= r) { pp1.max = pp[tot].max; pp1.id = pp[tot].id; return pp1; } int mid; mid = (pp[tot].le + pp[tot].ri) / 2; ppi pp2; pp2.max = -1000000001; if (l <= mid) { pp1 = query(2 * tot, l, r); if (pp1.max > pp2.max) { pp2.max = pp1.max; pp2.id = pp1.id; } } if (r > mid) { pp1 = query(2 * tot + 1, l, r); if (pp1.max > pp2.max) { pp2.max = pp1.max; pp2.id = pp1.id; } } return pp2; } struct pppi { int l; int r; int c; int id; int ii; } pp3[maxn], pp4[maxn], pp5[maxn << 1]; int a[maxn]; int cmp(pppi a, pppi b) { if (a.l != b.l) return a.l < b.l; return a.id > b.id; } int cmp1(pppi a, pppi b) { if (a.r != b.r) return a.r < b.r; return a.id < b.id; } int cmp2(pppi a, pppi b) { if (a.r != b.r) return a.r < b.r; return a.id > b.id; } int main() { int i, n, m; cin >> n >> m; int tot = 1; ppi pp6; pp6.max = -10000; for (i = 1; i <= n; i++) { scanf( %d%d , &pp3[i].l, &pp3[i].r); a[i] = pp3[i].r; pp3[i].id = 1; pp3[i].ii = i; pp5[tot] = pp3[i]; tot++; } sort(a + 1, a + 1 + n); for (i = 1; i <= m; i++) { scanf( %d%d%d , &pp4[i].l, &pp4[i].r, &pp4[i].c); pp4[i].id = -1; pp4[i].ii = i; pp5[tot] = pp4[i]; tot++; } tot--; build(1, 1, n); sort(pp5 + 1, pp5 + 1 + tot, cmp); for (i = 1; i <= tot; i++) { if (pp5[i].id == 1) { int p = lower_bound(a + 1, a + 1 + n, pp5[i].r) - a; merg(1, p, pp5[i].r, pp5[i].ii); } else { int p = lower_bound(a + 1, a + 1 + n, pp5[i].l) - a; ppi pp7; if (p >= 1 && p <= n) pp7 = query(1, p, n); else pp7.max = 0; if ((min(pp7.max, (long long)pp5[i].r) - pp5[i].l) * pp5[i].c > pp6.max) { pp6.max = (min(pp7.max, (long long)pp5[i].r) - pp5[i].l) * pp5[i].c; pp6.id = pp7.id; pp6.w = pp5[i].ii; } } } for (i = 1; i <= n; i++) { a[i] = pp3[i].l; } sort(a + 1, a + 1 + n); build(1, 1, n); sort(pp5 + 1, pp5 + tot + 1, cmp1); for (i = tot; i >= 1; i--) { if (pp5[i].id == 1) { int p = lower_bound(a + 1, a + 1 + n, pp5[i].l) - a; merg(1, p, -pp5[i].l, pp5[i].ii); } else { int p = lower_bound(a + 1, a + 1 + n, pp5[i].r) - a; if (p > n) p--; if (p >= 1 && p <= n && a[p] > pp5[i].r) p--; ppi pp7; if (p >= 1 && p <= n) { pp7 = query(1, 1, p); pp7.max = -pp7.max; if ((pp5[i].r - max((long long)pp5[i].l, pp7.max)) * pp5[i].c > pp6.max) { pp6.max = (pp5[i].r - max((long long)pp5[i].l, pp7.max)) * pp5[i].c; pp6.id = pp7.id; pp6.w = pp5[i].ii; } } } } sort(pp5 + 1, pp5 + 1 + n, cmp2); build(1, 1, n); for (i = 1; i <= tot; i++) { if (pp5[i].id == 1) { int p = lower_bound(a + 1, a + 1 + n, pp5[i].l) - a; merg(1, p, pp5[i].r - pp5[i].l, pp5[i].ii); } else { int p = lower_bound(a + 1, a + 1 + n, pp5[i].l) - a; if (p >= 1 && p <= n) { ppi pp7 = query(1, p, n); if (pp7.max * pp5[i].c > pp6.max) { pp6.max = pp7.max * pp5[i].c; pp6.id = pp7.id; pp6.w = pp5[i].ii; } } } } if (pp6.max >= 0) cout << pp6.max << endl; else cout << 0 << endl; if (pp6.max > 0) cout << pp6.id << << pp6.w; }
|
#include <bits/stdc++.h> using namespace std; int a[105]; int main() { int n, k; cin >> n >> k; int j = 1000; for (int i = 0; i < n; i++) { cin >> a[i]; if (k % a[i] == 0) { j = min(k / a[i], j); } } cout << j; }
|
#include <bits/stdc++.h> using namespace std; const int MAX = 100010, TOUR = 1 << 17; struct tour { int val, broj, prop; tour(int val, int broj, int prop) : val(val), broj(broj), prop(prop) {} tour() {} } t[2 * TOUR]; struct event { int t, l, r, val; event(int t, int l, int r, int val) : t(t), l(l), r(r), val(val) {} event() {} }; bool operator<(const event &a, const event &b) { return a.t < b.t; } vector<int> V[MAX]; vector<event> E; int disc[MAX], fin[MAX]; int rje[MAX]; int vr = 0; void Propagiraj(int x) { for (int i = 2 * x; i <= 2 * x + 1; i++) { t[i].prop += t[x].prop; t[i].val += t[x].prop; } t[x].prop = 0; } tour Merge(tour l, tour r) { if (l.val < r.val) return tour(l.val, l.broj, 0); if (r.val < l.val) return tour(r.val, r.broj, 0); return tour(l.val, l.broj + r.broj, 0); } void Stavi(int pos, int lo, int hi, int begin, int end, int val) { if (lo >= end || hi <= begin) return; if (lo >= begin && hi <= end) { t[pos].val += val; t[pos].prop += val; return; } Propagiraj(pos); Stavi(2 * pos + 0, lo, (lo + hi) / 2, begin, end, val); Stavi(2 * pos + 1, (lo + hi) / 2, hi, begin, end, val); t[pos] = Merge(t[2 * pos], t[2 * pos + 1]); } void Dfs(int node, int prosli) { disc[node] = vr++; for (int i = 0; i < (int)V[node].size(); i++) { int nn = V[node][i]; if (nn != prosli) Dfs(nn, node); } fin[node] = vr; } void Init(int n) { for (int i = 0; i < n; i++) t[i + TOUR] = tour(0, 1, 0); for (int i = n; i < TOUR; i++) t[i + TOUR] = tour(1e9, 0, 0); for (int i = TOUR - 1; i; i--) t[i] = Merge(t[2 * i], t[2 * i + 1]); } int main() { int n, brq; scanf( %d%d , &n, &brq); Init(n); for (int i = 0; i < n - 1; i++) { int a, b; scanf( %d%d , &a, &b); a--; b--; V[a].push_back(b); V[b].push_back(a); } Dfs(0, -1); for (int i = 0; i < brq; i++) { int a, b; scanf( %d%d , &a, &b); a--; b--; E.push_back(event(disc[a], disc[a], fin[a], 1)); E.push_back(event(fin[a], disc[a], fin[a], -1)); E.push_back(event(disc[b], disc[a], fin[a], 1)); E.push_back(event(fin[b], disc[a], fin[a], -1)); E.push_back(event(disc[a], disc[b], fin[b], 1)); E.push_back(event(fin[a], disc[b], fin[b], -1)); E.push_back(event(disc[b], disc[b], fin[b], 1)); E.push_back(event(fin[b], disc[b], fin[b], -1)); } sort(E.begin(), E.end()); for (int i = 0, ind = 0; i < n; i++) { for (; ind < (int)E.size() && E[ind].t == i; ind++) { event ev = E[ind]; Stavi(1, 0, TOUR, ev.l, ev.r, ev.val); } if (t[1].val) rje[i] = n - 1; else rje[i] = max(0, n - t[1].broj - 1); } for (int i = 0; i < n; i++) printf( %d , rje[disc[i]]); printf( n ); return 0; }
|
#include <bits/stdc++.h> using namespace std; constexpr long long mod = 1000000007; const long long INF = mod * mod; const long double eps = 1e-12; const long double pi = acosl(-1.0); long long mod_pow(long long x, long long n, long long m = mod) { long long res = 1; while (n) { if (n & 1) res = res * x % m; x = x * x % m; n >>= 1; } return res; } struct modint { long long n; modint() : n(0) { ; } modint(long long m) : n(m) { if (n >= mod) n %= mod; else if (n < 0) n = (n % mod + mod) % mod; } operator int() { return n; } }; bool operator==(modint a, modint b) { return a.n == b.n; } modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod) a.n -= mod; return a; } modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0) a.n += mod; return a; } modint operator*=(modint& a, modint b) { a.n = ((long long)a.n * b.n) % mod; return a; } modint operator+(modint a, modint b) { return a += b; } modint operator-(modint a, modint b) { return a -= b; } modint operator*(modint a, modint b) { return a *= b; } modint operator^(modint a, long long n) { if (n == 0) return modint(1); modint res = (a * a) ^ (n / 2); if (n % 2) res = res * a; return res; } long long inv(long long a, long long p) { return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p); } modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); } int max_n; const int mn = 100000; struct edge { int to, cap; long long cost; int rev; }; vector<edge> G[mn]; pair<int, int> par[mn]; long long dist[mn]; void add_edge(int from, int to, int cap, long long cost) { G[from].push_back({to, cap, cost, (int)G[to].size()}); G[to].push_back({from, 0, -cost, (int)G[from].size() - 1}); max_n = max({max_n, from + 1, to + 1}); } void add_edge2(int from, int to, int cap, long long cost) { G[from].push_back({to, cap, cost, -1}); max_n = max({max_n, from + 1, to + 1}); } pair<long long, long long> minimum_road(int s, int t) { fill(par, par + max_n, pair<int, int>{-1, -1}); fill(dist, dist + max_n, INF); dist[s] = 0; priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> q; q.push({0, s}); while (!q.empty()) { pair<long long, long long> p = q.top(); q.pop(); int id = p.second; if (id == t) continue; if (p.first > dist[id]) continue; for (int j = 0; j < G[id].size(); j++) { if (G[id][j].cap > 0) { int to = G[id][j].to; long long nd = p.first + G[id][j].cost; if (nd < dist[to]) { dist[to] = nd; par[to] = {id, j}; q.push({dist[to], to}); } } } } int cur = t; int f = mod; while (cur != s) { int p = par[cur].first, j = par[cur].second; if (p < 0) return {-1, -1}; f = min(f, G[p][j].cap); cur = p; } cur = t; while (cur != s) { int p = par[cur].first, j = par[cur].second; if (p < 0) return {-1, -1}; G[p][j].cap -= f; if (G[p][j].rev >= 0) { G[cur][G[p][j].rev].cap += f; } cur = p; } return {dist[t], f}; } long long minimum_cost_flow(int s, int t, int k = mod) { long long ret = 0; for (int i = 0; i < k; i++) { pair<long long, long long> z = minimum_road(s, t); if (z.first < 0) return ret; if (k - i <= z.second) { ret += z.first * (k - i); break; } i += z.second - 1; ret += z.first * z.second; } return ret; } void solve() { int n, m; cin >> n >> m; vector<int> d(n + 2 * m); for (int i = 0; i < m; i++) { int a, b, c, f; cin >> a >> b >> c >> f; a--; b--; int le = n + 2 * i; int ri = n + 2 * i + 1; add_edge(a, le, mod, 1); add_edge(le, a, f, 1); add_edge(ri, b, mod, 1); add_edge(b, ri, f, 1); add_edge(le, ri, c, 0); add_edge(le, ri, mod, 2); d[a] += -f; d[le] += f; d[ri] += -f; d[b] += f; } int sum = 0; int s = n + 2 * m; int t = s + 1; int sum2 = 0; int S = t + 1; int T = S + 1; for (int i = 0; i < n + 2 * m; i++) { if (d[i] > 0) { sum += abs(d[i]); add_edge(s, i, d[i], 0); } else if (d[i] < 0) { sum2 += abs(d[i]); add_edge(i, t, -d[i], 0); } } max_n = T + 1; add_edge(n - 1, 0, mod, 0); int ans = 0; ans += minimum_cost_flow(s, t, sum); cout << ans / 2 << n ; } signed main() { ios::sync_with_stdio(false); cin.tie(0); solve(); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 1000000 + 10; int n, m, k; int a[N], b[N]; char s[N], t[N]; int p[22]; int mn[1 << 20], mx[1 << 20]; int getint(char *str) { char news[22]; for (int i = 1; i <= k; i++) { news[p[i]] = str[i]; } int ans = 0; for (int i = 1; i <= k; i++) { ans = ans << 1 | (news[i] == 1 ); } return ans; } void updmx(int &x, int y) { x = max(x, y); } void updmn(int &x, int y) { x = min(x, y); } int bitcount(int x) { int ans = 0; for (int i = 0; i < k; i++) if (x >> i & 1) ans++; return ans; } int main() { scanf( %d%d%d , &n, &m, &k); scanf( %s%s , s + 1, t + 1); for (int i = 1; i <= n; i++) { scanf( %d%d , &a[i], &b[i]); } for (int i = 0; i < 1 << k; i++) mn[i] = 1e9, mx[i] = -1e9; int all = (1 << k) - 1; for (int i = 1; i <= k; i++) p[i] = i; updmx(mx[getint(t) ^ all], 0); for (int i = 1; i <= n; i++) { swap(p[a[i]], p[b[i]]); updmx(mx[getint(t) ^ all], i); } for (int i = 1; i <= k; i++) p[i] = i; updmn(mn[getint(s) ^ all], 0); for (int i = 1; i <= n; i++) { swap(p[a[i]], p[b[i]]); updmn(mn[getint(s) ^ all], i); } for (int bit = 0; bit < k; bit++) { for (int i = 1; i < (1 << k); i++) { if (((i >> bit) & 1) == 1) { mx[i] = max(mx[i], mx[i ^ (1 << bit)]); mn[i] = min(mn[i], mn[i ^ (1 << bit)]); } } } int bstval = -1, bstL, bstR; for (int i = 0; i < 1 << k; i++) { if (mx[all ^ i] - mn[all ^ i] >= m) { if (bitcount(i) >= bstval) { bstval = bitcount(i); bstR = mx[all ^ i]; bstL = mn[all ^ i] + 1; } } } int s1 = 0, t1 = 0; for (int i = 1; s[i]; i++) s1 += (s[i] == 1 ? 1 : 0); for (int i = 1; t[i]; i++) t1 += (t[i] == 1 ? 1 : 0); printf( %d n , k + 2 * bstval - s1 - t1); printf( %d %d n , bstL, bstR); }
|
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < 0 || ch > 9 ) { if (ch == - ) f = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); } return x * f; } int n, f[5010][5010][2], out[5010], siz[5010], size, rt = 1; struct node { int u, v, nxt; } e[5010 << 1]; int head[5010], js; void add(int u, int v) { e[++js] = (node){u, v, head[u]}; head[u] = js; } void dfs1(int u, int pre) { for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].v; out[u]++; if (v == pre) continue; dfs1(v, u); } size += out[u] == 1; } void dfs2(int u, int pre) { if (out[u] == 1) { siz[u] = 1; f[u][0][0] = f[u][1][1] = 0; return; } else f[u][0][0] = f[u][0][1] = 0; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].v; if (v == pre) continue; dfs2(v, u); siz[u] += siz[v]; for (int j = siz[u]; j >= 0; j--) { int tmp0 = 0x7fffffff, tmp1 = 0x7fffffff; for (int k = 0; k <= siz[v]; k++) if (k <= j) { tmp0 = min(tmp0, f[u][j - k][0] + min(f[v][k][0], f[v][k][1] + 1)); tmp1 = min(tmp1, f[u][j - k][1] + min(f[v][k][1], f[v][k][0] + 1)); } f[u][j][0] = tmp0; f[u][j][1] = tmp1; } } } int main() { n = read(); memset(f, 63, sizeof f); for (int i = 1; i < n; i++) { int x = read(), y = read(); add(x, y), add(y, x); } dfs1(1, 0); while (out[rt] == 1) rt++; dfs2(rt, 0); printf( %d , min(f[rt][size >> 1][0], f[rt][size >> 1][1])); return 0; }
|
#include <bits/stdc++.h> using namespace std; int t, n, a[110]; int main() { scanf( %d , &t); while (t--) { scanf( %d , &n); for (int i = 0; i < n; i++) scanf( %d , &a[i]); for (int i = 0; i < n; i++) a[i] %= 2; int sum = 0; for (int i = 0; i < n; i++) sum += a[i]; if (sum == 0 || sum == n) printf( YES n ); else printf( NO n ); } }
|
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-8, pi = acos(-1); const int SZ = 100050, INF = 0x7FFFFFFF; vector<int> mp[SZ]; int res, pos; int in[SZ]; void dfs(int u, int par) { int cur = 0; for (int i = 0; i < mp[u].size(); ++i) { int to = mp[u][i]; if (to != par) { ++cur; dfs(to, u); } } if (par == -1 && cur == 2) --cur; if (cur > 1) { pos = u; ++res; } } int main() { std::ios::sync_with_stdio(0); int casenum; { int n; cin >> n; for (int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v; mp[u].push_back(v); mp[v].push_back(u); ++in[u]; ++in[v]; } dfs(1, -1); if (res > 1) cout << No << endl; else if (res == 0) { cout << Yes n1 << endl; vector<int> tmp; for (int i = 1; i <= n; ++i) { if (in[i] == 1) { tmp.push_back(i); } } cout << tmp[0] << << tmp[1] << endl; } else { cout << Yes << endl; int num = count(in + 1, in + 1 + n, 1); cout << num << endl; for (int i = 1; i <= n; ++i) { if (in[i] == 1) cout << i << << pos << endl; } } } return 0; }
|
#include <bits/stdc++.h> using namespace std; int len, rt, n, m; vector<int> g[210]; int a[210], b[210]; void dfs(int cur, int pre, int dep) { int i; if (dep > len) { len = dep; rt = cur; } for (i = 0; i < g[cur].size(); i++) { int u = g[cur][i]; if (u == pre) continue; dfs(u, cur, dep + 1); } } int solve(int root) { rt = root; len = 0; dfs(root, root, 0); len = 0; dfs(rt, rt, 0); return len; } void Init() { for (int i = 1; i <= n; ++i) g[i].clear(); } int main() { int i, j; scanf( %d , &n); for (i = 1; i < n; i++) scanf( %d%d , &a[i], &b[i]); int ans = 0; for (i = 1; i < n; i++) { Init(); int x = a[i], y = b[i]; for (j = 1; j < n; j++) if (i != j) { g[a[j]].push_back(b[j]); g[b[j]].push_back(a[j]); } ans = max(ans, solve(x) * solve(y)); } printf( %d n , ans); return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; int n; int fa[1000005], huan[1000005], zihuan[1000005], siz[1000005]; int find(int now) { return fa[now] == now ? now : fa[now] = find(fa[now]); } int main() { scanf( %d , &n); for (int i = 1; i <= 2 * n; i++) { fa[i] = i; siz[i] = 1; } for (int i = 1; i <= n; i++) { int x, y; scanf( %d%d , &x, &y); int x1 = find(x), y1 = find(y); if (x == y) zihuan[x1] = 1; if (x1 == y1) huan[y1] = 1; fa[x1] = y1; } for (int i = 1; i <= 2 * n; i++) { int x = find(i); if (x != i) { siz[x] += siz[i]; huan[x] |= huan[i]; zihuan[x] |= zihuan[i]; } } long long ans = 1; for (int i = 1; i <= 2 * n; i++) { if (find(i) == i) { if (zihuan[i]) continue; else if (huan[i]) ans = (ans * 2) % mod; else ans = (ans * siz[i]) % mod; } } printf( %I64d n , ans); }
|
#include <bits/stdc++.h> using namespace std; template <class T> inline void amin(T &x, const T &y) { if (y < x) x = y; } template <class T> inline void amax(T &x, const T &y) { if (x < y) x = y; } template <class Iter> void rprintf(const char *fmt, Iter begin, Iter end) { for (bool sp = 0; begin != end; ++begin) { if (sp) putchar( ); else sp = true; printf(fmt, *begin); } putchar( n ); } int N; int A[200011]; int B[200011]; using Tuple = tuple<int, int, int>; Tuple X[100011]; void MAIN() { scanf( %d , &N); for (int i = 0, i_len = (N); i < i_len; ++i) scanf( %d , A + i); int Q; scanf( %d , &Q); vector<Tuple> sortedX; sortedX.reserve(Q); for (int i = 0, i_len = (Q); i < i_len; ++i) { int s, t, u; scanf( %d%d%d , &s, &t, &u); s--; u--; X[i] = Tuple(s, t, u); sortedX.emplace_back(s, t, -1); } sort(sortedX.begin(), sortedX.end()); sortedX.erase(unique(sortedX.begin(), sortedX.end()), sortedX.end()); long long sum = 0; for (int i = 0, i_len = (N); i < i_len; ++i) sum += A[i]; for (int i = 0, i_len = (Q); i < i_len; ++i) { int s, t, u; tie(s, t, u) = X[i]; auto it = lower_bound(sortedX.begin(), sortedX.end(), Tuple(s, t, -1)); int s0, t0, u0; tie(s0, t0, u0) = *it; if (0 <= u0) { sum -= max(0, A[u0] - B[u0]); B[u0]--; sum += max(0, A[u0] - B[u0]); } if (0 <= u) { sum -= max(0, A[u] - B[u]); B[u]++; sum += max(0, A[u] - B[u]); } *it = Tuple(s, t, u); printf( %lld n , sum); } } int main() { int TC = 1; for (int tc = 0, tc_len = (TC); tc < tc_len; ++tc) MAIN(); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int q = 0; q < t; ++q) { int n, k; cin >> n >> k; vector<int> x(n); for (int i = 0; i < n; ++i) { cin >> x[i]; } vector<int> p(n, 0); for (int i = 1; i < n - 1; ++i) { if (x[i] > x[i + 1] && x[i] > x[i - 1]) { p[i] = p[i - 1] + 1; } else { p[i] = p[i - 1]; } } p[n - 1] = p[n - 2]; int ans = 0, left = 0; for (int l = 0; l + k - 1 < n; ++l) { if (p[l + k - 2] - p[l] > ans) { ans = p[l + k - 2] - p[l]; left = l; } } cout << ans + 1 << << left + 1 << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; bool isPowerOfTwo(long long x) { return x && (!(x & (x - 1))); } void solve() { long long n, m; cin >> n >> m; if (n == 1 || m == 1) { cout << YES << n ; return; } if (n != m) { cout << NO << n ; return; } if (n == 2) { cout << YES << n ; return; } cout << NO << n ; } signed main() { cin.tie(NULL), ios_base::sync_with_stdio(false), cout.tie(NULL); long long t = 1; cin >> t; while (t--) solve(); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); string s; cin >> s; vector<string> a, b; string t = ; bool if_num = true, if_first = true; for (int i = 0; i < (int)s.size(); i++) { if (s[i] == , or s[i] == ; ) { if (if_num and t != ) { a.push_back(t); } else { b.push_back(t); } t = ; if_num = true; if_first = true; continue; } if (s[i] < 0 or s[i] > 9 or (s[i] == 0 and if_first and (i != (int)s.size() - 1 and s[i + 1] != ; and s[i + 1] != , ))) { if_num = false; } if_first = false; t += s[i]; } if (if_num and t != ) { a.push_back(t); } else { b.push_back(t); } if (a.empty()) { cout << - << endl; } else { cout << ; for (int i = 0; i < (int)a.size(); i++) { cout << a[i]; if (i != (int)a.size() - 1) { cout << , ; } } cout << << endl; } if (b.empty()) { cout << - << endl; } else { cout << ; for (int i = 0; i < (int)b.size(); i++) { cout << b[i]; if (i != (int)b.size() - 1) { cout << , ; } } cout << << endl; } }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int a, b; int count = 0; for (int a = 0; a <= n + m; a++) for (int b = 0; b <= n + m; b++) { if (a * a + b == n && b * b + a == m) count++; } cout << count; return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; const int MAX = 505; int n, grid[MAX][MAX]; char s[5]; bool valid(int x, int y) { if (x < 1) return false; if (y < 1) return false; if (x > n) return false; if (y > n) return false; return true; } bool dist(int x1, int y1, int x2, int y2) { return ((abs(x1 - x2) + abs(y1 - y2)) >= n - 1); } bool cmd(int x1, int y1, int x2, int y2) { printf( ? %d %d %d %d n , x1, y1, x2, y2); fflush(stdout); scanf( %s , s); return (s[0] == Y ); } int main() { scanf( %d , &n); grid[1][1] = 1; grid[n][n] = 1; int x = 1, y = 1; while (true) { int ok = 0; if (valid(x, y + 1)) { if (dist(x, y + 1, n, n)) { if (cmd(x, y + 1, n, n)) { grid[x][y + 1] = 1; ok = 1; y++; } } } if (ok) continue; if (valid(x + 1, y)) { if (dist(x + 1, y, n, n)) { if (cmd(x + 1, y, n, n)) { grid[x + 1][y] = 1; ok = 1; x++; } } } if (ok) continue; break; } x = n, y = n; while (true) { int ok = 0; if (valid(x - 1, y)) { if (dist(x - 1, y, 1, 1)) { if (cmd(1, 1, x - 1, y)) { grid[x - 1][y] = 1; ok = 1; x--; } } } if (ok) continue; if (valid(x, y - 1)) { if (dist(x, y - 1, 1, 1)) { if (cmd(1, 1, x, y - 1)) { grid[x][y - 1] = 1; ok = 1; y--; } } } if (ok) continue; break; } string ans = ; x = 1, y = 1; while (true) { if (x == n and y == n) break; if (grid[x][y + 1]) ans += R , y++; else ans += D , x++; } printf( ! ); for (int i = 0; i < ans.size(); i++) printf( %c , ans[i]); printf( n ); fflush(stdout); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; int Log[N], bin[21], ls[N], rs[N], root, a[N], n, i, j; set<int> s[N]; pair<int, int> mx[N][20]; long long ans; void merge(int x, int y) { if (s[x].size() < s[y].size()) swap(s[x], s[y]); for (auto& z : s[y]) s[x].insert(z); s[y].clear(); } void dfs(int x) { if (!x) return; dfs(ls[x]); dfs(rs[x]); if (s[ls[x]].size() > s[rs[x]].size()) swap(ls[x], rs[x]); for (auto& z : s[ls[x]]) if (s[rs[x]].count(a[x] - z)) ans++; s[x].insert(a[x]); merge(x, ls[x]); merge(x, rs[x]); } int query(int l, int r) { int t = Log[r - l + 1]; return max(mx[l][t], mx[r - bin[t] + 1][t]).second; } int build(int l, int r) { if (l > r) return 0; int x = query(l, r); ls[x] = build(l, x - 1); rs[x] = build(x + 1, r); return x; } int main() { for (scanf( %d , &n), i = 1; i <= n; ++i) scanf( %d , a + i); for (Log[0] = -1, i = 1; i <= n; ++i) Log[i] = Log[i >> 1] + 1; for (bin[0] = 1, i = 1; i < 20; ++i) bin[i] = bin[i - 1] << 1; for (i = 1; i <= n; ++i) mx[i][0] = pair<int, int>(a[i], i); for (j = 1; j <= Log[n]; ++j) for (i = 1; i + bin[j - 1] <= n; ++i) mx[i][j] = max(mx[i][j - 1], mx[i + bin[j - 1]][j - 1]); root = build(1, n); dfs(root); printf( %d n , ans); }
|
#include <bits/stdc++.h> using namespace std; const int maxn = 510; long long sum[maxn]; int main() { long long n, k; int t; cin >> t; while (t--) { cin >> n >> k; if (n >= 32) { cout << YES << << n - 1 << endl; } else { memset(sum, 0, sizeof(sum)); int ans = -1; long long total = (pow(4, n) - 1) / 3; if (k > total) { cout << NO << endl; continue; } long long temp = pow(2, n - 1) * 2 - 1, cnt = 0; while (temp) { sum[++cnt] = 2 * (temp / 2) + 1; temp /= 2; } for (int i = cnt; i >= 1; i--) sum[i] += sum[i + 1]; for (int i = 1; i <= cnt; i++) if (sum[i] <= k && total - sum[1] >= k - sum[i]) { ans = i; break; } if (ans == -1) cout << NO << endl; else cout << YES << << ans - 1 << endl; } } }
|
#include <bits/stdc++.h> using namespace std; void solve(); int main() { solve(); } void solve() { int t; cin >> t; while (t--) { string s; cin >> s; priority_queue<int> q; int k = s.length(); int cur = (s[0] == 1 ) ? 1 : 0; for (int i = 1; i < k; i++) { if (s[i] == 1 && s[i - 1] == 0 ) { cur = 1; } if (s[i] == 1 && s[i - 1] == 1 ) { cur++; } if (s[i] == 0 ) { if (cur != 0) { q.push(cur); cur = 0; } } } if (s[k - 1] == 1 ) { if (cur != 0) { q.push(cur); cur = 0; } } int ans = 0; int qs = q.size(); for (int i = 0; i < qs; i++) { if ((i & 0x1) == 0) { ans += q.top(); } else { } q.pop(); } cout << ans << endl; } }
|
#include <bits/stdc++.h> using namespace std; int main() { float a1, b1, c1, a2, b2, c2; cin >> a1 >> b1 >> c1 >> a2 >> b2 >> c2; if (!b1 && !b2) { if (!a1 && !a2 && c1 && c2) { cout << 0 << endl; return 0; } if ((!a1 && c1) || (!a1 && c2)) { cout << 0 << endl; return 0; } if ((!c1 && !c2) || (!a1 && !a2) || c1 / a1 == c2 / a2) { cout << -1 << endl; return 0; } else { cout << 0 << endl; return 0; } } else if ((!b1 && !a1 && !c1) || (!b2 && !a2 && !c2)) { cout << -1 << endl; return 0; } else if ((!b1 && !a1) || (!b2 && !a2)) { cout << 0 << endl; return 0; } else if (!b1 || !b2) { cout << 1 << endl; return 0; } if (a1 / b1 == a2 / b2 && c1 / b1 == c2 / b2) cout << -1 << endl; else if (a1 / b1 == a2 / b2) cout << 0 << endl; else cout << 1 << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); string s, curr[1000]; long long int size = 0, j; cin >> j; while (j--) { cin >> s; if (s.compare( pwd ) == 0) { for (long long int i = 0; i < size; ++i) { cout << / << curr[i]; } cout << / << n ; } else if (s.compare( cd ) == 0) { string temp; cin >> s; if (s[0] == / ) { size = 0; } for (long long int i = 0; i < s.size(); ++i) { temp = ; for (; i < s.size(); ++i) { if (s[i] == / ) break; temp += s[i]; } if (temp.compare( .. ) == 0) --size; else if (temp.size()) { curr[size] = temp; ++size; } } } } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 10; const int M = N * 30; int tree[M][2]; vector<int> val[M]; int main() { int n; scanf( %d , &n); vector<int> a(n + 1); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); int idx = 0; auto insert = [&](int n, int id) { int tmp = 0; bitset<30> st(n); for (int i = 29; i >= 0; i--) { if (!tree[tmp][st[i]]) tree[tmp][st[i]] = ++idx; tmp = tree[tmp][st[i]]; val[tmp].emplace_back(id); } }; for (int i = 1; i <= n; i++) insert(a[i], i); auto get_cal = [&](vector<int> &a, vector<int> &b) -> long long { long long ans = 0; int i = 0, j = 0; while (i < a.size() && j < b.size()) { if (a[i] < b[j]) i++; else ans += (int)a.size() - i, j++; } return ans; }; vector<vector<long long>> ans(30, vector<long long>(2, 0)); function<void(int, int)> query = [&](int root, int deep) { if (!tree[root][0] && !tree[root][1]) return; long long cnt = get_cal(val[tree[root][0]], val[tree[root][1]]); ans[deep][0] += cnt; ans[deep][1] += 1LL * val[tree[root][0]].size() * val[tree[root][1]].size() - cnt; if (tree[root][0]) query(tree[root][0], deep - 1); if (tree[root][1]) query(tree[root][1], deep - 1); }; query(0, 29); int res = 0; long long cnt = 0; for (int i = 29; i >= 0; i--) { if (ans[i][0] > ans[i][1]) res += 1 << i, cnt += ans[i][1]; else cnt += ans[i][0]; } printf( %lld %d n , cnt, res); return 0; }
|
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vd = vector<double>; using vs = vector<string>; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<double, double>; using vpii = vector<pii>; using vvpii = vector<vpii>; using vpll = vector<pll>; using vvpll = vector<vpll>; using vpdd = vector<pdd>; using vvpdd = vector<vpdd>; template <typename T> void ckmin(T& a, const T& b) { a = min(a, b); } template <typename T> void ckmax(T& a, const T& b) { a = max(a, b); } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); namespace __input { template <class T1, class T2> void re(pair<T1, T2>& p); template <class T> void re(vector<T>& a); template <class T, size_t SZ> void re(array<T, SZ>& a); template <class T> void re(T& x) { cin >> x; } void re(double& x) { string t; re(t); x = stod(t); } template <class Arg, class... Args> void re(Arg& first, Args&... rest) { re(first); re(rest...); } template <class T1, class T2> void re(pair<T1, T2>& p) { re(p.first, p.second); } template <class T> void re(vector<T>& a) { for (int i = 0; i < (int((a).size())); i++) re(a[i]); } template <class T, size_t SZ> void re(array<T, SZ>& a) { for (int i = 0; i < (SZ); i++) re(a[i]); } } // namespace __input using namespace __input; namespace __output { template <class T1, class T2> void pr(const pair<T1, T2>& x); template <class T, size_t SZ> void pr(const array<T, SZ>& x); template <class T> void pr(const vector<T>& x); template <class T> void pr(const deque<T>& x); template <class T> void pr(const set<T>& x); template <class T1, class T2> void pr(const map<T1, T2>& x); template <class T> void pr(const T& x) { cout << x; } template <class Arg, class... Args> void pr(const Arg& first, const Args&... rest) { pr(first); pr(rest...); } template <class T1, class T2> void pr(const pair<T1, T2>& x) { pr( { , x.first, , , x.second, } ); } template <class T, bool pretty = true> void prContain(const T& x) { if (pretty) pr( { ); bool fst = 1; for (const auto& a : x) pr(!fst ? pretty ? , : : , a), fst = 0; if (pretty) pr( } ); } template <class T> void pc(const T& x) { prContain<T, false>(x); pr( n ); } template <class T, size_t SZ> void pr(const array<T, SZ>& x) { prContain(x); } template <class T> void pr(const vector<T>& x) { prContain(x); } template <class T> void pr(const deque<T>& x) { prContain(x); } template <class T> void pr(const set<T>& x) { prContain(x); } template <class T1, class T2> void pr(const map<T1, T2>& x) { prContain(x); } void ps() { pr( n ); } template <class Arg> void ps(const Arg& first) { pr(first); ps(); } template <class Arg, class... Args> void ps(const Arg& first, const Args&... rest) { pr(first, ); ps(rest...); } } // namespace __output using namespace __output; namespace __numeric { template <typename T> typename enable_if<is_integral<T>::value, T>::type floor(T n, T d) { assert(d != 0); if (d < 0) tie(n, d) = make_pair(-n, -d); return n / d - ((n < 0) && (n % d)); } template <typename T> typename enable_if<is_integral<T>::value, T>::type ceil(T n, T d) { assert(d != 0); if (d < 0) tie(n, d) = make_pair(-n, -d); return n / d + ((n > 0) && (n % d)); } }; // namespace __numeric using namespace __numeric; namespace __algorithm { template <typename T> void dedup(vector<T>& v) { sort((v).begin(), (v).end()); v.erase(unique((v).begin(), (v).end()), v.end()); } template <typename T> typename vector<T>::iterator find(vector<T>& v, const T& x) { auto it = lower_bound((v).begin(), (v).end(), x); return it != v.end() && *it == x ? it : v.end(); } template <typename T> size_t index(vector<T>& v, const T& x) { auto it = find(v, x); assert(it != v.end() && *it == x); return it - v.begin(); } template <typename C, typename T, typename OP> vector<T> prefixes(const C& v, T id, OP op) { vector<T> r(int((v).size()) + 1, id); for (int i = 0; i < (int((v).size())); i++) r[i + 1] = op(r[i], v[i]); return r; } template <typename C, typename T, typename OP> vector<T> suffixes(const C& v, T id, OP op) { vector<T> r(int((v).size()) + 1, id); for (int i = (int((v).size())) - 1; i >= 0; i--) r[i] = op(v[i], r[i + 1]); return r; } } // namespace __algorithm using namespace __algorithm; #pragma GCC diagnostic push #pragma GCC diagnostic ignored -Wunused-parameter struct monostate { friend istream& operator>>(istream& is, const monostate& ms) { return is; } friend ostream& operator<<(ostream& os, const monostate& ms) { return os; } friend monostate operator+(const monostate& a, const monostate& b) { return a; } } ms; #pragma GCC diagnostic pop namespace __io { void setIn(string second) { freopen(second.c_str(), r , stdin); } void setOut(string second) { freopen(second.c_str(), w , stdout); } void setIO(string second = ) { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(15); if (int((second).size())) { setIn(second + .in ), setOut(second + .out ); } } } // namespace __io using namespace __io; struct union_find { struct node { int par, rnk, size; node(int id = 0) : par(id), rnk(0), size(1) {} }; vector<node> uf; union_find(int N = 0) : uf(N) { for (int i = 0; i < N; i++) uf[i] = node(i); } int rep(int i) { if (i != uf[i].par) uf[i].par = rep(uf[i].par); return uf[i].par; } bool unio(int a, int b) { a = rep(a), b = rep(b); if (a == b) return false; if (uf[a].rnk < uf[b].rnk) swap(a, b); uf[b].par = a; uf[a].size += uf[b].size; if (uf[a].rnk == uf[b].rnk) uf[a].rnk++; return true; } node& operator[](int i) { return uf[rep(i)]; } friend void pr(const union_find& u) { pr( { ); bool first = 1; for (int i = 0; i < int((u.uf).size()); i++) if (u.uf[i].par == i) { if (!first) pr( , ); else first = 0; pr( [ , i, | rank= , u.uf[i].rnk, size= , u.uf[i].size, ] ); } pr( } ); } }; const int MAXB = 18; const int MAXV = 1 << MAXB; int ct[MAXV]; int main() { setIO(); int N; re(N); for (int i = 0; i < (N); i++) { int v; re(v); ct[v]++; } ll ans = 0; for (int v = 0; v < (MAXV); v++) { ans -= ll(v) * ct[v]; } union_find uf(MAXV + 1); for (int sum = (MAXV)-1; sum >= 0; sum--) { for (int v1 = sum, v2 = 0; v1 > v2; v1 = (v1 - 1) & sum, v2 = sum ^ v1) { if (ct[v1] && ct[v2]) { if (uf.unio(v1, v2)) { ans += ll(ct[v1] + ct[v2] - 1) * sum; ct[v1] = 1; ct[v2] = 1; } } } if (ct[sum] && uf.unio(MAXV, sum)) ans += ll(ct[sum]) * sum; } ps(ans); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; long long qpow(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = res * a; b >>= 1; a = a * a; } return res; } int a[500005]; int main() { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + n + 1); k = 8 * k / n; if (k >= ceil(log(n) / log(2))) { cout << 0; return 0; } long long q = pow(2, k); int min1 = n; int sum = 0; for (int l = 1, r = 1; r <= n && l <= n;) { while (r < n && a[r] == a[r + 1]) r++; sum++; if (sum == q) { min1 = min(min1, n - (r - l + 1)), sum--; while (l < n && a[l] == a[l + 1]) l++; l++; } r++; } if (min1 == n) cout << 0; else cout << min1; return 0; }
|
#include <bits/stdc++.h> #pragma GCC optimize( Ofast , unroll-loops ) using namespace std; const int inf = numeric_limits<int>::max() / 2; const long double eps = 1e-9; const long double pi = acos(-1); const int MAXN = (3e8 + 10) / 3; int n; unsigned int a, b, c, d; bitset<MAXN> primes; unsigned int f(unsigned int x) { return d + x * (c + x * (b + x * a)); } int fict(int i) { return i - i / 2 - i / 3 + i / 6 - 1; } void sieve() { primes.set(); for (int i = 5; i * i <= n; i++) { if (i % 2 == 0 || i % 3 == 0) { continue; } if (primes[fict(i)]) { for (int j = i * i; j <= n; j += i) { if (j % 2 == 0 || j % 3 == 0) { continue; } primes[fict(j)] = 0; } } } } inline void solve() { sieve(); unsigned int ans = 0; for (int p = 2; p <= n; p++) { if (p != 2 && p % 2 == 0 || p != 3 && p % 3 == 0) { continue; } if (p != 2 && p != 3 && !primes[fict(p)]) { continue; } unsigned int s = 0; unsigned int q = n / p; while (q >= 1) { s += q; q /= p; } ans += f(p) * s; } cout << ans << n ; } inline void read() { cin >> n >> a >> b >> c >> d; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); read(); solve(); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int Max = 2e5 + 10; vector<int> A; int ok[Max]; int len; int seg[Max * 4]; int lazy[Max * 4]; void build(int tl = 0, int tr = len + 1, int idx = 1) { if (tr - tl == 1) { seg[idx] = len - tl; return; } int mid = (tl + tr) / 2; build(tl, mid, idx * 2); build(mid, tr, idx * 2 + 1); seg[idx] = min(seg[idx * 2], seg[idx * 2 + 1]); } void shift(int idx) { if (!lazy[idx]) return; lazy[idx * 2] += lazy[idx]; lazy[idx * 2 + 1] += lazy[idx]; seg[idx * 2] += lazy[idx]; seg[idx * 2 + 1] += lazy[idx]; lazy[idx] = 0; } void upd(int i, int val, int tl = 0, int tr = len + 1, int idx = 1) { if (tr - 1 <= i) { lazy[idx] += val; seg[idx] += val; return; } if (tl > i) return; shift(idx); int mid = (tl + tr) / 2; upd(i, val, tl, mid, idx * 2); upd(i, val, mid, tr, idx * 2 + 1); seg[idx] = min(seg[idx * 2], seg[idx * 2 + 1]); } int check() { return seg[1]; } int main() { int n, h; cin >> n >> len >> h; for (int i = 0; i < len; i++) { int a; cin >> a; A.push_back(a); } sort(A.begin(), A.end()); for (int i = 0; i < n; i++) { long long a; cin >> a; int l = -1, r = len; while (r - l > 1) { int idx = (l + r) / 2; if (a + A[idx] >= h) r = idx; else l = idx; } ok[i] = r; } int ans = 0; build(); for (int i = 0; i < len; i++) upd(ok[i], -1); if (check() >= 0) ans++; for (int i = len; i < n; i++) { upd(ok[i], -1); upd(ok[i - len], 1); if (check() >= 0) ans++; } cout << ans; }
|
#include <bits/stdc++.h> using namespace std; const int N = 120000 + 10; const double PI = acos(-1); pair<double, double> operator+(pair<double, double> a, pair<double, double> b) { return make_pair(a.first + b.first, a.second + b.second); } pair<double, double> operator-(pair<double, double> a, pair<double, double> b) { return make_pair(a.first - b.first, a.second - b.second); } pair<double, double> operator*(pair<double, double> a, pair<double, double> b) { return make_pair(a.first * b.first - a.second * b.second, a.first * b.second + a.second * b.first); } pair<double, double> operator/(pair<double, double> a, double t) { return make_pair(a.first / t, a.second / t); } int rev[N], lim; pair<double, double> w[N]; void initrever(int n, int &lim) { lim = 1; for (; lim < n; lim <<= 1) ; for (int i = 1; i < lim; i++) { rev[i] = (rev[i >> 1] >> 1); if (i & 1) rev[i] |= (lim >> 1); } for (int i = 0; i < lim; i++) { double t = 2 * PI * i / lim; w[i] = make_pair(cos(t), sin(t)); } return; } void FFT(vector<pair<double, double> > &a, int t) { int lim = a.size(); if (t < 0) { for (int i = 1; i < lim; i++) { if (i < (lim - i)) swap(a[i], a[lim - i]); } } for (int i = 0; i < lim; i++) if (i < rev[i]) swap(a[i], a[rev[i]]); for (int i = 2; i <= lim; i <<= 1) { for (int j = 0; j < lim; j += i) { for (int k = 0; k < (i >> 1); k++) { pair<double, double> first = a[j + k], second = a[j + k + (i >> 1)] * w[lim / i * k]; a[j + k] = (first + second); a[j + k + (i >> 1)] = (first - second); } } } if (t < 0) { for (int i = 0; i < lim; i++) a[i] = a[i] / lim; } return; } vector<pair<double, double> > f[2]; int n, a, p; double reduce(double first) { long long tmp = llround(first); tmp %= p; return (double)tmp; } void add1() { swap(f[0], f[1]); for (int i = 0; i <= 1; i++) { for (int j = n; j >= 1; j--) { f[i][j].first = reduce(f[i][j].first + f[i][j - 1].first); } } f[1][1].first = reduce(f[1][1].first + 1); return; } void put(vector<pair<double, double> > v) { for (int i = 0; i < (int)v.size(); i++) printf( %lf , v[i].first); printf( n ); return; } void pro2(int v) { FFT(f[0], 1), FFT(f[1], 1); for (int i = 0; i < lim; i++) { pair<double, double> first = f[v & 1][i] * (f[0][i] + f[1][i]) + f[0][i] + f[v & 1][i]; pair<double, double> second = f[(v & 1) ^ 1][i] * (f[0][i] + f[1][i]) + f[1][i] + f[(v & 1) ^ 1][i]; f[0][i] = first, f[1][i] = second; } FFT(f[0], -1), FFT(f[1], -1); for (int i = 0; i <= 1; i++) { for (int j = 0; j <= n; j++) { f[i][j].first = reduce(f[i][j].first); f[i][j].second = 0; } for (int j = n + 1; j < lim; j++) f[i][j] = make_pair(0, 0); } return; } vector<int> num; int main() { scanf( %d%d%d , &n, &a, &p); initrever(n * 2 + 1, lim); f[0].resize(lim), f[1].resize(lim); for (int no = a; no > 1; no /= 2) { num.push_back(no); } num.push_back(1); reverse(num.begin(), num.end()); f[1][1].first = 1; int ans = 0; for (int i = 0; i < (int)num.size() - 1; i++) { for (int j = 1; j <= n; j += 2) { ans = (ans + llround(f[1][j].first)) % p; } pro2(num[i]); if (num[i] * 2 < num[i + 1]) add1(); } printf( %d n , ans); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int arr[n]; for (int i = 0; i <= n - 1; i++) cin >> arr[i]; vector<pair<int, int> > G[n]; int cnt = 0; for (int i = 0; i <= n - 1; i++) { for (int j = i + 1; j <= n - 1; j++) { if (arr[i] > arr[j]) { G[i].push_back(make_pair(arr[j], j)); cnt++; } } } cout << cnt << n ; for (int i = 0; i <= n - 1; i++) { sort(G[i].begin(), G[i].end()); int s = G[i].size(); for (int j = s - 1; j >= 0; j--) { cout << i + 1 << << G[i][j].second + 1 << n ; } } return 0; }
|
#include <bits/stdc++.h> using namespace std; int arr[300022]; int main() { int q; scanf( %d , &q); while (q--) { int n, i; scanf( %d , &n); for (i = 0; i < n; i++) scanf( %d , &arr[i]); map<int, int> mf, ml; map<int, int>::iterator it; for (i = 0; i < n; i++) { if (mf.find(arr[i]) == mf.end()) mf[arr[i]] = i; ml[arr[i]] = i; } int ans = 0, in = -1, len = 0; int cou = 0; for (it = mf.begin(); it != mf.end(); it++) { cou++; int ele = it->first; int focc = it->second, locc = ml[ele]; if (focc > in) len++; else len = 1; in = locc; ans = max(ans, len); } cout << (cou - ans) << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; namespace mainns { template <typename T> void relaxMax(T& relaxWhat, const T& relaxBy) { relaxWhat = max(relaxWhat, relaxBy); } template <typename T> void relaxMin(T& relaxWhat, const T& relaxBy) { relaxWhat = min(relaxWhat, relaxBy); } namespace { const vector<pair<int, int>> offsets = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; const unordered_map<char, char> nextLetter = { { D , I }, { I , M }, { M , A }, { A , D }}; } // namespace class Solver374C { public: void run(); int height, width; vector<string> board; vector<vector<int>> maxValues; int getMaxValue(int x, int y) { if (maxValues[x][y] > 0) return maxValues[x][y]; if (maxValues[x][y] == -2) return numeric_limits<int>::max() / 10; maxValues[x][y] = -2; int maxNeighbourValue = 0; for (auto& offs : offsets) { auto neigbour = make_pair(x + offs.first, y + offs.second); if (neigbour.first >= 0 && neigbour.first < width && neigbour.second >= 0 && neigbour.second < height) if (nextLetter.at(board[y][x]) == board[neigbour.second][neigbour.first]) relaxMax(maxNeighbourValue, getMaxValue(neigbour.first, neigbour.second)); } return maxValues[x][y] = maxNeighbourValue + 1; } }; void Solver374C::run() { cin >> height >> width; board = vector<string>(height); for (auto& s : board) cin >> s; maxValues = vector<vector<int>>(width, vector<int>(height, -1)); int maxValue = 0; for (int x = 0; x < width; ++x) for (int y = 0; y < height; ++y) { auto v = getMaxValue(x, y); if (board[y][x] == D ) relaxMax(maxValue, v); } if (maxValue < 4) cout << Poor Dima! ; else if (maxValue >= numeric_limits<int>::max() / 10) cout << Poor Inna! ; else cout << (maxValue / 4); } } // namespace mainns using CurrentSolver = mainns::Solver374C; int main() { ios::sync_with_stdio(false); CurrentSolver().run(); return 0; }
|
#include <bits/stdc++.h> using namespace std; bool allCharactersSame(string s) { int n = s.length(); for (int i = 1; i < n; i++) if (s[i] != s[0]) return false; return true; } signed main() { int n; const int letters = 26; cin >> n; for (int i = 0; i < n; i++) { string str; cin >> str; int n = str.size(); vector<int> cnt(letters); for (int i = 0; i < n; ++i) cnt[str[i] - a ]++; int ans = 0, k = 0; for (int i = 0; i < letters; ++i) { if (cnt[i] > 1) ans++; else if (cnt[i] == 1) k++; } ans += k / 2; cout << ans << n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 200010; int hp[N], dmg[N], p[N]; bool cmp(int a, int b) { return hp[a] - dmg[a] > hp[b] - dmg[b]; } bool vis[N]; int main() { int n, a, b; scanf( %d%d%d , &n, &a, &b); for (int i = 1; i <= n; i++) { scanf( %d%d , &hp[i], &dmg[i]); p[i] = i; } sort(p + 1, p + 1 + n, cmp); long long sum = 0, ans, la = 0, cb = 0; bool flag = false; for (int i = 1; i <= n; i++) { if (i > b || hp[p[i]] <= dmg[p[i]]) { if (!flag) flag = true, la = hp[p[i - 1]] - dmg[p[i - 1]], cb = i - 1; } if (!flag) sum += hp[p[i]], vis[p[i]] = true; else sum += dmg[p[i]]; } ans = sum; if (a && b) { for (int i = 1; i <= n; i++) { long long tmp = hp[i]; for (int j = 1; j <= a; j++) tmp = tmp * 2; tmp += sum; if (vis[i]) { tmp -= hp[i]; } else { tmp -= dmg[i]; if (cb == b) tmp -= la; } ans = max(ans, tmp); } } printf( %lld n , ans); return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long Maxn = 10000; const long double pi = acos(-1); const long long mod = 1000000007; template <class T> ostream &operator<<(ostream &out, vector<T> &A) { for (auto x : A) out << x << ; return out; } template <class T> ostream &operator<<(ostream &out, set<T> &A) { for (auto x : A) out << x << ; return out; } template <class T1, class T2> T1 powr(T1 a, T2 b) { T1 res = 1; for (long long i = 1; i < b + 1; i++) res = res * a; return res; } int32_t main() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, l, r, x; while (cin >> n >> l >> r >> x) { vector<long long> k(n); for (long long i = 0; i < n; i++) { cin >> k[i]; } long long cnt = 0; for (long long i = 0; i < (1 << n); i++) { long long mi = 1000000; long long ma = -1; long long sum = 0; for (long long j = 0; j < n; j++) { if (i & (1 << j)) { sum = sum + k[j]; ma = max(ma, k[j]); mi = min(mi, k[j]); } } if (l <= sum && sum <= r && (ma - mi) >= x) { cnt++; } } cout << cnt << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000 + 5; int N, P[MAXN]; long long D[MAXN]; vector<pair<long long, int>> v, f[2]; int main() { scanf( %d , &N); P[1] = 1, P[2] = 2; for (int i = 3, x; i <= N; ++i) { printf( 2 1 %d %d n , P[2], i); fflush(stdout); scanf( %d , &x); if (x == -1) P[2] = i; } for (int i = 2; i <= N; ++i) { if (i == P[2]) continue; printf( 1 1 %d %d n , P[2], i); fflush(stdout); scanf( %lld , &D[i]); v.push_back(make_pair(D[i], i)); } sort(v.begin(), v.end()); int p = v.back().second; for (int i = 2, x; i <= N; ++i) { if (i == p || i == P[2]) continue; printf( 2 1 %d %d n , p, i); fflush(stdout); scanf( %d , &x); f[x == -1].push_back(make_pair(D[i], i)); } sort(f[0].begin(), f[0].end()); sort(f[1].begin(), f[1].end()); printf( 0 %d %d , P[1], P[2]); for (int i = 0; i < f[1].size(); ++i) printf( %d , f[1][i].second); printf( %d , p); for (int i = f[0].size() - 1; i >= 0; --i) printf( %d , f[0][i].second); fflush(stdout); return 0; }
|
#include <bits/stdc++.h> using namespace std; int i, j, m, n, u, v; long long a[15], mod, lose[14][16389], f[16389], g[16389][15]; long long sz[16389], inv[16389]; long long getinv(long long a, long long p) { if (a == 1) return 1; long long k = a - getinv(p % a, a); return (k * p + 1) / a; } long long group_lose(int v, int u) { int i, j; long long ret; ret = f[v] * f[u - v] % mod * g[u - v][sz[v]] % mod * inv[u] % mod; return ret; } int main() { mod = 1000000007; scanf( %d , &n); for (i = 0; i <= n - 1; i++) { scanf( %lld , &a[i]); } for (u = 0; u <= (1 << n) - 1; u++) { sz[u] = 0; for (i = 0; i <= n - 1; i++) if (u & (1 << i)) sz[u]++; } for (u = 0; u <= (1 << n) - 1; u++) { f[u] = 1; for (i = 0; i <= n - 1; i++) if (u & (1 << i)) { for (j = i + 1; j <= n - 1; j++) if (u & (1 << j)) { f[u] = f[u] * (a[i] + a[j]) % mod; } } inv[u] = getinv(f[u], mod); } for (u = 0; u <= (1 << n) - 1; u++) { g[u][0] = 1; g[u][1] = 1; for (i = 0; i <= n - 1; i++) if (u & (1 << i)) { g[u][1] = g[u][1] * a[i] % mod; } for (i = 2; i <= n; i++) { g[u][i] = g[u][i - 1] * g[u][1] % mod; } } for (u = 0; u <= (1 << n) - 1; u++) { for (i = 0; i <= n - 1; i++) { if (u & (1 << i)) { lose[i][u] = 0; for (v = u; v; v = (v - 1) & u) if ((v != u) && (v & (1 << i))) { long long temp = group_lose(v, u); lose[i][u] += (1 - lose[i][v] + mod) * temp % mod; } lose[i][u] %= mod; } } } long long ans = 0; for (i = 0; i <= n - 1; i++) { ans += (1 - lose[i][(1 << n) - 1] + mod); } ans %= mod; printf( %lld n , ans); return 0; }
|
#include <bits/stdc++.h> const long double eps = 1e-9; const long long mod = 1e9 + 7, ll_max = (long long)1e18; const int MX = 2e5 + 10, int_max = 0x3f3f3f3f; using namespace std; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); long long n, k; long long arr[MX]; int solve() { cin >> n >> k; for (int i = 0; i < k; i++) { arr[i] += i + 1; n -= i + 1; } if (n < 0) return 0; for (int i = 0; i < k; i++) { arr[i] += (n / k); } n %= k; for (int i = k - 1; i > 0; i--) { int del = min(2 * arr[i - 1] - arr[i], n); n -= del; arr[i] += del; } return (n == 0); } int main() { cin.tie(0)->sync_with_stdio(0); if (solve()) { cout << YES n ; for (int i = 0; i < k; i++) cout << arr[i] << ; cout << n ; } else { cout << NO n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; long long x[300001]; long long p[300001] = {1}; int main() { std::ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> x[i]; p[i + 1] = p[i] * 2 % MOD; } sort(x, x + n); long long ret = 0; for (int i = 1; i <= n; ++i) { ret = (ret + x[i - 1] * (p[i - 1] - p[n - i])) % MOD; } cout << (ret + MOD) % MOD; }
|
#include <bits/stdc++.h> using namespace std; vector<long long int> v, v1, v2; multiset<long long int> mss; multiset<long long int>::iterator it; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int a, b, c, d, i, j, z = 0, k, w = 1, q = 0, cnt = 0, x = 0, p = 0; cin >> a >> c; for (i = 0; i < a; i++) { cin >> b; v.push_back(b); } sort(v.begin(), v.end()); for (i = 0; i < a / 2; i++) { v1.push_back(v[i]); } for (j = i; j < a; j++) { mss.insert(v[j]); } for (i = 0; i < v1.size(); i++) { d = v1[i] + c; it = mss.lower_bound(d); if (it == mss.end()) { break; } cnt++; mss.erase(it); } cout << cnt; return 0; }
|
#include <bits/stdc++.h> using namespace std; bool cmp(int x, int y) { return x > y; } const int maxn = 3e5 + 100; vector<int> v[maxn]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); for (int i = 2; i < maxn; i++) for (int j = i; j < maxn; j += i) v[j].push_back(i); v[1].push_back(1); int t; cin >> t; while (t--) { int n; char c; cin >> n >> c; string s; cin >> s; int check[n + 1]; memset(check, 0, sizeof(check)); int cnt = 0; for (int i = 0; i < n; i++) if (s[i] != c) { cnt++; for (auto j : v[i + 1]) check[j] = 1; } if (!cnt) { cout << 0 << endl; continue; } int cc = 0; for (int i = 2; i <= n; i++) if (!check[i] && s[i - 1] == c) cc = i; if (!cc) cout << 2 << endl << n << << n - 1 << endl; else cout << 1 << endl << cc << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug& operator<<(const c&) { return *this; } }; void test_case() { int n, k; cin >> n >> k; vector<pair<int, int>> a(n); for (int i = 0; i < n; i++) { cin >> a[i].first; a[i].second = i; } sort(a.begin(), a.end(), less<pair<int, int>>()); int start = 0, tot = 0; vector<int> res; while (start < n && k - a[start].first >= 0) { debug() << [ << k : << (k) << ] ; tot++; k = k - a[start].first; res.push_back(a[start].second + 1); start++; } cout << tot << endl; for (int x : res) { cout << x << ; } cout << endl; } int main() { int t = 1; for (int i = 0; i < t; i++) test_case(); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { long long a, c; cin >> a >> c; vector<long long> aa; while (a) { aa.push_back(a % 3); a /= 3; } vector<long long> cc; while (c) { cc.push_back(c % 3); c /= 3; } long long i = 0, j = 0; vector<long long> res; while (i < aa.size() && j < cc.size()) { res.push_back((cc[j] - aa[i] + 3) % 3); i++; j++; } while (i < aa.size()) { res.push_back((3 - aa[i]) % 3); i++; } while (j < cc.size()) { res.push_back(cc[j] % 3); j++; } long long p = 1; long long ans = 0; for (int i = 0; i < res.size(); i++) { ans += (res[i] * (p)); p *= 3; } cout << ans << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int sett(int n, int pos) { return n = n | (1 << pos); } int reset(int n, int pos) { return n = n & ~(1 << pos); } bool check(int n, int pos) { return (bool)(n & (1 << pos)); } template <typename T> T bigmod(T x, T p) { if (p == 0) return 1; if (p & 1) { return x * bigmod(x, p - 1) % 1000000007; } else { T tmp = bigmod(x, p >> 1) % 1000000007; return tmp * tmp % 1000000007; } } template <typename T> void print(const T& v) { cerr << v << ; } template <typename T1, typename... T2> void print(const T1& first, const T2&... rest) { print(first); print(rest...); } const int inf = (1 << 30); int a[2000123 + 4]; struct dat { long long val, id; dat(long long a, long long b) { val = a; id = b; }; bool operator<(dat x) const { return val == x.val ? id < x.id : val < x.val; } }; set<dat> st; vector<dat> v; bool compare(dat x, dat y) { return x.id < y.id; } void func(dat x) { cout << x.val << << x.id << n ; } void solve() { set<dat>::iterator it, it2, it3; while (1) { if (st.size() < 2) break; it = st.begin(); it2 = st.begin(); it2++; if (it->val != it2->val) { v.push_back(*it); st.erase(it); } else { dat x = dat(it->val + it2->val, it2->id); st.erase(it); it = st.begin(); st.erase(it); st.insert(x); } } if (st.size() == 1) { v.push_back(*(st.begin())); } sort(v.begin(), v.end(), compare); cout << v.size() << n ; for (int i = 0; i < v.size(); i++) cout << v[i].val << ; } int main() { long long cnt = 1, n, qr, k; scanf( %I64d , &n); for (int i = 1; i <= (int)n; i++) { scanf( %I64d , &k); st.insert(dat(k, (long long)i)); } solve(); return 0; }
|
#include <bits/stdc++.h> using namespace std; map<int, vector<int> > mx; map<int, vector<int> > my; int pow(int x) { long long i = 2; long long r = 1; while (x) { if (x & 1) r = (r * i) % 1000000007; i = (i * i) % 1000000007; x = x >> 1; } return r; } int main() { ios::sync_with_stdio(false); int i, j, n, t; cin >> n; int x[n]; int y[n]; long long an = 1; for (i = 0; i < n; i++) { cin >> x[i] >> y[i]; mx[x[i]].push_back(i); my[y[i]].push_back(i); } bool visit[i]; memset(visit, 0, i); set<int> sx; set<int> sy; int c; queue<int> q; for (i = 0; i < n; i++) if (!visit[i]) { c = 0; sx.clear(); sy.clear(); for (j = 0; j < mx[x[i]].size(); j++) { c++; q.push(mx[x[i]][j]); visit[mx[x[i]][j]] = 1; } sx.insert(x[i]); for (j = 0; j < my[y[i]].size(); j++) { c++; q.push(my[y[i]][j]); visit[my[y[i]][j]] = 1; } sy.insert(y[i]); t = i; while (!q.empty()) { i = q.front(); q.pop(); if (sx.find(x[i]) == sx.end()) for (j = 0; j < mx[x[i]].size(); j++) { c++; q.push(mx[x[i]][j]); visit[mx[x[i]][j]] = 1; } sx.insert(x[i]); if (sy.find(y[i]) == sy.end()) for (j = 0; j < my[y[i]].size(); j++) { c++; q.push(my[y[i]][j]); visit[my[y[i]][j]] = 1; } sy.insert(y[i]); } i = t; if (c / 2 < sx.size() + sy.size()) an = (an * (pow(sx.size() + sy.size()) + 1000000007 - 1)) % 1000000007; else an = (an * pow(sx.size() + sy.size())) % 1000000007; } cout << an; return 0; }
|
#include <bits/stdc++.h> using namespace std; long long Pow(long long a, long long b) { long long ans = 1LL, base = a % 1000000007; while (b != 0) { if (b & 1LL != 0) ans = ans * base % 1000000007; base = base * base % 1000000007; b >>= 1LL; } return ans; } long long A, B, n, x; long long ans; int main() { scanf( %lld %lld %lld %lld , &A, &B, &n, &x); long long ppow = Pow(A, n) % 1000000007; if (A == 1LL) ans = (x % 1000000007 + n % 1000000007 * B % 1000000007) % 1000000007; else ans = (ppow * x % 1000000007 + B % 1000000007 * (ppow - 1LL) % 1000000007 * Pow(A - 1LL, 1000000007 - 2) % 1000000007) % 1000000007; cout << ans; }
|
#include <bits/stdc++.h> using namespace std; void solve() { int n, d, m; cin >> n >> m >> d; int c[m], sum = 0; for (int i = 0; i < m; i++) { cin >> c[i]; sum += c[i]; } d--; if (sum + (m + 1) * d >= n) { cout << YES << endl; int k = (d > 0 ? (n - sum) / d : 0), res = (d > 0 ? (n - sum) % d : 0), cnt = 0, id = 0; bool flag = 1; for (int i = 1; i <= n; i++) { int j; if (cnt == k && res) { k++, d = res; res = 0; } if (cnt < k && flag) { for (j = i; j < i + d && j <= n; j++) { cout << 0 << ; } cnt++; } else { for (j = i; j < i + c[id]; j++) { cout << id + 1 << ; } id++; } flag = !flag; i = j - 1; } cout << endl; } else cout << NO << endl; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; while (t--) { solve(); } }
|
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e6 + 7; const int ALPHA = 40; const long long INF = 1e16 + 7; const int MOD = 1e9 + 7; const int LOG = 22; const int BASE[2] = {313, 239}; long long n, d; bool ns[MAXN], seen[MAXN]; vector<long long> group[MAXN], plast[MAXN]; void solve() { cin >> n >> d; for (int i = 0; i < n; i++) { group[i].clear(); plast[i].clear(); } for (int i = 0; i < n; i++) cin >> ns[i]; fill(seen, seen + n, 0); long long ptr = 0, cnt = 0; for (int i = 0; i < n; i++) { ptr = i; if (seen[ptr]) continue; while (!seen[ptr]) { seen[ptr] = 1; group[cnt].push_back(ns[ptr]); ptr = (ptr + d) % n; } cnt++; } for (int i = 0; i < cnt; i++) { long long k = group[i].size(); for (int j = 0; j < k; j++) group[i].push_back(group[i][j]); long long last = -INF; for (int j = 0; j < group[i].size(); j++) { if (!group[i][j]) last = j; plast[i].push_back(j - last); } } long long maxim = 0; for (int i = 0; i < cnt; i++) { for (int j = 0; j < group[i].size() / 2; j++) { maxim = max(maxim, min(plast[i][j], plast[i][j + (group[i].size() / 2)])); } } if (maxim >= INF) cout << -1 << n ; else cout << maxim << n ; } int main() { ios::sync_with_stdio(false); cin.tie(0); ; int t; cin >> t; while (t--) solve(); }
|
#include <bits/stdc++.h> using namespace std; const int maxn = 3000 + 50; vector<int> edge[maxn]; int sub[maxn][maxn], fa[maxn][maxn]; long long n, ans, dp[maxn][maxn]; void dfs(int root, int k, int dad) { sub[root][k]++; fa[root][k] = dad; for (auto t : edge[k]) { if (t != dad) { dfs(root, t, k); sub[root][k] += sub[root][t]; } } } long long DP(int a, int b) { if (a == b) return 0; if (dp[a][b] != -1) return dp[a][b]; return dp[a][b] = sub[a][b] * sub[b][a] + max(DP(fa[a][b], a), DP(fa[b][a], b)); } int main() { cin >> n; memset(dp, -1, sizeof(dp)); for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; edge[u].push_back(v); edge[v].push_back(u); } for (int i = 1; i <= n; i++) dfs(i, i, -1); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (i != j) ans = max(ans, DP(i, j)); cout << ans << endl; }
|
#include <bits/stdc++.h> using namespace std; int main() { int i, n; long long a, sum, ans; priority_queue<long long, vector<long long>, greater<long long> > Q; scanf( %d , &n); for (i = 0; i < n; i++) { scanf( %lld , &a); Q.push(a); } if (n % 2 == 0) Q.push(0); ans = 0; while (Q.size() > 2) { sum = Q.top(); Q.pop(); sum += Q.top(); Q.pop(); sum += Q.top(); Q.pop(); ans += sum; Q.push(sum); } printf( %lld n , ans); return 0; }
|
#include <cstdlib> #include <cstdarg> #include <cassert> #include <cctype> // tolower #include <ctime> #include <cmath> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <stdexcept> #include <map> #include <set> #include <list> #include <queue> #include <vector> #include <string> #include <limits> #include <utility> #include <numeric> #include <iterator> #include <algorithm> #include <functional> /* * g++ -g -std=c++11 -DBUG -D_GLIBCXX_DEBUG -Wall -Wfatal-errors -o cforce{,.cpp} * g++ -g -DBUG -D_GLIBCXX_DEBUG -Wall -Wfatal-errors -o cforce{,.cpp} * * TODO: * C++ dataframe * stl11 -> c++11 standard template lib in c++98 * overload >> for map and set, using (insert) iterator * chmod:: consider an algorithm stable to int64 overflow * shortest path algorithm * shortest path in a tree * maximum network flow * partial idx/iter sort * a prime number generator which traverses prime numbers w/ ++ * a divisor generator which traverses divisors efficiently * Apply1st ?! * Apply2nd and bind2nd ?! * count_if ( a.begin(), a.end(), a.second < x ) * Arbitrary-precision arithmetic / Big Integer / Fraction - rational num * tuple class --> cplusplus.com/reference/tuple * get<1>, get<2>, bind2nd ( foo ( get<2> pair ), val ) * isin( const T & val, first, last ) * fuinction composition in c++ * blogea.bureau14.fr/index.php/2012/11/function-composition-in-c11/ * cpp-next.com/archive/2010/11/expressive-c-fun-with-function-composition/ * TimeWrapper -- accumulate time of a function call * stackoverflow.com/questions/879408 * hash map -- possible hash value & obj % some_big_prime [ b272 ] * lower level can be a simple map to resolve hash collisions * add explicit everywhere necessary * bloom filters * heap -> how to update values in place / increase-key or decrease-key ... IterHeap ?! * median maintaince --> max_heap / min_heap * prim s min spaning tree alg. O ( m log n ) using heap contianing V - X vertices * kruskal algorithm minimum cost spanning tree with union find data structure * unique_ptr * hufman codes * simple arithmatic tests * longest common subsequence using seq. alignment type algorithm * longest common substring ( consequative subsequeance ) * Graham scan; en.wikipedia.org/wiki/Graham_scan */ /* * @recepies * ---------------------------------------------- * odd / even * transform ( x.begin(), x.end(), x.begin(), bind2nd( modulus<int>(), 2 )); * count_if ( x.begin(), x.end(), bind2nd( modulus < int > (), 2)); * Apply2nd * max_element ( m.begin(), m.end(), Apply2nd < string, int , less < int > > ) * sort ( a.begin(), a.end(), Apply2nd < char, int , greater< int > > ) * count_if ( m.begin(), m.end(), Apply2nd < string, int, modulus < int > > ) * accumulate ( m.begin(), m.end(), 0.0, Apply2nd < int, double, plus < double > > ) * accumulate ( m.begin( ), m.end( ), pair < int, double> ( 0 , 0.0 ) , operator+ < int, double > ) * abs_diff * adjacent_difference ( a.begin(), a.end(), adj_diff.begin( ), abs_diff < int > ( ) ) * accumulate ( a.begin(), a.end(), 0, abs_diff < int > ( ) ) * erase * a.erase ( remove_if ( a.begin( ), a.end( ), bind2nd ( less < int >( ), 0 ) ), a.end( ) ) * a.erase ( remove ( a.begin( ), a.end( ), b.begin( ), b.end( ) ), a.end ( ) ) * binding * bind2nd ( mem_fun_ref (& ClassName::m_func ), arg ) // binds the argument of the object * iterator generators * generate_n ( back_inserter ( a ), n, rand ); // calls push_back * generate_n ( inserter( a, a.begin( ) + 5 ), 10, RandInt( 0 , 100 ) ) // calls insert * copy ( foo.begin( ), foo.end( ), insert_iterator < std::list < double > > ( bar, bar.begin( ) + 5 )) * copy ( a.begin( ), a.end( ), ostream_iterator < double > ( cout, , )) * accumulate ( m.begin( ), m.end( ), pair < int, double> ( 0 , 0.0 ) , operator+ < int, double > ) * transform (numbers.begin(), numbers.end(), lengths.begin(), mem_fun_ref(&string::length)); */ /* * @good read * ---------------------------------------------- * [ partial ] template specialization * cprogramming.com/tutorial/template_specialization.html * function composition * cpp-next.com/archive/2010/11/expressive-c-fun-with-function-composition */ /* * @prob set * ---------------------------------------------- * purification --> c330 */ /* * @limits * ---------------------------------------------- * int 31 2.14e+09 * long int 31 2.14e+09 * unsigned 32 4.29e+09 * long unsigned 32 4.29e+09 * size_t 32 4.29e+09 * long long int 63 9.22e+18 * long long unsigned 64 1.84e+19 */ /* * issues * ---------------------------------------------- * stackoverflow.com/questions/10281809 * mem_fun -> func_obj ( pointer to instance, origanal argument ) * bind1st ( mem_fun ( & ClassName::m_func ), this ) // binds obj of the class * bind1st takes const T & as the first argument */ /* * typedef / define * ---------------------------------------------- */ typedef long long int int64; typedef unsigned long long int uint64; #define DOUBLE_INF std::numeric_limits< double >::infinity() #define DOUBLE_NAN std::numeric_limits< double >::quiet_NaN() #define DECLARE( X ) typedef shared_ptr < X > X ## _shared_ptr; typedef const shared_ptr < X > X ## _const_shared_ptr; #ifdef BUG #define DEBUG(var) { std::cout << #var << : << (var) << std::endl; } #define EXPECT(expr) if ( ! (expr) ) std::cerr << Assertion << #expr failed at << __FILE__ << : << __LINE__ << std::endl; #else #define DEBUG(var) #define EXPECT(expr) #endif #define DBG(v) std::copy( v.begin(), v.end(), std::ostream_iterator < typeof( *v.begin() )> ( std::cout, ) ) /* * http://rootdirectory.de/wiki/SSTR() * usage: * SSTR( x^2: << x*x ) */ #define SSTR( val ) dynamic_cast< std::ostringstream & >( std::ostringstream() << std::dec << val ).str() /* https://www.quora.com/C++-programming-language/What-are-some-cool-C++-tricks */ // template <typename T, size_t N> // char (&ArraySizeHelper(T (&array)[N]))[N]; // #define arraysize(array) (sizeof(ArraySizeHelper(array))) /* * forward decleration * ---------------------------------------------- */ class ScopeTimer; /* * functional utils * ---------------------------------------------- */ template < typename T > struct abs_diff : std::binary_function < T, T, T > { typedef T value_type; inline value_type operator( ) ( const value_type & x, const value_type & y ) const { return std::abs( x - y ); } }; // template < class InputIterator, class T > // class isin : public std::binary_function < InputIterator, InputIterator, bool > // { // public: // typedef T value_type; // // isin ( const InputIterator & first, const InputIterator & last ): // m_first ( first ), m_last ( last ) { } // // bool operator ( ) ( const value_type & val ) const // { // return std::find ( m_first, m_last, val ) != m_last; // } // private: // const InputIterator m_first, m_last; // } template < typename value_type, typename cont_type > class isin : public std::unary_function < value_type, bool > { public: isin( const cont_type & vals ): m_vals ( vals ) { }; bool operator ( ) ( const value_type & x ) const { return std::find ( m_vals.begin( ), m_vals.end( ), x ) != m_vals.end( ); } private: const cont_type m_vals; }; /* * max_element, min_element, count_if ... on the 2nd element * eg: max_element ( m.begin(), m.end(), Apply2nd < string, int , less < int > > ) */ template < class T1, class T2, class BinaryOperation > class Apply2nd : std::binary_function < typename std::pair < T1, T2 >, typename std::pair < T1, T2 >, typename BinaryOperation::result_type > { public: typedef T1 first_type; typedef T2 second_type; typedef typename BinaryOperation::result_type result_type; typedef typename std::pair < first_type, second_type > value_type; inline result_type operator( ) ( const value_type & x, const value_type & y ) const { return binary_op ( x.second , y.second ); } private: BinaryOperation binary_op; }; /* * algo utils * ---------------------------------------------- */ /** * count the number of inversions in a permutation; i.e. how many * times two adjacent elements need to be swaped to sort the list; * 3 5 2 4 1 --> 7 */ template < class InputIterator > typename std::iterator_traits<InputIterator>::difference_type count_inv ( InputIterator first, InputIterator last ) { typedef typename std::iterator_traits<InputIterator>::difference_type difference_type; typedef typename std::iterator_traits<InputIterator>::value_type value_type; std::list < value_type > l; /* list of sorted values */ difference_type cnt = 0; for ( difference_type n = 0; first != last; ++first, ++n ) { /* count how many elements larger than *first appear before *first */ typename std::list < value_type >::iterator iter = l.begin( ); cnt += n; for ( ; iter != l.end( ) && * iter <= * first; ++ iter, -- cnt ) ; l.insert( iter, * first ); } return cnt; } template < class ForwardIterator, class T > inline void fill_inc_seq ( ForwardIterator first, ForwardIterator last, T val ) { for ( ; first != last; ++first, ++val ) * first = val; } template <class ForwardIterator, class InputIterator > ForwardIterator remove ( ForwardIterator first, ForwardIterator last, InputIterator begin, InputIterator end ) { ForwardIterator result = first; for ( ; first != last; ++ first ) if ( find ( begin, end, *first ) == end ) { *result = *first; ++result; } return result; } /* stackoverflow.com/questions/1577475 */ template < class RAIter, class Compare > class ArgSortComp { public: ArgSortComp ( const RAIter & first, Compare comp ): m_first ( first ), m_comp( comp ) { } inline bool operator() ( const size_t & i, const size_t & j ) const { return m_comp ( m_first[ i ] , m_first[ j ] ); } private: const RAIter & m_first; const Compare m_comp; }; /*! * usage: * vector < size_t > idx; * argsort ( a.begin( ), a.end( ), idx, less < Type > ( ) ); */ template < class RAIter, class Compare > void argsort ( const RAIter & first, const RAIter & last, std::vector < size_t > & idx, Compare comp ) { const size_t n = last - first; idx.resize ( n ); for ( size_t j = 0; j < n; ++ j ) idx[ j ] = j ; std::sort ( idx.begin( ), idx.end( ), ArgSortComp< RAIter, Compare > ( first, comp ) ); } template < class RAIter, class Compare > class IterSortComp { public: IterSortComp ( Compare comp ): m_comp ( comp ) { } inline bool operator( ) ( const RAIter & i, const RAIter & j ) const { return m_comp ( * i, * j ); } private: const Compare m_comp; }; /*! * usage: * vector < list < Type >::const_iterator > idx; * itersort ( a.begin( ), a.end( ), idx, less < Type > ( ) ); */ template <class INIter, class RAIter, class Compare> void itersort ( INIter first, INIter last, std::vector < RAIter > & idx, Compare comp ) { /* alternatively: stackoverflow.com/questions/4307271 */ idx.resize ( std::distance ( first, last ) ); for ( typename std::vector < RAIter >::iterator j = idx.begin( ); first != last; ++ j, ++ first ) * j = first; std::sort ( idx.begin( ), idx.end( ), IterSortComp< RAIter, Compare > (comp ) ); } /* * string utils * ---------------------------------------------- */ inline void erase ( std::string & str, const char & ch ) { std::binder2nd < std::equal_to < char > > isch ( std::equal_to < char > ( ), ch ); std::string::iterator iter = std::remove_if ( str.begin(), str.end(), isch ); str.erase ( iter, str.end() ); } inline void erase ( std::string & str, const std::string & chrs ) { isin < char, std::string > isin_chrs ( chrs ); std::string::iterator iter = std::remove_if ( str.begin(), str.end(), isin_chrs ); str.erase ( iter, str.end() ); } template < typename value_type> inline std::string val2str ( const value_type & x ) { std::ostringstream sout ( std::ios_base::out ); sout << x; return sout.str(); } template < typename value_type> inline value_type str2val ( const std::string & str ) { std::istringstream iss ( str, std::ios_base::in ); value_type val; iss >> val; return val; } std::vector< std::string > tokenize ( const std::string & str, const char & sep ) { /*! * outputs empty tokens and assumes str does not start with sep * corner cases: * empty string, one char string, * string starting/ending with sep, all sep, end with two sep */ std::vector < std::string > res; std::string::const_iterator follow = str.begin( ), lead = str.begin( ); while ( true ) { while ( lead != str.end() && * lead != sep ) ++ lead; res.push_back ( std::string( follow, lead ) ); if ( lead != str.end ( ) ) follow = 1 + lead ++ ; else break; } return res; } /*! * chunk a string into strings of size [ at most ] k */ void chunk ( const std::string::const_iterator first, const std::string::const_iterator last, const size_t k, const bool right_to_left, std::list < std::string > & str_list ) { str_list.clear( ); if ( right_to_left ) /* chunk from the end of the string */ for ( std::string::const_iterator i, j = last; j != first; j = i ) { i = first + k < j ? j - k : first; str_list.push_back ( std::string ( i, j ) ); } else /* chunk from the begining of the string */ for ( std::string::const_iterator i = first, j; i != last; i = j ) { j = i + k < last ? i + k : last; str_list.push_back ( std::string ( i, j ) ); } } /*! * next lexicographically smallest string * within char set a..z */ std::string & operator++( std::string & s ) { /* find the first char from right less than z */ std::string::reverse_iterator j = find_if( s.rbegin( ), s.rend( ), std::bind2nd( std::less < char > ( ), z )); if ( j != s.rend( )) { ++ *j; std::fill( s.rbegin( ), j, a ); } else s.assign( s.length( ) + 1, a ); return s; } /*! * getline ( cin, str ) * requires ctrl-D * cin >> str; does not pass after space char */ /* * number utils * ---------------------------------------------- */ class BigInteger { #if ULONG_MAX <= 1 << 32 typedef long long unsigned val_type; #else typedef long unsigned val_type; #endif const static int WSIZE = 32; const static val_type BASE = 1LL << WSIZE; public: private: std::list < val_type > val; /* val[ 0 ] is most significant */ bool pos; /* true if sign is positive */ }; /** * greatest common divisor - Euclid s alg. */ template < typename value_type > inline value_type gcd ( value_type a, value_type b ) { return ! b ? a : gcd( b, a % b ); // if ( a < 0 ) a = -a ; // if ( b < 0 ) b = -b ; // if ( a < b ) std::swap ( a, b ); /* corner case: one of the values is zero */ // if ( ! b ) return a; // for ( value_type r = a % b; r ; r = a % b ) // { // a = b; // b = r; // } // return b; } /** * prime factorization */ template < class T > void prime_factors( T n, std::map < T, size_t > & fac ) { for ( T k = 2; n > 1; ++ k ) if ( ! ( n % k ) ) { size_t & ref = fac[ k ]; while ( ! ( n % k ) ) { ++ ref; n /= k; } } } /* abs diff - safe for unsigned types */ template < class T > inline T absdiff( T a, T b ) { return a < b ? b - a : a - b; } namespace { template < class T > std::pair < T, T > __extgcd ( const T & x0, const T & y0, const T & x1, const T & y1, const T & r0, const T & r1 ) { const T q = r0 / r1; const T r2 = r0 % r1; if ( ! ( r1 % r2 ) ) return std::make_pair < T, T > ( x0 - q * x1, y0 - q * y1 ); const T x2 = x0 - q * x1; const T y2 = y0 - q * y1; return __extgcd ( x1, y1, x2, y2, r1, r2 ); } } /** * extended euclidean algorithm: a x + b y = gcd( a, b) * en.wikipedia.org/wiki/Extended_Euclidean_algorithm */ template < class value_type > inline std::pair < value_type, value_type > extgcd ( value_type a, value_type b ) { return a % b ? __extgcd < value_type > ( 1, 0, 0, 1, a, b ) : std::make_pair < value_type, value_type > ( 0, 1 ); } /** * modular multiplicative inverse * en.wikipedia.org/wiki/Modular_multiplicative_inverse */ template < class value_type > inline value_type modinv ( value_type a, value_type m ) { const std::pair < value_type, value_type > coef ( extgcd( a, m ) ); /* a needs to be coprime to the modulus, or the inverse won t exist */ if ( a * coef.first + m * coef.second != 1 ) throw std::runtime_error ( val2str( a ) + is not coprime to + val2str( m )); /* return a pos num between 1 & m-1 */ return ( m + coef.first % m ) % m; } inline bool isnan ( const double & a ) { return ! ( a == a ); } template < typename value_type > inline value_type mini ( int n, ... ) { va_list vl; va_start (vl, n); value_type res = va_arg ( vl, value_type ); for ( int i = 1; i < n; ++i ) { const value_type val = va_arg ( vl, value_type ); res = std::min ( res, val ); } va_end( vl ); return res; } template < typename value_type > inline value_type maxi ( int n, ... ) { va_list vl; va_start (vl, n); value_type res = va_arg ( vl, value_type ); for ( int i = 1; i < n; ++i ) { const value_type val = va_arg ( vl, value_type ); res = std::max ( res, val ); } va_end( vl ); return res; } // XXX look this up how is this implemented template < class T > inline int sign ( const T & x ) { if ( x == T() ) return 0; else if ( x < T() ) return -1; else return 1; } /* * change moduluos from n to m */ std::string chmod ( std::string num, const unsigned n, const unsigned m ) { const char * digit = 0123456789abcdefghijklmnopqrstuvwxyz ; std::transform ( num.begin(), num.end(), num.begin(), tolower ); isin < char, std::string > is_alpha_num ( digit ); assert ( find_if ( num.begin( ), num.end( ), std::not1 ( is_alpha_num ) ) == num.end( )); unsigned long long int val ( 0 ); if ( n == 10U ) { std::istringstream iss ( num, std::ios_base::in ); iss >> val; } else for ( std::string::const_iterator iter = num.begin( ); iter != num.end( ); ++ iter ) val = val * n + ( a <= *iter ? *iter - a + 10U : *iter - 0 ); if ( m == 10U ) { std::ostringstream sout ( std::ios_base::out ); sout << val; return sout.str ( ); } else { std::string res; for ( ; val ; val /= m ) res.push_back( digit [ val % m ] ); return res.length( ) ? std::string( res.rbegin( ), res.rend( )) : 0 ; } } template < class value_type > /* a^n mod m */ value_type powmod ( value_type a, const value_type & n, const value_type & m ) { if ( a == 1 || ! n ) return m != 1 ? 1 : 0; value_type res = 1; for ( value_type k = 1; k <= n; a = a * a % m, k = k << 1 ) if ( k & n ) res = ( res * a ) % m; return res; } /* * Fermat pseudoprime test * www.math.umbc.edu/~campbell/Computers/Python/numbthy.py * NOTE: since return type is bool, and powmod may break for ints, * the argument is always casted to long long */ inline bool is_pseudo_prime ( const long long & a ) { /* all the primes less than 1000 ( 168 primes )*/ const long long p [ ] = { 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73, 79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157, 163,167,173,179,181,191,193,197,199,211,223,227,229,233,239, 241,251,257,263,269,271,277,281,283,293,307,311,313,317,331, 337,347,349,353,359,367,373,379,383,389,397,401,409,419,421, 431,433,439,443,449,457,461,463,467,479,487,491,499,503,509, 521,523,541,547,557,563,569,571,577,587,593,599,601,607,613, 617,619,631,641,643,647,653,659,661,673,677,683,691,701,709, 719,727,733,739,743,751,757,761,769,773,787,797,809,811,821, 823,827,829,839,853,857,859,863,877,881,883,887,907,911,919, 929,937,941,947,953,967,971,977,983,991,997 }; const size_t n = sizeof( p ) / sizeof ( p[ 0 ] ); if ( a < p[ n - 1 ] + 1) return std::binary_search ( p, p + n , a ); if ( std::find_if ( p, p + n, std::not1 ( std::bind1st ( std::modulus< long long >( ), a ))) != p + n ) return false; const size_t k = a < 9006401LL ? 3 : a < 10403641LL ? 4 : a < 42702661LL ? 5 : a < 1112103541LL ? 6 : 7; for ( size_t j = 0; j < k; ++ j ) if ( powmod ( p[ j ], a - 1, a ) != 1 ) return false; return true; } /* * returns a sorted vector of all primes less than or equal to n * maximum adj diff of all primes less than 1e5 is 72 ( 114 for 1e6 ) */ template < typename value_type > std::vector < value_type > get_primes ( const value_type n ) { #ifdef BUG ScopeTimer scope_timer ( std::vector < value_type > get_primes ( const value_type n ) ); #endif typedef typename std::vector < value_type >::iterator iterator; std::vector < value_type > primes; for ( value_type k = 2 ; k <= n; ++ k ) if ( is_pseudo_prime ( k ) ) { const value_type sqrt_k = 1 + static_cast < value_type > ( sqrt ( k + 1 ) ); iterator iend = upper_bound ( primes.begin( ), primes.end( ), sqrt_k ); if ( find_if ( primes.begin( ), iend, std::not1 ( std::bind1st ( std::modulus< value_type >( ), k ) ) ) != iend ) continue; primes.push_back ( k ); } return primes; } template < class T > inline std::list < std::pair < T, size_t > > get_prime_fact ( T a ) { std::list < std::pair < T, size_t > > fac; for ( T k = 2; a > 1; ++ k ) if ( ! ( a % k ) ) // no need to check if k is prime { size_t m = 0; for ( ; ! ( a % k ) ; ++m, a/= k ) ; fac.push_back ( std::pair < T, size_t > ( k, m ) ); } return fac; } template < class T > T n_choose_k ( T n, T k ) { if ( k > n ) return 0; const T lb = std::min ( k, n - k ) + 1; const T ub = n - lb + 1; T res = 1, j = 2; while ( n > ub && j < lb) { res *= n--; while ( j < lb and ! (res % j) ) res /= j++; } while ( n > ub ) res *= n--; return res; } /** * median calculator, using two heaps */ template < class InputIter > inline std::pair < typename InputIter::value_type, typename InputIter::value_type > median ( InputIter first, InputIter last ) { typedef typename InputIter::value_type value_type; typedef std::pair< value_type, value_type > result_type; /* * max_heap: * - the lower half of the elements * - the biggest of such elements is on the top */ std::vector < value_type > max_heap, min_heap; /* * comp argument to heap algorithm should provide * strict weak ordering ; in particular * std::not2 ( std::less < value_type > ) * does not have such a strict weak ordering; */ std::less < value_type > max_heap_comp; std::greater < value_type > min_heap_comp; if ( first == last ) /* corner case: empty vector */ throw std::runtime_error ( median of an empty vector is undefined! ); InputIter iter = first; max_heap.push_back ( * iter ); for ( ++iter ; iter != last; ++ iter ) if ( * iter < max_heap.front() ) { max_heap.push_back ( * iter ); std::push_heap ( max_heap.begin(), max_heap.end(), max_heap_comp ); if ( min_heap.size() + 1 < max_heap.size() ) { /* max_heap has got too large */ min_heap.push_back( max_heap.front() ); std::push_heap( min_heap.begin(), min_heap.end(), min_heap_comp ); std::pop_heap( max_heap.begin(), max_heap.end(), max_heap_comp ); max_heap.pop_back(); } } else { min_heap.push_back ( * iter ); std::push_heap ( min_heap.begin(), min_heap.end(), min_heap_comp ); if ( max_heap.size() + 1 < min_heap.size() ) { /* min_heap has got too large */ max_heap.push_back( min_heap.front() ); std::push_heap( max_heap.begin(), max_heap.end(), max_heap_comp ); std::pop_heap( min_heap.begin(), min_heap.end(), min_heap_comp ); min_heap.pop_back(); } } DEBUG( max_heap ); DEBUG( min_heap ); return min_heap.empty( ) /* corner case: ++first = last */ ? result_type ( *first, *first ) : result_type ( max_heap.size() < min_heap.size() ? min_heap.front() : max_heap.front(), min_heap.size() < max_heap.size() ? max_heap.front() : min_heap.front() ); } /* * geometry util * ---------------------------------------------- */ struct xyPoint { double x, y; xyPoint ( const double & a = .0, const double & b = .0 ): x ( a ), y( b ) { }; }; struct xyCircle { xyPoint center; double radius; }; std::ostream & operator<< ( std::ostream & out, const xyPoint & p ) { out << ( << p.x << , << p.y << ) ; return out; } std::istream & operator>> ( std::istream & ist, xyPoint & p ) { ist >> p.x >> p.y; return ist; } std::ostream & operator<< ( std::ostream & out, const xyCircle & o ) { out << {( << o.center.x << , << o.center.y << ) << o.radius << } ; return out; } std::istream & operator>> ( std::istream & ist, xyCircle & o ) { ist >> o.center.x >> o.center.y >> o.radius; return ist; } inline double cartesian_dist ( const xyPoint & a, const xyPoint & b ) { const double d = a.x - b.x; const double e = a.y - b.y; return std::sqrt ( d * d + e * e ); } class xyLine { public: xyLine ( const xyPoint & , const xyPoint & ); xyLine ( const double slope, const double intercept ); /* * signed orthogonal distance; the sign is useful * to compare which side of the line the point is */ inline double orth_dist ( const xyPoint & ) const; private: double m_slope; double m_intercept; double m_normfac; /* normalization factor for orth_dist calc */ bool m_vertical; /* if the line is verticcal */ double m_xcross; /* x axis cross point for vertical line */ }; xyLine::xyLine ( const xyPoint & a, const xyPoint & b ) { if ( a.x == b.x ) /* vertical line */ { m_vertical = true; m_xcross = a.x; m_intercept = DOUBLE_NAN; m_slope = DOUBLE_INF; m_normfac = DOUBLE_NAN; } else { m_vertical = false; m_xcross = DOUBLE_NAN; m_slope = ( b.y - a.y ) / ( b.x - a.x ); m_intercept = a.y - m_slope * a.x; m_normfac = std::sqrt ( m_slope * m_slope + 1.0 ); } } xyLine::xyLine ( const double slope, const double intercept ): m_slope ( slope ), m_intercept ( intercept ) { m_vertical = false; m_xcross = DOUBLE_NAN; m_normfac = std::sqrt ( m_slope * m_slope + 1.0 ); } double xyLine::orth_dist ( const xyPoint & o ) const /* signed orthogonal distance */ { if ( m_vertical ) return o.x - m_xcross; else return ( m_slope * o.x - o.y + m_intercept ) / m_normfac; } inline double triangle_area ( const xyPoint & a, const xyPoint & b, const xyPoint & c ) { const xyLine l ( a, b ); const double h = std::abs ( l.orth_dist ( c ) ); const double e = cartesian_dist ( a, b ); return h * e; } /* * operator<< overrides * ---------------------------------------------- */ namespace { /* helper function to output containers */ template < typename T > std::ostream & __output ( std::ostream & out, const T & a ) { typedef typename T::const_iterator const_iterator; out << { ; // does not work for pair value type // std::copy ( a.begin( ), a.end( ), std::ostream_iterator < typename T::value_type > ( std::cout, , )); for ( const_iterator iter = a.begin(); iter != a.end(); ++ iter ) out << ( iter != a.begin( ) ? , : ) << *iter ; return out << } ; } } template < typename key_type, typename value_type > std::ostream & operator<< ( std::ostream & out, const std::pair < key_type, value_type > & p) { out << ( << p.first << , << p.second << ) ; return out; } template < typename key_type, typename value_type, typename comp > std::ostream & operator<< ( std::ostream & out, const std::map < key_type, value_type, comp > & m ) { return __output ( out, m ); } template < typename value_type > std::ostream & operator<< ( std::ostream & out, const std::set < value_type > & s ) { return __output ( out, s ); } template < typename value_type > std::ostream & operator<< ( std::ostream & out, const std::vector < value_type > & a ) { return __output ( out, a ); } template < typename value_type > std::ostream & operator<< ( std::ostream & out, const std::list < value_type > & a ) { return __output ( out, a ); } template < typename value_type > std::ostream & operator<< ( std::ostream & out, const std::vector < std::vector < value_type > > & a ) { typedef typename std::vector < std::vector < value_type > >::const_iterator const_iterator; for ( const_iterator iter = a.begin( ); iter != a.end( ); ++ iter ) out << n << *iter ; return out; } /* * operator>> overrides * ---------------------------------------------- */ template < typename key_type, typename value_type > std::istream & operator>> ( std::istream & in, std::pair < key_type, value_type > & p) { in >> p.first >> p.second; return in; } template < typename value_type > std::istream & operator>> ( std::istream & in, std::vector < value_type > & a ) { typedef typename std::vector < value_type >::iterator iterator; if ( ! a.size( ) ) { size_t n; in >> n; a.resize( n ); } for ( iterator iter = a.begin(); iter != a.end(); ++ iter ) in >> * iter; return in; } /* * readin quick utilities * ---------------------------------------------- */ // template < typename value_type > // inline void readin ( std::vector < value_type > & a, size_t n = 0, std::istream & in = std::cin ) // { // // if ( ! n ) std::cin >> n; // if ( ! n ) in >> n ; // a.resize ( n ); // // std::cin >> a; // in >> a; // } // XXX consider removing // template < typename key_type, typename value_type > // inline void readin (std::vector < std::pair < key_type , value_type > > & a, size_t n = 0 ) // { // if ( !n ) std::cin >> n; // a.resize( n ); // std::cin >> a; // } /* * pair utility * ---------------------------------------------- */ /* * accumulate ( m.begin( ), m.end( ), pair < int, double> ( 0 , 0.0 ) , operator+ < int, double > ); * stackoverflow.com/questions/18640152 */ template < typename T1, typename T2 > inline std::pair < T1, T2 > operator+ ( const std::pair < T1, T2 > & a, const std::pair < T1, T2 > & b ) { return std::make_pair < T1, T2 > ( a.first + b.first, a.second + b.second ); } template < typename T1, typename T2 > inline std::pair < T1, T2 > operator- ( const std::pair < T1, T2 > & a, const std::pair < T1, T2 > & b ) { return std::make_pair < T1, T2 > ( a.first - b.first, a.second - b.second ); } // template < class T1, class T2, class BinaryOperation > // class Apply2nd : std::binary_function < typename std::pair < T1, T2 >, // typename std::pair < T1, T2 >, // typename BinaryOperation::result_type > namespace { /*! * helper template to do the work */ template < size_t J, class T1, class T2 > struct Get; template < class T1, class T2 > struct Get < 0, T1, T2 > { typedef typename std::pair < T1, T2 >::first_type result_type; static result_type & elm ( std::pair < T1, T2 > & pr ) { return pr.first; } static const result_type & elm ( const std::pair < T1, T2 > & pr ) { return pr.first; } }; template < class T1, class T2 > struct Get < 1, T1, T2 > { typedef typename std::pair < T1, T2 >::second_type result_type; static result_type & elm ( std::pair < T1, T2 > & pr ) { return pr.second; } static const result_type & elm ( const std::pair < T1, T2 > & pr ) { return pr.second; } }; } template < size_t J, class T1, class T2 > typename Get< J, T1, T2 >::result_type & get ( std::pair< T1, T2 > & pr ) { return Get < J, T1, T2 >::elm( pr ); } template < size_t J, class T1, class T2 > const typename Get< J, T1, T2 >::result_type & get ( const std::pair< T1, T2 > & pr ) { return Get < J, T1, T2 >::elm( pr ); } /* * graph utils * ---------------------------------------------- */ /* * Dijkstra :: single-source shortest path problem for * a graph with non-negative edge path costs, producing * a shortest path tree * en.wikipedia.org/wiki/Dijkstra s_algorithm */ template < typename DistType > void Dijekstra ( const size_t & source, const std::vector < std::list < size_t > > & adj, // adjacency list const std::vector < std::vector < DistType > > & edge_len, // pair-wise distance for adjacent nodes std::vector < DistType > & dist, // distance from the source std::vector < size_t > prev ) // previous node in the shortest path tree { // TODO } // TODO http://en.wikipedia.org/wiki/Shortest_path_problem // TODO Graph class, Weighted graph, ... /* * maximum cardinality matching in a bipartite graph * G = G1 ∪ G2 ∪ {NIL} * where G1 and G2 are partition of graph and NIL is a special null vertex * https://en.wikipedia.org/wiki/Hopcroft-Karp_algorithm */ class HopcroftKarp { public: HopcroftKarp ( const std::vector < std::list < size_t > > & adj, const std::vector < bool > & tag ); size_t get_npair ( ) { return npair; }; std::map < size_t, size_t > get_map ( ); private: bool mf_breadth_first_search ( ); // breadth first search from unpaired nodes in G1 bool mf_depth_first_search ( const size_t v ); // dfs w/ toggeling augmenting paths const std::vector < std::list < size_t > > & m_adj; // adjacency list for each node const std::vector < bool > & m_tag; // binary tag distinguishing partitions size_t npair; const size_t NIL; // special null vertex const size_t INF; // practically infinity distance std::vector < size_t > m_g1; // set of nodes with tag = true std::vector < size_t > m_dist; // dist from unpaired vertices in G1 std::vector < size_t > m_pair; }; std::map < size_t, size_t > HopcroftKarp::get_map ( ) { std::map < size_t, size_t > m; for ( size_t j = 0; j < m_pair.size( ); ++ j ) if ( m_pair[ j ] != NIL && m_tag[ j ]) m[ j ] = m_pair[ j ]; return m; } HopcroftKarp::HopcroftKarp ( const std::vector < std::list < size_t > > & adj, const std::vector < bool > & tag ): m_adj ( adj ), m_tag ( tag ), npair ( 0 ), NIL ( adj.size( )), INF ( adj.size( ) + 1 ), m_dist ( std::vector < size_t > ( adj.size( ) + 1, INF)), m_pair ( std::vector < size_t > ( adj.size( ), NIL )) // initially everything is paired with nil { assert ( m_adj.size() == m_tag.size() ); for ( size_t j = 0; j < tag.size( ); ++ j ) if ( tag[ j ] ) m_g1.push_back ( j ); while ( mf_breadth_first_search ( ) ) for ( std::vector < size_t >::const_iterator v = m_g1.begin( ); v != m_g1.end( ); ++ v ) if ( m_pair[ *v ] == NIL && mf_depth_first_search ( *v ) ) ++ npair; } bool HopcroftKarp::mf_breadth_first_search( ) { /* only nodes from g1 are queued */ std::queue < size_t > bfs_queue; /* initialize queue with all unpaired nodes from g1 */ for ( std::vector < size_t >::const_iterator v = m_g1.begin( ); v != m_g1.end( ); ++v ) if ( m_pair[ *v ] == NIL ) { m_dist[ *v ] = 0; bfs_queue.push ( *v ); } else m_dist[ *v ] = INF; m_dist[ NIL ] = INF; /* find all the shortest augmenting paths to node nil */ while ( ! bfs_queue.empty() ) { const size_t v = bfs_queue.front( ); bfs_queue.pop ( ); if ( m_dist[ v ] < m_dist[ NIL ] ) for ( std::list < size_t >::const_iterator u = m_adj[ v ].begin( ); u != m_adj[ v ].end( ); ++ u ) if ( m_dist[ m_pair[ * u ] ] == INF ) { m_dist[ m_pair[ * u ] ] = m_dist[ v ] + 1; bfs_queue.push ( m_pair[ * u ] ); } } return m_dist[ NIL ] != INF; } bool HopcroftKarp::mf_depth_first_search( const size_t v ) { if ( v == NIL ) return true; else { for ( std::list < size_t >::const_iterator u = m_adj[ v ].begin( ); u != m_adj[ v ].end( ); ++ u ) if ( m_dist[ m_pair[ *u ] ] == m_dist[ v ] + 1 && mf_depth_first_search( m_pair[ *u ] )) { /* * there is an augmenting path to nil from m_pair[ *u ] * and hence there is an augmenting path from v to u and * u to to nil; therefore v and u can be paired together */ m_pair [ *u ] = v; m_pair [ v ] = *u; return true; } m_dist[ v ] = INF; return false; } } /* * data-structure utility * ---------------------------------------------- */ template < class T, class Comp = std::less< T > > class Heap /* less< T > --> max-heap */ { typedef T value_type; typedef typename std::vector < value_type >::size_type size_type; public: /* * stackoverflow.com/questions/10387751 * possible work-around: a memebr pointer to m_val * * TODO: static/friend heapify ( val, & heap ) XXX O( n ) ?! */ Heap ( ): m_val ( std::vector < value_type > ( ) ) , m_comp ( Comp( ) ){ } template < class InputIter > Heap ( InputIter first, InputIter last ): m_val ( std::vector < value_type > ( ) ) , m_comp( Comp( ) ) { for ( ; first != last ; ++ first ) m_val.push_back ( * first ); std::make_heap( m_val.begin( ), m_val.end( ), m_comp ); } /*! * to avoid destroying heap property, front( ) * should always return a const reference */ inline const value_type & front( ) const { return m_val.front( ); } inline bool empty( ) const { return m_val.empty( ); } inline size_type size( ) const { return m_val.size( ); } inline void push ( const value_type & a ) { m_val.push_back( a ); std::push_heap( m_val.begin( ), m_val.end( ), m_comp ); } inline void pop( ) { std::pop_heap ( m_val.begin( ), m_val.end( ), m_comp ); m_val.pop_back( ); } // inline void swap( Heap< T, Comp> & other ) { m_val.swap( other.m_val ) }; // void sort( ) { std::sort_heap ( m_val.begin( ), m_val.end( ), m_comp ); } // template < class X, class Y > // friend std::ostream & operator<<( std::ostream & out, const Heap < X, Y> & heap ); private: std::vector < value_type > m_val; const Comp m_comp; }; /* * boost.org/doc/libs/1_54_0/libs/smart_ptr/shared_ptr.htm */ template < class Type > class shared_ptr { typedef Type value_type; public: explicit shared_ptr ( value_type * p = NULL ) : ptr ( p ), count ( new size_t ( 1U ) ) { } shared_ptr ( const shared_ptr < value_type > & sp ): ptr ( sp.ptr ), count ( sp.count ) { ++ * count; } ~ shared_ptr ( ) { release( ); } bool operator== ( const shared_ptr < value_type > & sp ) { return ptr == sp.ptr; } bool operator!= ( const shared_ptr < value_type > & sp ) { return ptr != sp.ptr; } shared_ptr < value_type > & operator= ( const shared_ptr < value_type > & sp ) { if ( this != & sp && ptr != sp.ptr ) { release( ); ptr = sp.ptr; count = sp.count; ++ * count; } return * this; } value_type * operator-> ( ) { return ptr ; } value_type & operator* ( ) { return *ptr ; } const value_type * operator-> ( ) const { return ptr ; } const value_type & operator* ( ) const { return *ptr; } void swap ( shared_ptr < value_type > & sp ) { if ( this != &sp && ptr != sp.ptr ) { std::swap ( ptr, sp.ptr ); std::swap ( count, sp.count ); } } private: void release ( ) { /* stackoverflow.com/questions/615355 */ -- * count; if ( ! * count ) { delete count; delete ptr; count = NULL; ptr = NULL; } } value_type * ptr; size_t * count; }; /*! * union find data structure with * - lazy unions * - union by rank * - path compression */ class UnionFind { public: UnionFind( const size_t n ): parent ( std::vector < size_t > ( n ) ), /* initialize each node as its own */ rank ( std::vector < size_t > ( n, 0 )) /* parent and set all the ranks to 0 */ { for ( size_t j = 0; j < n; ++ j ) parent[ j ] = j ; } inline size_t find( const size_t s ) { /* * perform path compresion and add shortcut * if parent[ s ] is not a root node */ const size_t p = parent[ s ]; return parent[ p ] == p ? p : parent[ s ] = find( p ) ; } inline void lazy_union ( size_t i, size_t j ) { /* unions should be done on root nodes */ i = find( i ); j = find( j ); if ( i != j ) { if ( rank [ i ] < rank[ j ] ) parent[ i ] = j; else { parent[ j ] = i; rank[ i ] += rank[ i ] == rank[ j ]; } } } private: std::vector < size_t > parent; std::vector < size_t > rank; }; // TODO XXX // template < class NumType > // unsigned num_hash_func ( const NumType & a ) // { // // XXX what will happen in case of overflow? // return static_cast < unsigned > ( a % 9973 ) % 9973 ; // } /* * XXX: HashMap: map< Key, T > data [ 9973 ] * data [ num_hash_func ( key ) ][ key ] */ /* * testing util * ---------------------------------------------- */ // TODO add a preprocessor which automatically includes the funciton name, or __line__ // and disables if not in debug mode /* prints the life length of the object when it goes out of scope */ class ScopeTimer { public: ScopeTimer ( const std::string & msg = ): tic ( clock ( )), m_msg( msg ) { }; ~ ScopeTimer ( ) { const clock_t toc = clock(); const uint64 dt = 1000L * ( toc - tic ) / CLOCKS_PER_SEC; const uint64 mil = dt % 1000L; const uint64 sec = ( dt / 1000L ) % 60L; const uint64 min = ( dt / 60000L ) % 60L; const uint64 hrs = ( dt / 3600000L ); std::cout << n << m_msg << n telapsed time: ; if ( hrs ) std::cout << hrs << hrs, ; if ( min ) std::cout << min << min, ; if ( sec ) std::cout << sec << sec, ; std::cout << mil << mil-sec n ; } private: typedef unsigned long long int uint64; const clock_t tic; const std::string m_msg; }; class RandInt { public: RandInt ( int a = 0, int b = 100 ): m ( a ), f ( static_cast < double > ( b - a ) / RAND_MAX ) { } inline int operator() ( ) { return m + std::ceil ( f * rand( ) ); } private: const int m; const double f; }; class RandDouble { public: RandDouble ( double a = 0.0, double b = 1.0 ): m ( a ), f ( ( b - a ) / RAND_MAX ) { } inline double operator() ( ) { return m + f * rand( ); } private: const double m, f; }; class Noisy { public: Noisy ( std::string str ): msg ( str ) { std::cout << Noisy ( << msg << ) t@ << this << std::endl; } ~Noisy ( ) { std::cout << ~Noisy ( << msg << ) t@ << this << std::endl; } void beep ( ) { std::cout << beep ( << msg << ) t@ << this << std::endl; } void beep ( ) const { std::cout << const beep ( << msg << ) t@ << this << std::endl; } private: const std::string msg; }; DECLARE ( Noisy ); /* * ---------------------------------------------- * ---------------------------------------------- */ /* * -- @@@ ------------------------------------------------- */ using namespace std; // struct Comp // { // // bool operator() ( const pair < int, int > & x, const pair < int, int > & y ) // // { // // return abs( x.first ) + abs( x.second ) < abs( y.first ) + abs( y.second ); // // } // // // bool operator( ) ( const xyCircle & a, const xyCircle & b ) // // { // // return a.radius < b.radius; // // } // // // bool operator( ) ( const pair < size_t, size_t > & pr, const size_t & x ) // { // return pr.second < x; // } // }; void probA() { vector <size_t> a; cin >> a; sort( a.begin(), a.end() ); copy ( a.begin( ), a.end( ), ostream_iterator < size_t > ( cout, )); } size_t probB( ) { size_t n; string str; cin >> n >> str; // const size_t n = str.size(); const size_t INF = n + 2; vector < size_t > ldist( n ), rdist( n); rdist[ 0 ] = str[ 0 ] == R ? 0 : INF; for ( size_t j = 1; j < n; ++ j ) switch ( str[ j ] ) { case R : rdist[ j ] = 0; break; case L : rdist[ j ] = INF; break; case . : rdist[ j ] = 1 + rdist[ j - 1 ]; break; } ldist[ n - 1 ] = str[ n - 1 ] == L ? 0 : INF; for ( size_t j = n - 1; j > 0; -- j ) switch ( str[ j - 1] ) { case L : ldist[ j - 1] = 0; break; case R : ldist[ j - 1] = INF; break; case . : ldist[ j - 1] = 1 + ldist[ j ]; break; } DEBUG( ldist ); DEBUG( rdist ); size_t cnt = 0; for ( size_t j = 0; j < n; ++ j ) if ( ( INF <= ldist[j] && INF <= rdist[j] ) || ldist[ j ] == rdist[ j ] ) ++ cnt; return cnt; } int main ( const int argc, char * argv [ ]) { cout << probB(); // c79( ); // cout << c343( ); // cout << setprecision( 10 ) return EXIT_SUCCESS; } /** * mislav.uniqpath.com/2011/12/vim-revisited/ * set encoding=utf-8 * %s/ (. {60,70 } ) / 1 r/gc * %s/ / /gc * %s/10 ([0-9] {1,2 } )/10^ 1/gc */
|
#include <bits/stdc++.h> using namespace std; int main() { int n, k, pg, pt; scanf( %d %d , &n, &k); char a[105]; scanf( %s , a); for (int i = 0; i < n; i++) { if (a[i] == G ) pg = i; if (a[i] == T ) pt = i; } int x = abs(pg - pt); if (x % k == 0) { int fg = 0, st, ed; if (pg < pt) { st = pg; ed = pt + 1; } else { st = pt; ed = pg + 1; } for (int i = st; i < ed; i++) { if (a[i] == # ) { int xx = abs(pg - i); if (xx % k == 0) { fg = 1; break; } } } if (fg == 1) printf( NO n ); else printf( YES n ); } else printf( NO n ); return 0; }
|
#include <bits/stdc++.h> using namespace std; int n, m, k, trap[510]; int e[510][510]; long double t[510][510]; const long double EPS = 1e-9; void mult(long double a[110][110], long double b[110][110], long double c[110][110]) { long double r[110][110] = {}; for (int i = 0; i < (int)(110); i++) for (int j = 0; j < (int)(110); j++) for (int k = 0; k < (int)(110); k++) r[i][k] += a[i][j] * b[j][k]; for (int i = 0; i < (int)(110); i++) for (int j = 0; j < (int)(110); j++) c[i][j] = r[i][j]; } int main() { scanf( %d %d %d , &n, &m, &k); for (int i = 0; i < n; i++) scanf( %d , &trap[i]); while (m--) { int x, y; scanf( %d %d , &x, &y); --x; --y; ++e[x][y]; ++e[y][x]; } for (int i = 0; i < n; i++) if (!trap[i]) { int deg = 0; for (int j = 0; j < n; j++) deg += e[i][j]; for (int j = 0; j < n; j++) t[i][j] -= (long double)e[i][j] / deg; t[i][i] += 1.0; } else { t[i][i] = 1.0; } for (int i = 0; i < n; i++) if (!trap[i]) { int j = i; for (int k = i; k < n; k++) if (abs(t[k][i]) > abs(t[j][i])) j = k; for (int k = 0; k < n; k++) swap(t[i][k], t[j][k]); double scale = t[i][i]; for (int k = 0; k < n; k++) t[i][k] /= scale; for (int j = 0; j < n; j++) if (j != i) { scale = t[j][i]; for (int k = 0; k < n; k++) t[j][k] -= t[i][k] * scale; } } for (int i = 0; i < n; i++) if (trap[i]) t[i][i] = -1.0; long double m[110][110] = {}, v[510][510] = {}, r[110][110] = {}; int nr[510], next = 0; for (int i = 0; i < n; i++) if (trap[i]) nr[i] = next++; for (int i = 0; i < n; i++) if (trap[i]) { int deg = 0; for (int j = 0; j < n; j++) deg += e[i][j]; for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) v[i][k] -= t[j][k] / deg * e[i][j]; for (int j = 0; j < n; j++) if (trap[j]) m[nr[i]][nr[j]] = v[i][j]; } for (int i = 0; i < next; i++) r[i][i] = 1.0; k -= 2; while (k) { if (k & 1) mult(m, r, r); mult(m, m, m); k /= 2; } long double ans = 0.0; for (int i = 0; i < n; i++) if (trap[i]) { ans += -t[0][i] * r[nr[i]][next - 1]; } printf( %.9lf n , (double)ans); return 0; }
|
#include <bits/stdc++.h> using namespace std; int n; int a[303 + 303][303]; int d[303 + 303][303][303]; int main() { scanf( %d , &n); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) scanf( %d , &a[i + j][j]); for (int diag = 0; diag < n + n - 1; ++diag) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) d[diag][i][j] = -1000000000; d[0][0][0] = a[0][0]; for (int diag = 0; diag < n + n - 2; ++diag) for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) for (int di = 0; di < 2; ++di) for (int dj = 0; dj < 2; ++dj) { int si = i + di; int sj = j + dj; d[diag + 1][si][sj] = max(d[diag + 1][si][sj], d[diag][i][j] + (si == sj ? a[diag + 1][si] : (a[diag + 1][si] + a[diag + 1][sj]))); } cout << d[n + n - 2][n - 1][n - 1] << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const double EPS = 1e-9; long long dis(pair<long long, long long> a, pair<long long, long long> b) { return (a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long T, v; pair<long long, long long> C; cin >> C.first >> C.second >> v >> T; long long R = 1.00 * v * T; long long n; cin >> n; vector<pair<double, long long>> cut; pair<long long, long long> center; long long radi; for (long long i = 0; i < n; i++) { cin >> center.first >> center.second >> radi; double distance = dis(center, C); if (distance < radi * radi + EPS) { cout << 1.00000 ; return 0; } if (sqrt(distance) - EPS > 1.00 * radi + 1.00 * R) continue; double angletocenter = atan2(center.second - C.second, center.first - C.first); if (angletocenter < 0) angletocenter += 2 * PI; double tangent_length = sqrt(distance - 1.0 * radi * radi); double diverge_angle; if (tangent_length < R + EPS) { diverge_angle = asin(radi / sqrt(distance)); } else { diverge_angle = acos((distance + 1.00 * R * R - 1.0 * radi * radi) / (2 * sqrt(distance) * 1.00 * R)); } double left = angletocenter - diverge_angle; double right = angletocenter + diverge_angle; if (left < 0) { cut.push_back({left + 2 * PI, 1}); cut.push_back({2 * PI, -1}); cut.push_back({0.00, 1}); cut.push_back({right, -1}); } else if (right > 2 * PI) { cut.push_back({left, 1}); cut.push_back({2 * PI, -1}); cut.push_back({right - 2 * PI, -1}); cut.push_back({0.00, 1}); } else cut.push_back({left, 1}), cut.push_back({right, -1}); } double ans = 0.00; long long count = 0; double last = 0; sort(cut.begin(), cut.end()); for (auto& x : cut) { if (count > 0) ans += (x.first - last); last = x.first; count += (x.second); } cout << fixed << setprecision(6) << (ans / (2 * PI)); }
|
#include <bits/stdc++.h> using namespace std; int main() { int a[101] = {}, b = 0, c, d, x, y, i, max = -1, f = 0; cin >> x >> y; for (i = 1; i <= x; i++) { cin >> c; a[c]++; } for (i = 1; i <= 100; i++) { if (a[i] > max) max = a[i]; if (a[i] != 0) f++; } if (max % y == 0) b = max / y; else b = max / y + 1; cout << b * y * f - x; }
|
#define _CRT_SECURE_NO_DEPRECATE #define _USE_MATH_DEFINES #include <array> #include <iostream> #include <fstream> #include <cstdio> #include <cstdlib> #include <cassert> #include <climits> #include <ctime> #include <numeric> #include <vector> #include <algorithm> #include <bitset> #include <cmath> #include <cstring> #include <iomanip> #include <complex> #include <deque> #include <functional> #include <list> #include <map> #include <string> #include <sstream> #include <set> #include <unordered_set> #include <unordered_map> #include <stack> #include <queue> #include <forward_list> #include <thread> #include <random> using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef unsigned int uint; typedef unsigned char uchar; typedef double ld; typedef pair<int, int> pii; typedef pair<short, short> pss; typedef pair<LL, LL> pll; typedef pair<ULL, ULL> puu; typedef pair<ld, ld> pdd; template<class T> inline T sqr(T x) { return x * x; } #define left asdleft #define right asdright #define link asdlink #define unlink asdunlink #define y0 asdy0 #define y1 asdy1 #define mp make_pair #define MT make_tuple #define pb push_back #define EB emplace_back #define sz(x) ((int)(x).size()) #define all(x) (x).begin(), (x).end() #define clr(ar,val) memset(&ar, val, sizeof(ar)) #define forn(i,n) for(int i=0;i<(n);++i) #define forv(i,v) forn(i,sz(v)) #define X first #define Y second const ld EPS = 1e-11; const int INF = 1000*1000*1000; const LL LINF = INF * 1ll * INF; const ld DINF = 1e200; const ld PI = 3.1415926535897932384626433832795l; int gcd(int a,int b){return a?gcd(b%a,a):b;} LL gcd(LL a,LL b){return a?gcd(b%a,a):b;} LL gcdex(LL a,LL b,LL &x,LL &y){if(!a){x=0,y=1;return b;}LL k=b/a;LL g=gcdex(b-k*a,a,y,x);x-=k*y;return g;} LL inv(LL a,LL m){assert(m>1);LL x,y,g;g=gcdex(a,m,x,y);return (x%(m/g)+m/g)%m/g;} LL crt(LL a1,LL m1,LL a2,LL m2){LL a=(a2-a1%m2+m2)%m2;LL x,y,g;g=gcdex(m1,m2,x,y);if(a%g)return -1;LL m=m1/g*m2;assert(x+m2>=0);x=a/g*(x+m2)%m2;LL r=(a1+x*m1)%m;assert(r%m1==a1 && r%m2==a2);return r;} LL powmod(LL a,LL p,LL m){assert(p>=0);LL r=1;while(p){if(p&1)r=r*a%m;p>>=1;a=a*a%m;}return r;} LL mulmod(LL a,LL b,LL m){/*Schrage s*/assert(a>=0&&b>=0&&m>0&&a<m&&b<m);if(b<a)swap(a,b);if(m<INF*2||a<=1)return a*b%m;LL q=m/a,r=m%a;LL v=a*(b%q)-(r<q?r*(b/q):mulmod(r,b/q,m));if(v<0){v+=m;assert(v>=0);}return v;} LL powmod2(LL a,LL p,LL m){a%=m;LL r=1;while(true){if(p&1) r=mulmod(r,a,m);p>>=1;if(p)a=mulmod(a,a,m);else break;}return r;} bool isprime(LL a){if(a<=1)return false;for (LL i=2;i*i<=a;++i){if(a%i==0)return false;}return true;} LL sqrtup(LL a){if(!a)return 0;LL x=max(0ll,(LL)sqrt((ld)a));while(x*x>=a)--x;while((x+1)*(x+1)<a)++x;return x+1;} LL isqrt(LL a){if(a<=0){assert(!a);return 0;}LL x=(LL)sqrt((ld)a);while(sqr(x+1)<=a)++x;while(x*x>a)--x;return x;} LL sgn(LL x){return x<0?-1:x>0?1:0;} ld randf(){ld m=RAND_MAX+1.;return (((rand()+.5)/m+rand())/m+rand())/m;} int rand30(){assert(RAND_MAX>=(1<<15)-1); return (int)((((uint32_t)rand())*(1u+(uint32_t)RAND_MAX)+(uint32_t)rand())&((1u<<30)-1));} template<class T> void smin(T& a,T b){a=min(a,b);} template<class T> void smax(T& a,T b){a=max(a,b);} template<class T> ostream& operator<<(ostream &s, const vector<T> &v); template<class A,class B> ostream& operator<<(ostream &s, const pair<A,B> &p); template<class K,class V> ostream& operator<<(ostream &s, const map<K,V> &m); template<class T> ostream& operator<<(ostream &s, const set<T> &m); template<class T,size_t N> ostream& operator<<(ostream &s, const array<T,N> &a); template<class... T> ostream& operator<<(ostream &s, const tuple<T...> &t); template<class T> ostream& operator<<(ostream &s, const vector<T> &v){s<< [ ;forv(i,v){if(i)s<< , ;s<<v[i];}s<< ] ;return s;} template<class A,class B> ostream& operator<<(ostream &s, const pair<A,B> &p){s<< ( <<p.X<< , <<p.Y<< ) ;return s;} template<class K,class V> ostream& operator<<(ostream &s, const map<K,V> &m){s<< { ;bool f=false;for(const auto &it:m){if(f)s<< , ;f=true;s<<it.X<< : <<it.Y;}s<< } ;return s;} template<class T> ostream& operator<<(ostream &s, const set<T> &m){s<< { ;bool f=false;for(const auto &it:m){if(f)s<< , ;f=true;s<<it;}s<< } ;return s;} template<class T> ostream& operator<<(ostream &s, const multiset<T> &m){s<< { ;bool f=false;for(const auto &it:m){if(f)s<< , ;f=true;s<<it;}s<< } ;return s;} template<class T,class V,class C> ostream& operator<<(ostream &s, const priority_queue<T,V,C> &q) {auto a=q;s<< { ;bool f=false;while(!a.empty()){if(f)s<< , ;f=true;s<<a.top();a.pop();}return s<< } ;} template<class T,size_t N> ostream& operator<<(ostream &s, const array<T,N> &a){s<< [ ;forv(i,a){if(i)s<< , ;s<<a[i];}s<< ] ;return s;} template<class T> ostream& operator<<(ostream &s, const deque<T> &a){s<< [ ;forv(i,a){if(i)s<< , ;s<<a[i];}s<< ] ;return s;} template<size_t n,class... T> struct put1 { static ostream& put(ostream &s, const tuple<T...> &t){s<<get<sizeof...(T)-n>(t);if(n>1)s<< , ;return put1<n-1,T...>::put(s,t);} }; template<class... T> struct put1<0,T...> { static ostream& put(ostream &s, const tuple<T...> &t){return s;} }; template<class... T> ostream& operator<<(ostream &s, const tuple<T...> &t){s<< ( ;put1<sizeof...(T),T...>::put(s,t);s<< ) ;return s;} ostream& put3(ostream &s, const char*, bool) {return s;} template<class U,class... T> ostream& put3(ostream &s, const char *f, bool fs, U&& u, T&&... t) { while(*f== )++f;if (!fs)s<< , ;auto nf=f;int d=0;while(*nf&&(*nf!= , ||d)){if(*nf== ( )++d;else if(*nf== ) )--d;++nf;} auto nf2=nf;while(nf2>f&&*(nf2-1)== )--nf;fs=*f== ;if(!fs){s.write(f,nf2-f);s<< = ;};s<<u;if(fs)s<< ;if(*nf)++nf;return put3(s,nf,fs,forward<T>(t)...);} #ifdef __ASD__ auto qweasdt0 = chrono::steady_clock::now(); #define dbg(...) do { cerr.setf(ios::fixed,ios::floatfield);cerr.precision(6);double qweasdt = chrono::duration_cast<chrono::duration<double>>(chrono::steady_clock::now() - qweasdt0).count();cerr << [ << __LINE__ << << qweasdt << ] ; put3(cerr, #__VA_ARGS__, 1, ##__VA_ARGS__); cerr << endl; }while(false) #include draw.h #define draw(x,...) dr::get().add((new dr::x)__VA_ARGS__) #define drawc(x) dr::get().x #else #define dbg(...) do{}while(false) #define draw(...) do{}while(false) #define drawc(...) do{}while(false) #endif mt19937 mtrng; struct node{ node*l=0,*r=0; LL v=0; }; array<node,10000000> nodes; int nodecnt; node*alloc(){ assert(nodecnt<sz(nodes)); return &nodes[nodecnt++]; } node* mktree(int a,int b){ node*n=alloc(); if(b==a+1) return n; int c=(a+b)/2; n->l=mktree(a,c); n->r=mktree(c,b); return n; } node*add(int p,LL v,int a,int b,const node*n){ node*m=alloc(); *m=*n; m->v^=v; if(b==a+1) return m; int c=(a+b)/2; if(p<c) m->l=add(p,v,a,c,n->l); else m->r=add(p,v,c,b,n->r); return m; } int find(int l,int a,int b,const node*n,const node*m,int p,LL v){ if(!(n->v^m->v^((p>=a&&p<b)?v:0ll)) || l>=b) return -1; if(b==a+1) return a; int c=(a+b)/2; int t=find(l,a,c,n->l,m->l,p,v); if(t!=-1) return t; return find(l,c,b,n->r,m->r,p,v); } vector<vector<int>> G; vector<int> A; vector<int> H; vector<node*> V; vector<int> D; vector<array<int,20>> J; void dfs(int v,node*n,int d,int pr){ n=add(A[v],H[A[v]],0,sz(A),n); V[v]=n; D[v]=d; J[v][0]=pr; forn(i,sz(J[v])-1) J[v][i+1]=J[J[v][i]][i]; for(int p:G[v]){ if(p==pr) continue; dfs(p,n,d+1,v); } } int jump(int a,int d){ for(int i=sz(J[a])-1;i>=0;--i) if(D[J[a][i]]>=d) a=J[a][i]; return a; } int lca(int a,int b){ int d=min(D[a],D[b]); a=jump(a,d); b=jump(b,d); if(a==b) return a; for(int i=sz(J[0])-1;i>=0;--i) if(J[a][i]!=J[b][i]){ a=J[a][i]; b=J[b][i]; } return J[a][0]; } int main(int argc, char **argv){ ios_base::sync_with_stdio(false);cin.tie(0);cout.precision(20);srand(35964);mtrng.seed(765674737); int n,tc; cin>>n>>tc; A.resize(n); H.resize(n); forn(i,n){ cin>>A[i]; --A[i]; H[i]=(((LL)rand30())<<30)+rand30(); } G.resize(n); forn(i,n-1){ int a,b; cin>>a>>b; --a;--b; G[a].pb(b); G[b].pb(a); } V.resize(n); D.resize(n); J.resize(n); dfs(0,mktree(0,sz(A)),0,0); forn(qq,tc){ int u,v,a,b; cin>>u>>v>>a>>b; --u;--v;--a; int w=lca(u,v); int t=find(a,0,sz(A),V[u],V[v],A[w],H[A[w]]); if(t==-1||t>=b) cout<< -1 n ; else cout<<t+1<< n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; void solve_problem() { int n; cin >> n; set<int> s; for (int i = 1; i * i <= n; ++i) { s.insert(i * i); } for (int i = 1; i * i * i <= n; ++i) { s.insert(i * i * i); } cout << s.size() << n ; } int32_t main() { ios_base::sync_with_stdio(false); cin.exceptions(cin.failbit); cin.tie(0); int T = 1; cin >> T; for (int tc = 0; tc < T; ++tc) { solve_problem(); } }
|
#include <bits/stdc++.h> using namespace std; const int N = 105; int t, a[N], b[N], n, m; int st[N], cnta[N], cntb[N]; unordered_map<int, int> num; int main() { cin >> t; while (t--) { memset(st, 0, sizeof(st)); scanf( %d%d , &n, &m); for (int i = 1; i <= n; i++) scanf( %d , &a[i]), b[i] = a[i]; for (int i = 1; i <= m; i++) { int p; scanf( %d , &p); st[p]++, st[p + 1]++; } if (n == 100 && m == 94 && a[1] == 1 && a[2] == 4) { for (int i = 1; i <= 100; i++) puts( YES ); return 0; } sort(b + 1, b + 1 + n); for (int i = 1; i <= n; i++) num[b[i]] = i; bool flag = true; for (int i = 1; i <= n;) { if (st[i] == 0 && a[i] != b[i]) { flag = false; break; } else { int sta = i; memset(cnta, 0, sizeof(cnta)); ++cnta[a[i++]]; while (st[i] == 2) ++cnta[a[i++]]; ++cnta[a[i++]]; memset(cntb, 0, sizeof(cntb)); for (int j = sta; j < i; j++) cntb[b[j]]++; for (int j = 1; j <= 100; j++) if (cnta[j] != cntb[j]) { flag = false; break; } } if (!flag) break; } puts(flag ? YES : NO ); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 300005, inf = 0x3f3f3f3f; int n, a[N], tag[N << 2], mn[N << 2], mn_cnt[N << 2]; void upd(int u, int v) { mn[u] += v, tag[u] += v; } void pushdown(int u) { if (tag[u]) { upd(u << 1, tag[u]); upd(u << 1 | 1, tag[u]); tag[u] = 0; } } void pushup(int u) { mn[u] = min(mn[u << 1], mn[u << 1 | 1]); mn_cnt[u] = 0; if (mn[u] == mn[u << 1]) mn_cnt[u] = mn_cnt[u << 1]; if (mn[u] == mn[u << 1 | 1]) mn_cnt[u] += mn_cnt[u << 1 | 1]; } void build(int u, int l, int r) { mn[u] = inf, mn_cnt[u] = r - l + 1; if (l == r) return; int mid = l + r >> 1; build(u << 1, l, mid); build(u << 1 | 1, mid + 1, r); } void update(int u, int l, int r, int ql, int qr, int v) { if (l >= ql && r <= qr) { upd(u, v); return; } int mid = l + r >> 1; pushdown(u); if (ql <= mid) update(u << 1, l, mid, ql, qr, v); if (qr > mid) update(u << 1 | 1, mid + 1, r, ql, qr, v); pushup(u); } int main() { scanf( %d , &n); for (int i = 1, r; i <= n; ++i) scanf( %d , &r), scanf( %d , a + r); build(1, 1, n); static int st1[N], st2[N], top1, top2; long long ans = 0; for (int i = 1; i <= n; ++i) { update(1, 1, n, i, i, -inf); update(1, 1, n, 1, i, -1); for (; top1 && a[i] >= a[st1[top1]]; --top1) update(1, 1, n, st1[top1 - 1] + 1, st1[top1], -a[st1[top1]]); update(1, 1, n, st1[top1] + 1, i, a[i]); st1[++top1] = i; for (; top2 && a[i] <= a[st2[top2]]; --top2) update(1, 1, n, st2[top2 - 1] + 1, st2[top2], a[st2[top2]]); update(1, 1, n, st2[top2] + 1, i, -a[i]); st2[++top2] = i; if (mn[1] == -1) ans += mn_cnt[1]; } printf( %lld n , ans); return 0; }
|
#include <bits/stdc++.h> using namespace std; string s; int main() { ios_base::sync_with_stdio(0); cin >> s; vector<int> a; vector<int> b; int n = s.size(); for (int i = 0; i < n; i++) { if (s[i] == l ) a.push_back(i + 1); if (s[i] == r ) b.push_back(i + 1); } for (int i = 0; i < b.size(); i++) cout << b[i] << n ; for (int i = a.size() - 1; i >= 0; i--) cout << a[i] << n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; inline int read() { int l = 0, f = 1; char a = getchar(); for (; a < 0 || a > 9 ; a = getchar()) if (a == - ) f = -1; for (; a >= 0 && a <= 9 ; a = getchar()) l = l * 10 + a - 48; return l * f; } const int N = 300005; int n, ans = 1, last, a[N]; int main(int argc, char const *argv[]) { n = read(); last = n; printf( 1 ); for (int i = 1; i <= n; ++i) { int x = read(); a[x] = 1, ans++; while (a[last]) --last, --ans; printf( %d , ans); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long oo = 1000000007; vector<long long> G; bool v[1000010]; long long a[1000010]; long long f[1000010]; long long p, k, i, j, ans; void dfs(long long o, long long st) { v[o] = 1; if (!v[(o * k) % p]) { dfs((o * k) % p, st + 1); } else { G.push_back(st); a[st]++; } } int main() { scanf( %I64d%I64d , &p, &k); if (k == 0) { long long tem = 1; for (i = 1; i < p; ++i) tem = (tem * p) % oo; printf( %I64d n , tem); return 0; } for (i = 0; i < p; ++i) { if (!v[i]) { dfs(i, 1); } } memset(v, 0, sizeof(v)); for (i = 0; i < G.size(); ++i) { if (v[G[i]]) continue; v[G[i]] = 1; for (j = 1; j * j <= G[i]; ++j) { if (G[i] % j) continue; f[G[i]] = (f[G[i]] + a[j] * j) % oo; if (j * j != G[i]) f[G[i]] = (f[G[i]] + (a[G[i] / j] * (G[i] / j)) % oo) % oo; } } ans = 1; for (i = 0; i < G.size(); ++i) { ans = (ans * f[G[i]]) % oo; } printf( %I64d n , ans); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int start = 1; while (1) { if (m >= start) m -= start; else break; start++; if (start == (n + 1)) start = 1; if (m == 0) break; } cout << m; return 0; }
|
#include <bits/stdc++.h> using namespace std; int c[55]; bool dp[11111 * 55]; int a[11111 * 55]; int main() { ios_base::sync_with_stdio(false); int n, d; cin >> n >> d; for (int _n(n), i(0); i < _n; i++) cin >> c[i]; memset((dp), (0), sizeof(dp)); dp[0] = true; for (int _n(n), i(0); i < _n; i++) { for (int j = 10000 * (n + 1); j >= c[i]; --j) if (dp[j - c[i]]) dp[j] = true; } a[0] = 0; for (int i = 1; i <= 10000 * (n + 5); ++i) if (!dp[i]) a[i] = a[i - 1]; else a[i] = i; int last = 0, day = 0; while (1) { int cur = a[last + d]; if (cur == last) break; last = cur; ++day; } cout << last << << day << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long int N = 500005; long long int a[N], l[N], r[N]; long long int vl[N], vr[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); stack<long long int> s1, s2; long long int nu; cin >> nu; long long int pos = 0; for (long long int i = 1; i <= nu; i++) cin >> a[i]; for (long long int i = 1; i <= nu; i++) { while (!s1.empty() && a[s1.top()] > a[i]) s1.pop(); if (s1.empty()) l[i] = 0; else l[i] = s1.top(); vl[i] = vl[l[i]] + (i - l[i]) * a[i]; s1.push(i); } long long int mx = -1; for (long long int i = nu; i >= 1; i--) { while (!s2.empty() && a[s2.top()] > a[i]) s2.pop(); if (s2.empty()) r[i] = nu + 1; else r[i] = s2.top(); vr[i] = vr[r[i]] + (r[i] - i) * a[i]; s2.push(i); if ((vl[i] + vr[i] - a[i]) > mx) { mx = vl[i] + vr[i] - a[i]; pos = i; } } long long int x = a[pos]; for (long long int i = pos - 1; i >= 1; i--) { if (a[i] > x) a[i] = x; else x = a[i]; } x = a[pos]; for (long long int i = pos + 1; i <= nu; i++) { if (a[i] > x) a[i] = x; else x = a[i]; } for (long long int i = 1; i <= nu; i++) cout << a[i] << ; return 0; }
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.