solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
#include<bits/stdc++.h> using namespace std; int main(){ int t; cin>>t; while(t--){ int n,i,val=1; cin>>n; cout<<n<<" "; for(i=1;i<n;i++){ cout<<val<<" "; val++; } cout<<endl; } return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; inline int in() { int N = 0; register char c = getchar_unlocked(); while (c < 48 || c > 57) { c = getchar_unlocked(); } while (c > 47 && c < 58) { N = (N << 3) + (N << 1) + (c - 48); c = getchar_unlocked(); } return N; } inline long long int inl() { long long int N = 0; register char c = getchar_unlocked(); while (c < 48 || c > 57) { c = getchar_unlocked(); } while (c > 47 && c < 58) { N = (N << 3) + (N << 1) + (c - 48); c = getchar_unlocked(); } return N; } inline int inp() { int N = 0, sign = 1; register char c = getchar_unlocked(); while (c < 48 || c > 57) { if (c == '-') sign = 0; c = getchar_unlocked(); } while (c > 47 && c < 58) { N = (N << 3) + (N << 1) + (c - 48); c = getchar_unlocked(); } return (sign ? N : (-N)); } inline long long int inpl() { long long int N = 0, sign = 1; register char c = getchar_unlocked(); while (c < 48 || c > 57) { if (c == '-') sign = 0; c = getchar_unlocked(); } while (c > 47 && c < 58) { N = (N << 3) + (N << 1) + (c - 48); c = getchar_unlocked(); } return (sign ? N : (-N)); } inline bool inb() { char c = getchar_unlocked(); while (c < 48 || c > 57) { c = getchar_unlocked(); } return (c == '0' ? 0 : 1); } inline long long int ModP(long long int b, long long int e) { b %= 1000000007; long long int r = 1; while (e > 0) { if (e & 1) r = (r * b) % 1000000007; b = (b * b) % 1000000007; e >>= 1; } return r; } bool pri[10] = {1, 1, 0, 0}; vector<int> prm; inline void prime_init() { int i, j; for (i = 3; i * i < 10; i += 2) { if (!pri[i]) { for (j = i * i; j <= 10; j += 2 * i) { pri[j] = 1; } } } } inline void prime_asgn() { int i; prm.push_back(2); for (i = 3; i < 10; i += 2) if (!pri[i]) prm.push_back(i); } inline bool prime_check(long long int i) { if (i == 1) return 0; if (i < 4) return 1; if (i & 1) { if (i < 10) { if (!pri[i]) return 1; else return 0; } else { int j, x; for (j = 1; ((x = prm[j]) * prm[j]) <= i; j++) { if (!(i % x)) return 0; } return 1; } } else return 0; } inline bool rngprm_check(int i) { if (i == 1) return 0; if (i < 4) return 1; if (i & 1) { if (!pri[i]) return 1; else return 0; } else return 0; } inline int npf(long long int s) { int j, x; int c; for (c = j = 0; ((x = prm[j]) * prm[j]) <= s; j++) { if (!(s % x)) { c++; while (!(s % x)) { s /= x; } } } if (s > 1) c++; return (c); } inline int nf(long long int s) { if (s == 1) return 1; int j, x, f = 1; int c = 1; for (j = 0; ((x = prm[j]) * prm[j]) <= s; c = 1, j++) { if (!(s % x)) { while (!(s % x)) { s /= x; c++; } f = f * c; } } if (s > 1) { f = f << 1; } return (f); } vector<pair<int, int> > nfa; inline void nfx(long long int s) { nfa.clear(); if (s == 1) return; int j, x, c = 0; for (j = 0; ((x = prm[j]) * prm[j]) <= s; c = 0, j++) { if (!(s % x)) { while (!(s % x)) { s /= x; c++; } nfa.push_back(make_pair((x), (c))); } } if (s > 1) { nfa.push_back(make_pair((s), (1))); } } vector<long long int> factors; inline void factr() { factors.clear(); factors.push_back(1); long long int i, y, p, q, j; for (pair<int, int> x : nfa) { y = factors.size(); p = x.first; q = x.second; for (j = 0; j < q; j++) { for (i = 0; i < y; i++) { factors.push_back(factors[i] * p); } p *= x.first; } } } long long int fc[10]; inline long long int InverseEuler(long long int n) { return ModP(n, 1000000007 - 2); } inline long long int nCr(int n, int r) { if (r > n) return 0; return (fc[n] * ((InverseEuler(fc[r]) * InverseEuler(fc[n - r])) % 1000000007)) % 1000000007; } inline void facs() { fc[0] = fc[1] = 1; for (long long int i = 2; i < 10; i++) { fc[i] = (i * fc[i - 1]) % 1000000007; } } int main() { long long int n, p, i, x, y; cin >> n >> p; for (i = 1; i < 1000000; i++) { x = n - (i * p); if (x > 0) { if (__builtin_popcount(x) <= i && x >= i) { cout << i; return 0; } } } cout << -1; return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; int movei[] = {0, 0, 1, -1, 1, 1, -1, -1}; int movej[] = {1, -1, 0, 0, 1, -1, 1, -1}; const int MXN = 1e6 + 7; const int oo = 1e6 + 1; int pre1[MXN]; int n, k; int dp[MXN][2]; string str; int solve(int idx, bool started) { if (idx >= n + 1) return 0; int &ret = dp[idx][started]; if (ret != -1) return ret; ret = oo; int si = idx - 1; int seg = pre1[min(n, idx + k - 1)] - pre1[idx]; ret = (str[si] == '0') + seg + solve(idx + k, true); if (!started) ret = min(ret, (str[si] == '1') + solve(idx + 1, false)); ret = min(ret, pre1[n] - pre1[idx - 1]); return ret; } void init() { for (int i = 0; i <= n; i++) { for (int j = 0; j < 2; j++) { dp[i][j] = -1; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t; cin >> t; while (t--) { cin >> n >> k; cin >> str; for (int i = 1; i <= n; i++) pre1[i] = pre1[i - 1] + (str[i - 1] == '1'); init(); cout << solve(1, false) << '\n'; } }
1,900
CPP
def mi(): return map(int, input().split()) def fun(n): s = 0 while n: s+=n%10 n//=10 return s n,m = mi() from math import ceil b = pow(10,1+1*ceil(n//3)) a = int('2'+'3'*ceil(n//3)) print(a) print (b-a)
1,200
PYTHON3
from math import ceil import sys input=sys.stdin.readline t=int(input()) #t=1 for j in range(t): #temp1=input() #n,k=map(int,input().split()) #n=int(input()) #a=[int(x) for x in input().split()] #s=input().strip() #t=list(input().strip()) temp=input().split() n=list(temp[0]) n=[int(x) for x in n] k=int(temp[1]) if k==1: temp=n[0] le=len(n) for i in range(le-1): if n[i+1]>n[i]: temp+=1 break elif n[i+1]<n[i]: break for i in range(le): print(temp,end="") print() else: temp1=n[0] temp2=-1 ind=-1 le=len(n) for i in range(le-1): if n[i+1]!=n[i]: temp2=n[i+1] ind = i+1 break if temp2==-1: for i in n: print(i,end="") print() continue inde=-1 for i in range(ind,le): if n[i]!= temp1 and n[i]!=temp2: inde =i break if inde==-1: for i in n: print(i,end="") print() continue big = max(temp1,temp2) small=min(temp1,temp2) if n[inde]<small: for i in range(inde,le): n[i]=small elif n[inde]<big: for i in range(inde,le): n[i]=small n[inde]=big else: if n[inde-1]==small: if n[:inde].count(small)==1: temp=n[:inde].index(small) small=small+1 n[temp]=small if small==big: small=0 for i in range(temp+1,le): n[i]=small else: for i in range(inde,le): n[i]=small n[inde-1]=big else: temp=-1 for i in range(inde): if n[i]==small: temp=i flag=True if n[0]==small: flag=False for i in range(temp): if n[i]!=small: flag=True break if flag: if n[:inde].count(small)==1: temp=n[:inde].index(small) small=small+1 n[temp]=small if small==big: small=0 for i in range(temp+1,le): n[i]=small else: n[temp]=big for i in range(temp+1,le): n[i]=small else: n[temp+1]=big+1 for i in range(temp+2,le): n[i]=small for i in range(le): print(n[i],end="") print()
1,900
PYTHON3
print(sum(map(int,input().split()))) x=3
1,500
PYTHON3
#include <bits/stdc++.h> int N, K; char S[505][505]; int sum[505][505], trans[505][505]; int par[250050], sz[250050], cnt[250050], all, ans; int find(int x) { if (par[x] != x) par[x] = find(par[x]); return par[x]; } void in(int g) { g = find(g); if (++cnt[g] == 1) { all += sz[g]; } } void out(int g) { g = find(g); if (--cnt[g] == 0) { all -= sz[g]; } } int main() { scanf("%d %d", &N, &K); for (int i = 1; i <= N; i++) { scanf("%s", S[i] + 1); for (int j = 1; j <= N; j++) { trans[i][j] = (i - 1) * N + j; par[trans[i][j]] = trans[i][j]; if (S[i][j] == '.') { sum[i][j] = 1; sz[trans[i][j]] = 1; } sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1]; } } int dx[2] = {1, 0}; int dy[2] = {0, 1}; for (int k = 0; k < 2; k++) { for (int i = 1; i <= N - dx[k]; i++) for (int j = 1; j <= N - dy[k]; j++) { if (S[i][j] == '.' && S[i + dx[k]][j + dy[k]] == '.') { int x = find(trans[i][j]), y = find(trans[i + dx[k]][j + dy[k]]); if (x != y) { par[x] = y; sz[y] += sz[x]; } } } } for (int i = K; i <= N; i++) { for (int k = 0; k < K; k++) { in(trans[i - k][0]); in(trans[i - k][K + 1]); in(trans[i - K][k + 1]); in(trans[i + 1][k + 1]); } for (int k = 0; k < K; k++) for (int l = 1; l <= K; l++) in(trans[i - k][l]); for (int j = K; j <= N; j++) { int now = all - (sum[i][j] - sum[i - K][j] - sum[i][j - K] + sum[i - K][j - K]) + K * K; if (ans < now) ans = now; for (int k = 0; k < K; k++) { out(trans[i - k][j - K]); in(trans[i - k][j + 1 + 1]); } out(trans[i - K][j - K + 1]); out(trans[i + 1][j - K + 1]); in(trans[i - K][j + 1]); in(trans[i + 1][j + 1]); } for (int k = 0; k < K; k++) { out(trans[i - k][N - K + 1]); out(trans[i - k][N + 2]); out(trans[i - K][N - K + k + 2]); out(trans[i + 1][N - K + k + 2]); } for (int k = 0; k < K; k++) for (int l = 1; l <= K; l++) out(trans[i - k][N - K + l + 1]); } printf("%d\n", ans); return 0; }
2,400
CPP
T=int(input()) for _ in range(T): N=int(input()) A=list(map(int,input().split())) if N==1: ans=0 elif N==2: ans=0 else: res=0 for i in range(1,N-1): if (A[i-1]-A[i])*(A[i]-A[i+1])<0: res+=1 ans=res for x in range(1,N-1): L=x-2 if 1<x else 0 R=x+2 if x<N-1 else N-1 tmp=A[x] li_original=A[L:R+1] A[x]=A[x-1] li_left=A[L:R+1] A[x]=A[x+1] li_right=A[L:R+1] A[x]=tmp res0,res1,res2=0,0,0 for i in range(1,len(li_original)-1): if (li_original[i-1]-li_original[i])*(li_original[i]-li_original[i+1])<0: res0+=1 if (li_right[i-1]-li_right[i])*(li_right[i]-li_right[i+1])<0: res1+=1 if (li_left[i-1]-li_left[i])*(li_left[i]-li_left[i+1])<0: res2+=1 ans=min(ans,res-res0+res1,res-res0+res2) print(ans)
1,700
PYTHON3
s = input() l = list(s) stack=[] stack.append(l[0]) for i in range(1,len(l)): if len(stack) is not 0: if l[i] == stack[len(stack)-1]: stack.pop() else: stack.append(l[i]) else: stack.append(l[i]) if len(stack) == 0: print("Yes") else: print("No")
1,600
PYTHON3
n = int(input()) l = list(map(int, input().split())) post = [0] * (n+1) if l[n-1] == 1: post[n-1] = 1 for i in range(n-2, -1, -1): if l[i] == 1: post[i] = post[i+1] + 1 else: post[i] = post[i+1] k = 0 for i in range(n): if post[i+1] == 0 or n - i - 1 - post[i+1] == 0: break else: k+=1 print(k+1)
800
PYTHON3
t = int(input()) for i in range(0,t): n = int(input()) s = input() a = s[n-1] ans = a * n print(ans)
800
PYTHON3
n=int(input()) L=list(map(int,input().split())) c1=0 c2=0 c3=0 for i in range(len(L)): if i%3==0: c1+=L[i] elif i%3==1: c2=c2+L[i] else: c3+=L[i] if max(c1,c2,c3)==c1: print("chest") elif max(c1,c2,c3)==c2: print("biceps") else: print("back")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; struct Point { double x, y; }; struct Line { double a, b, c; }; int main() { Point start, dest; cin >> start.x >> start.y >> dest.x >> dest.y; if (start.x > dest.x) swap(start, dest); Line line; line.a = start.y - dest.y; line.b = dest.x - start.x; line.c = -(line.a * start.x + line.b * start.y); int n; cin >> n; int ans = 0; for (int i = 0; i < n; i++) { Line read; cin >> read.a >> read.b >> read.c; double zn = line.a * read.b - read.a * line.b; if (zn == 0) continue; double chx = line.b * read.c - read.b * line.c, chy = line.c * read.a - line.a * read.c; double x0 = chx / zn, y0 = chy / zn; if (start.x != dest.x && start.x < x0 && x0 < dest.x || start.x == dest.x && start.x == x0 && x0 == dest.x) if (start.y < dest.y && start.y < y0 && y0 < dest.y || start.y > dest.y && dest.y < y0 && y0 < start.y || start.y == dest.y && dest.y == y0 && y0 == start.y) ans++; } cout << ans << endl; return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; int a, b, c, x, y, z; int main() { cin >> a >> b >> c >> x >> y >> z; while (a - x >= 2) { if (b < y) b++; else c++; a -= 2; } while (b - y >= 2) { if (a < x) a++; else c++; b -= 2; } while (c - z >= 2) { if (a < x) a++; else b++; c -= 2; } if (a >= x && b >= y && c >= z) cout << "Yes\n"; else cout << "No\n"; return 0; }
1,200
CPP
arr = [] for i in range(4): arr.append(str(input())) answer = "NO" for i in range(3): for j in range(3): countOne=0 if arr[i][j:j+1] == "#": countOne+=1 if arr[i][j+1:j+2] == "#": countOne+=1 if arr[i+1][j:j+1] == "#": countOne+=1 if arr[i+1][j+1:j+2] == "#": countOne+=1 if 4-countOne == 1 or 4 - countOne == 3 or 4 - countOne == 0 or 4 - countOne==4: answer = "YES" print(answer)
1,100
PYTHON3
import sys def moves_route(moves_no, x_axis_moves, y_axis_moves): moves = [] for _ in range(moves_no): if x_axis_moves > 0: moves.append('R') x_axis_moves -= 1 elif x_axis_moves < 0: moves.append('L') x_axis_moves += 1 if y_axis_moves > 0: moves.append('U') y_axis_moves -= 1 elif y_axis_moves < 0: moves.append('D') y_axis_moves += 1 moves.append('\n') return moves def main(sinp=sys.stdin, sout=sys.stdout): start, end = sinp.read().split() letter_coords = 'abcdefgh' letter_to_num = {letter_coords[i]: i for i in range(len(letter_coords))} y_axis_moves = int(end[1]) - int(start[1]) x_axis_moves = letter_to_num[end[0]] - letter_to_num[start[0]] moves_no = max(abs(x_axis_moves), abs(y_axis_moves)) sout.write(str(moves_no) + '\n') sout.write(''.join(moves_route(moves_no, x_axis_moves, y_axis_moves))) if __name__ == '__main__': main()
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAXN = 30000 + 5; int n, m, x, mx; int dp[MAXN][255 * 2], dis[MAXN]; int dfs(int x, int y); int main() { memset(dp, -1, sizeof(dp)); cin >> n >> m; for (int i = 0; i < n; i++) scanf("%d", &x), dis[x]++, mx = max(mx, x); cout << dfs(0, 0) << endl; return 0; } int dfs(int x, int y) { if (x > mx || m + y == 0) return 0; if (dp[x][255 + y] != -1) return dp[x][255 + y]; int d = y + m, ans = 0; ans = max(max(dfs(x + d, y + 1), dfs(x + d, y)), dfs(x + d, y - 1)); ans += dis[x]; return dp[x][255 + y] = ans; }
1,900
CPP
x,y,n =[],{}, int(input()) for i in range(n): name, score =input().split() score = int(score) y[name]=y.get(name,0)+ score x.append([y[name],name]) m = max(y.values()) for n,name in x : if n>=m and y[name] == m: print(name) break
1,500
PYTHON3
// Ac #include <bits/stdc++.h> using namespace std; // #include <ext/pb_ds/tree_policy.hpp> // #include <ext/pb_ds/assoc_container.hpp> // using namespace __gnu_pbds; // typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set; // typedef tree<int, null_type, less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_multiset; #define mod 1000000007 #define rep(i, n) for(int i = 0; i < n; i++) #define rep1(i,a,b) for(int i=a;i<b;i++) #define endl "\n" #define PI 3.14159265358979323846 /* pi */ #define is_pot(n) (n&& !(n&(n-1))) #define all(v) ((v).begin()),((v).end()) #define degreesToRadians(angleDegrees) (angleDegrees * PI / 180.0) // Converts degrees to radians. #define radiansToDegrees(angleRadians) (angleRadians * 180.0 / PI) // Converts radians to degrees. #define int long long #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); #define epsilon 1e-9 typedef long long ll; typedef long double ld; int dx[]={1, 0, -1, 0}; int dy[]={0, 1, 0, -1}; void solve() { int n, k, x; cin>>n>>k>>x; int a[n]; rep(i, n) cin>>a[i]; sort(a, a+n); int ans=0; vector<int> v; multiset<int> mul; for(int i=1;i<n;i++) { if(a[i]-a[i-1]>x){ v.push_back(a[i]-a[i-1]); mul.insert(a[i]-a[i-1]); } } // return; sort(all(v)); // rep(i, v.size()) // { // cout<<v[i]<<" "; // } // return; for(int i=0;i<v.size();i++) { int p=v[i]/x; p+=(v[i]%x!=0); p--; if(p<=k) { k-=p; mul.erase(mul.find(v[i])); } } cout<<mul.size()+1<<endl; } signed main() { fastio // int t;cin>>t;while(t--) solve(); return 0; }
1,200
CPP
import sys input = sys.stdin.readline t = int(input()) for rrr in range(t): n,k = list(map(int,input().split())) s = input().rstrip() s = list(s) if n==k: if s.count('1') <= k//2 and s.count('0') <= k//2: print('YES') else: print('NO') else: ok = 1 #print(s) for i in range(k): tmp = set([]) for j in range(i,n,k): tmp.add(s[j]) for j in range(i,n,k): tmp.add(s[j]) if '1' in tmp and s[j]=='?': s[j] = '1' if '0' in tmp and s[j]=='?': s[j] = '0' if '0' in tmp and '1' in tmp: ok = 0 s = s[::-1] #print(s) for i in range(k): tmp = set([]) for j in range(i,n,k): tmp.add(s[j]) for j in range(i,n,k): tmp.add(s[j]) if '1' in tmp and s[j]=='?': s[j] = '1' if '0' in tmp and s[j]=='?': s[j] = '0' if '0' in tmp and '1' in tmp: ok = 0 tmp = s[:k].count('1') if tmp>k//2: ok = 0 for i in range(k,n): if s[i]=='1': tmp += 1 if s[i-k]=='1': tmp -= 1 if tmp>k//2: ok = 0 tmp = s[:k].count('0') if tmp>k//2: ok = 0 for i in range(k,n): if s[i]=='0': tmp += 1 if s[i-k]=='0': tmp -= 1 if tmp>k//2: ok = 0 #print(s) if ok==1: print('YES') else: print('NO')
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; long long a[100005]; long long s1, s2, n; long long check(long long mid) { long long l = 0, r = 1e12; for (long long i = n; i >= 1; i--) { if (l <= a[i] && a[i] <= r) l = a[i] - mid, r = a[i] + mid; else l = max(l, a[i] - mid), r = min(r, a[i] + mid); } return (l <= s1 && s1 <= r) || (l <= s2 && s2 <= r); } signed main() { scanf("%lld %lld %lld", &n, &s1, &s2); if (s1 > s2) swap(s1, s2); for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]); long long l = s2 - s1, r = 1e12, ans = 1e12; while (l <= r) { long long mid = (l + r) / 2; if (check(mid)) r = mid - 1, ans = mid; else l = mid + 1; } printf("%lld", ans); return 0; }
2,600
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> void maxtt(T& t1, T t2) { t1 = max(t1, t2); } template <typename T> void mintt(T& t1, T t2) { t1 = min(t1, t2); } bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = "URDL"; const long long MOD2 = (long long)1000000007 * (long long)1000000007; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? "YES" : "NO"); exit(0); } void addmod(int& x, int y, int mod = 1000000007) { assert(y >= 0); x += y; if (x >= mod) x -= mod; assert(x >= 0 && x < mod); } void et(int x = -1) { printf("%d\n", x); exit(0); } long long fastPow(long long x, long long y, int mod = 1000000007) { long long ans = 1; while (y > 0) { if (y & 1) ans = (x * ans) % mod; x = x * x % mod; y >>= 1; } return ans; } long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; } int a[5135], p[5135], q[5135], b[5135], pos[5135]; void fix(int i, int j) { int d = q[i] ^ b[i]; int z = pos[d]; if (d == p[i]) return; swap(p[z], p[i]); swap(pos[p[z]], pos[p[i]]); swap(q[z], q[j]); fix(z, j); } void fmain(int tid) { scanf("%d", &k); n = 1 << k; for (int(i) = 0; (i) < (int)(n); (i)++) { scanf("%d", a + i); m ^= a[i]; } if (m != 0) { puts("Fou"); return; } puts("Shi"); for (int(i) = 0; (i) < (int)(n); (i)++) { p[i] = q[i] = i; pos[i] = i; } for (int(i) = 0; (i) < (int)(n - 1); (i)++) { int d = a[i] ^ b[i]; b[i] ^= d; b[i + 1] ^= d; fix(i, i + 1); } for (int(i) = 0; (i) < (int)(n); (i)++) printf("%d ", p[i]); puts(""); for (int(i) = 0; (i) < (int)(n); (i)++) printf("%d ", q[i]); puts(""); } int main() { int t = 1; for (int(i) = 1; (i) <= (int)(t); (i)++) { fmain(i); } return 0; }
3,100
CPP
#include <bits/stdc++.h> using namespace std; long long int euclid_algo(long long int a, long long int b) { while (a != 0 && b != 0) { if (a > b) a = a % b; else b = b % a; } return a + b; } long long int NOK(long long int a, long long int b) { return a * b / euclid_algo(a, b); } int main() { ios_base::sync_with_stdio(false); int n, k; cin >> n >> k; vector<int> v(n); int cnt_nul = 0; for (int i = 0; i < n; ++i) { cin >> v[i]; if (v[i] == 0) ++cnt_nul; } int a; for (int i = 0; i < k; ++i) { cin >> a; } if (cnt_nul > 1) cout << "Yes\n"; else { auto index = find(v.begin(), v.end(), 0); int ind = index - v.begin(); v[ind] = a; bool ans = false; for (int i = 0; i < v.size() - 1; ++i) { if (v[i + 1] < v[i]) { ans = true; break; } } if (ans) cout << "Yes\n"; else cout << "No\n"; } }
900
CPP
def distribution(n): return (n-1)//2 def problem_A(): print(distribution(int(input()))) s = 'abcdefghijklmnopqrstuvwxyz' def cons_str(n,b): a = s[:b] ans = '' while n > b: ans += a n -= b ans += a[:n] return ans def problem_B(): n,a,b = list(map(int,input().split())) print(cons_str(n,b)) def comp_teams(a): if len(a) < 2: return 0 s = len(set(a)) if s == len(a): return 1 a.sort() t = 0 temp = 1 for i in range(1,len(a)): if a[i] == a[i-1]: temp += 1 else: if t < temp: t = temp temp = 1 if t < temp: t = temp if s <= t - 1: return s if s == t: return s - 1 else: return t def problem_C(): l = int(input()) a = list(map(int,input().split())) print(comp_teams(a)) def antisudoku(a): for i in range(9): for j in range(9): if a[i][j] == '2': a[i][j] = '1' return a def problem_D(): a = [] for _ in range(9): a.append(list(input())) b = antisudoku(a) for i in range(9): print(''.join(b[i])) def three_block_palindrome(s): a = [] b = [] c = [] l = len(s) for _ in range(l): a = [] for _ in range(l+1): a.append([0]*26) l += 1 for i in range(1,l): k = s[i-1] - 1 for j in range(26): a[i][j] = a[i-1][j] a[i][k] += 1 maxlength = 0 for i in range(l): for j in range(i,l): maxa = 0 maxb = 0 for k in range(26): if a[l-1][k]-a[j][k] > a[i][k]: if maxa < a[i][k]: maxa = a[i][k] else: if maxa < a[l-1][k]-a[j][k]: maxa = a[l-1][k]-a[j][k] if maxb < a[j][k] - a[i][k]: maxb = a[j][k] - a[i][k] if maxlength < 2*maxa + maxb: maxlength = 2*maxa + maxb return maxlength def problem_E1(): l = int(input()) s = list(map(int,input().split())) print(three_block_palindrome(s)) cases = int(input()) for _ in range(cases): # problem_A() problem_B() # problem_C() # problem_D() # problem_E1()
900
PYTHON3
T = int(input()) for _ in range(T): a, b, n = map(int, input().split()) if n % 3 == 0: print(a) elif n % 3 == 1: print(b) else: print(a^b)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; long long a[100005], b[100005], poz[100005], q[100005]; set<pair<int, int> > S; set<pair<int, int> >::iterator it, it2; long long x, n, d; long long getNextX() { x = (x * 37 + 10007) % 1000000007; return x; } void initAB() { int i; for (i = 0; i < n; i = i + 1) { a[i] = i + 1; } for (i = 0; i < n; i = i + 1) { swap(a[i], a[getNextX() % (i + 1)]); } for (i = 0; i < n; i = i + 1) { if (i < d) b[i] = 1; else b[i] = 0; } for (i = 0; i < n; i = i + 1) { swap(b[i], b[getNextX() % (i + 1)]); } } long long max(long long x, long long y) { return (x > y) ? x : y; } int main() { int i, j; int max1, max2, poz1, poz2, ans, pz, k = 0; int magic = 400; cin >> n >> d >> x; initAB(); for (i = 0; i < n; ++i) poz[a[i]] = i; for (i = 0; i < n; ++i) { if (b[i]) q[++k] = i; S.insert(make_pair(-a[i], i)); ans = 0; if (k < magic) { for (j = 1; j <= k; ++j) if (a[i - q[j]] > ans) ans = a[i - q[j]]; } else { for (it = S.begin(), it2 = S.end(); it != it2; ++it) { pz = it->second; if (b[i - pz]) { ans = -it->first; break; } } } cout << ans << '\n'; } return 0; }
2,300
CPP
#include <bits/stdc++.h> int vis[400050], rett[400050]; int main() { int T; scanf("%d", &T); while (T--) { int n, m; scanf("%d%d", &n, &m); memset(vis, 0, sizeof(int) * (3 * n + 1)); int cnt = 0; for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); if (!vis[u] && !vis[v]) { vis[u] = vis[v] = 1; rett[++cnt] = i; } } if (cnt >= n) { puts("Matching"); for (int i = 1; i <= n; i++) { printf("%d ", rett[i]); } puts(""); } else { puts("IndSet"); cnt = 1; for (int i = 1; cnt <= n; i++) { if (!vis[i]) { cnt++; printf("%d ", i); } } puts(""); } } }
2,000
CPP
from sys import stdin n = int(stdin.readline()) B = list(map(int, stdin.readline().split())) A = [0 for i in range(n)] A[0] = B[0] A[1] = B[1] + A[0] max_value=max(A[0],A[1]) for i in range(2, n): A[i] = B[i] + max_value max_value = max(max_value, A[i]) for item in A: print(item, end=" ") print("")
900
PYTHON3
import math t=int(input()) while t!=0: t-=1 n=int(input()) p=math.cos(math.pi / (4*n)) / math.sin(math.pi / (2*n)) print(p)
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; char a[10000010]; char b[10000010]; int main() { scanf("%s%s", a, b); int sum = 0, min; int len1 = strlen(a), len2 = strlen(b); min = len1; if (len1 > len2) min = len2; for (int i = 0; i < min; i++) { if (a[len1 - i - 1] == b[len2 - i - 1]) sum++; else break; } cout << len1 - sum + len2 - sum << endl; }
900
CPP
gauntlets = {'purple': 'Power', 'green': 'Time', 'blue':'Space', 'orange':'Soul', 'red':'Reality', 'yellow':'Mind'} n = int(input()) arr = [] for i in range(n): arr.append(input()) sol = [] for k, v in gauntlets.items(): if k not in arr: sol.append(v) print(len(sol)) for s in sol: print(s)
800
PYTHON3
# -*- coding: utf-8 -*- """ Created on Thu Aug 27 23:23:49 2020 1372A Omkar and Completion @author: RACHIT """ if __name__=="__main__": t=int(input()) while(t): n=int(input()) while(n): print(1,end=' ') n-=1 print() t-=1
800
PYTHON3
dates = sorted((tuple(map(int, input().split())) for _ in range(int(input())))) last_exam = 0 for a, b in dates: last_exam = a if b < last_exam else b print(last_exam)
1,400
PYTHON3
n = int(input()) for i in range (n): s= input() c = 0 for x in s : if x=='N': c+=1 if (c==1): print('NO') else : print('YES')
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int NMAX = 1005; const int MMAX = 15; const int MOD = 1000000007; int dp[NMAX][NMAX][MMAX]; int add(int a, int b) { a = a + b; if (a >= MOD) return a - MOD; return a; } int main() { int n, m, i, j, k, sum; scanf("%d%d", &n, &m); for (i = 1; i <= n; i++) for (j = i; j <= n; j++) dp[i][j][1] = 1; for (k = 2; k <= m; k++) { for (i = 1; i <= n; i++) { for (j = n; j >= i; j--) { dp[i][j][k] = (((dp[i][j][k - 1] + dp[i - 1][j][k]) % MOD + dp[i][j + 1][k]) % MOD + MOD - dp[i - 1][j + 1][k]) % MOD; } } } sum = 0; for (i = 1; i <= n; i++) for (j = i; j <= n; j++) sum = (sum + dp[i][j][m]) % MOD; printf("%d", sum); return 0; }
1,600
CPP
n=int(input()) t=input() s='' i=0 x=1 while(i<len(t)): s+=t[i] i+=x x+=1 print(s)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAX = 10010; const long long inf = 1e18; int p[MAX], s[MAX]; long long f[MAX], nf[MAX]; int main(void) { int n, c; scanf("%d %d", &n, &c); for (int i = (0); i < (n); ++i) scanf("%d", &p[i]); for (int i = (0); i < (n); ++i) scanf("%d", &s[i]); f[0] = 0; for (int i = (0); i < (n); ++i) { for (int j = (0); j < (i + 2); ++j) nf[j] = inf; for (int j = (0); j < (i + 1); ++j) { nf[j] = min(nf[j], f[j] + j * 1LL * c + p[i]); nf[j + 1] = min(nf[j], f[j] + s[i]); } for (int j = (0); j < (i + 2); ++j) f[j] = nf[j]; } long long ans = *min_element(f, f + n + 1); printf("%lld\n", ans); return 0; }
2,900
CPP
from sys import stdin,stderr,maxsize mod = int(1e9)+7 def I(): return int(stdin.readline()) def lint(): return [int(x) for x in stdin.readline().split()] def S(): return input().strip() def grid(r, c): return [lint() for i in range(r)] def debug(*args, c=6): print('\033[3{}m'.format(c), *args, '\033[0m', file=stderr) from collections import Counter,defaultdict from itertools import permutations import re # def bark(): # n = I(); s =S() # a = re.split('[A-Z]+',s) # p = max(map(lambda x: len(set(x)),a)) # return p def bark(): s =S() n = len(s) ; z = 0 for i in range(n-1,-1,-1): if s[i]=='0': z+=1 continue else:break debug(s[:(n-z)],s) if s[:(n-z)]== s[:(n-z)][::-1]:print("YES") else:print("NO") if __name__ == '__main__': bark()
900
PYTHON3
s = [int(x) for x in input().split("+")] s.sort() res = str(s[0]) for x in s[1:] : res = res +"+" + str(x) print(res)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int aa[2222], bb[2222], cc[2222]; int xx(int x) { if (x == 0 || x == 1) return 0; else { int y = 0; for (int i = 1; i < x; i++) { y += i; } return y; } } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; if (a - b >= 0) aa[a - b]++; else cc[b - a]++; bb[a + b]++; } int x = 0; for (int i = 0; i < 2222; i++) { x += xx(aa[i]); } for (int i = 0; i < 2222; i++) { x += xx(bb[i]); } for (int i = 0; i < 2222; i++) { x += xx(cc[i]); } cout << x << endl; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; const int inf(1010101010); struct pt { long long x, y; pt() {} pt(long long x, long long y) : x(x), y(y) {} bool operator<(pt const& p2) const { return (x < p2.x) || (x == p2.x && y < p2.y); } }; inline void rr(pt& p) { cin >> p.x >> p.y; } pt p[1005]; bool red[1005]; int n; inline long long sgn(pt& p1, pt& p2, pt& p3) { return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y); } inline long long dy(int i, int j) { return p[j].y - p[i].y; } inline long long dx(int i, int j) { return p[j].x - p[i].x; } struct by_angle { int i; by_angle(int i) : i(i) {} bool operator()(int j1, int j2) const { return dy(i, j1) * dx(i, j2) < dy(i, j2) * dx(i, j1); } }; int ids[1005]; int main() { int nR, nB; cin >> nR >> nB; n = 0; while (nR--) { rr(p[n]), red[n] = 1, n++; } while (nB--) { rr(p[n]), red[n] = 0, n++; } int res(0); for (int i(0); i < (n); i++) if (red[i]) { int npt(0); for (int j(0); j < (n); j++) if (p[i] < p[j]) { ids[npt++] = j; } sort(ids, ids + npt, by_angle(i)); for (int j(0); j < (npt); j++) if (red[ids[j]]) { pt p2(p[ids[j]]); int h(ids[j]); pt hulp(p[ids[j]]); for (int k(j + 1); k < (npt); k++) { pt& p3(p[ids[k]]); if (!red[ids[k]]) { if (sgn(p3, p2, p[h]) >= 0) h = ids[k]; } else { if (sgn(p3, p2, p[h]) >= 0) { res++; } } } } } cout << res << endl; }
2,600
CPP
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; int ans = min(min(a, b / 2), c / 4); cout << ans * 7 << "\n"; return 0; }
800
CPP
for _ in range (int(input())) : a,b,c,d = map(int,input().split()) x,y,x1,y1,x2,y2 = map(int,input().split()) if x1 == x2 and a != 0 : print("NO") elif x1 == x2 and b!= 0 : print("NO") elif y1 == y2 and c != 0 : print("NO") elif y1 == y2 and d != 0 : print("NO") elif a-b > 0 and x-x1 < a-b : print("NO") elif b-a > 0 and x2-x < b-a : print("NO") elif c-d > 0 and y-y1 < c-d : print("NO") elif d-c > 0 and y2 - y < d - c : print("NO") else : print("YES")
1,100
PYTHON3
def task(char,list1): map1[char]=[0] count=0 for i in range(len(list1)): if list1[i]==char: count+=1 map1[char].append(count) def recur(char,start,end,moves,count): if 2**count==n: if list1[start]==char: ans.append(moves+1) else: ans.append(moves) return recur(chr(ord(char)+1),start,(start+end-1)//2,moves+map1[char][end+1]-map1[char][(start+end+1)//2],count+1) recur(chr(ord(char)+1),(start+end+1)//2,end,moves+map1[char][(start+end-1)//2+1]-map1[char][start],count+1) t=int(input()) for _ in range(t): n=int(input()) list1=input().strip() map1=dict() for i in range(97,122+1): ch=chr(i) task(ch,list1) ans=[] recur("a",0,n-1,0,0) #print(ans) print(n-max(ans))
1,500
PYTHON3
for _ in range(int(input())): n=int(input()) l=list(map(int,input().split())) lh=[] lv=[] minans=10000000000 minh=10000000000000000 minv=10000000000000000 sumh=0 sumv=0 count=10000000000000000000 counth=100000000000 countv=100000000000 minv1=100000000000 sum = 0 for i in range(0,len(l)): if(i%2==0): lh.append(l[i]) minh=min(minh,l[i]) else: lv.append(l[i]) minv=min(minv,l[i]) sum= sum + l[i] if(i): count=min(count,sum+(n-len(lh))*minh+(n-len(lv))*minv) print(count)
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 7; int vis1[MAXN], vis2[MAXN], dis[MAXN], vis3[MAXN], dis1[MAXN]; int st, ed, n, ans; queue<int> q; vector<int> G[MAXN]; void Bfs1() { for (; !q.empty();) q.pop(); q.push(1); vis1[1] = 1; for (; !q.empty();) { int u = q.front(); q.pop(); for (auto v : G[u]) { if (vis1[v]) continue; q.push(v); vis1[v] = u; } st = u; } } void Bfs2() { for (; !q.empty();) q.pop(); q.push(st); vis2[st] = st; for (; !q.empty();) { int u = q.front(); q.pop(); for (auto v : G[u]) { if (vis2[v]) continue; q.push(v); vis2[v] = u; } ed = u; } } int degree(int x) { return (int)G[x].size(); } bool check(int root) { for (int i = 1; i <= n; i++) vis1[i] = dis[i] = 0; for (; !q.empty();) q.pop(); q.push(root); vis1[root] = 1; for (; !q.empty();) { int u = q.front(); q.pop(); for (auto v : G[u]) { if (vis1[v]) continue; q.push(v); vis1[v] = vis1[u] + 1; if (!dis[vis1[v]]) dis[vis1[v]] = v; if (degree(v) != degree(dis[vis1[v]])) return false; } } ans = root; return true; } bool checkCenter() { int D = 0; for (int u = ed; u != st; u = vis2[u]) D++; if (D & 1) return false; int center = ed; for (int i = 0; i != D / 2; i++, center = vis2[center]) ; if (check(center)) return true; for (int i = 1; i <= n; i++) vis3[i] = dis1[i] = 0; for (; !q.empty();) q.pop(); q.push(center); vis3[center] = 1; for (; !q.empty();) { int u = q.front(); q.pop(); for (auto v : G[u]) { if (vis3[v]) continue; q.push(v); vis3[v] = vis3[u] + 1; if (!dis1[vis3[v]]) dis1[vis3[v]] = v; if (degree(v) != degree(dis1[vis3[v]])) { for (int x = v, tim = 0;;) { if (++tim == n) break; if (G[x].size() == 1) { if (check(x)) return true; else break; } else { int y; for (y = 0; vis3[G[x][y]] != 0; y++) ; x = G[x][y]; } } for (int x = dis1[vis3[v]], tim = 0;;) { if (++tim == n) break; if (G[x].size() == 1) { if (check(x)) return true; else break; } else { int y; for (y = 0; vis3[G[x][y]] != 0; y++) ; x = G[x][y]; } } return false; } } } ans = center; return true; } int main() { cin >> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } Bfs1(); Bfs2(); if (check(st) || check(ed)) cout << ans; else if (checkCenter()) cout << ans; else cout << -1; }
2,400
CPP
a = int(input()) for i in range(a): b = [int(x) for x in input().split()] xs = [1,1] minside = min(b[0],b[1]) xe = b line = [] totalsum = 0 for j in range(xe[0]): line.append([int(x) for x in input().split()]) for k in range(1,b[0]+b[1]-1): #how far from start-end if((xe[1]<xs[1] and xe[0]==xs[0]) or (xs[0]>xe[0] and xe[1]==xs[1]) or xe==xs): break check = {} time = min(k,minside) for i in range(time): change = line[xe[0]-1-i][xe[1]-1+i] if change in check: check[change]+=1 else: check[change] = 1 change = line[xs[0]-1-i][xs[1]-1+i] if change in check: check[change]+=1 else: check[change] = 1 if 0 in check: if 1 in check: totalsum+=min(check[0],check[1]) #print(check) #print(totalsum) if(xe[1]!=1): xe[1]-=1 else: xe[0]-=1 if(xs[0]!=b[0]): xs[0]+=1 else: xs[1]+=1 #print(xe,xs) check = {} print(totalsum)
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, k, a, m, ans = -1, b[200005]; bool used[200005]; int main() { scanf("%d%d%d%d", &n, &k, &a, &m); for (int i = 0; i < m; i++) scanf("%d", b + i); int l = 0, r = m - 1; while (l <= r) { int mid = (l + r) >> 1, counter = 0, last = 0; for (int i = 0; i <= mid; i++) used[b[i] - 1] = true; used[n] = true; for (int i = 0; i <= n; i++) if (used[i]) counter += (i - last + 1) / (a + 1), used[i] = false, last = i + 1; if (k <= counter) l = mid + 1; else r = mid - 1, ans = mid + 1; } cout << ans; return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100005; const int V = 998244353; struct edge { int to, next; } e[N * 2]; int head[N], tot, n; long long f[N], g[N], q[N * 2]; int mp[N * 2], S, ans, ansv; void add(int x, int y) { e[++tot] = (edge){y, head[x]}; head[x] = tot; } void add(int x) { if (!mp[x]) ++S; ++mp[x]; } void del(int x) { --mp[x]; if (!mp[x]) --S; } void dfs1(int x, int fa) { f[x] = 114514; for (int i = head[x]; i; i = e[i].next) if (e[i].to != fa) { dfs1(e[i].to, x); f[x] += f[e[i].to] * (f[e[i].to] ^ V) + 1919813; } } void dfs2(int x, int fa) { for (int i = head[x]; i; i = e[i].next) if (e[i].to != fa) { g[e[i].to] = (x == 1 ? 0 : g[x] * (g[x] ^ V) + 1919813) + f[x]; g[e[i].to] -= f[e[i].to] * (f[e[i].to] ^ V) + 1919813; dfs2(e[i].to, x); } } void getans(int x, int fa) { if (S > ansv) ansv = S, ans = x; for (int i = head[x]; i; i = e[i].next) if (e[i].to != fa) { del(f[e[i].to]); add(g[e[i].to]); getans(e[i].to, x); del(g[e[i].to]); add(f[e[i].to]); } } int main() { scanf("%d", &n); for (int i = (int)(1); i <= (int)(n - 1); i++) { int x, y; scanf("%d%d", &x, &y); add(x, y); add(y, x); } dfs1(1, 0); dfs2(1, 0); for (int i = (int)(2); i <= (int)(n); i++) { q[++*q] = f[i]; q[++*q] = g[i]; } sort(q + 1, q + *q + 1); for (int i = (int)(2); i <= (int)(n); i++) { f[i] = lower_bound(q + 1, q + *q + 1, f[i]) - q; g[i] = lower_bound(q + 1, q + *q + 1, g[i]) - q; } for (int i = (int)(2); i <= (int)(n); i++) add(f[i]); ansv = -1; getans(1, 0); printf("%d\n", ans); }
2,900
CPP
from fractions import gcd k = int(input()) l = int(input()) m = int(input()) n = int(input()) d = int(input()) kl = k*l//gcd(k,l) km = k*m//gcd(k,m) kn = k*n//gcd(k,n) lm = l*m//gcd(l,m) ln = l*n//gcd(l,n) mn = m*n//gcd(m,n) klm = kl*m//gcd(kl,m) kln = kl*n//gcd(kl,n) kmn = km*n//gcd(km,n) lmn = lm*n//gcd(lm,n) klmn = kl*mn//gcd(kl,mn) first = d//k + d//l + d//m + d//n second = d//kl + d//km + d//kn + d//lm + d//ln + d//mn third = d//klm + d//kln + d//kmn + d//lmn fourth = d//klmn print(first-second+third-fourth) #print("DEBUG") #print(first, ' ', second, ' ', third, ' ', fourth)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void smin(T &a, U b) { if (a > b) a = b; } template <typename T, typename U> inline void smax(T &a, U b) { if (a < b) a = b; } template <class T> inline void gn(T &first) { char c, sg = 0; while (c = getchar(), (c > '9' || c < '0') && c != '-') ; for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9'; c = getchar()) first = (first << 1) + (first << 3) + c - '0'; if (sg) first = -first; } template <class T, class T1> inline void gn(T &first, T1 &second) { gn(first); gn(second); } template <class T, class T1, class T2> inline void gn(T &first, T1 &second, T2 &z) { gn(first); gn(second); gn(z); } template <class T> inline void print(T first) { if (first < 0) { putchar('-'); return print(-first); } if (first < 10) { putchar('0' + first); return; } print(first / 10); putchar(first % 10 + '0'); } template <class T> inline void printsp(T first) { print(first); putchar(' '); } template <class T> inline void println(T first) { print(first); putchar('\n'); } int power(int a, int b, int m, int ans = 1) { for (; b; b >>= 1, a = 1LL * a * a % m) if (b & 1) ans = 1LL * ans * a % m; return ans; } char str[100010]; bool is[50], no[50]; bool vst[50]; int main() { int n; int cnt = -1; char c, t; int tot = 0; cin >> n; for (int i = 0; i < 26; i++) is[i] = 0, no[i] = 0; for (int i = 0; i < n; i++) { scanf(" %c", &t); for (int j = 0; j < 26; j++) vst[j] = 0; if (t == '!') { scanf("%s", str); tot++; if (cnt >= 0) continue; int len = strlen(str); for (int j = 0; j < len; j++) { int k = str[j] - 'a'; vst[k] = 1; } for (int j = 0; j < 26; j++) { if (!vst[j]) { no[j] = 1; is[j] = 0; } else { if (no[j]) continue; is[j] = 1; } } } if (t == '.') { scanf("%s", str); if (cnt >= 0) continue; int len = strlen(str); for (int j = 0; j < len; j++) vst[str[j] - 'a'] = 1; for (int j = 0; j < 26; j++) if (vst[j]) no[j] = 1, is[j] = 0; } if (t == '?') { scanf(" %c", &c); if (i < n - 1) tot++, no[c - 'a'] = 1, is[c - 'a'] = 0; if (cnt >= 0) continue; } int nn = 0; for (int j = 0; j < 26; j++) if (no[j] == 0 && is[j]) nn++; if (nn == 1) cnt = tot; nn = 0; for (int j = 0; j < 26; j++) if (no[j]) nn++; if (nn == 25) cnt = tot; nn = 0; for (int j = 0; j < 26; j++) if (is[j]) nn++; if (nn == 1) cnt = tot; } if (cnt == -1) cnt = tot; println(tot - cnt); return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; int vis; vector<vector<int> > graph; void dfs(int u, int &use) { vis |= (1 << u); for (auto &e : graph[u]) if (!((1 << e) & vis) && ((1 << e) & use)) dfs(e, use); } int main() { ios::sync_with_stdio(0); int n, m; cin >> n >> m; vector<int> gp(n, 0); graph.assign(n, vector<int>()); for (int i = 0; i < n; i++) gp[i] |= (1 << i); for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--, b--; gp[a] |= (1 << b), gp[b] |= (1 << a); graph[a].push_back(b), graph[b].push_back(a); } bool is_ok = true; for (int i = 0; i < n; i++) if (gp[i] != (1 << n) - 1) is_ok = false; if (is_ok) { cout << 0 << endl; return 0; } int ans = 1000000000, st = 0; for (int i = 0; i < 1 << n; i++) { int cliq = 0, mn = 0, x = -1; for (int j = 0; j < n; j++) if ((1 << j) & i) cliq |= gp[j], mn++, x = j; if (cliq == (1 << n) - 1 && mn < ans) { vis = 0; dfs(x, i); if (vis == i) if (mn < ans) ans = mn, st = i; } } cout << ans << endl; for (int j = 0; j < n; j++) if ((1 << j) & st) cout << j + 1 << " "; return 0; }
2,400
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int n; cin >> n; vector<char> num(n); vector<long long int> ans(n, 9), ans1(n, 9); for (long long int i = 0; i < n; i++) cin >> num[i]; for (long long int i = 0; i < n; i++) { long long int x = 0, j = i, cnt = 0; while (x < n) { if (j == i) { ans1[x] = 0; cnt = (10 - (num[j] - '0')) % 10; } else { ans1[x] = ((num[j] - '0') + cnt) % 10; } x++; j++; j = j % n; } for (long long int i = 0; i < n; i++) { if (ans1[i] < ans[i]) { ans = ans1; break; } else if (ans1[i] > ans[i]) break; } } for (long long int i = 0; i < n; i++) cout << ans[i]; }
1,500
CPP
#include <bits/stdc++.h> using namespace std; const int MAX_N = 200005; char s[MAX_N]; int main() { string s = "fedabc"; unsigned long long n, ans = 0, x = 0; char c; scanf("%I64d%c", &n, &c); for (int i = 0; i < 6; i++) { x++; if (s[i] == c) break; } n--; ans = 16 * (n / 4); n = n % 4; ans += x; if (n % 2 == 1) ans += 7; cout << ans; return 0; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; int a[18258]; a[0] = 1; a[1] = 12; a[2] = 24; int i; for (i = 3; i < 18257; i++) a[i] = a[i - 1] + 12; while (cin >> n) { long long sum = 0; for (i = 0; i < n; i++) sum += a[i]; cout << sum << endl; } return 0; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (!(n & 1)) return cout << -1, 0; for (int i = 0; i < n; i++) cout << i << ' '; cout << endl; for (int i = 0; i < n; i++) cout << i << ' '; cout << endl; for (int i = 0; i < n; i++) cout << (2 * i) % n << ' '; return 0; }
1,300
CPP
num1 = input() num2 = input() out = '' for n in range(len(num1)): if num1[n] == num2[n]: out += '0' else: out += '1' print(out)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, i, sum = 0, cnt, money, mn, x, y, len, j, ans; cin >> n >> m; long long arr[200001], a[200001], l, k, r, mid; vector<long long> brr[200001]; for (i = 1; i <= n; i++) { cin >> arr[i]; sum += arr[i]; } sum *= 2; for (i = 0; i < m; i++) { cin >> x >> y; brr[x].push_back(y); } l = 1; r = sum; cnt = r; while (r >= l) { mid = (l + r) / 2; for (i = 1; i <= n; i++) a[i] = arr[i]; money = mid; ans = sum; for (j = min(mid, (long long)200000); j > 0; j--) { money = min(money, j); len = brr[j].size(); for (k = 0; k < len; k++) { mn = min(money, a[brr[j][k]]); ans -= mn; a[brr[j][k]] -= mn; money -= mn; } } if (ans <= mid) { cnt = mid; r = mid - 1; } else l = mid + 1; } cout << cnt << endl; return 0; }
2,000
CPP
n=int(input()) a=input().split() for i in range(n): a[i]=int(a[i]) for j in range(n-1,0,-1): if a[j-1]>=a[j] and a[j]!=0: a[j-1]=a[j]-1 elif a[j]==0: a[j-1]=0 print(sum(a))
1,000
PYTHON3
import math arr = [int(x) for x in input().split()] r = arr[0] x1 = arr[1] y1 = arr[2] x2 = arr[3] y2 = arr[4] resp = math.sqrt(math.pow(y2 - y1, 2) + pow(x2 - x1, 2)) resp = math.ceil((resp / r) / 2) print (resp)
1,400
PYTHON3
f=list(map(int,str(input()).split())) scores=list(map(int,str(input()).split())) k=f[1] t=0 for s in scores: if s >=scores[k-1] and s>0: t=t+1 else: break print(t)
800
PYTHON3
cnt = 0 tot = 0 n = int(input()) notes = [ 100, 20, 10, 5, 1 ] while(n > 0): if(notes[cnt] <= n): n -= notes[cnt] tot += 1 else: cnt+=1 print(tot)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; using Int = long long; constexpr static int mod = 1e9 + 7; constexpr static int inf = (1 << 30) - 1; constexpr static Int infll = (1LL << 61) - 1; int Competitive_Programming = (ios_base::sync_with_stdio(false), cin.tie(nullptr), cout << fixed << setprecision(15), 0); #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") int main() { int N, M, x, y, r, c; cin >> N >> M >> r >> c >> x >> y; r--, c--; vector<string> grid(N); for (int i = 0; i < N; i++) cin >> grid[i]; deque<tuple<int, int>> que; que.emplace_front(c, r); const int dx[4] = {0, 0, -1, 1}; const int dy[4] = {1, -1, 0, 0}; vector<vector<pair<int, int>>> dist(N, vector<pair<int, int>>(M, {inf, inf})); vector<vector<bool>> visited(N, vector<bool>(M)); visited[r][c] = true; dist[r][c] = {0, 0}; while (not que.empty()) { auto [vx, vy] = que.front(); que.pop_front(); auto d = dist[vy][vx]; for (int i = 0; i < 4; i++) { int nx = vx + dx[i], ny = vy + dy[i]; if (nx < 0 or nx >= M or ny < 0 or ny >= N) continue; if (grid[ny][nx] == '*' or visited[ny][nx]) continue; if (i == 2 and d.first + 1 > x) continue; if (i == 3 and d.second + 1 > y) continue; dist[ny][nx] = {d.first + (i == 2), d.second + (i == 3)}; if (i < 2) que.emplace_front(nx, ny); else que.emplace_back(nx, ny); visited[ny][nx] = true; } } int ans = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (dist[i][j].first != inf) ans++; } } cout << ans << "\n"; }
1,800
CPP
X=[] Y=[] Z=[] for _ in range(int(input())): x,y,z=input().split(" ") X.append(int(x)) Y.append(int(y)) Z.append(int(z)) if (sum(X)==0 and sum(Y)==0 and sum(Z)==0): print("YES") else: print("NO")
1,000
PYTHON3
n = int(input()) sticks = sorted(map(int, input().split()), key=lambda x: -x) area = 0 pairs = [] pos = 0 while pos < n - 1: a, b = sticks[pos], sticks[pos + 1] if a - b <= 1: if len(pairs) == 0: pairs.append(b) else: area += pairs.pop() * b pos += 1 pos += 1 print(area)
1,600
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, it; int sz[300030]; pair<int, int> ans; vector<int> node; struct Node { int can[26]; int mx; } bor[300030], t[300030]; void dfs(int v) { sz[v]++; for (int i = 0; i < 26; i++) { int to = bor[v].can[i]; if (to == 0) continue; dfs(to); sz[v] += sz[to]; } } void update(int v, int w) { for (int i = 0; i < 26; i++) { if (!bor[w].can[i]) continue; if (!t[v].can[i]) t[v].can[i] = ++it; update(t[v].can[i], bor[w].can[i]); } } void update_mx(int v, int w) { for (int i = 0; i < 26; i++) { if (!bor[w].can[i]) continue; if (!t[v].can[i]) it += sz[bor[w].can[i]]; else update_mx(t[v].can[i], bor[w].can[i]); } } int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 1; i < n; i++) { int x, y; char c; cin >> x >> y >> c; bor[x].can[c - 'a'] = y; } dfs(1); node.push_back(1); for (int i = 1; i <= n; i++) { int mx = 0; for (int j = 0; j < 26; j++) if (sz[bor[i].can[j]] > sz[bor[i].can[mx]]) mx = j; bor[i].mx = mx; } int l = 0, r = 1; for (int p = 0; p < n && node.size(); p++) { int NewSize = 0; for (int i = l; i < r; i++) { int v = node[i]; for (int j = 0; j < 26; j++) { if (!bor[v].can[j]) continue; node.push_back(bor[v].can[j]); if (j != bor[v].mx) update(0, bor[v].can[j]); } int it_mn = it; update_mx(0, bor[v].can[bor[v].mx]); NewSize += sz[v] - it - 1; for (int q = 0; q < it_mn + 5; q++) { for (int qq = 0; qq < 26; qq++) t[q].can[qq] = 0; t[q].mx = 0; } it = 0; } l = r; r = node.size(); if (NewSize > ans.first) { ans.first = NewSize; ans.second = p + 1; } } cout << sz[1] - ans.first << endl << ans.second << endl; }
2,500
CPP
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const long long MOD = 1000000007; vector<vector<pair<long long, long long> > > vec; long long s[200005], mini = 0, maxi = 0, n; void dfs(long long node, long long p) { s[node] = 1; for (auto it : vec[node]) if (it.first != p) { dfs(it.first, node); if (s[it.first] & 1) mini += it.second; maxi += it.second * s[it.first]; s[node] += s[it.first]; } } long long find_centroid(long long node, long long p) { for (auto it : vec[node]) if (it.first != p && s[it.first] > (n / 2)) return find_centroid(it.first, node); return node; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { long long a, b, w; cin >> n; n <<= 1; vec.resize(n); for (long long i = 0; i < n - 1; ++i) { cin >> a >> b >> w; a--, b--; vec[a].push_back({b, w}); vec[b].push_back({a, w}); } dfs(0, 0); cout << mini << " "; long long r = find_centroid(0, 0); maxi = 0, dfs(r, r); cout << maxi << "\n"; vec.clear(), mini = maxi = 0; } return 0; }
2,000
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") template <class T> inline T lowbit(T x) { return x & (-x); } template <class T> inline T sqr(T x) { return x * x; } template <class T> inline bool scan(T &ret) { char c; int sgn; if (c = getchar(), c == EOF) return 0; while (c != '-' && (c < '0' || c > '9')) c = getchar(); sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return 1; } const double pi = 3.14159265358979323846264338327950288L; using namespace std; int n, m; int a[23], b[23]; short dp[21][1 << 5][11 * 11 * 11 * 11 * 11 + 5]; int Pow[6]; int ans[6][21]; bool ok(int i, int j) { int ans = 0; while (j) { if (j & 1) { ans++; while (j & 1) { j >>= 1; } } j >>= 1; } return ans == b[i]; } bool ok(int num) { for (int i = 0; i < n; i++) { if (a[i] != num % 11) return 0; num /= 11; } return 1; } bool dfs(int i, int pre, int st) { if (dp[i][pre][st] != -1) return dp[i][pre][st]; if (i == m - 1) { if (ok(st)) { dp[i][pre][st] = 1; } else dp[i][pre][st] = 0; return dp[i][pre][st]; } i++; for (int now = 0; now < (1 << n); now++) { int tmp = st; for (int j = 0; j < n; j++) { if (!((pre & (1 << j)) > 0) && ((now & (1 << j)) > 0)) tmp += Pow[j]; } if (ok(i, now) && dfs(i, now, tmp)) { for (int j = 0; j < 5; j++) ans[j][i] = ((now & (1 << j)) > 0); return dp[i - 1][pre][st] = 1; } } return dp[i - 1][pre][st] = 0; } int main() { Pow[0] = 1; for (int i = 1; i <= 5; i++) Pow[i] = Pow[i - 1] * 11; cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < m; i++) cin >> b[i]; memset(dp, -1, sizeof(dp)); for (int i = 0; i < (1 << n); i++) { int tmp = 0; for (int j = 0; j < n; j++) { tmp += Pow[j] * ((i & (1 << j)) > 0); } if (ok(0, i) && dfs(0, i, tmp)) { for (int j = 0; j < 5; j++) ans[j][0] = ((i & (1 << j)) > 0); break; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { printf("%c", (ans[i][j] ? '*' : '.')); } printf("\n"); } return 0; }
2,400
CPP
#include <bits/stdc++.h> int k, l, m; int ans = 0, heap1; int n1[200050]; struct bet { int a, b; }; bet l1[200050], heap[200050]; void swap(int x, int y) { bet t = heap[x]; heap[x] = heap[y]; heap[y] = t; int o = l1[heap[x].b].b; l1[heap[x].b].b = l1[heap[y].b].b; l1[heap[y].b].b = o; } void push_heap(int x, int y) { heap[heap1].a = x; heap[heap1++].b = y; int i = heap1 - 1; while (i > 0) { if (heap[i].a > heap[(i - 1) / 2].a) swap(i, (i - 1) / 2), i = (i - 1) / 2; else break; } } void uyt(int x, int y) { heap[x].a += y; int i = x; if (heap[i].a == 0) { heap1--; swap(i, heap1); } while (1) { if (i > 0 && heap[i].a > heap[(i - 1) / 2].a) swap(i, (i - 1) / 2), i = (i - 1) / 2; else if (i * 2 + 1 < heap1) { int g = i * 2 + 1, h = i * 2 + 2; if (heap[g].a < heap[h].a && h < heap1) g = h; if (heap[i].a < heap[g].a) swap(i, g), i = g; else break; } else break; } } int main() { scanf("%d %d %d", &k, &l, &m); for (int i = 0; i < k; i++) scanf("%d", &n1[i]); int u = 0, y = -1, q = 0; while (y < k) { if (q <= m) { y++; if (l1[n1[y]].a == 0) { l1[n1[y]].a++, l1[n1[y]].b = heap1; push_heap(1, n1[y]); } else { l1[n1[y]].a++; uyt(l1[n1[y]].b, 1); } q = y - u + 1 - heap[0].a; if (q <= m && heap[0].a > ans) ans = heap[0].a; } else { l1[n1[u]].a--; uyt(l1[n1[u]].b, -1); u++; q = y - u + 1 - heap[0].a; } } printf("%d\n", ans); }
1,800
CPP
import math T = int(input()) for t in range(T): N, M, K = [int(_) for _ in input().split()] if M <= N//K: print(M) continue M -= N//K m = math.ceil(M/(K-1)) print(N//K-m)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const double eps = 1e-6; const int MAXN = 1e5 + 10; const int MAXM = 2e6 + 10; const long long mod = 1e9 + 7; int num[1510]; int main() { int k[5]; for (int i = 0; i < 3; i++) { scanf("%d", &k[i]); num[k[i]]++; } if (num[1] >= 1 || num[2] >= 2 || num[3] >= 3 || (num[2] >= 1 && num[4] >= 2)) puts("YES"); else puts("NO"); return 0; }
1,400
CPP
#!/usr/bin/env python3 from collections import deque t = int(input()) for _ in range(t): n, m = map(int, input().split()) a = [] for i in range(n): a.append(list(input())) g = [] b = [] for i in range(n): for j in range(m): if a[i][j] == 'G': g.append((i, j)) elif a[i][j] == 'B': b.append((i, j)) if len(g) == 0: print("Yes") continue possible = True for b_i in b: i, j = b_i if i != 0 and a[i - 1][j] == '.': a[i - 1][j] = '#' if i != n - 1 and a[i + 1][j] == '.': a[i + 1][j] = '#' if j != 0 and a[i][j - 1] == '.': a[i][j - 1] = '#' if j != m - 1 and a[i][j + 1] == '.': a[i][j + 1] = '#' if a[n - 1][m - 1] == '#': print("No") continue visited = set() d = deque() d.append((n - 1, m - 1)) visited.add((n - 1, m - 1)) while len(d) != 0: i, j = d[0] d.popleft() if a[i][j] == 'G': g.remove((i, j)) if a[i][j] == 'B': possible = False break if i != 0 and a[i - 1][j] != '#' and not (i - 1, j) in visited: d.append((i - 1, j)) visited.add((i - 1, j)) if i != n - 1 and a[i + 1][j] != '#' and not (i + 1, j) in visited: d.append((i + 1, j)) visited.add((i + 1, j)) if j != 0 and a[i][j - 1] != '#' and not (i, j - 1) in visited: d.append((i, j - 1)) visited.add((i, j - 1)) if j != m - 1 and a[i][j + 1] != '#' and not (i, j + 1) in visited: d.append((i, j + 1)) visited.add((i, j + 1)) if not possible or len(g) != 0: print("No") else: print("Yes")
1,700
PYTHON3
n, h = map(int, input().split()) array = list(map(int, input().split())) con = len(array) for i in array: if i > h: con += 1 print(con)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, a, b, ans[10000], k; int main() { cin >> n >> a >> b; if (a > n - b - 1) cout << n - a; else cout << b + 1; return 0; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; vector<long long> seg[2]; vector<int> ring_weight, ring_id; void init(int n) { for (int i = 0; i < int(2); i++) { seg[i].resize(4 * n + 3); } } void make(int node, int u, int v) { if (u == v) { seg[0][node] = seg[1][node] = ring_weight[u]; return; } int ls = (node << 1) | 1, rs = ls + 1, m = (u + v) >> 1; make(ls, u, m); make(rs, m + 1, v); seg[0][node] = seg[0][ls] + seg[0][rs]; seg[1][node] = min(seg[1][ls], seg[1][rs]); } tuple<long long, int> read(int node, int u, int v, int first, int second) { if (first > v || second < u) { return make_pair(0, 1 << 30); } if (first <= u && v <= second) { return make_pair(seg[0][node], seg[1][node]); } int ls = (node << 1) | 1, rs = ls + 1, m = (u + v) >> 1; long long lx, rx; int ly, ry; tie(lx, ly) = read(ls, u, m, first, second); tie(rx, ry) = read(rs, m + 1, v, first, second); return make_pair(lx + rx, min(ly, ry)); } vector<int> seen; vector<int> ring_nodes; vector<int> f, w; vector<vector<int> > r, g; int dfs(int u, int c) { seen[u] = c; if (seen[f[u]] == c) { ring_id[u] = int((ring_nodes).size()); ring_nodes.push_back(u); return (u == f[u] ? -1 : f[u]); } if (seen[f[u]] != -1) { return -1; } int t = dfs(f[u], c); if (t != -1) { ring_id[u] = int((ring_nodes).size()); ring_nodes.push_back(u); } return (t == u ? -1 : t); } vector<tuple<long long, int> > ans; vector<int> M, D; vector<long long> W; tuple<long long, int> get_ans(int id, long long h) { long long first = 0; int second = 1 << 30; if (h >= int((ring_nodes).size())) { first = h / int((ring_nodes).size()) * seg[0][0]; second = seg[1][0]; } h %= int((ring_nodes).size()); if (h <= 0) { return make_pair(first, second); } int jd = (id + h - 1 + int((ring_nodes).size())) % int((ring_nodes).size()); if (jd < id) { long long tx; int ty; tie(tx, ty) = read(0, 0, int((ring_nodes).size()) - 1, id, int((ring_nodes).size()) - 1); first += tx; second = min(second, ty); tie(tx, ty) = read(0, 0, int((ring_nodes).size()) - 1, 0, jd); first += tx; second = min(second, ty); } else { long long tx; int ty; tie(tx, ty) = read(0, 0, int((ring_nodes).size()) - 1, id, jd); first += tx; second = min(second, ty); } return make_pair(first, second); } long long k; vector<int> Q, to; long long dp[3][20][100005]; void bfs() { int fr = 0, bk = 0; for (int i = 0; i < int(int((ring_nodes).size())); i++) { M[ring_nodes[i]] = 1 << 30; W[ring_nodes[i]] = 0; D[ring_nodes[i]] = 0; Q[bk++] = ring_nodes[i]; while (fr < bk) { int u = Q[fr++]; if (D[u] < k) { long long first; int second; tie(first, second) = get_ans(i, k - D[u]); first += W[u]; second = min(second, M[u]); ans[u] = make_pair(first, second); } else { to.push_back(u); } for (int j = 0; j < int(int((r[u]).size())); j++) { int v = r[u][j]; if (ring_id[v] != -1) { continue; } D[v] = D[u] + 1; W[v] = W[u] + g[u][j]; M[v] = min(M[u], g[u][j]); Q[bk++] = v; dp[0][0][v] = u; dp[1][0][v] = g[u][j]; dp[2][0][v] = g[u][j]; } } } } void solve(int u) { ring_nodes.clear(); dfs(u, u); if (int((ring_nodes).size()) == 0) { return; } reverse((ring_nodes).begin(), (ring_nodes).end()); ring_weight.clear(); for (auto e : (ring_nodes)) { ring_weight.push_back(w[e]); } init(int((ring_nodes).size())); make(0, 0, int((ring_nodes).size()) - 1); bfs(); } tuple<long long, int> get(int u, int k, int l) { long long first = 0; int second = 1 << 30; for (int i = l; i >= 0; i--) { if (dp[0][i][u] == -1 || k < (1 << i)) { continue; } first += dp[1][i][u]; second = min(dp[2][i][u], 1ll * second); u = dp[0][i][u]; k -= 1 << i; } return make_pair(first, second); } int main() { std::ios_base::sync_with_stdio(false); int n; cin >> n >> k; seen.resize(n, -1); f.resize(n); w.resize(n); r.resize(n); g.resize(n); M.resize(n); W.resize(n); D.resize(n); Q.resize(n); ans.resize(n); ring_id.resize(n, -1); for (int i = 0; i < int(n); i++) { cin >> f[i]; } for (int i = 0; i < int(n); i++) { cin >> w[i]; } for (int i = 0; i < int(n); i++) { r[f[i]].push_back(i); g[f[i]].push_back(w[i]); } memset(dp, -1, sizeof(dp)); for (int i = 0; i < int(n); i++) { if (seen[i] == -1) { solve(i); } } if (int((to).size())) { for (int i = 1; (1 << i) <= n; i++) { for (int j = 0; j < int(n); j++) { int t = dp[0][i - 1][j]; if (t != -1) { dp[0][i][j] = dp[0][i - 1][t]; dp[1][i][j] = dp[1][i - 1][j] + dp[1][i - 1][t]; dp[2][i][j] = min(dp[2][i - 1][j], dp[2][i - 1][t]); } } } int l = 31 - __builtin_clz(k); for (auto e : (to)) { ans[e] = get(e, k, l); } } for (auto a : (ans)) { long long first; int second; tie(first, second) = a; cout << first << " " << second << "\n"; } return 0; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; int main(int argc, const char* argv[]) { int n; cin >> n; char str[202]; cin >> str; int i = 0; int max_ans = 0; while (i < n) { int alp['z' - 'a' + 1]; for (int j = 0; j < 'z' - 'a' + 1; ++j) { alp[j] = 0; } while (i < n && str[i] >= 'a' && str[i] <= 'z') { alp[str[i] - 'a'] = 1; i++; } int ans = 0; for (int j = 0; j < 'z' - 'a' + 1; ++j) { ans += alp[j]; } if (ans > max_ans) { max_ans = ans; } i++; } cout << max_ans << endl; return 0; }
1,000
CPP
a,b,c,d = sorted(int(i) for i in input().split()) p1 = a + b p2 = b + c if p1 > c or p2 > d: print("TRIANGLE") elif p1 == c or p2 == d: print("SEGMENT") else: print("IMPOSSIBLE")
900
PYTHON3
def solve(): n, k = map(int, input().split()) s = input() a = [-k] for i in range(n): if s[i] == "1": a.append(i+1) a.append(n+k+1) ans = 0 for i in range(1, len(a)): d = (a[i]-a[i-1])//(k + 1) if d >= 1: ans += d - 1 print(ans) return def main(): t = int(input()) for i in range(t): solve() return if __name__ == "__main__": main()
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; char c[256]; int k, a, b, n; void readInput() { scanf("%d%d%d", &k, &a, &b); scanf("%s", &c); n = strlen(c); } void solve() { int x, y, i, pos = 0, t, tx; x = n / k; tx = x; y = n % k; if (a * k > n || b * k < n) { printf("No solution\n"); return; } for (i = 1; i <= k; i++) { t = y; x = tx; if (y) { x++; y--; } while (x) { printf("%c", c[pos++]); x--; } printf("\n"); } } int main() { readInput(); solve(); return 0; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; int main() { int w, d, u1, h1, u2, h2; cin >> w >> d >> u1 >> h1 >> u2 >> h2; for (int i = d; i > 0; i--) { w = w + i; if (i == h1) { w = w - u1; } if (i == h2) { w = w - u2; } if (w < 0) w = 0; } cout << w << endl; }
800
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2E5 + 5; int n, f[N][5], ans = 0; vector<int> a[N]; bool DFS(int u, int p) { bool child = false; f[u][1] = p != 1; f[u][3] = INT_MAX; for (int v : a[u]) if (v != p) { DFS(v, u); child = true; f[u][1] += min(f[v][1], f[v][2]); f[u][2] += min(f[v][1], f[v][3]); } if (child) { int tmp = 0; for (int v : a[u]) if (v != p) tmp += min(f[v][1], f[v][3]); for (int v : a[u]) if (v != p) f[u][3] = min(f[u][3], tmp - min(f[v][1], f[v][3]) + f[v][1]); } return child; } int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; a[u].push_back(v); a[v].push_back(u); } for (int v : a[1]) DFS(v, 1), ans += f[v][1]; cout << ans << "\n"; return 0; }
2,100
CPP
x = int(input()) count = 0 while(x >= 5): x -= 5 count += 1 if x > 0: count += 1 print(count)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; map<unsigned long long int, int> mp; deque<unsigned long long int> dq; unsigned long long int source; bool y = 0; for (int i = 0; i < n; i++) { unsigned long long int x; cin >> x; if (y == 0) { source = x; y = 1; } mp[x]++; } dq.push_back(source); mp[source]--; while (dq.size() != n) { unsigned long long int x = dq.back(); unsigned long long int y = dq.front(); if (x % 3 == 0 && mp[x / 3] != 0) { dq.push_back(x / 3); mp[x / 3]--; } else if (mp[y * 3] != 0) { dq.push_front(y * 3); mp[y * 3]--; } else if (mp[x * 2] != 0) { dq.push_back(x * 2); mp[x * 2]--; } else if (mp[y / 2] != 0) { dq.push_front(y / 2); mp[y / 2]--; } } for (int i = 0; i < n; i++) { cout << dq.front() << " "; dq.pop_front(); } return 0; }
1,400
CPP
import sys;input=sys.stdin.readline T, = map(int, input().split()) for _ in range(T): p,q = map(int, input().split()) n,m = map(int, input().split()) w,v = map(int, input().split()) if w > v: w,v=v,w n,m=m,n R = 0 for c in range(n+1): if p < c*w: break pp=p-c*w if q >= (n-c)*w: qq = q - (n-c)*w r = min(pp//v + (qq//v), m) + n else: r, qq = divmod(q, w) r = r + min(pp//v, m) + c R = max(R, r) print(R)
1,700
PYTHON3
#include <bits/stdc++.h> using namespace std; struct abc { long long int y, x; }; bool compare(abc a1, abc a2) { if (a1.x == a2.x) return a1.y < a2.y; return a1.x < a2.x; } long long int countDivisors(long long int n) { long long int cnt = 0; for (long long int i = 1; i <= sqrt(n); i++) { if (n % i == 0) { if (n / i == i) cnt++; else cnt = cnt + 2; } } return cnt; } const long long int mod = 1e9 + 7; long long int n, m, x, y, l, r, ans, k, q, w, h; string second, p; map<long long int, long long int> mp; void solve() { cin >> n >> k; long long int a[n], sum = 0; vector<long long int> v; for (long long int i = 0; i < n; i++) { cin >> a[i]; ans += a[i] / 10; long long int x = a[i] % 10; if (x % 10 != 0) x = 10 - x; else x = 0; if (x) v.push_back(x); a[i] += x; sum += (100 - a[i]); } sort(v.begin(), v.end()); for (long long int i = 0; i < v.size(); i++) { if (k >= v[i]) ans++, k -= v[i]; else { k = 0; break; } } ans += min(sum, k) / 10; cout << ans; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); }
1,400
CPP
#include <bits/stdc++.h> long long R = 7 + 1e9, NUMTESTCASE; const int NN = 10 + 1e4; const double pi = acos(-1.0); int di[4] = {1, 0, -1, 0}, dj[4] = {0, 1, 0, -1}, DI[8] = {1, 1, 0, -1, -1, -1, 0, 1}, DJ[8] = {0, 1, 1, 1, 0, -1, -1, -1}; using namespace std; long long cnt[NN]; int main() { for (cin >> NUMTESTCASE; NUMTESTCASE; NUMTESTCASE--) { long long n, x; scanf("%lld", &n); vector<long long> draft, del; for (int i = (0); i <= (n - 1); ++i) { scanf("%lld", &x); if (cnt[x] >= 4) continue; if (cnt[x] % 2) draft.push_back(x); cnt[x]++; del.push_back(x); } for (auto it : del) cnt[it]--; sort(draft.begin(), draft.end()); long long a = draft[0], b; long long last = 1e12, flag; pair<long long, long long> ans = {-1, -1}; for (int i = (0); i <= (draft.size() - 1); ++i) { if (i == 0) continue; b = draft[i]; flag = 4 * (a + b) * (a + b); if (ans.first == -1) { ans = {a, b}; last = flag; } else if (flag * ans.first * ans.second < a * b * last) { ans = {a, b}; last = flag; } a = b; } printf("%lld %lld %lld %lld\n", ans.first, ans.first, ans.second, ans.second); } return 0; }
1,600
CPP
n = int(input()) a = list(map(int, input().split())) mx = 0 nr = 0 for i in range( n ): mx = max(mx, a[ i ]) if( i + 1 >= mx): nr += 1 print (nr)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 300010; long long t, n, a, k, res = 1e18; vector<long long> v[N]; int solve() { scanf("%lld%lld", &n, &k); for (int i = 0; i < n; i++) { scanf("%lld", &a); long long cnt = 0; v[a].push_back(cnt); while (a) { a /= 2; cnt++; v[a].push_back(cnt); } } for (int i = 0; i <= 200000; i++) { sort(v[i].begin(), v[i].end()); if ((long long)v[i].size() >= k) { a = 0; for (int j = 0; j < k; j++) { a += v[i][j]; } res = min(a, res); } } printf("%lld\n", res); return 0; } int main() { return solve(); }
2,200
CPP
#include <bits/stdc++.h> using namespace std; int count1, ans = 0, stud[108][108], deg[108], deg_red[108]; int main() { int n, m, a, b; cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> a >> b; stud[a][b] = 1; stud[b][a] = 1; deg[a]++; deg[b]++; deg_red[a]++; deg_red[b]++; } while (1) { count1 = 0; for (int i = 1; i <= n; i++) { if (deg[i] == 1) { count1++; deg_red[i] = 0; for (int j = 1; j <= n; j++) { if (stud[i][j] == 1) { stud[i][j] = 0; stud[j][i] = 0; deg_red[j]--; } } } } if (count1 == 0) break; else ans++; for (int i = 1; i <= n; i++) deg[i] = deg_red[i]; } cout << ans << "\n"; return 0; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0; char ch = getchar(); bool d = 1; for (; !isdigit(ch); ch = getchar()) if (ch == '-') d = 0; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; return d ? x : -x; } inline void write(long long x) { if (x < 0) x = -x, putchar('-'); if (x >= 10) write(x / 10); putchar('0' + x % 10); } inline void writeln(long long x) { write(x); puts(""); } inline void writep(long long x) { write(x); putchar(' '); } inline unsigned long long rnd() { return ((unsigned long long)rand() << 30 ^ rand()) << 4 | rand() % 4; } const int N = 1e5 + 5, M = 2e5 + 5, mo = 1e9 + 7; int p[M], opt, mn[M]; bool used[M]; vector<long long> v; void init(int n) { for (int i = (int)(2); i <= (int)(n); i++) { if (!used[i]) p[++opt] = mn[i] = i; for (int j = 1; j <= opt && p[j] * i <= n; j++) { used[i * p[j]] = 1; mn[i * p[j]] = p[j]; if (i % p[j] == 0) break; } } for (int i = (int)(2); i <= (int)(n); i++) if (!used[i] && i <= 447) v.push_back(i); } struct tree { int mx[N << 2], a[N]; void buildtree(int k, int l, int r) { if (l == r) { mx[k] = a[l]; return; } int mid = l + r >> 1; buildtree(k << 1, l, mid); buildtree(k << 1 | 1, mid + 1, r); mx[k] = max(mx[k << 1], mx[k << 1 | 1]); } int find(int k, int l, int r, int L, int R) { if (l >= L && r <= R) return mx[k]; int mid = l + r >> 1; if (R <= mid) return find(k << 1, l, mid, L, R); if (L > mid) return find(k << 1 | 1, mid + 1, r, L, R); return max(find(k << 1, l, mid, L, R), find(k << 1 | 1, mid + 1, r, L, R)); } } T[86]; int b[N], pos[M], fac[N], inv[N]; int ksm(int x, int p) { int res = 1; for (; p; p >>= 1, x = (long long)x * x % mo) { if (p & 1) res = (long long)res * x % mo; } return res; } int tot, root[N], las[M], lson[N * 40], rson[N * 40], zyk[N * 40], myh[N * 40]; void ins(int &k, int pre, int l, int r, int pos, int x, int y) { k = ++tot; lson[k] = lson[pre]; rson[k] = rson[pre]; zyk[k] = (long long)zyk[pre] * x % mo; myh[k] = (long long)myh[pre] * y % mo; if (l == r) return; int mid = l + r >> 1; if (pos <= mid) ins(lson[k], lson[pre], l, mid, pos, x, y); else ins(rson[k], rson[pre], mid + 1, r, pos, x, y); } int query(int k, int pre, int l, int r, int L, int R) { if (!k) return 1; if (l >= L && r <= R) return (long long)myh[k] * zyk[pre] % mo; int mid = l + r >> 1; if (R <= mid) return query(lson[k], lson[pre], l, mid, L, R); if (L > mid) return query(rson[k], rson[pre], mid + 1, r, L, R); int ssw = query(lson[k], lson[pre], l, mid, L, R); int ksg = query(rson[k], rson[pre], mid + 1, r, L, R); return (long long)ssw * ksg % mo; } int main() { init(M - 1); int n = read(); for (int i = (int)(1); i <= (int)(n); i++) b[i] = read(); for (int i = (int)(0); i <= (int)((int)v.size() - 1); i++) pos[v[i]] = i; for (int i = (int)(1); i <= (int)(n); i++) { int x = b[i]; b[i] = 1; while (x != 1) { if (mn[x] > 447) b[i] = mn[x]; else T[pos[mn[x]]].a[i]++; x /= mn[x]; } } zyk[0] = myh[0] = 1; for (int i = (int)(fac[0] = inv[0] = 1); i <= (int)(n); i++) { fac[i] = (long long)fac[i - 1] * b[i] % mo; int INV = ksm(b[i], mo - 2); inv[i] = (long long)inv[i - 1] * INV % mo; if (las[b[i]]) ins(root[i], root[i - 1], 1, n, las[b[i]], b[i], INV); else root[i] = root[i - 1]; las[b[i]] = i; } for (int i = (int)(0); i <= (int)((int)v.size() - 1); i++) T[i].buildtree(1, 1, n); int Q = read(), la = 0; while (Q--) { int l = (read() + la) % n + 1, r = (read() + la) % n + 1; if (l > r) swap(l, r); int ans = (long long)fac[r] * inv[l - 1] % mo; int jb = query(root[r], root[l - 1], 1, n, l, r); ans = (long long)ans * jb % mo; for (int i = (int)(0); i <= (int)((int)v.size() - 1); i++) { int ssw = T[i].find(1, 1, n, l, r); ans = (long long)ans * ksm(v[i], ssw) % mo; } writeln(la = ans); } }
2,700
CPP
#!/usr/bin/env python3 import os from sys import stdin, stdout def solve(tc): n, r = map(int, stdin.readline().split()) cnt = 0 if n <= r: cnt += n*(n-1)//2 cnt += 1 else: cnt = (r+1)*r//2 print(cnt) tcs = int(stdin.readline().strip()) tc = 1 while tc <= tcs: solve(tc) tc += 1
1,200
PYTHON3
was = {} dp = {} def solve(id, cnt): if (id == -1): return 0 if (was.get((id, cnt)) != None): return dp[(id, cnt)] else: was[(id, cnt)] = True dp[(id, cnt)] = 10 ** 18 for i in range(0, id + 1): cur = 0 for j in range(i, id + 1): cur = cur * 10 + ord(k[j]) - 48 if (cur < n and k[i] != '0'): dp[(id, cnt)] = min(dp[(id, cnt)], solve(i - 1, cnt + 1) + cur * (n ** cnt)) if (k[id] == '0'): dp[(id, cnt)] = min(dp[(id, cnt)], solve(id - 1, cnt + 1)) return dp[(id, cnt)] n = input() k = input() n = int(n) print(solve(len(k) - 1, 0))
2,000
PYTHON3
t=int(input()) total=m=0 for cases in range(t): l,e=map(int,input().split()) total=total+e-l m=max(total,m) print(m)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 2005; int n, m, l[N], s[N], c[N * 2], f[N * 2][N]; template <typename T> void umax(T& x, T y) { if (x < y) x = y; } int main() { scanf("%d%d", &n, &m); int i; for (i = n; i >= 1; --i) scanf("%d", &l[i]); for (i = n; i >= 1; --i) scanf("%d", &s[i]); for (i = 1; i <= n + m; ++i) scanf("%d", &c[i]); memset(f, 192, sizeof(f)); for (i = 0; i <= n + m; ++i) f[i][0] = 0; int j, k, p; for (i = 1; i <= n; ++i) { for (j = n; j >= 1; --j) umax(f[l[i]][j], f[l[i]][j - 1] + c[l[i]] - s[i]); for (j = l[i], k = n; j < n + m; ++j, k >>= 1) for (p = 0; p <= k; ++p) umax(f[j + 1][p / 2], f[j][p] + p / 2 * c[j + 1]); } printf("%d\n", f[n + m][0]); return 0; }
2,800
CPP
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string a, b, c; cin >> a; for (int i = 0; i < a.size(); i++) { if (i % 2) { b += a[i]; } else { c += a[i]; } } int qq = 0, qqq = 0; if (b.size() > 0) qq = stoi(b); if (c.size() > 0) qqq = stoi(c); cout << (qq + 1) * (qqq + 1) - 2 << '\n'; } return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; const int inf = 2000000000, mod = 1000000007; const double eps = 0.000001; int main() { long long n, s = 0; cin >> n; vector<long long> a(n); for (long long i = 0; i < n; i++) { cin >> a[i]; s += (i + 1) * (a[i] - 1); } cout << s + n; return 0; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; int tu(int val) { return (1 << val); } bool iset(int mask, int id) { if ((mask & tu(id)) != 0) return true; return false; } void doset(int &mask, int id) { mask |= tu(id); } void dounset(int &mask, int id) { mask = mask & (~tu(id)); } template <typename T> string tos(T a) { stringstream ss; string ret; ss << a; ss >> ret; return ret; } int ar[100]; int main() { int n; while (cin >> n) { int v; memset(ar, 0, sizeof ar); ; for (int(i) = (0); (i) < (n); (i)++) { cin >> v; ar[v] = 1; } int ans = 90; int br = 0; for (int(i) = (1); (i) < (91); (i)++) { if (ar[i] == 0) br++; else br = 0; if (br == 15) { ans = i; break; } } cout << ans << endl; } return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; int n, t, res[300005], Cnt; char a; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> n; Cnt = 0; queue<int> Q[2]; for (int i = 1; i <= n; i++) { cin >> a; a -= '0'; if (Q[1 - a].empty()) { Q[a].push(++Cnt); res[i] = Cnt; } else { Q[a].push(Q[1 - a].front()); res[i] = Q[1 - a].front(); Q[1 - a].pop(); } } cout << Cnt << "\n"; for (int i = 1; i <= n; i++) cout << res[i] << " "; cout << "\n"; } }
1,500
CPP
n = int(input()) arr = [0]*n arr[0] = 1 from collections import Counter for i in range(1, n): ini = int(input()) arr[i] = ini arr[ini-1] = ini x = Counter(arr) lis = x.keys() flag = 0 for i in lis: if x[i] <= 3: print("NO") flag = 1 break if flag==0: print("YES")
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int c = 0, ans = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == s[i + 1]) c++; else if (s[i] != s[i + 1]) { ans++; c = 0; } if (c == 5) { ans++; c = 0; } } cout << ans; return 0; }
900
CPP