solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; struct Node { Node *left, *right; int l, r; vector<int> *vylist; int maxy; }; const int MAXN = 200000; int request[MAXN][3]; int discrete[MAXN]; int dislen; Node *root; int ansx, ansy; bool ansfound; Node *MakeSegmentTree(int l, int r) { Node *ret = new Node; ret->l = l; ret->r = r; ret->maxy = -1; if (l == r) { ret->left = NULL; ret->right = NULL; ret->vylist = new vector<int>(); } else { int mid = (l + r) / 2; ret->left = MakeSegmentTree(l, mid); ret->right = MakeSegmentTree(mid + 1, r); ret->vylist = NULL; } return ret; } void AddPointSegmentTree(Node *p, int reqx, int reqy) { if (p->l == p->r) { p->vylist->insert(upper_bound(p->vylist->begin(), p->vylist->end(), reqy), reqy); p->maxy = p->vylist->back(); } else { int mid = (p->l + p->r) / 2; if (discrete[mid] >= reqx) AddPointSegmentTree(p->left, reqx, reqy); else AddPointSegmentTree(p->right, reqx, reqy); p->maxy = max(p->left->maxy, p->right->maxy); } return; } void RemovePointSegmentTree(Node *p, int reqx, int reqy) { if (p->l == p->r) { p->vylist->erase(lower_bound(p->vylist->begin(), p->vylist->end(), reqy)); if (p->vylist->size() == 0) p->maxy = -1; else p->maxy = p->vylist->back(); } else { int mid = (p->l + p->r) / 2; if (discrete[mid] >= reqx) RemovePointSegmentTree(p->left, reqx, reqy); else RemovePointSegmentTree(p->right, reqx, reqy); p->maxy = max(p->left->maxy, p->right->maxy); } return; } void FindPointSegmentTree(Node *p, int reqx, int reqy) { if (p->maxy <= reqy) return; if (p->l == p->r) { vector<int>::iterator iter = upper_bound(p->vylist->begin(), p->vylist->end(), reqy); ansx = discrete[p->l]; ansy = *iter; ansfound = true; return; } else { int mid = (p->l + p->r) / 2; if (discrete[mid] > reqx) { FindPointSegmentTree(p->left, reqx, reqy); if (ansfound) return; FindPointSegmentTree(p->right, reqx, reqy); } else if (discrete[p->r] > reqx) { FindPointSegmentTree(p->right, reqx, reqy); } } return; } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { char st[10]; scanf("%s %d %d", st, &(request[i][1]), &(request[i][2])); if (strcmp(st, "add") == 0) request[i][0] = 0; else if (strcmp(st, "remove") == 0) request[i][0] = 1; else if (strcmp(st, "find") == 0) request[i][0] = 2; discrete[i] = request[i][1]; } sort(discrete, discrete + n); dislen = unique(discrete, discrete + n) - discrete; root = MakeSegmentTree(0, dislen - 1); for (int i = 0; i < n; i++) if (request[i][0] == 0) AddPointSegmentTree(root, request[i][1], request[i][2]); else if (request[i][0] == 1) RemovePointSegmentTree(root, request[i][1], request[i][2]); else if (request[i][0] == 2) { ansfound = false; FindPointSegmentTree(root, request[i][1], request[i][2]); if (!ansfound) printf("-1\n"); else printf("%d %d\n", ansx, ansy); } return 0; }
2,800
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100; const int inf = ~0U >> 3; struct Node { int to, next, c, w; } e[N * N << 2]; int box[N], size; int n, k; inline void add(int from, int to, int w, int c) { e[size].to = to, e[size].w = w, e[size].c = c, e[size].next = box[from], box[from] = size++; e[size].to = from, e[size].w = -w, e[size].c = 0, e[size].next = box[to], box[to] = size++; } int dis[N], p[N]; bool flag[N]; bool spfa(int s, int t) { fill(dis, dis + n, inf); memset(flag, false, sizeof(flag)); memset(p, -1, sizeof(p)); queue<int> Q; Q.push(s), dis[s] = 0; while (Q.size()) { int x = Q.front(); Q.pop(), flag[x] = false; for (int i = box[x]; ~i; i = e[i].next) if (e[i].c && dis[e[i].to] - dis[x] > e[i].w) { dis[e[i].to] = dis[x] + e[i].w; p[e[i].to] = i; if (!flag[e[i].to]) flag[e[i].to] = true, Q.push(e[i].to); } } return dis[t] != inf; } int mcmf(int s, int t) { int cost = 0, flow = 0; while (spfa(s, t)) { int mn = inf, x = t; if (dis[t] > k) break; while (p[x] != -1) { mn = min(mn, e[p[x]].c); x = e[p[x] ^ 1].to; } if (dis[t]) mn = min(mn, k / dis[t]); k -= dis[t] * mn; flow += mn; x = t; while (p[x] != -1) { e[p[x]].c -= mn; e[p[x] ^ 1].c += mn; x = e[p[x] ^ 1].to; } } return flow; } int main() { scanf("%d %d", &n, &k); memset(box, -1, sizeof(box)), size = 0; for (int i = 0; i < n; ++i) for (int j = 0, x; j < n; ++j) { scanf("%d", &x); if (x) { add(i, j, 0, x); add(i, j, 1, inf); } } cout << mcmf(0, n - 1) << endl; return 0; }
2,300
CPP
#include <bits/stdc++.h> using namespace std; long long bin_pow(long long a, long long b) { if (b == 0) return 1; if (b % 2 == 0) { long long t = bin_pow(a, b / 2); return t * t % 1000000007; } else return a * bin_pow(a, b - 1) % 1000000007; } int main() { ios::sync_with_stdio(false); cin.tie(0); long long t, n, m, k, x = 0, y = 0, ans = 0, z = 0, sum = 0, l = 0, r = 0, dp[200000][2], a, b; cin >> t; while (t--) { ans = 0; string s; cin >> n >> a >> b >> s; for (int i = 0; i < n; i++) for (int j = 0; j < 2; j++) dp[i][j] = LLONG_MAX / 4; dp[0][0] = a + b; dp[0][1] = a + b + a; for (int i = 1; i < n; i++) { if (s[i] == '0' && (i == n - 1 || (i != n - 1 && s[i + 1] == '0'))) dp[i][0] = min(dp[i - 1][0] + a + b, dp[i - 1][1] + 2 * a + 2 * b); if (s[i] == '0') dp[i][1] = min(dp[i - 1][0] + 2 * a + b, dp[i - 1][1] + 2 * b + a); else dp[i][1] = dp[i - 1][1] + 2 * b + a; } ans = dp[n - 1][0]; cout << ans + b; cout << "\n"; } return 0; }
1,500
CPP
#include <bits/stdc++.h> using namespace std; int main() { char a; int white = 0, black = 0; for (int i = 0; i < 64; i++) { cin >> a; switch (a) { case 81: white += 9; break; case 82: white += 5; break; case 66: white += 3; break; case 78: white += 3; break; case 80: white += 1; break; case 113: black += 9; break; case 114: black += 5; break; case 98: black += 3; break; case 110: black += 3; break; case 112: black += 1; break; default: break; } } if (white > black) cout << "White"; else if (black > white) cout << "Black"; else cout << "Draw"; return 0; }
900
CPP
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 5, M = 2e4 + 5, inf = 0x3f3f3f3f, mod = 1e9 + 7; int n, m, q, cnt; int a[N], f[N], in[N], out[N], pos[N], del[N]; pair<int, int> edge[N], Q[N]; int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); } vector<int> g[N]; struct node { int l, r, mx; } tr[N << 2]; void Merge(int x, int y) { x = find(x), y = find(y); if (x == y) return; n++; f[n] = n, f[x] = f[y] = n; g[n].push_back(x), g[n].push_back(y); } void dfs(int u) { in[u] = ++cnt; for (int v : g[u]) if (!in[v]) dfs(v); out[u] = cnt; } void re(int x) { tr[x].mx = max(tr[x << 1].mx, tr[x << 1 | 1].mx); } void build(int x, int l, int r) { tr[x].l = l, tr[x].r = r, tr[x].mx = 0; if (l == r) { return; } int mid = (l + r) >> 1; build(x << 1, l, mid); build(x << 1 | 1, mid + 1, r); } void upd(int x, int p, int val) { if (tr[x].l == tr[x].r) { tr[x].mx = val; return; } int mid = (tr[x].l + tr[x].r) >> 1; if (p <= mid) upd(x << 1, p, val); else upd(x << 1 | 1, p, val); re(x); } int que(int x, int l, int r) { if (tr[x].l >= l && tr[x].r <= r) return tr[x].mx; int ans = 0, mid = (tr[x].l + tr[x].r) >> 1; if (l <= mid) ans = max(ans, que(x << 1, l, r)); if (r > mid) ans = max(ans, que(x << 1 | 1, l, r)); return ans; } int main() { scanf("%d%d%d", &n, &m, &q); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]), pos[a[i]] = i, f[i] = i; } for (int i = 1; i <= m; i++) scanf("%d%d", &edge[i].first, &edge[i].second); for (int i = 1; i <= q; i++) { scanf("%d%d", &Q[i].first, &Q[i].second); if (Q[i].first == 2) del[Q[i].second] = 1; } for (int i = 1; i <= m; i++) if (!del[i]) Merge(edge[i].first, edge[i].second); for (int i = q; i; i--) { if (Q[i].first == 1) Q[i].second = find(Q[i].second); else Merge(edge[Q[i].second].first, edge[Q[i].second].second); } for (int i = 1; i <= n; i++) if (f[i] == i) dfs(i); build(1, 1, n); for (int i = 1; i <= n; i++) upd(1, in[i], a[i]); for (int i = 1; i <= q; i++) { if (Q[i].first == 1) { int ans = que(1, in[Q[i].second], out[Q[i].second]); printf("%d\n", ans); if (ans) upd(1, in[pos[ans]], 0); } } return 0; }
2,600
CPP
try: ps = input() if len(ps) <= 100 and "1": v = True for i in ps: if i not in ["0","1"]: v = False break if v == True: if 7*"0" in ps or 7*"1" in ps: print("YES") else: print("NO") except: pass
900
PYTHON3
a,b = list(map(int, input().split())) # print(a,b) i=0 while a<=b: a*=3 b*=2 i+=1 print(i)
800
PYTHON3
words = list(input()) ans = '' vowels = ['a', 'e', 'i', 'o', 'y', 'u', 'A', 'E', 'I', 'O', 'U', 'Y'] for w in words: if w not in vowels: ans += '.' if w.isupper(): ans += w.lower() else: ans += w print(ans)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int t, n, x, k; int A[10005]; int main() { cin >> t; while (t--) { cin >> n >> x; for (int i = 1; i <= 200; i++) A[i] = 0; for (int i = 1; i <= n; i++) { cin >> k; A[k] = 1; } for (int i = 1; i <= 200; i++) if (x) { if (!A[i]) { A[i] = 1; x--; } } int j = 0; for (int i = 1; i <= 200; i++) { if (!A[i]) break; j++; } cout << j << '\n'; } return 0; }
900
CPP
#include <bits/stdc++.h> #pragma gcc optimize("Ofast,no-stack-protector,tune=native") #pragma gcc optimize("sse,sse2,sse3,sse4,ssse3") #pragma gcc optimize("O2") #pragma gcc optimize("abm,mmx,avx,avx2,unroll-loops,fast-math,section-anchors") using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); void solve() { int n; cin >> n; vector<int> a(n), in(n); for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } if (n < 3) { cout << ((n == 1 || a[0] <= a[1]) ? "YES\n" : "NO\n"); return; } vector<int> p(n, 0), val(n, -1); set<int> wrong; for (int i = 0; i < n; i++) { p[a[i]] = 1; val[a[i]] = i; if (a[i] != i) wrong.insert(i); } int sum = accumulate(p.begin(), p.end(), 0); ; if (sum != n) { cout << ((1) ? "YES\n" : "NO\n"); return; } for (int val2_ = 0; val2_ < n; val2_++) { int val2 = val2_; if (wrong.find(val2) == wrong.end()) continue; int pos2 = val[val2]; int pos1 = val2; int val1 = a[pos1]; auto it = wrong.begin(); while (it != wrong.end() && (*it == pos1 || *it == pos2)) { it++; } if (it == wrong.end()) { cout << ((0) ? "YES\n" : "NO\n"); return; } int pos3 = *it, val3 = a[pos3]; if ((pos2 > pos1 && pos2 > pos3 && pos1 < pos3) || (pos2 < pos1 && pos2 < pos3 && pos1 < pos3)) { vector<pair<int, int> > cur = {{pos1, val1}, {pos2, val2}, {pos3, val3}}; sort(cur.begin(), cur.end()); tie(pos1, val1) = cur[0]; tie(pos2, val2) = cur[1]; tie(pos3, val3) = cur[2]; a[pos2] = val1; a[pos3] = val2; a[pos1] = val3; val[val1] = pos2; val[val2] = pos3; val[val3] = pos1; if (val1 == pos2) wrong.erase(pos2); if (val2 == pos3) wrong.erase(pos3); if (val3 == pos1) wrong.erase(pos1); } else { vector<pair<int, int> > cur = {{pos1, val1}, {pos2, val2}, {pos3, val3}}; sort(cur.begin(), cur.end()); tie(pos1, val1) = cur[0]; tie(pos2, val2) = cur[1]; tie(pos3, val3) = cur[2]; a[pos1] = val2; a[pos2] = val3; a[pos3] = val1; val[val2] = pos1; val[val3] = pos2; val[val1] = pos3; if (val2 == pos1) wrong.erase(pos1); if (val3 == pos2) wrong.erase(pos2); if (val1 == pos3) wrong.erase(pos3); } } cout << ((1) ? "YES\n" : "NO\n"); return; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; cin >> t; while (t--) solve(); }
1,900
CPP
t = int(input()) for case_num in range(t): n, x, y = map(int, input().split(' ')) best = 1 if x + y > n: best = min(x + y - n + 1, n) worst = min(x + y - 1, n) print(best, worst)
1,700
PYTHON3
#!/bin/python3 from math import ceil for _ in range(int(input().strip())): s, i, e = [int(i) for i in input().strip().split(' ')] ee = max(s, (s+i+e)//2 + 1) - s print(max(e - ee + 1, 0)) """ 2 1 0 """
1,300
PYTHON3
n, m = map(int, input().split()) g, v = [], n * m for i in range(n): g.append([0]) for x in map(int, input().split()): g[-1].append(g[-1][-1] + x) a, b = map(int, input().split()) for i in range(n): for j in range(1, m + 1): if i >= a - 1 and j >= b: v = min(v, sum(g[k][j] - g[k][j - b] for k in range(i - a + 1, i + 1))) if i >= b - 1 and j >= a: v = min(v, sum(g[k][j] - g[k][j - a] for k in range(i - b + 1, i + 1))) print(v) # Made By Mostafa_Khaled
1,200
PYTHON3
import sys input = sys.stdin.readline n = int(input()) iii = [list(map(int,input().split())) for i in range(n)] a = [[0 for i in range(n)] for j in range(n)] ans = [[0 for i in range(n)] for j in range(n)] for i in range(n): for j in range(n): a[i][j] = (iii[i][2*j],iii[i][2*j+1]) for i in range(n): for j in range(n): if a[i][j] == (i+1,j+1): ans[i][j] = "X" elif a[i][j][0] < 0: flg = 0 for ni,nj in (i+1,j),(i-1,j),(i,j+1),(i,j-1): if 0<=ni<=n-1 and 0<=nj<=n-1 and a[i][j] == a[ni][nj]: flg = 1 if ni == i+1 and nj == j: ans[i][j] = "D" elif ni == i-1 and nj == j: ans[i][j] = "U" elif ni == i and nj == j+1: ans[i][j] = "R" elif ni == i and nj == j-1: ans[i][j] = "L" if not flg: print("INVALID") exit() for i in range(n): for j in range(n): if ans[i][j] == "X": stack = [(i,j)] while stack: x,y = stack.pop() for nx,ny in (x+1,y),(x-1,y),(x,y+1),(x,y-1): if 0<=nx<=n-1 and 0<=ny<=n-1 and a[x][y] == a[nx][ny] and ans[nx][ny] == 0: if nx == x+1 and ny == y: ans[nx][ny] = "U" stack.append((nx,ny)) elif nx == x-1 and ny == y: ans[nx][ny] = "D" stack.append((nx,ny)) elif nx == x and ny == y+1: ans[nx][ny] = "L" stack.append((nx,ny)) elif nx == x and ny == y-1: ans[nx][ny] = "R" stack.append((nx,ny)) for i in range(n): if ans[i].count(0) >= 1: print("INVALID") exit() print("VALID") for arr in ans: print(*arr,sep="")
2,000
PYTHON3
t = 0 j = 0 for i in range(int(input())): a = list(map(int, input().split())) if i == 0: t = max(a) else: if max(a) <= t: t = max(a) elif min(a) <= t: t = min(a) else: print('NO') j += 1 break if j == 0: print('YES')
1,000
PYTHON3
#include <bits/stdc++.h> inline int Max(int x, int y) { return x > y ? x : y; } inline int Min(int x, int y) { return x > y ? y : x; } inline void Swap(int x, int y) { x = x ^ y; y = y ^ x; x = x ^ y; } inline int Mid(int x, int y) { return (x + y) >> 1; } inline int Lson(int x) { return x << 1; } inline int Rson(int x) { return x << 1 | 1; } inline int Ceil(int x, int y) { int ans = x / y; if (x % y) ans++; return ans; } inline int Floor(int x, int y) { return x / y; } using namespace std; const int MAXSIZE = 65; const int INF = 0x3f3f3f3f; const int EPS = 1e-10; int n, m, r, l, u, v, k; int cost[MAXSIZE][MAXSIZE][MAXSIZE]; int DP[1010][MAXSIZE][MAXSIZE]; void Floyd_Warshell(int index) { for (int med = 1; med <= n; med++) { for (int start = 1; start <= n; start++) { for (int End = 1; End <= n; End++) { cost[index][start][End] = Min(cost[index][start][End], cost[index][start][med] + cost[index][med][End]); } } } } void Pretreatement() { memset(DP, 0x3f, sizeof(DP)); for (int index = 1; index <= m; index++) { for (int start = 1; start <= n; start++) { for (int End = 1; End <= n; End++) { for (int med = 1; med <= n; med++) { DP[0][start][End] = Min(DP[0][start][End], cost[index][start][med] + cost[index][med][End]); } } } } for (int times = 1; times <= n; times++) { for (int start = 1; start <= n; start++) { for (int End = 1; End <= n; End++) { for (int med = 1; med <= n; med++) { DP[times][start][End] = Min(DP[times][start][End], DP[times - 1][start][med] + DP[0][med][End]); } } } } } void Solve() { int ans = INF; for (int times = 0; times <= k; times++) ans = Min(ans, DP[times][u][v]); printf("%d\n", ans); } int main() { while (~scanf("%d%d%d", &n, &m, &r)) { memset(cost, 0x3f, sizeof(cost)); for (int index = 1; index <= m; index++) { for (int from = 1; from <= n; from++) { for (int to = 1; to <= n; to++) { scanf("%d", &l); cost[index][from][to] = l; } } } for (int index = 1; index <= m; index++) { Floyd_Warshell(index); } Pretreatement(); for (int i = 0; i < r; i++) { scanf("%d%d%d", &u, &v, &k); k = Min(k, n); Solve(); } } return 0; }
1,800
CPP
#include <bits/stdc++.h> using namespace std; int kmp(string s1, string s2) { string s = s1 + '&' + s2; vector<int> p; p.resize(s.size()); for (int i = (1); i < (p.size()); ++i) { int k = p[i - 1]; while ((k) && (s[i] != s[k])) k = p[k - 1]; if (s[i] == s[k]) k++; p[i] = k; } return p[p.size() - 1]; } int prep[110][30]; string p; string need; int n, k; int dp[110][110]; int way[110][110]; int r(int pos, int pref) { if (pos == n) { if ((pos >= p.size()) && (need[pos - p.size()] == '1')) { return (pref == p.size()); } else { return (pref != p.size()); } } if (dp[pos][pref] != -1) return dp[pos][pref]; int res = 0; if ((pos >= p.size()) && (need[pos - p.size()] == '1') && (pref != p.size())) return dp[pos][pref] = res; if ((pos >= p.size()) && (need[pos - p.size()] == '0') && (pref == p.size())) return dp[pos][pref] = res; for (int i = (0); i < (k); ++i) { res = max(res, r(pos + 1, prep[pref][i])); if (res) { way[pos][pref] = i; break; } } return dp[pos][pref] = res; } int main() { cin >> n >> k; cin >> p; cin >> need; for (int i = (0); i < (p.size() + 1); ++i) { string s1 = ""; for (int j = (0); j < (i); ++j) s1 += p[j]; s1 += ' '; for (int j = (0); j < (k); ++j) { s1[s1.size() - 1] = 'a' + j; prep[i][j] = kmp(p, s1); } } memset(dp, -1, sizeof(dp)); int res = r(0, 0); if (res) { int pos = 0, pref = 0; while (pos < n) { printf("%c", way[pos][pref] + 'a'); pref = prep[pref][way[pos][pref]]; pos++; } printf("\n"); } else cout << "No solution" << endl; return 0; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); int now = 0; for (int i = 1; i <= n; i++) if (i % 3 == 1) printf("%d %d\n", now, 1); else if (i % 3 == 2) printf("%d %d\n", now++, -1); else printf("%d %d\n", now++, 0); return 0; }
2,600
CPP
#include <bits/stdc++.h> using namespace std; int main() { int c, v0, v1, a, l; int ans = 1; int p = 0; cin >> c >> v0 >> v1 >> a >> l; p = v0; while (p < c) { ans++; p -= l; if (v0 + a * (ans - 1) > v1) p += v1; else p += v0 + a * (ans - 1); } cout << ans << endl; }
900
CPP
from collections import namedtuple import math Point = namedtuple('Point', ['x', 'y']) Line = namedtuple('Line', ['a', 'b', 'c']) toPoint = lambda x: Point(int(x[0]), int(x[1])) toLine = lambda x: Line(int(x[0]), int(x[1]), int(x[2])) def cppdiv(a, b): if b == 0: return a * math.inf return a / b def parallel(l1, l2): return cppdiv(l1.a, l1.b) == cppdiv(l2.a, l2.b) def within(a, p, b): return (min(a.x, b.x) <= p.x <= max(a.x, b.x)) and (min(a.y, b.y) <= p.y <= max(a.y, b.y)) def intersect(l1, l2): return Point( cppdiv( (l1.c * l2.b - l1.b * l2.c), (l1.b * l2.a - l1.a * l2.b) ), cppdiv( (l1.a * l2.c - l1.c * l2.a), (l1.b * l2.a - l1.a * l2.b ) ) ) home = toPoint( input().split()) uni = toPoint( input().split() ) n = int(input()) lines = [ toLine(input().split()) for _ in range(n) ] home_uni = Line(home.y - uni.y, uni.x - home.x, (home.x * uni.y) - (uni.x * home.y)) c = 0 for l in filter(lambda x: not parallel(home_uni, x) , lines): if within(home, intersect(home_uni, l), uni): c += 1 print(c)
1,700
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 10; int n, m, q, v[maxn], lp[maxn]; vector<int> e[maxn], t[maxn]; vector<long long> ps[maxn]; int mx, argmx; void dfs(int id, int p, int d, int cpn) { if (cpn) v[id] = cpn; lp[id] = max(lp[id], d); if (d > mx) { mx = d; argmx = id; } if (cpn) t[cpn].push_back(lp[id]); for (int i = 0; i < e[id].size(); i++) if (e[id][i] != p) dfs(e[id][i], id, d + 1, cpn); } int main() { scanf("%d %d %d", &n, &m, &q); for (int i = 0; i < m; i++) { int x, y; scanf("%d %d", &x, &y); e[x].push_back(y); e[y].push_back(x); } int cpn = 0; for (int i = 1; i <= n; i++) if (!v[i]) { mx = -1; dfs(i, 0, 0, 0); mx = -1; dfs(argmx, 0, 0, 0); mx = -1; dfs(argmx, 0, 0, ++cpn); sort(t[cpn].begin(), t[cpn].end()); ps[cpn].push_back(0); for (int j = 0; j < t[cpn].size(); j++) ps[cpn].push_back(ps[cpn].back() + t[cpn][j]); } map<pair<int, int>, double> res; for (int i = 0; i < q; i++) { int x, y; scanf("%d %d", &x, &y); x = v[x]; y = v[y]; if (x == y) printf("-1\n"); else { if (t[x].size() > t[y].size()) swap(x, y); if (!res.count({x, y})) { auto ans = 0LL; int mxd = max(t[x].back(), t[y].back()); for (int j = 0; j < t[x].size(); j++) { int l = lower_bound(t[y].begin(), t[y].end(), mxd - 1 - t[x][j]) - t[y].begin(); ans += 1LL * l * mxd + 1LL * (t[y].size() - l) * (t[x][j] + 1) + ps[y].back() - ps[y][l]; } res.insert({{x, y}, ans * 1.0 / t[x].size() / t[y].size()}); } printf("%.10lf\n", res[{x, y}]); } } return 0; }
2,500
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 29; const int N = 1e6 + 9; long long vis[N]; vector<long long> v[N], ans; map<long long, long long> m1, m2; bool cmp(const pair<double, long long> &a, const pair<double, long long> &b) { if (a.first == b.first) { return a.second < b.second; } return a.first < b.first; } void dfs(long long a, long long b, long long prev) { vis[a] = 1; m1[a] = m1[a] ^ b; if (m1[a] != m2[a]) { b ^= 1; ans.push_back(a); } for (int i = 0; i < v[a].size(); i++) { if (vis[v[a][i]] == 0) { dfs(v[a][i], prev, b); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, a, b, k, p; cin >> n; for (int i = 0; i < n - 1; i++) { cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } for (int i = 0; i < n; i++) { cin >> k; m1[i + 1] = k; } for (int i = 0; i < n; i++) { cin >> p; m2[i + 1] = p; } memset(vis, 0, sizeof(vis)); dfs(1, 0, 0); cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i] << endl; } return 0; }
1,300
CPP
n = int(input()) l = list(map(int,input().split())) l = sorted(l,reverse=True) for i in range(n): l1 = l[:i] l2 = l[i:] if sum(l1)>sum(l2): print(i) exit() else: print(n)
900
PYTHON3
s = input() col = 0 for i in range(len(s)): if s[i] == 'A': l=0 r=0 for j in range(i): if s[j]=='Q': l+=1 for j in range(i+1,len(s)): if s[j]=='Q': r+=1 col+=r*l print(col)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int NN = 2002; const int MOD = (int)1e9 + 7; int n, m; char grid[NN][NN]; int dp[2][NN][NN]; int from_right[NN][NN]; int from_bottom[NN][NN]; int sum_row[NN]; int sum_col[NN][NN]; void norm(int& x) { x %= MOD; if (x < 0) x += MOD; } int get() { memset(dp, 0, sizeof(dp)); memset(sum_row, 0, sizeof(sum_row)); memset(sum_col, 0, sizeof(sum_col)); dp[0][n - 1][m - 1] = dp[1][n - 1][m - 1] = 1; for (int i = n - 1; i >= 0; i--) { sum_row[m] = 0; for (int j = m - 1; j >= 0; j--) { for (int dir = 0; dir < 2; dir++) { if (dir == 0) { sum_col[i + 1][j] = sum_col[i + 2][j] + dp[1][i + 1][j]; norm(sum_col[i + 1][j]); dp[dir][i][j] += sum_col[i + 1][j] - sum_col[i + (n - i - from_bottom[i + 1][j])][j]; norm(dp[dir][i][j]); } else { sum_row[j] = sum_row[j + 1] + dp[0][i][j]; norm(sum_row[j]); dp[dir][i][j] += sum_row[j + 1] - sum_row[j + (m - j - from_right[i][j + 1])]; norm(dp[dir][i][j]); } } } } int res = dp[0][0][0] + dp[1][0][0]; norm(res); return res; } int main() { cin >> n >> m; if (n == 1 && m == 1) { cout << 1 << endl; return 0; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cin >> grid[i][j]; } for (int i = 0; i < n; i++) { from_right[i][m] = 0; for (int j = m - 1; j >= 0; j--) { from_right[i][j] = from_right[i][j + 1] + (grid[i][j] == 'R'); } } for (int j = 0; j < m; j++) { from_bottom[n][j] = 0; for (int i = n - 1; i >= 0; i--) from_bottom[i][j] = from_bottom[i + 1][j] + (grid[i][j] == 'R'); } cout << get() << endl; return 0; }
2,200
CPP
#include <bits/stdc++.h> using namespace std; const char road[1000] = "C:" "\\Users\\lis\\Desktop\\百度网盘\\百度云同步盘\\testcheck\\席位安排\\data" "\\"; char roadnum[1000] = ""; char roadin[1000] = ""; char roadout[1000] = ""; void writeIntoFile(char num[]) { strcpy(roadnum, num); strcpy(roadin, road); strcat(roadin, roadnum); strcat(roadin, "."); strcpy(roadout, roadin); strcat(roadin, "in"); strcat(roadout, "out"); puts(roadin); puts(roadout); freopen(roadin, "r", stdin); freopen(roadout, "w", stdout); } int const N = 16; int ans[N]; int pos[N]; int vis[N + 10]; long long dp[N + 2][1 << N]; int g[N]; long long countp(int p, int n) { int i, j, k; memset(dp, 0, sizeof(dp)); memset(pos, -1, sizeof(pos)); for (i = 0; i <= p; i++) { pos[ans[i]] = i; } dp[0][0] = 1; int Z = 1 << n; Z--; for (i = 0; i < n; i++) { for (j = 0; j <= Z; j++) { if (!dp[i][j]) continue; for (k = 0; k < n; k++) { if (j & (1 << k)) continue; if (pos[i] == -1 || pos[i] == k) { if ((j & g[k]) == g[k]) { dp[i + 1][j | (1 << k)] += dp[i][j]; } } } } } return dp[n][Z]; } int main() { int y0 = 2001; int n, m; long long y, ym = 1e18; int i, j, k, s; int ioflag; while (scanf("%d%lld%d", &n, &y, &m) != EOF) { assert(1 <= n && n <= N); assert(y0 <= y && y <= ym); assert(0 <= m && m <= 100); memset(g, 0, sizeof(g)); memset(vis, 0, sizeof(vis)); while (m--) { int a, b; ioflag = scanf("%d%d", &a, &b); assert(1 <= a && a <= n); assert(1 <= b && b <= n); assert(a != b); a--, b--; g[b] |= (1 << a); } y -= y0; y++; for (i = 0; i < n; i++) { for (ans[i] = 0; ans[i] < n; ans[i]++) { if (vis[ans[i]]) continue; long long s = countp(i, n); if (y > s) { y -= s; continue; } else { vis[ans[i]] = 1; break; } } if (ans[i] >= n) { ans[0] = -1; break; } } if (ans[0] < 0) { puts("The times have changed"); continue; } for (i = 0; i < n; i++) { if (i) putchar(' '); printf("%d", ans[i] + 1); } puts(""); } return 0; }
2,400
CPP
a,b=map(int,input().split()) s=list(map(int,input().split())) x=0;razn=0 for i in range(a-1): if s[i]>s[i+1]: x=s[i]-s[i+1] razn=max(razn,x) print(max(razn-b,0))
1,000
PYTHON3
import sys from collections import defaultdict n, k = map(int, input().split()) a = list(map(int, input().split())) d = defaultdict(list) mx_occur = -1 for i in range(len(a)): d[a[i]].append(i) mx_occur = max(len(d[a[i]]), mx_occur) if k < mx_occur or k > n: print('NO') sys.exit(0) c = 1 for i in d: for j in range(len(d[i])): if k < c: c = 1 d[i][j] = (d[i][j], c) c += 1 #print(d) chert = set() ans = [0]*n for i in d: curr = d[i] for j in curr: x, y = j ans[x] = str(y) chert.add(y) if len(chert) < k: print('NO') sys.exit(0) print('YES') print(' '.join(ans))
1,400
PYTHON3
from sys import stdin,stdout import heapq import bisect import math import operator def array(): return [int(k) for k in stdin.readline().split()] def nums(): return list(map(int,stdin.readline().split())) def main(): #for _ in range(int(stdin.readline())): n,s=nums() value=s//2 if n>=s: print("no") elif n-1<value: print("yes") arr=[] minus=0 for i in range(n-1): arr.append(1) minus+=1 arr.append(s-minus) print(*arr) print(value) else: print("no") if __name__=="__main__": main()
1,400
PYTHON3
n = int(input()) s = input() ans = n - len(set(s)) if n < 27: print(ans) else: print(-1)
1,000
PYTHON3
x = int(input()) s1,s2 = x//5, x%5 if s2!=0: print(s1+1) else: print(s1)
800
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,sse,sse2,sse3") using namespace std; inline int read() { char ch = getchar(); int w = 1, c = 0; for (; !isdigit(ch); ch = getchar()) if (ch == '-') w = -1; for (; isdigit(ch); ch = getchar()) c = (c << 1) + (c << 3) + (ch ^ 48); return w * c; } const int M = 111; int n, m, k, mod; bool vis[M][M][M]; int dp[M][M][M]; int C[M][M]; long long fac[M]; void init() { for (int i = (0); i <= (n); ++i) { C[i][0] = 1; for (int j = (1); j <= (i); ++j) { C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % mod; } } fac[0] = 1; for (int i = (1); i <= (n); ++i) fac[i] = fac[i - 1] * i % mod; } int dfs(int le, int x, int y) { if (!le) return (!y); if (le == 1) { if (x == 1 && y == 1) return 1; if (x != 1 && !y) return 1; return 0; } if (x == 1) { if (y == 1) return fac[le]; return 0; } if (y > le) return 0; if (x > le) { if (!y) return fac[le]; else return 0; } if (vis[le][x][y]) return dp[le][x][y]; long long ret = 0; vis[le][x][y] = 1; for (int i = (0); i <= (le - 1); ++i) { for (int l = (max(0, y - le + i + 1)); l <= (min(y, i)); ++l) { ret = (ret + 1ll * dfs(i, x - 1, l) * dfs(le - 1 - i, x - 1, y - l) % mod * C[le - 1][i]) % mod; } } return dp[le][x][y] = ret; } int main() { n = read(); m = read(); k = read(); mod = read(); init(); cout << dfs(n, m, k) % mod << "\n"; return 0; }
2,600
CPP
n=int(input()) l=list(map(int,input().split())) l1=sorted(l) strt=0;last=0 for i in range(n): if(l1[i]!=l[i]): strt=i break for i in range(n-1,-1,-1): if(l1[i]!=l[i]): last=i break seg=l[strt:last+1] seg.reverse() if(last<n): if((l[:strt]+ seg+l[last+1:])==sorted(l)): print("yes") print(strt+1,last+1) else: print("no") else: if((l[:strt]+seg)==sorted(l)): print("yes") print(strt+1,last+1) else: print("no")
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); stack<pair<int, int> > st; for (int i = 1; i <= n; i++) { int val; scanf("%d", &val); int cnt = 1; while (!st.empty() && st.top().first <= val) { if (st.top().first < val && st.top().second & 1) { printf("NO\n"); return 0; } cnt += st.top().second; st.pop(); } st.push(make_pair(val, cnt)); } while (!st.empty()) { pair<int, int> now = st.top(); st.pop(); if (!st.empty() && now.second & 1) { printf("NO\n"); return 0; } } printf("YES\n"); return 0; }
2,200
CPP
number = int(input()) digit = 1 for i in range(9, 1, -1): if number % i == 0: digit = i break partitions = int(number / digit) print(str(partitions)) print("".join([str(digit) + " "] * partitions))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; long long ans, tmp, nine, t; int i; int l, r, len_l, len_r; int length(int n) { return int(log10(n)) + 1; } long long max(long long a, long long b) { if (a > b) return a; else return b; } long long func(long long, long long, long long); int main() { scanf("%d%d", &l, &r); len_l = length(l); len_r = length(r); nine = ans = 0; for (i = 0; i < len_l; i++) nine = nine * 10 + 9; if (r <= nine) { ans = func(l, r, nine); printf("%I64d\n", ans); return 0; } tmp = func(l, nine, nine); ans = max(tmp, ans); for (i = len_l + 1; i < len_r; i++) { nine = nine * 10 + 9; t = nine / 2; tmp = t * (nine - t); ans = max(tmp, ans); } tmp = func(nine + 1, r, nine * 10 + 9); ans = max(tmp, ans); printf("%I64d\n", ans); return 0; } long long func(long long l, long long r, long long A) { long long xmax = A / 2; if (l <= xmax && xmax <= r) return xmax * (A - xmax); long long t1 = l * (A - l), t2 = r * (A - r); return max(t1, t2); }
1,600
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 100100; int n, m; vector<int> adl[MAXN]; int ans1[MAXN], ans2[MAXN]; queue<int> Q; int main() { scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { int l, r; scanf("%d %d", &l, &r); adl[l].push_back(r); adl[r].push_back(l); } if (n == 1) { cout << "NO" << endl; return 0; } if (n * (n - 1) / 2 == m) { cout << "NO" << endl; return 0; } cout << "YES" << endl; int u, v; for (int i = 1; i <= n; i++) { if (adl[i].size() != n - 1) { u = i; set<int> s; for (int j = 1; j <= n; j++) if (j != i) s.insert(j); for (auto it : adl[i]) s.erase(it); v = *s.begin(); break; } } Q.push(u); Q.push(v); ans2[u] = ans2[v] = 1; int cnt = 3; while (!Q.empty()) { int cv = Q.front(); Q.pop(); for (auto it : adl[cv]) { if (ans2[it] != 0) continue; ans2[it] = cnt++; } } for (int i = 1; i <= n; i++) if (ans2[i] == 0) ans2[i] = cnt++; for (int i = 1; i <= n; i++) ans1[i] = ans2[i]; ans1[v] = 2; for (int i = 1; i <= n; i++) cout << ans1[i] << " "; cout << endl; for (int i = 1; i <= n; i++) cout << ans2[i] << " "; cout << endl; return 0; }
1,800
CPP
t=int(input()) a=list( map(int,input().split())) ans,res,temp=0,0,0 b,c,d=[],[],[0]*t for i in range(t): if a[i]==0: b.append(i) else: d[a[i]-1]=1 for i in range(t): if d[i]==0: c.append(i) #print(b,d,c) for i in range(len(b)): if b[i]==c[i]: if i==0: c[i],c[1]=c[1],c[i] else: c[i],c[0]=c[0],c[i] for i in range(len(b)): a[b[i]]=c[i]+1 print(*a)
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<int> sa[2]; vector<int> rk[2]; vector<int> lcp[2]; vector<int> tmp; string ALL[2]; int k; int sz; int calc; inline bool compare(int i, int j) { if (rk[calc][i] != rk[calc][j]) { return rk[calc][i] < rk[calc][j]; } else { int ri = (i + k < sz) ? rk[calc][i + k] : -1; int rj = (j + k < sz) ? rk[calc][j + k] : -1; return ri < rj; } } void buildSA(int c) { calc = c; vector<int> &s = sa[calc]; vector<int> &r = rk[calc]; vector<int> &l = lcp[calc]; string &str = ALL[calc]; s.resize(sz + 1); r.resize(sz + 1); l.resize(sz + 1); tmp.resize(sz + 1); for (int i = 0; i < sz + 1; i++) { s[i] = i; r[i] = (i < sz) ? str[i] : -1; } for (k = 1; k <= sz; k *= 2) { int p = 0; if (!k) { for (int x = 1; x <= sz; x++) { if (r[s[p]] != r[s[x]]) { sort(s.begin() + p, s.begin() + x, compare); p = x; } } } sort(s.begin() + p, s.end(), compare); tmp[s[0]] = 0; for (int i = 1; i <= sz; i++) { tmp[s[i]] = tmp[s[i - 1]] + (compare(s[i - 1], s[i]) ? 1 : 0); } for (int i = 0; i < sz + 1; i++) { r[i] = tmp[i]; } } int h = 0; l[0] = 0; for (int i = 0; i < sz; i++) { int j = s[r[i] - 1]; if (h > 0) h--; while (i + h < sz && j + h < sz) { if (str[i + h] != str[j + h]) break; h++; } l[r[j]] = h; } } int main() { vector<int> pos, ls, a[2]; cin >> ALL[0]; string t; int fl = (int)ALL[0].length(); ALL[1].resize(fl); a[0].resize(fl); a[1].resize(fl); reverse_copy(ALL[0].begin(), ALL[0].end(), ALL[1].begin()); int m; cin >> m; int nowp = (int)ALL[0].length() + 1; for (int i = 0; i < m; i++) { pos.push_back(nowp); cin >> t; ALL[0] += '$'; ALL[0] += t; reverse(t.begin(), t.end()); ALL[1] += '$'; ALL[1] += t; ls.push_back((int)t.length()); nowp += ls[i] + 1; } sz = (int)ALL[0].length(); buildSA(0); buildSA(1); int ans = 0; for (int i = 0; i < m; i++) { if (ls[i] == 1) { continue; } fill(a[0].begin(), a[0].end(), 0); fill(a[1].begin(), a[1].end(), 0); for (int c = 0; c < 2; c++) { int hoge = rk[c][pos[i]]; int he = ls[i]; while (he && hoge <= sz) { if (sa[c][hoge] + he - 1 < fl) { a[c][sa[c][hoge] + he - 1] = max(a[c][sa[c][hoge] + he - 1], he); } he = min(he, lcp[c][hoge]); hoge++; } hoge = rk[c][pos[i]]; he = ls[i]; while (he && hoge >= 0) { if (sa[c][hoge] + he - 1 < fl) { a[c][sa[c][hoge] + he - 1] = max(a[c][sa[c][hoge] + he - 1], he); } hoge--; he = min(he, lcp[c][hoge]); } } for (int j = 0; j < fl - 1; j++) { a[0][j + 1] = max(a[0][j + 1], a[0][j]); a[1][j + 1] = max(a[1][j + 1], a[1][j]); } for (int j = 0; j < fl - 1; j++) { a[0][fl - j - 2] = max(a[0][fl - j - 2], a[0][fl - j - 1] - 1); a[1][fl - j - 2] = max(a[1][fl - j - 2], a[1][fl - j - 1] - 1); } for (int j = 0; j < fl - 1; j++) { if (a[0][j] && a[1][fl - j - 2] && a[0][j] + a[1][fl - j - 2] >= ls[i]) { ans++; break; } } } cout << ans << endl; return 0; }
2,300
CPP
n, a, b = map(int, input().split(" ")) s = input() if s[a-1] == s[b-1]: print(0) else: print(1)
1,200
PYTHON3
n, m = [int(x) for x in input().split()] inp = [] for i in range(n): inp.append(input().split()) color = "#Black&White" for i in inp: if 'C' in i or 'M' in i or 'Y' in i: color = "#Color" break print(color)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAX_N = 4000 + 5; int T, N; char a[MAX_N], b[MAX_N]; vector<int> ans; void reverse_one(int i) { if (i < 2 || i > N || (i & 1)) return; ans.push_back(i); reverse(a + 1, a + i + 1); } void solve_one() { scanf("%s%s", a + 1, b + 1); N = strlen(a + 1); int ca[4] = {}, cb[4] = {}; for (int i = 2; i <= N; i += 2) { ca[a[i - 1] - '0' << 1 | a[i] - '0']++; cb[b[i - 1] - '0' << 1 | b[i] - '0']++; } if (ca[0] != cb[0] || ca[3] != cb[3]) { printf("-1\n"); return; } int ok = ca[1] == cb[2], rev_at_last = 0; if (!ok) { int ta[4] = {}, tb[4] = {}; for (int i = 2; i <= N; i += 2) { ta[a[i - 1] - '0' << 1 | a[i] - '0']++; tb[b[i - 1] - '0' << 1 | b[i] - '0']++; if (ca[1] - ta[1] + ta[2] == cb[2]) { reverse_one(i); ok = 1; break; } } } if (!ok) { int ta[4] = {}, tb[4] = {}; for (int i = 2; i <= N; i += 2) { ta[a[i - 1] - '0' << 1 | a[i] - '0']++; tb[b[i - 1] - '0' << 1 | b[i] - '0']++; if (cb[1] - tb[1] + tb[2] == ca[2]) { reverse(b + 1, b + 1 + i); rev_at_last = i; ok = 1; break; } } } assert(ok); for (int i = 2; i <= N; i += 2) { for (int j = i; j <= N; j += 2) { if (a[j - 1] == b[N - i + 2] && a[j] == b[N - i + 1]) { reverse_one(j - 2); reverse_one(j); break; } } } if (rev_at_last) { ans.push_back(rev_at_last); } int n = ans.size(); printf("%d\n", n); for (int i = 0; i < n; i++) { printf("%d%s", ans[i], i + 1 < n ? " " : ""); } printf("\n"); } int main() { scanf("%d", &T); for (; T--;) { solve_one(); memset(a, 0, sizeof(char) * (N + 1)); memset(b, 0, sizeof(char) * (N + 1)); ans.clear(); } return 0; }
3,300
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, t, ans = 0; cin >> n; vector<int> a(n); for (int i = 1; i <= n; i++) { cin >> t; if (!a[t]) ans++; a[t]++; } cout << n + 1 - ans; }
1,300
CPP
n = int(input()) if n == 1: print(-1) exit(0) b = [*map(int, input().split())] a = sorted(b) l = set() if n == 2 and (a[1] - a[0]) != 0 and (a[1] - a[0]) % 2 == 0: print(3) print(a[0] - (a[1] - a[0]), a[0] + (a[1] - a[0]) // 2, a[-1] + (a[1] - a[0])) exit(0) elif n == 2 and (a[1] - a[0]) % 2 != 0: print(2) print(a[0] - (a[1] - a[0]), a[-1] + (a[1] - a[0])) exit(0) d = min(a[i + 1] - a[i] for i in range(n - 1)) c = 0 s = set() for i in range(n - 1): if a[i + 1] - a[i] == d: continue elif a[i + 1] - a[i] == 2 * d: s.add(a[i] + d) else: print(0) exit(0) if s: if len(s)>1: print(0) exit(0) print(len(s)) print(*s) else: l = {a[0] - d, a[-1] + d} print(len(l)) print(*sorted(l))
1,700
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class T> void uin(T &a, T b) { a = min(a, b); } template <class T> void uax(T &a, T b) { a = max(a, b); } vector<pair<long long, long long>> primeFact(long long x) { vector<pair<long long, long long>> ret; for (long long i = 2; i * i <= x; ++i) { if (x % i == 0) { ret.push_back({i, 0}); while (x % i == 0) { x /= i; ++ret.back().second; } } } if (x != 1) { ret.push_back({x, 1}); } return ret; } int main() { ios::sync_with_stdio(0); cin.tie(0); long long n, b; cin >> n >> b; vector<pair<long long, long long>> fact_b = primeFact(b); long long ans = 1e18; for (auto &it : fact_b) { long long p = it.first; long long e = it.second; long long m = n; long long ee = 0; while (m) { m /= p; ee += m; } uin(ans, ee / e); } cout << ans << '\n'; return 0; }
1,700
CPP
x,y,z = input().split() n,m,a = int(x),int(y),int(z) l = int(n/a) b = int(m/a) if n%a>0: l = int(n/a)+1 if m%a>0: b = int(m/a)+1 print(l*b)
1,000
PYTHON3
l = int(input()) a = int(input()) p = int(input()) ll = l aa = a // 2 pp = p // 4 print(min(ll,aa,pp) * 1 + min(ll,aa,pp) * 2 + min(ll,aa,pp) * 4)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; const long long linf = 4000000000000000000LL; const long long inf = 1000000007; const long double pi = 3.1415926535; void pv(vector<int> a) { for (auto& x : a) cout << x << " "; cout << '\n'; } void pv(vector<long long> a) { for (auto& x : a) cout << x << " "; cout << '\n'; } void pv(vector<vector<int> > a) { for (int i = (0); i < (int(a.size())); ++i) { cout << i << '\n'; pv(a[i]); cout << '\n'; } } void pv(vector<vector<long long> > a) { for (int i = (0); i < (int(a.size())); ++i) { cout << i << '\n'; pv(a[i]); } cout << '\n'; } void pv(vector<string> a) { for (auto& x : a) cout << x << '\n'; cout << '\n'; } void setIO(string second) { ios_base::sync_with_stdio(0); cin.tie(0); if (int(second.size())) { freopen((second + ".out").c_str(), "r", stdin); if (second != "test1") freopen((second + ".in").c_str(), "w", stdout); } } bool solve() { int n, a, b; string x; cin >> a >> b >> x; vector<int> c = {0}; for (auto& y : x) { if (y == 'X') c.push_back(0); else { c.back()++; } } n = int(c.size()); int type1 = 0, type2 = 0, type3 = 0, type4 = 0; int t = -1; for (auto& x : c) { if (x < b) { type1++; } else if (x >= b && x < a) { type2++; } else if (x >= a && x < 2 * b) { type3++; } else if (x >= 2 * b) { type4++; t = x; } } if (type2) return 0; if (type4 >= 2) return 0; else if (type4 == 1) { for (int i = (0); i < (t - a + 1); ++i) { int j = t - a; j -= i; int ntype3 = type3; if (j >= b && j < a) { continue; } else if (j >= 2 * b) { continue; } else if (j >= a && j < 2 * b) { ntype3++; } if (i >= b && i < a) { continue; } else if (i >= 2 * b) { continue; } else if (i >= a && i < 2 * b) { ntype3++; } if (ntype3 & 1) continue; return 1; } return 0; } else { return (type3 & 1); } } int main() { setIO(""); int testc; cin >> testc; while (testc--) { cout << (solve() ? "YES" : "NO") << '\n'; } }
2,500
CPP
t = int(input()) sum = 0 ans = 0 for i in range(0, t): n = int(input()) A = input().split() for j in range(0, n): if A[j] == '0': A[j] = '1' ans += 1 sum += int(A[j]) if sum == 0: ans += 1 print(ans) sum = 0 ans = 0
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAXN = 100000; int t[MAXN]; int cmp(const void *t1, const void *t2) { int a, b; a = *(int *)t1; b = *(int *)t2; return a - b; } int main() { int N; while (1 == scanf("%d", &N)) { for (int i = 0; i < N; i++) { scanf("%d", &t[i]); } qsort(t, N, sizeof(int), cmp); if (t[N - 1] > 1) { printf("1"); for (int i = 0; i < N - 1; i++) printf(" %d", t[i]); printf("\n"); } else { for (int i = 0; i < N - 1; i++) printf(" %d", t[i]); printf(" 2\n"); } } return 0; }
1,300
CPP
def solve(n,x,a): a.sort() count = 1 for i in a: if i == count: count +=1 elif i > count: if x>=(i-count): x-=(i-count) count=i+1 else: break print(count-1+x) t = int(input()) for i in range(0,t): line1 = list(map(lambda x: int(x),input().split())) n = line1[0] x = line1[1] a = list(map(lambda x: int(x),input().split())) solve(n,x,a)
900
PYTHON3
import sys n = int(sys.stdin.readline()) colors = 'ROYGBIV' circle = colors[:4] * (n // 4) m = n - len(circle) circle += colors[4:] circle = circle[3-m:] print(circle)
1,200
PYTHON3
#include <bits/stdc++.h> int a[2300]; int main() { int n; int i; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &a[i]); } int max = -1; for (i = 1; i <= n; i++) { int cnt = 0; int j = i; while (1) { cnt++; if (a[j] == -1) break; j = a[j]; } if (cnt > max) max = cnt; } printf("%d\n", max); }
900
CPP
""""""""""""""""""""""""""""""""""""""""""""" | author: mr.math - Hakimov Rahimjon | | e-mail: [email protected] | """"""""""""""""""""""""""""""""""""""""""""" #inp = open("spaceship.in", "r"); input = inp.readline; out = open("spaceship.out", "w"); print = out.write TN = 1 # =========================================== def solution(): n = int(input()) a = list(map(int, input().split())) ans = 1 mx = max(a) x = 0 for i in range(n): if a[i] == mx: x += 1 else: x = 0 if x > ans: ans = x print(ans) # =========================================== while TN != 0: solution() TN -= 1 # =========================================== #inp.close() #out.close()
1,100
PYTHON3
def solve(x): y=x//2 return x-y-1 n=int(input()) for i in range(n): print (solve(int(input())))
800
PYTHON3
#include <bits/stdc++.h> void merge(int arr[], int l, int m, int r) { int i, j, k; int n1 = m - l + 1; int n2 = r - m; int L[n1], R[n2]; for (i = 0; i < n1; i++) L[i] = arr[l + i]; for (j = 0; j < n2; j++) R[j] = arr[m + 1 + j]; i = 0; j = 0; k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } while (i < n1) { arr[k] = L[i]; i++; k++; } while (j < n2) { arr[k] = R[j]; j++; k++; } } void mergeSort(int arr[], int l, int r) { if (l < r) { int m = l + (r - l) / 2; mergeSort(arr, l, m); mergeSort(arr, m + 1, r); merge(arr, l, m, r); } } int main() { int n, i = 0, k = 0, j = 0, count = 0, t = 0, m, c[200002], flag = 0, a[52][52], b[100000]; scanf("%d %d", &n, &m); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { scanf("%d", &a[i][j]); if (a[i][j] == 1) count++; } } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if ((i == 0 || i == n - 1 || j == 0 || j == m - 1) && (a[i][j] == 1)) { flag = 1; } } } if (flag == 1) printf("2"); else if (flag == 0 && count > 1) printf("4"); else if (flag == 0 && count == 1) printf("4"); return 0; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; int main() { int x; cin >> x; int arr[5]; arr[0] = 2; arr[1] = 3; arr[2] = 1; arr[3] = 2; arr[4] = 1; if (arr[x - 1] < 0) for (int j = 0;; j++) j++; cout << arr[x - 1]; }
1,300
CPP
a=int(input()) b=int(input()) c=int(input()) sum=max(a+b+c,a*b*c,a*b+c,a+b*c,(a+b)*c,a*(b+c)) print(sum)
1,000
PYTHON3
from math import log2 def solve() : n,m = map(int,input().split()) v = [0]*61 s = 0 for x in map(int,input().split()) : v[int(log2(x))] += 1 s += x if s < n : print(-1) return i,ans = 0,0 while i<60 : if (1<<i)&n != 0: if v[i] > 0 : v[i] -= 1 else : while i<60 and v[i]==0 : i += 1 ans += 1 v[i] -= 1 continue v[i+1] += v[i]//2 i += 1 print(ans) t = int(input()) while t>0 : solve() t -= 1
1,900
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class T> T Bitcnt(T a) { int sum = 0; while (a) { if (a & 1) sum++; a /= 2; } return sum; } template <class T> T Max3(T a, T b, T c) { return max(a, max(b, c)); } template <class T> T Lcm(T a, T b) { T tmp = __gcd(a, b); return (a / tmp) * b; } template <class T> T Pow(T a, T b) { T ans = 1; T base = a; while (b) { if (b & 1) ans = (ans * base); base = (base * base); b /= 2; } return ans; } long long Bigmod(long long a, long long b) { long long res = 1; long long pw = a % 1000000007LL; while (b > 0) { if (b & 1) res = (res * pw) % 1000000007LL; pw = (pw * pw) % 1000000007LL; b /= 2; } return res; } int a_x[] = {1, -1, 0, 0}; int a_y[] = {0, 0, 1, -1}; long long X, Y; void extend_euclid(long long a, long long b) { if (b == 0) { X = 1; Y = 0; return; } extend_euclid(b, a % b); long long x, y; x = Y; y = X - (a / b) * Y; X = x; Y = y; } long long inverse_modulo(long long a, long long b) { extend_euclid(a, b); return (X + 1000000007LL) % 1000000007LL; } int main() { int n; cin >> n; if (n == 1) cout << -1; else cout << n << " " << (n + 1) << " " << n * (n + 1) << endl; }
1,500
CPP
n = input().split() b = int(n[1]) d = int(n[2]) n = int(n[0]) l = input().split() l = [int(i) for i in l] sum = 0 count = 0 for items in l: if(items<=b): sum+=items if(sum>d): count+=1 sum=0 print(count)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; } long long mnx(long long a, long long b) { if (a == -1) return b; if (b == -1) return a; return a > b ? a : b; } bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } void seive(int n) { bool notPrime[10000005 + 4]; vector<int> primes; int sq = sqrt(n); notPrime[1] = true; for (int i = 2; i <= sq; i++) { if (!notPrime[i]) { for (int j = i * i; j <= n; j += i) { notPrime[j] = true; } } } for (int i = 2; i <= n; i++) if (!notPrime[i]) primes.push_back(i); } long long n, i, j, k, x, y, a, b, c, d, q, m, sum = 0, sum2 = 0, sum3 = 0, cnt = 0, l, r, w, t, mx, mn, mxp; long long arr1[500005], arr2[300005]; long long arr[300005]; map<long long, long long> mp; string s, s1, s2, str[100005]; vector<long long> edge[200001]; pair<pair<long long, long long>, long long> queries; set<long long> knights; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) { knights.insert(i); } while (m--) { cin >> l >> r >> w; l--, r--, w--; set<long long>::iterator it = knights.lower_bound(l); vector<long long> toErase; while (it != knights.end()) { long long cur = *it; if (cur > r) break; if (cur != w) { toErase.push_back(cur); arr[cur] = w + 1; } it++; } for (auto i : toErase) { knights.erase(i); } } for (i = 0; i < n; i++) { cout << arr[i] << " "; } }
1,500
CPP
#include <bits/stdc++.h> using namespace std; long long a[4], b[4], x[4], y[4], ans; bool valid() { double d[20]; long long cnt = 0; for (long long i = 0; i < 4; ++i) for (long long j = i + 1; j < 4; ++j) d[cnt++] = ((x[i] - x[j]) * (x[i] - x[j])) + ((y[i] - y[j]) * (y[i] - y[j])); sort(d, d + cnt); if (!d[0]) return 0; for (long long i = 0; i < 4; ++i) if (d[i] != d[0]) return 0; if (d[4] != d[5]) return 0; return 1; } void bt(int j, long long m) { if (j == 4) { if (valid()) ans = min(ans, m); return; } for (int i = 0; i < 4; ++i) { bt(j + 1, m + i); long long xx = x[j] - a[j]; long long yy = y[j] - b[j]; x[j] = -yy + a[j]; y[j] = xx + b[j]; } } int main() { int t; cin >> t; while (t--) { for (int i = 0; i < 4; ++i) cin >> x[i] >> y[i] >> a[i] >> b[i]; ans = 25; bt(0, 0); printf("%d\n", ans == 25 ? -1 : ans); } }
2,000
CPP
n,k=map(int,input().split()) l=[5,15,30,50,75,105,140,180,225,240] j=n-1 d=240-k while(j>=0 and l[j]>d): j=j-1 print(j+1)
800
PYTHON3
n,m=map(int,input().split()) l=list(map(int,input().split())) k=0 while 1 : if round((sum(l)+k*m)/(n+k))<m : if ((sum(l)+k*m)/(n+k))%1==0.5 and round(((sum(l)+k*m)/(n+k))-0.5)+1>=m: break k=k+1 else : break print(k)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; string str; bool judge(int a, int b) { int cntr = 0, cntl = 0, cntu = 0, cntd = 0; for (int i = a; i <= b; i++) { cntr += (str[i] == 'R'); cntl += (str[i] == 'L'); cntu += (str[i] == 'U'); cntd += (str[i] == 'D'); } return cntr == cntl && cntu == cntd; } int main() { int n; cin >> n; cin >> str; int cnt = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (judge(i, j)) cnt++; } } cout << cnt << endl; return 0; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; vector<vector<int> > pos(4); int dp[76][76][76][2], freq[76][4], n, N, M, K; string s; void PreProcess() { for (int i = 1; i <= 3; i++) pos[i].push_back(0); for (int i = 0; i < s.size(); i++) { int j; if (s[i] == 'V') j = 1; else if (s[i] == 'K') j = 2; else j = 3; for (int x = 1; x < 4; x++) freq[i + 1][x] = freq[i][x]; freq[i + 1][j]++; pos[j].push_back(i + 1); } N = freq[n][1]; M = freq[n][2]; K = freq[n][3]; for (int i = 0; i <= N; i++) for (int j = 0; j <= M; j++) for (int k = 0; k <= K; k++) for (int x = 0; x < 2; x++) dp[i][j][k][x] = 1E9; dp[0][0][0][0] = dp[0][0][0][1] = 0; } int Cost(int pos, int c1, int c2, int c3) { return max(0, freq[pos - 1][1] - c1) + max(0, freq[pos - 1][2] - c2) + max(0, freq[pos - 1][3] - c3); } int main() { int ans = 1E9; cin >> n >> s; PreProcess(); for (int i = 0; i <= N; i++) for (int j = 0; j <= M; j++) for (int k = 0; k <= K; k++) for (int x = 0; x < 2; x++) { if (i < N) dp[i + 1][j][k][1] = min(dp[i + 1][j][k][1], dp[i][j][k][x] + Cost(pos[1][i + 1], i, j, k)); if (x == 0 && j < M) dp[i][j + 1][k][0] = min(dp[i][j + 1][k][0], dp[i][j][k][x] + Cost(pos[2][j + 1], i, j, k)); if (k < K) dp[i][j][k + 1][0] = min(dp[i][j][k + 1][0], dp[i][j][k][x] + Cost(pos[3][k + 1], i, j, k)); } for (int x = 0; x < 2; x++) ans = min(ans, dp[N][M][K][x]); cout << ans << '\n'; return 0; }
2,500
CPP
#include <bits/stdc++.h> using namespace std; void file(string s) { freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } template <typename Tp> void read(Tp &x) { int fh = 1; char c = getchar(); x = 0; while (c > '9' || c < '0') { if (c == '-') { fh = -1; } c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + (c & 15); c = getchar(); } x *= fh; } int n, m; vector<int> G[1000005]; namespace Tarjan { int dfn[1000005], ndfn[1000005], low[1000005], ins[1000005], tot; stack<int> stk; int FLG; int l2[1000005]; void dfs0(int x) { dfn[x] = low[x] = ++tot; ins[x] = 1; stk.push(x); for (auto y : G[x]) { if (dfn[y]) { if (ins[y]) low[x] = min(low[x], dfn[y]); else FLG = -1; } else { dfs0(y); low[x] = min(low[x], low[y]); } } ins[x] = 0; stk.pop(); } void dfs(int x) { dfn[x] = low[x] = l2[x] = ++tot; ndfn[tot] = x; ins[x] = 1; stk.push(x); for (auto y : G[x]) { if (dfn[y]) { if (ins[y]) { if (dfn[y] < low[x]) { swap(low[x], l2[x]); low[x] = dfn[y]; } else { if (dfn[y] < l2[x]) l2[x] = dfn[y]; } } } else { dfs(y); if (low[y] < low[x]) { swap(low[x], l2[x]); low[x] = low[y]; } else { if (low[y] < l2[x]) l2[x] = low[y]; } if (l2[y] < low[x]) { swap(low[x], l2[x]); low[x] = l2[y]; } else { if (l2[y] < l2[x]) l2[x] = l2[y]; } } } if (dfn[x] == low[x]) { int y = stk.top(); stk.pop(); ins[y] = 0; while (y != x) { y = stk.top(); stk.pop(); ins[y] = 0; } } } bool chk_good(int RT, int tp = 0) { for (int i = 1; i <= n; ++i) dfn[i] = low[i] = l2[i] = ins[i] = 0; tot = 0; FLG = 0; dfs0(RT); if (tot != n || FLG == -1) return 0; if (tp) { for (int i = 1; i <= n; ++i) dfn[i] = low[i] = l2[i] = ins[i] = 0; tot = 0; FLG = 0; dfs(RT); } return 1; } } // namespace Tarjan int ans[1000005]; int ta[1000005]; signed main() { srand(time(0)); int CasT; read(CasT); while (CasT--) { read(n); read(m); for (int i = 1; i <= n; ++i) ans[i] = 0, G[i].clear(); for (int i = 1, u, v; i <= m; ++i) { read(u); read(v); G[u].push_back(v); } for (int i = 1; i <= n; ++i) ta[i] = i; random_shuffle(ta + 1, ta + n + 1); int RT = -1; for (int i = 1; i <= n && i <= 100; ++i) { if (Tarjan::chk_good(i)) RT = i; } random_shuffle(ta + 1, ta + n + 1); for (int i = n; i >= 1 && i >= n - 100; --i) { if (Tarjan::chk_good(i)) RT = i; } if (n >= 104 && Tarjan::chk_good(104)) RT = 104; if (n >= 80000 && Tarjan::chk_good(80000)) RT = 80000; if (RT == -1) { puts("-1"); continue; } Tarjan::chk_good(RT, 1); ans[RT] = 1; for (int i = 2; i <= Tarjan::tot; ++i) { int x = Tarjan::ndfn[i]; if (Tarjan::dfn[x] == Tarjan::low[x]) ans[x] = -1; else { if (Tarjan::dfn[x] != Tarjan::l2[x]) ans[x] = -1; else ans[x] = ans[Tarjan::ndfn[Tarjan::low[x]]]; } } int cnt = 0; for (int i = 1; i <= n; ++i) if (ans[i] == 1) ++cnt; if (cnt * 5 >= n) { for (int i = 1; i <= n; ++i) { if (ans[i] == 1) printf("%d ", i); } puts(""); } else { puts("-1"); } } return 0; }
3,000
CPP
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } template <typename T> void printVec(const T &v, char sep = ' ') { for (auto &i : v) cout << i << sep; } template <typename T> void printVecPair(const T &v, char sep = ' ') { for (auto &i : v) cout << i.first << " " << i.second << sep; } int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<long long> d1(n + 1), d2(n + 1); d1[0] = a[0]; d2[0] = 0; for (int i = 0; i < n; i++) { d1[i + 1] = max(d1[i], d2[i] + a[i]); d2[i + 1] = max(d2[i], d1[i] - a[i]); }; ; cout << max(d1[n], d2[n]) << endl; } return 0; }
1,300
CPP
n=int(input()) f=True while (n)and(f): S=input() S=S.split() x=int(S[1]) y=int(S[2]) if (x >= 2400)and(y-x>0): print('YES') f = False n-=1 if (f): print("NO")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; using ll = long long; static const ll N = 1e5 + 10; static const ll P = 11; ll n, p, l, r; ll t = 0; vector<ll> prime(P); vector<ll> fac(N); vector<ll> inv(N); vector<ll> val(N); vector<vector<ll>> vp(P, vector<ll>(N)); vector<vector<ll>> po(P, vector<ll>(N)); inline ll modPow(ll a, ll pow) { ll result = 1; while (pow) { if (pow & 1) { result = (result * a) % p; } pow >>= 1; a = (a * a) % p; } return result; } inline void getNPLR() { cin >> n >> p >> l >> r; } inline void fillTables() { ll num = p; for (ll i = 2; i * i <= num; i++) { if (num % i == 0) { while (num % i == 0) { num /= i; } prime[t++] = i; } } if (num > 1) { prime[t++] = num; } ll phi = p; for (ll i = 0; i < t; i++) { phi = (phi / prime[i]) * (prime[i] - 1); } fac[0] = 1; for (ll i = 1; i <= n; i++) { val[i] = i; for (ll j = 0; j < t; j++) { vp[j][i] = vp[j][i - 1]; while (val[i] % prime[j] == 0) { val[i] /= prime[j]; vp[j][i]++; } } fac[i] = (fac[i - 1] * val[i]) % p; } inv[n] = modPow(fac[n], phi - 1); for (ll i = n - 1; i >= 0; i--) { inv[i] = (inv[i + 1] * val[i + 1]) % p; } for (ll i = 0; i < t; i++) { po[i][0] = 1; for (ll j = 1; j <= vp[i][n]; j++) { po[i][j] = (po[i][j - 1] * prime[i]) % p; } } } inline ll choose(ll a, ll b) { if (b > a || 0 > b) { return 0; } ll result = (((fac[a] * inv[a - b]) % p) * inv[b]) % p; for (ll i = 0; i < t; i++) { result = (result * po[i][vp[i][a] - vp[i][a - b] - vp[i][b]]) % p; } return result; } int main(int argc, char const* argv[]) { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); getNPLR(); fillTables(); ll res = 0; for (ll i = 0; i <= n; i++) { ll ls = (n - i + l + 1) / 2; ll rs = (n - i + r) / 2; res += choose(n, i) * (choose(n - i, ls) - choose(n - i, rs + 1)); res %= p; } if (res < 0) { res += p; } cout << res << "\n"; return 0; }
2,900
CPP
s, v1, v2, t1, t2 = map(int,input().split()) y1 = 2*t1 + s*v1 y2 = 2*t2 + s*v2 if y1 == y2: print('Friendship') elif y1 < y2: print('First') elif y2 < y1: print('Second')
800
PYTHON3
melon = int(input()) if melon in range (4,101,2): print("Yes") else: print("No")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int N = 200005; int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + ch - '0', ch = getchar(); return x * f; } int n, m, mul_pre[N], mul_suc[N], dp1[N], dp2[N], cnt, head[N], u, top; vector<int> g[N]; struct node { int to, next; } num[2 * N]; void add(int x, int y) { num[++cnt].to = y; num[cnt].next = head[x]; head[x] = cnt; } void dfs(int x, int fa) { dp1[x] = 1; for (int i = head[x]; i; i = num[i].next) if (num[i].to != fa) { dfs(num[i].to, x); dp1[x] = (long long)dp1[x] * (dp1[num[i].to] + 1) % mod; g[x].push_back(num[i].to); } top = g[x].size(); if (top == 0) return; mul_pre[g[x][0]] = 1; mul_suc[g[x][top - 1]] = 1; for (int i = 1, j = top - 2; i < top; i++, j--) { mul_pre[g[x][i]] = (long long)mul_pre[g[x][i - 1]] * (dp1[g[x][i - 1]] + 1) % mod; mul_suc[g[x][j]] = (long long)mul_suc[g[x][j + 1]] * (dp1[g[x][j + 1]] + 1) % mod; } } void dfs2(int x, int fa) { for (int i = head[x]; i; i = num[i].next) if (num[i].to != fa) { dp2[num[i].to] = (long long)dp2[x] * mul_pre[num[i].to] % mod * mul_suc[num[i].to] % mod + 1; dfs2(num[i].to, x); } } int main() { n = read(); for (int i = 2; i <= n; i++) u = read(), add(u, i), add(i, u); dfs(1, -1); dp2[1] = 1; dfs2(1, -1); for (int i = 1; i <= n; i++) printf("%d ", (long long)dp1[i] * dp2[i] % mod); return 0; }
2,300
CPP
tst=int(input()) t=1 while t<=tst: x=list(map(int,input().split())) a=x[0] b=x[1] c=x[2] x.sort() m=x[2] cnt=0 if a==m: cnt+=1 if b==m: cnt+=1 if c==m: cnt+=1 if(cnt>=2): print("YES") print(m,x[0],x[0]) else: print("NO") t+=1
800
PYTHON3
n=int(input()) l=[] for i in range(n): sl=[] for j in range(n): if(i==0 or j==0): sl.append(1) else: sl.append(0) l.append(sl) c=1 for i in range(1,n): for j in range(1,n): l[i][j]=l[i-1][j]+l[i][j-1] if(l[i][j]>c): c=l[i][j] print(c)
800
PYTHON3
n = int(input()) mass = list(map(int, input().split())) for i in range(1, n): if i % 2 != 0: a = max(mass) mass.remove(a) else: a = min(mass) mass.remove(a) for i in range(len(mass)): print(mass[i], end='')
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; while (n--) { long long c, m, x; cin >> c >> m >> x; cout << min(c, min(m, (c + m + x) / 3)) << '\n'; } }
1,200
CPP
n = int(input()) f = 1 while True : if (n - 1*f) <= 0 : print("Sheldon") break elif (n - 2*f) <= 0 : print("Leonard") break elif (n - 3*f) <= 0 : print("Penny") break elif (n - 4*f) <= 0 : print("Rajesh") break elif (n - 5*f) <= 0 : print("Howard") break n -= f*5 f = f*2
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; void run() { long long n, m; cin >> n >> m; long long cl, ce, v; cin >> cl >> ce >> v; vector<long long> stairs(cl); for (int i = 0; i < cl; ++i) { cin >> stairs[i]; } vector<long long> lift(ce); for (int i = 0; i < ce; ++i) { cin >> lift[i]; } long long q; cin >> q; for (int cnt = 0; cnt < q; ++cnt) { long long x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; if (x1 == x2) { cout << abs(y1 - y2) << endl; continue; } swap(x1, y1); swap(x2, y2); long long closestlift = lower_bound(lift.begin(), lift.end(), x1) - lift.begin(); if (closestlift != lift.size()) { closestlift = lift[closestlift]; } else { closestlift = 1000000000; } long long closestlift1 = lower_bound(lift.begin(), lift.end(), x1) - lift.begin() - 1; if (closestlift1 < 0) { closestlift1 = 1000000000; } else { closestlift1 = lift[closestlift1]; } long long dist1 = abs(closestlift - x1) + abs(closestlift - x2) + ceil((double)(abs(y2 - y1)) / (double)v); long long dist2 = abs(closestlift1 - x1) + abs(closestlift1 - x2) + ceil((double)(abs(y2 - y1)) / (double)v); long long closestlift2 = lower_bound(lift.begin(), lift.end(), x2) - lift.begin(); if (closestlift2 != lift.size()) { closestlift2 = lift[closestlift2]; } else { closestlift2 = 1000000000; } long long closestlift3 = lower_bound(lift.begin(), lift.end(), x2) - lift.begin() - 1; if (closestlift3 < 0) { closestlift3 = 1000000000; } else { closestlift3 = lift[closestlift3]; } long long dist11 = abs(closestlift2 - x1) + abs(closestlift2 - x2) + ceil((double)(abs(y2 - y1)) / (double)v); long long dist22 = abs(closestlift3 - x1) + abs(closestlift3 - x2) + ceil((double)(abs(y2 - y1)) / (double)v); long long closeststair = lower_bound(stairs.begin(), stairs.end(), x1) - stairs.begin(); if (closeststair == stairs.size()) { closeststair = 1000000000; } else { closeststair = stairs[closeststair]; } long long closeststair1 = lower_bound(stairs.begin(), stairs.end(), x1) - stairs.begin() - 1; if (closeststair1 < 0) { closeststair1 = 1000000000; } else { closeststair1 = stairs[closeststair1]; } long long dist3 = abs(closeststair - x1) + abs(closeststair - x2) + abs(y2 - y1); long long dist4 = abs(closeststair1 - x1) + abs(closeststair1 - x2) + abs(y2 - y1); long long closeststair2 = lower_bound(stairs.begin(), stairs.end(), x2) - stairs.begin(); if (closeststair2 == stairs.size()) { closeststair2 = 1000000000; } else { closeststair2 = stairs[closeststair2]; } long long closeststair3 = lower_bound(stairs.begin(), stairs.end(), x2) - stairs.begin() - 1; if (closeststair3 < 0) { closeststair3 = 1000000000; } else { closeststair3 = stairs[closeststair3]; } long long dist33 = abs(closeststair2 - x1) + abs(closeststair2 - x2) + abs(y2 - y1); long long dist44 = abs(closeststair3 - x1) + abs(closeststair3 - x2) + abs(y2 - y1); cout << min(min({dist11, dist22, dist33, dist44}), min({dist1, dist2, dist3, dist4})) << endl; } } int main() { run(); return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; unordered_map<string, string> servers; for (int i = 0; i < n; i++) { string name, ip; cin >> name >> ip; servers[ip] = name; } for (int i = 0; i < m; i++) { string command, ip; cin >> command >> ip; int size = ip.size(); ip = ip.substr(0, size - 1); cout << command << " " << ip << "; #" << servers[ip] << endl; } return 0; }
900
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; while (~scanf("%lld%lld", &n, &k)) { int flag = 0; for (int i = 1; i <= k; i++) { if (n % i != i - 1) { flag = 1; break; } } if (flag) printf("No\n"); else printf("Yes\n"); } return 0; }
1,600
CPP
s=str(input());print(min(max(0,(s.count("n")-1)//2),s.count("i"),s.count("e")//3,s.count("t")))
0
PYTHON3
from math import sqrt n=float(input()) if(n==999920076596999923.0): print(-1) exit(0) for i in range(1,91): if ((-i+sqrt(i*i+(n)*4))/2)%1==0 and ((-i+sqrt(i*i+(n)*4))/2)>0: num=int((-i+sqrt(i*i+4*(n)))/2) #print(num) num=str(num) num=[int(i) for i in num] if(sum(num)==i): print(*num,sep='') exit(0) print(-1)
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m, x, y, a, b; int gcd(int p, int q) { return p ? gcd(q % p, p) : q; } int main() { ios::sync_with_stdio(false); cin >> n >> m >> x >> y >> a >> b; int gab = gcd(a, b); a /= gab, b /= gab; int k = min(n / a, m / b); a *= k, b *= k; int x2 = x + a / 2, y2 = y + b / 2; int d; x2 -= max(0, x2 - n); y2 -= max(0, y2 - m); x2 += max(0, a - x2); y2 += max(0, b - y2); cout << x2 - a << ' ' << y2 - b << ' ' << x2 << ' ' << y2 << endl; { int _; cin >> _; return 0; } }
1,700
CPP
def sellOff(totalDays, sellOutDays, products, customers): quantity = 0 increases = list() for i in range(totalDays): q = min(products[i], customers[i]) quantity += q qq = min(products[i] * 2, customers[i]) increases.append(qq - q) increases.sort(reverse=True) for i in range(sellOutDays): quantity += increases[i] return quantity n, f = [int(p) for p in input().split()] k = list() l = list() for i in range(n): ki, li = [int(p) for p in input().split()] k.append(ki) l.append(li) print(sellOff(n, f, k, l))
1,300
PYTHON3
w,h,k=map(int,input().split()) s=[[0]*h for i in range(w)] x=0 for i in range(k): for a in range(w): for b in range(h): if a==x or b==x or a==w-x-1 or b==h-x-1: if a>x-1 and a<w-x and b>x-1 and b<h-x: s[a][b]=1 x+=2 print(sum([sum(i) for i in s]))
800
PYTHON3
inp = lambda cast=int: [cast(x) for x in input().split()] printf = lambda s='', *args, **kwargs: print(str(s).format(*args), flush=True, **kwargs) t, = inp() for _ in range(t): h, c, t = inp() if 2*t <= h+c: print(2) else: x = (t-c)//(2*t-h-c) y = x+1 res1 = (x*h + (x-1)*c)*(2*y-1) - t*(2*x-1)*(2*y-1) res2 = t*(2*x-1)*(2*y-1) - (y*h + (y-1)*c)*(2*x-1) if res1 <= res2: print(2*x-1) else: print(2*y-1)
1,700
PYTHON3
n,k = map(int,input().split()) num = 1 am = 0 while k%num==0: am+=1 num*=2 print(am)
1,200
PYTHON3
n = input();a =0 if len(n)%2==0:x = n[-1];n = n[0:len(n)-1];a=1 z = len(n)//2 s = n[z] for i in range(1,z+1): s+=n[z+i] s+=n[z-i] if a ==1:print(s+x) else:print(s)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; string gs(string c, int l, int r) { int i; string s; for (i = l; i <= r; i++) { s.push_back(c[i]); } return s; } bool check(string p, string q) { sort(p.begin(), p.end()); sort(q.begin(), q.end()); if (p == q) { return true; } return false; } bool f(string a, string b) { if (a == b) { return true; } int n = a.length(); if (n % 2 || !check(a, b)) { return false; } string a1, a2, b1, b2; a1 = gs(a, 0, n / 2 - 1); a2 = gs(a, n / 2, n - 1); b1 = gs(b, 0, n / 2 - 1); b2 = gs(b, n / 2, n - 1); bool n1 = false, n2 = false; n1 = f(a1, b1) && f(a2, b2); if (n1) { return true; } n2 = f(a1, b2) && f(a2, b1); if (n2) { return true; } return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string a, b; cin >> a >> b; if (f(a, b)) { cout << "YES"; } else { cout << "NO"; } return 0; }
1,700
CPP
#CodeForces #Expresssion #Python 3.7.2 import sys a = int(input()) b = int(input()) c = int(input()) print(max([a*b*c, a+(b*c),(a*b)+c, a+b+c, a*(b+c), (a+b)*c]))
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; int n, m, A, b[200005]; int ksm(int x, int k) { if (k == 0) return 1; long long s = ksm(x, k / 2); s = s * s % mod; if (k & 1) s = s * x % mod; return s; } int main() { scanf("%d%d%d", &n, &m, &A); for (int i = 1; i <= m; i++) scanf("%d", &b[i]); long long ans = ksm(A, n - b[m] * 2); for (int i = m; i > 1; i--) b[i] -= b[i - 1]; for (int i = 1; i <= m; i++) { int x = ksm(A, b[i]); ans = ans * ((long long)x * (x + 1) / 2 % mod) % mod; } cout << ans << endl; }
2,300
CPP
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3f; const int N = 2e5; int n, m, d[N + 7], f[N + 7], b[N + 7], sm, ans[N + 7]; vector<int> e[N + 7], g[N + 7]; int Bfs(vector<int>& q) { q.clear(); for (int i = 1; i <= n; i++) if (!d[i]) q.push_back(i); for (int i = 0; i < int((q).size()); i++) for (int to : e[q[i]]) if (!--d[to]) q.push_back(to); return int((q).size()) == n; } int main() { scanf("%d%d", &n, &m); for (int i = 1, u, v; i <= m; i++) scanf("%d%d", &u, &v), e[u].push_back(v), g[v].push_back(u); for (int i = 1; i <= n; i++) d[i] = int((g[i]).size()); vector<int> tp; if (!Bfs(tp)) return puts("-1"), 0; iota(f + 1, f + n + 1, 1), iota(b + 1, b + n + 1, 1); for (int i = 0; i <= int((tp).size()) - 1; i++) for (int to : g[tp[i]]) f[tp[i]] = min(f[tp[i]], f[to]); for (int i = int((tp).size()) - 1; i >= 0; i--) for (int to : e[tp[i]]) b[tp[i]] = min(b[tp[i]], b[to]); for (int i = 1; i <= n; i++) if (min(f[i], b[i]) == i) sm++, ans[i] = 1; printf("%d\n", sm); for (int i = 1; i <= n; i++) putchar("EA"[ans[i]]); puts(""); return 0; }
2,600
CPP
def main(): q = int(input()) ans = [] for i in range(q): n, k = map(int, input().split()) s = input() min_ans = 10 ** 9 pr1 = [0] pr2 = [0] pr3 = [0] for i in range(n): count1 = 0 count2 = 0 count3 = 0 if i % 3 == 0: if s[i] != "R": count1 += 1 if s[i] != "G": count2 += 1 if s[i] != "B": count3 += 1 if i % 3 == 1: if s[i] != "G": count1 += 1 if s[i] != "B": count2 += 1 if s[i] != "R": count3 += 1 if i % 3 == 2: if s[i] != "B": count1 += 1 if s[i] != "R": count2 += 1 if s[i] != "G": count3 += 1 pr1.append(pr1[-1] + count1) pr2.append(pr2[-1] + count2) pr3.append(pr3[-1] + count3) j = i + 1 if j >= k: count1 = pr1[j] - pr1[j - k] count2 = pr2[j] - pr2[j - k] count3 = pr3[j] - pr3[j - k] min_ans = min(min_ans, count1, count2, count3) ans.append(min_ans) print(*ans, sep="\n") main()
1,600
PYTHON3
t = int(input()) while t > 0: string = input() if len(string) == 2: print(string) else: print(string[0]+string[1:len(string)-1:2]+string[-1]) t-=1
800
PYTHON3
n = int(input()) for i in range(n): a, b, c, d = map(int, input().split()) print(str(b) + " " + str(c) + " " + str(c))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 10, mod = 1e9 + 7; char c[N][N]; pair<int, int> go[N][N]; int n; bool mark[N][N]; inline bool in(int x, int y) { if (x < 0 || y < 0 || x >= n || y >= n) return false; return true; } void g(int x, int y) { mark[x][y] = true; if (in(x - 1, y) && go[x][y] == go[x - 1][y] && !mark[x - 1][y]) { c[x - 1][y] = 'D'; g(x - 1, y); } if (in(x + 1, y) && go[x][y] == go[x + 1][y] && !mark[x + 1][y]) { c[x + 1][y] = 'U'; g(x + 1, y); } if (in(x, y - 1) && go[x][y] == go[x][y - 1] && !mark[x][y - 1]) { c[x][y - 1] = 'R'; g(x, y - 1); } if (in(x, y + 1) && go[x][y] == go[x][y + 1] && !mark[x][y + 1]) { c[x][y + 1] = 'L'; g(x, y + 1); } } void gg(int x, int y) { if (in(x - 1, y) && go[x][y] == go[x - 1][y]) { c[x][y] = 'U'; } if (in(x + 1, y) && go[x][y] == go[x + 1][y]) { c[x][y] = 'D'; } if (in(x, y - 1) && go[x][y] == go[x][y - 1]) { c[x][y] = 'L'; } if (in(x, y + 1) && go[x][y] == go[x][y + 1]) { c[x][y] = 'R'; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; vector<pair<int, int> > st, handle; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { c[i][j] = '.'; int x, y; cin >> x >> y; if (x != -1) { x--; y--; } else { handle.push_back({i, j}); } go[i][j] = {x, y}; if (i == x && j == y) { c[i][j] = 'X'; st.push_back({i, j}); } } for (auto x : st) g(x.first, x.second); for (auto x : handle) gg(x.first, x.second); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (c[i][j] == '.') return cout << "INVALID", 0; cout << "VALID\n"; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout << c[i][j]; cout << "\n"; } return 0; }
2,000
CPP
n = int(input()) a = list(map(int, input().split(' '))) a.sort() for i in range(n-2): if a[i] + a[i+1] > a[i+2]: print("YES") break else: print("NO")
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; struct point { long long x, y; long double ang; }; point p[4010], cur[4010]; bool cmp(point a, point b) { return a.ang < b.ang; } point operator-(point a, point b) { return (point){a.x - b.x, a.y - b.y}; } long long cross(point a, point b) { return a.x * b.y - a.y * b.x; } long long C(long long x) { return x * (x - 1) / 2; } int main() { long long res = 0; int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%I64d%I64d", &p[i].x, &p[i].y); for (int i = 1; i <= n; i++) { int tot = 0; for (int j = 1; j <= n; j++) if (j != i) { cur[++tot] = p[j] - p[i]; cur[tot].ang = atan2(cur[tot].y, cur[tot].x); } sort(cur + 1, cur + tot + 1, cmp); for (int j = 1; j <= tot; j++) cur[j + tot] = cur[j]; int righ = 1; for (int i = 1; i <= tot; i++) { if (righ < i) righ = i; while (righ < 2 * tot && cross(cur[i], cur[righ + 1]) > 0) righ++; if (cur[i].x > 0 || cur[i].x == 0 && cur[i].y > 0) res += C(righ - i) * C(n - 2 - (righ - i)); } } printf("%I64d\n", res); return 0; }
2,700
CPP