solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
import sys cnt = 1 arr = [] n, k = map(int, sys.stdin.readline().strip().split()) while cnt <= n: st = str(cnt) arr.append(int(st + st[::-1])) # print(arr[cnt-1]) cnt += 1 cnt = 1 while cnt < n: arr[cnt] = (arr[cnt]+arr[cnt-1])%k cnt += 1 sys.stdout.write(str(arr[n-1]%k))
1,300
PYTHON3
n = int(input()) nums = str(input()) if "1" in nums: print("HARD") else: print("EASY")
800
PYTHON3
#include <bits/stdc++.h> int main() { char s1[100001], s2[100001]; scanf("%s%s", s1, s2); int b1 = 0, b2 = 0, l = strlen(s1); for (int i = 0; i < l; i++) { if (s1[i] < s2[i]) b1++; else if (s1[i] > s2[i]) b2++; } int min = b1, max = b2; if (min > b2) { min = b2; max = b1; } printf("%i", max); }
1,200
CPP
#include <bits/stdc++.h> using namespace std; void sol() { long long n; cin >> n; long long m; cin >> m; vector<pair<long long, long long>> x(m); for (auto &in : x) cin >> in.first >> in.second; long long y[m]; sort(x.begin(), x.end()); y[0] = x[0].first; for (long long i = 1; i < m; ++i) { y[i] = x[i].first; y[i] += y[i - 1]; } long long mx = 0; for (long long i = 0; i < m; ++i) { long long low = -1; long long high = m; long long mid; while (high - low > 1) { mid = (high + low) / 2; if (x[mid].first >= x[i].second) high = mid; else low = mid; } long long cnt = n; high--; high = max(high, m - n - 1); long long ans = y[m - 1]; if (high >= 0) ans -= y[high]; cnt -= (m - high - 1); if (cnt > 0 && high >= i) { ans += x[i].first; cnt--; } ans += x[i].second * cnt; mx = max(mx, ans); } cout << mx << "\n"; } int main() { int tc; scanf("%d", &tc); while (tc--) { sol(); } }
2,000
CPP
import math num_cases = int(input()) for _ in range(num_cases): n,k = list(map(int,input().strip().split())) arr = input().strip() arr = [i for i in arr] ones = 0 zeros = 0 for i in range(k): if (arr[i] == '0'): zeros += 1 elif (arr[i] == '1'): ones += 1 qs = k - (zeros + ones) if (qs < abs(zeros - ones)): print('NO') else: not_poss = 0 start = arr[0] for i in range(1, n-k+1): last = arr[i+k-1] if (last == '?' and start != '?'): arr[i+k-1] = start last = start elif (last != '?' and start != '?' and last != start): not_poss = 1 break if (last != start): if (last=='?' and start=='0'): qs += 1 zeros -= 1 if (last=='?' and start=='1'): qs += 1 ones -= 1 if (last=='0' and start=='?'): qs -= 1 zeros += 1 if (last=='1' and start=='?'): qs -= 1 ones += 1 if (qs < abs(zeros-ones)): not_poss = 1 break start = arr[i] if (not_poss == 1): print('NO') else: print('YES')
1,500
PYTHON3
import sys input = sys.stdin.readline import collections q=int(input()) for _ in range(q): n=int(input()) A=[int(_) for _ in input().split()] AA=list(collections.Counter(A).values()) AA.sort(reverse=True) Ans=[AA[0]] for i in range(1,len(AA)): a=AA[i] if a>=Ans[-1]: Ans.append(max(Ans[-1]-1,1)) else: Ans.append(a) print(sum(set(Ans)))
1,400
PYTHON3
from collections import Counter n = int(input()) l = list(map(int,input().split())) c= Counter(l) if c[1] == 0: print('2 '*n) elif c[2] == 0: print('1 ' * n) else: print('2 1 '+"2 "*(c[2]-1) + "1 "*(c[1]-1))
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const long long int INF = 9e18 + 2e17; const int inf = 2e9; const int N = 1e3 + 22; const double eps = 1e-10; const double PI = 3.14159265; int n, x, y, c[100005], u[100005], v[100005], m = 0, f[100005]; void solve() { cin >> n; for (int i = 1; i < n; i++) cin >> u[i] >> v[i]; for (int i = 1; i <= n; i++) cin >> c[i]; for (int i = 1; i < n; i++) if (c[u[i]] != c[v[i]]) m++, f[v[i]]++, f[u[i]]++; for (int i = 1; i <= n; i++) { if (f[i] == m) { cout << "YES" << '\n' << i; return; } } cout << "NO"; return; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; void solve() { long long int n; cin >> n; int a = sqrt(n); int b = cbrt(n); int c = sqrt(b); cout << a + b - c << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int TC; cin >> TC; while (TC--) solve(); return 0; }
800
CPP
for _ in range(int(input())): n, m = map(int,input().split()) r1 = range(m,m*11,m) r2 = range(m,(n%(m*10))+1,m) s1 = sum(map(lambda x:x%10,r1)) s2 = sum(map(lambda x:x%10,r2)) print((n//(m*10))*s1+s2)
1,200
PYTHON3
n=int(input()) for i in range(n): s=input() if len(s)>10: print(s[0]+str(len(s)-2)+s[-1]) else: print(s) print()
800
PYTHON3
import re def calc_for_q(s): result = 0 q_count = s.count('q') for ch in s: if ch == 'a': result += q_count elif ch == 'q': q_count -= 1 return result s = input().lower() s = re.sub('[^aq]', '', s) try: s = s[s.index('q'):] except: print(0) exit() result = 0 for i in range(len(s)): if s[i] == 'q': result += calc_for_q(s[i:]) print(result)
800
PYTHON3
n = int(input()) a = list(map(int, input().split())) result = 1 flag = False res = 1 for i in a: if i == 1 and flag: result *= res res = 1 if i == 1: flag = True if i == 0 and flag: res += 1 if not flag: print(0) else: print(result)
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; int dp[55][55]; bool go(int inputs[55][55], int x, int y, int len) { if (dp[x][y] == 1) return 1; if (dp[x][y] == -1) return 0; if ((x == len - 1 || y == len - 1) && inputs[x][y] == 1) return dp[x][y] = 1; if ((x == len - 1 || y == len - 1) && inputs[x][y] == 0) return dp[x][y] = 0; bool ans = false; if (inputs[x + 1][y] == 1) ans = go(inputs, x + 1, y, len); if (ans) return dp[x][y] = 1; if (inputs[x][y + 1] == 1) ans = go(inputs, x, y + 1, len); if (ans) return dp[x][y] = 1; dp[x][y] = -1; return 0; } int main() { int td_testCase; cin >> td_testCase; while (td_testCase--) { int n; cin >> n; bool possible = true; int input[55][55]; memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; i++) { string in; cin >> in; for (int j = 0; j < n; j++) { if (in[j] == '1') input[i][j] = 1; else input[i][j] = 0; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (input[i][j] == 1) { if (!go(input, i, j, n)) possible = false; } } } if (possible) cout << "YES" << endl; else cout << "NO" << endl; } }
1,300
CPP
#include <bits/stdc++.h> using namespace std; template <class T> inline T min(T &a, T &b) { return a < b ? a : b; } template <class T> inline T max(T &a, T &b) { return a > b ? a : b; } template <class T> void read(T &x) { char ch; while ((ch = getchar()) && !isdigit(ch)) ; x = ch - '0'; while ((ch = getchar()) && isdigit(ch)) x = x * 10 + ch - '0'; } struct point { int x, y; point() {} point(int _x, int _y) : x(_x), y(_y) {} }; const int Mod = 777777777; const int bd[128] = { 482164403, 768209115, 462063756, 154906374, 36099042, 341766705, 678182556, 621882744, 478771358, 231881111, 175889805, 243630450, 168908523, 671961765, 55761813, 652682670, 773939082, 517628076, 756201264, 124604900, 750976272, 498253248, 676047609, 137170026, 705610017, 495032139, 561797418, 703097347, 500815609, 95984586, 739707108, 265613565, 387099846, 777331779, 594676173, 591219559, 407997044, 208947235, 93337440, 478908360, 685013007, 487033953, 671903001, 39521181, 738490312, 33785059, 465470131, 310453920, 54648783, 346831137, 427694175, 474743430, 705296781, 435828036, 429824745, 663532359, 261388683, 244690731, 533997135, 596108961, 506813013, 371892402, 590145264, 104733162, 143420103, 654339672, 700348950, 685038942, 578826927, 286484229, 501639192, 434962491, 299270097, 27702486, 335375775, 111746817, 565603164, 294926121, 676063665, 735862995, 710035809, 437011960, 668528077, 138765186, 508213986, 615036450, 353784942, 624827616, 343900011, 241289776, 52410890, 72018835, 352406796, 415705878, 4802637, 376367145, 65589678, 333633477, 341834527, 303717460, 282387700, 42951006, 254706039, 423048528, 526429710, 68131467, 669954708, 12787348, 500636381, 317959019, 479433192, 657133515, 416259390, 610216692, 340129188, 44594256, 257373347, 138718678, 530767740, 292922628, 37220268, 605295159, 480722613, 458170419, 30540300, 487159055, 232966794, 149150650}; const int db[256] = { 412133651, 386543325, 139952108, 289303402, 102404925, 317067177, 396414708, 80515854, 663739304, 317300809, 228877044, 493725043, 715317967, 490300965, 315527373, 743539734, 488329191, 553627998, 533025234, 242583957, 706116537, 614109258, 645447222, 523195911, 492109128, 722623041, 111085128, 766395126, 654378921, 691964847, 496688157, 399056049, 654363234, 102052314, 191720088, 473910948, 259736526, 332840025, 388047555, 665791056, 627111387, 139696515, 441456687, 443032569, 283264821, 771641703, 452641455, 511306362, 117572859, 127701891, 721298331, 176520078, 357242229, 611296308, 696994956, 405628839, 429224274, 465336054, 695091546, 689828796, 574648641, 351220905, 507964023, 675326610, 517248963, 453528621, 220301928, 494463186, 681789969, 339589656, 44524053, 417125457, 339589404, 747135963, 341780733, 734158215, 396817281, 21997836, 5728464, 147611205, 456248898, 714128667, 377654949, 3862068, 128418948, 589390074, 304947090, 11703825, 228266073, 127304142, 429215724, 361541124, 521572968, 468358191, 341231688, 65323503, 613778508, 15985323, 291661029, 410970006, 591638112, 349541550, 89967528, 224922159, 361094166, 584206074, 640051812, 324264456, 652625388, 693768537, 11740617, 309238398, 211085469, 194905872, 639416484, 110110707, 296645895, 748118511, 131177718, 511142751, 775975599, 421403409, 475528473, 434685258, 1768977, 80301375, 708023862, 569195676, 56238084, 632887668, 88089750, 631539342, 396695565, 38780154, 695798271, 469819224, 439587099, 69045921, 682966116, 112310856, 64943298, 534475872, 40215357, 389728458, 286368453, 736006257, 501181650, 54829908, 603489402, 338032656, 512182818, 627500097, 462674016, 3103092, 157324491, 43978329, 596818971, 259025598, 9088632, 91991781, 577291428, 211245489, 429471231, 142626330, 172560633, 510907446, 444609585, 758102058, 375112647, 744786693, 276174402, 19259856, 233672418, 745389414, 225772848, 23385663, 324290610, 519804558, 120337812, 402578568, 360676008, 450089262, 551043738, 337388940, 512108856, 28879011, 690040638, 106017282, 558262341, 99972432, 608226003, 612152037, 42414435, 776201013, 39580443, 518796945, 494437752, 583194366, 723936555, 415359657, 309569589, 751104774, 166684527, 249229170, 353120823, 130668327, 753823584, 580966092, 561963717, 543672234, 393846327, 586278000, 327398400, 278403867, 156455586, 363920382, 190245195, 290039148, 547014447, 466218648, 146037150, 585462906, 666008595, 691786683, 374707494, 622498779, 231158277, 685740951, 115612245, 681825249, 545555745, 551718468, 277206615, 640171035, 757727334, 195193908, 658656684, 457760646, 225925875, 505761984, 18685233, 506832921, 112511021, 396846646, 290147622, 113924623, 669986155, 336008070, 63611061, 238586775, 119956662, 616557739, 772784623, 334527774, 410403148, 51933421}; int n, m, dp[300][300][300], C[300][300]; int main() { scanf("%d%d", &n, &m); if (n == 128) { printf("%d\n", bd[m - 1]); return 0; } if (n == 256) { printf("%d\n", db[m - 1]); return 0; } C[0][0] = 1; for (int i = 1; i <= n; i++) { C[i][0] = 1; for (int j = 1; j <= n; j++) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % Mod; } dp[0][n][0] = 1; for (int i = 1; i <= n; i++) for (int j = 0; j <= n; j++) for (int k = 0; k <= m; k++) { for (int l = 0; l <= n - j; l++) if (l == i) dp[i][j][k] = (dp[i][j][k] + (long long)dp[i - 1][j + l][k] * C[j + l][l] % (long long)Mod) % Mod; else if (k >= l) dp[i][j][k] = (dp[i][j][k] + (long long)dp[i - 1][j + l][k - l] * C[j + l][l] % (long long)Mod) % Mod; } printf("%d\n", dp[n][0][m]); }
2,700
CPP
lst=[];k=0;lst_1=[];lst_2=[] num=int(input('')) lst = list(map(int, input().split(" "))) min_ar = min(lst) max_ar = max(lst) max_index = lst.index(max_ar) min_index = lst.index(min_ar) for n in range(len(lst)): if lst[n] == min_ar: lst_1.append(n) if lst[n] == max_ar: lst_2.append(n) min_ar2 = min(lst_2) max_ar1 = max(lst_1) min_index = max_ar1 max_index = min_ar2 if max_index > min_index: k=(max_index-1)+(num-min_index)-1 print(k) else: k=(max_index-1)+(num-min_index) print(k)
800
PYTHON3
"""r1 = input().split() r2 = input().split() r3 = input().split() r4 = input().split()""" flag = False n = [input(), input(), input(), input()] for i in range(1,4): for j in range(1,4): if n[i][j] == n[i][j-1]: if n[i-1][j] == n[i][j] or n[i-1][j-1] == n[i][j]: flag = True break if flag: break if flag: print("YES") else: print("NO")
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; inline void read_string_to_vector(vector<string> &vec) { string str; getline(cin, str); vec.push_back(str); } template <typename T> inline void read_digit_to_vector(vector<T> &vec) { T t; cin >> t; vec.push_back(t); } long long n, l, r, Ql, Qr; long long w[300001]; long long dp[300001][2]; long long ls[300001]; long long rs[300001]; long long res = 1e15; int main() { ios::sync_with_stdio(false); cin >> n >> l >> r >> Ql >> Qr; for (register long long i = 0; i < n; ++i) cin >> w[i]; for (register long long i = 1; i < n + 1; ++i) ls[i] = ls[i - 1] + w[i - 1]; for (register long long i = 1; i < n + 1; ++i) rs[i] = rs[i - 1] + w[n - i]; for (register long long i = 0; i < n + 1; ++i) { long long ck = l * ls[i] + rs[n - i] * r; if (i < n - i) ck += Qr * (n - (i << 1) - 1); if (i > n - i) ck += Ql * ((i << 1) - n - 1); res = min(ck, res); } cout << res << "\n"; return 0; }
1,500
CPP
import sys def func(n,graph,kaunsa,d): cnt = 0 ans = [] for i in range(1, n+1): if len(graph[i])==1: cnt+= 1 if cnt<3: for i in range(n-1): ans.append(i) else: for i in range(1, n+1): if len(graph[i])==1: not_found = str(i) + ' ' + str(graph[i][0]) q = str(graph[i][0]) + ' ' + str(i) # q = q[-1::-1] # q = str(q[0])+' '+str(q[1]) if not_found in d: d[not_found] = 10**10 else: d[q] = 10**10 here = cnt come = 0 for i in kaunsa: if d[i] != 10**10: ans.append(here) here += 1 else: ans.append(come) come += 1 for i in ans: sys.stdout.write(str(i)+'\n') n = int(sys.stdin.readline().rstrip().lstrip()) graph = [[] for i in range(n+1)] kaunsa = [] d = {} for i in range(n-1): u,v = list(map(int,sys.stdin.readline().rstrip().lstrip().split())) graph[u].append(v) graph[v].append(u) d[str(u)+' '+str(v)] = 1 kaunsa.append(str(u)+' '+str(v)) ans = func(n,graph,kaunsa,d)
1,500
PYTHON3
n=int(input()) d=0 for i in range(n): a=list(map(int,input().split())) if a[0]==1 and a[1]==1: d=d+1 elif a[0]==1 and a[2]==1: d=d+1 elif a[1]==1 and a[2]==1: d=d+1 print(d)
800
PYTHON3
for _ in range(int(input())): n=int(input()) a=list(map(int,input().split())) b=list(map(int,input().split())) if sorted(a)==a or len(set(b))>1: print("Yes") else: print("No")
1,300
PYTHON3
n=int(input()) m=[int(i) for i in input().split()] amazing=0 mx=m[0] mn=m[0] for i in range(1,n): if m[i]>mx: mx=m[i] amazing+=1 if m[i]<mn: mn=m[i] amazing+=1 print(amazing)
800
PYTHON3
n=list(input()) n=n[::-1] x=n.index('1') print(n[:x].count('0')+(n[x:].count('1')+1)*(n[x:].count('1')>1)+n[x:].count('0')*2 )
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { string s; cin >> s; string ns = s; sort(s.begin(), s.end()); if (s == ns && s[0] == ns[ns.size() - 1]) { cout << -1 << "\n"; continue; } cout << s << "\n"; } }
900
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 1; map<long long, long long> mp, pm; map<char, long long> hs; vector<long long> v, vc, ve, vv, vd; deque<char> pe; deque<string> ts, ss, st, rs; deque<double> sd, ds; long long a, b, c, d, e, f, cnt, ans, r, n, l, x, y; long double q, t, cnt1, cnt2; string g, h, w, o, m, s; char z, u; bool re, rt; vector<pair<long long, long long> > pr, rp, pp; map<pair<long long, long long>, long long> aw, wa; int main() { cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(0); cin >> f; for (long long j = 0; j < f; j++) { cin >> a; v.clear(); cnt = -1; for (long long i = 0; i < a; i++) { cin >> b; v.push_back(b); if (b % 2 == 0) cnt = i; } if (cnt != -1) cout << 1 << endl << cnt + 1 << endl; else { if (v.size() == 1) cout << -1 << endl; else cout << 2 << endl << 1 << " " << 2 << endl; } } }
800
CPP
#include <bits/stdc++.h> using namespace std; struct choc { long long index; long long price; }; bool cmp(struct choc choc1, struct choc choc2) { return choc1.price > choc2.price; } vector<choc> chocList; int main() { long long n, m, sum = 0; cin >> n; for (long long i = 0; i < n; i++) { long long temp; cin >> temp; sum += temp; struct choc tempC = {i + 1, temp}; chocList.push_back(tempC); } sort(chocList.begin(), chocList.end(), cmp); cin >> m; for (long long i = 0; i < m; i++) { long long temp; cin >> temp; cout << sum - chocList[temp - 1].price << endl; } return 0; }
900
CPP
#include <bits/stdc++.h> using namespace std; int n, m; int dp[3333]; bool d[3333]; int a[3333]; bool f[3333]; int qu[3333]; int main() { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= m; i++) { int x; cin >> x; d[x] = true; } m = 0; for (int i = 0; i <= 1000; i++) if (d[i]) { a[++m] = i - n; } qu[1] = 0; f[1000] = true; int l = 1; int r = 1; while (l <= r) { int u = qu[l]; l++; for (int i = 1; i <= m; i++) { int v = u + a[i]; if (v >= -1000 && v <= 1000) { if (f[v + 1000] == false) { f[v + 1000] = true; qu[++r] = v; dp[v + 1000] = dp[u + 1000] + 1; } } } } int ret = 1000000001; for (int i = -1000; i <= 1000; i++) if (f[i + 1000]) { bool ok = false; for (int j = 1; j <= m; j++) if (i + a[j] == 0) { ok = true; break; } if (ok) ret = min(ret, dp[i + 1000] + 1); } if (ret == 1000000001) cout << -1; else cout << ret; return 0; }
2,300
CPP
#include <bits/stdc++.h> const int N = 40010; using namespace std; inline long long read() { register long long num = 0; register char ch; register bool flag = false; while ((ch = getchar()) == ' ' || ch == '\n' || ch == '\r') ; if (ch == '-') flag = true; else num = ch ^ 48; while ((ch = getchar()) != ' ' && ch != '\n' && ch != '\r' && ~ch) num = num * 10 + (ch ^ 48); if (flag) return -num; return num; } int main() { register int t = read(); while (t--) { register int L = read(), v = read(), l = read(), r = read(); printf("%d\n", L / v - r / v + max(0, l - 1) / v); } return 0; }
1,100
CPP
n, m, k, l = map(int, input().split()) x = n // m ans = (k + l + m - 1) // m if ans * m > n: print(-1) else: print(ans)
1,400
PYTHON3
n=int(input()) arr=list(map(int,input().split())) if n<=2: if n==1: print(1,1) print(0) print(1,1) print(0) print(1,1) print(-1*arr[0]) if n==2: print(1,1) print(-1*arr[0]) print(2,2) print(-1*arr[1]) print(1,2) print(0,0) else: print(1,n-1) for i in range(n-1): k=arr[i]%n arr[i]+=k*(n-1) print(k*(n-1),end=' ') print() print(2,n) for i in range(1,n): k=arr[i]%n arr[i]+=k*(n-1) print(k*(n-1),end=' ') print() print(1,n) for i in arr: print(-1*i,end=' ') print()
1,600
PYTHON3
#include <bits/stdc++.h> using namespace std; long long Pow(long long x, int y) { long long sum = 1; for (int i = 1; i <= y; i++) { sum = sum * x; } return sum; } int main() { int w, m; scanf("%d%d", &w, &m); while (m != 1 && m != 0) { if (m % w != 1 && m % w != 0 && m % w != w - 1) { puts("NO"); return 0; } if (m % w == w - 1) { m = m / w + 1; } else { m = m / w; } } puts("YES"); return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; int sums[500000]; int l[500000]; int r[500000]; int maps[500000]; int num[500000]; void maketree(int i) { if (l[i] == r[i]) { maps[l[i]] = i; return; } l[2 * i + 1] = l[i]; r[2 * i + 1] = (l[i] + r[i]) / 2; sums[2 * i + 1] = r[2 * i + 1] + 1 - l[2 * i + 1]; l[2 * i + 2] = r[2 * i + 1] + 1; r[2 * i + 2] = r[i]; sums[2 * i + 2] = r[2 * i + 2] + 1 - l[2 * i + 2]; maketree(2 * i + 1); maketree(2 * i + 2); return; } void upd(int i, int val) { num[i] = val; int y = maps[i]; sums[y] = val; while (1) { y = (y - 1) / 2; sums[y] = sums[2 * y + 1] + sums[2 * y + 2]; if (y == 0) break; } } int query(int left, int right, int z) { if (l[z] == left && r[z] == right) { return sums[z]; } else if (left >= l[2 * z + 2]) { return query(left, right, 2 * z + 2); } else if (right <= r[2 * z + 1]) { return query(left, right, 2 * z + 1); } else { return query(left, r[2 * z + 1], 2 * z + 1) + query(l[2 * z + 2], right, 2 * z + 2); } } int main() { int n; cin >> n; l[0] = 0; r[0] = n - 1; sums[0] = n; maketree(0); for (int i = 0; i < n; i++) num[i] = 1; int ls = 0; int rs = n; int backwards = 0; int q; cin >> q; for (int i = 0; i < q; i++) { int zz = 0; cin >> zz; if (zz == 1) { int p; cin >> p; if (backwards) { p = rs - ls - p; backwards = 0; } if (2 * p >= (rs - ls)) { backwards = 1; for (int i = 0; i < (rs - ls - p); i++) { upd(ls + p - 1 - i, num[ls + p - 1 - i] + num[ls + p + i]); upd(ls + p + i, 0); } rs = ls + p; } else { for (int i = 0; i < p; i++) { upd(ls + p + i, num[ls + p - 1 - i] + num[ls + p + i]); upd(ls + p - 1 - i, 0); } ls += p; } } else { int l1, r1; cin >> l1 >> r1; if (backwards == 0) { cout << query(ls + l1, ls + r1 - 1, 0) << endl; } else { cout << query(rs - r1, rs - l1 - 1, 0) << endl; } } } }
2,200
CPP
def go(): n, m = [int(i) for i in input().split(' ')] matrix = [] line = [] column = [] for i in range(n): x = 0 matrix.append([]) for j in input(): w = 0 if j == '.': w = 1 x += 1 matrix[i].append(w) line.append(x) for j in range(m): x = 0 for i in range(n): x += matrix[i][j] column.append(x) o = float('inf') # import code # code.interact(local=dict(globals(), **locals())) for i in range(n): for j in range(m): x = line[i] + column[j] if matrix[i][j] == 1: x -= 1 o = min(o, x) return o o = '' q = int(input()) for _ in range(q): o += f'{go()}\n' print(o)
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; while (scanf("%d", &n) == 1) { int S[200000]; long long int ans1, ans2; int count, count1, count2; count1 = count2 = 0; for (int i = 0; i < n; i++) { scanf("%d", S + i); if (S[i] == 0) count1++; else count2++; } ans1 = ans2 = 0; count = 0; for (int i = 0; i < n; i++) { if (S[i] == 0) { ans1 += i - count; count++; } else ans2 += n - i + count; } printf("%lld\n", ans1 > ans2 ? ans2 : ans1); } return 0; }
1,600
CPP
def main(): n, k = map(int, input().split()) line = input() was = False prev = False res = 0 before = 0 spare_sizes = [] sz = 0 for i in line: if not was: if i == "L": before += 1 else: was = True if was: if i == "L": sz += 1 else: if sz > 0: spare_sizes.append(sz) sz = 0 if i == "W": if prev: res += 2 else: res += 1 prev = True else: prev = False if was: before += sz if res == 0 and k > 0: res -= 1 spare_sizes = sorted(spare_sizes) for i in range(len(spare_sizes)): if spare_sizes[i] > k: res += 2*k k = 0 break else: res += 2*(spare_sizes[i]) k -= spare_sizes[i] res += 1 if k == 0: break res += 2*min(k, before) print(res) if __name__ == '__main__': t = int(input()) for i in range(t): main()
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int A[2005]; int maxX = 0; for (int i = 0; i < n; ++i) { cin >> A[i]; maxX += A[i]; } char B[2005][2005]; memset(B, 0, sizeof(B)); int x = -1; int y = 1000; bool up = true; int maxY = 1000; int minY = 1000; for (int i = 0; i < n; ++i) { for (int j = 0; j < A[i]; ++j) { if (up) { ++x; --y; B[y][x] = '/'; if (maxY > y) maxY = y; } else { ++x; ++y; B[y][x] = '\\'; if (minY < y) minY = y; } } if (up) --y; else ++y; up ^= true; } for (int i = maxY; i <= minY; ++i) { for (int j = 0; j < maxX; ++j) { if (B[i][j] == 0) cout << " "; else cout << B[i][j]; } cout << endl; } return 0; }
1,600
CPP
for _ in range(int(input())): a,b = list(map(int, input().split())) array = [(1,8),(9,98),(99,998),(999,9998),(9999,99998),(99999,999998),(999999,9999998), (9999999, 99999998), (99999999, 999999998), (999999999, 9999999998), (9999999999, 99999999998), (99999999999, 999999999998) , (999999999999, 9999999999998)] for i in range(len(array)): if b >= array[i][0] and b <= array[i][1]: k = i break print(k*a)
1,100
PYTHON3
t = int(input()) d = list(map(int, input().split())) s = sum(d) d.sort(reverse=True) count = 0 a = 0 while a<= s//2: a+= d[count] count+=1 print(count)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; bool dp[10][16][500][500]; int n, m; string s[505]; bool f[505][505]; int cnt; bool t[16][2][2]; int main() { cin >> n >> m; for (int i = 0; i < n; i++) { cin >> s[i]; for (int j = 0; j < m; j++) { f[i][j] = s[i][j] == '*'; } } for (int i = 0; i < 16; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { t[i][j][k] = ((i >> (2 * j + k)) & 1); } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { dp[0][15][i][j] = f[i][j]; for (int k = 0; k < 15; k++) { dp[0][k][i][j] = !f[i][j]; } } } for (int sz = 1, p = 1; sz < min(n, m); sz *= 2, p++) { for (int i = 0; i <= n - sz * 2; i++) { for (int j = 0; j <= m - sz * 2; j++) { for (int k = 0; k < 16; k++) { bool ok = true; for (int y = 0; y < 2; y++) { for (int x = 0; x < 2; x++) { if (t[k][y][x] && !dp[p - 1][15][i + sz * y][j + sz * x]) ok = false; if (!t[k][y][x] && !dp[p - 1][k][i + sz * y][j + sz * x]) ok = false; } } dp[p][k][i][j] = ok; if (ok && sz > 1) { cnt++; } } } } } cout << cnt; return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 10; int n; int c[N], result[N]; int exist[30], p[30]; vector<pair<int, int>> G[N]; int main() { cin >> n; for (int i = 1; i <= n; ++i) cin >> c[i]; int q; cin >> q; for (int i = 1; i <= q; ++i) { int li, ri; cin >> li >> ri; G[ri].push_back({li, i}); } for (int i = 1; i <= n; ++i) { int x = c[i], position = i; for (int bit = 20; bit >= 0; --bit) if (x & (1 << bit)) { if (!exist[bit]) { exist[bit] = x; p[bit] = position; break; } if (p[bit] < position) swap(p[bit], position), swap(exist[bit], x); x ^= exist[bit]; } for (auto ss : G[i]) { for (int bit = 20; bit >= 0; --bit) if (p[bit] >= ss.first) result[ss.second] = max(result[ss.second], result[ss.second] ^ exist[bit]); } } for (int i = 1; i <= q; ++i) cout << result[i] << '\n'; }
2,500
CPP
#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { string x = ""; int n; cin >> n; int l = n / 4; int m = n % 4; for (int i = 0; i < l; i++) { x += "abcd"; } char c = 'a'; for (int i = 0; i < m; i++) { x += c; c++; } cout << x << endl; return 0; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); int max = 0, min = 0; int size, leftLimit, rightLimit; cin >> size >> leftLimit >> rightLimit; for (int i = 0; i < leftLimit; i++) min += pow(2, i); min += size - leftLimit; for (int i = 0; i < rightLimit; i++) max += pow(2, i); max += (size - rightLimit) * pow(2, rightLimit - 1); cout << min << ' ' << max; return 0; }
900
CPP
from itertools import product, chain n = int(input()) cake = [list(input()) for _ in range(n)] choc_row, choc_col = [0] * n, [0] * n for r, c in product(range(n), repeat=2): if cake[r][c] == 'C': choc_row[r] += 1 choc_col[c] += 1 ans = sum(c * (c - 1) // 2 for c in chain(choc_row, choc_col)) print(ans)
800
PYTHON3
n, k = map(int, input().split()) types = list(map(int,input().split())) least = n % types[0] tp_box = 1 # find min/max algorithm for i in range(1, k): remainder = n % types[i] if remainder < least: least = remainder tp_box = i + 1 num_box = n // (types[tp_box-1]) print(tp_box, num_box)
1,000
PYTHON3
#include <bits/stdc++.h> using std::vector; vector<int> a; long long heap[1 << 20 + 1], p[1 << 20 + 1], t, h, g, lg[1 << 20 + 1], ans; void pushup(int i) { if (heap[i << 1] == 0 && heap[i << 1 | 1] == 0) p[i] = i; else if (heap[i << 1] == 0 || heap[i << 1] < heap[i << 1 | 1]) p[i] = p[i << 1 | 1]; else p[i] = p[i << 1]; } void del(int i) { if (heap[i << 1] == 0 && heap[i << 1 | 1] == 0) return heap[i] = 0, p[i] = 0, void(); if (heap[i << 1] == 0 || heap[i << 1] < heap[i << 1 | 1]) heap[i] = heap[i << 1 | 1], del(i << 1 | 1), pushup(i); else heap[i] = heap[i << 1], del(i << 1), pushup(i); } int main() { for (int i = 1; i <= 1 << 20; i++) lg[i] = lg[i >> 1] + 1; scanf("%lld", &t); while (t--) { scanf("%lld%lld", &h, &g); ans = 0; a.clear(); for (int i = 1; i < 1 << (h + 1); i++) heap[i] = p[i] = 0; for (int i = 1; i < 1 << h; i++) scanf("%lld", heap + i), ans += heap[i]; for (int i = (1 << h) - 1; i > 0; i--) pushup(i); for (int i = 1; i < 1 << h; i++) while (heap[i] && lg[p[i]] > g) ans -= heap[i], del(i), a.push_back(i); printf("%lld\n", ans); for (int i = 0; i < a.size(); i++) printf("%d ", a[i]); puts(""); } }
2,400
CPP
import sys LI=lambda:list(map(int, sys.stdin.readline().strip('\n').split())) MI=lambda:map(int, sys.stdin.readline().strip('\n').split()) SI=lambda:sys.stdin.readline().strip('\n') II=lambda:int(sys.stdin.readline().strip('\n')) s=SI() n=len(s) print(3) print('R', n-1) print('L', n) print('L', 2)
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000 * 1000 + 5; vector<pair<int, int> > V; bool marked[MAXN]; set<int> S; int sum[MAXN]; void add(int x, int val) { for (x; x < MAXN; x += (x & (-x))) sum[x] += val; } int get(int x) { int t = 0; for (x; x > 0; x -= (x & -x)) t += sum[x]; return t; } int main() { int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { int a; cin >> a; V.push_back(pair<int, int>(a, i)); } for (int i = 1; i <= k; i++) { int a; cin >> a; marked[a] = true; } sort(V.begin(), V.end()); long long ans = 0; for (int i = 0; i < (int)V.size(); i++) { if (marked[V[i].first]) { S.insert(V[i].second); continue; } int left, right; set<int>::iterator x = S.lower_bound(V[i].second); if (x == S.end()) right = n + 1; else right = *x; right--; if (x == S.begin()) left = 1; else { x--; left = *x; left++; } ans += (long long)right - left + 1 - (get(right) - get(left - 1)); add(V[i].second, 1); } cout << ans << endl; return 0; }
2,200
CPP
#include <bits/stdc++.h> using namespace std; void dfs(int l, int r) { if (l > r) return; if (l == r) { cout << l << " "; return; } cout << l + 1 << " " << r << " "; dfs(l + 2, r - 2); cout << l << " " << r - 1 << " "; } int main() { int n; cin >> n; if (n == 2 || n % 4 == 2 || n % 4 == 3) return puts("-1"), 0; if (n == 1) return puts("1"), 0; dfs(1, n); return 0; }
1,400
CPP
n=int(input()) a=list(map(int,input().split()[:n])) b=list(map(int,input().split()[:n-1])) c=list(map(int,input().split()[:n-2])) print(sum(a)-sum(b)) print(sum(b)-sum(c))
1,100
PYTHON3
a = [] r = 0 c = 0 for i in range(5): p = list(map(int, input().split())) a.append(p) if 1 in p: r = abs(p.index(1) - 2) c = abs(i - 2) print(r + c)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int rt[100100], lc[15000000], rc[15000000], tot; long long c[15000000]; int n, q; int a[100100]; int las[100100], le[100100], ri[100100]; set<int> s[100100]; set<int>::iterator it; struct Bit { long long bit[100100]; void init() { for (int i = 0; i <= n; ++i) bit[i] = 0; } void add(int pos, int val) { if (!val) return; while (pos <= n) { bit[pos] += val; pos += pos & (-pos); } } long long sum(int pos) { long long ret = 0; while (pos > 0) { ret += bit[pos]; pos -= pos & (-pos); } return ret; } long long seg(int l, int r) { return sum(r) - sum(l - 1); } } bt; void ins(int &o, int l, int r, int pos, int val) { if (!o) o = ++tot; c[o] += val; if (l == r) return; if (pos <= ((l + r) >> 1)) ins(lc[o], l, ((l + r) >> 1), pos, val); else ins(rc[o], ((l + r) >> 1) + 1, r, pos, val); } long long query(int o, int l, int r, int y1, int y2) { if (!o) return 0; if (y1 <= l && y2 >= r) return c[o]; long long ans = 0; if (y1 <= ((l + r) >> 1)) ans += query(lc[o], l, ((l + r) >> 1), y1, y2); if (y2 > ((l + r) >> 1)) ans += query(rc[o], ((l + r) >> 1) + 1, r, y1, y2); return ans; } long long ask(int pos, int l, int r) { long long ans = 0; if (l > r) return 0; while (pos > 0) { ans += query(rt[pos], 1, n, l, r); pos -= pos & (-pos); } return ans; } void deal(int x, int pos, int val) { if (!val) return; while (x <= n) { ins(rt[x], 1, n, pos, val); x += x & (-x); } } void output() { for (int i = 1; i <= n; ++i) printf("%d%c", a[i], i == n ? '\n' : ' '); for (int i = 1; i <= n; ++i) printf("le[%d]=%d,ri[%d]=%d\n", i, le[i], i, ri[i]); } void change(int pos, int val) { if (a[pos] == val) return; int lef = le[pos], rig = ri[pos]; bt.add(pos, -(pos - lef)); bt.add(rig, -(rig - pos)); if (lef != pos && rig != pos) bt.add(rig, rig - lef); deal(pos, lef, pos - lef); deal(rig, pos, rig - pos); if (lef != pos && rig != pos) deal(rig, lef, lef - rig); le[rig] = lef; ri[lef] = rig; if (le[rig] == pos) le[rig] = rig; if (ri[lef] == pos) ri[lef] = lef; s[a[pos]].erase(pos); s[val].insert(pos); it = s[val].find(pos); if (it == s[val].begin()) lef = pos; else { --it; lef = (*it); ++it; } rig = (++it == s[val].end() ? pos : (*it)); if (lef != pos && rig != pos) bt.add(rig, -(rig - lef)); bt.add(pos, pos - lef); bt.add(rig, rig - pos); if (lef != pos && rig != pos) deal(rig, lef, rig - lef); deal(pos, lef, lef - pos); deal(rig, pos, pos - rig); ri[lef] = pos; le[rig] = pos; le[pos] = lef; ri[pos] = rig; a[pos] = val; } int main() { scanf("%d%d", &n, &q); bt.init(); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); if (las[a[i]]) { bt.add(i, i - las[a[i]]); le[i] = las[a[i]]; ri[las[a[i]]] = i; } else { le[i] = i; } las[a[i]] = i; s[a[i]].insert(i); deal(i, le[i], le[i] - i); } for (int i = 1; i <= n; ++i) if (las[i]) ri[las[i]] = las[i]; while (q--) { int op, l, r; scanf("%d%d%d", &op, &l, &r); if (op == 1) change(l, r); else { long long ans = bt.seg(l, r); ans += ask(r, 1, l - 1) - ask(l - 1, 1, l - 1); printf("%I64d\n", ans); } } return 0; }
2,600
CPP
#include <bits/stdc++.h> int main() { int w, h, n, k; scanf("%d%d%d", &w, &h, &k); n = 2 * k * (w + h - 4 * k + 2); printf("%d", n); }
800
CPP
#include <bits/stdc++.h> using namespace std; int t, k; string s, a, b; int used[26], ans[26], ans1[26], used1[26]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> t; for (int T = 0; T < t; T++) { cin >> k >> s >> a >> b; for (int i = 0; i < k; i++) { used[i] = 0; ans[i] = -1; } int dali = -1; int x = -1; int sz = s.size(); for (int i = 0; i < sz; i++) { int ch = s[i] - 'a'; if (ans[ch] != -1) { if (ans[ch] + 'a' > b[i] || ans[ch] + 'a' < a[i]) { cout << "NO\n"; break; } if (a[i] == b[i]) { if (i == sz - 1) { dali = i; x = 0; } continue; } if (ans[ch] + 'a' == a[i]) { dali = i; x = 1; break; } else if (ans[ch] + 'a' == b[i]) { dali = i; x = 2; break; } else { dali = i; x = 0; break; } } if (ans[ch] == -1) { for (int j = a[i] - 'a' + 1; j < b[i] - 'a'; j++) { if (!used[j]) { used[j] = 1; ans[ch] = j; dali = i; x = 0; break; } } if (x == 0) break; if (!used[a[i] - 'a']) { if (!used[b[i] - 'a']) { if (a[i] == b[i]) { used[a[i] - 'a'] = 1; ans[ch] = a[i] - 'a'; if (i == sz - 1) { dali = i; x = 0; } continue; } else { dali = i; x = 3; break; } } dali = i; used[a[i] - 'a'] = 1; ans[ch] = a[i] - 'a'; x = 1; break; } if (!used[b[i] - 'a']) { dali = i; used[b[i] - 'a'] = 1; ans[ch] = b[i] - 'a'; x = 2; break; } cout << "NO\n"; break; } } if (x == -1) continue; if (x == 0) { int j = 0; for (int i = 0; i < k; i++) { if (ans[i] != -1) continue; while (used[j]) j++; used[j] = 1; ans[i] = j; } cout << "YES\n"; for (int i = 0; i < k; i++) cout << char(ans[i] + 'a'); cout << '\n'; continue; } if (x == 1) { int j = k - 1; bool D = 0; for (int i = dali + 1; i < sz; i++) { if (ans[s[i] - 'a'] != -1) { if (ans[s[i] - 'a'] + 'a' > a[i]) { D = 1; break; } if (ans[s[i] - 'a'] + 'a' == a[i]) { if (i == sz - 1) D = 1; continue; } D = 0; cout << "NO\n"; break; } while (used[j]) j--; if (j + 'a' > a[i]) { D = 1; used[j] = 1; ans[s[i] - 'a'] = j; break; } if (j + 'a' == a[i]) { used[j] = 1; ans[s[i] - 'a'] = j; if (i == sz - 1) D = 1; continue; } cout << "NO\n"; break; } if (dali + 1 < sz && D == 0) continue; for (int i = 0; i < k; i++) { if (ans[i] != -1) continue; while (used[j]) j--; used[j] = 1; ans[i] = j; } cout << "YES\n"; for (int i = 0; i < k; i++) cout << char(ans[i] + 'a'); cout << '\n'; continue; } if (x == 2) { int j = 0; bool D = 0; for (int i = dali + 1; i < sz; i++) { if (ans[s[i] - 'a'] != -1) { if (ans[s[i] - 'a'] + 'a' < b[i]) { D = 1; break; } if (ans[s[i] - 'a'] + 'a' == b[i]) { if (i == sz - 1) D = 1; continue; } D = 0; cout << "NO\n"; break; } while (used[j]) j++; if (j + 'a' < b[i]) { D = 1; used[j] = 1; ans[s[i] - 'a'] = j; break; } if (j + 'a' == b[i]) { used[j] = 1; ans[s[i] - 'a'] = j; if (i == sz - 1) D = 1; continue; } cout << "NO\n"; break; } if (dali + 1 < sz && D == 0) continue; for (int i = 0; i < k; i++) { if (ans[i] != -1) continue; while (used[j]) j++; used[j] = 1; ans[i] = j; } cout << "YES\n"; for (int i = 0; i < k; i++) cout << char(ans[i] + 'a'); cout << '\n'; continue; } int j = k - 1; for (int i = 0; i < k; i++) ans1[i] = ans[i]; for (int i = 0; i < k; i++) used1[i] = used[i]; bool D = 0; used1[a[dali] - 'a'] = 1; ans1[s[dali] - 'a'] = a[dali] - 'a'; for (int i = dali + 1; i < sz; i++) { if (ans1[s[i] - 'a'] != -1) { if (ans1[s[i] - 'a'] + 'a' > a[i]) { D = 1; break; } if (ans1[s[i] - 'a'] + 'a' == a[i]) { if (i == sz - 1) D = 1; continue; } D = 0; break; } while (used1[j]) j--; if (j + 'a' > a[i]) { D = 1; used1[j] = 1; ans1[s[i] - 'a'] = j; break; } if (j + 'a' == a[i]) { used1[j] = 1; ans1[s[i] - 'a'] = j; if (i == sz - 1) D = 1; continue; } break; } if (D == 1 || dali + 1 == sz) { for (int i = 0; i < k; i++) { if (ans1[i] != -1) continue; while (used1[j]) j--; used1[j] = 1; ans1[i] = j; } cout << "YES\n"; for (int i = 0; i < k; i++) cout << char(ans1[i] + 'a'); cout << '\n'; continue; } j = 0; used[b[dali] - 'a'] = 1; ans[s[dali] - 'a'] = b[dali] - 'a'; for (int i = dali + 1; i < sz; i++) { if (ans[s[i] - 'a'] != -1) { if (ans[s[i] - 'a'] + 'a' < b[i]) { D = 1; break; } if (ans[s[i] - 'a'] + 'a' == b[i]) { if (i == sz - 1) D = 1; continue; } D = 0; cout << "NO\n"; break; } while (used[j]) j++; if (j + 'a' < b[i]) { D = 1; used[j] = 1; ans[s[i] - 'a'] = j; break; } if (j + 'a' == b[i]) { used[j] = 1; ans[s[i] - 'a'] = j; if (i == sz - 1) D = 1; continue; } cout << "NO\n"; break; } if (D == 0 && dali + 1 < sz) continue; for (int i = 0; i < k; i++) { if (ans[i] != -1) continue; while (used[j]) j++; used[j] = 1; ans[i] = j; } cout << "YES\n"; for (int i = 0; i < k; i++) cout << char(ans[i] + 'a'); cout << '\n'; continue; } return 0; }
2,300
CPP
def wrongSubtraction(Number, NoOfOperation): while NoOfOperation > 0: if Number % 10 == 0: Number = Number // 10 else: Number -= 1 NoOfOperation -= 1 return Number NumStr1, NumStr2 = input().split(' ') finalNumber = wrongSubtraction(int(NumStr1), int(NumStr2)) print(finalNumber)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, k, a, i; cin >> n >> k; if (k < n / 2 || (n < 2 && k)) { cout << -1 << endl; return 0; } else { long long a = (k + 1) - n / 2; cout << a << " "; a *= 2; n--; while (n--) cout << (a++) << " "; } return 0; }
1,500
CPP
y=input().split() n=int(y[0]) m=int(y[1]) a=int(y[2]) i=0 j=0 if n%a==0: i=int(n/a) else: i=int(n/a)+1 if m%a==0: j=int(m/a) else: j=int(m/a)+1 print(i*j)
1,000
PYTHON3
print(["Poor","Happy"][any(a!=b for a,b in(input().split()for i in range(int(input()))))],"Alex")
1,100
PYTHON3
n,m=map(int,input().split()) a=list(map(int,input().split())) val=0 for x in range(n): if a[x] >= a[m-1] and a[x] !=0 : val+=1 print(val)
800
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimize("O2") #pragma GCC optimize("unroll-loops") using namespace std; typedef double ld; const ld eps = 1e-7; const int inf = 1000000010; const long long INF = 10000000000000010LL; const int mod = 1000000007; const int MAXN = 100010, LOG = 20; int n, m, k, u, v, x, y, t, a, b, l, r; ld A[MAXN]; ld seg[MAXN << 2]; ld lazy1[MAXN << 2]; ld lazy2[MAXN << 2]; ld build(int id, int tl, int tr) { lazy1[id] = 1; if (tr - tl == 1) return seg[id] = A[tl]; int mid = (tl + tr) >> 1; return seg[id] = build(id << 1, tl, mid) + build(id << 1 | 1, mid, tr); } void add_lazy(int id, int tl, int tr, ld Mul, ld Add = 0) { seg[id] *= Mul; lazy1[id] *= Mul; lazy2[id] *= Mul; seg[id] += Add * (tr - tl); lazy2[id] += Add; } void shift(int id, int tl, int tr) { int mid = (tl + tr) >> 1; add_lazy(id << 1, tl, mid, lazy1[id], lazy2[id]); add_lazy(id << 1 | 1, mid, tr, lazy1[id], lazy2[id]); lazy1[id] = 1; lazy2[id] = 0; } void update(int id, int tl, int tr, int l, int r, ld Mul, ld Add) { if (r <= tl || tr <= l) return; if (l <= tl && tr <= r) { add_lazy(id, tl, tr, Mul, Add); return; } shift(id, tl, tr); int mid = (tl + tr) >> 1; update(id << 1, tl, mid, l, r, Mul, Add); update(id << 1 | 1, mid, tr, l, r, Mul, Add); seg[id] = seg[id << 1] + seg[id << 1 | 1]; } ld get(int id, int tl, int tr, int l, int r) { if (r <= tl || tr <= l) return 0; if (l <= tl && tr <= r) return seg[id]; shift(id, tl, tr); int mid = (tl + tr) >> 1; return get(id << 1, tl, mid, l, r) + get(id << 1 | 1, mid, tr, l, r); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> A[i]; build(1, 1, n + 1); while (m--) { cin >> t >> l >> r; r++; if (t == 2) cout << setprecision(5) << fixed << get(1, 1, n + 1, l, r) << '\n'; else { cin >> x >> y; y++; ld s1 = get(1, 1, n + 1, l, r), s2 = get(1, 1, n + 1, x, y); update(1, 1, n + 1, l, r, 1 - 1. / (r - l), s2 / (1. * (r - l) * (y - x))); update(1, 1, n + 1, x, y, 1 - 1. / (y - x), s1 / (1. * (r - l) * (y - x))); } } return 0; }
2,300
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, m, c, ck = 1, ans = 0, a, b; cin >> n >> m; vector<int> v1, v2; for (int i = 0; i < n; i++) cin >> c, v1.push_back(c); for (int i = 0; i < m; i++) cin >> c, v2.push_back(c); a = v1[0]; b = v2[0]; int i = 1, j = 1; while (ck) { if (a == b) { ans++; if (i + 1 > n && j + 1 > m) ck = 0; else { a = v1[i]; b = v2[j]; i++; j++; } } if (a < b) { a += v1[i]; i++; } else if (a > b) { b += v2[j]; j++; } } cout << ans << endl; return 0; }
1,100
CPP
for i in range(int(input())): k=int(input()) a=list(map(int,input().split())) a.sort() a=a[::-1] c=a[0]-a[1] for i in range(1,len(a)-1): if((a[i]-a[i+1])<c): c=a[i]-a[i+1] print(c)
800
PYTHON3
# URL: http://codeforces.com/problemset/problem/268/B def parse_input() -> int: return int(input()) def solve(n: int) -> int: if n == 1: return 1 k = 1 g = 0 while k <= n: g += (n - k) * k k += 1 return g + n if __name__ == '__main__': print(solve(parse_input()))
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 100010; char ch[N]; int n; long long m, k; int a[N]; int main() { int T; cin >> T; while (T--) { cin >> n >> m >> k; for (int i = 1; i <= n; i++) scanf("%d", &a[i]); int flag = 0; for (int i = 1; i < n; i++) { if (a[i + 1] > a[i] + k) { int d = a[i + 1] - a[i]; if (d - k > m) { flag = 1; break; } else { m -= d - k; } } else { m += min(1LL * a[i], a[i] - (a[i + 1] - k)); } } if (flag) cout << "NO" << endl; else cout << "YES" << endl; } return 0; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; long long cur[3005], n, sum[3005]; vector<long long> a[3005]; long long second[3005], ans, k; void go(long long l, long long r) { if (l == r) { long long pre = 0; for (int i = 0; i <= second[l]; i++) { pre += a[l][i]; if (k >= i) ans = max(ans, cur[k - i] + pre); } return; } long long bef[k + 1]; for (int i = 0; i <= k; i++) bef[i] = cur[i]; long long mid = (l + r) / 2; for (int i = mid + 1; i <= r; i++) for (int j = k; j >= second[i]; j--) cur[j] = max(cur[j], cur[j - second[i]] + sum[i]); go(l, mid); for (int i = 0; i <= k; i++) cur[i] = bef[i]; for (int i = l; i <= mid; i++) for (int j = k; j >= second[i]; j--) cur[j] = max(cur[j], cur[j - second[i]] + sum[i]); go(mid + 1, r); for (int i = 0; i <= k; i++) cur[i] = bef[i]; } int main() { ios::sync_with_stdio(false); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> second[i]; a[i].push_back(0); for (int j = 1; j <= second[i]; j++) { long long x; cin >> x; a[i].push_back(x); sum[i] += x; } } go(1, n); cout << ans << '\n'; return 0; }
2,800
CPP
n, k = map(int, input().split()) r = n % k print(n + k - r)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 2002; int n, a[N][N], c[N], p[N], t, A; string s[N]; bool C() { for (int i = 1; i <= n; i++) p[i] = c[i]; sort(p + 1, p + n + 1), t = 0; for (int i = 1; i < n; i++) { t += p[i]; if (t == i * (i - 1) / 2) return false; } return true; } void S(int x) { for (int i = 1; i <= n; i++) c[x] -= a[x][i], c[i] -= a[i][x], swap(a[x][i], a[i][x]), c[x] += a[x][i], c[i] += a[i][x]; } int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> s[i]; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) a[i][j] = s[i][j - 1] - '0', c[i] += a[i][j]; for (int i = 1; i <= n; i++) S(i), A += C(), S(i); if (C()) cout << 0 << ' ' << 1; else if (A) cout << 1 << ' ' << A; else if (n == 6) cout << 2 << ' ' << 18; else cout << -1; return 0; }
3,200
CPP
def method(): n=input() s=int(n,2) k=0 count=0 while 4**k<s: count+=1 k+=1 print(count) method()
1,000
PYTHON3
t=int(input()) for _ in range(t): n=int(input()) a=list(input()) b=list(input()) for i in range(n): a[i]=int(a[i]) b[i]=int(b[i]) p=[] i=0 while(i<n): if a[i]==b[i]: i+=1 else: p+=[i+1] p+=[1] p+=[i+1] i+=1 print(len(p),end=" ") for i in p: print(i,end=" ") print()
1,300
PYTHON3
#!/usr/bin/python import sys from sys import stdin #use ./ex1.py < input_ex1.py def solve(N, C): best_cost = N * C[0] + N * C[1] cur_cost = N * C[0] + N * C[1] min_1 = C[0] min_2 = C[1] use_min_1 = N + 0 use_min_2 = N + 0 for i in range(2, N): if i % 2 == 0: cost = C[i] if cost < min_1: cur_cost -= (min_1 - cost) * (use_min_1 - 1) min_1 = cost if cur_cost < best_cost: best_cost = cur_cost else: cur_cost += cost - min_1 use_min_1 -= 1 else: cost = C[i] if cost < min_2: cur_cost -= (min_2 - cost) * (use_min_2 - 1) min_2 = cost if cur_cost < best_cost: best_cost = cur_cost else: cur_cost += cost - min_2 use_min_2 -= 1 return best_cost def run(): out = "" T = int(input()) for i in range(T): N = int(stdin.readline()) C = [int(x) for x in stdin.readline().split()] print(solve(N, C)) run()
1,500
PYTHON3
import sys from math import * def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) x, y, z = mints() a, b, c = mints() a -= x if a < 0: print("NO") else: w = min(y,a) y -= w a -= w w = min(y,b) y -= w b -= w if y > 0: print("NO") else: w = min(z,a) z -= w a -= w w = min(z,b) z -= w b -= w w = min(z,c) z -= w c -= w if z > 0: print("NO") else: print("YES")
800
PYTHON3
n,k = map(int,input().split()) n = str(n) k2 = 0 i2 = 0 for i in range(len(n)-1,-1,-1): if int(n[i])>= k: k2 = int(n[i]) - k i2 = i break k -= (int(n[i])+1) for i in range(i2): print(n[i],end="") print(k2)
800
PYTHON3
d={} d1={} sm=0 sh=[] n=int(input()) for i in range(n): sh.append(list(map(int,input().split()))) for i in range(n): x=sh[i][0] y=sh[i][1] sm+=d.get(x+y,0) sm+=d1.get(x-y,0) d[x+y]=d.get(x+y,0)+1 d1[x-y]=d1.get(x-y,0)+1 print(sm)
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); for (int i = 21; i >= 0; i--) { long long int res = ((1LL << (i + 1)) - 1) * 1LL * (1LL << (i)); long long int tmp = 0; for (int j = 1; j <= (i + 1); j++) { tmp = tmp + (1LL << (i + j - 1)); } if (tmp != res) continue; if (n % res == 0 && n >= res) { cout << res << endl; return 0; } } }
1,000
CPP
n, x = map(int, input().split()) arr = list(map(int, input().split())) from collections import Counter cnt = Counter(arr) ans = 0 for v in cnt.keys(): if v == v ^ x: ans += cnt.get(v) * (cnt.get(v) - 1) else: ans += cnt.get(v) * cnt.get(v ^ x, 0) print(ans // 2)
1,500
PYTHON3
i = int(input()) s = input() unique = dict() if i < 26: print("NO") else: for char in s: # unique[char.lower()] += 1 if char.lower() not in unique: unique.__setitem__(char.lower(), 1) else: unique[char.lower()] += 1 # print(len(unique)) if len(unique) == 26: print("YES") else: print("NO") # 100 # mKtsiDRJypUieHIkvJaMFkwaKxcCIbBszZQLIyPpCDCjhNpAnYFngLjRpnKWpKWtGnwoSteeZXuFHWQxxxOpFlNeYTwKocsXuCoa
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int alen; char a[1000]; int blen; char b[1000]; int np; int plen[1000]; char p[1000][1000]; void addp() { int c = 0; int at = 0; for (; c != 0 || at < plen[np - 1] || at - 1 < plen[np - 1]; ++at) { if (0 <= at - 1 && at - 1 < plen[np - 1]) c += p[np - 1][at - 1] - '0'; if (at < plen[np - 1]) c += 2 * (p[np - 1][at] - '0'); p[np][at] = '0' + c % 10; c /= 10; } p[np][at] = '\0'; plen[np] = at; ++np; } void add(int i, int j) { while (i >= np || j >= np) addp(); int at = 0, c = 0; for (; c != 0 || at < plen[i] || at < plen[j]; ++at) { if (at < plen[i]) c += p[i][at] - '0'; if (at < plen[j]) c += p[j][at] - '0'; b[at] = '0' + c % 10; c /= 10; } b[at] = '\0'; blen = at; } bool check(int i, int j) { add(i, j); if (alen != blen) return false; for (int i = (0); i < (alen); ++i) if (a[i] != b[i]) return false; return true; } void output(int i, int j) { add(i, j); reverse(b, b + blen); printf("%s", b); } void run() { scanf("%s", a); alen = strlen(a); reverse(a, a + alen); p[0][0] = '1'; p[0][1] = '\0'; plen[0] = 1; np = 1; while (plen[np - 1] < alen) addp(); set<pair<int, int> > skip; vector<int> when; for (int i = (0); i < (np); ++i) for (int j = (i); j < (np); ++j) if (check(i, j)) { when.push_back(i + j); skip.insert(make_pair(i, j)); } sort(when.begin(), when.end()); when.erase(unique(when.begin(), when.end()), when.end()); if (((int)(when).size()) == 0) { printf("NO\n"); return; } printf("%s\n", "YES"); printf("%d\n", min(1000, ((int)(when).size()))); for (int i = (0); i <= (min(1000, ((int)(when).size()) - 1)); ++i) { printf("%d\n", when[i] + 1); } int cnt = 0; for (int i = 0; i <= when.back(); ++i) { int at = 0; for (int j = 0; j <= i; ++j) { while (at < ((int)(when).size()) && i + j > when[at]) ++at; if (at < ((int)(when).size()) && i + j == when[at] && !skip.count(make_pair(j, i))) { ++cnt; } } } cnt = min(cnt, 1000); printf("%d\n", cnt); for (int i = 0; i <= when.back(); ++i) { int at = 0; for (int j = 0; j <= i; ++j) { while (at < ((int)(when).size()) && i + j > when[at]) ++at; if (at < ((int)(when).size()) && i + j == when[at] && cnt > 0 && !skip.count(make_pair(j, i))) { --cnt; output(i, j); puts(""); } } } } int main() { run(); return 0; }
2,600
CPP
#include <bits/stdc++.h> using namespace std; static const int INF = 500000000; template <class T> void debug(T a, T b) { for (; a != b; ++a) cerr << *a << ' '; cerr << endl; } long long int n; pair<long long int, int> factor[100]; long long int tot[100]; int m = 0; long long int ans; bool f(long long int a) { if (a <= 0 || a & 1) return true; return false; } void check(long long int a, long long int b, long long int c) { if (f(b + c - a) || f(c + a - b) || f(a + b - c)) ; else ++ans; } void dfs(int d, long long int a, long long int b, long long int c) { if (d == m) { check(a, b, c); return; } long long int mul1 = 1; for (int i = 0; i < factor[d].second + 1; ++i) { long long int mul2 = 1; for (int j = 0; j < factor[d].second + 1 - i; ++j) { long long int mul3 = tot[d] / mul1 / mul2; dfs(d + 1, a * mul1, b * mul2, c * mul3); mul2 *= factor[d].first; } mul1 *= factor[d].first; } } int main() { cin >> n; if (n % 3) { puts("0"); return 0; } n /= 3; long long int t = n; for (long long int i = 2; i * i <= t; ++i) if (t % i == 0) { factor[m].first = i; long long int tmp = 1; while (t % i == 0) { t /= i; tmp *= i; factor[m].second++; } tot[m] = tmp; ++m; } if (t > 1) factor[m++] = make_pair(t, 1), tot[m - 1] = t; ; dfs(0, 1, 1, 1); cout << ans << endl; return 0; }
2,400
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; long long t, a[N]; long long DC(int l, int r) { if (r - l == 1) return a[l] < t; int mid = l + r >> 1; long long ans = DC(l, mid) + DC(mid, r); for (int i = l, j = mid; i < mid; i++, ans += j - mid) while (j < r && a[j] - a[i] < t) j++; return sort(a + l, a + r), ans; } int main() { ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n; cin >> n >> t; for (int i = 0; i < n; i++) cin >> a[i], a[i] += a[i - 1]; cout << DC(0, n) << endl; return 0; }
1,800
CPP
def is_substring(string, n_letters): for letter_idx in range(n_letters-1): if string[letter_idx] != string[letter_idx+1]: print('YES') print(string[letter_idx] + string[letter_idx+1]) return True return False n_letters = int(input()) string = input() if not is_substring(string, n_letters): print('NO')
1,000
PYTHON3
n=int(input()) l=[] if(n==1 or n%2!=0): print(-1) else: for i in range(1,n,2): print(i+1,end=' ') print(i,end=' ')
800
PYTHON3
from sys import * input = stdin.readline for _ in range(int(input())): n = int(input()) s = input() w = [] for i in range(0,2*n-1,2): w.append(s[i]) w = "".join(w) stdout.write(w+'\n')
800
PYTHON3
from sys import stdin import heapq as hp n,k=map(int,stdin.readline().strip().split()) s=[] for i in range(n): a,b=map(int,stdin.readline().strip().split()) s.append((b,a)) s.sort(reverse=True) s1=[] sm=0 ans=0 for i in range(k): sm+=s[i][1] hp.heappush(s1,s[i][1]) ans=max(sm*s[i][0],ans) for i in range(k,n): x=hp.heappop(s1) if s[i][1]>x: sm-=x sm+=s[i][1] hp.heappush(s1,s[i][1]) else: hp.heappush(s1,x) ans=max(ans,sm*s[i][0]) print(ans)
1,600
PYTHON3
from sys import stdin, stdout def main(): n = int(stdin.readline().strip()) d = {} for i in range(n): name = stdin.readline().strip() d[name] = i outputs = sorted(d.keys(), key = lambda t : d[t], reverse = True) for output in outputs: print(output) if __name__ == '__main__': main()
1,200
PYTHON3
n = int(input()) lst = [] for _ in range(n): s = str(input()) lst.append(s) for i in lst: if len(i) > 10: x = len(i) - 2 i = i[0] + str(x) + i[len(i) - 1] print(i)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 2e6 + 7; int a[N], b[N], c[2 * N]; vector<int> vec[N]; int boy[N], girl[N], used[N]; bool find(int x, int sign) { for (auto i : vec[x]) if (boy[i] == 0) { boy[i] = x; return true; } for (auto i : vec[x]) { if (used[i] == sign) continue; used[i] = sign; if (find(boy[i], sign)) { boy[i] = x; return true; } } return false; } template <typename T> inline void read(T &x) { int f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0'; x *= f; } int main() { int n; read(n); for (int i = 1; i <= n; i++) { read(a[i]); read(b[i]); c[2 * i - 1] = a[i]; c[2 * i] = b[i]; } sort(c + 1, c + 2 * n + 1); int len = unique(c + 1, c + 2 * n + 1) - c - 1; for (int i = 1; i <= n; i++) { a[i] = lower_bound(c + 1, c + len + 1, a[i]) - c; b[i] = lower_bound(c + 1, c + len + 1, b[i]) - c; vec[a[i]].push_back(i); vec[b[i]].push_back(i); } int ans = 0; memset(used, 0, sizeof(used)); for (int i = 1; i <= len; i++) { if (find(i, i)) ans++; if (ans == n) { printf("%d", c[i]); return 0; } } printf("-1"); return 0; }
2,400
CPP
t = int(input()) for _ in range(t): a, b = map(int, input().split()) if 2 * a >= b and 2 * b >= a and (a+b) % 3 == 0: print('YES') else: print('NO')
1,300
PYTHON3
arr = list(map(int, input().rstrip().split())) n = arr[0] m = arr[1] a = arr[2] from math import ceil ans = ceil(m/a) * ceil(n/a) print(ans)
1,000
PYTHON3
#include <bits/stdc++.h> long long exp(long long x, long long y, long long p) { long long res = 1; while (y) { if (y % 2) res = (res * x % p) % p; x = (x * x) % p; y /= 2; } return res; } long long expo(long long x, long long y) { long long res = 1; while (y) { if (y % 2) res = (res * x % ((long long)1e9 + 7)) % ((long long)1e9 + 7); x = (x * x) % ((long long)1e9 + 7); y /= 2; } return res; } long long add(long long a, long long b) { return (a % ((long long)1e9 + 7) + b % ((long long)1e9 + 7) + ((long long)1e9 + 7)) % ((long long)1e9 + 7); } long long sub(long long a, long long b) { return (a % ((long long)1e9 + 7) - b % ((long long)1e9 + 7) + ((long long)1e9 + 7)) % ((long long)1e9 + 7); } long long mul(long long a, long long b) { return ((a % ((long long)1e9 + 7)) * (b % ((long long)1e9 + 7)) + ((long long)1e9 + 7)) % ((long long)1e9 + 7); } long long inv(long long x) { return expo(x, ((long long)1e9 + 7) - 2); } using namespace std; signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; { long long n, i, j, k, x, y; cin >> n >> k; long long a[n]; for (i = 0; i < n; i++) cin >> a[i]; long long ans = 0; for (i = 0; i < n; i++) if (a[i] > k) break; ans += i; long long aa = ans; for (i = n - 1; i > aa; i--) { if (a[i] > k) break; ans++; } cout << ans << "\n"; } return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; long long op[1000010]; int main() { op[1] = 1LL; for (int i = 2; i <= 1000000; i++) { op[i] = op[i - 1] * 2 + 1; op[i] %= 1000000007; } string s; cin >> s; long long cnt = 0; int a = 0; int b = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == 'a') a++; else { cnt += op[a]; cnt %= 1000000007; } } cout << cnt << endl; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; int read() { int f = 1, x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s >= '0' && s <= '9') { x = x * 10 + (s - '0'); s = getchar(); } return x * f; } int n, m, i, j, s, o, k, b, c; int a[200005]; long long sum[200005]; int q2[200005 * 5], q[200005]; int cnt, cnt2; priority_queue<long long> st; int main() { n = read(); k = read(); b = read(); c = read(); for (int i = 1; i <= n; i++) a[i] = read(); sort(a + 1, a + 1 + n); if (b >= 5 * c) { for (int i = 1; i <= n; i++) { sum[i] = sum[i - 1] + a[i]; } long long ans = 1e16; for (int i = k; i <= n; i++) { ans = min(ans, (k * 1ll * a[i] - sum[i] + sum[i - k]) * c); } printf("%lld\n", ans); return 0; } for (int i = 1; i <= n; i++) { for (int j = 0; j < 5; j++) { q2[++cnt2] = a[i] + j; } } sort(q2 + 1, q2 + 1 + cnt2); q2[0] = -0x7f7f7f7f; for (int i = 1; i <= cnt2; i++) { if (q2[i] > q2[i - 1]) { q[++cnt] = q2[i]; } } long long ans = 1e18; for (int re = 0; re < 5; re++) { int pn = 0, ct = 0; long long ad = 0, as = 0; while (!st.empty()) st.pop(); for (int i = 1; i <= cnt; i++) { if ((q[i] % 5 + 5) % 5 == re) { ad += int((q[i] - pn) / 5ll) * 1ll * b; if (st.empty()) ad = 0; while (ct < n && a[ct + 1] <= q[i]) { ct++; int nm = q[i] - a[ct]; long long cs = (int(nm / 5ll) * 1ll * b + (nm % 5ll) * c) + 0ll - ad; st.push(cs); as += cs; } while (st.size() > k) as -= st.top(), st.pop(); if (st.size() == k) { ans = min(ans, as + ad * 1ll * k); } pn = q[i]; } } } printf("%lld\n", ans); return 0; }
2,400
CPP
#include <bits/stdc++.h> using namespace std; template <class T> inline bool uin(T& a, T b) { return a > b ? (a = b, true) : false; } template <class T> inline bool uax(T& a, T b) { return a < b ? (a = b, true) : false; } int main() { ios::sync_with_stdio(false); cin.tie(0); int tt; cin >> tt; for (int qq = 1; qq <= tt; ++qq) { int N; cin >> N; vector<int> max_pos(N + 1, -1), min_pos(N + 1, N + 1), dp(N + 1, 1); dp[0] = 0; for (int i = 0, a; i < N; ++i) { cin >> a; uax(max_pos[a], i); uin(min_pos[a], i); } int dis_ele = 0, last = 0; for (int i = 1; i <= N; ++i) { if (max_pos[i] == -1) continue; dp[i] = (max_pos[last] < min_pos[i] ? dp[last] + 1 : 1); last = i; ++dis_ele; } cout << dis_ele - *max_element(dp.begin(), dp.end()) << '\n'; } }
2,000
CPP
#include <bits/stdc++.h> using namespace std; char word[50]; int getP(char c) { if (c == 'f') return 1; else if (c == 'e') return 2; else if (c == 'd') return 3; else if (c == 'a') return 4; else if (c == 'b') return 5; else return 6; } int main() { scanf("%s", word); int i = 0; long long total = 0; int len = strlen(word); while (i < len && word[i] >= '0' && word[i] <= '9') { total = total * 10 + int(word[i] - '0'); i++; } long long cnt = ((total - 1) / 4) * 16; total = ((total - 1) % 4 + 4) % 4; if (total % 2 == 0) total = 0; else total = 7; long long ans = getP(word[len - 1]) + cnt + total; printf("%lld\n", ans); }
1,200
CPP
# import sys # sys.stdin=open("input.in",'r') # sys.stdout=open("output.out",'w') s=input() c=0 for x in range(len(s)): if s[x]=="A": p=s[0:x].count("Q") q=s[x+1:].count("Q") c+=p*q print(c)
800
PYTHON3
#include <bits/stdc++.h> #define ll long long #define arr(a) new int[a] #define db1(x) cout<<#x<<"="<<x<<'\n' #define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n' #define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n' #define dbV(arr) cout << #arr << "=[ "; \ for(auto i:arr) cout << i << " "; cout << "]\n"; #define rep(i,n) for(int i = 0; i < (n); ++i) #define repF(i,a,n) for(int i = (a); i <= (n); ++i) #define repB(i,a,n) for(int i = (a); i >= (n); --i) #define test(t) int t;cin>>t;while(t--) #define inp(a) int a;cin>>a; #define pii pair<int,int> #define pll pair<ll,ll> #define vi vector<int> #define vl vector<ll> #define umii unordered_map<int,int> #define umll unordered_map<ll,ll> #define mii map<int,int> #define mll map<ll,ll> #define seti set<int> #define setl set<ll> #define pb push_back #define F first #define S second #define MOD 1000000007 #define all(z) z.begin(),z.end() using namespace std; void printArray(int a[],int n) { rep(i,n) cout<<a[i]<<" "; cout<<"\n"; } void printVec(vi v) { int s = v.size(); rep(i,s) cout<<v[i]<<" "; cout<<"\n"; } void printBool(bool b) { if(b) cout<<"YES\n"; else cout<<"NO\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); test(t) { int n;cin>>n; double a = sqrt(n/2.0); double b = sqrt(n/4.0); if(int(a) == a || int(b) == b) { cout<<"YES\n"; } else cout<<"NO\n"; } return 0; }
1,000
CPP
t = int(input()) for _ in range(t): s = input() s = list(s) a = "DA" b = "NET" res = 0 i=0 while i<len(s)-1: if(s[i]!=s[i+1]): res+=1 s.pop(i+1) s.pop(i) i = 0 else: i+=1 if res&1: print(a) else: print(b)
900
PYTHON3
n = int(input()) index = 0 for i in range(n): p, v, t = map(int, input().split()) if (p == v == 1) or (p == t == 1) or (v == t == 1): index += 1 print(index)
800
PYTHON3
a = input() remain = int(a) steps = 0 while True: if int(a) <= 0: print(0) break else: if remain >= 5: steps += 1 remain -= 5 if remain == 4: steps += 1 remain -= 4 if remain == 3: steps += 1 remain -= 3 if remain == 2: steps += 1 remain -= 2 if remain == 1: steps += 1 remain -= 1 if remain == 0: print(steps) break
800
PYTHON3
#include <bits/stdc++.h> using namespace std; bool isprime(int n) { for (int i = 2; i < n; i++) { if (n % i == 0) { return false; } } return true; } int main() { int n; cin >> n; string s; cin >> s; int diff = 1; int i = 0; while (i < n) { cout << s[i]; i = i + diff; diff++; } }
800
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, last; bool can = false; cin >> n; vector<int> v(n); for (int i = 0; i < n; ++i) { cin >> v[i]; if (i == 0) last = v[i]; else if (!can) { if (last % 2 != v[i] % 2) can = true; } } if (can) sort(v.begin(), v.end()); for (auto i : v) cout << i << " "; cout << endl; return 0; }
1,200
CPP
t = int(input()) for i in range(t): s=list(input()) acount=s.count('A') bcount=s.count('B') ccount=s.count('C') length=len(s) ab_min=min(acount,bcount) length=length-(2*ab_min) acount=acount-ab_min bcount=bcount-ab_min length=length-(2*min(bcount,ccount)) if(length==0): print("YES") else: print("NO")
800
PYTHON3