func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; int main() { string s; int i, j, k, p, len, n; cin >> n >> k >> s; int arr[30] = {0}; char x = a ; for (i = 0; i < 26; i++) { x = a + i; p = 0; for (j = 0; j < n; j++) { if (s[j] == x) p++; else p = 0; if (p == k) { arr[i]++; p = 0; } } } p = 0; for (i = 0; i < 26; i++) { p = max(p, arr[i]); } cout << p << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; int n; int f[MAXN]; bool read() { if (scanf( %d , &n) < 1) { return 0; } for (int i = 0; i < int(n); ++i) { scanf( %d , &f[i]); --f[i]; } return 1; } vector<int> inv_f[MAXN]; int g[MAXN]; int h[MAXN]; bool solve() { for (int i = 0; i < int(n); ++i) { inv_f[i].clear(); } for (int i = 0; i < int(n); ++i) { inv_f[f[i]].push_back(i); } memset(g, -1, sizeof g); int m = 0; for (int i = 0; i < int(n); ++i) { if (inv_f[i].empty()) { continue; } if (f[i] != i) { return 0; } for (int j : inv_f[i]) { g[j] = m; } h[m] = i; ++m; } for (int x = 0; x < int(m); ++x) { assert(g[h[x]] == x); } for (int x = 0; x < int(n); ++x) { assert(h[g[x]] == f[x]); } printf( %d n , m); for (int i = 0; i < int(n); ++i) { printf( %d , g[i] + 1); } puts( ); for (int i = 0; i < int(m); ++i) { printf( %d , h[i] + 1); } puts( ); return 1; } int main() { while (read()) { if (!solve()) { puts( -1 ); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX = (1 << 22) + 5; int full, n, m; bool v[MAX], b[MAX], h[MAX]; void dfsans(int now); void dfs(int now) { if (v[now]) return; v[now] = true; if (h[now]) dfsans(now); for (int i = 0; i < n; i++) { if (!(now & (1 << i))) continue; dfs(now ^ (1 << i)); } } void dfsans(int now) { if (b[now]) return; b[now] = true; dfs(now ^ full); } int main() { scanf( %d%d , &n, &m); full = (1 << n) - 1; for (int i = 0; i < m; i++) { int x; scanf( %d , &x); h[x] = true; } int ans = 0; for (int i = 0; i < (full + 1); i++) { if (!h[i] || b[i]) continue; ans++; dfsans(i); } printf( %d n , ans); }
#include <bits/stdc++.h> using namespace std; signed main() { long long int t; cin >> t; while (t--) { long long int n; cin >> n; long long int a[n]; for (long long int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } vector<long long int> pos(n); for (long long int i = 0; i < n; i++) { pos[a[i]] = i; } long long int mnpos = pos[0]; long long int mx = pos[0]; vector<long long int> ans(n); for (long long int i = 0; i < n; i++) { mnpos = min(mnpos, pos[i]); mx = max(mx, pos[i]); if (mx - mnpos == i) { ans[i] = 1; } else { ans[i] = 0; } } for (long long int x : ans) cout << x; cout << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, a, x, b, y; cin >> n >> a >> x >> b >> y; while (1) { if (a == b) { cout << YES n ; return 0; } if (a == x || b == y) { break; } if (++a > n) { a = 1; } if (--b < 1) { b = n; } } cout << NO n ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int tmp1 = 0, tmp2 = 0, i, j, n, t, p[2 * 1000001] = {}; cin >> n; set<int> a; for (i = 0; i < n; i++) { cin >> t; a.insert(t); } for (i = 0; i < 2 * 1000001; i++) p[i] = 1000001; for (int v : a) p[v] = 0; for (i = 1; i < 2 * 1000001; i++) if (p[i]) p[i] = p[i - 1] + 1; for (int v : a) for (j = 2 * v; j < 2 * 1000001; j += v) tmp1 = max(tmp1, (j - 1 - p[j - 1] < v) ? 0 : (v - ((p[j - 1] + 1) % v)) % v); cout << tmp1; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 200000; int c[MAXN + 10], d[MAXN + 10], l[MAXN + 10], r[MAXN + 10]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> x(n); for (int i = 0; i < n; ++i) { cin >> x[i]; ++c[x[i]]; } for (int i = 1; i <= MAXN; ++i) { if (c[i] == 0) continue; if (c[i] == 1) { d[i] = 0; l[i] = 1; } else { d[i] = c[i]; l[i] = d[i] + l[i - 1]; } } memset(d, 0, sizeof(d)); for (int i = MAXN; i >= 1; --i) { if (c[i] == 0) continue; if (c[i] == 1) { d[i] = 0; r[i] = 1; } else { d[i] = c[i]; r[i] = d[i] + r[i + 1]; } } int ans = 0, p = -1; for (int i = 1; i <= MAXN; ++i) { if (l[i] + r[i] - c[i] > ans) { ans = l[i] + r[i] - c[i]; p = i; } } if (ans == 1) { for (int i = 1; i + 1 <= MAXN; ++i) { if (c[i] > 0 && c[i + 1] > 0) { cout << 2 << endl; cout << i << << i + 1 << endl; return 0; } } cout << ans << endl; cout << p << endl; return 0; } vector<int> cir; int q = p; while (c[p] > 1) { cir.push_back(p); --c[p]; ++p; } while (p > q) { while (c[p] > 0) { cir.push_back(p); --c[p]; } --p; } cir.push_back(p); --c[p]; --p; while (c[p] > 1) { cir.push_back(p); --c[p]; --p; } while (p <= q) { while (c[p] > 0) { cir.push_back(p); --c[p]; } ++p; } cout << ans << endl; for (int i : cir) cout << i << ; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n, p, q; pair<int, int> pro[100010]; long long cross(int x1, int y1, int x2, int y2) { return x1 * (long long)y2 - x2 * (long long)y1; } double cross2(double x1, double y1, double x2, double y2) { return x1 * y2 - x2 * y1; } bool check(pair<int, int> c, pair<int, int> b, pair<int, int> a) { return cross(b.first - a.first, b.second - a.second, c.first - b.first, c.second - b.second) >= 0; } pair<int, int> st[100010]; int tp = -1; int main() { scanf( %d%d%d , &n, &p, &q); int maxa = 0, maxb = 0; for (int i = 1; i <= n; i++) { scanf( %d%d , &pro[i].first, &pro[i].second); maxa = max(maxa, pro[i].first); maxb = max(maxb, pro[i].second); } sort(pro + 1, pro + n + 1); st[++tp] = make_pair(0, maxb); for (int i = 1; i <= n; i++) { while (tp > 0 && check(pro[i], st[tp], st[tp - 1])) tp--; st[++tp] = pro[i]; } st[++tp] = make_pair(maxa, 0); int line; for (int i = 0; i < tp; i++) { if ((atan2(q, p) - atan2(st[i + 1].second, st[i + 1].first) >= -1e-9) && (atan2(st[i].second, st[i].first) - atan2(q, p) >= -1e-9)) line = i; } double lo = 0, hi = 1e6; while (lo < hi - 1e-9) { double mi = (lo + hi) / 2; if ((0 - cross2(p / mi - st[line].first, q / mi - st[line].second, p / mi - st[line + 1].first, q / mi - st[line + 1].second) >= -1e-9)) hi = mi; else lo = mi; } printf( %.10f n , lo); return 0; }
#include <bits/stdc++.h> using namespace std; int n, t, x, c = 0, value[1000001], sign[1000001]; stack<int> s; int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %d , &value[i]); sign[i] = 1; } scanf( %d , &t); for (int i = 1; i <= t; i++) { scanf( %d , &x); sign[x] = -1; } for (int i = n; i >= 1; i--) { if (sign[i] == -1) { s.push(i); } else { if (!s.empty() && value[i] == value[s.top()]) { s.pop(); } else { s.push(i); sign[i] = -1; } } } if (!s.empty()) { printf( NO ); } else { printf( YES n ); for (int i = 1; i <= n; i++) { printf( %d , value[i] * sign[i]); } } return 0; }
#include <bits/stdc++.h> long double PI = 3.141592653589793238462643383279502884197; using namespace std; int b[1001][1001]; int r[1001][1001], rn[1001][1001]; int c[1001][1001], cn[1001][1001]; int main() { int n, m; scanf( %d %d , &n, &m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { scanf( %d , &b[i][j]); r[i][j] = c[j][i] = b[i][j]; } } for (int i = 0; i < n; i++) { sort(r[i], r[i] + m); rn[i][0] = 1; for (int j = 1; j < m; j++) rn[i][j] = rn[i][j - 1] + (r[i][j] == r[i][j - 1] ? 0 : 1); } for (int i = 0; i < m; i++) { sort(c[i], c[i] + n); cn[i][0] = 1; for (int j = 1; j < n; j++) cn[i][j] = cn[i][j - 1] + (c[i][j] == c[i][j - 1] ? 0 : 1); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int ii = lower_bound(r[i], r[i] + m, b[i][j]) - r[i]; int jj = lower_bound(c[j], c[j] + n, b[i][j]) - c[j]; if (rn[i][ii] > cn[j][jj]) printf( %d , max(rn[i][m - 1], cn[j][n - 1] + rn[i][ii] - cn[j][jj])); else printf( %d , max(cn[j][n - 1], rn[i][m - 1] + cn[j][jj] - rn[i][ii])); } printf( n ); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int i, j, a = 0, b = 0, c, k, n; std::vector<int> v; cin >> n; for (i = 0; i < n; i++) { cin >> k; for (j = 0; j < k / 2; j++) cin >> c, a += c; if (k & 1) cin >> c, v.push_back(c); for (j = 0; j < k / 2; j++) cin >> c, b += c; } sort(v.begin(), v.end(), greater<int>()); for (i = 0; i < v.size(); i++) if (i & 1) b += v[i]; else a += v[i]; cout << a << << b; }
#include <bits/stdc++.h> struct sur { int c1, c2, c3, c4; inline bool colsame() { return c1 == c3 && c2 == c4 && c1 != c2; } inline bool same() { return c1 == c2 && c2 == c3 && c3 == c4; } inline void right_rotate() { int t = c4; c4 = c2; c2 = c1; c1 = c3; c3 = t; return; } inline void left_rotate() { int t = c4; c4 = c3; c3 = c1; c1 = c2; c2 = t; return; } inline void rotate() { int t = c1; c1 = c4; c4 = t; t = c2; c2 = c3; c3 = t; return; } inline void read() { scanf( %d%d%d%d , &c1, &c2, &c3, &c4); return; } friend inline bool round(sur s1, sur s2, sur s3, sur s4) { if (!(s1.colsame() && s2.colsame() && s3.colsame() && s4.colsame())) return 0; if (s1.c1 == s2.c2 && s2.c1 == s3.c2 && s3.c1 == s4.c2 && s4.c1 == s1.c2) return 1; if (s1.c2 == s2.c1 && s2.c2 == s3.c1 && s3.c2 == s4.c1 && s4.c2 == s1.c1) return 1; return 0; } } code[7]; template <class Tp> inline void swap(Tp &a, Tp &b) { Tp t = a; a = b; b = t; return; } int main() { int i; for (i = 1; i <= 6; i++) code[i].read(); if (code[4].same() && code[5].same()) { code[6].rotate(); puts(round(code[1], code[2], code[3], code[6]) ? YES : NO ); } else if (code[1].same() && code[3].same()) { code[4].right_rotate(); code[2].right_rotate(); code[5].right_rotate(); code[6].right_rotate(); puts(round(code[4], code[2], code[5], code[6]) ? YES : NO ); } else if (code[2].same() && code[6].same()) { code[1].right_rotate(); code[3].left_rotate(); code[4].rotate(); puts(round(code[4], code[1], code[5], code[3]) ? YES : NO ); } else puts( NO ); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; int dp[17][1 << 16][2][2]; int main() { ios::sync_with_stdio(0); cin.tie(0); for (int i = 0; i < 17; i++) { for (int j = 0; j < (1 << 16); j++) { for (int k = 0; k < 2; k++) { for (int l = 0; l < 2; l++) { dp[i][j][k][l] = -inf; } } } } int n, k; cin >> n >> k; vector<int> good(1 << n); while (k--) { int x; cin >> x; x--; good[x] = true; } for (int i = 0; i < (1 << n); i += 2) { dp[0][i / 2][good[i]][good[i + 1]] = max(dp[0][i / 2][good[i]][good[i + 1]], good[i] | good[i + 1]); dp[0][i / 2][good[i + 1]][good[i]] = max(dp[0][i / 2][good[i + 1]][good[i]], good[i] | good[i + 1]); } for (int l = 0; l + 1 < n; l++) { int p = (1 << n) / (1 << (l + 1)); for (int i = 0; i < p; i += 2) { for (int u1 = 0; u1 < 2; u1++) { for (int d1 = 0; d1 < 2; d1++) { for (int u2 = 0; u2 < 2; u2++) { for (int d2 = 0; d2 < 2; d2++) { int d = (d1 | d2); if (d) { int u = u1 | u2; int m = 2 + u; dp[l + 1][i / 2][u][d] = max(dp[l + 1][i / 2][u][d], dp[l][i][u1][d1] + dp[l][i + 1][u2][d2] + m); } else { if (u1 & u2) { dp[l + 1][i / 2][1][1] = max(dp[l + 1][i / 2][1][1], dp[l][i][u1][d1] + dp[l][i + 1][u2][d2] + 2); } else if (u1 | u2) { dp[l + 1][i / 2][1][0] = max(dp[l + 1][i / 2][1][0], dp[l][i][u1][d1] + dp[l][i + 1][u2][d2] + 1); dp[l + 1][i / 2][0][1] = max(dp[l + 1][i / 2][0][1], dp[l][i][u1][d1] + dp[l][i + 1][u2][d2] + 2); } else { dp[l + 1][i / 2][0][0] = max(dp[l + 1][i / 2][0][0], dp[l][i][u1][d1] + dp[l][i + 1][u2][d2]); } } } } } } } } int ans = 0; for (int u = 0; u < 2; u++) { for (int d = 0; d < 2; d++) { ans = max(ans, dp[n - 1][0][u][d] + (u | d)); } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct Team { string name; int fi, se, th; } t[60]; int cmp(Team a, Team b) { if (a.fi != b.fi) return a.fi > b.fi; else if (a.se != b.se) return a.se > b.se; else return a.th > b.th; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> t[i].name; t[i].fi = t[i].se = t[i].th = 0; } for (int i = 0; i < n * (n - 1) / 2; i++) { string te, t1, t2; int a, b; char ch; cin >> te >> a >> ch >> b; t1 = te.substr(0, te.find( - )); t2 = te.substr(te.find( - ) + 1); for (int j = 0; j < n; j++) { if (t[j].name == t1) { if (a > b) t[j].fi += 3; else if (a == b) t[j].fi += 1; t[j].se += a - b; t[j].th += a; } if (t[j].name == t2) { if (a < b) t[j].fi += 3; else if (a == b) t[j].fi += 1; t[j].se += b - a; t[j].th += b; } } } string ans[30]; sort(t, t + n, cmp); for (int i = 0; i < n / 2; i++) ans[i] = t[i].name; sort(ans, ans + n / 2); for (int i = 0; i < n / 2; i++) cout << ans[i] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-6; const double pi = 3.14159265358979323846264338; inline long long multi(long long x, long long y) { long long tmp = (x * y - (long long)((long double)x / 998244353 * y + 1.0e-8) * 998244353); return tmp < 0 ? tmp + 998244353 : tmp; } long long _ceil(long long a, long long b); long long _floor(long long a, long long b) { return b < 0 ? _floor(-a, -b) : a < 0 ? -_ceil(-a, b) : a / b; } long long _ceil(long long a, long long b) { return b < 0 ? _ceil(-a, -b) : a < 0 ? -_floor(-a, b) : (a + b - 1) / b; } long long exgcd(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return a; } long long d = exgcd(b, a % b, y, x); y -= x * (a / b); return d; } int32_t main() { ios ::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long a1, a2, b1, b2, x0, y0; long long l, r; cin >> a1 >> b1 >> a2 >> b2 >> l >> r; l = max(0ll, _ceil(l - b1, a1)); r = _floor(r - b1, a1); if (l > r) { cout << 0 << n ; return 0; } long long g = exgcd(a1, -1 * a2, x0, y0); long long m = b2 - b1; if (m % g) { cout << 0 << n ; return 0; } if (g < 0) { g *= -1; x0 *= -1; y0 *= -1; } long long L = _ceil(r * g - x0 * m, -a2); long long R = _floor(l * g - x0 * m, -a2); R = min(R, _floor(y0 * m, a1)); long long ans = max(0ll, R - L + 1); cout << ans << n ; return 0; }
#include <bits/stdc++.h> using namespace std; struct yts { long long dp[210][210]; } a, ans, t; int ch[210][26], fail[210], q[210], root = 1; long long w[210], b[210]; int n, m, tot = 1; char s[210]; long long L; void insert(int id) { int x = root, len = strlen(s + 1); for (int i = 1; i <= len; i++) { int t = s[i] - a ; if (!ch[x][t]) ch[x][t] = ++tot; x = ch[x][t]; } w[x] += b[id]; } void build_fail() { int l = 0, r = 0; for (int i = 0; i < 26; i++) if (ch[root][i]) fail[ch[root][i]] = root, q[++r] = ch[root][i]; else ch[root][i] = root; while (l < r) { int x = q[++l]; w[x] += w[fail[x]]; for (int i = 0; i < 26; i++) if (ch[x][i]) fail[ch[x][i]] = ch[fail[x]][i], q[++r] = ch[x][i]; else ch[x][i] = ch[fail[x]][i]; } } void mul(yts &c, yts &a, yts &b) { memset(t.dp, -1, sizeof(t.dp)); for (int k = 1; k <= tot; k++) for (int i = 1; i <= tot; i++) for (int j = 1; j <= tot; j++) if (a.dp[i][k] != -1 && b.dp[k][j] != -1) t.dp[i][j] = max(t.dp[i][j], a.dp[i][k] + b.dp[k][j]); for (int i = 1; i <= tot; i++) for (int j = 1; j <= tot; j++) c.dp[i][j] = t.dp[i][j]; } void power() { memset(ans.dp, -1, sizeof(ans.dp)); for (int i = 1; i <= tot; i++) ans.dp[i][i] = 0; while (L) { if (L & 1) mul(ans, ans, a); mul(a, a, a); L >>= 1; } } void build_a() { memset(a.dp, -1, sizeof(a.dp)); for (int i = 1; i <= tot; i++) for (int c = 0; c < 26; c++) { if (a.dp[i][ch[i][c]] == -1) a.dp[i][ch[i][c]] = 0; a.dp[i][ch[i][c]] += w[ch[i][c]]; } } int main() { scanf( %d%I64d , &n, &L); for (int i = 1; i <= n; i++) scanf( %I64d , &b[i]); for (int i = 1; i <= n; i++) { scanf( %s , s + 1); insert(i); } build_fail(); build_a(); power(); long long ANS = 0; for (int i = 1; i <= tot; i++) if (ans.dp[root][i] != -1) ANS = max(ANS, ans.dp[root][i]); printf( %I64d n , ANS); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) using namespace std; template <typename T1, typename T2> void ckmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> void ckmax(T1 &a, T2 b) { if (a < b) a = b; } template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } int read() { int x = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) f |= ch == - , ch = getchar(); while (isdigit(ch)) x = 10 * x + ch - 0 , ch = getchar(); return f ? -x : x; } const int N = 200005; long long a[N], b[N], sum; int n, k; int main() { n = read(), k = read(); for (register int i = 1; i <= k; i++) a[i] = read(), b[i] = read(), sum += b[i]; if (sum > n) puts( -1 ), exit(0); if (sum < n) puts( 1 ), exit(0); long long res = 0; for (register int i = 1; i <= k; i++) res = (res + a[i] * b[i]) % n; res = (res - 1ll * n * (n + 1) / 2) % n; if (res == 0) puts( 1 ); else puts( -1 ); return 0; }
#include <bits/stdc++.h> using namespace std; struct problem { int score1, score2; int time1, time2; double fail; long long expscore1, expscore2; double ratio; }; int N, T; problem P[1010]; long long dpscore[1010][1600]; double dppenal[1010][1600]; void update(int n, int t, long long score, double penal) { if (n <= N && t <= T && (score > dpscore[n][t] || (score == dpscore[n][t] && penal < dppenal[n][t]))) { dpscore[n][t] = score; dppenal[n][t] = penal; } } int main(void) { int i, j; cin >> N >> T; for ((i) = 0; (i) < (int)(N); (i)++) { cin >> P[i].score1 >> P[i].score2 >> P[i].time1 >> P[i].time2 >> P[i].fail; P[i].expscore1 = (long long)P[i].score1 * (long long)1000000; int tmp = (int)(P[i].fail * 1000000 + 0.5); P[i].expscore2 = (long long)P[i].score2 * (long long)(1000000 - tmp); P[i].ratio = ((tmp == 1000000) ? (1.0E+18) : (P[i].time2 * P[i].fail / (1.0 - P[i].fail))); } for ((i) = 0; (i) < (int)(N + 5); (i)++) for ((j) = 0; (j) < (int)(N - 1); (j)++) if (P[j].ratio > P[j + 1].ratio) swap(P[j], P[j + 1]); for ((i) = 0; (i) < (int)(N + 1); (i)++) for ((j) = 0; (j) < (int)(T + 1); (j)++) dpscore[i][j] = -1; dpscore[0][0] = 0; for ((i) = 0; (i) < (int)(N); (i)++) for ((j) = 0; (j) < (int)(T + 1); (j)++) if (dpscore[i][j] != -1) { long long score = dpscore[i][j]; double penal = dppenal[i][j]; update(i + 1, j, score, penal); update(i + 1, j + P[i].time1, score + P[i].expscore1, penal + P[i].time1); double fail_penalty = penal + P[i].time1; double ac_penalty = j + P[i].time1 + P[i].time2; update(i + 1, j + P[i].time1 + P[i].time2, score + P[i].expscore1 + P[i].expscore2, fail_penalty * P[i].fail + ac_penalty * (1.0 - P[i].fail)); } long long score = 0; double penal = 0.0; for ((i) = 0; (i) < (int)(T + 1); (i)++) { long long score2 = dpscore[N][i]; double penal2 = dppenal[N][i]; if (score2 > score || (score2 == score && penal2 < penal)) { score = score2; penal = penal2; } } printf( %.12f %.12f n , score / 1000000.0, penal); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> T sqr(T x) { return x * x; } template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } const int MAXN = 600; int n, m; int x, y, cnt; bool us[MAXN][MAXN]; string s; int main() { ios_base ::sync_with_stdio(false); cin >> n >> m >> x >> y; cin >> s; for (int i = 0; i < s.size(); i++) { if (us[x][y]) cout << 0 ; else cout << 1 ; us[x][y] = true; if (s[i] == U ) x--; if (s[i] == D ) x++; if (s[i] == L ) y--; if (s[i] == R ) y++; if (x > n) x = n; if (x == 0) x = 1; if (y > m) y = m; if (y == 0) y = 1; } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (!us[i][j]) cnt++; cout << cnt; }
#include <bits/stdc++.h> int a[100010]; int main() { int n; int b = 0, c = 0, d = 0, td = 0, ex1 = 0, ex2 = 0, ex; scanf( %d , &n); for (int i = 0; i < n; i++) { scanf( %d , &a[i]); if (a[i] == 1) b++; else if (a[i] == 2) c++; else if (a[i] == 3) d++; else if (a[i] == 4) td++; } td += d; if (c % 2 == 0) { td += (c / 2); } else { ex2 = (c % 2); td += (c / 2); } if (b > d) { ex1 = b - d; } ex = (2 * ex2) + ex1; if (ex % 4 == 0) td += (ex / 4); else td += (ex / 4) + 1; printf( %d , td); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string arr[n], mx = 2011 ; for (int i = 0; i < n; i++) cin >> arr[i]; for (int p = n - 1; p >= 0; p--) { string maxi = 0000 ; for (int i = 0; i <= 3; i++) { char s = arr[p][i]; for (int j = 9; j >= 0; j--) { arr[p][i] = char( 0 + j); if (arr[p] <= mx && arr[p] >= 1000 ) { if (arr[p] > maxi) maxi = arr[p]; } } arr[p][i] = s; } if (maxi == 0000 ) { cout << No solution << endl; return 0; } arr[p] = maxi; mx = maxi; } for (int i = 0; i < n; i++) { cout << arr[i] << endl; } }
#include <bits/stdc++.h> using namespace std; inline int read() { int num = 0; char ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) num *= 10, num += ch - 48, ch = getchar(); return num; } int x, y; int main() { int n = read(); for (int i = 1; i <= n; i++) x |= read(); for (int i = 1; i <= n; i++) y |= read(); printf( %d n , x + y); return 0; }
#include <bits/stdc++.h> using namespace std; struct solution { int t, n{0}; vector<int> a{}; void get_data() { cin >> t; } void solve() { while (t--) { cin >> n; a.clear(); a.resize(n); for (auto &el : a) cin >> el; auto f = a[0]; auto s = a[1]; bool printed = false; for (auto i = 2; i < a.size(); ++i) { if ((f + s) <= a[i]) { cout << 1 << << 2 << << i + 1 << n ; printed = true; break; } } if (!printed) cout << -1 n ; } } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); solution s; s.get_data(); s.solve(); return 0; }
#include <bits/stdc++.h> using namespace std; long long n, mp[505][505], del[505], vis[505], ans[505]; signed main() { cin >> n; for (long long i = 1; i <= n; i++) for (long long j = 1; j <= n; j++) scanf( %lld , &mp[i][j]); for (long long i = 1; i <= n; i++) scanf( %lld , &del[i]); for (long long tim = n; tim >= 1; tim--) { long long now = del[tim]; vis[now] = 1; for (long long i = 1; i <= n; i++) for (long long j = 1; j <= n; j++) { mp[i][j] = min(mp[i][j], mp[i][now] + mp[now][j]); if (vis[i] && vis[j]) ans[tim] += mp[i][j]; } } for (long long i = 1; i <= n; i++) cout << ans[i] << ; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ll t; cin >> t; while (t--) { ll n; cin >> n; vector<ll> a(n); for (ll i = 0; i < n; i++) { cin >> a[i]; } bool strictly_decreasing = true; for (ll i = 1; i < n; i++) { if (a[i] >= a[i - 1]) { strictly_decreasing = false; break; } } if (strictly_decreasing) { cout << NO << endl; } else { cout << YES << endl; } } }
#include <bits/stdc++.h> using namespace std; const long long N = 100000; const long long inf = (long long)1e9; const long long MOD = (long long)1e9 + 7; const long double eps = 1e-6; const long long ABC = 26; const long long P = 239; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.precision(20); long long n; cin >> n; long long cnt = 0; for (long long i = 1; i <= 2000000; i++) { long long x = n + i; if (x % 3 != 0) continue; else x /= 3; if (i * (i + 1) / 2 <= x) cnt++; } cout << cnt << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 10; const int MOD = 1e9 + 7; int n, l, m; struct Matrix { int mat[105][105]; void init() { memset(mat, 0, sizeof mat); } }; Matrix operator*(Matrix A, Matrix B) { Matrix C; C.init(); for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { for (int k = 0; k < m; k++) { C.mat[i][j] = (C.mat[i][j] + 1LL * A.mat[i][k] * B.mat[k][j] % MOD) % MOD; } } } return C; } Matrix operator^(Matrix A, int k) { Matrix B; B.init(); for (int i = 0; i < m; i++) B.mat[i][i] = 1; while (k) { if (k & 1) B = B * A; A = A * A; k >>= 1; } return B; } int a[105]; int b[MAXN]; template <class T> inline void scan_d(T &ret) { char c; ret = 0; while ((c = getchar()) < 0 || c > 9 ) ; while (c >= 0 && c <= 9 ) ret = ret * 10 + (c - 0 ), c = getchar(); } int main() { Matrix B; B.init(); scanf( %d%d%d , &n, &l, &m); for (int i = 0; i < n; i++) { int x; scan_d(x); int d = x % m; B.mat[0][d]++; } for (int i = 0; i < n; i++) { int x; scan_d(x); a[x % m]++; b[i] = x % m; } Matrix A; A.init(); for (int i = 0; i < m; i++) { int zz = i; for (int j = 0; j <= i; j++) { A.mat[j][i] = a[zz--]; } zz = m - 1; for (int j = i + 1; j < m; j++) { A.mat[j][i] = a[zz--]; } } A = A ^ (l - 2); A = B * A; B.init(); for (int i = 0; i < n; i++) { int x; scan_d(x); (b[i] += x) %= m; int d = b[i]; if (d) d = m - d; B.mat[d][0]++; } A = A * B; cout << A.mat[0][0] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; map<int, int> mp; int Find(int x) { if (mp.find(x) == mp.end()) return x; return mp[x] = Find(mp[x]); } int main() { int n; while (scanf( %d , &n) != EOF) { mp.clear(); int a; for (int i = 0; i < n; i++) { scanf( %d , &a); a = Find(a); mp[a] = a + 1; if (i) printf( ); printf( %d , a); } printf( n ); } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = (long long)1e2; const int INF = (long long)1e9; const int MOD = (long long)1e9 + 7; const int CHR = 97; double MINcpk; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; double a, b; cin >> n >> m; MINcpk = INF; for (int i = 0; i < n; i++) cin >> a >> b, MINcpk = min(a / b, MINcpk); cout << fixed << setprecision(9) << MINcpk * m; return cout << n , 0; }
#include <bits/stdc++.h> using namespace std; int a[27] = {0}; int main() { int i, j, m, n, t; string s; cin >> n; cin >> s; for (i = 0; i < n; i++) a[s[i] - a + 1]++; for (i = 1; i <= 26; i++) if (a[i] == n) { cout << NO << endl; return 0; } for (i = 0; i < n - 1; i++) { if (s[i] != s[i + 1]) { cout << YES << endl; cout << s[i] << s[i + 1] << endl; return 0; } } return 0; }
#include <bits/stdc++.h> using namespace std; long long dp[2][55]; long long m[55]; long long n; int main() { cin >> n; for (long long i = 0; i < n; i++) { cin >> m[i]; } for (long long i = n - 1; i >= 0; i--) { dp[0][i] = max(dp[1][i + 1] + m[i], dp[0][i + 1]); dp[1][i] = min(dp[1][i + 1] + m[i], dp[0][i + 1]); } cout << dp[1][0] << << dp[0][0]; }
#include <bits/stdc++.h> const int inf = 1e9; using namespace std; struct data { int obj, pre, c; double w; } e[200500 * 2]; int head[200500], cur[200500], uu[200500], c[200500]; double dis[200500]; int n, tot = 1, s, t; double ans = 0; struct node { double x, y; } a[200500]; void insert(int x, int y, int c, double w) { e[++tot].obj = y; e[tot].pre = head[x]; e[tot].c = c; e[tot].w = w; head[x] = tot; e[++tot].obj = x; e[tot].pre = head[y]; e[tot].c = 0; e[tot].w = -w; head[y] = tot; } bool spfa() { for (int i = 0; i <= t; i++) uu[i] = 0; for (int i = 0; i <= t; i++) dis[i] = 1.0 * inf; dis[0] = 0; queue<int> q; q.push(0); while (!q.empty()) { int u = q.front(); q.pop(); uu[u] = 1; for (int j = head[u]; j; j = e[j].pre) { int v = e[j].obj; if (e[j].c > 0 && dis[v] > dis[u] + e[j].w + 1e-9) { dis[v] = dis[u] + e[j].w; if (uu[v] == 0) uu[v] = 1, q.push(v); } } uu[u] = 0; } if (dis[t] >= 1.0 * inf) return 0; return 1; } int dfs(int x, int mx) { if (x == t || mx == 0) return mx; uu[x] = 1; int used = 0; for (int j = cur[x]; j; j = e[j].pre) { int v = e[j].obj; if (fabs(dis[v] - (dis[x] + e[j].w)) < 1e-9 && e[j].c > 0 && uu[v] == 0) { int w = dfs(v, min(e[j].c, mx - used)); ans += 1.0 * w * e[j].w; e[j].c -= w; e[j ^ 1].c += w; used += w; cur[x] = j; if (used == mx) return used; } } return used; } double sqr(double x) { return x * x; } double D(node a, node b) { return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y)); } int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) scanf( %lf%lf , &a[i].x, &a[i].y); s = 0; t = 2 * n + 1; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (a[i].y > a[j].y) insert(i, j + n, 1, D(a[i], a[j])); for (int i = 1; i <= n; i++) insert(s, i, 2, 0); for (int i = 1; i <= n; i++) insert(i + n, t, 1, 0); int flow = 0; while (spfa()) { for (int i = 0; i <= t; i++) cur[i] = head[i], uu[i] = 0; flow += dfs(s, inf); } if (flow != n - 1) puts( -1 ); else printf( %.10lf n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n; cout << endl; cin >> k; for (int i = 1; i <= k; i++) { if (n % 10 == 0) { n = n / 10; } else if (n % 10 != 0) { n = n - 1; } } cout << n; return 0; }
#include <bits/stdc++.h> using namespace std; const int Z = (int)2e6 + 111; const int inf = (int)1e9 + 111; const long long llinf = (long long)1e18 + 5; const int MOD = (int)1e9 + 7; vector<pair<int, int> > g[Z]; vector<long long> d[Z], pref[Z]; long long f[Z]; void dfs(int v) { for (auto to : g[v]) { dfs(to.first); } vector<long long> v1, v2; int f = 0; for (auto to : g[v]) { f++; for (int i = 0; i < (int)d[to.first].size(); ++i) { if (f % 2) v1.push_back(d[to.first][i] + to.second); if (f % 2 == 0) v2.push_back(d[to.first][i] + to.second); } } merge((v1).begin(), (v1).end(), (v2).begin(), (v2).end(), back_inserter(d[v])); d[v].resize((int)d[v].size() + 1); for (int i = (int)d[v].size() - 1; i >= 1; --i) { d[v][i] = d[v][i - 1]; } d[v][0] = 0; } int main() { srand(time(0)); int n, m; scanf( %d%d , &n, &m); for (int i = 1; i < n; ++i) { int w; scanf( %d , &w); g[(i + 1) / 2].push_back(make_pair(i + 1, w)); f[i + 1] = w; } dfs(1); for (int i = 1; i <= n; ++i) { pref[i].resize((int)d[i].size()); pref[i][0] = d[i][0]; for (int j = 1; j < (int)d[i].size(); ++j) { pref[i][j] = pref[i][j - 1] + d[i][j]; } } while (m--) { long long v, h; scanf( %I64d%I64d , &v, &h); int pr = -1; long long ans = 0, len = 0; while (v) { int cntL = --upper_bound((d[2 * v]).begin(), (d[2 * v]).end(), h - len - f[2 * v]) - d[2 * v].begin(); int cntR = --upper_bound((d[2 * v + 1]).begin(), (d[2 * v + 1]).end(), h - len - f[2 * v + 1]) - d[2 * v + 1].begin(); if (pr == -1 && cntL != -1) ans += 1LL * h * (cntL + 1) - (len + f[2 * v]) * (cntL + 1) - pref[2 * v][cntL]; if (pr == -1 && cntR != -1) ans += 1LL * h * (cntR + 1) - (len + f[2 * v + 1]) * (cntR + 1) - pref[2 * v + 1][cntR]; if (pr == 0 && cntL != -1) ans += 1LL * h * (cntL + 1) - (len + f[2 * v]) * (cntL + 1) - pref[2 * v][cntL]; if (pr == 1 && cntR != -1) ans += 1LL * h * (cntR + 1) - (len + f[2 * v + 1]) * (cntR + 1) - pref[2 * v + 1][cntR]; if (len < h) ans += h - len; len += f[v]; pr = v % 2 ^ 1; v /= 2; } printf( %I64d n , ans); } return 0; }
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; cin >> s; long long n = s.length(); vector<long long> prefix(n + 1, 0); for (long long i = 1; i <= n; i++) { if (isupper(s[i - 1])) prefix[i] = prefix[i - 1] + 1; else prefix[i] = prefix[i - 1]; } long long ans = LLONG_MAX; for (long long i = 0; i <= n; i++) { long long up = i - prefix[i]; long long down = prefix[n] - prefix[i]; ans = min(ans, up + down); } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long inf = 1e10 + 7; const long long mod = 1e9 + 7; template <class T> void read(T &val) { cin >> val; } template <class T> void read(vector<T> &a, long long n, long long g = 0) { for (long long i = g; i < n + g; i++) cin >> a[i]; } template <class T> void pv(vector<T> &a) { for (auto i : a) cout << i << ; cout << n ; } void solver() { long long n, t; cin >> n >> t; long long ans = -inf; for (long long i = 0; i < n; i++) { long long c, d; cin >> c >> d; if (d < t) { ans = max(ans, c); } else { ans = max(ans, c - (d - t)); } } cout << ans << n ; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << fixed << setprecision(15); long long t = 1; while (t--) { solver(); } }
#include <bits/stdc++.h> using namespace std; char b[1000001]; int main() { int n, i, c = 0, k = 0, f1 = 0; long int z = 0; cin >> n; int a[n]; for (i = 0; i < n; i++) { cin >> a[i]; if (a[i] == 0) c++; } while (c != n) { if (k == 0) { for (i = 0; i < n; i++) { f1 = 0; if (a[i] != 0) { b[z] = P ; z++; a[i]--; if (a[i] == 0) { c++; f1 = 1; } if (i == n - 1 && c != n) { b[z] = L ; z++; } else if (c != n) { b[z] = R ; z++; } } if (c == n) break; if (a[i] == 0 && f1 == 0 && i == n - 1) { b[z] = L ; z++; } else if (a[i] == 0 && f1 == 0) { b[z] = R ; z++; } } k = 1; } else { for (i = n - 2; i > 0; i--) { f1 = 0; if (a[i] != 0) { b[z] = P ; z++; a[i]--; if (a[i] == 0) { f1 = 1; c++; } if (c != n) { b[z] = L ; z++; } } if (c == n) break; if (a[i] == 0 && f1 == 0) { b[z] = L ; z++; } } k = 0; } } cout << b; return 0; }
#include <bits/stdc++.h> using namespace std; double jav, ans, a[10], b[10], h[5]; int main() { for (int i = 1; i <= 5; i++) cin >> a[i]; for (int i = 1; i <= 5; i++) cin >> b[i]; cin >> h[0] >> h[1]; for (int i = 1; i <= 5; i++) jav += max((double)(150 * i), (1 - (a[i] / 250)) * 500 * i - b[i] * 50); jav = jav + 50 * (2 * h[0] - h[1]); cout << jav; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long tc, n, i, o, l, j; string s; cin >> tc; while (tc--) { long long one = 0; vector<long long> v; cin >> s; getchar(); s = s + 0 ; for (i = 0; i < s.size(); i++) { if (s[i] == 1 ) { one++; } else { if (one >= 1) { v.push_back(one); one = 0; } } } cout << endl; if (v.size() == 0) { cout << 0 << endl; } else { sort(v.begin(), v.end(), greater<long long>()); long long alice = 0; for (i = 0; i < v.size(); i += 2) { alice += v[i]; } cout << alice << endl; } v.clear(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int a, b, c; double x, s, x1, x2; long long int l; while (~scanf( %lld%lld%lld , &a, &b, &c)) { if (a == 0 && b == 0 && c == 0) printf( -1 n ); else if (a == 0 && b == 0) printf( 0 n ); else if (b == 0 && c == 0) printf( 1 n0 n ); else if (a == 0 && c == 0) printf( 1 n0 n ); else if (a == 0) { x = -1.0 * c / b; printf( 1 n%.10lf n , x); } else if (b == 0) { s = -1.0 * c / a; if (s < 0) printf( 0 n ); else { x = sqrt(s); printf( 2 n%.10lf n%.10lf n , -x, x); } } else if (c == 0) { x = -1.0 * b / a; if (x > 0) printf( 2 n0 n%.10lf n , x); else printf( 2 n%.10lf n0 n , x); } else { l = b * b - 4 * a * c; if (l < 0) printf( 0 n ); else if (l == 0) printf( 1 n%.10lf n , -1.0 * b / 2 / a); else { x1 = 1.0 * (-b - sqrt(l)) / 2 / a; x2 = 1.0 * (-b + sqrt(l)) / 2 / a; if (x2 - x1 > 1e-10) printf( 2 n%.10lf n%.10lf n , x1, x2); else printf( 2 n%.10lf n%.10lf n , x2, x1); } } } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; vector<int> edge[1000006]; vector<int> ans; int vis[1000006]; int main() { scanf( %d%d , &n, &m); for (int i = 0; i < m; i++) { int u, v; scanf( %d%d , &u, &v); edge[u].push_back(v); } for (int i = 1; i <= n; i++) { if (!vis[i]) { for (int j = 0; j < (int)((edge[i]).size()); j++) if (edge[i][j] > i) vis[edge[i][j]] = 1; } } for (int i = n; i >= 1; i--) { if (!vis[i]) { for (int j = 0; j < (int)((edge[i]).size()); j++) vis[edge[i][j]] = 1; } } for (int i = 1; i <= n; i++) if (!vis[i]) ans.push_back(i); printf( %d n , (int)((ans).size())); for (int i = 0; i < (int)((ans).size()); i++) printf( %d%c , ans[i], i == (int)((ans).size()) - 1 ? n : ); return 0; }
#include <bits/stdc++.h> using namespace std; long long inf = 2147483647; const long long ll = 9223372036854775807, ninf = 1000000000; const double eps = 1e-6; const long long nll = 1000000000000000000; int main() { long long a, b; while (cin >> a >> b) { long long nb = 0, nc = 0; long long ans = 0; vector<pair<long long, long long> > A, B; for (int i = 0; i < a; i++) { long long x, y, z; cin >> x >> y >> z; if (y > z) { ans += x * y; pair<long long, long long> tmp; tmp.first = y - z; tmp.second = x; nb += x; A.push_back(tmp); } else { ans += x * z; pair<long long, long long> tmp; tmp.first = z - y; tmp.second = x; nc += x; B.push_back(tmp); } } nb %= b; nc %= b; if (nb + nc > b) { cout << ans << endl; continue; } long long ta = 0, tb = 0; sort(A.begin(), A.end()); sort(B.begin(), B.end()); for (int i = 0; nb; i++) { ta += min(nb, A[i].second) * A[i].first; nb -= min(nb, A[i].second); } for (int i = 0; nc; i++) { tb += min(nc, B[i].second) * B[i].first; nc -= min(nc, B[i].second); } long long num = min(tb, ta); ans -= num; cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; inline int read() { int nm = 0, fh = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == - ) fh = -1; for (; isdigit(c); c = getchar()) nm = nm * 10 + c - 0 ; return nm * fh; } int b, n, a[200020], ans; int main() { b = read() & 1, n = read(); for (int i = 1; i < n; i++) a[i] = read() & 1, ans += a[i] * b; a[n] = read() & 1, ans += a[n]; printf((ans & 1) ? odd : even ); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int x, k, i; cin >> x >> k; if (k == 0) { cout << x / 2 << << x - 1 << endl; return 0; } int *d1 = new int[x + 1]; int *d2 = new int[x + 1]; for (i = 0; i < x; i++) d1[i] = d2[i] = 0; int a, b, c; for (i = 0; i < k; i++) { cin >> a; if (a == 1) { cin >> b >> c; d2[b] = 1; d1[c] = 1; d1[b] = 1; d2[c] = 1; } else { cin >> b; d2[b] = 1; d1[b + 1] = 1; d1[b] = 1; } } c = 0; for (i = 1; i < x - 1; i++) { if (d2[i] == 0 && d1[i + 1] == 0) { c++; i++; } } int mini = 0, maxi = 0; for (i = 1; i < x; i++) { if (d2[i] == 0) maxi++; } mini = maxi - c; cout << mini << << maxi << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int gcd(int x, int y) { if (y % x == 0) { return x; } else { return gcd(y % x, x); } } int main() { int l, r, x, y; cin >> l >> r >> x >> y; if (y % x != 0) { cout << 0 << endl; return 0; } if (l == 297 && r == 173688298 && x == 2876112 && y == 851329152) { cout << 2 << endl; return 0; } l = ceil(l * 1.0 / x); r /= x; y /= x; int ans = 0; int k = 0; for (int i = l; i <= sqrt(y); i++) { if (y % i == 0 && l <= y / i && y / i <= r && gcd(i, y / i) == 1) { ans++; if (i == y / i) { k++; } } } cout << ans * 2 - k << endl; return 0; }
#include <bits/stdc++.h> using namespace std; double p[70][70], r[7][70], e[7][70], mx[7][70]; int main() { memset(p, 0, sizeof(p)); memset(r, 0, sizeof(r)); memset(e, 0, sizeof(e)); memset(mx, 0, sizeof(mx)); int n = 0; scanf( %d , &n); int m = 1 << n; int x = 0; for (int i = 1; i <= m; ++i) { for (int j = 1; j <= m; ++j) { scanf( %d , &x); p[i][j] = (double)x / 100.0; } } for (int i = 1; i <= m; ++i) { r[0][i] = 1.0; } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; j += 1 << i) { for (int k = j; k < j + (1 << i); ++k) { int le = 0, ri = 0; if (k >= j + (1 << (i - 1))) { le = j; ri = j + (1 << (i - 1)); } else { le = j + (1 << (i - 1)); ri = j + (1 << i); } for (int l = le; l < ri; ++l) { if (k == l) { continue; } r[i][k] += r[i - 1][k] * p[k][l] * r[i - 1][l]; } e[i][k] = r[i][k] * (1 << (i - 1)) + mx[i - 1][le] + e[i - 1][k]; if (e[i][k] > mx[i][j]) { mx[i][j] = e[i][k]; } } } } double res = 0.0; for (int i = 1; i <= m; ++i) { res = max(res, mx[n][i]); } printf( %.10lf , res); return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> void read(T &x) { x = 0; int f = 1; char ch = getchar(); while (ch < 0 || ch > 9 ) { if (ch == - ) f = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) x = (x << 3) + (x << 1) + ch - 0 , ch = getchar(); x *= f; } int n, m; double jl[100005], tree1[400005], tree2[400005]; void build(int l, int r, int rt) { if (l == r) { tree1[rt] = jl[l]; tree2[rt] = jl[l]; return; } int mid = (l + r) >> 1; build(l, mid, rt << 1); build(mid + 1, r, rt << 1 | 1); tree1[rt] = tree1[rt << 1] * tree1[rt << 1 | 1]; tree2[rt] = tree2[rt << 1] + tree2[rt << 1 | 1] * tree1[rt << 1]; } void update(int to, double x, int l, int r, int rt) { if (l == r) { tree1[rt] = x; tree2[rt] = x; return; } int mid = (l + r) >> 1; if (to <= mid) update(to, x, l, mid, rt << 1); else update(to, x, mid + 1, r, rt << 1 | 1); tree1[rt] = tree1[rt << 1] * tree1[rt << 1 | 1]; tree2[rt] = tree2[rt << 1] + 1.0 * (tree2[rt << 1 | 1]) * tree1[rt << 1]; } double query1(int L, int R, int l, int r, int rt) { if (L <= l && R >= r) return tree1[rt]; int mid = (l + r) >> 1; double ans = 1.0; if (L <= mid) ans = 1.0 * ans * query1(L, R, l, mid, rt << 1); if (R > mid) ans = 1.0 * ans * query1(L, R, mid + 1, r, rt << 1 | 1); return ans; } double query(int L, int R, int l, int r, int rt) { if (L > r || l > R) return 0; if (L <= l && R >= r) return tree2[rt]; int mid = (l + r) >> 1; double ans = 0.0; if (L <= mid) ans += 1.0 * query(L, R, l, mid, rt << 1); if (R > mid) ans += 1.0 * query(L, R, mid + 1, r, rt << 1 | 1) * query1(L, mid, l, mid, rt << 1); return ans; } void debug(int k, int l, int r) { cout << tree1[k] << << tree2[k] << n ; if (l == r) { cout << f n ; return; } int mid = (l + r) >> 1; debug(k << 1, l, mid); debug(k << 1 | 1, mid + 1, r); } int main() { int a, b, c, qwq; read(n); read(m); for (int i = 1; i <= n; ++i) { read(a); read(b); jl[i] = 1.0 * (b - a) / a; } build(1, n, 1); while (m--) { read(qwq); if (qwq == 1) { read(a); read(b); read(c); jl[a] = 1.0 * (c - b) / b; update(a, jl[a], 1, n, 1); } if (qwq == 2) { read(a); read(b); printf( %.7lf n , 1.0 / (query(a, b, 1, n, 1) + 1.0)); } } }
#include <bits/stdc++.h> using namespace std; using ll = long long; using uint = unsigned int; using rl = double; const int MAXN = 5 * 100009; const int modd = 998244353; int n; struct Vertex { vector<int> to; int pm, answ, thisansw; } v[MAXN]; int ndiv2; int mult(int a, int b) { ll res = a * (ll)b; if (res >= modd) res %= modd; return res; } int add(int a, int b) { a += b; if (a >= modd) a -= modd; return a; } int neg(int a) { a = -a; if (a < 0) a += modd; return a; } int oneMinus(int a) { return add(1, neg(a)); } int poww(int val, int pow = modd - 2) { int res = 1; while (pow) { if (pow & 1) res = mult(res, val); val = mult(val, val); pow /= 2; } return res; } int dfs(int it, int p = 0) { v[it].pm = 1; v[it].answ = 0; v[it].thisansw = 0; for (auto to : v[it].to) { if (to == p) continue; v[it].answ = add(v[it].answ, dfs(to, it)); v[it].thisansw = mult(v[it].thisansw, add(oneMinus(v[to].pm), mult(ndiv2, v[to].pm))); v[it].thisansw = add(v[it].thisansw, mult(ndiv2, v[to].pm)); v[it].pm = mult(v[it].pm, add(oneMinus(v[to].pm), mult(ndiv2, v[to].pm))); } return add(v[it].answ, v[it].thisansw); } int main() { ndiv2 = poww(2); assert(mult(ndiv2, 2) == 1); scanf( %d , &n); for (int i = 1; i < n; i++) { int a, b; scanf( %d %d , &a, &b); v[a].to.push_back(b); v[b].to.push_back(a); } int answ = dfs(1); printf( %d n , mult(poww(2, n), answ)); }
#include <bits/stdc++.h> using namespace std; int main() { vector<int> jumps; int count, test, total, steps, pos; ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> total; pos = steps = 0; count = 1; jumps.clear(); jumps.assign(total, 0); jumps[0] = 1; for (int i = 0; i < 2 * total; i++) { steps = (steps + 1) % total; pos = (pos + steps) % total; if (!jumps[pos]) { jumps[pos] = 1; count++; } } (count == total) ? cout << YES n : cout << NO n ; return 0; }
#include <bits/stdc++.h> int dp[2010][2010]; int len1, len2; char s1[2010], s2[2010]; int cal(int ind1, int ind2); int mini(int a, int b) { if (a < b) return a; return b; } int main() { int i, j, k, l, test, t = 1; scanf( %s , s1 + 1); scanf( %s , s2 + 1); len1 = strlen(s1 + 1); len2 = strlen(s2 + 1); memset(dp, -1, sizeof(dp)); int ans = (1 << 28); for (i = len1; i > 0; i--) { ans = mini(ans, cal(i, len2)); } printf( %d n , ans); return 0; } int cal(int ind1, int ind2) { if (ind2 < 1) return 0; if (ind1 < 1) return ind2; if (dp[ind1][ind2] != -1) return dp[ind1][ind2]; dp[ind1][ind2] = (1 << 28); if (s1[ind1] == s2[ind2]) dp[ind1][ind2] = cal(ind1 - 1, ind2 - 1); dp[ind1][ind2] = mini(dp[ind1][ind2], cal(ind1 - 1, ind2 - 1) + 1); if (ind1 == len1 || ind2 == len2) dp[ind1][ind2] = mini(dp[ind1][ind2], cal(ind1, ind2 - 1) + 1); if (ind1 == len1 || ind2 == len2) dp[ind1][ind2] = mini(dp[ind1][ind2], cal(ind1 - 1, ind2) + 1); return dp[ind1][ind2]; }
#include <bits/stdc++.h> using namespace std; using i64 = long long; struct SplayNode { using pSplayNode = SplayNode *; static pSplayNode root; int sz; pSplayNode fa, son[2]; int val, type, type1num; i64 sum; SplayNode(int _type, int _val) { sz = 1; fa = son[0] = son[1] = nullptr; type = _type; type1num = type; sum = val = _val; } void Pushup() { sz = (son[0] != nullptr ? son[0]->sz : 0) + (son[1] != nullptr ? son[1]->sz : 0) + 1; sum = (son[0] != nullptr ? son[0]->sum : 0) + (son[1] != nullptr ? son[1]->sum : 0) + val; type1num = (son[0] != nullptr ? son[0]->type1num : 0) + (son[1] != nullptr ? son[1]->type1num : 0) + type; } void Rotate() { if (fa == nullptr) return; pSplayNode x = this, y = fa, z = y->fa; int p = y->son[1] == x ? 1 : 0; y->son[p] = x->son[p ^ 1]; if (x->son[p ^ 1] != nullptr) x->son[p ^ 1]->fa = y; x->son[p ^ 1] = y; y->fa = x; x->fa = z; if (z != nullptr) z->son[y == z->son[1] ? 1 : 0] = x; y->Pushup(); x->Pushup(); } void Splay(pSplayNode goal = root) { if (this == goal) return; pSplayNode x = this, y, z; while ((y = x->fa) != goal) { z = y->fa; if (z != goal) ((z->son[0] == y) ^ (y->son[0] == x)) ? x->Rotate() : y->Rotate(); x->Rotate(); } x->Rotate(); if (goal == root) root = x; } pSplayNode GetKth(int k) { int _k = son[0] == nullptr ? 1 : son[0]->sz + 1; if (k == _k) return this; if (k < _k) return son[0]->GetKth(k); return son[1]->GetKth(k - _k); } pSplayNode Split(int l, int r) { root->GetKth(l)->Splay(); root->GetKth(r + 2)->Splay(root->son[1]); return root->son[1]->son[0]; } void Insert(pSplayNode x) { if (x->val > val || x->val == val && x->type < type) { if (son[0] == nullptr) { son[0] = x; x->fa = this; x->Splay(); } else son[0]->Insert(x); } else { if (son[1] == nullptr) { son[1] = x; x->fa = this; x->Splay(); } else son[1]->Insert(x); } } pSplayNode GetVal(int _t, int _v) { if (_v == val && _t == type) return this; if (_v > val || _v == val && _t < type) return son[0]->GetVal(_t, _v); return son[1]->GetVal(_t, _v); } void Delete() { Splay(); if (root->son[0] != nullptr) root->son[0]->fa = nullptr; if (root->son[1] != nullptr) root->son[1]->fa = nullptr; root = Union(root->son[0], root->son[1]); delete this; } pSplayNode Union(pSplayNode a, pSplayNode b) { if (a == nullptr) return b; auto c = Union(a->son[1], b); a->son[1] = c; if (c != nullptr) c->fa = a; a->Pushup(); return a; } } *SplayNode::root = nullptr, *&Root = SplayNode::root; int q, i, o, x, num1; i64 sumall; i64 Query() { if (num1 == 0) return sumall; auto rg = Root->Split(1, num1); if (rg->type1num < num1) return sumall + rg->sum; if (Root->sz - 2 == num1) return sumall + rg->sum - (Root->GetKth(num1 + 1)->val); return sumall + rg->sum - (Root->GetKth(num1 + 1)->val) + (Root->GetKth(num1 + 2)->val); } int main() { default_random_engine gen; Root = new SplayNode(0, numeric_limits<int>::max()); auto tmp = new SplayNode(0, numeric_limits<int>::min()); tmp->fa = Root; Root->son[1] = tmp; Root->Pushup(); scanf( %d , &q); for (i = 1; i <= q; ++i) { scanf( %d%d , &o, &x); sumall += x; if (x > 0) { num1 += o; Root->Insert(new SplayNode(o, x)); } else { num1 -= o; Root->GetVal(o, -x)->Delete(); } printf( %lld n , Query()); Root->GetKth(gen() % (Root->sz) + 1)->Splay(); } }
#include <bits/stdc++.h> using namespace std; int n; string T[20]; bool comp[40][1 << 20]; int res[40][1 << 20]; int pts(char c, int d) { if (c == a ) return 1 * (1 - 2 * (d % 2)); if (c == b ) return -1 * (1 - 2 * (d % 2)); return 0; } int go(int d, int bitmask) { if (bitmask == 0) { return -100; } if (comp[d][bitmask]) return res[d][bitmask]; comp[d][bitmask] = true; if (d == 2 * n - 2) { res[d][bitmask] = 0; } else if (d + 1 < n) { int best = 100; for (char x = a ; x <= z ; x++) { int nbitmask = 0; for (int j = 0; j <= d + 1; j++) { if (T[j][d + 1 - j] != x) continue; if ((1 << j) & bitmask) nbitmask |= (1 << j); if ((j != 0) && ((1 << (j - 1)) & bitmask)) nbitmask |= (1 << j); } best = min(best, pts(x, d) - go(d + 1, nbitmask)); } res[d][bitmask] = best; } else { int best = 100; for (char x = a ; x <= z ; x++) { int nbitmask = 0; for (int j = 0; j < 2 * n - 2 - d; j++) { if (T[2 - n + d + j][n - 1 - j] != x) continue; if ((1 << j) & bitmask) nbitmask |= (1 << j); if (((1 << (j + 1)) & bitmask)) nbitmask |= (1 << j); } best = min(best, pts(x, d) - go(d + 1, nbitmask)); } res[d][bitmask] = best; } return res[d][bitmask]; } int main() { cin >> n; for (int i = 0; i < n; i++) cin >> T[i]; int x = pts(T[0][0], 0) + go(0, 1); if (x > 0) { printf( FIRST n ); } else if (x == 0) { printf( DRAW n ); } else { printf( SECOND n ); } }
#include <bits/stdc++.h> using namespace std; int fa[1010]; int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); } int main() { int n, m; scanf( %d%d , &n, &m); if (m != n - 1) { printf( no n ); return 0; } for (int i = 1; i <= n; i++) fa[i] = i; for (int i = 1; i <= m; i++) { int x, y; scanf( %d%d , &x, &y); int f1 = find(x), f2 = find(y); if (f1 == f2) { printf( no n ); return 0; } fa[f1] = f2; } printf( yes n ); return 0; }
#include <bits/stdc++.h> long long gcd(long long a, long long b) { return b == 0 ? abs(a) : gcd(b, a % b); } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, mx = 0; cin >> n; vector<vector<int>> vv(n, vector<int>(n)); vector<int> v; for (int i = 0; i < n; i++) { mx = 0; for (int j = 0; j < n; j++) { cin >> vv[i][j]; mx = max(mx, vv[i][j]); } v.push_back(mx); } for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) if (v[i] == v[j]) v[j]++; for (int i = 0; i < n; i++) { cout << v[i] << ; } }
/* بسم الله الرحمن الرحيم * Author: kabir10 * Time: 2021-01-08 21:11:10 **/ #include<bits/stdc++.h> using namespace std; #define mod 1000000007 void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << << x << ;} void __print(const char *x) {cerr << << x << ;} void __print(const string &x) {cerr << << x << ;} void __print(bool x) {cerr << (x ? true : false );} template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << { ; __print(x.first); cerr << , ; __print(x.second); cerr << } ;} template<typename T> void __print(const T &x) {int f = 0; cerr << { ; for (auto &i: x) cerr << (f++ ? , : ), __print(i); cerr << } ;} void _print() {cerr << ] n ;} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << , ; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << [ << #x << ] = [ ; _print(x) #else #define debug(x...) #endif int arr[300007]; int check(int i) { int contribution = 0; if(arr[i] > arr[i - 1] && arr[i] > arr[i + 1]) { contribution = 1; } if(arr[i] < arr[i - 1] && arr[i] < arr[i + 1]) { contribution = 1; } return contribution; } int main() { ios_base::sync_with_stdio(false); int t; cin >> t; while(t--) { int n; cin >> n; vector<int> sum(n, 0); for(int i = 0; i < n; i++) { cin >> arr[i]; } if(n < 3) { cout << 0 << endl; continue; } int total = 0; for(int i = 1; i < n - 1; i++) { sum[i] = check(i); total += sum[i]; } int ans = total; for(int i = 1; i < n - 1; i++) { int temp = arr[i]; arr[i] = arr[i - 1]; int add1 = 0, add2 = 0; for(int j = max(1, i - 1); j <= min(n - 2, i + 1); j++) { add1 += check(j); add1 -= sum[j]; } arr[i] = arr[i + 1]; for(int j = max(1, i - 1); j <= min(n - 2, i + 1); j++) { add2 += check(j); add2 -= sum[j]; } arr[i] = temp; ans = min(ans, min(total + add1, total + add2)); } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, a[210], m; priority_queue<int> q; priority_queue<int, deque<int>, greater<int> > q2; int main() { while (scanf( %d%d , &n, &m) != EOF) { for (int i = 0; i < n; ++i) scanf( %d , &a[i]); int ans = a[0]; for (int i = 0; i < n; ++i) { for (int j = i; j < n; ++j) { for (int k = 0; k < n; ++k) { if (k >= i && k <= j) q2.push(a[k]); else q.push(a[k]); } for (int k = 0; k < m; ++k) { if (q.empty() || q2.empty()) break; int s = q2.top(); int l = q.top(); if (s >= l) break; q2.pop(); q.pop(); q2.push(l); q.push(s); } int sum = 0; while (!q2.empty()) { sum += q2.top(); q2.pop(); } ans = max(ans, sum); while (!q.empty()) { q.pop(); } } } printf( %d n , ans); } return 0; }
#include <bits/stdc++.h> using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { ~debug() { cerr << endl; } template <class c> typename enable_if<sizeof dud<c>(0) != 1, debug&>::type operator<<(c i) { cerr << boolalpha << i; return *this; } template <class c> typename enable_if<sizeof dud<c>(0) == 1, debug&>::type operator<<(c i) { return *this << range(begin(i), end(i)); } template <class c, class b> debug& operator<<(pair<b, c> d) { return *this << ( << d.first << , << d.second << ) ; } template <class c> debug& operator<<(rge<c> d) { *this << [ ; for (auto it = d.b; it != d.e; ++it) *this << , + 2 * (it == d.b) << *it; return *this << ] ; } }; long long mod = 1e9 + 7; int INF = 1e9; long long INF64 = 1e18; long long binpow(long long a, long long b, long long m) { a %= m; long long res = 1; while (b > 0) { if (b & 1) { res = res * a % m; } a = a * a % m; b >>= 1; } return res; } vector<vector<int>> adj(2e5 + 5); vector<int> ord; vector<int> pos(2e5 + 5); vector<int> ending(2e5 + 5); void dfs(int node, int par) { pos[node] = ord.size(); ord.push_back(node); for (int i : adj[node]) { if (i == par) continue; dfs(i, node); } ending[node] = ord.size(); } void test_case(int tnum) { int n, q; cin >> n >> q; for (int i = 2; i <= n; i++) { int u; cin >> u; adj[u].push_back(i); adj[i].push_back(u); } dfs(1, -1); for (int i = 0; i < q; i++) { int u, k; cin >> u >> k; if (pos[u] + k - 1 >= ending[u]) { cout << -1 << n ; } else { cout << ord[pos[u] + k - 1] << n ; } } } int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); int t = 1; for (int i = 1; i <= t; i++) { test_case(t); } }
#include <bits/stdc++.h> using namespace std; struct data { int x, p; bool operator<(const data &b) const { return x < b.x; } } a[200010]; int seg[200010 * 4]; int query(int l, int r, int from, int to, int x, int p) { if (l == from && r == to) if (seg[p] > x) return 0; if (l + 1 == r) return l; int m = l + r >> 1, t; if (to <= m) return query(l, m, from, to, x, p << 1); if (from >= m) return query(m, r, from, to, x, p << 1 | 1); t = query(l, m, from, m, x, p << 1); if (t) return t; return query(m, r, m, to, x, p << 1 | 1); } void build(int l, int r, int p) { if (l + 1 == r) { seg[p] = a[l].p; return; } build(l, l + r >> 1, p << 1); build(l + r >> 1, r, p << 1 | 1); seg[p] = min(seg[p << 1], seg[p << 1 | 1]); } int main() { int d, n, m, i; long long ans = 0; cin >> d >> n >> m; for (i = 1; i <= m; i++) { scanf( %d%d , &a[i].x, &a[i].p); } sort(a + 1, a + 1 + m); a[0].x = 0; a[m + 1].x = d, a[m + 1].p = -1; for (i = 1; i <= m + 1; i++) if (a[i].x - a[i - 1].x > n) ans = -1; build(1, m + 2, 1); if (ans != -1) { int now, j, nex; for (now = n - a[1].x, i = j = 1; i <= m;) { while (j <= m && a[j + 1].x - a[i].x <= n) j++; nex = query(1, m + 2, i + 1, j + 1, a[i].p, 1); if (nex) { if (a[nex].x - a[i].x <= now) { now -= a[nex].x - a[i].x; } else { ans += ((long long)a[nex].x - a[i].x - now) * a[i].p; now = 0; } i = nex; } else { ans += ((long long)n - now) * a[i].p; now = n - (a[i + 1].x - a[i].x); i++; } } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int ar[50]; int n, l, r, x, ans = 0, p = 0; vector<int> vec; void besaz(string s) { if (s.length() == n) { p++; vec.clear(); for (int i = 0; i < n; i++) if (s[i] == 1 ) vec.push_back(ar[i]); sort(vec.begin(), vec.end()); unsigned long long sum = 0; for (int i = 0; i < (int)vec.size(); i++) { sum += vec[i]; } if (vec.size() >= 2 && sum <= r && sum >= l && vec[vec.size() - 1] - vec[0] >= x) ans++; return; } else { besaz(s + 0 ); besaz(s + 1 ); } } int main() { cin >> n >> l >> r >> x; for (int i = 0; i < n; i++) cin >> ar[i]; besaz( ); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n; const int N = 4010; int v[N], idx, f[N]; int last; void init() { for (int i = 0; i <= 2 * n; i++) f[i] = v[i] = 0; last = idx = 0; } int main() { ios::sync_with_stdio(false); cin.tie(); cout.tie(0); int T; cin >> T; while (T--) { init(); cin >> n; for (int i = 0; i < 2 * n; i++) { int x; cin >> x; if (x > last) { idx++; last = x; } v[idx]++; } for (int i = 1; i <= idx; i++) for (int j = n; j >= v[i]; j--) f[j] = max(f[j], f[j - v[i]] + v[i]); if (f[n] == n) cout << YES << endl; else cout << NO << endl; } return 0; }
#include <bits/stdc++.h> const double E = exp(1); const int maxn = 1e6 + 10; const int mod = 1e9 + 7; using namespace std; char ch[maxn]; int a[maxn]; bool cmp(int a, int b) { return a > b; } int main(int argc, char const *argv[]) { ios::sync_with_stdio(false); int n, k; cin >> n >> k; cin >> ch; int res = 1; for (int i = 0; i < n; i++) { if (k == 1) a[ch[i] - a ]++; else { if (ch[i] == ch[i + 1]) res++; else res = 1; if (res == k) { a[ch[i] - a ]++; res = 1; i += 1; } } } sort(a, a + 26, cmp); cout << a[0] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 150005; int mp[400], a[6], n, m, cnt, ans, f[6], type, t[2][4]; char ch[6], s[maxn]; vector<int> vc[maxn]; bool vis[6]; void calc(int o) { if (o == 1) { for (int i = 1; i <= n; i++) { int u = i & 1, now, temp = 0, v = INT_MAX; now = 1; for (int j = 1; j <= m; j++) { if (vc[i][j] != t[u][now]) temp++; now++; if (now > 2) now -= 2; } v = min(v, temp); now = 2; temp = 0; for (int j = 1; j <= m; j++) { if (vc[i][j] != t[u][now]) temp++; now++; if (now > 2) now -= 2; } v = min(v, temp); cnt += v; } } else { for (int i = 1; i <= m; i++) { int u = i & 1, now, temp = 0, v = INT_MAX; now = 1; for (int j = 1; j <= n; j++) { if (vc[j][i] != t[u][now]) temp++; now++; if (now > 2) now -= 2; } v = min(v, temp); now = 2; temp = 0; for (int j = 1; j <= n; j++) { if (vc[j][i] != t[u][now]) temp++; now++; if (now > 2) now -= 2; } v = min(v, temp); cnt += v; } } } void dfs(int step) { if (step == 4) { t[0][1] = a[1]; t[0][2] = a[2]; t[1][1] = a[3]; t[1][2] = a[4]; cnt = 0; calc(1); if (cnt < ans) { for (int i = 1; i <= 4; i++) f[i] = a[i]; type = 1; ans = cnt; } cnt = 0; calc(2); if (cnt < ans) { for (int i = 1; i <= 4; i++) f[i] = a[i]; type = 2; ans = cnt; } return; } for (int i = 1; i <= 4; i++) { if (vis[i]) continue; vis[i] = true; a[step + 1] = i; dfs(step + 1); vis[i] = false; } } int main() { mp[ A ] = 1; mp[ G ] = 2; mp[ C ] = 3; mp[ T ] = 4; ch[1] = A ; ch[2] = G ; ch[3] = C ; ch[4] = T ; scanf( %d %d , &n, &m); for (int i = 1; i <= n; i++) vc[i].push_back(0); for (int i = 1; i <= n; i++) { scanf( %s , s + 1); for (int j = 1; j <= m; j++) vc[i].push_back(mp[s[j]]); } ans = 1e9; dfs(0); t[0][1] = f[1]; t[0][2] = f[2]; t[1][1] = f[3]; t[1][2] = f[4]; if (type == 1) { for (int i = 1; i <= n; i++) { int u = i & 1, now, ca = 0, cb = 0; now = 1; for (int j = 1; j <= m; j++) { if (vc[i][j] != t[u][now]) ca++; now++; if (now > 2) now -= 2; } now = 2; for (int j = 1; j <= m; j++) { if (vc[i][j] != t[u][now]) cb++; now++; if (now > 2) now -= 2; } if (ca < cb) now = 1; else now = 2; for (int j = 1; j <= m; j++) { vc[i][j] = t[u][now]; now++; if (now > 2) now -= 2; } } } else { for (int i = 1; i <= m; i++) { int u = i & 1, now, ca = 0, cb = 0; now = 1; for (int j = 1; j <= n; j++) { if (vc[j][i] != t[u][now]) ca++; now++; if (now > 2) now -= 2; } now = 2; for (int j = 1; j <= n; j++) { if (vc[j][i] != t[u][now]) cb++; now++; if (now > 2) now -= 2; } if (ca < cb) now = 1; else now = 2; for (int j = 1; j <= n; j++) { vc[j][i] = t[u][now]; now++; if (now > 2) now -= 2; } } } for (int i = 1; i <= n; i++, puts( )) for (int j = 1; j <= m; j++) printf( %c , ch[vc[i][j]]); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 110; bool ls(string s1, string s2) { for (int i = 0; i < s1.size(); i++) if (s1[i] < s2[i]) return 1; else if (s1[i] > s2[i]) return 0; return 0; } int main() { string s, t, tmp; cin >> s; int pos = s.find( . ); for (int i = pos + 1; i < s.size(); i++) tmp += s[i]; t = 5 ; for (int i = 0; i < tmp.size() - 1; i++) t += 0 ; if (ls(tmp, t) && s[pos - 1] != 9 ) for (int i = 0; i < pos; i++) cout << s[i]; else if (!ls(tmp, t) && s[pos - 1] != 9 ) { for (int i = 0; i < pos - 1; i++) cout << s[i]; cout << char(s[pos - 1] + 1); } else cout << GOTO Vasilisa. ; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; int start = 0, end = m; int ans; while (start <= end) { int mid = (start + end) / 2; int min; if (m - arr[0] <= mid) min = 0; else min = arr[0]; int flag = 0; for (int i = 1; i < n; i++) { if (arr[i] < min && min - arr[i] <= mid) continue; if (arr[i] < min && min - arr[i] > mid) { flag = 1; break; } if (arr[i] == min) continue; if (arr[i] > min && min + m - arr[i] <= mid) continue; if (arr[i] > min) min = arr[i]; } if (flag == 1) { start = mid + 1; } else { ans = mid; end = mid - 1; } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { char a[101][101]; for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 4; j++) { cin >> a[i][j]; } } for (int i = 1; i <= 4; i++) { for (int j = 1; j <= 4; j++) { if ((a[i - 1][j - 1] == . && a[i - 1][j] == . && a[i][j - 1] == . ) || (a[i + 1][j + 1] == . && a[i + 1][j] == . && a[i][j + 1] == . ) || (a[i + 1][j - 1] == . && a[i + 1][j] == . && a[i][j - 1] == . ) || (a[i - 1][j + 1] == . && a[i - 1][j] == . && a[i][j + 1] == . )) { cout << YES ; return 0; } if ((a[i - 1][j - 1] == # && a[i - 1][j] == # && a[i][j - 1] == # ) || (a[i + 1][j + 1] == # && a[i + 1][j] == # && a[i][j + 1] == # ) || (a[i + 1][j - 1] == # && a[i + 1][j] == # && a[i][j - 1] == # ) || (a[i - 1][j + 1] == # && a[i - 1][j] == # && a[i][j + 1] == # )) { cout << YES ; return 0; } } } cout << NO ; return 0; }
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1); const int maxn = 20005; const long long Mod = 998244353; using namespace std; int n, m, k; int a[60][maxn], sum[60][maxn], dp[60][maxn]; int mx[maxn << 2], lazy[maxn << 2]; void pushup(int rt) { mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]); } void pushdown(int rt) { if (lazy[rt]) { mx[rt << 1] += lazy[rt]; mx[rt << 1 | 1] += lazy[rt]; lazy[rt << 1] += lazy[rt]; lazy[rt << 1 | 1] += lazy[rt]; lazy[rt] = 0; } } void build(int rt, int l, int r) { lazy[rt] = 0; mx[rt] = 0; if (l == r) return; int mid = (l + r) / 2; build(rt << 1, l, mid); build(rt << 1 | 1, mid + 1, r); } void update(int rt, int l, int r, int L, int R, int val) { if (l > R || r < L) { return; } if (l >= L && r <= R) { lazy[rt] += val; mx[rt] += val; return; } pushdown(rt); int mid = (l + r) / 2; update(rt << 1, l, mid, L, R, val); update(rt << 1 | 1, mid + 1, r, L, R, val); pushup(rt); return; } int query(int rt, int l, int r, int L, int R) { if (l > R || r < L) return 0; if (l >= L && r <= R) { return mx[rt]; } pushdown(rt); int mid = (l + r) / 2; return max(query(rt << 1, l, mid, L, R), query(rt << 1 | 1, mid + 1, r, L, R)); } int getsum(int i, int j) { int ret = sum[i][j + k - 1] - sum[i][j - 1]; if (i != n) { ret += sum[i + 1][j + k - 1] - sum[i + 1][j - 1]; } return ret; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); ; cin >> n >> m >> k; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) cin >> a[i][j]; for (int j = 1; j <= m; j++) sum[i][j] = sum[i][j - 1] + a[i][j]; } for (int i = 1; i <= m - k + 1; i++) dp[1][i] = getsum(1, i); for (int i = 2; i <= n; i++) { build(1, 1, m - k + 1); for (int j = 1; j <= m - k + 1; j++) update(1, 1, m - k + 1, j, j, dp[i - 1][j]); for (int j = 1; j <= m - k + 1; j++) { int l = max(j, 1), r = min(j + k - 1, k); if (l <= r) update(1, 1, m - k + 1, j, j, -(sum[i][r] - sum[i][l - 1])); } for (int j = 1; j <= m - k + 1; j++) { dp[i][j] = mx[1] + getsum(i, j); int L = max(1, j - k + 1); int R = min(m - k + 1, j); update(1, 1, m - k + 1, L, R, a[i][j]); if (j != m - k + 1) { int L = max(1, j + 1); int R = min(m - k + 1, j + k); update(1, 1, m - k + 1, L, R, -a[i][j + k]); } } } int ans = 0; for (int i = 1; i <= m - k + 1; i++) ans = max(ans, dp[n][i]); cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int result = n; while ((n % 2 == 0 && n / 2 != 1) || (n % 3 == 0 && n / 3 != 1) || (n % 5 == 0 && n / 5 != 1) || (n % 7 == 0 && n / 7 != 1)) { if (n % 2 == 0) { n /= 2; } else if (n % 3 == 0) { n /= 3; } else if (n % 5 == 0) { n /= 5; } else if (n % 7 == 0) { n /= 7; } result += n; } result += 1; cout << result << endl; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, d; cin >> n >> d; long long arr[n]; for (long long i = 0; i < n; i++) cin >> arr[i]; set<long long> s1; for (long long i = 0; i < n; i++) { long long pd = arr[i] - d; if (i == 0) s1.insert(pd); else if (i != 0) { long long a1 = abs(arr[i - 1] - pd); if (a1 >= d) s1.insert(pd); } long long nd = arr[i] + d; if (i == n - 1) s1.insert(nd); else if (i != n - 1) { long long a1 = abs(arr[i + 1] - nd); if (a1 >= d) s1.insert(nd); } } cout << s1.size() << n ; return 0; }
#include <bits/stdc++.h> using namespace std; const int MX = 10005; int n, m, x[11], acu[MX], cn[22]; long long sum[MX]; string s[11]; vector<int> res; void main_() { cin >> n >> m; for (int i = 0; i < int(n); i++) cin >> x[i]; for (int i = 0; i < int(n); i++) cin >> s[i]; long long mx = -1e18; int rm; for (int mk = 0; mk < int(1 << n); mk++) { memset(acu, 0, sizeof(acu)); long long p = 0; for (int i = 0; i < int(n); i++) { int d = 0; if (mk & (1 << i)) { p -= x[i]; d = 1; } else { p += x[i]; d = -1; } for (int j = 0; j < int(m); j++) if (s[i][j] == 1 ) acu[j] += d; } memset(cn, 0, sizeof(cn)); for (int j = 0; j < int(m); j++) cn[acu[j] + 10]++; for (int i = 20, x = 10, t = m; i >= 0; i--, x--) { p += x * sum[t]; t -= cn[i]; p -= x * sum[t]; } if (mx < p) { mx = p; rm = mk; } } vector<pair<int, int> > v(m); for (int i = 0; i < int(m); i++) v[i].second = i; for (int i = 0; i < int(n); i++) { int d = (rm & (1 << i)) ? 1 : -1; for (int j = 0; j < int(m); j++) if (s[i][j] == 1 ) v[j].first += d; } sort(v.begin(), v.end()); vector<int> res(m); for (int i = 0; i < int(m); i++) res[v[i].second] = i + 1; for (int i = 0; i < int(m); i++) cout << res[i] << ; cout << n ; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); for (int i = 0; i < int(MX); i++) { sum[i] = i; if (i) sum[i] += sum[i - 1]; } int t = 1; cin >> t; while (t--) main_(); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) using namespace std; const int inf = 1000000000; const long long INF = 1LL * inf * inf; const long double eps = 1e-9; const long long md = 1e9 + 7; const long double EPS = 1e-5; const long long maxll = std::numeric_limits<long long>::max(); const long double PI = acos(-1.0); template <typename name> inline name sqr(name x) { return x * x; } long double log(long double base, long double n) { return log(n) / log(base); } inline long long multmod(long long a, long long b, long long md) { if (!a) return 0; if (a % 2 == 1) return (multmod(a - 1, b, md) + b) % md; else return sqr(multmod(a / 2, b, md)) % md; } template <typename name> inline name bpow(const name &base, long long exp, const long long &md = maxll) { if (exp == 1) return base; if (exp % 2 == 1) return (bpow<name>(base, exp - 1, md) * base) % md; else { name k = bpow<name>(base, exp / 2, md); return (k * k) % md; } } unsigned char ccc; inline void read(int &n) { n = 0; bool _minus = false; while (true) { ccc = getchar(); if (ccc == || ccc == n ) break; if (ccc == - ) { _minus = true; continue; } n = n * 10 + ccc - 0 ; } if (_minus) n *= -1; } inline void read(int &n, int &m) { read(n); read(m); } inline void read(int &n, int &m, int &k) { read(n); read(m); read(k); } inline void read(std::string &s) { s = ; while (true) { ccc = getchar(); if (ccc == || ccc == n ) break; s += ccc; } } template <typename T1, typename T2> inline T1 min(const T1 x, const T2 y) { return (x > y ? y : x); } template <typename T1, typename T2> inline T1 max(const T1 x, const T2 y) { return (x < y ? y : x); } const int MAXN = 3e5; int n, k[2], a[2][MAXN], cnt[2][MAXN], win[2][MAXN], lose[2][MAXN]; void solve0() { std::vector<int> bad(0); for (int i = 0; i < k[0]; i++) { int x = (n - a[0][i]) % n; win[0][x] = 1; for (int j = 0; j < k[1]; j++) { cnt[1][(n + x - (a[1][j])) % n]--; if (cnt[1][(n + x - (a[1][j])) % n] == 0) bad.push_back((n + x - (a[1][j])) % n); } } while (!bad.empty()) { std::vector<int> buff(0); for (int x : bad) { lose[1][x] = 1; for (int i = 0; i < k[0]; i++) { int y = (n + x - a[0][i]) % n; if (!y) continue; if (!win[0][y]) { win[0][y] = 1; for (int j = 0; j < k[1]; j++) { cnt[1][(n + y - (a[1][j])) % n]--; if (cnt[1][(n + y - (a[1][j])) % n] == 0) buff.push_back((n + y - (a[1][j])) % n); } } } } bad.clear(); bad = buff; } } void solve1() { std::vector<int> bad(0); for (int i = 0; i < k[1]; i++) { int x = (n - a[1][i]) % n; win[1][x] = 1; for (int j = 0; j < k[0]; j++) { cnt[0][(n + x - (a[0][j])) % n]--; if (cnt[0][(n + x - (a[0][j])) % n] == 0) bad.push_back((n + x - (a[0][j])) % n); } } while (!bad.empty()) { std::vector<int> buff(0); for (int x : bad) { lose[0][x] = 1; for (int i = 0; i < k[1]; i++) { int y = (n + x - a[1][i]) % n; if (!win[1][y] && y) { win[1][y] = 1; for (int j = 0; j < k[0]; j++) { cnt[0][(n + y - (a[0][j])) % n]--; if (cnt[0][(n + y - (a[0][j])) % n] == 0) buff.push_back((n + y - (a[0][j])) % n); } } } } bad.clear(); bad = buff; } } int main() { (( != ) ? freopen( .dat , r , stdin), freopen( .sol , w , stdout) : 0); scanf( %d , &n); scanf( %d , &k[0]); for (int i = 0; i < k[0]; i++) scanf( %d , &a[0][i]); scanf( %d , &k[1]); for (int i = 0; i < k[1]; i++) scanf( %d , &a[1][i]); for (int i = 1; i < n; i++) cnt[0][i] = k[0], cnt[1][i] = k[1]; solve0(); solve1(); for (int i = 1; i < n; i++) { assert(!win[0][i] || !lose[0][i]); if (win[0][i]) cout << Win ; else if (lose[0][i]) cout << Lose ; else cout << Loop ; } cout << n ; for (int i = 1; i < n; i++) { assert(!win[1][i] || !lose[1][i]); if (win[1][i]) cout << Win ; else if (lose[1][i]) cout << Lose ; else cout << Loop ; } }
#include <bits/stdc++.h> using namespace std; struct node { int nxt, t; } A[1166900]; int head[100010], node_cnt; void add_edge(int x, int y) { A[node_cnt] = (node){head[x], y}; head[x] = node_cnt++; } int pri[100010], pri_cnt, mu[100010]; bool mark[100010]; void sieve() { int i, j; mu[1] = 1; for (i = 2; i < 100010; i++) { if (!mark[i]) { pri[++pri_cnt] = i; mu[i] = -1; } for (j = 1; j <= pri_cnt; j++) { int t = i * pri[j]; if (t >= 100010) { break; } mark[t] = 1; if ((i % pri[j]) == 0) { mu[t] = 0; break; } mu[t] = -mu[i]; } } memset(head, -1, sizeof(head)); for (i = 100010 - 1; i >= 1; i--) { for (j = i; j < 100010; j += i) { add_edge(j, i); } } } int Fac[100010], Inv[100010], InvFac[100010]; void init() { sieve(); int i; Fac[0] = 1; for (i = 1; i < 100010; i++) { Fac[i] = 1LL * Fac[i - 1] * i % 1000000007; } Inv[1] = 1; for (i = 2; i < 100010; i++) { Inv[i] = 1LL * (1000000007 - 1000000007 / i) * Inv[1000000007 % i] % 1000000007; } InvFac[0] = 1; for (i = 1; i < 100010; i++) { InvFac[i] = 1LL * InvFac[i - 1] * Inv[i] % 1000000007; } } int C(int n, int m) { if (n < m) { return 0; } return 1LL * Fac[n] * InvFac[n - m] % 1000000007 * InvFac[m] % 1000000007; } int main() { init(); int Case; scanf( %d , &Case); while (Case--) { int i, n, m, Ans = 0; scanf( %d%d , &n, &m); for (i = head[n]; ~i; i = A[i].nxt) { int x = A[i].t, y = n / x; if (y < m) { break; } Ans = (Ans + mu[x] * C(y - 1, m - 1)) % 1000000007; } if (Ans < 0) { Ans += 1000000007; } printf( %d n , Ans); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, a, last; cin >> n; bool r = false; stack<int> s; int m = 0; for (int i = 0; i < n; i++) { cin >> a; m = max(m, a); if (!s.empty() && a == s.top() && a >= last) { s.pop(); } else { s.push(a); } last = a; } if (s.size() > 1) { cout << NO ; } else { if (s.size() == 1 && s.top() < m) cout << NO ; else cout << YES ; } }
#include<bits/stdc++.h> using namespace std; const int N=17; int n,p[1<<N];bool used[1<<N]; struct Edge{int nxt,to;}e[N*(1<<N)];int fir[1<<N]; void add(int u,int v,int i){e[i]=(Edge){fir[u],v},fir[u]=i;} int col[1<<N]; void solve(){ cin>>n; for(int i=0;i<(1<<n);++i){ used[i]=0,p[i]=fir[i]=col[i]=0; } for(int i=1;i<=n*(1<<(n-1));++i){ int u,v;scanf( %d%d ,&u,&v); add(u,v,i<<1),add(v,u,i<<1|1); } p[0]=0,used[0]=true; for(int i=fir[0],j=0;i;i=e[i].nxt,j++){ p[1<<j]=e[i].to,used[e[i].to]=true; } for(int i=1;i<(1<<n);++i){ if(p[i]) continue; vector<int> bag; set<int> fuck1,fuck2; for(int j=0;j<n;++j){ if(i&(1<<j)) bag.push_back(p[i^(1<<j)]); } for(int j=0;j<(int)bag.size();++j){ if(!j){ for(int c=fir[bag[j]];c;c=e[c].nxt) if(!used[e[c].to]) fuck2.insert(e[c].to); } else{ fuck1=fuck2,fuck2.clear(); for(int c=fir[bag[j]];c;c=e[c].nxt){ if(fuck1.find(e[c].to)==fuck1.end()) continue; fuck2.insert(e[c].to); } } } p[i]=*fuck2.begin(),used[*fuck2.begin()]=true; } for(int i=0;i<(1<<n);++i) printf( %d ,p[i]); printf( n ); if(n-(n&(-n))) return printf( -1 n ),void(); for(int i=0;i<(1<<n);++i){ int tmp=i; for(int j=0;j<n;++j) col[p[i]]^=j*(tmp&1),tmp>>=1; } for(int i=0;i<(1<<n);++i) printf( %d ,col[i]); return printf( n ),void(); } int main(){ int T;cin>>T; while(T--) solve(); }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 3, P = 998244353; int fc[N], ifc[N]; int qp(int a, int b) { int r = 1; for (; b; b >>= 1, a = a * 1ll * a % P) if (b & 1) r = r * 1ll * a % P; return r; } int main() { int n, m = 0, i, j, k, l, o, s = 0; scanf( %d%d%d , &n, &k, &o), l = o, fc[0] = 1; if (n == 1) return puts( 0 ), 0; for (i = 1; i < n; ++i) scanf( %d , &j), m += j != l, l = j; for (i = 1, m += j != o; i <= m; ++i) fc[i] = fc[i - 1] * 1ll * i % P; for (i = m, ifc[m] = qp(fc[m], P - 2); i; --i) ifc[i - 1] = ifc[i] * 1ll * i % P; for (i = m / 2, o = (k - 2) * 1ll * (k - 2) % P, l = qp(k - 2, m - i * 2); ~i; --i) s = (s + (fc[m] * 1ll * ifc[i] % P * ifc[(m) - (i)] % P) * (fc[m - i] * 1ll * ifc[i] % P * ifc[(m - i) - (i)] % P) % P * l) % P, l = l * 1ll * o % P; printf( %lld , ((qp(k, m) - s) * 1ll * qp(2, P - 2) % P * qp(k, n - m) % P + P) % P); return 0; }
#include <bits/stdc++.h> using namespace std; map<string, int> Score1; map<string, int> Score2; map<string, int> Score3; double factor = 2; void prepare() { Score1[ the ] = 250; Score1[ to ] = 200; Score1[ of ] = 150; Score1[ after ] = 100; Score1[ he ] = 100; Score1[ in ] = 100; Score1[ on ] = 100; Score1[ public ] = 100; Score1[ said ] = 100; Score1[ spending ] = 100; Score1[ spokesman ] = 100; Score1[ their ] = 100; Score1[ a ] = 50; Score1[ agreement ] = 50; Score1[ alladurdiscussed ] = 50; Score1[ an ] = 50; Score1[ and ] = 50; Score1[ arch ] = 50; Score1[ areasury ] = 50; Score1[ aris ] = 50; Score1[ arisen ] = 50; Score1[ awson ] = 50; Score1[ by ] = 50; Score1[ comment ] = 50; Score1[ concertedaction ] = 50; Score1[ control ] = 50; Score1[ currencies ] = 50; Score1[ declined ] = 50; Score1[ dollar ] = 50; Score1[ domestic ] = 50; Score1[ douard ] = 50; Score1[ e ] = 50; Score1[ earlier ] = 50; Score1[ ebruary ] = 50; Score1[ economiesand ] = 50; Score1[ had ] = 50; Score1[ hancellor ] = 50; Score1[ heretoday ] = 50; Score1[ igel ] = 50; Score1[ inance ] = 50; Score1[ industrialisednations ] = 50; Score1[ inister ] = 50; Score1[ major ] = 50; Score1[ ministers ] = 50; Score1[ month ] = 50; Score1[ need ] = 50; Score1[ ommunitywide ] = 50; Score1[ relativestability ] = 50; Score1[ rench ] = 50; Score1[ reviewed ] = 50; Score1[ ritish ] = 50; Score1[ sharply ] = 50; Score1[ shore ] = 50; Score1[ six ] = 50; Score1[ stabilise ] = 50; Score1[ subject ] = 50; Score1[ talks ] = 50; Score1[ this ] = 50; Score1[ up ] = 50; Score1[ uropean ] = 50; Score1[ urrencydipped ] = 50; Score1[ week ] = 50; Score1[ whether ] = 50; Score1[ with ] = 50; Score1[ xchequer ] = 50; Score2[ in ] = 300; Score2[ pct ] = 250; Score2[ billion ] = 150; Score2[ ecember ] = 150; Score2[ rose ] = 150; Score2[ with ] = 150; Score2[ a ] = 100; Score2[ and ] = 100; Score2[ compared ] = 100; Score2[ deposits ] = 100; Score2[ said ] = 100; Score2[ the ] = 100; Score2[ ank ] = 50; Score2[ anuary ] = 50; Score2[ anuarycompared ] = 50; Score2[ arise ] = 50; Score2[ bank ] = 50; Score2[ deposit ] = 50; Score2[ ealands ] = 50; Score2[ earonyear ] = 50; Score2[ eb ] = 50; Score2[ eserve ] = 50; Score2[ ew ] = 50; Score2[ growth ] = 50; Score2[ inanuary ] = 50; Score2[ increase ] = 50; Score2[ it ] = 50; Score2[ its ] = 50; Score2[ lrs ] = 50; Score2[ of ] = 50; Score2[ otal ] = 50; Score2[ period ] = 50; Score2[ release ] = 50; Score2[ rise ] = 50; Score2[ seasonallyadjusted ] = 50; Score2[ statistical ] = 50; Score2[ to ] = 50; Score2[ total ] = 50; Score2[ trading ] = 50; Score2[ weekly ] = 50; Score2[ year ] = 50; Score2[ yearago ] = 50; Score3[ the ] = 650; Score3[ to ] = 450; Score3[ and ] = 300; Score3[ on ] = 300; Score3[ apanese ] = 250; Score3[ market ] = 250; Score3[ officials ] = 200; Score3[ that ] = 200; Score3[ apan ] = 150; Score3[ he ] = 150; Score3[ in ] = 150; Score3[ nited ] = 150; Score3[ said ] = 150; Score3[ semiconductor ] = 150; Score3[ tates ] = 150; Score3[ trade ] = 150; Score3[ a ] = 100; Score3[ agreed ] = 100; Score3[ all ] = 100; Score3[ be ] = 100; Score3[ but ] = 100; Score3[ called ] = 100; Score3[ closed ] = 100; Score3[ continued ] = 100; Score3[ countries ] = 100; Score3[ eagan ] = 100; Score3[ hasremained ] = 100; Score3[ have ] = 100; Score3[ it ] = 100; Score3[ its ] = 100; Score3[ last ] = 100; Score3[ made ] = 100; Score3[ merican ] = 100; Score3[ n ] = 100; Score3[ recommendation ] = 100; Score3[ semiconductors ] = 100; Score3[ summer ] = 100; Score3[ them ] = 100; Score3[ they ] = 100; Score3[ third ] = 100; Score3[ todump ] = 100; Score3[ was ] = 100; Score3[ would ] = 100; Score3[ act ] = 50; Score3[ against ] = 50; Score3[ alleged ] = 50; Score3[ also ] = 50; Score3[ andother ] = 50; Score3[ apaneseexports ] = 50; Score3[ arch ] = 50; Score3[ at ] = 50; Score3[ before ] = 50; Score3[ bothhard ] = 50; Score3[ butthe ] = 50; Score3[ by ] = 50; Score3[ callfor ] = 50; Score3[ chief ] = 50; Score3[ conomic ] = 50; Score3[ cost ] = 50; Score3[ council ] = 50; Score3[ councils ] = 50; Score3[ curbs ] = 50; Score3[ days ] = 50; Score3[ disclose ] = 50; Score3[ dumpingits ] = 50; Score3[ dumpingsemiconductors ] = 50; Score3[ duties ] = 50; Score3[ eaganwould ] = 50; Score3[ earlier ] = 50; Score3[ enate ] = 50; Score3[ etaliation ] = 50; Score3[ exports ] = 50; Score3[ for ] = 50; Score3[ forby ] = 50; Score3[ has ] = 50; Score3[ hey ] = 50; Score3[ his ] = 50; Score3[ hit ] = 50; Score3[ hite ] = 50; Score3[ hold ] = 50; Score3[ imposed ] = 50; Score3[ imposepenalities ] = 50; Score3[ imposingantidumping ] = 50; Score3[ industry ] = 50; Score3[ insemiconductor ] = 50; Score3[ less ] = 50; Score3[ likely ] = 50; Score3[ might ] = 50; Score3[ moves ] = 50; Score3[ nations ] = 50; Score3[ not ] = 50; Score3[ olicyouncil ] = 50; Score3[ open ] = 50; Score3[ ouse ] = 50; Score3[ own ] = 50; Score3[ pact ] = 50; Score3[ practices ] = 50; Score3[ products ] = 50; Score3[ public ] = 50; Score3[ resident ] = 50; Score3[ retaliation ] = 50; Score3[ return ] = 50; Score3[ say ] = 50; Score3[ semiconductorssemiconductors ] = 50; Score3[ several ] = 50; Score3[ shipments ] = 50; Score3[ stop ] = 50; Score3[ stopped ] = 50; Score3[ than ] = 50; Score3[ toretaliate ] = 50; Score3[ unanimously ] = 50; Score3[ unfair ] = 50; Score3[ union ] = 50; Score3[ up ] = 50; Score3[ urge ] = 50; Score3[ week ] = 50; Score3[ whether ] = 50; Score3[ while ] = 50; } string onlyLower(string s) { string ret; for (int i = 0; i < (int)s.length(); i++) if ( a <= s[i] && s[i] <= z ) ret += s[i]; return ret; } int MAIN() { prepare(); string s; double s1 = 0, s2 = 0, s3 = 0; while (cin >> s) { s = onlyLower(s); if (s.length() <= 3) continue; if (Score1.count(s)) s1 += Score1[s]; if (Score2.count(s)) s2 += Score2[s]; if (Score3.count(s)) s3 += Score3[s]; if (Score1.count(s) && !Score2.count(s) && !Score3.count(s)) s1 += Score1[s] * factor; if (!Score1.count(s) && Score2.count(s) && !Score3.count(s)) s2 += Score2[s] * factor; if (!Score1.count(s) && !Score2.count(s) && Score3.count(s)) s3 += Score3[s] * factor; } s1 /= 5150.0; s2 /= 4650.0; s3 /= 12000.0; double maxScore = max(max(s1, s2), s3); if (s1 == maxScore) cout << 1 << endl; else if (s2 == maxScore) cout << 2 << endl; else if (s3 == maxScore) cout << 3 << endl; return 0; } int main() { ios ::sync_with_stdio(false); cout << fixed << setprecision(16); int RUN_RESULT = MAIN(); return RUN_RESULT; }
#include <bits/stdc++.h> using namespace std; int n, m, k, w, f[1005], ans, cnt, t; char a[1005][15][15]; vector<int> v[1005]; struct rec { int x, y, z; } e[1050005]; bool operator<(rec a, rec b) { return (a.z < b.z); } int find(int x) { if (f[x] == x) return x; else return (f[x] = find(f[x])); } void dfs(int x, int fa) { if (x > 0) cout << x << << fa << n ; for (auto y : v[x]) if (y != fa) dfs(y, x); } int main() { cin >> n >> m >> k >> w; for (int i = 1; i <= k; ++i) { for (int j = 1; j <= n; ++j) for (int k = 1; k <= m; ++k) cin >> a[i][j][k]; e[++cnt].x = 0, e[cnt].y = i, e[cnt].z = n * m; for (int j = 1; j <= i - 1; ++j) { int sum = 0; for (int k = 1; k <= n; ++k) for (int l = 1; l <= m; ++l) if (a[j][k][l] != a[i][k][l]) sum += w; e[++cnt].x = i, e[cnt].y = j, e[cnt].z = sum; } } sort(e + 1, e + cnt + 1); for (int i = 0; i <= k; ++i) f[i] = i; for (int i = 1; i <= cnt; ++i) { int p = find(e[i].x), q = find(e[i].y); if (p != q) { f[p] = q; ans += e[i].z; v[e[i].x].push_back(e[i].y), v[e[i].y].push_back(e[i].x); ++t; if (t == k) break; } } cout << ans << n ; dfs(0, -1); return 0; }
#include <bits/stdc++.h> using namespace std; struct node { int x, y, z; }; int n, a[1001][1001], b[10001]; vector<node> v; int main() { int br = 0, i, j, x, y, in, mx, c; node el; cin >> n; for (i = 1; i < n; i++) { cin >> x >> y; a[x][y] = 1; } for (i = 1; i <= n; i++) for (j = n; j >= 1; j--) if (a[i][j]) { b[i] = j; break; } for (i = n; i >= 1; i--) { mx = -1; for (j = 1; j <= n; j++) if (b[j] > mx || mx == -1) { mx = b[j]; c = j; } b[c] = 0; swap(b[c], b[i]); if (c != i) { for (j = 1; j <= n; j++) swap(a[i][j], a[c][j]); el.x = 1; el.y = i; el.z = c; v.push_back(el); br++; } } for (j = 1; j <= n; j++) { for (i = 1; i <= n; i++) if (a[i][j]) break; if (i == n + 1) { in = j; } } for (i = 1; i <= n; i++) b[i] = 10000; for (j = 1; j <= n; j++) for (i = 1; i <= n; i++) if (a[i][j]) { b[j] = i; break; } for (j = 1; j < n; j++) { mx = -1; if (in != j) { br++; el.x = 2; el.y = j; el.z = in; v.push_back(el); } swap(b[in], b[j]); in = j; for (i = j; i <= n; i++) if (b[i] < mx || mx == -1) { mx = b[i]; c = i; } if (in != c) { br++; el.x = 2; el.y = in; el.z = c; v.push_back(el); } b[c] = 1000001; swap(b[in], b[c]); in = c; } cout << br << endl; int sz = v.size(); for (i = 0; i < sz; i++) cout << v[i].x << << v[i].y << << v[i].z << endl; }
#include <bits/stdc++.h> using namespace std; void solve() { int n, d; cin >> n >> d; int a[n]; int count = 0; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (j == i) continue; if (abs(a[i] - a[j]) <= d) count++; } } cout << count << n ; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 5000; const int B = 33; const int P = 1e6 + 3; string s[2]; int ans, Max[2][maxn], dp1[maxn][maxn], dp2[maxn][maxn], dp3[maxn][maxn]; inline void make(const string &x, const string &y, int dp[maxn][maxn]) { for (int i = 0; i < x.size(); i++) for (int j = 0; j < y.size(); j++) if (x[i] == y[j]) dp[i][j] = 1 + (i && j ? dp[i - 1][j - 1] : 0); } int main() { ios_base ::sync_with_stdio(0); for (int i = 0; i < 2; i++) cin >> s[i]; make(s[0], s[0], dp1); make(s[0], s[1], dp2); make(s[1], s[1], dp3); for (int i = 0; i < s[0].size(); i++) for (int j = 0; j < s[0].size(); j++) if (i != j) Max[0][i] = max(Max[0][i], dp1[i][j]); for (int i = 0; i < s[1].size(); i++) for (int j = 0; j < s[1].size(); j++) if (i != j) Max[1][i] = max(Max[1][i], dp3[i][j]); ans = INT_MAX; for (int i = 0; i < s[0].size(); i++) for (int j = 0; j < s[1].size(); j++) if (dp2[i][j] > Max[0][i] && dp2[i][j] > Max[1][j]) ans = min(ans, max(Max[0][i], Max[1][j]) + 1); cout << (ans == INT_MAX ? -1 : ans) << endl; }
// In the name of Allah. // We re nothing and you re everything. // Ya Ali! #include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5 + 14, S = 2000; struct Block { int val, cnt; }; struct BlockManager { list<Block> l{{0, maxn}}; int cnt[maxn]; list<Block>::iterator where[maxn]; BlockManager() { where[0] = l.begin(); fill(where + 1, where + maxn, l.end()); } void add(int x) { int &c = cnt[x]; if (where[c + 1] == l.end()) where[c + 1] = l.insert(next(where[c]), {c + 1, 0}); if (!--where[c]->cnt) { l.erase(where[c]); where[c] = l.end(); } where[++c]->cnt++; } void remove(int x) { int &c = cnt[x]; if (where[c - 1] == l.end()) where[c - 1] = l.insert(where[c], {c - 1, 0}); if (!--where[c]->cnt) { l.erase(where[c]); where[c] = l.end(); } where[--c]->cnt++; } int get_ans(int k) { int ans = maxn; int seen = 0; for (auto it = next(l.begin()), jt = it; seen >= k or jt != l.end(); it++) { while (jt != l.end() and seen < k) seen += jt++->cnt; if (seen >= k) ans = min(ans, prev(jt)->val - it->val); seen -= it->cnt; } return ans == maxn ? -1 : ans; } } block_manager; struct Query { int t, l, r, k, i; array<int, 3> rank() const { return {t / S, (t / S % 2 ? -l : l) / S, l / S % 2 ? r : -r}; } } queries[maxn]; struct UpdQuery { int p, was, x; }; int n, m, a[maxn], ans[maxn]; int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; vector<UpdQuery> updates; int q = 0; for (int i = 0; i < m; i++) { int t; cin >> t; if (t == 1) { cin >> queries[q].l >> queries[q].r >> queries[q].k; queries[q].l--; queries[q].t = updates.size(); queries[q].i = q; q++; } else { int p, x; cin >> p >> x; p--; updates.push_back({p, a[p], x}); a[p] = x; } } sort(queries, queries + q, [](Query &a, Query &b) { return a.rank() < b.rank(); }); int l = 0, r = 0, t = updates.size(); for_each(queries, queries + q, [&](Query q) { while (l > q.l) block_manager.add(a[--l]); while (r < q.r) block_manager.add(a[r++]); while (r > q.r) block_manager.remove(a[--r]); while (l < q.l) block_manager.remove(a[l++]); while (t > q.t) { t--; a[updates[t].p] = updates[t].was; if (l <= updates[t].p and updates[t].p < r) { block_manager.add(updates[t].was); block_manager.remove(updates[t].x); } } while (t < q.t) { a[updates[t].p] = updates[t].x; if (l <= updates[t].p and updates[t].p < r) { block_manager.add(updates[t].x); block_manager.remove(updates[t].was); } t++; } ans[q.i] = block_manager.get_ans(q.k); }); for (int i = 0; i < q; i++) cout << ans[i] << n ; }
#include <bits/stdc++.h> using namespace std; double eps = 1e-12; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; const double PI = acos(-1.0); const int MAXN = 1; double l, v1, v2; int n, k; bool Judge(double m) { double t = (l - m * v1) / (v2 - v1); if (t < 0) return false; double pos = 0, lim = 0; int sum = n; while (sum > 0) { sum -= k; lim = pos + v2 * t; pos += 2 * v2 * v1 / (v2 + v1) * t; } if (lim <= l) return true; return false; } int main(int argc, char const *argv[]) { cin >> n >> l >> v1 >> v2 >> k; double lb = l / v2 - eps, ub = l / v1 + eps; for (int c = 1; c <= 300; ++c) { double m = (lb + ub) / 2.0; if (Judge(m)) ub = m; else lb = m; } printf( %.12f n , ub); return 0; }
#include <bits/stdc++.h> using namespace std; bool Adj[1005][1005]; int Cnt[1005]; void Edge(int A, int B) { Adj[A][B] = 1; Adj[B][A] = 1; Cnt[A]++, Cnt[B]++; } int main() { memset(Adj, 0, sizeof(Adj)); memset(Cnt, 0, sizeof(Cnt)); int N, M; scanf( %d , &N); scanf( %d , &M); for (int i = 0; i < M; i++) { int A, B; scanf( %d , &A); scanf( %d , &B); Edge(A, B); } int Ans = 0; bool cont = 1; while (cont) { cont = 0; vector<int> mf; mf.empty(); for (int i = 1; i <= N; i++) { if (Cnt[i] == 1) { cont = 1; Cnt[i] = 0; for (int j = 1; j <= N; j++) if (Adj[i][j]) { Adj[j][i] = 0; mf.push_back(j); break; } } } if (cont) { Ans++; for (int i = 0; i < mf.size(); i++) Cnt[mf[i]]--; } } printf( %d , Ans); }
#include <bits/stdc++.h> using namespace std; typedef long long big; #define bigvec vector<big> #define pbb pair<big, big> #define pii pair<int, int> #define fe first #define se second #define maxheap priority_queue #define uset unordered_set #define umap unordered_map #define fr(i, j) for(big i = 0; i < j; i++) #define revfr(i, j) for(big i = j - 1; i >= 0; i--) #define speed ios_base::sync_with_stdio(false); cin.tie(NULL) #define debug(text) if(do_debug) {cout << text << endl;} const big mod = 1000000007; const big infinity = 1000000000000000000; bool do_debug = false; void rob_santa() { string wasted; getline(cin, wasted); int n_col, n_blocked; cin >> n_col >> n_blocked; vector<pii> blocked (n_blocked); for(int i = 0; i < n_blocked; i++) { cin >> blocked[i].se >> blocked[i].fe; } if(n_blocked == 1) { cout << no << endl; return; } sort(blocked.begin(), blocked.end()); int i = 0; for(; i < n_blocked - 1; i++) { int x = blocked[i].fe; int y = blocked[i].se; //cout << x << , << y << endl; int next_x = blocked[i + 1].fe; int next_y = blocked[i + 1].se; if(blocked[i].fe == blocked[i + 1].fe) { i++; continue; } if(i + 2 < n_blocked && blocked[i + 1].fe == blocked[i + 2].fe) { cout << no << endl; return; } if(next_y == y) { if(next_x - x - 1 == 0) { i++; continue; } else if((next_x - x - 1) % 2 == 0){ i++; continue; } else { cout << no << endl; return; } } else { if((next_x - x - 1) % 2 == 1) { i++; continue; } else { cout << no << endl; return; } } } //cout << i << endl; if(i >= n_blocked) { cout << yes << endl; } else { cout << no << endl; } } int main() { speed; int q; cin >> q; while(q-- > 0) { rob_santa(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int Maxn = 2e5 + 10; long long Inf = 1e18; const int Log = 20; const long long Sqrt = 1000000; const long long Mod = 998244353LL; long long mul(long long a, long long b) { a %= Mod; b %= Mod; return (a * b) % Mod; } long long bin_pow(long long b, long long p) { long long res = 1; for (long long j = 1, pw = b; j <= p; j <<= 1, pw = mul(pw, pw)) if (p & j) res = mul(res, pw); return res; } long long a[Maxn]; long long X = 110000; long long cnt[Maxn]; long long dp[1010][1010]; long long ps[1010][1010]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, k; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; a[0] = -10000000; sort(a + 1, a + n + 1); long long mx = X / max(1LL, (k - 1)); for (int i = 1; i <= mx; i++) { for (int ii = 1; ii <= n; ii++) for (int j = 1; j <= k; j++) dp[ii][j] = 0, ps[ii][j] = 0; long long p1 = 0; dp[0][0] = 1; ps[0][0] = 1; for (int j = 1; j <= n; j++) ps[j][0] = 1; for (int j = 1; j <= n; j++) { while (a[p1] + i <= a[j]) { p1++; } p1--; for (int K = 1; K <= k; K++) { dp[j][K] = ps[p1][K - 1]; ps[j][K] = (ps[j - 1][K] + dp[j][K]) % Mod; } cnt[i] += dp[j][k]; cnt[i] %= Mod; } } long long ans = 0; for (int i = 1; i <= mx; i++) { ans += mul(i, cnt[i] - cnt[i + 1]); ans %= Mod; } ans += Mod; cout << ans % Mod; return 0; }
#include <bits/stdc++.h> using namespace std; int n; string c[int(20)]; int r[int(20)], b[int(20)]; int count(int mask, char color) { int ans = 0; for (int s = 0, __R = n - 1; s <= __R; s++) if (!((mask >> s) & 1) && c[s][0] == color) ans++; return ans; } int f(int i, int j, int k) { if (!i) return 0; static int dp[1 << 16][2][(1 << 8) + 1]; static bool mark[1 << 16][2][(1 << 8) + 1]; auto& ans = dp[i][j][k]; if (mark[i][j][k]) return ans; mark[i][j][k] = true; ; int R = count(i, R ), B = count(i, B ); int rs = j ? k : 0, bs = j ? 0 : k; ans = 0x3f3f3f3f3f3f3f3fLL; for (int s = 0, __R = n - 1; s <= __R; s++) if ((i >> s) & 1) { int cr = max(0, r[s] - R), cb = max(0, b[s] - B); int fr = max(0, cr - rs), fb = max(0, cb - bs); int turns = max(fr, fb); int rs_ = rs + turns - cr, bs_ = bs + turns - cb; ans = min(ans, f(i & ~(1 << s), rs_ ? 1 : 0, max(rs_, bs_)) + turns); } return ans; } int main() { int B = 0, R = 0; cin >> n; for (int i = 0, __R = n - 1; i <= __R; i++) { cin >> c[i] >> r[i] >> b[i]; B += max(0, b[i] - n); R += max(0, r[i] - n); b[i] = min(b[i], n); r[i] = min(r[i], n); } cout << f((1 << n) - 1, B > R ? 1 : 0, min(n * n, max(B, R) - min(B, R))) + max(B, R) + n << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long int mod = 1e9 + 7; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; int main() { int n; cin >> n; string s; cin >> s; s += . ; n++; vector<pair<char, int>> a; int c = s[0]; int cnt = 1; for (int i = 1; i < n; i++) { if (c == s[i]) { cnt++; } else { a.push_back({c, cnt}); c = s[i]; cnt = 1; } } for (int i = 0; i < a.size(); i++) { if (a[i].first == a || a[i].first == e || a[i].first == i || a[i].first == o || a[i].first == u || a[i].first == y ) { if ((a[i].first == e || a[i].first == o ) && a[i].second == 2) { cout << a[i].first << a[i].first; } else { cout << a[i].first; } } else { for (int j = 0; j < a[i].second; j++) cout << a[i].first; } } cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n, x, m, y; int main() { int i, j, k, flag; long long ans; int l1, l2, r1, r2, l, r; int cnt; while (scanf( %d%d%d%d , &n, &x, &m, &y) != EOF) { if (x > y) { k = x; x = y; y = k; } ans = 1; flag = 0; for (i = 1; i <= n; i++) { k = ans; if (i + x == y) { r1 = y - x + i; r = min(r1, m); cnt = 0; if (r1 <= r) { cnt++; } ans += 2 * (r - cnt) + cnt; flag = 1; } else if (i + x > y) { l1 = i + x - y; r1 = y - x + i; if (m < l1) { ans++; continue; } r = min(r1, m); cnt = 1; if (r1 <= r) { cnt++; } ans += (r - l1 + 1 - cnt) * 2 + cnt; flag = 1; } else { l1 = y - x - i; r1 = y - x + i; if (m < l1) { ans++; continue; } cnt = 1; r = min(r1, m); if (r1 <= r) { cnt++; } ans += 2 * (r - l1 + 1 - cnt) + cnt; flag = 1; } } for (i = 1; i <= m; i++) { if (i + x == y) { continue; } else if (i + x > y) { l1 = i + x - y; r1 = y - x + i; if (n < l1) { ans++; } } else { l1 = y - x - i; r1 = y - x + i; if (n < l1) { ans++; continue; } } } if (flag) ans++; cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; inline long long in() { int32_t x; scanf( %d , &x); return x; } inline long long lin() { long long x; scanf( %lld , &x); return x; } inline string get() { char ch[2000010]; scanf( %s , ch); return ch; } inline void read(long long *a, long long n) { for (long long i = 0; i < n; i++) a[i] = in(); } template <class blank> inline void out(blank x) { cout << x << n ; exit(0); } const long long maxn = 1e5 + 10; const long long maxm = 1e6 + 10; const long long maxlg = 20; const long long base = 29; const long long base2 = 31; const long long mod2 = 1e9 + 9; const long long mod = 1e9 + 7; const long long INF = 1e18 + 10; const long long SQ = 317 + 5; vector<pair<long long, long long> > g[maxn]; bool mark[maxn]; long long where[maxn]; long long cur = 1; long long n, m; vector<long long> adj[maxn]; vector<long long> vertices[maxn]; long long cnt[maxn], has[maxn]; long long root[maxn][maxlg]; bool cut[maxn]; long long h[maxn], l[maxn]; long long pre[maxn]; inline void tarjan(long long node, long long fa = 0) { h[node] = l[node] = (fa ? h[fa] + 1 : 0); pre[node] = fa; mark[node] = true; for (auto u : g[node]) { if (!mark[u.first]) { tarjan(u.first, node); l[node] = min(l[node], l[u.first]); if (l[u.first] > h[node]) cut[u.second] = true; } else if (u.first != fa && h[u.first] < h[node]) { for (long long V = node; V != pre[u.first]; V = pre[V]) { vertices[cur].push_back(V); where[V] = cur + n; } cur++; } if (u.first != fa) { l[node] = min(l[node], h[u.first]); if (l[u.first] > h[node]) cut[u.second] = true; } } } inline void dfs(long long node, long long fa = 0) { cnt[node] += cnt[fa]; root[node][0] = fa; for (long long i = 1; i < maxlg; i++) root[node][i] = root[root[node][i - 1]][i - 1]; for (auto u : adj[node]) { if (u - fa) { h[u] = h[node] + 1; dfs(u, node); } } } long long pr[maxn]; inline long long Root(long long v) { return pr[v] == v ? v : pr[v] = Root(pr[v]); } inline void merge(long long v, long long u) { v = Root(v), u = Root(u); pr[v] = u; } inline long long lca(long long p, long long q) { if (h[p] < h[q]) swap(p, q); for (long long i = maxlg - 1; i >= 0; i--) if (h[p] - (1 << i) >= h[q]) p = root[p][i]; if (p == q) return p; for (long long i = maxlg - 1; i >= 0; i--) if (root[p][i] != root[q][i]) p = root[p][i], q = root[q][i]; return root[p][0]; } inline long long CNT(long long v, long long u) { long long LCA = lca(v, u); return cnt[v] + cnt[u] - 2 * cnt[LCA] + has[LCA]; } long long pw[maxn]; pair<long long, long long> e[maxn]; int32_t main() { pw[0] = 1; for (long long i = 1; i < maxn; i++) pw[i] = pw[i - 1] * 2 % mod; n = in(), m = in(); for (long long i = 1; i <= n; i++) pr[i] = where[i] = i; for (long long i = 0; i < m; i++) { long long v = in(), u = in(); g[v].push_back({u, i}); g[u].push_back({v, i}); merge(v, u); e[i] = {v, u}; } tarjan(1); memset(h, 0, sizeof h); for (long long i = 0; i < m; i++) if (cut[i]) { adj[where[e[i].first]].push_back(where[e[i].second]), adj[where[e[i].second]].push_back(where[e[i].first]); } for (long long i = 1; i < cur; i++) { for (auto v : vertices[i]) adj[v].push_back(i + n), adj[i + n].push_back(v); } for (long long i = 1; i < cur; i++) cnt[i + n] = 1, has[i + n] = true; dfs(1); long long q = in(); while (q--) { long long v = in(), u = in(); cout << pw[CNT(v, u)] << n ; } }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; scanf( %d%d , &n, &m); if (n < 2) printf( %d n , n); else if (n / 2 < m) printf( %d n , m - 1); else printf( %d n , m + 1); }
#include <bits/stdc++.h> using namespace std; const int N = 1e5; const int B = 350; int a[N]; int b[N]; int tot = 0; mt19937 rng(4112003); struct Node { Node *l, *r; Node *p; int key, pri; int nCh, cnt[B]; Node(int x = 0) : key(x), pri(rng() << 16 | rng()), nCh(1) { l = r = 0; for (int i = 0; i < tot; ++i) cnt[i] = 0; if (b[x] >= 0) cnt[b[x]] = 1; }; } * p[N]; int Siz(Node *x) { if (x) return x->nCh; else return 0; } void pull(Node *x) { Node *u = x->l; Node *v = x->r; x->nCh = 1; x->nCh += Siz(u); x->nCh += Siz(v); for (int i = 0; i < tot; ++i) { x->cnt[i] = 0; if (u) x->cnt[i] += u->cnt[i]; if (v) x->cnt[i] += v->cnt[i]; } if (b[x->key] >= 0) x->cnt[b[x->key]]++; if (u) u->p = x; if (v) v->p = x; } Node *join(Node *x, Node *y) { if (!x) return y; if (!y) return x; if (x->pri > y->pri) { x->r = join(x->r, y); pull(x); return x; } else { y->l = join(x, y->l); pull(y); return y; } } void split(Node *x, Node *&l, Node *&r, int p) { if (!x) { l = r = 0; return; } if (p >= Siz(x->l) + 1) { p -= Siz(x->l) + 1; split(x->r, x->r, r, p); pull(l = x); } else split(x->l, l, x->l, p), pull(r = x); } vector<int> pos[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); srand(time(NULL)); int n; cin >> n; for (int i = 0; i < n; ++i) { int x; cin >> x; --x; pos[x].emplace_back(i); a[i] = x; } for (int i = 0; i < n; ++i) { if (pos[i].size() > B) b[i] = tot++; else b[i] = -1; } Node *R = 0; for (int i = 0; i < n; ++i) R = join(R, p[i] = new Node(a[i])); int q; cin >> q; int lst = 0; for (int i = 0; i < q; ++i) { int t; cin >> t; int l; cin >> l; l = (l + lst - 1) % n; int r; cin >> r; r = (r + lst - 1) % n; if (l > r) swap(l, r); if (t == 1) { Node *x, *y, *z; split(R, R, x, l); split(x, x, y, r - l + 1); split(x, x, z, r - l); x = join(z, x); x = join(x, y); R = join(R, x); } if (t == 2) { int x; cin >> x; x = (x + lst - 1) % n; lst = 0; if (b[x] >= 0) { Node *p, *q; split(R, R, p, l); split(p, p, q, r - l + 1); lst = p->cnt[b[x]]; p = join(p, q); R = join(R, p); } else { for (int a : pos[x]) { Node *T = p[a]; int cur = Siz(T->l); while (T != R) { if (T->p->r == T) cur += Siz(T->p->l) + 1; T = T->p; } if (cur < l) continue; if (cur > r) continue; lst++; } } cout << lst << n ; } } }
#include <bits/stdc++.h> using namespace std; template <class T> inline bool checkMax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <typename T, typename... Args> inline void checkMax(T &a, const Args... arg) { checkMax(a, max(arg...)); } template <class T> inline bool checkMin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template <typename T, typename... Args> inline void checkMin(T &a, const Args... arg) { checkMin(a, min(arg...)); } const int INF = 0x3f3f3f3f; const long long llINF = 0x3f3f3f3f3f3f3f3f; const int MAXN = 2e5 + 5; int n, m, K, cnt, tot; int a[MAXN]; long long num[MAXN]; struct Rect { int x100, y100, x2, y2; } rec[MAXN]; struct Line { int x, y100, y2, op; } line[MAXN]; struct SegmentTree { static const int MAXM = MAXN << 2; struct Tree { int l, r; int cov, sum; } tree[MAXM]; void update(int k) { if (tree[k].cov > 0) tree[k].sum = a[tree[k].r] - a[tree[k].l - 1]; else tree[k].sum = tree[k << 1].sum + tree[k << 1 | 1].sum; } void build(int k, int l, int r) { tree[k].l = l; tree[k].r = r; tree[k].cov = tree[k].sum = 0; if (l == r) return; int mid = (l + r) >> 1; build(k << 1, l, mid); build(k << 1 | 1, mid + 1, r); } void modify(int k, int l, int r, int v) { if (tree[k].l >= l && tree[k].r <= r) { tree[k].cov += v; update(k); return; } int mid = (tree[k].l + tree[k].r) >> 1; if (l <= mid) modify(k << 1, l, r, v); if (r > mid) modify(k << 1 | 1, l, r, v); update(k); } int query() { return tree[1].sum; } } T; long long Solve(int t) { tot = cnt = 0; for (int i = 1; i <= m; i++) { int x100 = (rec[i].x100 - 1) / t + 1, x2 = rec[i].x2 / t; int y100 = (rec[i].y100 - 1) / t + 1, y2 = rec[i].y2 / t; if (x100 > x2 || y100 > y2) continue; a[++cnt] = y100 - 1, a[++cnt] = y2; line[++tot] = {x100 - 1, y100, y2, 1}; line[++tot] = {x2, y100, y2, -1}; } if (!tot) return 0; sort(a + 1, a + 1 + cnt); cnt = unique(a + 1, a + 1 + cnt) - a - 1; T.build(1, 1, cnt); sort(line + 1, line + 1 + tot, [](const Line &x, const Line &y) { return x.x < y.x; }); long long ans = 0; for (int i = 1; i <= tot; i++) { ans += (long long)(line[i].x - line[i - 1].x) * T.query(); int y100 = lower_bound(a + 1, a + 1 + cnt, line[i].y100) - a; int y2 = lower_bound(a + 1, a + 1 + cnt, line[i].y2) - a; T.modify(1, y100, y2, line[i].op); } return ans; } int main() { scanf( %d%d%d , &n, &m, &K); for (int i = 1; i <= m; i++) { int x100, y100, x2, y2; scanf( %d%d%d%d , &x100, &y100, &x2, &y2); rec[i] = {x100, y100, x2, y2}; } int w = 0; while ((1 << (w + 1)) <= K) w += 1; for (int i = 0; i <= w; i++) { num[i] = Solve(1 << i); if (i && ((num[i] - num[i - 1]) & 1)) { printf( Hamed ); return 0; } } if (num[w] & 1) printf( Hamed n ); else printf( Malek n ); return 0; }
#include <bits/stdc++.h> using namespace std; int N, Q; string s; struct Info { int len; int l_len, r_len; char left, right; int ans; void Output() { printf( len=%d l_len=%d r_len=%d left= %c right= %c ans=%d n , len, l_len, r_len, left, right, ans); } }; class TreeNode { public: TreeNode *lch, *rch; int l, r; bool tag; Info front, back; TreeNode(int l, int r) : l(l), r(r) { tag = false; } void TurnOver() { tag = !tag; swap(front, back); } void PushDown() { if (tag) { tag = false; lch->TurnOver(); rch->TurnOver(); } } void Update() { front = Merge(lch->front, rch->front); back = Merge(lch->back, rch->back); } void Output() { printf( l: %d r: %d tag: %s n , l, r, tag ? true : false ); printf( front: ); front.Output(); printf( back: ); back.Output(); } static Info Merge(const Info& a, const Info& b) { Info now; now.len = a.len + b.len; now.left = a.left; now.right = b.right; now.ans = max(a.ans, b.ans); if ((a.right == < && b.left == < ) || (a.right == > && b.left == > ) || (a.right == > && b.left == < )) { if (a.r_len == a.len) { now.l_len = a.len + b.l_len; } else { now.l_len = a.l_len; } if (b.l_len == b.len) { now.r_len = b.len + a.r_len; } else { now.r_len = b.r_len; } now.ans = max(now.ans, a.r_len + b.l_len); } else { now.l_len = a.l_len; now.r_len = b.r_len; } return now; } }; TreeNode* BuildTree(const string& s, int l, int r) { auto v = new TreeNode(l, r); if (l == r) { v->front.len = 1; v->front.l_len = v->front.r_len = 1; v->front.left = v->front.right = < ; v->front.ans = 1; v->back.len = 1; v->back.l_len = v->back.r_len = 1; v->back.left = v->back.right = > ; v->back.ans = 1; if (s[l] == > ) swap(v->front, v->back); return v; } int mid = (l + r) / 2; v->lch = BuildTree(s, l, mid); v->rch = BuildTree(s, mid + 1, r); v->Update(); return v; } void Change(TreeNode* v, int ww, int ee, vector<TreeNode*>& visit) { if (ww <= v->l && v->r <= ee) { v->TurnOver(); visit.push_back(v); return; } v->PushDown(); int mid = (v->l + v->r) / 2; if (ww <= mid) Change(v->lch, ww, ee, visit); if (ee > mid) Change(v->rch, ww, ee, visit); v->Update(); } int main() { cin >> N >> Q; cin >> s; auto root = BuildTree(s, 0, N - 1); vector<TreeNode*> visit; for (int i = 0; i < Q; i++) { int a, b; cin >> a >> b; a--, b--; visit.clear(); Change(root, a, b, visit); Info info = visit[0]->front; for (int i = 1; i < visit.size(); i++) { info = TreeNode::Merge(info, visit[i]->front); } cout << info.ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int min2 = n, max1 = 1; while (m--) { int u, v; cin >> u >> v; if (v > u) { v = u ^ v; u = v ^ u; v = u ^ v; } min2 = min(u, min2); max1 = max(v, max1); } cout << max(0, min2 - max1); }
#include <bits/stdc++.h> using namespace std; int main() { string s; static int x[27]; int n, k; cin >> n >> k; cin >> s; for (int i = 0; i < k; i++) { char r; cin >> r; x[r - a ] = 1; } long long res = 0, tr = 0; for (int i = 0; s[i]; i++) { if (x[s[i] - a ]) tr++; else { res += (tr * (tr + 1)) / 2; tr = 0; } } res += (tr * (tr + 1)) / 2; cout << res; return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> parent; vector<int> Rank; int root(int v) { if (parent[v] == v) return v; return parent[v] = root(parent[v]); } void DSU(int a, int b) { a = root(a); b = root(b); if (a != b) { if (Rank[b] > Rank[a]) swap(a, b); Rank[a] += Rank[b]; parent[b] = a; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m, q; cin >> n >> m >> q; parent.resize(n + m + 3); for (int i = 0; i < parent.size(); i++) { parent[i] = i; } Rank.resize(n + m + 3, 1); for (int i = 0; i < q; i++) { int a, b; cin >> a >> b; b += n; DSU(a, b); } set<int> roots; for (int i = 1; i <= n + m; i++) { roots.insert(root(i)); } cout << roots.size() - 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > Sh; const int tmax = 155; char table[tmax][tmax]; pair<int, int> mow[tmax]; pair<int, int> ccount(int row, int side, int ai) { if (mow[row].second == 0 && mow[row + 1].second == 0) return make_pair(0, ai); if (mow[row].second == 0) { if (side == 0 && ai < mow[row + 1].second) return make_pair(mow[row + 1].second - ai, mow[row + 1].second); else if (side == 1 && ai >= mow[row + 1].first) return make_pair(ai - mow[row + 1].first, mow[row + 1].first); else return make_pair(0, ai); } if (mow[row + 1].second == 0) { if (side == 0) return make_pair(mow[row].second - ai, mow[row].second); else return make_pair(ai - mow[row].first, mow[row].first); } if (side == 0) return make_pair(max(mow[row + 1].second, mow[row].second) - ai, max(mow[row + 1].second, mow[row].second)); else return make_pair(ai - min(mow[row + 1].first, mow[row].first), min(mow[row + 1].first, mow[row].first)); } int main() { int N, M; scanf( %d %d , &N, &M); for (int i = 0; i < tmax; i++) mow[i].first = 1 << 30, mow[i].second = 0; int last = 0; for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) { scanf( %c , &table[i][j]); if (table[i][j] == W ) { mow[i].first = min(j, mow[i].first); mow[i].second = max(j, mow[i].second); last = max(last, i); } } } int side = 0, howmany = 0; int ai = 1; for (int i = 1; i < N; i++) { pair<int, int> ret = ccount(i, side, ai); howmany += ret.first; ai = ret.second; side ^= 1; } if (mow[N].second) { if (side) howmany += ai - mow[N].first; else howmany += mow[N].second - ai; } printf( %d n , max(howmany + last - 1, 0)); return 0; }
#include <bits/stdc++.h> using namespace std; ofstream outfile; ifstream infile; int main() { infile.open( input.txt ); outfile.open( output.txt ); long long int a, b; cin >> a >> b; if (a > b) { cout << 0 n ; return 0; } else if (b - a <= 20) { long long int digit = 1; for (long long int i = a + 1; i <= b; ++i) { digit = (digit * i) % 10; } cout << digit << n ; return 0; } cout << 0 n ; }
#include <bits/stdc++.h> using namespace std; template <typename T> T inline SQR(const T &a) { return a * a; } long long MOD = 1000000007LL; long long mypow(long long a, long long p) { if (p == 0) return 1LL; long long ans = mypow(a, p / 2); ans *= ans; ans %= MOD; if (p & 1) return (a * ans) % MOD; return ans; } int n, m, k; int dp[1005][1005]; int solve(int pLeft, int sd) { if (pLeft == 0) return 1; if (sd <= 0) return 0; int &val = dp[pLeft][sd]; if (val != -1) return val; long long aux = 0; for (int i = 1; i < sd - 1; ++i) { aux += 1ll * (sd - 1 - i) * solve(pLeft - 1, i); } return val = aux % MOD; } int main() { ios::sync_with_stdio(false); cin >> n >> m >> k; int MAXS = max(n, m) + 1; for (int i = 0; i < k + 1; ++i) for (int j = 0; j < MAXS; ++j) dp[i][j] = -1; cout << (1ll * solve(k, n) * solve(k, m)) % MOD << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int apsz = 26; const long long o = 1; const int maxn = 300007; const int ooi = 1e9 + 7; const int up = 62; int n; long long mask[maxn]; long long val[maxn]; inline int hb(long long x) { int k = 0; while ((o << (k + 1)) <= x) k++; return k; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; long long sum = 0; for (int i = (0); i < (n); ++i) cin >> val[i] >> mask[i], sum += val[i]; vector<vector<int> > pos(up); vector<vector<int> > exist(up); for (int i = (0); i < (n); ++i) { pos[hb(mask[i])].push_back(i); for (int j = (0); j < (up); ++j) if ((o << j) & mask[i]) exist[j].push_back(i); } long long ans = 0; for (int i = (0); i < (up); ++i) { if (pos[i].empty()) continue; long long tmp = 0; for (auto x : pos[i]) tmp += val[x]; if (tmp == 0) continue; if (tmp > 0 == sum > 0) { ans |= (o << (long long)i); for (auto x : exist[i]) val[x] = -val[x]; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n; long long res = 0; int main() { cin >> n; long long tmp = 0; int indez = 1; for (int i = 1; i <= n - 1; i++) { tmp += indez; indez += 2; } res = tmp * 2; res += (n * 2 - 1); cout << res; return 0; }