solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vl = vector<ll>; using pi = pair<int, int>; const int MX = 200005; const int MOD = (int)(1e9 + 7); namespace output { void pr(int x) { cout << x; } void pr(long x) { cout << x; } void pr(ll x) { cout << x; } void pr(unsigned x) { cout << x; } void pr(unsigned long x) { cout << x; } void pr(unsigned long long x) { cout << x; } void pr(float x) { cout << x; } void pr(double x) { cout << x; } void pr(long double x) { cout << x; } void pr(char x) { cout << x; } void pr(const char* x) { cout << x; } void pr(const string& x) { cout << x; } void pr(bool x) { pr(x ? "true" : "false"); } template <class T1, class T2> void pr(const pair<T1, T2>& x); template <class T> void pr(const T& x); template <class T, class... Ts> void pr(const T& t, const Ts&... ts) { pr(t); pr(ts...); } template <class T1, class T2> void pr(const pair<T1, T2>& x) { pr("{", x.first, ", ", x.second, "}"); } template <class T> void pr(const T& x) { pr("{"); bool fst = 1; for (const auto& a : x) pr(!fst ? ", " : "", a), fst = 0; pr("}"); } void ps() { pr("\n"); } template <class T, class... Ts> void ps(const T& t, const Ts&... ts) { pr(t); if (sizeof...(ts)) pr(" "); ps(ts...); } void pc() { cout << "]" << endl; } template <class T, class... Ts> void pc(const T& t, const Ts&... ts) { pr(t); if (sizeof...(ts)) pr(", "); pc(ts...); } } // namespace output int main() { ios::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; vi a(n); for (int i = 0; i < n; i++) cin >> a[i]; sort(begin(a), end(a)); multiset<int> diffs; set<int> coords; for (int i = 1; i < n; i++) diffs.insert(a[i] - a[i - 1]); for (int i = 0; i < n; i++) coords.insert(a[i]); auto get = [&]() { if (coords.empty()) { return 0; } int fs = *(--coords.end()); int ss = *(coords.begin()); int ans = fs - ss; if ((int)(diffs).size()) { int bdiff = *(--diffs.end()); ans -= bdiff; } return ans; }; cout << get() << '\n'; for (int i = 0; i < q; i++) { int t, x; cin >> t >> x; if (t) { auto prev = coords.upper_bound(x); auto nxt = coords.upper_bound(x); if (prev != coords.begin()) { --prev; diffs.insert(x - (*prev)); if (nxt != coords.end()) { diffs.erase(diffs.find((*nxt) - (*prev))); } } if (nxt != coords.end()) { diffs.insert((*nxt) - x); } coords.insert(x); } else { coords.erase(x); auto prev = coords.upper_bound(x); auto nxt = coords.upper_bound(x); if (prev != coords.begin()) { --prev; diffs.erase(diffs.find(x - (*prev))); if (nxt != coords.end()) { diffs.insert((*nxt) - (*prev)); } } if (nxt != coords.end()) { diffs.erase(diffs.find((*nxt) - x)); } } cout << get() << '\n'; } }
2,100
CPP
#include <bits/stdc++.h> using namespace std; const long double eps = 1e-10; vector<vector<int>> v(200005), opp(200005); vector<int> d(200005, 1000000007); int main() { int n, m; cin >> n >> m; for (int i = 0; i < (int)m; i++) { int x, y; cin >> x >> y; v[x].push_back(y); opp[y].push_back(x); } int q; cin >> q; vector<int> p(q); for (int i = 0; i < (int)q; i++) cin >> p[i]; int start = p[0], end = p[q - 1]; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; pq.push({0, end}); d[end] = 0; while (!pq.empty()) { auto x = pq.top(); pq.pop(); int dist = x.first, u = x.second; for (auto z : opp[u]) { if (dist + 1 >= d[z]) continue; d[z] = dist + 1; pq.push({d[z], z}); } } vector<int> alt(n + 3, 0); for (int i = 1; i <= (int)n; i++) { for (auto z : v[i]) { if (d[z] + 1 == d[i]) alt[i]++; } } int maxx = 0, minn = 0; for (int i = 0; i < (int)q; i++) { if (i == q - 1) break; int z = p[i], next = p[i + 1]; if (d[next] + 1 == d[z]) { if (alt[z] > 1) maxx++; } else { minn++, maxx++; } } cout << minn << " " << maxx; }
1,700
CPP
n,k = map(int,input().split()) a = list(map(int,input().split())) mn = 0 ind = 0 for i in range(k): mn+=a[i] sn = mn for i in range(n-k): sn = sn-a[i]+a[i+k] if(sn<mn): mn = sn ind = i+1 print(ind+1)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; char a[200005] = {'*'}, b[200005] = {'*'}; vector<pair<long long int, long long int> > v; stack<char> st; int main() { long long int i, j, k, nb = 0, x, y, m, l, n, sc = 0, nbround = 1; string s, ch; cin >> s; n = s.size(); cin >> m; for (i = 1; i <= m; i++) { cin >> ch; long long int idx = 0; while (idx < n) { long long int nb1 = 0, nb2 = 0; j = idx; while ((s[j] == ch[0] || s[j] == ch[1]) && j < n) { if (s[j] == ch[0]) nb1++; else nb2++; j++; } nb += min(nb1, nb2); if (j != idx) idx = j; else idx++; } } cout << nb; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; long long int n, M, k, x, h; void purple18() { cin >> n >> h >> M >> k; long long int m[n]; for (long long int i = 0; i < n; i++) { cin >> h; cin >> m[i]; } vector<pair<long long int, long long int> > events; long long int cur = 0; for (long long int i = 0; i < n; i++) { long long int l = (m[i] + 1) % (M / 2); long long int r = (m[i] + k) % (M / 2); if (l > r) cur++; events.push_back({l, -1}); events.push_back({r, +1}); } sort(events.begin(), events.end()); long long int best_cancel = INT_MAX; long long int best_time = 0; for (pair<long long int, long long int> evt : events) { cur -= evt.second; if (cur < best_cancel) { best_cancel = cur; best_time = evt.first; } } cout << best_cancel << " " << best_time << "\n"; for (long long int i = 0; i < n; i++) { long long int l = (m[i] + 1) % (M / 2); long long int r = (m[i] + k - 1) % (M / 2); if ((l < r && best_time >= l && best_time <= r) || (l > r && (best_time >= l || best_time <= r))) { cout << i + 1 << " "; } } cout << "\n"; } signed main() { ios::sync_with_stdio(0); cin.tie(0); long long int t = 1; while (t--) purple18(); }
2,300
CPP
import sys import math from collections import defaultdict from collections import deque from itertools import combinations from itertools import permutations input = lambda : sys.stdin.readline().rstrip() read = lambda : list(map(int, input().split())) def write(*args, sep="\n"): for i in args: sys.stdout.write("{}{}".format(i, sep)) INF = float('inf') MOD = int(1e9 + 7) YES = "YES" NO = "NO" arr = read() d = deque() while 1: flag = False for i in range(4): if arr[i]: if not d: d.append(i) flag = True arr[i] -= 1 break else: if abs(d[0] - i) == 1: d.appendleft(i) flag = True arr[i] -= 1 break if abs(d[-1] - i) == 1: d.append(i) flag = True arr[i] -= 1 break if flag: continue else: break if sum(arr) == 0: print(YES) print(*d, sep=" ") else: print(NO)
1,900
PYTHON3
read = lambda: map(int, input().split()) n, k = read() a = sorted(read()) if k > n: print(-1) else: print(a[-k], a[-k])
900
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N_MAX = 5e5; int n, with[N_MAX + 1], c[2 * N_MAX + 1]; vector<int> G[2 * N_MAX + 1]; void DFS(int u) { for (int v : G[u]) { if (c[v] != -1) continue; c[v] = 1 - c[u]; DFS(v); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; if (n % 2 == 0) { cout << "First\n"; for (int t = 0; t < 2; t++) for (int i = 1; i <= n; i++) cout << i << " "; cout << "\n"; cout.flush(); return 0; } cout << "Second\n"; cout.flush(); for (int i = 0; i < n; i++) with[i] = -1; for (int i = 1; i <= 2 * n; i++) { int a; cin >> a; a--; if (with[a] != -1) { G[i].push_back(with[a]); G[with[a]].push_back(i); } with[a] = i; } for (int i = 1; i <= n; i++) { G[i].push_back(i + n); G[i + n].push_back(i); } for (int i = 1; i <= 2 * n; i++) c[i] = -1; for (int i = 1; i <= 2 * n; i++) { if (c[i] == -1) { c[i] = 0; DFS(i); } } int sum = 0; for (int i = 1; i <= 2 * n; i++) { if (c[i] == 0) { sum += i; if (sum >= 2 * n) sum -= 2 * n; } } int good = (sum % (2 * n) == 0 ? 0 : 1); for (int i = 1; i <= 2 * n; i++) if (c[i] == good) cout << i << " "; cout << "\n"; return 0; }
2,800
CPP
import sys r = sys.stdin.readline for _ in range(int(r())): N, M = map(int, r().split()) L = [] for i in range(2*N): ex = list(map(int, r().split())) if i % 2 == 1: L.append(a+ex) else: a = ex if M % 2 != 0: print("NO") continue flag = 0 for i in L: if i[1] == i[2]: flag = 1 break if flag: print("YES") else: print("NO")
900
PYTHON3
import sys n, a, x, b, y = [int(x)for x in input().split()] #truple while a!=x and b!=y: a+=1 if a== n+1: a = 1 b-=1 if b == 0: b = n if a==b: print('YES') sys.exit() print('NO')
900
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<int> graph[300006]; int visited[300006]; int level[300006]; void bfs(int start) { memset(visited, 0, sizeof(visited)); memset(level, 0, sizeof(level)); queue<int> q; q.push(start); visited[start] = 1; while (!q.empty()) { int current = q.front(); q.pop(); for (int i = 0; i < graph[current].size(); i++) { int next = graph[current][i]; if (!visited[next]) { visited[next] = true; q.push(next); } } } } int main() { int n, m; cin >> n >> m; for (int i = 1; i < n; i++) { int x; cin >> x; graph[i].push_back(x + i); } bfs(1); if (visited[m]) { cout << "YES" << endl; } else cout << "NO" << endl; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; bool vis[1010]; int n; int *arr, cur = 0; bool check() { for (int i = 0; i < n; ++i) if (!vis[i]) return false; return true; } int main() { memset(vis, false, sizeof vis); cin >> n; arr = new int[n]; for (int i = 0; i < n; ++i) { cin >> arr[i]; } int sol = 0, idx = 0; while (true) { for (int i = 0; i < n; ++i) { if (!vis[i] && cur >= arr[i]) { ++cur; vis[i] = true; } } if (check()) break; ++sol; for (int i = n - 1; i >= 0; --i) { if (!vis[i] && cur >= arr[i]) { ++cur; vis[i] = true; } } if (check()) break; ++sol; } cout << sol << endl; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 300005; int n, u, v, x, trie[maxn << 1][26], sz[maxn << 1], tot, cnt[maxn], res; char op[2]; int merge(int a, int b) { if (!a || !b) return a + b; int rt = ++tot; sz[rt] = 1; for (int i = 0; i < 26; ++i) { trie[rt][i] = merge(trie[a][i], trie[b][i]); if (trie[rt][i]) sz[rt] += sz[trie[rt][i]]; } return rt; } void dfs(int u, int dep) { sz[u] = 1; for (int i = 0; i < 26; ++i) if (trie[u][i]) { dfs(trie[u][i], dep + 1); sz[u] += sz[trie[u][i]]; } cnt[dep] += sz[u]; tot = n; int rt = 0; for (int i = 0; i < 26; ++i) rt = merge(rt, trie[u][i]); cnt[dep] -= sz[rt]; } int main() { scanf("%d", &n); for (int i = 0; i < n - 1; ++i) { scanf("%d%d%s", &u, &v, op); trie[u][op[0] - 'a'] = v; } sz[0] = 1; dfs(1, 0); res = 0; for (int i = 1; i < n; ++i) if (cnt[i] > cnt[res]) res = i; printf("%d\n%d\n", sz[1] - cnt[res], res + 1); return 0; }
2,500
CPP
a=[] def solve(i,j): if a[i][j]==0 and a[i+1][j+1]==0 and a[i+1][j]==0 and a[i][j+1]==0: return [] if a[i][j]==1 and a[i+1][j+1]==0 and a[i+1][j]==0 and a[i][j+1]==0: return [(0,0,0,1,1,0),(0,0,0,1,1,1),(0,0,1,0,1,1)] if a[i][j]==0 and a[i+1][j+1]==0 and a[i+1][j]==0 and a[i][j+1]==1: return [(0,0,0,1,1,1),(1,0,0,0,0,1),(1,1,1,0,0,1)] if a[i][j]==0 and a[i+1][j+1]==0 and a[i+1][j]==1 and a[i][j+1]==0: return [(0,0,1,0,1,1),(0,0,0,1,1,0),(0,1,1,0,1,1)] if a[i][j]==0 and a[i+1][j+1]==1 and a[i+1][j]==0 and a[i][j+1]==0: return [(0,1,1,0,1,1),(0,0,0,1,1,1),(0,0,1,0,1,1)] if a[i][j]==1 and a[i+1][j+1]==0 and a[i+1][j]==0 and a[i][j+1]==1: return [(0,1,1,0,1,1),(0,0,1,0,1,1)] if a[i][j]==0 and a[i+1][j+1]==1 and a[i+1][j]==1 and a[i][j+1]==0: return [(0,0,0,1,1,0),(0,0,0,1,1,1)] if a[i][j]==1 and a[i+1][j+1]==0 and a[i+1][j]==1 and a[i][j+1]==0: return [(0,0,0,1,1,1),(0,1,1,0,1,1)] if a[i][j]==0 and a[i+1][j+1]==1 and a[i+1][j]==0 and a[i][j+1]==1: return [(0,0,1,0,1,1),(0,0,0,1,1,0)] if a[i][j]==1 and a[i+1][j+1]==1 and a[i+1][j]==0 and a[i][j+1]==0: return [(0,1,1,0,1,1),(0,0,0,1,1,0)] if a[i][j]==0 and a[i+1][j+1]==0 and a[i+1][j]==1 and a[i][j+1]==1: return [(0,0,1,0,1,1),(0,0,0,1,1,1)] if a[i][j]==1 and a[i+1][j+1]==1 and a[i+1][j]==1 and a[i][j+1]==0: return [(0,0,1,0,1,1)] if a[i][j]==1 and a[i+1][j+1]==1 and a[i+1][j]==0 and a[i][j+1]==1: return [(0,0,1,1,0,1)] if a[i][j]==1 and a[i+1][j+1]==0 and a[i+1][j]==1 and a[i][j+1]==1: return [(0,0,1,0,0,1)] if a[i][j]==0 and a[i+1][j+1]==1 and a[i+1][j]==1 and a[i][j+1]==1: return [(0,1,1,0,1,1)] if a[i][j]==1 and a[i+1][j+1]==1 and a[i+1][j]==1 and a[i][j+1]==1: return [(0,0,0,1,1,1),(0,0,1,0,1,1),(0,0,0,1,1,0),(0,1,1,0,1,1)] for _ in range(int(input())): n,m=map(int,input().split()) a=[] for i in range(n): b=input().strip() b=[int(j) for j in b] a.append(b) res=[] for i in range(0,n-1,2): for j in range(0,m-1,2): # if a[i][j]==0: # continue val=solve(i,j) for tp in val: res.append([tp[0]+i+1,tp[1]+j+1,tp[2]+i+1,tp[3]+j+1,tp[4]+i+1,tp[5]+j+1]) a[tp[0]+i][tp[1]+j]^=1 a[tp[2]+i][tp[3]+j]^=1 a[tp[4]+i][tp[5]+j]^=1 j=m-2 for i in range(0,n-1,2): val=solve(i,m-2) for tp in val: res.append([tp[0]+i+1,tp[1]+j+1,tp[2]+i+1,tp[3]+j+1,tp[4]+i+1,tp[5]+j+1]) a[tp[0]+i][tp[1]+j]^=1 a[tp[2]+i][tp[3]+j]^=1 a[tp[4]+i][tp[5]+j]^=1 i=n-2 for j in range(0,m-1): val=solve(n-2,j) for tp in val: res.append([tp[0]+i+1,tp[1]+j+1,tp[2]+i+1,tp[3]+j+1,tp[4]+i+1,tp[5]+j+1]) a[tp[0]+i][tp[1]+j]^=1 a[tp[2]+i][tp[3]+j]^=1 a[tp[4]+i][tp[5]+j]^=1 print(len(res)) for i in res: print(*i)
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class F, class T> T convert(F a, int p = -1) { stringstream ss; if (p >= 0) ss << fixed << setprecision(p); ss << a; T r; ss >> r; return r; } template <class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template <class T> T sqr(T x) { return x * x; } template <class T> T cube(T x) { return x * x * x; } template <class T> int getbit(T s, int i) { return (s >> i) & 1; } template <class T> T onbit(T s, int i) { return s | (T(1) << i); } template <class T> T offbit(T s, int i) { return s & (~(T(1) << i)); } template <class T> int cntbit(T s) { return s == 0 ? 0 : cntbit(s >> 1) + (s & 1); } const int bfsz = 1 << 16; char bf[bfsz + 5]; int rsz = 0; int ptr = 0; char gc() { if (rsz <= 0) { ptr = 0; rsz = (int)fread(bf, 1, bfsz, stdin); if (rsz <= 0) return EOF; } --rsz; return bf[ptr++]; } void ga(char &c) { c = EOF; while (!isalpha(c)) c = gc(); } int gs(char s[]) { int l = 0; char c = gc(); while (isspace(c)) c = gc(); while (c != EOF && !isspace(c)) { s[l++] = c; c = gc(); } s[l] = '\0'; return l; } template <class T> bool gi(T &v) { v = 0; char c = gc(); while (c != EOF && c != '-' && !isdigit(c)) c = gc(); if (c == EOF) return false; bool neg = c == '-'; if (neg) c = gc(); while (isdigit(c)) { v = v * 10 + c - '0'; c = gc(); } if (neg) v = -v; return true; } const double PI = acos(-1.0); const double eps = 1e-9; const int inf = (int)1e9 + 5; const long long linf = (long long)1e17 + 5; const long long mod = 10003; int n; pair<long double, long double> P[1005]; bool f[10005]; int x; long double dist(pair<long double, long double> P0, pair<long double, long double> P1) { return sqrt(sqr(P0.first - P1.first) + sqr(P0.second - P1.second)); } bool makePolygon() { memset(f, 0, sizeof(f)); P[0] = make_pair(0.0, 0.0); long double g = 0; for (int i = (1); i <= (n); ++i) { while (true) { x = rand() % 500; if (!f[x]) { break; } } f[x] = 1; long double d = 200 + x * 1.0 / 300; if (i != n) { P[i].first = P[i - 1].first + d * cos(g); P[i].second = P[i - 1].second + d * sin(g); } else { P[i].second = 0; d = (P[i].second - P[i - 1].second) / sin(g); P[i].first = P[i - 1].first + d * cos(g); } g += (PI + PI) / n; } vector<long double> V; for (int i = (1); i <= (n); ++i) V.push_back(dist(P[i], P[i % n + 1])); sort((V).begin(), (V).end()); for (int i = 0; i < (((int)(V).size()) - 1); ++i) { if (V[i + 1] - V[i] < 0.0015) return false; } return true; } int main() { srand(time(NULL)); cin >> n; if (n <= 4) { cout << "No solution"; return 0; } while (true) { if (makePolygon()) break; } cout << fixed << setprecision(6); for (int i = (1); i <= (n); ++i) cout << P[i].first << " " << P[i].second << endl; return 0; }
2,300
CPP
'''n=int(input())to for i in range(n): s=input() l=len(s) if l>10: s=s[0]+str(l-2)+s[-1] print(s) else: print(s) ''' '''nm=input().split() n=int(nm[0]) m=int(nm[1]) l=list(map(int,input().split())) maxi=l[m-1] c=0 for i in l: if i>=maxi and i>0: c+=1 print(c) ''' '''s=input() s=s.lower() l=list(s) s1='' for i in range(len(l)): a=s[i] if a in "aeiouy": l.remove(a) else: s1+='.'+str(a) print(s1) ''' ''' nm=input().split() n=int(nm[0]) m=int(nm[1]) print((n*m)//2) ''' ''' t=int(input()) x=0 for i in range(t): n=input() if n[1]=='+': x+=1 else: x-=1 print(x) ''' ''' s1=input().lower() s2=input().lower() if len(s1)==len(s2): cs1=0 cs2=0 for i in range(len(s1)): if ord(s1[i])==ord(s2[i]): pass elif ord(s1[i])>ord(s2[i]): cs1=ord(s1[i])-ord(s2[i]) elif ord(s1[i])<ord(s2[i]): cs2=ord(s2[i])-ord(s1[i]) if cs1<cs2: print(-1) elif cs1>cs2: print(1) else: print(0) ''' s1=input().lower() s2=input().lower() if len(s1)==len(s2): if s1>s2: print(1) elif s1<s2: print(-1) elif s1==s2: print(0)
800
PYTHON3
def test_case(): a, b = map(int, input().split()) if a < b: j = (max(2 * a, b)) else: j = (max(2 * b, a)) print(j * j) t = int(input()) for i in range(t): test_case()
800
PYTHON3
#include <bits/stdc++.h> using namespace std; using namespace std; bool solve(int); int main() { int test = 0; while (solve(test++)) ; return 0; } const int N = 100 * 1000 + 5; const int INF = int(1e9); int n, b, m; pair<int, int> a[N]; struct edge { int to, f, c, rev; }; const int V = 1000 * 1000 + 3; vector<edge> g[V]; int lev[V]; int head, tail, q[V]; inline bool bfs(int s, int t) { memset(lev, 63, sizeof(lev)); head = tail = 0; lev[s] = 0; q[head++] = s; while (head != tail) { int v = q[tail++]; for (int i = 0; i < int(int(g[v].size())); ++i) if (g[v][i].f < g[v][i].c && lev[g[v][i].to] > lev[v] + 1) { lev[g[v][i].to] = lev[v] + 1; q[head++] = g[v][i].to; } } return lev[t] < INF / 2; } inline void add(int from, int to, int c) { edge f = {to, 0, c, int(g[to].size())}; edge b = {from, 0, 0, int(g[from].size())}; g[from].push_back(f); g[to].push_back(b); } int ptr[V]; int dfs(int v, int t, int f) { if (v == t) return f; for (; ptr[v] < int(g[v].size()); ptr[v]++) { edge& e = g[v][ptr[v]]; if (e.f == e.c || lev[e.to] != lev[v] + 1) continue; int df = dfs(e.to, t, min(f, e.c - e.f)); if (df > 0) { e.f += df; g[e.to][e.rev].f -= df; return df; } } return 0; } inline int dinic(int s, int t) { int ans = 0; while (bfs(s, t)) { memset(ptr, 0, sizeof(ptr)); for (int f; (f = dfs(s, t, INF)) != 0; ans += f) ; } return ans; } int cnt5[N][5]; int calc(int r, int rem) { return cnt5[r][rem]; } bool solve(int) { if (!(cin >> m >> b >> n)) return false; for (int i = 0; i < int(n); ++i) { scanf("%d %d", &a[i].first, &a[i].second); a[i].first++; } a[n++] = pair<int, int>(b + 1, m); a[n++] = pair<int, int>(1, 0); int cv = 5; int S = cv++; int T = cv++; for (int i = 0; i < int(5); ++i) add(i, T, m / 5); for (int i = 0; i < int(N - 1); ++i) { for (int j = 0; j < int(5); ++j) cnt5[i + 1][j] = cnt5[i][j]; cnt5[i + 1][(i + 1) % 5]++; } sort(a, a + n); for (int i = 0; i < int(n - 1); ++i) { int lf = a[i].first; int rg = a[i + 1].first; int cnt = a[i + 1].second - a[i].second; if (rg - lf < cnt || cnt < 0) { puts("unfair"); return true; } if (cnt == 0) continue; int v[5]; for (int i = 0; i < int(5); ++i) { v[i] = calc(rg - 1, i) - calc(lf - 1, i); add(cv, i, v[i]); } add(S, cv, cnt); cv++; } puts(dinic(S, T) == m ? "fair" : "unfair"); return true; }
2,500
CPP
#a module for any prime implication import random def primesbelow(N): # http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188 #""" Input N>=6, Returns a list of primes, 2 <= p < N """ correction = N % 6 > 1 N = {0:N, 1:N-1, 2:N+4, 3:N+3, 4:N+2, 5:N+1}[N%6] sieve = [True] * (N // 3) sieve[0] = False for i in range(int(N ** .5) // 3 + 1): if sieve[i]: k = (3 * i + 1) | 1 sieve[k*k // 3::2*k] = [False] * ((N//6 - (k*k)//6 - 1)//k + 1) sieve[(k*k + 4*k - 2*k*(i%2)) // 3::2*k] = [False] * ((N // 6 - (k*k + 4*k - 2*k*(i%2))//6 - 1) // k + 1) return [2, 3] + [(3 * i + 1) | 1 for i in range(1, N//3 - correction) if sieve[i]] smallprimeset = set(primesbelow(100000)) _smallprimeset = 100000 def isprime(n, precision=7): # http://en.wikipedia.org/wiki/Miller-Rabin_primality_test#Algorithm_and_running_time if n < 1: raise ValueError("Out of bounds, first argument must be > 0") elif n <= 3: return n >= 2 elif n % 2 == 0: return False elif n < _smallprimeset: return n in smallprimeset d = n - 1 s = 0 while d % 2 == 0: d //= 2 s += 1 for repeat in range(precision): a = random.randrange(2, n - 2) x = pow(a, d, n) if x == 1 or x == n - 1: continue for r in range(s - 1): x = pow(x, 2, n) if x == 1: return False if x == n - 1: break else: return False return True # https://comeoncodeon.wordpress.com/2010/09/18/pollard-rho-brent-integer-factorization/ def pollard_brent(n): if n % 2 == 0: return 2 if n % 3 == 0: return 3 y, c, m = random.randint(1, n-1), random.randint(1, n-1), random.randint(1, n-1) g, r, q = 1, 1, 1 while g == 1: x = y for i in range(r): y = (pow(y, 2, n) + c) % n k = 0 while k < r and g==1: ys = y for i in range(min(m, r-k)): y = (pow(y, 2, n) + c) % n q = q * abs(x-y) % n g = gcd(q, n) k += m r *= 2 if g == n: while True: ys = (pow(ys, 2, n) + c) % n g = gcd(abs(x - ys), n) if g > 1: break return g smallprimes = primesbelow(1000) # might seem low, but 1000*1000 = 1000000, so this will fully factor every composite < 1000000 def primefactors(n, sort=False): factors = [] for checker in smallprimes: while n % checker == 0: factors.append(checker) n //= checker if checker > n: break if n < 2: return factors while n > 1: if isprime(n): factors.append(n) break factor = pollard_brent(n) # trial division did not fully factor, switch to pollard-brent factors.extend(primefactors(factor)) # recurse to factor the not necessarily prime factor returned by pollard-brent n //= factor if sort: factors.sort() return factors def factorization(n): factors = {} for p1 in primefactors(n): try: factors[p1] += 1 except KeyError: factors[p1] = 1 return factors totients = {} def totient(n): if n == 0: return 1 try: return totients[n] except KeyError: pass tot = 1 for p, exp in factorization(n).items(): tot *= (p - 1) * p ** (exp - 1) totients[n] = tot return tot def gcd(a, b): if a == b: return a while b > 0: a, b = b, a % b return a def solve(): t=int(input()) for _ in range(t): a,m=(int(i) for i in input().strip().split(" ")) k=gcd(a,m) print(totient(m//k)) solve()
1,800
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<long long> lucky; long long n, k, x, ans; long long fact[30]; long long ahmad[30]; bool vis[30]; bool ok; map<long long, long long> lu; void f(long long x, int i) { if (i == 10) return; lucky.push_back(x * 10 + 4); f(x * 10 + 4, i + 1); lu[x * 10 + 4] = 1; lucky.push_back(x * 10 + 7); f(x * 10 + 7, i + 1); lu[x * 10 + 7] = 1; } void fa() { fact[0] = 1; for (long long i = 1; i <= 14; i++) fact[i] = i * fact[i - 1]; } void get(long long s) { for (long long i = 1; i <= s; i++) { for (long long j = 1; j <= s; j++) { if (k <= fact[s - i] && !vis[j]) { vis[j] = 1; ahmad[i] = j; break; } else if (!vis[j]) k -= fact[s - i]; } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; fa(); if (n < 15 && fact[n] < k) { cout << -1; return 0; } f(0, 0); sort(lucky.begin(), lucky.end()); if (n < 14) { get(n); for (int i = 1; i <= 13; i++) ans += (lu[ahmad[i] + x] && lu[i + x]); cout << ans; return 0; } x = n - 12; get(13); for (long long i = 0; i < lucky.size() && lucky[i] < x; i++) ans++; for (int i = 1; i <= 13; i++) ans += (lu[ahmad[i] + x - 1] && lu[i + x - 1]); cout << ans; return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; string s; long long int cnt[500005]; double avg = 0.0; bool check(char c) { if (c == 'I' || c == 'E' || c == 'A' || c == 'O' || c == 'U' || c == 'Y') return true; return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> s; int n = int((s).length()); s = "#" + s; for (int i = 1; i <= n; i++) { cnt[i] = cnt[i - 1]; if (check(s[i])) cnt[i]++; } long long int cur = cnt[n]; avg = 0.0; for (int m = int(1); m <= int(n); ++m) { avg += cur / (m + 0.0); if (m > n - m + 1) { cur -= cnt[m] - cnt[n - m]; } else { cur += cnt[n - m] - cnt[m]; } } printf("%.9f\n", avg); return 0; }
2,000
CPP
n, m = map(int, input().split()) s = 1 c = n - 1 arr = [0] * n i = 0 while i <= c: r = 0 j = s while j <= n and r < m: if j < n: r += 2 ** (n - j - 1) j += 1 #print(s, j, r, m) if j > s and j != n + 1: r -= 2 ** (n - j) m -= r j -= 1 arr[i] = j while s < j: arr[c] = s c -= 1 s += 1 s += 1 i += 1 for i in arr: print(i, end = ' ')
1,800
PYTHON3
t=int(input()) for i in range(t): n,m=map(int,input().split()) flag=0 for i in range(n): a,b=map(int,input().split()) c,d=map(int,input().split()) if b==c: flag=1 if m%2: print('NO') elif flag==1: print('YES') else: print('NO')
900
PYTHON3
n = int(input()) dct = {'purple': 'Power', 'green': 'Time', 'blue': 'Space', 'orange': 'Soul', 'red': 'Reality', 'yellow': 'Mind'} colors_has = [] for _ in range(n): colors_has.append(input()) print (6-n) doesnot_have = list(set(dct.keys()) - set(colors_has)) for i in range(6-n): print (dct[doesnot_have[i]])
800
PYTHON3
import math def isPerfectSquare(x): #if x >= 0, if(x >= 0): sr = math.sqrt(x) #return boolean T/F return ((sr*sr) == x) return false for _ in range(int(input())): a1,b1=sorted(list(map(int,input().split()))) a2,b2=sorted(list(map(int,input().split()))) d=a2*b2 e=a1*b1 if a1+a2 ==b1==b2: print("Yes") else: print("No")
900
PYTHON3
denominations = [100, 20, 10, 5, 1] amount = int(input()) count = 0 for denom in denominations: count += amount // denom amount %= denom print(count)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; long long GCD(long long u, long long v) { while (v != 0) { long long r = u % v; u = v; v = r; } return u; } int n; vector<long long> x; vector<vector<int>> e; vector<vector<pair<long long, int>>> gcdseq; long long ans = 0; void dfs(int node, int p, int dep) { long long num = x[node]; ans += num; for (auto gcp : gcdseq[p]) { long long ngcd = GCD(num, gcp.first); if (gcdseq[node].empty() || gcdseq[node].back().first != ngcd) gcdseq[node].push_back({ngcd, gcp.second}); } if (gcdseq[node].empty() || gcdseq[node].back().first != num) gcdseq[node].push_back({num, dep}); for (int i = 0; i + 1 < (int)gcdseq[node].size(); ++i) { ans += gcdseq[node][i].first * (gcdseq[node][i + 1].second - gcdseq[node][i].second); ans %= MOD; } ans += gcdseq[node].back().first * (dep - gcdseq[node].back().second); ans %= MOD; for (int ch : e[node]) { if (ch == p) continue; dfs(ch, node, dep + 1); } } int main() { ios::sync_with_stdio(false); cin >> n; x.resize(n + 1); e.resize(n + 1); gcdseq.resize(n + 1); for (int i = 1; i <= n; ++i) cin >> x[i]; for (int i = 1; i < n; ++i) { int x, y; cin >> x >> y; e[x].push_back(y); e[y].push_back(x); } dfs(1, 0, 0); cout << ans << endl; return 0; }
2,000
CPP
n = int(input()) arr, curr, res, i = [], 1, 0, 0 while i < n: s = input() if s[0] == 'a': arr.append(int(s[4::])) continue if len(arr) == 0: curr += 1 elif arr[-1] == curr: curr += 1 arr.pop() else: res += 1 arr = [] curr += 1 i += 1 print(res)
1,500
PYTHON3
n = int(input()) a = list(map(int, input().split())) k = 1 s = 0 for i in range(1, n): if a[i] == a[i - 1]: k += 1 else: s += (1 + k) * k // 2 k = 1 s += (1 + k) * k // 2 print(s)
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int x, y; cin >> x >> y; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int cx = 0, cy = 0; int moves = 0; for (;;) { if (((x >= cx && x <= cx + (moves / 2 + 1) * dx[moves % 4]) || (x <= cx && x >= cx + (moves / 2 + 1) * dx[moves % 4])) && ((y >= cy && y <= cy + (moves / 2 + 1) * dy[moves % 4]) || (y <= cy && y >= cy + (moves / 2 + 1) * dy[moves % 4]))) break; cx += (moves / 2 + 1) * dx[moves % 4]; cy += (moves / 2 + 1) * dy[moves % 4]; moves++; } cout << moves << endl; return 0; }
1,400
CPP
#include <bits/stdc++.h> int n, t, ans; bool d[100024]; int main() { scanf("%d", &n); memset(d, 0, sizeof(d)); for (int i = 0; i < n - 1; i++) { scanf("%d", &t); d[t] = 1; } ans = 0; for (int i = 1; i <= n; i++) { if (d[i] == 0) { ans = i; break; } } printf("%d", ans); return 0; }
800
CPP
from bisect import * from collections import * from itertools import * import functools import sys import math from decimal import * from copy import * from heapq import * from fractions import * getcontext().prec = 30 MAX = sys.maxsize MAXN = 300010 MOD = 10**9+7 spf = [i for i in range(MAXN)] spf[0]=spf[1] = -1 def sieve(): for i in range(2,MAXN,2): spf[i] = 2 for i in range(3,int(MAXN**0.5)+1): if spf[i]==i: for j in range(i*i,MAXN,i): if spf[j]==j: spf[j]=i def fib(n,m): if n == 0: return [0, 1] else: a, b = fib(n // 2) c = ((a%m) * ((b%m) * 2 - (a%m)))%m d = ((a%m) * (a%m))%m + ((b)%m * (b)%m)%m if n % 2 == 0: return [c, d] else: return [d, c + d] def charIN(x= ' '): return(sys.stdin.readline().strip().split(x)) def arrIN(x = ' '): return list(map(int,sys.stdin.readline().strip().split(x))) def ncr(n,r): num=den=1 for i in range(r): num = (num*(n-i))%MOD den = (den*(i+1))%MOD return (num*(pow(den,MOD-2,MOD)))%MOD def flush(): return sys.stdout.flush() '''*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*''' for _ in range(int(input())): n = int(input()) x = [int(i) for i in input()] a = [0]*n b = [0]*n z = -1 for i in range(n): if x[i]==2: a[i]=b[i]=1 elif x[i]==1: z = i break else: a[i]=b[i]=0 if z==-1: print(''.join(map(str,a))) print(''.join(map(str,b))) else: a[z]=1 b[z]=0 for i in range(z+1,n): if x[i]==2: a[i]=0 b[i] = 2 elif x[i]==1: a[i]=0 b[i]=1 else: a[i]=b[i]=0 print(''.join(map(str,a))) print(''.join(map(str,b)))
1,200
PYTHON3
#include <bits/stdc++.h> int main() { int x, i, j, t, s = 0, c, k, count = 0; scanf("%d", &c); for (k = 0; k < c; k++) { scanf("%d", &x); int a[x]; for (i = 0; i < x; i++) { scanf("%d", &a[i]); } for (i = 0; i < x - 1; i++) { for (j = i + 1; j < x; j++) { if (a[i] < a[j]) { t = a[i]; a[i] = a[j]; a[j] = t; } } } for (i = 0; i < x; i++) { if (a[i] <= 2048) { s = s + a[i]; if (s == 2048) { count++; } } } if (count == 0) { printf("NO\n"); } else { printf("YES\n"); } count = 0; s = 0; } }
1,000
CPP
#include <bits/stdc++.h> using namespace std; long long N, a, b, c, d; bool check(long long x) { return x >= 1 && x <= N; } int main() { cin >> N >> a >> b >> c >> d; long long res = 0; for (int p = 1; p <= N; ++p) { long long x = p + b - c; long long y = p + a - d; long long z = p + (a + b) - (c + d); if (check(x) && check(y) && check(z)) res += N; } cout << res << endl; return 0; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; int n; int main(void) { scanf("%d", &n); int i, x; int a1, a2, a3, a4, a5, a6; a1 = a2 = a3 = a4 = a5 = a6 = 0; for (i = 1; i <= n; i++) { scanf("%d", &x); if (x == 4) a1++; else if (x == 8 && a2 < a1) a2++; else if (x == 15 && a3 < a2) a3++; else if (x == 16 && a4 < a3) a4++; else if (x == 23 && a5 < a4) a5++; else if (x == 42 && a6 < a5) a6++; } printf("%d\n", n - a6 * 6); return 0; }
1,300
CPP
t,n=list(map(int,input().split())) for i in range(n): if t%10==0: t=t//10 else: t=t-1 print(t)
800
PYTHON3
#include <bits/stdc++.h> int count_vk(const std::string &s) { int n = s.size(); int res = 0; for (int i = 0; i + 1 < n; ++i) res += (s[i] == 'V' && s[i + 1] == 'K'); return res; } int solve(const std::string &s) { int cnt = count_vk(s); if (s.find("VVV") != std::string::npos) return cnt + 1; if (s.find("KKK") != std::string::npos) return cnt + 1; if (s.size() > 1 && (s.substr(0, 2) == "KK" || s.substr(s.size() - 2, 2) == "VV")) return cnt + 1; return cnt; } int main() { std::string s; std::cin >> s; std::cout << solve(s) << '\n'; }
1,100
CPP
n=int(input()) for i in range(n): k=int(input()) a=[int(i) for i in input().split()] for j in range(k): if (max(a)-a[j])%2==0: continue else: print('NO') break else: print('YES')
900
PYTHON3
#include <bits/stdc++.h> using namespace std; template <int M> struct static_mint { static_assert(0 < M, "Module must be positive"); int val; static_mint() : val() {} static_mint(long long x) : val(x % M) { if (val < 0) val += M; } static_mint pow(long long n) const { static_mint ans = 1, x(*this); while (n) { if (n & 1) ans *= x; x *= x; n /= 2; } return ans; } static_mint inv() const { return pow(M - 2); } friend static_mint pow(const static_mint &m, long long n) { return m.pow(n); } friend static_mint inv(const static_mint &m) { return m.inv(); } static_mint operator+() const { static_mint m; m.val = val; return m; } static_mint operator-() const { static_mint m; m.val = M - val; return m; } static_mint &operator+=(const static_mint &m) { if ((val += m.val) >= M) val -= M; return *this; } static_mint &operator-=(const static_mint &m) { if ((val -= m.val) < 0) val += M; return *this; } static_mint &operator*=(const static_mint &m) { val = (long long)val * m.val % M; return *this; } static_mint &operator/=(const static_mint &m) { val = (long long)val * m.inv().val % M; return *this; } friend static_mint operator+(const static_mint &lhs, const static_mint &rhs) { return static_mint(lhs) += rhs; } friend static_mint operator-(const static_mint &lhs, const static_mint &rhs) { return static_mint(lhs) -= rhs; } friend static_mint operator*(const static_mint &lhs, const static_mint &rhs) { return static_mint(lhs) *= rhs; } friend static_mint operator/(const static_mint &lhs, const static_mint &rhs) { return static_mint(lhs) /= rhs; } friend bool operator==(const static_mint &lhs, const static_mint &rhs) { return lhs.val == rhs.val; } friend bool operator!=(const static_mint &lhs, const static_mint &rhs) { return lhs.val != rhs.val; } static_mint &operator++() { return *this += 1; } static_mint &operator--() { return *this -= 1; } static_mint operator++(int) { static_mint result(*this); *this += 1; return result; } static_mint operator--(int) { static_mint result(*this); *this -= 1; return result; } template <typename T> explicit operator T() const { return T(val); } friend std::ostream &operator<<(std::ostream &os, const static_mint &m) { return os << m.val; } friend std::istream &operator>>(std::istream &is, static_mint &m) { long long x; is >> x; m = x; return is; } }; template <typename> struct is_mint : public std::false_type {}; template <int M> struct is_mint<static_mint<M>> : public std::true_type {}; template <typename T> struct matrix : std::vector<std::vector<T>> { int n, m; matrix() : n(), m() {} matrix(int n, int m, const T val = T()) : n(n), m(m), std::vector<std::vector<T>>(n, std::vector<T>(m, val)) {} matrix(std::initializer_list<std::vector<T>> l) : std::vector<std::vector<T>>(l) { n = l.size(); if (l.size()) { m = l.begin()->size(); } } matrix &operator+=(const matrix &mat) { assert(n == mat.n && m == mat.m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { (*this)[i][j] += mat[i][j]; } } return *this; } matrix &operator-=(const matrix &mat) { assert(n == mat.n && m == mat.m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { (*this)[i][j] -= mat[i][j]; } } return *this; } matrix &operator*=(const matrix &mat) { assert(m == mat.n); matrix res(n, mat.m); for (int i = 0; i < n; i++) { for (int j = 0; j < mat.m; j++) { for (int k = 0; k < m; k++) { res[i][j] += (*this)[i][k] * mat[k][j]; } } } this->swap(res); return *this; } matrix &operator*=(const T &val) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { (*this)[i][j] *= val; } } return *this; } friend matrix operator+(matrix a, const matrix &b) { a += b; return a; } friend matrix operator-(matrix a, const matrix &b) { a -= b; return a; } friend matrix operator*(matrix a, const matrix &b) { a *= b; return a; } friend matrix operator*(matrix a, const T &val) { a *= val; return a; } friend matrix operator*(const T &val, matrix a) { a *= val; return a; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); using mint = static_mint<1000000007>; int n, x; cin >> n >> x; vector<int> cnt(101); for (int i = 0, d; i < n; i++) { cin >> d, cnt[d] += 1; } matrix<mint> m(101, 101); m[0][0] = 1 + cnt[1]; for (int i = 1; i < 100; i++) { m[i][0] = cnt[i + 1] - cnt[i]; } m[100][0] = -cnt[100]; for (int j = 1; j < 101; j++) { m[j - 1][j] = 1; } matrix<mint> ans(101, 101); for (int i = 0; i < 101; i++) ans[0][0] = 1; for (; x; x /= 2) { if (x & 1) ans *= m; m *= m; } cout << ans[0][0] << '\n'; return 0; }
2,200
CPP
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); char s[n + 1]; scanf("%s", &s); int i, x = 0, X = 0, m; for (i = 0; i < n; i++) { if (s[i] == 'X') X++; if (s[i] == 'x') x++; } if (X >= x) { m = X - (n / 2); printf("%d\n", m); for (i = 0; i < n; i++) { if (m == 0) break; if (s[i] == 'X') { s[i] = 'x'; m--; } } printf("%s", s); } else { m = x - (n / 2); printf("%d\n", m); for (i = 0; i < n; i++) { if (m == 0) break; if (s[i] == 'x') { s[i] = 'X'; m--; } } printf("%s", s); } }
900
CPP
#!/usr/bin/env python #-*-coding:utf8-*- N = int(input()) # print( N ) l = str( input( ) ) # print( l ) ls = [int(x) - 1 for x in l.split() ] def love( x, times = 3 ): a = ls[x] if times == 1: return a else: return love( a, times = times - 1 ) for i in range( N ): if love( i ) == i: print( "YES" ) exit() print( "NO")
800
PYTHON3
# a, b = list(map(int, input().split())) q = int(input()) for i in range(0, q): n = int(input()) arr = list(map(int, input().split())) r = 0 for i in range(0, n): if i <= arr[i]: r = i else: break l = 0 for i in range(0, n): if i <= arr[n-i-1]: l = n-i-1 else: break if (l <= r): print('Yes') else: print('No') # 1 # 7 # 0 5 2 3 5 1 0
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long int INF = 1e18; const long long int inf = 1e9; const long long int MOD = 1e9 + 7; const long long int nax = 1000000 + 10; long long int n, m, k; long long int arr[nax], goreal[nax]; long long int graph[5001][5001]; long long int p[nax], s[nax]; long long int findi(long long int a) { long long int a1 = a; while (p[a] != a) a = p[a]; return p[a1] = a; } bool same(long long int a, long long int b) { return findi(a) == findi(b); } void unioni(long long int a, long long int b) { a = findi(a); b = findi(b); if (s[a] < s[b]) swap(a, b); p[b] = a; s[a] += s[b]; } void init(long long int n) { for (long long int i = 1; i <= nax - 1; i++) s[i] = 1, p[i] = i; for (long long int i = 1; i <= k; i++) { for (long long int j = 1; j <= k; j++) graph[i][j] = INF; } } int main() { ios::sync_with_stdio(0); cin >> n >> m >> k; init(n); for (long long int i = 1; i <= k; i++) cin >> arr[i]; long long int sum = arr[1]; long long int sumprev = 0; for (long long int i = 0; i < k; i++) { for (long long int j = sumprev + 1; j <= sum; j++) goreal[j] = i + 1; if (i == k - 1) break; sumprev = sum; sum += arr[i + 2]; } while (m--) { long long int u, v, w; cin >> u >> v >> w; if (w == 0) { if (same(u, v) == false) unioni(u, v); } if (goreal[u] != goreal[v]) { graph[goreal[u]][goreal[v]] = min(graph[goreal[u]][goreal[v]], w); graph[goreal[v]][goreal[u]] = min(graph[goreal[v]][goreal[u]], w); } } long long int flag = 0; sum = arr[1]; sumprev = 0; for (long long int i = 0; i < k; i++) { long long int tocheck = sumprev + 1; for (long long int j = tocheck + 1; j <= sum; j++) if (same(tocheck, j) == false) flag = 1; if (i == k - 1) break; sumprev = sum; sum += arr[i + 2]; } if (flag == 1) { cout << "No"; return 0; } for (long long int i = 1; i <= k; i++) { for (long long int j = 1; j <= k; j++) { if (i == j) graph[i][j] = 0; } } for (long long int z = 1; z <= k; z++) { for (long long int i = 1; i <= k; i++) { for (long long int j = 1; j <= k; j++) { graph[i][j] = min(graph[i][j], graph[i][z] + graph[z][j]); } } } cout << "Yes" << endl; for (long long int i = 1; i <= k; i++) { for (long long int j = 1; j <= k; j++) { if (graph[i][j] == INF) cout << -1; else cout << graph[i][j]; cout << " "; } cout << endl; } return 0; }
2,000
CPP
T = int(input()) for t in range(T): N = int(input()) A = [int(_) for _ in input().split()] A = sorted(A) i = len(A)//2 res = [A[i]] k = 0 while len(res) != len(A): k += 1 if i-k >= 0: res.append(A[i-k]) if i+k < len(A): res.append(A[i+k]) print(' '.join(map(str, res)))
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 505, M = 500005; template <typename T> void read(T &x) { x = 0; char c = getchar(); bool p = 0; for (; c < '0' || c > '9'; c = getchar()) if (c == '-') p = 1; for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48); if (p) x = -x; } int n, m; bitset<N> f[65][2][N], Hina, Aya; int main() { read(n); read(m); for (int i = 1, x, y, w; i <= m; ++i) { read(x); read(y); read(w); f[0][w][x][y] = 1; } for (int i = 1; i <= 64; ++i) { for (int w = 0; w < 2; ++w) { for (int x = 1; x <= n; ++x) { for (int k = 1; k <= n; ++k) { if (f[i - 1][w][x][k]) f[i][w][x] |= f[i - 1][1 - w][k]; } } } } if (f[64][0][1].count()) { printf("-1\n"); return 0; } unsigned long long ans = 0; Aya[1] = 1; int typ = 0; for (int i = 63; i >= 0; --i) { Hina.reset(); for (int j = 1; j <= n; ++j) { if (Aya[j]) Hina |= f[i][typ][j]; } if (Hina.count()) { ans += (1ull << i); Aya = Hina; typ = 1 - typ; } } if (ans > 1000000000000000000) { printf("-1\n"); } else { printf("%I64d\n", ans); } return 0; }
2,400
CPP
a = 0 b = 0 n = int(input()) c = list(map(int, input().split())) for e in c : if e > 0 : a = a + 1 if e < 0 : b = b + 1 if max(a, b) < (n + 1) // 2 : print(0) else : if a > b : print(1) else : print(-1)
800
PYTHON3
n = int(input()) L = [] for _ in range(n): x,y = input().split() x = int(x) y = int(y) L.append([x,y]) # print(L) count = 0 for i in range(n): flagTop = False flagLeft = False flagRight = False flagBottom = False for j in range(n): if i == j: pass else: #check for top bottom left right if L[i][1] == L[j][1] and L[i][0] > L[j][0]: #left neighbor flagLeft = True if L[i][1] == L[j][1] and L[i][0] < L[j][0]: flagRight = True if L[i][0] == L[j][0] and L[i][1] > L[j][1]: #bottom neighbor flagBottom = True if L[i][0] == L[j][0] and L[i][1] < L[j][1]: flagTop = True if flagTop == True and flagBottom == True and flagLeft == True and flagRight == True: count += 1 print(count)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n, k; cin >> n >> k; long long int a[n]; map<long long int, long long int> mp; for (long long int i = 0; i < n; i++) { cin >> a[i]; if (a[i] % k != 0) { mp[k - (a[i] % k)] += 1; } } long long int freq = INT_MIN; long long int maxm = INT_MIN; if (mp.size() == 0) { cout << "0" << endl; } else { for (auto i : mp) { if (i.second == freq) { maxm = max(maxm, i.first); } else if (i.second > freq) { maxm = i.first; freq = i.second; } } long long int val = (freq - 1) * k + maxm + 1; cout << val << endl; } } return 0; }
1,400
CPP
n, a, x, b, y = map(int, input().split()) flag = 0 for _ in range(n): if a == b: flag = 1 break if a == x or b == y: break else: a += 1 if a > n: a = 1 b -= 1 if b == 0: b = n if flag == 1: print("YES") else: print("NO")
900
PYTHON3
for _ in range(int(input())): n=int(input()) a=n*2+(n-2)*2 m=n//2 s=0 while m!=0: s=s+(a*m) m=m-1 a=a-8 print(s)
1,000
PYTHON3
n = int(input()) strin = input() dic = dict() ans = 0 for c in strin: if c.islower(): if c not in dic: dic[c] = 1 else: v = dic[c] dic[c] = v+1 else: C = c.lower() if C not in dic or dic[C] == 0: ans += 1 if C in dic: v = dic[C] dic[C] = 0 if v == 0 else v - 1 print(ans)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 505; int b[N][N]; struct node { int x, y, t; } a[N * N]; bool cmp(node a, node b) { return a.t < b.t; } int n, m, k; bool isok(int t) { memset(b, 0, sizeof(b)); for (int i = 1; i <= t; i++) { b[a[i].x][a[i].y] = 1; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { b[i][j] += b[i][j - 1]; } for (int j = 1; j <= m; j++) { b[i][j] += b[i - 1][j]; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int x = i + k - 1, y = j + k - 1; if (x > n || y > m) continue; if (b[x][y] + b[i - 1][j - 1] - b[i - 1][y] - b[x][j - 1] == k * k) return true; } } return false; } int main() { int q; scanf("%d%d%d%d", &n, &m, &k, &q); for (int i = 1; i <= q; i++) { scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].t); } sort(a + 1, a + 1 + q, cmp); int l = 1, r = q + 1; while (l < r) { int mid = (l + r) / 2; if (isok(mid)) r = mid; else l = mid + 1; } if (l == q + 1) printf("-1"); else printf("%d", a[l].t); return 0; }
1,900
CPP
n = input() items = [int(i) for i in input().split()] y, n = 0, 0 for i in items: if i % 2 == 0: y += 1 else: n += 1 print(min(y, n))
900
PYTHON3
n = int(input()) cnt = 0 for i in range(n): a, b = list(map(int, input().split())) if b - a >= 2: cnt += 1 print(cnt)
800
PYTHON3
t=int(input()) for i in range(t): n=input() n=list(n) n.reverse() if n[0]=="o": print("FILIPINO") elif n[0]=="u": print("JAPANESE") elif n[0]=="a": print("KOREAN")
800
PYTHON3
# cook your code here import math n = int(input()) arr = [int(x) for x in input().split(' ')] arr.sort() x = pow(arr[n-1],float(1/float(n-1))) x_1 = math.floor(x) x_2 = math.ceil(x) rem1=0 rem2=0 for i in range(n): rem1 = rem1 + abs(int(pow(x_1,i)-arr[i])) for i in range(n): rem2 = rem2 + abs(int(pow(x_2,i)-arr[i])) print(int(min(rem1,rem2)))
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<vector<int>> v; for (int i = 0; i < n; i++) { int p = a[i]; int f = 0; int la = v.size(); while (f < la) { int mid = f + (la - f) / 2; if (v[mid].back() < p) { la = mid; } else { f = mid + 1; } } if (v.size() == f) v.push_back({p}); else v[la].push_back(p); } for (vector<int> i : v) { for (int j : i) { cout << j << " "; } cout << "\n"; } }
1,600
CPP
#include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while (T--) { int a, b; cin >> a; cin >> b; int c = a > b ? a - b : b - a; if (c == 0) { cout << 0 << endl; continue; } int n = sqrt(2 * c) - 1; while (1) { if (n * (n + 1) / 2 < c) { n++; continue; } if ((n * (n + 1) / 2 - c) % 2 == 0) { cout << n << endl; break; } n++; } } }
1,500
CPP
s = input() v = 0 h = 0 for ch in s: if ch == '0': print(f'1 {v+1}') v = (v+1)%4 else: print(f'4 {h*2+1}') h = (h+1)%2
1,400
PYTHON3
s = input() if len(s) == 1: if s.isupper(): print(s.lower()) else: print(s.upper()) elif s[0].islower() and s[1:].isupper(): print(s[0].upper()+s[1:].lower()) elif s.isupper(): print(s.lower()) else: print(s)
1,000
PYTHON3
for _ in range(int(input())): n,m = map(int,input().split()) cnt = 0 for i in range(n): s = input() if s[-1]=='R': cnt += 1 if i==(n-1): cnt += s.count('D') print(cnt)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; int main() { scanf("%d", &n); int l = 1, r = n; int minx = 1000000; int ans1 = l, ans2 = r; for (int i = 1; i <= sqrt(n); i++) { if (i * (n / i) == n) { if (n / i - i < minx) { minx = n / i - i; ans1 = i; ans2 = n / i; } } } cout << ans1 << " " << ans2 << endl; return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; int main() { unordered_map<long long, long long> tab[2]; unordered_set<long long> ind[2]; long long n, x; cin >> n; vector<long long> a(n); for (int i = (0); i <= (int(n) - 1); ++i) cin >> a[i]; tab[0][a[0]]++; ind[0].insert(0); long long last = -1; for (int i = (0); i <= (int(n - 1) - 1); ++i) { cin >> x; if (ind[0].count(x - 1) > 0) { ind[1].insert(i + 1); tab[1][a[i + 1]]++; last = 1; } else { ind[0].insert(i + 1); tab[0][a[i + 1]]++; last = 0; } } if (last == 1) { swap(tab[0], tab[1]); swap(ind[0], ind[1]); } long long s = 0; long long c1 = 0, c2 = 0; for (auto it = tab[0].begin(); it != tab[0].end(); ++it) { if (it->second % 2 == 1) s ^= (it->first); c1 += (it->second); } for (auto it = tab[1].begin(); it != tab[1].end(); ++it) { c2 += (it->second); } long long res = 0; if (s == 0) { res = c1 * (c1 - 1) / 2 + c2 * (c2 - 1) / 2; for (auto it = tab[0].begin(); it != tab[0].end(); ++it) { res += (it->second * tab[1][it->first]); } } else { for (auto it = tab[1].begin(); it != tab[1].end(); ++it) { long long sn = s ^ it->first; res += (it->second * tab[0][sn]); } } cout << res << endl; return 0; }
2,300
CPP
a = [int(i) for i in input().split()] boo=False sa=a[0]+a[1]+a[2]+a[3]+a[4]+a[5] for i in range(4): for j in range(i+1,5): for k in range(j+1,6): if a[i]+a[k]+a[j]==(sa-(a[i]+a[k]+a[j])): boo=True if boo==True: print("YES") else: print("NO")
1,000
PYTHON3
a=input("").lower() b=input("").lower() check=0 for i in range(len(a)): if(a[i]==b[i]): continue elif(ord(a[i])<ord(b[i])): check=1 print(-1) break elif(ord(a[i])>ord(b[i])): check=1 print(1) break if(check==0): print(0)
800
PYTHON3
l, r = map(int, input().split()) for i in range(l, r+1): ds = set() j = i good = True while j > 0: d = j % 10 j //= 10 if d in ds: good = False break else: ds.add(d) if good: print(i) exit(0) print(-1)
800
PYTHON3
m, n = input().split() m = int(m) n = int(n) if (m*n >= 2): print(int(((m*n)-((m*n)%2))/2)) else: print(0)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = (1 << 8) + 7, mod = 1e9 + 7; struct mat { int n, a[N][N]; mat(int nn, int b = 0) { n = nn; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) a[i][j] = (i == j) * b; } int* operator[](int x) { return a[x]; } }; mat operator*(mat x, mat y) { mat t(x.n); for (int i = 0; i < x.n; i++) for (int j = 0; j < y.n; j++) for (int k = 0; k < x.n; k++) t[i][j] = (t[i][j] + x[i][k] * 1ll * y[k][j]) % mod; return t; } mat operator^(mat a, int x) { mat t(a.n, 1); if (x == 0) return t; t = a ^ (x / 2); if (x & 1) return t * t * a; else return t * t; } int pre[N * 2], tmp[N * 2]; int32_t main() { pre[1] = 1; for (int i = 0; i < 7; i++) { int o; cin >> o; mat t(1 << (i + 1)); for (int bit1 = 0; bit1 < (1 << (i + 1)); bit1++) for (int bit2 = 0; bit2 < (1 << (i + 1)); bit2++) for (int mid = 0; mid < (1 << i); mid++) { int x = ((mid + (1 << i)) << 1) | 1, is = 1; for (int j = 0; j <= i; j++) if ((bit1 >> j & 1) && (bit2 >> j & 1) && (x >> j & 1) && (x >> (j + 1)) & 1) { is = 0; break; } t[bit1][bit2] += is; } t = t ^ o; memset(tmp, 0, sizeof tmp); for (int j = 0; j < (1 << (i + 1)); j++) for (int k = 0; k < (1 << (i + 1)); k++) tmp[j] = (tmp[j] + pre[k] * 1ll * t[k][j]) % mod; memset(pre, 0, sizeof pre); for (int j = 0; j < (1 << (i + 1)); j++) pre[j + (1 << (i + 1))] = tmp[j]; } cout << pre[(1 << 8) - 1] << endl; }
2,700
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, i; cin >> n; int a[n], mx = 0; map<int, int> mp; for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 1; i < n - 1; i++) { if (a[i] > a[i + 1] && a[i] > a[i - 1]) { mx++; } else if (a[i] < a[i + 1] && a[i] < a[i - 1]) { mx++; } } cout << mx << endl; return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const long long INF = 1E18; const double EPS = 1E-10; template <class T> T ckmin(T& a, T b) { return a = min(a, b); } template <class T> T ckmax(T& a, T b) { return a = max(a, b); } namespace input { template <class T1, class T2> void re(pair<T1, T2>& p); template <class T> void re(vector<T>& a); template <class T> void re(T& x) { cin >> x; } void re(double& x) { string t; re(t); x = stod(t); } template <class A, class... As> void re(A& head, As&... tail) { re(head); re(tail...); } template <class T1, class T2> void re(pair<T1, T2>& p) { re(p.first, p.second); } template <class T> void re(vector<T>& a) { for (int i = (0); i < (((int)(a).size())); i++) re(a[i]); } } // namespace input using namespace input; namespace output { template <class T1, class T2> void pr(const pair<T1, T2>& x); template <class T> void pr(const vector<T>& x); template <class T> void pr(const set<T>& x); template <class T1, class T2> void pr(const map<T1, T2>& x); template <class T> void pr(const T& x) { cout << x; } template <class A, class... As> void pr(const A& head, const As&... tail) { pr(head); pr(tail...); } template <class T1, class T2> void pr(const pair<T1, T2>& x) { pr("{", x.first, ", ", x.second, "}"); } template <class T> void prc(const T& x) { pr("{"); bool fi = 1; for (auto& a : x) pr(!fi ? ", " : "", a), fi = 0; pr("}"); } template <class T> void pr(const vector<T>& x) { prc(x); } template <class T> void pr(const set<T>& x) { prc(x); } template <class T1, class T2> void pr(const map<T1, T2>& x) { prc(x); } void ps() { pr("\n"); } template <class A, class... As> void ps(const A& head, const As&... tail) { pr(head, " "); ps(tail...); } } // namespace output using namespace output; inline int cmp(double x, double y = 0, double tol = EPS) { return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; } int main() { cin.tie(0); ios::sync_with_stdio(0); string a; re(a); int n = ((int)(a).size()); vector<int> up(n), lo(n + 1); for (int i = (0); i < (n); i++) up[i] = ((i) ? up[i - 1] : 0) + (islower(a[i]) > 0); for (int i = (n)-1; i >= (0); i--) lo[i] = ((i != n - 1) ? lo[i + 1] : 0) + (isupper(a[i]) > 0); int ret = lo[0]; for (int i = (0); i < (n); i++) ret = min(ret, up[i] + lo[i + 1]); pr(ret, '\n'); }
1,400
CPP
import math for _ in range(int(input())): n = int(input()) i=1; j=2 ans = n while i<=n: ans += n//i i=i*j//math.gcd(i,j) j+=1 print(ans%(10**9+7))
1,600
PYTHON3
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools # import time,random,resource # sys.setrecursionlimit(10**6) inf = 10**20 eps = 1.0 / 10**10 mod = 10**9+7 mod2 = 998244353 dd = [(-1,0),(0,1),(1,0),(0,-1)] ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()] def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()] def LF(): return [float(x) for x in sys.stdin.readline().split()] def LS(): return sys.stdin.readline().split() def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def S(): return input() def pf(s): return print(s, flush=True) def pe(s): return print(str(s), file=sys.stderr) def JA(a, sep): return sep.join(map(str, a)) def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a) def IF(c, t, f): return t if c else f def YES(c): return IF(c, "YES", "NO") def Yes(c): return IF(c, "Yes", "No") def main(): t = 1 rr = [] for _ in range(t): s = S() sl = len(s) l = 0 r = sl - 1 t = [] while l < r: while l < sl and s[l] == ')': l += 1 while r >= 0 and s[r] == '(': r -= 1 if l >= r: break t.append(l+1) t.append(r+1) l += 1 r -= 1 if t: t.sort() rr.append(1) rr.append(len(t)) rr.append(JA(t, " ")) else: rr.append(0) return JA(rr, "\n") print(main())
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int getn() { int h; scanf("%d", &h); return h; } template <class T, class U> ostream& operator<<(ostream& out, const pair<T, U>& par) { cout << "[" << par.first << ";" << par.second << "]"; return out; } vector<vector<int> > mini; int n; int nacitaj(int i, int j) { mini[i][j] = getn(); i = (i != n - 1 ? i + 1 : 0); j = (i != 0 ? j : j + 1); return (j != n ? nacitaj(i, j) : 1); } int floydwardshal(int i, int j, int l) { mini[i][j] = min(mini[i][j], mini[i][l] + mini[l][j]); i = (i != n - 1 ? i + 1 : 0); j = (i != 0 ? j : j + 1); j = (j >= n ? 0 : j); l = (i == 0 && j == 0 ? l + 1 : l); return (l != n ? floydwardshal(i, j, l) : 0); } int maxi(int i, int j) { int mx = mini[i][j]; i = (i != n - 1 ? i + 1 : 0); j = (i != 0 ? j : j + 1); return (j != n ? max(maxi(i, j), mx) : mx); } int main() { n = getn(); mini.resize(n, vector<int>(n, 0)); nacitaj(0, 0); floydwardshal(0, 0, 0); cout << maxi(0, 0) << endl; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; int arr[1009]; int main() { int n; scanf("%d", &n); int m = n; n--; while (n--) { int a, b; scanf("%d", &a); scanf("%d", &b); arr[a]++; arr[b]++; } int cnt = 0; for (int i = 1; i <= m; i++) { if (arr[i] == 1) cnt++; } printf("%d", cnt), printf("\n"); return 0; }
1,000
CPP
def minmoves(a,b): cnt=0 ##add or subtract 10 until the diff between a and b becomes less <10 ## if not 0 after all such moves we need an additional move return (abs(a-b)+9)//10 def main(): t=int(input()) while(t>0): a,b=map(int,input().split()) print(minmoves(a,b)) t=t-1 if __name__=='__main__': main()
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[1006]; int k; int fun(int x) { int mid, high = k - 1, low = 0, i; while (high >= low) { mid = (low + high) >> 1; if (a[mid] < x) { low = mid + 1; } else if (a[mid] > x) { high = mid - 1; } else break; } if (a[mid] == x) return 999999999; if (a[mid] < x) { for (i = mid; a[i] <= x && i < k; ++i) if (a[i] == x) return 999999999; return i; } else { for (i = mid; a[i] >= x && i >= 0; --i) if (a[i] == x) return 999999999; return i + 1; } } int main() { int i, n, m, q, x, y; while (scanf("%d%d%d%d", &n, &m, &k, &q) != EOF) { for (i = 0; i < k; ++i) { scanf("%d%d", &x, &y); a[i] = (x - 1) * m + y; } sort(a, a + k); while (q--) { scanf("%d%d", &x, &y); i = fun((x - 1) * m + y); if (i == 999999999) puts("Waste"); else switch (((x - 1) * m + y - i - 1) % 3) { case 0: puts("Carrots"); break; case 1: puts("Kiwis"); break; case 2: puts("Grapes"); break; } } } return 0; }
1,400
CPP
n = input() s = input() def jap_cross(s): res = [] while True: start = s.find('B') if start == -1: return res else: s = s[start:] length = s.find('W') if length == -1: res.append(len(s)) return res else: res.append(length) s = s[length:] res = jap_cross(s) print(len(res)) for x in res: print(x, end=" ")
800
PYTHON3
t=int(input()) for _ in range(t): n=int(input()) l=[] for i in range(n): x,y=map(int,input().split()) a=[x,y] l.append(a) l.sort() q=(sorted(l, key = lambda x: x[1])) ans="" curx,cury,flag=0,0,1 for i in range(len(q)): if q[i][0]<curx: flag=0 break te=q[i][0]-curx ans+=("R"*te) curx=q[i][0] if q[i][1]<cury: flag=0 break te=q[i][1]-cury ans+=("U"*te) cury=q[i][1] if flag: print("YES") print(ans) else: print("NO")
1,200
PYTHON3
""" Think I'm Still Worthy ? Contact : [email protected] """ from math import sqrt, gcd def countFactors(n): s = int(sqrt(n)) c = 0 for i in range(1, s+1): if (n % i == 0): if i != n/i: c += 2 else: c += 1 return c n, data = int(input()), [int(i) for i in input().strip().split()] g = data[0] for i in range(1, n): g = gcd(g, data[i]) print(countFactors(g))
1,300
PYTHON3
s,v1,v2,t1,t2=map(int,input().split(' ')) s1=t1*2+v1*s s2=t2*2+v2*s if s2>s1: print('First') elif s1>s2: print('Second') else: print('Friendship')
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m; map<int, int> R, C; char ch; int main() { cin >> n >> m; for (int r = 0; r < n; ++r) { for (int c = 0; c < m; ++c) { cin >> ch; if (ch == '*') { ++R[r]; ++C[c]; } } } for (auto r : R) { if (r.second == 1) { cout << r.first + 1 << " "; } } for (auto c : C) { if (c.second == 1) { cout << c.first + 1 << endl; } } return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; void debug(vector<int> v) { cerr << "["; int n = (int)v.size(); for (int i = 0; i < n; i++) { cerr << v[i] << (i == n - 1 ? "" : ", "); } cerr << "]" << '\n'; } void test_case() { int n, m; cin >> n >> m; int goal = n / m; vector<int> cnt(m, 0); vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int& x : a) { cnt[x % m]++; } set<int> to_fill; for (int i = 0; i < m; i++) { if (cnt[i] < goal) { to_fill.insert(i); } } long long moves = 0; for (int i = 0; i < n; i++) { int& x = a[i]; if (cnt[x % m] > goal) { auto it = to_fill.lower_bound(x % m); int value; if (it == to_fill.end()) { it = to_fill.begin(); value = *it + m - x % m; } else { value = *it - x % m; } moves += value; cnt[x % m]--; x += value; cnt[x % m]++; if (cnt[x % m] == goal) { to_fill.erase(x % m); } } } cout << moves << '\n'; for (auto x : a) { cout << x << ' '; } cout << '\n'; } int32_t main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int T; T = 1; while (T-- > 0) { test_case(); } }
1,900
CPP
import math t = int(input('')) while t: t -= 1 a = int(input('')) if a % 2 == 0: print(2,a-3,1) else: a -= 1 f = a//2 s = a//2 while math.gcd(f , s) != 1: f += 1 s -= 1 print(f,s,1)
900
PYTHON3
T = int(input()) M =[ [0 for i in range(300)] for j in range(300)] for t in range(T): n = int(input()) for i in range(n): a = input() for j in range(n): if a[j] != 'S' and a[j] != 'F': M[i][j] = int(a[j]) res1 = [] res0 = [] if M[0][1] == 1: res0.append((1,2)) else: res1.append((1,2)) if M[1][0] == 1: res0.append((2,1)) else: res1.append((2,1)) if M[n-1][n-2] == 1: res1.append((n,n-1)) else: res0.append((n,n-1)) if M[n-2][n-1] == 1: res1.append((n-1,n)) else: res0.append((n-1, n)) if len(res1) <= 2: print(len(res1)) for (a,b) in res1: print(a,b) else: print(len(res0)) for (a,b) in res0: print(a,b)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; int d[100100], n, m, k, f, f1, pos[100100], ff; vector<int> li; vector<vector<int> > g(100100); void dfs(int ver, int p) { if (ff == 1) { return; } li.push_back(ver); d[ver] = li.size(); for (auto to : g[ver]) { if (to == ver) { continue; } if (d[to] == 0) { dfs(to, ver); } else { if (d[ver] - d[to] + 1 >= k && ff == 0) { cout << 2 << "\n" << d[ver] - d[to] + 1 << '\n'; for (int i = d[to] - 1; i < d[ver]; i++) { cout << li[i] << " "; } ff = 1; return; } } } if (!pos[ver]) { for (auto to : g[ver]) { pos[to] = 1; } } li.pop_back(); } int main() { cin >> n >> m; k = 1; while (k * k < n) { k++; } for (int i = 1; i <= m; i++) { cin >> f >> f1; g[f].push_back(f1); g[f1].push_back(f); } dfs(1, -1); if (ff == 1) { return 0; } cout << 1 << '\n'; for (int i = 1; k != 0; i++) { if (pos[i] == 0) { cout << i << " "; k--; } } }
2,500
CPP
n=int(input()) i=0 lz=0 l1=0 r1=0 rz=0 while i<n: l, r = map(int, input().split()) if l==0: lz=lz+1 else: l1=l1+1 if r==0: rz=rz+1 else: r1=r1+1 i=i+1 ans=0 if r1<rz: ans=ans+r1 else: ans=ans+rz if l1<lz: ans=ans+l1 else: ans=ans+lz print(ans)
800
PYTHON3
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int T,n; long long int a[200005]; long long int b[200005]; long long int func(int x){ if(x>n)return 0; if(b[x])return b[x]; b[x]=a[x]+func(x+a[x]); return b[x]; } int main(){ scanf("%d",&T); while(T--){ scanf("%d",&n); memset(b,0,sizeof(b)); for(int i=1;i<=n;i++){ scanf("%lld",&a[i]); } long long int ans=0; for(int i=1;i<=n;i++)if(!b[i]){ b[i]=func(i); ans=max(ans,b[i]); } printf("%lld\n",ans); } }
1,100
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; inline int read(); const int M = 4096, MOD = 1000000007; int save[M], ttt[M], table[531441]; int dfs(char* op, int t) { if (!*op) return table[t]; t *= 3; switch (*op++) { case 'A': return dfs(op, t) + dfs(op, t + 1); case 'O': return dfs(op, t); case 'X': return dfs(op, t) + dfs(op, t + 2); case 'a': return dfs(op, t + 2); case 'o': return dfs(op, t + 1) + dfs(op, t + 2); } return dfs(op, t + 1); } int main(void) { int w = read(), n = read(), m = read(), W = 1 << w; for (int i = 1; i < W; ++i) ttt[i] = ttt[i / 2] * 3 + i % 2; while (n--) ++save[read()]; for (int i = 0; i < M; ++i) if (save[i]) for (int j = 0; j < M; ++j) if (save[j]) table[ttt[i] + ttt[j]] += save[i] * save[j]; char str[10]; while (m--) { scanf("%s", str); printf("%d\n", dfs(str, 0)); } return 0; } inline 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 * 10 + ch - '0'; ch = getchar(); } return x * f; }
2,800
CPP
ans = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0,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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] r, c = map(int, input().split()) print('IN' if ans[r][c] == 1 else 'OUT')
0
PYTHON3
import sys nums=input() l= nums.split() n=int(l[0]) m=int(l[1]) s=[] r=1 for z in range (m): s.append(0) for j in range(n): line=input() if len(line) != 0: l=line.split() for i in range(1, len(l)): s[int(l[i])-1]=1 else: break for i in range(m): r &= s[i] if(r): print("YES") else: print("NO")
800
PYTHON3
t=int(input()) while(t): r,g,b=map(int,input().split()) a=[] a.append(r) a.append(b) a.append(g) a.sort() b=a[2] c=sum(a)-b if b>c+1: print("No") else:print("Yes") t-=1
900
PYTHON3
#include <bits/stdc++.h> using namespace std; const long double eps = 1e-13; const long double PI = acos(-1); const int INF = (int)1e9 + 7; const long long INFF = (long long)2e18; const int mod = (int)1e9 + 7; const int MXN = (int)2e5 + 7; int dep[MXN]; struct node { int v, f1, f2, l; node(){}; node(int _v, int _f1, int _f2) { v = _v, f1 = _f1, f2 = _f2; l = dep[f1] + dep[f2] - 2 * dep[v]; } const bool operator<(const node &p2) const { if (dep[v] != dep[p2.v]) return dep[v] < dep[p2.v]; return l < p2.l; } }; vector<int> edge[MXN]; int bst[MXN][2]; int in[MXN], out[MXN], cntp; void dfs(int u, int pa, int dpt) { dep[u] = dpt; bst[u][0] = bst[u][1] = u; in[u] = ++cntp; for (int v : edge[u]) { if (v == pa) continue; dfs(v, u, dpt + 1); if (dep[bst[v][0]] > dep[bst[u][0]]) { bst[u][1] = bst[u][0]; bst[u][0] = bst[v][0]; } else if (dep[bst[v][0]] > dep[bst[u][1]]) { bst[u][1] = bst[v][0]; } } out[u] = cntp; } int d1, d2; int s1, e1, s2, e2; node dp[MXN]; int ck(int u, int v) { if (in[v] <= in[u] && out[u] <= out[v]) return true; return false; } void dfs2(int u, int pa, int dis, int fr) { for (int v : edge[u]) { if (v == pa) continue; if (in[v] <= in[bst[u][0]] && out[bst[u][0]] <= out[v]) { if (dis > dep[bst[u][1]] - dep[u]) dfs2(v, u, dis + 1, fr); else dfs2(v, u, dep[bst[u][1]] - dep[u] + 1, bst[u][1]); } else { if (dis > dep[bst[u][0]] - dep[u]) dfs2(v, u, dis + 1, fr); else dfs2(v, u, dep[bst[u][0]] - dep[u] + 1, bst[u][0]); } if (dp[v].v == 0) continue; if (dis && bst[u][0] != u && !ck(bst[u][0], v)) { int v1 = dep[dp[v].v] - dep[u]; int v2 = dis + dep[bst[u][0]] - dep[u] + dp[v].l; if (d1 < v1 || (d1 == v1 && d2 < v2)) { d1 = v1, d2 = v2; s1 = fr, e1 = bst[u][0]; s2 = dp[v].f1, e2 = dp[v].f2; } } if (dis && bst[u][1] != u && !ck(bst[u][1], v)) { int v1 = dep[dp[v].v] - dep[u]; int v2 = dis + dep[bst[u][1]] - dep[u] + dp[v].l; if (d1 < v1 || (d1 == v1 && d2 < v2)) { d1 = v1, d2 = v2; s1 = fr, e1 = bst[u][1]; s2 = dp[v].f1, e2 = dp[v].f2; } } if (dp[u].v) { int v1 = dep[dp[u].v] + dep[dp[v].v] - 2 * dep[u]; int v2 = dp[u].l + dp[v].l; if (d1 < v1 || (d1 == v1 && d2 < v2)) { d1 = v1, d2 = v2; s1 = dp[u].f1, e1 = dp[u].f2; s2 = dp[v].f1, e2 = dp[v].f2; } } if (dp[u] < dp[v]) { dp[u] = dp[v]; } } if (dp[u].v == 0 && bst[u][0] != u && bst[u][1] != u) { dp[u] = node(u, bst[u][0], bst[u][1]); } } int main() { int n; scanf("%d", &n); for (int i = 1; i < n; i++) { int u, v; scanf("%d %d", &u, &v); edge[u].push_back(v); edge[v].push_back(u); } int rt = 0; for (int i = 1; i <= n; i++) if ((int)(edge[i]).size() == 1) rt = i; dfs(rt, 0, 1); dfs2(rt, 0, 0, rt); printf("%d %d\n%d %d\n", s1, e2, s2, e1); return 0; }
2,500
CPP
#include <bits/stdc++.h> using namespace std; struct skill { string name; int exp; bool operator<(const skill t) const { return name < t.name; } } a[30], b[60]; int n, m; double k; string mz; bool ok; int main() { int i, j = 0, l; cin >> n >> m >> k; for (i = 1; i <= n; i++) cin >> a[i].name >> a[i].exp; for (i = 1; i <= n; i++) { if (a[i].exp * k < 100) continue; j++; b[j] = a[i]; b[j].exp = int(b[j].exp * k + 1e-6); } for (i = 1; i <= m; i++) { ok = false; cin >> mz; for (l = 1; l <= j; l++) if (b[l].name == mz) { ok = true; break; } if (ok) continue; j++; b[j].name = mz; b[j].exp = 0; } cout << j << endl; sort(b + 1, b + j + 1); for (i = 1; i <= j; i++) cout << b[i].name << " " << b[i].exp << endl; return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; bool a[2000][2000], b[2000][2000], c[2000][2000]; int n, m; void jian() { memcpy(c, b, sizeof(b)); for (int i = 1; i < n - 1; i++) { for (int j = 1; j < m - 1; j++) { if (!b[i + 1][j] || !b[i][j + 1] || !b[i - 1][j] || !b[i][j - 1]) c[i][j] = 0; } } memcpy(b, c, sizeof(b)); } void jia() { memcpy(c, b, sizeof(b)); for (int i = 1; i < n - 1; i++) { for (int j = 1; j < m - 1; j++) { if (b[i + 1][j] || b[i][j + 1] || b[i - 1][j] || b[i][j - 1]) { c[i][j] = 1; } } } memcpy(b, c, sizeof(b)); } bool vis[2000][2000]; int d[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; int cnt, ans[20000]; pair<int, int> q1[6000000], q2[6000000]; int h1, t1, h2, t2; bool count(int x, int y) { h1 = 1; t1 = 1; q1[t1++] = make_pair(x, y); a[x][y] = 0; int cnt = 0; while (h1 < t1) { x = q1[h1].first; y = q1[h1].second; h1++; for (int i = 0; i < 4; i++) { int tx = x + d[i][0], ty = y + d[i][1]; if (tx > n || tx < 0 || ty > m || ty < 0) continue; if (a[tx][ty]) { a[tx][ty] = 0; cnt++; q1[t1++] = make_pair(tx, ty); } } } if (cnt > 5) return 1; else return 0; } int bfs(int x, int y) { h2 = 1; t2 = 1; q2[t2++] = make_pair(x, y); b[x][y] = 0; int an = 0; while (h2 < t2) { x = q2[h2].first; y = q2[h2].second; h2++; for (int i = 0; i < 4; i++) { int tx = x + d[i][0], ty = y + d[i][1]; if (a[tx][ty]) { an += count(tx, ty); } if (b[tx][ty]) { b[tx][ty] = 0; q2[t2++] = make_pair(tx, ty); } } } return an; } inline int read() { int x = 0; char c = getchar(); bool flag = 0; while (c < '0' || c > '9') { if (c == '-') flag = 1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 3) + (x << 1) + c - '0'; c = getchar(); } return flag ? -x : x; } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int C = read(); a[i][j] = b[i][j] = C; } } n += 2; m += 2; for (int i = 1; i <= 4; i++) jian(); for (int i = 1; i <= 8; i++) jia(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (b[i][j]) a[i][j] = 0; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (b[i][j]) { ans[++cnt] = bfs(i, j); } } } sort(ans + 1, ans + cnt + 1); cout << cnt << endl; for (int i = 1; i <= cnt; i++) { cout << ans[i] << " "; } cout << endl; return 0; }
2,200
CPP
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const double eps = 1e-9; struct MinCostMaxFlow { static const int MAXV = 110; static const int INF = 1000000000; struct Edge { int v, cap; double w; int rev; Edge() {} Edge(int t2, int t3, double t4, int t5) : v(t2), cap(t3), w(t4), rev(t5) {} }; int V, s, t; vector<Edge> g[MAXV]; void init(int n) { V = n + 2; s = n + 1, t = n + 2; for (int i = 1; i <= V; i++) g[i].clear(); } void addEdge(int a, int b, int cap, double w) { g[a].push_back(Edge(b, cap, w, (int)g[b].size())); g[b].push_back(Edge(a, 0, -w, (int)g[a].size() - 1)); } double d[MAXV]; int id[MAXV], mom[MAXV]; bool inqu[MAXV]; queue<int> qu; double solve() { int mxf = 0; double mnc = 0; while (1) { for (int i = 0; i <= V; i++) { d[i] = 1e9; inqu[i] = false; mom[i] = -1; } mom[s] = s; d[s] = 0; qu.push(s); inqu[s] = 1; while (qu.size()) { int u = qu.front(); qu.pop(); inqu[u] = 0; for (int i = 0; i < (int)g[u].size(); i++) { Edge &e = g[u][i]; int v = e.v; if (e.cap > 0 && d[v] > d[u] + e.w + eps) { d[v] = d[u] + e.w; mom[v] = u; id[v] = i; if (!inqu[v]) { qu.push(v); inqu[v] = 1; } } } } if (mom[t] == -1) break; int df = INF; for (int u = t; u != s; u = mom[u]) df = min(df, g[mom[u]][id[u]].cap); for (int u = t; u != s; u = mom[u]) { Edge &e = g[mom[u]][id[u]]; e.cap -= df; g[e.v][e.rev].cap += df; } mxf += df; mnc += df * d[t]; } return mnc; } } flow; int nf, ne, ns; int rf, re, rs; int df, de; double drf, dre, drs; void init() { cin >> nf >> ne >> ns; cin >> rf >> re >> rs; cin >> df >> de; drf = sqrt(rf * rf - 1); dre = sqrt(re * re - 1); drs = sqrt(rs * rs - 1); } double ans; int f[13 + 13]; pair<double, pair<int, int>> vv[99]; double score(int at, double r, int d) { int vt = 0; vv[vt++] = {at - r, {1, +d}}; vv[vt++] = {at + r, {1, -d}}; int tms = 1, dmg = 0; for (int i = 0; i < 13; i++) { if (f[i] == 0) continue; if (i + drs < at - r) continue; if (i - drs > at + r) continue; if (i - drs < at - r) tms += f[i]; else { vv[vt++] = {i - drs, {0, +f[i]}}; } if (i + drs < at + r) { vv[vt++] = {i + drs, {0, -f[i]}}; } } sort(vv, vv + vt); double ret = 0; for (int i = 0; i + 1 < vt; i++) { double tint = vv[i + 1].first - vv[i].first; if (vv[i].second.first) dmg += vv[i].second.second; else tms += vv[i].second.second; ret += tint * dmg * tms; } return ret; } double dp[22][22]; inline int enc() { int ret1 = 0; for (int i = 0; i < 13; i++) ret1 = (ret1 << 2) | f[i]; int ret2 = 0; for (int i = 13 - 1; i >= 0; i--) ret2 = (ret2 << 2) | f[i]; return min(ret1, ret2); } unordered_set<int> vst; void cal() { int r = enc(); if (vst.find(r) != vst.end()) return; vst.insert(r); if (ns < 12) { flow.init(2 + 13); flow.addEdge(flow.s, 13 + 1, nf, 0); flow.addEdge(flow.s, 13 + 2, ne, 0); for (int i = 0; i < 13; i++) { if (f[i] == 2) continue; double scoref = score(i, drf, df); double scoree = score(i, dre, de); flow.addEdge(13 + 1, i + 1, 2 - f[i], -scoref); flow.addEdge(13 + 2, i + 1, 2 - f[i], -scoree); flow.addEdge(i + 1, flow.t, 2 - f[i], 0); } double tret = -flow.solve(); ans = max(ans, tret); return; } vector<double> sf, se; for (int i = 0; i < 13; i++) { if (f[i] == 2) continue; double scoref = score(i, drf, df); double scoree = score(i, dre, de); for (int j = f[i]; j < 2; j++) { sf.push_back(scoref); se.push_back(scoree); } } sort(sf.begin(), sf.end(), greater<double>()); sort(se.begin(), se.end(), greater<double>()); for (int i = 0; i <= nf; i++) for (int j = 0; j <= ne; j++) dp[i][j] = 0.0; for (int i = 0; i <= nf; i++) for (int j = 0; j <= ne; j++) { int nxt = i + j; if (i < nf) dp[i + 1][j] = max(dp[i + 1][j], dp[i][j] + sf[nxt]); if (j < ne) dp[i][j + 1] = max(dp[i][j + 1], dp[i][j] + se[nxt]); } ans = max(ans, dp[nf][ne]); } void go(int now, int res) { if (now == 13) { if (res == 0) cal(); return; } if ((13 - now) * 2 < res) return; for (int i = 0; i <= min(2, res); i++) { f[now] = i; go(now + 1, res - i); } } void solve() { go(0, ns); printf("%.12f\n", ans); } int main() { init(); solve(); }
2,600
CPP
s = str(input())[-1] print(int(s)%2)
0
PYTHON3
# https://codeforces.com/problemset/problem/456/A import sys import math def main(): # sys.stdin = open('E:\\Sublime\\in.txt', 'r') # sys.stdout = open('E:\\Sublime\\out.txt', 'w') # sys.stderr = open('E:\\Sublime\\err.txt', 'w') n = int(sys.stdin.readline().strip()) for _ in range(n): a, b = map(int, sys.stdin.readline().strip().split()[:2]) if a != b: print('Happy Alex') exit() print('Poor Alex') if __name__ == '__main__': main() # hajj # γ€€γ€€γ€€γ€€γ€€γ€€ οΌΏοΌΏ # γ€€γ€€γ€€γ€€γ€€οΌοΌžγ€€γ€€γƒ• # γ€€γ€€γ€€γ€€γ€€| γ€€_γ€€ _ l # γ€€    /` γƒŸοΌΏxγƒŽ # γ€€γ€€ γ€€ /γ€€γ€€γ€€ γ€€ | # γ€€γ€€γ€€ /γ€€ ヽ   οΎ‰ # γ€€ γ€€ β”‚γ€€γ€€|γ€€|γ€€| #  / ̄|γ€€γ€€ |γ€€|γ€€| # γ€€| ( ̄ヽ__ヽ_)__) # γ€€οΌΌδΊŒγ€
1,100
PYTHON3
s = input() t = input() i=0 for x in t: if s[i] ==x: i+=1 print(i+1)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long INF = 1e14 + 9; string faces; void rotateX() { char aux = faces[2]; faces[2] = faces[1]; faces[1] = faces[5]; faces[5] = faces[3]; faces[3] = aux; } void rotateY() { char aux = faces[4]; faces[4] = faces[5]; faces[5] = faces[0]; faces[0] = faces[2]; faces[2] = aux; } void rotateZ() { char aux = faces[3]; faces[3] = faces[0]; faces[0] = faces[1]; faces[1] = faces[4]; faces[4] = aux; } int main() { ios::sync_with_stdio(false); cin.tie(0); string s; cin >> s; sort(begin(s), end(s)); set<string> perms; long long ans = 0; do { faces = ""; for (long long i = 0; i < ((long long)(s).size()); ++i) { faces += s[i]; } if (perms.find(faces) == perms.end()) { ans++; for (long long i = 0; i < (4); ++i) { for (long long j = 0; j < (4); ++j) { for (long long k = 0; k < (4); ++k) { perms.insert(faces); rotateZ(); } rotateY(); } rotateX(); } } } while (next_permutation(begin(s), end(s))); cout << ans << '\n'; return 0; }
1,700
CPP
for _ in range(int(input())): n=int(input()) list1=list(map(int,input().split())) list1.sort() count=1 cnt=0 for i in list1: #print(i,count,cnt+count) if i<=count: count+=1 elif cnt!=0: if (cnt+count)>=i: count+=cnt+1 cnt=0 else: cnt+=1 else: cnt=1 print(count)
1,000
PYTHON3