solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
import sys from collections import defaultdict from copy import copy R = lambda t = int: t(input()) RL = lambda t = int: [t(x) for x in input().split()] RLL = lambda n, t = int: [RL(t) for _ in range(n)] def solve(): n = R() b = 1 m = 1 d = 0 S = [] while b < n: if b + 2 * m >= n: S += [n - b - m] m += n - b - m b += m else: if b + 4 * m > n: x = (n-b)//2 - m S += [x] m += x else: S += [m] m *= 2 b += m d += 1 # print(S,b) print(d) for c in S: print(c, end = " ") print() T = R() for _ in range(T): solve()
1,900
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; int main() { cin >> n; switch (n) { case 0: cout << "0"; break; case 1: cout << "1"; break; case 2: cout << "1"; break; case 3: cout << "1"; break; case 4: cout << "2"; break; case 5: cout << "1"; break; case 6: cout << "2"; break; case 7: cout << "1"; break; case 8: cout << "5"; break; case 9: cout << "2"; break; case 10: cout << "2"; break; case 11: cout << "1"; break; case 12: cout << "5"; break; case 13: cout << "1"; break; case 14: cout << "2"; break; case 15: cout << "1"; break; case 16: cout << "14"; break; case 17: cout << "1"; break; case 18: cout << "5"; break; case 19: cout << "1"; break; case 20: cout << "5"; break; case 21: cout << "2"; break; case 22: cout << "2"; break; case 23: cout << "1"; break; case 24: cout << "15"; break; case 25: cout << "2"; break; case 26: cout << "2"; break; case 27: cout << "5"; break; case 28: cout << "4"; break; case 29: cout << "1"; break; case 30: cout << "4"; break; case 31: cout << "1"; break; case 32: cout << "51"; break; case 33: cout << "1"; break; case 34: cout << "2"; break; case 35: cout << "1"; break; case 36: cout << "14"; break; case 37: cout << "1"; break; case 38: cout << "2"; break; case 39: cout << "2"; break; case 40: cout << "14"; break; case 41: cout << "1"; break; case 42: cout << "6"; break; case 43: cout << "1"; break; case 44: cout << "4"; break; case 45: cout << "2"; break; case 46: cout << "2"; break; case 47: cout << "1"; break; case 48: cout << "52"; break; case 49: cout << "2"; break; case 50: cout << "5"; break; case 51: cout << "1"; break; case 52: cout << "5"; break; case 53: cout << "1"; break; case 54: cout << "15"; break; case 55: cout << "2"; break; case 56: cout << "13"; break; case 57: cout << "2"; break; case 58: cout << "2"; break; case 59: cout << "1"; break; case 60: cout << "13"; break; case 61: cout << "1"; break; case 62: cout << "2"; break; case 63: cout << "4"; break; case 64: cout << "267"; break; } return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; int n, m, dp[500005], arr[500005]; vector<pair<int, int>> ans; vector<int> d[500005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; d[u].push_back(v); d[v].push_back(u); } for (int i = 1; i <= n; i++) { cin >> arr[i]; ans.push_back(make_pair(arr[i], i)); } sort(ans.begin(), ans.end()); for (int i = 1; i <= n; i++) dp[i] = 1; int f = 0; for (int i = 0; i < n; i++) { int ps, vl; ps = ans[i].second; vl = ans[i].first; if (dp[ps] != vl) { f = 1; break; } for (auto j : d[ps]) { if (dp[j] == vl) dp[j]++; } if (f) break; } if (f) cout << -1 << endl; else { for (int i = 0; i < n; i++) { cout << ans[i].second << " "; } } return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; struct point { double x, y; }; int sign(double x) { if (x > 1.0E-9) return 1; if (x < -1.0E-9) return -1; return 0; } double area(point P, point Q, point R) { return ((Q.x - P.x) * (R.y - P.y) - (Q.y - P.y) * (R.x - P.x)) / 2.0; } double inprod(point O, point P, point Q) { return (P.x - O.x) * (Q.x - O.x) + (P.y - O.y) * (Q.y - O.y); } point foot(point P, point A, point B) { double t = inprod(A, B, P) / inprod(A, B, B); point ans = {A.x + (B.x - A.x) * t, A.y + (B.y - A.y) * t}; return ans; } bool sscross(point p1, point p2, point p3, point p4) { if (sign(area(p1, p2, p3)) * sign(area(p1, p2, p4)) == 1) return false; if (sign(area(p3, p4, p1)) * sign(area(p3, p4, p2)) == 1) return false; if (max(p1.x, p2.x) < min(p3.x, p4.x) - 1.0E-9) return false; if (max(p1.y, p2.y) < min(p3.y, p4.y) - 1.0E-9) return false; if (max(p3.x, p4.x) < min(p1.x, p2.x) - 1.0E-9) return false; if (max(p3.y, p4.y) < min(p1.y, p2.y) - 1.0E-9) return false; return true; } point cross(point A, point B, point C, point D) { double a = -A.y + B.y, b = A.x - B.x, c = A.x * B.y - A.y * B.x, d = -C.y + D.y, e = C.x - D.x, f = C.x * D.y - C.y * D.x; point ans = {(c * e - b * f) / (a * e - b * d), (c * d - a * f) / (b * d - a * e)}; return ans; } bool linear(point P, point Q, point R) { double tmp = area(P, Q, R); return (tmp > -1.0E-9 && tmp < 1.0E-9); } void print(point P) { cout << P.x << ' ' << P.y << endl; } bool func(point P, point Q, point W1, point W2, point M1, point M2) { if (!sscross(P, Q, W1, W2)) { if (!sscross(P, Q, M1, M2)) return true; if (linear(P, Q, M1) && linear(P, Q, M2)) return true; } if (sign(area(M1, M2, P)) * sign(area(M1, M2, Q)) == 1) { point R = foot(Q, M1, M2); point S = {2.0 * R.x - Q.x, 2.0 * R.y - Q.y}; if (sscross(P, S, M1, M2)) { point T = cross(P, S, M1, M2); if (!sscross(P, T, W1, W2) && !sscross(Q, T, W1, W2)) return true; } } return false; } point read(void) { double x, y; cin >> x >> y; point P = {x, y}; return P; } int main(void) { point P = read(), Q = read(), W1 = read(), W2 = read(), M1 = read(), M2 = read(); cout << (func(P, Q, W1, W2, M1, M2) ? "YES" : "NO") << endl; return 0; }
2,400
CPP
#include <bits/stdc++.h> using namespace std; int a[100]; map<int, int> m; map<int, int>::iterator it; int main() { int n; while (scanf("%d", &n) != EOF) { m.clear(); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (m.find(a[i]) == m.end()) m.insert(pair<int, int>(a[i], 1)); else m[a[i]]++; } int max = -1; int sum = 0; for (it = m.begin(); it != m.end(); it++) { if (it->second > max) max = it->second; sum += it->second; } sum -= max; if (max - 1 <= sum) printf("YES\n"); else printf("NO\n"); } }
1,100
CPP
#!/usr/bin/python import sys a = []; for line in sys.stdin: line = line.strip(); temp = line.split(' '); for x in temp: a.append(int(x)); a_min = min(a) a_max = max(a) pri = (a_min + a_max)/2 #print(a); best = 1000000; for i in range(a_min, a_max): sum = 0; for x in a: sum += abs(x - i); best = min(sum, best); print(best)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAX_N = 200000; const int CNT = 150; const long double DINF = 1e60; pair<int, int> ps[MAX_N]; long double checkasc(int n, long double s) { if (s == 0.0) return 0.0; long double sum = 0.0, r = s; for (int i = 0; i < n; i++) { int ti = ps[i].first, ai = ps[i].second; if (r >= ai) { sum += (long double)ti * ai; r -= ai; } else { sum += (long double)ti * r; break; } } return sum; } long double checkdsc(int n, long double s) { if (s == 0.0) return DINF; long double sum = 0.0, r = s; for (int i = n - 1; i >= 0; i--) { int ti = ps[i].first, ai = ps[i].second; if (r >= ai) { sum += (long double)ti * ai; r -= ai; } else { sum += (long double)ti * r; break; } } return sum; } int main() { int n, t; scanf("%d%d", &n, &t); for (int i = 0; i < n; i++) scanf("%d", &ps[i].second); for (int i = 0; i < n; i++) scanf("%d", &ps[i].first); sort(ps, ps + n); long long asum = 0, atsum = 0; for (int i = 0; i < n; i++) { asum += ps[i].second; atsum += (long long)ps[i].first * ps[i].second; } if (atsum == t * asum) { printf("%lld\n", asum); return 0; } bool f = (atsum > t * asum); long double s0 = 0.0, s1 = asum; for (int cnt = 0; cnt < CNT; cnt++) { long double s = (s0 + s1) / 2; if (f) { if (checkasc(n, s) > t * s) s1 = s; else s0 = s; } else { if (checkdsc(n, s) < t * s) s1 = s; else s0 = s; } } printf("%.15Lf\n", s0); return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; vector<int> m; map<int, int> win; int main() { int x, c, z, max1 = -1, map1 = 1, max2 = 0, maxp2 = 0; cin >> x >> c; for (int i = 0; i < c; i++) { for (int j = 1; j <= x; j++) { cin >> z; if (z > max1) { max1 = z; map1 = j; } } win[map1]++; max1 = -1; map1 = 0; } int mm = -1, p = 0; for (int i = 1; i <= x; i++) { if (win[i] > mm) { mm = win[i]; p = i; } } cout << p << endl; return 0; }
1,100
CPP
n = int(input()) lst_a = [int(el) for el in input().split()] lst_b = [int(el) for el in input().split()] index_a = {a: i + 1 for i, a in enumerate(lst_a)} current_books_lenght = 0 res = [] for i in range(n): depth = index_a[lst_b[i]] - current_books_lenght if depth < 0: res.append(0) else: res.append(depth) current_books_lenght += depth print(' '.join([str(el) for el in res]))
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int k = 1; int balls[5005]; int dominatecolor[5005]; int res[5005]; for (int i = 1; i <= n; i++) { dominatecolor[i] = 0; res[i] = 0; } for (int i = 1; i <= n; i++) cin >> balls[i]; int maxxj = 0; int maxx = 0; for (int i = 1; i <= n;) { dominatecolor[balls[k]]++; if (dominatecolor[balls[k]] > maxx) { maxx = dominatecolor[balls[k]]; maxxj = balls[k]; } if ((dominatecolor[balls[k]] == maxx) && (maxxj > balls[k])) { maxx = dominatecolor[balls[k]]; maxxj = balls[k]; } res[maxxj]++; if (k == n) { k = i; i++; maxx = 0; maxxj = 0; for (int o = 0; o <= n; o++) dominatecolor[o] = 0; } k++; } for (int i = 1; i <= n; i++) cout << res[i] << " "; return 0; }
1,500
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000; const long long INFll = 1ll * INF * INF; const int MOD = 1000000007; const int MAXN = 200001; int k[MAXN]; int bpow(int a, int b) { int res = 1; while (b) { if (b & 1) res = (1ll * res * a) % MOD; b >>= 1; a = (1ll * a * a) % MOD; } return res; } int main() { int m, n = 1; scanf("%d", &m); for (int i = 0; i < m; ++i) { int p; scanf("%d", &p); n = (1ll * n * p) % MOD; ++k[p]; } bool sqr = true; for (int i = 2; i < MAXN; ++i) sqr &= (k[i] % 2 == 0); int ans; if (!sqr) { bool f = false; ans = n; for (int i = 2; i < MAXN; ++i) { if (!k[i]) continue; if (!f && (k[i] % 2)) { ans = bpow(ans, (k[i] + 1) / 2); f = true; } else ans = bpow(ans, k[i] + 1); } } else { ans = 1; for (int i = 2; i < MAXN; ++i) if (k[i]) ans = (1ll * ans * bpow(i, k[i] / 2)) % MOD; for (int i = 2; i < MAXN; ++i) if (k[i]) ans = bpow(ans, k[i] + 1); } cout << ans << endl; return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; int n, m, t; int ans = 0, total = 0, sum = 0; string s, c, h, s1; int main() { scanf("%d", &t); while (t--) { scanf("%d", &n); cin >> s; int len = s.size(); h = s; sum = 1; for (int i = 2; i <= n; i++) { s1 = s; c = s.substr(0, i - 1); if ((n - i + 1) % 2 == 1) reverse(c.begin(), c.end()); s1 = s.substr(i - 1, n - i + 1) + c; if (s1 < h) { h = s1; sum = i; } } cout << h << endl << sum << endl; } return 0; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int odds = 0; long long int r, g, b, w; cin >> r >> g >> b >> w; long long int value = min({r, g, b}); if (r % 2 == 1) odds++; if (g % 2 == 1) odds++; if (b % 2 == 1) odds++; if (w % 2 == 1) odds++; if (odds <= 1) cout << "Yes" << "\n"; else if (value != 0) { r--; b--; w--; g--; odds = 0; if (r % 2 == 1) odds++; if (g % 2 == 1) odds++; if (b % 2 == 1) odds++; if (w % 2 == 1) odds++; if (odds <= 1) cout << "Yes" << "\n"; else cout << "No" << "\n"; } else cout << "No" << "\n"; } return 0; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; using Long = long long; int main(void) { ios_base::sync_with_stdio(0); cin.tie(0); Long n, k; cin >> n >> k; if (k * (k - 1) < n) cout << "NO\n"; else { cout << "YES\n"; int g = 0; for (int i = 0; i < n; ++i) { g += (i % k == 0); cout << i % k + 1 << " " << (g + i % k) % k + 1 << "\n"; } } return 0; }
1,700
CPP
import bisect k=int(input()) s=input() n=len(s) z=[] o=[] ind=[] for i in range(len(s)): if int(s[i])==0: if len(z)==0: z.append(1) else: z.append(z[-1]+1) else: z.append(0) if len(o)==0: o.append(1) ind.append(i) else: o.append(o[-1]+1) ind.append(i) ans=0 if k==0: for i in range(n): if z[i]==0: if i>0: ans=ans+(z[i-1]*(z[i-1]+1))//2 if z[-1]>0: ans=ans+(z[-1]*(z[-1]+1))//2 else: m=0 for i in range(n-1,-1,-1): if z[i]>0: m=max(m,z[i]) z[i]=m else: m=0 for i in range(len(o)): j=bisect.bisect_right(o,o[i]+k-1) if o[j-1]==o[i]+k-1: x=ind[j-1] y=ind[i] a,b=0,0 if y>0: if z[y-1]>0: ans=ans+(z[y-1]) a=z[y-1] if x<n-1: if z[x+1]>0: ans=ans+(z[x+1]) b=z[x+1] ans=ans+1+a*b else: break print(ans)
1,600
PYTHON3
n = int(input()) char = [0 for i in range(n)] for i in range(n): char[i] = int(input()) output = 1 for i in range(n-1): if char[i] == char[i+1]: continue else: output+=1 print(output)
800
PYTHON3
def ceil(x,y): if(x//y-x/y==0): return x//y else: return (x//y+1) t = int(input()) for _ in range(t): n = int(input()) x = ceil(n,4) res = "9"*(n-x) + "8"*x print(res)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { int aa, bb, cc; cin >> aa >> bb >> cc; int a = max({aa, bb, cc}); int c = min({aa, bb, cc}); int b = aa + bb + cc - a - c; int ans = 0; if (a > 0) { a--; ans++; } if (b > 0) { ans++; b--; } if (c > 0) { c--; ans++; } if (a > 0 && b > 0) { a--, b--; ans++; } if (a > 0 && c > 0) { a--, c--; ans++; } if (c > 0 && b > 0) { c--, b--; ans++; } if (a > 0 && b > 0 && c > 0) { a--, b--, c--; ans++; } cout << ans << '\n'; } return 0; }
900
CPP
t=int(input()) for i in range(t): n=int(input()) p=bin(n)[2:] k=len(p) j=0 s=0 while(j<len(p)): if p[j]=="1": s+=2**k-1 k+=-1 j+=1 print(s)
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; const double PI = 3.14159265358979323846264338327950288419716939937510582097494459230; struct debugger { static void call(const char *arg) {} template <typename T, typename... aT> static void call(const char *it, T a, aT... rest) { string b; for (; *it && (*it) != ','; it++) { b += *it; } cerr << b << " = " << a << " "; call(++it, rest...); } }; int main() { int n, b, m; cin >> n >> b; int ar[n]; m = b; for (int i = 0; i < (n); i++) { cin >> ar[i]; } for (int i = 0; i < (n); i++) { int c = b / ar[i], r = b % ar[i]; for (int j = (i + 1); j < (n); j++) { int w = ar[j] * c; w = w + r; m = max(w, m); } } cout << m; }
1,400
CPP
n, m = list(map(int, input().split())) total = 0 while n > 0: total += 1 n -= 1 if(total % m == 0): n += 1 print(total)
900
PYTHON3
s=input() n=int(s,2) j=0 while 4**j<n: j+=1 print(j)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<pair<int, int>> points; int ansx, ansy; int ans = 1e9; int check(int x, int y) { x = max(x, 0); y = max(y, 0); int ret = 0; int z = x - y; for (auto it : points) { int X = it.first; int Y = it.second; int Z = X - Y; ret = max(ret, abs(x - X)); ret = max(ret, abs(y - Y)); ret = max(ret, abs(z - Z)); } if (ret < ans && (x != 0 || y != 0)) { ans = ret; ansx = x; ansy = y; } return ret; } int bruteforce() { int xmax = -1e9, ymax = -1e9, zmax = -1e9; int xmin = 1e9, ymin = 1e9, zmin = 1e9; for (auto it : points) { int x = it.first; int y = it.second; int z = x - y; xmax = max(xmax, x); ymax = max(ymax, y); zmax = max(zmax, x - y); xmin = min(xmin, x); ymin = min(ymin, y); zmin = min(zmin, x - y); } int ans = 1e9; for (int x = xmin; x < (xmax + 1); ++x) for (int y = ymin; y < (ymax + 1); ++y) { ans = min(ans, check(x, y)); } return ans; } int main() { cin.sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit); int n; cin >> n; int xmax = -1e9, ymax = -1e9, zmax = -1e9; int xmin = 1e9, ymin = 1e9, zmin = 1e9; for (int i = 0; i < (n); ++i) { string s; cin >> s; int x = 0, y = 0; for (char c : s) { if (c == 'B') ++x; else ++y; } points.emplace_back(x, y); xmax = max(xmax, x); ymax = max(ymax, y); zmax = max(zmax, x - y); xmin = min(xmin, x); ymin = min(ymin, y); zmin = min(zmin, x - y); } int lo = -1, hi = 1e9; check(0, 1); check(1, 0); check(1, 1); while (hi - lo > 1) { int mid = (lo + hi) / 2; bool ok = false; for (int i = 0; i < (12); ++i) { int x, y; if (i == 0) { x = xmax - mid; y = ymax - mid; } else if (i == 1) { x = xmax - mid; y = x - zmax + mid; } else if (i == 2) { y = ymax - mid; x = y + zmax - mid; } else if (i == 3) { x = xmax - mid; y = ymin + mid; } else if (i == 4) { x = xmax - mid; y = x - zmin - mid; } else if (i == 5) { y = ymax - mid; x = y + zmin + mid; } else if (i == 6) { x = xmin + mid; y = ymax - mid; } else if (i == 7) { x = xmin + mid; y = x - zmax + mid; } else if (i == 8) { y = ymin + mid; x = y + zmax - mid; } else if (i == 9) { x = xmin + mid; y = ymin + mid; } else if (i == 10) { x = xmin + mid; y = x - zmin - mid; } else if (i == 11) { y = ymin + mid; x = y + zmin + mid; } x = max(x, 0); y = max(y, 0); int ret = 1e9; for (int a = -1; a < (2); ++a) for (int b = -1; b < (2); ++b) ret = min(ret, check(x + a, y + b)); if (ret <= mid) { ok = true; break; } } if (ok) { hi = mid; } else { lo = mid; } } for (int i = -1; i < (2); ++i) for (int j = -1; j < (2); ++j) check(ansx + i, ansy + j); cout << ans << endl; assert(ans <= 1e6); assert(ansx >= 0); assert(ansy >= 0); assert(ansx > 0 || ansy > 0); assert(check(ansx, ansy) == ans); for (int i = 0; i < (ansx); ++i) cout << 'B'; for (int i = 0; i < (ansy); ++i) cout << 'N'; cout << endl; }
2,600
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1010; const int dx[5] = {0, 1, 0, -1, 0}; const int dy[5] = {0, 0, 1, 0, -1}; int n, m, k; int col[N][N]; int dis[50][N][N]; int vis[N][N]; int cvis[60]; struct ha { int x, y; }; queue<ha> q; vector<ha> a[60]; void Bfs(int c) { memset(vis, 0, sizeof(vis)); memset(cvis, 0, sizeof(cvis)); for (int i = 0; i < a[c].size(); i++) q.push(a[c][i]), dis[c][a[c][i].x][a[c][i].y] = 0, vis[a[c][i].x][a[c][i].y] = 1; while (!q.empty()) { ha now = q.front(); q.pop(); int x = now.x, y = now.y; for (int i = 1; i <= 4; i++) { int tx = x + dx[i], ty = y + dy[i]; if (tx < 1 || tx > n || ty < 1 || ty > m) continue; if (!vis[tx][ty]) { vis[tx][ty] = 1; dis[c][tx][ty] = dis[c][x][y] + 1; q.push((ha){tx, ty}); } } if (!cvis[col[x][y]]) { cvis[col[x][y]] = 1; for (int i = 0; i < a[col[x][y]].size(); i++) { ha to = a[col[x][y]][i]; int tx = to.x, ty = to.y; if (!vis[tx][ty]) { vis[tx][ty] = 1; dis[c][tx][ty] = dis[c][x][y] + 1; q.push(to); } } } } } int main() { memset(dis, 0x3f, sizeof(dis)); scanf("%d %d %d", &n, &m, &k); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf("%d", &col[i][j]), a[col[i][j]].push_back((ha){i, j}); for (int i = 1; i <= k; i++) Bfs(i); int q; scanf("%d", &q); for (int i = 1; i <= q; i++) { int x, y, X, Y; scanf("%d %d %d %d", &x, &y, &X, &Y); int Ans = abs(x - X) + abs(y - Y); for (int j = 1; j <= k; j++) Ans = min(Ans, dis[j][x][y] + dis[j][X][Y] + 1); printf("%d\n", Ans); } }
2,600
CPP
#include <bits/stdc++.h> using namespace std; constexpr long double m_pi = 3.1415926535897932L; constexpr long long MOD = 1000000007; constexpr long long INF = 1LL << 61; constexpr long double EPS = 1e-10; string operator*(const string& s, int k) { if (k == 0) return ""; string p = (s + s) * (k / 2); if (k % 2 == 1) p += s; return p; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } struct Edge { long long to, cap, rev; Edge(long long _to, long long _cap, long long _rev) { to = _to; cap = _cap; rev = _rev; } }; void add_edge(vector<vector<Edge>>& G, long long from, long long to, long long cap, bool revFlag, long long revCap) { G[from].push_back(Edge(to, cap, (long long)G[to].size())); if (revFlag) G[to].push_back(Edge(from, revCap, (long long)G[from].size() - 1)); } long long max_flow_dfs(vector<vector<Edge>>& G, long long v, long long t, long long f, vector<bool>& used) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); ++i) { Edge& e = G[v][i]; if (!used[e.to] && e.cap > 0) { long long d = max_flow_dfs(G, e.to, t, min(f, e.cap), used); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } long long max_flow(vector<vector<Edge>>& G, long long s, long long t) { long long flow = 0; for (;;) { vector<bool> used(G.size()); for (long long(i) = (long long)(0); i < (long long)(used.size()); i++) used[i] = false; long long f = max_flow_dfs(G, s, t, INF, used); if (f == 0) { return flow; } flow += f; } } void BellmanFord(vector<vector<Edge>>& G, long long s, vector<long long>& d, vector<long long>& negative) { d.resize(G.size()); negative.resize(G.size()); for (long long(i) = (long long)(0); i < (long long)(d.size()); i++) d[i] = INF; for (long long(i) = (long long)(0); i < (long long)(d.size()); i++) negative[i] = false; d[s] = 0; for (long long(k) = (long long)(0); k < (long long)(G.size() - 1); k++) { for (long long(i) = (long long)(0); i < (long long)(G.size()); i++) { for (long long(j) = (long long)(0); j < (long long)(G[i].size()); j++) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; } } } } for (long long(k) = (long long)(0); k < (long long)(G.size() - 1); k++) { for (long long(i) = (long long)(0); i < (long long)(G.size()); i++) { for (long long(j) = (long long)(0); j < (long long)(G[i].size()); j++) { if (d[i] != INF && d[G[i][j].to] > d[i] + G[i][j].cap) { d[G[i][j].to] = d[i] + G[i][j].cap; negative[G[i][j].to] = true; } if (negative[i] == true) negative[G[i][j].to] = true; } } } } void Dijkstra(vector<vector<Edge>>& G, long long s, vector<long long>& d) { d.resize(G.size()); for (long long(i) = (long long)(0); i < (long long)(d.size()); i++) d[i] = INF; d[s] = 0; priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> q; q.push(make_pair(0, s)); while (!q.empty()) { pair<long long, long long> a = q.top(); q.pop(); if (d[a.second] < a.first) continue; for (long long(i) = (long long)(0); i < (long long)(G[a.second].size()); i++) { Edge e = G[a.second][i]; if (d[e.to] > d[a.second] + e.cap) { d[e.to] = d[a.second] + e.cap; q.push(make_pair(d[e.to], e.to)); } } } } void WarshallFloyd(vector<vector<Edge>>& G, vector<vector<long long>>& d) { d.resize(G.size()); for (long long(i) = (long long)(0); i < (long long)(d.size()); i++) d[i].resize(G.size()); for (long long(i) = (long long)(0); i < (long long)(d.size()); i++) { for (long long(j) = (long long)(0); j < (long long)(d[i].size()); j++) { d[i][j] = ((i != j) ? INF : 0); } } for (long long(i) = (long long)(0); i < (long long)(G.size()); i++) { for (long long(j) = (long long)(0); j < (long long)(G[i].size()); j++) { chmin(d[i][G[i][j].to], G[i][j].cap); } } for (long long(i) = (long long)(0); i < (long long)(G.size()); i++) { for (long long(j) = (long long)(0); j < (long long)(G.size()); j++) { for (long long(k) = (long long)(0); k < (long long)(G.size()); k++) { chmin(d[j][k], d[j][i] + d[i][k]); } } } } bool tsort(vector<vector<Edge>>& graph, vector<long long>& order) { int n = graph.size(), k = 0; vector<long long> in(n); for (auto& es : graph) for (auto& e : es) in[e.to]++; priority_queue<long long, vector<long long>, greater<long long>> que; for (long long(i) = (long long)(0); i < (long long)(n); i++) if (in[i] == 0) que.push(i); while (que.size()) { int v = que.top(); que.pop(); order.push_back(v); for (auto& e : graph[v]) if (--in[e.to] == 0) que.push(e.to); } if (order.size() != n) return false; else return true; } class lca { public: const int n = 0; const int log2_n = 0; std::vector<std::vector<int>> parent; std::vector<int> depth; lca() {} lca(const vector<vector<Edge>>& g, int root) : n(g.size()), log2_n(log2(n) + 1), parent(log2_n, std::vector<int>(n)), depth(n) { dfs(g, root, -1, 0); for (int k = 0; k + 1 < log2_n; k++) { for (int v = 0; v < (int)g.size(); v++) { if (parent[k][v] < 0) parent[k + 1][v] = -1; else parent[k + 1][v] = parent[k][parent[k][v]]; } } } void dfs(const vector<vector<Edge>>& g, int v, int p, int d) { parent[0][v] = p; depth[v] = d; for (auto& e : g[v]) { if (e.to != p) dfs(g, e.to, v, d + 1); } } int get(int u, int v) { if (depth[u] > depth[v]) std::swap(u, v); for (int k = 0; k < log2_n; k++) { if ((depth[v] - depth[u]) >> k & 1) { v = parent[k][v]; } } if (u == v) return u; for (int k = log2_n - 1; k >= 0; k--) { if (parent[k][u] != parent[k][v]) { u = parent[k][u]; v = parent[k][v]; } } return parent[0][u]; } }; void visit(const vector<vector<Edge>>& g, int v, vector<vector<long long>>& scc, stack<long long>& S, vector<long long>& inS, vector<long long>& low, vector<long long>& num, int& time) { low[v] = num[v] = ++time; S.push(v); inS[v] = true; for (decltype((g[v]).begin()) e = (g[v]).begin(); e != (g[v]).end(); ++e) { int w = e->to; if (num[w] == 0) { visit(g, w, scc, S, inS, low, num, time); low[v] = min(low[v], low[w]); } else if (inS[w]) low[v] = min(low[v], num[w]); } if (low[v] == num[v]) { scc.push_back(vector<long long>()); while (1) { int w = S.top(); S.pop(); inS[w] = false; scc.back().push_back(w); if (v == w) break; } } } void stronglyConnectedComponents(const vector<vector<Edge>>& g, vector<vector<long long>>& scc) { const int n = g.size(); vector<long long> num(n), low(n); stack<long long> S; vector<long long> inS(n); int time = 0; for (long long(u) = (long long)(0); u < (long long)(n); u++) if (num[u] == 0) visit(g, u, scc, S, inS, low, num, time); } class UnionFind { vector<int> data; long long num; public: UnionFind(int size) : data(size, -1), num(size) {} bool unite(int x, int y) { x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } num -= (x != y); return x != y; } bool findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } long long size(int x) { return -data[root(x)]; } long long numSet() { return num; } }; template <typename T, typename F> class SegmentTree { private: T identity; F merge; long long n; vector<T> dat; public: SegmentTree(F f, T id, vector<T> v) : merge(f), identity(id) { int _n = v.size(); n = 1; while (n < _n) n *= 2; dat.resize(2 * n - 1, identity); for (long long(i) = (long long)(0); i < (long long)(_n); i++) dat[n + i - 1] = v[i]; for (int i = n - 2; i >= 0; i--) dat[i] = merge(dat[i * 2 + 1], dat[i * 2 + 2]); } SegmentTree(F f, T id, int _n) : merge(f), identity(id) { n = 1; while (n < _n) n *= 2; dat.resize(2 * n - 1, identity); } void set_val(int i, T x) { i += n - 1; dat[i] = x; while (i > 0) { i = (i - 1) / 2; dat[i] = merge(dat[i * 2 + 1], dat[i * 2 + 2]); } } T query(int l, int r) { T left = identity, right = identity; l += n - 1; r += n - 1; while (l < r) { if ((l & 1) == 0) left = merge(left, dat[l]); if ((r & 1) == 0) right = merge(dat[r - 1], right); l = l / 2; r = (r - 1) / 2; } return merge(left, right); } }; class SumSegTree { public: long long n, height; vector<long long> dat; SumSegTree(long long _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<long long>(2 * n - 1, 0); } void add(long long i, long long x) { i += n - 1; dat[i] += x; while (i > 0) { i = (i - 1) / 2; dat[i] += x; } } long long sum(long long l, long long r) { long long ret = 0; l += n - 1; r += n - 1; while (l < r) { if ((l & 1) == 0) ret += dat[l]; if ((r & 1) == 0) ret += dat[r - 1]; l = l / 2; r = (r - 1) / 2; } return ret; } }; class RmqTree { private: long long _find(long long a, long long b, long long k, long long l, long long r) { if (r <= a || b <= l) return INF; if (a <= l && r <= b) return dat[k]; else { long long s1 = _find(a, b, 2 * k + 1, l, (l + r) / 2); long long s2 = _find(a, b, 2 * k + 2, (l + r) / 2, r); return min(s1, s2); } } public: long long n, height; vector<long long> dat; RmqTree(long long _n) { n = 1; height = 1; while (n < _n) { n *= 2; height++; } dat = vector<long long>(2 * n - 1, INF); } void update(long long i, long long x) { i += n - 1; dat[i] = x; while (i > 0) { i = (i - 1) / 2; dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]); } } long long find(long long a, long long b) { return _find(a, b, 0, 0, n); } }; void divisor(long long n, vector<long long>& ret) { for (long long i = 1; i * i <= n; i++) { if (n % i == 0) { ret.push_back(i); if (i * i != n) ret.push_back(n / i); } } sort(ret.begin(), ret.end()); } void prime_factorization(long long n, vector<pair<long long, long long>>& ret) { for (long long i = 2; i * i <= n; i++) { if (n % i == 0) { ret.push_back({i, 0}); while (n % i == 0) { n /= i; ret[ret.size() - 1].second++; } } } if (n != 1) ret.push_back({n, 1}); } long long mod_pow(long long x, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } long long mod_inv(long long x, long long mod) { return mod_pow(x, mod - 2, mod); } class Combination { public: vector<long long> fact; vector<long long> inv; long long mod; long long mod_inv(long long x) { long long n = mod - 2LL; long long res = 1LL; while (n > 0) { if (n & 1) res = res * x % mod; x = x * x % mod; n >>= 1; } return res; } long long nCr(long long n, long long r) { if (n < r) return 0; if (n < mod) return ((fact[n] * inv[r] % mod) * inv[n - r]) % mod; long long ret = 1; while (n || r) { long long _n = n % mod, _r = r % mod; n /= mod; r /= mod; (ret *= nCr(_n, _r)) %= mod; } return ret; } long long nPr(long long n, long long r) { return (fact[n] * inv[n - r]) % mod; } long long nHr(long long n, long long r) { return nCr(r + n - 1, r); } Combination(long long _n, long long _mod) { mod = _mod; long long n = min(_n + 1, mod); fact.resize(n); fact[0] = 1; for (long long(i) = (long long)(0); i < (long long)(n - 1); i++) { fact[i + 1] = (fact[i] * (i + 1LL)) % mod; } inv.resize(n); inv[n - 1] = mod_inv(fact[n - 1]); for (int i = n - 1; i > 0; i--) { inv[i - 1] = inv[i] * i % mod; } } }; long long popcount(long long x) { x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555); x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333); x = (x & 0x0F0F0F0F0F0F0F0F) + (x >> 4 & 0x0F0F0F0F0F0F0F0F); x = (x & 0x00FF00FF00FF00FF) + (x >> 8 & 0x00FF00FF00FF00FF); x = (x & 0x0000FFFF0000FFFF) + (x >> 16 & 0x0000FFFF0000FFFF); x = (x & 0x00000000FFFFFFFF) + (x >> 32 & 0x00000000FFFFFFFF); return x; } long long dp1[300][2]; long long dp2[300][300][300]; long long dp3[300][300]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n; cin >> n; string s; cin >> s; long long m = s.size(); for (long long(i) = (long long)(0); i < (long long)(m); i++) { string tmp = s.substr(0, i); string tmp0 = tmp + "("; string tmp1 = tmp + ")"; for (long long(j) = (long long)(0); j < (long long)(i + 2); j++) { if (s.substr(0, j) == tmp0.substr(i + 1 - j, j)) dp1[i][0] = j; if (s.substr(0, j) == tmp1.substr(i + 1 - j, j)) dp1[i][1] = j; } } dp2[0][0][0] = 1; for (long long(i) = (long long)(0); i < (long long)(2 * n); i++) { for (long long(j) = (long long)(0); j < (long long)(n + 1); j++) { for (long long(k) = (long long)(0); k < (long long)(m); k++) { for (long long(l) = (long long)(0); l < (long long)(2); l++) { if (l == 1 && j == 0) continue; long long next = dp1[k][l]; long long t = j + 1 - 2 * l; if (next == m) (dp3[i + 1][t] += dp2[i][j][k]) %= MOD; else (dp2[i + 1][t][next] += dp2[i][j][k]) %= MOD; } } if (j != 0) (dp3[i + 1][j - 1] += dp3[i][j]) %= MOD; (dp3[i + 1][j + 1] += dp3[i][j]) %= MOD; } } cout << dp3[2 * n][0] << "\n"; return 0; }
2,300
CPP
#include <bits/stdc++.h> using namespace std; int d[1234678], a[1234678], ans; int main() { int n, cnt = 0; char c; scanf("%d\n", &n); for (int i = 1; i <= n; i++) { scanf("%c", &c); d[i] = (c == 'R'); } for (int i = 1; i <= n; i++) scanf("%d", &a[i]); ans = 1047483647; for (int i = 2; i <= n; i++) if (d[i - 1] == 1 && d[i] == 0) ans = min(ans, (a[i] - a[i - 1]) / 2); if (ans == 1047483647) puts("-1"); else printf("%d\n", ans); }
1,000
CPP
n = int(input()) count = 0 while(n>0): if(n%2 == 1): count += 1 n = int(n/2) print(count)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; int m; vector<vector<int> > g; vector<int> a[2]; vector<int> b; long long INF = 1e9; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; cin >> s; a[0].resize(s.size() + 1); a[1].resize(s.size() + 1); b.resize(s.size() + 1); a[0][0] = 0; a[1][s.size()] = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '0') a[0][i + 1] = a[0][i] + 1; else a[0][i + 1] = a[0][i]; } for (int i = s.size() - 1; i > -1; i--) { if (s[i] == '1') a[1][i] = a[1][i + 1] + 1; else a[1][i] = a[1][i + 1]; } for (int i = 0; i < a[0].size(); i++) b[i] = a[0][i] + a[1][i]; vector<char> t; vector<int> bad; t.resize(s.size()); for (int i = 0; i < s.size(); i++) { t.push_back('#'); bad.push_back(0); } int pos = s.size(); int lpos = pos - 1; while (lpos > -1 && pos >= 1) { if (b[pos] > b[pos - 1]) { lpos = pos - 1; while (lpos >= 1 && b[pos] > b[lpos]) { bad[lpos - 1] = 1; lpos--; } if (lpos >= 0) pos = lpos; } else { pos = pos - 1; } } for (int i = 0; i < s.size(); i++) { if (bad[i] == 0) cout << '0'; else cout << s[i]; } }
2,100
CPP
k, n, w = map(int, input().split()) a = w * (w + 1) // 2 a = a * k a -= n if (a > 0): print(a) else: print(0)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000 + 5; int a[MAXN][MAXN]; int g1[MAXN][MAXN], g2[MAXN][MAXN], g3[MAXN][MAXN], g4[MAXN][MAXN]; int n, m; int max(int a, int b) { if (a > b) return a; return b; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) scanf("%d", &a[i][j]); memset(g1, 0, sizeof(g1)); g1[0][0] = a[0][0]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (i < n - 1) g1[i + 1][j] = max(g1[i + 1][j], g1[i][j] + a[i + 1][j]); if (j < m - 1) g1[i][j + 1] = max(g1[i][j + 1], g1[i][j] + a[i][j + 1]); } memset(g2, 0, sizeof(g2)); g2[0][m - 1] = a[0][m - 1]; for (int i = 0; i < n; i++) for (int j = m - 1; j >= 0; j--) { if (i < n - 1) g2[i + 1][j] = max(g2[i + 1][j], g2[i][j] + a[i + 1][j]); if (j > 0) g2[i][j - 1] = max(g2[i][j - 1], g2[i][j] + a[i][j - 1]); } memset(g3, 0, sizeof(g3)); g3[n - 1][0] = a[n - 1][0]; for (int i = n - 1; i >= 0; i--) for (int j = 0; j < m; j++) { if (i > 0) g3[i - 1][j] = max(g3[i - 1][j], g3[i][j] + a[i - 1][j]); if (j < m - 1) g3[i][j + 1] = max(g3[i][j + 1], g3[i][j] + a[i][j + 1]); } memset(g4, 0, sizeof(g4)); g4[n - 1][m - 1] = a[n - 1][m - 1]; for (int i = n - 1; i >= 0; i--) for (int j = m - 1; j >= 0; j--) { if (i > 0) g4[i - 1][j] = max(g4[i - 1][j], g4[i][j] + a[i - 1][j]); if (j > 0) g4[i][j - 1] = max(g4[i][j - 1], g4[i][j] + a[i][j - 1]); } int ans = 0; for (int i = 1; i < n - 1; i++) for (int j = 1; j < m - 1; j++) { ans = max(ans, g1[i - 1][j] + g2[i][j + 1] + g3[i][j - 1] + g4[i + 1][j]); ans = max(ans, g1[i][j - 1] + g2[i - 1][j] + g3[i + 1][j] + g4[i][j + 1]); } printf("%d\n", ans); }
1,600
CPP
import math z=list(map(int,input().split())) h=z[0] w=z[1] a=z[2] print(math.ceil(h/a) * math.ceil(w/a) )
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; void solve(int ncase) { int n; cin >> n; vector<double> p(n); for (int i = 0; i < n; i++) { cin >> p[i]; } sort(p.begin(), p.end(), greater<double>()); double S = 0, P = 1; for (int i = 0; i < n; i++) { if (p[i] == 1) { cout << 1 << endl; return; } if (1 - S < 0) break; S += p[i] / (1 - p[i]); P *= (1 - p[i]); } cout << fixed << setprecision(12) << P * S; } int main() { ios::sync_with_stdio(false); int T = 1; int ncase = 0; while (T--) { solve(++ncase); } }
1,800
CPP
t = int(input()) for i in range(t): n,m,k = input().split() n = int(n) m = int(m) k = int(k) h = list(map(int,input().split())) j = 0 a = 0 while(j<n-1): if(h[j]>=h[j+1]): m+=h[j] - h[j+1] + min(k,h[j+1]) j+=1 else: if(h[j+1]-h[j] <= k): m+=min(k,h[j+1])-h[j+1]+h[j] j+=1 else: if(h[j+1]-h[j]-k <=m): m-=h[j+1]-h[j]-k j+=1 else: a = 1 print("NO") break if(a == 0): print("YES")
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int d[100033]; int n, k; int c[100033]; vector<int> ve[100033], u, v; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) scanf("%d", d + i); int st = -1; bool ok = 1; memset(c, 0, sizeof c); for (int i = 1; i <= n; i++) { if (d[i] < 0 || d[i] > n - 1) { ok = 0; break; } c[d[i]]++; ve[d[i]].push_back(i); if (d[i] == 0) { if (st == -1) st = i; else ok = 0; } } if (c[0] != 1) ok = 0; if (c[1] > k) ok = 0; if (!ok) { cout << -1 << endl; return 0; } for (int i = 0; i < c[1]; i++) u.push_back(st), v.push_back(ve[1][i]); for (int i = 2; i < n; i++) { if ((long long)c[i] > (long long)c[i - 1] * (k - 1)) { ok = 0; break; } int cnt = c[i]; int x = 0; int j; for (j = 0; j < c[i - 1] && x < c[i]; j++) { for (int t = 0; x < c[i] && t < k - 1; t++, x++) u.push_back(ve[i - 1][j]), v.push_back(ve[i][x]); } if (j >= c[i - 1] && x < c[i]) { ok = 0; break; } } if (!ok) { cout << -1 << endl; return 0; } int m = u.size(); cout << m << endl; for (int i = 0; i < m; i++) printf("%d %d\n", u[i], v[i]); }
1,800
CPP
#include <bits/stdc++.h> using namespace std; long long modexp(long long n, long long m) { if (m == 0) return 1; else if (m == 1) return n; else { long long p = modexp(n, m / 2); if (m % 2 == 1) return (((p * p) % 1000000007) * n) % 1000000007; else return (p * p) % 1000000007; } } long long exp(long long n, long long m) { if (m == 0) return 1; if (m == 1) return n; long long p = exp(n, m / 2); if (m % 2 == 1) return p * p * n; else return p * p; } long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } long long inv(long long n) { return modexp(n, 1000000007 - 2); } long long lcm(long long a, long long b) { return a * b / gcd(a, b); } long long factorial(long long fact[], long long n) { fact[0] = 1; fact[1] = 1; long long prod = 1, i; for (i = 2; i < n + 1; i++) { prod = (prod * i) % 1000000007; fact[i] = prod; } return prod; } void update(vector<long long> &tree, long long l, long long r, long long tl, long long tr, long long index) { if (tl > r || tr < l) return; else if (tl >= l && tr <= r) tree[index]++; else { long long mid = (tl + tr) / 2; update(tree, l, r, tl, mid, 2 * index + 1); update(tree, l, r, mid + 1, tr, 2 * index + 2); } } long long get(vector<long long> &tree, long long l, long long r, long long tl, long long tr, long long index) { if (tl > r || tr < l) return 0; else if (tl >= l && tr <= r) return tree[index]; else { long long mid = (tl + tr) / 2; return tree[index] + get(tree, l, r, tl, mid, 2 * index + 1) + get(tree, l, r, mid + 1, tr, 2 * index + 2); } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t, n, m, q, i, j, k; cin >> n; string s; cin >> s; set<long long> oc[26]; for (i = 0; i < n; i++) oc[s[i] - 'a'].insert(i); reverse(s.begin(), s.end()); vector<long long> tree(5 * n, 0); long long ans = 0; for (i = 0; i < n; i++) { j = *oc[s[i] - 'a'].begin(); update(tree, 0, j - 1, 0, n - 1, 0); long long offset = get(tree, j, j, 0, n - 1, 0); j += offset; oc[s[i] - 'a'].erase(oc[s[i] - 'a'].begin()); if (i == j) continue; ans += j - i; } cout << ans << endl; return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; const double eps = 1e-7; const long long INFF = 1e18; const int INF = 1e9; const int mod = 1e9 + 7; const int MAX = 1e3 + 7; struct Point { long long x, y; Point(){}; Point(long long _x, long long _y) { x = _x, y = _y; } void read() { scanf("%lld %lld", &x, &y); } Point operator+(const Point& P2) { return Point(x + P2.x, y + P2.y); } Point operator-(const Point& P2) { return Point(x - P2.x, y - P2.y); } Point operator*(const long long& Len) { return Point(x * Len, y * Len); } long long operator*(const Point& P2) { return x * P2.x + y * P2.y; } long long operator^(const Point& P2) { return x * P2.y - y * P2.x; } long long dis() { return x * x + y * y; } const bool operator<(const Point& p2) { if (x != p2.x) return x < p2.x; return y < p2.y; } }; struct Line { Point s, e; Line(){}; Line(Point _s, Point _e) { s = _s, e = _e; } void read() { s.read(); e.read(); } }; int per[MAX]; Point pt[MAX]; Point pos[7]; int n, m, cur; bool cmp(const int& a, const int& b) { Point p1 = pt[a] - pos[cur], p2 = pt[b] - pos[cur]; long double v1 = atan2(p1.y, p1.x), v2 = atan2(p2.y, p2.x); if (fabs(v1 - v2) >= eps) return v1 < v2; long long val = p1 ^ p2; if (val == 0) return p1.dis() < p2.dis(); else return val > 0; } vector<int> con[7][MAX]; bitset<MAX> ok; int in[MAX], cntp; int main(void) { scanf("%d %d", &n, &m); for (int i = 0; i < (n); ++i) pos[i].read(); for (int i = (1); i < (m + 1); ++i) pt[i].read(); for (int j = (1); j < (m + 1); ++j) per[j] = j; for (int i = 0; i < n; ++i) { cur = i; sort(per + 1, per + m + 1, cmp); for (int j = (1); j < (m + 1); ++j) { int ll = j, rr = j; while (rr < m) { if ((pt[per[ll]] - pos[i]) ^ (pt[per[rr + 1]] - pos[i]) || (pt[per[ll]] - pos[i]) * (pt[per[rr + 1]] - pos[i]) <= 0) break; rr++; } for (int k = (ll); k < (rr + 1); ++k) { for (int kk = (ll); kk < (k); ++kk) { con[i][per[k]].push_back(per[kk]); if (((int)con[i][per[k]].size()) >= n) break; } } j = rr; } } for (int j = 0; j < (n); ++j) per[j] = j; do { for (int i = (1); i < (m + 1); ++i) if (!ok[i]) { cntp++; vector<int> stk; stk.reserve(16); stk.push_back(i); in[i] = cntp; for (int j = 0; j < (n); ++j) { if (((int)stk.size()) > n - j) break; int v = stk.back(); stk.pop_back(); for (int x : con[per[j]][v]) { if (in[x] == cntp) continue; in[x] = cntp; stk.push_back(x); } if (stk.empty()) { ok[i] = 1; break; } } } } while (next_permutation(per, per + n)); int ans = 0; for (int i = (1); i < (m + 1); ++i) if (ok[i]) ans++; printf("%d\n", ans); return 0; }
2,600
CPP
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } template <typename T> struct point { T x, y; point() {} point(T xx, T yy) : x(xx), y(yy) {} point(const point& B) : x(B.x), y(B.y) {} double radius() const { return sqrt(x * x + y * y); } double angle() const { return atan2(y, x); } double angle_degree() const { return atan2(y, x) * 180 / 3.14159265358979323846264338327950288419716939937510; } bool operator==(const point& B) const { return x == B.x && y == B.y; } bool operator!=(const point& B) const { return x != B.x || y != B.y; } bool operator<(const point& B) const { return x < B.x || (x == B.x && y < B.y); } bool operator>(const point& B) const { return x > B.x || (x == B.x && y > B.y); } bool operator<=(const point& B) const { return x < B.x || (x == B.x && y <= B.y); } bool operator>=(const point& B) const { return x > B.x || (x == B.x && y >= B.y); } point& operator=(const point& B) { x = B.x; y = B.y; return *this; } point operator+(const point& B) const { return point(x + B.x, y + B.y); } point operator-(const point& B) const { return point(x - B.x, y - B.y); } point& operator+=(const point& B) { x += B.x; y += B.y; return *this; } point& operator-=(const point& B) { x -= B.x; y -= B.y; return *this; } T operator*(const point& B) const { return x * B.x + y * B.y; } T operator^(const point& B) const { return x * B.y - y * B.x; } }; template <typename T> std::ostream& operator<<(std::ostream& os, const point<T>& p) { return os << "(" << p.x << ", " << p.y << ")" << std::flush; } template <typename T> double distance(const point<T>& A, const point<T>& B) { return sqrt((A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y)); } template <typename T> double distance(T x1, T y1, T x2, T y2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } template <typename T> struct hash<point<T> > { size_t operator()(const point<T>& p) const { return std::hash<T>{}((std::hash<T>{}(p.x) * 19921211) ^ (std::hash<T>()(p.y) * 2147483647)); } }; template <typename T> struct line_2p { point<T> pa, pb; line_2p(T x1, T y1, T x2, T y2) : pa(x1, y1), pb(x2, y2) {} T A() const { return pb.y - pa.y; } T B() const { return pa.x - pb.x; } T C() const { return pb.x * pa.y - pa.x * pb.y; } bool on_line(const point<T>& p) const { return A() * p.x + B() * p.y + C() == 0; } bool on_line_segment(const point<T>& p) const { if (!on_line(p)) return false; bool x_on = (pa.x <= p.x && p.x <= pb.x) || (pa.x >= p.x && p.x >= pb.x); bool y_on = (pa.y <= p.y && p.y <= pb.y) || (pa.y >= p.y && p.y >= pb.y); return x_on && y_on; } }; template <typename T> std::ostream& operator<<(std::ostream& os, const line_2p<T>& l) { return os << "line_2p{" << l.pa << ", " << l.pb << "}" << std::flush; } template <typename T> bool is_parallel(const line_2p<T>& l1, const line_2p<T>& l2) { return l1.A() * l2.B() - l1.B() * l2.A() == 0; } template <typename T> bool intersection(const line_2p<T>& l1, const line_2p<T>& l2, double& x, double& y) { if (is_parallel(l1, l2)) return false; if (l1.B() == 0) { x = double(-l1.C()) / l1.A(); y = double(-l2.C() - l2.A() * x) / l2.B(); return true; } if (l2.B() == 0) { x = double(-l2.C()) / l2.A(); y = double(-l1.C() - l1.A() * x) / l1.B(); return true; } x = double(l1.B() * l2.C() - l2.B() * l1.C()) / double(l1.A() * l2.B() - l2.A() * l1.B()); y = double(-x * l1.A() - l1.C()) / l1.B(); return true; } const int maxn = 1e3 + 10; int n; unordered_map<point<long long>, set<int> > dupcnt; vector<line_2p<long long> > lines; int ret; int count_points_online(int a, int b, int c, int d) { int dx = abs(a - c); int dy = abs(b - d); if (!dx) return dy + 1; if (!dy) return dx + 1; int g = gcd(dx, dy); return g + 1; } bool is_int(double d, double eps = 1e-9) { if (abs(static_cast<long long>(d) - d) <= eps) return true; return false; } int main() { scanf("%d\n", &n); for (int i = 0; i < (int)(n); ++i) { int a, b, c, d; scanf("%d %d %d %d\n", &a, &b, &c, &d); lines.push_back(line_2p<long long>(a, b, c, d)); int points = count_points_online(a, b, c, d); ret += points; ; } for (int i = 0; i < (int)(n); ++i) { line_2p<long long>& l1 = lines[i]; for (int j = i + 1; j < (int)(n); ++j) { line_2p<long long>& l2 = lines[j]; double x, y; bool has_intersection = intersection(l1, l2, x, y); bool is_x_int = is_int(x), is_y_int = is_int(y); if (!has_intersection || !is_x_int || !is_y_int) continue; point<long long> p(x, y); if (!l1.on_line_segment(p) || !l2.on_line_segment(p)) continue; dupcnt[p].insert(i); dupcnt[p].insert(j); } } for (auto& p : dupcnt) { ; ret -= p.second.size() - 1; } printf("%d\n", ret); return 0; }
2,400
CPP
#include <bits/stdc++.h> using namespace std; int i, j, m, xx, tst, n, ans[1050005], pick[22], inf; bool connected[22][22], visit[22]; char s[100005], t[100005]; bool dfs1(int u, int uu) { int i; bool flag = false; visit[u] = true; for (i = 0; i <= n - 1; i++) { if ((pick[i]) && (connected[u][i]) && (i != u)) { if (i == uu) { return true; } else { if (!visit[i]) { flag = dfs1(i, uu); if (flag) return true; } } } } return false; } bool in_cycle(int u) { memset(visit, false, sizeof(visit)); return dfs1(u, u); } void dfs2(int u) { int i; visit[u] = true; for (i = 0; i <= n - 1; i++) { if ((!visit[i]) && (pick[i]) && (connected[u][i] || connected[i][u])) { dfs2(i); } } } void solve(int u) { bool find_cycle = false; int i, j; for (i = 0; i <= n - 1; i++) { if ((pick[i]) && (in_cycle(i))) { find_cycle = true; if (ans[u - (1 << i)] == inf) { pick[i] = false; solve(u - (1 << i)); pick[i] = true; } if (ans[u - (1 << i)] + 2 < ans[u]) ans[u] = ans[u - (1 << i)] + 2; } } if (!find_cycle) { int sz = 0; int cpn = 0; for (i = 0; i <= n - 1; i++) { if (pick[i]) sz++; } ans[u] = sz; } } void outputgraph() { int i, j; for (i = 0; i <= n - 1; i++) { for (j = 0; j <= n - 1; j++) { printf("%d ", connected[i][j]); } printf("\n"); } } int main() { n = 20; inf = 1000000000; scanf("%d", &tst); while (tst--) { scanf("%d", &xx); scanf("%s", s); scanf("%s", t); memset(connected, false, sizeof(connected)); for (i = 0; i <= xx - 1; i++) { connected[s[i] - 'a'][t[i] - 'a'] = true; } for (i = 0; i < (1 << n); i++) { ans[i] = inf; } for (i = 0; i <= n - 1; i++) { pick[i] = true; } ans[0] = 0; solve((1 << n) - 1); int cpn = 0; memset(visit, false, sizeof(visit)); for (i = 0; i <= n - 1; i++) { if ((!visit[i]) && (pick[i])) { dfs2(i); cpn++; } } printf("%d\n", ans[(1 << n) - 1] - cpn); } return 0; }
1,700
CPP
s = input() t = input() ans=0 for i in range(len(t)): if s[ans] == t[i]: ans+=1 print(ans+1)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int mod = 1e9 + 7; const int N = 2e5 + 5; vector<pair<int, int>> v[N + 5]; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int tt = 1; while (tt--) { int n, k; cin >> n >> k; for (int i = 0, l, r; i < n; ++i) { cin >> l >> r; v[l].push_back(make_pair(r, i + 1)); } vector<int> ans; multiset<pair<int, int>> mst; for (int i = 1; i < N; ++i) { for (auto it : v[i]) { mst.insert(it); } while (mst.size() > k) { auto it = mst.end(); --it; ans.push_back(it->second); mst.erase(it); } while (mst.size() > 0 and mst.begin()->first == i) { mst.erase(mst.begin()); } } cout << ans.size() << '\n'; for (auto it : ans) cout << it << " "; } return 0; }
1,800
CPP
n,m=map(int,input().split()) gs=[list(map(int,input())) for _ in ' '*n] mx=[max(i) for i in zip(*gs)] print(sum(any(mx[i]==a[i] for i in range(m)) for a in gs))
900
PYTHON3
a = input() arr = list(map(int, input().split())) arr.sort() if abs(arr[0] - arr[-2]) < abs(arr[1] - arr[-1]): print(abs(arr[0] - arr[-2])) else: print(abs(arr[1] - arr[-1]))
900
PYTHON3
import os import sys from io import BytesIO, IOBase import math def main(): pass # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") # l = list(map(int, input().rstrip().split())) # n, m = map(int, input().rstrip().split()) l = list(map(int, input().split("+"))) l.sort() ans="" i=0 while i<len(l): ans+=str(l[i]) if i!=len(l)-1: ans+="+" i+=1 print(ans)
800
PYTHON3
#----------FASTIOSTART-----------# from __future__ import division, print_function import os import sys from io import BytesIO, IOBase if sys.version_info[0] < 3: from __builtin__ import xrange as range from future_builtins import ascii, filter, hex, map, oct, zip BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") def print(*args, **kwargs): sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") #----------FASTIOFINISH----------# import collections,string,bisect,re,random,queue,itertools,statistics,math from collections import * from bisect import * from string import * from itertools import * from statistics import * from math import * from re import * from queue import * #----------SASTA_STL-------------# # stack class Stack: def __init__(self):self.items = [] def push(self, item):self.items.append(item) def pop(self):return self.items.pop() def empty(self):return self.items == [] def size(self):return len(self.items) def at(self,idx): try:return self.items[idx] except:return -1 #priority_queue class priority_queue(object): def __init__(self):self.queue = [] def __str__(self):return ' '.join([str(i) for i in self.queue]) def empty(self):return len(self.queue) == 0 def push(self, data):self.queue.append(data) def pop(self): try: max = 0 for i in range(len(self.queue)): if self.queue[i] > self.queue[max]: max = i item = self.queue[max] del self.queue[max] return item except IndexError: print() exit() #----------SASTA_STL-------------# mod = int(1e9+7) imax = float("inf") imin = float("-inf") true = True false= False N = int(1e5) none = None inp = lambda : input() I = lambda : int(inp()) M = lambda : map(int,inp().split()) MS = lambda : map(str,inp().split()) S = lambda : list(MS()) L = lambda : list(M()) def IO(): try: sys.stdin = open('input.txt', 'r'); sys.stdout = open('uttar.txt', 'w') except: pass IO() class helper: def first(self,arr): return arr[0] def second(self,arr): return arr[1] def is_sorted(self,arr): return arr==sorted(arr) #----------TOTKA---------# def kabraji_ka_totka(): # totka hai dosto ! n=I() arr=L() temp=0 for i in arr: temp|=i; print(temp^i) #----------TOTKA----------# if __name__ == '__main__': case=1; case=I() for i in range(case): kabraji_ka_totka()
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; int findNum(int n, int m) { int rsl; if (n % 2 == 0) rsl = n / 2; else rsl = (n / 2) + 1; while (1) { if (rsl % m == 0) { return rsl; break; } else rsl++; if (rsl > n) { rsl = -1; break; } } } int main() { int m, n, result = 0; cin >> n >> m; if (n == 0 || n < m) result = -1; else { result = findNum(n, m); } cout << result << endl; return 0; }
1,000
CPP
t = int(input()) for i in range(t): n = int(input()) s = list(input()) if n < 11: print('NO') elif n == 11: if s[0] == '8': print('YES') else: print('NO') else: if '8' in s: k = s.index('8') l = len(s) - k if l >= 11: print('YES') else: print('NO') else: print('NO')
800
PYTHON3
import re s = input().strip() tags = re.findall('</?\w>', s) depth = 0 for tag in tags: if '/' in tag: depth -= 1 print('%s%s' % (depth * ' ', tag)) if '/' not in tag: depth += 1 # Made By Mostafa_Khaled
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; vector<int> num(2 * n); int numo = 0, nume = 0; vector<int> od, ev; for (int i = 0; i < 2 * n; i++) { cin >> num[i]; if (num[i] % 2) { numo += 1; od.push_back(i + 1); } else { nume += 1; ev.push_back(i + 1); } } if (numo % 2) { numo--; if (nume > 0) { nume--; } else numo--; } else { if (numo >= 2) numo -= 2; else nume -= 2; } for (int i = 0; i < numo; i += 2) { cout << od[i] << " " << od[i + 1] << endl; } for (int i = 0; i < nume; i += 2) { cout << ev[i] << " " << ev[i + 1] << endl; } } return 0; }
1,100
CPP
a,b=input().split() z=int(b[::-1]) x=int(a) print(z+x)
1,200
PYTHON3
def gennadyPartnerTask(ontable,cards): for c,s in cards: if ontable[0]==c or ontable[1]==s: return "YES" return "NO" if __name__=='__main__': ontable=input() cards=[i for i in input().split()] op=gennadyPartnerTask(ontable,cards) print(op)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m; int g[1010], b[1010]; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", &g[i]); for (int i = 0; i < m; i++) scanf("%d", &b[i]); int bcnt = 0; for (int i = 0; i < n; i++) { if (bcnt == m) continue; if (b[bcnt] >= g[i]) { bcnt++; } } printf("%d\n", bcnt); }
800
CPP
n = int(input()) e = n // 2 if n % 2 == 0: o = e else: o = e + 1 print(-(o**2) + e * (e + 1))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; double max(double a, double b) { if (a > b) return a; return b; } double min(double a, double b) { if (a > b) return b; return a; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); string s; int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int cnt = 0, i = 0, j = 0; sort(a, a + n); while (i < n && j < n) { if (a[i] == a[j]) { j++; } else { i++; j++; cnt++; } } cout << cnt; return 0; }
1,300
CPP
n,m,a,b = list(map(int,input().split())) if b/m >= a : print(n*a) else : k = n//m print(min(k*b + (n-k*m)*a,(k+1)*b))
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int place[220000], a[2100000]; int n; int main() { int temp; cin >> n; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); place[a[i]] = i; } int ans = 0, p = 0; for (int i = 0; i < n; i++) { scanf("%d", &temp); if (temp == a[p]) p++; else ans++; } cout << ans; return 0; }
1,500
CPP
t = int(input()) while(t): li = [int(i) for i in input().split()] a = li[0] b = li[1] k = li[2] if(k % 2 == 0): print((a-b)*(k//2)) else: print(((a-b)*(k//2))+a) t -= 1
800
PYTHON3
def solve(): t = input() s = set(t) if len(s) == 1: print(t) else: print("10" * len(t)) for i in range(int(input())): solve()
1,100
PYTHON3
#include <bits/stdc++.h> const int INF = 2147483647; const int INF2 = 0x3f3f3f3f; const long long INF64 = (long long)1e18; const double INFD = 1e30; const double EPS = 1e-9; const double PI = std::acos(-1); const int MOD = 998244353; template <typename T> inline T read() { T X = 0, w = 0; char ch = 0; while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); return w ? -X : X; } const int MAXN = 1000005; const int MAXV = 1000000; int dr[4] = {0, 1, 0, -1}; int dc[4] = {-1, 0, 1, 0}; int dr2[8] = {1, 1, 1, -1, -1, -1, 0, 0}; int dc2[8] = {1, 0, -1, 1, 0, -1, 1, -1}; int CASE = 1; int n, m, k; struct Inv { int l, r, w; bool operator<(const Inv& inv) const { return w < inv.w; } }; Inv invs[MAXN]; std::vector<int> id[MAXN]; struct Segtree { struct Node { long long minn, tag; }; Node nodes[MAXN << 2]; void push_up(int p) { nodes[p].minn = std::min(nodes[p << 1].minn, nodes[p << 1 | 1].minn); } void propagate(int p, int l, int r) { int fa = p >> 1; nodes[p].tag += nodes[fa].tag; nodes[p].minn += nodes[fa].tag; } void push_down(int p, int l, int r) { if (!nodes[p].tag) return; int mid = (l + r) / 2; propagate(p << 1, l, mid); propagate(p << 1 | 1, mid + 1, r); nodes[p].tag = 0; } void update(int p, int l, int r, int x, int y, long long v) { if (x <= l && y >= r) { nodes[p].minn += v; nodes[p].tag += v; return; } push_down(p, l, r); int mid = (l + r) / 2; if (x <= mid) update(p << 1, l, mid, x, y, v); if (y > mid) update(p << 1 | 1, mid + 1, r, x, y, v); push_up(p); } long long query(int p, int l, int r, int x, int y) { if (x > y) return 0; if (x <= l && y >= r) { return nodes[p].minn; } push_down(p, l, r); int mid = (l + r) / 2; long long ans = INF64; if (x <= mid) ans = std::min(ans, query(p << 1, l, mid, x, y)); if (y > mid) ans = std::min(ans, query(p << 1 | 1, mid + 1, r, x, y)); return ans; } }; Segtree tree; void solve() { scanf("%d%d", &n, &m); std::vector<Inv> vs; for (int i = 0; i < n; i++) { int l, r, w; scanf("%d%d%d", &l, &r, &w); vs.push_back({l, r, w}); } std::sort(vs.begin(), vs.end()); int ans = vs[n - 1].w - vs[0].w; int l = 0, r = -1; tree.update(1, 1, MAXV, 1, 1, 1); while (r < n) { if (tree.query(1, 1, MAXV, 1, m) > 0) { ans = std::min(ans, vs[r].w - vs[l].w); auto& inv = vs[l++]; tree.update(1, 1, MAXV, inv.l + 1, inv.r, -1); } else { if (r == n - 1) break; r++; auto& inv = vs[r]; tree.update(1, 1, MAXV, inv.l + 1, inv.r, 1); } } printf("%d\n", ans); } int main() { solve(); return 0; }
2,100
CPP
n = int(input()) x = list(map(int, input().split())) y = x.copy() x.sort() cur_min, cur_max = n + 1, 1 for i in range(1, n): if x[i] == x[i - 1]: cur_max += 1 else: if cur_max < cur_min: cur_min = cur_max cur_max = 1 if i == n - 1: cur_min = min(cur_max, cur_min) if len(set(x)) == 3: print(cur_min) for j in range(cur_min): print(y.index(1) + 1, y.index(2) + 1, y.index(3) + 1) y[y.index(1)] = 0 y[y.index(2)] = 0 y[y.index(3)] = 0 else: print(0)
800
PYTHON3
# import sys # sys.stdin = open("test.in","r") # sys.stdout = open("test.out","w") a,b,c=int(input()),int(input()),int(input()) print(7*min(a,b//2,c//4))
800
PYTHON3
num = int(input()) div, mod = divmod(num, 2) if mod == 0: print(div) print(' '.join(['2']*div)) else: print(div) print(' '.join(['2']*(div-1)) + ' 3')
800
PYTHON3
#include <bits/stdc++.h> using namespace std; unsigned long long aa = 1, n, m[20], r[20], k = 0; int main() { cin >> n; for (int i = 0; i < n; ++i) cin >> m[i]; for (int i = 0; i < n; ++i) cin >> r[i]; for (int i = 0; i < 1000001; ++i) { for (int j = 0; j < n; ++j) if (i % m[j] == r[j]) { ++k; break; } } cout << (double)k / 1000000; return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; int main() { int y, w; cin >> y >> w; int d = max(y, w); if (d == 1) { cout << "1/1"; } else if (d == 2) { cout << "5/6"; } else if (d == 3) { cout << "2/3"; } else if (d == 4) { cout << "1/2"; } else if (d == 5) { cout << "1/3"; } else if (d == 6) { cout << "1/6"; } }
800
CPP
s_num1 = input() # Input wrong alphabet (encoded) s_num2 = input() # Input true alphabet s = input() # Input your text, needs to be decoded l_num1 = list(s_num1) l_num2 = list(s_num2) l = list(s) s_swap1 = s_num1.swapcase() s_swap2 = s_num2.swapcase() l_high1 = list(s_swap1) l_high2 = list(s_swap2) for i in range(len(l)): for j in range(len(l_num1)): if l[i] == l_num1[j]: l[i] = l_num2[j] break # If you don't wanna to save the same time, you may delete "break" here... elif l[i] == l_high1[j]: l[i] = l_high2[j] break # ...and here. s_final = "" for i in l: s_final += i print(s_final)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int M = 5101; const int N = 330000; const long long oo = 4111111111L; int n, k; long long a[N]; long long f[M][M]; int main() { while (~scanf("%d%d", &n, &k)) { for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]); sort(a + 1, a + n + 1); int p = n / k, q = n % k; if (n <= k) { printf("0\n"); continue; } { for (int i = 0; i <= k - q; i++) { for (int j = 0; j <= q; j++) { f[i][j] = oo; } } f[0][0] = 0; for (int i = 0; i <= k - q; i++) { for (int j = 0; j <= q; j++) { if (i) f[i][j] = min(f[i][j], f[i - 1][j] + a[i * p + j * (p + 1)] - a[(i - 1) * p + j * (p + 1) + 1]); if (j) f[i][j] = min(f[i][j], f[i][j - 1] + a[i * p + j * (p + 1)] - a[(i)*p + (j - 1) * (p + 1) + 1]); } } } printf("%I64d\n", f[k - q][q]); } return 0; }
2,000
CPP
def solve(n, d): d = [int(x) for x in d] brim_even = list() brim_odd = list() raze_even = list() raze_odd = list() for i in range(1, len(d) + 1): if i % 2 == 0: if d[i - 1] % 2 == 0: brim_even.append(d[i-1]) else: brim_odd.append(d[i - 1]) else: if d[i - 1] % 2 == 0: raze_even.append(d[i - 1]) else: raze_odd.append(d[i - 1]) raze_even.extend(raze_odd) brim_odd.extend(brim_even) for i in range(0, len(d) - 1): if i % 2 == 0: if len(raze_even) != 0: raze_even.pop(0) else: if len(brim_odd) != 0: brim_odd.pop(0) if len(raze_even) == 1: if raze_even[-1] % 2 == 0: print("2") else: print("1") elif len(brim_odd) == 1: if brim_odd[-1] % 2 == 0: print("2") else: print("1") def main(): for i in range(int(input())): n = input() solve(n, input()) if __name__ == "__main__": main()
900
PYTHON3
#include <bits/stdc++.h> using namespace std; struct Point { long long x, y; int id; Point() {} Point(long long a, long long b, int c) : x(a), y(b), id(c) {} Point operator-(Point b) { return Point(x - b.x, y - b.y, 0); } bool operator<(const Point &b) const { return (x != b.x) ? x < b.x : y < b.y; } }; long long cross(Point x, Point y) { return x.x * y.y - x.y * y.x; } bool in[2005]; int gethull(Point *p, int n, int s, bool v) { static Point st[2005], hull[2005]; int top = 0, cnt = 0; for (int i = 1; i <= n; i++) { while (top > 1 && cross(p[i] - st[top], st[top] - st[top - 1]) >= 0) top--; st[++top] = p[i]; } for (int i = 1; i < top; i++) hull[++cnt] = st[i]; top = 0; for (int i = 1; i <= n; i++) { while (top > 1 && cross(p[i] - st[top], st[top] - st[top - 1]) <= 0) top--; st[++top] = p[i]; } for (int i = top; i > 1; i--) hull[++cnt] = st[i]; for (int i = 1; i <= cnt; i++) if (hull[i].id == s) { in[hull[i].id] = 1; return (v) ? hull[(i - 1 + cnt - 1) % cnt + 1].id : hull[i % cnt + 1].id; } } Point p[2005], q[2005]; char str[2005]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { int x, y; scanf("%d%d", &x, &y); p[i] = Point(x, y, i); } scanf("%s", str + 1); str[n - 1] = 'L'; sort(p + 1, p + n + 1); int cur = p[1].id; for (int i = 1; i < n; i++) { int cnt = 0; for (int j = 1; j <= n; j++) if (!in[p[j].id]) q[++cnt] = p[j]; printf("%d ", cur); cur = gethull(q, cnt, cur, (str[i] == 'R')); } printf("%d\n", cur); return 0; }
2,600
CPP
#include <bits/stdc++.h> using namespace std; int main() { int i, j, k, n; cin >> n; string a, b; vector<pair<string, string> > name; vector<int> pos; pos.resize(n); for (i = 0; i < n; i++) { cin >> a >> b; name.push_back(make_pair(a, b)); } for (i = 0; i < n; i++) cin >> pos[i]; bool dp[100005][2]; dp[0][0] = true; dp[0][1] = true; for (i = 1; i < n; i++) { j = pos[i] - 1; k = pos[i - 1] - 1; dp[i][0] = false; dp[i][1] = false; if (dp[i - 1][0] == true && name[j].first > name[k].first) { dp[i][0] = true; } if (dp[i - 1][1] == true && name[j].first > name[k].second) { dp[i][0] = true; } if (dp[i - 1][0] == true && name[j].second > name[k].first) { dp[i][1] = true; } if (dp[i - 1][1] == true && name[j].second > name[k].second) { dp[i][1] = true; } } if (dp[n - 1][0] || dp[n - 1][1]) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
1,400
CPP
n, k = [int(s) for s in input().split()] a = [int(s) for s in input().split()] max = -10**10 for b in range(n): e = 0 s = 0 closed = set() for i in range(b, len(a), k): closed.add(i) for i in range(b, -1, -k): closed.add(i) for i in range(len(a)): if i not in closed: if a[i] == -1: e += 1 else: s += 1 if abs(e-s) > max: max = abs(e-s) print(max)
1,000
PYTHON3
import sys from functools import reduce import operator def input(): return sys.stdin.readline().strip() def getibit(x,i): x = x//(1<<(i)) return x%2 for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) xorres = reduce(operator.xor, arr) addres = reduce(operator.add, arr) xorres*=2 i = 0 ans = 0 while (1<<i)<=addres or (1<<i)<=xorres: xor = getibit(xorres, i) add = getibit(addres, i) if xor != add: xorres = xorres ^ (1<<(i+1)) addres = addres+(1<<i) ans+=(1<<i) i+=1 print(1) print(ans)
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class T1> void deb(T1 e1) { cout << e1 << endl; } template <class T1, class T2> void deb(T1 e1, T2 e2) { cout << e1 << " " << e2 << endl; } template <class T1, class T2, class T3> void deb(T1 e1, T2 e2, T3 e3) { cout << e1 << " " << e2 << " " << e3 << endl; } template <class T1, class T2, class T3, class T4> void deb(T1 e1, T2 e2, T3 e3, T4 e4) { cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl; } template <class T1, class T2, class T3, class T4, class T5> void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) { cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl; } template <class T1, class T2, class T3, class T4, class T5, class T6> void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) { cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6 << endl; } int ara[5003], dp[5003], en[5003]; int main() { int i, j, k, l, m, n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &ara[i]); for (int i = 1; i <= n; i++) { int sum = 0; for (int j = i; j >= 1; j--) { sum += ara[j]; if (sum >= dp[j - 1]) { dp[i] = sum; en[i] = j; break; } } } int st = n, ans = 0; while (st > 0) { l = en[st]; ans += (st - l); st = l - 1; } cout << ans << endl; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; template <class A> void pr(A a) { cout << a; cout << '\n'; } template <class A, class B> void pr(A a, B b) { cout << a << ' '; pr(b); } template <class A, class B, class C> void pr(A a, B b, C c) { cout << a << ' '; pr(b, c); } template <class A, class B, class C, class D> void pr(A a, B b, C c, D d) { cout << a << ' '; pr(b, c, d); } template <class A> void PR(A a, long long n) { for (long long i = (long long)(0); i < (long long)(n); i++) { if (i) cout << ' '; cout << a[i]; } cout << '\n'; } long long check(long long n, long long m, long long x, long long y) { return x >= 0 && x < n && y >= 0 && y < m; } const long long MAX = 1e9 + 7, MAXL = 1LL << 61, dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; class RMQ { public: int n, dat[555555]; void init(int _n) { n = 1; while (n < _n) n *= 2; fill(dat, dat + 2 * n - 1, MAX); } void update(int k, int a) { k += n - 1; dat[k] = a; while (k > 0) { k = (k - 1) / 2; dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]); } } int query(int a, int b) { return query(a, b, 0, 0, n); } int query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return MAX; if (a <= l && r <= b) return dat[k]; int vl = query(a, b, k * 2 + 1, l, (l + r) / 2); int vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return min(vl, vr); } }; class RMQ2 { public: int n; pair<int, int> dat[555555]; void init(int _n) { n = 1; while (n < _n) n *= 2; for (long long i = (long long)(0); i < (long long)(2 * n); i++) dat[i] = pair<int, int>(-MAX, -1); } void update(int k, pair<int, int> a) { k += n - 1; dat[k] = a; while (k > 0) { k = (k - 1) / 2; dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]); } } pair<int, int> query(int a, int b) { return query(a, b, 0, 0, n); } pair<int, int> query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return pair<int, int>(-MAX, -1); if (a <= l && r <= b) return dat[k]; pair<int, int> vl = query(a, b, k * 2 + 1, l, (l + r) / 2); pair<int, int> vr = query(a, b, k * 2 + 2, (l + r) / 2, r); return max(vl, vr); } }; RMQ t; RMQ2 r; vector<int> v[222222]; void Main() { long long n, m; cin >> n >> m; long long a[n]; for (long long i = (long long)(0); i < (long long)(n); i++) { cin >> a[i]; if (!a[i]) a[i] = m + 1; v[a[i]].push_back(i); } t.init(n); r.init(n); for (long long i = (long long)(0); i < (long long)(n); i++) { t.update(i, a[i]); r.update(i, pair<int, int>(a[i], i)); } for (long long i = (long long)(m + 1) - 1; i >= 0; i--) { if (!v[i].size()) continue; if (t.query(v[i][0], v[i].back() + 1) < i) { pr("NO"); return; } while (1) { pair<int, int> p = r.query(v[i][0], v[i].back() + 1); if (p.first <= m) break; r.update(p.second, pair<int, int>(i, p.second)); } } bool f = 0; for (long long i = (long long)(0); i < (long long)(n); i++) { if (r.query(i, i + 1).first == m) f = 1; } if (!f) { for (long long i = (long long)(0); i < (long long)(n); i++) { if (t.query(i, i + 1) == m + 1) { r.update(i, pair<int, int>(m, i)); break; } } } for (long long i = (long long)(1); i < (long long)(n); i++) { pair<int, int> p = r.query(i, i + 1); if (p.first == m + 1) { pair<int, int> q = r.query(i - 1, i); if (q.first != m + 1) r.update(p.second, pair<int, int>(q.first, p.second)); } } for (long long i = (long long)(n - 1) - 1; i >= 0; i--) { pair<int, int> p = r.query(i, i + 1); if (p.first == m + 1) { pair<int, int> q = r.query(i + 1, i + 2); if (q.first != m + 1) r.update(p.second, pair<int, int>(q.first, p.second)); } } for (long long i = (long long)(0); i < (long long)(n); i++) { pair<int, int> p = r.query(i, i + 1); if (p.first == m + 1) r.update(i, pair<int, int>(m, i)); } f = 0; for (long long i = (long long)(0); i < (long long)(n); i++) { if (r.query(i, i + 1).first == m) f = 1; } if (!f) { pr("NO"); return; } pr("YES"); for (long long i = (long long)(0); i < (long long)(n); i++) { if (i) cout << " "; cout << r.query(i, i + 1).first; } cout << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); Main(); return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 205; int q, n, a[maxn], cnt[maxn]; int main() { scanf("%d", &q); while (q--) { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) { int tmp = a[i], cn = 1; while (i != tmp) { tmp = a[tmp]; cn++; } cnt[i] = cn; } for (int i = 1; i <= n; i++) printf("%d ", cnt[i]); puts(""); } return 0; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; const long long inf = 1e18 + 7; const long long N = 1e5 + 7; const long long md = 1e9 + 7; vector<pair<long long, long long>> g[N]; vector<vector<long long>> up; long long L, hu[N]; long long in_time[N]; long long out_time[N]; long long timer; long long e[N]; void subTreeSum(long long from, long long p) { for (auto &to : g[from]) { if (to.first != p) { subTreeSum(to.first, from); e[to.second] += hu[to.first]; hu[from] += hu[to.first]; } } } void computeAncestors(long long from, long long par) { in_time[from] = ++timer; up[from][0] = par; for (long long i = 1; i <= L; i++) { up[from][i] = up[up[from][i - 1]][i - 1]; } for (auto &to : g[from]) { if (to.first != par) computeAncestors(to.first, from); } out_time[from] = ++timer; } long long isAncestor(long long u, long long v) { return in_time[u] <= in_time[v] && out_time[u] >= out_time[v]; } long long LCA(long long u, long long v) { if (isAncestor(u, v)) return u; if (isAncestor(v, u)) return v; for (long long i = L; i >= 0; i--) { if (!isAncestor(up[u][i], v)) { u = up[u][i]; } } return up[u][0]; } void initialize(long long n) { L = (long long)ceil(log2(n)); up = vector<vector<long long>>(n + 1, vector<long long>(L + 1)); } void sol() { long long n, q, u, v; cin >> n; for (long long i = 1; i < n; i++) { cin >> u >> v; g[u].push_back({v, i}); g[v].push_back({u, i}); } initialize(n); computeAncestors(1, 1); cin >> q; for (long long i = 0; i < q; i++) { cin >> u >> v; hu[u]++; hu[v]++; hu[LCA(u, v)] -= 2; } subTreeSum(1, 1); for (long long i = 1; i < n; i++) { cout << e[i] << " "; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long T = 1; while (T--) sol(); return 0; }
1,900
CPP
import sys input = sys.stdin.readline t=int(input()) for tests in range(t): S=input().strip() ANSP=[] #ANSS=[] f=0 l=len(S)-1 while f<l and S[f]==S[l]: ANSP.append(S[f]) #ANSS.append(S[l]) f+=1 l-=1 if f==l or f==l+1: print(S) continue #print(ANSP) S2=S[f:l+1] #print(S2) SS2=[] for s in S2: SS2.append(s) SS2.append("$") SS2.pop() #print(SS2) LEN=len(SS2) i=0 j=0 R=[0]*LEN # 文字 i を中心とする最長の回文の半径 while i<LEN: while i-j>=0 and i+j<LEN and SS2[i-j]==SS2[i+j]: j+=1 R[i]=j k=1 while i-k>=0 and i+k<LEN and k+R[i-k]<j: R[i+k]=R[i-k] k+=1 i+=k j-=k #print(R) MAX=0 for i in range(LEN): if i-R[i]+1==0 or i+R[i]==LEN: if MAX<R[i]: MAX=R[i] MAXind=i S3=SS2[MAXind-MAX+1:MAXind+MAX] #print(S3) SS3=[] for s in S3: if s!="$": SS3.append(s) sys.stdout.write("".join(ANSP)+"".join(SS3)+"".join(ANSP[::-1])+"\n")
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; long long int n, l, timer = 0; vector<long long int> adj[100005]; vector<vector<long long int> > up(100005, vector<long long int>(20)); vector<long long int> tin(100005), tout(100005), dist(100005, 0); void dfs(long long int v, long long int p) { tin[v] = ++timer; up[v][0] = p; for (long long int i = 1; i <= l; ++i) up[v][i] = up[up[v][i - 1]][i - 1]; for (long long int u : adj[v]) { if (u != p) { dist[u] = dist[v] + 1LL; dfs(u, v); } } tout[v] = ++timer; } bool is_ancestor(long long int u, long long int v) { return ((tin[u] <= tin[v]) && (tout[u] >= tout[v])); } long long int lca(long long int u, long long int v) { if (is_ancestor(u, v)) return u; if (is_ancestor(v, u)) return v; for (long long int i = l; i >= 0; i--) { if (!is_ancestor(up[u][i], v)) u = up[u][i]; } return up[u][0]; } long long int length(long long int u, long long int v) { long long int req = lca(u, v); return (dist[u] + dist[v] - 2 * dist[req]); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; cin >> n; for (long long int i = 0; i <= n - 2; i++) { long long int x, y; cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } timer = 0; l = ceil(log2(n)); long long int root = 1; dfs(root, root); long long int w; cin >> w; while (w--) { long long int x, y, a, b, k; cin >> a >> b >> x >> y >> k; long long int dis = length(x, y); if (dis <= k && ((dis % 2) == (k % 2))) { cout << "YES" << '\n'; continue; } dis = length(x, a) + length(y, b) + 1LL; if (dis <= k && ((dis % 2) == (k % 2))) { cout << "YES" << '\n'; continue; } dis = length(x, b) + length(y, a) + 1LL; if (dis <= k && ((dis % 2) == (k % 2))) { cout << "YES" << '\n'; continue; } cout << "NO" << '\n'; } return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; int a[100001]; int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; } if (n == 1) { if (a[0] == 1) puts("NO"); else { puts("YES"); cout << a[0] << "\n"; } } else if (n == 2) { if (a[0] == 1 && a[1] == 0) { puts("YES"); cout << a[0] << "->" << a[1] << "\n"; } else puts("NO"); } else { if (a[n - 1] == 1) { puts("NO"); } else { int lastOne = -1; for (int i = 0; i < n; ++i) { if (a[i] == 1) lastOne = i; } if (lastOne == -1) { puts("YES"); for (int i = 0; i < n - 3; ++i) { cout << a[i] << "->"; } cout << "(" << a[n - 3] << "->" << a[n - 2] << ")->" << a[n - 1] << "\n"; } else if (lastOne == n - 2) { puts("YES"); for (int i = 0; i < n - 1; ++i) { cout << a[i]; if (i != n - 2) cout << "->"; } cout << "->" << a[n - 1] << "\n"; } else if (lastOne == n - 3) { int firstZero = -1; for (int i = n - 4; i >= 0; --i) { if (a[i] == 0) { firstZero = i; break; } } if (firstZero == -1) { puts("NO"); } else { puts("YES"); for (int i = 0; i < firstZero; ++i) { cout << a[i] << "->"; } for (int i = firstZero; i < n - 2; ++i) { cout << "(" << a[i] << "->"; } cout << a[n - 2]; for (int i = firstZero; i < n - 2; ++i) { cout << ")"; } cout << "->" << a[n - 1] << "\n"; } } else { puts("YES"); for (int i = 0; i < lastOne; ++i) { cout << a[i] << "->"; } for (int i = lastOne; i < n - 1; ++i) { if (i != n - 2) cout << "("; cout << a[i]; if (i != n - 2) cout << "->"; } for (int i = lastOne; i < n - 2; ++i) { cout << ")"; } cout << "->" << a[n - 1] << "\n"; } } } return 0; }
2,200
CPP
#include <bits/stdc++.h> #pragma optimization_level 3 #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx") using namespace std; using ll = long long; using ld = long double; const ll MOD = 1e+9 + 7; const ll INF = LLONG_MAX; const int N = (int)2e+5 + 8; string ter(int n) { string second = ""; while (n) { second = to_string(n % 3) + second; n /= 3; } return second; } int dec(string second) { int ans = 0; for (int i = 0; i < second.size(); i++) ans += (second[i] - '0') * pow(3, second.size() - i - 1); return ans; } void MAIN(ll tc) { int a, c; cin >> a >> c; string p = ter(a), q = ter(c), ans = ""; while (p.size() < q.size()) p = '0' + p; while (q.size() < p.size()) q = '0' + q; for (int i = 0; i < p.size(); i++) if (q[i] >= p[i]) ans += to_string(q[i] - p[i]); else ans += to_string(q[i] - p[i] + 3); cout << dec(ans); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout << fixed; cout << setprecision(10); int test_cases = 1; for (int i = 1; i <= test_cases; i++) { MAIN(i); } }
1,100
CPP
from sys import stdin, stdout T = int(stdin.readline()) for _ in range(T): [a, b, q] = [int(x) for x in stdin.readline().split()] maxAB = max(a, b) aGCD = a bGCD = b while aGCD != 0 and bGCD != 0: if aGCD > bGCD: aGCD %= bGCD else: bGCD %= aGCD gcd = aGCD + bGCD nok = a * b // gcd batch = nok - maxAB resArr = [] for __ in range(q): [l, r] = [int(x) for x in stdin.readline().split()] res = 0 if a == 1 and b == 1: res = 0 else: res += ((r - l) // nok) * batch if r % nok >= l % nok: if r % nok >= maxAB: if l % nok >= maxAB: res += r % nok - l % nok + 1 else: res += r % nok - maxAB + 1 else: if r % nok >= maxAB: res += r % nok - maxAB + 1 res += min(batch, nok - l % nok) resArr.append(str(res)) stdout.write(' '.join(resArr) + "\n")
1,600
PYTHON3
n = int(input()) ans = 0 for i in range(1, n+1): ans += min(n - i + 1, i) print(ans)
1,300
PYTHON3
import re s = input() if '@' not in s: print('NO') exit() u, h = s.split('@', 1) r = None if '/' in h: if '/' in h[h.index('/') + 1: ]: print('NO') exit() h, r = h.split('/') if not re.match('^\w{1,16}$', u): print('NO') elif r is not None and not re.match('^\w{1,16}$', r): print('NO') elif 1 > len(h) or len(h) > 32: print('NO') else: h = h.split('.') if any(not re.match('^\w{1,16}$', x) for x in h): print('NO') else: print('YES')
1,900
PYTHON3
x = int(input()[::-1]) print("YES" if str(x) == str(x)[::-1] else "NO")
900
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long maxn = 2e6; const long long mod = 1e9 + 7; const long double PI = 4 * atan((long double)1); long long pw(long long a, long long b) { return (!b ? 1 : (b & 1 ? a * pw(a * a % mod, b / 2) % mod : pw(a * a % mod, b / 2) % mod)); } int n, m; int a[maxn]; int dp[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i]; long double inp; cin >> inp; } dp[1] = 1; for (int i = 2; i <= n; i++) { dp[i] = 1; for (int j = 1; j < i; j++) { if (a[i] >= a[j]) { dp[i] = max(dp[i], dp[j] + 1); } } } cout << n - *max_element(dp + 1, dp + n + 1); return (0); }
1,700
CPP
import sys n=int(input()) a=[int(i) for i in input().split(" ")] b=[] c=[] summa=0 for i in a: if int(i)==int(i/2)*2: b.append(i) else: c.append(i) for i in b: summa+=i c=sorted(c) for i in range(int(c.__len__()/2)*2): summa+=c[c.__len__()-i-1] print(summa)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { cin.sync_with_stdio(0); cout.setf(ios::floatfield, ios::fixed); cout.precision(8); int res = -1; int n, s; cin >> n >> s; for (int i = 0; i < n; ++i) { int x, y; cin >> x >> y; if (s * 100 >= x * 100 + y) { int cand = 0; if (y) cand = 100 - y; if (res == -1 || res < cand) res = cand; } } cout << res << endl; return 0; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; int ar[1005][1005]; int main() { int n, a, b, i, j, rem, c; scanf("%d %d %d", &n, &a, &b); if (min(a, b) != 1) { printf("NO\n"); return 0; } c = max(a, b); if (c == 1) { if (n < 4 && n > 1) { printf("NO\n"); return 0; } printf("YES\n"); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { if (i + 1 == j || j + 1 == i) printf("1"); else printf("0"); } printf("\n"); } return 0; } printf("YES\n"); rem = n; for (i = 2; i <= n; i++) { if (rem == c) break; ar[1][i] = 1; ar[i][1] = 1; rem--; } if (b > a) { for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { if (i == j) continue; ar[i][j] = !ar[i][j]; } } } for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) printf("%d", ar[i][j]); printf("\n"); } return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; void doRoutine() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } const long long LOGN = 65; const long long MAXN = 1000000000000000000; long long max_sqrt[LOGN]; long long fpow(long long a, long long n) { if (n == 0) { return 1; } else if (n % 2 == 0) { return fpow(a * a, n / 2); } else { return fpow(a, n - 1) * a; } } long long get_root(long long n, long long i) { long long l = -1, r = max_sqrt[i] + 1; while (r - l > 1) { long long m = (l + r) / 2; if (fpow(m, i) <= n) { l = m; } else { r = m; } } return l; } long long solve(long long n) { long long logn = 65; vector<long long> dp(logn); vector<long long> root(logn); for (long long i = 1; i < logn; ++i) { root[i] = get_root(n, i); } for (long long i = logn - 1; i >= 0; --i) { if (root[i] >= 2) { long long sum = 0; for (long long x = 2; x < LOGN; ++x) { long long id = i * x; if (id < logn) { sum += dp[id]; } } dp[i] = root[i] - 1 - sum; } } return dp[1]; } signed main() { doRoutine(); max_sqrt[1] = MAXN; max_sqrt[2] = 1000000000; for (long long i = 3; i < LOGN; ++i) { max_sqrt[i] = (long long)floor(pow((double)MAXN, 1. / (double)i)); } long long t; cin >> t; while (t--) { long long n; cin >> n; cout << solve(n) << endl; } return 0; }
2,400
CPP
t = int(input()) for _ in range(t): a, b = map(int, input().split()) print((b-a)%b)
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int a[N], par[N]; vector<int> adj[N], ret; void dfs(int x) { int i, u; for (i = 0; i < adj[x].size(); ++i) { u = adj[x][i]; dfs(u); if (a[u] != -1 && a[u] != a[x]) { printf("-1\n"); exit(0); } } if (a[x] == x) ret.push_back(x), a[x] = -1; } int main() { int n, m, i, j, p, q; cin >> n >> m; memset(par, -1, sizeof(par)); for (i = 0; i < m; ++i) { scanf("%d%d", &p, &q); adj[p].push_back(q); par[q] = p; } for (i = 1; i <= n; ++i) scanf("%d", &a[i]); for (i = 1; i <= n; ++i) if (par[i] == -1) dfs(i); cout << ret.size() << endl; for (i = 0; i < ret.size(); ++i) printf("%d\n", ret[i]); }
2,000
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 205; const int INF = (long long)1e9 + 5; const long long MOD = 998244353; int R, C, a[MAXN][MAXN], row[MAXN], col[MAXN], diff[MAXN]; void get_diff() { for (int i = 0; i < R; i++) { diff[i] = 0; for (int j = 0; j < C - 1; j++) { if (a[i][j] ^ col[j] != a[i][j + 1] ^ col[j + 1]) { diff[i]++; } } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> R >> C; for (int i = 0; i < R; i++) { for (int j = 0; j < C; j++) { int x; cin >> x; a[i][j] = x; } } bool ok = false; for (int c = 0; c <= C && !ok; c++) { for (int j = 0; j < C; j++) { col[j] = a[0][j] ^ (j >= c); } get_diff(); int tot = 0; for (int k = 0; k < R; k++) { tot += diff[k]; } if (tot > 1) continue; ok = true; bool occ = false; for (int i = 0; i < R; i++) { if (diff[i] == 1 || !occ) { row[i] = a[i][0] ^ col[0]; } else { row[i] = a[i][0] ^ col[0] ^ 1; } occ = occ || diff[i] == 1; } } if (ok) { cout << "YES\n"; for (int i = 0; i < R; i++) cout << row[i]; cout << '\n'; for (int i = 0; i < C; i++) cout << col[i]; cout << '\n'; } else { cout << "NO\n"; } return 0; }
2,200
CPP
for _ in range(int(input())): c1,c2,c3=map(int,input().split()) a1,a2,a3,a4,a5=map(int,input().split()) if a1>c1 or a2>c2 or a3>c3: print("NO") else: c1=c1-a1 c2=c2-a2 c3=c3-a3 k=a4-min(c1,a4) l=a5-min(c2,a5) if k+l<=c3: print("YES") else: print("NO")
900
PYTHON3
#include <bits/stdc++.h> using namespace std; unsigned long long int A[100010]; int tab[100010]; int main() { int n; scanf("%d\n", &n); for (int i = 1; i <= n; i++) scanf("%d", &A[i]); for (int i = 1; i <= n; i++) for (int j = 1; i + j <= n; j <<= 1) tab[i] = i + j; unsigned long long int res = 0; for (int i = 1; i <= (n - 1); i++) { if (A[i] > 0) { A[tab[i]] += A[i]; res += A[i]; } printf("%I64d\n", res); } return (0); }
1,000
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n == 1) cout << 2; else { cout << 2 << endl; long long prev = 1; long double m = 2; for (long long i = 2; i <= n; i++) { long long ans = i * (i + 1) * (i + 1) - prev; cout << ans << endl; prev = i; } } }
1,600
CPP
n,v=map(int,input().split()) i=1 su=v+0 if n-1<=v: print(n-1) else: while n-1>v: i+=1 n-=1 su+=i print(su)
900
PYTHON3
n = int(input()) pos = [int(x) for x in input().split()] pos.sort() print(pos[(n-1)//2])
1,400
PYTHON3
for _ in range(int(input())): n=int(input()) s=input() x=[] if(n==1): print("NO") elif(n==2): if(s[0]>=s[1]): print("NO") else: print("YES") print('2') print(s[0],s[1]) else: c=1 x.append(s[0]) t=0 i=1 while(i+c<=n-1): #print(i,c,t) #print(x) #print(int(s[i:i+c]),int(x[t])) #print() if(int(s[i:i+c])>int(x[t])): x.append(s[i:i+c]) t=t+1 i=i+c #print(i,c) else: c=c+1 x.append(s[i:i+c]) if(int(x[len(x)-2])>=int(x[len(x)-1])): x[len(x)-2]=x[len(x)-2]+x[len(x)-1] x.pop(len(x)-1) print("YES") print(len(x)) print(*x)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; struct Wp { int row; int width; } w[200010]; char s[400010]; bool cmp(Wp aa, Wp bb) { return aa.width > bb.width; } int hs[200010]; bool ocp[200010]; int main() { int i, j, pt_intro, hs_pt = -1, tmp; bool flag = true; scanf("%d", &n); pt_intro = n + 1; for (i = 1; i <= n; i++) { scanf("%d", &w[i].width); w[i].row = i; ocp[i] = false; } sort(w + 1, w + 1 + n, cmp); scanf("%s", s); for (i = 0; i < 2 * n; i++) { if (s[i] == '0') { pt_intro--; printf("%d ", w[pt_intro].row); if (flag) { flag = false; hs_pt++; hs[hs_pt] = pt_intro; } else { hs[hs_pt]--; } } else { flag = true; tmp = w[hs[hs_pt]].row; printf("%d ", tmp); ocp[hs[hs_pt]] = true; hs[hs_pt]++; if (ocp[hs[hs_pt]]) { hs_pt--; } } } return 0; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; int t,s; struct p{ int x; int y; }; struct a{ int s; p pos[4]; }; char ch; a zip[405]; int main(){ cin>>t; for(int times=0;times<t;times++){ cin>>zip[times].s; int k=0,flag=1; for(int i=1;i<=zip[times].s;i++){ for(int j=1;j<=zip[times].s;j++){ cin>>ch; if(ch=='*'){ zip[times].pos[k].x=i; zip[times].pos[k].y=j; k++; } } } if(zip[times].pos[0].x==zip[times].pos[1].x){ if(zip[times].pos[0].x<zip[times].s){ zip[times].pos[2].x=zip[times].pos[0].x+1; zip[times].pos[3].x=zip[times].pos[0].x+1; zip[times].pos[2].y=zip[times].pos[0].y; zip[times].pos[3].y=zip[times].pos[1].y; } else if(zip[times].pos[0].x>1){ zip[times].pos[2].x=zip[times].pos[0].x-1; zip[times].pos[3].x=zip[times].pos[0].x-1; zip[times].pos[2].y=zip[times].pos[0].y; zip[times].pos[3].y=zip[times].pos[1].y; } } else if(zip[times].pos[0].y==zip[times].pos[1].y){ if(zip[times].pos[0].y<zip[times].s){ zip[times].pos[2].y=zip[times].pos[0].y+1; zip[times].pos[3].y=zip[times].pos[0].y+1; zip[times].pos[2].x=zip[times].pos[0].x; zip[times].pos[3].x=zip[times].pos[1].x; } else if(zip[times].pos[0].y>1){ zip[times].pos[2].y=zip[times].pos[0].y-1; zip[times].pos[3].y=zip[times].pos[0].y-1; zip[times].pos[2].x=zip[times].pos[0].x; zip[times].pos[3].x=zip[times].pos[1].x; } } else { zip[times].pos[2].y=zip[times].pos[0].y; zip[times].pos[3].y=zip[times].pos[1].y; zip[times].pos[2].x=zip[times].pos[1].x; zip[times].pos[3].x=zip[times].pos[0].x; } } for(int times=0;times<t;times++){ int flag[405][405]={0}; for(int k=0;k<4;k++){ flag[zip[times].pos[k].x][zip[times].pos[k].y]=1; } for(int i=1;i<=zip[times].s;i++){ for(int j=1;j<=zip[times].s;j++){ if(flag[i][j])printf("*"); else printf("."); } printf("\n"); } } return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; struct ED { int pre, id, w; } ed[N], ed1[N]; int head[N], tot, a[N], vis[N], flag[N], dis[N], head1[N], tot1; void add(int u, int v) { ed[++tot].pre = head[u]; ed[tot].id = v; ed[tot].w = 1; head[u] = tot; } void add1(int u, int v) { ed1[++tot1].pre = head1[u]; ed1[tot1].id = v; head1[u] = tot1; } priority_queue<pair<int, int> > q; void dij(int x) { memset(dis, 0x3f, sizeof dis); dis[x] = 0; q.push(make_pair(0, x)); while (q.size()) { int u = q.top().second; q.pop(); if (vis[u]) continue; vis[u] = 1; int i; for (i = head[u]; ~i; i = ed[i].pre) { int v = ed[i].id, w = 1; if (dis[v] > dis[u] + 1) { head1[v] = -1; add1(v, u); dis[v] = dis[u] + 1; q.push(make_pair(-dis[v], v)); } else if (dis[v] == dis[u] + 1) add1(v, u); } } } int main() { int n, m, i, j, k; while (scanf("%d %d", &n, &m) == 2) { int u, v; memset(head, -1, sizeof head); memset(head1, -1, sizeof head1); memset(vis, 0, sizeof vis); tot = 1; tot1 = 1; for (i = 1; i <= m; i++) { scanf("%d %d", &u, &v); add(v, u); } int len; scanf("%d", &len); for (i = 1; i <= len; i++) scanf("%d", &a[i]); dij(a[len]); int mi = 0, ma = 0; for (i = 1; i < len; i++) { int flag1 = 1; mi++; for (j = head1[a[i]]; ~j; j = ed1[j].pre) { if (ed1[j].id == a[i + 1]) mi--; else if (flag1) ma++, flag1 = 0; } } printf("%d %d\n", mi, ma); } }
1,700
CPP
#include <bits/stdc++.h> using namespace std; int t, tt, ii, xx, yy, x, y, ans, n, i, h, j, k, b, a, minn, maxx, p; int g[1000000], fix[1000000], F[1000000]; pair<int, int> d[1000000]; int main() { scanf("%d %d", &n, &h); for (i = 1; i <= n; i++) { scanf("%d", &a); d[i] = make_pair(a, i); } sort(d + 1, d + n + 1); reverse(d + 1, d + n + 1); if (n == 2) { cout << 0 << endl; cout << 1 << " " << 2 << " " << endl; return 0; } if (n == 3) { ans = 1000000000; for (i = 1; i <= 2; i++) for (j = 1; j <= 2; j++) for (k = 1; k <= 2; k++) { fix[1] = i; fix[2] = j; fix[3] = k; minn = 1000000000; maxx = -1000000000; for (a = 1; a <= 3; a++) for (b = a + 1; b <= 3; b++) if (fix[a] == fix[b]) { p = d[a].first + d[b].first; minn = min(minn, p); maxx = max(maxx, p); } else { p = d[a].first + d[b].first + h; minn = min(minn, p); maxx = max(maxx, p); } if (ans > maxx - minn) { ans = maxx - minn; g[1] = i; g[2] = j; g[3] = k; } } cout << ans << endl; for (i = 1; i <= 3; i++) F[d[i].second] = g[i]; for (i = 1; i <= 3; i++) cout << F[i] << " "; cout << endl; return 0; } ans = 1000000000; if (n <= 5) { for (i = 1; i <= 2; i++) for (j = 1; j <= 2; j++) for (k = 1; k <= 2; k++) for (a = 1; a <= 2; a++) { fix[1] = i; fix[2] = j; fix[3] = k; fix[4] = a; minn = 1000000000; maxx = -1000000000; for (x = 1; x <= 4; x++) for (y = x + 1; y <= 4; y++) if (fix[x] == fix[y]) { if (x >= 3) xx = x - 3 + n - 1; else xx = x; if (y >= 3) yy = y - 3 + n - 1; else yy = y; p = d[xx].first + d[yy].first; minn = min(minn, p); maxx = max(maxx, p); } else { if (x >= 3) xx = x - 3 + n - 1; else xx = x; if (y >= 3) yy = y - 3 + n - 1; else yy = y; p = d[xx].first + d[yy].first + h; minn = min(minn, p); maxx = max(maxx, p); } if (ans > maxx - minn) { ans = maxx - minn; for (ii = 1; ii <= n; ii++) F[ii] = 1; F[d[1].second] = fix[1]; F[d[2].second] = fix[2]; F[d[n - 1].second] = fix[3]; F[d[n].second] = fix[4]; } } } else for (i = 1; i <= 2; i++) for (j = 1; j <= 2; j++) for (t = 1; t <= 2; t++) for (k = 1; k <= 2; k++) for (a = 1; a <= 2; a++) for (tt = 1; tt <= 2; tt++) { fix[1] = i; fix[2] = j; fix[3] = t; fix[4] = k; fix[5] = a; fix[6] = tt; minn = 1000000000; maxx = -1000000000; for (x = 1; x <= 6; x++) for (y = x + 1; y <= 6; y++) if (fix[x] == fix[y]) { if (x >= 4) xx = x - 5 + n - 1; else xx = x; if (y >= 4) yy = y - 5 + n - 1; else yy = y; p = d[xx].first + d[yy].first; minn = min(minn, p); maxx = max(maxx, p); } else { if (x >= 4) xx = x - 5 + n - 1; else xx = x; if (y >= 4) yy = y - 5 + n - 1; else yy = y; p = d[xx].first + d[yy].first + h; minn = min(minn, p); maxx = max(maxx, p); } if (ans > maxx - minn) { ans = maxx - minn; for (ii = 1; ii <= n; ii++) F[ii] = 1; F[d[1].second] = fix[1]; F[d[2].second] = fix[2]; F[d[3].second] = fix[3]; F[d[n - 2].second] = fix[4]; F[d[n - 1].second] = fix[5]; F[d[n].second] = fix[6]; } } cout << ans << endl; for (i = 1; i <= n; i++) cout << F[i] << " "; cout << endl; return 0; }
1,800
CPP