func_code_string
stringlengths 59
71.4k
|
---|
#include <bits/stdc++.h> using namespace std; struct BIT { long long T[1000100]; long long ALL; BIT() { ALL = 0; } void add(int pos, long long w) { ++pos; ALL += w; for (; pos < 1000100; pos += (pos & (-pos))) T[pos] += w; } long long all_from(int pos) { long long ret = ALL; for (; pos > 0; pos -= pos & (-pos)) ret -= T[pos]; return ret; } }; int N, M, H[1000100], FL[1000100], FR[1000100]; stack<int> pos; vector<int> ib, ie, spl, spr, val; vector<int> ids; inline bool icompare(const int& f, const int& s) { return ie[f] - ib[f] > ie[s] - ib[s]; } vector<double> ans; vector<int> Q, qid; inline bool qcompare(const int& f, const int& s) { return Q[f] > Q[s]; } BIT rem_val, rem_sum; int main() { scanf( %d , &N); H[0] = H[N + 1] = -1100000000; for (int i = 0; i < N; ++i) scanf( %d , &H[i + 1]); pos.push(0); for (int i = 1; i <= N; ++i) { while (H[pos.top()] > H[i]) pos.pop(); FL[i] = pos.top(); pos.push(i); } while (!pos.empty()) pos.pop(); pos.push(N + 1); for (int i = N; i > 0; --i) { while (H[pos.top()] >= H[i]) pos.pop(); FR[i] = pos.top(); pos.push(i); } for (int i = 1; i <= N; ++i) { ib.push_back(FL[i] + 1); ie.push_back(FR[i] - 1); val.push_back(H[i]); spl.push_back(i - FL[i] - 1); spr.push_back(FR[i] - i - 1); } for (int i = 0; i < ((int)(ib).size()); ++i) ids.push_back(i); sort(ids.begin(), ids.end(), icompare); scanf( %d , &M); ans.resize(M); Q.resize(M); for (int i = 0; i < M; ++i) qid.push_back(i); for (int i = 0; i < M; ++i) scanf( %d , &Q[i]); sort(qid.begin(), qid.end(), qcompare); long long val_all = 0, sum_all = 0; int cur = 0; for (int i = 0; i < ((int)(qid).size()); ++i) { int V = Q[qid[i]]; while (cur < ((int)(ids).size()) && ie[ids[cur]] - ib[ids[cur]] + 1 >= V) { int T = ids[cur++]; val_all += val[T] * 1LL * (ie[T] - ib[T] + 1); sum_all += val[T]; if (spl[T] > 0) rem_val.add(spl[T], spl[T] * 1LL * val[T]), rem_sum.add(spl[T], val[T]); if (spr[T] > 0) rem_val.add(spr[T], spr[T] * 1LL * val[T]), rem_sum.add(spr[T], val[T]); } long long out = val_all - (V - 1) * sum_all; out -= rem_val.all_from(V) - (V - 1) * rem_sum.all_from(V); ans[qid[i]] = ((double)out) / (N - V + 1); } for (int i = 0; i < M; ++i) printf( %.12lf n , ans[i]); return 0; }
|
#include <bits/stdc++.h> const double PI = acos(-1.0); using namespace std; char a[2000010], b[2000010]; int main() { int n; int i; int x, y, z; int p, q; while (~scanf( %d %s %s , &n, a, b)) { x = y = z = 0; for (i = 0; i < 2 * n; i++) { if (a[i] == 1 && b[i] == 1 ) { z++; } else if (a[i] == 1 ) { x++; } else if (b[i] == 1 ) { y++; } } p = x + (z + 1) / 2; q = y + z - (z + 1) / 2; if (p == q || p + 1 == q) { puts( Draw ); } else if (p > q) { puts( First ); } else { puts( Second ); } } return 0; }
|
#include <bits/stdc++.h> using namespace std; int T; string hxt[16]; int main() { cin >> T; while (T--) { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> hxt[i]; bool iif = false; for (int i = 0; i < m; i++) { char x = hxt[1][i]; for (char j = a ; j <= z ; j++) { hxt[1][i] = j; bool flag = true; for (int k = 2; k <= n; k++) { int js = 0; for (int l = 0; l < m; l++) { if (hxt[k][l] != hxt[1][l]) js++; if (js > 1) break; } if (js > 1) { flag = false; break; } } if (flag) { cout << hxt[1] << endl; iif = true; break; } hxt[1][i] = x; } if (iif) break; } if (!iif) cout << -1 << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { vector<string> v; for (int i = 0; i < 3; i++) { string s; cin >> s; v.push_back(s); } vector<string> so; vector<string> po; vector<string> mo; for (int i = 0; i < v.size(); i++) { if (v[i][1] == s ) so.push_back(v[i]); else if (v[i][1] == p ) po.push_back(v[i]); else if (v[i][1] == m ) mo.push_back(v[i]); } sort(so.begin(), so.end()); sort(po.begin(), po.end()); sort(mo.begin(), mo.end()); if (v[0] == v[1] && v[1] == v[2]) cout << 0 << endl; else if (v[0] == v[1] || v[0] == v[2] || v[1] == v[2]) cout << 1 << endl; else if (so.size() == 3) { int m1 = (so[2][0] - 0 ) - (so[1][0] - 0 ); int m2 = (so[1][0] - 0 ) - (so[0][0] - 0 ); if (m1 == 1 && m2 == 1) cout << 0 << endl; else if (m1 <= 2 || m2 <= 2) cout << 1 << endl; else cout << 2 << endl; } else if (mo.size() == 3) { int m1 = (mo[2][0] - 0 ) - (mo[1][0] - 0 ); int m2 = (mo[1][0] - 0 ) - (mo[0][0] - 0 ); if (m1 == 1 && m2 == 1) cout << 0 << endl; else if (m1 <= 2 || m2 <= 2) cout << 1 << endl; else cout << 2 << endl; } else if (po.size() == 3) { int m1 = (po[2][0] - 0 ) - (po[1][0] - 0 ); int m2 = (po[1][0] - 0 ) - (po[0][0] - 0 ); if (m1 == 1 && m2 == 1) cout << 0 << endl; else if (m1 <= 2 || m2 <= 2) cout << 1 << endl; else cout << 2 << endl; } else if (so.size() == 2) { int m1 = (so[1][0] - 0 ) - (so[0][0] - 0 ); if (m1 == 1 || m1 == 2) cout << 1 << endl; else cout << 2 n ; } else if (mo.size() == 2) { int m1 = (mo[1][0] - 0 ) - (mo[0][0] - 0 ); if (m1 == 1 || m1 == 2) cout << 1 << endl; else cout << 2 n ; } else if (po.size() == 2) { int m1 = (po[1][0] - 0 ) - (po[0][0] - 0 ); if (m1 == 1 || m1 == 2) cout << 1 << endl; else cout << 2 n ; } else cout << 2 n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 2; int n; int a[N]; map<int, int> m; map<pair<int, int>, bool> checked; int main() { cin >> n; int zero = 0; for (int i = 0; i < n; ++i) { scanf( %d , a + i); if (!a[i]) zero++; ; m[a[i]]++; } sort(a, a + n); int ans = -1; int help1, help2, help3; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) if (i != j) { if (!a[i] and (!a[j])) continue; if (checked[make_pair(a[i], a[j])]) continue; checked[make_pair(a[i], a[j])] = true; map<int, int> m1; help3 = 2; m1[a[i]]++, m1[a[j]]++; help1 = a[i]; help2 = a[j]; while (m1[help1 + help2] < m[help1 + help2]) { m1[help1 + help2]++; help3++; help2 += help1; help1 = help2 - help1; } ans = max(ans, help3); } ans = max(ans, zero); cout << ans; }
|
#include <bits/stdc++.h> using namespace std; const int maxn = 4005; vector<int> v[maxn]; int vis[maxn][maxn]; int main(void) { int n, m, ans = maxn; memset(vis, 0, sizeof(vis)); scanf( %d%d , &n, &m); for (int i = 0; i < m; i++) { int x, y; scanf( %d%d , &x, &y); v[x].push_back(y); v[y].push_back(x); vis[x][y] = vis[y][x] = 1; } for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { if (!vis[i][j]) continue; for (int k = j + 1; k <= n; k++) { if (!vis[i][k] || !vis[k][j]) continue; int cur = 0; int len = v[i].size(); for (int d = 0; d < len; d++) { if (v[i][d] != j && v[i][d] != k) cur++; } len = v[j].size(); for (int d = 0; d < len; d++) { if (v[j][d] != i && v[j][d] != k) cur++; } len = v[k].size(); for (int d = 0; d < len; d++) { if (v[k][d] != i && v[k][d] != j) cur++; } ans = min(ans, cur); } } } if (ans == maxn) printf( -1 n ); else printf( %d n , ans); }
|
#include <bits/stdc++.h> using namespace std; int T, N; int arr[500005]; stack<int> stk; vector<pair<int, int>> v; int main() { cin.sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> T; while (T--) { cin >> N; bool pos = 1; while (stk.size()) { stk.pop(); } v.clear(); for (int i = 1; i <= N; i++) { while (stk.size() && stk.top() == i) { stk.pop(); } cin >> arr[i]; if (arr[i] == -1) { if (stk.empty()) { arr[i] = N + 1; } else { arr[i] = stk.top(); } } v.push_back({-arr[i], i}); if (stk.size() && stk.top() < arr[i]) { pos = 0; } stk.push(arr[i]); } if (pos) { sort(v.begin(), v.end()); int cnt = N; for (auto p : v) { arr[p.second] = cnt--; } for (int i = 1; i <= N; i++) { cout << arr[i] << ; } cout << n ; } else { cout << -1 << n ; } } }
|
#include <bits/stdc++.h> using namespace std; namespace io { const int __SIZE = (1 << 21) + 1; char ibuf[__SIZE], *iS, *iT, obuf[__SIZE], *oS = obuf, *oT = oS + __SIZE - 1, __c, qu[55]; int __f, qr, _eof; inline void flush() { fwrite(obuf, 1, oS - obuf, stdout), oS = obuf; } inline void gc(char &x) { x = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); } inline void pc(char x) { *oS++ = x; if (oS == oT) flush(); } inline void pstr(const char *s) { int __len = strlen(s); for (__f = 0; __f < __len; ++__f) pc(s[__f]); } inline void gstr(char *s) { for (__c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); __c < 32 || __c > 126 || __c == ;) __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); for (; __c > 31 && __c < 127 && __c != ; ++s, __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) *s = __c; *s = 0; } template <class I> inline bool gi(I &x) { _eof = 0; for (__f = 1, __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); (__c < 0 || __c > 9 ) && !_eof; __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) { if (__c == - ) __f = -1; _eof |= __c == EOF; } for (x = 0; __c <= 9 && __c >= 0 && !_eof; __c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) x = x * 10 + (__c & 15), _eof |= __c == EOF; x *= __f; return !_eof; } template <class I> inline void print(I x) { if (!x) pc( 0 ); if (x < 0) pc( - ), x = -x; while (x) qu[++qr] = x % 10 + 0 , x /= 10; while (qr) pc(qu[qr--]); } struct Flusher_ { ~Flusher_() { flush(); } } io_flusher_; } // namespace io using io::gc; using io::gi; using io::gstr; using io::pc; using io::print; using io::pstr; using ll = long long; using pii = pair<int, int>; const int MOD = 1000000007; pii dat[500005 << 2]; bool laz[500005 << 2]; void push(int idx, int l, int r) { if (!laz[idx] || l == r) return; dat[idx * 2] = {dat[idx * 2].second, dat[idx * 2].first}; laz[idx * 2] = !laz[idx * 2]; dat[idx * 2 + 1] = {dat[idx * 2 + 1].second, dat[idx * 2 + 1].first}; laz[idx * 2 + 1] = !laz[idx * 2 + 1]; laz[idx] = 0; } void upd(int idx, int l, int r, int i, pii v) { if (!(l <= i && i <= r)) return; push(idx, l, r); if (l == r) { dat[idx] = v; return; } upd(idx * 2, l, (l + r) / 2, i, v); upd(idx * 2 + 1, (l + r) / 2 + 1, r, i, v); dat[idx] = {max(dat[idx * 2].first, dat[idx * 2 + 1].first), max(dat[idx * 2].second, dat[idx * 2 + 1].second)}; } void tag(int idx, int l, int r, int ql, int qr) { if (qr < l || r < ql) return; push(idx, l, r); if (ql <= l && r <= qr) { dat[idx] = {dat[idx].second, dat[idx].first}; laz[idx] = !laz[idx]; return; } tag(idx * 2, l, (l + r) / 2, ql, qr); tag(idx * 2 + 1, (l + r) / 2 + 1, r, ql, qr); dat[idx] = {max(dat[idx * 2].first, dat[idx * 2 + 1].first), max(dat[idx * 2].second, dat[idx * 2 + 1].second)}; } pii el2[500005]; vector<pii> elist[500005]; pii dfsfar(int u, int f) { pii ans = {0, u}; for (pii &v : elist[u]) if (v.first != f) ans = max(ans, dfsfar(v.first, u)); return {ans.first + 1, ans.second}; } int clck = 1; int n; int L[500005], R[500005]; int qr[500005], ans[500005]; void dfs(int u, int f, int dep, int cnt) { L[u] = clck++; if (cnt % 2 == 0) upd(1, 1, n, L[u], {dep, 0}); else upd(1, 1, n, L[u], {0, dep}); for (pii &v : elist[u]) if (v.first != f) dfs(v.first, u, dep + 1, cnt + v.second); R[u] = clck - 1; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); gi(n); for (int i = (0); i < (n - 1); i++) { int u, v, w; gi(u), gi(v), gi(w); el2[i] = {u, v}; elist[u].push_back({v, w}); elist[v].push_back({u, w}); } int root = dfsfar(1, -1).second; dfs(root, -1, 0, 0); int q; gi(q); for (int i = (0); i < (q); i++) gi(qr[i]); for (int i = (0); i < (q); i++) { int par = el2[qr[i] - 1].first, ch = el2[qr[i] - 1].second; if (R[par] - L[par] < R[ch] - L[ch]) swap(par, ch); tag(1, 1, n, L[ch], R[ch]); ans[i] = max(ans[i], dat[1].first); } root = dfsfar(root, -1).second; memset(dat, 0, sizeof dat); memset(laz, 0, sizeof laz); clck = 1; dfs(root, -1, 0, 0); for (int i = (0); i < (q); i++) { int par = el2[qr[i] - 1].first, ch = el2[qr[i] - 1].second; if (R[par] - L[par] < R[ch] - L[ch]) swap(par, ch); tag(1, 1, n, L[ch], R[ch]); ans[i] = max(ans[i], dat[1].first); print(ans[i]), pc( n ); } }
|
#include <bits/stdc++.h> using namespace std; string s; int yekdast(char c, int l, int r) { int ans = 0; for (int i = l; i <= r; i++) { if (s[i] != c) ans++; } return ans; } int check(char c, int l, int r) { if ((r - l) > 1) { int mid1 = (floor((l * 1.0 + r * 1.0) / 2.0)), mid2 = (ceil((l * 1.0 + r * 1.0) / 2.0)); int first = check(char(c + 1), l, mid1) + yekdast(c, mid2, r); int second = check(char(c + 1), mid2, r) + yekdast(c, l, mid1); return min(first, second); } else if (r == l + 1) { int first = check(char(c + 1), l, l) + yekdast(c, r, r); int second = check(char(c + 1), r, r) + yekdast(c, l, l); return min(first, second); } else { return yekdast(c, l, r); } } int t, n; int main() { cin >> t; for (int i = 0; i < t; i++) { cin >> n >> s; cout << check( a , 0, n - 1) << endl; } }
|
#include <bits/stdc++.h> const int N = 1e5 + 2, p = 1e9 + 7; int siz[N], f[N]; int n, m, x, y, z, i, c; inline void read(int &x) { c = getchar(); while ((c < 48) || (c > 57)) c = getchar(); x = c ^ 48; c = getchar(); while ((c >= 48) && (c <= 57)) { x = x * 10 + (c ^ 48); c = getchar(); } } int getf(register int x) { if (f[x] == x) return f[x]; f[x] = getf(f[x]); siz[f[x]] += siz[x]; siz[x] = 0; return f[x]; } inline int ksm(register int x, register int y) { register int r = 1; while (y) { if (y & 1) r = (long long)r * x % p; x = (long long)x * x % p; y >>= 1; } return r; } int main() { read(n); read(m); for (i = 1; i <= n; i++) siz[f[i] = i] = 1; for (i = 1; i < n; i++) { read(x); read(y); read(z); if (z == 0) { siz[getf(y)] += siz[getf(x)]; siz[f[x]] = 0; f[f[x]] = f[y]; } } c = ksm(n, m); for (i = 1; i <= n; i++) if (siz[i]) c = (c - ksm(siz[i], m) + p) % p; printf( %d , c); }
|
#include <bits/stdc++.h> using namespace std; int n, m; char name[2000], part[2000000]; int work() { int p = 0; for (int i = 0; i < m; i++) { if (part[i] == name[p]) p++; if (p == n) return i; } return m; } int main() { scanf( %s , name); scanf( %s , part); n = strlen(name), m = strlen(part); int x = work(); reverse(name, name + n); reverse(part, part + m); int y = work(); printf( %d n , max(0, m - y - x - 1)); return 0; }
|
#include <bits/stdc++.h> using namespace std; long long int power(int b, int pw) { long long int res = 1; for (int i = 1; i <= pw; i++) { res = res * b; } return res; } long long int typ[1000000]; int main() { ios_base::sync_with_stdio(false); long long int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> typ[i]; } set<int> prev; vector<pair<int, int> > ns; long long int cnt = 0; long long int from = 1; for (int i = 1; i <= n; i++) { if (prev.find(typ[i]) != prev.end()) { ns.push_back(make_pair(from, i)); prev.clear(); from = i + 1; } else { prev.insert(typ[i]); } } if (ns.size() == 0) { printf( -1 n ); return 0; } cout << ns.size() << endl; int temp = ns.size(); for (int i = 0; i < ns.size(); i++) { if (i == temp - 1) { cout << ns[i].first << << n << endl; return 0; } cout << ns[i].first << << ns[i].second << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 5010; int n, m; int pnt[N * 4], last; char tab[N][N], a[N][N]; int find(int x) { if (x != pnt[x]) pnt[x] = find(pnt[x]); return pnt[x]; } int id(int x, int y, int d) { return (x * m + y) * 4 + d; } void add(int x, int y) { pnt[id(x, y, 0)] = x - 1 >= 0 ? id(x - 1, y, 0) : last; pnt[id(x, y, 1)] = x + 1 < n ? id(x + 1, y, 1) : last; pnt[id(x, y, 2)] = y - 1 >= 0 ? id(x, y - 1, 2) : last; pnt[id(x, y, 3)] = y + 1 < m ? id(x, y + 1, 3) : last; } void solve() { int ans = 0, cnt; last = n * m * 4; for (int i = 0; i < n; ++i) scanf( %s , tab[i]); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) { if (tab[i][j] == . ) continue; for (int u = 0; u <= last; ++u) pnt[u] = u; for (int u = 0; u < n; ++u) for (int v = 0; v < m; ++v) { a[u][v] = tab[u][v]; if (a[u][v] == . ) add(u, v); } int x = i, y = j, t = 0; while (1) { t++; int dir; if (a[x][y] == U ) dir = 0; if (a[x][y] == D ) dir = 1; if (a[x][y] == L ) dir = 2; if (a[x][y] == R ) dir = 3; add(x, y); int p = find(id(x, y, dir)); if (p == last) break; x = p / 4 / m; y = p / 4 % m; } if (ans < t) ans = t, cnt = 1; else if (ans == t) cnt++; } printf( %d %d n , ans, cnt); } int main() { while (scanf( %d%d , &n, &m) != EOF) { solve(); } return 0; }
|
#include <bits/stdc++.h> using namespace std; vector<long long> adj[100005]; long long a[100005]; long long col; bool ok; bool dfs(long long v, long long par) { ok &= (col == a[v]); for (auto i : adj[v]) { if (i != par) dfs(i, v); } } bool fn(long long v) { bool res = 1; for (auto i : adj[v]) { ok = 1; col = a[i]; dfs(i, v); res &= ok; } return res; } int main() { int t = 1; ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); while (t--) { long long n; cin >> n; long long i; long long p = -1, q = -1; for (i = 1; i < n; i++) { long long x, y; cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } for (i = 0; i < n; i++) cin >> a[i + 1]; for (i = 1; i <= n; i++) { for (auto j : adj[i]) { if (a[j] != a[i]) { p = i; q = j; } } } if (q == -1) { cout << YES << endl; cout << 1 << endl; continue; } bool a1 = fn(p); bool a2 = fn(q); if (a1) { cout << YES << endl; cout << p << endl; } else if (a2) { cout << YES << endl; cout << q << endl; } else cout << NO << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int R, r, k; int main() { int T; scanf( %d , &T); while (T--) { scanf( %d%d%d , &R, &r, &k); double a = R, b = r; printf( %lf n , 1 / (1 / (a - b) + k * k * (a - b) / a / b)); } return 0; }
|
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool checkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template <typename T> inline bool checkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } const int MAXN = 2600 / 2; const int MAXM = 51; const int MMAX = MAXN * MAXM + MAXN + MAXM + 100; const int di[] = {0, 0, 1, 0, -1}; const int dj[] = {0, 1, 0, -1, 0}; struct Graph { public: int n, m; int adj[MAXN][MAXM]; void init() { n = m = 0; memset(adj, 0, sizeof(adj)); } void insert(int u, int v) { n = max(n, v + 1); m = max(m, v + 1); adj[u][v] = 1; } int find_ans() { build_dlx(); for (int ans = 0; ans <= n; ++ans) { if (dfs(0, ans)) { return ans; } } return -1; } private: int head; int U[MMAX], D[MMAX], L[MMAX], R[MMAX], CN[MMAX], RN[MMAX]; void addUD(const int &a, const int &h) { U[a] = h; D[a] = D[h]; U[D[h]] = a; D[h] = a; CN[a] = h; } void addLR(const int &a, const int &h) { L[a] = h; R[a] = R[h]; L[R[h]] = a; R[h] = a; RN[a] = h; } void add(const int &k, const int &r, const int &c) { addUD(k, c); addLR(k, r); } void remove(const int &k) { for (int j = R[k]; j != k; j = R[j]) { for (int i = D[j]; i != j; i = D[i]) { L[R[i]] = L[i]; R[L[i]] = R[i]; } D[U[j]] = D[j]; U[D[j]] = U[j]; } } void unremove(const int &k) { for (int j = L[k]; j != k; j = L[j]) { D[U[j]] = j; U[D[j]] = j; for (int i = U[j]; i != j; i = U[i]) { L[R[i]] = i; R[L[i]] = i; } } } void build_dlx() { head = MMAX - 1; U[head] = D[head] = L[head] = R[head] = head; int cnt = 0; for (int i = 0; i < m; i++) { U[cnt] = D[cnt] = cnt; addLR(cnt++, head); } for (int i = 0; i < n; i++) { L[cnt] = R[cnt] = cnt; addUD(cnt++, head); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) if (adj[i][j]) { addLR(cnt, m + i); addUD(cnt, j); cnt++; } } } int h() { int hash[MAXM] = {}; int ans = 0; for (int c = R[head]; c != head; c = R[c]) { if (hash[c] == 0) { hash[c] = 1; ++ans; for (int j = D[c]; j != c; j = D[j]) { for (int i = R[j]; i != j; i = R[i]) { if (CN[i] != head) { hash[CN[i]] = 1; } } } } } return ans; } bool dfs(int k, int lim) { if (k + h() > lim) { return false; } if (R[head] == head) { return true; } int c; c = R[head]; L[R[c]] = L[c]; R[L[c]] = R[c]; for (int i = D[c]; i != c; i = D[i]) { L[R[i]] = L[i]; R[L[i]] = R[i]; } for (int i = D[c]; i != c; i = D[i]) { remove(RN[i]); if (dfs(k + 1, lim)) { return true; } unremove(RN[i]); } for (int i = U[c]; i != c; i = U[i]) { L[R[i]] = i; R[L[i]] = i; } L[R[c]] = c; R[L[c]] = c; return false; } } G; int N, M; int change(const int &i, const int &j) { return i * M + j; } int main() { while (scanf( %d%d , &N, &M) == 2) { G.init(); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { for (int p = 0; p < 5; p++) { int ii = i + di[p], jj = j + dj[p]; if (0 <= ii && ii < N && 0 <= jj && jj < M) { G.insert(change(i, j), change(ii, jj)); } } } } int res = G.find_ans(); printf( %d n , M * N - res); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 300005; priority_queue<pair<long long, int> > Q; int h[N], a[N]; int n, m, k, p; bool check(long long lim) { static long long now[N]; for (int i = 1; i <= n; i++) now[i] = lim; while (!Q.empty()) Q.pop(); for (int i = 1; i <= n; i++) if (now[i] - 1ll * a[i] * m < h[i]) Q.push(make_pair(-lim / a[i], i)); for (int i = 1; i <= m; i++) for (int j = 1; j <= k; j++) { if (Q.empty()) return true; pair<long long, int> mx = Q.top(); Q.pop(); if (-mx.first < i) return false; now[mx.second] += p; if (now[mx.second] - 1ll * a[mx.second] * m < h[mx.second]) Q.push(make_pair(-now[mx.second] / a[mx.second], mx.second)); } return Q.empty(); } int main() { scanf( %d%d%d%d , &n, &m, &k, &p); for (int i = 1; i <= n; i++) scanf( %d%d , &h[i], &a[i]); long long l = 0, r = 6e12, ans; while (l <= r) { long long mid = (l + r) >> 1ll; if (check(mid)) ans = mid, r = mid - 1; else l = mid + 1; } printf( %lld , ans); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = (int)3e5 + 5; const int K = (int)3e5 + 5; vector<pair<int, int>> adj[N]; queue<pair<int, int>> q; bool visited[N]; int res[300005]; int n; int k; int d; int main() { scanf( %d%d%d , &n, &k, &d); for (int i = 0; i < k; i++) { int place; scanf( %d , &place); q.push({place, 0}); } for (int i = 0; i < n - 1; i++) { int u, v; scanf( %d%d , &u, &v); adj[u].push_back({v, i + 1}); adj[v].push_back({u, i + 1}); } while (!q.empty()) { int pos = q.front().first; int from = q.front().second; q.pop(); if (visited[pos]) continue; visited[pos] = 1; for (pair<int, int> u : adj[pos]) { if (u.first != from) { if (visited[u.first]) res[u.second] = 1; else q.push({u.first, pos}); } } } int rescnt = 0; for (int i = 1; i <= n - 1; i++) if (res[i]) rescnt++; cout << rescnt << endl; for (int x = 1; x <= n - 1; x++) if (res[x]) cout << x << ; return 0; }
|
#include <bits/stdc++.h> using namespace std; int cnt, n; string ss; int main() { cin >> n >> ss; for (int i = 0; i < n; i++) { if (i < n - 1 && ((ss[i] == R && ss[i + 1] == U ) || (ss[i] == U && ss[i + 1] == R ))) { i++; cnt++; } else cnt++; } cout << cnt << endl; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, s, i, x, y; cin >> n >> s; int sol = 100; int f = 1; for (i = 0; i < n; i++) { cin >> x >> y; if (x < s) { if (y) { sol = min(sol, y); } } if (x < s || (x == s && y == 0)) { f = 0; } } if (f) cout << -1 << endl; else cout << 100 - sol << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; long long l, r, ans = 0; int main() { cin >> l >> r; for (long long i = 1; i <= r; i *= 2) { for (long long j = 1; j <= r; j *= 3) { if (i * j >= l && i * j <= r) ans++; } } cout << ans << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; using vpii = vector<pii>; using vi = vector<int>; using vvi = vector<vi>; using ll = long long; using vll = vector<long long>; template <class T> using min_queue = priority_queue<T, vector<T>, greater<T>>; template <class T> istream &operator>>(istream &, vector<T> &); template <class T> ostream &operator<<(ostream &, const vector<T> &); template <class T, class U> istream &operator>>(istream &, pair<T, U> &); template <class T, class U> ostream &operator<<(ostream &, const pair<T, U> &); template <class T> struct Inf { static constexpr T inf() { return std::numeric_limits<T>::has_infinity() ? std::numeric_limits<T>::infinty() : std::numeric_limits<T>::max(); } }; template <> struct Inf<int> { static constexpr int inf() { return 0x3f3f3f3f; } }; template <> struct Inf<long long> { static constexpr long long inf() { return 0x3f3f3f3f3f3f3f3fLL; } }; constexpr int INF = Inf<int>::inf(); constexpr ll BINF = Inf<ll>::inf(); int solve() { int n, k; cin >> n >> k; vi a(n); cin >> a; ll ans = a[n - 1] - a[0]; vi wtf; for (int i = 0; i + 1 < n; i++) wtf.push_back(a[i] - a[i + 1]); sort(wtf.begin(), wtf.end()); for (int i = 0; i < k - 1; i++) ans += wtf[i]; cout << ans << endl; return 0; } int main() { ios::sync_with_stdio(0); solve(); return 0; } template <class T> istream &operator>>(istream &is, vector<T> &v) { for (auto it = v.begin(); it != v.end(); ++it) is >> *it; return is; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto it = v.begin(); it != v.end();) os << *it, os << n [++it == v.end()]; return os; } template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) { return is >> p.first >> p.second; } template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << p.first << << p.second; }
|
#include <bits/stdc++.h> using namespace std; typedef pair<long long, long long> ll; typedef vector<long long> vl; typedef vector<ll> vll; typedef vector<vl> vvl; template <typename T> ostream &operator<<(ostream &o, vector<T> v) { if (v.size() > 0) o << v[0]; for (unsigned i = 1; i < v.size(); i++) o << << v[i]; return o << n ; } template <typename U, typename V> ostream &operator<<(ostream &o, pair<U, V> p) { return o << ( << p.first << , << p.second << ) ; } template <typename T> istream &operator>>(istream &in, vector<T> &v) { for (unsigned i = 0; i < v.size(); i++) in >> v[i]; return in; } template <typename T> istream &operator>>(istream &in, pair<T, T> &p) { in >> p.first; in >> p.second; return in; } vvl g(111000); vl ind(111000, 0), vis(111000, false); long long c = 0; bool rec(long long node, long long p) { vl ch; ind[node] = c++; vis[node] = 1; for (auto &it : g[node]) { if (it == p) continue; if (!vis[it]) { if (!rec(it, node)) ch.push_back((it)); } else if (ind[node] < ind[it]) ch.push_back((it)); } for (long long i = 0; i + 1 < ch.size(); i += 2) cout << ch[i] << << node << << ch[i + 1] << n ; if (ch.size() % 2 == 1) { cout << p << << node << << ch[ch.size() - 1] << n ; return true; } return false; } int main() { long long n, m; cin >> n >> m; if (m % 2 == 1) { cout << No solution << n ; return 0; } while (m--) { long long x, y; cin >> x >> y; g[x].push_back((y)); g[y].push_back((x)); } rec(1, -1); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int INF = (int)1e9; const int maxn = 100010; const int maxbit = 18; const int sz = 1500; inline int read() { int x = 0, f = 1; char ch = 0; for (; !isdigit(ch); ch = getchar()) if (ch == - ) f = -1; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48; return x * f; } struct node { int l, r, tid, ans, id; node() {} node(int _l, int _r, int _tid, int _ans, int _id) { l = _l; r = _r; tid = _tid; ans = _ans; id = _id; } } que[maxn]; struct node2 { int p, fr, to; node2() {} node2(int _p, int _fr, int _to) { p = _p; fr = _fr; to = _to; } } cz[maxn]; vector<int> oo; inline int bl(int x) { return (x - 1) / sz; } bool cmp(node x, node y) { if (bl(x.l) != bl(y.l)) return x.l < y.l; else if (bl(x.r) != bl(y.r)) return x.r < y.r; else return x.tid < y.tid; } bool cmp2(node x, node y) { return x.id < y.id; } int n, q, cntz, cntq; int a[maxn], fl[maxn], Now, L, R, cnt[maxn << 2]; inline void add(int x) { --fl[cnt[x]]; ++fl[++cnt[x]]; } inline void del(int x) { --fl[cnt[x]]; ++fl[--cnt[x]]; } inline void go(node2 &t) { if (t.p >= L && t.p <= R) del(t.fr), add(t.to); a[t.p] = t.to; } inline void re(node2 &t) { if (t.p >= L && t.p <= R) del(t.to), add(t.fr); a[t.p] = t.fr; } int main() { fl[0] = INF; n = read(); q = read(); for (int i = 1; i <= n; ++i) a[i] = read(), oo.push_back(a[i]); for (int i = 1; i <= q; ++i) { int t = read(); if (t == 1) { int l = read(), r = read(); que[++cntq] = node(l, r, Now, 0, i); } else { int p = read(), x = read(); oo.push_back(x); cz[++cntz] = node2(p, a[p], x); a[p] = x; ++Now; } } sort(oo.begin(), oo.end()); int ss = unique(oo.begin(), oo.end()) - oo.begin(); oo.resize(ss); for (int i = 1; i <= n; ++i) a[i] = lower_bound(oo.begin(), oo.end(), a[i]) - oo.begin() + 1; for (int i = 1; i <= cntz; ++i) { cz[i].fr = lower_bound(oo.begin(), oo.end(), cz[i].fr) - oo.begin() + 1; cz[i].to = lower_bound(oo.begin(), oo.end(), cz[i].to) - oo.begin() + 1; } sort(que + 1, que + cntq + 1, cmp); for (int i = 1; i <= cntq; ++i) { node now = que[i]; while (R < now.r) add(a[++R]); while (L > now.l) add(a[--L]); while (R > now.r) del(a[R--]); while (L < now.l) del(a[L++]); while (Now < now.tid) go(cz[++Now]); while (Now > now.tid) re(cz[Now--]); while (fl[que[i].ans]) ++que[i].ans; } sort(que + 1, que + cntq + 1, cmp2); for (int i = 1; i <= cntq; ++i) printf( %d n , que[i].ans); }
|
#include <bits/stdc++.h> using namespace std; vector<int> myvec[100001]; set<int> myset[2]; set<int>::iterator it; int mark[100001]; bool isbipartite(int src) { int color[100001]; memset(color, -1, sizeof(color)); queue<int> q; q.push(src); mark[src] = 1; color[src] = 1; myset[color[src]].insert(src); while (!q.empty()) { int u = q.front(); q.pop(); for (int v = 0; v < myvec[u].size(); v++) { if (color[myvec[u][v]] == -1) color[myvec[u][v]] = 1 - color[u], q.push(myvec[u][v]), myset[color[myvec[u][v]]].insert(myvec[u][v]), mark[myvec[u][v]] = 1; else if (color[myvec[u][v]] == color[u]) return false; } } return true; } int main() { long long int x, y; int a, b; cin >> a >> b; while (b--) { cin >> x >> y; myvec[x].push_back(y); myvec[y].push_back(x); } bool solve = true; for (int i = 1; i <= a; i++) { if (mark[i] == 0) { solve = (solve && isbipartite(i)); } } if (solve == true) { cout << myset[0].size() << endl; for (it = myset[0].begin(); it != myset[0].end(); it++) { cout << *it << ; } cout << endl; cout << myset[1].size() << endl; for (it = myset[1].begin(); it != myset[1].end(); it++) { cout << *it << ; } cout << endl; } else cout << -1 << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int timeTook[50010][10]; int timeSpent[50010]; int main() { int numOfPictures, numOfPainters; scanf( %d %d , &numOfPictures, &numOfPainters); for (int i = 0; i < numOfPictures; i++) { for (int j = 0; j < numOfPainters; j++) { scanf( %d , &timeTook[i][j]); } } for (int i = 0; i < numOfPictures; i++) timeSpent[i] = 0; for (int painter = 0; painter < numOfPainters - 1; painter++) { int curTime = 0; for (int i = 0; i < numOfPictures; i++) { if (curTime >= timeSpent[i]) { timeSpent[i] += (curTime - timeSpent[i]) + timeTook[i][painter]; curTime += timeTook[i][painter]; } else { int waitTime = timeSpent[i] - curTime; timeSpent[i] += timeTook[i][painter]; curTime += waitTime + timeTook[i][painter]; } } } int curTime = 0; for (int i = 0; i < numOfPictures; i++) { if (curTime >= timeSpent[i]) { curTime += timeTook[i][numOfPainters - 1]; } else { int waitTime = timeSpent[i] - curTime; curTime += waitTime + timeTook[i][numOfPainters - 1]; } printf( %d , curTime); } printf( n ); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; string s; int a[maxn], tot, n; int main() { while (cin >> n) { memset(a, 0, sizeof a); tot = 0; cin >> s; string temp = s; if (n == 1) { puts( 0 ); continue; } for (int i = n - 1; i >= 1; i--) { if (s[i] == B ) s[i] = W , s[i - 1] = (s[i - 1] == B ? W : B ), a[++tot] = i - 1; } if (s[0] == B ) { tot = 0; s = temp; for (int i = n - 1; i >= 1; i--) { if (s[i] == W ) s[i] = B , s[i - 1] = (s[i - 1] == B ? W : B ), a[++tot] = i - 1; } if (s[0] == W ) puts( -1 ); else { cout << tot << endl; for (int i = 1; i <= tot; i++) cout << a[i] + 1 << ; cout << endl; } } else { cout << tot << endl; for (int i = 1; i <= tot; i++) cout << a[i] + 1 << ; cout << endl; } } }
|
#include <bits/stdc++.h> using namespace std; int main() { int a = 0, b = 0, c = 0, d = 0, n, temp, dl; cin >> n; for (int i = 0; i <= n; i++) { if (i < n) { cin >> temp; } else { temp = max(a, d) + 1; } dl = temp - a; if (dl != 1 && dl != -1) { if (!b) { b = i + 1; d = temp; } else if (!c) { c = i; } else { b = 0; c = 0; break; } } else if (!c && b) { if (dl != -1) { b = 0; c = 0; break; } } a = temp; } cout << b << << c; }
|
#include <bits/stdc++.h> using namespace std; long long a, b, T, q, w, e, d[100001], v[100001], t[100001], B = 130; int main() { scanf( %lld%lld , &a, &b); for (int i = 1; i <= a; i++) scanf( %lld , &d[i]), v[d[i]] = i, t[i] = i; for (int i = 1; i <= a; i++) for (int j = 1; j <= B; j++) t[i] = d[t[i]]; for (int ii = 1; ii <= b; ii++) { scanf( %lld%lld%lld , &q, &w, &e); if (q == 1) { swap(d[w], d[e]); swap(v[d[w]], v[d[e]]); long long tt = e, yy = e; for (int i = 1; i <= B; i++) yy = d[yy]; for (int i = 1; i <= B + 1; i++) t[tt] = yy, tt = v[tt], yy = v[yy]; tt = w, yy = w; for (int i = 1; i <= B; i++) yy = d[yy]; for (int i = 1; i <= B + 1; i++) t[tt] = yy, tt = v[tt], yy = v[yy]; } else { long long ff = w; for (int i = 1; i <= e / B; i++) ff = t[ff]; e %= B; for (int i = 1; i <= e; i++) ff = d[ff]; printf( %lld n , ff); } } return 0; }
|
#include <bits/stdc++.h> using namespace std; int n, k, t, s; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> k; if (n < 3 * k) return cout << -1, 0; for (int i = 0; i < n; i++) if (i / k % 2 == 1) cout << (1 + i) % k + 1 << ; else cout << i % k + 1 << ; }
|
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& a) { in >> a.first >> a.second; return in; } template <typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1, T2> a) { out << a.first << << a.second; return out; } template <typename T, typename T1> T amax(T& a, T1 b) { if (b > a) a = b; return a; } template <typename T, typename T1> T amin(T& a, T1 b) { if (b < a) a = b; return a; } const long long fx[] = {+1, -1, +0, +0}; const long long fy[] = {+0, +0, +1, -1}; const int32_t M = 1e9 + 7; const int32_t MM = 998244353; long long xo, yo, ax, ay, bx, by, xs, ys, t; void solve() { cin >> xo >> yo >> ax >> ay >> bx >> by; cin >> xs >> ys >> t; vector<long long> x(1, xo), y(1, yo); long long LIMIT = (1LL << 62) - 1; while ((LIMIT - bx) / ax >= x.back() && (LIMIT - by) / ay >= y.back()) { x.push_back(ax * x.back() + bx); y.push_back(ay * y.back() + by); } long long n = x.size(); long long ans = 0; for (long long i = 0; i < n; i++) { for (long long j = i; j < n; j++) { long long length = x[j] - x[i] + y[j] - y[i]; long long dist2Left = abs(xs - x[i]) + abs(ys - y[i]); long long dist2Right = abs(xs - x[j]) + abs(ys - y[j]); if (length <= t - dist2Left || length <= t - dist2Right) ans = max(ans, j - i + 1); } } cout << ans << n ; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; while (t--) solve(); return 0; }
|
#include <bits/stdc++.h> #define For(i, a, b) for (int i = (a), _##i = (b); i < _##i; ++i) #define FOR(i, a, b) for (int i = (a), _##i = (b); i <= _##i; ++i) #define Forr(i, a, b) for (int i = (a), _##i = (b); i > _##i; --i) #define FORR(i, a, b) for (int i = (a), _##i = (b); i >= _##i; --i) #define all(a) a.begin(), a.end() #define range(i, a) for (auto& i : a) #define RANGE(i, a) for (auto i = a.rbegin(); i != a.rend(); ++i) #define SINGLE using ll = long long; using pii = std::pair<int, int>; using pdd = std::pair<double, double>; namespace Solver { const int N = (1 << 18) + 5; std::string opt; int res[N << 1]; int n, cnt; void update(int p, char c) { if (opt[p] == c) return; opt[p] = c; // if (c == ? ) // res[p] = res[p << 1] + res[p << 1 | 1]; while (p) { if (opt[p] == ? ) res[p] = res[p << 1] + res[p << 1 | 1]; else if (opt[p] == 1 ) res[p] = res[p << 1]; else res[p] = res[p << 1 | 1]; p >>= 1; } } void build(int u) { if ((u << 1) <= cnt) build(u << 1); if ((u << 1 | 1) <= cnt) build(u << 1 | 1); if (opt[u] == ? ) res[u] = res[u << 1] + res[u << 1 | 1]; else if (opt[u] == 1 ) res[u] = res[u << 1]; else res[u] = res[u << 1 | 1]; } void main() { std::cin >> n; std::string temp; std::cin >> temp; std::fill(res, res + (N << 1) - 1, 1); cnt = (1 << n) - 1; opt = ; RANGE (i, temp) opt.push_back(*i); build(1); int T; for (std::cin >> T; T--;) { int p; char c; std::cin >> p >> c; update(cnt - p + 1, c); std::cout << res[1] << std::endl; } } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); #ifndef SINGLE int T; for (std::cin >> T; T--;) #endif Solver::main(); return 0; }
|
#include <bits/stdc++.h> using namespace std; int ag[9][2], cg[9][2]; int main() { int n, m, k, s, t, t2, ans = 0; scanf( %d %d %d %d , &n, &m, &k, &s); memset(cg, 0x3f3f3f3f, sizeof(cg)); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { scanf( %d , &t); ag[t - 1][0] = max(ag[t - 1][0], i + 1 + j + 1); cg[t - 1][0] = min(cg[t - 1][0], i + 1 + j + 1); ag[t - 1][1] = max(ag[t - 1][1], i - j); cg[t - 1][1] = min(cg[t - 1][1], i - j); } scanf( %d , &t); for (int i = 1; i < s; i++) { scanf( %d , &t2); ans = max(ans, ag[t - 1][0] - cg[t2 - 1][0]); ans = max(ans, ag[t2 - 1][0] - cg[t - 1][0]); ans = max(ans, ag[t - 1][1] - cg[t2 - 1][1]); ans = max(ans, ag[t2 - 1][1] - cg[t - 1][1]); t = t2; } printf( %d n , ans); return 0; }
|
#include<iostream> #include<vector> #include<queue> #include<cstring> #include<cmath> #include<map> #include<set> #include<cstdio> #include<algorithm> #define debug(a) cout<<#a<< = <<a<<endl; using namespace std; const int maxn=1e3+100; typedef long long LL; LL a[maxn]; int main(void) { cin.tie(0);std::ios::sync_with_stdio(false); LL n;cin>>n; if(n==1){ cout<<4<<endl; } else{ if(n&1) cout<<(1+ ( (3*n*(n+4)+2)+ ( (n+4)*n+2 ) )/8 )<<endl; else cout<<(1+ ( (3*n*(n+4)+2)- ( (n+4)*n+2 ) )/8 )<<endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { long long int n; cin >> n; if (n == 1 || n == 2) { cout << n << endl; return 0; } if (n % 2 == 1) { cout << (n * (n - 1) * (n - 2)) << endl; return 0; } if (n == 4 || n == 6) { cout << ((n * (n - 1) * (n - 2)) / 2) << endl; return 0; } if (n % 3 == 0) { cout << ((n - 1) * (n - 2) * (n - 3)) << endl; return 0; } cout << (n * (n - 1) * (n - 3)) << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; long long a, b, t, z; bool dfs(long long x, long long y) { if (x < y) swap(x, y); if (!y) return 0; if (!dfs(y, x % y)) return 1; z = x / y; if (y & 1) return (z & 1) ^ 1; return z % (y + 1) & 1 ^ 1; } signed main() { scanf( %lld , &t); while (t--) { scanf( %lld%lld , &a, &b); if (dfs(a, b)) printf( First n ); else printf( Second n ); } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); cin.sync_with_stdio(0); cout.tie(0); string s, t, b; int n; vector<pair<char, char>> v; set<char> z; bool fine = false; char a[26]; for (int i = 0; i < 26; i++) a[i] = @ ; cin >> s >> t; for (int i = 0; i < s.length(); i++) if (a[s[i] - a ] == @ ) a[s[i] - a ] = t[i]; else if (a[s[i] - a ] != t[i]) { cout << -1; return 0; } for (int i = 0; i < 26; i++) if (a[i] != @ ) { if (((char)(i + a ) != a[a[i] - a ]) && ( @ != a[a[i] - a ])) { cout << -1; return 0; } } for (int i = 0; i < 26; i++) if ((a[i] != (char)( a + i)) && (a[i] != @ )) { fine = false; for (int j = 0; j < v.size(); j++) if ((v[j].first == a[i]) && (v[j].second == (char)( a + i))) fine = true; if (fine == false) v.push_back(make_pair((char)( a + i), a[i])); } for (int i = 0; i < v.size(); i++) z.insert(v[i].second); if (z.size() != v.size()) { cout << -1; return 0; } cout << v.size() << endl; for (int i = 0; i < v.size(); i++) cout << v[i].first << << v[i].second << endl; return 0; }
|
#include <bits/stdc++.h> #pragma GCC diagnostic ignored -Wunused-result #pragma GCC diagnostic ignored -Wsign-compare using namespace std; void solve(); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int test_case = 1; cin >> test_case; while (test_case--) { solve(); } cerr << time taken : << (float)clock() / CLOCKS_PER_SEC << secs << endl; return 0; } void solve() { int n, m, t; vector<int> arr; bool odd = false; bool zero = false; cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> t; if (t == 0) { zero = true; } if (t < 0) { odd = !odd; arr.push_back(-t); } else { arr.push_back(t); } } } sort(arr.begin(), arr.end()); int sum = 0; for (int i = 0; i < arr.size(); i++) { sum += arr[i]; } if (zero) { cout << sum << endl; } else { if (odd) { sum -= 2 * arr[0]; cout << sum << endl; } else { cout << sum << endl; } } }
|
#include <bits/stdc++.h> using namespace std; const int N = 200002; int n, x1, x2, p[N]; pair<int, int> a[N]; int i1, i2; bool solve() { i1 = i2 = -1; for (int i = n; i >= 1; --i) { if (a[i].first * (n - i + 1) >= x1) { i1 = i; break; } } for (int i = i1 - 1; i >= 1; --i) { if (a[i].first * (i1 - i) >= x2) { i2 = i; break; } } return i1 != -1 && i2 != -1; } int main() { ios_base::sync_with_stdio(0); cin >> n >> x1 >> x2; for (int i = 1; i <= n; ++i) { cin >> a[i].first; a[i].second = i; } sort(a + 1, a + 1 + n); if (solve()) { cout << Yes << endl; cout << (n - i1 + 1) << << (i1 - i2) << endl; for (int i = i1; i <= n; ++i) cout << a[i].second << ; cout << endl; for (int i = i2; i < i1; ++i) cout << a[i].second << ; cout << endl; return 0; } swap(x1, x2); if (solve()) { cout << Yes << endl; cout << (i1 - i2) << << (n - i1 + 1) << endl; for (int i = i2; i < i1; ++i) cout << a[i].second << ; cout << endl; for (int i = i1; i <= n; ++i) cout << a[i].second << ; cout << endl; return 0; } cout << No ; }
|
#include <bits/stdc++.h> using namespace std; const int N = 1E5 + 9; vector<int> g[N]; int tin[N]; int tout[N]; int sz[N]; int tt; int toys[N]; bool root[N]; void dfs(int v) { tin[v] = tt++; sz[v] = 1; for (auto i : g[v]) { dfs(i); sz[v] += sz[i]; } tout[v] = tt++; } bool parent(int v, int p) { return (tin[v] > tin[p] && tout[v] < tout[p]); } int main() { int n, m, k, q; cin >> n >> m >> k >> q; for (int i = 1; i <= n; i++) root[i] = true; for (int i = 1; i <= k; i++) { int v, t; cin >> v >> t; if (toys[t] == 0) { toys[t] = v; continue; } g[toys[t]].push_back(v); root[v] = false; toys[t] = v; } for (int i = 1; i <= n; i++) if (root[i]) dfs(i); for (int i = 1; i <= q; i++) { int v, t; cin >> v >> t; if (toys[t] == 0) { cout << 0 << endl; continue; } if (parent(toys[t], v)) { cout << sz[v] << endl; continue; } cout << 0 << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int sum, a, b, c, ans = 0; cin >> sum >> a >> b >> c; for (int i = 0; i <= a; i += 2) for (int j = 0; j <= c; ++j) if (sum - i / 2 - 2 * j <= b && sum - i / 2 - 2 * j >= 0) ans++; cout << ans << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int z = 1000000007; int main() { int n; cin >> n; if (n % 3 == 0) cout << 1 << 1 << (n - 2); else if (n % 3 == 1) cout << 1 << 1 << (n - 2); else cout << 1 << 2 << (n - 3); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 128; struct Matrix { double a[N][N]; Matrix() { for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) a[i][j] = (i == j ? 1 : 0); } Matrix operator*(const Matrix &A) const { Matrix R = Matrix(); for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) { R.a[i][j] = 0; for (int h = 0; h < N; h++) R.a[i][j] += a[i][h] * A.a[h][j]; } return R; } }; Matrix bin_pow(Matrix A, int p) { if (p == 0) return Matrix(); if (p == 2 || (p & 1)) return A * bin_pow(A, p - 1); return bin_pow(bin_pow(A, p / 2), 2); } double p[N]; int main() { int n, m; scanf( %d%d , &n, &m); for (int i = 0; i <= m; i++) scanf( %lf , &p[i]); Matrix A = Matrix(); for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) A.a[i][j] = p[i ^ j]; A = bin_pow(A, n); printf( %.13lf n , 1 - A.a[0][0]); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int t, b, p, f, h, c; cin >> t; vector<int> arr(t); for (int i = 0; i < t; ++i) { cin >> b >> p >> f; cin >> h >> c; if (h > c) { while (b >= 2 && p > 0) { arr[i] = arr[i] + h; b = b - 2; p--; } while (b >= 2 && f > 0) { arr[i] = arr[i] + c; b = b - 2; f--; } } else { while (b >= 2 && f > 0) { arr[i] = arr[i] + c; b = b - 2; f--; } while (b >= 2 && p > 0) { arr[i] = arr[i] + h; b = b - 2; p--; } } } for (int i = 0; i < t; ++i) { cout << arr[i] << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int A[2222][2]; int main() { int n, k, x, a, c, s, i; bool j; scanf( %d%d%d , &n, &k, &x); for (i = 0; i < n; i++) { scanf( %d , &a); A[a][0]++; } for (j = 0; k--; j = !j) { for (i = s = 0; i <= 2047; i++) { c = (s + A[i][j] + 1) / 2 - (s + 1) / 2; A[i][!j] += A[i][j] - c; A[i ^ x][!j] += c; s += A[i][j]; A[i][j] = 0; } } for (i = 2047; i >= 0; i--) { if (A[i][j]) { printf( %d , i); break; } } for (i = 0; i <= 2047; i++) { if (A[i][j]) { printf( %d n , i); break; } } return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; void solve() { long long int n, k; cin >> n >> k; if (n / 2 > k or n == 1) { if (k == 0 and n == 1) { cout << 1 << n ; return; } cout << -1 << n ; return; } else { vector<long long int> v; k -= (n / 2); k++; if (k == 0) k = 1; v.push_back(k); v.push_back(2 * k); map<long long int, long long int> make_pair; make_pair[k]++; make_pair[2 * k]++; long long int c = 1; for (__typeof(n / 2 - 1) i = (0) - ((0) > (n / 2 - 1)); i != (n / 2 - 1) - ((0) > (n / 2 - 1)); i += 1 - 2 * ((0) > (n / 2 - 1))) { while (make_pair[c] + make_pair[c + 1] != 0) { c++; } v.push_back(c); v.push_back(c + 1); make_pair[c]++; make_pair[c + 1]++; c += 2; } if (v.size() != n) { while (make_pair[2 * k + 1] != 0) k++; v.push_back(2 * k + 1); } for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); i += 1 - 2 * ((0) > (n))) { cout << v[i] << ; } } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t; t = 1; while (t--) { solve(); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-9; const long long MOD = 1000000007; const int inf = 1 << 30; const long long linf = 1LL << 60; const double PI = 3.14159265358979323846; int n, k; char s[100000]; vector<int> v[2]; int main() { scanf( %d%d , &n, &k); for (int i = 0; i < n; i++) scanf( %c , s + i); for (int i = 0; i < n; i++) { if (s[i] == 0 ) v[0].push_back(i); else v[1].push_back(i); } if (v[0].size() == 0 || v[1].size() == 0) { puts( tokitsukaze ); return 0; } if (v[0][v[0].size() - 1] - v[0][0] + 1 <= k || v[1][v[1].size() - 1] - v[1][0] + 1 <= k) { puts( tokitsukaze ); return 0; } for (int i = 0; i < n - k + 1; i++) { for (int j = 0; j < 2; j++) { if (i <= v[j ^ 1][0] && v[j ^ 1][0] < i + k) { int pos = lower_bound(v[j ^ 1].begin(), v[j ^ 1].end(), i + k) - v[j ^ 1].begin(); if (v[j ^ 1][v[j ^ 1].size() - 1] - v[j ^ 1][pos] + 1 > k) { puts( once again ); return 0; } } else if (i <= v[j ^ 1][v[j ^ 1].size() - 1] && v[j ^ 1][v[j ^ 1].size() - 1] < i + k) { int pos = lower_bound(v[j ^ 1].begin(), v[j ^ 1].end(), i) - v[j ^ 1].begin(); pos--; if (v[j ^ 1][pos] - v[j ^ 1][0] + 1 > k) { puts( once again ); return 0; } } else { puts( once again ); return 0; } } } puts( quailty ); }
|
#include <bits/stdc++.h> using namespace std; template <class T> void _R(T &x) { cin >> x; } void _R(int &x) { scanf( %d , &x); } void _R(long long &x) { scanf( %lld , &x); } void _R(double &x) { scanf( %lf , &x); } void _R(char &x) { scanf( %c , &x); } void _R(char *x) { scanf( %s , x); } void R() {} template <class T, class... U> void R(T &head, U &...tail) { _R(head); R(tail...); } template <class T> void _W(const T &x) { cout << x; } void _W(const int &x) { printf( %d , x); } void _W(const long long &x) { printf( %lld , x); } void _W(const double &x) { printf( %.16f , x); } void _W(const char &x) { putchar(x); } void _W(const char *x) { printf( %s , x); } template <class T, class U> void _W(const pair<T, U> &x) { _W(x.first); putchar( ); _W(x.second); } template <class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar( ); } void W() {} template <class T, class... U> void W(const T &head, const U &...tail) { _W(head); putchar(sizeof...(tail) ? : n ); W(tail...); } int MOD = 1e9 + 7; void ADD(long long &x, long long v) { x = (x + v) % MOD; if (x < 0) x += MOD; } const int SIZE = 1e6 + 10; int max_score[5] = {25, 25, 25, 25, 15}; pair<int, int> dp[6][4][201][201]; void pre() { for (int i = 0; i < (6); ++i) for (int win = 0; win < (4); ++win) for (int j = 0; j < (201); ++j) for (int k = 0; k < (201); ++k) dp[i][win][j][k].first = -1; dp[0][0][0][0] = {0, 0}; for (int i = 0; i < (5); ++i) for (int win = 0; win < (4); ++win) for (int j = 0; j < (201); ++j) for (int k = 0; k < (201); ++k) { if (dp[i][win][j][k].first == -1) continue; if (win == 3 || i - win == 3) continue; if (i - win < 3 && k + max_score[i] <= 200) { for (int score = (0); score <= (max_score[i] - 2); ++score) { if (j + score <= 200) { dp[i + 1][win][j + score][k + max_score[i]] = {score, max_score[i]}; } } } if (win < 3 && j + max_score[i] <= 200) { for (int score = (0); score <= (max_score[i] - 2); ++score) { if (k + score <= 200) { dp[i + 1][win + 1][j + max_score[i]][k + score] = {max_score[i], score}; } } } for (int score = (max_score[i] + 1); score <= (200); ++score) { int good = 0; if (i - win < 3 && j + score - 2 <= 200 && k + score <= 200) { good = 1; dp[i + 1][win][j + score - 2][k + score] = {score - 2, score}; } if (win < 3 && j + score <= 200 && k + score - 2 <= 200) { good = 1; dp[i + 1][win + 1][j + score][k + score - 2] = {score, score - 2}; } if (!good) break; } } } void print(int i, int win, int j, int k) { printf( %d:%d n , win, i - win); vector<pair<int, int> > AA; while (i) { AA.push_back(dp[i][win][j][k]); i--; if (AA.back().first > AA.back().second) win--; j -= AA.back().first; k -= AA.back().second; } reverse((AA).begin(), (AA).end()); for (int i = 0; i < (((int)(AA).size())); ++i) { if (i) printf( ); printf( %d:%d , AA[i].first, AA[i].second); } W( ); } void solve(int a, int b) { for (int lose = (0); lose <= (2); ++lose) { if (dp[lose + 3][3][a][b].first != -1) { print(lose + 3, 3, a, b); return; } } for (int win = 2; win >= 0; win--) { if (dp[win + 3][win][a][b].first != -1) { print(win + 3, win, a, b); return; } } W( Impossible ); } int main() { pre(); int ___T; scanf( %d , &___T); for (int cs = 1; cs <= ___T; cs++) { int a, b; R(a, b); solve(a, b); } return 0; }
|
#include <bits/stdc++.h> using namespace std; void inputOutput() {} signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; inputOutput(); string yelo = AHIMOTUVWXY ; string s; cin >> s; long long int n = s.length(); for (long long int i = 0; i < n; i++) { if (yelo.find(s[i]) == string::npos) return cout << NO , 0; } for (long long int i = 0; i < n / 2; i++) { if (s[i] != s[n - i - 1]) return cout << NO , 0; } cout << YES ; cerr << time elapsed : << 1.0 * clock() / CLOCKS_PER_SEC << sec n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 100, V = 100; struct bottle { int a, b; bool operator<(const bottle &bt) const { return b > bt.b; } } bot[N + 1]; int dp[N + 1][N * V + 1]; int main() { int n, shui = 0; scanf( %d , &n); for (int i = 1; i <= n; ++i) { scanf( %d , &bot[i].a); shui += bot[i].a; } for (int j = 1; j <= n; ++j) scanf( %d , &bot[j].b); sort(bot + 1, bot + n + 1); int ping = 0; for (int i = 1, w = shui; w > 0; ++i) { w -= bot[i].b; ++ping; } memset(dp, -1, sizeof dp); dp[0][0] = 0; for (int i = 1; i <= n; ++i) for (int j = ping; j; --j) for (int k = shui; k >= bot[i].a; --k) if (~dp[j - 1][k - bot[i].a]) dp[j][k] = max(dp[j][k], dp[j - 1][k - bot[i].a] + bot[i].b); int mx = 0; for (int i = shui; ~i; --i) if (dp[ping][i] >= shui) { mx = i; break; } printf( %d %d n , ping, shui - mx); return 0; }
|
#include <bits/stdc++.h> using namespace std; vector<int> g[300001]; bool mark[300001]; bool vis[300001]; int road_count = 0; int main(void) { int n, m; cin >> n >> m; int a, b; for (int i = 0; i < m; i++) { cin >> a >> b; a--; b--; g[a].push_back(b); g[b].push_back(a); } memset(mark, 0, sizeof(bool) * n); queue<int> q; for (int i = 0; i < n; i++) q.push(i); while (!q.empty()) { int x = q.front(); q.pop(); int count = 0; for (int i = 0; i < g[x].size(); i++) { if (mark[x] == mark[g[x][i]]) count++; } if (count > 1) { mark[x] = !mark[x]; for (int i = 0; i < g[x].size(); i++) { if (mark[x] == mark[g[x][i]]) q.push(g[x][i]); } } } for (int i = 0; i < n; i++) { if (mark[i]) cout << 1 ; else cout << 0 ; } }
|
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; struct node { int id, next; node() {} node(int a, int b) { id = a; next = b; } }; struct Brige { int x, y; } brige[maxn]; vector<int> v[maxn], g[maxn]; vector<node> q[maxn]; int dfn[maxn], low[maxn]; int f[maxn], belong[maxn]; bool visit[maxn]; int n, m, k, cnt, t, bge; void init() { for (int i = 1; i <= n; i++) { v[i].clear(); g[i].clear(); } } int find(int x) { if (x != f[x]) f[x] = find(f[x]); return f[x]; } void merge(int a, int b) { int x = find(a), y = find(b); if (x == y) return; f[y] = x; } void tarjan(int u, int father) { visit[u] = true; cnt++; dfn[u] = low[u] = cnt; for (int i = 0; i < (int)v[u].size(); i++) { int x = v[u][i]; if (!visit[x]) { tarjan(x, u); low[u] = min(low[u], low[x]); if (low[x] > dfn[u]) { bge++; brige[bge].x = u; brige[bge].y = x; } if (low[x] <= dfn[u]) merge(x, u); } else if (x != father) low[u] = min(low[u], dfn[x]); } } void rebuild() { t = 0; memset(belong, -1, sizeof(belong)); for (int i = 1; i <= n; i++) { int x = find(i); if (belong[x] == -1) { t++; belong[x] = t; } belong[i] = belong[x]; } for (int i = 1; i <= bge; i++) { int a = brige[i].x, b = brige[i].y; g[belong[a]].push_back(belong[b]); g[belong[b]].push_back(belong[a]); } } void solve() { memset(visit, false, sizeof(visit)); cnt = 0; bge = 0; for (int i = 1; i <= n; i++) f[i] = i; tarjan(1, 0); rebuild(); } bool Solve[maxn], check[maxn]; int ans[maxn], d[maxn]; void lca(int u) { visit[u] = true; for (int i = 0; i < (int)g[u].size(); i++) { int x = g[u][i]; if (!visit[x]) { d[x] = d[u] + 1; lca(x); f[x] = u; } } check[u] = true; for (int i = 0; i < (int)q[u].size(); i++) { int x = q[u][i].next; if (check[x] == true && Solve[q[u][i].id] == false) { int ansestor = find(x); ans[q[u][i].id] = d[u] + d[x] - 2 * d[ansestor]; Solve[q[u][i].id] = true; } } } int main() { int a, b; while (scanf( %d%d , &n, &m) != EOF) { init(); while (m--) { scanf( %d%d , &a, &b); v[a].push_back(b); v[b].push_back(a); } solve(); scanf( %d , &k); for (int i = 1; i <= k; i++) { scanf( %d%d , &a, &b); a = belong[a], b = belong[b]; q[a].push_back(node(i, b)); q[b].push_back(node(i, a)); } memset(visit, false, sizeof(visit)); memset(Solve, false, sizeof(Solve)); memset(check, false, sizeof(check)); for (int i = 1; i <= t; i++) f[i] = i; d[1] = 0; lca(1); for (int i = 1; i <= k; i++) printf( %d n , ans[i]); } return 0; }
|
#include <bits/stdc++.h> using namespace std; using INT = long long; int a[200100], b[200100]; map<int, int> mp[2]; int arr[200100]; map<int, int> vst; int main() { int n, st; cin >> n >> st; for (int i = 1; i <= n; i++) { scanf( %d , &a[i]); } int m, ed; cin >> m >> ed; for (int i = 1; i <= m; i++) { scanf( %d , &b[i]); } int d = 1; int ans = 1; for (int j = 1; j <= 30; j++) { mp[0].clear(); mp[1].clear(); int x = (1 << j); int nn = 0; for (int i = 1; i <= n; i++) { int r = a[i] % x; mp[0][r]++; ans = max(ans, mp[0][r]); arr[++nn] = r; } for (int i = 1; i <= m; i++) { int r = b[i] % x; mp[1][r]++; ans = max(ans, mp[1][r] + mp[0][(r + x / 2) % x]); arr[++nn] = r; } } for (int i = 1; i <= n; i++) vst[a[i]] = 1; for (int i = 1; i <= m; i++) if (vst[b[i]]) ans = max(ans, 2); cout << ans << endl; }
|
#include <bits/stdc++.h> using namespace std; int main() { long long int x; cin >> x; cout << x / 2520; }
|
#include <bits/stdc++.h> using namespace std; template <typename T> T abs(T a) { return a < 0 ? -a : a; } template <typename T> T sqr(T a) { return a * a; } const int INF = (int)1e9; const long double EPS = 1e-9; const long double PI = 3.1415926535897932384626433832795; const int N = 1000; pair<long double, long double> operator-( const pair<long double, long double>& a, const pair<long double, long double>& b) { return pair<long double, long double>(a.first - b.first, a.second - b.second); } pair<long double, long double> operator+( const pair<long double, long double>& a, const pair<long double, long double>& b) { return pair<long double, long double>(a.first + b.first, a.second + b.second); } inline pair<long double, long double> rotate( const pair<long double, long double>& a, const long double& ang) { return pair<long double, long double>( a.first * cos(ang) - a.second * sin(ang), a.first * sin(ang) + a.second * cos(ang)); } int m; int n; pair<long double, long double> p[N]; vector<int> g[N]; vector<int> pol[N]; const long double ROT_ANG = 2 * PI / 5; void draw(int cnt, int s1, int s2) { int cp[10]; cp[0] = s1; cp[1] = s2; pair<long double, long double> v = p[s2] - p[s1]; int prev = s2; for (int i = 0; i < int(3); ++i) { v = rotate(v, ROT_ANG); p[n] = p[prev] + v; cp[2 + i] = n; prev = n; n++; } v = rotate(v, ROT_ANG); pair<long double, long double> u = p[n - 1] + v; if (abs(p[s1].first - u.first) > 1e-1 || abs(p[s1].second - u.second) > 1e-1) throw; for (int i = 0; i < int(5); ++i) { int i1 = (i + 1) % 5; int i2 = (i + 4) % 5; for (int j = 0; j < int(5); ++j) { if (j != i1 && j != i && j != i2) g[cp[i]].push_back(cp[j]); } } for (int i = 0; i < int(5); ++i) pol[m - cnt].push_back(cp[i]); if (cnt > 1) { int i1 = 0; for (int i = 1; i < 5; ++i) if (p[cp[i1]] < p[cp[i]]) i1 = i; int i2 = i1 + 1; if (i2 == 5) i2 = 0; draw(cnt - 1, cp[i2], cp[i1]); } } set<pair<long double, long double> > visited; vector<int> ans; void fleury(int v) { while (!g[v].empty()) { int u = g[v].back(); g[v].pop_back(); if (visited.count(pair<long double, long double>(min(v, u), max(v, u)))) continue; visited.insert(pair<long double, long double>(min(v, u), max(v, u))); fleury(u); ans.push_back(u); } } int main() { cin >> m; p[0] = pair<long double, long double>(0, 0); p[1] = pair<long double, long double>(10, 0); n = 2; draw(m, 0, 1); long double minY = 1e100, minX = 1e100; for (int i = 0; i < int(n); ++i) { minY = min(minY, p[i].second); minX = min(minX, p[i].first); } for (int i = 0; i < int(n); ++i) { p[i].first -= minX + 4999; p[i].second -= minY + 4999; } cout << n << endl; for (int i = 0; i < int(n); ++i) { printf( %0.10lf %0.10lf n , (double)p[i].first, (double)p[i].second); } for (int i = 0; i < int(m); ++i) { for (int j = 0; j < int(5); ++j) { if (j != 0) printf( ); printf( %d , pol[i][j] + 1); } puts( ); } fleury(0); ans.push_back(0); for (int i = 0; i < int(int((ans).size())); ++i) { if (i != 0) printf( ); printf( %d , ans[i] + 1); } puts( ); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { set<char> deputi; vector<vector<char> > off; int m, n; char boss; cin >> n >> m >> boss; off.resize(n); for (int i = 0; i < n; i++) off[i].resize(m); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) do { cin >> off[i][j]; } while (off[i][j] == n ); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (off[i][j] == boss) { if (i > 0) deputi.insert(off[i - 1][j]); if (i < n - 1) deputi.insert(off[i + 1][j]); if (j > 0) deputi.insert(off[i][j - 1]); if (j < m - 1) deputi.insert(off[i][j + 1]); } deputi.erase( . ); deputi.erase(boss); cout << deputi.size() << endl; }
|
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, m; cin >> n >> m; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int num : a) { m -= num; } if (m != 0) cout << NO << endl; else cout << YES << endl; } }
|
#include <bits/stdc++.h> using namespace std; long long int n, t; int main() { cin >> n >> t; long long int a = (n - t) / 2; for (int i = 1; i <= n; i++) { cout << !((i) % (a + 1)); } }
|
#include <bits/stdc++.h> using namespace std; int main() { long long n, res = 0, cp = 0, cn = 0, cz = 0; cin >> n; vector<long long> arr(n); for (long long i = 0; i < n; i++) { cin >> arr[i]; if (arr[i] > 0) cp++; else if (arr[i] < 0) cn++; else cz++; } if (cp > (cn + cz)) { cout << 1 ; } else if (cn > (cp + cz)) { cout << -1 ; } else { if (cp == (cn + cz) && n % 2 == 0) cout << 1 ; else if (cn == (cp + cz) && n % 2 == 0) cout << -1 ; else cout << 0 ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; using ll = long long; const int inf = 1e9 + 7; const ll longinf = 1LL << 60; const ll mod = 1e9 + 7; int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; for (int i = 0; i < t; ++i) { int n; cin >> n; cout << n / 2 + 1 << n ; } }
|
#include <bits/stdc++.h> using namespace std; const int mod = (int)1e9 + 7; const int N_ = (int)1e6 + 2; long long Hash_a[N_]; long long Hash_b[N_]; long long pow26[N_]; string a, b; long long n; long long get_hash_a(long long x, long long y) { return (Hash_a[y] - Hash_a[x - 1] * pow26[y - x + 1] + 1LL * mod * mod) % mod; } long long get_hash_b(long long x, long long y) { return (Hash_b[y] - Hash_b[x - 1] * pow26[y - x + 1] + 1LL * mod * mod) % mod; } bool F(long long L1, long long R1, long long L2, long long R2) { int len = R1 - L1 + 1; if (len % 2 == 1) { return get_hash_a(L1, R1) == get_hash_b(L2, R2); } else { if (get_hash_a(L1, R1) == get_hash_b(L2, R2)) return true; int R_1 = L1 + len / 2 - 1; int L_1 = R_1 + 1; int R_2 = L2 + len / 2 - 1; int L_2 = R_2 + 1; if (F(L1, R_1, L_2, R2) && F(L_1, R1, L2, R_2)) return true; if (F(L1, R_1, L2, R_2) && F(L_1, R1, L_2, R2)) return true; return false; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> a >> b; n = a.size(); pow26[0] = 1; for (int i = 1; i <= n; i++) { Hash_a[i] = (Hash_a[i - 1] * 26 + (a[i - 1] - a + 1)) % mod; Hash_b[i] = (Hash_b[i - 1] * 26 + (b[i - 1] - a + 1)) % mod; pow26[i] = pow26[i - 1] * 26; pow26[i] %= mod; } if (F(1, n, 1, n)) cout << YES ; else cout << NO ; }
|
#include <bits/stdc++.h> using namespace std; inline void chkmax(long long &a, long long b) { if (a < b) a = b; } inline void chkmin(long long &a, long long b) { if (a > b) a = b; } inline long long read() { long long a = 0, fh = 1; char c = getchar(); while (c > 9 || c < 0 ) { if (c == - ) fh = -1; c = getchar(); } while ( 0 <= c && c <= 9 ) { a = a * 10 + c - 48; c = getchar(); } return a * fh; } vector<long long> edge[300005]; long long f[300005][2][2], tmp[2][2]; long long n, fa[300005]; void dfs(long long x) { f[x][0][0] = 1; f[x][0][1] = 1; for (long long i = 0; i < edge[x].size(); ++i) { if (fa[x] == edge[x][i]) continue; fa[edge[x][i]] = x; dfs(edge[x][i]); long long v = edge[x][i]; for (long long j = 0; j < 2; ++j) for (long long k = 0; k < 2; ++k) tmp[j][k] = f[x][j][k]; f[x][0][0] *= (f[v][0][0] + f[v][1][0] + f[v][1][1]) % 998244353; f[x][0][1] *= (f[v][0][0] + f[v][1][0] + f[v][1][1]) % 998244353; f[x][1][0] = (f[x][1][0] * ((f[v][0][0] * 2 + f[v][0][1] + f[v][1][1] * 2 + f[v][1][0] * 2) % 998244353) % 998244353 + tmp[0][0] * ((f[v][0][0] + f[v][0][1] + f[v][1][1] + f[v][1][0]) % 998244353)) % 998244353; f[x][1][1] = (f[x][1][1] * ((f[v][0][0] + f[v][1][0] * 2 + f[v][0][1] + f[v][1][1]) % 998244353) % 998244353 + tmp[0][1] * (f[v][0][0] + f[v][1][0])) % 998244353; for (long long j = 0; j < 2; ++j) for (long long k = 0; k < 2; ++k) f[x][j][k] %= 998244353; } } signed main() { n = read(); for (long long i = 1; i < n; ++i) { long long x = read(), y = read(); edge[x].push_back(y); edge[y].push_back(x); } dfs(1); printf( %lld n , (f[1][1][0] + f[1][1][1] + f[1][0][0] - 1 + 998244353) % 998244353); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, p, ans(0); cin >> n; map<string, int> mp; string s; while (n--) { cin >> s; mp[s]++; } for (auto x : mp) { if (x.first == Tetrahedron ) p = 4; else if (x.first == Cube ) p = 6; else if (x.first == Octahedron ) p = 8; else if (x.first == Dodecahedron ) p = 12; else p = 20; ans += p * x.second; } cout << ans; }
|
#include <bits/stdc++.h> using namespace std; struct ele { long long a, b; }; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long t; cin >> t; while (t--) { long long n; cin >> n; struct ele arr[n]; bool issorted = true, zer0 = false, one = false; for (long long i = 0; i < n; i++) { cin >> arr[i].a; if (i > 0 && arr[i].a < arr[i - 1].a) issorted = false; } for (long long i = 0; i < n; i++) { cin >> arr[i].b; if (arr[i].b == 1) one = true; else zer0 = true; } if (issorted) cout << Yes << n ; else if (one && zer0) cout << Yes << n ; else cout << No << n ; } return 0; }
|
#include<bits/stdc++.h> using namespace std; #define ll long long #define ld long double #define Test int T;cin>>T;while(T--) #define PI acos(-1) #define endl n #define fx(x) fixed<<setprecision(x) #define sz(s) (int)s.size() #define all(v) (v).begin(),(v).end() #define allr(v) (v).rbegin(),(v).rend() #define mem(a,n) memset((a),n,sizeof (a)) #define INF 1e9 #define ii pair<ll,ll> ll gcd(ll x,ll y){return(!y?x:gcd(y,x%y));} ll lcm(ll x,ll y){return x/gcd(x,y)*y;} void file(){ #ifndef ONLINE_JUDGE freopen( in.txt , r , stdin); freopen( out.txt , w , stdout); #else //freopen( bridges.in , r , stdin); //freopen( bridges.out , w , stdout); #endif } void fast(){ ios_base::sync_with_stdio(false); cin.tie(NULL); file(); } int dx[]= {-1,0,1,0}; int dy[]= {0,1,0,-1}; const double eps=1e-9; const int mod=1e9+7; const int N=1e5+5; int a[N]; int ask(int i){ if(a[i])return a[i]; cout<< ? <<i<<endl; cout.flush(); int x;cin>>x; return a[i]=x; } int main(){ fast(); int n; cin>>n; a[0]=a[n+1]=1e9; int l=1,r=n; while(l<=r){ int md=l+r>>1; int xl=ask(md); int xr=ask(md+1); if(xl>=xr){ l=md+1; } else r=md-1; } cout<< ! <<l<<endl; cout.flush(); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int qnt, time, felicity, time_to_eat, best = -999999999; cin >> qnt >> time; for (int i = 0; i < qnt; i++) { cin >> felicity >> time_to_eat; if (time_to_eat <= time && felicity > best) { best = felicity; } else if (time_to_eat > time) { int aux = felicity - (time_to_eat - time); if (aux > best) { best = felicity - (time_to_eat - time); } } } cout << best << endl; return 0; }
|
#include <bits/stdc++.h> int va[100000], vb[100000]; int main() { int na, nb, k, m, i, j; scanf( %d%d%d%d , &na, &nb, &k, &m); for (i = 0; i < na; i++) scanf( %d , &va[i]); for (i = 0; i < nb; i++) scanf( %d , &vb[i]); if (va[k - 1] < vb[nb - m]) printf( YES ); else printf( NO ); return 0; }
|
#include <bits/stdc++.h> using namespace std; vector<long long> erat; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long nd(long long a) { map<long long, long long> m; while (a != 1) { ++m[erat[a]]; a /= erat[a]; } long long count = 1; for (auto e : m) count *= e.second + 1; return count; } int main() { vector<vector<vector<bool> > > count( 8, vector<vector<bool> >(8, vector<bool>(8, true))); count[1][7][6] = false; count[3][7][6] = false; count[5][3][6] = false; count[5][3][7] = false; count[5][7][6] = false; count[7][2][5] = false; count[7][3][4] = false; count[7][3][5] = false; count[7][3][6] = false; count[7][3][7] = false; count[7][6][5] = false; count[7][7][5] = false; count[7][7][6] = false; ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); erat = vector<long long>(1e5 + 1, 1); erat[0] = erat[1] = 1; for (long long i = 2; i <= 1e5; ++i) { if (erat[i] == 1) { erat[i] = i; for (long long j = i * i; j <= 1e5; j += i) { if (erat[j] == 1) erat[j] = i; } } } long long t; cin >> t; while (t--) { long long a, b, c; cin >> a >> b >> c; long long d = gcd(a, b); long long e = gcd(a, c); long long f = gcd(b, c); long long g = gcd(d, c); a = nd(a); b = nd(b); c = nd(c); d = nd(d); e = nd(e); f = nd(f); g = nd(g); d -= g, e -= g, f -= g; a -= d + e + g, b -= d + f + g, c -= e + f + g; vector<long long> v(8); v[1] = a, v[2] = b, v[3] = d, v[4] = c, v[5] = e, v[6] = f, v[7] = g; long long total = 0; for (int i = 0; i < 4; ++i) { for (int j = 0; j < 4; ++j) { for (int k = 0; k < 4; ++k) { int l = 2 * i + 1; int m = 2 + j; if (j >= 2) m += 2; int p = 4 + k; if (count[l][m][p]) { if (l == m) { if (m == p) total += v[l] * (v[m] + 1) * (v[p] + 2) / 6; else total += v[l] * (v[m] + 1) * v[p] / 2; } else if (l == p or m == p) total += v[l] * v[m] * (v[p] + 1) / 2; else total += v[l] * v[m] * v[p]; } } } } cout << total << endl; } }
|
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; long long x[maxn], y[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long n; cin >> n; long long s = 0, sx = 0, sy = 0; for (int i = 0; i < n; i++) { cin >> x[i] >> y[i]; sx += x[i]; sy += y[i]; s += (n - 1) * (x[i] * x[i]) + (n - 1) * (y[i] * y[i]); } for (int i = 0; i < n; i++) { s -= ((x[i] * (sx - x[i])) + (y[i] * (sy - y[i]))); } cout << s; return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; long long max(long long a, long long b) { return a > b ? a : b; } struct SegmentTree { public: SegmentTree() { null_value = -INF; null_modifier = 0; } void insert_data(vector<long long> values) { size = 1; while (size < values.size()) { size *= 2; } modifier_tree.assign(size * 2 - 1, null_modifier); tree.assign(size * 2 - 1, null_value); for (int i = 0; i < values.size(); i++) { tree[size + i - 1] = values[i]; } for (int i = size - 2; i >= 0; i--) { tree[i] = merge(tree[l_child(i)], tree[r_child(i)]); } } void modify_segment(int begin, int end, long long modifier) { modify_segment(begin, end, modifier, 0, 0, size); } long long get_segment(int begin, int end) { return get_segment(begin, end, 0, 0, size); } protected: vector<long long> tree; vector<long long> modifier_tree; int size; long long null_value; long long null_modifier; long long merge(long long segment_1, long long segment_2) { if (segment_1 != null_value && segment_2 != null_value) { return max(segment_1, segment_2); } else if (segment_1 != null_value) { return segment_1; } else { return segment_2; } } long long modify(long long segment, long long modifier) { if (modifier != null_modifier) { return segment + modifier; } else { return segment; } } long long combine(long long new_modifier, long long old_modifier) { if (new_modifier != null_modifier && old_modifier != null_modifier) { return new_modifier + old_modifier; } else if (new_modifier != null_modifier) { return new_modifier; } else { return old_modifier; } } void add_modifier(int inode, long long modifier) { if (modifier != null_modifier) { modifier_tree[inode] = combine(modifier, modifier_tree[inode]); tree[inode] = modify(tree[inode], modifier); } } int l_child(int inode) { return inode * 2 + 1; } int r_child(int inode) { return inode * 2 + 2; } void push(int inode) { if (modifier_tree[inode] != null_modifier) { add_modifier(l_child(inode), modifier_tree[inode]); add_modifier(r_child(inode), modifier_tree[inode]); modifier_tree[inode] = null_modifier; } } void modify_segment(int begin, int end, long long modifier, int inode, int lnode, int rnode) { if (lnode < rnode) { if (lnode >= begin && rnode <= end) { add_modifier(inode, modifier); } else if (lnode >= end || rnode <= begin) { return; } else { push(inode); int mnode = (lnode + rnode) / 2; modify_segment(begin, end, modifier, l_child(inode), lnode, mnode); modify_segment(begin, end, modifier, r_child(inode), mnode, rnode); tree[inode] = merge(tree[l_child(inode)], tree[r_child(inode)]); } } } long long get_segment(int begin, int end, int inode, int lnode, int rnode) { if (lnode >= begin && rnode <= end) { return tree[inode]; } else if (lnode >= end || rnode <= begin) { return null_value; } else { push(inode); int mnode = (lnode + rnode) / 2; long long l_segment = get_segment(begin, end, l_child(inode), lnode, mnode); long long r_segment = get_segment(begin, end, r_child(inode), mnode, rnode); return merge(l_segment, r_segment); } } }; signed main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); int n, m, p; cin >> n >> m >> p; vector<pair<long long, long long>> atk(n), def(m); vector<pair<long long, pair<long long, long long>>> mon(p); for (int i = 0; i < n; i++) { cin >> atk[i].first >> atk[i].second; } for (int i = 0; i < m; i++) { cin >> def[i].first >> def[i].second; } for (int i = 0; i < p; i++) { cin >> mon[i].first >> mon[i].second.first >> mon[i].second.second; } sort(atk.begin(), atk.end()); sort(def.begin(), def.end()); sort(mon.begin(), mon.end()); vector<long long> b(m), cb(m); for (int i = 0; i < m; i++) { b[i] = def[i].first; cb[i] = -def[i].second; } SegmentTree tree; tree.insert_data(cb); int imon = 0; long long ans = -INF; for (int i = 0; i < n; i++) { while (imon < p && mon[imon].first < atk[i].first) { int rb = upper_bound(b.begin(), b.end(), mon[imon].second.first) - b.begin(); tree.modify_segment(rb, m, mon[imon].second.second); imon++; } ans = max(ans, tree.get_segment(0, m) - atk[i].second); } cout << ans; return 0; }
|
#include <bits/stdc++.h> using namespace std; #define int long long #define ld long double #define ll unsigned int #define endl n #define mp make_pair #define hcf __gcd #define test cout<< hello ; #define lcm(m,n) m*(n/__gcd(m,n)) #define maxa(v) *(max_element(all(v)) #define mina(v) *(min_element(all(v)) #define pb push_back #define ppb pop_back #define pf push_front #define ppf pop_front #define ff first #define ss second #define doge cout<< * #define mod (int)1e9 #define all(x) (x).begin(),(x).end() #define uniq(v) (v).erase(unique(all(v)),(v).end()) #define sz(x) (int)((x).size()) #define asort(v) sort(v.begin(),v.end()) #define dsort(v) sort(v.begin(),v.end(),greater<int>()) #define present(c,e) (c.find(e) != c.end()) #define vfind(c,e) (find(all(c),e) != c.end()) #define tr(c,it) for(typeof(c.begin()) it = c.begin(); it != c.end(); it++) ////////////////////////////////////////////////////////////////////////////////////////////////////////// bool isPrime(ll n) { if (n <= 1) return false; if (n <= 3) return true; if (n%2 == 0 || n%3 == 0) return false; for (ll i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false; return true; } long long power(int n,int k) { if(k==0) return 1; else return (n*power(n,k-1)%1000000007); } long long nCr(int n,int r) { if(n==r) return 1; else if (r==0) return 1; else return ((nCr(n-1,r-1)+nCr(n-1,r))%998244353); } long long sumDigit(ll x) { ll ans=0; while(x) { ans+=x%10; x/=10; } return ans;vector<int> v; } int bs(int a[],int b,int l,int r) { int mid=(l+r)/2; if(l>r) return (int)-1; if(a[mid]==b||l==r) return mid; if(a[mid]>b) return bs(a,b,l,mid-1); else return bs(a,b,mid+1,r); } int divideandconquerbc(int a[],int l,int r) { if(l>=r) return 0; int mid=(l+r)/2,ans=0; ans+=divideandconquerbc(a,l,mid); ans+=divideandconquerbc(a,mid+1,r); int x=r-l+1,y=mid-l+1; int c[x]; int i=0,j=0; for(int k=0;k<x;k++) { if((a[l+i]<=a[mid+1+j]&&i<y)||j>=x-y) {c[k]=a[l+i];i++;} else {c[k]=a[mid+1+j];j++;ans+=y-i;} } for(int k=l;k<=r;k++) a[k]=c[k-l]; return ans; } long long gcd(ll a,ll b) { if (b==0) return a; return gcd(b,a%b); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////// // count sum count1, cnt,cnt1 sum1 carry tip mx mn a1 a2 a3 // may the force be with us void solve(){ int n,q; cin>>n>>q; set<int>s; map<int,int>m; for (int i = 0; i < n; ++i) { int x; cin>>x; if(!present(s,x)){ m[x]=i+1; s.insert(x); } } for (int i = 0; i < q; ++i) { int z; cin>>z; cout<<m[z]<< ; for(auto i:m){ if(i.ss<m[z]){ m[i.ff]++; } } m[z]=1; } cout<<endl;} signed main(){ ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0); #ifndef ONLINE_JUDGE freopen( input.txt , r , stdin); freopen( output.txt , w , stdout); #endif int tsc=1; // cin>>tsc; while(tsc--) solve(); return 0; }
|
#include <bits/stdc++.h> using uint = unsigned int; using cat = long long; using dbl = long double; constexpr dbl pi = 3.14159265358979323846; using namespace std; template <typename T> T abs(T x) { return (x < 0) ? (-x) : x; } cat pw(cat a, cat e, cat mod) { if (e <= 0) return 1; cat x = pw(a, e / 2, mod); x = x * x % mod; return (e & 1) ? x * a % mod : x; } struct fin { vector<int> T; fin(int N) { T.resize(N + 10, 0); } int lastone(int x) { return x & (x ^ (x - 1)); } void put(int pos, int val) { for (int i = pos + 1; i < (int)T.size(); i += lastone(i)) T[i] += val; } int get(int pos) { int ret = 0; for (int i = pos + 1; i > 0; i -= lastone(i)) ret += T[i]; return ret; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(10); int N; cat K; cin >> N >> K; K = 1LL * N * (N - 1) / 2 - K + 1; vector<pair<cat, cat> > P(N); for (int i = 0; i < N; i++) cin >> P[i].first >> P[i].second; dbl ans_le = 0, ans_gt = 1e5; while (ans_gt - ans_le > 1e-8) { dbl R = (ans_le + ans_gt) / 2; vector<pair<dbl, int> > events; for (int i = 0; i < N; i++) { dbl d = sqrt(P[i].first * P[i].first + P[i].second * P[i].second); if (d < R + 1e-8) continue; dbl ang_c = atan2(P[i].second, P[i].first); if (ang_c < 0) ang_c = 2 * pi + ang_c; dbl ang_d = acos(R / d); if (ang_c - ang_d < 1e-8) { events.push_back({ang_c + ang_d, 2 * i}); events.push_back({ang_c - ang_d, 2 * i + 1}); events.push_back({ang_c - ang_d + 2 * pi, 2 * i + 1}); } else if (ang_c + ang_d > 2 * pi - 1e-8) { events.push_back({ang_c + ang_d - 2 * pi, 2 * i}); events.push_back({ang_c - ang_d, 2 * i + 1}); events.push_back({ang_c - ang_d - 2 * pi, 2 * i + 1}); } else { events.push_back({ang_c + ang_d, 2 * i}); events.push_back({ang_c - ang_d, 2 * i + 1}); } } sort(begin(events), end(events)); vector<int> last_open(N, -1); cat cnt = 0; fin F(events.size()); for (int i = 0; i < (int)events.size(); i++) { int id = events[i].second / 2; if (events[i].second % 2 == 0) { if (last_open[id] != -1) { F.put(events.size() - last_open[id], -1); cnt += F.get(events.size() - last_open[id]); last_open[id] = -1; } } else { last_open[id] = i; F.put(events.size() - i, 1); } } if (cnt >= K) ans_le = R; else ans_gt = R; } cout << ans_le << n ; }
|
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 5; struct edg { int u, v, l, r; edg(int u = 0, int v = 0, int l = 0, int r = 0) : u(u), v(v), l(l), r(r) {} bool operator<(const edg &n1) const { return l > n1.l; } }; priority_queue<edg> q; vector<edg> adj[maxn][2]; int n, m; int dp[maxn][2]; inline void add(int u, int v, int l, int r) { int op = l % 2; if (dp[u][op] >= l) { if (v == n) { cout << l + 1 << endl; exit(0); } if (r + 1 > dp[v][op ^ 1]) { dp[v][op ^ 1] = r + 1; for (int i = 0; i < adj[v][op ^ 1].size(); i++) { edg t = adj[v][op ^ 1][i]; if (l + 1 <= t.r) q.push(edg(v, t.v, l + 1, t.r)); } adj[v][op ^ 1].clear(); } } else { adj[u][op].push_back(edg(u, v, l, r)); } } int main() { cin >> n >> m; memset(dp, 0xf3, sizeof(dp)); dp[1][0] = 0; for (int i = 1; i <= m; i++) { int u, v, l, r; scanf( %d%d%d%d , &u, &v, &l, &r); if ((r - l + 1) % 2 == 0) { q.push(edg(u, v, l, r - 1)); q.push(edg(v, u, l, r - 1)); if (l + 1 <= r - 2) { q.push(edg(u, v, l + 1, r - 2)); q.push(edg(v, u, l + 1, r - 2)); } } else { if (l <= r - 2) { q.push(edg(u, v, l, r - 2)); q.push(edg(v, u, l, r - 2)); } q.push(edg(u, v, l + 1, r - 1)); q.push(edg(v, u, l + 1, r - 1)); } } if (n == 1) { cout << 0 << endl; return 0; } while (!q.empty()) { edg t = q.top(); q.pop(); add(t.u, t.v, t.l, t.r); } cout << -1 << endl; return 0; }
|
#include<iostream> #include <bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 #define INF 100000007 #define MAX 1000000 #define nl n #define F first #define S second #define pb(a) push_back(a) typedef vector<ll> vi; typedef priority_queue<ll , vector<ll> , greater<ll>()> pqmx; typedef priority_queue<ll> pqmn; typedef pair<ll , ll> pi; typedef vector< pair<int , int> > vpi; #define fr(i,n) for(i=0;i<n;i++) #define rep(i,a,n) for(i=a;i<n;i++) int n , t , m , temp , temp2, root , p ,q , k , i , j; struct comp { bool operator()( pair<int, int> const& a, pair<int, int> const& b) { return a.first < b.first || (a.first == b.first && a.second > b.second); } }; void func(){ ll A , B , C; cin>>A>>B>>C; if((A + B + C)%9 != 0){ cout<< NO << n ; return; } ll p = (A + B + C) / 9; if(p > A || p > B || p > C){ cout<< NO << n ; return; } cout<< YES << n ; } int main(){ #ifndef ONLINE_JUDGE freopen( input.txt , r , stdin); freopen( output.txt , w , stdout); #endif cin>>t; while(t--){ func(); } }
|
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; namespace Xrocks {} using namespace Xrocks; namespace Xrocks { class in { } user_input; class out { } output; in& operator>>(in& X, int& Y) { scanf( %d , &Y); return X; } in& operator>>(in& X, char* Y) { scanf( %s , Y); return X; } in& operator>>(in& X, float& Y) { scanf( %f , &Y); return X; } in& operator>>(in& X, double& Y) { scanf( %lf , &Y); return X; } in& operator>>(in& X, char& C) { scanf( %c , &C); return X; } in& operator>>(in& X, string& Y) { cin >> Y; return X; } in& operator>>(in& X, long long& Y) { scanf( %lld , &Y); return X; } template <typename T> in& operator>>(in& X, vector<T>& Y) { for (auto& x : Y) user_input >> x; return X; } template <typename T> out& operator<<(out& X, T Y) { cout << Y; return X; } template <typename T> out& operator<<(out& X, vector<T>& Y) { for (auto& x : Y) output << x << ; return X; } out& operator<<(out& X, const int& Y) { printf( %d , Y); return X; } out& operator<<(out& X, const char& C) { printf( %c , C); return X; } out& operator<<(out& X, const string& Y) { printf( %s , Y.c_str()); return X; } out& operator<<(out& X, const long long& Y) { printf( %lld , Y); return X; } out& operator<<(out& X, const float& Y) { printf( %f , Y); return X; } out& operator<<(out& X, const double& Y) { printf( %lf , Y); return X; } out& operator<<(out& X, const char Y[]) { printf( %s , Y); return X; } template <typename T> T max(T A) { return A; } template <typename T, typename... args> T max(T A, T B, args... S) { return max(A > B ? A : B, S...); } template <typename T> T min(T A) { return A; } template <typename T, typename... args> T min(T A, T B, args... S) { return min(A < B ? A : B, S...); } template <typename T> void vectorize(int y, vector<T>& A) { A.resize(y); } template <typename T, typename... args> void vectorize(int y, vector<T>& A, args&&... S) { A.resize(y); vectorize(y, S...); } long long fast(long long a, long long b, long long pr) { if (b == 0) return 1 % pr; long long ans = 1 % pr; while (b) { if (b & 1) ans = (ans * a) % pr; b >>= 1; a = (a * a) % pr; } return ans; } int readInt() { int n = 0; scanf( %d , &n); return n; int ch = getchar_unlocked(); int sign = 1; while (ch < 0 || ch > 9 ) { if (ch == - ) sign = -1; ch = getchar_unlocked(); } while (ch >= 0 && ch <= 9 ) n = (n << 3) + (n << 1) + ch - 0 , ch = getchar_unlocked(); n = n * sign; return n; } long long readLong() { long long n = 0; int ch = getchar_unlocked(); int sign = 1; while (ch < 0 || ch > 9 ) { if (ch == - ) sign = -1; ch = getchar_unlocked(); } while (ch >= 0 && ch <= 9 ) n = (n << 3) + (n << 1) + ch - 0 , ch = getchar_unlocked(); n = n * sign; return n; } long long inv_(long long val, long long pr = static_cast<long long>(163577857)) { return fast(val, pr - 2, pr); } } // namespace Xrocks int op1(int a, int b) { return a | b; } int op2(int a, int b) { return a & b; } int op3(int a, int b) { return a ^ b; } class solve { public: solve() { vector<int (*)(int, int)> g = {op1, op2, op3}; vector<int> v{0, 1, 2}; vector<int> w(4); user_input >> w; int x = 0; do { vector<int (*)(int, int)> f = g; for (int i = 0; i < 3; i++) f[i] = g[v[i]]; if (x == 5) output << f[0](f[1](f[0](w[0], w[1]), f[2](w[2], w[3])), f[2](f[1](w[1], w[2]), f[0](w[3], w[0]))) << n ; ++x; } while (next_permutation(v.begin(), v.end())); } }; int32_t main() { int t = 1, i = 1; if (0) scanf( %d , &t); while (t--) { if (0) printf( Case #%d: , i++); new solve; } return 0; }
|
#include <bits/stdc++.h> using namespace std; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << = << a << endl; err(++it, args...); } template <class T, class R> ostream& operator<<(ostream& os, pair<T, R>& v) { os << ( << v.first << << v.second << ) << ; return os; } template <class T> ostream& operator<<(ostream& os, vector<T>& v) { for (auto i : v) os << i << ; return os; } template <class T> ostream& operator<<(ostream& os, set<T>& v) { for (auto i : v) os << i << ; return os; } unordered_map<string, string*> some; struct Node { Node* parent; vector<Node*> children; int wordsCount; string* word; Node(Node* parent = nullptr) : parent(parent), wordsCount(0), word(nullptr) { children.resize(26, nullptr); } Node* getChild(char c) { if (children[c - a ] == nullptr) { children[c - a ] = new Node(this); } return children[c - a ]; } }; struct Bor { unordered_set<string> words; Node* root; Bor() : root(new Node()) {} int addWord(string& s) { bool was = (words.find(s) != words.end()); Node* now = root; int ans = 0; int step = 1; int stepForStep = 1e9; int len = 0; bool gg = false; for (char& x : s) { len++; now = now->getChild(x); stepForStep--; if (stepForStep == 0) step = 1; ans += step; if (!gg && now->wordsCount == 1) { if (was && len != s.size()) { ans++; step = 0; stepForStep = 1e9; } else if (now->word != nullptr && now->word->size() != len && *now->word == s.substr(0, now->word->size())) { ans++; step = 0; stepForStep = now->word->size() - len + 1; } gg = true; } } if (step != 0) { if (now->word == nullptr) now->word = new string(s); auto kek = now->word; while (now != root) { now->wordsCount += 1; if (now->wordsCount == 1) now->word = kek; now = now->parent; } if (now->wordsCount == 1) now->word = kek; now->wordsCount += 1; } words.insert(s); return ans; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(0); string s; int ans = 0; int count = 0; Bor bor; int gl = 0; int vsego = 0; while (getline(cin, s)) { vsego += s.size() + 1; ans += 1; string now; for (char x : s) { if (x >= a && x <= z || x >= A && x <= Z ) { now += x; } else { ans++; if (now != ) { int cnt = bor.addWord(now); ans += cnt; gl += cnt; now = ; } } } if (now != ) { int cnt = bor.addWord(now); ans += cnt; gl += cnt; } } cout << ans; }
|
#include <bits/stdc++.h> using ul = std::uint32_t; using li = std::int32_t; using ull = std::uint64_t; using ll = std::int64_t; using vul = std::vector<ul>; using vvul = std::vector<vul>; using vb = std::vector<bool>; using vvb = std::vector<vb>; using vvvb = std::vector<vvb>; using vull = std::vector<ull>; using vvull = std::vector<vull>; using vli = std::vector<li>; using vll = std::vector<ll>; using pulul = std::pair<ul, ul>; using vpulul = std::vector<pulul>; ul n; ll s; class point { public: li x = 0; li y = 0; point() = default; point(li a, li b) : x(a), y(b) {} }; point operator-(const point& a, const point& b) { return point(a.x - b.x, a.y - b.y); } point operator+(const point& a, const point& b) { return point(a.x + b.x, a.y + b.y); } ul region(const point& a) { if (a.x < 0) { if (a.y < 0) { return 3; } else { return 2; } } else { if (a.y < 0) { return 4; } else { return 1; } } } class lessbydir { public: bool operator()(const point& a, const point& b) { if (region(a) != region(b)) { return region(a) < region(b); } else { return ll(a.x) * b.y > ll(b.x) * a.y; } } } less; std::vector<point> points(2000); vul order(2000); vul pos(2000); std::vector<std::pair<ul, ul>> vecs; ll det(const point& a, const point& b) { return ll(a.x) * b.y - ll(b.x) * a.y; } ll inner(const point& a, const point& b) { return ll(a.x) * b.x + ll(a.y) * b.y; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cin >> n >> s; s <<= 1; points.resize(n); order.resize(n); for (ul i = 0; i != n; ++i) { std::cin >> points[i].x >> points[i].y; order[i] = i; } for (ul i = 0; i != n; ++i) { for (ul j = 0; j != n; ++j) { if (i == j) { continue; } vecs.push_back(std::pair<ul, ul>(i, j)); } } std::sort(vecs.begin(), vecs.end(), [&](const pulul& a, const pulul& b) { return less(points[a.second] - points[a.first], points[b.second] - points[b.first]); }); point dir0 = points[vecs[0].second] - points[vecs[0].first]; std::sort(order.begin(), order.end(), [&](ul a, ul b) { return det(dir0, points[a]) < det(dir0, points[b]); }); for (ul i = 0; i != n; ++i) { pos[order[i]] = i; } for (auto vec : vecs) { ul i = vec.first; ul j = vec.second; ul l = 0, r = n; point dir = points[j] - points[i]; while (l + 1 != r) { ul mid = l + r >> 1; if (det(dir, points[order[mid]] - points[i]) > s) { r = mid; } else { l = mid; } } if (det(points[j] - points[i], points[order[l]] - points[i]) == s) { std::cout << Yes << std::endl; std::cout << points[i].x << << points[i].y << std::endl; std::cout << points[j].x << << points[j].y << std::endl; std::cout << points[order[l]].x << << points[order[l]].y << std::endl; return 0; } if (pos[j] > pos[i]) { std::swap(order[pos[j]], order[pos[i]]); std::swap(pos[j], pos[i]); } } std::cout << No << std::endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MaxN = 1000; int n, m; int cnt[MaxN + 5][MaxN + 5]; bool Check(int x, int y) { if (x < 0) return false; if (y < 0) return false; if (x > n) return false; if (y > n) return false; return true; } int main() { while (~scanf( %d%d , &n, &m)) { memset(cnt, 0, sizeof(cnt)); for (int i = 1; i <= m; i++) { int x, y; scanf( %d%d , &x, &y); for (int nx = x - 2; nx <= x; nx++) for (int ny = y - 2; ny <= y; ny++) if (Check(nx, ny)) { cnt[nx][ny]++; if (cnt[nx][ny] == 9) { printf( %d n , i); return 0; } } } printf( -1 n ); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int www = 9; const int Size = 10000; char buffer[Size]; int dr[] = {-1, 0, 1, 0}; int dc[] = {0, 1, 0, -1}; const int inf = 0x0fffffff; const int size = 1000; int d[size]; int solution(int nTest) { int k, a, b; scanf( %d%d%d , &k, &a, &b); scanf( %s , buffer); string s = buffer; int n = s.size(); if (n / k >= a && (n + k - 1) / k <= b) { vector<string> res; int t = n / k; int tt = (n + k - 1) / k; int r = k - (n % k); for (int i = 0; i < k - (n % k); i++) { res.push_back(s.substr(i * t, t)); } for (int i = 0; i < n % k; i++) { res.push_back(s.substr(t * r + i * tt, tt)); } for (int i = 0; i < res.size(); i++) { puts(res[i].c_str()); } return 0; } puts( No solution ); return 0; } int main() { int i = 0, n = 9999; while (i < n && solution(i)) { i++; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int radix_len = 1e7 + 5; long long cnt[radix_len]; int main() { int n, k, a; ios_base::sync_with_stdio(false); cin >> n >> k; long long tot = 0; for (int i = (0), i_end_ = (n); i < i_end_; ++i) { cin >> a; cnt[a]++; tot += a; } if (tot < k) { cout << -1 << endl; return 0; } auto bound = 1; auto sum = 0LL; for (int i = radix_len - 1; i > 0; i--) { sum += cnt[i]; if (sum >= k) { bound = i; break; } } for (int i = radix_len - 1; i > 1; i--) { if (i / 2 < bound) break; cnt[i / 2] += cnt[i]; cnt[i - i / 2] += cnt[i]; sum += cnt[i]; cnt[i] = 0; while (sum - cnt[bound] >= k) { sum -= cnt[bound]; bound++; } } cout << bound << endl; }
|
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; if (k < n / 2) { cout << -1; return 0; } if (n == 1) { if (k) cout << -1; else cout << 1; return 0; } int x = k - (n - 2) / 2; cout << x << << 2 * x << ; for (int i = 2, j = 2 * x + 1; i < n; i++, j++) cout << j << ; return 0; }
|
#include <bits/stdc++.h> using namespace std; int n, m, u, v, i, j, c[505], ca = 0, cb = 0, cc = 0, cnt, nt; int main() { cin >> n >> m; vector<int> g[505]; for (i = 0; i < 505; i++) c[i] = 0; for (i = 0; i < m; i++) { cin >> u >> v; g[u].push_back(v); g[v].push_back(u); if (g[u].size() == n - 1) { c[u] = 2; cb++; } if (g[v].size() == n - 1) { c[v] = 2; cb++; } } for (i = 1; i <= n; i++) { if (c[i] == 0) break; } if (i != n + 1) { c[i] = 1; ca++; for (j = 0; j < g[i].size(); j++) { v = g[i][j]; if (c[v] == 0) { c[v] = 1; ca++; } } } while (i <= n && c[i] != 0) i++; if (i != n + 1) { c[i] = 3; cc++; for (j = 0; j < g[i].size(); j++) { v = g[i][j]; if (c[v] == 2) continue; if (c[v] == 1) { cout << NO << endl; return 0; } else { c[v] = 3; cc++; } } } while (i <= n) { if (c[i] == 0) { cout << NO << endl; return 0; } i++; } for (i = 1; i <= n; i++) { u = i; if (c[i] == 1) { cnt = ca - 1 + cb; nt = 3; } else if (c[i] == 2) cnt = n - 1; else { cnt = cc - 1 + cb; nt = 1; } if (g[u].size() == n - 1) continue; if (g[u].size() != cnt) { cout << NO << endl; return 0; } for (j = 0; j < g[u].size(); j++) { if (c[g[u][j]] == nt) { cout << NO << endl; return 0; } } } cout << YES << endl; for (i = 1; i <= n; i++) { if (c[i] == 1) cout << a ; else if (c[i] == 2) cout << b ; else cout << c ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; map<string, int> Map; string s[181], sn; int n, i; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> sn; cin >> n; s[1] = January ; s[2] = February ; s[3] = March ; s[4] = April ; s[5] = May ; s[6] = June ; s[7] = July ; s[8] = August ; s[9] = September ; s[10] = October ; s[11] = November ; s[12] = December ; for (i = 13; i <= 24; i++) s[i] = s[i - 12]; for (i = 1; i <= 12; i++) Map[s[i]] = i; cout << s[Map[sn] + n % 12] << n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; int n, k, h; struct Node { int m, v; int id; bool operator<(const Node &x) const { if (m != x.m) return m < x.m; return v < x.v; } } q[100005]; bool ok(double times) { int i, j; for (j = 1, i = 0; i < n; i++) { if (q[i].v * times - (double)j * h >= -1e-9) j++; if (j > k) return true; } return false; } int main() { int i, j; cin >> n >> k >> h; for (i = 0; i < n; i++) cin >> q[i].m; for (i = 0; i < n; i++) { cin >> q[i].v; q[i].id = i + 1; } sort(q, q + n); double l, r, mid; l = 0, r = 1e20; for (int i = 0; i < 200; i++) { mid = (l + r) * 0.5; if (!ok(mid)) l = mid; else r = mid; } j = 1; for (i = 0; i < n; i++) { if (q[i].v * r - (double)j * h >= -1e-9) { cout << q[i].id << ; j++; } if (j > k) break; } }
|
#include <bits/stdc++.h> int n, a[1000][1000]; int main() { for (int i = 1; i <= 100; i++) for (int j = 1; j <= 100; j++) a[i][j] = 0; scanf( %d , &n); for (int i = 1; i <= n; i++) { int t, v; scanf( %d , &t); for (int j = 1; j <= t; j++) { scanf( %d , &v); a[i][v] = 1; } } bool f1; for (int i = 1; i <= n; i++) { f1 = true; bool f; for (int j = 1; j <= n; j++) if (i != j) { f = false; for (int k = 1; k <= 100; k++) if (a[j][k] == 1 && a[i][k] == 0) { f = true; break; } if (f == false) { f1 = false; break; } } if (f1 == true) printf( YES ); else printf( NO ); printf( n ); } return 0; }
|
#include <bits/stdc++.h> using namespace std; int N[10]; string shirt[5] = { S , M , L , XL , XXL }; map<string, int> mp; int main() { int K, i, d; for ((i) = 0; (i) < (int)(5); (i)++) cin >> N[i]; cin >> K; for ((i) = 0; (i) < (int)(5); (i)++) mp[shirt[i]] = i; for ((i) = 0; (i) < (int)(K); (i)++) { string s; cin >> s; int x = mp[s]; int ans = -1; for (d = 0;; d++) { if (x + d >= 0 && x + d < 5 && N[x + d] > 0) { ans = x + d; break; } if (x - d >= 0 && x - d < 5 && N[x - d] > 0) { ans = x - d; break; } } N[ans]--; cout << shirt[ans] << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; template <typename T> inline T Abs(T a) { return a > 0 ? a : -a; } template <typename T> inline T sqr(T a) { return a * a; } template <typename T> inline void relaxMin(T &a, T b) { if (b < a) a = b; } template <typename T> inline void relaxMax(T &a, T b) { if (b > a) a = b; } const int INF = (int)1E9; const long double EPS = 1E-6; const long double PI = 3.1415926535897932384626433832795; int dp[2000][2000]; int mod = 1000000007; void solve() { int n; cin >> n; int k; int x = 0, y = 0; for (int i(0), _bb(n); i < _bb; ++i) { cin >> k; if (k == 1) x++; else y++; } dp[0][0] = 1; dp[1][0] = dp[1][1] = 1; for (int i(2), _bb(1001); i < _bb; ++i) { dp[i][0] = (dp[i - 1][0] + (i - 1) * 1LL * dp[i - 2][0]) % mod; for (int j(1), _bb(1001); j < _bb; ++j) dp[i][j] = (dp[i - 1][j - 1] * 1LL * i) % mod; } cout << dp[n][y] << endl; } int main() { srand(0xA1B2C3D4); solve(); return 0; }
|
#include <bits/stdc++.h> using namespace std; int n, m; void update(int *c, int x, int v) { while (x <= m) { c[x] += v; x += x & -x; } } long long sum(int *c, int x) { long long ret = 0; while (x > 0) { ret += c[x]; x -= x & -x; } return ret; } long long calc(int *c, int L, int R) { return sum(c, R) - sum(c, L - 1); } int c[3060 + 3060][3060], L[3060][3060], R[3060][3060], Ld[3060][3060]; char s[3060][3060]; vector<pair<int, int> > g[3060]; int main() { scanf( %d%d , &n, &m); for (int i = 1; i <= n; ++i) scanf( %s , s[i] + 1); for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) { if (s[i][j] == . ) L[i][j] = 0; else L[i][j] = L[i][j - 1] + 1; } for (int i = n; i > 0; --i) for (int j = m; j > 0; --j) { if (s[i][j] == . ) R[i][j] = 0; else R[i][j] = R[i][j + 1] + 1; if (s[i][j] == . ) Ld[i][j] = 0; else Ld[i][j] = Ld[i + 1][j - 1] + 1; } for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) g[j + R[i][j] - 1].push_back(make_pair(i, j)); long long ans = 0; for (int j = m; j > 0; --j) { for (int i = 0; i < g[j].size(); ++i) { int x = g[j][i].first; int y = g[j][i].second; update(c[x + y], y, 1); } for (int i = 1; i <= n; ++i) { int mi = min(L[i][j], Ld[i][j]); if (!mi) continue; ans += calc(c[i + j], j - mi + 1, j); } } cout << ans << endl; return 0; }
|
#include <bits/stdc++.h> int main() { int t; scanf( %d , &t); int A[t]; for (int i = 0; i < t; i++) { scanf( %d , &A[i]); } int count = 1, max = 1; for (int i = 0; i < t - 1; i++) { if (A[i] <= A[i + 1]) { count++; if (max <= count) max = count; } else { if (max < count) { max = count; } count = 1; } } printf( %d , max); }
|
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; template <int mod> struct ModInt { int x; ModInt() : x(0) {} ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {} ModInt &operator+=(const ModInt &p) { if ((x += p.x) >= mod) x -= mod; return *this; } ModInt &operator-=(const ModInt &p) { if ((x += mod - p.x) >= mod) x -= mod; return *this; } ModInt &operator*=(const ModInt &p) { x = (int)(1LL * x * p.x % mod); return *this; } ModInt &operator/=(const ModInt &p) { *this *= p.inverse(); return *this; } ModInt operator-() const { return ModInt(-x); } ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; } ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; } ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; } ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; } bool operator==(const ModInt &p) const { return x == p.x; } bool operator!=(const ModInt &p) const { return x != p.x; } ModInt inverse() const { int a = x, b = mod, u = 1, v = 0, t; while (b > 0) { t = a / b; swap(a -= t * b, b); swap(u -= t * v, v); } return ModInt(u); } ModInt pow(int64_t n) const { ModInt ret(1), mul(x); while (n > 0) { if (n & 1) ret *= mul; mul *= mul; n >>= 1; } return ret; } friend ostream &operator<<(ostream &os, const ModInt &p) { return os << p.x; } friend istream &operator>>(istream &is, ModInt &a) { int64_t t; is >> t; a = ModInt<mod>(t); return (is); } static int get_mod() { return mod; } }; using mint = ModInt<MOD>; long long mod_pow(long long x, long long n) { if (n == 0) return 1; long long res = mod_pow(x * x % MOD, n / 2); if (n & 1) res = res * x % MOD; return res; } long long power(long long num, long long pw) { if (pw == 0) return 1; long long res = power(num * num, pw / 2); if (pw & 1) res *= pw; return res; } long long ln(long long num, long long base) { int steps = 0; while (num > 1) { num /= base; steps++; } return steps; } int S = 100001; vector<int> Seive(S); void seive() { fill(Seive.begin(), Seive.end(), 0); Seive[0] = 0; Seive[1] = 0; for (long long i = 2; i <= S; i++) { if (Seive[i]) continue; for (long long j = i * i; j <= S; j += i) Seive[j] = 1; } } long long gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm(int a, int b) { return (a / gcd(a, b)) * b; } bool sortbysec(const pair<long long, long long> &a, const pair<long long, long long> &b) { if (a.second == b.second) return a.first < b.first; return (a.second < b.second); } bool compare(const pair<string, int> &a, const pair<string, int> &b) { for (int i = 0; i < a.first.length(); i++) { if (i % 2 == 0) { if (a.first[i] < b.first[i]) return true; if (a.first[i] > b.first[i]) return false; } else { if (a.first[i] > b.first[i]) return true; if (a.first[i] < b.first[i]) return false; } } } int binarySearch(vector<pair<int, int>> a) { int r = a.size(); int l = 1; int ans = r; while (l <= r) { int mid = (l + r) / 2; int rem = mid, curr = 0; for (int i = 0; i < a.size(); i++) { if (a[i].first >= rem - 1) { if (a[i].second >= curr) { curr++; rem--; } } } if (rem <= 0) { ans = mid; l = mid + 1; } else { r = mid - 1; } } return ans; } void toggle(bool &x) { x ^= 1; } void solve() { int q; cin >> q; map<int, list<int>> m; int i = 0; while (q--) { int num; cin >> num; if (num == 1) { int x; cin >> x; m[x].push_back(i); i++; } else { int x, y; cin >> x >> y; if (x == y) continue; if (m[x].size() > m[y].size()) m[x].swap(m[y]); for (auto &i : m[x]) { m[y].push_back(i); } m.erase(x); } } vector<pair<int, int>> v; for (auto x : m) { for (auto y : x.second) { v.push_back({x.first, y}); } } sort(v.begin(), v.end(), sortbysec); for (auto x : v) { cout << x.first << ; } cout << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; for (int T = 1; T <= t; T++) { solve(); } }
|
#include <bits/stdc++.h> using namespace std; long long pow(long long a, long long b) { long long res = 1; while (b) { if (b & 1) { res = (res * a) % 1000000007; } b >>= 1; a = (a * a) % 1000000007; } return res; } int main() { long long n, k; cin >> n >> k; long long ans = (pow(k, k - 1) * pow(n - k, n - k)) % 1000000007; cout << ans << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n + 1]; int b[n + 1]; for (int i = 1; i <= n; i++) { cin >> a[i]; b[a[i]] = i; } for (int i = 1; i <= n; i++) { cout << b[i] << ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; void Yahia74() { ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); } const int N = 3e5 + 74, OO = 0x3f3f3f3f, MOD = (int)1e9 + 7; int main() { Yahia74(); long long n, d; cin >> n >> d; long long ans = n, c = 9, t = 10; while (c <= n) { long long st = 0, ed = 1e18 / t; while (st < ed) { long long m = st + (ed - st + 1) / 2; if (m * t + c <= n) st = m; else ed = m - 1; } if (st * t + c <= n && st * t + c >= n - d) ans = st * t + c; t *= 10; c = c * 10 + 9; } cout << ans << n ; return 0; }
|
#include <bits/stdc++.h> int main() { long long n, p; scanf( %I64d , &n); if (n % 2 == 0 && n != 0) p = n + 1; else p = (n + 1) / 2; printf( %I64d , p); return 0; }
|
#include <bits/stdc++.h> using namespace std; bool isopen(char ch) { if (ch == < || ch == { || ch == ( || ch == [ ) return true; return false; } int main() { int k, sum = 0, ans = 0; string s; cin >> k >> s; int c[10] = {0}; for (long long int i = 0; i < s.size(); i++) { sum = sum + s[i] - 0 ; int d = s[i] - 0 ; c[d]++; } for (int i = 0; i < 9; i++) for (int j = 0; j < c[i]; j++) { if (k > sum) { sum = sum + 9 - i; ans++; } } cout << ans; }
|
#include <bits/stdc++.h> using namespace std; void scan(int &x) { bool neg = false; register int c; x = 0; c = getchar(); if (c == - ) { neg = true; c = getchar(); } for (; (c > 47 && c < 58); c = getchar()) x = (x << 1) + (x << 3) + c - 48; if (neg) x *= -1; } int main() { string s; cin >> s; int k; cin >> k; int n = s.size(); if (k > n) cout << impossible ; else { bool ch[26] = {false}; int cnt = 0; for (int i = 0; i < n; i++) { if (!ch[s[i] - a ]) { cnt++; ch[s[i] - a ] = true; } } if (k - cnt >= 0) cout << k - cnt; else cout << 0 ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int dy[] = {-1, 1, 0, 0}; int dx[] = {0, 0, -1, 1}; int n, m; char mp[1005][1005]; int k; pair<int, int> p[30]; int row[1005][1005]; int col[1005][1005]; pair<int, int> d[100005]; int return_dir(char dir) { if (dir == N ) return 0; else if (dir == S ) return 1; else if (dir == W ) return 2; else return 3; } int main() { for (int i = 0; i < 30; i++) p[i].first = -1; char ch; scanf( %d %d , &n, &m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { ch = getchar(); while (ch == 32 || ch == n ) ch = getchar(); mp[i][j] = ch; if (mp[i][j] >= A && mp[i][j] <= Z ) p[mp[i][j] - A ] = make_pair(i, j); } } int c = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (mp[i][j] == # ) c++; else row[i][j] = c; } } c = 0; for (int j = 0; j < m; j++) { for (int i = 0; i < n; i++) { if (mp[i][j] == # ) c++; else col[i][j] = c; } } scanf( %d , &k); for (int i = 0; i < k; i++) { string s; cin >> s; int a; cin >> a; d[i].first = return_dir(s[0]); d[i].second = a; } bool was = false; for (int i = 0; i < 30; i++) { if (p[i].first == -1) continue; bool ok = true; int si = p[i].first; int sj = p[i].second; for (int j = 0; j < k; j++) { int ni = si + dy[d[j].first] * d[j].second; int nj = sj + dx[d[j].first] * d[j].second; if (ni >= n || nj >= m || ni < 0 || nj < 0) { ok = false; break; } if (d[j].first < 2) { if (col[ni][nj] != col[si][sj] || mp[ni][nj] == # ) ok = false; } else { if (row[ni][nj] != row[si][sj] || mp[ni][nj] == # ) ok = false; } si = ni; sj = nj; } if (ok) { was = true; putchar( A + i); } } if (!was) cout << no solution ; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { long long n, b; cin >> n >> b; long long a[n + 1]; for (int i = 0; i < n; ++i) { cin >> a[i]; } a[n] = b; sort(a, a + n + 1); if (a[0] == b) { cout << a[n - 1] - a[0]; } else { if (a[n] == b) { cout << a[n] - a[1]; } else { long long s1, s2, s3, s4, ans; s1 = (b - a[0]) + (a[n - 1] - a[0]); ans = s1; s2 = (a[n] - b) + (a[n] - a[1]); ans = min(ans, s2); s3 = (b - a[1]) + (a[n] - a[1]); ans = min(ans, s3); s4 = (a[n - 1] - b) + (a[n - 1] - a[0]); ans = min(ans, s4); cout << ans; } } return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; long long int n, a[100005], dis[100005] = {0}, vis[100050] = {0}, cnt = 0; vector<pair<long long int, long long int> > v[100005]; void dfs(long long int s, long long int f) { vis[s] = 1; long long int i; for (i = 0; i < v[s].size(); i++) { long long int x = v[s][i].first, y = v[s][i].second; if (!vis[x]) { if (f == 0) { dis[x] = max(y + dis[s], y); if (dis[x] > a[x]) { cnt++; dfs(x, 1); } else dfs(x, 0); } else { cnt++; dfs(x, f); } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); memset(dis, 0, sizeof(dis[0])); memset(vis, 0, sizeof(vis[0])); long long int i, j, x, y; cin >> n; for (i = 1; i <= n; i++) cin >> a[i]; for (i = 2; i <= n; i++) { cin >> x >> y; v[i].push_back({x, y}); v[x].push_back({i, y}); } for (i = 1; i <= n; i++) { if (!vis[i]) dfs(i, 0); } cout << cnt; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 100000; int p[N], u[N]; int main() { int n, k; cin >> n >> k; for (int i = 1; i <= k; i++) { int x; cin >> x; if (p[x] == 0) p[x] = i; u[x] = i; } long long rez = 0; for (int i = 1; i <= n; i++) { if (p[i] == 0) { if (i == 1 || i == n) rez += 2; else rez += 3; } else { if (i < n && (p[i] > u[i + 1])) rez++; if (i > 1 && (p[i] > u[i - 1])) rez++; } } cout << rez; return 0; }
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.