solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; string s; map<char, bool> p; int main() { p['A'] = 1, p['E'] = 1, p['F'] = 1, p['H'] = 1, p['I'] = 1, p['K'] = 1, p['L'] = 1, p['M'] = 1, p['N'] = 1, p['T'] = 1, p['V'] = 1, p['W'] = 1, p['X'] = 1, p['Y'] = 1, p['Z'] = 1; cin >> s; int sum = 0; for (int i = 0; i < s.size(); i++) sum += p[s[i]]; if (sum == s.size() or sum == 0) cout << "YES"; else cout << "NO"; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; typedef struct { int x, y, z; } Node; Node e[1000010]; char a[1010][1010]; int vis[1010][1010]; int main() { int i, j, k, n, m, d; scanf("%d%d", &n, &m); for (i = 0; i < n; i++) scanf("%s", a[i]); d = 0; for (i = 0; i < n; i++) for (j = 0; j < m; j++) if (a[i][j] == '*') { k = 1; while (i - k >= 0 && a[i - k][j] == '*' && i + k < n && a[i + k][j] == '*' && j - k >= 0 && a[i][j - k] == '*' && j + k < m && a[i][j + k] == '*') { vis[i][j] = 1; vis[i - k][j] = 1; vis[i + k][j] = 1; vis[i][j - k] = 1; vis[i][j + k] = 1; k++; } k--; if (k > 0) { e[d].x = i + 1; e[d].y = j + 1; e[d].z = k; d++; } } for (i = 0; i < n; i++) for (j = 0; j < m; j++) if (a[i][j] == '*' && vis[i][j] == 0) { printf("-1\n"); return 0; } printf("%d\n", d); for (i = 0; i < d; i++) printf("%d %d %d\n", e[i].x, e[i].y, e[i].z); return 0; }
1,700
CPP
n = int(input()) for i in range(0,n): p,a,b,c = map(int, input().split()) minm = min(a,b,c) if p%a==0 or p%b==0 or p%c==0: print(0) elif minm>p: print(minm-p) else: print(min(a-(p%a), b-(p%b), c-(p%c)))
800
PYTHON3
# coding: utf-8 # In[20]: n = int(input()) for i in range(n): str = input() if(("".join(reversed(str)) != str)): print(str) else: if(len(str) == 1): print(-1) else: for x in range(len(str) - 1): if str[x] != str[x + 1]: a = x b = x + 1 temp = str[b] trailer = str[b + 1:] if b + 1 < len(str) else '' str = str[0:b] + str[a] + trailer str = str[0:a] + temp + str[a + 1:] print(str) break elif x == len(str) - 2: print(-1) break
900
PYTHON3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:167177216") const unsigned long long mod = 1000000007; int Inf = (int)2e9; long long LINF = (long long)1e18 + 1e17; using namespace std; int main() { int n; cin >> n; vector<long long> a(n); map<long long, long long> cnt; for (int(i) = 0; (i) < n; (i)++) { cin >> a[i]; cnt[a[i]]++; } sort((a).begin(), (a).end()); long long x, y, z; x = a[0]; y = a[1]; z = a[2]; if (z != y) { cout << cnt[z]; } else { if (x == y) { cout << cnt[z] * (cnt[z] - 1) * (cnt[z] - 2) / 6; } else { cout << cnt[z] * (cnt[z] - 1) / 2; } } return 0; }
1,500
CPP
n=int(input()) dic={'C':6,'T':4,'O':8,'D':12,'I':20} ans=0 for _ in range(n): ans+=dic[input()[0]] print(ans)
800
PYTHON3
n=int(input()) d=[int(i) for i in input().split()] a,b=map(int,input().split()) k=0 while a!=b: k+=d[a-1] a+=1 print(k)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; char s[500001]; int n, t; bool judge(int x) { int far = 0, far2 = 0, sum = x; for (int i = 1; i <= n; i++) { if (s[i] == 'S') sum++; if (s[i] == 'H') sum--; if (sum == 0 || s[i] == 'H') far = i; if (s[i] == 'H') far2 = i; } if (sum < 0) return false; int las = 0, res = 0, ans = 999999999; sum = x; for (int i = 1; i <= far; i++) { res++; if (s[i] == 'S') { sum++; if (sum == 0) { res += (i - las) * 2; las = 0; } } if (s[i] == 'H') { sum--; if (sum == -1) { ans = min(ans, res + (far - i) * 2); las = i; } } if (i >= far2 && las == 0) ans = min(ans, res - (i - far2)); } return ans <= t; } signed main() { cin >> n >> t; cin >> s + 1; int l = -1, r = 100001; while (l + 1 < r) { int mid = (l + r) / 2; if (judge(mid)) r = mid; else l = mid; } printf("%d", (r != 100001) ? r : -1); return 0; }
2,300
CPP
#include <bits/stdc++.h> using namespace std; bool res, res2 = false; char swapc(char a) { if (a == 'W') return 'B'; else return 'W'; } void solution(string s, char d, vector<int> &a) { int cnt = 0; for (int i = 0; i < s.size() - 1; i++) { if (s[i] != d) { s[i] = swapc(s[i]); s[i + 1] = swapc(s[i + 1]); a.push_back(i + 1); } else cnt++; } if (s[0] != s[s.size() - 1]) a.clear(); else { cnt++; res2 = true; } if (cnt == s.size()) { res = true; a.clear(); } } int main() { int n; cin >> n; string ans; cin >> ans; vector<int> v; solution(ans, 'B', v); if (!res2) solution(ans, 'W', v); if (res) cout << 0 << '\n'; else if (v.empty()) cout << -1 << '\n'; else { cout << v.size() << '\n'; for (auto i : v) cout << i << ' '; cout << '\n'; } return 0; }
1,300
CPP
#include<bits/stdc++.h> using namespace std; #define ll long long int #define ld long double #define pi (3.141592653589) #define pb push_back #define mp make_pair #define INF 1000000000 #define MOD 1000000007 #define vll vector<ll> #define vpll vector<pair<ll,ll> > #define pll pair<ll> #define ff first #define ss second int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll t; cin>>t; while(t--){ ll d,k; cin>>d>>k; ll j=k; ll ma=0; ll a,b; while(d>=j) { a=j/k; b=(d*d)-(j*j); b=sqrt(b); a+=(b/k); if(a>ma) ma=a; j+=k; } if(ma%2) cout<<"Ashish"<<"\n"; else cout<<"Utkarsh"<<"\n"; } return 0; }
1,700
CPP
n=set(input()) n.discard('{') n.discard('}') n.discard(' ') n.discard(',') print(len(n))
800
PYTHON3
# https://codeforces.com/problemset/problem/71/A t = int(input()) for i in range(t): text = input() if len(text) <= 10: print(text) else: print(text[0]+str(len(text)-2)+text[-1])
800
PYTHON3
from math import * sInt = lambda: int(input()) mInt = lambda: map(int, input().split()) lInt = lambda: list(map(int, input().split())) t= sInt() for _ in range(t): a,b = mInt() if abs(b-a)%10==0: print(abs(b-a)//10) else: print(abs(b-a)//10 + 1)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; const long long INF = 1000000000000; using Graph = vector<vector<long long>>; signed main() { long long N; cin >> N; vector<pair<long long, long long>> A(N); for (long long i = 0; i < (long long)(N); i++) { long long a; cin >> a; A[i] = {a, i}; } sort(A.rbegin(), A.rend()); long long ans = 0; vector<long long> B; for (long long i = 0; i < (long long)(N); i++) { ans += A[i].first * i + 1; B.emplace_back(A[i].second + 1); } cout << ans << "\n"; for (long long i = 0; i < (long long)(N); i++) cout << B[i] << " "; cout << "\n"; }
900
CPP
from sys import stdin, stdout from collections import Counter nmbr = lambda: int(input()) lst = lambda: list(map(int, input().split())) for _ in range(nmbr()): s=input() n=len(s) p1=p2=-1 p1=0 ans=[0]*n for i in range(1,n): if s[i]!=s[p1]: p2=i break if p2==-1: print(-1) continue pt=n//2-1 ans[pt], ans[n-pt-1]=s[p1], s[p2] p=0 for i in range(n): if p==p1:p+=1 if p==p2:p+=1 if i not in [pt, n-pt-1]: ans[i]=s[p] p+=1 # print(ans) print(''.join(ans))
900
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, i, male[370] = {0}, female[370] = {0}, start, endd, temp, maxx = 0; char ch[2]; scanf("%d", &n); while (n--) { scanf("%s %d %d", ch, &start, &endd); if (ch[0] == 'M') male[start] += 1, male[endd + 1] -= 1; else female[start] += 1, female[endd + 1] -= 1; } for (i = 2; i <= 366; i++) { male[i] = male[i] + male[i - 1]; female[i] = female[i] + female[i - 1]; } for (i = 1; i <= 366; i++) { temp = (male[i] > female[i]) ? female[i] : male[i]; if (temp > maxx) maxx = temp; } maxx *= 2; printf("%d\n", maxx); return 0; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; #pragma GCC optimizer("O3") int main() { int t; cin >> t; while (t--) { int x, y, a, b; cin >> x >> y >> a >> b; int del = (y - x) / (a + b); if (del * (a + b) == y - x) { cout << del << "\n"; } else { cout << -1 << "\n"; } } }
800
CPP
#include <bits/stdc++.h> using namespace std; template <class T> inline T bigmod(T p, T e, T M) { long long ret = 1; for (; e > 0; e >>= 1) { if (e & 1) ret = (ret * p) % M; p = (p * p) % M; } return (T)ret; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T modinverse(T a, T M) { return bigmod(a, M - 2, M); } vector<int> vc; int main() { ios_base::sync_with_stdio(0); cin.tie(0); vc.push_back(0); string s; while (cin >> s) { if (s == "?") continue; if (s == "=") break; if (s == "+") vc.push_back(0); if (s == "-") vc.push_back(1); } int n; cin >> n; int pos = 0, neg = 0; for (int i = (0); i < (vc.size()); ++i) { if (vc[i] == 0) pos++; else neg++; } if (n > 100) { if (pos == 1 && neg > 0) { cout << "Impossible" << endl; return 0; } cout << "Possible" << endl; cout << n << " "; pos--; for (int i = (1); i < (vc.size()); ++i) { if (pos < neg && vc[i] == 0) { int v = neg - pos + 1; cout << "+ " << v << " "; pos = neg; } else if (vc[i] == 0) { cout << "+ " << 1 << " "; } else if (neg < pos && vc[i] == 1) { int v = pos - neg + 1; cout << "- " << v << " "; neg = pos; } else if (vc[i] == 1) { cout << "- " << 1 << " "; } } cout << "= " << n << endl; return 0; } for (int i = pos; i <= pos * n; i++) { for (int j = neg; j <= neg * n; j++) { if (i - j != n) continue; cout << "Possible" << endl; int c1 = 0, c2 = 0; for (int k = (0); k < (vc.size()); ++k) { if (vc[k] == 0) { c1++; int v = i / pos; if (c1 <= (i % pos)) v++; if (k != 0) cout << "+ "; cout << v << " "; } else { c2++; int v = j / neg; if (c2 <= (j % neg)) v++; cout << "- "; cout << v << " "; } } cout << "= " << n << endl; return 0; } } cout << "Impossible" << endl; }
1,800
CPP
n = int(input()) for i in range(n): l,v,r,d=map(int,input().split()) print(l//v-d//v+(r-1)//v)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const double EPS = 1e-9; const double PI = acos(-1.); const long long LL_INF = 1e17 + 16; const int INF = 1e9 + 10; const long long MOD = 1e9 + 7; const int MAXN = 3e5 + 5; char s[MAXN]; int cnt[2]; int used[MAXN]; void solve() { int n, k; scanf("%d%d%s", &n, &k, &s[0]); cnt[0] = cnt[1] = 0; for (int i = (0); i < (int)(k); i++) { used[i] = -1; } for (int i = (0); i < (int)(n); i++) { if (s[i] != '?') { int ind = i % k; int v = s[i] - '0'; if (used[ind] != v && used[ind] >= 0) { printf("NO\n"); return; } if (used[ind] == -1) { used[ind] = v; cnt[v]++; } } } if (cnt[0] * 2 > k || cnt[1] * 2 > k) { printf("NO\n"); return; } printf("YES\n"); } int main() { int t = 1; cin >> t; while (t--) { solve(); } }
1,500
CPP
#include <bits/stdc++.h> using namespace std; const double eps = 1e-10, inf = 1e10; char s[5010], t[5010]; int cs[30], ct[30], cc[30], n, m; bool cangive(int lim) { if (lim >= n) return false; for (int(i) = 0; (i) < (lim); (i)++) { if (cc[t[i] - 'a'] == 0) return false; cc[t[i] - 'a']--; } for (int(i) = (t[lim] - 'a' + 1); (i) <= ('z' - 'a'); (i)++) if (cc[i] > 0) return true; return false; } int main() { scanf("%s", s); scanf("%s", t); n = strlen(s); m = strlen(t); for (int(i) = 0; (i) < (n); (i)++) cs[s[i] - 'a']++; for (int(i) = 0; (i) < (m); (i)++) ct[t[i] - 'a']++; for (int(i) = (m); (i) >= (0); (i)--) { for (int(j) = 0; (j) < (30); (j)++) cc[j] = cs[j]; if (cangive(i)) { for (int(j) = 0; (j) < (i); (j)++) putchar(t[j]), cs[t[j] - 'a']--; if (i < m) for (int(j) = (t[i] - 'a' + 1); (j) <= ('z' - 'a'); (j)++) if (cs[j] > 0) { cs[j]--; putchar(j + 'a'); break; } for (int(j) = 0; (j) < (26); (j)++) while (cs[j] > 0) putchar(j + 'a'), cs[j]--; putchar('\n'); return 0; } } printf("-1\n"); return 0; }
1,900
CPP
import sys import math from collections import defaultdict MAXNUM = math.inf MINNUM = -1 * math.inf ASCIILOWER = 97 ASCIIUPPER = 65 def getInt(): return int(sys.stdin.readline().rstrip()) def getInts(): return map(int, sys.stdin.readline().rstrip().split(" ")) def getString(): return sys.stdin.readline().rstrip() def printOutput(ans): sys.stdout.write() pass def solve(ops, queries): prefixdp = [0 for _ in range(len(ops) + 1)] prefixdp[0] = (0, 0, 0) # highest, lowest, cur suffixdp = [0 for _ in range(len(ops) + 1)] for i in range(len(ops)): highest, lowest, cur = prefixdp[i] if ops[i] == "+": cur += 1 else: cur -= 1 prefixdp[i + 1] = (max(highest, cur), min(lowest, cur), cur) #print(prefixdp) # suffix, calculating max displacement by looking at cur value of prefix # assumption is that we are starting r-side from square i (looping from back) # we do this by looking at the cur of the prefixdp and calculating # a relative minimum and maximum cur = prefixdp[-1][ -1 ] # the final cur of the prefixdp, we base relative calcs off this lowest = cur highest = cur suffixdp[-1] = (cur, cur) for i in reversed(range(len(suffixdp))): cur2 = prefixdp[i][-1] lowest = min(lowest, cur2) highest = max(highest, cur2) suffixdp[i] = (cur2 - lowest, highest - cur2) for l, r in queries: # all instruction between l and r inclusive are ignored highest, lowest, cur = prefixdp[l - 1] #print(l, prefixdp[l - 1], r, suffixdp[r]) drop, rise = suffixdp[r] lowest = min(lowest, cur - drop) highest = max(highest, cur + rise) # distinct vals: # 0 to highest plus abs lowest if lowest < 0 total = highest + 1 # to include 0 if lowest < 0: total += abs(lowest) print(total) def readinput(): tc = getInt() for _ in range(tc): n, m = getInts() ops = getString() queries = [] for _ in range(m): l, r = getInts() queries.append((l, r)) solve(ops, queries) readinput()
1,700
PYTHON3
import sys,math,itertools from collections import Counter,deque,defaultdict from bisect import bisect_left,bisect_right from heapq import heappop,heappush,heapify from copy import deepcopy 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() # for _ in range(inp()): n,k = inpl() a = inpl() se = set() q = deque([]) ln = 0 for x in a: if x in se: continue if ln < k: q.appendleft(x) se.add(x) ln += 1 else: p = q.pop() se.discard(p) q.appendleft(x) se.add(x) print(len(q)) print(*list(q))
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > v; bool seen[1005][1005]; int main() { int n; scanf("%d", &n); memset(seen, 0, sizeof(seen)); for (int(i) = 0; (i) < (n); (i)++) { int x, y; scanf("%d", &x); scanf("%d", &y); v.push_back(make_pair(x, y)); seen[x][y] = true; } sort((v).begin(), (v).end()); long long ans = 0; for (int(i) = 0; (i) < ((int)(v.size())); (i)++) { int a = v[i].first; int b = v[i].second; int cnt1 = 0, cnt2 = 0; for (int(j) = (1); (j) <= (1000); (j)++) { if (a + j <= 1000 and b + j <= 1000) { if (seen[a + j][b + j]) ++cnt1; } if (a - j >= 1 and b + j <= 1000) { if (seen[a - j][b + j]) ++cnt2; } } ans += cnt1 + cnt2; } cout << ans; return 0; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 200005; long long dat[MAXN]; int lc[MAXN * 4], rc[MAXN * 4], top = 0, root = 0; long long min_val[MAXN * 4], tag[MAXN * 4]; void init() { top = 0, memset(min_val, 0, sizeof min_val), memset(tag, 0, sizeof tag); } void pdw(int nd) { if (lc[nd]) tag[lc[nd]] += tag[nd], tag[rc[nd]] += tag[nd]; min_val[nd] += tag[nd], tag[nd] = 0; } void update(int nd) { min_val[nd] = min(min_val[lc[nd]], min_val[rc[nd]]); } void build(int &nd, int opl, int opr) { nd = ++top; if (opl == opr) min_val[nd] = dat[opl]; else { int mid = (opl + opr) >> 1; build(lc[nd], opl, mid), build(rc[nd], mid + 1, opr); update(nd); } } void modify(int nd, int opl, int opr, int dt, int L, int R) { if (opl > opr) return; pdw(nd); if (opl == L && opr == R) tag[nd] += dt; else { int mid = (L + R) >> 1; if (opr <= mid) modify(lc[nd], opl, opr, dt, L, mid); else if (opl > mid) modify(rc[nd], opl, opr, dt, mid + 1, R); else modify(lc[nd], opl, mid, dt, L, mid), modify(rc[nd], mid + 1, opr, dt, mid + 1, R); pdw(lc[nd]), pdw(rc[nd]), update(nd); } } inline long long query() { return pdw(root), min_val[root]; } int A[MAXN], B[MAXN]; vector<pair<int, int> > vec[MAXN]; int n, m, q; long long cutall = 0; int main() { scanf("%d%d%d", &n, &m, &q); init(); for (int i = 1; i < n; i++) scanf("%d%d", &A[i], &B[i]), dat[i] = B[i]; build(root, 1, n); for (int i = 1; i <= m; i++) { int u, v, d; scanf("%d%d%d", &u, &v, &d); cutall += d; vec[u].push_back(make_pair(v, d)); } for (int i = 1; i <= n; i++) { for (register int j = 0; j < vec[i].size(); j++) { modify(root, 1, vec[i][j].first - 1, vec[i][j].second, 1, n); modify(root, n, n, vec[i][j].second, 1, n); } dat[i] = query() + A[i]; } init(); build(root, 1, n); printf("%lld\n", query()); for (int i = 1; i <= q; i++) { int u, v; scanf("%d%d", &u, &v); modify(root, u, u, v - A[u], 1, n), A[u] = v; printf("%lld\n", query()); } return 0; }
2,700
CPP
#include <bits/stdc++.h> using namespace std; const int iinf = 1e9 + 7; const long long linf = 1ll << 60; const double dinf = 1e10; template <typename T> inline void scf(T &x) { bool f = 0; x = 0; char c = getchar(); while ((c < '0' || c > '9') && c != '-') c = getchar(); if (c == '-') { f = 1; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } if (f) x = -x; return; } template <typename T1, typename T2> void scf(T1 &x, T2 &y) { scf(x); return scf(y); } template <typename T1, typename T2, typename T3> void scf(T1 &x, T2 &y, T3 &z) { scf(x); scf(y); return scf(z); } const int N = 2e4 + 100; const int K = 1e3 + 10; const long long _p = 10000019ll; const long long _q = 1000000007ll; int n, k, q, tim; int dp[K]; int tin[N], tout[N]; int v[N], w[N]; long long _pow[N]; bool used[N]; inline int MAX(const int &x, const int &y) { return x < y ? y : x; } inline void solve(int l = 1, int r = tim) { int pd[K], q[N]; int rar = 0; memcpy(pd, dp, sizeof(dp)); for (int i = (1); i <= (n); i++) if (tin[i] < l && tout[i] >= r && !used[i]) { used[i] = 1; q[rar++] = i; for (int j = (k); j >= (w[i]); j--) dp[j] = MAX(dp[j], dp[j - w[i]] + v[i]); } if (l == r) { long long cur = 0; for (int i = (1); i <= (k); i++) cur = (cur + _pow[i - 1] * (long long)dp[i]) % _q; printf("%I64d\n", cur); } else { int mid = l + r >> 1; solve(l, mid); solve(mid + 1, r); } for (int i = 0; i < (rar); i++) used[q[i]] = 0; memcpy(dp, pd, sizeof(pd)); return; } int main() { _pow[0] = 1ll; scf(n, k); for (int i = (1); i <= (k); i++) _pow[i] = _pow[i - 1] * _p % _q; for (int i = (1); i <= (n); i++) scf(v[i], w[i]), tout[i] = -1; scf(q); for (int i = (1); i <= (q); i++) { int op; scf(op); if (op == 1) { n++; scf(v[n], w[n]); tin[n] = tim; tout[n] = -1; } else if (op == 2) { int x; scf(x); tout[x] = tim; } else tim++; } for (int i = (1); i <= (n); i++) if (tout[i] < 0) tout[i] = tim; solve(); return 0; }
2,800
CPP
s=input() n=0 x=len(s)-1 count=0 if x!=0 and s[0]!='0': for i in range(0,len(s)): if s[i]=='1': n+=2**x x=x-1 x=1 while x<n: x=4*x count=count+1 print(count) else: print(0)
1,000
PYTHON3
#!python from collections import defaultdict iter_cnt = 1 def iter_pattern(lst): global iter_cnt per = len(lst) while True: for c in lst: yield c iter_cnt += 1 t = int(input()) for _ in range(t): n,k = map(int,input().split()) a = list(map(int,input().split())) c = defaultdict(int) for i in a: c[i] += 1 num_values = len(c) if num_values > k: print(-1) else: # times = max(c.values())+1 r = [1]*k r[0:num_values] = list(c.keys()) times = 0 iter_cnt = 0 it = iter_pattern(r) p = next(it) for i in a: while (i != p): p = next(it) p = next(it) times = iter_cnt + 1 print(str(k*times)) print(" ".join(map(str,r*times)))
1,400
PYTHON3
tests = int (input()) for test in range (tests): n, x, m = map (int, input().split()) l, r = x, x for i in range (m): ln, rn = map (int, input().split()) if ln <= r and rn >= l: l = min (l, ln) r = max (r, rn) print (r - l + 1)
1,300
PYTHON3
n=int(input()) s=input() c=0 for i in s: if c>0 and i=='-': c-=1 elif i=='+': c+=1 print(c)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; char c; int n, i, j, k, mb, rb, bn, t = 0, ato = 0, atb, nm; struct sd { int bl, at, u, num; } a[1010] = {0}; struct ssd { int ti, num; } ans[1010] = {0}; bool cmp(sd x, sd y) { return x.at > y.at; } int rd() { nm = 0; c = getchar(); while (c < '0' || '9' < c) c = getchar(); while ('0' <= c && c <= '9') nm = nm * 10 + c - '0', c = getchar(); return nm; } int main() { n = rd(); bn = mb = rd(); rb = rd(); for (i = 1; i <= n; ++i) { a[i].bl = rd(); a[i].at = rd(); a[i].num = i; } sort(a + 1, a + n + 1, cmp); for (i = 0;; ++i) { bn = min(mb, bn - ato + rb); if (bn <= 0) { printf("YES\n%d %d", i, t); for (j = 1; j <= t; ++j) printf("\n%d %d", ans[j].ti, ans[j].num); return 0; } atb = ato; for (j = 1; j <= n; ++j) if (a[j].u == 0 && a[j].bl * mb >= bn * 100) { a[j].u = 1; ato += a[j].at; ans[++t].ti = i; ans[t].num = a[j].num; break; } if (bn == mb && ato == atb) { printf("NO\n"); return 0; } } return 0; }
1,800
CPP
#include <bits/stdc++.h> using namespace std; inline int in() { int x; scanf("%d", &x); return x; } const double PI = 3.14159265; const int N = 200002; long long x[N], y[N], sz[N], center, n, m; map<pair<int, int>, int> w; vector<int> g[N], psum[N]; map<int, int> ind[N]; bool mark[N]; double getKhar(int a, int b) { int dx = x[b] - x[a]; int dy = y[b] - y[a]; double th = atan2(dy, dx); return PI - th; } bool cmp(int i, int j) { return getKhar(center, i) < getKhar(center, j); } int dfs(int v) { mark[v] = sz[v] = 1; for (int u : g[v]) if (!mark[u]) { sz[v] += dfs(u); w[{v, u}] = -sz[u]; w[{u, v}] = sz[u]; } return sz[v]; } double area(vector<int> &v) { double ans = 0; for (int i = 0; i < v.size(); i++) { int a = v[i]; int b = v[(i + 1) % v.size()]; ans += (x[a] + x[b]) * (y[b] - y[a]); } return ans; } int calc(int a, int b, int c) { a = ind[b][a]; c = ind[b][c]; if (a < c) return psum[b][c - 1] - psum[b][a]; return psum[b].back() - psum[b][a] + (c ? psum[b][c - 1] : 0); } int main() { cin >> n >> m; while (m--) { int u = in(), v = in(); g[v].push_back(u); g[u].push_back(v); } int best = 1; for (int i = 1; i <= n; i++) { if ((x[i] = in()) < x[best]) best = i; y[i] = in(); } x[0] = x[best] - 1; y[0] = y[best]; g[0].push_back(best); g[best].push_back(0); dfs(0); for (int i = 1; i <= n; i++) { center = i; sort(g[i].begin(), g[i].end(), cmp); for (int j = 0; j < g[i].size(); j++) { ind[i][g[i][j]] = j; psum[i].push_back(psum[i].size() ? psum[i].back() : 0); psum[i].back() += w[{i, g[i][j]}]; } } for (int q = in(); q; q--) { vector<int> cs; for (int t = in(); t; t--) cs.push_back(in()); int cnt = 0; if (area(cs) > 0) reverse(cs.begin(), cs.end()); for (int i = 0; i < cs.size(); i++) { int x = calc(cs[i], cs[(i + 1) % cs.size()], cs[(i + 2) % cs.size()]); cnt += x; } assert(cnt >= cs.size()); cout << cnt << "\n"; } }
3,000
CPP
#include <bits/stdc++.h> using namespace std; const long double PI = acos(-1.0); long double fRand(long double fMin, long double fMax) { long double f = (long double)rand() / RAND_MAX; return fMin + f * (fMax - fMin); } template <class T> T min(T a, T b, T c) { return min(a, min(b, c)); } template <class T> T max(T a, T b, T c) { return max(a, max(b, c)); } struct pt { long double x, y, z; pt operator+(pt p) { return {x + p.x, y + p.y, z + p.z}; } pt operator-(pt p) { return {x - p.x, y - p.y, z - p.z}; } pt operator*(long double k) { return {x * k, y * k, z * k}; } pt operator/(long double k) { return {x / k, y / k, z / k}; } friend istream& operator>>(istream& is, pt& p) { is >> p.x >> p.y >> p.z; return is; } friend ostream& operator<<(ostream& os, const pt& p) { os << p.x << " " << p.y << " " << p.z; return os; } }; long double mag(pt v) { return sqrt(v.x * v.x + v.y * v.y + v.z * v.z); } const int MAXN = 1e4 + 5; const long double oo = 1e9 + 7; const long double EPS = 1e-9; int n; pt s[MAXN], p, ans; long double vp, vs, len[MAXN], sumT; bool Check(long double x) { long double curX = x; pt posS = s[n]; for (int i = (0); i <= (n - 1); ++i) { long double t = len[i] / vs; if (t < curX) curX -= t; else { posS = s[i] + (s[i + 1] - s[i]) * curX / t; break; } } if (mag(posS - p) < x * vp + EPS) { ans = posS; return true; } return false; } int main() { cin >> n; for (int i = (0); i <= (n); ++i) cin >> s[i]; cin >> vp >> vs; cin >> p; for (int i = (0); i <= (n - 1); ++i) { len[i] = mag(s[i] - s[i + 1]); sumT += len[i] / vs; } long double lo = 0, hi = sumT, res = oo; for (int iter = (0); iter <= (100); ++iter) { long double mid = (lo + hi) / 2; if (Check(mid)) res = hi = mid; else lo = mid; } if (res == oo) cout << "NO" << endl; else { cout << "YES" << endl; cout << setprecision(69) << res << endl; cout << ans << endl; } return 0; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n{}; cin >> n; double sum{}; for (float i = 1; i <= n; ++i) { sum += (1 / i); } cout << sum; }
1,000
CPP
n, m = map(int, input().split()) if(m == 0): print("1") elif(m <= n/2): print(m) elif(m > n/2): print(n-m)
900
PYTHON3
year=input() x=int(year) while 1: x+=1 a=int(x/1000) b=int(((x%1000)-(x%100))/100) c=int(((x%100)-(x%10))/10) d=int(x%10) if a != b and a!=c and a!=d and b!=c and b!=d and c!=d: print(x) break
800
PYTHON3
s = input() num = [int(n) for n in s.split()] n = num[0] m = num[1] a = num[2] b = (n+a-1)//a c = (m+a-1)//a print(b*c)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); long long ans = 0; vector<pair<int, int>> items(n); for (int i = 0; i < n; i++) { int x; char c[3]; scanf("%d%s", &x, &c); items[i] = make_pair(x, (c[0] == 'P' ? 0 : (c[0] == 'R' ? 1 : -1))); } int prev = -1; bool isP = false; bool isM = false; for (int i = 0; i < n; i++) { if (items[i].second != 0) { isP = isP || (items[i].second == 1); isM = isM || (items[i].second == -1); } else { int firstP = -1; int firstM = -1; int lastP = -1; int lastM = -1; if (isP) { for (int j = prev + 1; j < i; j++) { if (items[j].second == 1) { firstP = j; break; } } for (int j = i - 1; j > prev; j--) { if (items[j].second == 1) { lastP = j; break; } } } if (isM) { for (int j = prev + 1; j < i; j++) { if (items[j].second == -1) { firstM = j; break; } } for (int j = i - 1; j > prev; j--) { if (items[j].second == -1) { lastM = j; break; } } } if (isP && isM) { if (prev == -1) { ans += items[i].first - items[firstP].first; ans += items[i].first - items[firstM].first; } else { long long d1 = 3LL * (items[i].first - items[prev].first); int p1 = prev; int p2 = prev; long long d2 = 0; long long d3 = 0; for (int j = prev + 1; j <= i; j++) { if (items[j].second >= 0) { d2 = max(d2, (long long)(items[j].first - items[p1].first)); p1 = j; } if (items[j].second <= 0) { d3 = max(d3, (long long)(items[j].first - items[p2].first)); p2 = j; } } ans += min(2LL * (items[i].first - items[prev].first), (d1 - d2 - d3)); } } else if (isP && !isM) { if (prev == -1) { ans += items[i].first - items[firstP].first; } else { ans += 2LL * (items[i].first - items[prev].first); long long d = 0; for (int j = prev + 1; j <= i; j++) d = max(d, (long long)(items[j].first - items[j - 1].first)); ans -= d; } } else if (!isP && isM) { if (prev == -1) { ans += items[i].first - items[firstM].first; } else { ans += 2LL * (items[i].first - items[prev].first); long long d = 0; for (int j = prev + 1; j <= i; j++) d = max(d, (long long)(items[j].first - items[j - 1].first)); ans -= d; } } else { if (prev != -1) { ans += items[i].first - items[prev].first; } } prev = i; isP = isM = false; } } if (prev == -1) { int firstP = -1; int firstM = -1; int lastP = -1; int lastM = -1; for (int i = 0; i < n; i++) { if (items[i].second == 1) { firstP = i; break; } } for (int i = 0; i < n; i++) { if (items[i].second == -1) { firstM = i; break; } } for (int i = n - 1; i >= 0; i--) { if (items[i].second == 1) { lastP = i; break; } } for (int i = n - 1; i >= 0; i--) { if (items[i].second == -1) { lastM = i; break; } } if (firstP != -1) ans += (items[lastP].first - items[firstP].first); if (firstM != -1) ans += (items[lastM].first - items[firstM].first); } else if (prev != n - 1) { int lastP = -1; int lastM = -1; for (int i = n - 1; i > prev; i--) { if (items[i].second == 1) { lastP = i; break; } } for (int i = n - 1; i > prev; i--) { if (items[i].second == -1) { lastM = i; break; } } if (lastP != -1) ans += (items[lastP].first - items[prev].first); if (lastM != -1) ans += (items[lastM].first - items[prev].first); } printf("%I64d\n", ans); return 0; }
2,200
CPP
def solution(name): names = ["Danil", "Olya", "Slava", "Ann", "Nikita"] count = 0 for n in names: count += name.count(n) return count == 1 name = input() print('YES' if solution(name) else 'NO')
1,100
PYTHON3
test_case = int(input()) while test_case>0: s = input() if len(s)<=2: print(s) test_case-=1 continue res = s[0] for i in range(1,len(s)-1,2): res+=s[i] res+=s[-1] print(res) test_case-=1
800
PYTHON3
import math for _ in range(int(input())): n,k=map(int,input().split()) if n%2==0 and k%2==0 and k<=math.sqrt(n): print("YES") elif n%2!=0 and k%2!=0 and k<=math.sqrt(n): print("YES") else: print("NO")
1,100
PYTHON3
import sys input = sys.stdin.readline def prog(): for _ in range(int(input())): n = int(input()) print(n) a = [i for i in range(1,n+1)] print(*a) prog()
800
PYTHON3
for _ in range(int(input())): n, jumps = int(input()), 0 while jumps * (jumps + 1) / 2 < n: jumps += 1 if jumps * (jumps + 1) / 2 == n + 1: jumps += 1 print(jumps)
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[5005]; int main() { int i, j, k, t, r1, r2, g1, g2, v, n; cin >> t; while (t--) { r1 = r2 = g1 = g2 = -1; cin >> n; for (i = 1; i <= n; i++) { cin >> v; if (r1 == -1) { g1 = v; r1 = i; } else { if (r2 == -1 && v != g1) { r2 = i; g2 = v; } } a[i] = v; } if (r1 != -1 && r2 != -1) { cout << "YES" << endl; cout << r1 << " " << r2 << endl; for (i = 1; i <= n; i++) { if (i != r1 && i != r2) { if (a[i] != g1) { cout << r1 << " " << i << endl; } else { cout << r2 << " " << i << endl; } } } } else { cout << "NO" << endl; } } return 0; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; int main() { int a[4]; int sum = 0; for (int i = (0); i < (4); i++) cin >> a[i], sum += a[i]; sort(a, a + 4); if (sum % 2 == 1) { cout << "NO"; return 0; } if (a[3] + a[0] == sum / 2 || a[3] == sum / 2 || a[2] + a[1] + a[0] == sum / 2 || a[2] + a[1] == sum / 2 || a[2] + a[0] == sum / 2) { cout << "YES"; return 0; } cout << "NO"; }
800
CPP
#include <bits/stdc++.h> using namespace std; int T, n, i, j, k, a[200005], E[200005][2], fa[200005]; int father(int x) { return (fa[x] == x) ? x : fa[x] = father(fa[x]); } void link(int x, int y) { x = father(x), y = father(y); if (x != y) fa[x] = y; } int em, e[200005 * 2], nx[200005 * 2], ls[200005]; void insert(int x, int y) { if (father(x) != father(y)) { x = fa[x], y = fa[y]; em++; e[em] = y; nx[em] = ls[x]; ls[x] = em; em++; e[em] = x; nx[em] = ls[y]; ls[y] = em; } } int rt, rt0, mx; void dfs(int x, int p, int s, int pre) { if (a[x] != 0) { if (pre != a[x]) s++; pre = a[x]; } if (s > mx) mx = s, rt0 = x; for (int i = ls[x]; i; i = nx[i]) if (e[i] != p) dfs(e[i], x, s, pre); } int main() { scanf("%d", &T); while (T--) { scanf("%d", &n); em = 0, memset(ls, 0, sizeof(int) * (n + 1)); for (i = 1; i <= n; i++) scanf("%d", &a[i]), fa[i] = i; for (i = 1; i < n; i++) { scanf("%d%d", &E[i][0], &E[i][1]); if (a[E[i][0]] == a[E[i][1]]) link(E[i][0], E[i][1]); } for (i = 1; i < n; i++) insert(E[i][0], E[i][1]); for (i = 1; i <= n; i++) if (fa[i] == i) rt = i; rt0 = mx = -1; dfs(rt, 0, 0, 0); mx = 0; dfs(rt0, 0, 0, 0); if (mx <= 1) printf("1\n"); else printf("%d\n", mx / 2 + 1); } }
3,000
CPP
#include <bits/stdc++.h> using namespace std; void rf() {} const int nx[8] = {2, -2, -2, 2, 1, 1, -1, -1}; const int ny[8] = {1, 1, -1, -1, 2, -2, -2, 2}; const int Nx[4] = {0, 0, -1, 1}; const int Ny[4] = {1, -1, 0, 0}; const long long LINF = (long long)5e18; const int INF = 1e9 + 7; const int N = 2e5 + 1; const int MAXN = 1e6 + 50; const double EPS = 1e-9, PI = 3.14159265359; inline int get_int() { char x = getchar(); int ret = 0; bool neg = 0; while (!('0' <= x && x <= '9')) { if (x == '-') neg = 1; x = getchar(); } while ('0' <= x && x <= '9') { ret = ret * 10 + x - '0'; x = getchar(); } if (neg) ret *= -1; return ret; } int n, m; vector<int> queries[N], divisors[N]; int a[N], id[N], l[N], r[N]; long long t[N], ans[N]; void update(int x, int y) { if (!x) return; for (int i = x - 1; i < n; i = (i | (i + 1))) t[i] += y; } long long get(int x) { long long ans = 0; for (int i = x - 1; i >= 0; i = (i & (i + 1)) - 1) ans += t[i]; return ans; } int main() { srand(time(0)); rf(); scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j += i) divisors[j].push_back(i); } for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (int i = 1; i <= m; i++) { scanf("%d%d", &l[i], &r[i]); queries[r[i]].push_back(i); } for (int i = 1; i <= n; i++) { id[a[i]] = i; for (auto d : divisors[a[i]]) update(id[d], 1); for (auto q : queries[i]) ans[q] = get(i) - get(l[q] - 1); } memset(id, 0, sizeof(id)); memset(t, 0, sizeof(t)); for (int i = 1; i <= n; i++) queries[i].clear(); for (int i = 1; i <= m; i++) queries[l[i]].push_back(i); for (int i = n; i >= 1; i--) { id[a[i]] = i; for (auto d : divisors[a[i]]) if (d != a[i]) update(id[d], 1); for (auto q : queries[i]) ans[q] += get(r[q]) - get(i - 1); } for (int i = 1; i <= m; i++) printf("%lld\n", ans[i]); return 0; }
2,200
CPP
for nt in range(int(input())): n=int(input()) a=list(map(int,input().split())) b=list(map(int,input().split())) if a==b: print ("YES") continue z=-1 o=-1 m=-1 for i in range(n): if a[i]==0 and z==-1: z=i elif a[i]==1 and o==-1: o=i elif a[i]==-1 and m==-1: m=i ans="YES" for i in range(n-1,-1,-1): # print (z,o,m,i) x=b[i]-a[i] if x==0: continue if x<0: if m==-1 or m>=i: ans="NO" break if x>0: if o==-1 or o>=i: ans="NO" break print (ans)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-8; const int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1}; const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1}; const int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int leap[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; char dp[24][(1 << 23) + 10]; int a[33], n; int main() { int i, j, k, l; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &a[i]); } for (i = 0; i < 24; i++) { for (j = 0; j < (1 << 23) + 10; j++) { dp[i][j] = 100; } } dp[0][1 << 0] = 1; int bt[33], f; for (i = 0; i < n - 1; i++) { for (j = 0; j < (1 << (i + 1)); j++) if (dp[i][j] < 100) { f = 0; for (k = 0; k < n; k++) if (j & (1 << k)) bt[f++] = k; bool bi = true; for (k = 0; bi && k < f; k++) for (l = k; bi && l < f; l++) if (a[bt[k]] + a[bt[l]] == a[i + 1]) bi = false; if (!bi) { dp[i + 1][j | (1 << (i + 1))] = min(dp[i + 1][j | (1 << (i + 1))], (char)(dp[i][j] + 1)); for (int l = 0; l < f; ++l) dp[i + 1][(j | (1 << (i + 1))) ^ (1 << bt[l])] = min(dp[i + 1][(j | (1 << (i + 1))) ^ (1 << bt[l])], dp[i][j]); } } } int ans = 100; for (i = 0; i < (1 << 23) + 10; i++) { ans = min(ans, (int)dp[n - 1][i]); } if (ans > n) ans = -1; printf("%d\n", ans); return 0; }
2,200
CPP
n = int(input()) res = [] while n > 0: res.append(n%2) n//=2 ans=[] for i in range(len(res)): if(res[i])>0: ans.append(i+1) ans.reverse() print(' '.join(map(str, ans)))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; a[i] = abs(a[i]); } sort(a.begin(), a.end()); long long res = 0; for (int i = 0; i < n; i++) { long long x = a[i]; int lb = i; int ub = n - 1; if (a[n - 1] <= 2 * x) { lb = n - 1; ub = n - 1; } else { while (ub > lb + 1) { int m = (lb + ub + 1) / 2; if (a[m] > 2 * x) { ub = m; } else { lb = m; } } } res += (lb - i); } cout << res << endl; return 0; }
1,500
CPP
import io import os from collections import Counter, defaultdict, deque def solve(N, K, M, C): testcases = [] lens = [] M.sort(reverse=True) for x in M: # Everything assigned so far counts towards constraint allowed = C[x - 1] if False: for i, tc in enumerate(testcases): if len(tc) < allowed: tc.append(x) lens[i] += 1 break else: testcases.append([x]) lens.append(1) assert all(x == len(y) for x, y in zip(lens, testcases)) else: # lens is decreasing # Add to first index where lens[i] < allowed lo = 0 hi = len(lens) - 1 if not lens or lens[hi] >= allowed: # nothing will work testcases.append([x]) lens.append(1) else: if lens[lo] >= allowed: assert lens[hi] < allowed while hi - lo > 1: mid = (lo + hi) // 2 if lens[mid] < allowed: hi = mid else: lo = mid assert lens[lo] >= allowed assert lens[hi] < allowed else: hi = lo testcases[hi].append(x) lens[hi] += 1 return ( str(len(testcases)) + "\n" + "\n".join(str(len(ans)) + " " + " ".join(map(str, ans)) for ans in testcases) ) if __name__ == "__main__": input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline N, K = [int(x) for x in input().split()] M = [int(x) for x in input().split()] C = [int(x) for x in input().split()] ans = solve(N, K, M, C) print(ans)
1,900
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; const long long mod = 1e9 + 7; long long a[200010]; int main() { long long n, k; cin >> n >> k; long long i; for (i = 1; i <= n; i++) scanf("%d", &a[i]); long long sum = 0; for (i = 1; i <= n; i++) { long long temp = min(i, n - i + 1); temp = min(temp, n - k + 1); temp = min(temp, k); sum += temp * a[i]; } double ans = double(sum) / (n - k + 1); printf("%.10lf", ans); }
1,300
CPP
for _ in range(int(input())): n=int(input()) a=list(map(int,input().split())) c=0 for i in range(1,n): if a[i]<a[i-1]: x=len(bin(a[i-1]-a[i]))-2 if x>c:c=x a[i]=a[i-1] print(c)
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; long long ar[1000010]; int main() { long long n; cin >> n; long long ans = 0; if (n % 2 == 1) puts("0"); else { n = n / 2; n = (n - 1) / 2; cout << n << endl; } return 0; }
1,000
CPP
import sys def minp(): return sys.stdin.readline().strip() def mint(): return int(minp()) def mints(): return map(int, minp().split()) def solve(): n, x = mints() s = minp() p = 0 all = dict() m = int(1e9) M = int(-1e9) for i in range(n): all[p] = all.get(p,0) + 1 m = min(m, p) M = max(M, p) if s[i] == '0': p += 1 else: p -= 1 #print(m, M, all) if p == 0: if x in all: print(-1) else: print(0) return ss = 1 if p < 0: p = -p m, M = -M, -m ss = -1 x = -x do = max((x - M) // p, 0) r = 0 while x - do * p >= m: y = ss*(x - p * do) #print(do) if y in all: r += all[y] do += 1 print(r) for i in range(mint()): solve()
1,700
PYTHON3
#include <bits/stdc++.h> using namespace std; long long p, k; long long ret; bool vis[1000005]; long long compsThisSize[1000005]; long long val[1000005]; long long sz; long long logExp(long long a, long long b) { if (b == 1) return a; long long m = logExp(a, b / 2); m = (m * m) % 1000000007; if (b % 2 == 0) return m; else return (m * a) % 1000000007; } int main() { scanf("%lld %lld", &p, &k); if (k == 0) { ret = logExp(p, p - 1); printf("%lld\n", ret); return 0; } for (long long i = 0; i <= p - 1; i++) { if (!vis[i]) { sz = 0; int it = i; while (!vis[it]) { vis[it] = true; sz++; it = (k * it) % p; } compsThisSize[sz]++; } } long long ret = 1; for (long long i = 1; i <= p; i++) { for (long long j = i; j <= p; j += i) { val[j] = (val[j] + (i * compsThisSize[i]) % 1000000007) % 1000000007; } if (compsThisSize[i] > 0) ret = (ret * logExp(val[i], compsThisSize[i])) % 1000000007; } printf("%lld\n", ret); return 0; }
1,800
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100005, inf = 0x3f3f3f3f; const double pw = 2.7; int n, Q, wk; int a[N], ans[N]; struct query { int k, id; } q[N]; bool cmpk(const query &x, const query &y) { return x.k < y.k; } struct RMQ { int sgn; int lg[N], f[17][N]; void pref(int SGN) { sgn = SGN; lg[0] = -1; for (int i = 1; i <= n; i++) lg[i] = lg[i >> 1] + 1, f[0][i] = sgn * a[i]; for (int i = 1; i <= lg[n]; i++) for (int j = 1; j <= n - (1 << i) + 1; j++) f[i][j] = min(f[i - 1][j], f[i - 1][j + (1 << i - 1)]); } int qm(int l, int r) { int k = lg[r - l + 1]; return sgn * min(f[k][l], f[k][r - (1 << k) + 1]); } } mi, mx; int qd(int l, int r) { return mx.qm(l, r) - mi.qm(l, r); } struct LCT { int fa[N], ch[N][2], siz[N]; int nroot(int u) { return ch[fa[u]][0] == u || ch[fa[u]][1] == u; } void pushup(int u) { siz[u] = siz[ch[u][0]] + siz[ch[u][1]] + 1; } void rot(int u) { int v = fa[u], t = fa[v], d = (u == ch[v][1]); if (nroot(v)) ch[t][v == ch[t][1]] = u; fa[u] = t; fa[ch[u][d ^ 1]] = v; ch[v][d] = ch[u][d ^ 1]; fa[v] = u; ch[u][d ^ 1] = v; pushup(v); pushup(u); } void splay(int u) { while (nroot(u)) { int v = fa[u]; if (nroot(v)) rot(u == ch[v][1] ^ v == ch[fa[v]][1] ? u : v); rot(u); } } int access(int u) { int v = 0; for (; u; v = u, u = fa[u]) splay(u), ch[u][1] = v, pushup(u); return v; } void makeroot(int u) { access(u); splay(u); } void Link(int u, int v) { makeroot(u); makeroot(v); fa[u] = v; } void Cut(int u, int v) { access(u); splay(v); fa[u] = ch[v][1] = 0; pushup(v); } } lt; int len; int nxt[N]; priority_queue<pair<int, int> > hp; int bis(int x, int y) { int l = nxt[x], r = n + 1; while (l < r) { int mid = (l + r) >> 1; if (qd(x, mid) > y) r = mid; else l = mid + 1; } return r; } int main() { scanf("%d%d%d", &n, &wk, &Q); for (int i = 1; i <= n; i++) scanf("%d", a + i); for (int i = 1; i <= Q; i++) scanf("%d", &q[i].k), q[i].k = wk - q[i].k, q[i].id = i; sort(q + 1, q + Q + 1, cmpk); mi.pref(1); mx.pref(-1); len = int(ceil(pow(n, 1.0 / pw))); for (int i = 1; i <= n; i++) nxt[i] = i, hp.push(make_pair(0, i)); for (int i = 1; i <= Q; i++) { int K = q[i].k; while (!hp.empty()) { pair<int, int> P = hp.top(); int d = -P.first, x = P.second; if (d <= K) { hp.pop(); if (nxt[x] > x) lt.Cut(x, nxt[x]); nxt[x] = bis(x, d); if (nxt[x] - x <= len) { if (nxt[x] <= n) hp.push(make_pair(-qd(x, nxt[x]), x)); lt.Link(x, nxt[x]); } } else break; } int ti = 0, u = 1; while (u <= n) { if (nxt[u] - u > len) ti++, u = nxt[u] = bis(u, K); else { lt.makeroot(u); ti += lt.siz[u] - 1; while (lt.ch[u][0]) u = lt.ch[u][0]; lt.splay(u); } } ans[q[i].id] = ti - 1; } for (int i = 1; i <= Q; i++) printf("%d\n", ans[i]); return 0; }
3,400
CPP
n=int(input()) a=[] a.append(1) a.append(2) q=1 while a[q]<n: a.append(a[q]+a[q-1]) q+=1 if a[q]==n: print(q) else: print(q-1)
1,600
PYTHON3
#include <bits/stdc++.h> using namespace std; int dp[22][22][1 << 8]; char mp[55][55]; int stx, sty; int val[55]; int allcnt; int dx[4] = {0, 0, 1, -1}; int dy[4] = {1, -1, 0, 0}; int n, m; pair<int, int> pos[22]; int q[20 * 20 * 256]; int calc(int x, int y, int m1) { return x * 20 * 256 + y * 256 + m1; } void dfs(int x, int y, int m1) { dp[x][y][m1] = 0; int front = 0, rear = 0; q[++front] = calc(x, y, m1); while (front > rear) { int now = q[++rear]; int nm1 = now % 256; int ny = now / 256 % 20; int nx = now / 256 / 20; for (int i = 0; i < 4; i++) { int nnx = nx + dx[i]; int nny = ny + dy[i]; if (nnx < 0 || nny < 0 || nnx >= n || nny >= m || (mp[nnx][nny] != '.' && mp[nnx][nny] != 'S')) continue; int tm1 = nm1; for (int j = 0; j < allcnt; j++) { if (i == 2 && pos[j].first == nx && pos[j].second > ny) tm1 ^= 1 << j; if (i == 3 && pos[j].first == nnx && pos[j].second > nny) tm1 ^= 1 << j; } if (dp[nnx][nny][tm1] == -1) { dp[nnx][nny][tm1] = dp[nx][ny][nm1] + 1; q[++front] = calc(nnx, nny, tm1); } } } } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%s", mp[i]); int cnt = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (isdigit(mp[i][j])) { cnt = max(cnt, mp[i][j] - '0'); pos[mp[i][j] - '0' - 1] = make_pair(i, j); mp[i][j]--; } allcnt = cnt; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (mp[i][j] == 'B') { pos[allcnt++] = make_pair(i, j); } else if (mp[i][j] == 'S') { stx = i, sty = j; } for (int i = 0; i < 10; i++) val[i] = -10000; for (int i = 0; i < cnt; i++) scanf("%d", val + i); memset(dp, -1, sizeof(dp)); ; dfs(stx, sty, 0); long long res = 0; for (int i = 0; i < (1 << cnt); i++) if (dp[stx][sty][i] != -1) { long long all = 0; for (int k = 0; k < cnt; k++) if ((i & (1 << k))) all += val[k]; res = max(res, all - dp[stx][sty][i]); } cout << res << endl; }
2,600
CPP
n = int(input()) r = 0 if n%2 == 0: print (n//2) else: print (int((-n-1)/2))
800
PYTHON3
a = list(map(int, input().split())) b = list(map(int, input().split())) cnt = 0 sum1 = 0 for i in b: sum1 += i n = a[0] while sum1/n < a[1]-0.5: n += 1 sum1 += a[1] # print(n , sum1) cnt += 1 print(cnt)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; void solve() { long long n; cin >> n; long long bal = 0, mn = 0; for (int i = 0; i < n; i++) { char c; cin >> c; if (c == '+') bal++; else bal--; mn = min(bal, mn); } cout << -1 * mn + bal; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long q = 1; while (q--) { solve(); } }
800
CPP
x,y,z=0,0,0 for _ in range(int(input())): tx,ty,tz=list(map(int,input().split())) x+=tx y+=ty z+=tz if x==y and y==z and x==0: print("YES") else: print("NO") print(end='')
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; long long int n, m, ar[1000005], sum, tut[1000005], mki, k; int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) { cin >> ar[i], tut[ar[i]]++; if (tut[ar[i]] > tut[mki]) mki = ar[i], k = i; } cout << n - tut[mki] << "\n"; ; for (int i = k + 1; i <= n; i++) { if (ar[i] == mki) continue; if (ar[i] > mki) cout << 2 << " "; else cout << 1 << " "; cout << i << " " << i - 1 << "\n"; } for (int i = k - 1; i >= 1; i--) { if (ar[i] == mki) continue; if (ar[i] > mki) cout << 2 << " "; else cout << 1 << " "; cout << i << " " << i + 1 << "\n"; } }
1,400
CPP
#include <bits/stdc++.h> using namespace std; int main() { const int N = int(2e5) + 9; int a, temp = 0, cont; scanf("%d", &a); int b[N]; int c[N]; bool d[N]; for (int i = 0; i < a; i++) { scanf("%d", b + i); } for (int i = 0; i < a; i++) { scanf("%d", c + i); } for (int i = 0; i < a; ++i) { if (d[c[i]]) { printf("0 "); continue; } cont = 0; while (true) { ++cont; d[b[temp]] = true; if (b[temp] == c[i]) break; ++temp; } ++temp; printf("%d ", cont); } }
1,000
CPP
#include <bits/stdc++.h> using namespace std; typedef struct { char s[26]; } ttt; char s[5003]; int a, b, c, l, ans; int main() { scanf("%s", s); l = strlen(s); for (int i = 0; i < l; i++) { a = b = c = 0; for (int j = i; j < l; j++) { if (s[j] == '(') { a++; } else if (s[j] == '?') { if (b + c < a) b++; else a++; } else if (s[j] == ')') { if (b + c < a) { c++; } else { if (b > 0) { b--; a++; c++; } else break; } } if (b + c == a && (j - i) % 2 == 1) ans++; } } printf("%d\n", ans); return 0; }
1,800
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2005; int a[N]; int n, k; int main() { cin >> n >> k; for (int i = 0; i < n; ++i) cin >> a[i]; sort(a, a + n); int pos = upper_bound(a, a + n, 5 - k) - a; cout << pos / 3 << endl; }
800
CPP
#include <bits/stdc++.h> using namespace std; int main() { float n, m; int t; cin >> t; while (t--) { cin >> n >> m; int s = n * m; if (s % 2 == 0) { s = s / 2; } else { s = (s + 1) / 2; } cout << s << endl; } }
800
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long n, q; cin >> n; long long arr[n], cm[n], cm_sorted[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } cm[0] = arr[0]; for (int i = 1; i < n; i++) { cm[i] = cm[i - 1] + arr[i]; } sort(arr, arr + n); cm_sorted[0] = arr[0]; for (int i = 1; i < n; i++) { cm_sorted[i] = cm_sorted[i - 1] + arr[i]; } cin >> q; long long ans[q]; for (int j = 0; j < q; j++) { long long l = 0, r = 0, type = 0; cin >> type >> l >> r; l--; r--; if (type == 1) { if (l == 0) { ans[j] = cm[r]; continue; } ans[j] = cm[r] - cm[l - 1]; } else { if (l == 0) { ans[j] = cm_sorted[r]; continue; } ans[j] = cm_sorted[r] - cm_sorted[l - 1]; } } for (int i = 0; i < q; i++) { cout << ans[i] << "\n"; } }
1,200
CPP
#include <bits/stdc++.h> using namespace std; const int N = 259, K = 'z' - 'a' + 1, base = 301, inf = 1e9 + 9; int n, m, cnt[N][N][K], kq = 0, x[3 * N]; void Inp() { cin >> m >> n; char ch; for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { cin >> ch; for (int k = 0; k < K; k++) { cnt[i][j][k] = cnt[i][j - 1][k]; } cnt[i][j][ch - 'a']++; } } } void Calc(int L, int R) { vector<int> p; p.push_back(-1); for (int i = 1; i <= m; ++i) { int sum = 0, Cnt = 0; for (int k = 0; k < K; ++k) { sum = (sum * base + (cnt[i][R][k] - cnt[i][L - 1][k])); if ((cnt[i][R][k] - cnt[i][L - 1][k]) % 2 == 1) { Cnt++; } } if (Cnt > 1) { p.push_back(inf); } else { p.push_back(sum); } p.push_back(-1); } int sz = p.size(); int l = 0, r = 0; for (int i = 0; i < sz; i++) { if (p[i] == inf) { continue; } if (i >= r) { x[i] = 0; } else { x[i] = min(x[l + r - i], r - i); } x[i]++; while (i - x[i] >= 0 && i + x[i] < sz && p[i - x[i]] == p[i + x[i]] && p[i - x[i]] != inf && p[i + x[i]] != inf) { x[i]++; } x[i]--; if (r < i + x[i]) { l = i - x[i]; r = i + x[i]; } kq = kq + (x[i] + 1) / 2; } } void Solve() { for (int i = 1; i <= n; ++i) { for (int j = i; j <= n; ++j) { Calc(i, j); } } cout << kq; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); Inp(); Solve(); }
2,400
CPP
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long N = 13 + 5; long long a[N]; long long ans = 1; signed main() { long long n; cin >> n; if (n == 0) return cout << 15, 0; else if (n == 1) return cout << 14, 0; else if (n == 2) return cout << 12, 0; else if (n == 3) return cout << 13, 0; else if (n == 4) return cout << 8, 0; else if (n == 5) return cout << 9, 0; else if (n == 6) return cout << 10, 0; else if (n == 7) return cout << 11, 0; else if (n == 8) return cout << 0, 0; else if (n == 9) return cout << 1, 0; else if (n == 10) return cout << 2, 0; else if (n == 11) return cout << 3, 0; if (n == 12) return cout << 4, 0; if (n == 13) return cout << 5, 0; if (n == 14) return cout << 6, 0; if (n == 15) return cout << 7, 0; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; ll g(ll a, ll b) { ll r = a % b; while (r) { a = b; b = r; r = a % b; } return b; } int main() { ll n, m, q; cin >> n >> m >> q; ll gc = g(n, m); ll lm = m / gc, ln = n / gc; for (int i = 0; i < q; i++) { ll sx, sy, ex, ey; ll im, in; cin >> sx >> sy >> ex >> ey; if (sx == 1) { in = (sy - 1) / ln; } else { in = (sy - 1) / lm; } if (ex == 1) { im = (ey - 1) / ln; } else { im = (ey - 1) / lm; } if (im == in) { cout << "YES\n"; } else { cout << "NO\n"; } } }
1,400
CPP
#include <bits/stdc++.h> using namespace std; int main() { int m, n, ans; cin >> m; cin >> n; ans = m * n / 2; cout << ans; return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int dx[8] = {1, -1, 0, 0, -1, -1, 1, 1}; int dy[8] = {0, 0, -1, 1, -1, 1, -1, 1}; vector<int> adj[300010], val(300010, 1), vis(300010, 0), dis1(300010, 0), dis2(300010, 0); int myrandom(int i) { return std::rand() % i; } map<vector<string>, int> mt; int solve(vector<string> v) { if (mt.find(v) != mt.end()) { return mt[v]; } if ((int)v.size() == 1) { return 1; } int n = (int)v.size(); int maxi = 0; if (v[n - 1][0] == v[n - 2][0] || v[n - 1][1] == v[n - 2][1]) { string temp = v.back(); string kemp = v[n - 2]; swap(v[n - 2], v[n - 1]); v.pop_back(); maxi = max(maxi, solve(v)); v.push_back(kemp); swap(v[n - 1], v[n - 2]); } if ((int)v.size() > 3) { if (v[n - 1][0] == v[n - 4][0] || v[n - 1][1] == v[n - 4][1]) { swap(v[n - 4], v[n - 1]); v.pop_back(); maxi = max(maxi, solve(v)); } } mt[v] = maxi; return maxi; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<string> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } int ans = solve(v); if (ans) cout << "YES" << '\n'; else cout << "NO" << '\n'; }
1,900
CPP
y, k, n = map(int, input().split()) print(*range((k - y) % k or k, n - y + 1, k) or (-1,))
1,200
PYTHON3
def ins(s): for i in range(1, len(s)): val = s[i] j = i - 1 while s[j] > val and j>=0: s[j+1] = s[j] j = j - 1 s[j+1] = val return(s) n=int(input()) b=[] for i in range(n): b.append(int(input())) b=ins(b) b1=[] b2=[] for i in range(n): if i<n/2 : b1.append(b[i]) else : b2.append(b[i]) if b1.count(b1[0])==len(b1) and b2.count(b2[0])==len(b2) and b.count(b[0])!=len(b): print('YES') print(b1[0],b2[0]) else : print('NO')
1,000
PYTHON3
s = input().split() l = int(s[0]) r = int(s[1]) k = int(s[2]) cnt = 0 xx = 1 ans = "" for i in range(100): if xx > r: break if (l <= xx and xx <= r): ans = ans + str(xx) + " " cnt += 1 xx *= k if (cnt == 0): print("-1") else: print(ans)
1,500
PYTHON3
class Request: def __init__(self, _n, _ai): self.n = _n self.ai = _ai requests = [] q = int(input()) for i in range(q): requests.append(Request(int(input()), input().split())) for i in requests: summ = 0 for j in i.ai: summ += int(j) if summ % i.n == 0: print(int(summ / i.n)) else: print(int(summ / i.n) + 1)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAXN = 100; int gcd(int, int); int main() { int n; scanf("%d", &n); register int i; if ((n & 1) == 0) { printf("-1"); return 0; } for (i = 0; i < n; ++i) printf("%d ", i); printf("\n"); for (i = 0; i < n; ++i) printf("%d ", i); printf("\n"); for (i = 0; i < n; ++i) printf("%d ", (i * 2) % n); return 0; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; int N, M, x[100]; double dp[100001][100]; int main() { scanf("%d%d", &N, &M); int ts = 0; for (int n = 0; n < int(N); n++) { scanf("%d", x + n); ts += x[n]; } if (M == 1) { puts("1"); return 0; } for (int i = 0; i < int(N); i++) for (int ms = 1; ms < int(ts); ms++) { double& ans = dp[ms][i]; if (i) { if (ms) ans += dp[ms - 1][i - 1]; if (ms - x[i] >= 0) ans -= dp[ms - x[i]][i - 1]; if (ms - x[i] - 1 >= 0) ans += dp[ms - x[i] - 1][i - 1]; if (ms - M - 1 >= 0) ans -= dp[ms - M - 1][i - 1]; ans /= M - 1; } else { if (ms != x[0] && ms <= M) ans++; } if (ms) ans += dp[ms - 1][i]; } printf("%.16f\n", 1.0 + dp[ts - 1][N - 1]); }
2,300
CPP
#include <bits/stdc++.h> int getint() { char ch = getchar(); int ret = 0; bool flag = true; while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') flag = false, ch = getchar(); while (ch >= '0' && ch <= '9') ret = ret * 10 + ch - '0', ch = getchar(); return flag ? ret : -ret; } using namespace std; double dist[2000]; int x[2000], rk[2000], y[2000], k[2000], num, tot, s, n; bool cmp(const int& a, const int& b) { return dist[a] < dist[b]; } int main() { cin >> n >> s; s = 1000000 - s; for (int i = 1; i <= n; i++) cin >> x[i] >> y[i] >> k[i], num += k[i]; for (int i = 1; i <= n; i++) dist[i] = sqrt(x[i] * x[i] + y[i] * y[i]); for (int i = 1; i <= n; i++) rk[i] = i; if (num < s) return puts("-1"), 0; sort(rk + 1, rk + n + 1, cmp); tot = 0; for (int i = 1; i <= n; i++) { int v = rk[i]; tot += k[v]; if (tot >= s) { printf("%.8lf", dist[v]); return 0; } } return 0; }
1,200
CPP
n = int(input()) a = [int(i) for i in input().split(' ')] ans = True while len(a): for i in range(len(a)-1): if abs(a[i+1]-a[i])>=2: ans = False break if not ans: break m = max(a) for i in range(len(a)): if a[i]==m: a.pop(i) break print("YES" if ans else "NO")
1,600
PYTHON3
n=int(input()) line=input() v=[int(h) for h in line.split()] v2=[] for i in range(len(v)-1, -1, -1): v2.append(v[i]) cont, cont2=0,0 k,j=0,0 sub,sub2=0,0 for i in range(len(v)): if sub2>=sub: cont+=1 sub+=v[k] k+=1 else: cont2+=1 sub2+=v2[j] j+=1 if cont+cont2>=n: break ans=str(cont) + ' ' + str(cont2) print(ans)
1,200
PYTHON3
from sys import stdin from math import ceil n = int(input()) alist = [] blist = [] for i in range(n): vals = stdin.readline() a, b = [int(x) for x in vals.split()] alist.append(a) blist.append(b) for i, j in zip(alist, blist): print((ceil(i / j) * j) - i)
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, b = 0, sum = 0; cin >> n; while (n >= sum) { b++; sum += (b * (b + 1) / 2); } cout << b - 1; return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1; void solve() { long long n, k; cin >> n >> k; vector<string> mp(n); for (auto& x : mp) cin >> x; vector<vector<int>> mtx(n, vector<int>(n)); for (int i = 0; i < n; ++i) for (int j = 0; j < n - k + 1; ++j) { bool fl = true; for (int s = 0; s < k && fl; ++s) fl &= mp[i][j + s] == '.'; for (int s = 0; s < k && fl; ++s) mtx[i][j + s]++; } for (int i = 0; i < n - k + 1; ++i) for (int j = 0; j < n; ++j) { bool fl = true; for (int s = 0; s < k && fl; ++s) fl &= mp[i + s][j] == '.'; for (int s = 0; s < k && fl; ++s) mtx[i + s][j]++; } int imx = 0, jmx = 0; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) if (mtx[i][j] > mtx[imx][jmx]) { imx = i; jmx = j; } cout << imx + 1 << ' ' << jmx + 1 << endl; } int main() { solve(); return 0; }
1,300
CPP
a=input() for i in range(len(a)): if(a[i]=="A" or a[i]=="E" or a[i]=="I" or a[i]=="O" or a[i]=="U" or a[i]=="Y" or a[i]=="a" or a[i]=="e" or a[i]=="i" or a[i]=="o" or a[i]=="u" or a[i]=="y"): continue elif(a[i].isupper()): print(".",end="") print(a[i].lower(),end="") else: print(".",end="") print(a[i],end="")
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5, M = 1e9 + 7; int par[N], sz[N], is[N], ed[N]; int get(int u) { return !(par[u] - u) ? u : par[u] = get(par[u]); } int main() { int n; scanf("%d", &n); for (int i = 0; i < N; i++) par[i] = i, sz[i] = 1; for (int i = 0; i < n; i++) { int a, b; scanf("%d%d", &a, &b); a--; b--; if (a == b) is[get(a)] = 1; else { if (get(a) != get(b)) { is[get(a)] |= is[get(b)]; ed[get(a)] += ed[get(b)]; sz[get(a)] += sz[get(b)]; } par[get(b)] = get(a); ed[get(a)]++; } } int ans = 1; for (int i = 0; i < 2 * n; i++) if (!(par[i] - i)) { if (is[i]) continue; if (ed[i] == sz[i]) ans <<= 1; else ans = (ans * 1LL * sz[i]) % M; if (ans >= M) ans -= M; } cout << ans << endl; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, i; cin >> n; vector<pair<int, int> > v(n); for (i = 0; i < n; i++) { int x, y; cin >> x >> y; v[i].first = max(x, y); v[i].second = min(x, y); } sort(v.begin(), v.end()); vector<int> ans; ans.push_back(v[0].first); int temp = v[0].first; for (i = 1; i < n; i++) { if (v[i].second > temp) { temp = v[i].first; ans.push_back(temp); } } cout << ans.size() << "\n"; for (i = 0; i < ans.size(); i++) cout << ans[i] << " "; }
1,900
CPP
def solve(): n = int(input()) camels = {} for i in range(n): x, d = [int(s) for s in input().split(' ')] camels[x] = d for c in camels: y = c + camels[c] if y in camels and camels[y] == -camels[c]: return "YES" return "NO" print(solve())
1,000
PYTHON3
def solve(): n = int(input()) a = [int(c) for c in input().split(" ")] total = sum(a) if n < 2: return None elif n == 2: if a[0] == a[1]: return None else: return (0,) else: for i in range(n): if a[i] != total - a[i]: return (i,) result = solve() if not result: print(-1) else: print(len(result)) for num in result: print(num+1, end = " ")
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int t; int n, i, mid, back; cin >> n; int arr[n]; for (i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); mid = n / 2; back = n - 1; for (i = 0; i < mid; i++) cout << arr[i] << " " << arr[back - i] << " "; if (n % 2 == 1) cout << arr[mid]; return 0; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; int n; long long BIT[1000010]; void update(int x, int val) { while (x <= n) { BIT[x] += val; x += (x & -x); } } long long sum(int x) { long long res = 0; while (x) { res += BIT[x]; x -= (x & -x); } return res; } int main() { scanf("%d", &n); int a[n]; for (int i = 0; i < n; i++) scanf("%d", &a[i]); int b[n]; for (int i = 0; i < n; i++) b[i] = a[i]; sort(b, b + n); for (int i = 0; i < n; i++) a[i] = 1 + (lower_bound(b, b + n, a[i]) - b); long long inv = 0; long long prev[n]; long long next[n]; for (int i = n - 1; i >= 0; i--) { update(a[i], 1); prev[i] = sum(a[i] - 1); } memset(BIT, 0, (n + 1) * sizeof(long long)); for (int i = 0; i < n; i++) { update(a[i], 1); next[i] = sum(n) - sum(a[i]); } for (int i = 1; i < n - 1; i++) inv += prev[i] * next[i]; cout << inv << endl; return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; void solve() { long long int n, m, q; cin >> n >> m >> q; vector<vector<bool>> grid(n + 2, vector<bool>(m + 2, true)); vector<vector<long long int>> one(n + 2, vector<long long int>(m + 2, 0)); vector<vector<long long int>> two(n + 2, vector<long long int>(m + 2, 0)); long long int total = 0; for (long long int i = 1; i <= n; i++) { for (long long int j = 1; j <= m; j++) { one[i][j] = two[i][j] = 1; one[i][j] += two[i][j - 1]; two[i][j] += one[i - 1][j]; total += one[i][j] + two[i][j] - 1; } } for (long long int i = 1; i <= q; i++) { long long int x, y; cin >> x >> y; if (grid[x][y]) { grid[x][y] = !grid[x][y]; total -= one[x][y]; total -= two[x][y]; total++; one[x][y] = 0; two[x][y] = 0; for (long long int step = 1; step <= max(n, m); step++) { if ((x + step - 1 <= n) and (y + step <= m) && grid[x + step - 1][y + step]) { long long int temp = two[x + step - 1][y + step - 1] + 1; total += (temp - one[x + step - 1][y + step]); one[x + step - 1][y + step] = temp; } if ((x + step <= n) and (y + step - 1 <= m) && grid[x + step][y + step - 1]) { long long int temp = one[x + step - 1][y + step - 1] + 1; total += (temp - two[x + step][y + step - 1]); two[x + step][y + step - 1] = temp; } if (x + step > n || y + step > m) break; if (!grid[x + step][y + step]) break; long long int temp = one[x + step - 1][y + step] + 1; total += (temp - two[x + step][y + step]); two[x + step][y + step] = temp; temp = two[x + step][y + step - 1] + 1; total += (temp - one[x + step][y + step]); one[x + step][y + step] = temp; } } else { grid[x][y] = !grid[x][y]; one[x][y] = 1 + two[x][y - 1]; two[x][y] = 1 + one[x - 1][y]; total += one[x][y] + two[x][y] - 1; for (long long int step = 1; step <= max(n, m); step++) { if ((x + step - 1 <= n) and (y + step <= m) && grid[x + step - 1][y + step]) { long long int temp = two[x + step - 1][y + step - 1] + 1; total += (temp - one[x + step - 1][y + step]); one[x + step - 1][y + step] = temp; } if ((x + step <= n) and (y + step - 1 <= m) && grid[x + step][y + step - 1]) { long long int temp = one[x + step - 1][y + step - 1] + 1; total += (temp - two[x + step][y + step - 1]); two[x + step][y + step - 1] = temp; } if (x + step > n || y + step > m) break; if (!grid[x + step][y + step]) break; long long int temp = one[x + step - 1][y + step] + 1; total += (temp - two[x + step][y + step]); two[x + step][y + step] = temp; temp = two[x + step][y + step - 1] + 1; total += (temp - one[x + step][y + step]); one[x + step][y + step] = temp; } } cout << total << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long tc = 1; while (tc--) { solve(); } return 0; }
2,100
CPP
for i in range(int(input())): l,r=map(int,input().split()) od=(r+1)//2 odd=(l)//2 minus=(od**2)-(odd**2) eve=r//2 even=(l-1)//2 plus=(eve*(eve+1)-even*(even+1)) print(plus-minus)
900
PYTHON3
n=int(input()) ar=list(map(int,input().split())) s=sum(ar) a=ar[0] need=s//2 +1 if(ar[0]>=need): print(1) print(1) else: ans=[] for i in range(1,n): if(2*ar[i]<=ar[0]): a+=ar[i] ans.append(i) if(a>=need): break if(len(ans)==0 or a<need): print(0) else: print(len(ans)+1) print(1,end=" ") for i in range(len(ans)): print(ans[i]+1,end=" ")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int SIZE = 1e6 + 10; bool inter(int x1, int y1, int x2, int y2) { return y1 >= x2 && y2 >= x1; } int main() { vector<pair<int, int> > pp; int(n); scanf("%d", &n); for (int i = 0; i < (n); ++i) { int x, y; scanf("%d%d", &x, &y); bool err = 0; for (int j = 0; j < (((int)(pp).size())); ++j) if (inter(x, x + y - 1, pp[j].first, pp[j].second)) { err = 1; break; } if (!err) { printf("%d %d\n", x, x + y - 1); pp.push_back(make_pair(x, x + y - 1)); } else { int lt = 0; bool suc = 0; for (int j = 0; j < (((int)(pp).size())); ++j) { if (pp[j].first - lt - 1 >= y) { suc = 1; pp.push_back(make_pair(lt + 1, lt + y)); printf("%d %d\n", lt + 1, lt + y); break; } lt = pp[j].second; } if (!suc) { printf("%d %d\n", pp.back().second + 1, pp.back().second + y); pp.push_back(make_pair(pp.back().second + 1, pp.back().second + y)); } } sort((pp).begin(), (pp).end()); } return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; using uint = unsigned int; using rl = double; const int MAXN = 5 * 100009; const int modd = 998244353; int n; struct Vertex { vector<int> to; int pm, answ, thisansw; } v[MAXN]; int ndiv2; int mult(int a, int b) { ll res = a * (ll)b; if (res >= modd) res %= modd; return res; } int add(int a, int b) { a += b; if (a >= modd) a -= modd; return a; } int neg(int a) { a = -a; if (a < 0) a += modd; return a; } int oneMinus(int a) { return add(1, neg(a)); } int poww(int val, int pow = modd - 2) { int res = 1; while (pow) { if (pow & 1) res = mult(res, val); val = mult(val, val); pow /= 2; } return res; } int dfs(int it, int p = 0) { v[it].pm = 1; v[it].answ = 0; v[it].thisansw = 0; for (auto to : v[it].to) { if (to == p) continue; v[it].answ = add(v[it].answ, dfs(to, it)); v[it].thisansw = mult(v[it].thisansw, add(oneMinus(v[to].pm), mult(ndiv2, v[to].pm))); v[it].thisansw = add(v[it].thisansw, mult(ndiv2, v[to].pm)); v[it].pm = mult(v[it].pm, add(oneMinus(v[to].pm), mult(ndiv2, v[to].pm))); } return add(v[it].answ, v[it].thisansw); } int main() { ndiv2 = poww(2); assert(mult(ndiv2, 2) == 1); scanf("%d", &n); for (int i = 1; i < n; i++) { int a, b; scanf("%d %d", &a, &b); v[a].to.push_back(b); v[b].to.push_back(a); } int answ = dfs(1); printf("%d\n", mult(poww(2, n), answ)); }
2,800
CPP
#include <bits/stdc++.h> using namespace std; void swap(long long *a, long long *b) { long long t = *a; *a = *b; *b = t; } long long _gcd(long long a, long long b) { if (a > b) swap(&a, &b); if (a == 0) return b; return _gcd(b % a, a); } long long power_(long long a, long long b) { if (!b) return 1; long long ans = power_(a, b / 2); ans = (ans * ans) % 998244353; if (b % 2) ans = (ans * a) % 998244353; return ans; } bool valid(int x, int y, int n, int m) { if (x >= 0 && y >= 0 && x < n && y < m) return true; return false; } int pos[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; void slv() { int n, c; cin >> n; set<int> a, b; for (int i = 0; i < n; i++) { int x; cin >> x; a.insert(x); } set<int>::iterator it; for (int i = 1; i <= 1024; i++) { b.clear(); for (it = a.begin(); it != a.end(); it++) { int x = (*it) ^ i; b.insert(x); } if (a == b) { cout << i << "\n"; return; } } cout << -1 << "\n"; return; } int main() { int t; cin >> t; while (t--) slv(); return 0; }
1,200
CPP