solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; int const N = 150 + 20; int n, a[N], dp[N][N], full[N][N][N]; string s; bool pal[N][N], mark[N][N][N]; void sMax(int &a, int b) { a = max(a, b); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; cin >> s; memset(full, -1, sizeof full); for (int len = 0; len <= n; len++) { for (int i = 0; i + len <= n; i++) { int j = i + len; if (len <= 1) pal[i][j] = 1; else if (s[i] == s[j - 1]) pal[i][j] = pal[i + 1][j - 1]; } } for (int i = 0; i < n; i++) { mark[i][i][0] = 1; full[i][i][0] = 0; mark[i][i + 1][1] = 1; full[i][i + 1][1] = a[1]; if (~a[1]) { sMax(dp[i][i + 1], a[1]); sMax(full[i][i + 1][0], a[1]); mark[i][i + 1][0] = 1; } } for (int len = 2; len <= n; len++) { for (int i = 0; i + len <= n; i++) { int j = i + len; if (pal[i][j]) { mark[i][j][j - i] = 1; sMax(full[i][j][j - i], a[j - i]); if (~a[j - i]) sMax(full[i][j][0], full[i][j][j - i]); } for (int k = i + 1; k < j; k++) { sMax(dp[i][j], dp[i][k] + dp[k][j]); for (int l = 0; l <= j - i; l++) { if (mark[i][k][l] && mark[k][j][0]) { sMax(full[i][j][l], full[i][k][l] + full[k][j][0]); mark[i][j][l] = 1; if (~a[l]) sMax(full[i][j][0], full[i][j][l]); } if (mark[k][j][l] && mark[i][k][0]) { sMax(full[i][j][l], full[i][k][0] + full[k][j][l]); mark[i][j][l] = 1; if (~a[l]) sMax(full[i][j][0], full[i][j][l]); } } } if (s[i] == s[j - 1]) for (int k = 0; k <= j - i - 2; k++) { if (mark[i + 1][j - 1][k]) { int x = full[i + 1][j - 1][k]; sMax(full[i][j][k + 2], x - a[k] + a[k + 2]); mark[i][j][k + 2] = 1; if (~a[k + 2]) { sMax(full[i][j][0], full[i][j][k + 2]); mark[i][j][0] = 1; } } } sMax(dp[i][j], full[i][j][0]); } } return cout << dp[0][n] << endl, 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { char ch = 1; vector<int> v; v.push_back(1); while (ch != '=') { scanf(" %c", &ch); if (ch == '?') continue; if (ch == '+') v.push_back(1); if (ch == '-') v.push_back(-1); } int n; scanf("%d", &n); int men = 0, mai = 0; for (int i = 0; i < v.size(); i++) { if (v[i] == 1) men++, mai += n; else men -= n, mai--; }; if (men > n || mai < n) { printf("Impossible\n"); return 0; } printf("Possible\n"); for (int i = 0; i < v.size(); i++) { if (i) { if (v[i] == 1) printf(" + "); else printf(" - "); } int dif = mai - n; int u = min(dif, n - 1); mai -= u; if (v[i] == 1) printf("%d", n - u); else printf("%d", 1 + u); } printf(" = %d\n", n); }
1
#include<iostream> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<vector> using namespace std; const int maxn=1e5+10; int n,ord[maxn],cnt,deg[maxn],ans1[maxn],ans2[maxn]; vector<int> vec[maxn],g[maxn]; int main(){ scanf("%d",&n); for(int i=1,u,v;i<n;++i){ scanf("%d%d",&u,&v); vec[u].push_back(v); vec[v].push_back(u); } if(n<=3){ for(int i=1;i<=n;++i) printf("%d\n",i); return 0; } for(int i=1;i<=n;++i) if(vec[i].size()==1) ++deg[vec[i][0]]; else for(int j=0;j<vec[i].size();++j) if(vec[vec[i][j]].size()!=1) g[i].push_back(vec[i][j]); for(int i=1;i<=n;++i) if(g[i].size()>2){ puts("-1"); return 0; } for(int i=1;i<=n;++i) if(g[i].size()==1){ for(int p=i,last=-1;;){ ord[++cnt]=p; bool flag=false; for(int j=0;j<g[p].size();++j) if(g[p][j]!=last){ flag=true; last=p; p=g[p][j]; break; } if(!flag) break; } goto fxckglt; } printf("1 "); for(int i=2;i<=n-2;++i) printf("%d ",i+1); printf("2 %d",n); return 0; fxckglt: --deg[ord[1]]; --deg[ord[cnt]]; ++cnt; for(int i=0,top=0;i<=cnt;++i){ for(int j=0;j<deg[ord[i]];++j) ++top,ans1[top]=top+1; ++top; ans1[top]=top-deg[ord[i]]; } for(int i=cnt,top=0;~i;--i){ for(int j=0;j<deg[ord[i]];++j) ++top,ans2[top]=top+1; ++top; ans2[top]=top-deg[ord[i]]; } for(int i=1;i<=n;++i) if(ans1[i]<ans2[i]){ for(int j=1;j<=n;++j) printf("%d ",ans1[j]); return 0; } else if(ans1[i]>ans2[i]){ for(int j=1;j<=n;++j) printf("%d ",ans2[j]); return 0; } for(int i=1;i<=n;++i) printf("%d ",ans1[i]); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int INF = INT_MAX; int main() { int n; cin >> n; vector<int> fs(n); vector<int> c(n); vector<vector<int> > dp(n, vector<int>(3, INT_MAX)); for (int i = 0; i < n; i++) { cin >> fs[i]; } for (int i = 0; i < n; i++) { cin >> c[i]; dp[i][0] = c[i]; } for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { if (fs[i] > fs[j]) { dp[i][1] = min(dp[i][1], dp[j][0] + c[i]); } } } for (int i = 1; i < n; i++) { for (int j = 0; j < i; j++) { if (fs[i] > fs[j] && dp[j][1] != INT_MAX) { dp[i][2] = min(dp[i][2], dp[j][1] + c[i]); } } } int ans = dp[0][2]; for (int i = 1; i < n; i++) { ans = min(ans, dp[i][2]); } if (ans == INT_MAX) { cout << "-1\n"; } else cout << ans << "\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { long long n, i, k, target, number, count = 0, position; cin >> n >> k; vector<long long> a; for (i = 0; i < n; i++) { cin >> number; a.push_back(number); } while (n--) { if (a[n] != a[n - 1]) { count = n; break; } } if (count < k) cout << count; else printf("-1"); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; long long v[maxn], t[maxn], sum[maxn]; long long num[maxn]; long long remain[maxn], len[maxn]; long long n; int main() { scanf("%lld", &n); for (int i = 1; i <= n; i++) scanf("%lld", &v[i]); for (int i = 1; i <= n; i++) { scanf("%lld", &t[i]); sum[i] = sum[i - 1] + t[i]; } for (int i = 1; i <= n; i++) { int pos = lower_bound(sum + 1, sum + n + 1, sum[i - 1] + v[i]) - sum; num[i] += 1; num[pos] -= 1; remain[pos] += sum[i - 1] + v[i] - sum[pos - 1]; } for (int i = 1; i <= n; i++) len[i] = len[i - 1] + num[i]; long long ans = 0; for (int i = 1; i <= n; i++) { ans = len[i] * t[i] + remain[i]; printf("%lld%c", ans, i == n ? '\n' : ' '); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { long long i, j, k; long long n, m; cin >> n; m = sqrt(n * 2); j = m * (m + 1) / 2; if (j > n) { m--; j = m * (m + 1) / 2; } k = n % j; if (!k) cout << m << endl; else cout << k << endl; return 0; }
1
#include<iostream> using namespace std; int main() { for(;;){ int x, y; int map[100][100]; int dp[100][100]; cin >> x; cin >> y; if(x == 0 && y == 0){ break; } for(int i = 0; i < y; i++){ for(int j = 0; j < x; j++){ cin >> map[i][j]; } } //ツ鳴催渉可づ可暗ェツ氾板湘」ツづ個療アツづ個づつィツづィツ青板づーツ個按津ィ //0:ツ暗堋督ョツ嘉つ能 //1:ツ湘。ツ害ツ閉ィ //2:ツジツδδδ督プツ妥、 for(int i = 0; i < x; i++){ dp[0][i] = map[0][i] == 0; } const int dx[] = {-1,0,1}; for(int i = 1; i < y; i++){ for(int j = 0; j < x; j++){ if(map[i][j] == 0 || map[i][j] == 2){ dp[i][j] = 0; for(int k = 0; k < 3; k++){ int px = j+dx[k]; if(px < 0 || x <= px || map[i-1][px] == 2 || (map[i][j] == 2 && dx[k] != 0)){ continue; } dp[i][j] += dp[i-1][px]; } if(i-2 >= 0 && map[i-2][j] == 2){ dp[i][j] += dp[i-2][j]; } } else{ dp[i][j] = 0; } } } int ans = 0; /* for(int i = 0; i < y; i++){ for(int j = 0; j < x; j++){ cout<<dp[i][j] << " " << flush; } cout << endl; } */ for(int i = 0; i < x; i++){ ans += dp[y-1][i]; if(y-2 >= 0 && map[y-2][i] == 2){ ans+= dp[y-2][i]; } } cout << ans << endl; } }
0
#include <bits/stdc++.h> using namespace std; const int inf = 0x3c3c3c3c; const long long infl = 0x3c3c3c3c3c3c3c3c; const int MAX_N = 1e5 + 9; int ans[MAX_N]; int main() { cin.tie(NULL); cin.sync_with_stdio(false); cout.sync_with_stdio(false); string str; cin >> str; int strSz = str.size(); int ansSz = 0; int lc = 0, rc = 0; int p = 0; for (int i = 0; i < strSz; i++) { if (str[i] == '(') lc++; else rc++; if (str[i] == '#') { p = i; ans[ansSz] = 1; ansSz++; } } if (lc < rc) return !printf("-1"); for (int i = p - 1; i >= 0; i--) { if (str[i] != '#') continue; str[i] = ')'; } int c = 0; for (int i = 0; i < strSz; i++) { if (str[i] == '(') c++; if (str[i] == ')') c--; if (str[i] == '#') { int v = min(c, lc - rc); ans[ansSz - 1] += v; c -= (v + 1); } if (c < 0) return !printf("-1"); } if (c != 0) return !printf("-1"); for (int i = 0; i < ansSz; i++) printf("%d\n", ans[i]); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int NR = 1e6 + 5; const int MR = 1e6 + 5; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; inline int read() { int x = 0; int bei = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') bei = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * bei; } int u[MR], v[MR]; int du[NR]; int cnt[NR]; int fa[NR]; int getfa(int x) { return x == fa[x] ? x : fa[x] = getfa(fa[x]); } long long ans; int main() { int n = read(), m = read(); for (int i = 1; i <= n; ++i) fa[i] = i; for (int i = 1; i <= m; ++i) { u[i] = read(), v[i] = read(), ++du[u[i]], ++du[v[i]]; fa[getfa(u[i])] = getfa(v[i]); } for (int i = 1; i <= n; ++i) cnt[getfa(i)] += du[i] % 2; int kuai = 0; for (int i = 1; i <= n; ++i) { if (getfa(i) != i) continue; if (!du[i] && getfa(1) != i) continue; ++kuai; if (!cnt[i]) ans += 2; ans += cnt[i]; } if (kuai == 1 && cnt[getfa(1)] == 0) ans -= 2; printf("%lld", ans / 2); return 0; }
3
#include <bits/stdc++.h> using namespace std; int is_pri[100 + 10]; int a[5 * 10000 + 10]; int ape[100 + 10]; int main() { int n; memset(is_pri, 0, sizeof(is_pri)); for (int i = 2; i <= 100; i++) { if (is_pri[i] == 0) is_pri[i] = 1; for (int j = 2; j * i <= 100; j++) { if (is_pri[i * j] == 0) is_pri[i * j] = -1; } } while (cin >> n) { memset(ape, 0, sizeof(ape)); int sum = 0; int minn = 1000; int maxx = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; ape[a[i]] = 1; if (minn > a[i]) minn = a[i]; if (maxx < a[i]) maxx = a[i]; sum += a[i]; } int sum_min = sum; for (; maxx >= 1; maxx--) { if (ape[maxx] == 1 && is_pri[maxx] != 1) { for (int i = 2; i < maxx; i++) { if (maxx % i == 0) { sum_min = min(sum_min, sum - (maxx - maxx / i) + (i * minn - minn)); } } } } cout << sum_min << endl; } }
2
#include <iostream> #include <algorithm> #include <vector> #include <queue> #include <bitset> #include <stack> #include <set> using namespace std; typedef long long ll; typedef pair<int,int> P; typedef pair<int,P> P2; const int inf=1000000000; int dp[101][101][301]={}; ll w1[101]={}; int v1[101]={}; ll km[101]={}; int main() { int n; ll w; cin>>n>>w; for(int i=1;i<n+1;++i){ cin>>w1[i]>>v1[i]; km[i]=km[i-1]+w1[i]-w1[1]; } int r=0; for(int i=1;i<=n;++i){ for(int j=1;j<=i;++j){ for(ll k=0;k<=km[i];++k){ ll k1=w1[i]-w1[1]; if(k1<=k){ dp[i][j][k]=max(dp[i-1][j][k],dp[i-1][j-1][k-k1]+v1[i]); }else{ dp[i][j][k]=dp[i-1][j][k]; } } } } for(int i=1;i<=n;++i){ for(ll k=0;k<=km[n];++k){ if(w>=w1[1]*i+k)r=max(r,dp[n][i][k]); } } cout<<r<<endl; return 0; }
0
#include <bits/stdc++.h> struct point { int x, l; } pp[1000000 * 2]; int qq[1000000 * 2], sk; int pcompare(const void *a, const void *b) { struct point *pa = (struct point *)a; struct point *pb = (struct point *)b; return pa->x != pb->x ? pa->x - pb->x : pb->l - pa->l; } void sort(struct point *pp, int n) { int i; srand(time(NULL)); for (i = n - 1; i >= 0; i--) { struct point tmp; int j = rand() % (i + 1); tmp = pp[i], pp[i] = pp[j], pp[j] = tmp; } qsort(pp, n, sizeof *pp, pcompare); } void sweep(struct point *pp, int n, int k) { int d, i; d = 0; for (i = 0; i <= n; i++) if (pp[i].l) { d++; if (d == k) qq[sk++] = pp[i].x; } else { d--; if (d == k - 1) qq[sk++] = pp[i].x; } printf("%d\n", sk / 2); for (i = 0; i < sk; i++) printf("%d%c", qq[i], i % 2 == 0 ? ' ' : '\n'); } int main() { int n, k, i; scanf("%d%d", &n, &k); for (i = 0; i < n; i++) { int l, r; scanf("%d%d", &l, &r); pp[i * 2].x = l, pp[i * 2].l = 1; pp[i * 2 + 1].x = r, pp[i * 2 + 1].l = 0; } sort(pp, n * 2); sk = 0; sweep(pp, n * 2, k); return 0; }
4
#include <bits/stdc++.h> const int inf = 1061109567; const double eps = 1e-9; using namespace std; int n, m; inline int pw(int a, int b, int c) { if (!b) return 1 % c; int r = pw(a, b / 2, c); r = (1ll * r * r) % c; if (b % 2) r = (1ll * r * a) % c; return r; } int main() { scanf("%d%d", &n, &m); int res = pw(3, n, m) - 1; if (res < 0) res += m; cout << (res) << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 10; int a[MAXN]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); int ans = 0; for (int i = 1; i <= n; i++) { for (int j = i + 1, sm = a[i]; j <= n + 1; j++) { if (sm > 100 * (j - i)) ans = max(ans, j - i); sm += a[j]; } } printf("%d\n", ans); return 0; }
2
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const double pi = acos(-1); const int M = 3e5 + 10; const int inf = 2e9 + 10; const int mod = 998244353; int q, n, a[M], dp[M], nxt[M]; map<int, int> mp[M]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> q; while (q--) { cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n + 1; ++i) { mp[i].clear(); nxt[i] = -1; dp[i] = 0; } for (int i = n; i; --i) { if (mp[i + 1].count(a[i])) { int pos = mp[i + 1][a[i]]; nxt[i] = pos; swap(mp[i], mp[pos + 1]); } mp[i][a[i]] = i; } long long res = 0; for (int i = n; i; --i) { if (nxt[i] == -1) continue; dp[i] = dp[nxt[i] + 1] + 1; res += dp[i]; } cout << res << '\n'; } }
6
#include <bits/stdc++.h> using namespace std; int main() { cin.sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit); long long p, k; cin >> p >> k; vector<int> ans; while (p >= k) { ans.push_back(p - (p / k) * k); p = p / k; ans.push_back((p + k - 1) / k * k - p); p = (p + k - 1) / k; } ans.push_back(p); while (!ans.empty() && ans.back() == 0) ans.pop_back(); if (ans.empty()) cout << 1 << '\n' << 0; else { cout << (int)(ans).size() << '\n'; for (auto& i : ans) cout << i << ' '; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, q; scanf("%lld %lld %lld", &n, &m, &q); long long a[n][m]; long long b[q], d[q], cou = 0, t, r, c, x; for (long long i = 0; i < q; i++) { scanf("%lld", &t); if (t == 1) { scanf("%lld", &r); b[cou] = t; d[cou] = r - 1; cou++; x = a[r - 1][0]; for (long long j = 0; j < m - 1; j++) a[r - 1][j] = a[r - 1][j + 1]; a[r - 1][m - 1] = x; } else if (t == 2) { scanf("%lld", &c); b[cou] = t; d[cou] = c - 1; cou++; x = a[0][c - 1]; for (long long j = 0; j < n - 1; j++) a[j][c - 1] = a[j + 1][c - 1]; a[n - 1][c - 1] = x; } else { scanf("%lld %lld %lld", &r, &c, &x); a[r - 1][c - 1] = x; } } for (long long i = 0; i < cou; i++) { if (b[cou - 1 - i] == 1) { r = d[cou - 1 - i]; x = a[r][m - 1]; for (long long j = m - 1; j > 0; j--) a[r][j] = a[r][j - 1]; a[r][0] = x; } else { c = d[cou - 1 - i]; x = a[n - 1][c]; for (long long j = n - 1; j > 0; j--) a[j][c] = a[j - 1][c]; a[0][c] = x; } } for (long long i = 0; i < n; i++) { for (long long j = 0; j < m; j++) if (abs(a[i][j]) > 1000000000) cout << 0 << " "; else cout << a[i][j] << " "; cout << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(NULL); int tc; cin >> tc; while (tc--) { long long n, m; cin >> n >> m; long long ans = 0; long long d = n / 2; ans += d * m; if (n % 2 == 1) { long long q = ceil(m / 2.00); ans += q; } cout << ans << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, cnt, sum, len, tlen, tcnt; int a[26], ans[N], tans[N], id[26], pos[N], tpos[N]; void work1() { printf("0\n"); for (int i = 0; i < n; i++) for (int j = 1; j <= a[i]; j++) printf("%c", 'a' + i); } bool cmp(int x, int y) { return a[x] > a[y]; } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int t, d; void work(int x, int y) { int j = 0; tcnt = tlen = 0; for (int i = x; i <= cnt; i += x) { while (j + 1 <= pos[i]) { j++; tans[++tlen] = ans[j]; } if (i % (2 * x) == 0) { for (int l = 1; l <= y; l++) tans[++tlen] = t; tpos[++tcnt] = tlen; for (int l = 1; l <= y; l++) tans[++tlen] = t; } else tpos[++tcnt] = tlen; } while (j < len) tans[++tlen] = ans[++j]; cnt = tcnt; for (int i = 1; i <= cnt; i++) pos[i] = tpos[i]; len = tlen; for (int i = 1; i <= len; i++) ans[i] = tans[i]; } void work2(int x, int y) { int j = 0; tlen = 0; for (int i = x; i <= cnt; i += x) { while (j + 1 <= pos[i]) { j++; tans[++tlen] = ans[j]; } for (int l = 1; l <= y; l++) tans[++tlen] = t; } while (j < len) tans[++tlen] = ans[++j]; len = tlen; for (int i = 1; i <= len; i++) ans[i] = tans[i]; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]), cnt += a[i] & 1, sum += a[i], id[i] = i; if (cnt > 1) { work1(); return 0; } sort(id, id + n); cnt = 0; for (int i = 0; i < n; i++) { t = id[i]; if (a[t] & 1) continue; if (len == 0) { len = a[t]; cnt = len; for (int i = 1; i <= len; i++) ans[i] = t, pos[i] = i; continue; } d = gcd(cnt, a[t]); work(cnt / d, a[t] / d); } for (int i = 0; i < n; i++) if (a[id[i]] & 1) { t = id[i]; if (len == 0) { len = a[t]; cnt = len; for (int i = 1; i <= len; i++) ans[i] = t, pos[i] = i; continue; } d = gcd(cnt, a[t]); work2(cnt / d, a[t] / d); cnt = d; } printf("%d\n", cnt); for (int i = 1; i <= len; i++) printf("%c", 'a' + ans[i]); return 0; }
5
#include <bits/stdc++.h> using namespace std; #define int int64_t template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; } void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #ifdef DEBUG #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif map<char, int> idx; class sum_segTree { public: int n; vector<int> t; sum_segTree(int N) { n = N; t.resize(4 * n + 10); build(0, n - 1, 1); } void build(int L, int R, int V) { if(L == R) { t[V] = 0; return; } int MID = (L + R) / 2; build(L, MID, 2 * V); build(MID + 1, R, 2 * V + 1); t[V] = t[2 * V] + t[2 * V + 1]; } int get_sum(int l, int r, int L, int R, int V) { if(l == L && r == R) return t[V]; int MID = (L + R) / 2; if(r <= MID) return get_sum(l, r, L, MID, 2 * V); if(l > MID) return get_sum(l, r, MID + 1, R, 2 * V + 1); return get_sum(l, MID, L, MID, 2 * V) + get_sum(MID + 1, r, MID + 1, R, 2 * V + 1); } void update(int pos_to_update, int dif, int L, int R, int V) { if(L == R) { t[V] += dif; return; } int MID = (L + R) / 2; if(pos_to_update <= MID) update(pos_to_update, dif, L, MID, 2 * V); else update(pos_to_update, dif, MID + 1, R, 2 * V + 1); t[V] = t[2 * V] + t[2 * V + 1]; } }; int check(const string& given, const string& cur) { int n = int(given.size()); vector<vector<int>> idx_in_cur(4); vector<int> iterator(4); for(int i = 0; i < n; i++) idx_in_cur[idx[cur[i]]].push_back(i); int moves = 0; sum_segTree moved_in(n); for(int i = 0; i < n; i++) { char ch = given[i]; int pos = idx_in_cur[idx[ch]][iterator[idx[ch]]]; iterator[idx[ch]]++; int actual_pos = pos + (pos == n - 1 ? 0 : moved_in.get_sum(pos + 1, n - 1, 0, n - 1, 1)); moves += actual_pos - i; moved_in.update(pos, 1, 0, n - 1, 1); } return moves; } void gogo() { string given; cin >> given; int n = int(given.size()); vector<int> freq(4); string permute = ""; for(char ch : given) { freq[idx[ch]]++; if(freq[idx[ch]] == 1) permute.push_back(ch); } sort(permute.begin(), permute.end()); int ans = 0; string ans_string = ""; do { string cur = ""; for(char ch : permute) { string temp(freq[idx[ch]], ch); cur += temp; } int temp = check(given, cur); dbg(temp, cur); if(temp >= ans) { ans = temp; ans_string = cur; } } while(next_permutation(permute.begin(), permute.end())); cout << ans_string << endl; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); idx['A'] = 0, idx['N'] = 1, idx['T'] = 2, idx['O'] = 3; int tc; cin >> tc; while(tc--) gogo(); return 0; }
4
#include <bits/stdc++.h> using namespace std; mt19937 rd(chrono::steady_clock::now().time_since_epoch().count()); const int N = (int)1e5 + 9, K = 10, F = (int)1e4 + 7; struct node { node *l, *r; int val, prio, sz; node(int v) : l(NULL), r(NULL), val(v), prio(rd()), sz(1) {} } * root[F], *ll, *mm, *rr; int sz(node* u) { return u ? u->sz : 0; } void pull(node* u) { if (u) u->sz = sz(u->l) + sz(u->r) + 1; } void merge(node*& v, node* l, node* r) { if (!l) { v = r; } else if (!r) { v = l; } else if (l->prio > r->prio) { merge(l->r, l->r, r); v = l; } else { merge(r->l, l, r->l); v = r; } pull(v); } void split(node* v, node*& l, node*& r, int x) { if (!v) { l = r = 0; } else if (x <= v->val) { split(v->l, l, v->l, x); r = v; } else { split(v->r, v->r, r, x); l = v; } pull(v); } int get(node*& u, int l, int r) { split(u, ll, rr, r + 1); split(ll, ll, mm, l); int ans = sz(mm); merge(u, ll, mm); merge(u, u, rr); return ans; } void add(node*& u, int x) { split(u, ll, rr, x); mm = new node(x); merge(u, ll, mm); merge(u, u, rr); } struct radio { int x, r, f; bool operator<(const radio& other) const { return r > other.r; } } a[N]; int n, k; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> k; for (int i = 1; i <= n; ++i) cin >> a[i].x >> a[i].r >> a[i].f; sort(a + 1, a + n + 1); long long ans = 0; for (int i = 1; i <= n; ++i) { for (int j = max(0, a[i].f - k), ma = min(F, a[i].f + k + 1); j < ma; ++j) ans += get(root[j], a[i].x - a[i].r, a[i].x + a[i].r); add(root[a[i].f], a[i].x); } cout << ans; }
5
#include <bits/stdc++.h> int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); size_t n; std::cin >> n; std::vector<int32_t> a(n); int32_t sum = 0; for (size_t i = 0; i < n; ++i) { std::cin >> a[i]; sum += a[i]; } if (sum % 3 != 0) { std::cout << "0\n"; return 0; } const int32_t third = sum / 3; const int32_t two_thirds = third * 2; int64_t count = 0; int32_t cur = 0; int64_t result = 0; for (size_t i = 0; i + 1 < n; ++i) { cur += a[i]; if (cur == two_thirds) { result += count; } if (cur == third) { count += 1; } } std::cout << result << '\n'; }
3
#include <bits/stdc++.h> using namespace std; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; vector<int> f, col, wlk; int Find(int a) { if (f[a] == a) return a; int fa = Find(f[a]); f[a] = fa; return fa; } void Union(int a, int b) { int fa = Find(a); int fb = Find(b); if (fa == fb) return; wlk[fa] += wlk[fb]; f[fb] = f[fa]; } int main() { ios_base::sync_with_stdio(0); int n, m; cin >> n >> m; vector<vector<int> > pan(3 * n, vector<int>(3 * m, 0)); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) cin >> pan[n + i][m + j]; vector<vector<int> > sym(3 * n, vector<int>(3 * m, -1)); for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) cin >> sym[n + i][m + j]; int x, y; cin >> x >> y; x = x - 1 + n; y = y - 1 + m; vector<vector<int> > spi(3 * n, vector<int>(3 * m)); spi[x][y] = 0; int nr = 1, ileMam = 1, d = 1; int akt_x = x, akt_y = y; while (ileMam < 9 * n * m) { for (int kier = 0; kier < 4; ++kier) { for (int i = 0; i < d; ++i) { akt_x += dx[kier]; akt_y += dy[kier]; if (akt_x >= 0 && akt_x < 3 * n && akt_y >= 0 && akt_y < 3 * m) { spi[akt_x][akt_y] = nr; ++ileMam; } ++nr; } if (kier == 1 || kier == 3) ++d; } } map<int, vector<pair<int, pair<int, int> > > > M; map<int, pair<int, int> > gdzieCol; for (int i = 0; i < 3 * n; ++i) { for (int j = 0; j < 3 * m; ++j) { gdzieCol[pan[i][j]] = make_pair(i, j); M[pan[i][j]].push_back(make_pair(-1, make_pair(i, j))); int H = i * 3 * m + j; f.push_back(H); col.push_back(pan[i][j]); wlk.push_back(1); if (M.find(pan[i][j]) != M.end()) { int Hroot = (M[pan[i][j]][0]).second.first * 3 * m + (M[pan[i][j]][0]).second.second; Union(Hroot, H); } } } long long odp = 0; queue<pair<int, int> > Q; Q.push(make_pair(x, y)); while (!Q.empty()) { pair<int, int> akt = Q.front(); Q.pop(); int colPan = col[Find(akt.first * 3 * m + akt.second)]; int colSym = sym[akt.first][akt.second]; if (colPan == 0 || colPan == colSym) continue; if (M[colPan].size() != 0) { for (int i = 0; i < M[colPan].size(); ++i) { int wx = akt.first - x; int wy = akt.second - y; wx *= -1; wy *= -1; M[colPan][i].first = spi[M[colPan][i].second.first + wx] [M[colPan][i].second.second + wy]; } sort(M[colPan].begin(), M[colPan].end()); for (int i = 0; i < M[colPan].size(); ++i) if ((M[colPan][i].second.first != akt.first || M[colPan][i].second.second != akt.second) && sym[M[colPan][i].second.first][M[colPan][i].second.second] != -1) Q.push(M[colPan][i].second); M[colPan].clear(); } odp += wlk[Find(akt.first * 3 * m + akt.second)]; if (gdzieCol.find(colSym) != gdzieCol.end() && gdzieCol[colSym].first != -1) Union(gdzieCol[colSym].first * 3 * m + gdzieCol[colSym].second, akt.first * 3 * m + akt.second); else { col[Find(akt.first * 3 * m + akt.second)] = colSym; gdzieCol[colSym] = akt; } gdzieCol[colPan] = make_pair(-1, -1); } cout << odp; return 0; }
4
#include <iostream> using namespace std; int main() { char p,q; int a,b,c; int x=0,y=0; while(cin >> a >> p >> b >> q >> c) { if(a*a+b*b==c*c) x++; if(a==b) y++; } cout << x << endl << y << endl; }
0
#include<bits/stdc++.h> using namespace std; int square[] = { 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289 }; int ans; void dfs(int num, int upper){ //cout << num << "\t" << upper << endl; for (int i = upper; i >= 0; i--){ if (num > square[i]) dfs(num - square[i], i); else if (num == square[i]) ans++; } return; } int main(void){ int num; while (cin >> num, num){ ans = 0; dfs(num, 16); cout << ans << endl; } return 0; }
0
#include <bits/stdc++.h> #define ed end() #define bg begin() #define mp make_pair #define pb push_back #define all(x) x.bg,x.ed #define newline puts("") #define si(x) ((int)x.size()) #define rep(i,n) for(int i=1;i<=n;++i) #define rrep(i,n) for(int i=0;i<n;++i) #define srep(i,s,t) for(int i=s;i<=t;++i) #define drep(i,s,t) for(int i=t;i>=s;--i) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int Maxn = 1e5+10; const int Inf = 0x7f7f7f7f; const ll Inf_ll = 1ll*Inf*Inf; const int Mod = 1e9+7; const double eps = 1e-7; int n, m; char s[550][550]; int a[550][550]; void solve(){ scanf("%d %d",&n,&m); for(int i=1;i<=n;i++) scanf("%s",s[i]+1); int ans = 0; for(int i=n;i>=1;i--) for(int j=1;j<=m;j++) { if( s[i][j] == '*' ) a[i][j] = min(a[i+1][j-1], min(a[i+1][j], a[i+1][j+1])) + 1; ans += a[i][j]; } printf("%d\n", ans); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) a[i][j] = 0; } int main(){ int T; scanf("%d",&T); for(int _=1;_<=T;_++) { solve(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; #define pp pair<int,int> #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) #define ll long long #define ld long double #define all(a) (a).begin(),(a).end() #define mk make_pair ll MOD=1000000007; int inf=1000001000; ll INF=100000000000000000; int main() { int n,m; cin >> n >> m; vector<vector<int>> e(n+1,vector<int>(0)),a(n+1,vector<int>(n+1,0)); rep(i,m){ int f,g; cin >> f >> g; e.at(f).push_back(g); } for (int i=1;i<n+1;i++){ queue<int> q; q.push(i); a.at(i).at(i)=1; while(!q.empty()){ int t=q.front(); q.pop(); rep(j,e.at(t).size()){ int u=e.at(t).at(j); if (a.at(i).at(u)==0){ q.push(u); a.at(i).at(u)=1; } } } } vector<vector<int>> w(n+1,vector<int>(0)); rep(i,n){ rep(j,n){ if (a.at(i+1).at(j+1)==1 && a.at(j+1).at(i+1)==1) w.at(i+1).push_back(j+1); } } rep(i,n){ rep(j,w.at(i+1).size()-1) cout << w.at(i+1).at(j) << " "; cout << w.at(i+1).at(w.at(i+1).size()-1) << endl; } }
0
#include <bits/stdc++.h> using namespace std; long n, k, i, j, x; map<long, long> b; priority_queue<pair<long, long> > c; int main() { cin >> n >> k; for (i = 0; i < n; i++) { cin >> x; b[x]++; } for (i = 0; i < b.size(); i++) if (b[i] != 0) for (j = 1; j <= b[i]; j++) c.push(make_pair(b[i] / j, i)); for (i = 0; i < k; i++) { cout << c.top().second << " "; c.pop(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int INF = 20000; int n, m; vector<vector<int>> capacity; vector<vector<int>> adj; int bfs(int s, int t, vector<int>& parent) { fill(parent.begin(), parent.end(), -1); parent[s] = -2; queue<pair<int, int>> q; q.push({s, INF}); while (!q.empty()) { int cur = q.front().first; int flow = q.front().second; q.pop(); for (int next : adj[cur]) { if (parent[next] == -1 && capacity[cur][next]) { parent[next] = cur; int new_flow = min(flow, capacity[cur][next]); if (next == t) return new_flow; q.push({next, new_flow}); } } } return 0; } int maxflow(int s, int t) { int flow = 0; vector<int> parent(2 * n + 2); int new_flow; while (new_flow = bfs(s, t, parent)) { flow += new_flow; int cur = t; while (cur != s) { int prev = parent[cur]; capacity[prev][cur] -= new_flow; capacity[cur][prev] += new_flow; cur = prev; } } return flow; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); ; cin >> n >> m; int arr[n + 1], brr[n + 1]; int aa = 0, bb = 0; for (int i = 1; i <= n; ++i) { cin >> arr[i]; aa += arr[i]; } for (int i = 1; i <= n; ++i) { cin >> brr[i]; bb += brr[i]; } if (aa != bb) { cout << "NO\n"; return 0; } vector<int> edge[n + 1]; for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; edge[u].push_back(v); edge[v].push_back(u); } adj.resize(2 * n + 2, vector<int>(0)); capacity.resize(2 * n + 2, vector<int>(2 * n + 2, 0)); for (int i = 1; i <= n; ++i) { adj[0].push_back(i); adj[i].push_back(0); capacity[0][i] = arr[i]; } for (int i = 1; i <= n; ++i) { for (auto ch : edge[i]) { adj[i].push_back(n + ch); adj[n + ch].push_back(i); capacity[i][n + ch] = arr[i]; } adj[i].push_back(i + n); adj[i + n].push_back(i); capacity[i][n + i] = arr[i]; } for (int i = 1; i <= n; ++i) { adj[i + n].push_back(2 * n + 1); adj[2 * n + 1].push_back(n + i); capacity[n + i][2 * n + 1] = brr[i]; } int ans[n + 1][2 * n + 1]; for (int i = 1; i <= n; ++i) { for (int j = n + 1; j <= 2 * n; ++j) { ans[i][j] = capacity[i][j]; } } int ff = maxflow(0, 2 * n + 1); if (ff != aa) { cout << "NO\n"; return 0; } cout << "YES\n"; for (int i = 1; i <= n; ++i) { for (int j = n + 1; j <= 2 * n; ++j) { ans[i][j] -= capacity[i][j]; cout << ans[i][j] << " "; } cout << "\n"; } }
5
#include <bits/stdc++.h> using namespace std; void secrets() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } int n, k; vector<pair<int, pair<int, int>>> b; long long counter = 0; int main() { secrets(); cin >> n >> k; b.resize(n + 2); for (int i = 0; i < n; i++) { b[i].first = 0; b[i].second.first = INT32_MAX; b[i].second.second = 0; } for (int i = 0; i < k; i++) { int temp; cin >> temp; if (b[temp - 1].first == 1) b[temp - 1].second.second = i; else { b[temp - 1].first = 1; b[temp - 1].second.first = i; b[temp - 1].second.second = i; } } for (int i = 0; i < n; i++) { if (b[i].first == 0) { counter++; } if (i > 0) { if (b[i - 1].first == 0 || b[i - 1].second.second < b[i].second.first) { counter++; } } if (i < n - 1) { if (b[i + 1].first == 0 || b[i + 1].second.second < b[i].second.first) { counter++; } } } cout << counter; return 0; }
1
#include<iostream> using namespace std; int main() { int A,B,C; cin>>A>>B>>C; if (A*B*C==175) cout<<"YES"<<endl; else cout<<"NO"<<endl; }
0
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; template <class U, class T> void Max(U &first, T second) { if (first < second) first = second; } template <class U, class T> void Min(U &first, T second) { if (first > second) first = second; } template <class T> void add(int &a, T b) { a = (a + b) % 998244353; } inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } inline long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } int pow(int a, int b) { int ans = 1; while (b) { if (b & 1) ans = 1LL * ans * a % 998244353; a = 1LL * a * a % 998244353; b >>= 1; } return ans; } int pow(int a, int b, int c) { int ans = 1; while (b) { if (b & 1) ans = 1LL * ans * a % c; a = 1LL * a * a % c; b >>= 1; } return ans; } vector<int> g[300010]; int d[300010], p[300010][19], st[300010], ed[300010], cnt, a[300010]; void dfs(int u, int fa = -1) { st[u] = cnt++; for (auto &v : g[u]) { if (v == fa) continue; d[v] = d[u] + 1; p[v][0] = u; for (int i = 1; i < 19; i++) p[v][i] = p[p[v][i - 1]][i - 1]; dfs(v, u); } ed[u] = cnt; } bool cmp(int a, int b) { return st[a] < st[b]; } int Ans(int a, int b) { return st[a] <= st[b] && ed[a] >= ed[b]; } int lca(int a, int b) { if (d[a] < d[b]) swap(a, b); int h = d[a] - d[b]; for (int i = 19 - 1; i >= 0; i--) if (h >> i & 1) a = p[a][i]; if (a == b) return a; for (int i = 19 - 1; i >= 0; i--) if (p[a][i] != p[b][i]) a = p[a][i], b = p[b][i]; return p[a][0]; } int go(int u, int rt, int co) { if (a[rt] == 0) a[rt] = co; else if (a[rt] != co) return 0; while (u != rt) { if (a[u] == 0) a[u] = co; else if (a[u] != co) return 0; u = p[u][0]; } return 1; } int vis[300010], sz, e[300010], f[300010], dp[300010][2]; void dfs1(int u, int fa = -1) { if (a[u]) { dp[u][1] = 0; dp[u][0] = 1; return; } dp[u][1] = 1; for (auto &v : g[u]) { if (vis[v] || v == fa) continue; dfs1(v, u); f[v] = (dp[v][1] + dp[v][0]) % 998244353; dp[u][1] = 1LL * dp[u][1] * f[v] % 998244353; } for (auto &v : g[u]) { if (vis[v] || v == fa) continue; add(dp[u][0], 1LL * dp[u][1] * pow(f[v], 998244353 - 2) % 998244353 * dp[v][0] % 998244353); } a[u] = 1; } vector<int> q[300010]; int main() { int T, ca = 0, k, i, j, m = 0, K, n; scanf("%d%d", &n, &K); for (int i = 0; i < n; i++) { scanf("%d", &k); if (k) q[k].push_back(i); a[i] = k; } for (int i = 1; i < n; i++) { scanf("%d%d", &j, &k); j--, k--; g[j].push_back(k), g[k].push_back(j); } int rt = 0; for (int i = 0; i < n; i++) if (a[i]) { rt = i; break; } for (int i = 0; i < 19; i++) p[rt][i] = rt; dfs(rt); for (int i = 1; i < K + 1; i++) if ((int)q[i].size() > 1) { sz = 0; for (auto &first : q[i]) e[sz++] = first; sort(e, e + sz, cmp); int t = 0, rt = e[0]; for (int j = 1; j < sz; j++) { rt = lca(rt, e[j]); } f[t++] = rt; for (int j = rt == e[0] ? 1 : 0; j < sz; j++) { int fa = lca(f[t - 1], e[j]); while (t > 1 && d[fa] <= d[f[t - 2]]) { if (!go(f[t - 1], f[t - 2], i)) { puts("0"); return 0; } t--; } if (f[t - 1] != fa) { if (!go(f[t - 1], fa, i)) { puts("0"); return 0; } f[t - 1] = fa; } f[t++] = e[j]; } while (t > 1) { if (!go(f[t - 1], f[t - 2], i)) { puts("0"); return 0; } t--; } } queue<int> qu; for (int i = 0; i < n; i++) d[i] = (int)g[i].size(); for (int i = 0; i < n; i++) if (a[i] == 0 && d[i] == 1) { qu.push(i); vis[i] = 1; } while (!qu.empty()) { int u = qu.front(); qu.pop(); for (auto &v : g[u]) { if (a[v] || vis[v]) continue; d[v]--; if (d[v] == 1) vis[v] = 1, qu.push(v); } } int ans = 1; for (int i = 0; i < n; i++) if (a[i] == 0 && !vis[i]) { dfs1(i); ans = 1LL * ans * dp[i][0] % 998244353; } assert(ans > 0); printf("%d\n", ans); }
6
#include <bits/stdc++.h> using namespace std; const int N = 100005; int main() { int n, v, f = 0, m = 0, r = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &v); r = max(r - v, m + v); m = max(m + v, f - v); f -= v; } printf("%d\n", max(f, max(m, r))); return 0; }
3
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ int h,w,x,y; cin >> h >> w >> x >> y; if(h*w%2==1 and (x+y)%2==1){ cout << "No" << endl; }else{ cout << "Yes" << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; using namespace std; int n; int d[1 << 16], s[1 << 16]; vector<pair<int, int> > ans; int main() { cin >> n; set<pair<int, int> > m; for (int i = 0; i < n; i++) { cin >> d[i] >> s[i]; m.insert(pair<int, int>(d[i], i)); } while (!m.empty()) { pair<int, int> v = *m.begin(); if (v.first == 0) { m.erase(v); continue; } ans.push_back(pair<int, int>(v.second, s[v.second])); int b = s[v.second]; s[b] ^= v.second; m.erase(v); v.first--; if (v.first != 0) m.insert(v); m.erase(pair<int, int>(d[b], b)); d[b]--; if (d[b] != 0) m.insert(pair<int, int>(d[b], b)); } cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i].first << " " << ans[i].second << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; //#define int long long const int mod = 1e9+7; int dp[10003][100][2]; int main(){ string K; int D; cin >> K >> D; int n = K.length(); for(int i = 0; i < K[0] - '0'; i++){ dp[0][i%D][0]++; } dp[0][(K[0]-'0')%D ][1]++; for(int i = 1; i < n; i++){ for(int j = 0; j < D; j++){ for(int k = 0; k < 2; k++){ int up = 9; if(k) up = K[i] - '0'; for(int d = 0; d <= up; d++){ int ns = k & (d==up); dp[i][(j+d)%D][ns]+=dp[i-1][j][k]; dp[i][(j+d)%D][ns]%=mod; } } } } int res = 1ll*(dp[n-1][0][0] + dp[n-1][0][1]-1+mod) % mod; cout << res << "\n"; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 5e3 + 2; const int M = 1e3 + 2; int num[N][M]; int flag[N]; int id[N][2]; int res[M][2]; int op[N]; int n, m; map<string, int> has; int ress[M][2]; int ans1[M]; int ans2[M]; void check(int val) { for (int i = 0; i < m; i++) num[0][i] = val; for (int i = 1; i <= n; i++) if (flag[i]) { for (int j = 0; j < m; j++) if (op[i] == 0) num[i][j] = num[id[i][0]][j] ^ num[id[i][1]][j]; else if (op[i] == 1) num[i][j] = num[id[i][0]][j] | num[id[i][1]][j]; else num[i][j] = num[id[i][0]][j] & num[id[i][1]][j]; } for (int i = 1; i <= n; i++) for (int j = 0; j < m; j++) res[j][val] += num[i][j]; } int main() { cin >> n >> m; string a, b, c, d, e; has["?"] = 0; for (int i = 1; i <= n; i++) { cin >> a >> b >> c; if (c[0] == '0' || c[0] == '1') { for (int j = 0; j < m; j++) num[i][j] = c[j] - '0'; has[a] = i; } else { flag[i] = 1; cin >> d >> e; id[i][0] = has[c]; id[i][1] = has[e]; if (d[0] == 'X') op[i] = 0; else if (d[0] == 'O') op[i] = 1; else op[i] = 2; has[a] = i; } } check(0); check(1); for (int i = 0; i < m; i++) { if (res[i][0] > res[i][1]) ans1[i] = 1; if (res[i][0] < res[i][1]) ans2[i] = 1; } for (int i = 0; i < m; i++) cout << ans1[i]; cout << endl; for (int i = 0; i < m; i++) cout << ans2[i]; cout << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int S, T; struct dat { int to, rev, f, cap; dat(int to = 0, int rev = 0, int f = 0, int cap = 0) : to(to), rev(rev), f(f), cap(cap) {} }; vector<dat> v[11000]; int a[11000], b[11000], c[11000], l[11000], r[11000], st[11000]; int dist[11000]; queue<int> q; int work[11000]; void add(int x, int y, int cap) { int n = v[x].size(); int m = v[y].size(); v[x].push_back(dat(y, m, 0, cap)); v[y].push_back(dat(x, n, 0, 0)); } bool bfs() { for (int i = 0; i <= T; i++) dist[i] = 1000000000; while (!q.empty()) q.pop(); q.push(S); dist[S] = 0; while (!q.empty()) { int x = q.front(); q.pop(); for (int i = 0; i < v[x].size(); i++) { dat aa = v[x][i]; if (aa.f == aa.cap) continue; int y = aa.to; if (dist[y] != 1000000000) continue; dist[y] = dist[x] + 1; q.push(y); } } return dist[T] != 1000000000; } int dfs(int x, int f) { if (x == T) return f; for (; work[x] < v[x].size(); work[x]++) { dat aa = v[x][work[x]]; if (aa.f == aa.cap) continue; int y = aa.to; if (dist[y] != dist[x] + 1) continue; int d = dfs(y, min(f, aa.cap - aa.f)); if (d) { v[x][work[x]].f += d; v[y][aa.rev].f -= d; return d; } } return 0; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d%d%d", &a[i], &b[i], &c[i]); } T = 0; S = 0; int val = 400000; for (int i = 1; i <= n; i++) { scanf("%d%d", &l[i], &r[i]); ++T; st[i] = T; add(S, T, 1000000000); for (int j = l[i]; j <= r[i]; j++) { int xx = a[i] * j * j + b[i] * j + c[i]; add(T, T + 1, val - xx); T++; } } ++T; for (int i = 1; i <= n; i++) { add(st[i] + r[i] - l[i] + 1, T, 1000000000); } for (int i = 1; i <= m; i++) { int x, y, d; scanf("%d%d%d", &x, &y, &d); for (int j = l[x]; j <= r[x]; j++) { int xx = j - d; if (xx >= l[y] && xx <= r[y] + 1) { add(st[x] + j - l[x], st[y] + xx - l[y], 1000000000); } } } int ans = val * n; while (bfs()) { for (int i = 0; i <= T; i++) work[i] = 0; while (1) { int d = dfs(S, 1000000000); if (!d) break; ans -= d; } } printf("%d\n", ans); return 0; }
4
#include<bits/stdc++.h> using namespace std; int Decoding(char c,int key){ int num; if(c >= 'A' && c <= 'Z'){ num=c-65; }else{ num=c-71; } num=(num+51*key)%52; return num; } int main(){ char list[52]; string str; int n; int key[100]; for(int i=0 ; i+65 <= 90 ; i++)list[i]=i+65; for(int j=0 ; j+97 <= 122 ; j++)list[j+26]=j+97; while(1){ cin >>n; if(n == 0)break; for(int i=0 ; i < n ; i++)cin >>key[i]; cin >>str; for(int i=0 ; i < (int)str.size() ; i++){ cout <<list[Decoding(str[i],key[i%n])]; } cout <<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int k1, k2; cin >> k1; queue<int> a; for (int i = 0; i < k1; i++) { int x; cin >> x; a.push(x); } cin >> k2; queue<int> b; for (int i = 0; i < k2; i++) { int x; cin >> x; b.push(x); } int count = 0; while (a.size() != 0 && b.size() != 0) { int temp1 = a.front(); int temp2 = b.front(); a.pop(); b.pop(); if (temp1 > temp2) { a.push(temp2); a.push(temp1); } else { b.push(temp1); b.push(temp2); } count++; if (count > 106) { cout << -1 << endl; return 0; } } if (!a.empty()) cout << count << " " << 1 << endl; else cout << count << " " << 2 << endl; }
3
#include <bits/stdc++.h> using namespace std; template <class T> void print(vector<T> a) { int n = a.size(); for (long long i = 0; i < n; ++i) { cout << a[i] << (i == n - 1 ? "\n" : " "); } } int sum_vector(vector<int> v) { return accumulate(v.begin(), v.end(), 0); } void sort_vector(vector<int> &v) { sort(v.begin(), v.end()); } void sort_comp(vector<int> &v, bool func(int, int)) { sort(v.begin(), v.end(), func); } bool comp(int a, int b) { return a < b; } struct CustomCompare { bool operator()(pair<int, int> lhs, pair<int, int> rhs) { return lhs.first > rhs.first; } }; long long gcd(long long a, long long b) { a = abs(a); b = abs(b); while (a) { long long temp = a; a = b % a; b = temp; } return abs(b); } long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } string binary(long long num) { string ans = ""; do { ans = to_string(num % 2) + ans; num /= 2; } while (num); return ans; } const int mxn = 5e5 + 7; const int d = 18; const int mill = 1e6 + 3; const long long mod = 998244353; long long pwr(long long num, long long p) { long long res = 1; while (p > 0) { if (p & 1) res = (res * num) % mod; num = (num * num) % mod; p /= 2; } return res; } long long inverse(long long num) { return pwr(num, mod - 2); } long long get_rand(long long n) { return ((rand() << 15) + rand()) % n; } void solve() { int q; cin >> q; vector<int> show(mxn); for (int i = 0; i < mxn; i++) { show[i] = i; } vector<array<int, 3>> arr(q); for (int i = 0; i < q; i++) { cin >> arr[i][0]; cin >> arr[i][1]; arr[i][2] = -1; if (arr[i][0] == 2) cin >> arr[i][2]; } vector<int> v; for (int i = q - 1; i >= 0; i--) { if (arr[i][0] == 1) { v.push_back(show[arr[i][1]]); } else { show[arr[i][1]] = show[arr[i][2]]; } } reverse(v.begin(), v.end()); print(v); } int main() { ios_base::sync_with_stdio(0), cin.tie(0); int t = 1; for (int ii = 1; ii <= t; ii++) { solve(); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int a, b, l, r, c, pl, pr, cl, cr; cin >> a >> b >> l >> r; c = a + b; cl = --l / c; cr = --r / c; pl = l % c; pr = r % c; if (cl == cr) { if (pl < a && pr < a) { cout << pr - pl + 1 << endl; } else if (pl < a && pr >= a) { cout << a - pl << endl; } else { cout << 1 << endl; } } else if (a <= b) { if (cl < cr - 1) { cout << a + 1 << endl; } else { if (pr >= a) { cout << a + 1 << endl; } else { if (pl >= a) { cout << pr + 1 + 1 << endl; } else { int left = a - pl; int right = pr + 1; if (left + right > a) { cout << a + (right == a) << endl; } else { cout << left + right + (right == a) << endl; } } } } } else { int d = a - b; if (cl == cr - 1) { if (cr >= a && cl >= a) { cout << a + 1 << endl; } else if (cr < a && cl >= a) { cout << cr + 1 + 1 << endl; } else { int right = min(pr + 1, a); int left = max(1, min(a - b, a - pl)); cout << right + left << endl; } } else if (cl == cr - 2) { cout << a + max(1, d + min(0, pr - a + 1 + max(0, a - pl))); } else { cout << a + d; } } }
1
#include <iostream> using namespace std; int n,q; int dat[300000]; void init(){ int nn = 1; while(nn < n) nn *= 2; n = nn; for(int i = 0;i < n * 2 - 1;i++){ dat[i] = 0; } } void update(int a,int b,int x,int k,int l,int r){ if(r <= a || b <= l) return; if(a <= l && r <= b){ dat[k] += x; return; } update(a,b,x,k * 2 + 1,l,(l + r) / 2); update(a,b,x,k * 2 + 2,(l + r) / 2,r); } int get(int i){ i = i + n - 1; int sum = dat[i]; while(i){ i = (i - 1) / 2; sum += dat[i]; } return sum; } int main(){ cin >> n >> q; init(); for(int i = 0;i < q;i++){ int v; cin >> v; if(v == 0){ int s,t,x; cin >> s >> t >> x; update(s - 1,t,x,0,0,n); } else{ int x; cin >> x; cout << get(x - 1) << endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int Z = 2500, BASE = 1e9 + 7; int n, k; long long f[55][55][5050]; long long dp(int i, int j, int sum) { if (i > n) return j == 0 && sum == k; long long &res = f[i][j][sum + Z]; if (res >= 0) return res; res = dp(i + 1, j, sum); if (j) { res = (res + dp(i + 1, j - 1, sum + i * 2) * j * j) % BASE; res = (res + dp(i + 1, j, sum) * j * 2) % BASE; } res = (res + dp(i + 1, j + 1, sum - i * 2)) % BASE; return res; } int main() { cin >> n >> k; memset(f, -1, sizeof f); cout << dp(1, 0, 0) << endl; }
0
#include <bits/stdc++.h> typedef struct { double x; double y; double r; } circle; typedef struct { double x; double y; } pt; int x[3]; int y[3]; int r[3]; pt np(double x, double y) { pt q; q.x = x; q.y = y; return q; } double abs_d(double d1) { if (d1 > 0) return d1; return -d1; } bool flag2 = true; pt p; double dist(pt p1, pt p2) { return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y)); } void circumcenter(pt p1, pt p2, pt p3) { double K = abs_d(.5 * (p1.x * p2.y + p2.x * p3.y + p3.x * p1.y - p1.y * p2.x - p2.y * p3.x - p3.y * p1.x)); if (K < .01) { flag2 = false; return; } double a = dist(p1, p2); double b = dist(p1, p3); double c = dist(p2, p3); double R = a * b * c / 4 / K; double d = sqrt(R * R - (a / 2) * (a / 2)); double nx = (p1.x + p2.x) / 2; double ny = (p1.y + p2.y) / 2; double mx = p1.y - p2.y; double my = p2.x - p1.x; double len = sqrt(mx * mx + my * my); mx /= len; my /= len; p.x = nx + d * mx; p.y = ny + d * my; if (abs_d(dist(p, p3) - R) > .001) { p.x = nx - d * mx; p.y = ny - d * my; } } bool flag = true; pt pos1; pt pos2; void intersection(circle c1, circle c2) { pt p; p.x = -1e9; p.y = -1e9; double d = sqrt((c1.x - c2.x) * (c1.x - c2.x) + (c1.y - c2.y) * (c1.y - c2.y)); if (d > c1.r + c2.r) flag = false; if (abs_d(c1.r - c2.r) > d) flag = false; if (flag == false) return; double a = d; double b = c1.r; double c = c2.r; double s = (a + b + c) / 2; double K = sqrt(s * (s - a) * (s - b) * (s - c)); double h = 2 * K / a; double d_par = sqrt(b * b - h * h); if (b * b + d * d < c * c) d_par = -d_par; double nx = c1.x + (c2.x - c1.x) * d_par / d; double ny = c1.y + (c2.y - c1.y) * d_par / d; double oppx = c2.y - c1.y; double oppy = c1.x - c2.x; double len = sqrt(oppx * oppx + oppy * oppy); oppx /= len; oppy /= len; pos1.x = nx + h * oppx; pos1.y = ny + h * oppy; pos2.x = nx - h * oppx; pos2.y = ny - h * oppy; } int main() { for (int i = 0; i < 3; i++) { scanf("%d %d %d", &x[i], &y[i], &r[i]); } pt p1 = np(x[0], y[0]); pt p2 = np(x[1], y[1]); pt p3 = np(x[2], y[2]); if (r[0] == r[1] && r[1] == r[2]) { circumcenter(p1, p2, p3); if (flag2) { printf("%.5f %.5f", p.x, p.y); } } else { circle c1, c2, c3; if (r[0] != r[1]) { double x1 = (r[1] * p1.x + r[0] * p2.x) / (r[1] + r[0]); double y1 = (r[1] * p1.y + r[0] * p2.y) / (r[1] + r[0]); double x2 = (r[1] * p1.x - r[0] * p2.x) / (r[1] - r[0]); double y2 = (r[1] * p1.y - r[0] * p2.y) / (r[1] - r[0]); c1.x = (x1 + x2) / 2; c1.y = (y1 + y2) / 2; c1.r = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) / 2; } if (r[0] != r[2]) { double x1 = (r[2] * p1.x + r[0] * p3.x) / (r[2] + r[0]); double y1 = (r[2] * p1.y + r[0] * p3.y) / (r[2] + r[0]); double x2 = (r[2] * p1.x - r[0] * p3.x) / (r[2] - r[0]); double y2 = (r[2] * p1.y - r[0] * p3.y) / (r[2] - r[0]); c2.x = (x1 + x2) / 2; c2.y = (y1 + y2) / 2; c2.r = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) / 2; } if (r[1] != r[2]) { double x1 = (r[2] * p2.x + r[1] * p3.x) / (r[2] + r[1]); double y1 = (r[2] * p2.y + r[1] * p3.y) / (r[2] + r[1]); double x2 = (r[2] * p2.x - r[1] * p3.x) / (r[2] - r[1]); double y2 = (r[2] * p2.y - r[1] * p3.y) / (r[2] - r[1]); c3.x = (x1 + x2) / 2; c3.y = (y1 + y2) / 2; c3.r = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)) / 2; } if (r[0] == r[1]) { intersection(c2, c3); } else if (r[0] == r[2]) { intersection(c1, c3); } else { intersection(c1, c2); } if (flag) { if (dist(pos1, p1) > dist(pos2, p1)) { printf("%.5f %.5f", pos2.x, pos2.y); } else { printf("%.5f %.5f", pos1.x, pos1.y); } } } fflush(stdin); getchar(); }
3
#include <bits/stdc++.h> #define N 2005 #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; int gets(){ int v=0; char ch; bool f=0; while(!isdigit(ch=getchar())) if(ch=='-') f=1; v=ch-48; while( isdigit(ch=getchar())) v=(v<<3)+(v<<1)+ ch-48; return f?-v:v; } char ch[N]; int T,n,m,ans[15],t,v,lef[15],rig[15],upp[15],dwn[15],flag[N],a[N][N]; int main(){ T=gets(); while(T--){ n=gets(); rep(i,0,9) lef[i]=upp[i]=n+1, rig[i]=dwn[i]=-1, ans[i]=0; rep(i,1,n) { scanf("%s",ch+1); rep(j,1,n){ v=a[i][j]=ch[j] - '0'; upp[v]=min(i,upp[v]); dwn[v]=max(i,dwn[v]); lef[v]=min(j,lef[v]); rig[v]=max(j,rig[v]); } } rep(i,1,n){ rep(j,1,n){ v = a[i][j]; t = max(i-1,n-i); ans[v]=max(ans[v],t*max(j-lef[v],rig[v]-j)); t = max(j-1,n-j); ans[v]=max(ans[v],t*max(i-upp[v],dwn[v]-i)); } } rep(i,0,8) printf("%d ",ans[i]); printf("%d\n",ans[9]); } return 0; }
3
#include <bits/stdc++.h> const int inf = (1ll << 30) - 1; const int maxn = (int)1e5 + 10; using namespace std; long long L[111], R[111], SHIFT[111], SHIFTVAL[111]; int sz = 1; void upd1() { long long x, k; scanf("%lld%lld", &x, &k); int g = 1; while (R[g] < x) g++; SHIFTVAL[g] -= k; SHIFTVAL[g] %= (R[g] - L[g] + 1); SHIFTVAL[g] += (R[g] - L[g] + 1); SHIFTVAL[g] %= (R[g] - L[g] + 1); } void upd2() { long long x, k; scanf("%lld%lld", &x, &k); int g = 1; while (R[g] < x) g++; while (g <= sz) { SHIFTVAL[g] -= k; SHIFTVAL[g] %= (R[g] - L[g] + 1); SHIFTVAL[g] += (R[g] - L[g] + 1); SHIFTVAL[g] %= (R[g] - L[g] + 1); k = (k * 2) % (R[sz] - L[sz] + 1); g++; } } void get() { long long x; scanf("%lld", &x); int g = 1; while (R[g] < x) g++; long long pos = x - L[g]; pos -= SHIFTVAL[g]; if (pos < 0) pos += R[g] - L[g] + 1; while (g > 0) { long long d = (SHIFTVAL[g] + pos) % (R[g] - L[g] + 1); printf("%lld", d + L[g]); pos /= 2; if (g > 1) printf(" "); g--; } printf("\n"); } void solve() { L[1] = R[1] = 1; while (R[sz] <= (long long)2e18) { R[sz + 1] = R[sz] * 2 + 1; L[sz + 1] = R[sz] + 1; sz++; } int q; scanf("%d", &q); for (int i = 0, ty; i < q; i++) { scanf("%d", &ty); if (ty == 1) { upd1(); } else if (ty == 2) { upd2(); } else { get(); } } } int main() { int t = 1; for (int i = 1; i <= t; i++) { solve(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, t[100100]; int dp[100100]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &t[i]); for (int i = 0; i < n; i++) { int x = 1000000000; if (i == 0) x = 20; else { x = dp[i - 1] + 20; int ini = 0, fim = i - 1; while (ini <= fim) { int meio = (ini + fim) / 2; if (t[i] - t[meio + 1] + 1 > 90) ini = meio + 1; else fim = meio - 1; } x = min(x, dp[ini] + 50); if (t[i] - t[0] + 1 <= 90) x = min(x, 50); ini = 0, fim = i - 1; while (ini <= fim) { int meio = (ini + fim) / 2; if (t[i] - t[meio + 1] + 1 > 1440) ini = meio + 1; else fim = meio - 1; } x = min(x, dp[ini] + 120); if (t[i] - t[0] + 1 <= 1440) x = min(x, 120); } dp[i] = x; if (i == 0) printf("%d\n", dp[i]); else printf("%d\n", dp[i] - dp[i - 1]); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, m, s, w[100005], ans; double f[1050000], a[1050000]; char S[100005]; void fwt(double a[], int n, int d = 1) { if (d > 0) for (int m = 2, k = m >> 1; m <= n; m <<= 1, k <<= 1) { for (int i = 0; i < n; i += m) for (int j = i; j < i + k; ++j) { double u = a[j], v = a[j + k]; a[j] = u + v, a[j + k] = u - v; } } else for (int m = n, k = m >> 1; m >= 2; m >>= 1, k >>= 1) { for (int i = 0; i < n; i += m) for (int j = i; j < i + k; ++j) { double u = a[j], v = a[j + k]; a[j] = (u + v) / 2, a[j + k] = (u - v) / 2; } } } int main() { scanf("%d%d", &n, &m); s = 1 << n; for (int i = 1, x = 1; i <= n; ++i, x <<= 1) { scanf("%s", S + 1); for (int j = 1; j <= m; ++j) if (S[j] == '1') w[j] += x; } for (int i = 0; i < s; ++i) f[i] = min(__builtin_popcount(i), __builtin_popcount(s - 1 ^ i)); for (int i = 1; i <= m; ++i) ++a[w[i]]; fwt(f, s); fwt(a, s); for (int i = 0; i < s; ++i) f[i] = f[i] * a[i]; fwt(f, s, -1); ans = n * m; for (int i = 0; i < s; ++i) ans = min(ans, (int)round(f[i])); printf("%d\n", ans); return 0; }
5
// 2020.12.13 // Code by LTb // #pragma GCC optimize(2) #include <bits/stdc++.h> using namespace std; // #define int long long #define debug puts("QwQ"); inline int read(){ int x=0,f=1; char ch; while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();} return f*x; } inline char readch(){ char ch=getchar(); while (ch==' '||ch=='\n') ch=getchar(); return ch; } inline string readstr(){ char ch=getchar(); string ans=""; while (ch==' '||ch=='\n') ch=getchar(); while (ch!=' '&&ch!='\n') {ans+=ch;ch=getchar();} return ans; } inline void chmax(int &x,int y){x=max(x,y);} inline void chmin(int &x,int y){x=min(x,y);} const int MAXN=100005; int n; int a[MAXN]; bool Plus,Minus,Multiple; int dp[MAXN],bel[MAXN]; char ans[MAXN]; int nex[MAXN]; int P=1e7; void sol1(){ for (int i=2;i<=n;i++) if (Plus) ans[i]='+'; else ans[i]='-'; } void sol2(){ for (int i=2;i<=n;i++) ans[i]='*'; } void sol3(){ int pos=0; for (int i=1;i<=n;i++) if (!pos && a[i]==0) pos=i; for (int i=2;i<=n;i++){ if (i==pos) ans[i]='-'; else ans[i]='*'; } } void init(){ int cur=0; for (int i=n;i>=1;i--){ nex[i]=cur; if (a[i]!=1) cur=i; } } void foo(int l,int r){ int cnt=1; dp[l-1]=0; for (int i=l;i<=r;i++){ if (cnt<P) cnt*=a[i]; dp[i]=bel[i]=0; // cout<<i<<' '<<nex[i]<<endl; } if (cnt>=P){ for (int i=l+1;i<=r;i++) ans[i]='*'; return ; } for (int i=l;i<=r;i++){ cnt=1; for (int j=i;j<=r && j;j=nex[j]){ cnt*=a[j]; if (dp[i-1]+cnt>dp[j]){ dp[j]=dp[i-1]+cnt; bel[j]=i-1; } } } int cur=r; while (cur>=l){ int tmp=bel[cur]; ans[tmp+1]='+'; for (int i=tmp+2;i<=cur;i++) ans[i]='*'; cur=tmp; } } void sol4(){ init(); for (int i=1;i<=n;i++){ if (a[i]==0){ ans[i+1]=ans[i]='+'; continue; } int cur=i; while (cur<n && a[cur+1]!=0) cur++; int l=i,r=cur; while (l<=r && a[l]==1) ans[++l]='+'; while (l<=r && a[r]==1) ans[r--]='+'; if (l<=r) foo(l,r); i=cur; } } void print(){ cout<<a[1]; for (int i=2;i<=n;i++) cout<<ans[i]<<a[i]; cout<<endl; } signed main() { n=read();P=2*n; for (int i=1;i<=n;i++) a[i]=read(); string allowed=readstr(); for (auto i:allowed){ if (i=='+') Plus=true; else if (i=='-') Minus=true; else Multiple=true; } if (!Multiple) sol1(); else{ if (!Plus && !Minus) sol2(); else if (!Plus) sol3(); else sol4(); } print(); return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, m, i, j, k, head[N], adj[N * 2], nxt[N * 2], mn[N], Mn[N], pt[N], Pt[N], dep[N], fa[N], ans[N], cnt; bool v[N]; void Print() { printf("%d", cnt); for (int i = 1; i <= cnt; ++i) printf(" %d", ans[i]); puts(""); } void work(int x, int d) { if (dep[x] != d) work(fa[x], d); ans[++cnt] = x; } bool dfs(int x, int dad) { v[x] = true; mn[x] = Mn[x] = dep[x], pt[x] = Pt[x] = x; for (int y = head[x]; y; y = nxt[y]) if (!v[adj[y]]) { dep[adj[y]] = dep[x] + 1; fa[adj[y]] = x; if (dfs(adj[y], x)) return true; if (mn[adj[y]] < mn[x]) { Mn[x] = mn[x]; Pt[x] = pt[x]; mn[x] = mn[adj[y]]; pt[x] = pt[adj[y]]; } else if (mn[adj[y]] < Mn[x]) Mn[x] = mn[adj[y]], Pt[x] = pt[adj[y]]; } else if (adj[y] != dad) { if (dep[adj[y]] < mn[x]) { Mn[x] = mn[x]; Pt[x] = pt[x]; mn[x] = dep[adj[y]]; pt[x] = x; } else if (dep[adj[y]] < Mn[x]) { Mn[x] = dep[adj[y]]; Pt[x] = x; } } if (Mn[x] < dep[x]) { puts("YES"); cnt = 0; for (i = x; dep[i] > Mn[x]; i = fa[i]) ans[++cnt] = i; ans[++cnt] = i; Print(); cnt = 0; work(Pt[x], dep[x]); ans[++cnt] = i; Print(); cnt = 0; work(pt[x], dep[x]); work(i, mn[x]); Print(); return true; } return false; } int main() { scanf("%d%d", &n, &m); for (i = 1; i <= m; ++i) { scanf("%d%d", &j, &k); adj[i * 2 - 1] = k; nxt[i * 2 - 1] = head[j]; head[j] = i * 2 - 1; adj[i * 2] = j; nxt[i * 2] = head[k]; head[k] = i * 2; } for (i = 1; i <= n; ++i) if (!v[i]) { if (dfs(i, -1)) return 0; } printf("NO"); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int a[n], b[m]; for (long long i = 0; i < n; i++) cin >> a[i]; for (long long i = 0; i < m; i++) cin >> b[i]; sort(a, a + n); sort(b, b + m); int index = 0; for (long long i = 0; i < m; i++) if (index < n && a[index] <= b[i]) index++; cout << n - index << "\n"; return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long M = 1e9 + 7; long long add(long long a, long long b) { (a += b) %= M; (a += M) %= M; return a; } long long mul(long long a, long long b) { (a *= b) %= M; (a += M) %= M; return a; } long long fastpo(long long a, long long x) { if (x == 0) return 1ll; if (x == 1) return a; long long y = fastpo(a, x / 2); if (x % 2 == 0) { return y * y; } return y * y * a; } long long inv(long long a, long long M) { return fastpo(a, M - 2); } long long gcdExtended(long long a, long long b, long long *x, long long *y) { if (a == 0) { *x = 0, *y = 1; return b; } long long x1, y1; long long gcd = gcdExtended(b % a, a, &x1, &y1); *x = y1 - (b / a) * x1; *y = x1; return gcd; } long long modInverse(long long a, long long m) { long long x, y; long long g = gcdExtended(a, m, &x, &y); if (g != 1) return -1; else { long long res = (x % m + m) % m; return res; } } long long gcd(long long a, long long b) { if (a == 0) { return b; } return gcd(b % a, a); } long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } void print(vector<int> &a) { int n = a.size(); for (int i = 0; i < n; i++) { cout << a[i] << " "; } cout << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; string s; cin >> s; bool cy = true; vector<int> cont; int ans = 0; bool st = false; int l = 0, x = 0; for (int i = 0; i < n; i++) { if (s[i] != s[0]) cy = false; if (!st) { if (s[i] != s[0]) { st = true; l++; } else { x++; } } else { if (s[i] != s[i - 1]) { ans += (l / 3); l = 1; } else { l++; } } } if (cy) { ans++; n--; ans += (n / 3); cout << ans << endl; continue; } if (s[0] == s[n - 1]) { l += x; ans += (l / 3); } else { ans += (l / 3); ans += (x / 3); } cout << ans << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; string s; cin >> s; string res = "BGR"; int cnt = mod; string ans; do { int mn = 0; string aux = s; for (int i = 0; i < n; i++) { if (aux[i] != res[i % 3]) { aux[i] = res[i % 3]; mn++; } } if (mn < cnt) { cnt = mn; ans = aux; } } while (next_permutation(res.begin(), res.end())); cout << cnt << endl << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int Max = 2e6 + 10; const int Mod = 1e9 + 7; const long long int Inf = 1LL << 62; int ar[Max]; int main() { int n, mx = 0, mn = 1e9; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &ar[i]); mn = min(mn, ar[i]); mx = max(mx, ar[i]); } long long int dif = mx - mn; long long int y = Inf, z = 0; for (long long int i = 1; i * i <= dif; i++) { if (dif % i == 0) { bool f = 1; long long int tot = 0; for (int j = 1; j <= n; j++) { f &= ((mx - ar[j]) % i == 0); tot += (mx - ar[j]) / i; } if (f) { if (tot <= y) { y = tot; z = i; } } if (i * i != dif) { long long int k = dif / i; bool f = 1; long long int tot = 0; for (int j = 1; j <= n; j++) { f &= ((mx - ar[j]) % k == 0); tot += (mx - ar[j]) / k; } if (f) { if (tot <= y) { y = tot; z = k; } } } } } cout << y << " " << z << endl; return 0; }
4
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; int read() { int x = 0, f = 1; char ch; while(! isdigit(ch = getchar())) (ch == '-') && (f = -f); for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 3) + (x << 1) + (ch ^ 48)); return x * f; } template <class T> T Max(T a, T b) { return a > b ? a : b; } template <class T> T Min(T a, T b) { return a < b ? a : b; } string s, t; int n, m, k; int main() { n = read(); m = read(); k = read(); cin >> s >> t; reverse(s.begin(), s.end()); reverse(t.begin(), t.end()); for(int i = 1; i <= m + k + 5; ++ i) s.push_back('0'); for(int i = 1; i <= n + k + 5; ++ i) t.push_back('0'); for(int i = n + m - 1; i >= 0; -- i) { if(s[i] == '1' && t[i] == '1') { s[i] = t[i] = '0'; s[i + 1] ++; t[i + 1] ++; int li = k - 1, j = i + 1; while(li) { if(s[j] == '2') s[j] = '0', s[j + 1] ++; else if(t[j] == '2') t[j] = '0', t[j + 1] ++; else if(s[j] == '1' && t[j] == '1') { li --; s[j] = t[j] = '0'; s[j + 1] ++; t[j + 1] ++; } else break; j ++; } if(s[j] == '2') s[j] = '0', s[j + 1] ++; if(t[j] == '2') t[j] = '0', t[j + 1] ++; } } while(s.back() == '0') s.pop_back(); while(t.back() == '0') t.pop_back(); reverse(s.begin(), s.end()); reverse(t.begin(), t.end()); cout << s << endl << t << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; template <class T> inline void Max(T &a, T b) { if (a < b) a = b; } template <class T> inline void Min(T &a, T b) { if (a > b) a = b; } int a[200005]; double cal(double first, int n) { double s = 0, mi = 0, mx = 0; for (int i = 1; i <= n; i++) { s += a[i] - first; Min(mi, s); Max(mx, s); } return abs(mx - mi); } int main() { int T, i, j, k, m, n; scanf("%d", &n); double l = -1e9, r = 1e9; for (i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (int it = 0; it < 100; it++) { double x1 = (11 * l + 10 * r) / 21; double x2 = (10 * l + 11 * r) / 21; double y1 = cal(x1, n), y2 = cal(x2, n); if (y1 >= y2) l = x1; else r = x2; } printf("%.9lf\n", cal(l, n)); return 0; }
3
#include <bits/stdc++.h> using namespace std; char cells[1001][1001] = {0}; int scope[1001][1001] = {0}; int processNum[100001] = {0}; int ans[100001] = {0}; int pictFound = 0; int nCells, mCells, startingPoint, rowPos, colPos, maxPict, i; void visit(int rowPos, int colPos) { if (rowPos == 0 || colPos == 0 || rowPos == nCells + 1 || colPos == mCells + 1) return; if (cells[rowPos][colPos] == '*') { maxPict++; return; } if (scope[rowPos][colPos] != 0) return; scope[rowPos][colPos] = i; visit(rowPos + 1, colPos); visit(rowPos, colPos + 1); visit(rowPos - 1, colPos); visit(rowPos, colPos - 1); return; } int main() { cin >> nCells >> mCells >> startingPoint; for (int i = 1; i <= nCells; i++) { for (int j = 1; j <= mCells; j++) { cin >> cells[i][j]; } } for (i = 1; i <= startingPoint; i++) { cin >> rowPos >> colPos; pictFound++; maxPict = 0; if (!scope[rowPos][colPos]) visit(rowPos, colPos); else maxPict = processNum[scope[rowPos][colPos]]; processNum[i] = maxPict; cout << maxPict << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; void solve() { long long int n, m; cin >> n >> m; if (n % m == 0) { cout << "Yes" << endl; } else { cout << "No" << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t = 1; cin >> t; while (t--) { solve(); } return 0; }
1
#include <bits/stdc++.h> const int N = 100010, INF = 1e9 + 10; std::vector<std::pair<int, int>> get_ancestors(int x, int y) { std::vector<std::pair<int, int>> res; res.push_back(std::make_pair(x, y)); for (int i = 0; i < 30; i++) { if (x >> i & 1) { assert(~y >> i & 1); x ^= 1 << i; res.push_back(std::make_pair(x, y)); } if (y >> i & 1) { y ^= 1 << i; res.push_back(std::make_pair(x, y)); } } return res; } std::map<int, std::vector<std::pair<int, int>>> fx, fy; std::map<int, int> g; void add(std::map<int, std::vector<std::pair<int, int>>>& f, int x, int l, int r) { f[x].push_back(std::make_pair(l, r)); } void add(int x1, int y1, int x2, int y2) { if (x1 == x2) add(fx, x1, y2 + 1, y1); else { assert(y1 == y2); add(fy, y1, x2 + 1, x1); } } int getdir(int x, int y) { int xt = x & -x, yt = y & -y; if (xt == 0) xt = 1 << 30; if (yt == 0) yt = 1 << 30; if (xt < yt) return 1; return 2; } void addp(int x, int y) { if (getdir(x, y) == 1) add(fy, y, x, x); else add(fx, x, y, y); } void addseg(int l, int r) { g[l] ^= 1; g[r + 1] ^= 1; } void work2(int v, std::vector<std::pair<int, int>>& s) { std::vector<std::pair<int, int>> t; for (std::pair<int, int> x : s) { t.push_back(std::make_pair(x.first, 1)); t.push_back(std::make_pair(x.second + 1, -1)); } std::sort(t.begin(), t.end()); int sum = 0, lst = INF; for (std::pair<int, int> x : t) { sum += x.second; if (sum == 0) addseg(v + lst, v + x.first - 1), lst = INF; if (sum) lst = std::min(lst, x.first); } } void work(std::map<int, std::vector<std::pair<int, int>>>& f) { for (auto& a : f) work2(a.first, a.second); } int main() { int m; scanf("%d", &m); for (int i = 1; i <= m; i++) { int x1, y1, x2, y2; scanf("%d%d%d%d", &x1, &y1, &x2, &y2); std::vector<std::pair<int, int>> a = get_ancestors(x1, y1); std::vector<std::pair<int, int>> b = get_ancestors(x2, y2); int u = 0, v = 0; while (a[u] != b[v]) { int ad = a[u].first + a[u].second, bd = b[v].first + b[v].second; if (ad > bd) u++; else v++; } bool flag; if (u == 0 || v == 0) { flag = 0; } else { int ad = getdir(a[u - 1].first, a[u - 1].second), bd = getdir(b[v - 1].first, b[v - 1].second); if (ad != bd) flag = 0; else { flag = 1; int as = a[u - 1].first + a[u - 1].second, bs = b[v - 1].first + b[v - 1].second; if (bs < as) b.swap(a), std::swap(u, v); } } if (!flag) { for (int j = 0; j < u; j++) { add(a[j].first, a[j].second, a[j + 1].first, a[j + 1].second); } for (int j = 0; j < v; j++) { add(b[j].first, b[j].second, b[j + 1].first, b[j + 1].second); } addp(b[v].first, b[v].second); } else { for (int j = 0; j < u - 1; j++) { add(a[j].first, a[j].second, a[j + 1].first, a[j + 1].second); } for (int j = 0; j < v - 1; j++) { add(b[j].first, b[j].second, b[j + 1].first, b[j + 1].second); } add(b[v - 1].first, b[v - 1].second, a[u - 1].first, a[u - 1].second); addp(a[u - 1].first, a[u - 1].second); } } work(fx), work(fy); int ans = 0; for (std::pair<int, int> x : g) if (x.first && x.second) ans++; printf("%d\n", ans); }
5
#include <bits/stdc++.h> using namespace std; const int sz = 2e6 + 9; int n, m; char a[sz], b[sz]; int arr[sz]; int cs[sz]; int ans; int main() { scanf("%s %s", a, b); n = strlen(a); m = strlen(b); for (int i = 0; i < n; i += m) { for (int j = 0; j < m; j++) arr[i + j] = (a[i + j] != b[j]); } cs[0] = arr[0]; for (int i = 1; i < n; i++) cs[i] = cs[i - 1] + arr[i]; for (int i = 0; i <= n - m; i++) { int tmp = cs[i + m - 1] - (i - 1 >= 0 ? cs[i - 1] : 0); ans += ((tmp & 1) == 0); } printf("%d", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a, b; int day = 0; for (int i = 0; i < n; ++i) { cin >> a >> b; if (a > day) day = a; else { while (a <= day) a += b; day = a; } } cout << day << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long int inf = 1e+15; const long long int mod = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); vector<pair<int, int> > A, B; int n, x; cin >> n; vector<int> ans; for (int i = (0); i < (n); i++) { cin >> x; A.push_back(make_pair(x, i)); ans.push_back(0); } sort(A.begin(), A.end()); for (int i = (0); i < (n); i++) { cin >> x; B.push_back(make_pair(x, i)); } sort(B.begin(), B.end()); for (int i = (0); i < (n); i++) ans[B[i].second] = A[n - i - 1].first; for (int i = (0); i < (n); i++) cout << ans[i] << " "; cout << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int a[100010]; set<int> s; int n, L; int lo, hi, mid; int fin(int tar) { lo = 0; hi = n - 1; while (lo <= hi) { mid = (lo + hi) / 2; if (a[mid] > tar) hi = mid - 1; else lo = mid + 1; } if (hi >= 0 && a[hi] == tar) return 1; return 0; } int get(int val) { int i; for (i = 0; i < n; i++) { if (fin(a[i] + val) || fin(a[i] - val)) return 1; } return 0; } int make(int val, int other_val) { int i, val2; for (i = 0; i < n; i++) { if ((a[i] + val) <= L) { val2 = a[i] + val; if (fin(val2 - other_val) || fin(val2 + other_val)) { printf("1\n"); printf("%d\n", val2); return 1; } } if ((a[i] - val) >= 0) { val2 = a[i] - val; if (fin(val2 - other_val) || fin(val2 + other_val)) { printf("1\n"); printf("%d\n", val2); return 1; } } } return 0; } int main() { int i, j, k, x, y; scanf("%d %d %d %d", &n, &L, &x, &y); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } int gotx = get(x); int goty = get(y); if (gotx && goty) { printf("0\n"); return 0; } if (gotx || goty) { printf("1\n"); if (gotx) printf("%d\n", y); if (goty) printf("%d\n", x); return 0; } int val = x; int other_val = y; int sts = make(val, other_val); if (sts) return 0; sts = make(other_val, val); if (sts) return 0; printf("2\n"); printf("%d %d\n", x, y); return 0; }
2
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; const int MOD = 1e9 + 7; const int dx[] = {0, 0, 1, -1}; const int dy[] = {1, -1, 0, 0}; const char dir[] = {'R', 'L', 'D', 'U'}; ll add(ll a, ll b) { a += b; if (a >= MOD) { a -= MOD; } return a; } int sub(int a, int b) { a -= b; if (a < 0) a += MOD; return a; } int mult(int a, int b) { return ((ll)a * b) % MOD; } void setIO() { ios_base::sync_with_stdio(0); cin.tie(0); } int main() { setIO(); int t; cin >> t; while (t--) { string s; cin >> s; vector<int> ns, ew; int n = s.length(); for (int i = 0; i < n; ++i) { if (s[i] == 'W') ns.emplace_back(1); else if (s[i] == 'S') ns.emplace_back(-1); else if (s[i] == 'D') ew.emplace_back(1); else ew.emplace_back(-1); } int d1 = 1e9, d2 = 1e9; int origd1 = 1, origd2 = 1; int sz = 0; vector<int> cur; cur = ns; sz = ns.size(); if (sz) { int pmx[sz], smx[sz], pmn[sz], smn[sz]; int vals[sz]; for (int i = 0; i < sz; ++i) { vals[i] = cur[i]; if (i) vals[i] += vals[i - 1]; } for (int i = 0; i < sz; ++i) { pmx[i] = max(0, vals[i]); pmn[i] = min(0, vals[i]); if (i) { pmx[i] = max(pmx[i], pmx[i - 1]); pmn[i] = min(pmn[i], pmn[i - 1]); } } for (int i = sz - 1; i >= 0; --i) { smx[i] = vals[i]; smn[i] = vals[i]; if (i < sz - 1) { smx[i] = max(smx[i], smx[i + 1]); smn[i] = min(smn[i], smn[i + 1]); } } d1 = pmx[ns.size() - 1] - pmn[ns.size() - 1] + 1; origd1 = d1; for (int i = 0; i < ns.size() - 1; ++i) { int curn = max(pmx[i], smx[i + 1] + 1); int curs = min(pmn[i], smn[i + 1] + 1); d1 = min(d1, curn - curs + 1); curn = max(pmx[i], smx[i + 1] - 1); curs = min(pmn[i], smn[i + 1] - 1); d1 = min(d1, curn - curs + 1); } } cur = ew; sz = ew.size(); if (sz) { int pmx2[sz], smx2[sz], pmn2[sz], smn2[sz]; int vals2[sz]; for (int i = 0; i < sz; ++i) { vals2[i] = cur[i]; if (i) vals2[i] += vals2[i - 1]; } for (int i = 0; i < sz; ++i) { pmx2[i] = max(0, vals2[i]); pmn2[i] = min(0, vals2[i]); if (i) { pmx2[i] = max(pmx2[i], pmx2[i - 1]); pmn2[i] = min(pmn2[i], pmn2[i - 1]); } } for (int i = sz - 1; i >= 0; --i) { smx2[i] = vals2[i]; smn2[i] = vals2[i]; if (i < sz - 1) { smx2[i] = max(smx2[i], smx2[i + 1]); smn2[i] = min(smn2[i], smn2[i + 1]); } } d2 = pmx2[ew.size() - 1] - pmn2[ew.size() - 1] + 1; origd2 = d2; for (int i = 0; i < ew.size() - 1; ++i) { int cure = max(pmx2[i], smx2[i + 1] + 1); int curw = min(pmn2[i], smn2[i + 1] + 1); d2 = min(d2, cure - curw + 1); cure = max(pmx2[i], smx2[i + 1] - 1); curw = min(pmn2[i], smn2[i + 1] - 1); d2 = min(d2, cure - curw + 1); } } if (d1 == 1e9) d1 = 1; if (d2 == 1e9) d2 = 1; cout << min((ll)d1 * origd2, (ll)origd1 * d2) << "\n"; } }
3
#include <bits/stdc++.h> using namespace std; const static int N = 3e5 + 2; const static int alpha = 26, K = 550; int trie[N][alpha], cnt[N], sz; void init() { memset(trie[0], 0, sizeof trie[0]); sz = 0; } void add(int l, const string &s, int x) { int v = 0; for (int i = l; i < s.size(); ++i) { int c = s[i] - 'a'; int &next = trie[v][c]; if (!next) { next = ++sz; memset(trie[next], 0, sizeof trie[next]); } v = next; } cnt[v] += x; } int query(int l, const string &s) { int v = 0, ans = 0; for (int i = l; i < s.size(); ++i) { int c = s[i] - 'a'; int &next = trie[v][c]; if (!next) { return ans; } v = next; ans += cnt[v]; } return ans; } string pat[N]; vector<int> phis[N]; vector<int> get_phi(string &s) { int j = 0, n = ((int)s.size()); vector<int> pi(n); for (int i = 1; i <= (int)n - 1; ++i) { while (j > 0 && s[i] != s[j]) j = pi[j - 1]; j += (s[i] == s[j]); pi[i] = j; } return pi; } int kmp(string &t, int id) { if (((int)phis[id].size()) == 0) phis[id] = get_phi(pat[id]); vector<int> phi = phis[id]; string &p = pat[id]; int matches = 0; for (int i = 0, j = 0; i < ((int)t.size()); ++i) { while (j > 0 && t[i] != p[j]) j = phi[j - 1]; if (t[i] == p[j]) ++j; if (j == ((int)p.size())) { matches++; j = phi[j - 1]; } } return matches; } unordered_map<string, int> mp; long long solve(string &s) { long long ans = 0; for (auto &x : mp) ans += kmp(s, x.second); for (int i = 0; i < s.size(); ++i) { ans += query(i, s); } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int m; cin >> m; int t; for (int i = 0; i < (int)m; ++i) { cin >> t >> pat[i]; int n = ((int)pat[i].size()); if (t == 1) { if (n <= K) { add(0, pat[i], 1); } else { mp[pat[i]] = i; } } else if (t == 2) { if (n <= K) { add(0, pat[i], -1); pat[i].clear(); } else { int x = mp[pat[i]]; phis[x].clear(); pat[x].clear(); mp.erase(pat[i]); } } else { cout << solve(pat[i]) << endl; } } }
6
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool upmin(T &x, T y) { return y < x ? x = y, 1 : 0; } template <typename T> inline bool upmax(T &x, T y) { return x < y ? x = y, 1 : 0; } const long double pi = acos(-1); const int oo = 1 << 30; const long long OO = 1e18; const int mod = 1e9 + 7; const int N = 3e5 + 100; int qpow(int x, int y) { int ans = 1; while (y) { if (y & 1) ans = 1LL * ans * x % mod; x = 1LL * x * x % mod; y >>= 1; } return ans; } int gi() { int w = 0; bool q = 1; char c = getchar(); while ((c < '0' || c > '9') && c != '-') c = getchar(); if (c == '-') q = 0, c = getchar(); while (c >= '0' && c <= '9') w = w * 10 + c - '0', c = getchar(); return q ? w : -w; } namespace io { const int L = (1 << 21) + 1; char ibuf[L], *iS, *iT, obuf[L], *oS = obuf, *oT = obuf + L - 1, c, st[55]; int f, tp; inline void flush() { fwrite(obuf, 1, oS - obuf, stdout); oS = obuf; } inline void putc(char x) { *oS++ = x; if (oS == oT) flush(); } template <class I> inline void gi(I &x) { for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, L, stdin), (iS == iT ? EOF : *iS++)) : *iS++); c < '0' || c > '9'; c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, L, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) if (c == '-') f = -1; for (x = 0; c <= '9' && c >= '0'; c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, L, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) x = x * 10 + (c & 15); x *= f; } template <class I> inline void print(I x) { if (!x) putc('0'); if (x < 0) putc('-'), x = -x; while (x) st[++tp] = x % 10 + '0', x /= 10; while (tp) putc(st[tp--]); } inline void gs(char *s, int &l) { for (c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, L, stdin), (iS == iT ? EOF : *iS++)) : *iS++); c < 'a' || c > 'z'; c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, L, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) ; for (l = 0; c <= 'z' && c >= 'a'; c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, L, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) s[l++] = c; s[l] = 0; } inline void ps(const char *s) { for (int i = 0, n = strlen(s); i < n; i++) putc(s[i]); } struct IOFLUSHER { ~IOFLUSHER() { flush(); } } _ioflusher_; }; // namespace io using io::gi; using io::gs; using io::print; using io::ps; using io::putc; int c[N], sa[N], n; void work(int x1, int x2, bool o) { int i, k, mi; for (mi = 1; mi <= n && c[sa[mi]] * mi < x1; mi++) ; for (i = mi; i <= n; i++) { if (c[sa[i]] * (i - mi) >= x2) { ps("Yes\n"); if (o) reverse(sa + 1, sa + 1 + i), mi = i - mi; print(mi); putc(' '); print(i - mi); putc('\n'); for (k = 1; k <= mi; k++) print(sa[k]), putc(' '); putc('\n'); for (; k <= i; k++) print(sa[k]), putc(' '); putc('\n'); exit(0); } } } int main() { int x1, x2, i; gi(n); gi(x1), gi(x2); for (i = 1; i <= n; i++) gi(c[i]), sa[i] = i; sort(sa + 1, sa + 1 + n, [&](int x, int y) { return c[x] > c[y]; }); work(x1, x2, false); work(x2, x1, true); ps("No"); return 0; }
2
#include <bits/stdc++.h> using namespace std; string s[105]; vector<string> v; int a[105]; int main() { memset(a, 0, sizeof(a)); int n; int ans = 0; string home; cin >> n >> home; for (int i = 0; i < n; i++) { cin >> s[i]; if (s[i].substr(0, 3) == home) { v.push_back(s[i].substr(5, 3)); } } for (int i = 0; i < v.size(); i++) { for (int j = 0; j < n; j++) { if (!a[j] && s[j].substr(0, 3) == v[i] && s[j].substr(5, 3) == home) { a[j]++; ans++; break; } } } if (ans == v.size()) { cout << "home" << endl; } else { cout << "contest" << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int cor[n], wro[m]; for (int i = 0; i < n; i++) cin >> cor[i]; for (int i = 0; i < m; i++) cin >> wro[i]; int tl; sort(cor, cor + n); sort(wro, wro + m); int v = max(2 * cor[0], cor[n - 1]); if (v < wro[0]) cout << v; else cout << "-1"; return 0; }
1
#include <bits/stdc++.h> using namespace std; long long int n, m, i, j, k; long long int a[5050]; long long int mini = 1000000100, maxi = -1, res, sum = 0; long long f(long long int l, long long int r, long long int x) { if (l > r) return 0; long long int t = min_element(a + l, a + r + 1) - a; return min(r - l + 1, f(l, t - 1, a[t]) + f(t + 1, r, a[t]) + a[t] - x); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (i = 0; i < n; i++) cin >> a[i]; cout << f(0, n - 1, 0); return 0; }
3
#include<iostream> using namespace std; int map[110]; bool solve() { if (map[1] % 2 == 1 && map[2] % 2 == 1) { for (int i = 3; i <= 100; i++){ if (map[i] % 2 == 1) return false; } return true; } else return false; } int main() { int a, b; while (cin >> a >> b) { if (!a && !b) { if (solve()) cout << "OK\n"; else cout << "NG\n"; for (int i = 1; i <= 100; i++)map[i] = 0; continue; } else { map[a]++; map[b]++; } } }
0
#include <bits/stdc++.h> using namespace std; const long long inf = (1ll << 62) - 1; const long long MOD = 1e9 + 7; const int MAX = 1e9; const long long int N = 1e6 + 100; using namespace std; vector<int> factor(N, -1), primes; int bPow(int a, int b) { int res = 1; while (b) { if (b & 1ll) { res = 1ll * res * a % MOD; } b >>= 1; a = 1ll * a * a % MOD; } return res; } void sieve() { for (int i = 2; i <= N; ++i) { if (factor[i] == -1) { primes.push_back(i); for (int j = i; j <= N; j += i) { if (factor[j] == -1) { factor[j] = i; } } } } } void solve() { int n, m; cin >> n >> m; vector<int> vertical(n); for (int i = 0; i < n; ++i) { cin >> vertical[i]; } vertical.push_back(MAX); vector<int> horizontal; multiset<int> len; for (int i = 0; i < m; ++i) { int x = 0, y = 0, z = 0; cin >> x >> y >> z; if (x == 1) { horizontal.push_back(y); } } sort(vertical.begin(), vertical.end()); sort(horizontal.begin(), horizontal.end()); int ans = MAX; for (int i = 0; i < n + 1; ++i) { int idx = lower_bound(horizontal.begin(), horizontal.end(), vertical[i]) - horizontal.begin(); ans = min(ans, (int)horizontal.size() - idx + i); } cout << ans << endl; } int main() { solve(); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int INF = 1e8; const int N = 1001; int n, m, x, y, pref[N], arr[N], dp[N][2]; char c; int solve(int idx, int t) { if (!idx) return dp[idx][t] = 0; if (idx < x) return dp[idx][t] = INF; int& ret = dp[idx][t]; if (ret) return dp[idx][t]; ret = INF; for (int i = x; i <= y; i++) { if (idx < i) break; int col = pref[idx] - pref[idx - i]; if (t) col = i * n - col; ret = min(ret, solve(idx - i, !t) + col); } return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); memset(dp, 0, sizeof dp); pref[0] = 0; cin >> n >> m >> x >> y; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> c, arr[j] += (c == '.'); for (int i = 1; i <= m; i++) pref[i] = pref[i - 1] + arr[i]; cout << min(solve(m, 0), solve(m, 1)); }
3
#include <iostream> using namespace std; int main() { int n,c=0; cin>>n; int arr[n]; for(int i=1;i<=n;i++){ cin>>arr[i]; if(i%2==1&&arr[i]%2==1){ c++; } } cout<<c; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int a, b, c, d; cin >> a >> b >> c >> d; int x = a + b, y = c + d; x = max(x, y); cout << x << endl; } }
2
#include <bits/stdc++.h> using namespace std; int fc[100005], fp[100005], h[15]; int dp[505][5042], ans, n, k; int main() { cin >> n >> k; for (int i = k * n, x; i--; ++fc[x]) cin >> x; for (int i = n, x; i--; ++fp[x]) cin >> x; for (int i = 1; i <= k; cin >> h[i++]) ; for (int x = 0, y, i; x < n; ++x) for (y = 0; y <= k * n; ++y) for (i = 0; i <= k; ++i) dp[x + 1][y + i] = max(dp[x + 1][y + i], dp[x][y] + h[i]); for (int i = 100005; i--; ans += dp[fp[i]][fc[i]]) ; cout << ans; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n, aux, T, R; scanf("%d", &T); while (T--) { scanf("%d%d", &n, &R); int sum = 0; for (int i = 1; i < n; i++) scanf("%d", &aux), sum += aux; printf("%d\n", (R % n + n - sum % n + n - 1) % n + 1); } return 0; }
1
#include<iostream> using namespace std; int f; char c; int main(){ while(cin>>c)f^=1<<(c-97); cout<<(f?"No":"Yes")<<endl; }
0
#include <bits/stdc++.h> using namespace std; bool comp(pair<int, int> &f, pair<int, int> &s) { if (f.first > s.first) { return true; } if (f.first == s.first) { if (f.second < s.second) { return true; } else { return false; } } return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, k, p, t; cin >> n >> k; vector<pair<int, int>> pt; map<pair<int, int>, int> d; while (n--) { cin >> p >> t; pt.push_back({p, t}); d[{p, t}]++; } sort(pt.begin(), pt.end(), comp); auto a = pt[k - 1]; cout << d[a] << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, c = getchar(), f = 0; for (; c > '9' || c < '0'; f = c == '-', c = getchar()) ; for (; c >= '0' && c <= '9'; x = (x << 1) + (x << 3) + c - '0', c = getchar()) ; return f ? -x : x; } inline void write(int x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) write(x / 10); putchar(x % 10 + 48); } const int mo = 1e9 + 7; const int N = 1000000 + 10; vector<pair<int, int> > L[N], R[N]; set<pair<int, int> > Set; int n, m, k; int Ql, Qr, Qv, tag[N * 4], sum[N * 4]; void down(int o) { int &x = tag[o]; if (x != -1) { tag[o << 1] = tag[o << 1 | 1] = sum[o << 1] = sum[o << 1 | 1] = x; x = -1; } } void modify(int o, int l, int r) { if (Ql <= l && Qr >= r) { tag[o] = sum[o] = Qv; return; } down(o); if (Ql <= ((l + r) >> 1)) modify(o << 1, l, ((l + r) >> 1)); if (Qr > ((l + r) >> 1)) modify(o << 1 | 1, ((l + r) >> 1) + 1, r); sum[o] = sum[o << 1]; if ((sum[o] += sum[o << 1 | 1]) >= mo) sum[o] -= mo; } int query(int o, int l, int r) { if (Ql > Qr) return 0; if (Ql <= l && Qr >= r) return sum[o]; down(o); int res = 0; if (Ql <= ((l + r) >> 1)) res = query(o << 1, l, ((l + r) >> 1)); if (Qr > ((l + r) >> 1)) if ((res += query(o << 1 | 1, ((l + r) >> 1) + 1, r)) >= mo) res -= mo; return res; } int main() { n = read(), m = read(), k = read(); for (int i = 1, _y = k; i <= _y; ++i) { int saklfjhau = read(), daiosfhas = read(), wsdkfihoq = read(), edqwioury = read(); L[saklfjhau].push_back(make_pair(daiosfhas, edqwioury)); R[wsdkfihoq + 1].push_back(make_pair(daiosfhas, edqwioury)); } memset(tag, -1, sizeof(int) * (n + 5) * 4); Set.insert(make_pair(0, 0)); for (int i = 0, _y = L[1].size() - 1; i <= _y; ++i) Set.insert(L[1][i]); Ql = Qr = Qv = 1, modify(1, 1, m); for (int i = 2, _y = n; i <= _y; ++i) { sort(L[i].begin(), L[i].end()); for (int j = L[i].size() - 1, _y = 0; j >= _y; --j) { int u = L[i][j].second; if (u >= m) continue; Ql = (--Set.upper_bound(make_pair(u + 2, 0)))->second + 1, Qr = u + 1, Qv = query(1, 1, m); Ql = u + 1, Qr = u + 1, modify(1, 1, m); } for (int j = 0, _y = R[i].size() - 1; j <= _y; ++j) Set.erase(R[i][j]); for (int j = 0, _y = L[i].size() - 1; j <= _y; ++j) { Set.insert(L[i][j]); Ql = L[i][j].first, Qr = L[i][j].second, Qv = 0, modify(1, 1, m); } } Ql = Set.rbegin()->first + 1, Qr = m, printf("%d\n", query(1, 1, m)); }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; while(n--){ double x1,x2,x3,y1,y2,y3; cin>>x1>>y1>>x2>>y2>>x3>>y3; double a,b,c; a=pow((x2-x3),2)+pow((y2-y3),2); b=pow((x3-x1),2)+pow((y3-y1),2); c=pow((x1-x2),2)+pow((y1-y2),2); double ox,oy; ox=(a*(b+c-a)*x1+b*(c-b+a)*x2+c*(a+b-c)*x3)/(2*(a*b+b*c+c*a)-(a*a+b*b+c*c)); oy=(a*(b+c-a)*y1+b*(c-b+a)*y2+c*(a+b-c)*y3)/(2*(a*b+b*c+c*a)-(a*a+b*b+c*c)); double r; r=hypot((ox-x1),(oy-y1)); printf("%.3lf %.3lf %.3lf\n",ox,oy,r); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; const double pi = acos(-1.0), eps = 1e-6; double dist(int x, int y) { return sqrt((long long)x * x + (long long)y * y); } struct Range { double l, r; bool operator<(const Range &b) const { return fabs(l - b.l) < eps ? r > b.r : l < b.l; } } R[maxn << 1]; int X[maxn], Y[maxn]; int n, m, N; void DFS(int x, int *f, int *g, int *outrange) { int res = 0, X = x; while (!g[x] && x + f[x] <= N) { res++; x += f[x]; } if (!g[x]) { outrange[x] = x + f[x] - 1 - N; g[x] = 1; } while (X != x) { g[X] = g[x] + res--; outrange[X] = outrange[x]; X += f[X]; } } int Solve(double radius) { static int f[maxn], g[maxn], outrange[maxn]; for (int i = 1; i <= n; i++) { double delta = acos(radius / dist(X[i], Y[i])); double initangle = atan2(Y[i], X[i]); R[i].l = initangle - delta; R[i].r = initangle + delta; if (R[i].l < 0) R[i].l += 2 * pi; if (R[i].r < 0) R[i].r += 2 * pi; if (R[i].r < R[i].l) R[i].r += 2 * pi; } sort(R + 1, R + 1 + n); N = 0; for (int i = 1; i <= n; i++) { while (N && R[i].r < R[N].r) N--; R[++N] = R[i]; } while (R[N].r - 2 * pi >= R[1].r) N--; for (int i = N + 1; i <= N + N; i++) R[i] = R[i - N], R[i].l += 2 * pi; memset(g, 0, sizeof(int) * (N + 1)); memset(outrange, 0, sizeof(int) * (N + 1)); for (int i = 1, r = 1; i <= N; i++) { while (r < N + i - 1 && R[r + 1].l <= R[i].r) r++; f[i] = r - i + 1; } if (f[1] == N) return 1; int res = N; for (int i = 1; i <= f[1] + 1; i++) { DFS(i, f, g, outrange); res = min(res, g[i] + (outrange[i] < i - 1)); } return res; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d%d", &X[i], &Y[i]); double l = 0, r = 1e10, mid; for (int i = 1; i <= n; i++) r = min(r, dist(X[i], Y[i])); while (r - l > eps) Solve(mid = (l + r) / 2.0) > m ? r = mid : l = mid; printf("%.9f\n", l); }
5
#include <bits/stdc++.h> using namespace std; int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s; cin >> s; long long n = s.length(); long long st_row[] = {4, 5, 6, 3, 2, 1}; long long i, m = 0, ans = 0; for (--n, i = 0; i < n; i++) { m = m * 10 + (s[i] - '0'); } ans = (m / 4 - (m % 4 == 0)) * 16 + (m % 4 % 2 == 0 ? 7 : 0) + st_row[s[n] - 'a']; cout << ans; return 0; }
2
#include<bits/stdc++.h> using namespace std; int n,m; int main(){ scanf("%d%d",&n,&m); printf("%d\n",m==1?0:n-m); return 0; }
0
#include <bits/stdc++.h> int main() { int a, b, c = 0, d = 0; scanf("%d%d", &a, &b); int m[1005] = {0}, n[1005] = {0}; for (int i = 0; i < a; i++) { scanf("%d", &m[i]); } for (int i = 0; i < b; i++) { scanf("%d", &n[i]); } for (int i = 0; i < a; i++) { if (n[c] >= m[i]) { c++; d++; } } printf("%d\n", d); return 0; }
1
#include <bits/stdc++.h> using namespace std; long long INF = 1E9; long long M = 1E9 + 7; inline long long power(long long a, long long p) { long long res = 1, x = a; while (p) { if (p & 1) res = (res * x); x = (x * x); p >>= 1; } return res; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); if (fopen("input.txt", "r")) { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } int n; cin >> n; vector<char> query(n); vector<string> word(n); for (int i = 0; i < n; i++) { cin >> query[i] >> word[i]; } set<char> s; for (int i = 0; i < 26; i++) { s.insert('a' + i); } char ans = word[n - 1][0]; int guesses = 26, excess = 0; for (int i = 0; i < n; i++) { if (query[i] == '!') { if ((int)s.size() == 1) excess++; set<char> temp, aux; for (int j = 0; j < (int)word[i].size(); j++) { temp.insert(word[i][j]); } for (int i = 0; i < 26; i++) { if (s.find('a' + i) != s.end() && temp.find('a' + i) != temp.end()) aux.insert('a' + i); } s = aux; guesses = min(guesses, (int)s.size()); } else if (query[i] == '.') { for (int j = 0; j < (int)word[i].size(); j++) { if (s.find(word[i][j]) != s.end()) s.erase(word[i][j]); } } else if (query[i] == '?') { if ((int)s.size() == 1 && word[i][0] != ans) excess++; if (s.find(word[i][0]) != s.end()) { guesses--; s.erase(word[i][0]); } } } cout << excess << "\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; char A[2005]; int ctos, debe, va; long long D[2005][2005], modd = 1000000007, suma; int main() { scanf("%d%d%s", &ctos, &debe, A + 1); for (int queda = 0; queda <= debe; queda++) { suma = 0; if (queda == 0) suma = 1; for (int dnde = ctos + 1; dnde > 0; dnde--) { if (queda == 0 and dnde == ctos) D[dnde][queda] = A[dnde] - 'a' + 1; else if (dnde > ctos) { D[dnde][queda] = 0; if (queda == 0) D[dnde][queda] = 1; } else { D[dnde][queda] = D[dnde][queda] + suma; D[dnde][queda] = D[dnde][queda] % modd; } suma = suma + D[dnde][queda] * (A[dnde - 1] - 'a'); suma = suma % modd; va = dnde - 1; for (int i = queda + ctos - dnde + 2; i <= debe and va > 0; i += ctos - dnde + 2) { D[va][i] = D[va][i] + D[dnde][queda] * ('z' - A[dnde - 1]); D[va][i] = D[va][i] % modd; --va; } } } printf("%I64d\n", D[1][debe]); return 0; }
5
#include <bits/stdc++.h> using namespace std; long long sqr(long long x) { return x * x; } int mysqrt(long long x) { int l = 0, r = 1e9 + 1; while (r - l > 1) { int m = (l + r) / 2; if (m * (long long)m <= x) l = m; else r = m; } return l; } mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); mt19937_64 rndll( chrono::high_resolution_clock::now().time_since_epoch().count()); long long AR = 19, BR = 13, CR = 23, XR = 228, YR = 322, MODR = 1e9 + 993; long long myrand() { long long ZR = (XR * AR + YR * BR + CR) % MODR; XR = YR; YR = ZR; return ZR; } int gcd(int a, int b) { return a ? gcd(b % a, a) : b; } int gcdex(int a, int b, int &x, int &y) { if (a == 0) { x = 0; y = 1; return b; } int x1, y1; int ret = gcdex(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return ret; } const int Mod = 998244353; int Bpow(int x, long long y) { int ret = 1; int w = x; while (y) { if (y & 1) ret = (ret * (long long)w) % Mod; w = (w * (long long)w) % Mod; y >>= 1; } return ret; } int Bdiv(int x) { int a, b; gcdex(x, Mod, a, b); if (a < 0) a += Mod; return a; } int Bdiv(int x, int y) { return (x * (long long)Bpow(y, Mod - 2)) % Mod; } inline int ad(int x, int y) { return x + y >= Mod ? x + y - Mod : x + y; } inline int dif(int x, int y) { return x - y < 0 ? x - y + Mod : x - y; } inline int mul(int x, int y) { return (x * (long long)y) % Mod; } int inv(int a) { int b = Mod, x = 0, y = 1; while (a) { int t = b / a; b -= a * t; x -= t * y; swap(a, b); swap(x, y); } if (x < 0) x += Mod; return x; } void setmin(int &x, int y) { x = min(x, y); } void setmax(int &x, int y) { x = max(x, y); } void setmin(long long &x, long long y) { x = min(x, y); } void setmax(long long &x, long long y) { x = max(x, y); } const long long llinf = 4e18 + 100; const long double eps = 1e-9; const int maxn = 2e5 + 100, maxw = 1e6 + 100, inf = 2e9 + 100, sq = 600, X = 43, mod = 1e9 + 7, LG = 17; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, s; cin >> n >> s; vector<int> e(n); for (int i = 1; i < n; i++) { int v, u; cin >> v >> u; v--; u--; e[v]++; e[u]++; } int w = 0; for (int i = 0; i < n; i++) w += (e[i] == 1); cout << fixed << setprecision(20) << 2 * s / (double)w; }
2
#include <bits/stdc++.h> using namespace std; int n; int dat[128]; pair<int, int> d2[128]; int gcd(int a, int b) { while (b) { int tmp = a % b; a = b; b = tmp; } return a; } vector<int> cand; int ans = INT_MAX; vector<int> anscand; void rec(int lower) { if (cand.size() > n) { return; } int cur = 0; for (int i = 0; i < n - cand.size(); i++) { cur += dat[i] - 1; } for (int i = n - cand.size(), j = 0; i < n; i++, j++) { cur += abs(cand[j] - dat[i]); } if (ans > cur) { ans = cur; anscand.clear(); for (int i = 0; i < n - cand.size(); i++) { anscand.push_back(1); } for (int i = n - cand.size(), j = 0; i < n; i++, j++) { anscand.push_back(cand[j]); } } for (int j = lower; j <= 59; j++) { bool fail = false; for (int i = 0; i < cand.size(); i++) { if (gcd(cand[i], j) != 1) { fail = true; break; } } if (fail) continue; cand.push_back(j); rec(j + 1); cand.pop_back(); } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &d2[i].first); d2[i].second = i; } std::sort(d2, d2 + n); for (int i = 0; i < n; i++) dat[i] = d2[i].first; rec(2); vector<int> real(n); for (int i = 0; i < n; i++) { real[d2[i].second] = anscand[i]; } for (int i = 0; i < n; i++) { printf("%d%c", real[i], (i + 1 == n) ? '\n' : ' '); } return 0; }
4
#include <bits/stdc++.h> using namespace std; long long C, h1, h2, w1, w2; int main() { cin >> C >> h1 >> h2 >> w1 >> w2; long long asd = C / w1, ans = 0; for (long long i = asd, k = 1; i >= 0 && k <= 10000000; i--, k++) { long long left = C - i * w1; left /= w2; ans = max(ans, i * h1 + left * h2); } asd = C / w2; for (long long i = asd, k = 1; i >= 0 && k <= 10000000; i--, k++) { long long left = C - i * w2; left /= w1; ans = max(ans, i * h2 + left * h1); } cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int INF = (-1u) / 2; const long long int INF2 = (-1ull) / 2; int a[310], b[310], i, d[310][310], j, k, n, m, timer = 0, l, r, x, y, len, prevv[310], now[310], mx[310], mn[310]; int c[1011000], cnt = 0, fl = 0, a2, a3 = -1000000, ans = 0, res[1110]; deque<int> v; deque<int>::iterator it; void doo(int id, int len, int sum, int st) { if (id == len) return; int x = st; for (; x < 10; x++) { if ((len - id - 1) * 9 >= sum - x) { v[id] = x; doo(id + 1, len, sum - x, 0); return; } } } int main() { scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &c[i]); } x = c[0]; while (x > 0) { v.push_back(min(x, 9)); x -= min(x, 9); } sort(v.begin(), v.end()); for (i = 0; i < v.size(); i++) cout << v[i]; cout << endl; for (i = 1; i < n; i++) { cnt = 0; for (j = 0; j < v.size(); j++) { cnt += v[j]; } k = v.size(); for (j = k - 1; j >= 0; j--) { if (cnt >= c[i] || c[i] > (cnt - v[j] + (k - j) * 9) || v[j] == 9) { cnt -= v[j]; if (j == 0) { j = 1; k++; v.push_front(0); } continue; } cnt -= v[j]; c[i] -= cnt; doo(j, k, c[i], v[j] + 1); break; } for (j = 0; j < v.size(); j++) cout << v[j]; cout << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { long long int n, k, A[100], i, j; cin >> n >> k; for (i = 0; i < n; i++) { cin >> A[i]; } sort(A, A + n); cout << A[n - k]; return 0; }
2
#include <bits/stdc++.h> using namespace std; vector<long long> gp[100001]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, s; cin >> n >> s; for (long long i = 0; i < n - 1; i++) { long long a, b; cin >> a >> b; gp[a].push_back(b); gp[b].push_back(a); } long long one = 0; for (long long i = 1; i < n + 1; i++) { if (gp[i].size() == 1) one++; } long double ans = 2 * s; cout << setprecision(15) << ans / (long double)one << endl; return 0; }
2
#include <cstdio> using namespace std; char map[8][8]; void check(void); int main() { while (scanf(" %c", &map[0][0]) != EOF){ for (int i = 1; i < 8; i++){ scanf(" %c", &map[0][i]); } for (int i = 1; i < 8; i++){ for (int j = 0; j < 8; j++){ scanf(" %c", &map[i][j]); } } check(); } return (0); } void check(void) { for (int i = 0; i < 8; i++){ for (int j = 0; j < 8; j++){ if (map[i][j] == '1'){ if (map[i][j + 1] == '1' && map[i + 1][j] == '1' && map[i + 1][j + 1] == '1'){ puts("A"); } else if (map[i + 1][j] == '1' && map[i + 2][j] == '1' && map[i + 3][j] == '1'){ puts("B"); } else if (map[i][j + 1] == '1' && map[i][j + 2] == '1' && map[i][j + 3] == '1'){ puts("C"); } else if (map[i + 1][j] == '1' && map[i + 1][j - 1] == '1' && map[i + 2][j - 1] == '1'){ puts("D"); } else if (map[i][j + 1] == '1' && map[i + 1][j + 1] == '1' && map[i + 1][j + 2] == '1'){ puts("E"); } else if (map[i + 1][j] == '1' && map[i + 1][j + 1] == '1' && map[i + 2][j + 1] == '1'){ puts("F"); } else if (map[i][j + 1] == '1' && map[i + 1][j] == '1' && map[i + 1][j - 1] == '1'){ puts("G"); } } } } }
0
#include <bits/stdc++.h> using namespace std; const int INF = 2147000000; const long long inf = 1e18; long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } void exgcd(long long a, long long b, long long& d, long long& x, long long& y) { if (!b) { d = a; x = 1; y = 0; } else { exgcd(b, a % b, d, y, x); y -= x * (a / b); } } long long pow_mod(long long a, long long p, long long n) { if (p == 0) return 1; long long ans = pow_mod(a, p / 2, n); ans = ans * ans % n; if (p % 2 == 1) ans = ans * a % n; return ans; } const int maxn = 6e5 + 100; struct Seg { int l, r; } seg[maxn]; vector<int> V; int n; int sumv[4 * maxn], maxv[4 * maxn], addv[4 * maxn]; void maintain(int o) { int lc = 2 * o, rc = 2 * o + 1; sumv[o] = sumv[lc] + sumv[rc]; maxv[o] = max(maxv[lc], maxv[rc]); } void pushdown(int o, int L, int R) { if (addv[o]) { int lc = 2 * o, rc = 2 * o + 1; int M = L + (R - L) / 2; addv[lc] += addv[o]; addv[rc] += addv[o]; maxv[lc] += addv[o]; maxv[rc] += addv[o]; sumv[lc] += (M - L + 1) * addv[o]; sumv[rc] += (R - M) * addv[o]; addv[o] = 0; } } void update(int o, int L, int R, int ql, int qr, int v) { if (ql <= L && qr >= R) { addv[o] += v; sumv[o] += v * (R - L + 1); maxv[o] += v; return; } int M = L + (R - L) / 2; pushdown(o, L, R); if (ql <= M) { update(2 * o, L, M, ql, qr, v); } if (qr > M) { update(2 * o + 1, M + 1, R, ql, qr, v); } maintain(o); } int query(int o, int L, int R, int v) { if (maxv[o] < v) return 0; if (sumv[o] == (R - L + 1) * v) { return V[R + 1] - V[L]; } pushdown(o, L, R); int M = L + (R - L) / 2; int res = 0; if (maxv[2 * o] >= v) res = query(2 * o, L, M, v); if (maxv[2 * o + 1] >= v) res += query(2 * o + 1, M + 1, R, v); maintain(o); return res; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d%d", &seg[i].l, &seg[i].r); V.push_back(seg[i].l); V.push_back(seg[i].r); } sort(V.begin(), V.end()); V.erase(unique(V.begin(), V.end()), V.end()); int N = V.size(); int flag = 0; for (int i = 1; i <= n; i++) { int ql = lower_bound(V.begin(), V.end(), seg[i].l) - V.begin(); int qr = lower_bound(V.begin(), V.end(), seg[i].r) - V.begin(); qr--; if (ql > qr) { flag++; continue; } update(1, 0, N - 1, ql, qr, 1); } if (flag) { if (flag >= 2 || flag == n) { printf("0\n"); return 0; } printf("%d\n", query(1, 0, N - 1, n - flag)); return 0; } int ans = 0; for (int i = 1; i <= n; i++) { int ql = lower_bound(V.begin(), V.end(), seg[i].l) - V.begin(); int qr = lower_bound(V.begin(), V.end(), seg[i].r) - V.begin(); qr--; update(1, 0, N - 1, ql, qr, -1); ans = max(ans, query(1, 0, N - 1, n - 1)); update(1, 0, N - 1, ql, qr, 1); } printf("%d\n", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int INF = 2147483647; const long long LLINF = 9223372036854775807LL; const int ST = 100010; const int ST1 = 1000010; const long long MOD = 1000000007; long long ABS(long long a) { if (a < 0) return a * (-1); else return a; } int main() { int n, m, s; cin >> n >> m >> s; long long ans = 0; int n1 = (n - 1) / 2, m1 = (m - 1) / 2; for (int x = 0; x <= m1; x++) { for (int y = 0; y <= n1; y++) { for (int x1 = 0; x1 <= m1; x1++) { long long d = x * 2 + 1; long long h = y * 2 + 1; long long sc = d * h; long long d1 = x1 * 2 + 1; if (sc > s) break; if (sc == s && x1 <= x) { ans += (y + 1) * ((m - d + 1) * (n - h + 1)); } else { if (x1 <= x) { long long ned = s - sc; long long h1 = ned / d1; if (h1 % 2 == 1 || ned % d1 != 0) continue; if (h1 + h <= n) { ans += (n - h1 - h + 1) * (m - d + 1); } } else { long long h1 = (s - d * h) / (d1 - d); if (s % d1 == 0 && s / d1 > h && n - s / d1 + 1 > 0) ans += (n - s / d1 + 1) * (m - d1 + 1); if (h1 > h || h1 % 2 == 0 || (s - d * h) % (d1 - d) != 0) continue; ans += (n - h + 1) * (m - d1 + 1); } } } } } cout << ans; }
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 5e6 + 13; const int MAXK = 5e6 + 13; const long long INF = 2e9 + 7; const long long MD = 1e9 + 7; const long double EPS = 1e-9; const long double PI = 3.14159265359; bool check(long long a, long long b, long long h, long long w) { return (a <= h && b <= w) || (a <= w && b <= h); } void solve() { long long a, b, h, w; int n; cin >> a >> b >> h >> w >> n; vector<long long> prods(n); for (int i = 0; i < n; i++) { cin >> prods[i]; } if (check(a, b, h, w)) { cout << 0; return; } sort(prods.begin(), prods.end(), greater<long long>()); vector<long long> dp(MAXN, 0); dp[h] = w; for (int i = 0; i < min(34, n); i++) { for (int j = MAXN - 1; j >= 0; j--) { if (dp[j]) { long long value = min(dp[j] * prods[i], 1ll * (MAXN - 1)), index = min(j * prods[i], 1ll * (MAXN - 1)); dp[index] = max(dp[index], dp[j]); dp[j] = max(dp[j], value); if (check(a, b, dp[j], j) || check(a, b, dp[index], index)) { cout << i + 1; return; } } } } cout << -1; } int main() { solve(); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MAXn = 100 + 17; int n; int arr[MAXn]; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> arr[i]; int min = 100 + 17, max = 0, jamin = 0, jamax = 0; for (int i = n; i >= 1; i--) { if (arr[i] < min) { min = arr[i]; jamin = i; } if (arr[n - i + 1] > max) { max = arr[n - i + 1]; jamax = n - i + 1; } } if (jamin < jamax) cout << n - jamin + jamax - 1 - 1 << endl; else cout << n - jamin + jamax - 1 << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 1000 + 10; vector<long long> p; long long a, b; void DFS(long long x) { if (x > b) return; p.push_back(x); if (x == b) { printf("YES\n"); printf("%lld\n", (long long)p.size()); for (int i = 0; i < (int)p.size(); i++) printf("%lld%c", p[i], i == (int)p.size() - 1 ? '\n' : ' '); exit(0); } DFS(2 * x); DFS(10 * x + 1); p.pop_back(); } int main() { scanf("%d%d", &a, &b); p.clear(); DFS(a); printf("NO\n"); return 0; }
1