solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int t[n]; for (int i = 0; i < n; i++) cin >> t[i]; int tA = 0, cntA = 0; int tB = 0, cntB = 0; int l = 0, r = n - 1; while (l <= r) { if (tA <= tB) { tA += t[l]; cntA++; l++; } else { tB += t[r]; cntB++; r--; } } cout << cntA << " " << cntB << endl; return 0; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; int n, x, y, p; vector<int> v[15]; int a[15]; int get_ans(int t) { int l = 0; int r = (int)v[t].size() - 1; while (l < r) { int mid = (l + r) >> 1; printf("? %d", mid - l + 1); for (int i = l; i <= mid; i++) { printf(" %d", v[t][i]); } printf("\n"); fflush(stdout); scanf("%d", &p); if (p != x && p != 0) r = mid; else l = mid + 1; } return v[t][l]; } int main() { scanf("%d%d%d", &n, &x, &y); for (int i = 1; i <= n; i++) { int now = i; int t = 1; while (now) { if (now % 2) v[t].push_back(i); t++; now /= 2; } } int ans1 = 0; for (int i = 1; i <= 10; i++) { if (!v[i].size()) continue; printf("? %d", v[i].size()); for (auto u : v[i]) printf(" %d", u); printf("\n"); fflush(stdout); scanf("%d", &p); if (p != x && p != 0) { a[i] = 1; if (ans1 == 0) ans1 = get_ans(i); } else a[i] = 0; } int t = 0; p = 1; for (int i = 1; i <= 10; i++) t += p * a[i], p *= 2; int ans2 = ans1 ^ t; if (ans2 < ans1) swap(ans1, ans2); printf("! %d %d\n", ans1, ans2); return 0; }
2,400
CPP
import sys input = sys.stdin.readline for f in range(int(input())): n,k=map(int,input().split()) a=list(map(int,input().split())) poss=0 lastk=-1 lastb=0 mb=-1 i=0 big=0 if n==1 and a[0]==k: poss=1 while i<n and poss==0: if a[i]==k: if mb>=0: poss=1 else: mb=1 lastk=i lastb=0 else: if a[i]>k: mb+=1 if mb>0: big=1 if mb<1: mb=1 lastb+=1 if lastb>=0 and lastk>=0: poss=1 else: mb-=1 lastb-=1 i+=1 if big==1 and lastk>=0: poss=1 if poss==1: print("yes") else: print("no")
2,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; int n, f[maxn][2][3]; char s[maxn]; void chk(int &x, int y) { if (x < y) x = y; } int main() { scanf("%d %s", &n, s + 1); memset(f, -0x3f, sizeof(f)); f[0][0][0] = f[0][1][0] = 0; for (int i = 1; i <= n; i++) { for (int j : {0, 1}) for (int k : {0, 1, 2}) { chk(f[i][j][k], f[i - 1][j][k]); if (s[i] - '0' == j) { if (k <= 1) chk(f[i][j ^ 1][1], f[i - 1][j][k] + 1); } else { chk(f[i][j ^ 1][!k ? 0 : 2], f[i - 1][j][k] + 1); } } } int ans = 0; for (int i : {0, 1}) for (int j : {0, 1, 2}) { chk(ans, f[n][i][j]); } printf("%d\n", ans); return 0; }
1,600
CPP
k, n, w = [int(s) for s in input().split()] itog = 0 for i in range(1, w + 1): itog += k * i if n - itog < 0: print(abs(n - itog)) else: print(0)
800
PYTHON3
n, m = map(int, input().split()) if(n%2==0): print(int(n/2*m)) else: print(int(n//2*m + m//2))
800
PYTHON3
def main(): d = {} for _ in range(int(input())): c, *l = input().split() if c == "1": v, u, w = map(int, l) while u != v: if u < v: d[v] = d.get(v, 0) + w u, v = v // 2, u else: d[u] = d.get(u, 0) + w u //= 2 else: res = 0 v, u = map(int, l) while u != v: if u < v: res += d.get(v, 0) u, v = v // 2, u else: res += d.get(u, 0) u //= 2 print(res) if __name__ == "__main__": main()
1,500
PYTHON3
def find_min(ar, count, m): ans = 0 total = ar for i in range(100, 0, -1): total += count[i] * i for i in range(100, 0, -1): if total <= m: break if count[i] == 0: continue diff = total - m req = diff // i if diff % i > 0: req += 1 cur = min(req, count[i]) ans += cur total -= cur * i return ans n, m = map(int, input().split()) ar = list(map(int, input().split())) count = [0] * 101 print(0, end = ' ') count[ar[0]] += 1 for i in range(1, n): print(find_min(ar[i], count, m), end = ' ') count[ar[i]] += 1
1,200
PYTHON3
t = int(input()) for i in range(t): a, b, c, n = input().split() a = int(a) b = int(b) c = int(c) n = int(n) if (a + b + c + n) % 3 == 0 and (b + c + n >= 2 * a) and (a + c + n >= 2 * b) and (b + a + n >= 2 * c): print("YES") else: print("NO")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n, i, j, a[1010], k, la, f[1010][1010], g[1010][1010]; char s[1010]; int main() { scanf("%lld", &n); scanf("%s", s + 1); for (i = 1; i <= n; i++) scanf("%lld", &a[i]); for (i = n; i; i--) { f[i][i] = a[1]; memset(g, 172, sizeof(g)); g[i][1] = 0; for (j = i + 1; j <= n; j++) if (s[j] == s[i]) for (la = 1; la <= n; la++) { for (k = i; k < j; k++) if (s[k] == s[i]) g[j][la] = max(g[j][la], g[k][la - 1] + f[k + 1][j - 1]); f[i][j] = max(f[i][j], g[j][la] + a[la]); } for (j = i + 1; j <= n; j++) for (k = i; k < j; k++) f[i][j] = max(f[i][j], f[i][k] + f[k + 1][j]); } printf("%lld", f[1][n]); }
2,400
CPP
#include <bits/stdc++.h> using namespace std; int n; long long a[200005], ans, mina; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); mina = a[n]; ans = a[n]; for (int i = n - 1; i >= 1; i--) if (a[i] > mina - 1) { mina--; if (mina > 0) ans += mina; } else { mina = a[i]; if (mina > 0) ans += mina; } printf("%lld", ans); return 0; }
1,000
CPP
t = int(input()) for _ in range(t): n = int(input()) ans = "9"*((3*n)//4) ans = ans + '8'*(n-len(ans)) print(ans)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s; cin >> s; for (long long i = 2; i < s.size(); i++) { long long x = s[i] - 'A'; long long y = s[i - 1] - 'A'; long long z = s[i - 2] - 'A'; if (x != (y + z) % 26) { cout << "NO" << '\n'; exit(0); } } cout << "YES" << '\n'; return 0; }
0
CPP
n,t=map(int,input().strip().split()[:2]) l=list(map(int,input().strip().split()[:n-1])) k=0 while k<n-1: k=l[k]+k if k==t-1: print('YES') break else: print('NO')
1,000
PYTHON3
import sys n=eval(input()) Array=list(map(int, input().split())) Array=sorted(Array,reverse=True) if n==0: print(0) sys.exit(0) else: count=0 sum=0 for i in range(len(Array)): sum+=Array[i] count+=1 if sum>=n: print(count) sys.exit(0) print(-1)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; char use[62] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; int main() { int T; scanf("%d", &T); while (T--) { int r, c, k; int now = 0; scanf("%d %d %d", &r, &c, &k); char mp[102][102]; char ans[102][102]; for (int i = 0; i < r; i++) { scanf("%s", mp[i]); } for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { if (mp[i][j] == 'R') now++; } } int hehe = now / k; int res = now % k; int cnt = 0; int ct = 0; int cr = 1; for (int i = 0; i < r; i++) { if (i % 2 == 0) { for (int j = 0; j < c; j++) { if (mp[i][j] == 'R') { if (cr <= res) { ct++; ans[i][j] = use[cnt]; if (ct == hehe + 1) { cr++; ct = 0; cnt++; } } else { ct++; ans[i][j] = use[cnt]; if (ct == hehe) { ct = 0; cnt++; } } } else { if (cnt >= k) ans[i][j] = use[cnt - 1]; else ans[i][j] = use[cnt]; } } } else { for (int j = c - 1; j >= 0; j--) { if (mp[i][j] == 'R') { if (cr <= res) { ct++; ans[i][j] = use[cnt]; if (ct == hehe + 1) { cr++; ct = 0; cnt++; } } else { ct++; ans[i][j] = use[cnt]; if (ct == hehe) { ct = 0; cnt++; } } } else { if (cnt >= k) ans[i][j] = use[cnt - 1]; else ans[i][j] = use[cnt]; } } } } for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { printf("%c", ans[i][j]); } printf("\n"); } } return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using db = double; using str = string; using pi = pair<int, int>; using pl = pair<ll, ll>; using pd = pair<db, db>; using vi = vector<int>; using vb = vector<bool>; using vl = vector<ll>; using vd = vector<db>; using vs = vector<str>; using vpi = vector<pi>; using vpl = vector<pl>; using vpd = vector<pd>; template <class T> using V = vector<T>; template <class T, size_t SZ> using AR = array<T, SZ>; const int MOD = 1e9 + 7; const int MX = 2e5 + 5; const ll INF = 1e18; const ld PI = acos((ld)-1); const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1}; mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); constexpr int pct(int x) { return __builtin_popcount(x); } constexpr int bits(int x) { return 31 - __builtin_clz(x); } ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } ll fdiv(ll a, ll b) { return a / b - ((a ^ b) < 0 && a % b); } template <class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template <class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } template <class T, class U> T fstTrue(T lo, T hi, U f) { hi++; assert(lo <= hi); while (lo < hi) { T mid = lo + (hi - lo) / 2; f(mid) ? hi = mid : lo = mid + 1; } return lo; } template <class T, class U> T lstTrue(T lo, T hi, U f) { lo--; assert(lo <= hi); while (lo < hi) { T mid = lo + (hi - lo + 1) / 2; f(mid) ? lo = mid : hi = mid - 1; } return lo; } template <class T> void remDup(vector<T>& v) { sort(begin(v), end(v)); v.erase(unique(begin(v), end(v)), end(v)); } template <class T, class U> void erase(T& t, const U& u) { auto it = t.find(u); assert(it != end(t)); t.erase(u); } template <class T> void re(complex<T>& c); template <class T, class U> void re(pair<T, U>& p); template <class T> void re(vector<T>& v); template <class T, size_t SZ> void re(AR<T, SZ>& a); template <class T> void re(T& x) { cin >> x; } void re(db& d) { str t; re(t); d = stod(t); } void re(ld& d) { str t; re(t); d = stold(t); } template <class T, class... U> void re(T& t, U&... u) { re(t); re(u...); } template <class T> void re(complex<T>& c) { T a, b; re(a, b); c = {a, b}; } template <class T, class U> void re(pair<T, U>& p) { re(p.f, p.s); } template <class T> void re(vector<T>& x) { for (auto& a : x) re(a); } template <class T, size_t SZ> void re(AR<T, SZ>& x) { for (auto& a : x) re(a); } str to_string(char c) { return str(1, c); } str to_string(const char* s) { return (str)s; } str to_string(str s) { return s; } str to_string(bool b) { return to_string((int)b); } template <class T> str to_string(complex<T> c) { stringstream ss; ss << c; return ss.str(); } str to_string(vector<bool> v) { str res = "{"; for (int i = (0); i < ((int)(v).size()); ++i) res += char('0' + v[i]); res += "}"; return res; } template <size_t SZ> str to_string(bitset<SZ> b) { str res = ""; for (int i = (0); i < (SZ); ++i) res += char('0' + b[i]); return res; } template <class T, class U> str to_string(pair<T, U> p); template <class T> str to_string(T v) { bool fst = 1; str res = ""; for (const auto& x : v) { if (!fst) res += " "; fst = 0; res += to_string(x); } return res; } template <class T, class U> str to_string(pair<T, U> p) { return to_string(p.f) + " " + to_string(p.s); } template <class T> void pr(T x) { cout << to_string(x); } template <class T, class... U> void pr(const T& t, const U&... u) { pr(t); pr(u...); } void ps() { pr("\n"); } template <class T, class... U> void ps(const T& t, const U&... u) { pr(t); if (sizeof...(u)) pr(" "); ps(u...); } void DBG() { cerr << "]" << endl; } template <class T, class... U> void DBG(const T& t, const U&... u) { cerr << to_string(t); if (sizeof...(u)) cerr << ", "; DBG(u...); } void setIn(str s) { freopen(s.c_str(), "r", stdin); } void setOut(str s) { freopen(s.c_str(), "w", stdout); } void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } void setIO(str s = "") { unsyncIO(); if ((int)(s).size()) { setIn(s + ".in"), setOut(s + ".out"); } } void solve(); int main() { setIO(); ll T = 1; for (int i = (0); i < (T); ++i) solve(); } void solve() { ll n, bx, x = 0; re(n, bx); for (int i = (0); i < (n); ++i) { ll a; cin >> a; x += a * pow(bx, n - i - 1); } ll y = 0; re(n, bx); for (int i = (0); i < (n); ++i) { ll a; cin >> a; y += a * pow(bx, n - i - 1); } if (x > y) { cout << ">"; } else if (x < y) { cout << "<"; } else { cout << "="; } }
1,100
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 105; int n; long long k; long long a[MAXN], b[MAXN], c[MAXN]; long long MOD = 1000000007; const int MC = 20; struct matrix { long long l; long long e[MC][MC]; void print(string s) { printf("%s : \n", s.c_str()); for (int i = 0; i < l; i++) { for (int j = 0; j < l; j++) { printf("%lld ", e[i][j]); } printf("\n"); } } matrix operator*(const matrix &m) const { matrix res; res.l = l; for (int i = 0; i < l; i++) { for (int j = 0; j < l; j++) { res.e[i][j] = 0; for (int k = 0; k < l; k++) { res.e[i][j] += e[i][k] * m.e[k][j]; res.e[i][j] %= MOD; } } } return res; } }; matrix bin_pow(matrix m, long long exp) { if (exp == 1) return m; matrix h = bin_pow(m, exp / 2); if (exp % 2 == 0) return h * h; return h * h * m; } int main() { scanf("%d%lld", &n, &k); for (int i = 0; i < n; i++) { scanf("%lld%lld%lld", a + i, b + i, c + i); } b[n - 1] = min(b[n - 1], k); if (a[n - 1] == k) n--; long long x[MC]; x[0] = 1; for (int j = 1; j < MC; j++) x[j] = 0; long long y[MC]; for (int I = 0; I < n; I++) { matrix m; m.l = c[I] + 1; long long L = b[I] - a[I]; for (int i = 0; i < m.l; i++) for (int j = 0; j < m.l; j++) if (abs(i - j) <= 1) m.e[i][j] = 1; else m.e[i][j] = 0; m = bin_pow(m, L); for (int i = 0; i < MC; i++) y[i] = 0; for (int i = 0; i < m.l; i++) { y[i] = 0; for (int j = 0; j < m.l; j++) { y[i] += m.e[i][j] * x[j]; y[i] %= MOD; } } for (int i = 0; i < MC; i++) x[i] = y[i]; } long long res = x[0]; printf("%lld\n", res); return 0; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; long long r = 0; void f() { long long w, x, d; cin >> w >> x; d = 6 - max(w, x) + 1; if (d == 1) cout << d << "/" << 6 << endl; if (d == 2) cout << 1 << "/" << 3 << endl; if (d == 3) cout << 1 << "/" << 2 << endl; if (d == 4) cout << 2 << "/" << 3 << endl; if (d == 5) cout << 5 << "/" << 6 << endl; if (d == 6) cout << 1 << "/" << 1 << endl; if (d == 0) cout << 0 << "/" << 1 << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; f(); }
800
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1000009; struct data { int v, id; } d[N]; int n, m, k, a[N], b[N], c[N + N]; inline void merge(int p) { int i = 1, j = p, k = 1; while (i <= n && j <= m) { if (a[i] < b[j]) c[k++] = a[i++]; else c[k++] = b[j++]; } while (i <= n) c[k++] = a[i++]; while (j <= m) c[k++] = b[j++]; } inline bool chk(int p) { merge(p); int t = n + m - p + 1; long long now = 0, lef = k; for (int i = 1; i <= t; i++) { lef += 1ll * (c[i] - now) * k; now = c[i]; lef--; if (lef < 0) { return false; } } return true; } inline bool cmp(data a, data b) { return a.v < b.v; } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= m; i++) scanf("%d", &b[i]), d[i] = (data){b[i], i}; sort(a + 1, a + n + 1); sort(b + 1, b + m + 1); if (!chk(m + 1)) { cout << "-1"; exit(0); } int l = 1, r = m + 1, mid; while (l < r) { mid = (l + r) / 2; if (chk(mid)) r = mid; else l = mid + 1; } sort(d + 1, d + m + 1, cmp); printf("%d\n", m - r + 1); for (int i = r; i <= m; i++) printf("%d ", d[i].id); return 0; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int arr[n]; int parent[n]; for (int i = 0; i < n - 1; i++) { cin >> arr[i]; arr[i] -= 1; parent[i] = -1; } parent[n - 1] = -1; parent[arr[0]] = -2; int max_cur = n - 1; for (int i = 0; i < n - 1; i++) { if (i != n - 2 && parent[arr[i + 1]] == -1) { parent[arr[i + 1]] = arr[i]; } else { while (parent[max_cur] != -1) { max_cur -= 1; } parent[max_cur] = arr[i]; } } cout << arr[0] + 1 << endl; for (int i = 0; i < n; i++) { if (i != arr[0]) { cout << (i + 1) << " " << parent[i] + 1 << endl; } } }
2,200
CPP
T = int(input()) for _ in range(T): a, b = map(int, input().split()) total = abs(b - a) if total % 10 == 0: print(total // 10) else: print(total // 10 + 1)
800
PYTHON3
# Author : -pratyay- '''inp=sys.stdin.buffer.readline inar=lambda: list(map(int,inp().split())) inin=lambda: int(inp()) inst=lambda: inp().decode().rstrip('\n\r')''' # Am I debugging, check if I'm using same variable name in two places import sys _ord, inp, num, neg, _Index = lambda x: x, [], 0, False, 0 i, s = 0, sys.stdin.buffer.read() try: while True: if s[i] >= b"0"[0]:num = 10 * num + _ord(s[i]) - 48 elif s[i] == b"-"[0]:neg = True elif s[i] != b"\r"[0]: inp.append(-num if neg else num) num, neg = 0, False i += 1 except IndexError: pass if s and s[-1] >= b"0"[0]: inp.append(-num if neg else num) def inin(size=None): global _Index if size==None: ni=_Index;_Index+=1 return inp[ni] else: ni=_Index;_Index+=size return inp[ni:ni+size] from types import GeneratorType def recursive(f, stack=[]): def wrappedfunc(*args, **kwargs): if stack: return f(*args, **kwargs) else: to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to); to = next(to) else: stack.pop() if not stack: break to = stack[-1].send(to) return to return wrappedfunc # End PyRival Bootstrap; counter=0 init=[] goal=[] flip=[] ans=[] @recursive def dfs(gr,node): global init,goal,counter,flip,parent,flip,ans if node!=0 and node not in gr[0] : flip[node]=flip[parent[parent[node]]] init[node]^=flip[node] if init[node]!=goal[node]: init[node]=goal[node] flip[node]^=1 counter+=1 ans.append(node+1) for i in gr[node]: if i!=parent[node]: parent[i]=node yield dfs(gr,i) yield _T_=1#inin() for _t_ in range(_T_): n=inin() counter=0 graph=[[] for i in range(n)] for i in range(n-1): u,v=inin(2) graph[u-1].append(v-1) graph[v-1].append(u-1) flip=[0 for i in range(n)] parent=[-1 for i in range(n)] init=inin(n) goal=inin(n) dfs(graph,0) print(counter) for i in ans: print(i) #print(init) #print(goal)
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; struct sol { int x, y, z, a, b, t; }; vector<pair<int, int> > stations; vector<sol> sols; int e, s, n, m; sol driveback(sol car, int si, int fin) { int dist = stations[si].first - fin; int i; i = max(min(car.z, min(s - car.t, dist - car.t)), 0); car.z -= i; car.t += i; i = max(min(car.y, min(s - car.t, dist - car.t)), 0); car.y -= i; car.t += i; car.b += i; i = max(min(car.x, min(s - car.t, dist - car.t)), 0); car.x -= i; car.t += i; car.a += i; if (dist > car.t) { car.a = car.b = -1; car.x = car.y = car.z = 0; car.t = 0; return car; } i = s - car.t; car.z = min(i, car.z); i -= car.z; car.y = min(i, car.y); i -= car.y; car.x = min(i, car.x); car.t -= dist; return car; } int main() { cin >> e >> s >> n >> m; int i, j; for (int c = 0; c < n; c++) { cin >> i >> j; if (j <= e) stations.push_back(pair<int, int>(j, i)); } n = stations.size(); stations.push_back(pair<int, int>(pair<int, int>(e, 3))); sort(stations.begin(), stations.end()); sols.resize(n + 1); sol car; car.x = car.y = car.z = car.a = car.b = 0; car.t = s; sols[n] = car; for (int c = n - 1; c >= 0; c--) { car = driveback(car, c + 1, stations[c].first); if (car.a != -1) { int t = stations[c].second; switch (t) { case 1: car.x = s; break; case 2: car.y = s; break; case 3: car.z = s; break; } } sols[c] = car; } for (int c = 0; c < m; c++) { cin >> i; vector<pair<int, int> >::iterator it = upper_bound(stations.begin(), stations.end(), pair<int, int>(i, 3)); car = driveback(sols[it - stations.begin()], it - stations.begin(), i); cout << car.a << ' ' << car.b << endl; } return 0; }
2,800
CPP
a=input();print((a.capitalize() if a[0].islower() else a.lower()) if a[1:]==a[1:].upper() else a)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; template <typename T> inline long long Chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; } template <typename T> inline long long Chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; } template <typename T> inline T read() { T sum = 0; long long fl = 1, ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1; for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0'; return sum * fl; } const long long maxn = 1000000 + 5; long long n, k, ans; long long a[maxn]; inline void Solve() { for (long long i = 1; i <= n; i++) { a[i] = i; k -= i; ans += i; } if (k < 0) { printf("-1\n"); return; } for (long long i = n; i >= 1; i--) { if (i > n - i + 1 && k) { long long tmp = min(i * 2 - n - 1ll, k); k -= tmp; ans += tmp; swap(a[i], a[i - tmp]); } else break; } printf("%lld\n", ans); for (long long i = 1; i < n; i++) { printf("%lld ", i); } printf("%lld\n", n); for (long long i = 1; i < n; i++) { printf("%lld ", a[i]); } printf("%lld\n", a[n]); } inline void Input() { n = read<long long>(); k = read<long long>(); } signed main() { Input(); Solve(); return 0; }
2,400
CPP
from collections import Counter a=list(input()) b=list(input()) a=a+b n=list(input()) if Counter(a)==Counter(n): print("YES") else: print("NO")
800
PYTHON3
n = int(input()) if n == 0 or n == 2: print('NO') elif (n - 2) % 2 == 0: print('YES') else: print('NO')
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b, i, count = 0, sum = 0, cheak = 0; cin >> n >> a >> b; int s[n]; for (i = 0; i < n; i++) { cin >> s[i]; sum = sum + s[i]; } sort(s + 1, s + n, greater<int>()); if ((a * s[0]) / sum < b) count++; for (i = 1; i < n; i++) { if ((a * s[0]) / (sum - s[i]) >= b) { cheak = 1; break; } sum = sum - s[i]; count++; } if (n == 1) cout << 0; else if (cheak) cout << count; else cout << count - 1; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; istream& operator>>(istream& o, pair<long long, long long>& v) { o >> v.first >> v.second; return o; } ostream& operator<<(ostream& o, pair<long long, long long>& v) { o << "(" << v.first << "," << v.second << ")"; return o; } template <typename T> istream& operator>>(istream& o, vector<T>& v) { for (long long i = 0; i < v.size(); ++i) cin >> v[i]; return o; } template <typename T> ostream& operator<<(ostream& o, vector<T>& v) { for (long long i = 0; i < v.size(); ++i) cout << v[i] << " "; return o; } void solve() { long long n, m; cin >> n >> m; vector<vector<long long> > v(n, vector<long long>(m)); cin >> v; long long len = n + m - 1; long long ans = 0; for (long long i = 0; i < len / 2; ++i) { long long zero = 0, one = 0; long long v1 = i; long long v2 = len - i - 1; for (long long r = 0; r < n; ++r) { for (long long c = 0; c < m; ++c) { if (r + c == v1 && v[r][c] == 0) zero++; if (r + c == v1 && v[r][c] == 1) one++; if (r + c == v2 && v[r][c] == 0) zero++; if (r + c == v2 && v[r][c] == 1) one++; } } ans += min(zero, one); } cout << ans << "\n"; } int main() { time_t start, end; time(&start); time(&end); long long t; cin >> t; while (t--) { solve(); } return 0; }
1,500
CPP
#include <bits/stdc++.h> using namespace std; vector<long long> three; bool vis[1000000 + 2]; void seive() { memset(vis, false, sizeof(vis)); for (long long i = 3; i <= 1000000; i = i + 3) { vis[i] = true; } } int main() { seive(); long long n; cin >> n; cout << "1 "; if (n == 3) { cout << "1 1"; return 0; } n--; for (long long j = n - 1; j > 1; j--) { if (j % 3 != 0) { long long y = n - j; if (vis[y] == false) { cout << y << " " << j; break; } } } }
800
CPP
import bisect import collections import copy import functools import heapq import itertools import math import random import re import sys import time import string from typing import List sys.setrecursionlimit(99999) n, = map(int,input().split()) arr = list(map(int,input().split())) arr.append(10**9+5) ans = 0 for j in range(n-1,-1,-1): arr[j] = max(0,min(arr[j],arr[j+1]-1)) ans+=arr[j] print(ans)
1,000
PYTHON3
n = int(input()) aps = 0 aman = 0 #Выбрать любое положительное нечетное целое число x (x>0) и заменить a на a+x; #выбрать любое положительное четное целое число y (y>0) и заменить a на a−y. for i in range(0, n): a, b = map(int, input().split()) if a == b: print("0") continue if b > a: aman = b - a if aman % 2 == 1: print("1") continue if b < a: aman = a - b if aman % 2 == 0: print("1") continue print("2")
800
PYTHON3
n = int(input()) total = 1 while n>1: if n%2: total = total + 1 n = n//2 print(total)
1,000
PYTHON3
buf = [] def cal(b, a): buf.append(b) if a == b: return True elif b < a: return False elif b % 2 == 0: return cal(b // 2, a) elif b % 10 == 1: return cal((b - 1) // 10, a) a, b = map(int, input().split()) if cal(b, a): print('YES') print(len(buf)) print(' '.join(str(x) for x in buf[::-1])) else: print('NO')
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n, tt, mn, mx, ans = 0, m, k, A, B, C, T; long long arr[100005]; string s; void init() { ios_base::sync_with_stdio(false); cin.tie(NULL); } int main() { init(); cin >> n >> A >> B >> C >> T; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); ans = 0; for (int i = 0; i < n; i++) { long long st = arr[i]; long long pp = A; long long rem = 0; mx = INT_MIN; while (st <= T) { mx = max(mx, pp + rem); rem += C; pp -= B; st++; } ans += mx; } cout << ans << endl; }
1,300
CPP
import sys,math n = int(input()) a = [int(x) for x in input().split()] s = set(a) exist = set(s) next = 1 for i in range(n): if a[i] <= n and a[i] in s: s.remove(a[i]) else: while next in exist: next += 1 a[i] = next next += 1 print(' '.join(str(x) for x in a))
1,200
PYTHON3
r = input() data = input() if data.count("A") > data.count("D"): print('Anton') elif data.count("A") == data.count("D"): print('Friendship') else: print("Danik")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; inline void setmin(int &x, int y) { if (y < x) x = y; } inline void setmax(int &x, int y) { if (y > x) x = y; } inline void setmin(long long &x, long long y) { if (y < x) x = y; } inline void setmax(long long &x, long long y) { if (y > x) x = y; } const int N = 51; const int inf = (int)1e9 + 1; const long long big = (long long)1e18 + 1; const int P = 239; const int P1 = 31; const int P2 = 57; const int MOD = (int)1e9 + 7; const int MOD1 = (int)1e9 + 9; const int MOD2 = 998244353; const double eps = 1e-9; const double pi = atan2(0, -1); const int ABC = 26; int dp[N][N][N]; int cost[N][N][N][N]; int L[N], R[N], x[N], c[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.precision(20); cout << fixed; int n, H, m; cin >> n >> H >> m; for (int i = 0; i < m; i++) { cin >> L[i] >> R[i] >> x[i] >> c[i]; L[i]--; R[i]--; } for (int i = 0; i < m; i++) { if (x[i] == H) { continue; } for (int l = 0; l < L[i] + 1; l++) { for (int r = R[i]; r < n; r++) { for (int p = L[i]; p < R[i] + 1; p++) { cost[l][r][p][x[i] + 1] -= c[i]; } } } } for (int l = 0; l < n; l++) { for (int r = l; r < n; r++) { for (int p = l; p < r + 1; p++) { for (int h = 1; h < H + 1; h++) { cost[l][r][p][h] += cost[l][r][p][h - 1]; } } } } for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { for (int h = 0; h < H + 1; h++) { dp[i][j][h] = -inf; } } } for (int l = n - 1; l > -1; l--) { for (int r = l; r < n; r++) { for (int p = l; p < r + 1; p++) { for (int h = 0; h < H + 1; h++) { int val = h * h + cost[l][r][p][h]; if (p > l) { val += dp[l][p - 1][h]; } if (p < r) { val += dp[p + 1][r][h]; } setmax(dp[l][r][h], val); } } for (int h = 1; h < H + 1; h++) { setmax(dp[l][r][h], dp[l][r][h - 1]); } } } cout << dp[0][n - 1][H] << "\n"; return 0; }
800
CPP
t=int(input()) for i in range(t): val=str(input()) arr=val.split(" ") a=int(arr[0]) b=int(arr[1]) n=int(arr[2]) m=int(arr[3]) if(a>=b): if(b>=m): if(a+(b-m)>=n): print("Yes") else: print("No") else: print("No") elif(b>a): if(a>=m): if(b+(a-m)>=n): print("Yes") else: print("No") else: print("No")
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; long long sumdig(long long n) { long long sum = 0; while (n) { sum += n % 10; n = n / 10; } return sum; } long long numdig(long long n) { long long num = 0; while (n) { n = n / 10; num += 1; } return num; } int main() { int t = 1; cin >> t; while (t--) { long long n, s; cin >> n >> s; if (sumdig(n) <= s) { cout << 0 << '\n'; continue; } long long temp = pow(10ll, (numdig(n))); long long ans = temp - n; long long dig = 1; long long moves = 0; long long flag = 1; while (true) { if (n % 10 == 0) { n = n / 10; dig += 1; } else { moves += pow(10ll, dig - 1ll) * (10ll - (n % 10ll)); long long x = numdig(n); n += 10ll - (n % 10ll); if (numdig(n) > x) { flag = 0; break; } if (sumdig(n) <= s) { break; } } } if (flag) { cout << min(ans, moves) << "\n"; } else { cout << ans << "\n"; } } return 0; }
1,500
CPP
import sys import os # import math # input = sys.stdin.readline def int_array(): return list(map(int, input().strip().split())) def str_array(): return input().strip().split() def gcd(a,b): if b == 0: return a return gcd(b, a % b);x def take_input(): if os.environ.get("check"): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') take_input() ######################### TEMPLATE ENDS HERE ################################# """ n = 6; arr = [1, 2, 2, 1, 2, 3] hsh = { "key1" : "value1", "key2" : "value2", "key3" : "value3", } """ for i in range(int(input())): n = int(input()); arr = int_array(); hsh = {} for i in arr: hsh[i] = hsh.get(i,0)+1 INF = float('inf'); ans = INF; for k in hsh: v = hsh[k] if v == 1: ans = min(ans, k); if ans == INF: print(-1); else: print(arr.index(ans) + 1);
800
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class T> T abs(T x) { return x > 0 ? x : -x; } const int MAX = 3000000; int n; int m, o; int tree[MAX]; int sl[MAX], sr[MAX], L[MAX], R[MAX]; map<int, int> pr, all; set<pair<int, pair<int, int> > > FREE; int del(int l, int r) { pr.erase(l); pr.erase(r); FREE.erase(make_pair(r - l + 1, make_pair(l, r))); return 0; } int add(int l, int r) { if (l <= r) { pr[l] = r; pr[r] = l; FREE.insert(make_pair(r - l + 1, make_pair(l, r))); } return 0; } pair<int, int> get(int x) { if (pr.count(x)) { int y = pr[x]; if (x > y) swap(x, y); return make_pair(x, y); } return make_pair(-1, -1); } int upd(int x, int y, int z) { if (L[x] > y || R[x] < y) return 0; if (L[x] == R[x]) { tree[x] += z; return 0; } int s = (L[x] + R[x]) / 2; if (y <= s) { if (sl[x] == -1) { sl[x] = o; L[o] = L[x]; R[o] = s; sl[o] = sr[o] = -1; tree[o] = 0; o++; } upd(sl[x], y, z); } else { if (sr[x] == -1) { sr[x] = o; L[o] = s + 1; R[o] = R[x]; sl[o] = sr[o] = -1; tree[o] = 0; o++; } upd(sr[x], y, z); } tree[x] += z; return 0; } int get(int x, int l, int r) { if (L[x] > r || R[x] < l) return 0; if (L[x] >= l && R[x] <= r) return tree[x]; int res = 0; if (sl[x] != -1) res += get(sl[x], l, r); if (sr[x] != -1) res += get(sr[x], l, r); return res; } int main() { scanf("%d%d", &m, &n); o = 1; L[0] = 1; R[0] = m; sl[0] = sr[0] = -1; tree[0] = 0; FREE.insert(make_pair(m, make_pair(1, m))); pr[1] = m; pr[m] = 1; for (int i = 0; i < n; i++) { int x, y, l, r; scanf("%d", &x); if (x == 0) { scanf("%d%d", &l, &r); printf("%d\n", get(0, l, r)); } else { if (all.count(x)) { y = all[x]; upd(0, y, -1); all.erase(x); pair<int, int> al = get(y - 1); pair<int, int> ar = get(y + 1); l = r = y; if (al.first != -1) { del(al.first, al.second); l = al.first; } if (ar.first != -1) { del(ar.first, ar.second); r = ar.second; } add(l, r); } else { pair<int, pair<int, int> > cur = *FREE.rbegin(); l = cur.second.first; r = cur.second.second; del(l, r); int y = (l + r + 1) / 2; upd(0, y, 1); add(l, y - 1); add(y + 1, r); all[x] = y; } } } return 0; }
2,400
CPP
#include <bits/stdc++.h> using namespace std; long long l, r, sum = 0; vector<long long> all; int main() { cin >> l >> r; long long num = 4; while (num <= 10000000000) { all.push_back(num); string s = ""; long long n = num; while (n) { if (n % 10 == 4) s = s + '4'; else s = s + '7'; n /= 10; } reverse(s.begin(), s.end()); int ch = -1; for (int i = 0; i < s.length(); ++i) if (s[i] == '4') ch = i; if (ch == -1) { for (int i = 0; i < s.length(); ++i) s[i] = '4'; s = s + '4'; } else { s[ch] = '7'; for (int i = ch + 1; i < s.length(); ++i) s[i] = '4'; } num = 0; for (int i = 0; i < s.length(); ++i) num = num * 10 + (int)(s[i] - '0'); } int j = 0, jj = 0; while (all[j] < l) ++j; while (all[jj] < r) ++jj; if (l == r) { cout << all[j]; return 0; } if (all[j] == all[jj]) { for (int i = l; i <= r; ++i) sum += all[j]; cout << sum << endl; return 0; } sum += (all[j] - l + 1) * all[j]; l = all[j]; ++j; if (r != all[jj]) { sum += (r - all[jj - 1]) * all[jj]; r = all[jj - 1]; } while (l < r) { sum += (all[j] - l) * all[j]; l = all[j]; ++j; } cout << sum; return 0; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long t = 1; while (t--) { long long n; cin >> n; long long arr[n]; long long same = 0; unordered_map<long long, long long> mp; int fl = 0; vector<pair<int, int> > v; for (int i = 0; i < n; i++) { long long x; cin >> x; v.push_back(make_pair(x, i + 1)); mp[x] += 1; if (mp[x] > 1) same++; } if (same >= 2) cout << "YES\n"; else { cout << "NO\n"; continue; } sort(v.begin(), v.end()); vector<int> a, b, c; for (int i = 0; i < n; i++) { if (mp[v[i].first] > 1 && fl != -1) { if (fl == 0) { a.push_back(v[i].second); a.push_back(v[i + 1].second); b.push_back(v[i + 1].second); b.push_back(v[i].second); if (mp[v[i].first] > 2) { c.push_back(v[i + 2].second); c.push_back(v[i + 1].second); c.push_back(v[i].second); a.push_back(v[i + 2].second); b.push_back(v[i + 2].second); i = i + 2; fl = -1; continue; } c.push_back(v[i].second); c.push_back(v[i + 1].second); i = i + 1; fl = 1; continue; } else if (fl == 1) { c.push_back(v[i].second); c.push_back(v[i + 1].second); a.push_back(v[i + 1].second); a.push_back(v[i].second); b.push_back(v[i].second); b.push_back(v[i + 1].second); i = i + 1; fl = -1; continue; } } else { a.push_back(v[i].second); b.push_back(v[i].second); c.push_back(v[i].second); } } for (auto i : a) cout << i << " "; cout << endl; for (auto i1 : b) cout << i1 << " "; cout << endl; for (auto i2 : c) cout << i2 << " "; cout << endl; } return 0; }
1,300
CPP
n,m=list(map(int,input().split())) A=[] In=input().split()[::-1] Set=set() for val in In: Set.add(val) A.append(len(Set)) A=[0]+A[::-1] for _ in range(m): N=int(input()) print(A[N])
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[2000010]; map<long long, long long> mp, mp1; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } long long tol = 0; long double ans = 0; for (int i = 1; i <= n; i++) { mp[a[i]]++; mp1[a[i]] += a[i]; tol += a[i]; int res = i - mp[a[i]] - mp[a[i] + 1] - mp[a[i] - 1]; long long res1 = tol - mp1[a[i]] - mp1[a[i] - 1] - mp1[a[i] + 1]; ans += 1ll * res * a[i] - res1; } cout << fixed << setprecision(0) << ans; }
2,200
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1001; set<int> g[N]; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; for (int i = 1; i <= n; ++i) { int cur = k; for (int j = 1; j <= n && cur > 0; ++j) { if (j == i) { continue; } if (g[j].find(i) == g[j].end()) { g[i].insert(j); --cur; } } if (cur != 0) { cout << -1; return 0; } } cout << n * k << endl; for (int i = 1; i <= n; ++i) { for (auto j : g[i]) { cout << i << " " << j << '\n'; } } }
1,400
CPP
n = int(input()) a = [] for i in range(n): a.append([int(j) for j in input().split()]) count = 0 for row in a: if row.count(1) > 1: count += 1 print(count)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vi = vector<ll>; const ll mod = 998244353ll; ll dx = 1, dy = 1, x = 0, y = 0, n, m, stop = 0, k, pos = 0; struct pt { ll x, y, i; pt(ll x = -1, ll y = -1, ll i = -1) { this->x = x; this->y = y; this->i = i; } bool operator<(const pt &rhs) const { return i < rhs.i; } }; void go() { ll t = INT_MAX; if (dx == 1) t = min(t, n - x); else t = min(t, x); if (dy == 1) t = min(t, m - y); else t = min(t, y); x += dx * t; y += dy * t; pos += t; if (x == n) dx *= -1; if (x == 0) dx *= -1; if (y == m) dy *= -1; if (y == 0) dy *= -1; if (x == n && y == m) { stop = 1; } if (x == 0 && y == m) { stop = 1; } if (x == n && y == 0) { stop = 1; } if (x == 0 && y == 0) { stop = 1; } } ll ans[200200], bias = 100100; set<pt> diag[200200], adiag[200200]; int main() { ios::sync_with_stdio(0); cout.tie(0); cin.tie(0); cin >> n >> m >> k; for (ll x, y, i = 0; i < k; i++) { cin >> x >> y; ans[i] = -1; pt t(x, y, i); diag[x + y].insert(t); adiag[bias + x - y].insert(t); } while (!stop) { if (dx != dy) { ll p = x + y; for (auto i : diag[p]) adiag[bias + i.x - i.y].erase(i), ans[i.i] = pos + abs(x - i.x); diag[p].clear(); } else { ll p = bias + x - y; for (auto i : adiag[p]) diag[i.x + i.y].erase(i), ans[i.i] = pos + abs(x - i.x); adiag[p].clear(); } go(); } for (ll i = 0; i < k; i++) cout << ans[i] << "\n"; }
1,800
CPP
a=int(input()) b=int(input()) c=int(input()) print(max(a+b*c,max(a*(b+c),max(a*b*c,max(a+b+c,max(a*b+c,(a+b)*c))))))
1,000
PYTHON3
#include <bits/stdc++.h> struct result { long long level; long long sum_up_to; }; result find_level(long long n) { result res; res.sum_up_to = 0; for (long long level = 0; level <= 100; level += 2) { long long this_level = (1ll << level); if (n < res.sum_up_to + this_level) { res.level = level; return res; } res.sum_up_to += this_level; } return res; } long long solve1(long long n) { auto lvl = find_level(n); return (n - lvl.sum_up_to) + (1ll << lvl.level); } long long solve2(long long n) { static const long long mp[] = {0, 2, 3, 1}; auto lvl = find_level(n); long long offset = (n - lvl.sum_up_to); long long base = ((1ll << lvl.level) << 1); long long spec_offset = 0; for (long long level = lvl.level - 2; level >= 0; level -= 2) { long long bitmask = 3 * (1ll << level); long long idx = (offset & bitmask) >> level; spec_offset += mp[idx] * (1ll << level); } return base + spec_offset; } long long solve3(long long n) { static const long long mp[] = {0, 3, 1, 2}; auto lvl = find_level(n); long long offset = (n - lvl.sum_up_to); long long base = ((1ll << lvl.level) << 1) + ((1ll << lvl.level)); long long spec_offset = 0; for (long long level = lvl.level - 2; level >= 0; level -= 2) { long long bitmask = 3 * (1ll << level); long long idx = (offset & bitmask) >> level; spec_offset += mp[idx] * (1ll << level); } return base + spec_offset; } void solve() { long long n; std::cin >> n; long long ans = 0; if (n % 3 == 1) { ans = solve1((n - 1) / 3); } else if (n % 3 == 2) { ans = solve2((n - 2) / 3); } else if (n % 3 == 0) { ans = solve3((n - 3) / 3); } std::cout << ans << "\n"; } int main() { std::ios::sync_with_stdio(false); int t; std::cin >> t; while (t--) solve(); return 0; }
2,200
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long i, n, t, s = 0, max = 0, k = 0; cin >> n >> t; long long a[n]; for (i = 0; i < n; i++) { cin >> a[i]; s = s + a[i]; if (s <= t) { max++; } else { s = s - a[k]; k++; } } cout << max << endl; return 0; }
1,400
CPP
import math import sys import bisect input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__ ilele = lambda: map(int,input().split()) alele = lambda: list(map(int, input().split())) def list2d(a, b, c): return [[c] * b for i in range(a)] def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)] INF = 10 ** 18 MOD = 10 ** 9 + 7 from collections import deque Ans=1;Ans2 = 1 B = [1] for i in range(40): Ans*=2 Ans2 +=Ans B.append(Ans2) #print(B) for _ in range(int(input())): N = int(input()) A = alele() maxi = A[0];m=0 for i in A: maxi = max(maxi,i) m = max(m,maxi-i) if m==0: print(0) else: z = bisect.bisect_left(B,m) print(z+1)
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; const int inf = 1000000009; void wt(int x) { if (x > 9) wt(x / 10); putchar(x % 10 + 48); return; } struct ZKW { long long val; int sl, sr, l, r, ans, len; void inc(long long t) { val += t; sl = sr = (val > 0) - (val < 0); l = r = ans = (val != 0); len = 1; return; } void merge(ZKW x, ZKW y) { l = x.l, r = y.r; sl = x.sl, sr = y.sr; len = x.len + y.len; ans = max(x.ans, y.ans); if (x.sr <= y.sl && x.sr && y.sl) { ans = max(ans, x.r + y.l); if (l == x.len) l = x.len + y.l; if (r == y.len) r = y.len + x.r; } } } A[1 << 20]; int n, M, lst, tmp, q; void modify(int x, int d) { A[x = x + M].inc(d); while (x >>= 1) A[x].merge(A[x << 1], A[x << 1 | 1]); return; } int main() { scanf("%d", &n); for (M = 1; M < (n + 2); M <<= 1) ; for (int i = 1; i <= n; i++) { scanf("%d", &tmp); if (i > 1) modify(i - 1, lst - tmp); lst = tmp; } scanf("%d", &q); for (int l, r, d; q--;) { scanf("%d %d %d", &l, &r, &d); if (l > 1) modify(l - 1, -d); if (r < n) modify(r, d); wt(A[1].ans + 1), putchar(10); } return 0; }
2,500
CPP
def igual(cadena1, cadena2, l): if cadena1 == cadena2: return True hl = int(l/2) if l % 2 == 1: return cadena1 == cadena2 else: a1 = cadena1[0:hl] b1 = cadena2[0:hl] a2 = cadena1[hl:l] b2 = cadena2[hl:l] if (igual(a1, b2, hl) and igual(a2, b1, hl)): return True else: return (igual(a1, b1, hl) and igual(a2, b2, hl)) cadena1 = input() cadena2 = input() if igual(cadena1, cadena2, len(cadena1)): print('YES') else: print('NO')
1,700
PYTHON3
num = [int(z) for z in input().split(' ')] n, x, y = num[0], num[1], num[2] nm = input() cnt = 0 for i in range(n - 1, n - x - 1, - 1): if i == n - y - 1: if nm[i] != '1': cnt += 1 else: if nm[i] != '0': cnt += 1 print(cnt)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAXN = 55; long long s, f[MAXN][MAXN * 2][2], pw[MAXN]; inline long long dp(long long s, int a, int b, int t) { memset(f, 0, sizeof(f)); f[1][0][0] = 1; for (int i = 1; i <= log2(s) + 1; i++) { int v = (s >> i) & 1; for (int j = 0; j <= 2 * i - 2; j++) for (int k = 0; k <= 1; k++) if (f[i][j][k]) for (int x = 0; x <= 1; x++) if (!x || i < a) for (int y = 0; y <= 1; y++) if (!y || i < b) if ((k + x + y) % 2 == v) f[i + 1][j + x + y][(k + x + y) / 2] += f[i][j][k]; } return f[(int)log2(s) + 2][t][0]; } int main() { cin >> s; pw[0] = 1; for (int i = 1; i <= 60; i++) pw[i] = pw[i - 1] << 1; long long ans = 0; for (int h = 1; (pw[h] - 1) <= s; h++) { long long x = s / (pw[h] - 1); long long y = s - x * (pw[h] - 1); for (int i = 60; i >= 1; i--) if (y >= pw[i] - 1) y -= pw[i] - 1; if (!y) ans++; } for (int i = 1; (pw[i] - 1) <= s; i++) for (int j = 1; (pw[j] - 1) <= s; j++) { long long x = (s - pw[j] + 1) / (pw[i + 1] + pw[j + 1] - 3); if (!x) continue; long long y = (s - pw[j] + 1) - x * (pw[i + 1] + pw[j + 1] - 3); if (!y) { ans++; continue; } if (i == 1 && j == 1) { if (y == 5 * x + 1) ans++; continue; } for (int k = 1; k <= i + j; k++) if ((y + k) % 2 == 0) ans += dp(y + k, i, j, k); } cout << ans << endl; return 0; }
3,200
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 3 * 1e5 + 100; int dp[maxn]; int A[maxn]; queue<int> q; int main() { int n; scanf("%d", &n); int i, j; for (i = 1; i <= n; i++) scanf("%d", &A[i]); memset(dp, -1, sizeof dp); dp[1] = 0; q.push(1); while (!q.empty()) { int tmp = q.front(); q.pop(); if (dp[A[tmp]] == -1) { dp[A[tmp]] = dp[tmp] + 1; q.push(A[tmp]); } for (i = -1; i < 2; i += 2) if (tmp + i >= 1 && tmp + i <= n && dp[tmp + i] == -1) { dp[tmp + i] = dp[tmp] + 1; q.push(tmp + i); } } printf("%d", dp[1]); for (i = 2; i <= n; i++) printf(" %d", dp[i]); return 0; }
1,600
CPP
""" #If FastIO not needed, use this and don't forget to strip #import sys, math #input = sys.stdin.readline """ import os import sys from io import BytesIO, IOBase import heapq as h from bisect import bisect_left, bisect_right import time from types import GeneratorType BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): import os self.os = os 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 = self.os.read(self._fd, max(self.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 = self.os.read(self._fd, max(self.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: self.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") from collections import defaultdict as dd, deque as dq, Counter as dc import math, string start_time = time.time() def getInts(): return [int(s) for s in input().split()] def getInt(): return int(input()) def getStrs(): return [s for s in input().split()] def getStr(): return input() def listStr(): return list(input()) def getMat(n): return [getInts() for _ in range(n)] def isInt(s): return '0' <= s[0] <= '9' MOD = 998244353 """ in one array we need to answer, in the other we can have whatever we want is it possible to get a subsequence where the max element is <= D, with a space of at least 1 between elements? if K is even, then K//2 is fine if K is odd, then K//2 + 1 if first element is A0 K//2 if first element is A1 or later """ def solve(): A, B, C = getInts() best = 10**18 best_out = [-1,-1,-1] for a in range(1,2*10**4+1): for b in range(a,2*10**4+1,a): for i in range(2): c = b*(C//b) + i*b if c < b: continue x = min(best,abs(a-A)+abs(b-B)+abs(c-C)) if x < best: best = x best_out = [a,b,c] print(best) print(*best_out) return for _ in range(getInt()): #print(solve()) solve() #print(time.time()-start_time)á
2,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int gcd(int x, int y) { if (y == 0) return x; return gcd(y, x % y); } int main() { int x = 1, y = 1, heap = 100, gdid = 0; bool turn = 1, turn2 = 0; scanf("%d%d%d", &x, &y, &heap); while (1) { if (turn == 0) gdid = gcd(heap, y); else gdid = gcd(heap, x); if (gdid > heap) break; heap -= gdid; swap(turn, turn2); } printf("%d\n", turn); return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; int n, l, t, a[4000000]; long long ans; int main() { scanf("%d%d%d", &n, &l, &t); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) { ans += (n - 1) * ((2 * t) / l); int x = (2 * t) % l; int L = i + 1, r = n + i - 1, mid, pos = i; while (L <= r) { mid = L + r >> 1; if (a[mid] <= x + a[i]) L = mid + 1, pos = mid; else r = mid - 1; } ans += pos - i; a[n + i] = l + a[i]; } printf("%.10lf\n", ans * 0.25); return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return a % b ? gcd(b, a % b) : b; } bool isp[1000005]; vector<int> pl; multiset<long long> ap; int main() { isp[2] = true; for (int i = 3; i < 1000005; i += 2) isp[i] = true; for (int i = 3; i * i < 1000005; i += 2) { if (!isp[i]) continue; for (int j = i * i; j < 1000005; j += i) isp[j] = false; } pl.push_back(2); for (int i = 3; i < 1000005; i += 2) if (isp[i]) pl.push_back(i); long long x, y, ans = 0, g; scanf("%lld%lld", &x, &y); g = gcd(x, y); x /= g, y /= g; for (int i : pl) { if (1LL * i * i > x) break; while (x % i == 0) { x /= i; ap.insert(i); } } if (x > 1) ap.insert(x); while (y) { if (ap.empty()) { ans += y; break; } long long d = 1e18, ch = 0; for (long long i : ap) { long long t = y % i; if (t < d) d = t; } y -= d; ans += d; multiset<long long> nap; for (long long i : ap) { if (y % i == 0) y /= i; else nap.insert(i); } nap.swap(ap); } printf("%lld\n", ans); }
2,100
CPP
#include <bits/stdc++.h> using namespace std; pair<int, int> e[100000]; vector<int> L[100001]; int Q[100001], head, tail, color[100001]; vector<int> A, B, not_adj[100001]; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0, u, v; i < m; ++i) { scanf("%d %d", &u, &v); e[i] = make_pair(u, v); L[u].push_back(v); L[v].push_back(u); } memset(color, -1, sizeof color); for (int i = 1; i <= n; ++i) { if (color[i] == -1) { head = tail = 0; Q[tail++] = i; color[i] = 0; while (head < tail) { int cur = Q[head++]; if (color[cur] == 0) A.push_back(cur); else B.push_back(cur); for (int j = L[cur].size() - 1, to; j >= 0; --j) { to = L[cur][j]; if (color[to] == -1) { Q[tail++] = to; color[to] = color[cur] ^ 1; } } } } } bool ok = false; if (A.size() % 3 == 0) { ok = true; int cont = 1; while (!A.empty()) { for (int it = 0; it < 3 && !A.empty(); ++it) { color[A.back()] = cont; A.pop_back(); } ++cont; } while (!B.empty()) { for (int it = 0; it < 3 && !B.empty(); ++it) { color[B.back()] = cont; B.pop_back(); } ++cont; } } else { if (A.size() % 3 == 2) swap(A, B); int colorA = color[A[0]]; for (int i = 0; i < m; ++i) if (color[e[i].first] != colorA) swap(e[i].first, e[i].second); sort(e, e + m); sort(A.begin(), A.end()); sort(B.begin(), B.end()); int szA = A.size(), szB = B.size(); for (int i = 0, j = 0; i < szA; ++i) { int k = j; while (k < m && e[k].first == A[i]) ++k; if (k - j <= szB - 2) { ok = true; vector<int> aux; int cont = 0; for (int a = 0, b = j, u; a < szB && cont < 2; ++a) { u = B[a]; if (b == k || u != e[b].second) { aux.push_back(u); ++cont; } else ++b; } memset(color, -1, sizeof color); color[A[i]] = color[aux[0]] = color[aux[1]] = 1; cont = 2; while (!A.empty()) { for (int it = 0; it < 3 && !A.empty();) { if (color[A.back()] == -1) { color[A.back()] = cont; ++it; } A.pop_back(); if (it == 3) ++cont; } } while (!B.empty()) { for (int it = 0; it < 3 && !B.empty();) { if (color[B.back()] == -1) { color[B.back()] = cont; ++it; } B.pop_back(); if (it == 3) ++cont; } } break; } else if (k - j == szB - 1) { for (int a = 0, b = j, u; a < szB; ++a) { u = B[a]; if (b == k || u != e[b].second) { not_adj[u].push_back(A[i]); break; } else ++b; } } j = k; } if (!ok) { for (int i = 0; i < szB; ++i) { if (not_adj[B[i]].size() >= 2) { for (int j = i + 1; j < szB; ++j) { if (not_adj[B[j]].size() >= 2) { ok = true; memset(color, -1, sizeof color); color[B[i]] = color[not_adj[B[i]][0]] = color[not_adj[B[i]][1]] = 1; color[B[j]] = color[not_adj[B[j]][0]] = color[not_adj[B[j]][1]] = 2; int cont = 3; while (!A.empty()) { for (int it = 0; it < 3 && !A.empty();) { if (color[A.back()] == -1) { color[A.back()] = cont; ++it; } A.pop_back(); if (it == 3) ++cont; } } while (!B.empty()) { for (int it = 0; it < 3 && !B.empty();) { if (color[B.back()] == -1) { color[B.back()] = cont; ++it; } B.pop_back(); if (it == 3) ++cont; } } break; } } break; } } } } if (!ok) puts("NO"); else { puts("YES"); for (int i = 1; i <= n; ++i) printf("%d ", color[i]); printf("\n"); } return 0; }
2,500
CPP
t = int(input()) for _ in range(t): n, k, d = map(int, input().split()) timetable = list(map(int, input().split())) i = 0 j = d used = {} for x in range(d): if timetable[x] in used.keys(): used[timetable[x]] += 1 else: used[timetable[x]] = 1 ans = len(used) while j < n: if timetable[i] in used.keys(): used[timetable[i]] -= 1 if used[timetable[i]] == 0: used.pop(timetable[i]) i += 1 if timetable[j] in used.keys(): used[timetable[j]] += 1 else: used[timetable[j]] = 1 j += 1 ans = min(ans, len(used)) print(ans)
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 200 + 10, maxm = 4e4 + 10, maxp = 2e4 + 10; struct Graph { int ndn, edn, last[maxn]; int u[maxm * 2], v[maxm * 2], c[maxm * 2], nxt[maxm * 2]; void init(int _n) { ndn = _n; edn = 0; memset(last, -1, sizeof last); } void adde(int _u, int _v, int _c) { u[edn] = _u; v[edn] = _v; c[edn] = _c; nxt[edn] = last[_u]; last[_u] = edn++; } }; struct Dinic { Graph *G; int second, T, dist[maxn], cur[maxn]; int bfs(); int dfs(int, int); int solve(Graph *, int, int); }; int N, A[maxn]; bool sieve[maxp]; bool vis[maxn]; vector<int> to[maxn]; vector<vector<int> > ans; Graph G; Dinic D; void gen(int); void dfs(int, vector<int> &); int main() { for (int i = 2; i < maxp; i++) if (!sieve[i]) for (int j = i * i; j < maxp; j += i) sieve[j] = 1; int second, T; while (~scanf("%d", &N)) { second = N + 1, T = N + 2; G.init(N + 2); for (int i = 1; i <= N; i++) scanf("%d", &A[i]); int sum = 0; for (int i = 1; i <= N; i++) { if (A[i] & 1) G.adde(second, i, 2), G.adde(i, second, 0); else G.adde(i, T, 2), G.adde(T, i, 0), sum += 2; } int eno = G.edn; for (int e0 = G.last[second]; ~e0; e0 = G.nxt[e0]) for (int e1 = G.last[T]; ~e1; e1 = G.nxt[e1]) { int u = G.v[e0], v = G.v[e1]; if (!sieve[A[u] + A[v]]) { G.adde(u, v, 1); G.adde(v, u, 0); } } vector<int> res; if (D.solve(&G, second, T) != sum) puts("Impossible"); else { memset(vis, 0, sizeof vis); ans.clear(); for (int i = 1; i <= N; i++) to[i].clear(); for (int e = eno; e < G.edn; e += 2) if (!G.c[e]) { to[G.u[e]].push_back(G.v[e]); to[G.v[e]].push_back(G.u[e]); } for (int i = 1; i <= N; i++) if (!vis[i]) { res.clear(); dfs(i, res); ans.push_back(res); } cout << ans.size() << endl; for (auto now : ans) { cout << now.size(); for (auto ct : now) cout << " " << ct; puts(""); } } } return 0; } void dfs(int u, vector<int> &ans) { vis[u] = 1; ans.push_back(u); for (auto v : to[u]) if (!vis[v]) { dfs(v, ans); return; } } int Dinic::solve(Graph *g, int s, int t) { G = g; second = s; T = t; int res = 0; while (bfs()) { for (int i = 1; i <= G->ndn; i++) cur[i] = G->last[i]; res += dfs(second, 1e9); } return res; } int Dinic::bfs() { memset(dist, -1, sizeof dist); dist[second] = 0; queue<int> que; que.push(second); while (que.size()) { int u = que.front(); que.pop(); for (int e = G->last[u]; ~e; e = G->nxt[e]) { int v = G->v[e]; if (dist[v] == -1 && G->c[e] > 0) { dist[v] = dist[u] + 1; que.push(v); } } } return ~dist[T]; } int Dinic::dfs(int u, int tmin) { if (u == T || tmin == 0) return tmin; int nflw = 0, f; for (int &e = cur[u]; ~e; e = G->nxt[e]) { int v = G->v[e]; if (dist[u] + 1 == dist[v] && (f = dfs(v, min(tmin, G->c[e]))) > 0) { G->c[e] -= f; G->c[e ^ 1] += f; nflw += f; tmin -= f; if (tmin == 0) break; } } return nflw; }
2,300
CPP
alf = 'abcdefghijklmnopqrstuvwxyz' n = int(input()) for j in range(n): tmp = '' n, m, k = map(int, input().split()) tmp = alf[:k] res = tmp for i in range(n//k-1): res += tmp res += alf[:n-len(res)] print(res)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; cout << n - (n / 2 + n / 3 + n / 5 + n / 7 - n / 6 - n / 10 - n / 14 - n / 15 - n / 21 - n / 35 + n / 30 + n / 105 + n / 70 + n / 42 - n / 210); }
1,100
CPP
#include <bits/stdc++.h> using namespace std; set<int> st; map<int, int> mp1; vector<vector<int> > adj; vector<int> deg1, deg2; int main() { int n; cin >> n; adj.resize(n + 2); for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } for (int i = 1; i <= n; i++) { if (adj[i].size() == 1) { deg1.push_back(i); } else if (adj[i].size() > 2) { deg2.push_back(i); } } if (deg2.size() > 1) { cout << "No" << endl; return 0; } cout << "Yes" << endl; if (deg2.size() == 1) { int u = deg2[0]; cout << adj[u].size() << endl; for (int i = 0; i < deg1.size(); i++) { int v = deg1[i]; cout << u << ' ' << v << endl; } } else { int u = deg1[0]; int v = deg1[1]; cout << 1 << endl; cout << u << ' ' << v << endl; } return 0; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 310; vector<int> L, C; const int INF = 1 << 29; int dp[MAXN][1 << 9]; int mask[MAXN]; int main() { int n; scanf("%d", &n); L.resize(n); C.resize(n); for (int i = 0; i < n; i++) { scanf("%d", &L[i]); } for (int i = 0; i < n; i++) { scanf("%d", &C[i]); } int ans = INF; for (int i = 0; i < n; i++) { if (L[i] == 1) { ans = min(C[i], ans); continue; } int x = L[i]; vector<int> facts; for (int j = 2; j * j <= x; j++) { if (x % j == 0) { facts.push_back(j); while (x % j == 0) { x /= j; } } } if (x != 1) { facts.push_back(x); } int m = facts.size(); int sl = L[i], sc = C[i]; fill(dp[0], dp[0] + (1 << m), INF); for (int j = 0; j < n; j++) { mask[j] = 0; for (int k = 0; k < m; k++) { if (L[j] % facts[k] != 0) { mask[j] |= 1 << k; } } } dp[0][0] = 0; for (int j = 0; j < n; j++) { fill(dp[j + 1], dp[j + 1] + (1 << m), INF); for (int k = 0; k < (1 << m); k++) { if (dp[j][k] == INF) continue; dp[j + 1][k] = min(dp[j + 1][k], dp[j][k]); int nk = k | mask[j]; dp[j + 1][nk] = min(dp[j + 1][nk], dp[j][k] + C[j]); } } ans = min(ans, sc + dp[n][(1 << m) - 1]); } if (ans < INF) printf("%d\n", ans); else printf("-1\n"); return 0; }
1,900
CPP
s = input() LETTERS = 'abcdefghijklmnopqrstuvwxyz' sum1 = 0 sum2 = 0 def getPrice(s): global sum1, sum2 if '.' in s: if s[-3] == '.': first = '' for i in range(len(s)-3): if s[i] != '.': first+=s[i] first = int(first) last = int(s[-2:]) else: first = '' last = 0 for i in range(len(s)): if s[i] != '.': first+=s[i] first = int(first) else: first = int(s) last = 0 sum1 += first sum2 += last return 0 def insertDots(s): i = -1 new_s = '' count = 0 while i >= -len(s): new_s+=s[i] count += 1 if count == 3 and i > -len(s): new_s +='.' count = 0 i -=1 return new_s[::-1] def getItem(): global s done = False #name = '' curr = 0 while not done: if s[curr].lower() in LETTERS: #name+=s[curr] curr += 1 else: done = True done = False price = '' while not done and curr < len(s): if s[curr].lower() not in LETTERS: price+=s[curr] curr += 1 else: done = True s = s[curr:] getPrice(price) return 0 def decode(s): if '.' in s: rans = insertDots(s[:s.find('.')])+s[s.find('.'):] if rans[-3] != '.': rans+='0' return rans else: return s ls1 = [] while s: getItem() ans = decode(str(sum1+sum2/100.0)) if len(ans) >=3: if ans[-2] == '.': ans += '0' if ans[-1] == '0' and ans[-2] == '0': ans = ans[:-3] print(ans)
1,600
PYTHON3
n = int(input()) l = list(map(int, input().rstrip().split(" "))) if n == 1: print(0) elif n == 2: if l[0] > l[1]: print(1) else: print(0) else: going_down = False la = l[0] c = 0 for i in range(1,n): if not going_down: if l[i]<l[i-1]: going_down=True if l[i]>la: c = -1 break else: c=c+1 else: if l[i]<l[i-1] or l[i]>la: c = -1 break c+=1 print(c)
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long int INF = (1LL << 58); const bool DEBUG = 0; const int NODES_LIMIT = 100005; int nodes, edges; std::vector<int> adj[2][NODES_LIMIT]; bool vis[NODES_LIMIT]; int color[NODES_LIMIT], globalColor = 1; int componentSize[NODES_LIMIT]; bool hasCycle[NODES_LIMIT]; int doColor(int node, int thisColor) { vis[node] = 1; color[node] = thisColor; int i, limit = adj[0][node].size(); int ret = 0; for (i = 0; i < limit; i++) { int v = adj[0][node][i]; if (!vis[v]) ret += doColor(v, thisColor); } return ret + 1; } bool recStack[NODES_LIMIT]; bool detectCycle(int node) { int limit = adj[1][node].size(); vis[node] = 1; recStack[node] = 1; for (int i = 0; i < limit; i++) { int v = adj[1][node][i]; if (!vis[v]) { if (detectCycle(v)) return 1; } else if (recStack[v] == 1) return 1; } recStack[node] = 0; return 0; } int main() { int i, j; scanf("%d", &nodes); scanf("%d", &edges); for (i = 0; i < edges; i++) { int x, y; scanf("%d", &x); scanf("%d", &y); x--; y--; adj[0][x].push_back(y); adj[0][y].push_back(x); adj[1][x].push_back(y); } for (i = 0; i < nodes; i++) { if (!vis[i]) { componentSize[globalColor] = doColor(i, globalColor); globalColor++; } } memset(vis, 0, sizeof(vis)); for (i = 0; i < nodes; i++) if (!vis[i]) hasCycle[color[i]] |= detectCycle(i); int ans = 0; for (i = 1; i < globalColor; i++) ans += (componentSize[i] - 1) + (int)hasCycle[i]; printf("%d\n", ans); return 0; }
2,200
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int a, b, length = 0; while (n > 0) { cin >> a >> b; length += b - a + 1; n--; } if (length % k == 0) { cout << 0; } else { cout << k - (length % k); } }
1,100
CPP
n=int(input()) s=(n+1)*n//2 for i in range(1,n-1): s+=i*(n-1-i) print(s)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n, a, b; long long r1 = 0, r2 = 0; int main() { cin >> n; for (int i = 0; i < n * 2; i++) { cin >> a >> b; r1 += a, r2 += b; } cout << r1 / n << ' ' << r2 / n; return 0; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; inline int SG(double x) { if (x > 1E-8) return 1; if (x < -1E-8) return -1; return 0; } class PT { public: double x, y; PT() {} PT(double _x, double _y) { x = _x; y = _y; } PT operator+(const PT& p) const { return PT(x + p.x, y + p.y); } PT operator-(const PT& p) const { return PT(x - p.x, y - p.y); } double operator^(const PT& p) const { return x * p.y - y * p.x; } double operator*(const PT& p) const { return x * p.x + y * p.y; } double disTo(PT& p) const { return sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y)); } }; inline double tri(PT& p1, PT& p2, PT& p3) { return (p2 - p1) ^ (p3 - p1); } class PY { public: int n; PT pt[5]; PT& operator[](const int x) { return pt[x]; } void input() { int i; n = 4; for (i = 0; i < n; i++) { scanf("%lf %lf", &pt[i].x, &pt[i].y); } } double getArea() { int i; double s = pt[n - 1] ^ pt[0]; for (i = 0; i < n - 1; i++) { s += pt[i] ^ pt[i + 1]; } return s / 2; } }; PY py[500]; pair<double, int> c[5000]; inline double segP(PT& p, PT& p1, PT& p2) { if (SG(p1.x - p2.x) == 0) return (p.y - p1.y) / (p2.y - p1.y); return (p.x - p1.x) / (p2.x - p1.x); } double polyUnion(int n) { int i, j, ii, jj, ta, tb, r, d; double z, w, s, sum, tc, td; for (i = 0; i < n; i++) { py[i][py[i].n] = py[i][0]; } sum = 0; for (i = 0; i < n; i++) { for (ii = 0; ii < py[i].n; ii++) { r = 0; c[r++] = make_pair(0.0, 0); c[r++] = make_pair(1.0, 0); for (j = 0; j < n; j++) { if (i == j) continue; for (jj = 0; jj < py[j].n; jj++) { ta = SG(tri(py[i][ii], py[i][ii + 1], py[j][jj])); tb = SG(tri(py[i][ii], py[i][ii + 1], py[j][jj + 1])); if (ta == 0 && tb == 0) { if ((py[j][jj + 1] - py[j][jj]) * (py[i][ii + 1] - py[i][ii]) > 0 && j < i) { c[r++] = make_pair(segP(py[j][jj], py[i][ii], py[i][ii + 1]), 1); c[r++] = make_pair(segP(py[j][jj + 1], py[i][ii], py[i][ii + 1]), -1); } } else if (ta >= 0 && tb < 0) { tc = tri(py[j][jj], py[j][jj + 1], py[i][ii]); td = tri(py[j][jj], py[j][jj + 1], py[i][ii + 1]); c[r++] = make_pair(tc / (tc - td), 1); } else if (ta < 0 && tb >= 0) { tc = tri(py[j][jj], py[j][jj + 1], py[i][ii]); td = tri(py[j][jj], py[j][jj + 1], py[i][ii + 1]); c[r++] = make_pair(tc / (tc - td), -1); } } } sort(c, c + r); z = min(max(c[0].first, 0.0), 1.0); d = c[0].second; s = 0; for (j = 1; j < r; j++) { w = min(max(c[j].first, 0.0), 1.0); if (!d) s += w - z; d += c[j].second; z = w; } sum += (py[i][ii] ^ py[i][ii + 1]) * s; } } return sum / 2; } int main() { int n, i, j, k; double sum, ds; scanf("%d", &n); sum = 0; for (i = 0; i < n; i++) { py[i].input(); ds = py[i].getArea(); if (ds < 0) { for (j = 0, k = py[i].n - 1; j < k; j++, k--) { swap(py[i][j], py[i][k]); } ds = -ds; } sum += ds; } printf("%.9f\n", sum / polyUnion(n)); return 0; }
2,700
CPP
def main(): first = second = third = 0; n = int(input()); answer = ""; for _ in range(n): first = second; second = third; if first == 1: third = 2; else: third = 1; if third == 1: answer += 'a'; else: answer += 'b'; print(answer); return main()
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, *Z; string s; long long sum = 0, mx = 0, mn = 0, t, x = 0; cin >> n; Z = new int[n]; for (int i = 0; i < n; ++i) { cin >> Z[i]; sum += Z[i]; } cin >> s; for (int i = 0; i < n; ++i) { if (s[i] == 'A') { x -= Z[i]; if (x < mn) mn = x; } else { x += Z[i]; if (x > mx) mx = x; } } if (mx - x > -mn) t = mx - x; else t = -mn; cout << (sum + x + 2 * t) / 2 << endl; return 0; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1 << 20 + 5; int board[2][N], n, est[22]; long long solver[22][2]; inline void merged() { int num = 1 << n; for (int lvl = int(1); lvl < int(n + 1); ++lvl) { int ato = 1 << lvl; int ito = ato >> 1; int sig = (lvl - 1) & 1; for (int i = 0; i < num; i += ato) { int c1 = 0, c2 = 0; while (c1 < ito && c2 < ito) { int p1 = i + c1; int p2 = i + ito + c2; if (board[sig][p1] > board[sig][p2]) board[lvl & 1][i + c1 + c2] = board[sig][p2], c2++, solver[lvl][0] += ito - c1; else board[lvl & 1][i + c1 + c2] = board[sig][p1], c1++; } while (c1 < ito) board[lvl & 1][i + c1 + c2] = board[sig][i + c1], c1++; while (c2 < ito) board[lvl & 1][i + c1 + c2] = board[sig][i + ito + c2], c2++; } } } inline void mergei() { int num = 1 << n; for (int lvl = int(1); lvl < int(n + 1); ++lvl) { int ato = 1 << lvl; int ito = ato >> 1; int sig = (lvl - 1) & 1; for (int i = 0; i < num; i += ato) { int c1 = 0, c2 = 0; while (c1 < ito && c2 < ito) { int p1 = i + c1; int p2 = i + ito + c2; if (board[sig][p1] > board[sig][p2]) board[lvl & 1][i + c1 + c2] = board[sig][p2], c2++, solver[lvl][1] += ito - c1; else board[lvl & 1][i + c1 + c2] = board[sig][p1], c1++; } while (c1 < ito) board[lvl & 1][i + c1 + c2] = board[sig][i + c1], c1++; while (c2 < ito) board[lvl & 1][i + c1 + c2] = board[sig][i + ito + c2], c2++; } } } inline long long query(int k) { est[k] = 1 - est[k]; long long ret = 0; int e = 0; for (int i = int(n); i >= int(0); i--) { e ^= est[i]; ret += solver[i][e]; } return ret; } int main() { scanf("%d", &n); vector<int> v((1 << n)); for (int i = int(0); i < int(1 << n); ++i) scanf("%d", &v[i]); for (int i = int(0); i < int(1 << n); ++i) board[0][i] = v[i]; merged(); reverse((v).begin(), (v).end()); for (int i = int(0); i < int(1 << n); ++i) board[0][i] = v[i]; mergei(); int q; scanf("%d", &q); while (q--) { int k; scanf("%d", &k); printf("%lld\n", query(k)); } return 0; }
2,100
CPP
n = int(input()) arr = [] for i in range(n): arr.append(list(map(int,input().split()))) prev = 10000000000000000000000 for i in arr: m = max(i[0], i[1]) n = min(i[0], i[1]) if m <= prev: prev = m elif n <= prev: prev = n else : print("NO") break else : print("YES")
1,000
PYTHON3
# __ __ __ # _________ ____/ /__ ____/ /_______ ____ _____ ___ ___ _____ ____/ /___ _ # / ___/ __ \/ __ / _ \/ __ / ___/ _ \/ __ `/ __ `__ \/ _ \/ ___// __ / __ `/ # / /__/ /_/ / /_/ / __/ /_/ / / / __/ /_/ / / / / / / __/ / / /_/ / /_/ / # \___/\____/\__,_/\___/\__,_/_/ \___/\__,_/_/ /_/ /_/\___/_/____\__,_/\__, / # /_____/ /____/ from sys import * '''sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') ''' from collections import defaultdict as dd from math import * from bisect import * #sys.setrecursionlimit(10 ** 8) def sinp(): return input() def inp(): return int(sinp()) def minp(): return map(int, sinp().split()) def linp(): return list(minp()) def strl(): return list(sinp()) def pr(x): print(x) mod = int(1e9+7) for _ in range(inp()): n, m = minp() grid = [list(sinp()) for i in range(n)] temp1 = [] temp2 = [] for i in range(m): if i % 2 == 0: temp1.append('R') temp2.append('W') else: temp2.append('R') temp1.append('W') grid1 = [] grid2 = [] for i in range(n): if not(i & 1): grid1.append(temp1) grid2.append(temp2) else: grid2.append(temp1) grid1.append(temp2) i = 0 f = False while i < n and not f: for j in range(m): if grid[i][j] != '.' and grid[i][j] != grid2[i][j]: f = True break i += 1 if not f: pr('YES') for i in grid2: print(*i, sep='') continue f = False i = 0 while i < n and not f: for j in range(m): if grid[i][j] != '.' and grid[i][j] != grid1[i][j]: f = True break i += 1 if not f: pr('YES') for i in grid1: print(*i, sep='') continue pr('NO')
0
PYTHON3
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool uax(T &x, T y) { return (y > x) ? x = y, true : false; } template <typename T> inline bool uin(T &x, T y) { return (y < x) ? x = y, true : false; } template <typename T> void kek(T ans) { cout << ans << endl; exit(0); } using i64 = int64_t; const int MOD = (int)1e9 + 7; const i64 INF = (i64)1e18 + 42; void SolveCase() { int n, m, k; cin >> n >> m >> k; vector<int> xs(n), ys(m); for (int &x : xs) cin >> x; for (int &x : ys) cin >> x; vector<pair<int, int>> a(k); for (auto &[x, y] : a) cin >> x >> y; i64 ans = 0; a.emplace_back(1e9, 1e9); for (int rep = 0; rep < 2; ++rep) { sort(begin(a), end(a)); for (int i = 0, p = 0; i < n - 1; ++i) { while (a[p].first <= xs[i]) ++p; vector<int> t; while (a[p].first < xs[i + 1]) t.push_back(a[p++].second); sort(begin(t), end(t)); int cnt = 0, run = 0, prv = -1; for (int x : t) { if (x > prv) { cnt += run; run = 0; } ++run; ans += cnt; prv = x; } } swap(n, m); swap(xs, ys); for (auto &[x, y] : a) swap(x, y); } cout << ans << '\n'; } int32_t main() { cin.tie(nullptr)->sync_with_stdio(false); int tt; cin >> tt; while (tt--) SolveCase(); }
1,900
CPP
import math t=int(input()) for x in range(t): ans=0 n=int(input()) if n==1: print(0) else: n=math.ceil(n/2) for i in range(1,n): ans=ans+(8*i*i) print(ans)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); const int m = 200001; vector<int> a(n), cnt(m, 0); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); cnt[a[i]]++; } map<int, set<int>> mp; for (int i = 1; i < m; i++) { if (cnt[i] < 2) { mp[cnt[i]].insert(i); } cnt[i] += cnt[i - 1]; } mp[0].insert(m); mp[1].insert(m); int k = 0, l, r; for (int i = 1; i < m; i++) { if (cnt[i] - cnt[i - 1]) { int idx0 = *mp[0].upper_bound(i); int idx1 = *mp[1].upper_bound(i); if (idx0 == m) { idx0--; } if (idx1 == m) { idx1--; } int idx = min(idx0, idx1); int temp = cnt[idx] - cnt[i - 1]; if (temp > k) { k = temp; l = i; r = idx; } } } vector<int> ans(k); int leftIdx = 0, rightIdx = k - 1; while (leftIdx <= rightIdx && l <= r) { ans[leftIdx++] = l; int x = (cnt[l] - cnt[l - 1]) - 1; while (x--) { ans[rightIdx--] = l; } l++; } printf("%d\n", int(ans.size())); for (auto p : ans) { printf("%d ", p); } printf("\n"); }
2,000
CPP
I=lambda:map(int,input().split()) n,k=I() a=[[*map(int,input().split())]for _ in[0]*n] r=0 for u,v in zip(a,a[1:]): r+=((u[0]-v[0])**2+(u[1]-v[1])**2)**0.5 print(r*k/50)
900
PYTHON3
n, m, a = (int(i) for i in input().split()) x = n // a + bool((n % a)) y = m // a + bool((m % a)) print(x*y)
1,000
PYTHON3
t = int(input()) for j in range(t): k = int(input()) a = list(map(int, input().split())) m = a[0]; r = 0 for i in range(1, k): if a[i] >= m and (a[i-1] < a[i] or (i < k-1 and a[i+1] < a[i])): m = a[i]; r = i if r > 0 or a[1] < a[0]: print(r+1) else: print(-1)
900
PYTHON3
h, w = map(int, input().split()) board = [input() for _ in range(h)] out = 0 cx = 0 cy = 0 if board[0][0] == '*': out += 1 while True: poss = [] for i in range(h): for j in range(w): if board[i][j] == '*' and i >= cx and j >= cy and i + j > cx +cy: poss.append((i+j,i,j)) if poss: _, cx, cy = min(poss) out += 1 else: break print(out)
1,800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int tc; cin >> tc; while (tc--) { int n; scanf("%d", &n); int ans; if (n % 2 == 0) { ans = (n / 2) - 1; } else { ans = (n / 2); } cout << ans << endl; } return 0; }
800
CPP
t=int(input()) for o in range(t): n=int(input()) a=list(map(int,input().split())) pos=[0]*n for i in range(n): pos[a[i]-1]=i m1=pos[0] m2=pos[0] ans=[] for i in range(n): if pos[i]>m2: m2=pos[i] if pos[i]<m1: m1=pos[i] if m2-m1==i: ans.append(1) else: ans.append(0) print(*ans,sep="")
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { string s; int counter = 0, count = 1, c = 0, dif = 0; cin >> s; int n = s.length(); for (int i = 0; i < n; i++) { if (s[i] == '(') counter++; if (s[i] == ')') counter--; if (s[i] == '#') c++; if (counter < 0) { cout << "-1"; return 0; } } if (counter < c) { cout << "-1"; return 0; } if (c == 0 && counter != 0) { cout << "-1"; return 0; } dif = counter; counter = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') counter++; if (s[i] == ')') counter--; if (s[i] == '#' && count != c) { counter--; count++; } else if (s[i] == '#' && count == c) counter -= dif - c + 1; if (counter < 0) { cout << "-1"; return 0; } } for (int i = 0; i < c - 1; i++) { cout << 1 << endl; } if (c > 0) cout << dif - c + 1; }
1,500
CPP
#include <bits/stdc++.h> using namespace std; const int DMAX = 66000; int n, a, b, nr[DMAX], x[DMAX], ans; class cmp { public: bool operator()(const int& a, const int& b) { if (nr[a] != nr[b]) return nr[a] < nr[b]; else return a < b; } }; set<int, cmp> s; vector<pair<int, int> > v; void print_set() { cout << "\nSET:"; for (set<int>::iterator i = s.begin(); i != s.end(); ++i) cout << *i << " \n"[i == s.end()]; cout << '\n'; } void solve() { while (!s.empty()) { int k = *s.begin(); if (nr[k] == 0) s.erase(s.begin()); else { v.push_back({x[k], k}); ans++; nr[k]--; int kk = x[k]; s.erase(s.begin()); if (s.find(kk) != s.end()) s.erase(s.find(kk)); x[kk] = x[kk] ^ k; nr[kk]--; s.insert(kk); } } } void print() { cout << ans << '\n'; for (int i = 0; i < v.size(); i++) cout << v[i].first << ' ' << v[i].second << '\n'; } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a >> b; nr[i] = a; x[i] = b; s.insert(i); } solve(); print(); return 0; }
1,500
CPP
#include <bits/stdc++.h> using namespace std; int a[300005], b[300005]; int main() { int i, k, t, x, y, n, q, num1, num2; string s; scanf("%d", &t); for (i = 0; i < t; i++) { scanf("%d %d", &n, &q); cin >> s; s = " " + s; for (k = 0; k <= n; k++) a[k] = b[k] = 0; if (s[1] == '+') a[1] = a[2] = 1; else a[1] = a[2] = -1; if (s[2] == '+') b[2] = 1; else b[2] = -1; for (k = 3; k <= n; k++) { if (k % 2 == 1) { if (s[k] == '+') a[k] = a[k - 2] + 1; else a[k] = a[k - 2] - 1; b[k] = b[k - 1]; } else { if (s[k] == '+') b[k] = b[k - 2] + 1; else b[k] = b[k - 2] - 1; a[k] = a[k - 1]; } } for (k = 0; k < q; k++) { scanf("%d %d", &x, &y); num1 = a[y] - a[x - 1]; num2 = b[y] - b[x - 1]; if (abs(num1 - num2) > 2) { if (abs(num1 - num2) % 2 == 1) printf("1\n"); else printf("2\n"); } else printf("%d\n", abs(num1 - num2)); } } return 0; }
1,700
CPP
students, total = list(map(int, input().split(" "))) puzzles = list(map(int, input().split(" "))) puzzles.sort() minimum = 1001 for i in range(total - students + 1): minimum = min(minimum, puzzles[i + students - 1] - puzzles[i]) print(minimum)
900
PYTHON3
t=int(input()) for i in range(t): k=int(input()) n=list(input()) #l,r=map(int,input().split()) #print(n) if("4" in n): print(1) print(4) elif("6" in n): print(1) print(6) elif("8" in n): print(1) print(8) elif("9" in n): print(1) print(9) elif("1" in n): print(1) print(1) elif("5" in n): print(2) c=n.index("5") if(c==0): if(n[1]=="3"): if(len(n)>2 and n[2]=="7"): print(57) else: print("".join(n[1:3])) else: print("".join(n[:2])) else: print("".join(n[c-1:c+1])) elif("2" in n): print(2) c=n.index("2") if(c==0): if(n[1]=="3"): if(len(n)>2 and n[2]=="7"): print(27) else: print("".join(n[1:3])) else: print("".join(n[0:2])) else: print("".join(n[c-1:c+1])) else: print(2) c3=n.count("3") c7=n.count("7") if(c3>1): print(33) elif(c7>1): print(77)
1,000
PYTHON3
n = int(input()) COUNT = 0 for _ in range(n): ones = input().count("1") if ones>= 2: COUNT += 1 print(COUNT)
800
PYTHON3
vowels = 'AEIOUY' s = list(input()) counter = 0 ans = 0 if len(s) > 1: found = False for i in range(len(s)): if s[i] in vowels: found = True if counter > ans: ans = counter counter = 0 continue counter += 1 if counter > ans: ans = counter if found: print(ans + 1) else: print(len(s) + 1) else: if s[0] in vowels: print(1) else: print(2)
1,000
PYTHON3
from collections import Counter as cnt n,m=map(int,input().split()) ll=[list(map(int,input().split())) for _ in range(m)] if n==1: print(1) else: l=[] for city in ll: l.append(1+(city.index(max(city)))) dic=cnt(l) maxvalur=dic[max(dic,key=dic.get)] ans=[] for key in dic: if dic[key]==maxvalur: ans.append(key) print(min(ans))
1,100
PYTHON3
def main(): a,b = [int(v) for v in input().split()] for i in range(1,11): if a*i%10==0 or (a*i-b)%10 == 0: print(i) return if __name__ == "__main__": main()
800
PYTHON3