solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; long long int N = 1e9 + 7; int Nmax = 200004; int n; vector<int> bit(Nmax), a(Nmax), b(Nmax), c(Nmax); vector<int> p, q; void update(int idx, int val) { while (idx <= n) { bit[idx] += val; idx += (idx & -idx); } } int query(int idx) { int ans = 0; while (idx > 0) { ans += bit[idx]; idx -= (idx & -idx); } return ans; } int kth(int n1) { int l = 1, r = n, mid; while (l < r) { mid = (l + r) / 2; if (query(mid) < n1) { l = mid + 1; } else { r = mid; } } return l; } void solve() { cin >> n; p.resize(n); q.resize(n); for (int i = 0; i < n; i++) { cin >> p[i]; p[i]++; } for (int i = 0; i < n; i++) { cin >> q[i]; q[i]++; } reverse(p.begin(), p.end()); reverse(q.begin(), q.end()); for (int i = 0; i < n; i++) { a[i + 1] = query(p[i]); update(p[i], 1); } for (int i = 1; i <= n; i++) { bit[i] = 0; } for (int i = 0; i < n; i++) { b[i + 1] = query(q[i]); update(q[i], 1); } for (int i = 1; i <= n; i++) { c[i] += (a[i] + b[i]) % i; c[i + 1] += (a[i] + b[i]) / i; } for (int i = 1; i <= n; i++) { c[i + 1] += c[i] / i; c[i] = c[i] % i; } vector<int> res(n); fill(bit.begin(), bit.begin() + n + 2, 0); for (int i = 1; i <= n; i++) { update(i, 1); } for (int i = n; i > 0; i--) { res[i - 1] = kth(c[i] + 1); update(res[i - 1], -1); } reverse(res.begin(), res.end()); for (auto v : res) { cout << v - 1 << " "; } cout << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) { solve(); } }
12
#include <bits/stdc++.h> using namespace ::std; using ll = long long; using P = pair<ll, int>; const int maxn = 1e5 + 5; int n, m, c[maxn], w[maxn], vis[maxn]; ll ans; priority_queue<P, vector<P>, greater<P> > pq; void solve() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", &c[i]); for (int i = 0; i < n; i++) scanf("%d", &w[i]); int rm = m; for (int i = 0; i < n; i++) { int coin = c[i] % 100; ll x = (100 - coin) % 100 * w[i]; if (rm >= coin) { rm -= coin; if (coin != 0) pq.push(P(x, i)); } else { if (!pq.empty() && x > pq.top().first) { ans += pq.top().first; pq.pop(); rm += 100 - coin; pq.push(P(x, i)); } else { rm += (100 - coin) % 100; ans += x; } } } while (!pq.empty()) { vis[pq.top().second] = 1; pq.pop(); } printf("%lld\n", ans); for (int i = 0; i < n; i++) { if (vis[i]) { printf("%d %d\n", c[i] / 100, c[i] % 100); } else { printf("%d 0\n", c[i] / 100 + (int)(c[i] % 100 != 0)); } } } int main() { solve(); return 0; }
16
#include <bits/stdc++.h> using namespace std; const int mn = 2e5 + 1; const long long INF = 1e18; int p[mn], sz[mn]; long long ans = 0; int find_set(int x) { return x == p[x] ? x : p[x] = find_set(p[x]); } void merge(int a, int b, long long c) { a = find_set(a); b = find_set(b); if (a ^ b) { if (sz[a] < sz[b]) swap(a, b); sz[a] += sz[b]; p[b] = a; ans += c; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, x = 0, c; cin >> n >> m; long long a[n + 1], y = INF; for (int i = 1; i <= n; ++i) { cin >> a[i]; if (a[i] < y) { y = a[i]; x = i; } } for (int i = 1; i <= n; ++i) { p[i] = i; sz[i] = 1; } array<long long, 3> v[m + n - 1]; for (int i = 0; i < m; ++i) cin >> v[i][1] >> v[i][2] >> v[i][0]; c = m; for (int i = 1; i <= n; ++i) if (i ^ x) v[c++] = {a[i] + a[x], i, x}; sort(v, v + n + m - 1); for (int i = 0; i < n + m - 1; ++i) merge(v[i][1], v[i][2], v[i][0]); cout << ans; return 0; }
11
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int total = 0; for (int i = 0; i < n; i++) { string str; cin >> str; if (str == "Tetrahedron") total += 4; else if (str == "Cube") total += 6; else if (str == "Octahedron") total += 8; else if (str == "Dodecahedron") total += 12; else if (str == "Icosahedron") total += 20; } cout << total; }
0
#include <bits/stdc++.h> using namespace std; int A[500010]; int k, sol; int p; void upd(int i) { if (k) { if (k & 1) { for (int j = p; j < i; j++) A[j] = A[p - 1]; } else { int pol = (p + i) / 2; for (int j = p; j < pol; j++) A[j] = A[p - 1]; for (int j = pol; j < i; j++) A[j] = A[i]; } } sol = max(sol, k); k = 0; } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", A + i); } for (int i = 1; i < n - 1; i++) { if (A[i - 1] != A[i] && A[i + 1] != A[i]) { if (!k) p = i; k++; } else upd(i); } upd(n - 1); printf("%d\n", (sol + 1) / 2); for (int i = 0; i < n; i++) printf("%d%c", A[i], " \n"[i + 1 == n]); return 0; }
9
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; const long long MAX = 2e5 + 10; const long long inf = 1e18 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; while (t--) { long long n, m, cont = 0, sum = 0, mini = inf; cin >> n >> m; for (long long i = 0; i < n; i++) { for (long long i = 0; i < m; i++) { long long lec; cin >> lec; if (lec < 0) cont++; mini = min(mini, abs(lec)); sum += abs(lec); } } if (cont & 1) sum -= 2 * mini; cout << sum << "\n"; } return 0; }
2
#include <bits/stdc++.h> using namespace std; inline void readInt(int &x) { x = 0; int f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); x *= f; } inline void readLong(long long &x) { x = 0; int f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); x *= f; } int type, x, n, k, q, val[150010]; bool exist[150010]; priority_queue<int, vector<int>, greater<int> > Q; map<int, int> pos; int main() { readInt(n); readInt(k); readInt(q); for (int i = 1; i <= n; i++) { readInt(val[i]); pos[val[i]] = i; } while (q--) { readInt(type); readInt(x); if (type == 1) { if (Q.size() == k) { if (Q.top() < val[x]) { int now = Q.top(); Q.pop(); exist[pos[now]] = 0; exist[x] = 1; Q.push(val[x]); } else continue; } else { Q.push(val[x]); exist[x] = 1; } } else { if (exist[x]) puts("YES"); else puts("NO"); } } return 0; }
4
#include <cstdio> #include <cstring> using namespace std; const int N = 5e5 + 5; char s[3][N]; int cnt[3]; int n; bool flag[2][N]; void run(int a, int b, bool x) { // printf("%d %d %d\n", a, b, x); int c = 0; for (int i = 0; i < n + n; ++i) { if (c < n && s[a][i] - '0' == x) { flag[0][i] = 1; ++c; } else { flag[0][i] = 0; } // printf("%d ", flag[0][i]); } c = 0; for (int i = 0; i < n + n; ++i) { if (c < n && s[b][i] - '0' == x) { flag[1][i] = 1; ++c; } else { flag[1][i] = 0; } // printf("%d ", flag[1][i]); } int i = 0, j = 0; while (i < n + n || j < n + n) { while (i < n + n && !flag[0][i]) { printf("%d", s[a][i] - '0'); ++i; } while (j < n + n && !flag[1][j]) { printf("%d", s[b][j] - '0'); ++j; } if (j < n + n || i < n + n) printf("%d", x); ++i; ++j; } printf("\n"); } void solve() { memset(cnt, 0, sizeof cnt); scanf("%d", &n); int c = 0; for (int i = 0; i < 3; ++i) { scanf("%s", s[i]); for (int j = 0; j < n + n; ++j) { cnt[i] += s[i][j] - '0'; } if (cnt[i] >= n) { ++c; } } if (c >= 2) { if (cnt[0] >= n) { if (cnt[1] >= n) { run(0, 1, 1); } else { run(0, 2, 1); } } else { run(1, 2, 1); } } else { if (cnt[0] <= n) { if (cnt[1] <= n) { run(0, 1, 0); } else { run(0, 2, 0); } } else { run(1, 2, 0); } } } int main() { int t; scanf("%d", &t); while (t--) { solve(); } }
11
#include <bits/stdc++.h> using namespace std; char c; int a[1010][5], p, cnt, n, m; long long ans; int main() { scanf("%d%d", &n, &m); char aa = getchar(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { scanf("%c", &c); a[j][c - 'A']++; } char aa = getchar(); } for (int i = 1; i <= m; i++) { scanf("%d", &p); cnt = 0; for (int j = 0; j < 5; j++) { if (a[i][j] > cnt) cnt = a[i][j]; } ans += (long long)cnt * (long long)p; } printf("%lld\n", ans); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string s; cin >> s; int cnt[10]; for (int i = 0; i < 10; i++) cnt[i] = 0; for (char i : s) cnt[i - '0']++; int n = s.size(); if ((cnt[0] == n - 1 && cnt[1] == 1) or (cnt[0] == n - 2 && cnt[1] == 2 && s.back() == '1')) { for (int i = 0; i < n - 2; i++) printf("9"); cout << endl; continue; } int dod = 0; while (n < 10) { dod++; n += 2; s = string("00") + s; } int it2 = n - 1; while (s[it2] == '0') { s[it2] = '9'; it2--; } s[it2]--; for (int i = 0; i < 10; i++) cnt[i] = 0; for (int i = 0; i < n - 10; i++) cnt[s[i] - '0'] ^= 1; vector<int> lic; for (int i = 0; i < 10; i++) if (cnt[i]) lic.push_back(i); while (lic.size() < 10) lic.push_back(0); sort(lic.begin(), lic.end()); int syf = 0; for (int i = 0; i < 10 && syf == 0; i++) { if (lic[i] < s[n - 10 + i] - '0') break; if (lic[i] > s[n - 10 + i] - '0') syf = 1; } if (syf == 1) { int it = n - 10 - 1; while (s[it] == '0') { s[it] = '9'; it--; } s[it]--; for (int i = 0; i < 10; i++) cnt[i] = 0; for (int i = 0; i < n - 10; i++) cnt[s[i] - '0'] ^= 1; lic.clear(); for (int i = 0; i < 10; i++) if (cnt[i]) lic.push_back(i); while (lic.size() < 10) lic.push_back(9); sort(lic.begin(), lic.end()); for (int i = 0; i < 10; i++) s[n - 1 - i] = lic[i] + '0'; for (int i = dod * 2; i < n; i++) printf("%c", s[i]); cout << endl; } else { int od = -1, it = 0; for (int i = n - 10; i < n && od == -1; i++) { cnt[s[i] - '0'] ^= 1; lic.clear(); for (int j = 0; j < 10; j++) if (cnt[j]) lic.push_back(j + '0'); if (lic.size() > n - 1 - i) { od = 1; it = i; } else { while (lic.size() < n - 1 - i) lic.push_back('0'); sort(lic.begin(), lic.end()); syf = 0; for (int j = n - lic.size(); j < n && syf == 0; j++) { if (lic[j - (n - lic.size())] < s[j]) break; if (lic[j - (n - lic.size())] > s[j]) syf = 1; } if (syf == 1) { od = 1; it = i; } } } if (od == -1) { for (int i = dod * 2; i < n; i++) printf("%c", s[i]); cout << endl; } else { int git = 0; while (git == 0) { cnt[s[it] - '0'] ^= 1; s[it]--; cnt[s[it] - '0'] ^= 1; lic.clear(); for (int j = 0; j < 10; j++) if (cnt[j]) lic.push_back(j + '0'); if (lic.size() > n - 1 - it) { continue; } else { while (lic.size() < n - 1 - it) lic.push_back('9'); git = 1; sort(lic.begin(), lic.end()); for (int i = 0; i < lic.size(); i++) s[n - 1 - i] = lic[i]; for (int i = dod * 2; i < n; i++) printf("%c", s[i]); cout << endl; } } } } } }
14
#include <bits/stdc++.h> using namespace std; template <class T> ostream& operator<<(ostream& cout, vector<T> V) { cout << "[ "; for (auto v : V) cout << v << ' '; return cout << ']'; } template <class L, class R> ostream& operator<<(ostream& cout, pair<L, R> P) { return cout << '(' << P.first << ',' << P.second << ')'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t; cin >> t; while (t--) { long long int n; cin >> n; vector<long long int> a(n); for (long long int i = 0; i < (n); i++) cin >> a[i]; sort(a.begin(), a.end()); long long int ans = 0; long long int i = 0; while (i < n) { long long int j = i + 1; long long int ma = a[i]; while (j < n && ma > j - i) { ma = a[j]; j++; } if (ma <= j - i) { ans++; i = j; } else { break; } } cout << ans << '\n'; } cerr << "Time : " << 1000 * (long double)clock() / (long double)CLOCKS_PER_SEC << "ms\n"; ; }
4
#include <bits/stdc++.h> using namespace std; int n, a[100005], pos[100005], nxt[100005]; int root[100005]; int lson[5000005], rson[5000005]; int w[5000005]; int num; int build(int l, int r) { ++num; w[num] = 0; int ret = num; if (l != r) { int mid = (l + r) / 2; lson[ret] = build(l, mid); rson[ret] = build(mid + 1, r); } return ret; } int update(int last, int l, int r, int pos, int ww) { ++num; int ret = num, dq = num; w[dq] = w[last] + ww; while (l != r) { int mid = (l + r) / 2; if (pos <= mid) { rson[dq] = rson[last]; ++num; lson[dq] = num; w[num] = w[lson[last]] + ww; dq = lson[dq]; last = lson[last]; r = mid; } else { lson[dq] = lson[last]; ++num; rson[dq] = num; w[num] = w[rson[last]] + ww; dq = rson[dq]; last = rson[last]; l = mid + 1; } } return ret; } int find1(int root, int k) { int dq = root; int l = 1, r = n + 1; while (l < r) { int mid = (l + r) / 2; if (w[lson[dq]] > k) { dq = lson[dq]; r = mid; } else { k -= w[lson[dq]]; dq = rson[dq]; l = mid + 1; } } return l; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1; i <= n; ++i) pos[i] = -1; for (int i = n; i >= 1; --i) { nxt[i] = pos[a[i]]; pos[a[i]] = i; } root[n + 1] = build(1, n + 1); for (int i = n; i >= 1; --i) { if (nxt[i] == -1) root[i] = update(root[i + 1], 1, n + 1, i, 1); else { int dq = update(root[i + 1], 1, n + 1, nxt[i], -1); root[i] = update(dq, 1, n + 1, i, 1); } } for (int k = 1; k <= n; ++k) { int ans = 0; int dq = 1; while (dq <= n) { ans++; dq = find1(root[dq], k); } printf("%d ", ans); } return 0; }
16
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long int a, b, i, cnt = 0; cin >> a >> b; for (i = 2; i * i <= (a + b); i++) { if ((a + b) % i == 0) cnt++; } if ((a - b) == 1) { if (cnt == 0) { cout << "YES" << endl; continue; } else { cout << "NO" << endl; continue; } } cout << "NO" << endl; } }
3
#include <bits/stdc++.h> using namespace std; template <class T> inline void rd(T &x) { x = 0; char c = getchar(); int f = 1; while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) x = x * 10 - '0' + c, c = getchar(); x *= f; } const int N = 4010; const int mod = 1e9 + 7; inline void Add(int &x, int y) { x += y; if (x >= mod) x -= mod; } int num[N], len; int P, K; namespace I_love_Kakyoin { char str[N]; int S[N]; int n; void main() { rd(P), rd(K); scanf("%s", str); n = strlen(str); for (int i = 0; i < n; ++i) S[i] = str[n - 1 - i] - '0'; while (n) { long long cur = 0; for (int i = n - 1; i >= 0; --i) cur = (cur * 10 + S[i]) % P; num[len++] = cur; cur = 0; for (int i = n - 1; i >= 0; --i) { cur = cur * 10 + S[i]; S[i] = cur / P, cur %= P; } while (n && S[n - 1] == 0) n--; } } } // namespace I_love_Kakyoin inline int Sum(long long l, long long r) { return 1ll * (l + r) * (r - l + 1) / 2 % mod; } inline int calc(long long l, long long r) { l = max(l, 0ll); if (l > r) return 0; long long tot = 0; if (r >= P - 1) tot += (2 * P - 1) * (r - max(l, P - 1ll) + 1) - Sum(max(l, P - 1ll), r), r = P - 2; if (l <= r) tot += Sum(l + 1, r + 1); return (tot % mod + mod) % mod; } int f[N][N][2][2]; int dp(int p, int tot, int s, int t) { if (p == -1) return !s && tot >= K; if (~f[p][tot][s][t]) return f[p][tot][s][t]; int res = 0; if (t) if (!s) { Add(res, 1ll * calc(0, P - 2) * dp(p - 1, tot + 1, 1, 1) % mod); Add(res, 1ll * calc(0, P - 1) * dp(p - 1, tot, 0, 1) % mod); } else { Add(res, 1ll * calc(P - 1, 2 * P - 2) * dp(p - 1, tot + 1, 1, 1) % mod); Add(res, 1ll * calc(P, 2 * P - 1) * dp(p - 1, tot, 0, 1) % mod); } else if (!s) { Add(res, 1ll * calc(0, num[p] - 1) * dp(p - 1, tot, 0, 1) % mod); Add(res, 1ll * calc(0, num[p] - 2) * dp(p - 1, tot + 1, 1, 1) % mod); Add(res, 1ll * calc(num[p], num[p]) * dp(p - 1, tot, 0, 0) % mod); Add(res, 1ll * calc(num[p] - 1, num[p] - 1) * dp(p - 1, tot + 1, 1, 0) % mod); } else { Add(res, 1ll * calc(P, P + num[p] - 1) * dp(p - 1, tot, 0, 1) % mod); Add(res, 1ll * calc(P - 1, P + num[p] - 2) * dp(p - 1, tot + 1, 1, 1) % mod); Add(res, 1ll * calc(P + num[p], P + num[p]) * dp(p - 1, tot, 0, 0) % mod); Add(res, 1ll * calc(P + num[p] - 1, P + num[p] - 1) * dp(p - 1, tot + 1, 1, 0) % mod); } return f[p][tot][s][t] = res; } int main() { I_love_Kakyoin::main(); if (K > len) return printf("0"), 0; memset(f, -1, sizeof(f)); printf("%d", dp(len - 1, 0, 0, 0)); return 0; }
25
#include <bits/stdc++.h> int N; int A[(100005)]; int B[(100005)]; int main() { int i; scanf("%d", &N); for (i = 0; i < N; ++i) scanf("%d", A + i); if (N == 1) { printf("+\n"); return 0; } else if (N == 2) { printf("-+\n"); return 0; } int s = A[N - 1] - A[N - 2]; B[N - 2] = 1; B[N - 1] = 1; for (i = N - 3; i >= 0; --i) { if (s >= A[i]) { s -= A[i]; ++B[i]; ++B[i + 1]; } else { s = A[i] - s; ++B[i + 1]; } } int a = 0; for (i = 0; i < N; ++i) { a += B[i]; if (a & 1) putchar('-'); else putchar('+'); } putchar('\n'); return 0; }
11
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, temp = 0; cin >> n; int a[n + 5]; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n - 1; j++) { if (a[i] == a[j + 1]) { temp = 1; break; } } } if (temp == 0) { cout << "NO\n"; } else { cout << "YES\n"; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); if (n % 2 == 0) cout << a[n / 2 - 1]; else cout << a[n / 2]; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); int k = 0; for (int i = 1; i <= n; i++) { int t; scanf("%d", &t); t -= k; if (t < 0) return printf("NO"), 0; k = t % 2; } if (k != 0) printf("NO"); else printf("YES"); }
3
#include <bits/stdc++.h> using namespace std; long long mark[100010]; int main() { long long n, m, k; cin >> n >> m >> k; for (int i = 0; i < m; i++) cin >> mark[i]; long long special = 0, page = 0, z = 0, cnt = 0; while (special < m) { for (int i = special; i < m; i++) { if (mark[i] - k * page - special > k) break; z++; } if (z == 0) { page += (mark[special] - k * page - special - 1) / k; continue; } special += z; z = 0; cnt++; } cout << cnt; }
6
#include <bits/stdc++.h> using namespace std; const int INF = (int)1E9; const long long LINF = (long long)1E18; const long double PI = acos(-1.0); const long double EPS = 1E-12; template <typename T> T gcd(T a, T b) { return (b == 0) ? abs(a) : gcd(b, a % b); } template <typename T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; } template <typename T> inline T mod(T a, T b) { a %= b; if (a < 0) a += b; return a; } template <typename T> inline T sqr(T x) { return x * x; } template <typename T> inline T gmax(T a, T b) { return (a > b ? a : b); } template <typename T> inline T gmin(T a, T b) { return (a < b ? a : b); } template <typename T> inline string toString(T x) { ostringstream oss; oss << x; return oss.str(); } inline long long toInt(const string& st) { istringstream iss(st); long long x; iss >> x; return x; } inline long double toDouble(const string& st) { istringstream iss(st); long double x; iss >> x; return x; } inline string toLower(string st) { for (int i = 0; i < (((int)(st).size())); i++) st[i] = tolower(st[i]); return st; } inline string toUpper(string st) { for (int i = 0; i < (((int)(st).size())); i++) st[i] = toupper(st[i]); return st; } inline long long mul(long long b, long long e, long long n) { long long ret; while (e) { if (e & 1) ret = (ret + b) % n; e >>= 1; b = (b + b) % n; } return ret; } inline long long pow(long long b, long long e, long long n) { long long ret = 1; while (e) { if (e & 1) ret = mul(ret, b, n); e >>= 1; b = mul(b, b, n); } return ret; } int na, ma, nb, mb; char a[500][500], b[500][500]; int main() { memset((a), ('0'), sizeof(a)); memset((b), ('0'), sizeof(b)); cin >> na >> ma; for (int i = (1); i <= (na); i++) for (int j = (1); j <= (ma); j++) cin >> a[i][j]; cin >> nb >> mb; for (int i = (1); i <= (nb); i++) for (int j = (1); j <= (mb); j++) cin >> b[i][j]; long long res = 0, cur; long long resx, resy; for (int x = (-50); x <= (50); x++) for (int y = (-50); y <= (50); y++) { cur = 0; for (int i = (1); i <= (na); i++) for (int j = (1); j <= (ma); j++) if (i + x > 0 && i + x <= nb && j + y > 0 && j + y <= mb) cur += (a[i][j] - '0') * (b[i + x][j + y] - '0'); if (cur > res) { res = cur; resx = x; resy = y; } } if (res == 0) { cout << "-1 -1" << endl; return 0; } cout << resx << " " << resy << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; int setBit(int n, int pos) { return n = n | (1 << pos); } int resetBit(int n, int pos) { return n = n & ~(1 << pos); } bool checkBit(long long n, long long pos) { return (bool)(n & (1LL << pos)); } const int MOD = 1000003; const int MAX = 1000010; int inv[MAX], fact[MAX]; long long bm(long long a, long long b, long long m) { if (b == 0) return 1; long long ret = bm(a, b / 2, m); ret *= ret; ret %= MOD; if (b % 2 == 1) ret *= a; return (ret % m); } void precal(int N) { fact[0] = 1; for (int i = 1; i <= N; i++) fact[i] = ((long long)fact[i - 1] * i) % MOD; inv[N] = bm(fact[N], MOD - 2, MOD); for (int i = N - 1; i >= 0; i--) inv[i] = ((long long)inv[i + 1] * (i + 1)) % MOD; } int bin(int n, int r) { if (n < r) return 0; long long ret = fact[n]; ret *= inv[r], ret %= MOD; ret *= inv[n - r], ret %= MOD; return ret; } int main() { precal(1e6); int n, k; scanf("%d %d", &n, &k); long long ans = 0; for (int i = 0; i <= n; i++) { ans += bin(k + i - 1, k - 1); } cout << (ans - 1 + MOD) % MOD << endl; return 0; }
10
#include <bits/stdc++.h> using namespace std; const int N = 100005, M = 255, S = 26; int n, q, a, b, c; char ch[N], sa[M], sb[M], sc[M]; int nxt[N][S]; int f[M][M][M]; int main() { scanf("%d%d", &n, &q); scanf("%s", ch + 1); for (int i = 0; i < S; ++i) { nxt[n + 1][i] = n + 1; nxt[n + 2][i] = n + 1; } for (int i = n; i; --i) { for (int j = 0; j < S; ++j) nxt[i][j] = nxt[i + 1][j]; nxt[i][ch[i] - 97] = i; } a = b = c = 0; while (q--) { char s[2]; scanf("%s", s); int x; scanf("%d", &x); if (s[0] == '+') { scanf("%s", s); if (x == 1) { sa[++a] = s[0]; for (int i = 0; i <= b; ++i) for (int j = 0; j <= c; ++j) { f[a][i][j] = n + 1; if (i) f[a][i][j] = min(f[a][i][j], nxt[f[a][i - 1][j] + 1][sb[i] - 97]); if (j) f[a][i][j] = min(f[a][i][j], nxt[f[a][i][j - 1] + 1][sc[j] - 97]); f[a][i][j] = min(f[a][i][j], nxt[f[a - 1][i][j] + 1][sa[a] - 97]); } } else if (x == 2) { sb[++b] = s[0]; for (int i = 0; i <= a; ++i) for (int j = 0; j <= c; ++j) { f[i][b][j] = n + 1; if (i) f[i][b][j] = min(f[i][b][j], nxt[f[i - 1][b][j] + 1][sa[i] - 97]); if (j) f[i][b][j] = min(f[i][b][j], nxt[f[i][b][j - 1] + 1][sc[j] - 97]); f[i][b][j] = min(f[i][b][j], nxt[f[i][b - 1][j] + 1][sb[b] - 97]); } } else if (x == 3) { sc[++c] = s[0]; for (int i = 0; i <= a; ++i) for (int j = 0; j <= b; ++j) { f[i][j][c] = n + 1; if (i) f[i][j][c] = min(f[i][j][c], nxt[f[i - 1][j][c] + 1][sa[i] - 97]); if (j) f[i][j][c] = min(f[i][j][c], nxt[f[i][j - 1][c] + 1][sb[j] - 97]); f[i][j][c] = min(f[i][j][c], nxt[f[i][j][c - 1] + 1][sc[c] - 97]); } } } else { if (x == 1) --a; else if (x == 2) --b; else if (x == 3) --c; } puts(f[a][b][c] <= n ? "YES" : "NO"); } return 0; }
14
#include <bits/stdc++.h> using namespace std; const int BufSize = 1 << 21; struct FastIO { char ibuf[BufSize + 10], *p, *e; char obuf[BufSize + 10], *q, stk[128]; FastIO() { q = obuf; } ~FastIO() { fwrite(obuf, 1, q - obuf, stdout); } int getChar() { if (p == e) p = ibuf, e = ibuf + fread(ibuf, 1, BufSize, stdin); return p == e ? EOF : *p++; } void putChar(int c) { if (q - obuf == BufSize) fwrite(obuf, 1, BufSize, stdout), q = obuf; *q++ = c; } FastIO& operator>>(char& c) { do { c = getChar(); } while (isspace(c)); return *this; } FastIO& operator>>(char* s) { *s = ' '; while (isspace(*s)) *s = getChar(); while (!isspace(*s)) *++s = getChar(); *s = 0; return *this; } template <typename T> FastIO& operator>>(T& x) { char c, l; for (c = 0; !isdigit(c); c = getChar()) l = c; for (x = 0; isdigit(c); c = getChar()) x = x * 10 - '0' + c; if (l == '-') x = -x; return *this; } FastIO& operator<<(char c) { putChar(c); return *this; } FastIO& operator<<(char* s) { while (*s) putChar(*s++); return *this; } FastIO& operator<<(const char* s) { while (*s) putChar(*s++); return *this; } template <typename T> FastIO& operator<<(T x) { int tp = 0; if (x < 0) x = -x, putChar('-'); do { stk[++tp] = x % 10, x /= 10; } while (x); while (tp) putChar(stk[tp--] + '0'); return *this; } } IO; const int MX = 505, MY = 660000; bitset<MX> A[MX][MX], B[MX][MX]; int N, M, Qu; char S[MX][MX]; int An[MY]; struct Query { int xi, yi, xj, yj, id; } Q[MY], tmpl[MY], tmpr[MY]; void Divide(int L, int R, int l, int r) { if (L > R || l > r) return; int m = (l + r) >> 1, lc = 0, rc = 0; for (int j = (M); j >= (1); --j) { if (S[m][j] == '.') A[m][j] = A[m][j + 1], A[m][j][j] = 1; else A[m][j].reset(); } for (int i = (m - 1); i >= (l); --i) for (int j = (M); j >= (1); --j) { if (S[i][j] == '.') A[i][j] = A[i + 1][j] | A[i][j + 1]; else A[i][j].reset(); } for (int j = (1); j <= (M); ++j) { if (S[m][j] == '.') B[m][j] = B[m][j - 1], B[m][j][j] = 1; else B[m][j].reset(); } for (int i = (m + 1); i <= (r); ++i) for (int j = (1); j <= (M); ++j) { if (S[i][j] == '.') B[i][j] = B[i - 1][j] | B[i][j - 1]; else B[i][j].reset(); } for (int i = (L); i <= (R); ++i) { int xi = Q[i].xi, yi = Q[i].yi, xj = Q[i].xj, yj = Q[i].yj; if (xi < m && xj < m) { tmpl[++lc] = Q[i]; continue; } if (xi > m && xj > m) { tmpr[++rc] = Q[i]; continue; } An[Q[i].id] = (A[xi][yi] & B[xj][yj]).any(); } for (int i = (1); i <= (lc); ++i) Q[L + i - 1] = tmpl[i]; for (int i = (1); i <= (rc); ++i) Q[L + lc + i - 1] = tmpr[i]; Divide(L, L + lc - 1, l, m - 1); Divide(L + lc, L + lc + rc - 1, m + 1, r); } int main() { IO >> N >> M; for (int i = (1); i <= (N); ++i) IO >> (S[i] + 1); IO >> Qu; for (int i = (1); i <= (Qu); ++i) IO >> Q[i].xi >> Q[i].yi >> Q[i].xj >> Q[i].yj, Q[i].id = i; Divide(1, Qu, 1, N); for (int i = (1); i <= (Qu); ++i) IO << (An[i] ? "Yes" : "No") << '\n'; return 0; }
22
#include <bits/stdc++.h> using namespace std; template <class A, class B> A cvt(B x) { stringstream ss; ss << x; A y; ss >> y; return y; } int n, m; vector<int> gr[100000]; int c[100000], d[100000], v[100000], cycn; vector<int> cyc; int n2cyc[100000]; void findCycle() { queue<int> q; for (int i = 0; i < (int)(n); i++) { c[i] = 1; d[i] = gr[i].size(); if (d[i] == 1) q.push(i); } while (!q.empty()) { int x = q.front(); q.pop(); c[x] = 0; for (int y : gr[x]) { if (--d[y] == 1) q.push(y); } } int x = -1; for (int i = 0; i < (int)(n); i++) if (c[i]) x = i; int stop = 0; while (!stop) { v[x] = 1; cyc.push_back(x); n2cyc[x] = cyc.size() - 1; stop = 1; for (int y : gr[x]) if (c[y] && !v[y]) { x = y; stop = 0; break; } } cycn = cyc.size(); } vector<int> ch[100000]; int p[100000], s[100000]; void dfs(int x, int px) { s[x] = 1; for (int y : gr[x]) if (y != px && !c[y]) { ch[x].push_back(y); dfs(y, x); s[x] += s[y]; } } struct node { int o, f; }; vector<node> cycTree; vector<vector<node> > chains; vector<node> dummy; int n2c[100000], n2i[100000], n2r[100000], n2h[100000], cs[100000]; int ro[100000]; void hl(int x, int c, int cn, int r, int h) { n2c[x] = c; n2i[x] = cn - 1; n2r[x] = r; n2h[x] = h; cs[c] = cn; if (s[x] == 1) { chains[c] = vector<node>(4 * cn); } else { int j = 0; for (int i = 0; i < (int)(ch[x].size()); i++) { if (s[ch[x][i]] > s[ch[x][j]]) j = i; } for (int i = 0; i < (int)(ch[x].size()); i++) { int y = ch[x][i]; if (i == j) { hl(y, c, cn + 1, r, h); } else { chains.push_back(dummy); hl(y, chains.size() - 1, 1, r, x); } } } } void update(vector<node> &t, int x, int l, int r, int ul, int ur) { if (r < ul || ur < l) return; else if (ul <= l && r <= ur) { t[x].o = (r - l + 1) - t[x].o; t[x].f ^= 1; } else { int m = (l + r) / 2; update(t, 2 * x, l, m, ul, ur); update(t, 2 * x + 1, m + 1, r, ul, ur); t[x].o = t[2 * x].o + t[2 * x + 1].o; if (t[x].f) t[x].o = (r - l + 1) - t[x].o; } } void flip(int x) { int rx = n2r[x]; if (rx == x) return; int c = n2c[x], i = n2i[x]; ro[rx] -= chains[c][1].o; update(chains[c], 1, 0, cs[c] - 1, 0, i); ro[rx] += chains[c][1].o; flip(n2h[x]); } int main() { scanf("%d %d", &n, &m); int a, b; for (int i = 0; i < (int)(n); i++) { scanf("%d %d", &a, &b); a--; b--; gr[a].push_back(b); gr[b].push_back(a); } findCycle(); cycTree = vector<node>(cycn * 4); for (int x : cyc) { dfs(x, x); chains.push_back(dummy); hl(x, chains.size() - 1, 0, x, x); } int et = 0, ec = 0; for (int i = 0; i < (int)(m); i++) { scanf("%d %d", &a, &b); a--; b--; int ra = n2r[a], rb = n2r[b]; et -= ro[ra]; flip(a); et += ro[ra]; et -= ro[rb]; flip(b); et += ro[rb]; if (ra != rb) { int ia = n2cyc[ra], ib = n2cyc[rb]; pair<int, int> dp = {(ia < ib) ? ib - ia : cycn - ia + ib, cyc[(ia + 1) % cycn]}; pair<int, int> dn = {(ib < ia) ? ia - ib : ia + cycn - ib, cyc[(ia - 1 + cycn) % cycn]}; ec -= cycTree[1].o; if (dn < dp) swap(ia, ib); if (ia < ib) { update(cycTree, 1, 0, cycn - 1, ia, ib - 1); } else { update(cycTree, 1, 0, cycn - 1, ia, cycn); update(cycTree, 1, 0, cycn - 1, 0, ib - 1); } ec += cycTree[1].o; } printf("%d\n", n - et - ec + (ec == cycn)); } return 0; }
21
#include <bits/stdc++.h> using namespace std; int const N = 5e5 + 10, oo = 1e9; int mod = oo + 7; long long const OO = 1e18; int n, m, k, w = 1, vis[N]; vector<int> adj[N], lv[N]; map<int, int> mp[N]; queue<int> q; string an; void BFS() { q.push(1); vis[1] = 1; int d = 2; while (!q.empty()) { int sz = q.size(); while (sz--) { int cur = q.front(); q.pop(); if (vis[cur] != 1) { w *= lv[cur].size(); w = min(w, k); } for (auto v : adj[cur]) { if (vis[v]) { if (vis[v] == d) lv[v].push_back(mp[cur][v]); continue; } lv[v].push_back(mp[cur][v]); q.push(v); vis[v] = d; } } d++; } } void rec(int u) { if (u > n) { cout << an << endl; w--; if (w == 0) exit(0); return; } for (auto e : lv[u]) { an[e] = '1'; rec(u + 1); an[e] = '0'; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); mp[a][b] = i; mp[b][a] = i; } BFS(); cout << w << endl; for (int i = 0; i < m; i++) an += '0'; rec(2); return 0; }
13
#include <bits/stdc++.h> const double eps = 0.0000001; using namespace std; inline int sgn(double x) { return (x > eps) - (x < -eps); } inline char gc() { static char buf[1 << 16], *S, *T; if (S == T) { T = (S = buf) + fread(buf, 1, 1 << 16, stdin); if (S == T) return EOF; } return *S++; } inline int read() { register int x = 0, f = 1; register char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); return x * f; } long long f[(long long)1e5]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n = 3; f[1] = 1; f[2] = 1; long long s, k; cin >> s >> k; for (; f[n - 1] < s; ++n) { f[n] = 2 * f[n - 1] - (n - k - 1 < 0 ? 0 : f[n - k - 1]); } vector<int> v; n; while (s) { if (s >= f[n]) { s = s - f[n]; v.push_back(f[n]); } n--; } if (v.size() < 2) v.push_back(0); cout << v.size() << endl; for (int i = 0; i < v.size(); ++i) cout << v[i] << " "; }
8
#include <bits/stdc++.h> using namespace std; int get_cost(string s) { int len = s.length(); int ans = 0; for (int i = 0; i < len; i++) { string a = s.substr(0, i); string b = s.substr(i, len - i); string s1 = b + a; string s2 = s1; reverse(s2.begin(), s2.end()); if (s1 == s2) ans++; } return ans; } string rev(string s) { reverse(s.begin(), s.end()); return s; } int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int main() { int n; scanf("%d", &n); vector<int> cnt(n); for (int i = 0; i < n; i++) scanf("%d", &cnt[i]); int len = 0; for (int x : cnt) len += x; if (n == 1) { printf("%d\n", len); printf("%s\n", string(len, 'a').c_str()); return 0; } int odd_cnt = 0; for (int x : cnt) if (x % 2 == 1) odd_cnt++; if (odd_cnt > 1) { printf("0\n"); string s; for (int i = 0; i < n; i++) s += string(cnt[i], (char)('a' + i)); printf("%s\n", s.c_str()); return 0; } int g = cnt[0]; for (int i = 1; i < n; i++) g = gcd(g, cnt[i]); int gr = g / 2; if (odd_cnt == 1) { int who_odd = -1; int who_even = -1; for (int i = 0; i < n; i++) { if (cnt[i] % 2 == 1) who_odd = i; if (cnt[i] % 2 == 0) who_even = i; } string a = string(cnt[who_odd] / g, (char)('a' + who_odd)); string b = string(cnt[who_even] / g, (char)('a' + who_even)); string other; for (int i = 0; i < n; i++) { if (i != who_odd && i != who_even) other += string(cnt[i] / g / 2, (char)('a' + i)); } a = other + a + rev(other); string s; for (int i = 0; i < g; i++) { s += a + b; } printf("%d\n", g); printf("%s\n", s.c_str()); return 0; } string a = string(cnt[0] / gr, 'a'); string b = string(cnt[1] / gr, 'b'); string other; for (int i = 2; i < n; i++) other += string(cnt[i] / g, (char)('a' + i)); string rev_other = rev(other); string s; for (int i = 0; i < gr; i++) { s += a + other + b + rev_other; } printf("%d\n", g); printf("%s\n", s.c_str()); return 0; }
17
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> bool compare(T x, U y) { return (abs(x - y) <= 1e-9); } const int MOD = 1e9 + 7; void solve() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; if (x2 == x1 || y2 == y1) { if (x2 == x1) cout << abs(y2 - y1); else cout << abs(x2 - x1); } else { cout << 2 + abs(x2 - x1) + abs(y2 - y1); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { solve(); cout << "\n"; } }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; int res = 0; char ls; for (int i = 0; i < n; i++) { cin >> s; if (i == 0) { ls = s[1]; res++; continue; } if (ls == s[0]) { ls = s[1]; res++; continue; } } cout << res << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long md = 1e9 + 7; long long n, m, s[5050], f[5050], h[5050], l[5050], r[5050]; vector<long long> v[5050]; pair<long long, long long> sl(long long x) { if (l[x] == n + 1) { return make_pair(0, 0); } long long mx, cnt = 1; if (x == 0) { mx = 0; } else { mx = 1; } long long nw = l[x]; for (long long i = 1; i <= n; i++) { if (v[i].size()) { if (i == f[x]) { long long cn = 0; for (long long j = 0; j < v[i].size(); j++) { long long xx = v[i][j]; if (xx != x && r[xx] > nw) { cn++; } } if (cn) { mx++; cnt = cnt * cn % md; cnt %= md; } } else { long long ct1 = 0, ct2 = 0; for (long long j = 0; j < v[i].size(); j++) { long long xx = v[i][j]; if (l[xx] < nw) { ct1++; } if (r[xx] > nw) { ct2++; } } if (ct1 == 0 && ct2 == 0) { continue; } if (ct1 != 0 && ct2 != 0 && ct1 + ct2 >= 3) { mx += 2; cnt = cnt * min(ct1, ct2) % md * (max(ct1, ct2) - 1) % md; cnt %= md; } else { mx++; cnt = cnt * (ct1 + ct2) % md; cnt %= md; } } } } return make_pair(mx, cnt); } signed main() { cin >> n >> m; for (long long i = 1; i <= n; i++) { cin >> s[i]; } for (long long i = 1; i <= m; i++) { cin >> f[i] >> h[i]; v[f[i]].push_back(i); long long nw = h[i]; for (l[i] = 1; l[i] <= n; l[i]++) { if (s[l[i]] == f[i]) { nw--; } if (nw == 0) { break; } } nw = h[i]; for (r[i] = n; r[i] >= 1; r[i]--) { if (s[r[i]] == f[i]) { nw--; } if (nw == 0) { break; } } } bool ok = true; for (long long i = 1; i <= m; i++) { if (r[i]) { ok = false; break; } } if (ok == true) { cout << "0 1" << endl; return 0; } long long mx = 0, cnt = 1; for (long long i = 0; i <= m; i++) { pair<long long, long long> p = sl(i); if (p.first == mx) { cnt += p.second; cnt %= md; } if (p.first > mx) { mx = p.first; cnt = p.second; } } cout << mx << ' ' << cnt << endl; return 0; }
17
#include <bits/stdc++.h> using namespace std; vector<int> g[101010]; vector<int> sta; vector<int> cyc; int u[101010]; int cp[101010]; int dfs(int x, int e) { if (u[x]) { while (1) { cyc.push_back(sta.back()); sta.pop_back(); if (cyc.back() == x) break; } return 1; } else { u[x] = 1; sta.push_back(x); for (int nx : g[x]) { if (nx != e) { if (dfs(nx, x)) return 1; } } sta.pop_back(); return 0; } } int dist(int a, int b, int k) { return min(max(a, b) - min(a, b), k - max(a, b) + min(a, b)); } const int N = 1 << 17; int ces = 0; int tes = 0; int st[2 * N][3]; int togglest(int i, int stl, int str, int l, int r) { if (stl > r || str < l) return 0; if (st[i][2]) { if (i * 2 < 2 * N) { swap(st[i * 2][0], st[i * 2][1]); st[i * 2][2] ^= 1; } if (i * 2 + 1 < 2 * N) { swap(st[i * 2 + 1][0], st[i * 2 + 1][1]); st[i * 2 + 1][2] ^= 1; } st[i][2] = 0; } if (l <= stl && str <= r) { swap(st[i][0], st[i][1]); st[i][2] = 1; return st[i][1] - st[i][0]; } else { int m = (stl + str) / 2; int re = 0; re += togglest(i * 2, stl, m, l, r); re += togglest(i * 2 + 1, m + 1, str, l, r); st[i][0] = st[i * 2][0] + st[i * 2 + 1][0]; st[i][1] = st[i * 2][1] + st[i * 2 + 1][1]; return re; } } void init(int i, int stl, int str) { if (i >= 2 * N) return; st[i][0] = str - stl + 1; int m = (stl + str) / 2; init(i * 2, stl, m); init(i * 2 + 1, m + 1, str); } void togglecpath2(int a, int b) { if (a > b) { togglecpath2(a, (int)cyc.size()); togglecpath2(0, b); return; } ces += togglest(1, 0, N - 1, a, b - 1); } void togglecpath1(int a, int b) { if (a == b) return; a = cp[a]; b = cp[b]; int d1 = dist((a + 1) % (int)cyc.size(), b, cyc.size()); int d2 = dist((a - 1 + (int)cyc.size()) % (int)cyc.size(), b, cyc.size()); if (d1 < d2) { togglecpath2(a, b); } else if (d2 < d1) { togglecpath2(b, a); } else { if (cyc[(a + 1) % (int)cyc.size()] < cyc[(a - 1 + (int)cyc.size()) % (int)cyc.size()]) { togglecpath2(a, b); } else { togglecpath2(b, a); } } } struct Node { Node *c[2], *p; int id, rev; int lt, c0, c1, tg; int isr() { return !p || (p->c[0] != this && p->c[1] != this); } int dir() { return p->c[1] == this; } void setc(Node* s, int d) { c[d] = s; if (s) s->p = this; } void push() { if (rev) { swap(c[0], c[1]); if (c[0]) c[0]->rev ^= 1; if (c[1]) c[1]->rev ^= 1; rev = 0; } if (lt) { if (c[0]) c[0]->toggle(); if (c[1]) c[1]->toggle(); lt = 0; } } void toggle() { swap(c0, c1); lt ^= 1; tg ^= 1; } void upd() { c1 = tg; c0 = 1 - tg; if (c[0]) { c0 += c[0]->c0; c1 += c[0]->c1; } if (c[1]) { c0 += c[1]->c0; c1 += c[1]->c1; } } Node(int i) : id(i) { c[0] = 0; c[1] = 0; c0 = 1; c1 = 0; tg = 0; lt = 0; p = 0; rev = 0; } }; Node* ns[101010]; struct LinkCut { void rot(Node* x) { Node* p = x->p; int d = x->dir(); if (!p->isr()) { p->p->setc(x, p->dir()); if (p->p) p->p->upd(); } else { x->p = p->p; if (x->p) x->p->upd(); } p->setc(x->c[!d], d); if (p) p->upd(); x->setc(p, !d); if (x) x->upd(); } void pp(Node* x) { if (!x->isr()) pp(x->p); x->push(); } void splay(Node* x) { pp(x); while (!x->isr()) { if (x->p->isr()) rot(x); else if (x->dir() == x->p->dir()) { rot(x->p); rot(x); } else { rot(x); rot(x); } } } Node* expose(Node* x) { Node* q = 0; for (; x; x = x->p) { splay(x); x->c[1] = q; x->upd(); q = x; } return q; } void evert(Node* x) { x = expose(x); x->rev ^= 1; x->push(); } void link(Node* x, Node* y) { evert(x); evert(y); splay(y); x->setc(y, 1); x->upd(); } void cut(Node* x, Node* y) { evert(x); expose(y); splay(x); x->c[1] = 0; x->upd(); y->p = 0; } int rootid(Node* x) { expose(x); splay(x); while (x->c[0]) { x = x->c[0]; x->push(); } splay(x); return x->id; } } lc; int cid[101010]; int isc[101010]; void dfs2(int x, int p, int c) { cid[x] = c; for (int nx : g[x]) { if (nx != p && isc[nx] == 0) { lc.link(ns[nx], ns[x]); dfs2(nx, x, c); } } } void toggletree(int a, int b) { lc.evert(ns[a]); lc.expose(ns[b]); lc.splay(ns[a]); ns[a]->toggle(); tes += ns[a]->c1 - ns[a]->c0; } int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { int a, b; scanf("%d %d", &a, &b); g[a].push_back(b); g[b].push_back(a); } for (int i = 1; i <= n; i++) { ns[i] = new Node(i); } dfs(1, 0); for (int i = 0; i < (int)cyc.size(); i++) { cp[cyc[i]] = i; isc[cyc[i]] = 1; } for (int c : cyc) { dfs2(c, 0, c); } init(1, 0, N - 1); for (int qq = 0; qq < m; qq++) { int a, b; scanf("%d %d", &a, &b); if (a == b) goto loppu; if (cid[a] != cid[b]) { togglecpath1(cid[a], cid[b]); if (a != cid[a]) { toggletree(cid[a], a); toggletree(cid[a], cid[a]); } if (b != cid[b]) { toggletree(cid[b], b); toggletree(cid[b], cid[b]); } } else { toggletree(cid[a], a); toggletree(cid[a], b); } loppu:; int cc = n; cc -= min((int)cyc.size() - 1, ces); cc -= tes; printf("%d\n", cc); } }
21
#include <bits/stdc++.h> using namespace std; const int maxn = 1005; int cnt[maxn], vis[maxn]; int ans[maxn][2]; int main() { int n, x, y, ct = 0, num = 0; cin >> n; for (int i = 1; i < n; i++) { cin >> x >> y; if (x == n) cnt[y]++, ct++; else if (y == n) cnt[x]++, ct++; } for (int i = 1; i < n; i++) if (cnt[i] > i) return cout << "NO", 0; if (ct < n - 1) return cout << "NO", 0; int mi = 1, p; for (int i = 1; i < n; i++) { if (cnt[i]) { p = i; cnt[i]--; while (cnt[i]--) { while (vis[mi]) mi++; if (mi >= i) return cout << "NO", 0; vis[mi] = 1; ans[num][0] = p; ans[num++][1] = mi; p = mi; } vis[i] = 1; ans[num][0] = p; ans[num++][1] = n; } } cout << "YES" << endl; for (int i = 0; i < num; i++) cout << ans[i][0] << " " << ans[i][1] << endl; return 0; }
11
#include <bits/stdc++.h> using namespace std; void init() { freopen("a.in", "r", stdin); cout.precision(9); } const double EPS = 1e-9; const int INF = (int)1e9 + 41; const int N = (int)1e6 + 34; int rnk[N], parent[N], a[N], b[N]; void make_set(int v) { parent[v] = v; rnk[v] = 0; } int find_set(int v) { if (v == parent[v]) return v; return parent[v] = find_set(parent[v]); } void union_sets(int a, int b) { a = find_set(a); b = find_set(b); if (a != b) { if (rnk[a] < rnk[b]) swap(a, b); parent[b] = a; if (rnk[a] == rnk[b]) ++rnk[a]; } } void solve() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; for (int j = 0; j < m; j++) cin >> b[j]; vector<pair<int, pair<int, int> > > e; long long ans = 0; for (int i = 0; i < n; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int x; cin >> x; x--; e.push_back({-a[i] - b[x], {i, n + x}}); } } for (int i = 0; i < n + m; i++) make_set(i); sort(e.begin(), e.end()); for (auto ed : e) { int w = -ed.first; int f = ed.second.first; int s = ed.second.second; if (find_set(f) == find_set(s)) { ans += w; } union_sets(f, s); } cout << ans; } int main() { solve(); return 0; }
16
#include <bits/stdc++.h> using namespace std; int n, lcnt; long long m, p, q; int a[100005], lsum[100005]; long long sum[100005], rmax[100005], lmin[100005], ls[100005]; long long ans1, ans2, ans; set<int> unu; inline long long Min(long long x, long long y) { return x < y ? x : y; } inline long long Max(long long x, long long y) { return x > y ? x : y; } inline int lowbit(int k) { return k & -k; } struct ta { long long t[100005]; void add(int k, long long x) { for (; k <= lcnt; k += lowbit(k)) t[k] += x; } inline long long query(int k) { long long ans = 0; for (; k; k -= lowbit(k)) ans += t[k]; return ans; } } tsum, tnum; struct ta2 { int t[100005]; void build() { for (int i = 1; i <= lcnt; i++) t[i] = n + 1; } void add(int k, int x) { for (; k <= lcnt; k += lowbit(k)) t[k] = Min(t[k], x); } inline int query(int k) { int ans = n + 1; for (; k; k -= lowbit(k)) ans = Min(ans, t[k]); return ans; } } tmin; struct subs { int l, r; long long w; bool operator<(const subs k) const { if (w != k.w) return w < k.w; else if (r != k.r) return r > k.r; else return l > k.l; } bool operator==(const subs k) const { return (k.l == l) && (k.r == r) && (k.w == w); } bool operator!=(const subs k) const { return (k.l != l) || (k.r != r) || (k.w != w); } }; priority_queue<subs> subsq; void del(int k); struct sg1 { bool b[400005]; void cover(int L, int R, int l, int r, int index) { if (L > R) return; if (b[index]) return; if (l == r) { del(l); b[index] = true; return; } if (L <= ((l + r) >> 1)) cover(L, R, l, ((l + r) >> 1), index << 1); if (R > ((l + r) >> 1)) cover(L, R, ((l + r) >> 1) + 1, r, index << 1 | 1); b[index] = b[index << 1] && b[index << 1 | 1]; } } s1; struct node2 { long long max, min; void pushup(node2 l, node2 r) { max = Max(l.max, r.max); min = Min(l.min, r.min); } }; struct sg2 { node2 t[400005]; void build(int l, int r, int index) { if (l == r) { t[index].min = t[index].max = sum[l]; return; } build(l, ((l + r) >> 1), index << 1); build(((l + r) >> 1) + 1, r, index << 1 | 1); t[index].pushup(t[index << 1], t[index << 1 | 1]); } node2 query(int L, int R, int l, int r, int index) { if ((L <= l) && (R >= r)) return t[index]; if (R <= ((l + r) >> 1)) return query(L, R, l, ((l + r) >> 1), index << 1); if (L > ((l + r) >> 1)) return query(L, R, ((l + r) >> 1) + 1, r, index << 1 | 1); node2 ans; ans.pushup(query(L, R, l, ((l + r) >> 1), index << 1), query(L, R, ((l + r) >> 1) + 1, r, index << 1 | 1)); return ans; } } s2; struct node3 { int l, r, s; }; struct sg3 { node3 t[4000005]; int root[100005], cnt; void ins(int &rt, int l, int r, int k) { if (rt == 0) rt = ++cnt; t[rt].s++; if (l == r) return; if (k <= ((l + r) >> 1)) ins(t[rt].l, l, ((l + r) >> 1), k); else ins(t[rt].r, ((l + r) >> 1) + 1, r, k); } void build() { for (int i = 0; i <= n; i++) ins(root[lsum[i]], 0, n, i); } inline int qsum(int rt, int l, int r, int k) { if (rt == 0) return 0; if (l == r) return t[rt].s; if (k <= ((l + r) >> 1)) return qsum(t[rt].l, l, ((l + r) >> 1), k); else return t[t[rt].l].s + qsum(t[rt].r, ((l + r) >> 1) + 1, r, k); } inline int query(int rt, int l, int r, int k) { if (l == r) return r; if (k <= t[t[rt].l].s) return query(t[rt].l, l, ((l + r) >> 1), k); else return query(t[rt].r, ((l + r) >> 1) + 1, r, k - t[t[rt].l].s); } } s3; struct sg4 { node3 t[4000005]; int cnt, root[100005]; void ins(int &rt, int Rt, int l, int r, int k) { rt = ++cnt; t[rt].s = t[Rt].s + 1; if (l == r) return; if (k <= ((l + r) >> 1)) { t[rt].r = t[Rt].r; ins(t[rt].l, t[Rt].l, l, ((l + r) >> 1), k); } else { t[rt].l = t[Rt].l; ins(t[rt].r, t[Rt].r, ((l + r) >> 1) + 1, r, k); } } void build() { ins(root[0], 0, 1, lcnt, lsum[0]); for (int i = 1; i <= n; i++) ins(root[i], root[i - 1], 1, lcnt, lsum[i]); } inline int query(int rt, int l, int r, int k) { if (k > r) return 0; if (rt == 0) return 0; if (l == r) return l; if (k > ((l + r) >> 1)) return query(t[rt].r, ((l + r) >> 1) + 1, r, k); else { int tmp = query(t[rt].l, l, ((l + r) >> 1), k); if (tmp) return tmp; else return query(t[rt].r, ((l + r) >> 1) + 1, r, k); } } } s4; struct splaynode { int l, r, size, fa; subs s; long long sum; bool operator<(const splaynode k) const { return s < k.s; } }; struct Splay { splaynode t[100005]; int root, top, cnt, stack[100005]; inline int newnode() { return stack[top--]; } void pushup(int rt) { t[rt].size = t[t[rt].l].size + t[t[rt].r].size + 1; t[rt].sum = t[t[rt].l].sum + t[t[rt].r].sum + t[rt].s.w; } void rorate(int k, int i) { int tmp = t[k].fa; t[k].fa = t[tmp].fa; if (t[t[tmp].fa].l == tmp) t[t[tmp].fa].l = k; else t[t[tmp].fa].r = k; t[tmp].fa = k; if (i == 0) { if (t[k].l > 0) t[t[k].l].fa = tmp; t[tmp].r = t[k].l; t[k].l = tmp; } else { if (t[k].r > 0) t[t[k].r].fa = tmp; t[tmp].l = t[k].r; t[k].r = tmp; } pushup(tmp); pushup(k); } void splay(int k, int g) { while (t[k].fa != g) { int tmp = t[k].fa; if (t[tmp].fa == g) if (t[tmp].r == k) rorate(k, 0); else rorate(k, 1); else { int Tmp = t[tmp].fa; if (t[Tmp].l == tmp) { if (t[tmp].l == k) rorate(tmp, 1); else rorate(k, 0); rorate(k, 1); } else { if (t[tmp].r == k) rorate(tmp, 0); else rorate(k, 1); rorate(k, 0); } } } if (g == 0) root = k; } void build(int l, int r, int &root) { root = ++cnt; t[root].s = (subs){((l + r) >> 1) + 1, ((l + r) >> 1), 0}; if (l < ((l + r) >> 1)) { build(l, ((l + r) >> 1) - 1, t[root].r); t[t[root].r].fa = root; } if (r > ((l + r) >> 1)) { build(((l + r) >> 1) + 1, r, t[root].l); t[t[root].l].fa = root; } pushup(root); } inline long long query(int k) { if (k > t[root].size) return -8333583335000000000ll; if (k == t[root].size) return t[root].sum; int p = root; while (t[t[p].r].size != k) { if (k < t[t[p].r].size) p = t[p].r; else k -= t[t[p].r].size + 1, p = t[p].l; } splay(p, 0); return t[t[p].r].sum; } void ins(subs k) { if (root == 0) { root = newnode(); t[root].l = t[root].r = t[root].fa = 0; t[root].s = k; pushup(root); return; } int p = root, v; while (true) { if (k < t[p].s) if (t[p].l) p = t[p].l; else { t[p].l = v = newnode(); break; } else if (t[p].r) p = t[p].r; else { t[p].r = v = newnode(); break; } } t[v].fa = p; t[v].l = t[v].r = 0; t[v].s = k; pushup(v); splay(v, 0); } void del(subs k) { if (t[root].size == 1) { root = 0; return; } int p = root; while (t[p].s != k) { if (k < t[p].s) p = t[p].l; else p = t[p].r; } splay(p, 0); int tmp = t[p].l; if (tmp == 0) { t[t[p].r].fa = 0; root = t[p].r; stack[++top] = p; return; } while (t[tmp].r) tmp = t[tmp].r; splay(tmp, root); t[tmp].fa = 0; root = tmp; if (t[p].r) t[t[p].r].fa = tmp, t[tmp].r = t[p].r; pushup(tmp); stack[++top] = p; } } s; inline long long read() { long long ret = 0, p = 1; char c = getchar(); while ((c < '0') || (c > '9')) { if (c == '-') p = -1; c = getchar(); } while ((c >= '0') && (c <= '9')) ret = (ret << 1) + (ret << 3) + c - '0', c = getchar(); return ret * p; } inline int find(long long k) { int l = 0, r = lcnt + 1; while (r - l > 1) { if (ls[((l + r) >> 1)] > k) r = ((l + r) >> 1); else l = ((l + r) >> 1); } return l; } inline long long checknum(long long std) { long long ans = 0; for (int i = 0; i <= n; i++) { ans += tnum.query(find(sum[i] - std)); tnum.add(lsum[i], 1); } for (int i = 0; i <= n; i++) tnum.add(lsum[i], -1); return ans; } void del(int k) { set<int>::iterator p = unu.find(k), st = unu.begin(), ed = unu.end(); ed--; if (p != st) { set<int>::iterator pre = p; pre--; node2 tmp = s2.query((*pre), k - 1, 1, n - 1, 1); s.del((subs){(*pre) + 1, k - 1, tmp.max - tmp.min}); } if (p != ed) { set<int>::iterator nex = p; nex++; node2 tmp = s2.query(k, (*nex) - 1, 1, n - 1, 1); s.del((subs){k + 1, (*nex) - 1, tmp.max - tmp.min}); } if ((p != st) && (p != ed)) { set<int>::iterator pre = p, nex = p; pre--; nex++; node2 tmp = s2.query((*pre), (*nex) - 1, 1, n - 1, 1); s.ins((subs){(*pre) + 1, (*nex) - 1, tmp.max - tmp.min}); } unu.erase(k); } inline subs Next(subs now) { int l = now.l, r = now.r, L, R = r; int tmp = s3.qsum(s3.root[lsum[l]], 0, n, l); if (tmp == 1) { int S = s4.query(s4.root[r - 1], 1, lcnt, lsum[l] + 1); if (S == 0) return (subs){0, R, -50000000001ll}; int Sum = s3.qsum(s3.root[S], 0, n, r - 1); L = s3.query(s3.root[S], 0, n, Sum); } else L = s3.query(s3.root[lsum[l]], 0, n, tmp - 1); return (subs){L, R, sum[R] - sum[L]}; } inline long long checksum(long long std, long long rest) { long long ans = 0; tmin.build(); for (int i = 0; i <= n; i++) { int t = find(sum[i] - std); rest -= tnum.query(t); ans += tnum.query(t) * sum[i]; ans += tsum.query(t); s1.cover(tmin.query(t) + 1, i, 1, n, 1); tnum.add(lsum[i], 1); tsum.add(lsum[i], -sum[i]); tmin.add(lsum[i], i); } for (int i = 0; i <= n; i++) tnum.add(lsum[i], -1); std--; ans += rest * std; tnum.add(lsum[0], 1); for (int i = 1; i <= n; i++) { int Tmp = find(sum[i] - std); if (ls[Tmp] != sum[i] - std) { Tmp++; tnum.add(lsum[i], 1); int p = s4.query(s4.root[i - 1], 1, lcnt, Tmp); if (p == 0) continue; int L = s3.query(s3.root[p], 0, n, s3.qsum(s3.root[p], 0, n, i - 1)); subsq.push((subs){L, i, sum[i] - sum[L]}); } else { int tmp = tnum.query(Tmp) - tnum.query(Tmp - 1); tnum.add(lsum[i], 1); if (tmp == 0) { Tmp++; int p = s4.query(s4.root[i - 1], 1, lcnt, Tmp); if (p == 0) continue; int L = s3.query(s3.root[p], 0, n, s3.qsum(s3.root[p], 0, n, i - 1)); subsq.push((subs){L, i, sum[i] - sum[L]}); } if (tmp <= rest) { int L = s3.query(s3.root[Tmp], 0, n, 1); s1.cover(L + 1, i, 1, n, 1); rest -= tmp; Tmp++; int p = s4.query(s4.root[i - 1], 1, lcnt, Tmp); if (p == 0) continue; L = s3.query(s3.root[p], 0, n, s3.qsum(s3.root[p], 0, n, i - 1)); subsq.push((subs){L, i, sum[i] - sum[L]}); } else { int L; if (rest) { L = s3.query(s3.root[Tmp], 0, n, tmp - rest + 1); s1.cover(L + 1, i, 1, n, 1); } L = s3.query(s3.root[Tmp], 0, n, tmp - rest); subsq.push((subs){L, i, sum[i] - sum[L]}); rest = 0; } } } return ans; } void upans2() { if (unu.empty()) { if (q) ans2 = -8333583335000000000ll; else ans2 = 0; return; } if (q == 0) { ans2 = -8333583335000000000ll; return; } ans2 = s.query(q - 1); if (ans2 == -8333583335000000000ll) return; set<int>::iterator p = unu.begin(); ans2 -= lmin[(*p) - 1]; p = unu.end(); p--; ans2 += rmax[(*p)]; } void work() { for (int i = 1; i <= n; i++) unu.insert(i); s.build(1, n - 1, s.root); long long l = ls[1] - ls[lcnt] - 1, r = ls[lcnt] - ls[1] + 2; while (r - l > 1) { if (checknum(((l + r) >> 1)) >= p) l = ((l + r) >> 1); else r = ((l + r) >> 1); } ans1 = checksum(r, p); upans2(); if (ans2 > -8333583335000000000ll) ans = ans1 + ans2; else ans = -9000000000000000000ll; } void pre() { n = read(); m = read(); for (int i = 1; i <= n; i++) a[i] = read(), ls[i] = sum[i] = sum[i - 1] + a[i]; ls[n + 1] = 0; sort(ls + 1, ls + 2 + n); ls[0] = -10000000000ll; for (int i = 1; i <= n + 1; i++) if (ls[i] != ls[i - 1]) ls[++lcnt] = ls[i]; for (int i = 0; i <= n; i++) lsum[i] = find(sum[i]); lmin[0] = sum[0]; for (int i = 1; i <= n; i++) lmin[i] = Min(lmin[i - 1], sum[i]); rmax[n] = sum[n]; for (int i = n - 1; i >= 0; i--) rmax[i] = Max(rmax[i + 1], sum[i]); s2.build(1, n - 1, 1); s3.build(); s4.build(); if (m > n) p = m - n, q = n; else p = 0, q = m; work(); } void doit() { q--; p++; for (; q >= 0; q--, p++) { subs tmp = subsq.top(); subsq.pop(); subsq.push(Next(tmp)); s1.cover(tmp.l + 1, tmp.r, 1, n, 1); while (subsq.top() == tmp) subsq.pop(); ans1 += tmp.w; upans2(); if (ans2 > -8333583335000000000ll) ans = Max(ans, ans1 + ans2); } printf("%I64d", ans); } int main() { pre(); doit(); }
23
#include <bits/stdc++.h> using namespace std; vector<int> adj[109][109]; vector<int> w; bool mark[1000]; void dfs(int v, int c) { mark[v] = true; for (int i = 0; i < adj[c][v].size(); i++) { int u = adj[c][v][i]; if (!mark[u]) dfs(u, c); } } int main() { int n, m; int v, u, c; int i, j; cin >> n >> m; for (i = 0; i < m; i++) { cin >> v >> u >> c; v--, u--, c--; if (!mark[c]) { mark[c] = true; w.push_back(c); } adj[c][v].push_back(u); adj[c][u].push_back(v); } int t, q; vector<int> tt; cin >> q; for (i = 0; i < q; i++) { t = 0; cin >> v >> u; v--, u--; for (j = 0; j < w.size(); j++) { fill(mark, mark + n, false); dfs(v, w[j]); if (mark[u]) t++; } tt.push_back(t); } for (int i = 0; i < q; i++) cout << tt[i] << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i; cin >> n >> m; for (i = 0; i < n; i++) { if (i % m == 0) n++; } cout << i - 1; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t) { int a, i; cin >> a; if (a == 1) cout << "-1" << endl; else { cout << "2"; for (i = 1; i < a; i++) { cout << "3"; } cout << "\n"; } t--; } }
2
#include <bits/stdc++.h> using namespace std; int n; int a[300010]; int odd1, odd2, even1, even2; int main() { cin >> n; odd1 = 0; odd2 = 0; even1 = 0; even2 = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if ((i + 1) % 2 == 0) { even2 += a[i]; } else { odd2 += a[i]; } } int ans = 0; for (int i = 0; i < n; i++) { int even; int odd; if ((i + 1) % 2 == 0) { even2 -= a[i]; } else { odd2 -= a[i]; } even = even1 + odd2; odd = odd1 + even2; if (even == odd) { ans++; } if ((i + 1) % 2 == 0) { even1 += a[i]; } else { odd1 += a[i]; } } cout << ans << endl; }
4
#include <bits/stdc++.h> const int maxn = 5005; const long long P = 998244353; using namespace std; int O[maxn][maxn], C[maxn][maxn], jc[maxn]; int main() { int n; cin >> n; for (int i = 0; i <= n; i++) O[i][0] = C[i][0] = 1; jc[0] = 1; for (int i = 1; i <= n; i++) jc[i] = 1LL * jc[i - 1] * i % P; for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; if (C[i][j] >= P) C[i][j] -= P; O[i][j] = 1LL * (i - j) * O[i - 1][j - 1] % P + 1LL * (j + 1) * O[i - 1][j] % P; if (O[i][j] >= P) O[i][j] -= P; } } for (int i = 0; i <= n - 1; i++) { long long ans = 0; for (int j = max(1, i); j <= n; j++) { ans = ans + 1LL * O[j][i] * C[n][j] % P * jc[n - j] % P; if (ans >= P) ans -= P; } printf("%lld ", ans); } return 0; }
23
#include <bits/stdc++.h> using namespace std; int main() { int n = 0; int d = 0; cin >> n >> d; int m = 0; cin >> m; int x = 0; int y = 0; for (int i = 0; i < m; i++) { cin >> x >> y; if ((y <= x + d) && (y <= -x + 2 * n - d) && (y >= x - d) && (y >= -x + d)) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long test, i, j, xy, flag = 0, n, u, count, d, o1 = 0, o2 = 0, s, e, l, r, x, y, m, z, max1, x1, y1, k, x2, y2, z1, sum, f, min1; map<long long, long long> mk; mk[1] = 1; mk[2] = 1; x = y = 1; while (x <= 1000) { x1 = x; x = x + y; y = x1; mk[x]++; } cin >> n; string a = string(n, 'o'); for (i = 0; i < n; i++) { if (mk[i + 1]) { a[i] = 'O'; } } cout << a << "\n"; return 0; }
0
#include <bits/stdc++.h> using namespace std; struct point { long double x, y; }; point centroid(int n, point pnt[]); long double dis(point a, point b); point new_centroid(point pivot, long double dist); long double ang(point final, point initial, point pivot); point print(point p, point cent, long double angle, long double rot, long double dist); long double ma(long double a, long double b); long double mi(long double a, long double b); int main() { int n, q; int i, j, k; scanf("%d %d", &n, &q); point pnt[n], cnt, prev_cnt, del; long double dist[n], an[n], rot = 0; int preva, prevb; cin >> del.x >> del.y; pnt[0].x = 0.0; pnt[0].y = 0.0; for (i = 1; i < n; i++) { long double ab, bb; cin >> ab >> bb; pnt[i].x = ab - del.x; pnt[i].y = bb - del.y; } cnt = centroid(n, pnt); for (i = 0; i < n; i++) { dist[i] = dis(pnt[i], cnt); an[i] = atan2(pnt[i].y - cnt.y, pnt[i].x - cnt.x); } preva = 0; prevb = 1; for (i = 0; i < q; i++) { scanf("%d", &j); if (j == 1) { int f, t, p; point piv; scanf("%d %d", &f, &t); prev_cnt = cnt; if (preva == f - 1) { p = prevb; preva = t - 1; } else { p = preva; prevb = t - 1; } piv = print(pnt[p], cnt, an[p], rot, dist[p]); cnt = new_centroid(piv, dist[p]); rot += ang(cnt, prev_cnt, piv); } else { int key; point ans; scanf("%d", &key); ans = print(pnt[key - 1], cnt, an[key - 1], rot, dist[key - 1]); printf("%0.10lf %0.10lf\n", (double)(ans.x + del.x), (double)(ans.y + del.y)); } } return 0; } point print(point p, point cent, long double angle, long double rot, long double dist) { point ans; ans.x = cent.x + dist * cosl(angle + rot); ans.y = cent.y + dist * sinl(angle + rot); return ans; ; } long double ang(point final, point initial, point pivot) { long double theta, a, b, c, k; a = dis(final, pivot); b = dis(initial, pivot); c = dis(final, initial); k = (a * a + b * b - c * c) / (2 * a * b); if (k > 0) k = mi(k, 1); else k = ma(k, -1); theta = acosl(k); if (theta < 0) theta += acosl(-1.0); if (final.x > initial.x) return theta; else return -theta; } point new_centroid(point pivot, long double dist) { point cnt; cnt.x = pivot.x; cnt.y = pivot.y - dist; return cnt; } point centroid(int n, point pnt[]) { point cnt; long double area, k; int i, a, b; cnt.x = 0.0; cnt.y = 0.0; area = 0.0; for (i = 0; i < n; i++) { a = i; b = (i + 1) % n; k = pnt[a].x * pnt[b].y - pnt[a].y * pnt[b].x; cnt.x += (pnt[a].x + pnt[b].x) * k; cnt.y += (pnt[a].y + pnt[b].y) * k; area += k; } area *= 0.5; cnt.x /= (6 * area); cnt.y /= (6 * area); return cnt; } long double ma(long double a, long double b) { if (a > b) return a; else return b; } long double mi(long double a, long double b) { if (a > b) return b; else return a; } long double dis(point a, point b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); }
18
#include <bits/stdc++.h> using namespace std; const int N = 300; int a[4], n, av, c, mx, need; string str; char ch[5] = {'A', 'C', 'G', 'T'}; int main() { cin >> n >> str; for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { if (str[i] == ch[j]) a[j]++; } if (str[i] == '?') av++; } for (int i = 0; i < 4; i++) mx = max(mx, a[i]); for (int i = 0; i < 4; i++) { need += (mx - a[i]); } for (int i = 0; i < n; i++) { if (str[i] == '?') { for (int j = 0; j < 4; j++) { if (a[j] < mx) { av--; a[j]++; str[i] = ch[j]; need--; break; } } } } if (av && (av % 4) == 0) { for (int i = 0; i < n; i++) { if (str[i] == '?') { str[i] = ch[c]; c++; c %= 4; } } cout << str << '\n'; } else if (av || need) cout << "===\n"; else cout << str << '\n'; return 0; }
1
#include <bits/stdc++.h> using namespace std; vector<int> adj[200000]; set<int> prob[200000]; int counter[200000]; bool vis[200000]; vector<int> res; bool white[200000]; int flood(int ind) { vis[ind] = 1; for (int i = 0; i < (int)adj[ind].size(); i++) { int j = adj[ind][i]; if (vis[j]) continue; int x = 0; if (prob[ind].count(j)) x = 1; counter[ind] += flood(j) + x; } return counter[ind]; } int main() { int n, x, y, t; scanf("%d", &n); for (int i = 0; i < n - 1; i++) { scanf("%d%d%d", &x, &y, &t); x--; y--; adj[x].push_back(y); adj[y].push_back(x); if (t == 2) { prob[x].insert(y); prob[y].insert(x); white[x] = 1; white[y] = 1; } } flood(0); for (int i = 0; i < n; i++) { if (counter[i] == 0 && white[i]) res.push_back(i); } printf("%d\n", (int)res.size()); for (int i = 0; i < (int)res.size(); i++) { printf("%d", res[i] + 1); if (i == (int)res.size() - 1) printf("\n"); else printf(" "); } }
8
#include <bits/stdc++.h> long long int o[200005]; long long int e[200005]; long long int dp[200005][2]; long long int k; long long int dpf(long long int r, long long int dif) { if (dp[r][dif] > 0) { return dp[r][dif] - 1; } if (r == 0) { return dif; } long long int ans = 0; if (dif) { ans += dpf(r - 1, 0); ans += dpf(r - 1, 1) * (k - 2); } else { ans += dpf(r - 1, 1) * (k - 1); } ans %= 998244353; dp[r][dif] = ans + 1; return ans; } int main(void) { long long int i, j, n, ol, el, ans, mul, ct, neww, last; ol = el = 0; scanf("%lli %lli", &n, &k); for (i = 0; i < n; i++) { if (i % 2 == 1) { scanf("%lli", &o[i / 2]); ol++; } else { scanf("%lli", &e[i / 2]); el++; } } ans = 1; for (i = 0; i < ol - 1; i++) { if (o[i] == o[i + 1] && o[i] != -1) { ans = 0; } } for (i = 0; i < el - 1; i++) { if (e[i] == e[i + 1] && e[i] != -1) { ans = 0; } } for (i = 0; i < ol; i++) { mul = 0; ct = 0; while (o[i] != -1 && i < ol) { i++; } if (i >= ol) { break; } last = i; if (last > 0) { last--; } last = o[last]; while (o[i] == -1 && i < ol) { ct++; i++; } neww = i; if (neww == ol) { neww--; } neww = o[neww]; if (last == neww && last == -1 && ct == 1) { ans *= k; } else if (last == neww && last == -1) { ans *= ((dpf(ct - 2, 0) * k + dpf(ct - 2, 1) * (((k * (k - 1))) % 998244353)) % 998244353); } else if (last == -1 || neww == -1) { ans *= ((dpf(ct - 1, 1) * (k - 1) + dpf(ct - 1, 0)) % 998244353); } else if (last != neww) { ans *= dpf(ct, 1); } else if (last == neww) { ans *= dpf(ct, 0); } ans %= 998244353; } ans %= 998244353; for (i = 0; i < el; i++) { ct = 0; while (e[i] != -1 && i < el) { i++; } if (i >= el) { break; } last = i; if (last > 0) { last--; } last = e[last]; while (e[i] == -1 && i < el) { ct++; i++; } neww = i; if (neww == el) { neww--; } neww = e[neww]; if (last == neww && last == -1 && ct == 1) { ans *= k; } else if (last == neww && last == -1) { ans *= ((dpf(ct - 2, 0) * k + dpf(ct - 2, 1) * (((k * (k - 1))) % 998244353)) % 998244353); } else if (last == -1 || neww == -1) { ans *= ((dpf(ct - 1, 1) * (k - 1) + dpf(ct - 1, 0)) % 998244353); } else if (last != neww) { ans *= dpf(ct, 1); } else if (last == neww) { ans *= dpf(ct, 0); } ans %= 998244353; } printf("%lli\n", ans % 998244353); }
14
#include <bits/stdc++.h> int s[300005]; int n, m; bool check(int x) { int tt = 0; for (int i = 1; i <= n; i++) { if (s[i] + x < tt) return false; if (s[i] > tt && (s[i] + x < m || s[i] + x - m < tt)) tt = s[i]; } return true; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &s[i]); } int l = 0, r = m; while (l <= r) { int mid = (l + r) >> 1; if (check(mid)) r = mid - 1; else l = mid + 1; } printf("%d", l); }
9
#include <bits/stdc++.h> using namespace std; const int maxn = 300003; long long n, a, b, c, d, st, len; long long t[maxn], q[maxn]; int main() { long long rat = 0ll, pos = 1, sum = 0, m1 = 0; scanf("%lld%lld%lld%lld%lld%lld%lld", &n, &a, &b, &c, &d, &st, &len); for (int i = 1; i <= n; ++i) scanf("%lld%lld", &t[i], &q[i]); rat = st; t[0] = -1; for (int i = 1; i <= n; ++i) { while (pos <= n && t[pos] - t[i] < len) { sum += q[pos] ? c : -d; m1 = min(m1, sum); ++pos; } if (rat + m1 >= 0) { printf("%lld\n", t[i - 1] + 1); return 0; } sum -= q[i] ? c : -d; m1 -= q[i] ? c : -d; rat += q[i] ? a : -b; if (rat < 0) { printf("-1\n"); return 0; } } printf("%lld\n", t[n] + 1); return 0; }
16
#include <bits/stdc++.h> using namespace std; long long f[10005]; char s[100005]; int main() { long long maxx, n, i, ans = 1, tmp = 0; scanf("%I64d%s", &n, s); for (i = 0; i < n; i++) f[s[i]]++; maxx = max(max(f['A'], f['G']), max(f['T'], f['C'])); tmp = 0; if (f['A'] == maxx) tmp++; if (f['C'] == maxx) tmp++; if (f['G'] == maxx) tmp++; if (f['T'] == maxx) tmp++; for (i = 0; i < n; i++) ans = (ans * tmp) % 1000000007; printf("%I64d\n", ans); return 0; }
7
#include <bits/stdc++.h> using namespace std; int grid[55][55]; int R, C; int state[55][55]; map<pair<int, int>, pair<int, int> > parent; string ans; int dr[4] = {1, 0, -1, 0}; int dc[4] = {0, 1, 0, -1}; void dfs(int r, int c) { if (ans == "Yes") return; state[r][c] = 1; for (int d = 0; d < 4; d++) { int nr = r + dr[d]; int nc = c + dc[d]; if (nr < 0 || nr >= R || nc < 0 || nc >= C) continue; if (grid[nr][nc] != grid[r][c]) continue; if (state[nr][nc] == 0) { parent[pair<int, int>(nr, nc)] = pair<int, int>(r, c); dfs(nr, nc); } else if (state[nr][nc] == 1 && parent[pair<int, int>(r, c)] != pair<int, int>(nr, nc)) { int lenCycle = 0; for (pair<int, int> curr = pair<int, int>(nr, nc); curr != pair<int, int>(-1, -1); curr = parent[curr]) lenCycle++; if (lenCycle >= 4) ans = "Yes"; } } } int main() { cin >> R >> C; for (int r = 0; r < R; r++) { string tmp; cin >> tmp; for (int c = 0; c < C; c++) { grid[r][c] = tmp[c]; state[r][c] = 0; } } parent.clear(); ans = "No"; for (int r = 0; r < R; r++) for (int c = 0; c < C; c++) if (state[r][c] == 0) { parent[pair<int, int>(r, c)] = pair<int, int>(-1, -1); dfs(r, c); } cout << ans << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> ostream& operator<<(ostream& output, const pair<T1, T2>& p) { output << "(" << p.first << "," << p.second << ")"; return output; } template <typename T> ostream& operator<<(ostream& output, const vector<T>& v) { unsigned int i; bool f = 1; for (i = 0; i < v.size(); i++) { if (!f) output << " "; output << v[i]; f = 0; } return output; } template <typename T> ostream& operator<<(ostream& output, const list<T>& l) { typename list<T>::const_iterator it; bool f = 1; for (it = l.begin(); it != l.end(); it++) { if (!f) output << " "; output << *it; f = 0; } return output; } template <typename T1, typename T2> ostream& operator<<(ostream& output, const map<T1, T2>& m) { typename map<T1, T2>::const_iterator it; bool f = 1; for (it = m.begin(); it != m.end(); it++) { if (!f) output << " "; output << "(" << it->first << " -> " << it->second << ")"; f = 0; } return output; } template <typename T> ostream& operator<<(ostream& output, const set<T>& s) { typename set<T>::const_iterator it; bool f = 1; for (it = s.begin(); it != s.end(); it++) { if (!f) output << " "; output << *it; f = 0; } return output; } char s[200001]; int l[200000], r[200000]; vector<pair<int, int> > ans; set<pair<int, char> > S; int occ[26]; vector<int> Q[26], Q2[26]; int getAns(int s, int e) { if (s > e) return 0; int i = s; if (r[s] == s) { getAns(s + 1, e); ans.push_back(make_pair(s, s)); return 0; } vector<int> v; while ((r[i] != -1) && (r[i] > i)) v.push_back(i), i = r[i]; v.push_back(i); for (i = 1; i < v.size(); i++) getAns(v[i - 1] + 1, v[i] - 1); getAns(v.back() + 1, e); ans.push_back(make_pair(v[0], v.back())); return 0; } int bit[200001]; set<int> sad; int main() { int i; int t; scanf("%d", &t); while (t--) { scanf("%s", s); int n = strlen(s); for (i = 0; i < n; i++) r[i] = (i == n - 1) ? -1 : i + 1, l[i] = (i == 0) ? -1 : i - 1; fill(occ, occ + 26, 0); for (i = 0; i < n - 1; i++) { if (s[i] == s[i + 1]) S.insert(make_pair(i, s[i])), occ[s[i] - 'a']++; } S; for (auto it = S.begin(); it != S.end(); it++) { auto it2 = it; it2++; if ((it2 != S.end()) && (it->second != it2->second)) { Q[it->second - 'a'].push_back(it->first); Q2[it2->second - 'a'].push_back(it2->first); } } while (1) { int mi = -1; for (i = 0; i < 26; i++) { if (occ[i] == 0) continue; if ((mi == -1) || (occ[i] > occ[mi])) mi = i; } if (mi == -1) break; ; S; while (!Q[mi].empty()) { "here"; auto it = S.lower_bound(make_pair(Q[mi].back(), '\0')); if ((it == S.end()) || (it == --S.end()) || (*it != make_pair(Q[mi].back(), s[Q[mi].back()]))) Q[mi].pop_back(); else { auto it2 = it; it2++; if (it->second == it2->second) Q[mi].pop_back(); else break; } } while (!Q2[mi].empty()) { auto it = S.lower_bound(make_pair(Q2[mi].back(), '\0')); if ((it == S.end()) || (it == S.begin()) || (*it != make_pair(Q2[mi].back(), s[Q2[mi].back()]))) Q2[mi].pop_back(); else { auto it2 = it; it2--; if (it->second == it2->second) Q2[mi].pop_back(); else break; } } if (!Q[mi].empty()) { int u = Q[mi].back(); ; Q[mi].pop_back(); auto it = S.lower_bound(make_pair(u, '\0')); auto it2 = it; it2++; int a = it->first, b = it2->first, c = r[b]; ; ; ; l[r[a]] = r[b] = -1; l[c] = a, r[a] = c; assert(it->second != it2->second); occ[it->second - 'a']--, occ[it2->second - 'a']--; S.erase(it), it2 = S.erase(it2); it = it2; if ((it != S.begin()) && (it2 != S.end())) { it--; if (it->second != it2->second) { Q[it->second - 'a'].push_back(it->first); Q2[it2->second - 'a'].push_back(it2->first); } } } else if (!Q2[mi].empty()) { int u = Q2[mi].back(); ; Q2[mi].pop_back(); auto it = S.lower_bound(make_pair(u, '\0')); auto it2 = it; it--; int a = it->first, b = it2->first, c = r[b]; l[r[a]] = r[b] = -1; l[c] = a, r[a] = c; assert(it->second != it2->second); occ[it->second - 'a']--, occ[it2->second - 'a']--; S.erase(it), it2 = S.erase(it2); it = it2; if ((it != S.begin()) && (it2 != S.end())) { it--; if (it->second != it2->second) { Q[it->second - 'a'].push_back(it->first); Q2[it2->second - 'a'].push_back(it2->first); } } } else break; } int c = 0; char z; for (i = 0; i < 26; i++) { if (occ[i] > 0) c++, z = i + 'a'; } assert(c <= 1); for (auto it = S.begin(); it != S.end(); it++) { int u = it->first, v = r[it->first]; assert(it->second == z); r[u] = l[v] = -1; }; ; getAns(0, n - 1); printf("%d\n", ans.size()); int j; fill(bit, bit + n + 1, 0); for (i = 0; i < n; i++) { for (j = i + 1; j <= n; j += j & (-j)) bit[j]++; } for (i = 0; i < n; i++) sad.insert(i); for (i = 0; i < ans.size(); i++) { int s = 0, e = 0; for (j = ans[i].first + 1; j > 0; j -= j & (-j)) s += bit[j]; for (j = ans[i].second + 1; j > 0; j -= j & (-j)) e += bit[j]; auto soclose = sad.lower_bound(ans[i].first); while ((soclose != sad.end()) && (*soclose <= ans[i].second)) { int iamsodumb = *soclose; for (j = iamsodumb + 1; j <= n; j += j & (-j)) bit[j]--; soclose = sad.erase(soclose); } printf("%d %d\n", s, e); } for (i = 0; i < 26; i++) { Q[i].clear(); Q2[i].clear(); } S.clear(); ans.clear(); } return 0; }
23
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int MAXN = 1e5 + 7; long long n, a[MAXN], even = -1; long long qpow(long long a, long long b) { long long ret = 1, base = a; while (b) { if (b & 1) { ret *= base; ret %= MOD; } b >>= 1; base *= base; base %= MOD; } return ret; } const long long thr = qpow(3, 1e9 + 5); const long long twr = qpow(2, 1e9 + 5); int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; if (!(a[i] & 1)) { even = 1; } } long long p = 2; for (int i = 1; i <= n; ++i) { p = qpow(p, a[i]); p %= MOD; } p *= twr; p %= MOD; long long q = p; p += even; p = (p + MOD) % MOD; p *= thr; p %= MOD; cout << p << '/' << q << '\n'; return 0; }
12
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long i; vector<long long> a(n + 10), vp, v; map<long long, long long> mp; for (i = 1; i <= n; i++) { cin >> a[i]; mp[a[i]] = 1; } for (i = 1; i <= n; i++) { if (mp[i] == 0 and a[i] == 0) vp.push_back(i); if (mp[i] == 0) v.push_back(i); } long long x = vp.size(), y = v.size(), j = 0; for (i = 0; i < x; i++) { for (;; j++) { j %= y; if (vp[i] != v[j] and v[j] != 0) { a[vp[i]] = v[j]; v[j] = 0; j++; break; } } } j = 0; for (i = 1; i <= n; i++) { if (a[i] == 0) { for (; j < y; j++) { if (v[j] != 0) { a[i] = v[j]; v[j] = 0; j++; break; } } } } for (i = 1; i <= n; i++) cout << a[i] << ' '; cout << endl; }
7
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; long long dp[101][100001]; long long comb[101][101]; void init() { comb[0][0] = 1; for (int i = 1; i <= 100; i++) { for (int j = 0; j <= i; j++) { if (j - 1 >= 0) comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % MOD; else comb[i][j] = comb[i - 1][j]; } } } long long power(long long x, long long p) { long long ret = 1; long long base = x; while (p > 0) { if (p & 1) ret = ret * base % MOD; p >>= 1; base = base * base % MOD; } return ret; } int main() { long long r, c, n; cin >> r >> c >> n; init(); dp[0][0] = 1; for (int i = 0; i < r; i++) { long long cnt = c / r; if (c % r > i) cnt++; for (int j = 0; j <= r; j++) { long long ptr = power(comb[r][j], cnt); for (int k = j; k <= n; k++) { dp[i + 1][k] = (dp[i + 1][k] + dp[i][k - j] * ptr) % MOD; } } } cout << dp[r][n] << endl; return 0; }
11
#include <bits/stdc++.h> using namespace std; int main() { long long int n, q; scanf("%lld%lld", &n, &q); long long int a[n], b[n]; for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); b[i] = a[i]; } int ind = 0; vector<long long int> list; for (int i = 0; i < n; i++) { if (a[i] > a[ind]) { ind = i; } } list.push_back(a[ind]); for (int i = ind + 1; i < n; i++) { list.push_back(a[i]); } map<long long int, pair<long long int, long long int>> m; for (int i = 0; i < ind; i++) { m[i + 1] = {b[i], b[i + 1]}; if (b[i] > b[i + 1]) { list.push_back(b[i + 1]); b[i + 1] = b[i]; } else { list.push_back(b[i]); } } cout << endl; while (q--) { long long int x; scanf("%lld", &x); if (x <= ind) { printf("%lld%c%lld\n", m[x].first, ' ', m[x].second); } else { x -= (ind); x %= (n - 1); if (!x) { x = n - 1; } printf("%lld%c%lld\n", a[ind], ' ', list[x]); } } }
7
#include <bits/stdc++.h> using namespace std; const int MAX_N = 2e5 + 10; long long a[MAX_N], n, m, t; vector<int> vec; long long get(long long d) { d = vec[d]; long long cnt = 0, mm = 0; long long T = 0, tmpT = 0; for (int i = 0; i < n; i++) { if (a[i] > d) { continue; } tmpT += a[i]; T += a[i]; cnt++; mm++; if (T > t) { return cnt - 1; } else if (mm == m) { mm = 0; T += tmpT; tmpT = 0; } } return cnt; } int main() { int _; for (scanf("%d", &_); _; _--) { vec.clear(); scanf("%lld%lld%lld", &n, &m, &t); for (int i = 0; i < n; i++) { scanf("%lld", a + i); if (a[i] <= t) vec.push_back(a[i]); } vec.push_back(t); sort(vec.begin(), vec.end()); vec.erase(unique(vec.begin(), vec.end()), vec.end()); int siz = vec.size(); long long l = 0, r = siz - 1; while (l < r - 1) { long long mid = (l + r) / 2, midd = (mid + r) / 2; if (get(mid) < get(midd)) { l = mid + 1; } else { r = midd; } } long long ans1, ans2; if (get(r) >= get(l)) ans2 = vec[r], ans1 = get(r); else ans2 = vec[l], ans1 = get(l); if (r + 1 != siz) { if (get(r + 1) > get(r)) { ans2 = vec[r + 1], ans1 = get(r + 1); } } printf("%lld %lld\n", ans1, ans2); } return 0; }
13
#include <bits/stdc++.h> using namespace std; char a[150000]; unsigned long long k; int b[150000]; unsigned long long mod = 1000000007; unsigned long long getA(unsigned long long b, unsigned long long base) { unsigned long long ans = 1; while (b > 0) { if (b % 2) ans *= base; b /= 2; base = base * base; base %= mod; ans %= mod; } ans %= mod; return ans; } unsigned long long GetAns(unsigned long long base, unsigned long long k) { if (k == 0) return 1LL; if (k == 1) return (long long)(1 + base) % mod; if (k % 2) { unsigned long long temp = k / 2; unsigned long long ans = GetAns(base, temp); temp = 1 + getA(temp + 1, base); temp %= mod; ans = ans * temp % mod; return ans; } else { unsigned long long tempp = getA(k, base); k--; unsigned long long temp = k / 2; unsigned long long ans = GetAns(base, temp); temp = 1 + getA(temp + 1, base); ans = ans * temp % mod; ans += tempp; ans %= mod; return ans; } } int main() { cin >> a >> k; k--; int l = strlen(a); int counter = 0; for (int i = 0; i < l; i++) { if (a[i] == '0' || a[i] == '5') { b[counter++] = i; } } unsigned long long ans = 0; for (int i = 0; i < counter; i++) { ans += getA(b[i], 2LL); ans %= mod; } unsigned long long cheng = getA(l, 2); unsigned long long re = GetAns(cheng, k); re *= ans; re %= mod; cout << re << endl; }
9
#include <bits/stdc++.h> using namespace std; std::vector<int> luckynums; void bt(int num, int idx, int n) { if (idx == n) { luckynums.push_back(num); return; } bt(num * 10 + 4, idx + 1, n); bt(num * 10 + 7, idx + 1, n); return; } int upperbnd(int l, int r, int val) { int low = l, high = r, ans = r + 1; while (low <= high) { int mid = (low + high) / 2; if (mid > val) { ans = min(ans, mid); high = mid - 1; } else low = mid + 1; } return ans; } int lowerbnd(int l, int r, int val) { int low = l, high = r, ans = r + 1; while (low <= high) { int mid = (low + high) / 2; if (mid >= val) { ans = min(ans, mid); high = mid - 1; } else low = mid + 1; } return ans; } int main() { luckynums.push_back(0); for (int i = 1; i <= 9; i++) bt(0, 0, i); int l1, r1, l2, r2, k; scanf("%d%d%d%d%d", &l1, &r1, &l2, &r2, &k); long long poss = 0; luckynums.push_back(1e9 + 5); for (int i = 1, j = k; j < luckynums.size() - 1; i++, j++) { int yr = lowerbnd(l2, r2, luckynums[j + 1]); int yl = lowerbnd(l2, r2, luckynums[j]); yr--; int xl = upperbnd(l1, r1, luckynums[i - 1]); int xr = upperbnd(l1, r1, luckynums[i]); xr--; if (xl <= luckynums[i] && xr <= luckynums[i] && xl > luckynums[i - 1] && yr < luckynums[j + 1] && yl < luckynums[j + 1] && yl >= luckynums[j] && yr >= luckynums[j]) { int c1 = xr - xl + 1; int c2 = yr - yl + 1; poss += 1LL * c1 * c2; } int oldyl = yl, oldxr = xr; yr = lowerbnd(l1, r1, luckynums[j + 1]); yl = lowerbnd(l1, r1, luckynums[j]); yr--; xl = upperbnd(l2, r2, luckynums[i - 1]); xr = upperbnd(l2, r2, luckynums[i]); xr--; if (xl <= luckynums[i] && xr <= luckynums[i] && xl > luckynums[i - 1] && yr < luckynums[j + 1] && yl < luckynums[j + 1] && yl >= luckynums[j] && yr >= luckynums[j]) { int h = 0; if (yl == oldxr) { h += (oldyl - xl); yl++; } int c1 = xr - xl + 1; int c2 = yr - yl + 1; poss += 1LL * c1 * c2 + h; } } cout << setprecision(15) << fixed << (double)poss / (1LL * (r1 - l1 + 1) * (r2 - l2 + 1)) << endl; }
11
#include <bits/stdc++.h> int bucket[256]; int n; int k; void init() { for (int i = 0; i < 256; i++) { bucket[i] = -1; } scanf("%d%d", &n, &k); bucket[0] = 0; } void process(int kk) { if (bucket[kk] != -1) { printf("%d ", bucket[kk]); return; } int found_prev = -1; int found_prev_idx = -1; for (int i = 1; i < k; i++) { if (kk - i == 0) { found_prev = 0; found_prev_idx = 0; break; } if (bucket[kk - i] != -1) { found_prev_idx = kk - i; found_prev = bucket[kk - i]; break; } } if (found_prev == -1) { found_prev = kk - k + 1; found_prev_idx = kk - k + 1; } if (kk - found_prev + 1 > k) { for (int i = 0; i < k; i++) { if (bucket[kk - i] != -1) { break; } bucket[kk - i] = found_prev_idx + 1; } } else { for (int i = 0; i < k; i++) { if (bucket[kk - i] == found_prev) { break; } bucket[kk - i] = found_prev; } } printf("%d ", bucket[kk]); } int main() { init(); for (int i = 0; i < n; i++) { int kk; scanf("%d", &kk); process(kk); } putchar('\n'); }
9
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("tune=native") using namespace std; const int MAX = 5e5 + 5; const long long MOD = 1000000007; const long long MOD2 = 2010405347; const long long INF = 9e18; const int dr[] = {1, 0, -1, 0, 1, 1, -1, -1, 0}; const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1, 0}; const double pi = acos(-1); const double EPS = 1e-9; const int block = 500; long long n, k, x[MAX], y[MAX], le, ri, mid, res; priority_queue<pair<long long, long long> > pq; pair<long long, long long> f(long long lambda) { long long ret = 0, cnt = 0; for (int i = 1; i <= n; ++i) { ret += x[i]; pq.push({-x[i], 0}); pq.push({y[i] - lambda, 1}); pq.pop(); } for (int i = 1; i <= n; ++i) { ret += pq.top().first; cnt += pq.top().second; pq.pop(); } return {ret, cnt}; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 1; i <= n; ++i) cin >> x[i]; for (int i = 1; i <= n; ++i) cin >> y[i]; le = 0, ri = 10e10; while (le <= ri) { mid = le + ri >> 1; if (f(mid).second <= k) res = mid, le = mid + 1; else ri = mid - 1; } cout << f(res).first + res * k << '\n'; return 0; }
21
#include <bits/stdc++.h> using namespace std; template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) { os << '{'; for (int i = (0), __i = (((int)(vec).size())); i < __i; i++) { os << vec[i]; if (i + 1 != ((int)(vec).size())) os << ','; } os << '}'; return os; } template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &par) { os << '(' << par.first << ',' << par.second << ')'; return os; } int gcd(int x, int y) { return y ? gcd(y, x % y) : abs(x); } template <class T> T sqr(T x) { return x * x; } const long long MOD = 1e9 + 7; long long bin_pow(long long base, long long p) { if (p == 1) return base; if (p % 2 == 0) { long long t = bin_pow(base, p / 2); return t * t % MOD; } else return bin_pow(base, p - 1) * base % MOD; } long long inverse_element(long long x) { return bin_pow(x, MOD - 2); } long long divide(long long a, long long b) { return a * inverse_element(b) % MOD; } long long mult(long long a, long long b) { return (a * b) % MOD; } vector<int> g[202020]; int p[202020]; int ed[202020]; int d[202020]; bool used[202020]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = (0), __i = (n - 1); i < __i; i++) { int a, b; cin >> a >> b; g[a - 1].push_back(b - 1); g[b - 1].push_back(a - 1); } if (n <= 3) { cout << 0; return 0; } vector<int> ans; stack<int> st; st.push(0); p[0] = -1; while (!st.empty()) { int u = st.top(); if (ed[u] == ((int)(g[u]).size())) { d[u]++; if (p[u] != -1) d[p[u]] += d[u]; st.pop(); continue; } int v = g[u][ed[u]++]; if (v != p[u]) { p[v] = u; st.push(v); } } vector<int> centroids; int u = 0; while (true) { bool cent = (n - d[u] <= n / 2); int cur = u; for (int i = (0), __i = (((int)(g[u]).size())); i < __i; i++) { int v = g[u][i]; if (v == p[u]) continue; if (d[v] > n / 2) { u = v; cent = false; break; } else if (d[v] == n / 2) { u = v; break; } } if (cent) { centroids.push_back(cur); used[cur] = true; } if (cur == u) break; } for (int i = (0), __i = (n); i < __i; i++) d[i] = ed[i] = 0; for (int j = (0), __j = (((int)(centroids).size())); j < __j; j++) { for (int i = (0), __i = (((int)(g[centroids[j]]).size())); i < __i; i++) { if (used[g[centroids[j]][i]]) continue; vector<pair<int, int> > leaves; st.push(g[centroids[j]][i]); p[g[centroids[j]][i]] = centroids[j]; int deep = 0; while (!st.empty()) { int u = st.top(); if (ed[u] == ((int)(g[u]).size())) { if (ed[u] == 1) leaves.push_back({deep, u}); st.pop(); deep--; continue; } int v = g[u][ed[u]++]; if (!used[v] && v != p[u]) { p[v] = u; st.push(v); deep++; } } sort((leaves).begin(), (leaves).end()); for (int i = (0), __i = (((int)(leaves).size()) - 1); i < __i; i++) { int u = leaves[i].second; while (ed[u] < 3) { d[p[u]] = u; u = p[u]; } d[leaves[i].second] = p[u]; ed[u]--; ans.push_back(p[u]); ans.push_back(u); ans.push_back(leaves[i].second); while (u != d[leaves[i].second]) { p[u] = d[u]; u = d[u]; } } if (((int)(leaves).size()) > 0) { int leaf = leaves[((int)(leaves).size()) - 1].second; int u = p[leaf]; while (!used[u]) { ans.push_back(p[u]); ans.push_back(u); ans.push_back(leaf); u = p[u]; } } } } cout << ((int)(ans).size()) / 3; for (int i = (0), __i = (((int)(ans).size()) / 3); i < __i; i++) { cout << endl << ans[i * 3] + 1 << ' ' << ans[i * 3 + 1] + 1 << ' ' << ans[i * 3 + 2] + 1 << ' '; } return 0; }
18
#include <bits/stdc++.h> using namespace std; const int max_n = 40; int mod; int t, x, dp[max_n], cnt[max_n]; vector<int> v; int mul(int x, int y) { return 1LL * x * y % mod; } void get_v(int x) { v.clear(); while (x) { v.push_back(x % 2); x /= 2; } } int main() { cin >> t; while (t--) { cin >> x >> mod; get_v(x); for (int i = 0; i < v.size(); ++i) { cnt[i] = 1 << i; } cnt[v.size() - 1] = 1 + x - (1 << (v.size() - 1)); for (int i = 0; i < v.size(); ++i) { cnt[i] %= mod; dp[i] = cnt[i]; } for (int i = 0; i < v.size(); ++i) { for (int j = i + 1; j < v.size(); ++j) { dp[j] += mul(dp[i], cnt[j]); dp[j] %= mod; } } cout << accumulate(dp, dp + v.size(), 0LL) % mod << "\n"; } return 0; }
9
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") template <class S, class T> ostream& operator<<(ostream& os, const pair<S, T>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template <class T> ostream& operator<<(ostream& os, const vector<T>& p) { os << "[ "; for (auto& it : p) os << it << " "; return os << "]"; } template <class T> ostream& operator<<(ostream& os, const unordered_set<T>& p) { os << "[ "; for (auto& it : p) os << it << " "; return os << "]"; } template <class S, class T> ostream& operator<<(ostream& os, const unordered_map<S, T>& p) { os << "[ "; for (auto& it : p) os << it << " "; return os << "]"; } template <class T> ostream& operator<<(ostream& os, const set<T>& p) { os << "[ "; for (auto& it : p) os << it << " "; return os << "]"; } template <class T> ostream& operator<<(ostream& os, const multiset<T>& p) { os << "[ "; for (auto& it : p) os << it << " "; return os << "]"; } template <class S, class T> ostream& operator<<(ostream& os, const map<S, T>& p) { os << "[ "; for (auto& it : p) os << it << " "; return os << "]"; } template <class T> void dbs(string str, T t) { cerr << str << " : " << t << "\n"; } template <class T, class... S> void dbs(string str, T t, S... s) { long long idx = str.find(','); cerr << str.substr(0, idx) << " : " << t << ","; dbs(str.substr(idx + 1), s...); } template <class T> void prc(T a, T b) { cerr << "["; for (T i = a; i != b; ++i) { if (i != a) cerr << ", "; cerr << *i; } cerr << "]\n"; } long long power(long long x, long long y) { long long res = 1; while (y) { if (y & 1) res = (res * x) % 1000000007; y = y / 2, x = (x * x) % 1000000007; } return res % 1000000007; } long long arr[1000001]; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, k; cin >> n >> k; long long x = (n * (n + 1)) / 2; if (x > k) { cout << "-1"; return 0; } k -= x; long long i; for (i = 1; i <= n; i++) arr[i] = i; i = 1; while (1) { if (i > n / 2) break; long long xtra = (n - i + 1 - i); if (k >= xtra) { swap(arr[i], arr[n - i + 1]); k -= xtra; } else { swap(arr[n - i + 1], arr[n - i + 1 - k]); break; } i++; } long long ans = 0; for (i = 1; i <= n; i++) ans += max(i, arr[i]); cout << ans << "\n"; for (i = 1; i <= n; i++) cout << arr[i] << " "; cout << "\n"; for (i = 1; i <= n; i++) cout << i << " "; }
16
#include <bits/stdc++.h> using namespace std; long long isprime[1234567]; long long minprime[1234567]; long long cost[1234567]; vector<long long> pf[1234567]; void prime() { long long i, j; isprime[0] = 0; isprime[1] = 0; for (long long i = 2; i <= 1e6; i++) isprime[i] = 1; for (i = 2; i <= 1e6; i++) { if (isprime[i] == 1) { pf[i].emplace_back(i); minprime[i] = i; for (j = 2 * i; j <= 1e6; j = j + i) { isprime[j] = 0; minprime[j] = i; pf[j].emplace_back(i); } } } } signed main() { prime(); for (long long i = 1; i <= 1e6; i++) { if (minprime[i] == i) cost[i] = i; else cost[i] = i - minprime[i] + 1; } long long x2; cin >> x2; long long cst = INT_MAX; for (auto it : pf[x2]) { for (long long i = x2 - it + 1; i <= x2; i++) { cst = min(cst, cost[i]); } } cout << cst; }
9
#include <bits/stdc++.h> using namespace std; template <class T> T abs(T x) { return x > 0 ? x : -x; } template <class T> T gcd(T a, T b) { return a ? gcd(b % a, a) : b; } template <class T> T sqr(T a) { return a * a; } template <class T> T sgn(T a) { return a > 0 ? 1 : (a < 0 ? -1 : 0); } int n; int m; long long divup(long long x, long long y) { if (y < 0) x *= -1, y *= -1; return x < 0 ? x / y : (x + y - 1) / y; } int main() { int f, T, t0; int a1, t1, p1; int a2, t2, p2; scanf("%d%d%d", &f, &T, &t0); scanf("%d%d%d", &a1, &t1, &p1); scanf("%d%d%d", &a2, &t2, &p2); if (t1 > t2) { swap(a1, a2); swap(t1, t2); swap(p1, p2); } long long A = (long long)a1 * (t1 - t0), B = (long long)a2 * (t2 - t0), C = T - (long long)f * t0; long long ansp = -1; if (C >= 0) ansp = 0; else if (A >= 0) ansp = -1; else if (A < 0) { A = -A; B = -B; C = -C; bool isfirst = true; for (long long y = 0; (y - 100) * a2 <= f; y++) { auto upd_ans = [&](long long x, long long y) { x = max(0ll, x); y = max(0ll, y); long long z = max(0ll, f - x * a1 - y * a2); long long curf = x * a1 + y * a2 + z, curt = x * a1 * t1 + y * a2 * t2 + z * t0; long long df = curf - f; bool ok = false; if (df >= 0) { if (x > 0) ok |= (df - a1 <= 0) && (curt - df * t1 <= T); if (y > 0) ok |= (df - a2 <= 0) && (curt - df * t2 <= T); if (z > 0) ok |= curt <= T; } long long p = x * p1 + y * p2; if (ok) { if (isfirst || ansp > p) ansp = p, isfirst = false; } }; long long x = max(0ll, divup(C - y * B, A)); for (int dx = -1; dx <= 1; dx++) for (int dy = -1; dy <= 1; dy++) upd_ans(x + dx, y + dy); } } cout << ansp << endl; return 0; }
15
#include <bits/stdc++.h> using namespace std; multiset<int> ms[35]; long long sum[35]; char c; int q, x, cnt = 0; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> q; for (int i = 1; i <= q; i++) { cin >> c >> x; int p = 0; if (c == '+') { ++cnt; for (int y = x; y > 0; y >>= 1, p++) continue; ms[p].insert(x); sum[p] += x; } else { --cnt; for (int y = x; y > 0; y >>= 1, p++) continue; ms[p].erase(ms[p].find(x)); sum[p] -= x; } long long cur = 0, res = 0; for (int i = 1; i <= 30; i++) { if (ms[i].size() && cur > 0) res += *ms[i].begin() > 2 * cur; cur += sum[i]; } if (cnt == 0) cout << 0 << endl; else cout << (cnt - res - 1) << endl; } return 0; }
20
#include <bits/stdc++.h> const int maxN = 2e5 + 2; int h, sum; bool flag; int a[maxN]; int main() { scanf("%d %d", &h, a); for (register int i = 1; i <= h; ++i) { scanf("%d", a + i); if (!flag) { if (a[i - 1] == 1 or a[i] == 1) continue; flag = true; } } if (!flag) { puts("perfect"); return 0; } printf("ambiguous\n0 "), sum = 1; for (register int i = 1; i <= h; ++i) { for (register int j = 1; j <= a[i]; ++j) printf("%d ", sum); sum += a[i]; } sum = 1, flag = false; printf("\n0 "); for (register int i = 1; i <= h; ++i) { if (!flag and a[i - 1] != 1 and a[i] != 1) { printf("%d ", sum - 1); for (register int j = 1; j < a[i]; ++j) printf("%d ", sum); flag = true; } else for (register int j = 1; j <= a[i]; ++j) printf("%d ", sum); sum += a[i]; } return 0; }
7
#include <bits/stdc++.h> using namespace std; struct EDGE { int to, next; } e[300010]; int n, head[150010], etop; long long a[150010]; void adde(int u, int v) { e[etop].to = v; e[etop].next = head[u]; head[u] = etop++; } long long ans; int vis[150010]; int rt, sz[150010], sum; void dfs(int x, int fa = 0) { sz[x] = 1; int i; int max_ = 0; for (i = head[x]; ~i; i = e[i].next) { if (e[i].to == fa) continue; if (vis[e[i].to]) continue; dfs(e[i].to, x); sz[x] += sz[e[i].to]; max_ = max(max_, sz[e[i].to]); } if (max(max_, sum - sz[x]) << 1 <= sum) rt = x; } struct seg { long long k, b; } s[600010]; void clear(int l, int r, int rt) { if (s[rt].k == 0 && s[rt].b == 0) return; s[rt].k = s[rt].b = 0; if (l == r) return; int m = l + r >> 1; clear(l, m, rt << 1); clear(m + 1, r, rt << 1 | 1); } inline long long calc(seg x, int pos) { return x.k * pos + x.b; } long long query(int pos, int l, int r, int rt) { if (s[rt].k == 0 && s[rt].b == 0) return calc(s[rt], pos); int m = l + r >> 1; long long ans = calc(s[rt], pos); if (pos <= m) ans = max(ans, query(pos, l, m, rt << 1)); else ans = max(ans, query(pos, m + 1, r, rt << 1 | 1)); return ans; } void query(int x, int fa, int c, long long s, long long f) { ++c; s += a[x]; f += s; int i; sz[x] = 1; for (i = head[x]; ~i; i = e[i].next) { if (e[i].to == fa) continue; if (vis[e[i].to]) continue; query(e[i].to, x, c, s, f); sz[x] += sz[e[i].to]; } if (sz[x] == 1) { ans = max(ans, query(c, 1, n, 1) + f); } } void update(seg c, int l, int r, int rt) { if (s[rt].k == 0 && s[rt].b == 0) { s[rt] = c; return; } if (calc(c, l) > calc(s[rt], l)) swap(c, s[rt]); if (calc(c, r) <= calc(s[rt], r)) return; if (l == r) return; int m = l + r >> 1; if (calc(c, m) > calc(s[rt], m)) { swap(c, s[rt]); update(c, l, m, rt << 1); } else { update(c, m + 1, r, rt << 1 | 1); } } void update(int x, int fa, int c, long long s, long long f) { ++c; s += a[x]; f += a[x] * c; int i; sz[x] = 1; for (i = head[x]; ~i; i = e[i].next) { if (e[i].to == fa) continue; if (vis[e[i].to]) continue; update(e[i].to, x, c, s, f); sz[x] += sz[e[i].to]; } if (sz[x] == 1) { update((seg){s, f}, 1, n, 1); } } vector<int> vec; void solve(int x) { dfs(x); sum = sz[x]; dfs(x); x = rt; vis[x] = 1; int i; vec.clear(); for (i = head[x]; ~i; i = e[i].next) { if (vis[e[i].to]) continue; vec.push_back(e[i].to); } clear(1, n, 1); update((seg){a[x], a[x]}, 1, n, 1); for (i = 0; i < vec.size(); ++i) { query(vec[i], x, 0, 0, 0); update(vec[i], x, 1, a[x], a[x]); } ans = max(ans, query(0, 1, n, 1)); clear(1, n, 1); update((seg){a[x], a[x]}, 1, n, 1); for (i = vec.size() - 1; i >= 0; --i) { query(vec[i], x, 0, 0, 0); update(vec[i], x, 1, a[x], a[x]); } for (i = head[x]; ~i; i = e[i].next) { if (vis[e[i].to]) continue; solve(e[i].to); } } int main() { memset(head, 255, sizeof(head)); ios::sync_with_stdio(0); cin.tie(0); cin >> n; int i; int u, v; for (i = 1; i < n; ++i) { cin >> u >> v; adde(u, v); adde(v, u); } for (i = 1; i <= n; ++i) { cin >> a[i]; } solve(1); cout << ans << '\n'; return 0; }
19
#include <bits/stdc++.h> using namespace std; int pr[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997}; int main(int argc, char **argv) { vector<int> numbers; int n; cin >> n; numbers.reserve(n); for (int i = 0; i < sizeof(pr) / sizeof(int); ++i) { int cpy = pr[i]; if (cpy > n) { break; } while (cpy <= n) { numbers.push_back(cpy); cpy *= pr[i]; } } cout << numbers.size() << endl; for (int i = 0; i < numbers.size(); ++i) { cout << numbers[i] << " "; } cout << endl; return 0; }
7
#include <bits/stdc++.h> const int N = 1e6 + 7; const int INF = 1e9 + 111; const double EPS = 1e-8; using namespace std; int main() { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; v[i]--; } bool f = 0; for (int i = 0; i < n; i++) { if (i == v[v[v[i]]] && v[v[i]] != i) f = 1; } if (f) cout << "YES"; else cout << "NO"; }
0
#include <bits/stdc++.h> using namespace std; bool comp(pair<char, int> a, pair<char, int> b) { return (a.second > b.second); } vector<int> factors(int n) { vector<int> v; for (int i = 1; i <= sqrt(n); i++) { if (n % i == 0) { if (n / i == i) v.push_back(i); else { v.push_back(i); v.push_back(n / i); } } } return v; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { long a, b, n; cin >> a >> b >> n; if (a < b) swap(a, b); long ctr = 0; if (a > n) { cout << 0 << endl; continue; } while (true) { if (a > b) { b += a; ctr++; if (b > n) break; a += b; ctr++; if (a > n) break; } else { a += b; ctr++; if (a > n) break; b += a; ctr++; if (b > n) break; } } cout << ctr << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; bool B, bb[109]; vector<pair<int, int> > a; vector<int> b[109]; void DFS(int x, int y) { if (x == y) B = true; for (int i = 0; i < b[x].size(); i++) { if (!bb[b[x][i]]) { bb[b[x][i]] = true; DFS(b[x][i], y); } } } int main() { int i, j, n, p, x, y, k; cin >> n; p = 0; for (i = 0; i < n; i++) { cin >> k; cin >> x >> y; B = false; if (k == 1) { a.push_back(make_pair(x, y)); for (j = 0; j < a.size(); j++) { if ((x > a[j].first && x < a[j].second) || (y > a[j].first && y < a[j].second)) b[p].push_back(j); if ((a[j].first > x && a[j].first < y) || (a[j].second > x && a[j].second < y)) b[j].push_back(p); } p++; } else { for (j = 0; j < n; j++) bb[j] = false; x--; y--; bb[x] = true; DFS(x, y); if (B) cout << "YES\n"; else cout << "NO\n"; } } return 0; }
7
#include <bits/stdc++.h> using namespace std; const int N = 100010; const int M = 2010; const int K = 670000; const int LIT = 2500; const int INF = 1 << 29; const int base = 137; const double eps = 1e-10; const int dir[5][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; const int dir2[10][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {0, -1}, {0, 1}}; const int ABS(int a) { return a > 0 ? a : -a; } int cnt[N]; int n, m, h, tot; vector<int> v; void init() { h--; tot = 0; memset(cnt, 0, sizeof(cnt)); v = vector<int>(m); for (int i = 0; i < m; i++) { scanf("%d", &v[i]); tot += v[i]; } } void solve() { if (tot < n) { cout << -1.0 << endl; return; } if (tot - v[h] < (n - 1)) { cout << 1.0 << endl; return; } for (int i = tot - v[h], j = n - 1; j > 0; i--, j--) cnt[i]++; for (int i = tot - 1, j = n - 1; j > 0; i--, j--) cnt[i]--; double res = 1.0; for (int i = 1; i < N; i++) { if (!cnt[i]) continue; if (cnt[i] > 0) res *= i; else res /= i; } res = 1 - res; printf("%.7lf\n", res); } int main() { while (cin >> n >> m >> h) { init(); solve(); } }
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int pos; int flag = 0; for (int i = 1; i < n; i++) { if (s[i] != s[i - 1]) { flag = 1; pos = i; break; } } if (flag == 1) { cout << "YES" << endl; cout << s[pos - 1] << s[pos] << endl; } else cout << "NO" << endl; }
2
#include <bits/stdc++.h> using namespace std; struct FastIO { inline FastIO& operator>>(int& x) { x = 0; char f = 0, ch = getchar(); while (ch > '9' || ch < '0') f |= (ch == '-'), ch = getchar(); while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar(); return x = (f ? -x : x), *this; } inline FastIO& operator>>(long long& x) { x = 0; char f = 0, ch = getchar(); while (ch > '9' || ch < '0') f |= (ch == '-'), ch = getchar(); while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar(); return x = (f ? -x : x), *this; } inline FastIO& operator>>(double& x) { x = 0; char f = 0, ch = getchar(); double d = 0.1; while (ch > '9' || ch < '0') f |= (ch == '-'), ch = getchar(); while (ch <= '9' && ch >= '0') x = x * 10 + ch - 48, ch = getchar(); if (ch == '.') { ch = getchar(); while (ch <= '9' && ch >= '0') x += d * (ch ^ 48), d *= 0.1, ch = getchar(); } return x = (f ? -x : x), *this; } } rin; const int N = 200050, M = 2000050, inf = 1 << 30; int head[N], to[M], nxt[M], tot, dep[N]; int Head[N], To[M], Nxt[M], Tot, f[N], top[N], siz[N], son[N]; int n, m, q, x, y, col, tmp; void add(int x, int y) { to[++tot] = y, nxt[tot] = head[x], head[x] = tot; } void Add(int x, int y) { To[++Tot] = y, Nxt[Tot] = Head[x], Head[x] = Tot; } int dfn[N], low[N], st[N], Top, k, vis[N]; void tarjan(int now) { dfn[now] = low[now] = ++k; vis[now] = 1, st[++Top] = now; for (int i = head[now]; i; i = nxt[i]) { if (!dfn[to[i]]) { tarjan(to[i]); low[now] = min(low[to[i]], low[now]); if (low[to[i]] >= dfn[now]) { ++col; do { tmp = st[Top--]; Add(col, tmp); Add(tmp, col); } while (tmp != to[i]); Add(col, now); Add(now, col); } } else if (vis[to[i]]) low[now] = min(low[now], dfn[to[i]]); } } void dfs1(int now, int fa) { siz[now] = 1, f[now] = fa, dep[now] = dep[fa] + 1; for (int i = Head[now]; i; i = Nxt[i]) { if (To[i] == fa) continue; dfs1(To[i], now); siz[now] += siz[To[i]]; if (siz[son[now]] < siz[To[i]]) son[now] = To[i]; } } void dfs2(int now, int tp) { top[now] = tp; if (!son[now]) return; dfs2(son[now], tp); for (int i = Head[now]; i; i = Nxt[i]) { if (To[i] == son[now] || To[i] == f[now]) continue; dfs2(To[i], To[i]); } } int lca(int x, int y) { while (top[x] != top[y]) { if (dep[top[x]] < dep[top[y]]) swap(x, y); x = f[top[x]]; } return dep[x] < dep[y] ? x : y; } int main() { rin >> n >> m >> q; col = n; for (int i = 1; i <= m; ++i) rin >> x >> y, add(x, y), add(y, x); tarjan(1); dfs1(1, 0); dfs2(1, 1); for (int i = 1; i <= q; ++i) { rin >> x >> y; int Lca = lca(x, y); printf("%d\n", (dep[x] + dep[y] - dep[Lca] * 2 + 1) >> 1); } }
15
#include <bits/stdc++.h> using namespace std; const int MAXN = 100 + 10; const int MAXM = 100 + 10; const int INF = 0x3f3f3f3f; void File(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } inline long long fread(void) { long long ret = 0, op = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') op = -1; for (; isdigit(ch); ch = getchar()) ret = (ret << 1) + (ret << 3) + ch - '0'; return ret * op; } long long n; string a; long long arr[MAXN]; inline void init(void) { n = fread(); cin >> a; for (long long i = 1; i <= n; ++i) arr[i] = a[i - 1] - 'A' + 1; } string gene; long long ans, g[MAXN]; inline void work(void) { gene = "ACTG"; ans = INF; for (long long i = 1; i <= 4; ++i) g[i] = gene[i - 1] - 'A' + 1; for (long long i = 1; i <= n - 3; ++i) { long long now = 0; for (long long j = 0; j <= 3; ++j) { now += min(abs(g[j + 1] - arr[i + j]), 26 - abs(g[j + 1] - arr[i + j])); } ans = min(ans, now); } cout << ans; } int main() { init(); work(); return 0; }
2
#include <bits/stdc++.h> #pragma warning(disable : 4996) #pragma comment(linker, "/STACK:666000000") using namespace std; const int INF = (1 << 30) - 1; const long double EPS = 1e-9; void ML(const bool v) { if (v) return; int *ass; for (;;) { ass = new int[2500000]; for (int i = 0; i < 2500000; i++) ass[i] = rand(); } } void TL(const bool v) { if (v) return; for (;;) cout << rand() % (rand() % 1000) << endl; } void PE(const bool v) { if (v) return; for (int i = 0; i < 10000; i++) printf("%c", rand() % 256); exit(0); } const int MX = 60; int a[105]; int n; int ps[18]; int pn = 0; int divs_msk[MX]; void pre() { bool was[MX]; memset(was, false, sizeof(was)); for (int i = 2; i < MX; i++) if (!was[i]) { ps[pn++] = i; for (int j = i; j < MX; j += i) was[j] = true; } for (int i = 1; i < MX; i++) { for (int j = 0; j < pn; j++) if (0 == i % ps[j]) divs_msk[i] = (divs_msk[i] | (1 << (j))); } } void LoAd() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; } int sv[105][(1 << 18) + 5]; int nxt[105][(1 << 18) + 5]; int go(const int pos, const int msk) { if (pos == n) return 0; int nmsk = 0; int &res = sv[pos][msk]; if (-1 != res) return res; res = INF; for (int i = 1; i < MX; i++) if (0 == (msk & divs_msk[i])) { int tmp = abs(i - a[pos]) + go(pos + 1, msk | divs_msk[i]); if (tmp < res) { res = tmp; nxt[pos][msk] = i; } } return res; } void print(const int pos, const int msk) { int i = nxt[pos][msk]; if (-1 != i) { printf("%d ", i); print(pos + 1, msk | divs_msk[i]); } } void SoLvE() { memset(sv, -1, sizeof(sv)); memset(nxt, -1, sizeof(nxt)); int res = go(0, 0); ; print(0, 0); } int main() { srand((int)time(NULL)); pre(); LoAd(); SoLvE(); return 0; }
12
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; string b; b = s; string t; int sz = s.size(); int ans = 0; while (s != t) { t = ""; t += b[sz - 1]; for (int i = 0; i < sz - 1; i++) { t += b[i]; } ans++; b = t; } cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int a[1000001]; bool used[1000001]; int main() { int n; cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; } int ans = -1; for (int i = n; i >= 1; --i) { if (!used[a[i]]) { ans = a[i]; used[a[i]] = true; } } cout << ans; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n; cin >> n; long long int a[n]; for (long long int i = 0; i < n; i++) cin >> a[i]; ; long long int ans = 0; long long int flag1 = 0, c = 0; for (int i = 1; i < n - 1; i++) { if (a[i] == 0 && a[i - 1] == 1 && a[i + 1] == 1) { a[i + 1] = 0; c++; } } cout << c; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int max_num = 1e3 + 10; bool adj_mat[max_num][max_num]; bool mark[max_num][max_num]; int n; void dfs_visit(int x, int y) { for (int i = 1; i < max_num; i++) if (adj_mat[i][y] && !mark[i][y]) { mark[i][y] = true; dfs_visit(i, y); } for (int i = 1; i < max_num; i++) if (adj_mat[x][i] && !mark[x][i]) { mark[x][i] = true; dfs_visit(x, i); } } int dfs() { int ans = 0; for (int i = 1; i < max_num; i++) for (int j = 1; j < max_num; j++) if (adj_mat[i][j] && !mark[i][j]) { ans++; mark[i][j] = true; dfs_visit(i, j); } return ans; } int main() { cin >> n; int a, b; for (int i = 0; i < n; i++) { cin >> a >> b; adj_mat[a][b] = true; } cout << dfs() - 1 << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; using ll = long long int; using ld = long double; const long double pi = 2 * acos(0.0); const int mod = 1e9 + 7; const int maxn = 2e5 + 5; const int e = 1e5 + 5; void solve() { ios::sync_with_stdio(false); cin.tie(0); unordered_map<char, int> map; string s; cin >> s; string a[2] = {"YES", "NO"}; int g = 1; for (int i = 0; i < s.length(); ++i) { map[s[i]]++; } if (map['A'] and map['B']) { while ((map['A'] > 0) and (map['B'] > 0)) { map['A']--; map['B']--; } if (map['B'] == map['C']) g = 0; else g = 1; } else if (map['C'] and map['B']) { while ((map['C'] >= 1) or (map['B'] >= 1)) { map['C']--; map['B']--; } if (map['B'] == map['A']) g = 0; else g = 1; } cout << a[g] << endl; return; } int main() { ios::sync_with_stdio(false); cin.tie(0); int T; cin >> T; while (T--) solve(); return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, p[100001], ans[100001], isok = true, a, b, tot; vector<int> g[100001]; bool vis[100001], can[2]; map<int, int> pos; void dfs(int x) { vis[x] = true; if (!can[0]) ans[x] = 0; else ans[x] = 1; for (int i = 0; i < g[x].size(); i++) if (!vis[g[x][i]]) dfs(g[x][i]); } void dfs(int x, int y) { vis[x] = true; ans[x] = y; for (int i = 0; i < g[x].size(); i++) if (!vis[g[x][i]]) dfs(g[x][i], y); else { if (ans[g[x][i]] != y) isok = false; } } int main() { scanf("%d%d%d", &n, &a, &b); for (int i = 1; i <= n; i++) { scanf("%d", &p[i]); pos[p[i]] = i; ans[i] = -1; } for (int i = 1; i <= n; i++) { if (!pos[b - p[i]] && !pos[a - p[i]]) { printf("NO\n"); return 0; } if (!pos[b - p[i]] && pos[a - p[i]]) { ans[i] = 0; can[0] = true; vis[i] = true; } if (!pos[a - p[i]] && pos[b - p[i]]) { ans[i] = 1; can[1] = true; vis[i] = true; } } for (int i = 1; i <= n; i++) { if (pos[b - p[i]]) { g[i].push_back(pos[b - p[i]]); } if (pos[a - p[i]]) { g[i].push_back(pos[a - p[i]]); } } for (int i = 1; i <= n; i++) if (vis[i]) { dfs(i, ans[i]); } for (int i = 1; i <= n; i++) if (!vis[i]) dfs(i); if (isok) { printf("YES\n"); for (int i = 1; i <= n; i++) printf("%d ", ans[i]); } else printf("NO\n"); return 0; }
12
#include <bits/stdc++.h> using namespace std; using namespace std; const int MedN = 100, MaxN = MedN << 1, MaxL = 120; const int NA = -1, MaxC = 0x3F3F3F3F; double a[MaxN + 1]; double f[2][MaxN + 1]; int n; int main(void) { int b, i, j, k, v; while (scanf(" %d", &n) != EOF) { for (i = 0; i <= n; i++) scanf(" %lf", &a[i]); b = 0; for (i = 0; i <= MaxN; i++) f[b][i] = -MaxC; for (i = 0; i <= n; i++) for (j = 0; j <= n; j++) { v = i + j - n; v += MedN; f[b][v] = max(f[b][v], (a[i] + a[j]) * 0.5); } for (k = 0; k < MaxL; k++) { b ^= 1; for (i = 0; i <= MaxN; i++) f[b][i] = -MaxC; for (i = 0; i <= MaxN; i++) for (j = 0; j <= MaxN; j++) { v = i + j - MaxN; v += MedN; if (v < 0 || v > MaxN) continue; f[b][v] = max(f[b][v], (f[!b][i] + f[!b][j]) * 0.5); } } printf("%.20lf\n", f[b][MedN]); } return 0; }
16
#include <bits/stdc++.h> using namespace std; int n; int idxs[500500]; long long vals[500500]; long long l[500500], r[500500]; bool cmp_idx(int a, int b) { return vals[a] < vals[b]; } void rem(int idx) { l[r[idx]] = l[idx]; r[l[idx]] = r[idx]; } int main() { while (cin >> n) { for (int i = 1; i <= n; ++i) { cin >> vals[i]; idxs[i] = i; l[i] = i - 1; r[i] = i + 1; } vals[0] = vals[n + 1] = 0; sort(idxs + 1, idxs + n + 1, cmp_idx); int l_idx = 1, r_idx = n; long long ans = 0; for (int i = 1; i <= n; ++i) { int idx = idxs[i]; ans += min(vals[l[idx]], vals[r[idx]]); if (l_idx == idx) { l_idx = r[idx]; } else if (r_idx == idx) { r_idx = l[idx]; } else { rem(idx); } } cout << ans << '\n'; } return 0; }
17
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int n, ans, tot, Q; struct node { int c, d; } a[maxn]; inline bool operator<(node a, node b) { return a.d < b.d; } priority_queue<node> q; int main() { scanf("%d", &Q); while (Q--) { scanf("%d", &n); int x, f; ans = tot = 0; for (int i = (1); i <= (n); ++i) a[i] = (node){0, 0}; for (int i = (1); i <= (n); ++i) scanf("%d%d", &x, &f), a[x].c++, a[x].d += f; sort(a + 1, a + n + 1, [](node x, node y) { return x.c > y.c; }); int j = 1; while (!q.empty()) q.pop(); for (int i = (n); i >= (1); --i) { while (j <= n && a[j].c >= i) q.push(a[j]), ++j; if (!q.empty()) { node x = q.top(); q.pop(); ans += i; tot += min(x.d, i); } } printf("%d %d\n", ans, tot); } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 105; int n; int zero[N], one[N]; string str; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> str; zero[0] = 0, one[0] = 0; str = " " + str; for (int i = 1; i < str.size(); i++) { if (str[i] == '1') { zero[i] = zero[i - 1]; one[i] = one[i - 1] + 1; } else if (str[i] == '0') { zero[i] = zero[i - 1] + 1; one[i] = one[i - 1]; } } for (int i = 0; i <= n; i++) { if (zero[n] != one[n]) { cout << 1 << endl; for (int i = 1; i < str.size(); i++) { cout << str[i]; } break; } else if (zero[i] != one[i] && zero[n] - zero[i] != one[n] - one[i]) { cout << 2 << endl; for (int j = 1; j <= i; j++) { cout << str[j]; } cout << " "; for (int j = i + 1; j < str.size(); j++) { cout << str[j]; } break; } } cout << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int n; bool check(int m) { long long v = (m - 1) / 2; if ((v + 1) * (m - v + 1) >= n || (v + 2) * (m - v) >= n) return true; else return false; } void xuly() { int t; t = 1; while (t--) { cin >> n; int p = ceil(sqrt(n)), q = floor(sqrt(n)); if (p * q >= n) cout << p + q << endl; else cout << p + p << endl; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); xuly(); }
3
#include <bits/stdc++.h> using namespace std; int main() { char str[3] = {'H', 'Q', '9'}, sen[1000]; int a = 1; cin >> sen; for (int i = 0; i < strlen(sen); i++) { for (int j = 0; j < 3; j++) { if (sen[i] == str[j]) { cout << "YES\n"; j = 4; i = strlen(sen); a = 0; } } } if (a == 1) cout << "NO\n"; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int Maxv = 100100; const int Inf = 1000000000; int m; char str[Maxv][10]; bool flag; int l; int ls[Maxv]; int rs[Maxv]; int len; bool vis[Maxv]; void dfs(int x) { if (x >= len) { flag = false; return; } vis[x] = true; l++; if (str[x][0] == 'p') { ls[x] = l; dfs(l); rs[x] = l; dfs(l); } } void dfsp(int x) { printf("%s", str[x]); if (str[x][0] == 'p') { printf("<"); dfsp(ls[x]); printf(","); dfsp(rs[x]); printf(">"); } } void Put2() { flag = true; l = 0; dfs(0); for (int i = 0; i < len; i++) if (!vis[i]) flag = false; if (!flag) { puts("Error occurred"); return; } dfsp(0); puts(""); } int main() { int i = 0, k; int n; scanf("%d", &n); while (~scanf("%s", str[i++])) ; len = i - 1; Put2(); return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { int t, candies[4], inter, i; cin >> t; while (t--) { cin >> candies[0] >> candies[1] >> candies[2]; sort(candies, candies + 3); inter = -candies[2] + candies[1] + candies[0]; if (inter < 0) cout << +candies[1] + candies[0]; else if (inter > 0) cout << candies[0] + candies[1] - (inter + 1) / 2; else if (inter == 0) cout << candies[2]; cout << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, m, q; vector<long long> adj[1 << 20]; vector<long long> g[1 << 20]; multiset<long long> st_[1 << 20]; vector<long long> X_[1 << 20]; vector<long long> XS[1 << 20]; long long p[1 << 20]; vector<long long> st[1 << 20]; map<pair<long long, long long>, double> cache; bool calced[1 << 20]; long long d[1 << 20]; long long l[1 << 20]; long long find(long long x) { return p[x] == x ? x : p[x] = find(p[x]); } long long D; long long dfs(long long u, long long p) { long long L = 0; st_[u].insert(0); g[u].resize(adj[u].size()); long long i = 0; for (long long v : adj[u]) { if (v != p) { g[u][i] = dfs(v, u) + 1; st_[u].insert(g[u][i]); L = max(g[u][i], L); } i++; } long long a = *st_[u].rbegin(); auto it = st_[u].rbegin(); it++; long long b = *it; D = max(D, a + b); return l[u] = L; } void dfs2(long long u, long long p, long long L) { long long i = 0; for (long long v : adj[u]) { if (v != p) { st_[u].erase(st_[u].find(g[u][i])); dfs2(v, u, max(L, *st_[u].rbegin()) + 1); st_[u].insert(g[u][i]); } i++; } l[u] = max(l[u], L); D = max(D, L + *st_[u].rbegin()); } void gao(long long rt) { if (!calced[rt]) { calced[rt] = true; D = 0; dfs(rt, -1); dfs2(rt, -1, 0); d[rt] = D; for (long long x : st[rt]) { X_[rt].push_back(l[x]); } sort(X_[rt].begin(), X_[rt].end()); XS[rt].resize(X_[rt].size()); for (long long i = X_[rt].size() - 1, sm = 0; i >= 0; i--) { sm += X_[rt][i]; XS[rt][i] = sm; } } } void gao(long long u, long long v) { if (u > v) swap(u, v); if (!cache.count((make_pair(u, v)))) { gao(u); gao(v); double& ans = cache[make_pair(u, v)]; long long D = max(d[u], d[v]); long long x = u, y = v; if (X_[x].size() > X_[y].size()) { swap(x, y); } vector<long long>& X = X_[x]; vector<long long>& Y = X_[y]; for (long long i = 0; i < X.size(); i++) { long long t = lower_bound(Y.begin(), Y.end(), D - X[i] - 1) - Y.begin(); ans += t * D + ((Y.size() - t) * (X[i] + 1) + XS[y][t]); } ans /= X.size(); ans /= Y.size(); } printf("%.8f\n", cache[make_pair(u, v)]); } int main() { scanf("%d%d%d", &n, &m, &q); for (long long i = 0; i < n; i++) p[i] = i; for (long long i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v), --u, --v; adj[u].push_back(v); adj[v].push_back(u); p[find(u)] = find(v); } for (long long i = 0; i < n; i++) st[find(i)].push_back(i); for (long long i = 0; i < q; i++) { int u, v; scanf("%d%d", &u, &v), --u, --v; u = find(u); v = find(v); if (u == v) { puts("-1"); } else { gao(u, v); } } }
17
#include <bits/stdc++.h> using namespace std; namespace zyt { const int N = 5e4 + 10; const double EPS = 1e-7, PI = acos(-1); bool equal(const double a, const double b) { return fabs(a - b) < EPS; } struct point { double x, y, theta; point(const double _x = 0, const double _y = 0) : x(_x), y(_y) { cal(); } void cal() { theta = atan2(y, x) + PI; } bool operator!=(const point &b) const { return !equal(x, b.x) || !equal(y, b.y); } }; double cross(const point &A, const point &B) { return A.x * B.y - B.x * A.y; } double area(const point &A, const point &B) { return fabs(cross(A, B)) / 2; } double sq(const double x) { return x * x; } bool internal(const double &k, const double &a, const double &b) { if (a <= b) return a <= k && k <= b; else return k >= a || k <= b; } struct line { point A, B; line(const point &a = point(), const point &b = point()) : A(a), B(b) { if (cross(A, B) < 0) swap(A, B); } bool operator<(const line &b) const; } L[N << 1]; point get(const line &L, const double theta) { double kk = tan(theta); if (equal(L.A.x, L.B.x)) return point(L.A.x, kk * L.A.x); else { double k = (L.A.y - L.B.y) / (L.A.x - L.B.x), b = L.A.y - k * L.A.x; return point(b / (kk - k), kk * b / (kk - k)); } } double dis(const point &a) { return sqrt(sq(a.x) + sq(a.y)); } double dis(const line &L, const double theta) { return dis(get(L, theta)); } bool line::operator<(const line &b) const { if (A != b.A && A != b.B && internal(A.theta, b.A.theta, b.B.theta)) return dis(*this, A.theta) < dis(b, A.theta); else if (B != b.A && B != b.B && internal(B.theta, b.A.theta, b.B.theta)) return dis(*this, B.theta) < dis(b, B.theta); else if (b.A != A && b.A != B && internal(b.A.theta, A.theta, B.theta)) return dis(*this, b.A.theta) < dis(b, b.A.theta); else if (b.B != A && b.B != B && internal(b.B.theta, A.theta, B.theta)) return dis(*this, b.B.theta) < dis(b, b.B.theta); else return false; } set<line> s; int n, m, cnt; double d[N]; vector<int> in[N], out[N]; int unique(double *const d, const int n) { int cnt = 0; d[cnt++] = d[0]; for (int i = 1; i < n; i++) if (!equal(d[i], d[cnt - 1])) d[cnt++] = d[i]; return cnt; } int find(const double x) { int p = lower_bound(d, d + cnt, x) - d; return p - (!equal(d[p], x) ? 1 : 0); } int work() { scanf("%d", &n); d[cnt++] = 0; d[cnt++] = 2 * PI - EPS; for (int i = 0; i < n; i++) { int k; scanf("%d", &k); point last, first; scanf("%lf%lf", &first.x, &first.y); first.cal(); last = first; while (k--) { point p; if (k) { scanf("%lf%lf", &p.x, &p.y); p.cal(); } else p = first; if (!equal(p.theta, last.theta)) { d[cnt++] = p.theta; line tmp = line(last, p); if (tmp.A.theta < tmp.B.theta) L[m++] = tmp; else { L[m++] = line(get(tmp, 2 * PI - EPS), tmp.A); L[m++] = line(get(tmp, 0), tmp.B); } } last = p; } } sort(d, d + cnt); cnt = unique(d, cnt); for (int i = 0; i < m; i++) { int a = find(L[i].A.theta), b = find(L[i].B.theta); if (a > b) fprintf(stderr, "gg1"); else if (a < b) in[a].push_back(i), out[b].push_back(i); } double ans = 0; for (int i = 0; i < cnt - 1; i++) { for (auto j : out[i]) s.erase(L[j]); for (auto j : in[i]) s.insert(L[j]); if (s.size() >= 2) { if (s.size() < 2) fprintf(stderr, "gg2"); line a = *s.begin(), b = *++s.begin(); ans += area(get(b, d[i]), get(b, d[i + 1])) - area(get(a, d[i]), get(a, d[i + 1])); } } printf("%.6f", ans); return 0; } } // namespace zyt int main() { return zyt::work(); }
22
#include <bits/stdc++.h> using namespace std; int n, a, b, t, dp[1000000 + 10], dp1[1000000 + 10], dp2[1000000 + 10], l, r, c, tren, br, maxx; char c1; int main() { scanf("%d %d %d %d\n", &n, &a, &b, &t); for (int i = 1; i <= n; i++) { scanf("%c", &c1); if (c1 == 'w') dp[i] = 1 + a + b; else dp[i] = 1 + a; } dp[1] -= a; for (int i = 1; i <= n; i++) dp1[i] = dp1[i - 1] + dp[i]; for (int i = n; i >= 1; i--) dp2[i] = dp2[i + 1] + dp[i]; for (int i = 1; i <= n; i++) { br = i; tren = dp1[i]; if (tren <= t) maxx = max(maxx, i); tren += (i - 1) * a; l = 0; r = n + 1; while (r - l > 1) { c = (r + l) / 2; if (dp2[c] + tren <= t) r = c; else l = c; } if (dp2[r] + tren <= t) maxx = max(maxx, i + n - r + 1); } for (int i = 1; i <= n; i++) { br = i; tren = dp2[n - i + 2]; if (tren + dp[1] <= t) maxx = max(maxx, i); tren += (i - 1) * a; l = 0; r = n + 1; while (r - l > 1) { c = (r + l) / 2; if (dp1[c] + tren <= t) l = c; else r = c; } if (dp1[l] + tren <= t) maxx = max(maxx, i + l - 1); } maxx = min(maxx, n); printf("%d\n", maxx); return 0; }
11
#include <bits/stdc++.h> using namespace std; int n, m, a[111], x[111], saven; long double f[2][200111], sum[2][200111]; int main() { ios ::sync_with_stdio(0); cin.tie(0); cout << (fixed) << setprecision(12); while (cin >> n >> m) { int sumA = 0; for (int i = (1), _b = (n); i <= _b; ++i) { int x; cin >> x; --x; a[i] = x; sumA += x; } if (m == 1) { cout << 1.0 << endl; continue; } for (int i = (1), _b = (n); i <= _b; ++i) { int cur = i % 2; memset(f[cur], 0, sizeof f[cur]); if (i == 1) { for (int val = 0, _a = (m * i); val < _a; ++val) if (val < m && val != a[i]) f[cur][val] = 1; } else { for (int val = 0, _a = (m * i); val < _a; ++val) { f[cur][val] = sum[1 - cur][val]; if (val >= m) f[cur][val] -= sum[1 - cur][val - m]; if (val >= a[i]) f[cur][val] -= f[1 - cur][val - a[i]]; } } for (int val = 0, _a = (m * (i + 1)); val < _a; ++val) { sum[cur][val] = f[cur][val]; if (val > 0) sum[cur][val] += sum[cur][val - 1]; } } long double t = sum[n % 2][sumA - 1]; for (int turn = 0, _a = (n); turn < _a; ++turn) t /= (m - 1); cout << t * (m - 1.0) + 1 << endl; } }
15
#include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while (T--) { string str; cin >> str; if (str.length() <= 10) cout << str << endl; else cout << str[0] << str.length() - 2 << str[str.length() - 1] << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000 * 1000 + 10; const long long INF = 1LL * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 10; int arr[600]; vector<int> barg, tool; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, one = 0; cin >> n; long long sum = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; sum += arr[i]; if (arr[i] == 1) { one++; barg.push_back(i); } else { tool.push_back(i); } } if (sum / 2 < n - 1) { cout << "NO" << endl; return 0; } int d = tool.size() - 1; if (one == 1) d++; if (one >= 2) d += 2; cout << "YES" << ' ' << d << endl << n - 1 << endl; for (int i = 1; i < tool.size(); i++) { cout << tool[i] + 1 << ' ' << tool[i - 1] + 1 << endl; arr[tool[i]]--; arr[tool[i - 1]]--; } if (one == 0) return 0; cout << barg.back() + 1 << ' ' << tool.back() + 1 << endl; arr[tool.back()]--; barg.pop_back(); if (one == 1) return 0; int ind = 0; while (barg.size() != 0) { while (arr[tool[ind]] == 0) ind++; cout << barg.back() + 1 << ' ' << tool[ind] + 1 << endl; arr[tool[ind]]--; barg.pop_back(); } return 0; }
10
#include <bits/stdc++.h> using namespace std; using ll = long long; template <long long mod> struct modular { long long value; modular(long long x = 0) { value = x % mod; if (value < 0) value += mod; } modular& operator+=(modular other) { if ((value += other.value) >= mod) value -= mod; return *this; } modular& operator-=(modular other) { if ((value -= other.value) < 0) value += mod; return *this; } modular& operator*=(modular other) { value = value * other.value % mod; return *this; } modular& operator/=(modular other) { long long a = 0, b = 1, c = other.value, m = mod; while (c != 0) { long long t = m / c; m -= t * c; swap(c, m); a -= t * b; swap(a, b); } a %= mod; if (a < 0) a += mod; value = value * a % mod; return *this; } modular operator-() { return modular(-value); } modular operator+(modular rhs) { return modular(*this) += rhs; } modular operator-(modular rhs) { return modular(*this) -= rhs; } modular operator*(modular rhs) { return modular(*this) *= rhs; } modular operator/(modular rhs) { return modular(*this) /= rhs; } bool operator==(modular rhs) { return value == rhs.value; } bool operator!=(modular rhs) { return value != rhs.value; } bool operator<(modular rhs) { return value < rhs.value; } }; template <long long mod> string to_string(modular<mod> x) { return to_string(x.value); } template <long long mod> ostream& operator<<(ostream& stream, modular<mod> x) { x.value %= mod; if (x.value < 0) x.value += mod; return stream << x.value; } template <long long mod> istream& operator>>(istream& stream, modular<mod>& x) { stream >> x.value; x.value %= mod; if (x.value < 0) x.value += mod; return stream; }; const long long mod = (long long)1e9 + 7; using mint = modular<mod>; inline mint pw(mint a, long long n) { mint res = 1; while (n > 0) { if (n & 1) { res *= a; } a *= a; n >>= 1; } return res; } vector<mint> fact, finv; inline void cinit(int n) { fact.resize(n, 1); finv.resize(n, 1); for (int i = 2; i < n; i++) { fact[i] = fact[i - 1] * i; } finv[n - 1] /= fact[n - 1]; for (int i = n - 2; i >= 2; i--) { finv[i] = finv[i + 1] * (i + 1); } } inline mint C(int n, int k) { if (n < k || k < 0 || n < 0) return 0; return fact[n] * finv[k] * finv[n - k]; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<vector<int>> g(n); vector<pair<int, int>> edges(m); for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; x--, y--; g[x].emplace_back(i); g[y].emplace_back(i); edges[i] = {x, y}; } vector<int> d(n, 1e9); d[0] = 0; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; pq.emplace(d[0], 0); while (!pq.empty()) { int v = pq.top().second; if (pq.top().first != d[v]) { pq.pop(); continue; } pq.pop(); for (int id : g[v]) { int to = v ^ edges[id].first ^ edges[id].second; int cost = 0; int tmp = id + 1; while (tmp) { cost++; tmp /= 10; } if (d[v] + cost < d[to]) { d[to] = d[v] + cost; pq.emplace(d[to], to); } } } vector<vector<int>> ng(n); vector<pair<int, int>> nedges; vector<int> cost; for (int i = 0; i < m; i++) { int x, y; tie(x, y) = edges[i]; if (d[x] > d[y]) { swap(x, y); } int c = 0; int tmp = i + 1; while (tmp) { c++; tmp /= 10; } if (d[y] - d[x] != c) { continue; } string s = to_string(i + 1); int from = x; int to = ng.size(); for (int j = 0; j < s.size(); j++) { if (j == s.size() - 1) { to = y; } else { ng.emplace_back(); } ng[from].emplace_back(nedges.size()); nedges.emplace_back(from, to); cost.emplace_back(s[j] - '0'); from = to; to++; } } swap(g, ng); swap(edges, nedges); ; m = g.size(); vector<mint> ans(m); vector<bool> was(m); function<void(vector<int>, mint)> dfs = [&](vector<int> vs, mint val) { for (int i : vs) { ans[i] = val; } mint tmp = val; vector<vector<int>> nxt(10); for (int c = 0; c < 10; c++) { for (int v : vs) { for (int id : g[v]) { int to = edges[id].second; if (!was[to] && cost[id] == c) { was[to] = true; nxt[c].emplace_back(to); } } } } for (int i = 0; i < 10; i++) { if (nxt[i].empty()) { continue; } dfs(nxt[i], val * 10 + i); } }; was[0] = true; dfs(vector<int>(1, 0), 0); for (int i = 1; i < n; i++) { cout << ans[i] << '\n'; }; return 0; }
18
#include <bits/stdc++.h> using namespace std; int main() { int N, K; cin >> N >> K; char address[200005]; scanf("%s", address); vector<int> indexes[26]; for (int i = (0); i < (K); i++) { indexes[address[i] - 'a'].push_back(i); } while (N--) { long long result = 0; char potential[200005]; scanf("%s", potential); int len = strlen(potential); for (int i = (0); i < (len); i++) { vector<int>& arr = indexes[potential[i] - 'a']; if (arr.empty()) { result += len; continue; } int dif = 987654321; for (int j = (0); j < (arr.size()); j++) { dif = min(dif, abs(i - arr[j])); } result += dif; } cout << result << endl; } }
10
#include <bits/stdc++.h> using namespace std; const int maxn = 500010; int n, ans, fa[2][maxn], par[maxn]; vector<int> G[2][maxn]; int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); } int main() { scanf("%d", &n); for (int c : {0, 1}) { for (int _ = 0, u, v; _ < n - 1; _++) { scanf("%d %d", &u, &v); G[c][u].push_back(v), G[c][v].push_back(u); } function<void(int)> dfs = [&](int v) { for (int u : G[c][v]) if (u ^ fa[c][v]) { fa[c][u] = v, dfs(u); } }; dfs(1); } par[1] = 1; for (int i = 2; i <= n; i++) { if (fa[1][i] == fa[0][i] || fa[0][fa[1][i]] == i) par[i] = fa[1][i]; else ans++, par[i] = i; } function<void(int)> dfs = [&](int v) { for (int u : G[0][v]) if (u ^ fa[0][v]) { dfs(u); if (u != fa[1][v] && v != fa[1][u]) { printf("%d %d %d %d\n", u, v, find(u), fa[1][find(u)]); } } }; printf("%d\n", ans), dfs(1); return 0; }
24
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long int t, n; cin >> t; while (t--) { cin >> n; long long int a[n]; for (auto i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n, greater<long long int>()); for (auto i = 0; i < n; i++) { if (i + 1 == a[i]) { cout << i + 1 << endl; break; } else if (i + 1 > a[i]) { cout << i << endl; break; } } } return 0; }
0