solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; vector<int> a(n), c(n); multiset<int> b; const int q = 2e5 + 7; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) { int x; cin >> x; b.insert(x); } for (int i = 0; i < n; i++) { int mn = (n - a[i]) % n; auto it = b.equal_range(mn).first; if (it != b.end()) { c[i] = (a[i] + *it) % n; b.erase(it); } else { it = b.equal_range(*b.begin()).first; c[i] = (a[i] + *it) % n; b.erase(it); } } for (int i = 0; i < n; i++) cout << c[i] << " "; return 0; }
11
CPP
S = input() N = len(S) K = int(input()) Data = set() for s in range(N): for e in range(s+1,min(N+1,s+1+K+1)): Data.add(S[s:e]) print(sorted(Data)[K-1])
0
PYTHON3
#include<iostream> #include<algorithm> #include<string> using namespace std; int main(){ int N,last,sum; bool a[200001],b[200001]={0}; cin>>N; for(int i=1;i<=N;i++)cin>>a[i]; for(int i=N;i>=1;i--){ last=N/i; sum=0; for(int j=last;j>=2;j--)sum+=b[j*i]; b[i]= sum%2 != a[i]; } sum=0; for(int i=1;i<=N;i++)sum+=b[i]; cout<<sum<<endl; for(int i=1;i<=N;i++)if(b[i])cout<<i<<" "; return 0; }
0
CPP
mis,ch=0,0 for _ in range(int(input())): m,c=input().split() m=int(m);c=int(c) if m>c: mis=mis+1 if c>m: ch=ch+1 if mis>ch: print("Mishka") if ch>mis: print("Chris") if ch==mis: print("Friendship is magic!^^")
7
PYTHON3
#define _USE_MATH_DEFINES #include<iostream> #include<cstdio> #include<algorithm> #include<climits> #include<string> #include<vector> #include<list> #include<map> #include<set> #include<cmath> #include<queue> #include<cstring> #include<stack> #include<functional> #include<sstream> using namespace std; typedef long long ll; const ll MOD = 1000000007; int dx[5] = {0, 1, 0, -1, 0}, dy[5] = {0, 0, 1, 0, -1}; int fld[10][10]; void Reverse(int x, int y, int cnt[10][10]) { for(int i = 0; i < 5; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if(0 <= nx && nx < 10 && 0 <= ny && ny < 10) cnt[ny][nx]++; } } int main(){ int N; cin >> N; for(int c = 0; c < N; c++) { for(int i = 0; i < 10; i++) { for(int j = 0; j < 10; j++) { cin >> fld[i][j]; } } bool ans = false; int out[10][10]; for(int p = 0; p < (1 << 10); p++) { int cnt[10][10] = {}; for(int i = 0; i < 10; i++) { out[0][i] = ((p >> i) & 1); if(out[0][i] == 1) Reverse(i, 0, cnt); } for(int i = 0; i < 9; i++) { for(int j = 0; j < 10; j++) { if(((fld[i][j] + cnt[i][j]) & 1) == 1) { out[i + 1][j] = 1; Reverse(j, i + 1, cnt); } else { out[i + 1][j] = 0; } } } ans = true; for(int i = 0; i < 10; i++) { if(((fld[9][i] + cnt[9][i]) & 1) == 1) ans = false; } if(ans) break; } for(int i = 0; i < 10; i++) { printf("%d", out[i][0]); for(int j = 1; j < 10; j++) { printf(" %d", out[i][j]); } puts(""); } } return 0; }
0
CPP
n=int(input()) l=list(map(int,input().split())) k=l.count(1) l1=[] c=1 for i in range(1,len(l)): if(l[i]==1): l1.append(c) c=1 else: c+=1 l1.append(c) print(k) print(*l1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, i, u, flag = 0, j, ctr = 0; scanf("%d", &n); vector<int> graph[n + 1]; for (i = 1; i < n; i++) { scanf("%d", &u); graph[u].push_back(i + 1); } for (i = 1; i <= n; i++) { if (graph[i].size() != 0) { ctr = 0; for (j = 0; j < graph[i].size(); j++) { int k = graph[i][j]; if (graph[k].size() == 0) { ctr++; } } if (ctr < 3) { flag = 1; break; } } } if (flag == 1) { printf("NO\n"); } else { printf("YES\n"); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int m1 = 998244353; const int m2 = 977787779; const int m3 = 950747059; struct hsh { int x, y, z; hsh(int a = 0) : x(a % m1), y(a % m2), z(a % m3) {} hsh(int a, int b, int c) : x(a), y(b), z(c) {} hsh operator+(hsh b) { return hsh((x + b.x) % m1, (y + b.y) % m2, (z + b.z) % m3); } hsh operator-(hsh b) { return hsh((x - b.x + m1) % m1, (y - b.y + m2) % m2, (z - b.z + m3) % m3); } hsh operator*(hsh b) { return hsh((long long)x * b.x % m1, (long long)y * b.y % m2, (long long)z * b.z % m3); } bool operator<(hsh b) const { return make_tuple(x, y, z) < make_tuple(b.x, b.y, b.z); } bool operator==(hsh b) const { return make_tuple(x, y, z) == make_tuple(b.x, b.y, b.z); } }; const int N = 200050; const int L = 18; struct EERTREE { int IMAG, ZERO, tsz, go[N][26], len[N], link[N][L], at, sz, nod[N], pos[N]; char s[N]; void init() { tsz = sz = 0; IMAG = ++tsz; link[IMAG][0] = IMAG; len[IMAG] = -1; ZERO = ++tsz; link[ZERO][0] = IMAG; len[ZERO] = 0; at = IMAG; } EERTREE() { init(); } void Grow(char c) { s[++sz] = c; while (c != s[sz - len[at] - 1]) at = link[at][0]; if (!go[at][c - 'a']) { go[at][c - 'a'] = ++tsz; pos[tsz] = sz; len[tsz] = len[at] + 2; int suf = link[at][0]; while (c != s[sz - len[suf] - 1]) suf = link[suf][0]; if (len[tsz] == 1) link[tsz][0] = ZERO; else link[tsz][0] = go[suf][c - 'a']; } at = go[at][c - 'a']; nod[sz] = at; } void Build(char s[], int n) { init(); for (int i = 1; i <= n; i++) Grow(s[i]); for (int j = 1; j < L; j++) for (int i = 1; i <= tsz; i++) link[i][j] = link[link[i][j - 1]][j - 1]; } int Get(int u, int x) { if (len[u] <= x) return len[u]; for (int i = L - 1; ~i; i--) if (len[link[u][i]] > x) u = link[u][i]; return len[link[u][0]]; } bool Check(int u, int x) { for (int i = L - 1; ~i; i--) if (len[link[u][i]] >= x) u = link[u][i]; return len[u] == x; } } FW[2], BW[2]; int Decompose(int t, int l, int r, int n) { int pre = BW[t].Get(BW[t].nod[n - l + 1], r - l + 1); if (pre > 0 && FW[t].Check(FW[t].nod[r], r - l + 1 - pre)) return pre; int suf = FW[t].Get(FW[t].nod[r], r - l + 1); if (suf > 0 && BW[t].Check(BW[t].nod[n - l + 1], r - l + 1 - suf)) return r - l + 1 - suf; return -1; } char s[N], t[N]; hsh H[2][N], po[N]; hsh Get(int t, int l, int r) { return H[t][r] - H[t][l - 1] * po[r - l + 1]; } map<hsh, int> cnt[2]; set<hsh> st[2]; int main() { int n, m; scanf("%s", s + 1); n = strlen(s + 1); scanf("%s", t + 1); m = strlen(t + 1); FW[0].Build(s, n); FW[1].Build(t, m); reverse(s + 1, s + 1 + n); reverse(t + 1, t + 1 + m); BW[0].Build(s, n); BW[1].Build(t, m); reverse(s + 1, s + 1 + n); reverse(t + 1, t + 1 + m); long long ans = (long long)(FW[0].tsz - 2) * (FW[1].tsz - 2); po[0] = hsh(1); po[1] = hsh(777); for (int i = 2; i < N; i++) po[i] = po[i - 1] * po[1]; for (int i = 1; i <= n; i++) H[0][i] = H[0][i - 1] * po[1] + hsh(s[i] - 'a' + 1); for (int i = 1; i <= m; i++) H[1][i] = H[1][i - 1] * po[1] + hsh(t[i] - 'a' + 1); for (int t = 0; t < 2; t++) { for (int i = 3; i <= FW[t].tsz; i++) { st[t].insert(Get(t, FW[t].pos[i] - FW[t].len[i] + 1, FW[t].pos[i])); int border = FW[t].link[i][0], l, r; if (border < 3) continue; if (t == 0) l = FW[t].pos[i] - (FW[t].len[i] - FW[t].len[border]) + 1, r = FW[t].pos[i]; else l = FW[t].pos[i] - FW[t].len[i] + 1, r = FW[t].pos[i] - FW[t].len[border]; if (FW[t].len[border] * 2 >= FW[t].len[i]) cnt[t][Get(t, l, r)]++; } } for (int t = 0; t < 2; t++) { for (int i = 3; i <= FW[t].tsz; i++) { int border = FW[t].link[i][0], l, r; if (border < 3) continue; if (t == 0) l = FW[t].pos[i] - (FW[t].len[i] - FW[t].len[border]) + 1, r = FW[t].pos[i]; else l = FW[t].pos[i] - FW[t].len[i] + 1, r = FW[t].pos[i] - FW[t].len[border]; ans -= cnt[t ^ 1][Get(t, l, r)]; int pre = Decompose(t, l, r, t == 0 ? n : m); if (~pre && pre != r - l + 1) { if (t == 0) { if (st[t ^ 1].count(Get(t, l, l + pre - 1)) && st[t ^ 1].count(Get(t, l, r) * po[pre] + Get(t, l, l + pre - 1))) ans--; } else { int suf = r - l + 1 - pre; if (st[t ^ 1].count(Get(t, r - suf + 1, r)) && st[t ^ 1].count(Get(t, r - suf + 1, r) * po[r - l + 1] + Get(t, l, r))) ans--; } } } } for (int t = 0; t < 2; t++) { for (int i = 3; i <= FW[t].tsz; i++) { int border = FW[t].link[i][0], l, r; if (border < 3) continue; if (t == 0) l = FW[t].pos[i] - (FW[t].len[i] - FW[t].len[border]) + 1, r = FW[t].pos[i]; else l = FW[t].pos[i] - FW[t].len[i] + 1, r = FW[t].pos[i] - FW[t].len[border]; if (FW[t].len[border] * 2 < FW[t].len[i]) cnt[t][Get(t, l, r)]++; } } for (auto p : cnt[0]) ans += (long long)p.second * cnt[1][p.first]; printf("%lld\n", ans); return 0; }
14
CPP
k,n,w=map(int,input().split()) s=0 for i in range (1,w+1): s+=k*i d=s-n if d<=0: print(0) else: print(d)
7
PYTHON3
ans=0 x=int(input()) for i in range(int(x**0.5)+1): for j in range(10): k=i**j if k<=x: ans=max(ans,k) print(ans)
0
PYTHON3
n=list(input()) def sw(w): ones=w.count('1') a=list(filter(lambda x: x!='1',w)) if '2' in a: i=a.index('2') print(''.join(a[:i])+'1'*ones+''.join(a[i:])) else: print(''.join(a) + '1' * ones) sw(n)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n, m, S, d, x[300001]; vector<pair<char, long long> > ans; bool run(long long pos, long long l) { if (x[l + 1] >= pos && x[l + 1] <= pos + S) return false; return true; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> S >> d; for (int i = 0; i < n; i++) cin >> x[i]; sort(x, x + n); long long start = 0; for (int i = 0; i < n; i++) { ans.push_back(make_pair('r', x[i] - 1 - start)); if (x[i] - 1 - start < S) { cout << "IMPOSSIBLE"; return 0; } start = x[i] - 1; for (int j = i; j < n; j++) { if (run(x[j] + 1, j)) { if (x[j] + 1 - start > d) { cout << "IMPOSSIBLE"; return 0; } ans.push_back(make_pair('j', x[j] + 1 - start)); start = x[j] + 1; i = j; break; } else { if (j == n - 1) { cout << "IMPOSSIBLE"; return 0; } } } } if (ans.empty()) { cout << "IMPOSSIBLE"; return 0; } for (int i = 0; i < ans.size(); i++) { if (ans[i].first == 'r') cout << "RUN "; else cout << "JUMP "; cout << ans[i].second << endl; } if (start != m) cout << "RUN " << m - start; }
10
CPP
#include<stdio.h> #include<stdlib.h> #include<string.h> int top; int S[1000]; int pop(){ top--; return S[top+1]; } void push(int x){ S[++top]=x; } int main(){ char s[100]; int a,b; top=0; while(~scanf("%s",s)){ if(s[0]=='+'){ a=pop(); b=pop(); push(a+b); } else if(s[0]=='-'){ b=pop(); a=pop(); push(a-b); } else if(s[0]=='*'){ a=pop(); b=pop(); push(a*b); } else{ push(atoi(s)); } } printf("%d\n",pop()); }
0
CPP
board = list(map(int,input().split())) print((board[0]*board[1])//2)
7
PYTHON3
n = int(input()) l = list(map(int, input().split())) s = {0} ls = [0] x = 0 for i in l: x += i s.add(x) ls.append(x) if (len(s) == n) and (max(s) - min(s)) == n - 1: mn = min(s) - 1 for i in ls: print(i - mn, end = ' ') exit() print(-1)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 100 + 23; const long long MOD = 1000000007; int n, m, ans, ansx, ansy; char s[maxn][maxn]; long long H, H1, H2, H3, H4; int solve(int x, int y) { map<long long, bool> M; for (int i = 1; i <= n; i += x) for (int j = 1; j <= m; j += y) { H1 = 0, H2 = 0; for (int a = i; a <= i + x - 1; a++) for (int b = j; b <= j + y - 1; b++) H1 = (H1 * 28 + (long long)(s[a][b] - 'A' + 1)) % MOD; for (int a = i + x - 1; a >= i; a--) for (int b = j + y - 1; b >= j; b--) H2 = (H2 * 28 + (long long)(s[a][b] - 'A' + 1)) % MOD; if (x == y) { H3 = 0, H4 = 0; for (int b = j; b <= j + y - 1; b++) for (int a = i + x - 1; a >= i; a--) H3 = (H3 * 28 + (long long)(s[a][b] - 'A' + 1)) % MOD; for (int b = j + y - 1; b >= j; b--) for (int a = i; a <= i + x - 1; a++) H4 = (H4 * 28 + (long long)(s[a][b] - 'A' + 1)) % MOD; } if ((M[H1]) || (M[H2])) return false; if (x == y) if ((M[H3]) || (M[H4])) return false; M[H1] = true, M[H2] = true; if (x == y) M[H3] = true, M[H4] = true; } if ((x * y) <= (ansx * ansy)) ansx = x, ansy = y; return 1; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1); ans = 0, ansx = n, ansy = m; for (int i = n; i >= 1; i--) if ((n % i) == 0) { for (int j = 1; j <= m; j++) if ((m % j) == 0) ans += solve(i, j); } printf("%d\n%d %d\n", ans, ansx, ansy); }
8
CPP
n,k=map(int,input().split()) print(((n//k)+1)*k)
7
PYTHON3
N,L=map(int,input().split()) a=list(map(int,input().split())) x=-1 for i in range(0,N-1): if a[i]+a[i+1]>=L: x=i break if x==-1: print("Impossible") else: print("Possible") for i in range(N-1, x+1,-1): print(i) for i in range(0,x+1): print(i+1)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; char S[2]; int n, m, x, y, z, k, w; int add, minu; int A[105], ANS[105]; int main() { add = 1; A[0] = 1; while (1) { scanf("%s", S); if (S[0] == '?') n++; if (S[0] == '-') minu++; else if (S[0] == '+') add++; if (S[0] == '-') A[++w] = 0; else if (S[0] == '+') A[++w] = 1; if (S[0] == '=') break; } scanf("%d", &x); if (add == 1 && minu >= 1) { printf("Impossible\n"); return 0; } else if (add == 1 && minu == 0) { printf("Possible\n%d = %d\n", x, x); return 0; } ANS[0] = x; for (int i = (1); i <= (w); i++) if (A[i] == 0) ANS[i] = -1; else ANS[i] = 1; y = 0; for (int i = (0); i <= (w); i++) y += ANS[i]; if (y > x) { for (int i = (0); i <= (w); i++) { while (ANS[i] > 1 && y > x) { ANS[i]--; y--; } while (ANS[i] <= -1 && ANS[i] > -x && y > x) { ANS[i]--; y--; } } } else { for (int i = (0); i <= (w); i++) { while (ANS[i] >= 1 && ANS[i] < x && y < x) { ANS[i]++; y++; } } } if (y != x) { printf("Impossible\n"); return 0; } printf("Possible\n"); for (int i = (0); i <= (w); i++) { printf("%d", abs(ANS[i])); if (i == w) break; if (A[i + 1] == 0) printf(" - "); else printf(" + "); } printf(" = %d\n", x); return 0; }
7
CPP
f=lambda:map(int,input().split()) n,k=f() l=list(f()) print(sum(x>=max(1,l[k-1]) for x in l))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; bool chk1(char* s) { int len = strlen(s); if (len < 1 || len > 16) return false; for (int i = 0; i < len; i++) if (!isalnum(s[i]) && s[i] != '_') return false; return true; } bool chk2(char* s) { int len = strlen(s), cnt = 0; bool ctag = true; if (len < 1 || len > 32) return false; for (int i = 0; i <= len; i++) { if (!isalnum(s[i]) && s[i] != '_' && s[i] != '.' && s[i] != '\0') return false; if (s[i] == '.' || s[i] == '\0') ctag = false; else cnt++; if (cnt < 1 || cnt > 16) return false; if (!ctag) { cnt = 0; ctag = true; } } return true; } int main() { char s[100], s1[100], s2[100], s3[100]; while (scanf("%s", s) != EOF) { int len = strlen(s), i = 0; while (i < len && s[i] != '@') i++; if (i == len) { printf("NO\n"); continue; } strncpy(s1, s, i); s1[i] = '\0'; if (!chk1(s1)) { printf("NO\n"); continue; } int j = len - 1; while (i < j && s[j] != '/') j--; if (j == i) { strncpy(s2, s + i + 1, len - i - 1); s2[len - i - 1] = '\0'; if (!chk2(s2)) { printf("NO\n"); continue; } } else { strncpy(s2, s + i + 1, j - i - 1); s2[j - i - 1] = '\0'; strncpy(s3, s + j + 1, len - j - 1); s3[len - j - 1] = '\0'; if (!chk2(s2)) { printf("NO\n"); continue; } if (!chk1(s3)) { printf("NO\n"); continue; } } printf("YES\n"); } return 0; }
7
CPP
i=list(map(int,input().split())) i=sorted(i) if(int(i[0])+int(i[3])==int(i[1])+int(i[2])): print("YES") elif(int(i[0])+int(i[1])+int(i[2])==int(i[3])): print("YES") else: print("NO")
7
PYTHON3
n=int(input()) s=input() c1=0 for i in range (len(s)-1): if s[i]==s[i+1]: c1+=1 i+=1 print(c1)
7
PYTHON3
#include <iostream> using namespace std; int main(){ int i = 1; int x; while (cin >> x && x != 0) { cout << "Case " << i << ": " << x << endl; i++; } }
0
CPP
p = input() l = len(p) s = int(p, 2) r = int(input() , 2) #print(int(input(), 2)) print(("{0:b}".format(s^r)).zfill(l))
7
PYTHON3
# coding=utf-8 __author__ = 'Alexander' n = input() n = int(n) l = [] r = [] L0 = 0 R0 = 0 for i in range(n): s = input() lt, rt = map(int, s.split()) l.append(lt) r.append(rt) L0 += lt R0 += rt B0 = abs(L0-R0) # print(L0, R0, B0) BN = B0 N = 0 for i in range(n): L = L0 - l[i] + r[i] R = R0 - r[i] + l[i] B = abs(L-R) if B > BN: BN = B N = i+1 # print(L, R, B) print(N)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; double esp = 1e-9; const double PI = acos(-1.0); const int INF = 0x3f3f3f3f; const int MOD = 1000000007; int dx[4] = {1, 1, -1, -1}; int dy[4] = {1, -1, 1, -1}; int s[110010]; int q[110010]; int main() { int n, t; memset(s, 0, sizeof(s)); memset(q, 0, sizeof(q)); scanf("%d", &n); for (int x = 1; x <= n; x++) { scanf("%d", &s[x]); q[s[x]]++; } int flag = 0; for (int x = 2; x <= 100000; x++) if (q[x] > q[x - 1]) { flag = 1; break; } int ans = q[1]; memset(q, 0, sizeof(q)); if (flag) printf("%d\n", -1); else { printf("%d\n", ans); for (int x = 1; x <= n; x++) printf("%d ", ++q[s[x]]); } return 0; }
10
CPP
line=map(int,input().split()) line=set(line) print(4-len(line))
7
PYTHON3
import os import sys from io import BytesIO, IOBase # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline() # ------------------------------ def RL(): return map(int, sys.stdin.readline().split()) def RLL(): return list(map(int, sys.stdin.readline().split())) def N(): return int(input()) def print_list(l): print(' '.join(map(str,l))) class SortedList: def __init__(self, iterable=[], _load=200): """Initialize sorted list instance.""" values = sorted(iterable) self._len = _len = len(values) self._load = _load self._lists = _lists = [values[i:i + _load] for i in range(0, _len, _load)] self._list_lens = [len(_list) for _list in _lists] self._mins = [_list[0] for _list in _lists] self._fen_tree = [] self._rebuild = True def _fen_build(self): """Build a fenwick tree instance.""" self._fen_tree[:] = self._list_lens _fen_tree = self._fen_tree for i in range(len(_fen_tree)): if i | i + 1 < len(_fen_tree): _fen_tree[i | i + 1] += _fen_tree[i] self._rebuild = False def _fen_update(self, index, value): """Update `fen_tree[index] += value`.""" if not self._rebuild: _fen_tree = self._fen_tree while index < len(_fen_tree): _fen_tree[index] += value index |= index + 1 def _fen_query(self, end): """Return `sum(_fen_tree[:end])`.""" if self._rebuild: self._fen_build() _fen_tree = self._fen_tree x = 0 while end: x += _fen_tree[end - 1] end &= end - 1 return x def _fen_findkth(self, k): """Return a pair of (the largest `idx` such that `sum(_fen_tree[:idx]) <= k`, `k - sum(_fen_tree[:idx])`).""" _list_lens = self._list_lens if k < _list_lens[0]: return 0, k if k >= self._len - _list_lens[-1]: return len(_list_lens) - 1, k + _list_lens[-1] - self._len if self._rebuild: self._fen_build() _fen_tree = self._fen_tree idx = -1 for d in reversed(range(len(_fen_tree).bit_length())): right_idx = idx + (1 << d) if right_idx < len(_fen_tree) and k >= _fen_tree[right_idx]: idx = right_idx k -= _fen_tree[idx] return idx + 1, k def _delete(self, pos, idx): """Delete value at the given `(pos, idx)`.""" _lists = self._lists _mins = self._mins _list_lens = self._list_lens self._len -= 1 self._fen_update(pos, -1) del _lists[pos][idx] _list_lens[pos] -= 1 if _list_lens[pos]: _mins[pos] = _lists[pos][0] else: del _lists[pos] del _list_lens[pos] del _mins[pos] self._rebuild = True def _loc_left(self, value): """Return an index pair that corresponds to the first position of `value` in the sorted list.""" if not self._len: return 0, 0 _lists = self._lists _mins = self._mins lo, pos = -1, len(_lists) - 1 while lo + 1 < pos: mi = (lo + pos) >> 1 if value <= _mins[mi]: pos = mi else: lo = mi if pos and value <= _lists[pos - 1][-1]: pos -= 1 _list = _lists[pos] lo, idx = -1, len(_list) while lo + 1 < idx: mi = (lo + idx) >> 1 if value <= _list[mi]: idx = mi else: lo = mi return pos, idx def _loc_right(self, value): """Return an index pair that corresponds to the last position of `value` in the sorted list.""" if not self._len: return 0, 0 _lists = self._lists _mins = self._mins pos, hi = 0, len(_lists) while pos + 1 < hi: mi = (pos + hi) >> 1 if value < _mins[mi]: hi = mi else: pos = mi _list = _lists[pos] lo, idx = -1, len(_list) while lo + 1 < idx: mi = (lo + idx) >> 1 if value < _list[mi]: idx = mi else: lo = mi return pos, idx def add(self, value): """Add `value` to sorted list.""" _load = self._load _lists = self._lists _mins = self._mins _list_lens = self._list_lens self._len += 1 if _lists: pos, idx = self._loc_right(value) self._fen_update(pos, 1) _list = _lists[pos] _list.insert(idx, value) _list_lens[pos] += 1 _mins[pos] = _list[0] if _load + _load < len(_list): _lists.insert(pos + 1, _list[_load:]) _list_lens.insert(pos + 1, len(_list) - _load) _mins.insert(pos + 1, _list[_load]) _list_lens[pos] = _load del _list[_load:] self._rebuild = True else: _lists.append([value]) _mins.append(value) _list_lens.append(1) self._rebuild = True def discard(self, value): """Remove `value` from sorted list if it is a member.""" _lists = self._lists if _lists: pos, idx = self._loc_right(value) if idx and _lists[pos][idx - 1] == value: self._delete(pos, idx - 1) def remove(self, value): """Remove `value` from sorted list; `value` must be a member.""" _len = self._len self.discard(value) if _len == self._len: raise ValueError('{0!r} not in list'.format(value)) def pop(self, index=-1): """Remove and return value at `index` in sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) value = self._lists[pos][idx] self._delete(pos, idx) return value def bisect_left(self, value): """Return the first index to insert `value` in the sorted list.""" pos, idx = self._loc_left(value) return self._fen_query(pos) + idx def bisect_right(self, value): """Return the last index to insert `value` in the sorted list.""" pos, idx = self._loc_right(value) return self._fen_query(pos) + idx def count(self, value): """Return number of occurrences of `value` in the sorted list.""" return self.bisect_right(value) - self.bisect_left(value) def __len__(self): """Return the size of the sorted list.""" return self._len def __getitem__(self, index): """Lookup value at `index` in sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) return self._lists[pos][idx] def __delitem__(self, index): """Remove value at `index` from sorted list.""" pos, idx = self._fen_findkth(self._len + index if index < 0 else index) self._delete(pos, idx) def __contains__(self, value): """Return true if `value` is an element of the sorted list.""" _lists = self._lists if _lists: pos, idx = self._loc_left(value) return idx < len(_lists[pos]) and _lists[pos][idx] == value return False def __iter__(self): """Return an iterator over the sorted list.""" return (value for _list in self._lists for value in _list) def __reversed__(self): """Return a reverse iterator over the sorted list.""" return (value for _list in reversed(self._lists) for value in reversed(_list)) def __repr__(self): """Return string representation of sorted list.""" return 'SortedList({0})'.format(list(self)) # import heapq as hq # import bisect as bs # from collections import deque as dq # from collections import defaultdict as dc # from math import ceil,floor,sqrt # from collections import Counter spell = SortedList([0]) lightning = SortedList() s,k = 0,-1 for _ in range(N()): t,power = RL() s+=power if t==1: if lightning: spell.add(lightning[0]) if lightning[0]>spell[k]: s+=lightning[0]-spell[k] if power>0: lightning.add(power) spell.add(power) if power>spell[k]: s+=power-spell[k] else: lightning.discard(-power) if -power>spell[k]: s-=-power-spell[k] spell.discard(-power) if lightning: if lightning[0]>spell[k]: s-=lightning[0]-spell[k] spell.discard(lightning[0]) else: if power>0: spell.add(power) if power>spell[k]: s+=power-spell[k] else: if -power>spell[k]: s-=-power-spell[k] spell.discard(-power) k = max(k,-len(spell)) if -k>len(lightning)+1: k+=1 s-=spell[k] elif -k<len(lightning)+1 and spell[k]>0: s+=spell[k] k-=1 # print(spell,lightning,k,s) print(s)
11
PYTHON3
b=list(map(int,input().split())) m=b[0] a=0 for i in range (3): if b[i]>m: m=b[i] a=i ass=0 for i in range(3): if a!=i: ass=ass+b[i] if ass>m: print("0") else: print(m-ass+1)
7
PYTHON3
#include <iostream> #include <string> using namespace std; int main(void){ string s; int n; cin>>s; n=s.length(); for(int i=0;i<8;i++){ string t=s.substr(0,i)+s.substr(n-7+i,7-i); //cout<<t<<endl; if(t=="keyence"){ cout<<"YES"<<endl; return 0; } } cout<<"NO"<<endl; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; bool prime(int n) { if (n < 2) return 0; if (n == 2) return 1; if (n % 2 == 0) return 0; for (int i = 3; i * i <= n; i += 2) if (n % i == 0) return 0; return 1; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; vector<int> res; int tmp = n; while (!prime(tmp)) tmp--; res.push_back(tmp); n -= tmp; if (prime(n)) { res.push_back(n); n = 0; } for (int i = 0; i <= n; ++i) { if (prime(i) && prime(n - i)) { res.push_back(i); res.push_back(n - i); break; } } cout << res.size() << '\n'; for (int i = 0; i < res.size(); ++i) cout << res[i] << " "; cout << '\n'; return 0; }
10
CPP
def distribute(numbers): sum = 0 i=0 free = [0]*len(numbers) for x in numbers: sum = sum + int(x) singleSum = sum*2/len(numbers) for x in range(0,len(numbers)-1): if free[x]==1: continue for y in range(x+1,len(numbers)): if free[y]==0 and (int(numbers[x])+int(numbers[y])==singleSum): print(str(x+1)+' '+str(y+1)) free[x]=1 free[y]=1 break n = input() numbers = input().split() distribute(numbers)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int P = 512; int m, n, friends[P], ans1, ans2, max_count, min_price = INT_MAX; set<pair<int, int>> pizza[P], all_pizza; bool new_solution(int count, int price) { if (count > max_count) return true; return count == max_count and price < min_price; } int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr), cin >> n >> m; for (int _ = 0; _ < n; ++_) { int f, x, y = 0; cin >> f; for (int _ = 0; _ < f; ++_) cin >> x, y |= (1 << (x - 1)); ++friends[y]; } for (int i = 0; i < m; ++i) { int c, r, x, y = 0; cin >> c >> r; for (int _ = 0; _ < r; ++_) cin >> x, y |= (1 << (x - 1)); pizza[y].emplace(c, i + 1), all_pizza.emplace(c, i + 1); } for (int i = 1; i < P; ++i) if (not pizza[i].empty()) for (int p = pizza[i].size(), j = i; j < P; ++j) if (not pizza[j].empty() and (i != j or p > 1)) { int price, count = 0; for (int k = i | j, y = 1; y < P; ++y) if (friends[y] and (k & y) == y) count += friends[y]; if (count == 0) continue; auto it = pizza[i].cbegin(), iu = pizza[j].cbegin(); if (i == j) ++iu; if (new_solution(count, price = it->first + iu->first)) max_count = count, min_price = price, ans1 = it->second, ans2 = iu->second; } if (min_price == INT_MAX) { auto it = all_pizza.cbegin(), iu = it; ++iu, ans1 = it->second, ans2 = iu->second; } cout << ans1 << ' ' << ans2; }
12
CPP
n=int(input()) coin=[int(x)for x in input().split()] coin.sort(reverse=True) ave=sum(coin)/2 m=0 for i in range(n): m+=coin[i] if m>ave: print(i+1) break
7
PYTHON3
############################################## x = input() l = [] for i in x: if i.isupper(): l += [i.lower()] else: l += [i.upper()] if x.isupper() or (x[0].islower() and (x[1:].isupper() or len(x)==1)): print(''.join(l)) else: print(x)
7
PYTHON3
#include<bits/stdc++.h> #define MOD 1000000007 using namespace std; long long solve(vector<long long> neg, vector<long long> pos, int k) { long long ans = 1; if(k&1) { if(pos.size()>0) ans *= pos.back(),pos.pop_back(),--k; else return -1; } vector<long long> v; for(int i=pos.size()-1;i>=1;i-=2) v.push_back(pos[i]*pos[i-1]); for(int i=0;i+1<neg.size();i+=2) v.push_back(neg[i]*neg[i+1]); if(v.size()<k/2) return -1; sort(v.rbegin(),v.rend()); for(int i=0;i<k/2;++i) ans *= v[i]%MOD, ans %= MOD; return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n,k; cin>>n>>k; long long x; vector<long long> neg,pos,zero; while(n--) { cin>>x; if(x<0) neg.push_back(abs(x)); else if(x>0) pos.push_back(x); else zero.push_back(x); } sort(pos.begin(),pos.end()); sort(neg.rbegin(),neg.rend()); long long ans = solve(neg,pos,k); if(ans<0) { if(zero.size()>0) cout<<"0"<<endl; else { for(long long y:neg) pos.push_back(y); sort(pos.begin(),pos.end()); ans = 1; while(k--) ans *= pos[k], ans %= MOD; cout<<MOD-ans<<endl; } } else cout<<ans<<endl; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; string check(const string &s, long long int x) { string z; for (long long int i = 0; i < s.length(); i++) { bool flag1 = false, flag2 = false; if (i - x >= 0 && s[i - x] == '1') { flag1 = true; } if (i + x <= s.length() - 1 && s[i + x] == '1') { flag2 = true; } if (flag1 || flag2) { z += '1'; } else { z += '0'; } } return z; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t; cin >> t; while (t--) { string s; long long int x; cin >> s >> x; char arr[s.length()]; for (long long int i = 0; i < s.length(); i++) { arr[i] = '@'; } bool flag = false; for (long long int i = 0; i < s.length(); i++) { if (s[i] == '0') { if (i + x < s.length()) { if (arr[i + x] == '@' || arr[i + x] == '0') { arr[i + x] = '0'; } else { flag = true; break; } } if (i - x >= 0) { if (arr[i - x] == '@' || arr[i - x] == '0') { arr[i - x] = '0'; } else { flag = true; break; } } } } for (long long int i = s.length() - 1; i >= 0; i--) { if (s[i] == '0') { if (i + x < s.length()) { if (arr[i + x] == '@' || arr[i + x] == '0') { arr[i + x] = '0'; } else { flag = true; break; } } if (i - x >= 0) { if (arr[i - x] == '@' || arr[i - x] == '0') { arr[i - x] = '0'; } else { flag = true; break; } } } } if (flag) { cout << -1; continue; } for (long long int i = 0; i < s.length(); i++) { if (arr[i] == '@') { arr[i] = '1'; } } string z; for (long long int i = 0; i < s.length(); i++) { z += arr[i]; } if (check(z, x) == s) { cout << z << '\n'; } else { cout << -1 << '\n'; } } return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; long long t, d, ct, q, ans, l, m, r, n, mx, mn, i, j, k, x, y, z, s; char ch; string ss = "", str = ""; long long a[200005], b[200005]; vector<long long> g[200005]; bool vis[200005]; long long c[200005]; void dfs(long long node, long long col) { vis[node] = 1; c[node] = col; for (auto h : g[node]) { if (vis[h]) { if (c[h] == col) { cout << "NO"; exit(0); } continue; } dfs(h, col ^ 1); } } int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); s = k = d = l = mx = m = mn = r = q = i = j = z = x = y = ct = ans = 0; cin >> n >> ss; for (i = 0; i < n; i++) for (j = i + 1; j < n; j++) if (ss[i] > ss[j]) g[i].push_back(j), g[j].push_back(i); for (i = 0; i < n; i++) if (!vis[i]) dfs(i, 0); cout << "YES\n"; for (i = 0; i < n; i++) cout << c[i]; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 200200; string s; long long n; long long a[26][N]; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> s; n = s.size(); for (long long i = 0; i < n; i++) { a[s[i] - 'a'][i + 1]++; } for (long long i = 0; i < 26; i++) { for (long long j = 1; j < N; j++) { a[i][j] += a[i][j - 1]; } } long long q; cin >> q; while (q--) { long long l, r; cin >> l >> r; if (r - l + 1 == 1 || s[l - 1] != s[r - 1]) { cout << "Yes" << "\n"; } else { long long cnt = 0; for (long long i = 0; i < 26; i++) { cnt += (a[i][r] - a[i][l - 1] > 0); } cout << (cnt > 2 ? "Yes" : "No") << "\n"; } } return 0; }
8
CPP
n, m = input().split() n = int(n) m = int(m) lst = [] lst2 = [] for i in range(n): a, b = input().split() tup = (a, b) lst.append(tup) for k in range(m): c, d = input().split() tup = (c, d) lst2.append(tup) for h in range(0,m,1): for j in range(0,n,1): if lst[j][1] == lst2[h][1][:-1]: print(lst2[h][0] + " " + lst2[h][1] + " #" + lst[j][0])
8
PYTHON3
n, k = map(int, input().split()) a = 0 b = 0 c = 0 while n % 5 == 0 and b < k: n /= 5 b += 1 while n % 2 == 0 and c < k: n /= 2 c += 1 print(int(n*10**k))
7
PYTHON3
#include <iostream> #include <algorithm> using namespace std; int main(){ int n; while(cin >> n, n){ int w, h, x, y, s, t; cin >> w >> h; bool a[101][101] = {false}; for(int i = 0; i < n; i++){ cin >> x >> y; a[y-1][x-1] = true; } cin >> s >> t; int sum = 0; for(int i = 0; i < h-t+1; i++){ for(int j = 0; j < w-s+1; j++){ int tmp = 0; for(int l = 0; l < t; l++){ for(int k = 0; k < s; k++){ if(a[i+l][j+k]) tmp++; } } sum = max(tmp, sum); } } cout << sum << endl; } return 0; }
0
CPP
from sys import stdin input = lambda: stdin.readline().rstrip("\r\n") from collections import deque as que, defaultdict as vector from heapq import* inin = lambda: int(input()) inar = lambda: list(map(int,input().split())) Testcase=inin() for i in range(Testcase): n=inin() a=inar() a.reverse() st='' if n==1: print(0) continue if a[0]>a[1]: st='d' else: st='i' for i in range(1,n): #print(st) if a[i]>a[i-1] and st=='d': print(n-i) break st='i' if a[i]<a[i-1] and st=='i': st='d' else: print(0)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long n, k, t; cin >> n >> k; set<long long> a; for (long long cnt = 1; cnt <= min((long long)1000000, k); cnt++) { t = n % cnt; if (a.find(t) != a.end()) { cout << "No"; return 0; } a.insert(t); } cout << "Yes"; return 0; }
9
CPP
import sys from functools import lru_cache, cmp_to_key from heapq import merge, heapify, heappop, heappush from math import * from collections import defaultdict as dd, deque, Counter as C from itertools import combinations as comb, permutations as perm from bisect import bisect_left as bl, bisect_right as br, bisect, insort from time import perf_counter from fractions import Fraction import copy from copy import deepcopy import time starttime = time.time() mod = int(pow(10, 9) + 7) mod2 = 998244353 def data(): return sys.stdin.readline().strip() def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end) def L(): return list(sp()) def sl(): return list(ssp()) def sp(): return map(int, data().split()) def ssp(): return map(str, data().split()) def l1d(n, val=0): return [val for i in range(n)] def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)] try: # sys.setrecursionlimit(int(pow(10,6))) sys.stdin = open("input.txt", "r") # sys.stdout = open("../output.txt", "w") except: pass def pmat(A): for ele in A: print(*ele,end="\n") l, xyz, res = [(0, 0, 0)], [0, 0, 0], [] for c in input(): xyz[ord(c) - 120] += 1 l.append(tuple(xyz)) for _ in range(int(input())): a, b = map(int, input().split()) if b - a > 1: xyz = [i - j for i, j in zip(l[b], l[a - 1])] res.append(("NO", "YES")[max(xyz) - min(xyz) < 2]) else: res.append("YES") print('\n'.join(res)) endtime = time.time() # print(f"Runtime of the program is {endtime - starttime}")
7
PYTHON3
for i in range(int(input())): x,y=map(int,input().split()) num=list(map(int,input().split())) s=0 for j in num: if j%2==1: s+=1 if s==0 or (x==y and s%2==0) or (s==x and y%2==0): print("No") else: print("Yes")
7
PYTHON3
import sys class Scanner: def __init__(self): self.current_tokens = [] def remaining_tokens(self): return len(self.current_tokens) def nextline(self): assert self.remaining_tokens() == 0, "Reading next line with remaining tokens" return input() def nexttokens(self): return self.nextline().split() def nexttoken(self): if len(self.current_tokens) == 0: self.current_tokens = self.nexttokens() assert self.remaining_tokens() > 0, "Not enough tokens to parse." return self.current_tokens.pop(0) def nextints(self, n=-1): if n == -1: return list(map(int, self.nexttokens())) else: return [self.nextint() for i in range(n)] def nextint(self): return int(self.nexttoken()) def quit(): sys.exit(0) stdin = Scanner() nextint = stdin.nextint nextints = stdin.nextints nextline = stdin.nextline T = nextint() for i in range(T): n = nextint() s = n*(n+1)//2 p = 1 while p <= n: s -= p * 2 p = p * 2 print(int(s))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int Maxn = 200005; int n, s, ct, tmp_n, ans_ct, a[Maxn], b[Maxn], fa[Maxn], pos[Maxn], ord[Maxn], bel[Maxn], Pos[Maxn]; vector<int> spec, Ve[Maxn]; bool vis[Maxn]; void dfs(int u) { if (vis[u]) return; bel[u] = ct, vis[u] = true, dfs(ord[u]); } void dfs2(int u) { if (vis[u]) return; Ve[ans_ct].push_back(u), vis[u] = true, dfs2(pos[u]); } int get_fa(int x) { return x == fa[x] ? x : fa[x] = get_fa(fa[x]); } int main() { scanf("%d%d", &n, &s); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i]; sort(b + 1, b + 1 + n); for (int i = 1; i <= n; i++) if (a[i] != b[i]) a[++tmp_n] = a[i], Pos[tmp_n] = i; n = tmp_n; if (s < n) { puts("-1"); return 0; } s -= n; for (int i = 1; i <= n; i++) ord[i] = i; sort(ord + 1, ord + 1 + n, [](int x, int y) { return a[x] < a[y]; }); for (int i = 1; i <= n; i++) pos[ord[i]] = i; a[n + 1] = -1; for (int i = 1; i <= n; i++) if (!vis[i]) ct++, fa[ct] = ct, dfs(i); int las = 1; for (int i = 2; i <= n + 1; i++) if (a[ord[i]] != a[ord[i - 1]]) { for (int j = las; j < i; j++) if (get_fa(bel[ord[las]]) != get_fa(bel[ord[j]])) fa[get_fa(bel[ord[j]])] = get_fa(bel[ord[las]]), swap(ord[j], ord[las]); las = i; } memset(vis, 0, sizeof(bool[n + 1])); ct = 0; for (int i = 1; i <= n; i++) pos[ord[i]] = i; for (int i = 1; i <= n; i++) if (!vis[i]) { ct++; if (ct == 1 || ct > s) ++ans_ct; if (ct <= s) spec.push_back(i); dfs2(i); } printf("%d\n", ans_ct + (spec.size() > 1)); if (ans_ct) { printf("%d\n", (int)Ve[1].size()); for (vector<int>::iterator it = Ve[1].begin(); it != Ve[1].end(); it++) printf("%d ", Pos[*it]); puts(""); } if (spec.size() > 1) { printf("%d\n", (int)spec.size()); for (vector<int>::reverse_iterator it = spec.rbegin(); it != spec.rend(); it++) printf("%d ", Pos[*it]); puts(""); } for (int i = 2; i <= ans_ct; i++) { printf("%d\n", (int)Ve[i].size()); for (vector<int>::iterator it = Ve[i].begin(); it != Ve[i].end(); it++) printf("%d ", Pos[*it]); puts(""); } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); long long int n, k, a, b, i; char gg = 'G', bb = 'B'; cin >> n >> k >> a >> b; if (a > b) { int t = a; a = b; b = t; char tt = gg; gg = bb; bb = tt; } if (max(a, b) > (min(a, b) + 1) * k) { cout << "NO\n"; return 0; } if (a == b) { for (i = 0; i < n; i++) { if (i & 1) cout << gg; else cout << bb; } } else { int x = b / (a + 1), y = b % (a + 1); while (1) { for (i = 0; i < x; i++) { cout << bb; b--; } if (y) { cout << bb; y--; b--; } if (a) { cout << gg; a--; } if (a == 0 && b == 0) break; } } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n; cin >> n; long long a[n + 1]; for (long long i = 1; i <= n; i++) cin >> a[i]; long long dp[n + 1]; for (long long i = 0; i < n + 1; i++) dp[i] = 1; for (long long i = 1; i <= n / 2; i++) { for (long long j = 2 * i; j <= n; j += i) { if (a[j] > a[i]) dp[j] = max(dp[j], dp[i] + 1); } } long long ans = 0; for (long long i = 1; i <= n; i++) { ans = max(ans, dp[i]); } cout << ans << endl; } }
8
CPP
n, x, y = (int(d) for d in input().split()) a = [int(d) for d in input().split()] if x - y <= 0: res = ([a[i] <= x for i in range(n)].count(True) + 1) // 2 else: res = n print(res)
9
PYTHON3
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; #define REP(i,n) for(int i = 0;i < n;i++) #define INF 100000000 typedef long long ll; ll d[110][110]; int V; int N,M,L,K,A,H; bool cold[110]; void WF() { REP(k,N)REP(i,N)REP(j,N) d[i][j] = min(d[i][j] , d[i][k] + d[k][j]); } int main() { while(cin >> N >> M >> L >> K >> A >> H, N) { REP(i,110)REP(j,110)d[i][j] = INF; REP(i,110)cold[i] = false; cold[A] = cold[H] = true; int tmp; REP(i,L) {cin >> tmp; cold[tmp] = true;} REP(i,K) { int a,b,l; cin >> a >> b >> l; d[a][b] = d [b][a] = l; } WF(); REP(i,N)REP(j,N) { if(!(cold[i] & cold[j]) || d[i][j] > M) { d[i][j] = INF; } } WF(); if(d[A][H] >= INF) { cout << "Help!" << endl; } else { if(d[A][H] > M) { cout << d[A][H]*2 - M << endl; } else { cout << d[A][H] << endl; } } } return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int w[200005]; vector<int> grafo[200005]; int visited[200000]; pair<int, int> dfs(int v) { if (visited[v] == 2) return {0, w[v]}; visited[v] = 1; if (grafo[v].size() == 0) { visited[v] = 2; return {-1, w[v]}; } int u = grafo[v][0]; if (visited[u] == 2) { visited[v] = 2; return {0, w[u]}; } if (visited[u] and u != v) return {1, u}; dfs(u); } int dfs2(int v, int pai, int peso) { int u = grafo[v][0]; if (visited[u] == 2) return 0; if (u == pai) return peso; dfs2(u, pai, min(peso, w[u])); } int dfs3(int v, int pai, int peso) { visited[v] = 2; w[v] = peso; int u = grafo[v][0]; if (u == pai) return peso; dfs2(u, pai, peso); } int dfs4(int v, int peso) { visited[v] = 2; w[v] = peso; if (grafo[v].size() == 0) return 0; int u = grafo[v][0]; dfs4(u, peso); } int main() { int n, ans = 0; cin >> n; for (int i = 0; i < n; i++) scanf("%d", &w[i + 1]); for (int x = 1; x <= n; x++) { int y; scanf("%d", &y); if (x == y) continue; grafo[x].push_back(y); } for (int i = 1; i <= n; i++) { if (visited[i] != 0) continue; pair<int, int> aux = dfs(i); if (aux.first == 0) continue; if (aux.first == 1) { int peso = dfs2(aux.second, aux.second, w[aux.second]); dfs3(aux.second, aux.second, peso); ans += peso; } else { dfs4(i, aux.second); ans += aux.second; } } cout << ans << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; void readint(int &v) { char c; while ((c = getchar()) < '0' || c > '9') ; v = c & 15; while ((c = getchar()) >= '0' && c <= '9') v = v * 10 + (c & 15); } const int MaxN = 100100; pair<int, int> g[MaxN * 2]; int h[MaxN]; inline void Add(int u, int v) { g[++h[0]] = make_pair(v, h[u]); h[u] = h[0]; } pair<int, int> dfn[MaxN]; int num[MaxN]; void dfs(int u, int fa) { static int cur; dfn[u].first = ++cur; num[cur] = u; for (int i = h[u]; i; i = g[i].second) if (g[i].first != fa) dfs(g[i].first, u); dfn[u].second = cur; } int c[MaxN]; pair<pair<int, int>, pair<int, int> > q[MaxN]; int siz; class cmp { public: inline bool operator()( const pair<pair<int, int>, pair<int, int> > &a1, const pair<pair<int, int>, pair<int, int> > &a2) const { if (a1.first.first / siz != a2.first.first / siz) return a1.first.first < a2.first.first; if (a1.first.first / siz & 1) return a1.first.second < a2.first.second; return a1.first.second > a2.first.second; } }; int col_cnt[MaxN]; int nod_cnt[MaxN]; int ans[MaxN]; inline void upda(int x, int flg) { if (flg == 1) ++nod_cnt[c[num[x]]]; col_cnt[nod_cnt[c[num[x]]]] += flg; if (flg == -1) --nod_cnt[c[num[x]]]; } int main() { int n, m; readint(n); readint(m); for_each(c + 1, c + n + 1, readint); for (int i = 1; i < n; ++i) { int a, b; readint(a); readint(b); Add(a, b); Add(b, a); } dfs(1, 0); for (int i = 1; i <= m; ++i) { int v, k; readint(v); readint(k); q[i] = make_pair(dfn[v], make_pair(k, i)); } siz = sqrt(n); sort(q + 1, q + m + 1, cmp()); int l = 1, r = 0; for (int i = 1; i <= m; ++i) { while (l < q[i].first.first) upda(l++, -1); while (l > q[i].first.first) upda(--l, 1); while (r < q[i].first.second) upda(++r, 1); while (r > q[i].first.second) upda(r--, -1); ans[q[i].second.second] = col_cnt[q[i].second.first]; } for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; long long m, n, k, t; struct trip { int l, r, d; }; vector<int> a; vector<trip> trips; bool comp(trip first, trip second) { if (first.l == second.l) { return first.r > second.r; } return first.l < second.l; } bool check(int kolvo) { int mx = 0; vector<pair<int, int>> v; for (auto u : trips) { if (u.d > a[kolvo - 1]) { v.push_back({u.l, u.r}); } } vector<pair<int, int>> last; for (int i = 0; i < v.size(); i++) { if (mx < v[i].second) { mx = v[i].second; last.push_back(v[i]); } } long long time = n + 1; if (last.empty()) { return 1; } int l = last[0].first; int r = last[0].second; for (int i = 1; i < last.size(); i++) { if (r < last[i].first) { time += (r - l + 1) * 2; l = last[i].first; r = last[i].second; } else { r = last[i].second; } } time += (r - l + 1) * 2; return (time <= t); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); ; cin >> m >> n >> k >> t; a.resize(m); for (int i = 0; i < m; i++) { cin >> a[i]; } sort(a.begin(), a.end()); reverse(a.begin(), a.end()); trips.resize(k); for (int i = 0; i < k; i++) { cin >> trips[i].l >> trips[i].r >> trips[i].d; } sort(trips.begin(), trips.end(), comp); int l = 0; int r = m + 1; while (r > l + 1) { int mid = (r + l) / 2; if (check(mid)) { l = mid; } else { r = mid; } } cout << l << endl; }
10
CPP
#include <bits/stdc++.h> const int inf = 0x3f3f3f3f; const int mod = (int)1e9 + 7; const int maxn = (int)2e5 + 5; const long long INF = 0x3f3f3f3f3f3f3f3f; using namespace std; int a[maxn]; vector<int> v, v2; int main() { int n, k; scanf("%d %d", &n, &k); for (__typeof(n) i = 1; i <= (n); i++) { int x; scanf("%d", &x); a[i] = x; if (x < 0) v.push_back(i); } int len = v.size(); if (!len) return 0 * puts("0"); if (len > k) return 0 * puts("-1"); int res = k - len; for (__typeof(len - 2) i = 0; i <= (len - 2); i++) { v2.push_back(v[i + 1] - v[i] - 1); } int ans = 0; for (__typeof(n) i = 1; i <= (n); i++) { if ((a[i] < 0 && a[i - 1] >= 0) || (a[i] >= 0 && a[i - 1] < 0)) ans++; } int t = inf; if (a[n] >= 0) t = (n - v[len - 1]); sort(v2.begin(), v2.end()); len = v2.size(); for (__typeof(len - 1) i = 0; i <= (len - 1); i++) { if (!v2[i]) continue; if (res >= v2[i]) res -= v2[i], ans -= 2; else break; } if (res >= t) ans--; printf("%d\n", ans); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { string s, t; cin >> s >> t; int left = -1, right = -1; int i = 0, j = 0; while (i < s.size() && j < t.size()) { if (s[i] == t[j]) i++; j++; } if (i == s.size()) left = j - 1; else { cout << 0 << endl; return 0; } i = s.size() - 1; j = t.size() - 1; while (i >= 0 && j >= 0) { if (s[i] == t[j]) i--; j--; } if (i == -1) right = j + 1; if (left < right) { cout << right - left << endl; } else cout << 0 << endl; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int n; vector<int> v(10000), ans; set<int> st; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> v[i]; } for (int i = n - 1; i >= 0; i--) { if (st.find(v[i]) == st.end()) { st.insert(v[i]); ans.push_back(v[i]); } } cout << ans.size() << endl; for (int i = ans.size() - 1; i >= 0; i--) { cout << ans[i] << " "; } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int N, M, K, F[2005]; int Get(int u) { if (F[u] == u) return u; return F[u] = Get(F[u]); } void Join(int u, int v) { int fx = Get(u), fy = Get(v); if (fx != fy) F[fx] = fy; } int main() { scanf("%d%d%d", &N, &M, &K); for (int i = 1; i <= N; i++) F[i] = i; for (int i = 1; i <= N - K + 1; i++) { int l = i, r = i + K - 1; for (int k = 1; k <= K / 2; k++) Join(l + k - 1, r - k + 1); } int Ans = 1; for (int i = 1; i <= N; i++) if (Get(i) == i) Ans = (1ll * Ans * M) % 1000000007; printf("%d\n", Ans); }
8
CPP
import io, os #input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline R = lambda : map(int, input().split()) for _ in range(int(input())) : n = int(input()) a = list(R()) a.sort() ans = 0 for i in range(1, a[-1] + a[-1] + 1) : cnt = 0 l, r = 0, n - 1 while r > l : if a[l] + a[r] == i : cnt += 1 l += 1 r -= 1 elif a[l] + a[r] < i : l += 1 else : r -= 1 ans = max(ans, cnt) print(ans)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; int factorial(int n); int factorial(int n) { if (n == 1) { return 1; } else { return n * factorial(n - 1); } } bool is_prime(long long n) { if (n == 1) { return false; } long long i = 2; while (i * i <= n) { if (n % i == 0) { return false; } i += 1; } return true; } int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } int check(int x, vector<int> a, int n) { int county = 0; map<int, int> mapa; for (int i = 0; i < n; i++) { mapa.insert(pair<int, int>(a[i], 0)); } for (int i = 0; i < n; i++) { mapa[a[i]]++; } for (int i = 0; i < a.size(); i++) { mapa[a[i]]--; if (mapa[abs(a[i] - x)] > 0) { county++; mapa[abs(a[i] - x)]--; } } return county; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t > 0) { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } if (a[0] <= 0 || a[n - 1] <= 0) { cout << "NO" << endl; } else { long long sum = 0; int i; for (i = 0; i < n - 1; i++) { sum = sum + a[i]; if (sum <= 0) { break; } } long long sum2 = 0; int j; for (j = n - 1; j > 0; j--) { sum2 = sum2 + a[j]; if (sum2 <= 0) { break; } } if (i == n - 1 && j == 0) { cout << "YES" << endl; } else { cout << "NO" << endl; } } t--; } }
8
CPP
#include <bits/stdc++.h> using namespace std; int ans[104][104]; int main(void) { int n, m; scanf("%d%d", &n, &m); int cur = n * m, i, j; if (min(n, m) == 1 && (max(n, m) == 2 || max(n, m) == 3)) { printf("-1\n"); return 0; } if (n == 2 && m == 2) { printf("-1\n"); return 0; } for (i = 0; i < n; i++) { if (i % 2) for (j = 0; j < m; j += 2) ans[i][j] = cur--; else for (j = 1; j < m; j += 2) ans[i][j] = cur--; } for (i = 0; i < n; i++) { if (i % 2) for (j = 1; j < m; j += 2) ans[i][j] = cur--; else for (j = 0; j < m; j += 2) ans[i][j] = cur--; } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) printf("%d ", ans[i][j]); printf("\n"); } return 0; }
16
CPP
t=input() s=input() a='' for i,l in zip(t,s): if i!=l: a+='1' else: a+='0' print(a)
7
PYTHON3
#include <bits/stdc++.h> inline void read(char &x) { do x = getchar(); while (x <= ' '); } inline void read(char *s) { char ch; do ch = getchar(); while (ch <= ' '); while (ch > ' ') { (*s) = ch; s++; ch = getchar(); } (*s) = 0; } inline void read(std::string &s) { char ch; do ch = getchar(); while (ch <= ' '); while (ch > ' ') { s.push_back(ch); ch = getchar(); } } template <class A> inline void readint(A &x) { bool neg = false; char ch; do ch = getchar(); while (ch <= ' '); if (ch == '-') { neg = true; ch = getchar(); } x = 0; while (ch > ' ') { x = x * 10 + (ch & 15); ch = getchar(); } if (neg) x = -x; } inline void read(int &x) { readint(x); } inline void read(long long &x) { readint(x); } template <class A, class B> inline void read(A &a, B &b) { read(a); read(b); } template <class A, class B, class C> inline void read(A &a, B &b, C &c) { read(a); read(b); read(c); } template <class A, class B, class C, class D> inline void read(A &a, B &b, C &c, D &d) { read(a); read(b); read(c); read(d); } inline void writeln() { putchar('\n'); } template <class A> inline void write(A x) { static char buf[20]; int top = 0; if (!x) { putchar('0'); return; } if (x < 0) { putchar('-'); x = -x; } while (x) { buf[++top] = (x % 10) | 48; x /= 10; } while (top) putchar(buf[top--]); } template <class A> inline void write_(A x) { write(x); putchar(' '); } template <class A> inline void writeln(A x) { write(x); putchar('\n'); } template <class A, class B> inline void write(A a, B b) { write_(a); write(b); } template <class A, class B> inline void writeln(A a, B b) { write_(a); writeln(b); } template <class A, class B, class C> inline void writeln(A a, B b, C c) { write_(a); write_(b); writeln(c); } template <class A, class B, class C, class D> inline void writeln(A a, B b, C c, D d) { write_(a); write_(b); write_(c); writeln(d); } const int N = 2e5 + 10; const int HMOD = 1e7 + 7; const int B = 700; const int KB = N / B + 2; int n; int a[N], s[N]; int nxt[N]; char ts[N]; int val[N], pre[N]; long long key[N]; int head[HMOD], nodecnt; void add(long long k) { int p = head[std::abs(k) % HMOD]; while (p and key[p] ^ k) p = pre[p]; if (!p) { pre[++nodecnt] = head[std::abs(k) % HMOD]; head[std::abs(k) % HMOD] = nodecnt; key[nodecnt] = k; p = nodecnt; } val[p]++; } void reset() { int i; for (i = int(1); i <= int(nodecnt); i++) { head[std::abs(key[i]) % HMOD] = 0; pre[i] = val[i] = key[i] = 0; } nodecnt = 0; } int main() { int i, l, r, k; long long cnt = 0; read(ts + 1); n = strlen(ts + 1); for (i = int(1); i <= int(n); i++) { a[i] = ts[i] & 1; s[i] = s[i - 1] + a[i]; } nxt[n] = n + 1; for (i = int(n - 1); i >= int(0); i--) nxt[i] = a[i + 1] ? (i + 1) : nxt[i + 1]; for (i = int(0); i <= int(n - 1); i++) { for (l = nxt[i]; l <= n and s[l] - s[i] <= B; l = r + 1) { r = nxt[l] - 1; cnt += std::max(0, (r - i) / (s[l] - s[i]) - std::max((l - i - 1) / (s[l] - s[i]) + 1, KB + 1) + 1); } } for (k = int(1); k <= int(KB); k++) { for (i = int(0); i <= int(n); i++) add(i - 1LL * k * s[i]); for (i = int(1); i <= int(nodecnt); i++) cnt += (val[i] - 1LL) * val[i] / 2; reset(); } writeln(cnt); return 0; }
12
CPP
def help(): n,x = map(int,input().split(" ")) arr = list(map(int,input().split(" "))) if(sum(arr)%x!=0): print(n) return notd = [] for i in range(n): if(arr[i]%x!=0): notd.append(i) if(len(notd)==0): print(-1) return s1 = notd[0] s2 = notd[-1] s2 = n-1-s2 print(max(s1,n-1-s1,s2,n-1-s2)) for _ in range(int(input())): help()
7
PYTHON3
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; if (n == 4) { cout << "2 4 1 3" << endl; return; } int a[n]; if (n % 4 == 0 || (n - 1) % 4 == 0) { a[n - 3] = n + 1 - 4; a[0] = 2; a[n - 2] = 1; a[2] = 4; a[n - 1] = n - 1; a[1] = n; if (n % 2 == 0) { for (int i = 3; i <= n / 2; i++) { if (i % 2 == 0) { a[i] = min((a[i - 2] + 2), (n - a[i - 2] - 1)); a[n - i - 1] = n + 1 - a[i]; } else { a[i] = max((a[i - 2] - 2), (n - a[i - 2] + 3)); a[n - i - 1] = n + 1 - a[i]; } } } else { for (int i = 3; i <= n / 2; i++) { if (i % 2 == 0) { a[i] = min((a[i - 2] + 2), (n - a[i - 2] - 1)); a[n - i - 1] = n + 1 - a[i]; } else { a[i] = max((a[i - 2] - 2), (n - a[i - 2] + 3)); a[n - i - 1] = n + 1 - a[i]; } } a[n / 2] = (n + 1) / 2; } for (int i = 0; i < n; i++) { cout << a[i] << " "; } } else cout << "-1" << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5; const long long mod = 1e9 + 7; long long to_int(string s) { long long res = 0; for (long long i = 0; i < s.size(); ++i) { res = res * 10 + (s[i] - '0'); } return res; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n, k; cin >> n >> k; vector<string> s(n); vector<long long> per(k); for (long long i = 0; i < n; ++i) { cin >> s[i]; } for (long long i = 0; i < k; ++i) { per[i] = i; } long long ans = 1e9; do { long long mi = 1e9, mx = -1; for (long long i = 0; i < n; ++i) { string t = ""; for (long long j = 0; j < k; ++j) { t += s[i][per[j]]; } long long a = to_int(t); mi = min(a, mi); mx = max(mx, a); } ans = min(ans, mx - mi); } while (next_permutation(per.begin(), per.end())); cout << ans; }
8
CPP
#include <bits/stdc++.h> using namespace std; int a,b; int main(){ char c; for(int i=1;cin>>c;i++){ if(c=='A'&&!a)a=i; if(c=='Z')b=i; } cout<<b-a+1<<endl; return 0; }
0
CPP
input();print(['EASY','HARD']['1' in input().replace(' ','')])
7
PYTHON3
n, g, m = int(input()), list(map(int, input().split())), int(input()) for i in range(m): x, y = map(lambda x: int(x) - 1, input().split()) if x != 0: g[x-1] += y if x != n-1: g[x+1] += g[x] - y - 1 g[x] = 0 for c in g: print(c)
7
PYTHON3
n = int(input()) l,r = [0,0],[0,0] for _ in range(n): cl,cr = map(int,input().split()) l[cl]+=1 r[cr]+=1 print(min(l)+min(r))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m, p = 1000000007; long long z = 1; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { z = z * i % p; } for (int i = 1; i <= m; i++) { z = z * i % p; } if (abs(n - m) > 1) { z = 0; } else if (n == m) { z = z * 2 % p; } printf("%lld\n", z); return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int g[111][111]; int grundy(int a11, int a12, int an1, int an2, int n) { int mask = (a11 << 3) + (a12 << 2) + (an1 << 1) + (an2 << 0); if (g[mask][n] != -1) return g[mask][n]; int a[111][5]; for (int i = 1; i <= n; i++) a[i][1] = a[i][2] = 0; a[1][1] = a11; a[1][2] = a12; a[n][1] = an1; a[n][2] = an2; bool was[260]; for (int i = 0; i < 260; i++) was[i] = false; bool alive[111]; for (int i = 1; i <= n; i++) for (int j = 1; j <= 2; j++) if (a[i][j] == 0) { a[i][j]++; a[i - 1][3 - j]++; a[i][3 - j]++; a[i + 1][3 - j]++; for (int q = 1; q <= n; q++) alive[q] = (a[q][1] * a[q][2] == 0); int st = 1, res = 0; while (st <= n) { while (st <= n && !alive[st]) st++; if (st > n) break; int fin = st; while (fin + 1 <= n && alive[fin + 1]) fin++; res ^= grundy(a[st][1], a[st][2], a[fin][1], a[fin][2], fin - st + 1); st = fin + 1; } was[res] = true; a[i][j]--; a[i - 1][3 - j]--; a[i][3 - j]--; a[i + 1][3 - j]--; } int &ans = g[mask][n]; ans = 0; while (was[ans]) ans++; return ans; } int main() { int n, cnt, a[111][5]; scanf("%d %d", &n, &cnt); for (int i = 1; i <= n; i++) a[i][1] = a[i][2] = 0; for (int i = 0; i < cnt; i++) { int x, y; scanf("%d %d", &x, &y); a[x][y] = 1; a[x - 1][3 - y] = 1; a[x][3 - y] = 1; a[x + 1][3 - y] = 1; } memset(g, 255, sizeof(g)); bool alive[111]; for (int q = 1; q <= n; q++) alive[q] = (a[q][1] * a[q][2] == 0); int st = 1, res = 0; while (st <= n) { while (st <= n && !alive[st]) st++; if (st > n) break; int fin = st; while (fin + 1 <= n && alive[fin + 1]) fin++; res ^= grundy(a[st][1], a[st][2], a[fin][1], a[fin][2], fin - st + 1); st = fin + 1; } puts(res > 0 ? "WIN" : "LOSE"); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, s = 0, b = 0, cnt1 = 0, cnt2 = 0; char ch[200]; cin >> n; int mid = n / 2; for (int i = 0; i < n; i++) { cin >> ch[i]; if (ch[i] == 'x') b++; else if (ch[i] == 'X') s++; } for (int i = 0; i < n; i++) { if (b > mid) { if (ch[i] == 'x') { ch[i] = 'X'; cnt1++; } } if (s > mid) { if (ch[i] == 'X') { ch[i] = 'x'; cnt2++; } } if (cnt1 == (b - mid) || cnt2 == (s - mid)) break; } cout << (b > mid ? (b - mid) : (s - mid)) << endl; for (int i = 0; i < n; i++) cout << ch[i]; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; string a, b; int s4a, s4b, s7a, s7b, t, ans; int main() { cin >> a >> b; for (int i = 0; a[i]; i++) { if (a[i] == '4') s4a++; else s7a++; if (b[i] == '4') s4b++; else s7b++; if (a[i] != b[i]) t++; } ans += abs(s4a - s4b); t -= abs(s4a - s4b); cout << (ans += t / 2); }
7
CPP
import math as m n,o=map(int,input().split()) count=0 if n>o: a=m.floor(m.sqrt(n)) for i in range(a+1): b= n-pow(i,2) if i+pow(b,2)==o: count+=1 else: b=m.floor(m.sqrt(o)) for i in range(b+1): a=o-pow(i,2) if pow(a,2)+i==n: count+=1 print(count)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; bool isPrime(long long int n) { for (long long int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } int main() { long long int t; bool flag; long long int n, m, i, j, k, temp; vector<long long int> nums, vec; long long int count = 0; string s; map<long long int, long long int> mapp; cin >> n; count = 0; long long int count_2 = 0; for (i = 0; i < n; i++) { cin >> temp, nums.push_back(temp); mapp[temp]++; if (mapp[temp] == 4) { count++; count_2--; } if (mapp[temp] == 2) count_2++; if (mapp[temp] > 4) { if (mapp[temp] % 2 == 0) count_2++; } } char c; cin >> t; long long int x; while (t--) { cin >> c; cin >> x; if (c == '+') { mapp[x]++; if (mapp[x] == 4) { count++; count_2--; } if (mapp[x] == 2) count_2++; if (mapp[x] > 4) { if (mapp[x] % 2 == 0) count_2++; } if (count < 1) cout << "NO" << endl; else if (count > 1) cout << "YES" << endl; else if (count_2 > 1) cout << "YES" << endl; else cout << "NO" << endl; } else { mapp[x]--; if (mapp[x] + 1 == 4) { count--; count_2++; } if (mapp[x] + 1 == 2) count_2--; if (mapp[x] + 1 > 4) { if (mapp[x] % 2 == 1) count_2--; } if (count < 1) cout << "NO" << endl; else if (count > 1) cout << "YES" << endl; else if (count_2 > 1) cout << "YES" << endl; else cout << "NO" << endl; } } nums.clear(); vec.clear(); return 0; }
8
CPP
#include <bits/stdc++.h> template <typename T> inline void rd(T& x) { int si = 1; char c = getchar(); x = 0; while (!isdigit(c)) si = c == '-' ? -1 : si, c = getchar(); while (isdigit(c)) x = x * 10 + c - 48, c = getchar(); x *= si; } template <typename T, typename... Args> inline void rd(T& x, Args&... args) { rd(x); rd(args...); } const int kN = 3e3 + 5; const long long kMod = 998244353; long long QPow(long long a, long long b) { long long ans = 1, bas = a; for (; b; b >>= 1, bas = bas * bas % kMod) if (b & 1) ans = ans * bas % kMod; return ans; } char s[kN], t[kN]; int n, m; long long f[kN][kN]; int main() { int T = 1; while (T--) { scanf("%s%s", s + 1, t + 1); n = strlen(s + 1); m = strlen(t + 1); for (int i = 1; i <= n; ++i) f[i][i] = 2 * (i <= m ? s[1] == t[i] : 1); for (int len = 1; len < n; ++len) for (int i = 1; i + len - 1 <= n; ++i) { int j = i + len - 1, x = len + 1; if (j < n && (j + 1 > m || s[x] == t[j + 1])) f[i][j + 1] = (f[i][j + 1] + f[i][j]) % kMod; if (i > 1 && (i - 1 > m || s[x] == t[i - 1])) f[i - 1][j] = (f[i - 1][j] + f[i][j]) % kMod; } long long ans = 0; for (int i = m; i <= n; ++i) ans = (ans + f[1][i]) % kMod; printf("%lld", ans); } return 0; }
9
CPP
#include<bits/stdc++.h> using namespace std; int main() { int N;scanf("%d",&N); int A[N]; for(int i=0;i<N;i++)scanf("%d ",&A[i]); int minus=1; for(int i=0;i<N;i++)minus*=(2-A[i]%2); printf("%d\n",(int)pow(3,N)-minus); }
0
CPP
x = int(input()) count = 0 while x != 0: if x >= 5: x -= 5 count += 1 elif x < 5: if x == 4: x -= 4 count += 1 elif x == 3: x -= 3 count += 1 elif x == 2: x -= 2 count += 1 elif x == 1: x -= 1 count += 1 print(count)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int x[55], y[55]; int n; int main() { int i, j; scanf("%d", &n); for (i = 1; i <= n; ++i) scanf("%d%d%*d", x + i, y + i); for (i = 1; i < n; ++i) putchar('('); printf("(%d*((1-abs((t-%d)))+abs((abs((t-%d))-1))))", x[1] >> 1, 0, 0); for (i = 2; i <= n; ++i) { putchar('+'); printf("(%d*((1-abs((t-%d)))+abs((abs((t-%d))-1)))))", x[i] >> 1, i, i); } putchar('\n'); for (i = 1; i < n; ++i) putchar('('); printf("(%d*((1-abs((t-%d)))+abs((abs((t-%d))-1))))", y[1] >> 1, 0, 0); for (i = 2; i <= n; ++i) { putchar('+'); printf("(%d*((1-abs((t-%d)))+abs((abs((t-%d))-1)))))", y[i] >> 1, i, i); } putchar('\n'); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; struct domino { int x, h, i, z; void inp(int _i) { scanf("%d%d", &x, &h), i = _i, z = 1; } }; bool cmp_toado(domino p, domino q) { return (p.x < q.x); } bool cmp_thutu(domino p, domino q) { return (p.i < q.i); } int n; domino a[100010]; int main() { cin >> n; for (int i = 1; i <= n; i++) a[i].inp(i); sort(a + 1, a + n + 1, cmp_toado); for (int i = n - 1; i >= 1; i--) { int j = i + 1; while (j <= n && a[i].h + a[i].x > a[j].x) { a[i].z += a[j].z; j += a[j].z; } } sort(a + 1, a + n + 1, cmp_thutu); for (int i = 1; i <= n; i++) printf("%d ", a[i].z); }
11
CPP
n, a, x, b, y = map(int, input().split()) a=a-1 x=x-1 b=b-1 y=y-1 while a != b and a != x and b != y: a = (a+1) % n b = (b-1) % n if a == b: print('YES') else: print('NO')
7
PYTHON3
s = input().split() n, k = int(s[0]), int(s[1]) for l in range(k - 1, 0, -1): if n % l == 0: print((n // l) * k + l) break
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); string s; map<char, int> m; int n, cnt = 0; cin >> n >> s; for (char i = 'a'; i < 'z'; i++) m[i] = 0; for (int i = 0; i < s.size(); i++) { if (islower(s[i])) m[s[i]]++; else if (isupper(s[i]) && m[s[i] + 32]) m[s[i] + 32]--; else cnt++; } cout << cnt << endl; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; // #define int long long // <-----!!!!!!!!!!!!!!!!!!! #define rep(i,n) for (int i=0;i<(n);i++) #define rep2(i,a,b) for (int i=(a);i<(b);i++) #define rrep(i,n) for (int i=(n)-1;i>=0;i--) #define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--) #define all(a) (a).begin(),(a).end() typedef long long ll; typedef pair<int, int> P; typedef tuple<int, int, int> TUPLE; typedef vector<int> V; typedef vector<V> VV; typedef vector<VV> VVV; const int MAX_N = 10000; int n; int a[MAX_N]; // la[i][j] = num of color j "successive" in the left of i (excluding i) int la[MAX_N][3]; int ra[MAX_N][3]; // calc num of remnants when change a[s] to c int calcNumRem(int s, int c) { if (la[s][c] + 1 + ra[s][c] < 4) { return n; } int l = s - la[s][c]; int r = s + ra[s][c]; bool flag = true; while (flag) { flag = false; rep(j, 3) { if (la[l][j] + ra[r][j] >= 4) { l -= la[l][j]; r += ra[r][j]; flag = true; break; } } } return n - (r - l + 1); } void init() { rep(i, n) { a[i] = 0; rep(j, 3) la[i][j] = 0, ra[i][j] = 0; } } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); while (cin >> n, n) { init(); rep(i, n) { cin >> a[i]; a[i]--; } rep2(i, 1, n) { la[i][a[i - 1]] += la[i - 1][a[i - 1]] + 1; } rrep(i, n - 1) { ra[i][a[i + 1]] += ra[i + 1][a[i + 1]] + 1; } int ans = n; rep(i, n) { rep(j, 3) { ans = min(ans, calcNumRem(i, j)); } } cout << ans << endl; } }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long w, len; cin >> w >> len; long long arr[w], sum = 0, ans = 0; for (int i = 0; i < w - 1; i++) { cin >> arr[i]; } for (int i = 0; i < len; i++) sum += arr[i]; ans = sum; for (int i = len; i < w - 1; i++) { sum = sum - arr[i - len] + arr[i]; ans = min(ans, sum); } cout << ans << "\n"; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int inf = 1e5 + 9, lg = 30; int a[inf * 3], n, table[lg][inf << 2]; int ans[inf], nxt[inf], sml[inf << 2]; void build() { int i, j; for (i = 1; i <= n + n; i++) table[0][i] = a[i]; for (j = 1; j < lg - 1; j++) for (i = 1; i + (1 << (j - 1)) - 1 <= n + n; i++) table[j][i] = min(table[j - 1][i], table[j - 1][i + (1 << (j - 1))]); } int query(int x, int y) { int len, k; len = y - x + 1; k = log2(len); return min(table[k][x], table[k][y + 1 - (1 << k)]); } int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i], a[i + n] = a[i], a[i + n + n] = a[i]; int mx = 0; for (int i = n;; i++) { if (mx > a[i]) break; if (i == n + n + n) { ans[n] = -1; break; } ans[n]++; mx = max(mx, (a[i] % 2 == 0 ? a[i] / 2 : a[i] / 2 + 1)); } ans[n + n] = ans[n]; stack<int> st; for (int i = n + n; i >= 1; i--) { while (!st.empty() && a[st.top()] <= a[i]) st.pop(); if (st.empty()) nxt[i] = -1; else nxt[i] = st.top(); st.push(i); } build(); for (int i = 1; i <= n; i++) { sml[i] = 1e9; int val = (a[i] % 2 == 0 ? a[i] / 2 : a[i] / 2 + 1); int l = i, r = min(n + n + 1, n + i + 1); while (r - l > 1) { int mid = (l + r) / 2; if (query(i, mid) < val) r = mid; else l = mid; } if (a[r] < val) sml[i] = r; } vector<pair<int, int> > v; for (int i = 1; i <= n; i++) { v.push_back(make_pair(a[i], i)); } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); for (auto o : v) { int i = o.second; if (sml[i] == 1e9) { if (nxt[i] == -1) ans[i] = -1; else ans[i] = (ans[nxt[i]] != -1 ? ans[nxt[i]] + nxt[i] - i : -1); } else { if (nxt[i] == -1) ans[i] = sml[i] - i; else { if (sml[i] < nxt[i]) ans[i] = sml[i] - i; else ans[i] = (ans[nxt[i]] != -1 ? ans[nxt[i]] + nxt[i] - i : -1); } } ans[i + n] = ans[i]; } for (int i = 1; i <= n; i++) cout << ans[i] << " "; }
10
CPP
from functools import reduce from operator import mul def si(): return str(input()) def ii(): return int(input()) def mi(): return map(int, input().split()) def li(): return list(mi()) t = 1 for _ in range(t): n = ii() prod = reduce(mul, range(1, n + 1)) print(prod//(n*n//2))
11
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; int arr[500500]; long long suf[500500]; int sol[500500]; void ok(int l, int r) { sol[l]++; sol[r + 1]--; } int main() { scanf("%d", &n); long long sm = 0; for (int i = 1; i <= n; i++) { scanf("%d", &arr[i]); sm += arr[i]; } sort(arr + 1, arr + 1 + n); reverse(arr + 1, arr + 1 + n); for (int i = 1; i <= n; i++) { suf[i] = arr[i]; } for (int i = n - 1; i >= 1; i--) { suf[i] += suf[i + 1]; } int lower = 0, upper = n; long long left = 0, right = 0; for (long long i = 1; i <= n; i++) { int l = i - 1, r = n + 1; while (r - l > 1) { int mid = (r + l) / 2; if (arr[mid] >= i) { l = mid; } else { r = mid; } } right = (l - i + 1) * i + suf[r]; long long ok_l = max(0, arr[i]), ok_r = min(i * (i - 1) + right - left, (long long)n); left += arr[i]; if (r < i) r = i; right = max(l - i, 0ll) * i + suf[r]; long long hh = left - right - i * (i - 1); long long ok_l2, ok_r2; if (hh > i) { ok_l2 = 1; ok_r2 = 0; } else { ok_l2 = max(0ll, hh); ok_r2 = arr[i]; } if (ok_l2 < ok_l) { swap(ok_l2, ok_l); swap(ok_r2, ok_r); } if (ok_l2 <= ok_r) { ok(ok_l, max(ok_r, ok_r2)); } else { ok(ok_l, ok_r); ok(ok_l2, ok_r2); } } for (int i = 1; i <= n; i++) { sol[i] += sol[i - 1]; } bool found = false; for (int i = 0; i <= n; i++) { if ((sm + i) % 2 == 0 && sol[i] == n) { found = true; cout << i << " "; } } if (!found) { cout << -1 << endl; } }
11
CPP
if int(input()) != 2: print(1) else: print(2)
7
PYTHON3
x=i=0 n=int(input()) while i<n: s=input() if s.count('++')==1: x+=1 else: x-=1 i+=1 print(x)
7
PYTHON3
n=int(input()) for i in range(1,n): if(i%2==1): print("I hate that",end=" ") else: print("I love that",end=" ") if(n%2==1): print("I hate it") else: print("I love it")
7
PYTHON3
from itertools import accumulate as acc def bit_sum(ls, i): s = 0 i += 1 while i>0: s += ls[i] i -= i & -i return s def bit_add(ls, i, n): i += 1 while i<len(ls): ls[i] += n i += i & -i N, K, *A = map(int, open(0).read().split()) A = [a-K for a in A] B = [(b,i) for i, b in enumerate(acc(A))] bit_B = [0]*(N+1) B.sort(reverse=True) ans = sum(c[0]>=0 for c in B) for b,i in B: bit_add(bit_B,i,1) ans += bit_sum(bit_B,N-1)-bit_sum(bit_B,i) print(ans)
0
PYTHON3
s=input() m=0 b=0 for i in range(len(s)): if(ord(s[i])>96): m+=1 else: b+=1 if(b>m): print(s.upper()) else: print(s.lower())
7
PYTHON3
import math import sys input = sys.stdin.readline def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input().strip() return(list(s[:len(s)])) def invr(): return(map(int,input().split())) def is_hv(z): if z>0 and z<n-1: if l[z]>l[z-1] and l[z]>l[z+1]: return 1 elif l[z]<l[z-1] and l[z]<l[z+1]: return 1 return 0 for _ in range(inp()): n=inp() l=inlt() cnt=0 for i in range(1,n-1): if is_hv(i): cnt+=1 mn=cnt ans=mn for i in range(1,n-1): if is_hv(i): temp1=l[i] temp2=is_hv(i)+is_hv(i+1)+is_hv(i-1) temp=l[i+1] l[i]=temp temp3=is_hv(i+1)+is_hv(i-1) mn=min(mn,ans-temp2+temp3) temp=l[i-1] l[i]=temp temp3=is_hv(i+1)+is_hv(i-1) mn=min(mn,ans-temp2+temp3) l[i]=temp1 print(mn)
8
PYTHON3
import math n = int(input()) a = list(map(int, input().split(' '))) min_index, max_index = 0, 0 for i, x in enumerate(a): if x > a[max_index]: max_index = i if x <= a[min_index]: min_index = i if min_index < max_index: print(max_index + len(a) - min_index - 2) else: print(max_index + len(a) - min_index - 1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) {} long long t, d, n, m, q, k; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.precision(20); long long TestCases = 1; while (TestCases--) { string s; cin >> n >> s; long long p = (n - 11) / 2; vector<long long> v; q = 0; for (long long i = (0); i < (n); ++(i)) if (s[i] == '8') ++q, v.push_back(i); if (q <= p) { cout << "NO"; return 0; } { string _s = "v[p]"; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, v[p]); }; if (v[p] <= 2 * p) cout << "YES"; else cout << "NO"; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; int main() { ios::sync_with_stdio(false); int m, n, o, a[200005] = {}, b[200005] = {}; cin >> m >> n; while (n--) cin >> o, a[o]++; for (int i = 1; i <= m; i++) { if (a[i]) b[i] = i; else b[i] = b[i - 1]; } for (int z = 1; z <= m; z++) { int c = m, e = b[m]; bool d = 0; while (e || (!d && z <= c)) { if (!d && z >= e && z <= c) { c -= z; e = b[c]; d = 1; } else { if (c >= 1ll * e * a[e]) { c -= 1ll * e * a[e]; e = b[e - 1]; } else { c %= e; e = b[c]; } } } if (c) { cout << z; return 0; } } cout << "Greed is good"; }
11
CPP