solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; char s[1000010]; int c[20]; int main() { int k; scanf("%d", &k); scanf("%s", s + 1); int n = strlen(s + 1); int sum = 0; int i; memset(c, 0, sizeof c); int ans = 0; for (i = 1; i <= n; i++) { c[s[i] - '0']++; sum += s[i] - '0'; } sum = k - sum; for (i = 0; i <= 9; i++) while (sum > 0 && c[i]) { sum -= 9 - i; ans++; c[i]--; } printf("%d\n", ans); return 0; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; string to_string(string s) { return '"' + s + '"'; } string to_string(const char* ch) { return to_string((string)ch); } string to_string(char ch) { return (string) "'" + ch + (string) "'"; } string to_string(bool b) { return (b ? "true" : "false"); } template <class A, class B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <class A> string to_string(A a) { string res = "{"; bool first = true; for (const auto& x : a) { if (first == false) res += ", "; first = false; res += to_string(x); } res += "}"; return res; } void debug() { cerr << "]\n"; } template <class H, class... T> void debug(H head, T... tail) { cerr << to_string(head) << " "; debug(tail...); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int t; cin >> t; while (t--) { int n; cin >> n; vector<long long int> a(n); long long int sum = 0; for (auto& i : a) cin >> i, sum += i; if (sum % n) { cout << -1 << '\n'; continue; } long long int x = sum / n; vector<pair<pair<int, int>, long long int>> ans; auto g = [&](int i, int j, int X) { i++; j++; ans.push_back(make_pair(make_pair(i, j), X)); assert(a[i - 1] >= X * i); a[i - 1] -= X * i; a[j - 1] += X * i; }; for (int i = 1; i < n; i++) { g(0, i, (i + 1 - (a[i] % (i + 1))) % (i + 1)); g(i, 0, a[i] / (i + 1)); } for (int i = 1; i < n; i++) { g(0, i, x); } for (int i = 0; i < n; i++) { assert(a[i] == x); } cout << (int)ans.size() << '\n'; for (auto& i : ans) cout << i.first.first << ' ' << i.first.second << ' ' << i.second << '\n'; } return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; long long edges; pair<long long, pair<long long, long long> > edgelist[2 * 200005]; vector<long long> link(200005), size(200005, 1); long long find(long long x) { while (x != link[x]) x = link[x]; return x; } bool same(long long i, long long j) { return find(i) == find(j); } void unite(long long a, long long b) { a = find(a); b = find(b); if (size[a] < size[b]) swap(a, b); size[a] += size[b]; link[b] = a; } long long kruskal() { long long x, y; long long cost, minimumCost = 0; for (long long i = 0; i < edges; ++i) { x = edgelist[i].second.first; y = edgelist[i].second.second; cost = edgelist[i].first; if (!same(x, y)) { minimumCost += cost; unite(x, y); } } return minimumCost; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int TESTS = 1; while (TESTS--) { for (long long i = 1; i < 200005 + 1; i++) link[i] = i; long long n, m; cin >> n >> m; if (n == 1) { cout << "0"; return 0; } long long cost[n], node, v = pow(10, 18); for (long long i = 1; i < n + 1; i++) { cin >> cost[i]; if (cost[i] < v) { v = cost[i]; node = i; } } for (long long i = 0; i < m; i++) { cin >> edgelist[i].second.first >> edgelist[i].second.second >> edgelist[i].first; } for (long long i = 1; i < n + 1; i++) { edgelist[i + m - 1].second.first = node; edgelist[i + m - 1].second.second = i; edgelist[i + m - 1].first = cost[i] + cost[node]; } edges = m + n; sort(edgelist, edgelist + edges); cout << kruskal(); } return 0; }
1,900
CPP
from math import * from sys import * t=int(stdin.readline()) for _ in range(t): n = int(stdin.readline()) a=list(map(int,stdin.readline().split())) f=0 d={} for i in range(n): if a[i] not in d: d[a[i]]=0 else: f=1 break if f==1: print("YES") else: print("NO")
1,000
PYTHON3
import sys # from math import * def ints_input(): return [int(i) for i in sys.stdin.readline().strip("\n").split(" ")] def print_list(arr): sys.stdout.writelines(str(x)+" " for x in arr) sys.stdout.write("\n") def fast_input(type=str): return type(sys.stdin.readline().strip("\n")) n, a, b = ints_input() arr = ints_input() for i in arr: print(((i * a) % b)//a, end=" ")
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { string s; int64_t N, n, k, i = 1; cin >> n >> k; cin >> s; N = s.size(); if (N == 1 && k != 0) { cout << "0" << endl; } else if (N == 1 && k == 0) { cout << s << endl; } else { if (s[0] != '1' && k != 0) { s[0] = '1'; k--; } while (k--) { if (i == n) { break; } else { if (s[i] != '0') { s[i] = '0'; } else { k++; } } i++; } cout << s << endl; } return 0; }
1,000
CPP
mod = 10 ** 9 + 7 ii = lambda : int(input()) si = lambda : input() dgl = lambda : list(map(int, input())) f = lambda : map(int, input().split()) il = lambda : list(map(int, input().split())) ls = lambda : list(input()) n=ii() l=il() l1=[0]*n mx=0 for i in range(n-1, -1, -1): if l[i]<=mx: l1[i]=mx-l[i]+1 mx=max(l[i],mx) print(*l1)
1,100
PYTHON3
T = int(input()) for t in range(T): x,y = [int(x) for x in input().split()] if x>=4: print("YES") elif (x==3 or x==2) and y<=3: print("YES") elif x==1 and y==1: print("YES") else: print("NO")
1,000
PYTHON3
n=int(input()) if n%2!=0: print (11) else: if (n%5==1): print (14) elif(n%5==2): print (12) elif (n%5==3): print (8) elif (n%5==4): print (11) else: z=1 while (True): if int((n*z+1)**0.5)**2==(n*z+1): print (z) break z+=1
800
PYTHON3
def getNext(num): nums = list( map( int , list(str(num)) ) ) nums.sort() return nums[0] * nums[-1] def solve(st , k): for i in range(k-1): nex = getNext(st) if nex == 0: break st = st + nex print(st) # input = open('file.txt').readline for _ in range( int( input() ) ): a , b = map( int , input().strip().split(" ") ) solve(a , b)
1,200
PYTHON3
for i in range(int(input())): x=int(input()) if x%2==0: v=x//2 else: v=x//2+1 print(v-1)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int INT_MAX_VAL = (int)1e20; int INT_MIN_VAL = (int)-1e20; long long LONG_MAX_VAL = (long long)1e20; long long LONG_MIN_VAL = (long long)-1e20; long long a[1005][1005]; long long n, m, k, p; int main() { cin >> n >> m >> k >> p; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { cin >> a[i][j]; } set<pair<long long, long long> > row, col; for (int i = 0; i < n; i++) { long long sum = 0; for (int j = 0; j < m; j++) { sum += a[i][j]; } row.insert(make_pair(-sum, i)); } for (int j = 0; j < m; j++) { long long sum = 0; for (int i = 0; i < n; i++) { sum += a[i][j]; } col.insert(make_pair(-sum, j)); } long long res = 0; long long min_col = 0, min_row = 0; vector<long long> cl(k + 1), rw(k + 1); for (int z = 0; z < k; z++) { pair<long long, long long> r = *(row.begin()); pair<long long, long long> c = *(col.begin()); cl[z + 1] = cl[z] - c.first; col.erase(col.begin()); col.insert(make_pair(c.first + p * n, c.second)); rw[z + 1] = rw[z] - r.first; row.erase(row.begin()); row.insert(make_pair(r.first + p * m, r.second)); } long long mx = LONG_MIN_VAL; for (int i = 0; i <= k; i++) { mx = max(mx, rw[i] + cl[k - i] - i * (k - i) * p); } cout << mx << '\n'; return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; struct G { int ind[101000], fa[101000]; int e[1010000], last[1010000], t, num[1010000], eg[1010000][2]; bool direction[1010000]; int m = 0, n = 0; int indegree[101000], outdegree[101000]; bool checked[101000]; void addE(int a, int b, int num) { t++; e[t] = b; this->num[t] = num; last[t] = ind[a]; ind[a] = t; } void addEE(int a, int b) { m++; eg[m][0] = a; eg[m][1] = b; direction[m] = false; outdegree[a]++; indegree[b]++; addE(a, b, m); if (a != b) addE(b, a, m); } void solve1() { int i, last = 0; for (i = 1; i <= n; i++) { if ((indegree[i] + outdegree[i]) & 1) { if (last) { addEE(i, last); last = 0; } else { last = i; } } } } int other(int nowe, int a) { if (eg[nowe][0] == a) return eg[nowe][1]; else return eg[nowe][0]; } void join(int a, int b, int root) { int f; while (a != root) { f = fa[a]; direction[f] = !direction[f]; a = other(f, a); } while (b != root) { f = fa[b]; direction[f] = !direction[f]; b = other(f, b); } } int dfs(int a) { if (checked[a]) return 0; checked[a] = true; int i, j, now, last = 0; for (j = ind[a]; j; j = this->last[j]) { i = e[j]; if (checked[i]) continue; fa[i] = num[j]; now = dfs(i); if (now) { if (last) { join(now, last, a); last = 0; } else { last = now; } } } if (indegree[a] & 1) { if (last) { join(a, last, a); last = 0; } else { last = a; } } return last; } void solve2() { int last = dfs(1); if (last) { addEE(last, last); } } void output() { int i; printf("%d\n", m); for (i = 1; i <= m; i++) { if (direction[i]) { printf("%d %d\n", eg[i][0], eg[i][1]); } else { printf("%d %d\n", eg[i][1], eg[i][0]); } } } } p; int main() { int n, m, a, b; scanf("%d %d", &n, &m); p.n = n; while (m--) { scanf("%d %d", &a, &b); p.addEE(a, b); } p.solve1(); p.solve2(); p.output(); return 0; }
2,600
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int v[N]; bool used[N]; void no() { cout << -1; exit(0); } stack<int> s; bool in_s[N]; int bit[N], n; void update(int p, int x) { for (; p <= n; p += p & (-p)) { bit[p] += x; } } int query(int p) { int ret = 0; for (; p; p -= p & (-p)) { ret += bit[p]; } return ret; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int k; cin >> n >> k; for (int i = 1; i <= n; i++) { update(i, 1); } for (int i = 1; i <= k; i++) { cin >> v[i]; used[v[i]] = 1; update(v[i], -1); } int c = 1; for (int i = 1; i <= n; i++) { if (in_s[i] == true) { if (s.empty() == false && s.top() == i) { s.pop(); } else { no(); } } else { while (c <= n && v[c] != i) { if (v[c] == 0) { int lf = 1; int rg, sol, md; int x; if (s.empty() == true) { sol = n; } else { rg = s.top(); sol = rg; x = query(rg); while (lf <= rg) { md = (lf + rg) >> 1; int val = query(md); if (val >= x) { sol = md; rg = md - 1; } else { lf = md + 1; } } } v[c] = sol; update(sol, -1); } else { if (s.empty() == false && s.top() < v[c]) { no(); } } if (v[c] == i) { break; } s.push(v[c]); in_s[v[c]] = true; c++; } if (v[c] != i) { no(); } c++; } } for (int i = 1; i <= n; i++) { cout << v[i] << ' '; } return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; vector<int> t[4 * 100100], ai[2][100100]; int query(int root, int rl, int rr, int a, int b, int x) { if (b < a) return 0; if (a == rl && b == rr) return upper_bound(t[root].begin(), t[root].end(), x) - t[root].begin(); int rm = (rl + rr) / 2; return query(2 * root + 1, rl, rm, a, min(b, rm), x) + query(2 * root + 2, rm + 1, rr, max(a, rm + 1), b, x); } void init(int root, int rl, int rr) { if (rl == rr) t[root] = ai[0][rl]; else { int rm = (rl + rr) / 2; init(2 * root + 1, rl, rm); init(2 * root + 2, rm + 1, rr); t[root] = vector<int>(t[2 * root + 1].size() + t[2 * root + 2].size()); merge(t[2 * root + 1].begin(), t[2 * root + 1].end(), t[2 * root + 2].begin(), t[2 * root + 2].end(), t[root].begin()); } } vector<int> ids[2]; int gxid(int k, int x) { return lower_bound(ids[k].begin(), ids[k].end(), x) - ids[k].begin(); } int s[2][3], qids[2], sai[2][100100]; int v[100100][2], a[10]; bool flines(int k, int &xa, int &xb) { int es0 = 1, di0 = qids[k]; while (di0 > es0) { int me = (es0 + di0) / 2; if (sai[k][me] >= s[k][0]) di0 = me; else es0 = me + 1; } int es1 = es0 + 1, di1 = qids[k]; while (di1 > es1) { int me = (es1 + di1) / 2; if (sai[k][me] - sai[k][es0] >= s[k][1]) di1 = me; else es1 = me + 1; } if (sai[k][es0] != s[k][0] || sai[k][es1] - sai[k][es0] != s[k][1]) return 0; xa = es0 - 1; xb = es1 - 1; return 1; } int main() { int n; while (scanf("%d", &n) == 1) { for (int k = 0; k < (int)2; k++) ids[k].clear(); for (int i = 0; i < (int)n; i++) for (int k = 0; k < (int)2; k++) { scanf("%d", &v[i][k]); ids[k].push_back(v[i][k]); } for (int k = 0; k < (int)2; k++) { sort(ids[k].begin(), ids[k].end()); ids[k].resize(unique(ids[k].begin(), ids[k].end()) - ids[k].begin()); qids[k] = ids[k].size(); } for (int k = 0; k < (int)2; k++) { for (int i = 0; i < (int)qids[k]; i++) ai[k][i].clear(); for (int i = 0; i < (int)n; i++) ai[k][gxid(k, v[i][k])].push_back(gxid(1 - k, v[i][1 - k])); for (int i = 0; i < (int)qids[k]; i++) { sort(ai[k][i].begin(), ai[k][i].end()); sai[k][i + 1] = sai[k][i] + ai[k][i].size(); } } init(0, 0, qids[0] - 1); for (int i = 0; i < (int)9; i++) scanf("%d", &a[i]); sort(a, a + 9); bool ok = 0; do { for (int i = 0; i < (int)3; i++) { s[0][i] = s[1][i] = 0; for (int j = 0; j < (int)3; j++) s[0][i] += a[i + 3 * j]; for (int j = 0; j < (int)3; j++) s[1][i] += a[i * 3 + j]; } if (a[0] == 1 && a[1] == 2 && a[2] == 2 && a[3] == 1 && a[4] == 2 && a[5] == 2 && a[6] == 1 && a[7] == 3) ok = 0; int xa, xb, ya, yb; if (!flines(0, xa, xb) || !flines(1, ya, yb)) continue; int p[6]; p[0] = query(0, 0, qids[0] - 1, 0, xa, ya); p[1] = query(0, 0, qids[0] - 1, xa + 1, xb, ya); p[2] = query(0, 0, qids[0] - 1, xb + 1, qids[0] - 1, ya); p[3] = query(0, 0, qids[0] - 1, 0, xa, yb) - p[0]; p[4] = query(0, 0, qids[0] - 1, xa + 1, xb, yb) - p[1]; p[5] = query(0, 0, qids[0] - 1, xb + 1, qids[0] - 1, yb) - p[2]; ok = 1; for (int i = 0; i < (int)6; i++) if (p[i] != a[i]) ok = 0; if (ok) { printf("%lf %lf\n%lf %lf\n", ids[0][xa] + 0.5, ids[0][xb] + 0.5, ids[1][ya] + 0.5, ids[1][yb] + 0.5); break; } } while (next_permutation(a, a + 9)); if (!ok) printf("-1\n"); } return 0; }
2,500
CPP
def f(x,y): return x&~y t = int(input()) for i in range(t): n = int(input()) a = list(map(int,input().strip().split())) b = [0 for i in range(n)] for i in range(1,n): b[i]=f(b[i-1]^a[i-1],a[i]) for x in b: print(x,end=" ") print()
1,300
PYTHON3
t = input().split() a = int(t[1]) #a = int(input()) s = input() l = [] for i in range(0,len(s)) : l.append(s[i]) while a != 0 : i = 0 #for i in range(0,len(l)-1) : while i < len(l)-1 : if l[i] == "B" and l[i+1] == "G" : l[i],l[i+1] = l[i+1],l[i] i += 1 i +=1 a -=1 new_str = "" new_str +=new_str.join(l) print(new_str)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main(void) { int n, k; cin >> n >> k; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i == j) cout << k << ' '; else cout << 0 << ' '; } cout << endl; } return 0; }
800
CPP
q = int(input()) for _ in range(q): a = list(map(int, input().split())) a.sort() d = a[1] - a[0] a[2] -= d print(a[1] + a[2] // 2)
800
PYTHON3
for _ in range(int(input())): x,y,z = map(int, input().split()) if(x==y and y==z and z==x): print("YES") print(x,x,x) elif(x==y): if(z>y): print("NO") else: print("YES") print(x,z,z) elif(x==z): if(z<y): print("NO") else: print("YES") print(x,y,y) elif(z==y): if(x>y): print("NO") else: print("YES") print(x,x,z) else: print("NO")
800
PYTHON3
n = int(input()) a = list(map(int, input().split())) i = n-1 while i and a[i]>a[i-1]: i= i-1 print(i)
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n >> n; int r = 0; for (int i = 0; i < n; i++) { int m; scanf("%d", &m); int a[m]; bool bo = 0; for (int j = 0; j < m; j++) { scanf("%d", &a[j]); for (int k = 0; k < j; k++) { if ((a[j] + a[k]) == 0) { bo = 1; } } } r += bo; } if (r != n) { cout << "YES"; } else { cout << "NO"; } }
1,300
CPP
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353; const int maxn = 1e3 + 5; int n, k; long long dp[maxn][maxn]; int m = 1e5; int a[maxn]; long long suf[maxn][maxn]; int main() { scanf("%d%d", &n, &k); int mn = m / (k - 1); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sort(a + 1, a + n + 1); a[0] = -1e9; long long ans = 0; for (int w = 1; w <= mn; w++) { int lb = 0; suf[0][0] = 1; for (int j = 1; j <= k; j++) suf[0][j] = 0; for (int i = 1; i <= n; i++) { while (a[i] - a[lb] >= w) lb++; lb--; suf[i][0] = 1; for (int j = 1; j <= k; j++) { suf[i][j] = suf[lb][j - 1] + suf[i - 1][j], suf[i][j] %= mod; } } ans += suf[n][k]; ans %= mod; } printf("%lld\n", ans); return 0; }
2,500
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 200100; long long a[maxn]; int main() { int n; cin >> n; long long MIN = 1e10; ; long long last, dic, fir; last = -1; dic = 0; for (int i = 0; i < n; i++) cin >> a[i]; MIN = *min_element(a, a + n); for (int i = 0; i < n; i++) { if (MIN == a[i]) { if (last == -1) fir = i; dic = max(dic, i - last - 1); last = i; } } dic = max(dic, fir + n - last - 1); cout << dic + n * MIN << endl; return 0; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; int greedy1(vector<int> atk, vector<int> myAtk) { vector<int>::iterator it1 = atk.begin(); vector<int>::reverse_iterator it2 = myAtk.rbegin(); int res = 0; while (1) { if (it1 == atk.end() || it2 == myAtk.rend() || *it1 >= *it2) break; res += *it2 - *it1; it1++; it2++; } return res; } int greedy2(vector<int> atk, vector<int> def, vector<int> myAtk) { int sumMyAtk = accumulate(myAtk.begin(), myAtk.end(), 0); bool vis[101] = {0}; for (int i = 0; i < (int)def.size(); ++i) { bool flag = false; for (int j = 0; j < myAtk.size(); ++j) { if (vis[j] == 0 && def[i] < myAtk[j]) { vis[j] = 1; sumMyAtk -= myAtk[j]; flag = 1; break; } } if (flag == false) return 0; } for (int i = 0; i < (int)atk.size(); ++i) { bool flag = false; for (int j = 0; j < myAtk.size(); ++j) { if (!vis[j] && atk[i] <= myAtk[j]) { vis[j] = 1; flag = 1; break; } } if (flag == false) return 0; } return sumMyAtk - accumulate(atk.begin(), atk.end(), 0); } int main() { int n, m, t; string s; vector<int> atk, def, myAtk; cin >> n >> m; for (int i = 0; i < n; ++i) { cin >> s >> t; if (s[0] == 'A') atk.push_back(t); else def.push_back(t); } for (int i = 0; i < m; ++i) { cin >> t; myAtk.push_back(t); } sort(atk.begin(), atk.end()); sort(def.begin(), def.end()); sort(myAtk.begin(), myAtk.end()); cout << max(greedy1(atk, myAtk), greedy2(atk, def, myAtk)) << endl; return 0; }
1,900
CPP
l=int(input()) n=list(input()) m=set() for i in range(len(n)): n[i]=n[i].lower() m.add(n[i]) if len(m) == 26: print("Yes") else: print("No")
800
PYTHON3
n = int(input()) + 1 a = [0] + list(map(int, input().split())) ans = 0 power = 0 for i in range(n - 1): if a[i] < a[i + 1]: delta = a[i + 1]-a[i] dd = min(delta, power) delta -= dd power -= dd ans+=delta else: power+=a[i]-a[i+1] print(ans)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int n, m; int a[N]; vector<int> b[N]; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; ++i) { scanf("%d", &a[i]); if (i > 0 && a[i] != a[i - 1]) { b[a[i]].push_back(a[i - 1]); b[a[i - 1]].push_back(a[i]); } } long long sum = 0; for (int i = 1; i < m; ++i) { sum += abs(a[i] - a[i - 1]); } long long ans = sum; for (int it = 1; it <= n; ++it) { vector<int> vec = b[it]; if (vec.empty()) continue; sort(vec.begin(), vec.end()); int x = vec[vec.size() / 2]; if (x > n) x = n; long long ret = sum; for (int i = 0; i < vec.size(); ++i) { ret += abs(vec[i] - x) - abs(vec[i] - it); } ans = min(ans, ret); } printf("%I64d\n", ans); }
1,800
CPP
import sys LI=lambda:list(map(int, sys.stdin.readline().strip('\n').split())) MI=lambda:map(int, sys.stdin.readline().strip('\n').split()) SI=lambda:sys.stdin.readline().strip('\n') II=lambda:int(sys.stdin.readline().strip('\n')) n, m=MI() q=LI() d={v:1 for v in q} ans=[] i, cnt, dist=0, 0, 0 while cnt<m: u=q[i] if d.get(u-1, 0)==0: ans.append(u-1) d[u-1]=d[u]+1 cnt+=1 dist+=d[u-1]-1 q.append(u-1) if cnt>=m: break if d.get(u+1, 0)==0: ans.append(u+1) d[u+1]=d[u]+1 cnt+=1 dist+=d[u+1]-1 q.append(u+1) i+=1 print(dist) print(*ans)
1,800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { string s[3]; s[0] += "qwertyuiop"; s[1] += "asdfghjkl;"; s[2] += "zxcvbnm,./"; string ss; char t; cin >> t; cin >> ss; if (t == 'R') { for (int i = 0; i < ss.size(); i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 10; k++) { if (ss[i] == s[j][k]) { cout << s[j][k - 1]; } } } } cout << endl; } else { for (int i = 0; i < ss.size(); i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 10; k++) { if (ss[i] == s[j][k]) cout << s[j][k + 1]; } } } cout << endl; } }
900
CPP
#include <bits/stdc++.h> using namespace std; int n, k; int f[1 << 11]; void input() { scanf("%d %d", &n, &k); int i; for (i = 1; i <= n; i++) scanf("%d", &f[i]); sort(f + 1, f + 1 + n); } int recurse(int pos) { if (pos <= 0) return 0; return f[pos] + recurse(pos - k) - 1; } void solve() { printf("%d\n", 2 * recurse(n)); } int main() { input(); solve(); return 0; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > compus; long long money[128], probs[128]; long long DP[(1LL << 20LL) + 1]; int main() { long long n, m, b; cin >> n >> m >> b; compus.resize(n); for (int i = 0; i < n; i++) { int s; cin >> money[i] >> compus[i].first >> s; compus[i].second = i; for (int j = 0; j < s; j++) { long long a; cin >> a; a--; probs[i] |= (1LL << a); } } sort(compus.begin(), compus.end()); memset(DP, 0x3f, sizeof(DP)); long long tb = DP[0]; DP[0] = 0; for (int i = n - 1; i >= 0; i--) { int cur = compus[i].second; long long cost = b * compus[i].first + money[cur]; for (long long mask = 0; mask < (1LL << m); mask++) { DP[mask | probs[cur]] = min(DP[mask | probs[cur]], DP[mask] + cost); if (mask == 0) cost -= b * compus[i].first; } } long long goal = (1LL << m) - 1LL; if (DP[goal] == tb) cout << -1 << endl; else cout << DP[goal] << endl; }
1,900
CPP
n = int(input()) count = 0 for i in range(1, n): a=n-i if (a%i == 0): count = count+1 else: continue print(count)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7, N = 1e5; int main() { int Arr[N + 1]; Arr[0] = 1; Arr[1] = 1; Arr[2] = 2; for (int i = 3; i < N + 1; i++) { Arr[i] = (Arr[i - 1] + Arr[i - 2]) % mod; } string s; cin >> s; int n = s.length(); int p = 0, t = 0; long long Ans = 1; for (int i = 0; i < n; i++) { if (s[i] == 'm' || s[i] == 'w') { cout << 0; return 0; } if (s[i] == 'u') p++; else { Ans = (Ans * Arr[p]) % mod; p = 0; } if (s[i] == 'n') { t++; } else { Ans = (Ans * Arr[t]) % mod; t = 0; } } Ans = (Ans * Arr[p]) % mod; Ans = (Ans * Arr[t]) % mod; cout << Ans; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int i, j, k; int n, m, t; long long x, y; cin >> n >> m >> k >> t; set<long long> s; vector<long long> vec; while (k--) { cin >> x >> y; x--; vec.push_back(x * m + y); } sort(vec.begin(), vec.end()); long long pos; while (t--) { cin >> x >> y; x--; pos = x * m + y; k = 0, j = 0; for (int e : vec) { if (e < pos) k++; else if (e == pos) j = 1; else break; } pos -= k; pos = pos % 3; if (j) cout << "Waste\n"; else if (pos == 1) cout << "Carrots\n"; else if (pos == 2) cout << "Kiwis\n"; else cout << "Grapes\n"; } return 0; }
1,400
CPP
#include <bits/stdc++.h> struct succ { int c, d; }; using namespace std; int f[8] = {2, 3, 4, 6, 12}; int main() { int t; cin >> t; cin.get(); while (t--) { int a[15] = {0}, flag = 0; string s; vector<succ> v; getline(cin, s); for (int i = 0; i < 12; i++) if (s[i] == 'X') { a[i + 1] = 1; flag = 1; } succ p; if (flag) { p.c = 1; p.d = 12; v.push_back(p); } for (int i = 0; i < 5; i++) { flag = 0; int k = 12 / f[i], total; for (int j = 1; j <= k; j++) { total = 1; if (a[j] == 1) for (int e = j + k; e <= 12; e += k) if (a[e] == 1) total++; if (total == f[i] && a[j] == 1) flag = 1; } if (flag) { p.c = f[i]; p.d = 12 / p.c; v.push_back(p); } } int vol = v.size(); cout << vol; for (int i = 0; i < vol; i++) { cout << ' ' << v[i].c << 'x' << v[i].d; } cout << endl; } return 0; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; struct node { int pos, h, ans, id, f; } Do[100001]; int n; int cmd(node a, node b) { return a.pos < b.pos; } int cmd2(node a, node b) { return a.id < b.id; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d%d", &Do[i].pos, &Do[i].h); Do[i].id = i; } sort(Do + 1, Do + n + 1, cmd); Do[n].ans = 1; Do[n].f = n; for (int i = n - 1; i >= 1; i--) { Do[i].ans = 1; Do[i].f = i; int next = i + 1; if (Do[i].pos + Do[i].h - 1 < Do[i + 1].pos) continue; while (next <= n) { if (Do[Do[next].f + 1].pos > Do[i].pos + Do[i].h - 1) { Do[i].ans += Do[next].ans; Do[i].f = Do[next].f; break; } else { Do[i].ans += Do[next].ans; Do[i].f = Do[next].f; next = Do[next].f + 1; } } } sort(Do + 1, Do + n + 1, cmd2); for (int i = 1; i <= n; i++) printf("%d ", Do[i].ans); }
2,200
CPP
def lucky(n): s = {4,7,44,77,47,74,444,777,447,474,744,774,747,477} for x in s: if not n%x: return True return False n = int(input()) if lucky(n): print('YES') else: print('NO')
1,000
PYTHON3
n, m = map(int, input().split()) maximum = 10 ** 5 + 3 prime = [True] * (maximum + 1) prime[0], prime[1] = False, False for i in range(2, maximum + 1): if (prime[i]): for j in range(i + i, maximum + 1, i): prime[j] = False matrix = [] sums = [[0] * n, [0] * m] ans = 1e9 + 7 for i in range(n): row = [int(i) for i in input().split()] new_row = [] for j in range(m): num = row[j] add = 0 while ((num <= maximum) and (not prime[num])): add += 1 num += 1 new_row.append(add) sums[0][i] += add sums[1][j] += add matrix.append(new_row) ans = min(ans, sums[0][i]) for i in range(m): ans = min(ans, sums[1][i]) print (ans)
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, m, k; cin >> n >> m >> k; int a[1001]; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] == 0) { a[i] = 1e6; } } int id1 = -1; for (int i = m - 1; i >= 1; i--) { if (k >= a[i]) { id1 = i; break; } } int id2 = -1; for (int i = m + 1; i <= n; i++) { if (k >= a[i]) { id2 = i; break; } } int ans = 1e6; if (id1 != -1) { ans = min(ans, m - id1); } if (id2 != -1) { ans = min(ans, id2 - m); } cout << ans * 10; }
800
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1000000 + 10; int a[N]; int n; int l[N]; map<int, int> M; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", a + i); } for (int i = 1; i <= n; ++i) { if (M.count(a[i])) { l[i] = M[a[i]]; } M[a[i]] = i; } long long cnt = 0, pre = 0; for (int i = 1; i <= n; ++i) { cnt += pre + i - l[i] - 1; pre += i - l[i]; } cnt = cnt * 2 + n; printf("%.8lf\n", cnt * 1.0 / n / n); }
1,800
CPP
#include <bits/stdc++.h> using namespace std; int read() { int x = 0, f = 1; char ch = getchar(); while (!isdigit(ch) && ch != '-') ch = getchar(); if (ch == '-') f = -1, ch = getchar(); while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar(); return x * f; } void write(long long x) { if (x == 0) { putchar('0'), putchar(' '); return; } int f = 0; char ch[20]; if (x < 0) putchar('-'), x = -x; while (x) ch[++f] = x % 10 + '0', x /= 10; while (f) putchar(ch[f--]); putchar(' '); return; } int n, tr[100007]; long long t[100007]; long long p, ans[100007], Q[100007], hd = 1, tl; pair<long long, long long> a[100007]; priority_queue<int> q; int lt(int x) { return x & (-x); } void add(int x, int k) { for (; x <= n; x += lt(x)) tr[x] += k; return; } int ask(int x) { int k = 0; for (; x; x -= lt(x)) k += tr[x]; return k; } int main() { n = read(), p = read(); for (register int i = (1); i <= (n); ++i) t[i] = read(), a[i].first = t[i], a[i].second = i; sort(a + 1, a + n + 1); long long start = 0, tim = 0, now = 1; for (register int i = (1); i <= (n); ++i) { while (now <= n && a[now].first <= tim) { if (a[now].second < start && !ask(a[now].second)) add(a[now].second, 1), Q[++tl] = now; else q.push(-a[now].second); now++; } if (hd <= tl) add(a[Q[hd]].second, -1), start = a[Q[hd]].second, hd++; else if (!q.empty()) start = -q.top(), q.pop(); else { start = a[now].second, tim = a[now].first, now++; } tim += p, ans[start] = tim; } for (register int i = (1); i <= (n); ++i) write(ans[i]); return 0; }
2,300
CPP
# -*- coding: utf-8 -*- """ Created on Mon Dec 24 10:48:57 2018 @author: piyus """ n = int(input()) l = list(map(int,input().split(' '))) from collections import Counter count = Counter(l) lim = max(l) arr = [0]*(lim+1) arr[1] = count[1]*1 for i in range(2,lim+1): arr[i] = max(arr[i-1],arr[i-2] + count[i]*i) print(arr[lim])
1,500
PYTHON3
w = int(input()) possible = 'NO' for p in range(2, w, 2): if (w - p) % 2 == 0: possible = 'YES' break print(possible)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int arr[3000000]; int main() { int n, q; scanf("%d %d", &n, &q); int p1, p2; p1 = 1; p2 = 2; while (q--) { int a, b; scanf("%d", &a); if (a == 1) { scanf("%d", &b); b += n; p1 += b; p1 = p1 % n; if (p1 == 0) p1 = n; p2 += b; p2 = p2 % n; if (p2 == 0) p2 = n; continue; } if (p1 % 2 == 0) p1--; else { p1++; p1 = p1 % n; if (p1 == 0) p1 = n; } if (p2 % 2 == 0) p2--; else { p2++; p2 = p2 % n; if (p2 == 0) p2 = n; } } int pre = p1; arr[pre] = 1; for (int i = 3; i <= n; i += 2) { pre += 2; pre = pre % n; if (pre == 0) pre = n; arr[pre] = i; } pre = p2; arr[pre] = 2; for (int i = 4; i <= n; i += 2) { pre += 2; pre = pre % n; if (pre == 0) pre = n; arr[pre] = i; } for (int i = 1; i <= n; i++) { printf("%d ", arr[i]); } return 0; }
1,800
CPP
for _ in range(int(input())): n,x=map(int,input().split()) a=list(map(int,input().split())) b=-1 for i in range(n): if(a[i]%x)!=0: b=i break if b==-1: print(-1) else: s=0 ans=1 for i in range(n): s+=a[i] if((s%x)==0): ans=max(i-b,ans) else: ans=i+1 print(ans)
1,200
PYTHON3
x = input() ex=[-1]*len(x);m=0; f=[0]*len(x);ans1=-1;s=[] for index,i in enumerate(x): if i=='(': s.append(index) else: if len(s) > 0 : p = s.pop() ex[index] = p if p > 0 and ex[p-1] > -1 : ex[index] = ex[p-1] f[index-ex[index]]+=1 ans1 = max(ans1,index-ex[index]) print(ans1+1,max(1,f[ans1]))
1,900
PYTHON3
for _ in range(int(input())): a, b, c = map(int, input().split()) print(max(a,b,c)+min(a,b,c))
800
PYTHON3
N = int(input()) x = 0 for i in range(0,N): oper = input() if '++' in oper: x += 1 if '--' in oper: x -= 1 print(x)
800
PYTHON3
n,d=map(int,input().split()) arr=list(map(int,input().split())) c=0 for i in range(n): for j in range(i+1,n): if abs(arr[i]-arr[j])<=d: c+=2 print(c)
800
PYTHON3
#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); vector<long long> p2, p3; for (long long i = 1; i < 2E9 + 10; i *= 2) p2.emplace_back(i); for (long long i = 1; i < 2E9 + 10; i *= 3) p3.emplace_back(i); vector<int> a; for (auto& u : p2) for (auto& v : p3) { long long z = u * v; if (z > 2E9) break; a.emplace_back(z); } sort(begin(a), end(a)); int l, r; re(l, r); ps(upper_bound(begin(a), end(a), r) - lower_bound(begin(a), end(a), l)); }
1,300
CPP
#include <bits/stdc++.h> using namespace std; map<long long, int> mp; set<long long> s; int main(void) { int n, m; cin >> n >> m; string arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } for (int i = 0; i < n; i++) { int cont = 0; for (int j = 0; j < m; j++) { if (arr[i][j] == '*') cont++; } if (cont == 1) { cout << i + 1 << " "; break; } } for (int i = 0; i < m; i++) { int cont = 0; for (int j = 0; j < n; j++) { if (arr[j][i] == '*') cont++; } if (cont == 1) { cout << i + 1 << endl; break; } } return 0; }
800
CPP
input() t = list(sum(int(i) for i in j) % 3 for j in input().split()) p = [0] * 3 for i in t: p[i] += 1 print(p[0] // 2 + min(p[1], p[2]))
1,300
PYTHON3
###### ### ####### ####### ## # ##### ### ##### # # # # # # # # # # # # # ### # # # # # # # # # # # # # ### ###### ######### # # # # # # ######### # ###### ######### # # # # # # ######### # # # # # # # # # # # #### # # # # # # # # # # ## # # # # # ###### # # ####### ####### # # ##### # # # # from __future__ import print_function # for PyPy2 # from itertools import permutations # from functools import cmp_to_key # for adding custom comparator # from fractions import Fraction from collections import * from sys import stdin # from bisect import * from heapq import * from math import * g = lambda : stdin.readline().strip() gl = lambda : g().split() gil = lambda : [int(var) for var in gl()] gfl = lambda : [float(var) for var in gl()] gcl = lambda : list(g()) gbs = lambda : [int(var) for var in g()] rr = lambda x : reversed(range(x)) mod = int(1e9)+7 inf = float("inf") n, = gil() a = gil() a.sort() length = [] ans = 0 # 10 # 123 124 123 124 2 2 2 2 9 9 while len(a) > 1: if a.pop() - a[-1] <= 1: length.append(a.pop()) for i in range(0, len(length), 2): if i+1 < len(length): ans += length[i]*length[i+1] print(ans)
1,600
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int a[100100], n; scanf("%d", &n); int ans = 0; memset(a, -1, sizeof(a)); int flag = 0; int ok = 1; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (i == a[i]) ans++; else if (a[a[i]] == i && flag != 1) { ok = 0; ans += 2; flag = 1; } if (a[i] != i && ok) flag = 2; } if (flag == 2 && ok) ans++; printf("%d\n", ans); return 0; }
1,100
CPP
n = int(input()) for i in range(n): s = input() l = len(s) if l > 10: print(s[0], end="") print(l - 2, end="") print(s[-1]) else: print(s)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; if (n & 1) { cout << -1; return; } vector<bool> trk(n); vector<int> euler; function<void(int)> dfs = [&](int u) { if (trk[u]) return; trk[u] = 1; dfs((u << 1) % n); dfs((u << 1 | 1) % n); euler.push_back(u); }; dfs(0); reverse(euler.begin(), euler.end()); euler.push_back(0); for (auto& x : euler) { cout << x << ' '; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
2,800
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, d; cin >> n >> d; vector<int> arr; arr.push_back(0); for (int i = 0; i < d; i++) { int temp; cin >> temp; arr.push_back(temp); } arr.push_back(n + 1); sort(arr.begin() + 1, arr.end() - 1); if (arr[1] == 1 || arr[d] == n) { cout << "NO\n"; return 0; } vector<pair<int, int>> c; for (int i = 0; i < arr.size() - 1; i++) { if (arr[i + 1] - arr[i] - 1 > 0) { c.push_back({arr[i] + 1, arr[i + 1] - 1}); } } for (int i = 0; i < c.size() - 1; i++) { if ((c[i + 1].first - c[i].second - 1) > 2) { cout << "NO\n"; return 0; } } cout << "YES\n"; return 0; }
1,100
CPP
n=int(input()) a=list(map(int,input().split())) b=list(map(int,input().split())) mark=[0]*(n+1) cnt1=0 cnt2=0 while cnt2<n: if mark[b[cnt2]]==1: print(0,end=" ") else: count=1 while True : if a[cnt1]==b[cnt2]: mark[b[cnt2]]=1 cnt1+=1 print(count,end=" ") break else: mark[a[cnt1]]=1 count+=1 cnt1+=1 cnt2+=1
1,000
PYTHON3
#include <bits/stdc++.h> const long long INF = 4e18L + 1; const int IINF = 2e9 + 1; const int limit = 1048576; using namespace std; int main() { ios_base::sync_with_stdio(0); string num; cin >> num; if (num[0] == '.') { num.insert(0, "0"); } int dot = num.find("."); if (dot == int(string::npos)) { num.push_back('.'); dot = num.size() - 1; } if (0) cout << "dot " << dot << "\n"; int sig = num.find_first_of("123456789"); if (0) cout << "sig " << sig << "\n"; int exp = (dot > sig) ? dot - (sig + 1) : dot - sig; if (0) cout << "exp " << exp << "\n"; num.erase(dot, 1); if (0) cout << "num after erase " << num << "\n"; sig = num.find_first_of("123456789"); if (0) cout << "new sig " << sig << "\n"; num.insert(sig + 1, "."); if (0) cout << "num after insert " << num << "\n"; int last_sig = num.find_last_not_of("0."); if (0) cout << "last_sig " << last_sig << "\n"; cout << num.substr(sig, last_sig - sig + 1); if (exp != 0) { cout << "E" << exp << "\n"; } else { cout << "\n"; } return 0; }
1,800
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; for (int i = n; i > n - k; i--) { cout << i << " "; } for (int i = 1; i <= n - k; i++) { cout << i << " "; } cout << endl; }
1,100
CPP
from sys import stdin input=lambda : stdin.readline().strip() lin=lambda :list(map(int,input().split())) iin=lambda :int(input()) main=lambda :map(int,input().split()) from math import ceil,sqrt,factorial,log from collections import deque from bisect import bisect_left def gcd(a,b): a,b=max(a,b),min(a,b) while a%b!=0: a,b=b,a%b return b mod=998244353 def solve(we): l=lin() l.sort() if l[-1]==l[-2]: print(l[0]) else: print(ceil(sqrt(l[0]*l[0]+(l[-1]-l[-2])**2))) qwe=1 qwe=int(input()) for _ in range(qwe): solve(_+1)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using ppi = pair<int, pii>; using vpi = vector<pii>; const int kMod = 1e9 + 7; struct ModInt { long long n; ModInt(long long n = 0) : n(n % kMod) {} ModInt operator+(const ModInt& oth) { return n + oth.n; } ModInt operator-(const ModInt& oth) { return n - oth.n; } ModInt operator*(const ModInt& oth) { return n * oth.n; } long long get() { return n < 0 ? n + kMod : n; } }; using vi = vector<ModInt>; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll R, G, val = 0, n = 0; cin >> R >> G; while (val <= R + G) val += ++n; n--; cerr << (int)(1000 * ((double)clock()) / CLOCKS_PER_SEC) << endl; vector<ll> dp(R + 1, 0); dp[0] = 1; ll ans = 0; for (int i = 1; i <= n; i++) { vector<ll> newdp(R + 1, 0); int tmp = (1LL * i * (i + 1)) / 2; for (int r = 0; r <= R; r++) { int g = tmp - r; if (r >= i) newdp[r] = dp[r - i]; if (g <= G) newdp[r] = (newdp[r] + dp[r]) % kMod; } dp.swap(newdp); } for (int r = 0; r <= R; r++) ans = (ans + dp[r]) % kMod; cerr << (int)(1000 * ((double)clock()) / CLOCKS_PER_SEC) << endl; cout << ans << '\n'; }
2,000
CPP
import sys import math def getAndParseInt(num=1): string = (sys.stdin.readline()).strip() if num==1: return int(string) else: return [int(part) for part in string.split()] def getAndParseString(num=1,delim=" "): string = (sys.stdin.readline()).strip() if num==1: return string else: return [part for part in string.split(delim)] def findSol(k,n,a,b): start = 0 end = n mid = 0 while start<=end: mid = (start+end)//2 total = mid*a + (n-mid)*b if total >= k: end = mid -1 elif (mid ==n) or ((mid+1)*a + b*(n - mid - 1) >=k): break else: start = mid+1 if mid*a + (n-mid)*b < k: return mid else: return -1 q = getAndParseInt() for i in range(q): k,n,a,b = getAndParseInt(2) print(findSol(k,n,a,b))
1,400
PYTHON3
from math import * n=int(input()) for j in range(n): ab=input().split() a=int(ab[0]) b=int(ab[1]) y = int(sqrt(a)) u = min(y, b) q = [] w = [] for i in range(1, u + 1): if a % i == 0: x = i q.append(x) m = a // i if m <= b: w.append(m) if len(w) > 0: ans = max(max(q), max(w)) else: ans = max(q) print(a // ans)
1,300
PYTHON3
s = str(input()) ab = s.find("AB") ba = s.find("BA") if ab != -1 and s.find("BA", ab+2) != -1 or ba != -1 and s.find("AB", ba+2) != -1: print("YES") else: print("NO")
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int a; int ans = 0; cin >> a; vector<int> b(6); vector<int> c{4, 1, 3, 2, 0, 5}; for (int i = 0; i < 6; i++) { b[i] = (a >> i) & 1; if (b[i]) ans += (1 << c[i]); } cout << ans; }
0
CPP
total,contestent = input().split() values = [int(i) for i in input().split()] counter = 0 for i in range(int(total)): if values[i] >= values[int(contestent)-1]: if values[i] > 0: counter += 1 print(counter)
800
PYTHON3
n = int(input()) counter = 0 for i in range(n): a, b, c = [int(s) for s in input().split(' ')] if a + b + c >= 2: counter += 1 print(counter)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, m; scanf("%d", &(n)), scanf("%d", &(m)); int a[(100000 + 10)][2]; for (int i(0), _n(n); i < _n; ++i) { scanf("%d", &a[i][1]); if (a[i][1] > 0) a[i][0] = '+'; else a[i][0] = '-', a[i][1] = -a[i][1]; } int yes[(100000 + 10)], no[(100000 + 10)]; memset((yes), 0, sizeof(yes)); memset((no), 0, sizeof(no)); for (int i(0), _n(n); i < _n; ++i) { if (a[i][0] == '+') yes[a[i][1]]++; else no[a[i][1]]++; } int sno = 0; for (int i(1), _h(n); i <= _h; ++i) sno += no[i]; bool may[(100000 + 10)]; memset((may), false, sizeof(may)); int cnt = 0; for (int i(1), _h(n); i <= _h; ++i) if (yes[i] + sno - no[i] == m) { may[i] = true; cnt++; } for (int i(0), _n(n); i < _n; ++i) { if (a[i][0] == '+') { if (may[a[i][1]] && cnt == 1) puts("Truth"); else if (may[a[i][1]]) puts("Not defined"); else puts("Lie"); } else { if (may[a[i][1]] && cnt == 1) puts("Lie"); else if (may[a[i][1]]) puts("Not defined"); else puts("Truth"); } } return 0; }
1,600
CPP
import math n, h = map(int, input().strip().split()) area = (h)/(2*n) rezi = [] alfa = math.atan(2*h) spodnja = 1 visina = h for _ in range(n-1): m = 0 M = visina while abs(M-m) > 10**-10: sred = (M+m) / 2 ploscina = spodnja*visina/2 d = sred/(2*h) novi_a = spodnja - 2*d novi_visina = visina - sred p2 = ploscina - novi_a*novi_visina/2 if p2 > area: M = sred else: m = sred rezi.append(sred) spodnja = novi_a visina = novi_visina res = [] acc = 0 for e in rezi: acc += e res.append(h-acc) res = res[::-1] print(' '.join(map(str,res)))
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; int n; for (int i = 0; i < t; i++) { cin >> n; for (int i = 1; i <= n; i++) { cout << 5 << " "; } cout << endl; } return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; template <class T> inline void read(T& x) { x = 0; char ch = getchar(); while (ch < '0' || ch > '9') { ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } } const int N = 310; int a[N]; int book[N]; int n, p; int h(int x) { return x % p; } int main() { read(p); read(n); for (int i = 1; i <= n; i++) { read(a[i]); } for (int i = 1; i <= n; i++) { int pos = h(a[i]); if (book[pos] == 1) { printf("%d\n", i); return 0; } else { book[pos] = 1; } } puts("-1"); return 0; }
800
CPP
n, m = map(int,input().split()) for i in range(m): if n%10 == 0 : n = (n/10) else : n = n -1 print(int(n))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; int main() { string s; cin >> s; int ans = 0, b = 0, n = s.length(); for (register int i = n - 1; i >= 0; --i) if (s[i] == 'a') { ans = (ans + b) % MOD; b = (b << 1) % MOD; } else if (s[i] == 'b') ++b; cout << ans << endl; return 0; }
1,400
CPP
import sys S , N = map(int,input().split()) dragons =[] for i in range (N): dragons.append(list(map(int,input().split()))) dragons.sort() for k in range (N): if S > dragons[k][0]: S = S + dragons[k][1] else: print("NO") sys.exit() print("YES")
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 300100; const int M = 1000100; const int K = 200100; const int oo = 2e9; const long long OO = 2e18; const int PW = 20; const int md = 1000000007; int tp[N], q, BLOCK, tt, siz, TT, mrk[N], vc[N], vec[N]; long long x[N], y[N], vl[N], fr[N], X[N], Y[N]; multiset<pair<int, int> > st; inline long long divide(long long tp, long long a, long long b) { if (b == 0) { if (tp >= 0) return oo; else return -oo; } long long res = a / b, ost = a % b; if ((a <= 0 && b < 0) || (a >= 0 && b > 0)) return res + bool(ost); else return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> q; st.clear(); for (int i = 0; i < q; i++) { cin >> tp[i]; mrk[i] = oo; if (tp[i] == 1) cin >> x[i] >> y[i]; else cin >> vl[i]; } BLOCK = 2500; for (int it = 0; it < q; it += BLOCK) { int l = it, r = min(it + BLOCK, q); TT = 0; for (int i = l; i < r; i++) if (tp[i] == 2) { int nm = vl[i] - 1; mrk[nm] = i; vec[TT++] = nm; if (nm < l) st.erase(st.find(make_pair(x[nm], y[nm]))); } if (TT > 0) reverse(vec, vec + TT); siz = 0; for (auto cr : st) { if (!siz) { siz = 1; fr[1] = -OO; X[1] = cr.first; Y[1] = cr.second; } else { long long CX = cr.first, CY = cr.second; while (siz > 1) { long long pos = divide(CX, CY - Y[siz - 1], X[siz - 1] - CX); if (pos > fr[siz]) break; siz--; } siz++; fr[siz] = divide(CX, CY - Y[siz - 1], X[siz - 1] - CX); X[siz] = CX; Y[siz] = CY; } } tt = 0; for (int i = l; i < r; i++) if (tp[i] == 1) { vc[tt++] = i; if (mrk[i] == oo) st.emplace(x[i], y[i]); } else if (tp[i] == 3) { long long res = -OO; bool was = 0; if (siz > 0) { was = 1; int ps = upper_bound(fr + 1, fr + siz + 1, vl[i]) - fr - 1; res = max(res, X[ps] * vl[i] + Y[ps]); } for (int j = 0; j < tt; j++) { if (mrk[vc[j]] > i) { was = 1; res = max(res, x[vc[j]] * vl[i] + y[vc[j]]); } } for (int j = 0; j < TT; j++) { if (vec[j] < i) { was = 1; res = max(res, x[vec[j]] * vl[i] + y[vec[j]]); } } if (!was) cout << "EMPTY SET\n"; else cout << res << '\n'; } else TT--; } return 0; }
2,500
CPP
import os import sys from io import BytesIO, IOBase def solution(n): if ((n * (n + 1)) // 2) & 1 == 1: write('NO') return first = list(range(2, n + 1, 2)) second = list(range(1, n, 2)) sum_f = sum(first) sum_s = sum(second) while sum_f >= sum_s: second[-1] += 2 sum_s += 2 if sum_s + 2 > sum_f: break write("YES") write(*(first + second)) def main(): for _ in range(r_int()): n = r_int() solution(n) # fast-io region BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) def input(): return sys.stdin.readline().rstrip("\r\n") def write(*args, end='\n'): for x in args: sys.stdout.write(str(x) + ' ') sys.stdout.write(end) def r_array(): return [int(x) for x in input().split()] def r_int(): return int(input()) if __name__ == "__main__": main()
800
PYTHON3
#include <bits/stdc++.h> int MOD = int(1e9) + 7; inline int add(int a, int b) { return (a + b >= MOD) ? (a + b - MOD) : (a + b); } inline void inc(int &a, int b) { a = add(a, b); } inline int sub(int a, int b) { return (a - b < 0) ? (a - b + MOD) : (a - b); } inline void dec(int &a, int b) { a = sub(a, b); } inline int mul(int a, int b) { return (a * 1ll * b) % MOD; } using namespace std; int n, q; vector<pair<int, int> > L, R; int whereL[30013], whereR[30013]; long long cost[30013][2]; long long dp[30013]; const long long INF = -3.1e16; inline long long match(int a, int b) { if (a <= 0 || b <= 0 || a > n || b > n) return INF; return (long long)L[a].first * R[b].first; } inline void update(int i) { if (i <= 0 || i > n) return; cost[i][0] = match(i, i + 1) + match(i + 1, i); cost[i][1] = max(match(i, i + 1) + match(i + 1, i + 2) + match(i + 2, i), match(i, i + 2) + match(i + 1, i) + match(i + 2, i + 1)); } int main() { scanf("%d%d", &n, &q); for (int i = 0; i <= n; i++) { L.push_back(make_pair(0, i)); R.push_back(make_pair(0, i)); } for (int i = 1; i <= n; i++) scanf("%d", &L[i].first); for (int i = 1; i <= n; i++) scanf("%d", &R[i].first); sort(L.begin(), L.end()); sort(R.begin(), R.end()); for (int i = 1; i <= n; i++) { whereL[L[i].second] = i; whereR[R[i].second] = i; } for (int i = 1; i <= n; i++) update(i); for (int Q = 0; Q < q; Q++) { int a, b; scanf("%d%d", &a, &b); swap(R[whereR[a]].second, R[whereR[b]].second); swap(whereR[a], whereR[b]); for (int i = -2; i <= 0; i++) { update(whereL[a] + i); update(whereR[a] + i); update(whereL[b] + i); update(whereR[b] + i); } dp[n + 1] = 0; for (int i = n; i; i--) { dp[i] = max(dp[i + 2] + cost[i][0], dp[i + 3] + cost[i][1]); if (L[i].second != R[i].second) dp[i] = max(dp[i], (long long)L[i].first * R[i].first + dp[i + 1]); } printf("%I64d\n", dp[1]); } return 0; }
3,000
CPP
b = input() a = [] for i in range(len(b)): if b[i] not in a: a.append(b[i]) if len(a) % 2 == 0: print("CHAT WITH HER!") else: print("IGNORE HIM!")
800
PYTHON3
# A. Theatre Square # Theatre Square in the capital city of Berland has a rectangular # shape with the size n × m meters. On the occasion of the city's # anniversary, a decision was taken to pave the Square with square # granite flagstones. Each flagstone is of the size a × a. # What is the least number of flagstones needed to pave the Square? # It's allowed to cover the surface larger than the Theatre Square, # but the Square has to be covered. It's not allowed to break the # flagstones. The sides of flagstones should be parallel to the sides # of the Square. import math lba = input().split() l = int(lba[0]) b = int(lba[1]) a = int(lba[2]) print(math.ceil(l/a) * math.ceil(b/a))
1,000
PYTHON3
for i in range(0,int(input())): n,m,x,y=list(map(int,input().split())) p=[] for j in range(0,n): s=input() p.append(s) cx=0 cy=0 for j in range(0,n): k=0 while(k<m): if(p[j][k]=="."): if(k+1<m and p[j][k+1]=="."): if(2*x>=y): k=k+2 cy=cy+1 else: k=k+2 cx=cx+2 else: k=k+1 cx=cx+1 else: k=k+1 print(cy*y+cx*x)
1,000
PYTHON3
summands = list(input().split("+")) summands.sort() print("+".join(summands))
800
PYTHON3
s = list(str(input())) s_list = [] for i in s: if not i in s_list: s_list.append(i) if len(s_list) % 2 != 0 : print("IGNORE HIM!") else: print("CHAT WITH HER!")
800
PYTHON3
t=int(input()) for z in range(t): n=(int(input())) if n%2==0: s='1'*(n//2) else: if n==3: s='7' else: n-=3 s=('7'+'1'*(n//2)) print(s)
900
PYTHON3
x=[int(y) for y in input().split()] print(max(x)-min(x))
800
PYTHON3
l,a,b=int(input()),int(input()),int(input()) print(a*(l/(a+b)))
900
PYTHON3
#include <bits/stdc++.h> long long Ans[36] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8092, 16184, 32368, 64736, 129472, 258944, 517888, 1035776, 2071552, 4143104, 8286208, 16572416, 33144832, 66289664, 132579328, 265158656, 530317312, 1060634624, 2121269248, 4242538496, 8485076992, 16970153984, 33940307968}; using namespace std; int main() { int n; cin >> n; cout << Ans[n]; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; vector<int> v; long long int f(int lo, int hi, int prev) { long long int res = 0; int tl = -1; int min = 2000000000; int c = 0; for (int i = lo; i <= hi; i++) { if (v[i] < min) min = v[i]; } for (int i = lo; i <= hi; i++) { if (v[i] > min && tl == -1) tl = i; else if (v[i] == min) { c++; if (tl != -1) { long long int r = f(tl, i - 1, min); if (r > 0) res += r; tl = -1; } } else if (i == hi && tl != -1) { long long int r = f(tl, i, min); if (r > 0) res += r; } } res += c; res -= min - prev; return res; } int main() { int n; scanf("%d", &n); int res = n; for (int i = 0; i < n; i++) { int a; scanf("%d", &a); v.push_back(a); } long long int r = f(0, v.size() - 1, 0); if (r > 0) res = res - r; printf("%d\n", res); return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; int main() { map<set<int>, int> m; int n, maxL = 0; char c; scanf("%d\n", &n); for (int i = 0; i < n; i++) { set<int> temp; for (int j = 0; j < n; j++) { scanf("%c", &c); if (c == '0') temp.insert(j + 1); } m[temp]++; maxL = max(maxL, m[temp]); scanf("\n"); } printf("%d", maxL); }
1,200
CPP
n = int(input()) a = list(input()) arr = a[::-1] c = 0 for i in range(10,n): if arr[i] == '8': c+=1 num = n-11 need = num//2 if c>need: print('YES') else: print('NO')
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 9, oo = 1e9; bool a[105][105]; void solve() { int k, n = 2; cin >> k; while (++n * (n - 1) * (n - 2) < k * 6) ; k -= --n * (n - 1) * (n - 2) / 6; for (int i = 0; i < n; i++) { a[i][i] = 0; for (int j = i + 1; j < n; j++) { a[i][j] = 1; a[j][i] = 1; } } while (k) { int x = 0; while (++x * (x + 1) <= 2 * k) ; k -= --x * (x + 1) / 2; for (int i = 0; i < x; i++) { a[n][i + 1] = 1; a[i + 1][n] = 1; } a[n][0] = 1; a[0][n++] = 1; } cout << n << endl; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout << a[i][j]; cout << endl; } } int main() { ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) solve(); }
1,600
CPP
import math from collections import defaultdict import functools as f from itertools import permutations as p t = 1 t = int(input()) for _ in range(t): n=str(input()) val=n[0] val=int (val) ans=10*(val-1) le=len(n) ans+=(le*(le+1))//2 print(ans)
800
PYTHON3
def is_prime(num): i = 2 while i * i <= num: if num % i == 0: return False i += 1 return True n = int(input()) if n == 2: print(1) elif n % 2 == 1: if is_prime(n): print(1) elif is_prime(n-2): print(2) else: print(3) else: print(2)
1,600
PYTHON3
def revenue(b, p, f, h, c): if h > c: rev = min(p, b//2)*h b -= 2*min(p, b//2) rev += min(f, b//2)*c else: rev = min(f, b//2)*c b -= 2*min(f, b//2) rev += min(p, b//2)*h return rev t = int(input()) for _ in range(t): b, p, f = map(int, input().split()) h, c = map(int, input().split()) print(revenue(b, p, f, h, c))
800
PYTHON3
m, n = input().split(' ') s = input().split(' ') t = input().split(' ') q = int(input()) queries = [] for i in range(q): queries.append(int(input())) m = int(m) n = int(n) # print (m, n, s, t, q, queries) for query in queries: year = s[query%m-1] + t[query%n-1] print (year)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n) { if (n % 2 == 0) { cout << "NO" << endl; } else { int d = 4; vector<int> v; v.push_back(1); while (d <= (2 * n)) { v.push_back(d); v.push_back(d + 1); d += 4; } d = 2; while (d <= (2 * n)) { v.push_back(d); if (d == (2 * n)) break; v.push_back(d + 1); d += 4; } cout << "YES" << endl; for (int i = 0; i < v.size(); i++) cout << v[i] << " "; cout << endl; } } return 0; }
1,200
CPP
t=int(input()) for _ in range(t): n,m=list(map(int,input().split())) if min(n,m)!=1: for __ in range(n-1): print('B'*m) print('B'*(m-1)+'W')
1,000
PYTHON3
n=int(input()) t=n//2 for i in range(n): for j in range(n): if i+j<t or n-i+j-1<t or n-j+i-1<t or 2*n-i-j-2<t: print("*",end="") else: print("D",end="") print()
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; long long f[1 << 20], inv, n; long long P(long long a, long long b) { if (!b) return 1; long long y = P(a, b / 2); y = y * y % mod; if (b % 2) return y * a % mod; return y; } long long C(long long k) { long long ans = inv; if (k < 0) ans = 0; for (long long i = k % mod; i >= k % mod - n + 2; i--) { ans = ans * i % mod; } return ans; } int32_t main() { long long s; cin >> n >> s; inv = 1; for (long long i = 2; i < n; i++) { inv *= i; inv %= mod; } inv = P(inv, mod - 2); for (long long i = 0; i < n; i++) { cin >> f[1 << i]; f[1 << i]++; } for (long long mask = 1; mask < (1 << n); mask++) f[mask] = f[mask & (mask - 1)] + f[mask & -mask]; long long ans = 0; for (long long mask = 0; mask < (1 << n); mask++) { if (__builtin_popcount(mask) % 2) ans -= C(s - f[mask] + n - 1); else ans += C(s - f[mask] + n - 1); ans %= mod; ans += mod; ans %= mod; } cout << ans; }
2,300
CPP