solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
n = int(input()) line = list(input()) counter = 0 flag = 0 for i in range(1,n): cur = line[i] prev = line[i-1] if cur == prev: counter += 1 print(counter)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int INF = 2147483647; template <class T> int size(const T &x) { return x.size(); } list<int> inst[100100]; int arr[100100], res[100100], sum[100100], inside = 0, ok = 0, st; bool cmp(const pair<pair<int, int>, int> &a, const pair<pair<int, int>, int> &b) { return pair<int, int>(a.first.first / st, a.first.second) < pair<int, int>(b.first.first / st, b.first.second); } void add(int i, bool left) { int cur = arr[i]; int add = 0; if (inst[cur].empty()) { ok++; inside++; } if (left) { inst[cur].push_front(i); list<int>::const_iterator it = inst[cur].begin(); int a = *it; it++; if (it != inst[cur].end()) { int b = *it; it++; if (it != inst[cur].end()) { int c = *it; add += abs((b - a) - (c - b)); } } } else { inst[cur].push_back(i); list<int>::reverse_iterator it = inst[cur].rbegin(); int c = *it; it++; if (it != inst[cur].rend()) { int b = *it; it++; if (it != inst[cur].rend()) { int a = *it; add += abs((b - a) - (c - b)); } } } if (add > 0 && sum[cur] == 0) { ok--; } sum[cur] += add; } void remove(int i, bool left) { int cur = arr[i]; int add = 0; assert(!inst[cur].empty()); if (left) { list<int>::const_iterator it = inst[cur].begin(); int a = *it; it++; if (it != inst[cur].end()) { int b = *it; it++; if (it != inst[cur].end()) { int c = *it; add -= abs((b - a) - (c - b)); } } inst[cur].pop_front(); } else { list<int>::reverse_iterator it = inst[cur].rbegin(); int c = *it; it++; if (it != inst[cur].rend()) { int b = *it; it++; if (it != inst[cur].rend()) { int a = *it; add -= abs((b - a) - (c - b)); } } inst[cur].pop_back(); } if (sum[cur] != 0 && sum[cur] + add == 0) { ok++; } sum[cur] += add; if (inst[cur].empty()) { ok--; inside--; } } int main() { memset(sum, 0, sizeof(sum)); int n; scanf("%d\n", &n); st = static_cast<int>(sqrt(n) + 1); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } int m; scanf("%d\n", &m); vector<pair<pair<int, int>, int> > queries; for (int i = 0; i < m; i++) { int l, r; scanf("%d %d\n", &l, &r); l--, r--; queries.push_back(make_pair(pair<int, int>(l, r), i)); } sort((queries).begin(), (queries).end(), cmp); int l = 0, r = 0; add(0, true); for (int i = 0; i < size(queries); i++) { while (l > queries[i].first.first) add(--l, true); while (r < queries[i].first.second) add(++r, false); while (l < queries[i].first.first) remove(l++, true); while (r > queries[i].first.second) remove(r--, false); res[queries[i].second] = inside; if (ok == 0) res[queries[i].second]++; } for (int i = 0; i < m; i++) { cout << res[i] << endl; } return 0; }
2,700
CPP
#include <bits/stdc++.h> using namespace std; const long long UNDEF = -1; const long long INF = 1e18; template <typename T> inline bool chkmax(T &aa, T bb) { return aa < bb ? aa = bb, true : false; } template <typename T> inline bool chkmin(T &aa, T bb) { return aa > bb ? aa = bb, true : false; } const long long mn = 2e5 + 2; long long vx[mn]; long long n, m; long long get_geq(long long x) { long long imin = 0, imax = n; while (imin < imax) { long long imid = (imin + imax) / 2; if (vx[imid] < x) imin = imid + 1; else imax = imid; } if (0 <= imin && imin < n && vx[imin] >= x) return imin; return -1; } long long get_leq(long long x) { long long imin = 0, imax = n; while (imin < imax) { long long imid = (imin + imax) / 2; if (vx[imid] <= x) imin = imid + 1; else imax = imid; } --imin; if (0 <= imin && imin < n && vx[imin] <= x) return imin; return -1; } const long long RIGHT = 1; map<long long, long long> h[2]; long long f(long long dir, long long a, long long l) { if (l == 0) return a; auto it = h[dir].find(a); if (it != h[dir].end()) { long long diff = it->second - l; assert(diff != 0); long long freshl = (l % diff) + diff; if (freshl < l) l = freshl; } h[dir][a] = l; long long newa; if (dir == RIGHT) { newa = get_leq(vx[a] + l); } else { newa = get_geq(vx[a] - l); } if (newa == -1 || newa == a) { dir ^= 1; if (dir == RIGHT) { newa = get_leq(vx[a] + l); } else { newa = get_geq(vx[a] - l); } } if (newa == -1 || newa == a) return a; long long newl = l - abs(vx[newa] - vx[a]); long long newdir = dir ^ 1; return f(newdir, newa, newl); } long long oldIdxToNew[mn]; long long newIdxToOld[mn]; pair<long long, long long> px[mn]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); scanf("%lld%lld", &n, &m); for (long long i = 0; i < n; i++) { scanf("%lld", &px[i].first); px[i].second = i + 1; } sort(px, px + n); for (long long i = 0; i < n; i++) { newIdxToOld[i] = px[i].second; oldIdxToNew[px[i].second] = i; } for (long long i = 0; i < n; i++) vx[i] = px[i].first; for (long long i = 0; i < m; i++) { long long olda, l; scanf("%lld%lld", &olda, &l); long long a = oldIdxToNew[olda]; for (long long d = 0; d < 2; d++) h[d].clear(); long long ans = f(RIGHT, a, l); long long oldans = newIdxToOld[ans]; printf("%lld\n", oldans); } }
2,500
CPP
n=int(input()) i=1 x=0 while (n>i*5): n-=i*5 i*=2 if (n%i==0): x=n//i else: x=n//i+1 if x==1: print('Sheldon') if x==2: print('Leonard') if x==3: print('Penny') if x==4: print('Rajesh') if x==5: print('Howard')
1,100
PYTHON3
n = int(input()) u = list(map(int, input().split())) ans = min(u) for i in range(1, n): ans = min(ans, min(u[0], u[i]) // i) if i != n - 1: ans = min(ans, min(u[-1], u[i]) // (n - i - 1)) print(ans)
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; using namespace std; int main() { cout << "? "; for (int i = 1; i < 101; i++) cout << 128 * i << " "; cout << endl; int ans = 0; int in1; cin >> in1; ans += in1 % 128; cout << "? "; for (int i = 0; i < 100; i++) cout << i << " "; cout << endl; int in2; cin >> in2; ans += 128 * (in2 / 128); cout << "! " << ans << endl; return 0; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; long long solve(long long s, long long x) { if ((s & 1) != (x & 1)) return 0; if (s == x && x <= 1) return x + 1; if (s & 1) return 2 * solve(s >> 1, x >> 1); return solve((s >> 1) - 1, x >> 1) + solve((s >> 1), x >> 1); } int main() { long long s, x; scanf("%lld %lld", &s, &x); printf("%lld\n", solve(s, x) - 2 * (s == x)); }
1,700
CPP
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; struct vertical { int x, t; }; struct horizontal { int y, t; }; bool operator<(vertical A, vertical B) { if (A.x == B.x) return A.t < B.t; return A.x < B.x; } bool operator<(horizontal A, horizontal B) { if (A.y == B.y) return A.t < B.t; return A.y < B.y; } vector<pair<pair<int, int>, pair<int, int> > > v; vector<pair<pair<int, int>, pair<int, int> > > v2; vector<vertical> s1; vector<horizontal> s2; inline bool solve(int x1, int y1, int x2, int y2) { s1.resize(0); s2.resize(0); v2.resize(0); for (int i = 0; i < v.size(); i++) { int a = v[i].first.first, c = v[i].first.second; int b = v[i].second.first, d = v[i].second.second; if (x1 <= a && c <= x2 && y1 <= b && d <= y2) { s1.push_back({a, 1}); s1.push_back({c, -1}); s2.push_back({b, 1}); s2.push_back({d, -1}); v2.push_back(v[i]); } } if (v2.size() <= 1) return 1; v = v2; sort(s1.begin(), s1.end()); sort(s2.begin(), s2.end()); auto it1 = s1.begin(); auto it2 = s1.end(); it2--; auto it3 = s2.begin(); auto it4 = s2.end(); it4--; int ver_balance1 = 0, hor_balance1 = 0; int ver_balance2 = 0, hor_balance2 = 0; int solving_ver = 1, solving_hor = 1; while (1) { if (solving_ver == 1) { ver_balance1 += it1->t; it1++; if (it1 == s1.end()) solving_ver = 0; it1--; ver_balance2 += it2->t; if (solving_ver == 1 && ver_balance1 == 0 && x1 < it1->x && it1->x < x2) { if (solve(x1, y1, it1->x, y2) && solve(it1->x, y1, x2, y2)) return 1; else return 0; } if (solving_ver == 1 && ver_balance2 == 0 && x1 < it2->x && it2->x < x2) { if (solve(x1, y1, it2->x, y2) && solve(it2->x, y1, x2, y2)) return 1; else return 0; } it1++; it2--; } if (solving_hor == 1) { hor_balance1 += it3->t; it3++; if (it3 == s2.end()) solving_hor = 0; it3--; hor_balance2 += it4->t; if (solving_hor == 1 && hor_balance1 == 0 && y1 < it3->y && it3->y < y2) { if (solve(x1, y1, x2, it3->y) && solve(x1, it3->y, x2, y2)) return 1; else return 0; } if (solving_hor == 1 && hor_balance2 == 0 && y1 < it4->y && it4->y < y2) { if (solve(x1, y1, x2, it4->y) && solve(x1, it4->y, x2, y2)) return 1; else return 0; } it3++; it4--; } if (solving_hor == 0 && solving_ver == 0) break; } return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) { int a, b, c, d; cin >> a >> b >> c >> d; v.push_back({{a, c}, {b, d}}); } if (solve(0, 0, 1000000000, 1000000000)) { cout << "YES"; } else { cout << "NO"; } }
2,500
CPP
#include <bits/stdc++.h> using namespace std; int read() { int x = 0, w = 1; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar(); return x * w; } int n, k, l1, r1, l2, r2; int dp[110][200010], l[110], r[110], team1[200010], team2[200010]; int main() { n = read(); k = read(); memset(dp, 0x3f, sizeof(dp)); for (int i = 1; i <= k; i++) l[i] = read(), r[i] = read(); dp[0][0] = 0; for (int i = 1; i <= k; i++) { l1 = 1; r1 = 0; memset(team1, 0, sizeof(team1)); memset(team2, 0, sizeof(team2)); for (int j = r[i]; j >= l[i]; j--) { while (l1 <= r1 && dp[i - 1][j] <= dp[i - 1][team1[r1]]) r1--; team1[++r1] = j; } for (int j = 0; j <= min(n, r[i]); j++) { dp[i][j] = dp[i - 1][j]; while (l1 <= r1 && team1[l1] > r[i] - j) l1++; if (l[i] - j >= 0) { while (l1 <= r1 && dp[i - 1][team1[r1]] >= dp[i - 1][l[i] - j]) r1--; team1[++r1] = l[i] - j; } if (l1 <= r1) dp[i][j] = min(dp[i][j], dp[i - 1][team1[l1]] + 1); } l2 = 1; r2 = 0; for (int j = 0; j <= min(n, r[i]); j++) { while (l2 <= r2 && team2[l2] < j - r[i] + l[i]) l2++; while (l2 <= r2 && dp[i - 1][team2[r2]] >= dp[i - 1][j]) r2--; team2[++r2] = j; dp[i][j] = min(dp[i][j], dp[i - 1][team2[l2]] + 2); } } if (dp[k][n] != dp[0][1]) cout << "Full\n" << dp[k][n] << endl; else cout << "Hungry"; }
2,400
CPP
sides = {'S': [0, -1], 'N': [0, 1],'W': [-1, 0] ,'E': [1, 0]} s = {'S': 'N', 'N': 'S','W': 'E', 'E': 'W'} for i in range(int(input())): sl = {'S': set(), 'N': set(),'W': set(), 'E': set()} cur = [0, 0] l = 0 w = input() for ch in w: if (cur[0], cur[1]) in sl[ch]: l += 1 cur = list(map(lambda x, y: x+y, cur, sides[ch])) else: l += 5 sl[ch].add((cur[0], cur[1])) cur = list(map(lambda x, y: x+y, cur, sides[ch])) sl[s[ch]].add((cur[0], cur[1])) print(l)
1,400
PYTHON3
n = int(input()) arr = sorted([int(i) for i in input().split()], reverse=True) ind = 0 changeInd = True for i in range(len(arr)+1): if changeInd and sum(arr[:i]) > sum(arr[i:]): ind = i changeInd = False print(ind)
900
PYTHON3
''' Author : Shubhanshu Jain; ''' from math import * import random; mod =1000000007 r1 = lambda : int(input()); rm = lambda : map(int,input().split()); rls = lambda : list(rm()) test = int(input()); while(test): test-=1; x1,y1,x2,y2=rm(); if(x1==x2 or y1==y2): print(1); else: x=abs(x1-x2); y=abs(y1-y2); print((x*y)+1)
1,600
PYTHON3
for u in range(int(input())): a,b,c=map(int,input().split()) x=-1 y=-1 if(a<c): x=1 if(a*b>c): y=b else: y=-1 else: x=-1 if(a*b>c): y=b else: y=-1 print(x,y)
1,000
PYTHON3
s1 = input() s2 = input() n1 = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"].index(s1) n2 = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"].index(s2) if n1 == n2: print("YES") else: if (n2 - n1 + 7) % 7 in [2, 3]: print("YES") else: print("NO")
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; long long int mod = 1e9 + 7; const long long int Size = 2e6 + 1; long long int factorial[Size]; long long int pow1(long long int x, long long int n, long long int m) { long long int result = 1; while (n > 0) { x %= m; if (n % 2 == 1) result = (result * x) % m; x = (x * x) % m; n = n / 2; } return result; } long long int pow2(long long int x, long long int n) { long long int result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x); x = (x * x); n = n / 2; } return result; } long long int modInverse(long long int A, long long int M) { return pow1(A, M - 2, M); } long long int n, T, X[Size], Time[Size], L[Size], f[Size], BIT[Size], co[Size], dp[Size]; vector<int> Adj[Size]; set<int> ti; map<int, long long int> fre; void update(long long int i, long long int val1, long long int val2) { for (; i < Size; i += i & (-i)) { BIT[i] += val1; co[i] += val2; } } long long int query1(long long int id) { long long int s = 0; for (; id > 0; id -= id & (-id)) s += BIT[id]; return s; } long long int query2(long long int id) { long long int s = 0; for (; id > 0; id -= id & (-id)) s += co[id]; return s; } long long int cal(long long int z) { long long int l = 0, r = *ti.rbegin(); while (l < r) { int mid = (l + r + 1) / 2; long long int s = query1(mid); if (s <= z) l = mid; else r = mid - 1; } long long int s = query2(l); long long int rt = z - query1(l); if (l != *ti.rbegin()) { int up = *ti.upper_bound(l); s += rt / up; } return s; } void dfs1(int s, int p, long long int u) { if (2 * u > T) return; update(Time[s], X[s] * Time[s], X[s]); ti.insert(Time[s]); fre[Time[s]] += X[s]; f[s] = cal(T - 2 * u); for (auto e : Adj[s]) { if (e == p) continue; dfs1(e, s, L[e] + u); } fre[Time[s]] -= X[s]; if (fre[Time[s]] == 0) ti.erase(Time[s]); update(Time[s], -X[s] * Time[s], -X[s]); } void dfs2(int s, int p) { vector<long long int> v; v.push_back(0); v.push_back(0); for (auto e : Adj[s]) { if (e == p) continue; dfs2(e, s); v.push_back(dp[e]); } sort(v.begin(), v.end()); if (s == 1) dp[s] = max(f[s], v[v.size() - 1]); else dp[s] = max(f[s], v[v.size() - 2]); } void solve() { cin >> n >> T; for (int i = int(0); i < int(n); i++) cin >> X[i + 1]; for (int i = int(0); i < int(n); i++) cin >> Time[i + 1]; for (int i = int(0); i < int(n - 1); i++) { int p, l; cin >> p >> l; Adj[p].push_back(i + 2); Adj[i + 2].push_back(p); L[i + 2] = l; } dfs1(1, -1, 0); dfs2(1, -1); cout << dp[1]; } int main() { int t; t = 1; while (t--) { solve(); } return 0; }
2,400
CPP
for _ in range(int(input())): n = int(input()) a = input() a = list(a) k = 0 m = 0 l = 0 for i in range(len(a)): if a[i]=="A": break else: l=l+1 for i in range(l,len(a)): if a[i]=="A": m = 0 else: m=m+1 k=max(m,k) print(k)
800
PYTHON3
from bisect import * from collections import * from itertools import * import functools import sys import math from decimal import * from copy import * getcontext().prec = 30 MAX = sys.maxsize MAXN = 10**5+10 MOD = 10**9+7 def isprime(n): n = abs(int(n)) if n < 2: return False if n == 2: return True if not n & 1: return False for x in range(3, int(n**0.5) + 1, 2): if n % x == 0: return False return True def mhd(a,b): return abs(a[0]-b[0])+abs(b[1]-a[1]) def charIN(x= ' '): return(sys.stdin.readline().strip().split(x)) def arrIN(x = ' '): return list(map(int,sys.stdin.readline().strip().split(x))) def eld(x,y): a = y[0]-x[0] b = x[1]-y[1] return (a*a+b*b)**0.5 def lgcd(a): g = a[0] for i in range(1,len(a)): g = math.gcd(g,a[i]) return g def ms(a): msf = -MAX meh = 0 st = en = be = 0 for i in range(len(a)): meh+=a[i] if msf<meh: msf = meh st = be en = i if meh<0: meh = 0 be = i+1 return msf,st,en def ncr(n,r): num=den=1 for i in range(r): num = (num*(n-i))%MOD den = (den*(i+1))%MOD return (num*(pow(den,MOD-2,MOD)))%MOD def flush(): return sys.stdout.flush() def fac(n): ans = 1 for i in range(1,n+1): ans*=i ans%=MOD return ans '''*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*''' for _ in range(int(input())): n,m,k = arrIN() h = arrIN() i = 0 while i<n-1: if abs(h[i]-h[i+1])<=k: if h[i]>=h[i+1]: t = min(h[i]-h[i+1],k) m+=t h[i]-=t if h[i+1]>=h[i]: t = max(0,h[i+1]-k) m+=(h[i]-t) h[i]-=(h[i]-t) i+=1 else: if h[i]>=h[i+1]: m+=(h[i]-h[i+1]-k) h[i]-=(h[i]-h[i+1]-k) if h[i]>=h[i+1]: t = min(h[i]-h[i+1],k) m+=t h[i]-=t if h[i+1]>=h[i]: t = max(0,h[i+1]-k) m+=(h[i]-t) h[i]-=(h[i]-t) else: req = (h[i+1]-h[i]-k) if req>m: print('NO') break else: m-=req i+=1 if i==n-1: print('YES')
1,200
PYTHON3
def __main__(): n_k = list(map(int,input().split(" "))) c = list(map(int,input().split(" "))) count = 0 for i in c : if (c[n_k[1]-1]>0 and i>=c[n_k[1]-1]) or (c[n_k[1]-1]==0 and i>c[n_k[1]-1]): count+=1 print(count) __main__()
800
PYTHON3
for _ in range(int(input())): n=int(input()) t=max((-1+(1+24*n)**.5)//6,(-1-(1+24*n)**.5)//6);t=int(t);Ans=0 while n!=0 and t>0: k=(3*t**2+t)//2;m=n//k;Ans+=m;n=n-m*k;t-=1 print(Ans)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 100; const int mod = 1e9 + 7; mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); int n; vector<int> adj[maxn]; long long cost[maxn], dp[maxn][2]; void dfs(int v, int p) { if ((int)adj[v].size() - (p != 0) == 0) { dp[v][0] = cost[v]; dp[v][1] = 0; return; } long long zersum = 0; for (int to : adj[v]) if (to != p) { dfs(to, v); zersum += dp[to][0]; } dp[v][0] = zersum; dp[v][1] = 1e18; for (int to : adj[v]) if (to != p) { dp[v][0] = min(dp[v][0], zersum + cost[v] - dp[to][0] + dp[to][1]); dp[v][1] = min(dp[v][1], zersum - dp[to][0] + dp[to][1]); } } set<int> ans; void dfs2(int v, int p, int st) { if ((int)adj[v].size() - (p != 0) == 0) { if (st == 0) ans.insert(v); return; } long long zersum = 0; for (int to : adj[v]) if (to != p) zersum += dp[to][0]; int u = -1; for (int to : adj[v]) if (to != p) { if (st == 0) { if (dp[v][0] == zersum + cost[v] - dp[to][0] + dp[to][1]) { ans.insert(v); if (u == -1) u = to; else u = 0; } } else { if (dp[v][1] == zersum - dp[to][0] + dp[to][1]) { if (u == -1) u = to; else u = 0; } } } if (st == 0 && zersum == dp[v][0]) u = 0; for (int to : adj[v]) if (to != p) { if (u == to) dfs2(to, v, 1); else dfs2(to, v, 0); } } int main() { ios_base::sync_with_stdio(false), cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> cost[i]; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } dfs(1, 0); cout << dp[1][0] << " "; dfs2(1, 0, 0); cout << (int)ans.size() << "\n"; for (int v : ans) cout << v << " "; cout << "\n"; return 0; }
2,500
CPP
t=int(input()) for i in range(t): a,b=map(int,input().split()) if a!=b: if 2*min(a,b)>max(a,b): s=2*min(a,b) else: s=max(a,b) if a==b: s=2*a a=s*s print(a)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; char s[2010]; int nums[2010]; bitset<2000> transit(char s[], int &pos) { int len = strlen(s); for (int i = 0; i < (len); i++) nums[len - i - 1] = s[i] - '0'; bitset<2000> result; result.reset(); for (pos = 0; len; pos++) { result[pos] = nums[0] % 2; for (int k = len - 1, c = 0; k >= 0; k--) { int cc = nums[k] % 2; nums[k] = (nums[k] + c * 10) / 2; c = cc; } if (nums[len - 1] == 0) len--; } return result; } bitset<2000> data[2010]; bitset<2000> from[2010]; int mark[2010]; int main() { int m; scanf("%d", &m); memset(mark, -1, sizeof mark); for (int i = 0; i < m; i++) { scanf("%s", s); int pos; bitset<2000> a = transit(s, pos); bitset<2000> ans; ans.set(i); for (int k = pos - 1; k >= 0; k--) if (a.test(k)) { if (mark[k] != -1) { a = a ^ data[k]; ans = ans ^ from[k]; } else { data[k] = a; from[k] = ans; mark[k] = i; break; } } if (a.any()) puts("0"); else { vector<int> v; for (int j = 0; j < (i); j++) if (ans.test(j)) v.push_back(j); printf("%d", (int)v.size()); for (int i = 0; i < (v.size()); i++) printf(" %d", v[i]); puts(""); } } return 0; }
2,700
CPP
n = int(input()) height = 0 total = 0 # level = 1 while True: if (n-total)>=0: height +=1 for i in range(height): total += (i+1) # height +=1 else: break print(height-1)
800
PYTHON3
# takes a value of int and determines if it is divisible by two val = int(input()) if (val % 2 == 0 and val >= 4): print("Yes") else: print("no")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int t, n; vector<int> g[100100]; long long ans[100100]; bool extra[100100]; bool poss(int c, int l, int k) { int cv = 0; for (int i : g[c]) { if (i == l) continue; if (!poss(i, c, k)) return false; cv += extra[i]; } if (cv % k == 0) { extra[c] = true; return true; } if ((cv + 1) % k == 0) { extra[c] = false; return true; } return false; } int main() { scanf("%d", &t); while (t--) { scanf("%d", &n); long long ctot = 1; for (int i = 1; i < n; i++) { int n1, n2; scanf(" %d%d", &n1, &n2); g[n1].push_back(n2); g[n2].push_back(n1); ctot *= 2; ctot %= 998244353; } ans[1] = ctot; for (int i = n; i > 1; i--) { ans[i] = 0; if ((n - 1) % i) continue; for (int j = 1; j <= n; j++) extra[j] = false; bool cv = poss(1, -1, i); if (cv && extra[1]) ans[i] = 1; } for (int i = n; i >= 1; i--) { for (int j = i * 2; j <= n; j += i) ans[i] -= ans[j]; ans[i] += 998244353; ans[i] %= 998244353; } for (int i = 1; i <= n; i++) printf("%lld ", ans[i]); printf("\n"); for (int i = 1; i <= n; i++) g[i].clear(); } }
2,600
CPP
t = int(input()) def make_divisors(n): divisors = [] for i in range(1, int(n**0.5)+1): if n % i == 0: divisors.append(i) if i != n // i: divisors.append(n//i) #divisors.sort(reverse=True) return divisors for _ in range(t): n = int(input()) D = list(map(int, input().split())) D.sort() #print(D) if len(D) == 1: x = D[0]**2 else: x = D[0]*D[-1] D = set(D) D.add(1) D.add(x) L = make_divisors(x) if len(D) != len(L): print(-1) else: flag = True for i in L: if i not in D: flag = False break if flag: print(x) else: print(-1)
1,600
PYTHON3
a = int(input()) for _ in range(a): b, c = [int(i) for i in input().split()] if b-1 != c: print('YES') else: print('NO')
900
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n > 36) { cout << "-1\n"; return 0; } for (int i = 0; i < n / 2; i++) cout << '8'; if ((n % 2)) cout << '9'; cout << '\n'; return 0; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1024; int ans[maxn]; int a[64], b[64]; int n, m; int solve() { for (int i = 0; i < (n); ++i) { int ok = 0, k = -1; for (int j = 0; j < (m); ++j) if (a[j] > 0 && (i == 0 || j != ans[i - 1]) && (i < n - 1 || j != ans[0])) { if (k == -1 || (a[j] > a[k] || (a[j] == a[k] && b[j] > b[k]))) k = j; } if (k == -1) return 0; ans[i] = k; a[k]--, b[k]++; } return 1; } int main() { scanf("%d %d", &n, &m); for (int i = 0; i < (m); ++i) scanf("%d", a + i); if (solve()) { for (int i = 0; i < (n); ++i) { if (i) printf(" "); printf("%d", ans[i] + 1); } puts(""); } else printf("-1\n"); return 0; }
2,100
CPP
import os import sys from io import BytesIO, IOBase 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().rstrip("\r\n") from collections import defaultdict from math import ceil,floor,sqrt,log2,gcd,pi from heapq import heappush,heappop from fractions import Fraction from bisect import bisect_left,bisect import sys abc='abcdefghijklmnopqrstuvwxyz' ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ" l,r=input().split('|') s=input() ll=len(l) lr=len(r) ls=len(s) tot=ll+lr+ls if (ll+lr+ls)%2==1: print("Impossible") elif ll>tot//2 or lr>tot//2: print("Impossible") else: s=s[::-1] s=list(s) r=list(r) l=list(l) while s: if lr<ll: lr+=1 r.append(s.pop()) else: ll+=1 l.append(s.pop()) print("".join(l)+"|"+"".join(r))
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mxn = 2e5 + 5; int cnt[3]; void solve() { vector<long long> alice, bob, both, bad; int n; cin >> n; long long sum = 0; long long tt = 0; for (int i = 0; i < n; i++) { string s; long long x; cin >> s >> x; if (s == "00") bad.push_back(x); if (s == "01") bob.push_back(x); if (s == "10") alice.push_back(x); if (s == "11") sum += x, tt++, cnt[0]++, cnt[1]++; } sort(bob.begin(), bob.end()); sort(alice.begin(), alice.end()); sort(bad.begin(), bad.end()); while (bob.size() && alice.size()) { cnt[0]++; cnt[1]++; tt += 2; sum += (bob.back() + alice.back()); bob.pop_back(); alice.pop_back(); } vector<long long> tmp; int p = 0; if (bob.size()) tmp = bob, p = 0; else if (alice.size()) tmp = alice, p = 1; while (tmp.size() && bad.size()) { if (tmp.back() >= bad.back() && tt + 1 <= cnt[p] * 2 + 2 && tt + 1 <= cnt[1 - p] * 2) { cnt[p]++; sum += tmp.back(); tmp.pop_back(); tt++; } else if (tt + 1 <= cnt[0] * 2 && tt + 1 <= cnt[1] * 2) { sum += bad.back(); bad.pop_back(); tt++; } else break; } while (tmp.size() && tt + 1 <= cnt[p] * 2 + 2 && cnt[1 - p] * 2 >= tt + 1) { sum += tmp.back(); tmp.pop_back(); tt++; } while (bad.size() && cnt[0] * 2 >= tt + 1 && cnt[1] * 2 >= tt + 1) { sum += bad.back(); bad.pop_back(); tt++; } cout << sum << '\n'; return; } int main() { int T; T = 1; while (T--) { solve(); } return 0; }
1,500
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int req; if (n % 2 == 0) req = n / 2; else req = n / 2 + 1; map<int, int> cnt1; map<int, int> cnt2; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; cnt1[x]++; if (x != y) { cnt2[x]++; cnt2[y]++; } else cnt2[x]++; } int ans = INT_MAX; for (auto it = cnt2.begin(); it != cnt2.end(); it++) { if (it->second >= req) { ans = min(ans, max(0, req - cnt1[it->first])); } } if (ans != INT_MAX) cout << ans << endl; else cout << -1 << endl; }
1,500
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 2, MOD = 1e9 + 7; using vi = vector<int>; using vp = vector<pair<long long, long long>>; mt19937 Flash_new_51(515151); vi simple = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59}; int a[102], prime[100], dp[102][N], p[102][N]; signed main() { cin.tie(0); ios_base::sync_with_stdio(0); int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 2; i <= 60; i++) { for (int j = 0; j < 17; j++) { if (i % simple[j] == 0) { prime[i] |= (1 << j); } } } int mask = (1 << 17); for (int i = 0; i <= n; i++) { for (int j = 0; j < mask; j++) dp[i][j] = 1e9; } dp[0][0] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < mask; j++) { for (int tek = 1; tek <= 60; tek++) { int lol = prime[tek]; if (dp[i][j] == 1e9 || (j & lol)) continue; int new_mask = j | lol; if (dp[i + 1][new_mask] > dp[i][j] + abs(a[i] - tek)) { dp[i + 1][new_mask] = dp[i][j] + abs(a[i] - tek); p[i + 1][new_mask] = tek; } } } } int ans = 1e9, imax; for (int i = 0; i < mask; i++) { if (ans > dp[n][i]) { ans = dp[n][i]; imax = i; } } vector<int> ans1; while (n > 0) { ans1.push_back(p[n][imax]); int lol = prime[p[n][imax]]; imax = imax - lol; n--; } reverse(ans1.begin(), ans1.end()); for (auto to : ans1) cout << to << ' '; }
2,000
CPP
def chast(x): x = str(x) y = '0' for i in range(len(x)): if x[i] == '4' or x[i] == '7': y += x[i] return(int(y)) a,b = map(int, input().split()) c = a+1 while chast(c) != b: c += 1 print(c)
1,300
PYTHON3
n,t = input().split() n = int(n) t = int(t) s = [str(x) for x in input()] T = 1 while T <= t: i = 1 while i < len(s): if s[-i] == 'G' and s[-i - 1] == 'B': s[-i] = 'B' s[-i - 1] = 'G' i += 2 else: i += 1 T += 1 print(''.join(s))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { unsigned long long int n, k, d, i, j, x; cin >> n >> k >> d; unsigned long long int dp[n + 1][k + 1]; dp[0][1] = 0; for (i = 0; i <= n; i++) dp[i][0] = 1; for (i = 1; i <= n; i++) { unsigned long long int sum = 0; for (j = 1; j <= min(i, k); j++) { if (j == i) sum = (sum + 1) % 1000000007; else sum = (sum + dp[i - j][1]) % 1000000007; } dp[i][1] = sum % 1000000007; } for (i = 1; i <= k; i++) dp[0][i] = 0; for (j = 2; j <= k; j++) { for (i = 1; i <= n; i++) { unsigned long long int sum = 0; for (x = 1; x <= min(i, k); x++) { if (x < j) sum = (sum + dp[i - x][j]) % 1000000007; else if (x != i) sum = (sum + dp[i - x][1]) % 1000000007; else if (x == i) sum = (sum + 1) % 1000000007; } dp[i][j] = sum; } } cout << dp[n][d] << '\n'; return 0; }
1,600
CPP
import sys import math from heapq import *; input = sys.stdin.readline from functools import cmp_to_key; def pi(): return(int(input())) def pl(): return(int(input(), 16)) def ti(): return(list(map(int,input().split()))) def ts(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int,input().split())) mod = 1000000007; f = []; def fact(n,m): global f; f = [1 for i in range(n+1)]; f[0] = 1; for i in range(1,n+1): f[i] = (f[i-1]*i)%m; def fast_mod_exp(a,b,m): res = 1; while b > 0: if b & 1: res = (res*a)%m; a = (a*a)%m; b = b >> 1; return res; def inverseMod(n,m): return fast_mod_exp(n,m-2,m); def ncr(n,r,m): if n < 0 or r < 0 or r > n: return 0; if r == 0: return 1; return ((f[n]*inverseMod(f[n-r],m))%m*inverseMod(f[r],m))%m; def main(): B(); def cmp(a,b): if a[1] < b[1]: return 1; if a[1] > b[1]: return -1; if a[1] == b[1]: if a[0] < b[0]: return -1; else: return 1; def B(): [n,t1,t2,k] = ti(); a,b = [],[]; for i in range(n): [x,y] = ti(); a.append(x); b.append(y); pair = [0 for i in range(n)]; for i in range(n): pair[i] = [i,round(max(t1*a[i]*(100-k)/100+t2*b[i], t1*b[i]*(100-k)/100+t2*a[i]),2)]; pair.sort(key=cmp_to_key(cmp)); for i in range(n): print(pair[i][0]+1, pair[i][1] if len(str(pair[i][1]).split('.')[1]) == 2 else str(pair[i][1])+'0'); main();
1,200
PYTHON3
z=int(input()) a=list(map(int,input().split(" "))) b=min(a) c=max(a) for i in range (0,len(a)): if(b==a[i]): d=i+1 for j in range (len(a)-1,-1,-1): if(c==a[j]): e=j+1 if(e>d): n=e-1; p=len(a)-d print(n+p-1) elif(e<d): n=e-1; p=len(a)-d print(n+p)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; long long nwd(long long a, long long b) { if (a > b) swap(a, b); if (!a) return b; return nwd(b % a, a); } struct wymierna { long long a, b; wymierna(long long a, long long b) : a(a), b(b) { normuj(); } wymierna(long long a) : a(a), b(1) {} wymierna() {} void normuj() { if (b < 0) { b = -b; a = -a; } if (b == 0) { a = 0; } else { long long d = nwd(abs(a), b); a /= d; b /= d; } } friend wymierna operator*(const wymierna &p, const wymierna &q) { return wymierna(p.a * q.a, p.b * q.b); } friend wymierna operator/(const wymierna &p, const wymierna &q) { return wymierna(p.a * q.b, p.b * q.a); } friend wymierna operator+(const wymierna &p, const wymierna &q) { return wymierna(p.a * q.b + p.b * q.a, p.b * q.b); } friend wymierna operator-(const wymierna &p, const wymierna &q) { return wymierna(p.a * q.b - p.b * q.a, p.b * q.b); } friend bool operator<(const wymierna &p, const wymierna &q) { if (p.b == 0) return false; if (q.b == 0) return true; return 0 < p.a * q.b - p.b * q.a; } friend bool operator==(const wymierna &p, const wymierna &q) { if (p.b == 0) return q.b == 0; return p.a * q.b == p.b * q.a; } }; wymierna odwroc(wymierna p) { return wymierna(p.b, p.a); } struct point { wymierna x, y; point(wymierna x, wymierna y) : x(x), y(y) {} friend point operator*(const point &x, const wymierna &q) { return point(x.x * q, x.y * q); } friend point operator/(const point &x, const wymierna &q) { return point(x.x * odwroc(q), x.y * odwroc(q)); } friend bool operator<(const point &p, const point &q) { if (p.x == q.x) return p.y < q.y; return p.x < q.x; } friend point operator+(const point &p, const point &q) { return point(p.x + q.x, p.y + q.y); } friend point operator-(const point &p, const point &q) { return point(p.x - q.x, p.y - q.y); } }; point invert(point p) { wymierna alfa(((p.x.b) * (p.x.b)) * ((p.y.b) * (p.y.b)), ((p.x.a) * (p.x.a)) * ((p.y.b) * (p.y.b)) + ((p.x.b) * (p.x.b)) * ((p.y.a) * (p.y.a))); return p * alfa; } const long long MOD = 1000000007; long long qpot(long long a, long long b) { if (a == 0) return 0; long long res = 1; while (b) { if (b & 1) { res = (res * a) % MOD; } a = (a * a) % MOD; b /= 2; } return res; } long long licz(int ile) { if (ile < 2) return 0; return (qpot(2, ile) - 1 - ile + MOD) % MOD; } void jebaj() { int n; cin >> n; vector<point> v; for (auto i = (0); i <= ((n)-1); ++i) { int a, b, c, d; cin >> a >> b >> c >> d; point p(wymierna(a, b), wymierna(c, d)); point q = invert(p); q = q * 2; v.push_back(q); } map<point, vector<wymierna>> mapka; for (auto i = (0); i <= ((n)-1); ++i) for (auto j = (i + 1); j <= (n - 1); ++j) { point p = (v[i] + v[j]) / 2; point q = v[j] - v[i]; mapka[p].push_back(q.y / q.x); } long long res = 0; for (auto lel : mapka) { auto w = lel.second; sort((w).begin(), (w).end()); int pos = 0; long long rest = 1; while (pos < (int)((w).size())) { int act = pos; while (act < (int)((w).size()) && w[act] == w[pos]) act++; rest = rest * ((long long)(act - pos + 1)) % MOD; pos = act; } rest--; rest -= (int)((w).size()); rest += MOD; rest %= MOD; res += rest; res %= MOD; } cout << res << "\n"; } int main() { ios_base::sync_with_stdio(0); int t = 1; for (auto i = (0); i <= ((t)-1); ++i) { jebaj(); } }
3,000
CPP
n,m=map(int,input().split()) ar=(list(map(int,input().split()))) count=1 for i in range(n-1): if ar[i+1]-ar[i]<=m: count+=1 else: count=1 print(count)
800
PYTHON3
#include <iostream> #include <vector> #include <cmath> using namespace std; int main(void) { int t; cin >> t; while(t--) { string s; cin >> s; cout << s.size() << endl; } return 0; }
800
CPP
a = list(map(int, input().split())) a = sorted(a) print('YES' if a[0] + a[3] == a[1] + a[2] or a[3] == a[0] + a[1] + a[2] else 'NO')
800
PYTHON3
t = int(input()) for ti in range(t): n,k = input().split() n = int(n) k = int(k) a = input().split() a = [int(i) for i in a] b = input().split() b = [int(i) for i in b] a.sort() b.sort() change = 0 for i in range(k-1,-1,-1): if a[i] < b[n-i-1]: change = 1 break if change == 1: print(sum(a[i+1:])+sum(b[n-i-1:])) else: print(sum(a)) # if k < n: # else: # print(sum(a[k:]+b[n-k])) # try: # raise Exception # except: # print("-1") # thenos.sort(key=lambda x: x[2], reverse=True) # int(math.log(max(numbers)+1,2)) # 2**3 (power)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; vector<vector<long long int> > v; vector<pair<long long int, long long int> > ans; vector<long long int> vis; void dfs(long long int u) { vis[u] = 1; for (long long int i : v[u]) if (!vis[i]) ans.push_back({i, u}), dfs(i); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int n; cin >> n; v.resize(26); vis.resize(26); string a, b; cin >> a >> b; for (long long int i = 0; i < n; i++) { v[a[i] - 'a'].push_back(b[i] - 'a'); v[b[i] - 'a'].push_back(a[i] - 'a'); } for (long long int i = 0; i < 26; i++) if (!vis[i]) dfs(i); cout << ans.size() << endl; for (long long int i = 0; i < ans.size(); i++) cout << (char)(ans[i].first + 'a') << " " << (char)(ans[i].second + 'a') << endl; return 0; }
1,600
CPP
s=input() l=len(s) ans=min(26+97-ord(s[0]),ord(s[0])-97) for i in range(0,l-1): a=max(ord(s[i]),ord(s[i+1])) b=min(ord(s[i]),ord(s[i+1])) a=min(26+b-a,a-b) ans+=a print(ans)
800
PYTHON3
c= int(input()) for k in range(c): d = int(input()) if d % 2 == 0: print(d // 2) else: print(((d - 3) // 2)+ 1)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; pair<int, int> grid[1005][1005]; bool processed[1005][1005] = {0}; char gg[1005][1005]; void dfs(int x, int y, int px, int py) { if (grid[x][y] != make_pair(px, py)) return; processed[x][y] = 1; if (x + 1 < n && grid[x + 1][y] == make_pair(px, py) && !processed[x + 1][y]) { gg[x + 1][y] = 'U'; dfs(x + 1, y, px, py); } if (y + 1 < n && grid[x][y + 1] == make_pair(px, py) && !processed[x][y + 1]) { gg[x][y + 1] = 'L'; dfs(x, y + 1, px, py); } if (x - 1 >= 0 && grid[x - 1][y] == make_pair(px, py) && !processed[x - 1][y]) { gg[x - 1][y] = 'D'; dfs(x - 1, y, px, py); } if (y - 1 >= 0 && grid[x][y - 1] == make_pair(px, py) && !processed[x][y - 1]) { gg[x][y - 1] = 'R'; dfs(x, y - 1, px, py); } } bool single(int x, int y) { if (x + 1 < n && grid[x + 1][y] == make_pair(-2, -2)) { gg[x][y] = 'D'; return false; } if (y + 1 < n && grid[x][y + 1] == make_pair(-2, -2)) { gg[x][y] = 'R'; return false; } if (x - 1 >= 0 && grid[x - 1][y] == make_pair(-2, -2)) { gg[x][y] = 'U'; return false; } if (y - 1 >= 0 && grid[x][y - 1] == make_pair(-2, -2)) { gg[x][y] = 'L'; return false; } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int a, b; cin >> a >> b; --a; --b; grid[i][j] = {a, b}; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (processed[i][j]) continue; if (grid[i][j] != make_pair(-2, -2)) { gg[grid[i][j].first][grid[i][j].second] = 'X'; dfs(grid[i][j].first, grid[i][j].second, grid[i][j].first, grid[i][j].second); } else { if (!single(i, j)) dfs(i, j, grid[i][j].first, grid[i][j].second); } if (!processed[i][j]) { cout << "INVALID" << endl; exit(0); } } } cout << "VALID" << endl; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << gg[i][j]; } cout << endl; } }
2,000
CPP
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; const int maxn = 2 * 1e5 + 123; string s; int main() { ios_base::sync_with_stdio(0); cin >> s; int n = s.size(); int p = 0; for (int i = 0; i < n; i++) { if (s[i] - 'a' <= p) { s[i] = 'a' + p; p++; } if (p == 26) break; } if (p < 26) { cout << -1; } else cout << s; return 0; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long int n, t; cin >> t; while (t--) { cin >> n; vector<long long int> v(n); for (long long int i = 0; i < n; i++) { cin >> v[i]; } if (n == 2) { cout << 0 << endl; } else { sort(v.begin(), v.end()); long long int mn = min(v[n - 2], v[n - 1]); if (n - 1 >= mn) { cout << mn - 1 << endl; } else { cout << n - 2 << endl; } } } }
900
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); vector<int> a(n), b(n), c(n); for (int i = 0; i < (n); ++i) scanf("%d", &a[i]); for (int i = 0; i < (n); ++i) scanf("%d", &b[i]); for (int i = 0; i < (n); ++i) scanf("%d", &c[i]); long long hungry, full; for (int i = 0; i < (n); ++i) { if (i) { long long nhungry = max(hungry + b[i], full + a[i]); long long nfull = max(hungry + c[i], full + b[i]); hungry = nhungry; full = nfull; } else { hungry = a[i]; full = b[i]; } } cout << hungry << endl; return 0; }
1,800
CPP
#include <bits/stdc++.h> using namespace std; char s[1000000]; int main() { int i, j, k, f, t, r, l, m, n, p, z; int a, b, c; scanf("%d%d", &n, &m); int fl = 0; if (m % 2) { k = m / 2 + 1; fl = 1; } else k = m / 2; t = 1; l = 0; for (i = 1; i <= n; i++) { printf("%d\n", k); l++; if (fl) { k -= t++; fl = 0; } else { k += t++; fl = 1; } if (l == m) { fl = 0; if (m % 2) { k = m / 2 + 1; fl = 1; } else k = m / 2; t = 1; l = 0; } } return 0; }
1,300
CPP
import random def read_ints(): return [int(v) for v in input().split()] def main(): n, k = read_ints() l, r = 1, n shoot_range = 50 while l <= r: if r - l < shoot_range: c = random.randint(l, r) print('{} {}'.format(c, c), flush=True) ans = input() if ans == 'Yes': return l, r = max(1, l - k), min(n, r + k) else: m = (l + r) // 2 print('{} {}'.format(l, m), flush=True) ans = input() if ans == 'Yes': r = m else: l = m + 1 l, r = max(1, l - k), min(n, r + k) if __name__ == '__main__': main()
2,100
PYTHON3
N = int(input()) #arr = input() #arr = [int(x) for x in arr.split(' ')] s = input() ans = list(s) #print(ans) freq = [0,0,0] for letter in s: if letter=='0': freq[0] += 1 if letter=='1': freq[1] += 1 if letter=='2': freq[2] += 1 base = N//3 i = 0 for j in range(2,0,-1): if i!=j: if freq[i]>base and freq[j]<base: fix = min(freq[i]-base,base-freq[j]) freq[i] -= fix freq[j] += fix #print(i,j) fixed = 0 for k in range(N-1,-1,-1): if ans[k]==str(i): ans[k] = str(j) fixed += 1 if fixed==fix: break i = 1 j = 2 if i!=j: if freq[i]>base and freq[j]<base: fix = min(freq[i]-base,base-freq[j]) freq[i] -= fix freq[j] += fix #print(i,j) fixed = 0 for k in range(N-1,-1,-1): if ans[k]==str(i): ans[k] = str(j) fixed += 1 if fixed==fix: break #print(ans) i = 1 j = 0 if i!=j: if freq[i]>base and freq[j]<base: fix = min(freq[i]-base,base-freq[j]) freq[i] -= fix freq[j] += fix #print(i,j) fixed = 0 for k in range(N): if ans[k]==str(i): ans[k] = str(j) fixed += 1 if fixed==fix: break #print(ans) i = 2 for j in range(2): if i!=j: if freq[i]>base and freq[j]<base: fix = min(freq[i]-base,base-freq[j]) freq[i] -= fix freq[j] += fix #print(i,j) fixed = 0 for k in range(N): if ans[k]==str(i): ans[k] = str(j) fixed += 1 if fixed==fix: break for letter in ans: print(letter,end='')
1,500
PYTHON3
n=int(input()) l=[] r=[] for i in range(n): a=int(input()) l.append(a) if a==1: r.append(l[i]//2) elif a%2==0: r.append(l[i]//2-1) else: r.append(l[i]//2) for i in r: print(i)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; map<string, vector<string> > dir; map<string, vector<string> > file; int main() { string s; while (cin >> s) { int count = 0; int pos = 0; for (size_t i = 0; i < s.size(); i++) { if (s[i] == '\\') { pos = i; } } string file_name = s.substr(pos, s.size() - pos); string parent; int count1 = 0; for (size_t i = 0; i < s.size(); i++) { if (s[i] == '\\') { if (count1 == 1) { parent = s.substr(0, i); break; } count1++; } } for (size_t i = 3; i < s.size(); i++) { if (s[i] == '\\') { if (count >= 1) { dir[parent].push_back(s.substr(0, i)); } file[s.substr(0, i)].push_back(file_name); count++; } } } size_t mnd = 0; for (map<string, vector<string> >::const_iterator it = dir.begin(); it != dir.end(); it++) { vector<string> buf = it->second; sort(buf.begin(), buf.end()); buf.erase(unique(buf.begin(), buf.end()), buf.end()); mnd = mnd > buf.size() ? mnd : buf.size(); } int mn = 0; for (map<string, vector<string> >::const_iterator it = file.begin(); it != file.end(); it++) { mn = it->second.size() > mn ? it->second.size() : mn; } cout << mnd << " " << mn << endl; return 0; }
1,800
CPP
#include <bits/stdc++.h> using namespace std; int n, d, x, y, v, r; int main() { scanf("%d%d", &n, &d); for (int i = 2 * n - 2; i >= 0; i -= 2) { r = 4 * v | (d >> i) & 3; x = (x << 1) | (0x936c >> r) & 1; y = (y << 1) | (0x39c6 >> r) & 1; v = (0x3e6b94c1 >> 2 * r) & 3; } printf("%d %d\n", x, y); }
1,700
CPP
#include <bits/stdc++.h> using namespace std; char c; int main() { int n, e = 0, m = 0; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i], (a[i] > 0) ? e++ : 0; sort(a, a + n); cout << "1 " << a[0] << endl; if (e == 0) { cout << "2 " << a[1] << " " << a[2] << endl; cout << n - 3 << " "; for (int i = 3; i < n; i++) cout << a[i] << " "; return 0; } for (int i = 1; i < n; i++) if (a[i] == 0) m = i; cout << n - m - 1 << " "; for (int i = m + 1; i < n; i++) cout << a[i] << " "; cout << endl; cout << m << " "; for (int i = 1; i < m + 1; i++) cout << a[i] << " "; }
1,100
CPP
def main(): s = input() l, i = [], s.find('bear') while i >= 0: l.append(i) i = s.find('bear', i + 4) res = [] for shift in range(min(len(l), 2)): res.append(sum((i + 1) * (len(s) - j - 3) for i, j in zip(l, l[shift:]))) res.append(0) res.append(0) print(res[0] - res[1]) if __name__ == '__main__': main()
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; bool sortin(const pair<long long int, long long int> &e, const pair<long long int, long long int> &f) { return (e.first < f.first); } long long int i, j, k, l, m, n, c, p, q, ts, mn = 10e17; long long int a[200002], b[200002], x[200002]; int main() { { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); }; cin >> n; cin >> m; a[0] = 0; a[1] = 1; a[2] = 2; for (long long int i = 0; i <= n % 6 - 1; i++) { if (i % 2 == 0) swap(a[0], a[1]); else swap(a[1], a[2]); } cout << a[m]; }
1,000
CPP
def round(n,k,l): r=0 for i in l: if i>0 and i>=l[k-1]: r+=1 return r l=[] n,k=map(int,input().split()) l=list(map(int,input().split())) print(round(n,k,l))
800
PYTHON3
#include <bits/stdc++.h> int n, i, j, k, now, cnt; int flag; bool done[100111]; std::list<int> ans; void go(int now) { done[now] = true; ++cnt; if (now == 1) { printf("0 1"); return; } if (now & 1) if (!done[n + now - 1 >> 1]) go(n + now - 1 >> 1); else go(now - 1 >> 1); else if (!done[n + now >> 1]) go(n + now >> 1); else go(now >> 1); printf(" %d", now); } int main() { scanf("%d", &n); if (n & 1) { puts("-1"); return 0; } { memset(done, 0, sizeof done); cnt = 0; go(0); puts(""); if (cnt != n) { printf("%d %d\n", cnt, n); return 0; } } return 0; }
2,800
CPP
def gcd(a,b): if b==0: return a return gcd(b,a%b) p = int(input()) c=0 for i in range(1,p): if gcd(i,p-1)==1: c+=1 print(c)
1,400
PYTHON3
s,n=map(int,input().split()) flag=True dragons=[] for i in range(n): dragons.append(list(map(int,input().split()))) dragons=sorted(dragons,key=lambda l:l[0]) for e_d in range(len(dragons)): for j in range(1): if dragons[e_d][0]<s: s +=dragons[e_d][1] else: flag=False if flag: print('YES') else: print('NO')
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mXn = 2 * 1e5 + 10; vector<int> ad[mXn]; int dpmx[mXn], dpmn[mXn], t, cnt[mXn]; bool mark[mXn], mark2[mXn]; void lc(int v) { mark[v] = true; bool b = true; for (int i = 0; i < (int)ad[v].size(); i++) { int u = ad[v][i]; if (!mark[u]) { lc(u); b = false; cnt[v] += cnt[u]; } } if (b) cnt[v] = 1; } void dfs(int v) { mark2[v] = true; dpmx[v] = 0; for (int i = 0; i < (int)ad[v].size(); i++) { int u = ad[v][i]; if (!mark2[u]) { dfs(u); dpmx[v] = max(dpmx[v], cnt[v] - cnt[u] + dpmn[u]); dpmn[v] += dpmx[u]; } } } int main() { int n; cin >> n; for (int i = 0; i < n - 1; i++) { int x, y; cin >> x >> y; x--; y--; ad[x].push_back(y); ad[y].push_back(x); } lc(0); dfs(0); for (int i = 0; i < n; i++) { } cout << dpmx[0] + 1 << " " << cnt[0] - dpmn[0] << endl; int ans = cnt[0]; for (int i = 0; i < (int)ad[0].size(); i++) { ans = min(ans, cnt[0] - cnt[ad[0][i]] + dpmn[ad[0][i]]); } return 0; }
2,200
CPP
#include <bits/stdc++.h> using namespace std; int main() { string s; int count = 0; cin >> s; while (count < s.size()) { if (s[count] == '.') { cout << "0"; } else { if (s[count + 1] == '.') { cout << "1"; count++; } else { cout << "2"; count++; } } count++; } return 0; }
800
CPP
x,y,z=list(map(int,input().split())) r=x//z+y//z t=0 s=min(x%z,y%z) i=z-(max(x%z,y%z)%z) if i!=z and i<=s: r+=(max(x%z,y%z)+i)//z t=i print(r,t)
1,000
PYTHON3
for _ in range(int(input())): n, k = [int(s) for s in input().split()] a = sorted([int(s) for i, s in enumerate(input().split())]) b = sorted([int(s) for i, s in enumerate(input().split())], reverse=True) for i in range(k): if b[i] > a[i]: a[i] = b[i] print(sum(a))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int p[5000]; bool prime[5000]; int a[100005]; int b[100005]; int up; map<int, int> fm; map<int, int> fz; void Prime() { int i, j; memset(prime, 0, sizeof(prime)); for (i = 2; i <= 3500; i++) { if (prime[i] == 1) continue; p[up++] = i; for (j = i * i; j <= 3500; j += i) { prime[j] = 1; } } } int main() { int i, j, n, m, t, now; up = 0; Prime(); scanf("%d%d", &n, &m); for (i = 0; i < n; i++) { scanf("%d", &a[i]); t = a[i]; for (j = 0; j < up; j++) { if (t == 1) break; while (t % p[j] == 0) { fm[p[j]]++; t /= p[j]; } } if (t != 1) fm[t]++; } for (i = 0; i < m; i++) { scanf("%d", &b[i]); t = b[i]; for (j = 0; j < up; j++) { if (t == 1) break; while (t % p[j] == 0) { t /= p[j]; fz[p[j]]++; } } if (t != 1) fz[t]++; } map<int, int>::iterator it; printf("%d %d\n", n, m); for (i = 0; i < n; i++) { t = a[i]; now = 1; for (j = 0; j < up; j++) { if (t == 1) break; while (t % p[j] == 0) { if (fz[p[j]] > 0) fz[p[j]]--; else now *= p[j]; t /= p[j]; } } if (t != 1) { if (fz[t] > 0) fz[t]--; else now *= t; } printf("%d ", now); } printf("\n"); for (i = 0; i < m; i++) { t = b[i]; now = 1; for (j = 0; j < up; j++) { if (t == 1) break; while (t % p[j] == 0) { if (fm[p[j]] > 0) fm[p[j]]--; else now *= p[j]; t /= p[j]; } } if (t != 1) { if (fm[t] > 0) fm[t]--; else now *= t; } printf("%d ", now); } printf("\n"); return 0; }
1,800
CPP
a=list(map(str,input().split())) p=[0]*9 m=[0]*9 s=[0]*9 p1=[0]*9 m1=[0]*9 s1=[0]*9 p2=[0]*9 m2=[0]*9 s2=[0]*9 for i in a: if(i[1]=="p"): u=int(i[0]) p[u-1]+=1 p2[u-1]=1 elif(i[1]=="m"): u=int(i[0]) m[u-1]+=1 m2[u-1]=1 else: u=int(i[0]) s[u-1]+=1 s2[u-1]=1 for i in range(9): if(i==0): p1[i]=p2[i]+p2[i+1]+p2[i+2] m1[i]=m2[i]+m2[i+1]+m2[i+2] s1[i]=s2[i]+s2[i+1]+s2[i+2] elif(i==8): p1[i]=p2[i]+p2[i-1]+p2[i-2] m1[i]=m2[i]+m2[i-1]+m2[i-2] s1[i]=s2[i]+s2[i-1]+s2[i-2] else: p1[i]=p2[i]+p2[i+1]+p2[i-1] m1[i]=m2[i]+m2[i+1]+m2[i-1] s1[i]=s2[i]+s2[i+1]+s2[i-1] if(p.count(3) or m.count(3) or s.count(3)or p1.count(3) or m1.count(3) or s1.count(3) >=1): print("0") elif(p.count(2) or m.count(2) or s.count(2)or p1.count(2) or m1.count(2) or s1.count(2) >=1): print("1") else: print("2")
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; mt19937 gene(233); inline char GET_CHAR() { const int maxn = 131072; static char buf[maxn], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, maxn, stdin), p1 == p2) ? EOF : *p1++; } inline int getInt() { int res(0); char c = getchar(); while (c < '0') c = getchar(); while (c >= '0') { res = res * 10 + (c - '0'); c = getchar(); } return res; } inline long long fastpo(long long x, long long n, long long mod) { long long res(1); while (n) { if (n & 1) { res = res * (long long)x % mod; } x = x * (long long)x % mod; n /= 2; } return res; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } inline string itoa(int x, int width = 0) { string res; if (x == 0) res.push_back('0'); while (x) { res.push_back('0' + x % 10); x /= 10; } while ((int)res.size() < width) res.push_back('0'); reverse(res.begin(), res.end()); return res; } const int _B = 131072; char buf[_B]; int _bl = 0; inline void flush() { fwrite(buf, 1, _bl, stdout); _bl = 0; } __inline void _putchar(char c) { if (_bl == _B) flush(); buf[_bl++] = c; } inline void print(long long x, char c) { static char tmp[20]; int l = 0; if (!x) tmp[l++] = '0'; else { while (x) { tmp[l++] = x % 10 + '0'; x /= 10; } } for (int i = l - 1; i >= 0; i--) _putchar(tmp[i]); _putchar(c); } struct P { double x, y; }; const int N = 300033; const int LOG = 20; const int mod = 1e9 + 7; const int inf = 1e9 + 7; int n, m, t, X1, Y1, X2, Y2, q; long long a[500011], c[500011], f[1000001]; char str[500011]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; long long calc(long long n) { long long v = 0; for (int i = 0; n; n /= 10, i++) if (n % 10 == 3) v += a[i]; else if (n % 10 == 6) v += a[i] * 2; else if (n % 10 == 9) v += a[i] * 3; return v; } int main() { scanf("%d", &m); for (int i = 0; i <= 5; i++) scanf("%lld", &a[i]); memset(f, 128, sizeof(f)); f[0] = 0; if (m) { for (int i = 1; i <= 6; i++) { int res = 1; for (int j = 1; j < i; j++) res *= 10; for (int j = 999999; j >= 0; j--) if (f[j] >= 0) { if (j + 3 * res < 1000000) f[j + 3 * res] = max(f[j + 3 * res], f[j] + a[i - 1]); if (j + 6 * res < 1000000) f[j + 6 * res] = max(f[j + 6 * res], f[j] + 2 * a[i - 1]); if (j + 9 * res < 1000000) f[j + 9 * res] = max(f[j + 9 * res], f[j] + 3 * a[i - 1]); for (int k = 1; k < 10; k++) if (j + k * res < 1000000) f[j + k * res] = max(f[j + k * res], f[j]); } if (m > 1) for (int j = 999999; j >= 0; j--) if (f[j] >= 0) { if (j + 3 * res < 1000000) f[j + 3 * res] = max(f[j + 3 * res], f[j] + a[i - 1]); if (j + 6 * res < 1000000) f[j + 6 * res] = max(f[j + 6 * res], f[j] + 2 * a[i - 1]); if (j + 9 * res < 1000000) f[j + 9 * res] = max(f[j + 9 * res], f[j] + 3 * a[i - 1]); } int x = m - 2; if (x <= 0) continue; for (int j = 0; j < 20; j++) if (x < (1 << j)) break; else { x -= (1 << j); long long s = 1LL * ((1 << j)) * res * 9, v = 1LL * ((1 << j)) * 3 * a[i - 1]; for (int k = 999999; k >= 0; --k) if (f[k] >= 0 && k + s < 1000000) f[k + s] = max(f[k + s], f[k] + v); } long long s = 1LL * x * res * 9, v = 1LL * x * 3 * a[i - 1]; for (int k = 999999; k >= 0; --k) if (f[k] >= 0 && k + s < 1000000) f[k + s] = max(f[k + s], f[k] + v); } } scanf("%d", &q); for (; q--;) { scanf("%d", &n); printf("%lld\n", f[n]); } }
3,000
CPP
t = int(input()) while t != 0: n = input() print(len(set(input().split()))) t -= 1
800
PYTHON3
t = int(input()) for _ in range(t): n = [x for x in range (1,int(input())+1)] n.reverse() print(*n)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int dp[15][15], num[15], n; int cur[15], bad[15], f; vector<int> ans[11]; void output() { int i, j; for (i = 0; i < n; i++) ans[i].clear(); for (i = 0; i < n; i++) ans[cur[i]].push_back(i + 1); for (i = 0;;) { printf("{%d", ans[i][0]); for (j = 1; j < ans[i].size(); j++) printf(",%d", ans[i][j]); putchar('}'); i++; if (!ans[i].empty()) putchar(','); else break; } putchar('\n'); f = 1; } void rec_solve(int depth) { if (depth == n) { output(); return; } int i, mx = min(1, depth), j, last, lim1, lim2, d; for (i = 0; i < depth; i++) if (cur[i] + 1 > mx) { mx = cur[i] + 1; } if (bad[depth] != -1) rec_solve(depth + 1); if (f) { lim1 = mx; lim2 = -1; d = -1; } else { lim1 = 0; lim2 = mx + 1; d = 1; } for (i = lim1; i != lim2; i += d) if (bad[depth] != i) { cur[depth] = i; rec_solve(depth + 1); last = i; } bad[depth] = last; } int main() { int i, j; cin >> n; dp[1][1] = 1; for (i = 2; i <= n; i++) for (j = 1; j <= i; j++) { dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j] * j; } for (i = 1; i <= n; i++) for (j = 1; j <= i; j++) num[i] += dp[i][j]; printf("%d\n", num[n]); for (int i = 0; i < n; i++) bad[i] = cur[i] = -1; rec_solve(0); }
2,300
CPP
from sys import * input = stdin.readline n = int(input()) nxt = 1 curr = [] ordered = set() cnt=0 for i in range(2*n): cmd= input() if cmd[:3] == 'add': _,val = cmd.split() curr.append(int(val)) else: if curr: if curr[-1]==nxt: curr.pop() else: cnt+=1 curr=[] nxt+=1 print(cnt)
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 202222; int n, s; int a[N]; priority_queue<pair<int, int> > q; vector<pair<int, int> > ans, qq; int main() { cin >> n >> s; for (int i = (1); i <= (n); i++) { scanf("%d", a + i); if (a[i]) q.push(pair<int, int>(a[i], i)); } while (!q.empty()) { pair<int, int> u = q.top(); q.pop(); qq.clear(); for (int i = (1); i <= (u.first); i++) { if (q.empty()) { printf("No\n"); return 0; } pair<int, int> v = q.top(); q.pop(); ans.push_back(pair<int, int>(v.second, u.second)); if (v.first > 1) qq.push_back(pair<int, int>(v.first - 1, v.second)); } for (pair<int, int> tmp : qq) q.push(tmp); } cout << "Yes\n"; printf("%d\n", ans.size()); for (pair<int, int> tmp : ans) printf("%d %d\n", tmp.first, tmp.second); return 0; }
2,200
CPP
#include <bits/stdc++.h> using namespace std; bool a[2001][2001]; int main() { long long int n, e = 0; cin >> n; long long int b[n], c[n]; for (long long int i = 0; i < n; ++i) { cin >> b[i] >> c[i]; a[b[i] + 1000][c[i] + 1000] = true; } for (long long int i = 0; i < n; ++i) { for (long long int j = i + 1; j < n; ++j) { double w = (b[i] + b[j]) / 2.0; double v = (c[i] + c[j]) / 2.0; if (w == floor(w) && v == floor(v)) { if (a[(long long int)w + 1000][(long long int)v + 1000]) { ++e; } } } } cout << e << endl; }
1,300
CPP
t=int(input()) import math for i in range(0,t): n=int(input()) s=input() if s==s[::-1]: if s.count('0')==1: print('BOB') elif s.count('0')%2==1: print('ALICE') elif s.count('0')%2==0: print('BOB') else: if len(s)%2==1 and s[len(s)//2]=='0' and s.count('0')==2: print('DRAW') else: print('ALICE')
1,900
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int t; cin >> t; while (t--) { long long n, k; cin >> n >> k; vector<long long> x(n); vector<long long> y(n); for (int i = 0; i < n; i++) cin >> x[i]; for (int i = 0; i < n; i++) cin >> y[i]; sort(x.begin(), x.end()); vector<long long> max_val(n + 1, 0); for (int i = n - 1; i >= 0; i--) { long long greater = int(upper_bound(x.begin() + i, x.end(), x[i] + k) - x.begin()) - i; max_val[i] = max(greater, max_val[i + 1]); } long long ans = 0; for (int i = 0; i < n; i++) { int new_idx = int(upper_bound(x.begin() + i, x.end(), x[i] + k) - x.begin()); long long add = (new_idx - i) + max_val[new_idx]; ans = max(ans, add); } cout << ans << endl; } }
1,800
CPP
n = int(input()) l = list(input()) g = [] for i in range(2, n+1): if n % i == 0: g.append(i) for j in g: c = l[:j] c = c[::-1] l[:j] = c print(''.join(l))
900
PYTHON3
import operator def gen(s, t, c): for a, b, in zip(s, t): if c and a != b: c -= 1 yield a else: yield b s = input() t = input() count = len(s) - sum(map(operator.eq, s, t)) if count & 1 == 0: print(str.join("", gen(s, t, count // 2))) else: print("impossible")
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m, q; int dp[1000005], nr[1000005], last[1000005], rez, prevv[17][200005]; int a[1000005], perm[1000005], poz[1000005], arb[1000005]; void insert(int now, int st, int dr, int poz, int val) { int mij = (st + dr) / 2; if (st == dr) { arb[now] = val; return; } if (poz <= mij) insert(now * 2, st, mij, poz, val); else insert(now * 2 + 1, mij + 1, dr, poz, val); arb[now] = max(arb[now * 2], arb[now * 2 + 1]); } void query(int now, int st, int dr, int le, int ri) { int mij = (st + dr) / 2; if (le > dr || ri < st) return; if (le <= st && dr <= ri) rez = max(rez, arb[now]); else { query(now * 2, st, mij, le, ri); query(now * 2 + 1, mij + 1, dr, le, ri); } } int anterior(int poz, int mutari) { int rez = poz; for (int i = 0; i < 17; i++) if ((1 << i) & mutari) rez = prevv[i][rez]; return rez; } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL); string s; cin >> n >> m >> q; for (int i = 1; i <= n; i++) cin >> perm[i], poz[perm[i]] = i; perm[0] = perm[n]; for (int i = 1; i <= m; i++) { cin >> a[i]; int b = poz[a[i]]; b = last[perm[b - 1]]; if (b) { prevv[0][i] = b; nr[i] = nr[b] + 1; } else { nr[i] = 1; } last[a[i]] = i; } for (int i = 1; i < 17; i++) for (int j = 1; j <= m; j++) prevv[i][j] = prevv[i - 1][prevv[i - 1][j]]; for (int i = 1; i <= m; i++) { if (nr[i] >= n) dp[i] = anterior(i, n - 1), insert(1, 1, m, i, dp[i]); } for (int i = 1; i <= q; i++) { int c, d; cin >> c >> d; rez = 0; query(1, 1, m, c, d); if (rez >= c) cout << 1; else cout << 0; } return 0; }
2,000
CPP
a,b,x=map(int,input().split()) c=[];m=a;n=b for i in range(x): if n>m: if i%2==0: c.append('1');b-=1 else: c.append('0');a-=1 else: if i%2==0: c.append('0');a-=1 else: c.append('1');b-=1 if c[-1]=='0': for i in range(a): c.append('0') for i in range(b): c.append('1') else: for i in range(b): c.append('1') for i in range(a): c.append('0') print(''.join(c))
1,300
PYTHON3
#include<bits/stdc++.h> #define MOD 1000000007 #define PI 3.1415926535897932384626 #define pb push_back #define bs binary_search #define eb emplace_back #define ef emplace_front #define it iterator #define us unordered_set #define deb(x) cout<<#x<<"="<<x<<endl #define deb2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<endl typedef long long ll; typedef unsigned long long int ull; using namespace std; void solve(){ ll n; cin>>n; ll a[n]; for(ll i=0;i<n;i++){ cin>>a[i]; } sort(a,a+n); set<ll>b; for(ll i=0;i<n;i++){ for(ll j=i;j<n;j++){ ll diff=a[j]-a[i]; b.insert(diff); } } cout<<b.size()-1<<endl; return ; } int main(){ ll t; cin>>t; while(t--){ solve();} return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; int main() { do { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); } while (false); int n; cin >> n; vector<vector<pair<int, int> > > adj(n); for (int i = 1; i < n; i++) { int u, v, w; cin >> u >> v >> w; adj[--u].emplace_back(--v, w); adj[v].emplace_back(u, w); } int root = -1; for (int i = 0; i < n; i++) { if (adj[i].size() == 2) { cout << "NO" << endl; return 0; } if (adj[i].size() == 1) { root = i; } } vector<pair<pair<int, int>, int> > ans; cout << "YES" << endl; function<int(int, int, int)> dfs = [&](int u, int fa, int faw) { int sum = 0, a = -1, b = -1; for (auto e : adj[u]) { int v = e.first, w = e.second; if (v != fa) { sum += w; if (a == -1) { a = dfs(v, u, w); } else { b = dfs(v, u, w); } } } if (u != root) { if (adj[u].size() == 1) { a = u; ans.emplace_back(make_pair(u, root), faw); } else { ans.emplace_back(make_pair(a, root), (faw - sum) / 2); ans.emplace_back(make_pair(b, root), (faw - sum) / 2); ans.emplace_back(make_pair(a, b), -(faw - sum) / 2); } } return a; }; dfs(root, -1, 0); cout << (int)ans.size() << endl; for (auto e : ans) { cout << e.first.first + 1 << " " << e.first.second + 1 << " " << e.second << "\n"; } return 0; }
1,600
CPP
import math if __name__ == '__main__': t = int(input()) for numtests in range(t): n = int(input()) d = math.floor(math.log(n, 2)) if n == 2: print("1") print("0") elif n == 3: print("1") print("1") else: print(str(d)) for i in range(d-2): print(str(int(2**(i))), end = " ") if n < int(3*(2**(d-1))): print(str(math.floor((n+1-2**d)/2)), end = " ") print(str((n+1)%2)) else: print(str(int(2**(d-2))), end = " ") print(str(int(n+1-3*(2**(d-1)))))
1,900
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; long long a[100005], b[100005], cres[100005], desc[100005]; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; b[i] = a[i]; if (a[i] <= a[i - 1]) cres[i] += a[i - 1] - a[i] + 1, a[i] = a[i - 1] + 1; cres[i] += cres[i - 1]; } for (int i = n - 1; i >= 1; i--) { if (b[i] <= b[i + 1]) desc[i] += b[i + 1] - b[i] + 1, b[i] = b[i + 1] + 1; desc[i] += desc[i + 1]; } long long best = cres[0] + desc[1]; for (int i = 1; i <= n; i++) { int add = 0; if (a[i] <= b[i + 1]) add = b[i + 1] + 1 - a[i]; best = min(best, cres[i] + add + desc[i + 1]); } cout << best; return 0; }
1,600
CPP
import sys,os,io input = sys.stdin.readline sys.setrecursionlimit(10**6) #input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline T = int(input()) def dfs(start): stack = [start] visited = [False]*N visited[start] = True cnts = [-1]*N while stack: v = stack[-1] ans = cnt = marker = 0 for u in edge[v]: if visited[u]==False: marker = 1 visited[u]=True stack.append(u) elif cnts[u]>-1: ans = max(ans, cnts[u]) cnt += cnts[u] if marker==0: cnts[v] = cnt+1 ans = max(ans, N-1-cnt) if ans==N//2: lis.append(v) stack.pop() return for t in range(T): N = int(input()) edge = [[] for _ in range(N)] for i in range(N-1): a,b = map(int, input().split()) if i==0: edges = [a,b] edge[a-1].append(b-1) edge[b-1].append(a-1) if N%2==1: print(*edges) print(*edges) else: lis = [] dfs(0) if len(lis)<=1: print(*edges) print(*edges) else: for l in edge[lis[0]]: if l!=lis[1]: print(lis[0]+1,l+1) print(lis[1]+1,l+1) break
1,700
PYTHON3
""" Author : Pavan Chikkanna R """ from collections import defaultdict def solve(): n,k=list(map(int,input().split())) arr=list(map(int,input().split())) h=defaultdict(int) mx=float('-inf') for a in arr: if a%k==0: continue h[k-a%k]+=1 mx=max(mx,h[k-a%k]) ans=0 for i in h.items(): if i[1]==mx: ans=max(ans,k*(mx-1)+i[0]+1) return ans def main(): t=int(input()) while t: print(solve()) t-=1 if __name__ == "__main__": main()
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { if (0 == 1) { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } int x, y, x0, y0; string s; cin >> x >> y >> x0 >> y0; getline(cin, s); char t[110000]; scanf("%s", t); int n = strlen(t); int kt[511][511]; int xx, yy, dem; xx = x0; yy = y0; dem = 0; for (int i = 0; i < n; i++) { if (kt[xx][yy] == 0) { cout << 1 << ' '; kt[xx][yy] = 1; dem += 1; } else { cout << 0 << ' '; } if (t[i] == 'L') { if (yy > 1) yy -= 1; } if (t[i] == 'R') { if (yy < y) yy += 1; } if (t[i] == 'U') { if (xx > 1) xx -= 1; } if (t[i] == 'D') { if (xx < x) xx += 1; } } int res = 0; if (kt[xx][yy] == 0) { res += 1; dem += 1; } res += x * y - dem; cout << res; return 0; }
1,600
CPP
l, r = map(int, input().split()) if l&1: l += 1 if l+2 > r: print("-1") else: print(l, l+1, l+2)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; string s[45]; int main() { int n, p; scanf("%d%d", &n, &p); for (int i = 0; i < n; ++i) cin >> s[i]; reverse(s, s + n); long long ans = 0; long long k = 0; for (int i = 0; i < n; ++i) { k *= 2; if (s[i] == "halfplus") k++; ans += (k * p) / 2; } cout << ans; }
1,200
CPP
n,k=map(int,input().split()) m=n//2 if(n%2==1): m+=1 if(k<=m): print(k*2-1) else: print((k-m)*2)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; t = 1; while (t--) { long long n, b, a; cin >> n >> b >> a; long long ini = a; vector<long long> s(n); for (long long i = 0; i < n; i++) cin >> s[i]; long long cnt = 0; for (long long i = 0; i < n; i++) { if (a == 0 && b == 0) break; if (s[i] == 1) { if (b > 0 && a < ini) { b--; a++; } else a--; } else { if (a > 0) a--; else b--; } cnt++; } cout << cnt << "\n"; } return 0; }
1,500
CPP
n, m = list(map(int, input().split())) palin = "" d = dict() for i in range(n): tmp = input() p = True if(palin == ""): k = m-1 for j in range(int(m/2)): if(tmp[j] != tmp[k]): p = False k -= 1 if(p): palin = tmp if(tmp not in d.keys()): aux = tmp[::-1] if(aux in d.keys()): d[aux] += 1 else: d[tmp] = 1 resp = palin for key in d: if(d[key] == 2): resp = key + resp + key[::-1] print(len(resp)) print(resp)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; ; long long res = 0; long long x, hh, h; cin >> hh; ; multiset<long long> ms; bool stop = false; for (int i = 0; i < n; i++) { h = hh; cin >> x; if (stop) continue; ms.insert(x); long long l = 0; for (multiset<long long>::reverse_iterator it = ms.rbegin(); it != ms.rend(); it++) { long long u = *it; if (u > h) { stop = true; } if (stop) break; if (l == 0) { l = u; } else { h -= l; l = 0; } } if (!stop) if (!stop) res++; } cout << res; return 0; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; struct data { int val; int col; vector<int> vec; } arr[100005]; void connect(int a, int b) { arr[a].vec.push_back(b); arr[b].vec.push_back(a); } int goal[100005]; vector<int> ans; void dfs(int n, int fl, int flip, int flip1) { arr[n].col = 1; if (fl % 2 == 0) { if (flip % 2 == 0) { if (arr[n].val != goal[n]) { if (arr[n].val == 0) arr[n].val = 1; else arr[n].val = 0; ans.push_back(n); flip++; } else { } } else { if (arr[n].val == 0) arr[n].val = 1; else arr[n].val = 0; if (arr[n].val != goal[n]) { if (arr[n].val == 0) arr[n].val = 1; else arr[n].val = 0; ans.push_back(n); flip++; } } } else { if (flip1 % 2 == 0) { if (arr[n].val != goal[n]) { if (arr[n].val == 0) arr[n].val = 1; else arr[n].val = 0; ans.push_back(n); flip1++; } } else { if (arr[n].val == 0) arr[n].val = 1; else arr[n].val = 0; if (arr[n].val != goal[n]) { if (arr[n].val == 0) arr[n].val = 1; else arr[n].val = 0; ans.push_back(n); flip1++; } } } fl++; for (int i = 0; i < arr[n].vec.size(); i++) { if (arr[arr[n].vec[i]].col == 0) dfs(arr[n].vec[i], fl, flip, flip1); } return; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; for (int i = 1; i < n; i++) { int aa, bb; cin >> aa >> bb; connect(aa, bb); } for (int i = 1; i <= n; i++) { int v; cin >> v; arr[i].val = v; arr[i].col = 0; } for (int i = 1; i <= n; i++) { cin >> goal[i]; } dfs(1, 0, 0, 0); int si = ans.size(); cout << si << endl; for (int i = 0; i < si; i++) { cout << ans[i] << endl; } }
1,300
CPP
#include <bits/stdc++.h> using namespace std; long long int a1[100], a2[100]; map<long long int, long long int> mymap; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long int n, m; cin >> n >> m; for (int i = 0; i < n; i++) cin >> a1[i]; for (int i = 0; i < m; i++) cin >> a2[i]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { long long int val = a1[i] * a2[j]; mymap[val]++; } } long long int ans = 9e18; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { long long int val = a1[i] * a2[j]; mymap[val]--; } long long int pp = -9e18; for (map<long long int, long long int>::iterator it = mymap.begin(); it != mymap.end(); it++) { if (it->second != 0) { pp = max(it->first, pp); } } ans = min(ans, pp); for (int j = 0; j < m; j++) { long long int val = a1[i] * a2[j]; mymap[val]++; } } cout << ans << endl; return 0; }
1,400
CPP
n=int(input()) s=input() c=0 r='' if(n==1): print(s) else: for i in range(n-1): if(s[0]=='0'): c=0 elif(s[i]=='1'): c=c+1 elif(s[i-1]=='1' and s[i]=='0'): r=r+str(c) c=0 elif(s[i-1]=='0' and s[i]=='0'): r=r+'0' c=0 if(s[-1]=='1' and s[-2]=='0'): r=r+'1' elif(s[-1]=='1' and s[-2]=='1'): c=c+1 r=r+str(c) elif(s[-1]=='0' and s[-2]=='0'): r=r+'00' elif(s[-1]=='0' and s[-2]=='1'): r=r+str(c)+'0' print(r)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; long long power(int num) { long long res = 1; for (int i = 0; i < num; i++) { res *= 2; } return res; } int main() { ios::sync_with_stdio(false); long long S, X, a; cin >> S >> X; int num = 0; a = X; while (a > 0) { if ((a % 2) == 1) num++; a /= 2; } long long res = power(num); if (S < X) { cout << 0 << endl; return 0; } if (((S - X) % 2) == 1) { cout << 0 << endl; return 0; } long long rem = (S - X) / 2; if (rem == 0) { long long zero = 0; cout << max(zero, res - 2) << endl; return 0; } a = X; while (a > 0 || rem > 0) { if ((a % 2) == 1 && (rem % 2) == 1) { cout << 0 << endl; return 0; } a /= 2; rem /= 2; } cout << res << endl; return 0; }
1,700
CPP
def isprime(p): if p<2 : return False else: for scan in range(2,p): if p%scan ==0: return False return True n = int(input()) found = False if n < 12: print(-1) else: for i in range(2,n-3): if not found: if not isprime(n-i): if not isprime( i ) : print(n-i ,i); found=True
800
PYTHON3