solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
n, m = [int(x) for x in input().split()] k = 0 while max(n, m) > 1 and min(m, n) > 0: k += 1 n, m = max(n, m) - 2, min(n, m) + 1 print(k)
1,100
PYTHON3
n=int(input()) z=list(map(int,input().split())) z.sort() for i in range(n) : if i==n-1 : print(z[i]) else : print(z[i],end=" ")
900
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m; vector<int> ve; const int MAX = 51; long long C[MAX][MAX]; void build() { for (int i = 0; i < MAX; ++i) for (int j = 0; j < MAX; ++j) C[i][j] = (j == 0) ? 1 : ((i == 0) ? 0 : C[i - 1][j - 1] + C[i - 1][j]); } double memo[MAX][MAX][MAX]; double solve(int i, int last, int mx) { if (i == m) { if (last != n) return 0; return mx; } double &ret = memo[i][last][mx]; if (ret == ret) return ret; ret = 0; for (int j = 0; j <= n - last; j++) { ret += C[n - last][j] * 1LL * solve(i + 1, last + j, max(mx, (j / ve[i]) + (j % ve[i] != 0))); } return ret; } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); build(); cin >> n >> m; for (int i = 0; i < m; i++) { int x; cin >> x; ve.push_back(x); } memset(memo, -1, sizeof memo); cout << fixed << setprecision(20) << powl(1.0 / m, n) * solve(0, 0, 0) << endl; return 0; }
2,200
CPP
#include <bits/stdc++.h> const int N = 1205; struct Node { int a[3]; int t; } a[N]; int n, vis[N], ans[N][3], num, st[N], f[N], g[N], cnt; int main() { scanf("%d", &n); for (int i = 1, x; i <= n * 3; i++) { scanf("%d", &x); vis[x] = 1; } int top = 0; for (int i = 1; i <= n * 6; i++) { if (!top) { st[++top] = ++cnt; a[cnt].a[0] = i; a[cnt].t = 1; } else if (vis[a[st[top]].a[a[st[top]].t - 1]] == vis[i]) { a[st[top]].a[a[st[top]].t++] = i; } else { st[++top] = ++cnt; a[cnt].a[0] = i; a[cnt].t = 1; } if (a[st[top]].t == 3) { int tmp = st[top]; --top; if (top) { f[tmp] = st[top]; ++g[st[top]]; } } } for (int i = 1; i <= 2 * n; i++) { int x; for (int v = 1; v <= 2 * n; v++) { if (g[v] < 0) continue; if (g[v] == 0 && vis[a[v].a[0]] == i % 2) { x = v; if (f[x] != 0) break; } } printf("%d %d %d\n", a[x].a[0], a[x].a[1], a[x].a[2]); g[x]--; g[f[x]]--; } return 0; }
3,200
CPP
t = int(input()) for i in range(t): l, r = list(map(int, input().split())) if l*2>r: print("yes\n") else: print("no\n")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAXN = 210000; int n; int a[MAXN]; vector<int> go[MAXN]; vector<pair<int, int> > gg[MAXN]; long long sm[MAXN * 4], mn[MAXN * 4], mx[MAXN * 4]; long long pp[MAXN * 4]; void push(int v, int tl, int tr) { if (pp[v]) { int m = (tl + tr) >> 1; pp[v * 2 + 1] = pp[v * 2 + 2] = pp[v]; mn[v * 2 + 1] = mn[v * 2 + 2] = mn[v]; mx[v * 2 + 1] = mx[v * 2 + 2] = mx[v]; sm[v * 2 + 1] = pp[v] * (long long)(m - tl); sm[v * 2 + 2] = pp[v] * (long long)(tr - m); pp[v] = 0; } }; void upd(int v, int tl, int tr, int l, int r, int d) { if (mn[v] >= d || r <= tl || tr <= l) return; if (l <= tl && tr <= r) { if (mx[v] <= d) { pp[v] = d; mx[v] = d; mn[v] = d; sm[v] = (tr - tl) * (long long)d; return; } } int m = (tl + tr) >> 1; push(v, tl, tr); upd(v * 2 + 1, tl, m, l, r, d); upd(v * 2 + 2, m, tr, l, r, d); mn[v] = min(mn[v * 2 + 1], mn[v * 2 + 2]); mx[v] = max(mx[v * 2 + 1], mx[v * 2 + 2]); sm[v] = sm[v * 2 + 1] + sm[v * 2 + 2]; } long long get(int v, int tl, int tr, int l, int r) { if (tr <= l || r <= tl) return 0; if (l <= tl && tr <= r) return sm[v]; push(v, tl, tr); int m = (tl + tr) >> 1; return get(v * 2 + 1, tl, m, l, r) + get(v * 2 + 2, m, tr, l, r); } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", a + i); for (int i = 0; i < n; ++i) { for (int j = 1; j * j <= a[i]; ++j) { if (a[i] % j == 0) { go[j].push_back(i); if (j * j != a[i]) go[a[i] / j].push_back(i); } } } for (int i = 1; i <= 200000; ++i) { if (go[i].size() >= 2) { int l, r; l = go[i][1] + 1; r = n; gg[l].push_back(make_pair(r, i)); l = 0; r = go[i][go[i].size() - 2]; gg[l].push_back(make_pair(r, i)); l = go[i][0] + 1; r = go[i].back(); gg[l].push_back(make_pair(r, i)); } } long long ans = 0; for (int i = 0; i < n; ++i) { for (auto j : gg[i]) upd(0, 0, n + 1, 0, j.first + 1, j.second); ans += get(0, 0, n + 1, i + 1, n + 1); } cout << ans << "\n"; return 0; }
2,800
CPP
t = int(input()) for _ in range(t): a, b, c, r = [int(x) for x in input().split(' ')] if a > b: a, b = b, a print(b-a - max(0, min(b, c+r) - max(a, c-r)))
900
PYTHON3
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } long long poww(long long a, long long b) { if (b == 0) return 1; long long tmp = poww(a, b / 2); return (b & 1 ? a * tmp * tmp : tmp * tmp); } string itos(long long i) { string s = ""; while (i) { s += char(i % 10 + '0'); i /= 10; } reverse(s.begin(), s.end()); return s; } long long stoi(string &s) { long long tot = 0; for (int i = (int)s.length() - 1, j = 1; i >= 0; i--, j *= 10) { tot += j * (s[i] + '0'); } return tot; } int months[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; using namespace std; void tt() { freopen("test.txt", "r", stdin); } const int MAX = 1e6 + 10; int n, m, seed, vmax; int a[MAX]; int rnd() { int ret = seed; seed = (1ll * seed * 7 + 13) % (1000 * 1000 * 1000 + 7); return ret; ; } int pw(int num, int power, int MOD) { if (power == 0) { return 1 % MOD; } int x = 1ll * pw(num, power / 2, MOD) % MOD; int ans = 1ll * x % MOD * x % MOD * ((power & 1) ? num : 1) % MOD; return ans % MOD; } long long Power(long long Base, long long Exp, long long Mod) { Base %= Mod; long long res = 1; do { if (Exp & 1) (res *= Base) %= Mod; (Base *= Base) %= Mod; } while (Exp >>= 1); return res; } map<int, long long> s; int main() { cin >> n >> m >> seed >> vmax; for (int i = 1; i <= n; i++) a[i] = (rnd() % vmax) + 1; for (int i = 1; i <= n; i++) s.insert({i, a[i]}); for (int i = 1; i <= m; i++) { int x, y; int op = (rnd() % 4) + 1; int l = (rnd() % n) + 1; int r = (rnd() % n) + 1; if (l > r) { swap(l, r); } if (op == 3) x = (rnd() % (r - l + 1)) + 1; else x = (rnd() % vmax) + 1; if (op == 4) y = (rnd() % vmax) + 1; auto itl = s.upper_bound(l); --itl; if (itl->first != l) { s[l] = itl->second; ++itl; } auto itr = s.upper_bound(r + 1); --itr; if (itr->first != r + 1) { s[r + 1] = itr->second; ++itr; } if (op == 1) { while (itl != itr) { itl->second += x; itl++; } } else if (op == 2) { while (itl != itr) { s.erase(itl++); } s[l] = x; } else if (op == 3) { vector<pair<long long, int> > v; while (itr != itl) { int a = itr->first; itr--; int b = itr->first; v.push_back({itr->second, a - b}); } long long res = 0, cnt = 0; sort(v.begin(), v.end()); for (auto t : v) { cnt += t.second; if (cnt >= x) { res = t.first; break; } } cout << res << '\n'; } else if (op == 4) { vector<pair<int, int> > v; long long res = 0; for (int ub; itl != itr;) ub = (itr--)->first, (res += Power(itr->second, x, y) * (ub - itr->first)) %= y; cout << res << '\n'; } } return 0; }
2,600
CPP
n=int(input()) aa=0 bb=0 cc=0 for i in range(n): a,b,c=map(int,input().split()) aa+=a bb+=b cc+=c if(aa==bb==cc==0): print("YES") else: print("NO")
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long MAX = 1000; const long long MAXN = 1000; const double PI = acos(-1.0); long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long a[300000]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); int s = 0, t = 0; while (t + 1 < n && a[t + 1] - a[s] <= 5) t++; int ans = t - s + 1; while (t != n) { while (s + 1 < n && a[s + 1] == a[s]) s++; s++; while (t + 1 < n && a[t + 1] - a[s] <= 5) t++; if (t - s + 1 > ans) ans = t - s + 1; if (t == n - 1) break; } cout << ans << endl; return 0; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; namespace io { const int SI = 1 << 21 | 1; char IB[SI], *IS, *IT, OB[SI], *OS = OB, *OT = OS + SI - 1, c, ch[100]; int f, t; inline void flush() { fwrite(OB, 1, OS - OB, stdout), OS = OB; } inline void pc(char x) { *OS++ = x; if (OS == OT) flush(); } template <class I> inline void rd(I &x) { for (f = 1, c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin), IS == IT ? EOF : *IS++) : *IS++); c < '0' || c > '9'; c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin), IS == IT ? EOF : *IS++) : *IS++)) if (c == '-') f = -1; for (x = 0; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + (c & 15), c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin), IS == IT ? EOF : *IS++) : *IS++)) ; x *= f; } inline void rds(char *s, int &x) { for (c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin), IS == IT ? EOF : *IS++) : *IS++); c < 33 || c > 126; c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin), IS == IT ? EOF : *IS++) : *IS++)) ; for (x = 0; c >= 33 && c <= 126; s[++x] = c, c = (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin), IS == IT ? EOF : *IS++) : *IS++)) ; s[x + 1] = '\0'; } template <class I> inline void print(I x, char k = '\n') { if (!x) pc('0'); if (x < 0) pc('-'), x = -x; while (x) ch[++t] = x % 10 + '0', x /= 10; while (t) pc(ch[t--]); pc(k); } inline void prints(string s) { int x = s.length(); while (t < x) pc(s[t++]); pc('\n'), t = 0; } struct Flush { ~Flush() { flush(); } } flusher; } // namespace io using io::print; using io::prints; using io::rd; using io::rds; const int N = 1e5 + 7, inf = INT_MAX >> 1; int n, m, a[N], b[N], l[N], p[N], f[N], g[N], v[N], ans[N]; inline void get(int i, int k, int &x) { int o = lower_bound(b + 1, b + m + 1, k) - b - 1; v[o] = 1, x = ans[i] = b[o]; } int main() { rd(n); for (int i = 1; i <= n; i++) rd(a[i]), f[i] = inf; ++n, a[n] = f[n] = inf; rd(m); for (int i = 1; i <= m; i++) rd(b[i]); sort(b + 1, b + m + 1); for (int i = 1; i <= n; i++) if (~a[i]) { int j = lower_bound(f + 1, f + n + 1, a[i]) - f - 1; l[i] = j + 1, p[i] = g[j], f[j + 1] = a[i], g[j + 1] = i; } else for (int j = n, o = m; o; o--) { while (f[j] >= b[o]) --j; f[j + 1] = b[o], g[j + 1] = i; } int i = l[n], j = n, x = a[n]; while (i--) if (~a[j]) { if (!~a[p[j]]) get(p[j], a[j], x); else x = a[p[j]]; j = p[j]; } else { bool ok = 0; for (int s = j - 1; s; s--) if (~a[s] && l[s] == i && a[s] < x) { x = a[j = s], ok = 1; break; } if (ok) continue; for (int s = j - 1; s; s--) if (!~a[s]) { get(s, x, x), j = s; break; } } for (int i = 1, j = 1; i <= n; i++) if (!~a[i]) { if (ans[i]) continue; while (v[j]) ++j; v[j] = 1, ans[i] = b[j]; } else ans[i] = a[i]; for (int i = 1; i < n; i++) print(ans[i], " \n"[i == n]); return 0; }
3,000
CPP
#include <bits/stdc++.h> using namespace std; bool sortbysecond(const pair<int, int> &a, const pair<int, int> &b) { return (a.second > b.second); } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } template <typename T> string NumberTostring(T Number) { ostringstream second; second << Number; return second.str(); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { long long n; cin >> n; string a, b; cin >> a >> b; vector<long long> vec; for (int i = n - 1; i >= 0; i--) { if (a[i] != b[i]) { if (a[0] == b[i]) { vec.push_back(1); a[0] = '0' + !(a[0] - '0'); } reverse(a.begin(), a.begin() + i + 1); for (int j = 0; j <= i; j++) { a[j] = '0' + !(a[j] - '0'); } vec.push_back(i + 1); } } cout << vec.size() << " "; for (long long o = 0; o < vec.size(); o++) cout << vec[o] << " "; cout << "\n"; } return 0; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; int a[100050]; int m[100050]; int b[100050]; int main() { int n; int maxn = -1; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } if (n % 2 == 0) { for (int i = 1; i <= n / 2; i++) { b[i] = a[i] - i; if (b[i] >= 0) { m[b[i]]++; } } for (int i = n / 2 + 1; i <= n; i++) { b[i] = a[i] - (n - i + 1); if (b[i] >= 0) { m[b[i]]++; } } } else { for (int i = 1; i <= (n + 1) / 2; i++) { b[i] = a[i] - i; if (b[i] >= 0) { m[b[i]]++; } } for (int i = (n + 1) / 2 + 1; i <= n; i++) { b[i] = a[i] - (n - i + 1); if (b[i] >= 0) { m[b[i]]++; } } } for (int i = 0; i <= 100050; i++) { maxn = max(maxn, m[i]); } printf("%d\n", n - maxn); return 0; }
1,800
CPP
str = input() woerte = str.lower() alfabet = [] yuanyin = ['a','e','i','o','u','y'] for letter in woerte: if letter not in yuanyin: alfabet.append('.') alfabet.append(letter) print(''.join(alfabet))
1,000
PYTHON3
def main(): table_card = input() hand_cards = input().split() answer = 'NO' for card in hand_cards: if card[0] == table_card[0]: answer = 'YES' if card[1] == table_card[1]: answer = 'YES' print(answer) if __name__ == '__main__': main()
800
PYTHON3
n=int(input()) arr=list(map(int,input().strip().split(' '))) ans=10000000 for i in range(1,n-1): m=-1000000 for j in range(n-1): if(j==i): continue else: if(j+1==i): m=max(arr[j+2]-arr[j],m) else: m=max(arr[j+1]-arr[j],m) if(m<ans): ans=m print(ans)
900
PYTHON3
n=int(input()) a=list(map(int,input().split())) s=sum(a) elec=0 elecnext=0 for k in range(n): elec=elec+k*a[k]*4 for i in range(n): for j in range(n): elecnext=elecnext+2*(i+abs(i-j)+j)*a[j] if(elecnext<=elec): elec=elecnext elecnext=0 print(elec)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const int infinity = 100000000000; int main() { int n, m; while (cin >> n >> m) { int i, j, ans[105], b[105], p; for (i = 1; i <= n; i++) ans[i] = 0; for (i = 1; i <= m; i++) cin >> b[i]; for (i = 1; i <= m; i++) { p = b[i]; for (j = p; j <= n; j++) { if (ans[j] == 0) ans[j] = p; } } for (i = 1; i <= n; i++) cout << ans[i] << " "; cout << endl; } return (0); }
900
CPP
# ---------------------------iye ha aam zindegi--------------------------------------------- import math import random import heapq,bisect import sys from collections import deque, defaultdict from fractions import Fraction import sys import threading from collections import defaultdict threading.stack_size(10**8) mod = 10 ** 9 + 7 mod1 = 998244353 # ------------------------------warmup---------------------------- import os import sys from io import BytesIO, IOBase sys.setrecursionlimit(300000) 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) input = lambda: sys.stdin.readline().rstrip("\r\n") # -------------------game starts now----------------------------------------------------import math class TreeNode: def __init__(self, k, v): self.key = k self.value = v self.left = None self.right = None self.parent = None self.height = 1 self.num_left = 1 self.num_total = 1 class AvlTree: def __init__(self): self._tree = None def add(self, k, v): if not self._tree: self._tree = TreeNode(k, v) return node = self._add(k, v) if node: self._rebalance(node) def _add(self, k, v): node = self._tree while node: if k < node.key: if node.left: node = node.left else: node.left = TreeNode(k, v) node.left.parent = node return node.left elif node.key < k: if node.right: node = node.right else: node.right = TreeNode(k, v) node.right.parent = node return node.right else: node.value = v return @staticmethod def get_height(x): return x.height if x else 0 @staticmethod def get_num_total(x): return x.num_total if x else 0 def _rebalance(self, node): n = node while n: lh = self.get_height(n.left) rh = self.get_height(n.right) n.height = max(lh, rh) + 1 balance_factor = lh - rh n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right) n.num_left = 1 + self.get_num_total(n.left) if balance_factor > 1: if self.get_height(n.left.left) < self.get_height(n.left.right): self._rotate_left(n.left) self._rotate_right(n) elif balance_factor < -1: if self.get_height(n.right.right) < self.get_height(n.right.left): self._rotate_right(n.right) self._rotate_left(n) else: n = n.parent def _remove_one(self, node): """ Side effect!!! Changes node. Node should have exactly one child """ replacement = node.left or node.right if node.parent: if AvlTree._is_left(node): node.parent.left = replacement else: node.parent.right = replacement replacement.parent = node.parent node.parent = None else: self._tree = replacement replacement.parent = None node.left = None node.right = None node.parent = None self._rebalance(replacement) def _remove_leaf(self, node): if node.parent: if AvlTree._is_left(node): node.parent.left = None else: node.parent.right = None self._rebalance(node.parent) else: self._tree = None node.parent = None node.left = None node.right = None def remove(self, k): node = self._get_node(k) if not node: return if AvlTree._is_leaf(node): self._remove_leaf(node) return if node.left and node.right: nxt = AvlTree._get_next(node) node.key = nxt.key node.value = nxt.value if self._is_leaf(nxt): self._remove_leaf(nxt) else: self._remove_one(nxt) self._rebalance(node) else: self._remove_one(node) def get(self, k): node = self._get_node(k) return node.value if node else -1 def _get_node(self, k): if not self._tree: return None node = self._tree while node: if k < node.key: node = node.left elif node.key < k: node = node.right else: return node return None def get_at(self, pos): x = pos + 1 node = self._tree while node: if x < node.num_left: node = node.left elif node.num_left < x: x -= node.num_left node = node.right else: return (node.key, node.value) raise IndexError("Out of ranges") @staticmethod def _is_left(node): return node.parent.left and node.parent.left == node @staticmethod def _is_leaf(node): return node.left is None and node.right is None def _rotate_right(self, node): if not node.parent: self._tree = node.left node.left.parent = None elif AvlTree._is_left(node): node.parent.left = node.left node.left.parent = node.parent else: node.parent.right = node.left node.left.parent = node.parent bk = node.left.right node.left.right = node node.parent = node.left node.left = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) def _rotate_left(self, node): if not node.parent: self._tree = node.right node.right.parent = None elif AvlTree._is_left(node): node.parent.left = node.right node.right.parent = node.parent else: node.parent.right = node.right node.right.parent = node.parent bk = node.right.left node.right.left = node node.parent = node.right node.right = bk if bk: bk.parent = node node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1 node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right) node.num_left = 1 + self.get_num_total(node.left) @staticmethod def _get_next(node): if not node.right: return node.parent n = node.right while n.left: n = n.left return n # -----------------------------------------------binary seacrh tree--------------------------------------- class SegmentTree1: def __init__(self, data, default, func): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------game starts now----------------------------------------------------import math class SegmentTree: def __init__(self, data, default=0, func=lambda a, b: a+b): """initialize the segment tree with data""" self._default = default self._func = func self._len = len(data) self._size = _size = 1 << (self._len - 1).bit_length() self.data = [default] * (2 * _size) self.data[_size:_size + self._len] = data for i in reversed(range(_size)): self.data[i] = func(self.data[i + i], self.data[i + i + 1]) def __delitem__(self, idx): self[idx] = self._default def __getitem__(self, idx): return self.data[idx + self._size] def __setitem__(self, idx, value): idx += self._size self.data[idx] = value idx >>= 1 while idx: self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1]) idx >>= 1 def __len__(self): return self._len def query(self, start, stop): if start == stop: return self.__getitem__(start) stop += 1 start += self._size stop += self._size res = self._default while start < stop: if start & 1: res = self._func(res, self.data[start]) start += 1 if stop & 1: stop -= 1 res = self._func(res, self.data[stop]) start >>= 1 stop >>= 1 return res def __repr__(self): return "SegmentTree({0})".format(self.data) # -------------------------------iye ha chutiya zindegi------------------------------------- class Factorial: def __init__(self, MOD): self.MOD = MOD self.factorials = [1, 1] self.invModulos = [0, 1] self.invFactorial_ = [1, 1] def calc(self, n): if n <= -1: print("Invalid argument to calculate n!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.factorials): return self.factorials[n] nextArr = [0] * (n + 1 - len(self.factorials)) initialI = len(self.factorials) prev = self.factorials[-1] m = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = prev * i % m self.factorials += nextArr return self.factorials[n] def inv(self, n): if n <= -1: print("Invalid argument to calculate n^(-1)") print("n must be non-negative value. But the argument was " + str(n)) exit() p = self.MOD pi = n % p if pi < len(self.invModulos): return self.invModulos[pi] nextArr = [0] * (n + 1 - len(self.invModulos)) initialI = len(self.invModulos) for i in range(initialI, min(p, n + 1)): next = -self.invModulos[p % i] * (p // i) % p self.invModulos.append(next) return self.invModulos[pi] def invFactorial(self, n): if n <= -1: print("Invalid argument to calculate (n^(-1))!") print("n must be non-negative value. But the argument was " + str(n)) exit() if n < len(self.invFactorial_): return self.invFactorial_[n] self.inv(n) # To make sure already calculated n^-1 nextArr = [0] * (n + 1 - len(self.invFactorial_)) initialI = len(self.invFactorial_) prev = self.invFactorial_[-1] p = self.MOD for i in range(initialI, n + 1): prev = nextArr[i - initialI] = (prev * self.invModulos[i % p]) % p self.invFactorial_ += nextArr return self.invFactorial_[n] class Combination: def __init__(self, MOD): self.MOD = MOD self.factorial = Factorial(MOD) def ncr(self, n, k): if k < 0 or n < k: return 0 k = min(k, n - k) f = self.factorial return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD # --------------------------------------iye ha combinations ka zindegi--------------------------------- def powm(a, n, m): if a == 1 or n == 0: return 1 if n % 2 == 0: s = powm(a, n // 2, m) return s * s % m else: return a * powm(a, n - 1, m) % m # --------------------------------------iye ha power ka zindegi--------------------------------- def sort_list(list1, list2): zipped_pairs = zip(list2, list1) z = [x for _, x in sorted(zipped_pairs)] return z # --------------------------------------------------product---------------------------------------- def product(l): por = 1 for i in range(len(l)): por *= l[i] return por # --------------------------------------------------binary---------------------------------------- def binarySearchCount(arr, n, key): left = 0 right = n - 1 count = 0 while (left <= right): mid = int((right + left) / 2) # Check if middle element is # less than or equal to key if (arr[mid] < key): count = mid + 1 left = mid + 1 # If key is smaller, ignore right half else: right = mid - 1 return count # --------------------------------------------------binary---------------------------------------- def countdig(n): c = 0 while (n > 0): n //= 10 c += 1 return c def binary(x, length): y = bin(x)[2:] return y if len(y) >= length else "0" * (length - len(y)) + y def countGreater(arr, n, k): l = 0 r = n - 1 # Stores the index of the left most element # from the array which is greater than k leftGreater = n # Finds number of elements greater than k while (l <= r): m = int(l + (r - l) / 2) if (arr[m] >= k): leftGreater = m r = m - 1 # If mid element is less than # or equal to k update l else: l = m + 1 # Return the count of elements # greater than k return (n - leftGreater) # --------------------------------------------------binary------------------------------------ n,t=map(int,input().split()) ind=defaultdict(int) se=set() e=[0]*n s=SegmentTree(e) for i in range(t): k=int(input()) st=0 end=n-1 ans=n-1 while(st<=end): mid=(st+end)//2 if mid in se: inp=ind[mid]-s.query(0,mid) else: print("? 1",mid+1,flush=True) inp=int(input()) inp=mid+1-inp se.add(mid) ind[mid]=inp+s.query(0,mid) if inp>=k: ans=mid end=mid-1 else: st=mid+1 s.__setitem__(ans,1) print("!",ans+1,flush=True)
2,200
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; struct node { int to, next; } e[N * 2]; int head[N], cnt, tim, fa[N][21], son[N], dep[N], top[N], en[N], siz[N], pos[N]; int f_mx[N * 4], mx[N * 4], mi[N * 4], f_mi[N * 4]; int n, m, rt; void add(int x, int y) { e[cnt].to = y; e[cnt].next = head[x]; head[x] = cnt++; } void dfs1(int u) { siz[u] = 1; for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].to; if (v != fa[u][0]) { fa[v][0] = u; dep[v] = dep[u] + 1; dfs1(v); siz[u] += siz[v]; if (son[u] == 0 || siz[v] > siz[son[u]]) son[u] = v; } } } void dfs2(int u, int f) { tim++; top[u] = f; pos[u] = tim; if (son[u]) dfs2(son[u], f); for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].to; if (v != fa[u][0] && v != son[u]) { dfs2(v, v); } } en[u] = tim; } void pushdown(int root) { mx[root << 1] = max(mx[root << 1], f_mx[root]); mx[root << 1 | 1] = max(mx[root << 1 | 1], f_mx[root]); f_mx[root << 1] = max(f_mx[root << 1], f_mx[root]); f_mx[root << 1 | 1] = max(f_mx[root << 1 | 1], f_mx[root]); f_mx[root] = 0; mi[root << 1] = min(mi[root << 1], f_mi[root]); mi[root << 1 | 1] = min(mi[root << 1 | 1], f_mi[root]); f_mi[root << 1] = min(f_mi[root << 1], f_mi[root]); f_mi[root << 1 | 1] = min(f_mi[root << 1 | 1], f_mi[root]); f_mi[root] = 1e9; } void add(int l, int r, int root, int ql, int qr, int val, int op) { if (l >= ql && r <= qr) { if (op) { mx[root] = max(mx[root], val); f_mx[root] = max(f_mx[root], val); } else { mi[root] = min(mi[root], val); f_mi[root] = min(f_mi[root], val); } return; } pushdown(root); int mid = l + r >> 1; if (mid >= ql) add(l, mid, root << 1, ql, qr, val, op); if (mid < qr) add(mid + 1, r, root << 1 | 1, ql, qr, val, op); mx[root] = max(mx[root << 1], mx[root << 1 | 1]); mi[root] = min(mi[root << 1], mi[root << 1 | 1]); } int q_mx(int l, int r, int root, int ql, int qr) { if (l >= ql && r <= qr) return mx[root]; int mid = l + r >> 1; pushdown(root); int ans = 0; if (mid >= ql) ans = q_mx(l, mid, root << 1, ql, qr); if (mid < qr) ans = max(ans, q_mx(mid + 1, r, root << 1 | 1, ql, qr)); return ans; } int q_mi(int l, int r, int root, int p) { if (l == r) return mi[root]; int mid = l + r >> 1; pushdown(root); if (mid >= p) return q_mi(l, mid, root << 1, p); else return q_mi(mid + 1, r, root << 1 | 1, p); } void update(int x, int y, int val, int op) { while (top[x] != top[y]) { if (dep[top[x]] < dep[top[y]]) swap(x, y); add(1, n, 1, pos[top[x]], pos[x], val, op); x = fa[top[x]][0]; } if (pos[x] > pos[y]) swap(x, y); add(1, n, 1, pos[x], pos[y], val, op); } int query(int x, int y) { int ans = 0; while (top[x] != top[y]) { if (dep[top[x]] < dep[top[y]]) swap(x, y); ans = max(ans, q_mx(1, n, 1, pos[top[x]], pos[x])); x = fa[top[x]][0]; } if (pos[x] > pos[y]) swap(x, y); ans = max(ans, q_mx(1, n, 1, pos[x], pos[y])); return ans; } struct edge { int x, y, v, id; bool operator<(const edge& a) const { return v < a.v; } } p[N]; int lca(int x, int y) { if (dep[x] < dep[y]) swap(x, y); for (int i = 20; i >= 0; i--) if (dep[fa[x][i]] >= dep[y]) x = fa[x][i]; if (x == y) return x; for (int i = 20; i >= 0; i--) if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; return fa[x][0]; } int f[N], in[N]; int finds(int x) { return x == f[x] ? f[x] : f[x] = finds(f[x]); } void deal() { for (int j = 1; (1 << j) <= n; j++) for (int i = 1; i <= n; i++) fa[i][j] = fa[fa[i][j - 1]][j - 1]; } int ans[N]; int main() { memset(head, -1, sizeof(head)); scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) f[i] = i; for (int i = 1; i <= m; i++) scanf("%d%d%d", &p[i].x, &p[i].y, &p[i].v), p[i].id = i; dep[1] = 1; sort(p + 1, p + 1 + m); for (int i = 1; i <= m; i++) { int fax = finds(p[i].x); int fay = finds(p[i].y); if (fax != fay) { in[p[i].id] = 1; f[fay] = fax; add(p[i].x, p[i].y); add(p[i].y, p[i].x); } } dfs1(1); dfs2(1, 1); deal(); for (int i = 1; i <= n * 4; i++) mi[i] = f_mi[i] = 1e9; for (int i = 1; i <= m; i++) { int x = p[i].x, y = p[i].y, firstson, val = p[i].v; int l = lca(x, y); if (y != l) { firstson = y; for (int i = 20; i >= 0; i--) if (dep[fa[firstson][i]] > dep[l]) firstson = fa[firstson][i]; update(firstson, y, val, in[p[i].id] ? 1 : 0); } if (x != l) { firstson = x; for (int i = 20; i >= 0; i--) if (dep[fa[firstson][i]] > dep[l]) firstson = fa[firstson][i]; update(firstson, x, val, in[p[i].id] ? 1 : 0); } } for (int i = 1; i <= m; i++) { if (in[p[i].id]) ans[p[i].id] = q_mi(1, n, 1, dep[p[i].x] > dep[p[i].y] ? pos[p[i].x] : pos[p[i].y]); else { int x = p[i].x, y = p[i].y, firstson, val = p[i].v; int l = lca(x, y); int aa = 0; if (y != l) { firstson = y; for (int i = 20; i >= 0; i--) if (dep[fa[firstson][i]] > dep[l]) firstson = fa[firstson][i]; aa = query(firstson, y); } if (x != l) { firstson = x; for (int i = 20; i >= 0; i--) if (dep[fa[firstson][i]] > dep[l]) firstson = fa[firstson][i]; aa = max(aa, query(firstson, x)); } ans[p[i].id] = aa; } } for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); return 0; }
2,400
CPP
s = list(input()) if ord(s[0]) >= 97: s[0] = chr(ord(s[0])-32) print(''.join(s))
800
PYTHON3
#include <bits/stdc++.h> const int MOD = 1000000007; const int SIZE_N = 2000 + 10; int n; int ary[SIZE_N]; int vis[SIZE_N]; int fac[SIZE_N]; int madd(long long a, long long b) { return (a + b) % MOD; } int mmul(long long a, long long b) { return (a * b) % MOD; } int dp[SIZE_N][SIZE_N], v[SIZE_N][SIZE_N]; int f(int a, int b) { if (v[a][b] == 1) return dp[a][b]; v[a][b] = 1; if (a == 0) return dp[a][b] = fac[b]; if (a == 1) return dp[a][b] = mmul(b, fac[b]); return dp[a][b] = madd(mmul(b, f(a - 1, b)), mmul(a - 1, f(a - 2, b + 1))); } int main(void) { fac[0] = 1; scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", ary + i); vis[ary[i]] = 1; fac[i] = mmul(fac[i - 1], i); } int a = 0, b = 0; for (int i = 1; i <= n; ++i) { if (vis[i] == 1) continue; if (ary[i] == -1) ++a; else ++b; } int ans = 0; ans = f(a, b); printf("%d\n", ans); return 0; }
2,000
CPP
import math n, r = map(int, input().split()) X = math.pi*2/n x = 2-2*math.cos(X) a = x-4 b = 2*x*r c = x*r*r # print(a,b,c) delta = b*b-4*a*c # print(delta) x1 = (-b+math.sqrt(delta))/(2*a) x2 = (-b-math.sqrt(delta))/(2*a) if x1>0: print(format(x1, '.7f')) else: print(format(x2, '.7f'))
1,200
PYTHON3
n=int(input()) s=input() for i in range(n): if s[0]=="S" and s[n-1:n]=="F": print("YES") break elif s[0]=="F" or (s[0]=="S" and s[n-1:n]=="S"): print("NO") break
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[150]; for (int i = 1; i <= n; i++) a[i] = 0; for (int i = 1; i <= n; i++) scanf("%1d", &a[i]); int i = 1; while (a[i] == 1 && i <= n) i++; if (i > n) i--; cout << i; return 0; }
900
CPP
#include <bits/stdc++.h> using namespace std; int a[1110000], b[1110000]; int main() { int n, cnt = 0, i; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &a[i]); b[a[i]]++; } for (i = 0; i <= 1110000; i++) { cnt += b[i] % 2; b[i + 1] += b[i] / 2; } printf("%d\n", cnt); return 0; }
1,500
CPP
for _ in range(int(input())): n, k = map(int, input().split()) a = [int(x) for x in input().split()] a.sort(reverse = True) s = 0 x = a[-1] for i in range(n-1): s = s + (k-a[i])//x print(s)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAXN = 300; struct point { int x, y; point() {} point(const int _x, const int _y) : x(_x), y(_y) {} } points[MAXN]; istream &operator>>(istream &in, point &p) { in >> p.x >> p.y; return in; } int operator^(const point a, const point b) { return a.x * b.y - a.y * b.x; } point operator-(const point a, const point b) { return point(a.x - b.x, a.y - b.y); } int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> points[i]; } int max_result_area_sum = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { point diagonal = points[j] - points[i]; int max_positive_area = 0, min_negative_area = 0; for (int k = 0; k < n; k++) { if (k == i || k == j) { continue; } point side = points[k] - points[i]; int current_area = side ^ diagonal; max_positive_area = max(max_positive_area, current_area); min_negative_area = min(min_negative_area, current_area); } if (max_positive_area > 0 && min_negative_area < 0) { max_result_area_sum = max(max_result_area_sum, max_positive_area - min_negative_area); } } } cout.precision(9); cout.setf(ios::fixed); cout << max_result_area_sum / 2.0L << endl; return 0; }
2,100
CPP
n = int(input()) sum = n//5 if n%5 !=0: sum += 1 print(sum)
800
PYTHON3
n,k=map(int,input().split()) l=list(map(int,input().split())) c=0 for i in range(n): if l[i]>k: c=c+2 else: c=c+1 print(c)
800
PYTHON3
x=int(input()) for i in range(x): s=0 m=0 y=int(input()) z=list(map(int,input().split(" "))) for i in range (y): s=s+z[i] m=m^z[i] print(2) print(m,s+m)
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = (int)1e5 + 7; const int inf = (int)1e9 + 7; int n, m; int ask(int x, int y) { if (x <= 0 || y > n || x > n || y <= 0) return 0; printf("1 %d %d\n", x, y); fflush(stdout); string s; cin >> s; return s == "TAK"; } void finish(int x, int y) { printf("2 %d %d", x, y); fflush(stdout); exit(0); } int get(int l, int r) { if (l > r) return n; while (l < r) { int mid = l + r >> 1; if (ask(mid, mid + 1)) { r = mid; } else { l = mid + 1; } } return l; } int main() { scanf("%d %d", &n, &m); int ans1 = get(1, n), ans2; int t1 = get(1, ans1 - 1); int t2 = get(ans1 + 1, n); if (t1 != ans1 && ask(t1, t2)) ans2 = t1; else ans2 = t2; finish(ans1, ans2); }
2,200
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long m, n, count = 0, ans = 0; cin >> m >> n; if (m == n) ans = 1; while (m != n) { if (m > n) { count = m / n; ans += count; m = m % n; if (m == 0) m = n; } if (m < n) { count = n / m; ans += count; n = n % m; if (n == 0) n = m; } } cout << ans; return 0; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; const int MAX = 5e5 + 5; const int inf = 1e9; const long long INF = 1e18; const long long MOD = 1e9 + 7; template <typename T> void LastOut(T x) { cout << x << '\n'; exit(0); } long long n, m, k, a[MAX], ans, kek; int main() { ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> k; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); kek = m / (k + 1); cout << kek * (a[n - 2] + a[n - 1] * k) + min(m % (k + 1), k) * a[n - 1] << '\n'; return 0; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; long long int count = ((x2 - x1) / 2 + 1) * ((y2 - y1) / 2 + 1) + ((x2 - x1) / 2) * ((y2 - y1) / 2); cout << count << endl; return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = (int)(1e5) + 123; const long long inf = (long long)(1e18); const int INF = (int)(1e9); int n, a1[maxn], a2[maxn], d1[4][maxn], d2[4][maxn], ans, b[maxn]; vector<pair<int, int> > ans1, ans2; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i < n; ++i) { cin >> a1[i]; d1[1][i] = d1[1][i - 1] + a1[i]; } for (int i = n - 1; i >= 1; --i) d1[0][i] = d1[0][i + 1] + a1[i]; for (int i = 1; i < n; ++i) { cin >> a2[i]; d2[1][i] = d2[1][i - 1] + a2[i]; } for (int i = n - 1; i >= 1; --i) d2[0][i] = d2[0][i + 1] + a2[i]; for (int i = 1; i <= n; ++i) cin >> b[i]; for (int i = 1; i <= n; ++i) { ans1.push_back(make_pair(b[i] + d2[0][i] + d1[1][i - 1], i)); } sort(ans1.begin(), ans1.end()); cout << ans1[0].first + ans1[1].first; return 0; }
1,300
CPP
import sys input = sys.stdin.readline I = lambda : list(map(int,input().split())) n,m,k=I() r=0;cc=[1,1] tot=0 an=[[] for i in range(k)] while r!=k: an[r]+=cc tot+=1 if len(an[r])==4: r+=1 while r==k-1 and tot<n*m: cc=[cc[0]+1,cc[1]] if ((cc[0]%2 and cc[1]==m) or (cc[0]%2==0 and cc[1]==1)) else [cc[0],cc[1]+(1 if cc[0]%2 else -1)] an[r]+=cc tot+=1 if tot==n*m: break cc=[cc[0]+1,cc[1]] if ((cc[0]%2 and cc[1]==m) or (cc[0]%2==0 and cc[1]==1)) else [cc[0],cc[1]+(1 if cc[0]%2 else -1)] for i in an: print(len(i)//2,*i)
1,500
PYTHON3
def unfortunate(arr): if 1 in arr: return -1 else: return 1 n = int(input()) arr = list(map(int,input().split())) print(unfortunate(arr))
1,000
PYTHON3
#include <bits/stdc++.h> using int_t = long long int; template <typename T, typename FirstOp = std::plus<T>, typename SecondOp = std::multiplies<T>, typename UpdateOp = std::plus<T>> class SegmentTree { public: explicit SegmentTree(std::size_t size, FirstOp first_operation = FirstOp(), SecondOp second_operation = SecondOp(), UpdateOp update_operation = UpdateOp(), T identity = T()) : m_size(size), m_tree(std::max(size * 4, std::size_t(4)), identity), m_lazy_tag(std::max(size * 4, std::size_t(4)), identity), m_first_operation(first_operation), m_second_operation(second_operation), m_update_operation(update_operation), m_identity(identity) {} void update(std::size_t pos, const T& value) { update(pos + 1, value, 1, 1, m_size); } void update(std::size_t start, std::size_t end, const T& value) { update(start + 1, end, value, 1, 1, m_size); } T query(std::size_t start, std::size_t end) { return query(start + 1, end, 1, 1, m_size); } private: std::size_t m_size; std::vector<T> m_tree; std::vector<T> m_lazy_tag; FirstOp m_first_operation; SecondOp m_second_operation; UpdateOp m_update_operation; const T m_identity; void push_up(std::size_t root) { m_tree[root] = m_first_operation(m_tree[left(root)], m_tree[right(root)]); } void push_down(std::size_t root, std::size_t length) { m_tree[left(root)] = m_update_operation( m_tree[left(root)], m_second_operation(m_lazy_tag[root], length - length / 2)); m_lazy_tag[left(root)] = m_update_operation(m_lazy_tag[left(root)], m_lazy_tag[root]); m_tree[right(root)] = m_update_operation( m_tree[right(root)], m_second_operation(m_lazy_tag[root], length / 2)); m_lazy_tag[right(root)] = m_update_operation(m_lazy_tag[right(root)], m_lazy_tag[root]); m_lazy_tag[root] = m_identity; } void update(std::size_t pos, const T& value, std::size_t root, std::size_t l, std::size_t r) { if (l == r) { m_tree[root] = m_update_operation(m_tree[root], value); return; } std::size_t m = (l + r) / 2; if (pos <= m) { update(pos, value, left(root), l, m); } else { update(pos, value, right(root), m + 1, r); } push_up(root); } void update(std::size_t start, std::size_t end, const T& value, std::size_t root, std::size_t l, std::size_t r) { if (start <= l && r <= end) { m_tree[root] = m_update_operation(m_tree[root], m_second_operation(value, r - l + 1)); m_lazy_tag[root] = m_update_operation(m_lazy_tag[root], value); return; } if (m_lazy_tag[root] != m_identity) { push_down(root, r - l + 1); } std::size_t m = (l + r) / 2; if (start <= m) { update(start, end, value, left(root), l, m); } if (m < end) { update(start, end, value, right(root), m + 1, r); } push_up(root); } T query(std::size_t start, std::size_t end, std::size_t root, std::size_t l, std::size_t r) { if (start <= l && r <= end) { return m_tree[root]; } if (m_lazy_tag[root] != m_identity) { push_down(root, r - l + 1); } std::size_t m = (l + r) / 2; if (start <= m && m < end) { return m_first_operation(query(start, end, left(root), l, m), query(start, end, right(root), m + 1, r)); } else if (end <= m) { return query(start, end, left(root), l, m); } else { return query(start, end, right(root), m + 1, r); } } static std::size_t left(std::size_t root) { return root * 2; } static std::size_t right(std::size_t root) { return root * 2 + 1; } }; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int_t m; std::cin >> m; std::vector<int_t> x(m); struct FirstOp { int_t operator()(int_t lhs, int_t rhs) const { return std::max(lhs, rhs); } }; struct SecondOp { int_t operator()(int_t x, std::size_t) const { return x; } }; SegmentTree<int_t, FirstOp, SecondOp> segment_tree(m); for (int_t i = 0; i < m; ++i) { int_t p; int_t t; std::cin >> p >> t; int_t index = m - p; if (t == 0) { segment_tree.update(index, m, -1); } else { std::cin >> x[index]; segment_tree.update(index, m, 1); } if (m == 1) { if (segment_tree.query(0, 1) > 0) { std::cout << x[0] << '\n'; } else { std::cout << -1 << '\n'; } continue; } int_t start = 0; int_t end = m; while (start < end - 1) { int_t mid = (start + end - 1) / 2; if (segment_tree.query(start, mid + 1) > 0) { end = mid + 1; } else if (segment_tree.query(mid, end) > 0) { start = mid + 1; } else { start = end = -1; } } if (start == -1) { std::cout << -1 << '\n'; } else { std::cout << x[start] << '\n'; } } return 0; }
2,200
CPP
k = int(input()) w = input() a = 0 d = 0 for i in range(0,k): if w[i] == "A": a = a + 1 else: d = d + 1 if a>d: print("Anton") elif d>a: print("Danik") else: print("Friendship")
800
PYTHON3
summA, summB, summC = 0, 0, 0 Q = [] Count = int(input()) for x in range(Count): Q.append(list(map(int, input().split()))) for z in range(len(Q)): summA += Q[z][0] summB += Q[z][1] summC += Q[z][2] if (summA == 0) & (summB == 0) & (summC == 0): print('YES') else: print('NO')
1,000
PYTHON3
test = int(input()) for t in range(0,test): n,k = map(int,input().split()) s = input() ans = 0 count = 0 s = s + '2' for ch in s: if ch == '0': count = count + 1 elif ch == '1': count = count - k if count > 0: ans = ans + ( (count + k) // (k + 1) ) count = -k elif count > 0: ans = ans + ( (count + k) // (k + 1) ) print(ans)
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; int parent[100001]; int flag = 0; void dfs(vector<long long int> v[], int n, long long int s[], long long int a[], int source, long long int h[], bool visited[]) { visited[source] = true; if (h[source] % 2 != 0) { for (int i = 0; i < v[source].size(); i++) { h[v[source][i]] = h[source] + 1; dfs(v, n, s, a, v[source][i], h, visited); } } else { long long int m = INT_MAX; long long y = s[source]; if (v[source].size() == 0) { a[source] = 0; } else { for (int i = 0; i < v[source].size(); i++) { h[v[source][i]] = h[source] + 1; long long int q = v[source][i]; m = min(m, s[q]); if (s[q] < s[parent[source]]) { flag = 1; break; } } a[source] = m - s[parent[source]]; for (int i = 0; i < v[source].size(); i++) { long long int q = v[source][i]; a[q] = s[q] - m; dfs(v, n, s, a, v[source][i], h, visited); } } } } int main() { int n; cin >> n; vector<long long int> v[n + 1]; for (int i = 2; i <= n; i++) { long long p; cin >> p; parent[i] = p; v[p].push_back(i); } long long s[n + 1], a[n + 1]; for (int i = 1; i <= n; i++) cin >> s[i]; a[1] = s[1]; bool visited[n + 1]; long long int h[n + 1]; h[1] = 1; memset(visited, n + 1, false); dfs(v, n, s, a, 1, h, visited); if (flag == 1) cout << "-1" << endl; else { long long sum = 0; for (int i = 1; i <= n; i++) sum = sum + a[i]; cout << sum << endl; } }
1,600
CPP
n,x,y = list(map(int,input().split())) a = input() b = list(a) count = 0 #print(b) if b[len(b)-y-1]=='0': b[len(b)-y-1] = '1' count+=1 #print(len(b)-1,len(b)-x-1,len(b)-y-1) for i in range(len(b)-1,len(b)-x-1,-1): if i!=(len(b) - y-1) and b[i]=='1': b[i] = '0' count+=1 print(count)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { char str[80]; string pass = ""; cin >> str; string bins[10]; for (int i = 0; i < 10; i++) cin >> bins[i]; for (int i = 0; i < 80; i = i + 10) { string temp = ""; for (int x = 0; x < 10; x++) temp += str[x + i]; for (int x = 0; x < 10; x++) if (bins[x] == temp) pass += to_string(x); } cout << pass; }
900
CPP
print("".join([str(x) for x in range (1,1000)])[int(input())-1])
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, t, a[200005]; int main() { ios::sync_with_stdio(0); cin >> n >> t; int tt = t - 1; for (int i = 1; i <= n; i++) cin >> a[i]; priority_queue<int> q; int ans = 0; t--; for (int i = 1; i <= min(n, tt); i++) { t--; q.push(a[i] - i); while (!q.empty() && q.top() > t) q.pop(); ans = max(ans, (int)q.size()); } cout << ans << endl; return 0; }
2,200
CPP
#include <bits/stdc++.h> #pragma GCC optimize ("O3", "unroll-all-loops") #pragma GCC target ("sse4.2") using namespace std; #define F first #define S second typedef long long ll; typedef long double ld; typedef pair<ll, ll> pll; typedef pair<int, int> pii; ifstream in; ofstream out; const long long kk = 1000; const long long ml = kk * kk; const long long mod = ml * kk + 7; const long long inf = ml * ml * ml + 7; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ll n; vector<ll> v, p; bool viv = false; void solve() { cin >> n; v.resize(n); p.resize(n); for (auto &i : v) cin >> i, i--; for (auto &i : p) cin >> i; vector<vector<ll>> u(n), pr(n); for (int i = 0; i < n; i++) u[v[i]].push_back(p[i]); for (int i = 0; i < n; i++) { sort(u[i].rbegin(), u[i].rend()); } sort(u.begin(), u.end(), [](const vector<ll> &a, const vector<ll> &b) { return a.size() > b.size(); }); for (int i = 0; i < n; i++) { pr[i].push_back(0); for (auto val : u[i]) pr[i].push_back(pr[i].back() + val); } // for (int i = 0; i < n; i++) // cout << '\t' << u[i].size() << " - " << pr[i].size() << '\n'; for (int k = 1; k <= n; k++) { ll ans = 0; while (u.size() && u.back().size() < k) { u.pop_back(); pr.pop_back(); } for (int j = 0; j < u.size(); j++) { int sz = u[j].size() / k * k; // cout << "\t" << j << ": " << sz << '\n'; // if (sz > pr[j].size()) { // cout << "BAD" << endl; // cout << k << ' ' << u[j].size() << ' ' << sz << ' ' << pr[j].size() << endl; // exit(1); // } ans += pr[j][sz]; } cout << ans << '\n'; } cout << '\n'; } int main() { // viv = true; ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll t = 1; cin >> t; while (t--) solve(); return 0; }
1,400
CPP
t = int(input()) for _ in range(t): cards= int(input()) ans = 0 while cards > 1: case = 0 height = 0 cum = 0;level = 0 while cum <= cards: cards -= cum level += 1 height = (level-1) case = 2*level cum = case+height ans += 1 print(ans)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 666666; const int INF = 1e9; int sum[maxn]; int n, k; int a[maxn]; int b[maxn]; int t[maxn]; int minval; int maxval; int main() { bool flag = false; scanf("%d%d", &n, &k); int tt = 1; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); if (a[i] != 0) t[tt++] = a[i]; } tt--; for (int i = 1; i <= k; i++) scanf("%d", &b[i]); for (int i = 1; i < tt; i++) if (t[i + 1] < t[i]) flag = true; for (int i = 1; i < k; i++) { if (b[i] != b[i + 1]) { flag = true; break; } } if (!flag) { for (int i = n; i > 1; i--) { if (a[i] == 0) { if (a[i - 1] != 0 && a[i - 1] > b[1]) flag = true; } } for (int i = n - 1; i >= 1; i--) { if (a[i] == 0) { if (a[i + 1] != 0 && a[i + 1] < b[1]) flag = true; } } } if (flag) printf("Yes"); else printf("No"); return 0; }
900
CPP
n=int(input()) a=input().split() for i in range(n): a[i]=int(a[i]) a=sorted(a) if n%2==0: print(a[(n//2)-1]) else: print(a[(n//2)])
800
PYTHON3
#!/usr/bin/env python import os import sys from io import BytesIO, IOBase def main(): for _ in range(int(input())): n = int(input()) print(" ".join(map(str, range(2 * n , 3 * n)))) # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main()
800
PYTHON3
import sys import math import collections import heapq import decimal input=sys.stdin.readline n=int(input()) s=input() s1=set() c=0 for i in range(n): s1.add(s[i]) if(len(s1)==3): c+=1 s1=set([s[i]]) elif((s[i]=='L' and 'R' in s1) or (s[i]=='R' and 'L' in s1) or (s[i]=='U' and 'D' in s1) or (s[i]=='D' and 'U' in s1)): c+=1 s1=set([s[i]]) print(c+1)
1,400
PYTHON3
#include <bits/stdc++.h> int main() { int i, j, n, k, arr[101], arr2[101], count = 0, sum = 0, temp = 0, temp2 = 0, temp3 = 0, temp4, flag = 0; scanf("%d %d", &n, &k); for (i = 0; i < n; i++) { scanf("%d", &arr2[i]); } for (i = 0; i < n; i++) { arr[i] = arr2[i]; } for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (arr[i] > arr[j]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } for (i = 0; i < n; i++) { count++; sum = sum + arr[i]; if (sum == k) { temp2 = i; break; } else if (sum > k) { count--; if (i == 0) { flag = 1; temp2 = 0; } else temp2 = (i - 1); break; } } if (sum < k) { printf("%d\n", n); for (i = 1; i <= n; i++) { printf("%d ", i); } } else if (flag == 1) printf("0\n"); else { printf("%d\n", count); for (i = 0; i <= temp2; i++) { for (j = 0; j < n; j++) { if (arr[i] == arr2[j]) { arr2[j] = arr2[j] + 100; printf("%d ", j + 1); break; } } } } }
1,000
CPP
#include <bits/stdc++.h> using namespace std; int give[101]; int take[101]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; give[a] += c; take[b] += c; } int sum = 0; for (int i = 1; i <= n; i++) { int mn = min(give[i], take[i]); give[i] -= mn; take[i] -= mn; sum += give[i]; } cout << sum << endl; }
1,300
CPP
#include <bits/stdc++.h> int main() { int n, k = 0; char s1[200000], s2[200000]; scanf("%s%s", s1, s2); n = strlen(s1); for (int i = 0; i < n; i++) { if (s1[i] != s2[i]) { k++; } } if (k % 2 != 0) { printf("impossible"); n = 0; } int s = 0; for (int i = 0; i < n; i++) { if (s1[i] == s2[i]) { printf("0"); } else { if (s % 2) { printf("%c", s1[i]); } else { printf("%c", s2[i]); } s++; } } }
1,100
CPP
for _ in " "*int(input()): n=int(input()) s=input() print(s[n-1]*n)
800
PYTHON3
from sys import stdin input() results = '' for line in stdin: a, b = map(int, line.split()) if a % b: results += f'{-a % b}\n' else: results += '0\n' print(results[:-1])
800
PYTHON3
n,m=map(int,input().split()) d=dict(reversed(input().split()) for _ in range(n)) for _ in range(m): s=input() print(s+' #'+d[s.split()[1][:-1]])
900
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[100000 + 10]; int main() { int n; cin >> n; cout << "0" << " " << "0" << " " << n; }
900
CPP
n=int(input()) for i in range(n): s=input() if(s.endswith('po')): print("FILIPINO") elif(s.endswith('desu') or s.endswith('masu')): print("JAPANESE") else: print("KOREAN")
800
PYTHON3
import sys t = int(input()) for i in range(t): n, k = map(int, sys.stdin.readline().strip().split()) m = k-1 restungerade = n - m restgerade = n - 2*m if restungerade > 0 and (restungerade % 2): print('YES') print(' '.join(m * ['1'] + [str(restungerade)])) elif restgerade > 0 and not (restgerade % 2): print('YES') print(' '.join(m * ['2'] + [str(restgerade)])) else: print("NO")
1,200
PYTHON3
import sys,math,itertools from collections import Counter,deque,defaultdict from bisect import bisect_left,bisect_right from heapq import heappop,heappush,heapify, nlargest from copy import deepcopy,copy mod = 10**9+7 INF = float('inf') def inp(): return int(sys.stdin.readline()) def inpl(): return list(map(int, sys.stdin.readline().split())) def inpl_1(): return list(map(lambda x:int(x)-1, sys.stdin.readline().split())) def inps(): return sys.stdin.readline() def inpsl(x): tmp = sys.stdin.readline(); return list(tmp[:x]) def err(x): print(x); exit() class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): return len(self.roots()) n,m = inpl() un = [set() for _ in range(n)] for _ in range(m): a,b = inpl_1() un[b].add(a); un[a].add(b) uf = UnionFind(n) seen = [0]*n unknown = set(range(n)) for i in range(n): if seen[i]: continue seen[i] = 1 unknown.discard(i) adj = deque() noadj = set() for j in unknown: if not j in un[i]: adj.append(j) else: noadj.add(j) while adj: j = adj.popleft() for k in noadj - un[j]: adj.append(k) noadj.discard(k) seen[j] = 1 unknown.discard(j) uf.union(i,j) res = [] for root in uf.roots(): res.append(uf.size(root)) res.sort() print(len(res)) print(*res)
2,100
PYTHON3
order = list(input()) if 'H' in order or 'Q' in order or '9' in order: print('YES') else: print('NO')
900
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAX = 1000005; vector<int> v[MAX]; int a[MAX], sum[MAX], st[MAX], le[MAX], ri[MAX]; int get_cnt(int l, int r, int x) { if (l >= r) return 0; return lower_bound(v[x].begin(), v[x].end(), r) - lower_bound(v[x].begin(), v[x].end(), l); } int main() { ios::sync_with_stdio(false); int n, k; cin >> n >> k; v[0].push_back(0); for (int i = 0; i < n; i++) { cin >> a[i]; sum[i + 1] = (sum[i] + a[i]) % k; v[sum[i + 1]].push_back(i + 1); } int t = 0; for (int i = 0; i < n; i++) { while (t > 0 && a[st[t - 1]] < a[i]) t--; le[i] = -1; if (t) le[i] = st[t - 1]; st[t++] = i; } t = 0; for (int i = n - 1; i >= 0; i--) { while (t > 0 && a[st[t - 1]] <= a[i]) t--; ri[i] = n; if (t) ri[i] = st[t - 1]; st[t++] = i; } for (int i = 0; i < n; i++) a[i] %= k; long long ans = 0; for (int i = 0; i < n; i++) { if (i - le[i] < ri[i] - i) { for (int j = i; j > le[i]; j--) if (j == i) ans += get_cnt(i + 2, ri[i] + 1, (sum[j] + a[i]) % k); else ans += get_cnt(i + 1, ri[i] + 1, (sum[j] + a[i]) % k); } else { for (int j = i + 1; j <= ri[i]; j++) if (j == i + 1) ans += get_cnt(le[i] + 1, i, (sum[j] - a[i] + k) % k); else ans += get_cnt(le[i] + 1, i + 1, (sum[j] - a[i] + k) % k); } } cout << ans << endl; return 0; }
2,800
CPP
#include <bits/stdc++.h> using namespace std; int fa[1050], used[100500], u[100500], v[100500]; char k[100500]; int n, m; int fin(int u) { return fa[u] == u ? u : fa[u] = fin(fa[u]); } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) fa[i] = i; int num = 0; for (int i = 1; i <= m; ++i) { scanf("%d%d %c", &u[i], &v[i], &k[i]); int x = fin(u[i]); int y = fin(v[i]); if (x != y) { fa[x] = y; used[i] = 1; if (k[i] == 'S') num++; } } if (n == 1) { printf("0\n"); return 0; } if ((n - 1) % 2) { printf("-1\n"); return 0; } if (num * 2 + 1 != n) { for (int i = 1; i <= n; ++i) fa[i] = i; if (num * 2 + 1 < n) { for (int i = 1; i <= m; ++i) if (used[i] && k[i] == 'S') fa[fin(u[i])] = fin(v[i]); else used[i] = 0; for (int i = 1; i <= m; ++i) if (!used[i] && k[i] == 'S') { int x = fin(u[i]); int y = fin(v[i]); if (x == y) continue; fa[x] = y; used[i] = 1; num++; if (2 * num + 1 == n) break; } for (int i = 1; i <= m; ++i) if (!used[i] && k[i] == 'M') { int x = fin(u[i]); int y = fin(v[i]); if (x != y) { fa[x] = y; used[i] = 1; } } } else { for (int i = 1; i <= m; ++i) if (used[i] && k[i] == 'M') fa[fin(u[i])] = fin(v[i]); else used[i] = 0; for (int i = 1; i <= m; ++i) if (!used[i] && k[i] == 'M') { int x = fin(u[i]); int y = fin(v[i]); if (x == y) continue; fa[x] = y; used[i] = 1; num--; if (2 * num + 1 == n) break; } for (int i = 1; i <= m; ++i) if (!used[i] && k[i] == 'S') { int x = fin(u[i]); int y = fin(v[i]); if (x != y) { fa[x] = y; used[i] = 1; } } } } if (2 * num + 1 == n) { printf("%d\n", n - 1); int z = 0; for (int i = 1; i <= m; ++i) if (used[i]) { printf("%d", i); z++; if (z < n - 1) printf(" "); else printf("\n"); } } else printf("-1\n"); return 0; }
2,300
CPP
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } long long powm(long long base, long long exp, long long mod = 1000000007) { base %= mod; long long ans = 1; while (exp) { if (exp & 1LL) ans = (ans * base) % mod; exp >>= 1LL, base = (base * base) % mod; } return ans; } long long a[22]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; long long sum = 0; for (long long i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; } if (n < 2 || (n == 2 && a[1] == a[2])) { cout << -1 << '\n'; return 0; } if (sum & 1) { cout << 1 << '\n'; cout << 1 << '\n'; return 0; } vector<long long> ans; long long t = 0; for (int i = 1; i <= 10; i++) { t += a[i]; ans.push_back(i); if (t != sum / 2) break; } cout << ans.size() << '\n'; for (auto it : ans) cout << it << " "; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 100; vector<pair<int, int> > E[MAXN]; vector<int> answer[MAXN]; int seen[MAXN]; set<int> added[MAXN]; void dfs(int u) { seen[u] = 1; int pointer = 1; for (pair<int, int> edge : E[u]) { int v = edge.first; int id = edge.second; if (seen[v]) continue; while (added[pointer].count(u) || added[pointer].count(v)) { pointer++; } answer[pointer].push_back(id); added[pointer].insert(u); added[pointer].insert(v); dfs(v); } } int main() { int n; cin >> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; E[u].push_back(make_pair(v, i)); E[v].push_back(make_pair(u, i)); } int maxi = 0; int best = -1; for (int i = 1; i <= n; i++) { int sz = E[i].size(); if (sz > maxi) { maxi = sz; best = i; } } cout << maxi << endl; dfs(best); for (int i = 1; i <= maxi; i++) { cout << answer[i].size(); for (int v : answer[i]) cout << " " << v; cout << endl; } return 0; }
1,800
CPP
t = int(input()) while t > 0: n , m = [int(i) for i in input().split(" ")] l = [int(i) for i in input().split(" ")] if sum(l) == m: print("YES") else: print("NO") t -= 1
800
PYTHON3
s = str(input()) big = 0 small = 0 for i in s: if i in 'QWERTYUIOPASDFGHJKLZXCVBNM': big += 1 elif i in 'qwertyuiopasdfghjklzxcvbnm': small += 1 if big <= small: print(s.lower()) else: print(s.upper())
800
PYTHON3
import sys inf = float("inf") # sys.setrecursionlimit(1000000) # abc='abcdefghijklmnopqrstuvwxyz' # abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25} mod,MOD=1000000007,998244353 # vow=['a','e','i','o','u'] # dx,dy=[-1,1,0,0],[0,0,1,-1] # from collections import deque, Counter, OrderedDict,defaultdict # from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace # from math import ceil,floor,log,sqrt,factorial,pow,pi,gcd,log10,atan,tan # from bisect import bisect_left,bisect_right def get_array(): return list(map(int , sys.stdin.readline().strip().split())) def get_ints(): return map(int, sys.stdin.readline().strip().split()) def input(): return sys.stdin.readline().strip() n,m = get_ints() Arr = get_array() last = 1 ans = 0 for i in range(m): if Arr[i]==last: continue elif Arr[i]>last: ans += (Arr[i] - last) last = Arr[i] else: ans+=(n-last)+Arr[i] last = Arr[i] print(ans)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 100000; const int maxw = 1000000; const int oo = (int)1e9; int n, m; int x[maxn + 1], y[maxn + 1], w[maxn + 1]; map<int, int> best[maxn + 1]; map<int, int> RR; int res; int getBIT(int id, int x) { int res = 0; for (; x > 0; x &= x - 1) { if (best[id].count(x)) res = max(res, best[id][x]); } return res; } void updateBIT(int id, int x, int v) { for (; x <= RR.size(); x += x & (-x)) { if (best[id].count(x)) best[id][x] = max(best[id][x], v); else best[id][x] = v; } } int main() { ios_base::sync_with_stdio(0); cin >> n >> m; for (int i = 1; i <= n; i++) best[i].clear(); res = 0; for (int i = 1; i <= m; i++) { cin >> x[i] >> y[i] >> w[i]; RR[w[i]] = 0; } int dem = 0; for (auto it = RR.begin(); it != RR.end(); it++) { dem++; (*it).second = dem; } for (int i = 1; i <= m; i++) w[i] = RR[w[i]]; for (int i = 1; i <= m; i++) { cin >> x[i] >> y[i] >> w[i]; int p = getBIT(x[i], w[i]); updateBIT(y[i], w[i], p + 1); res = max(res, p + 1); } cout << res; return 0; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; int ABS(int a) { if (a < 0) return (-a); return a; } int main() { string s[2]; int n, k; cin >> n >> k; cin >> s[0] >> s[1]; bool vis[2][200005] = {0}; bool ans = false; queue<pair<int, int> > q; queue<int> dq; q.push(pair<int, int>(0, 0)); dq.push(0); vis[0][0] = true; while (!q.empty()) { pair<int, int> x = q.front(); int depth = dq.front(); q.pop(); dq.pop(); bool wall = x.first; int idx = x.second; if (idx + k > n) { ans = true; break; } if (!vis[wall][idx + 1]) { if (idx + 1 >= depth + 1) { if (idx + 1 < n && s[wall][idx + 1] == 'X') ; else { q.push(pair<int, int>(wall, idx + 1)); dq.push(depth + 1); vis[wall][idx + 1] = true; } } } if (idx - 1 > 0 && !vis[wall][idx - 1]) { if (idx - 1 >= depth + 1) { if (idx - 1 >= 0 && s[wall][idx - 1] == 'X') ; else { q.push(pair<int, int>(wall, idx - 1)); dq.push(depth + 1); vis[wall][idx - 1] = true; } } } if (!vis[!wall][idx + k]) { if (idx + k >= depth + 1) { if (idx + k < n && s[!wall][idx + k] == 'X') ; else { q.push(pair<int, int>(!wall, idx + k)); dq.push(depth + 1); vis[!wall][idx + k] = true; } } } } cout << (ans ? "YES" : "NO") << endl; return 0; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; int main() { int n; cin >> n; int dp[2 * n + 1][n + 1]; for (int i = 0; i < n + 1; i++) dp[0][i] = 0; dp[0][0] = 1; for (int i = 1; i <= 2 * n; i++) { for (int j = 0; j <= n; j++) dp[i][j] = 0; dp[i][1] += dp[i - 1][0]; for (int j = 1; j < n; j++) { dp[i][j - 1] += dp[i - 1][j]; dp[i][j + 1] += dp[i - 1][j]; } dp[i][n - 1] += dp[i - 1][n]; for (int j = 0; j <= n; j++) { dp[i][j] %= MOD; } } for (int i = 0; i < n; i++) { for (int j = n + 1; j < 2 * n + 1 - i; j++) { dp[j + i][n - i] = 0; } } int ans = 0; for (int i = 1; i <= 2 * n; i += 2) { for (int j = 0; j <= n; j++) { ans += dp[i][j]; ans %= MOD; } } cout << ans; }
2,100
CPP
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const long long mod = (1e+9) + 7; const long long sz = 2e6 + 9; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long power(long long a, long long b, long long m = mod) { long long res = 1; while (b > 0) { if (b & 1) res = (res * a) % m; b = b >> 1; a = (a * a) % m; } return res; } void solve() { long long t; cin >> t; while (t--) { long long a, b, x, y; cin >> a >> b >> x >> y; long long ans = (a - x - 1) * b; ans = max(ans, (b - y - 1) * a); ans = max(ans, (x)*b); ans = max(ans, (y)*a); cout << ans << endl; } } int main() { clock_t beg = clock(); solve(); clock_t end = clock(); fprintf(stderr, "%.3f sec, Copyright %c 2019 Skyscraper. \n", double(end - beg) / CLOCKS_PER_SEC, 184); return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; inline char get_char(); const int N = 3e6 + 10; int T, n, cnt = 1; int c[40], trie[N][3]; int rec[N]; inline void change(long long x) { memset(c, 0, sizeof(c)); int ct = 0; while (x) c[++ct] = x & 1, x >>= 1; } inline int reply() { int p = 1, s = 0; for (register int i = 33; i >= 1; i--) { int &k = c[i]; s <<= 1; if (!trie[p][k ^ 1] or !rec[trie[p][k ^ 1]]) { if (trie[p][k] and rec[trie[p][k]]) p = trie[p][k]; else break; } else p = trie[p][k ^ 1], s |= 1; } return s; } inline void insert() { int p = 1; for (register int i = 33; i >= 1; i--) { int &k = c[i]; if (!trie[p][k]) trie[p][k] = ++cnt; p = trie[p][k], rec[p]++; } } inline void _delete() { int p = 1; for (register int i = 33; i >= 1; i--) { int &k = c[i]; if (!trie[p][k] or !rec[trie[p][k]]) return; p = trie[p][k]; rec[p]--; } } int main() { scanf("%d", &n); insert(); while (n--) { static char c; static int x; c = get_char(); scanf("%d", &x); change(x); if (c == '?') printf("%d\n", reply()); else if (c == '+') insert(); else _delete(); } return 0; } inline char get_char() { register char c = getchar(); while (c != '+' and c != '-' and c != '?') c = getchar(); return c; }
1,800
CPP
'''input aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbb ''' a, b = input(), input() if a == b: print(-1) else: print(len(max(a, b, key=len)))
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int t, i, j, n, x, ans, *a; long long int s; cin >> t; while (t--) { cin >> n >> x; a = new int[n]; for (i = s = 0; i < n; i++) { cin >> a[i]; s += a[i]; } if (s % x != 0) cout << n; else { j = n; for (i = 0; i < n; i++) { if (a[i] % x != 0) break; else j--; } if (i == n) cout << -1; else { j--; for (i = n; i > 0; i--) { if (a[i - 1] % x != 0) break; else n--; } n--; if (j > n) cout << j; else cout << n; } } cout << endl; } }
1,200
CPP
#include <bits/stdc++.h> using namespace std; const int N9 = 1e9 + 5; int l, r; long long ans; long long a[1025] = { 0, 4, 7, 44, 47, 74, 77, 444, 447, 474, 477, 744, 747, 774, 777, 4444, 4447, 4474, 4477, 4744, 4747, 4774, 4777, 7444, 7447, 7474, 7477, 7744, 7747, 7774, 7777, 44444, 44447, 44474, 44477, 44744, 44747, 44774, 44777, 47444, 47447, 47474, 47477, 47744, 47747, 47774, 47777, 74444, 74447, 74474, 74477, 74744, 74747, 74774, 74777, 77444, 77447, 77474, 77477, 77744, 77747, 77774, 77777, 444444, 444447, 444474, 444477, 444744, 444747, 444774, 444777, 447444, 447447, 447474, 447477, 447744, 447747, 447774, 447777, 474444, 474447, 474474, 474477, 474744, 474747, 474774, 474777, 477444, 477447, 477474, 477477, 477744, 477747, 477774, 477777, 744444, 744447, 744474, 744477, 744744, 744747, 744774, 744777, 747444, 747447, 747474, 747477, 747744, 747747, 747774, 747777, 774444, 774447, 774474, 774477, 774744, 774747, 774774, 774777, 777444, 777447, 777474, 777477, 777744, 777747, 777774, 777777, 4444444, 4444447, 4444474, 4444477, 4444744, 4444747, 4444774, 4444777, 4447444, 4447447, 4447474, 4447477, 4447744, 4447747, 4447774, 4447777, 4474444, 4474447, 4474474, 4474477, 4474744, 4474747, 4474774, 4474777, 4477444, 4477447, 4477474, 4477477, 4477744, 4477747, 4477774, 4477777, 4744444, 4744447, 4744474, 4744477, 4744744, 4744747, 4744774, 4744777, 4747444, 4747447, 4747474, 4747477, 4747744, 4747747, 4747774, 4747777, 4774444, 4774447, 4774474, 4774477, 4774744, 4774747, 4774774, 4774777, 4777444, 4777447, 4777474, 4777477, 4777744, 4777747, 4777774, 4777777, 7444444, 7444447, 7444474, 7444477, 7444744, 7444747, 7444774, 7444777, 7447444, 7447447, 7447474, 7447477, 7447744, 7447747, 7447774, 7447777, 7474444, 7474447, 7474474, 7474477, 7474744, 7474747, 7474774, 7474777, 7477444, 7477447, 7477474, 7477477, 7477744, 7477747, 7477774, 7477777, 7744444, 7744447, 7744474, 7744477, 7744744, 7744747, 7744774, 7744777, 7747444, 7747447, 7747474, 7747477, 7747744, 7747747, 7747774, 7747777, 7774444, 7774447, 7774474, 7774477, 7774744, 7774747, 7774774, 7774777, 7777444, 7777447, 7777474, 7777477, 7777744, 7777747, 7777774, 7777777, 44444444, 44444447, 44444474, 44444477, 44444744, 44444747, 44444774, 44444777, 44447444, 44447447, 44447474, 44447477, 44447744, 44447747, 44447774, 44447777, 44474444, 44474447, 44474474, 44474477, 44474744, 44474747, 44474774, 44474777, 44477444, 44477447, 44477474, 44477477, 44477744, 44477747, 44477774, 44477777, 44744444, 44744447, 44744474, 44744477, 44744744, 44744747, 44744774, 44744777, 44747444, 44747447, 44747474, 44747477, 44747744, 44747747, 44747774, 44747777, 44774444, 44774447, 44774474, 44774477, 44774744, 44774747, 44774774, 44774777, 44777444, 44777447, 44777474, 44777477, 44777744, 44777747, 44777774, 44777777, 47444444, 47444447, 47444474, 47444477, 47444744, 47444747, 47444774, 47444777, 47447444, 47447447, 47447474, 47447477, 47447744, 47447747, 47447774, 47447777, 47474444, 47474447, 47474474, 47474477, 47474744, 47474747, 47474774, 47474777, 47477444, 47477447, 47477474, 47477477, 47477744, 47477747, 47477774, 47477777, 47744444, 47744447, 47744474, 47744477, 47744744, 47744747, 47744774, 47744777, 47747444, 47747447, 47747474, 47747477, 47747744, 47747747, 47747774, 47747777, 47774444, 47774447, 47774474, 47774477, 47774744, 47774747, 47774774, 47774777, 47777444, 47777447, 47777474, 47777477, 47777744, 47777747, 47777774, 47777777, 74444444, 74444447, 74444474, 74444477, 74444744, 74444747, 74444774, 74444777, 74447444, 74447447, 74447474, 74447477, 74447744, 74447747, 74447774, 74447777, 74474444, 74474447, 74474474, 74474477, 74474744, 74474747, 74474774, 74474777, 74477444, 74477447, 74477474, 74477477, 74477744, 74477747, 74477774, 74477777, 74744444, 74744447, 74744474, 74744477, 74744744, 74744747, 74744774, 74744777, 74747444, 74747447, 74747474, 74747477, 74747744, 74747747, 74747774, 74747777, 74774444, 74774447, 74774474, 74774477, 74774744, 74774747, 74774774, 74774777, 74777444, 74777447, 74777474, 74777477, 74777744, 74777747, 74777774, 74777777, 77444444, 77444447, 77444474, 77444477, 77444744, 77444747, 77444774, 77444777, 77447444, 77447447, 77447474, 77447477, 77447744, 77447747, 77447774, 77447777, 77474444, 77474447, 77474474, 77474477, 77474744, 77474747, 77474774, 77474777, 77477444, 77477447, 77477474, 77477477, 77477744, 77477747, 77477774, 77477777, 77744444, 77744447, 77744474, 77744477, 77744744, 77744747, 77744774, 77744777, 77747444, 77747447, 77747474, 77747477, 77747744, 77747747, 77747774, 77747777, 77774444, 77774447, 77774474, 77774477, 77774744, 77774747, 77774774, 77774777, 77777444, 77777447, 77777474, 77777477, 77777744, 77777747, 77777774, 77777777, 444444444, 444444447, 444444474, 444444477, 444444744, 444444747, 444444774, 444444777, 444447444, 444447447, 444447474, 444447477, 444447744, 444447747, 444447774, 444447777, 444474444, 444474447, 444474474, 444474477, 444474744, 444474747, 444474774, 444474777, 444477444, 444477447, 444477474, 444477477, 444477744, 444477747, 444477774, 444477777, 444744444, 444744447, 444744474, 444744477, 444744744, 444744747, 444744774, 444744777, 444747444, 444747447, 444747474, 444747477, 444747744, 444747747, 444747774, 444747777, 444774444, 444774447, 444774474, 444774477, 444774744, 444774747, 444774774, 444774777, 444777444, 444777447, 444777474, 444777477, 444777744, 444777747, 444777774, 444777777, 447444444, 447444447, 447444474, 447444477, 447444744, 447444747, 447444774, 447444777, 447447444, 447447447, 447447474, 447447477, 447447744, 447447747, 447447774, 447447777, 447474444, 447474447, 447474474, 447474477, 447474744, 447474747, 447474774, 447474777, 447477444, 447477447, 447477474, 447477477, 447477744, 447477747, 447477774, 447477777, 447744444, 447744447, 447744474, 447744477, 447744744, 447744747, 447744774, 447744777, 447747444, 447747447, 447747474, 447747477, 447747744, 447747747, 447747774, 447747777, 447774444, 447774447, 447774474, 447774477, 447774744, 447774747, 447774774, 447774777, 447777444, 447777447, 447777474, 447777477, 447777744, 447777747, 447777774, 447777777, 474444444, 474444447, 474444474, 474444477, 474444744, 474444747, 474444774, 474444777, 474447444, 474447447, 474447474, 474447477, 474447744, 474447747, 474447774, 474447777, 474474444, 474474447, 474474474, 474474477, 474474744, 474474747, 474474774, 474474777, 474477444, 474477447, 474477474, 474477477, 474477744, 474477747, 474477774, 474477777, 474744444, 474744447, 474744474, 474744477, 474744744, 474744747, 474744774, 474744777, 474747444, 474747447, 474747474, 474747477, 474747744, 474747747, 474747774, 474747777, 474774444, 474774447, 474774474, 474774477, 474774744, 474774747, 474774774, 474774777, 474777444, 474777447, 474777474, 474777477, 474777744, 474777747, 474777774, 474777777, 477444444, 477444447, 477444474, 477444477, 477444744, 477444747, 477444774, 477444777, 477447444, 477447447, 477447474, 477447477, 477447744, 477447747, 477447774, 477447777, 477474444, 477474447, 477474474, 477474477, 477474744, 477474747, 477474774, 477474777, 477477444, 477477447, 477477474, 477477477, 477477744, 477477747, 477477774, 477477777, 477744444, 477744447, 477744474, 477744477, 477744744, 477744747, 477744774, 477744777, 477747444, 477747447, 477747474, 477747477, 477747744, 477747747, 477747774, 477747777, 477774444, 477774447, 477774474, 477774477, 477774744, 477774747, 477774774, 477774777, 477777444, 477777447, 477777474, 477777477, 477777744, 477777747, 477777774, 477777777, 744444444, 744444447, 744444474, 744444477, 744444744, 744444747, 744444774, 744444777, 744447444, 744447447, 744447474, 744447477, 744447744, 744447747, 744447774, 744447777, 744474444, 744474447, 744474474, 744474477, 744474744, 744474747, 744474774, 744474777, 744477444, 744477447, 744477474, 744477477, 744477744, 744477747, 744477774, 744477777, 744744444, 744744447, 744744474, 744744477, 744744744, 744744747, 744744774, 744744777, 744747444, 744747447, 744747474, 744747477, 744747744, 744747747, 744747774, 744747777, 744774444, 744774447, 744774474, 744774477, 744774744, 744774747, 744774774, 744774777, 744777444, 744777447, 744777474, 744777477, 744777744, 744777747, 744777774, 744777777, 747444444, 747444447, 747444474, 747444477, 747444744, 747444747, 747444774, 747444777, 747447444, 747447447, 747447474, 747447477, 747447744, 747447747, 747447774, 747447777, 747474444, 747474447, 747474474, 747474477, 747474744, 747474747, 747474774, 747474777, 747477444, 747477447, 747477474, 747477477, 747477744, 747477747, 747477774, 747477777, 747744444, 747744447, 747744474, 747744477, 747744744, 747744747, 747744774, 747744777, 747747444, 747747447, 747747474, 747747477, 747747744, 747747747, 747747774, 747747777, 747774444, 747774447, 747774474, 747774477, 747774744, 747774747, 747774774, 747774777, 747777444, 747777447, 747777474, 747777477, 747777744, 747777747, 747777774, 747777777, 774444444, 774444447, 774444474, 774444477, 774444744, 774444747, 774444774, 774444777, 774447444, 774447447, 774447474, 774447477, 774447744, 774447747, 774447774, 774447777, 774474444, 774474447, 774474474, 774474477, 774474744, 774474747, 774474774, 774474777, 774477444, 774477447, 774477474, 774477477, 774477744, 774477747, 774477774, 774477777, 774744444, 774744447, 774744474, 774744477, 774744744, 774744747, 774744774, 774744777, 774747444, 774747447, 774747474, 774747477, 774747744, 774747747, 774747774, 774747777, 774774444, 774774447, 774774474, 774774477, 774774744, 774774747, 774774774, 774774777, 774777444, 774777447, 774777474, 774777477, 774777744, 774777747, 774777774, 774777777, 777444444, 777444447, 777444474, 777444477, 777444744, 777444747, 777444774, 777444777, 777447444, 777447447, 777447474, 777447477, 777447744, 777447747, 777447774, 777447777, 777474444, 777474447, 777474474, 777474477, 777474744, 777474747, 777474774, 777474777, 777477444, 777477447, 777477474, 777477477, 777477744, 777477747, 777477774, 777477777, 777744444, 777744447, 777744474, 777744477, 777744744, 777744747, 777744774, 777744777, 777747444, 777747447, 777747474, 777747477, 777747744, 777747747, 777747774, 777747777, 777774444, 777774447, 777774474, 777774477, 777774744, 777774747, 777774774, 777774777, 777777444, 777777447, 777777474, 777777477, 777777744, 777777747, 777777774, 777777777, 4444444444}; vector<long long> hav; int ch(int x) { while (x) { if (x % 10 != 4 && x % 10 != 7) return 0; x /= 10; } return 1; } int main() { cin >> l >> r; auto x = lower_bound(a + 1, a + 1024, l) - a; for (int i = x; i <= 1023; ++i) { hav.push_back(a[i]); if (a[i] >= r) break; } for (int i = 0; i < hav.size(); ++i) { if (hav[i] <= r) { ans += hav[i] * (hav[i] - l + 1); l = hav[i] + 1; } else { ans += hav[i] * (r - l + 1); break; } } cout << ans; }
1,100
CPP
s = input() words = '' g = 'aeiou' i=0 while(len(s)>2): if((s[i] not in g)and(s[i+1] not in g)and(s[i+2]not in g)): if(s[i]==s[i+1]==s[i+2]): i+=1 if(i==len(s)-2): break continue words+=s[:i+2]+' ' s = s[i+2:] i=0 else: i+=1 if(i==len(s)-2): break print(words+s)
1,500
PYTHON3
n, m = map(int, input().split()) s = 0 a = list(map(int, input().split())) for i in range(n): s += a[i] print(s // m, end=' ') s %= m
900
PYTHON3
import math #constraints n = 1000000 ### #sieve of erastotheses prime = {} p = 2 while (p * p <= n): if p not in prime: for i in range(p * p, n+1, p): prime[i] = "composite" p += 1 ####################### #print(prime) N = int(input()) x = map(int, input().split()) for e in x: pr = (math.sqrt(e)) #print(pr) if pr not in prime and pr == int(pr) and pr != 1: print("YES") else: print("NO")
1,300
PYTHON3
from sys import stdin, stdout, setrecursionlimit #import threading # tail-recursion optimization # In case of tail-recusion optimized code, have to use python compiler. # Otherwise, memory limit may exceed. # declare the class Tail_Recursion_Optimization class Tail_Recursion_Optimization: def __init__(self, RECURSION_LIMIT, STACK_SIZE): setrecursionlimit(RECURSION_LIMIT) threading.stack_size(STACK_SIZE) return None class SOLVE: def solve(self): R = stdin.readline #f = open('input.txt');R = f.readline W = stdout.write ans = [] for i in range(int(R())): n, k = map(int, R().split()) a = [int(x) for x in R().split()] mx = max(a) a = [mx - a[j] for j in range(n)] mx = max(a) for j in range(n): if k%2: ans.append('{} '.format(a[j])) else: ans.append('{} '.format(mx - a[j])) ans.append('\n') W(''.join(ans)) return 0 def main(): s = SOLVE() s.solve() #Tail_Recursion_Optimization(10**7, 100*1024**2) # recursion-call size, stack-size in byte (MB*1024**2) #threading.Thread(target=main).start() main()
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; struct DSU { int par[N]; DSU() { for (int i = 0; i < N; i++) par[i] = i; } int find(int u) { if (par[u] == u) return u; return par[u] = find(par[u]); } void merge(int u, int v) { par[find(v)] = find(u); } }; vector<int> rowvals[N]; vector<int> colvals[N]; bool rowhas[N], colhas[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, q; cin >> n >> m >> q; vector<pair<int, int> > st; set<pair<int, int> > st2; for (int i = 0; i < q; i++) { int x, y; cin >> x >> y; x--; y--; st.push_back(pair<int, int>(x, y)); rowvals[x].push_back(y); colvals[y].push_back(x); } DSU rows, cols; for (int i = 0; i < n; i++) { if (rowvals[i].empty()) continue; for (auto u : rowvals[i]) cols.merge(u, rowvals[i][0]); } for (int i = 0; i < m; i++) { if (colvals[i].empty()) continue; for (auto u : colvals[i]) rows.merge(u, colvals[i][0]); } for (auto pr : st) { int x = rows.find(pr.first); int y = cols.find(pr.second); rowhas[x] = 1; colhas[y] = 1; } int nn = 0, mm = 0, kn = 0, km = 0; for (int i = 0; i < n; i++) if (rows.par[i] == i) { nn++; if (rowhas[i]) kn++; } for (int i = 0; i < m; i++) if (cols.par[i] == i) { ++mm; if (colhas[i]) km++; } assert(kn == km); cout << (mm + nn) - (kn + 1) << endl; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 50; const int mod = 1e9 + 7; long long qp(long long a, long long n) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } template <class T> inline bool scan(T &ret) { char c; int sgn; if (c = getchar(), c == EOF) return 0; while (c != '-' && (c < '0' || c > '9')) c = getchar(); sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return 1; } using namespace std; long long n, m; long long du[maxn], nxt[maxn], vis[maxn]; long long sum[maxn]; vector<pair<long long, long long> > e[maxn]; vector<long long> d[maxn]; inline long long read() { char ch = getchar(); long long s = 0, f = 1; while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { s = (s << 3) + (s << 1) + ch - '0'; ch = getchar(); } return s * f; } bool cmp(pair<long long, long long> x, pair<long long, long long> y) { return du[x.first] < du[y.first]; } struct node { priority_queue<long long> A, B; void push(long long x) { A.push(x); } void del(long long x) { B.push(x); } long long top() { while (!B.empty() && A.top() == B.top()) A.pop(), B.pop(); return A.top(); } void pop() { top(); A.pop(); } long long size() { return A.size() - B.size(); } void clear() { while (A.size()) A.pop(); while (B.size()) B.pop(); } } h[maxn]; void upd(long long x, long long num) { while (h[x].size() > num) { sum[x] = sum[x] - h[x].top(); h[x].pop(); } } void upd1(long long x, long long num, vector<long long> &add) { while (h[x].size() > num) { sum[x] = sum[x] - h[x].top(); add.push_back(h[x].top()); h[x].pop(); } } void dele(long long x) { vis[x] = 1; for (long long i = 0; i < e[x].size(); i++) { long long y = e[x][i].first, c = e[x][i].second; ; if (vis[y]) continue; h[y].push(c); sum[y] = sum[y] + c; } } long long D; long long f[maxn][2], st[maxn]; void dfs(long long x) { vis[x] = 1; long long num = du[x] - D; upd(x, num); vector<long long> add, del; add.clear(); del.clear(); long long siz = e[x].size(), tot = 0; while (st[x] < siz && du[e[x][st[x]].first] <= D) st[x]++; for (long long i = st[x]; i < siz; i++) { long long y = e[x][i].first, c = e[x][i].second; ; if (vis[y]) continue; dfs(y); if (f[y][1] + c <= f[y][0]) { num--; tot = tot + f[y][1] + c; } else { tot = tot + f[y][0]; long long o = f[y][1] + c - f[y][0]; del.push_back(o); h[x].push(o); sum[x] = sum[x] + o; } } upd1(x, max(0ll, num), add); f[x][0] = tot + sum[x]; upd1(x, max(0ll, num - 1), add); f[x][1] = tot + sum[x]; for (long long i = 0; i < add.size(); i++) h[x].push(add[i]), sum[x] += add[i]; for (long long i = 0; i < del.size(); i++) h[x].del(del[i]), sum[x] -= del[i]; } int main() { int T; scanf("%d", &T); for (int kase = 1; kase <= T; ++kase) { n = read(); m = read(); D = 0; for (int i = 1; i <= n; ++i) { du[i] = nxt[i] = vis[i] = sum[i] = 0; e[i].clear(); d[i].clear(); h[i].clear(); f[i][0] = f[i][1] = st[i] = 0; } long long ans = 0; for (long long i = 1; i < n; i++) { long long x = read(), y = read(), c = read(); e[x].push_back({y, c}); e[y].push_back({x, c}); du[x]++; du[y]++; ans += c; } long long tot = ans; if (m >= n) { printf("%lld\n", ans); continue; } for (long long i = 1; i <= n; i++) { d[du[i]].push_back(i); sort(e[i].begin(), e[i].end(), cmp); } nxt[n] = n + 1; for (long long i = n - 1; i >= 1; i--) { if (d[i + 1].size()) nxt[i] = i + 1; else nxt[i] = nxt[i + 1]; } for (long long u = 1; u < n; u++) { for (long long i = 0; i < d[u].size(); i++) dele(d[u][i]); ans = 0; D = u; for (long long i = u + 1; i < n; i = nxt[i]) for (long long j = 0; j < d[i].size(); j++) { if (vis[d[i][j]]) continue; dfs(d[i][j]); ans = ans + f[d[i][j]][0]; } for (long long i = u + 1; i < n; i = nxt[i]) for (long long j = 0; j < d[i].size(); j++) vis[d[i][j]] = 0; if (u == m) { printf("%lld\n", tot - ans); break; } } } return 0; }
2,100
CPP
n = int(input()) total = int(n * (n + 1) / 2) l = [] cur = 0 if total % 2 == 0: print(0) target = total // 2 while cur != target: if target - cur <= n: l.append(target - cur) break else: l.append(n) cur += n n -= 1 else: print(1) target = total // 2 while cur != target: if target - cur <= n: l.append(target - cur) break else: l.append(n) cur += n n -= 1 print(len(l), end = ' ') print(*l)
1,300
PYTHON3
#include <bits/stdc++.h> const int N = 125; using namespace std; int a[N][5], num[5]; int n; int getmaxpoint(int all, int sov) { double x = 1.0 * sov / all; if (x > 0.5 && x <= 1) return 500; if (x > 0.25 && x <= 0.5) return 1000; if (x > 1.0 / 8 && x <= 0.25) return 1500; if (x > 1.0 / 16 && x <= 1.0 / 8) return 2000; if (x > 1.0 / 32 && x <= 1.0 / 16) return 2500; return 3000; } int getscore(bool isv, int add, int j) { int po; if (isv) { if (a[1][j] == -1) return 0; if (a[2][j] == -1) po = getmaxpoint(add + n, num[j]); if (a[1][j] <= a[2][j] || a[2][j] == -1) po = getmaxpoint(add + n, num[j]); else po = getmaxpoint(add + n, add + num[j]); return po * (1 - a[1][j] / 250.0); } else { if (a[2][j] == -1) return 0; if (a[1][j] == -1) po = getmaxpoint(add + n, add + num[j]); if (a[1][j] <= a[2][j] || a[2][j] == -1) po = getmaxpoint(add + n, num[j]); else po = getmaxpoint(add + n, add + num[j]); return po * (1 - a[2][j] / 250.0); } } int main() { ios::sync_with_stdio(0); cin >> n; for (int i = 1; i <= n; i++) { for (int j = 0; j < 5; j++) { cin >> a[i][j]; if (a[i][j] != -1) num[j]++; } } for (int add = 0; add < 33 * N; add++) { int p = 0, v = 0; for (int i = 0; i < 5; i++) { int x = getscore(true, add, i); v += x; } for (int i = 0; i < 5; i++) { int x = getscore(false, add, i); p += x; } if (v > p) { cout << add << '\n'; return 0; } } cout << -1 << '\n'; return 0; }
2,000
CPP
from sys import stdin def main(): n, x, y = map(int, stdin.readline().strip().split()) xy = x + y for health in map(int,(stdin.readline().strip()for _ in range(n))): health %= xy hitsx = health * x // xy hitsy = health * y // xy health -= hitsx + hitsy tx = hitsx * y ty = hitsy * x while health > 0: tx1, ty1 = tx + y, ty + x if tx1 < ty1: tx = tx1 health -= 1 elif tx1 > ty1: ty = ty1 health -= 1 else: tx, ty = tx1, ty1 health -= 1 print('Vova' if tx < ty else 'Vanya' if tx > ty else 'Both') main()
1,800
PYTHON3
def main(): n, m = input().split(" ") n = int(n) m = int(m) rooms = [] receiver = 0 dormitory = 0 a = input().split(" ") b = input().split(" ") rooms.append(int(a[0])) for j in range(1, n): rooms.append(int(a[j]) + rooms[j-1]) for i in range(m): letter = int(b[i]) if(letter <= rooms[0]): receiver = letter dormitory = 1 else: dormitory = binarySearch(rooms, letter) receiver = letter - rooms[dormitory-1] dormitory = dormitory + 1 print("{} {}".format(dormitory, receiver)) return def binarySearch(A, x): start = 0 end = len(A) - 1 while (start <= end): m = (start + end)//2 if(x == A[m]): return m else: if(x > A[m]): if(x < A[m+1]): return m+1 start = m + 1 else: if(x > A[m-1]): return m end = m - 1 return -1 main()
1,000
PYTHON3
teams = int(input()) home = [] away = [] count = 0 for x in range(teams): colors = input().split(' ') home.append(int(colors[0])) away.append(int(colors[1])) for a in home: for b in away: if a == b: count += 1 print(count)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; long long dp[30][50][2522]; int a[30]; int _hash[2522]; int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } long long dfs(int len, int sta1, int sta2, bool limit) { if (len < 0) { return sta2 % sta1 == 0 ? 1 : 0; } if (dp[len][_hash[sta1]][sta2] != -1 && !limit) return dp[len][_hash[sta1]][sta2]; int up = limit ? a[len] : 9; long long ans = 0; for (int i = 0; i <= up; i++) { int t1; if (i == 0) t1 = sta1; else t1 = sta1 * i / gcd(i, sta1); int t2 = (sta2 * 10 + i) % 2520; ans += dfs(len - 1, t1, t2, limit && i == up); } return limit ? ans : dp[len][_hash[sta1]][sta2] = ans; } long long solve(long long x) { int cnt = 0; while (x > 0) { a[cnt++] = x % 10; x /= 10; } return dfs(cnt - 1, 1, 0, 1); } int main() { long long n, m; int T; int cnt = 1; memset(dp, -1, sizeof dp); for (int i = 1; i <= 2520; i++) { if (2520 % i == 0) _hash[i] = cnt++; } for (scanf("%d", &T); T--;) { scanf("%I64d %I64d", &n, &m); printf("%I64d\n", solve(m) - solve(n - 1)); } return 0; }
2,500
CPP
import sys import collections from collections import Counter, deque import itertools import math import timeit import random ######################### # imgur.com/Pkt7iIf.png # ######################### def sieve(n): if n < 2: return list() prime = [True for _ in range(n + 1)] p = 3 while p * p <= n: if prime[p]: for i in range(p * 2, n + 1, p): prime[i] = False p += 2 r = [2] for p in range(3, n + 1, 2): if prime[p]: r.append(p) return r def divs(n, start=1): divisors = [] for i in range(start, int(math.sqrt(n) + 1)): if n % i == 0: if n / i == i: divisors.append(i) else: divisors.extend([i, n // i]) return divisors def divn(n, primes): divs_number = 1 for i in primes: if n == 1: return divs_number t = 1 while n % i == 0: t += 1 n //= i divs_number *= t def flin(d, x, default=-1): left = right = -1 for i in range(len(d)): if d[i] == x: if left == -1: left = i right = i if left == -1: return (default, default) else: return (left, right) def ceil(n, k): return n // k + (n % k != 0) def ii(): return int(input()) def mi(): return map(int, input().split()) def li(): return list(map(int, input().split())) def lcm(a, b): return abs(a * b) // math.gcd(a, b) def prr(a, sep=' '): print(sep.join(map(str, a))) def dd(): return collections.defaultdict(int) def ddl(): return collections.defaultdict(list) input = sys.stdin.readline n = ii() d = li() pre = [0] for i in d: pre.append(pre[-1] ^ i) res = 0 for i in range(n + 1): for j in range(i + 1, n + 1): res = max(res, pre[j] ^ pre[i]) print(res)
1,100
PYTHON3
k,n,w = map(int,input().split()) sum = 0 for i in range(w+1): sum+=i*k s = sum - n if(s != 0) and (s > 0): print(s) else: print("0")
800
PYTHON3
t = int(input()) for i in range(t): a,b=map(int,input().split()) if a == 1 : if b == 1: print("YES") else: print("NO") elif a==2 or a==3: if b<=3: print("YES") else: print("NO") else: print("YES")
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; int a, b; cin >> n; while (n--) { cin >> a >> b; int res; if (a == b) { cout << "0" << endl; } else if (a > b) { res = a - b; if (res % 2 == 0) { cout << "1" << endl; } else { cout << "2" << endl; } } else if (a < b) { res = a - b; if (res % 2 == 0) { cout << "2" << endl; } else { cout << "1" << endl; } } } return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; const bool DEBUG = true; using ll = long long; using ii = pair<int, int>; using vi = vector<int>; using vii = vector<ii>; const double PI = acos(-1); const double EPS = 1e-9; int main() { ios::sync_with_stdio(0); int t; cin >> t; while (t--) { int n, x; cin >> n >> x; priority_queue<int> q; int db = 0; for (int i = 0; i < n; ++i) { int a, b; cin >> a >> b; if (a > b) q.push(a - b); db = max(db, a); } int ans = 1; x -= db; if (x > 0 and q.empty()) { cout << -1 << endl; continue; } else if (x > 0) { ans += ceil(double(x) / double(q.top())); } cout << ans << endl; } }
1,600
CPP
x=int(input()) while 1: x+=1 a=str(x) if a[0]!=a[1] and a[1]!=a[2] and a[2]!=a[3] and a[0]!=a[3] and a[1]!=a[3] and a[0]!=a[2]: break else: pass print(x)
800
PYTHON3
x = int(input()) r = x % 4 if r == 0: print('1 A') elif r == 1: print('0 A') elif r == 2: print('1 B') else: print('2 A')
800
PYTHON3
#import sys #sys.stdin = open("input.txt") T=int(input()) t=0 while t<T: i=0 N=int(input()) L=list(map(int, input().split())) s=len(L)-1 m=L[s] while s!=-1: if L[s]<m: m=L[s] if L[s]>m: i+=1 s-=1 t+=1 print(i)
1,100
PYTHON3
from math import floor newspaper = input() bulbasaur = dict() for i in newspaper: if not i in bulbasaur: bulbasaur[i] = 1 else: bulbasaur[i] += 1 for l in 'Bulbasr': if not l in bulbasaur: print(0) quit() letters = list() for l in 'Bulbasr': if l == 'u' or l == 'a': letters.append(int(floor(bulbasaur[l]/2))) else: letters.append(bulbasaur[l]) print(min(letters))
1,000
PYTHON3