solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
for t in range(int(input())): n,l,r=map(int,input().split()) b=1 for i in range(1,n): a=b b+=2*(n-i) if l<b: break x,y=i,(l-a)//2+i+1 b=(l-a)%2 for _ in range(r-l): if b: print(y,end=" ") y+=1 if y==n+1: x+=1 y=x+1 else: print(x,end=" ") b^=1 if r==n*(n-1)+1: print(1) else: print(y if b else x)
1,800
PYTHON3
for t in range(int(input())): n=int(input()) eight=((n-1)//4)+1 print("9"*(n-eight)+"8"*eight)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; long long r, g, b; int main() { cin >> r >> g >> b; cout << min((r + g + b) / 3, min(r + g, min(g + b, r + b))); }
1,800
CPP
#include <bits/stdc++.h> using namespace std; int main() { int sum = 0; int max = 0; int n, s, temp; cin >> n >> s; for (int i = 0; i < n; i++) { cin >> temp; if (temp > max) max = temp; sum += temp; } sum -= max; if (sum <= s) cout << "YES"; else cout << "NO"; }
800
CPP
#include <bits/stdc++.h> using namespace std; char f[1500][1500]; pair<int, int> r[1500][1500]; pair<int, int> s; int h, w; int dx[4] = {-1, 0, 0, 1}; int dy[4] = {0, 1, -1, 0}; pair<pair<int, int>, pair<int, int> > check(int x, int y, pair<int, int> p) { if (x < 0) { x += h; p.first--; } if (x >= h) { x -= h; p.first++; } if (y < 0) { y += w; p.second--; } if (y >= w) { y -= w; p.second++; } if (f[x][y] == '#') return pair<pair<int, int>, pair<int, int> >( pair<int, int>(100000000, 100000000), p); return pair<pair<int, int>, pair<int, int> >(pair<int, int>(x, y), p); } int main() { int i, j; scanf("%d %d", &h, &w); for (i = 0; i < h; i++) { for (j = 0; j < w; j++) { scanf(" %c", &f[i][j]); if (f[i][j] == 'S') s = pair<int, int>(i, j); r[i][j] = pair<int, int>(100000000, 100000000); } } queue<pair<int, int> > que; r[s.first][s.second] = pair<int, int>(0, 0); que.push(s); bool loop = false; while (!que.empty()) { pair<int, int> p = que.front(); que.pop(); for (i = 0; i < 4; i++) { int x = p.first + dx[i]; int y = p.second + dy[i]; pair<pair<int, int>, pair<int, int> > next = check(x, y, r[p.first][p.second]); pair<int, int> p1 = next.first; pair<int, int> p2 = next.second; if (p1.first == 100000000) continue; if (r[p1.first][p1.second].first == 100000000) { r[p1.first][p1.second] = p2; que.push(p1); } else if (r[p1.first][p1.second] != p2) loop = true; } if (loop) break; } if (loop) printf("Yes\n"); else printf("No\n"); return 0; }
2,000
CPP
t=int(input()) for _ in range(0,t): l,r,d,u=map(int,input().split()) x,y,x1,y1,x2,y2=map(int,input().split()) le=abs(x-x1) ri=abs(x2-x) #print(le,end=" ") #print(ri) do=abs(y-y1) up=abs(y2-y) #print(do,end=" ") #p#rint(up) if(le==0 and ri==0 and (l!=0 or r!=0)): print('No') elif(do==0 and up==0 and (d!=0 or u!=0)): print('No') else: flag=0 temp1=abs(l-r) temp2=abs(u-d) if(temp1!=0): if(l>r): if(temp1>le): flag=1 else: if(temp1>ri): flag=1 if(temp2!=0): if(u>d): if(temp2>up): flag=1 else: if(temp2>do): flag=1 if(flag==1): print('No') else: print('Yes')
1,100
PYTHON3
def checker(word): if len(word)>10: new_word = word[0]+str(len(word)-2)+word[-1] return new_word else: return word if __name__ == "__main__": n = int(input()) for i in range(n): result = checker(input()) print(result)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; long long BM(long long b, long long p, long long mod) { long long ret = 1; while (p) { if (p & 1) ret = (ret * b) % mod; p /= 2; b = (b * b) % mod; } return ret; } int main() { int n, m, k; scanf("%d %d %d", &n, &m, &k); long long ans; if (k == 1 || k > n) ans = BM(m, n, mod); else if (k == n) ans = BM(m, (n + 1) / 2, mod); else if (k % 2 == 0) ans = m; else ans = m * m; printf("%lld\n", ans); }
1,600
CPP
weight = int(input()) if weight <= 3: print("NO") elif weight % 2 == 0: print("YES") else: print("NO")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, q; vector<int> blocks[5005]; map<pair<int, int>, int> pos[5005]; vector<pair<int, int> > ops; int a[5005], id[5005]; int ret[100005]; struct Partition { map<pair<int, int>, int> ranges; Partition() {} }; int cnt; Partition construct(int low, int high, vector<int>& order) { if (low > high) { return Partition(); } if (low == high) { Partition ps; ps.ranges[make_pair(0, 0)] = id[low]; return ps; } int mid = (low + high) >> 1; vector<int> la, ra; for (int x : order) { if (x <= mid) { la.push_back(x); } else { ra.push_back(x); } } auto ls = construct(low, mid, la); auto rs = construct(mid + 1, high, ra); Partition ps; int i = 0; int lowx = 0, lowy = 0, highx = 0, highy = 0; while (i < order.size()) { if (i > 0) { if (order[i - 1] <= mid) { lowx++; } else { highx++; } } lowy = lowx - 1; highy = highx - 1; for (int j = i; j < order.size(); j++) { if (order[j] <= mid) { lowy++; } else { highy++; } int lindex = ls.ranges[make_pair(lowx, lowy)]; int rindex = rs.ranges[make_pair(highx, highy)]; if (lowx <= lowy && highx <= highy) { assert(lindex > 0 && rindex > 0); ps.ranges[make_pair(i, j)] = ++cnt; ops.push_back(make_pair(lindex, rindex)); } else if (lowx <= lowy) { assert(lindex > 0); ps.ranges[make_pair(i, j)] = lindex; } else { if (rindex == 0) { } assert(rindex > 0); ps.ranges[make_pair(i, j)] = rindex; } } i++; } return ps; } int main() { scanf("%d %d", &n, &q); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); id[a[i]] = i; } int S = sqrt(q); cnt = n; int nblocks = 0; for (int low = 1; low <= n; low += S, nblocks++) { int high = min(n, low + S - 1); for (int i = 1; i <= n; i++) if (a[i] >= low && a[i] <= high) { blocks[nblocks].push_back(a[i]); } auto ps = construct(low, high, blocks[nblocks]); for (int i = 0; i < blocks[nblocks].size(); i++) { for (int j = i; j < blocks[nblocks].size(); j++) { pos[nblocks][make_pair(i, j)] = ps.ranges[make_pair(i, j)]; } } } for (int it = 0; it < q; it++) { int l, r; scanf("%d %d", &l, &r); ret[it] = -1; vector<pair<int, int> > ranges; ranges.resize(nblocks); for (int i = 0; i < nblocks; i++) { ranges[i] = make_pair(0, -1); } for (int i = 1; i <= r; i++) { int block = (a[i] - 1) / S; if (i < l) { ranges[block].first++; ranges[block].second++; } else { ranges[block].second++; } } for (int i = 0; i < nblocks; i++) if (ranges[i].first <= ranges[i].second) { int lindex = pos[i][make_pair(ranges[i].first, ranges[i].second)]; if (ret[it] < 0) { ret[it] = lindex; } else { assert(ret[it] > 0 && lindex > 0); ops.push_back(make_pair(ret[it], lindex)); ret[it] = ++cnt; } } } printf("%d\n", cnt); for (auto op : ops) { printf("%d %d\n", op.first, op.second); } for (int i = 0; i < q; i++) { printf("%d ", ret[i]); } printf("\n"); return 0; }
3,300
CPP
#include <bits/stdc++.h> using namespace std; long long power(long long x, long long y, long long md = 1000000007) { long long res = 1; x %= md; while (y > 0) { if (y & 1) res = (res * x) % md; x = (x * x) % md; y = y >> 1; } return res % md; } struct node { vector<long long> v, w; }; long long ask(string second) { cout << second << '\n'; cout << flush; long long ans; cin >> ans; if (ans == 0) exit(0); return ans; } signed main() { string second(300, 'a'); long long a = 300 - ask(second); second = string(300, 'b'); long long b = 300 - ask(second); long long n = a + b; second = string(n, 'a'); long long prev = b; if (a == n) { cout << second << '\n'; cout << flush; } for (long long i = 0; i < n; i++) { second[i] = 'b'; long long temp = ask(second); if (temp < prev) { prev = temp; b++; } else { a++; second[i] = 'a'; } } cout << second << '\n'; cout << flush; }
2,300
CPP
#include <bits/stdc++.h> using namespace std; int get(int n, int p) { int res = 0; while (n >= p) res += n / p, n /= p; return res; } template <class T> T power(T N, T P) { return (P == 0) ? 1 : N * power(N, P - 1); } struct hash_pair { template <class T1, class T2> size_t operator()(const pair<T1, T2>& p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); return hash1 ^ hash2; } }; template <typename T> void print(const T& v) { cerr << v << ' '; } template <typename T1, typename... T2> void print(const T1& first, const T2&... rest) { print(first); print(rest...); } const int Maxn = 1e5 + 1; template <typename T, typename T1> T mod(T x, T1 p) { x %= p; if (x < 0) x += p; return x; } int fact[Maxn]; void factorial() { fact[0] = 1; for (int i = 1; i < Maxn; i++) { fact[i] = 1LL * fact[i - 1] * i % 1000000007; } } int inv[Maxn], ifact[Maxn]; template <typename T> T inverse(T x, T p) { x = mod(x, p); if (x == 1) return x; return mod((1LL * (-p / x) * (inv[p % x] % p)), p); } void inverse_fact() { ifact[0] = 1; for (int i = 1; i < Maxn; i++) { inv[i] = inverse(i, 1000000007); ifact[i] = (1LL * ifact[i - 1] * inv[i]) % 1000000007; } } int nCr(int n, int r) { int ret = (1LL * ifact[n - r] * ifact[r]) % 1000000007; ret = (1LL * ret * fact[n]) % 1000000007; return ret; } bool cmp(int a, int b) { return a > b; } vector<pair<int, int> > v; int main() { int n, d, i, x; cin >> n >> d; for (int i = 0; i < n; i++) { scanf("%d", &(x)); v.push_back(make_pair(x, i + 1)); } sort(v.begin(), v.end()); vector<int> ans; int sum = 0; for (int i = 0; i < n; i++) { if (sum + v[i].first <= d) sum += v[i].first, ans.push_back(v[i].second); else break; } printf("%d\n", ans.size()); for (auto it : ans) { printf("%d ", it); } return 0; }
1,000
CPP
x=input() str=list(input().split()) for i in str: if x[0]==i[0] or x[1]==i[1]: print("YES") exit() print("NO")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main(void) { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector<int> A(n); for (long long int i = 0; i < n; i++) { cin >> A[i]; } multiset<pair<int, int>> dp; int prev = 20; dp.insert({A[0] + 89, 50}); dp.insert({A[0] + 1440 - 1, 120}); cout << 20 << endl; for (long long int i = 1; i < n; i++) { int ans = prev + 20; auto p = dp.lower_bound({A[i], -1}); for (; p != dp.end(); p++) { ans = min(ans, p->second); } dp.insert({A[i] + 89, prev + 50}); dp.insert({A[i] + 1440 - 1, prev + 120}); cout << ans - prev << endl; prev = ans; } return (0); }
1,600
CPP
n = int(input()) count = n // 100 n -= n // 100 * 100 count += n // 20 n -= n // 20 * 20 count += n // 10 n -= n // 10 * 10 count += n // 5 n -= n // 5 * 5 count += n // 1 n -= n // 1 * 1 print(count)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; double EPS = 1e-9; double pi = acos(-1.0); map<pair<double, double>, int> m; map<double, int> especial; set<pair<double, double> > s; void proo(complex<double> p1, complex<double> p2) { double x = p1.real() - p2.real(), y = p1.imag() - p2.imag(); double pend = x / y, mag = sqrt(x * x + y * y); if (y != 0) m[make_pair(mag, pend)]++; else especial[mag]++; } int main() { int n; cin >> n; vector<complex<double> > v; for (int i = 0; i < n; i++) { double c1, c2; cin >> c1 >> c2; v.push_back(complex<double>(c1, c2)); } for (long long int i = (long long int)(0); i < (long long int)(n); i++) { for (long long int j = (long long int)(0); j < (long long int)(n); j++) { if (i == j) continue; if (!s.count({i, j})) { proo(v[i], v[j]); s.insert({i, j}), s.insert({j, i}); } } } long long int ans = 0; for (auto x : m) ans += ((x.second - 1) * (x.second)) / 2; for (auto x : especial) ans += ((x.second - 1) * (x.second)) / 2; cout << ans / 2; return 0; }
1,900
CPP
# @author --> ajaymodi # Naive approach import sys # sys.stdin=open("input.in","r") # sys.stdout=open("output.out","w") input=lambda : sys.stdin.readline().strip() char = [chr(i) for i in range(97,123)] CHAR = [chr(i) for i in range(65,91)] mp = lambda:list(map(int,input().split())) INT = lambda:int(input()) rn = lambda:range(INT()) from math import ceil,sqrt,factorial,gcd for _ in rn(): n = INT() l = sorted(mp()) if l[0] + l[1] <= l[-1]: print(1,2,n) else: print(-1)
800
PYTHON3
#include <bits/stdc++.h> int main(void) { long long int n, count = 1, max = 0; long long int i; long long int a[100000]; scanf("%lld", &n); for (i = 0; i < n; i++) scanf("%lld", &a[i]); for (i = 0; i < n - 1; i++) { if (a[i] <= a[i + 1]) { count++; } else { if (max < count) { max = count; } count = 1; } } if (max < count) max = count; printf("%lld\n", max); return 0; }
900
CPP
n = int(input()) s = "I hate" s1 = "I hate" s2 = "I love" for i in range(n): print(s, end = " ") if i != n - 1: print("that", end = " ") if s == s1: s = s2 else: s = s1 print("it")
800
PYTHON3
def sol(s, t): l = len(s) if(l != len(t)): return False j = l-1 for i in range(l): if s[i] != t[j]: return False j -= 1 return True def main(): s = input() t = input() if sol(s, t): print("YES") else: print("NO") if __name__ == '__main__': main()
800
PYTHON3
#Consistency is the key. #code by: amritanshu from sys import stdin,stdout import math from collections import deque input=stdin.readline def print(*args,end='\n'): s=[] for i in args: s.append(str(i)+' ') s=''.join(s) stdout.write(s+end) def solve(): n,m=map(int,input().split()) flag=0 for i in range(n): mat=[] for j in range(2): mat.append(list(map(int,input().split()))) if mat[0][1]==mat[1][0]: flag=1 if m%2: print('NO') return if flag: print('YES') else: print('NO') tt=1 tt=int(input()) for __ in range(tt): solve()
900
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { double l; double p, q; cin >> l >> p >> q; cout << l * (p / (p + q)) << endl; return 0; }
900
CPP
#include <bits/stdc++.h> using namespace std; int main() { string str; cin >> str; for (int i = 0; i < str.size(); i++) if (str[i] == 'r') cout << i + 1 << '\n'; for (int i = str.size() - 1; i >= 0; i--) if (str[i] == 'l') cout << i + 1 << '\n'; return 0; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; void yay(int flag) { if (flag == 0) cout << "NO"; else cout << "YES"; cout << endl; } int eve(long long int x) { if (x % 2 == 0) return 1; return 0; } void solve() { long long int n, x, y, a, b; cin >> n >> x >> y; a = min(x - 1, y - 1); a += max(x - (1 + a), y - (1 + a)); b = a; a = min(n - x, n - y); a += max(abs(x + a - n), abs(y + a - n)); if (a >= b) cout << "White"; else cout << "Black"; } int main() { ios_base::sync_with_stdio(false); int t = 1; while (t--) { solve(); cout << " \n"; } }
800
CPP
n, m = map(int, input().split()) line = input() alphabet = 'abcdefghijklmnopqrstuvwxyz' vocab = dict() antivocab = dict() for x in range(len(alphabet)): vocab[alphabet[x]] = alphabet[x] antivocab[alphabet[x]] = alphabet[x] for i in range(m): x, y = input().split() for i in antivocab[x]: vocab[i] = y for i in antivocab[y]: vocab[i] = x antivocab[x], antivocab[y] = antivocab[y], antivocab[x] new = [] for i in range(n): new.append(vocab[line[i]]) print(''.join(map(str, new)))
1,200
PYTHON3
t = int(input()) i = 0 while i < t: n, m = map(int, input().split()) arr = [] ans = "NO" for times in range(2 * n): arr.append(list(map(int, input().split()))) if m % 2 == 0: for p in range(0, len(arr) - 1, 2): index1 = arr[p][1] index2 = arr[p + 1][0] if index1 == index2: ans = "YES" break else: ans = "NO" print(ans) i += 1
900
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long mx = 1e5 + 9, INF = 1e9 + 7; long long T, N, K, A[mx]; void open() { if (fopen("aa.inp", "r")) { freopen("aa.inp", "r", stdin); freopen("aa.out", "w", stdout); } } void solve() { cin >> T; while (T--) { cin >> N; long long K; if (N == 1) { cout << "a" << "\n"; continue; } if (N % 2 == 0) { K = (N - 1) / 2; for (long long i = 1; i <= K; ++i) { cout << "a"; } cout << "b"; for (long long i = 1; i <= K + 1; ++i) { cout << "a"; } cout << "\n"; continue; } K = (N - 2) / 2; for (long long i = 1; i <= K; ++i) { cout << "a"; } cout << "bc"; for (long long i = 1; i <= K + 1; ++i) { cout << "a"; } cout << "\n"; } } int main() { open(); solve(); return 0; }
1,800
CPP
import math answer = [] t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split())) answer.append(math.ceil(sum(a)/n)) for i in answer: print(i)
800
PYTHON3
for s in[*open(0)][1:]:a,b,c=(10**(int(x)-1) for x in s.split());print(a,b+c)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long i, j, k, l, sum, r, q, f, x, z, p, y, e, n, m, kmax, maxi, t, mini, v, b, d, a, l1, l2, r1, r2, c; cin >> n; cin >> m; cin >> a; cin >> b; if (m == 1) { cout << 1 << endl; } else { if (a % m == 1 and b % m == 0) { cout << 1 << endl; } else { if ((a - 1) / m == (b - 1) / m) { cout << 1 << endl; } else { if (a % m == 1 and b == n) { cout << 1 << endl; } else { if (b == n) { cout << 2 << endl; } else { if ((a - 1) / m + 1 == (b - 1) / m) { cout << 2 << endl; } else { if ((a - 1) % m == b % m) { cout << 2 << endl; } else { if (b % m == 0 or a % m == 1) { cout << 2 << endl; } else { cout << 3 << endl; } } } } } } } } return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; int main() { int flag = 1; string x, p, y; cin >> x >> p; for (int i = 0; i < x.length(); i++) { if (p[i] > x[i]) { cout << -1 << endl; return 0; } } cout << p; return 0; }
900
CPP
#include <bits/stdc++.h> using namespace std; int calc(int n, int s) { if (s == 0) return 0; return s / min(n, s) + calc(n, s % min(n, s)); } int32_t main() { int n, s; cin >> n >> s; cout << calc(n, s) << "\n"; return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; int m, n, i, x, j; int a[1005]; long long c[1005][1005]; long long power[1005]; int l[1005], r[1005]; void fun() { c[0][0] = 1; for (i = 1; i < 1005; i++) { c[i][0] = 1; c[i][1] = i; for (j = 2; j <= i; j++) { c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % 1000000007; } } power[0] = 1; for (i = 1; i < 1005; i++) power[i] = (power[i - 1] * 2) % 1000000007; } int main() { fun(); scanf("%d%d", &n, &m); for (i = 1; i <= m; i++) { scanf("%d", &a[i]); } sort(a + 1, a + m + 1); a[0] = 0; for (i = 1; i <= m; i++) { l[i] = a[i - 1] + 1; r[i] = a[i] - 1; } l[i] = a[i - 1] + 1; r[i] = n; int tot = n - m; long long ans; if (r[1] >= l[1]) { ans = c[tot][r[1] - l[1] + 1]; tot -= r[1] - l[1] + 1; } else ans = 1; for (i = 2; i <= m; i++) { if (r[i] >= l[i]) { ans = (ans * (c[tot][r[i] - l[i] + 1] * power[r[i] - l[i]] % 1000000007)) % 1000000007; tot -= r[i] - l[i] + 1; } } if (r[m + 1] >= l[m + 1]) ans = ans * (c[tot][r[m + 1] - l[m + 1] + 1]) % 1000000007; printf("%d\n", ans); return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; const int M = 100000 + 10; struct NODE { int x; int h; int l; int r; } Node[M]; struct MUSH { int x; int po; int index; bool operator<(const MUSH& t) const { return x < t.x; } } imushroom[M]; int mushroom[M]; struct TREE { int r1; int r2; double val; double sval; } tree[4 * M]; void build(int st_len) { for (int i = st_len; i < 2 * st_len; i++) { tree[i].r1 = i - st_len; tree[i].r2 = tree[i].r1; } for (int i = st_len - 1; i >= 1; i--) { tree[i].r1 = min(tree[2 * i].r1, tree[2 * i + 1].r1); tree[i].r2 = max(tree[2 * i].r2, tree[2 * i + 1].r2); } return; } void update(int st_len, int node, int x1, int x2, double pr) { if (tree[node].sval != 0) { tree[node].val = tree[node].val + tree[node].sval * (tree[node].r2 - tree[node].r1 + 1); if (2 * node + 1 < 2 * st_len) { tree[2 * node].sval = tree[2 * node].sval + tree[node].sval; tree[2 * node + 1].sval = tree[2 * node + 1].sval + tree[node].sval; } tree[node].sval = 0; } if (tree[node].r1 > x2 || tree[node].r2 < x1) { return; } else { if (tree[node].r1 >= x1 && tree[node].r2 <= x2) { tree[node].sval += pr; return; } else { int b1 = max(tree[node].r1, x1); int b2 = min(tree[node].r2, x2); tree[node].val = tree[node].val + pr * (b2 - b1 + 1); update(st_len, 2 * node, x1, x2, pr); update(st_len, 2 * node + 1, x1, x2, pr); return; } } } double cal(int node, int target, int st_len) { if (tree[node].sval != 0) { tree[node].val = tree[node].val + tree[node].sval * (tree[node].r2 - tree[node].r1 + 1); if (2 * node + 1 < 2 * st_len) { tree[2 * node].sval = tree[2 * node].sval + tree[node].sval; tree[2 * node + 1].sval = tree[2 * node + 1].sval + tree[node].sval; } tree[node].sval = 0; } if (node == target + st_len) { return tree[node].val; } else { if (tree[2 * node].r1 <= target && target <= tree[2 * node].r2) { return cal(2 * node, target, st_len); } else { return cal(2 * node + 1, target, st_len); } } } int main() { for (int i = 0; i < 4 * M; i++) { tree[i].sval = tree[i].val = 0; } int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { scanf("%d %d %d %d", &Node[i].x, &Node[i].h, &Node[i].l, &Node[i].r); } for (int i = 0; i < m; i++) { scanf("%d %d", &imushroom[i].x, &imushroom[i].po); imushroom[i].index = i; } sort(imushroom, imushroom + m); for (int i = 0; i < m; i++) { mushroom[i] = imushroom[i].x; } int st_len = 1; while (st_len < m) { st_len *= 2; } build(st_len); for (int i = 0; i < n; i++) { int x1 = Node[i].x - Node[i].h; int x2 = Node[i].x - 1; double pr = 1 - 1.0 * Node[i].l / 100; if (pr < 1e-10) { pr = 1e-10; } pr = log(pr); int id1 = lower_bound(mushroom, mushroom + m, x1) - mushroom; int id2 = upper_bound(mushroom, mushroom + m, x2) - mushroom; id2 = id2 - (id2 != m); if (id1 <= id2) { update(st_len, 1, id1, id2, pr); } x1 = Node[i].x + 1; x2 = Node[i].x + Node[i].h; pr = 1 - 1.0 * Node[i].r / 100; if (pr < 1e-10) { pr = 1e-10; } pr = log(pr); id1 = lower_bound(mushroom, mushroom + m, x1) - mushroom; id2 = upper_bound(mushroom, mushroom + m, x2) - mushroom; id2 = id2 - (id2 != m); if (id1 <= id2) { update(st_len, 1, id1, id2, pr); } } double ans = 0; for (int i = 0; i < m; i++) { double res = cal(1, i, st_len); res = exp(res); ans = ans + res * imushroom[i].po; } printf("%.10f\n", ans); return 0; }
2,200
CPP
t=int(input()) for a in range (t): n=int(input()) s=input() s1=[] c=0 for i in range (n): if int(s[i])%2!=0 : c+=1 s1.append(s[i]) if c==2 : break if c==2 : print("".join(s1)) else : print("-1")
900
PYTHON3
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; const int MAXN = 2010; int n, pre[MAXN], cyc[MAXN]; int cnt[MAXN], maks[MAXN], m; vector<int> e[MAXN]; pii p[MAXN]; int bio[MAXN], cookie, f[MAXN], max_x; void add(int i, int a, int b) { for (int j = (0); j < (m); ++j) if ((a == -1 || p[j].first == a) && (b == -1 || p[j].second == b)) { e[i].push_back(n + j); e[n + j].push_back(i); } } void assign(int i, int a, int b) { pre[i] = a; cyc[i] = b; } int dfs(int first) { if (first == -1) return 1; if (bio[first] == cookie) return 0; bio[first] = cookie; for (int second : e[first]) if (dfs(f[second])) { f[second] = first; return 1; } return 0; } bool done[MAXN]; int matching(void) { memset(f, -1, sizeof(f)); memset(done, false, sizeof(done)); int ret = 0; for (int i = (0); i < (n); ++i) if (pre[i] == 0 && cyc[i] != -1) { for (int j = (0); j < (m); ++j) if (f[n + j] == -1 && p[j].first == 0 && p[j].second == cyc[i]) { f[n + j] = i; done[i] = true; ++ret; break; } } for (int first = (0); first < (n); ++first) { if (done[first]) continue; ++cookie; if (dfs(first)) ++ret; } return ret; } void load() { scanf("%d", &n); for (int i = (1); i < (n + 1); ++i) maks[i] = -2; for (int i = (0); i < (n); ++i) { char a[10], b[10]; scanf("%s %s", a, b); pre[i] = cyc[i] = -1; if (a[0] != '?') sscanf(a, "%d", &pre[i]); if (b[0] != '?') sscanf(b, "%d", &cyc[i]); if (cyc[i] == -1 && pre[i] != -1) max_x = max(max_x, pre[i]); if (cyc[i] != -1) { if (pre[i] == 0) ++cnt[cyc[i]]; maks[cyc[i]] = max(maks[cyc[i]], pre[i]); } } } int main(void) { load(); for (int l = 1; l <= n; ++l) { m = 0; bool ok = true; for (int k = 1; k <= n; ++k) if (maks[k] != -2 || k == l) { int t = max(k, (cnt[k] + k - 1) / k * k); for (int j = (0); j < (t); ++j) p[m++] = {0, k}; int to = (l == k && max_x != -1) ? max(maks[k], max_x) + 1 : maks[k] + 1; for (int j = (1); j < (to); ++j) p[m++] = {j, k}; if (m > n) { ok = false; break; } } if (!ok) continue; for (int i = (0); i < (n + m); ++i) e[i].clear(); for (int i = (0); i < (n); ++i) { add(i, pre[i], cyc[i]); } int match = matching(); if (match != m) { continue; } for (int i = (n); i < (n + m); ++i) { assign(f[i], p[i - n].first, p[i - n].second); } for (int i = (0); i < (n); ++i) if ((pre[i] == -1 || pre[i] == 0) && cyc[i] == -1) assign(i, 0, 1); else if (pre[i] == -1) assign(i, 1, cyc[i]); else if (cyc[i] == -1) assign(i, pre[i], l); for (int k = (1); k < (n + 1); ++k) { int tot = 0; for (int i = (0); i < (n); ++i) if (pre[i] == 0 && cyc[i] == k) ++tot; assert(tot % k == 0); } for (int i = (0); i < (n); ++i) if (pre[i] == 0) { int k = cyc[i]; int first = -1, c = 0; for (int j = (0); j < (i + 1); ++j) { if (cyc[j] != k || pre[j] != 0) continue; if (c % k == 0) first = j; ++c; } int nxt = -1; for (int j = (i + 1); j < (n); ++j) if (cyc[j] == k && pre[j] == 0) { nxt = j; break; } if (c % k == 0) printf("%d ", first + 1); else printf("%d ", nxt + 1); } else { for (int j = (0); j < (n); ++j) if (pre[j] == pre[i] - 1 && cyc[j] == cyc[i]) { printf("%d ", j + 1); break; } } printf("\n"); return 0; break; } printf("-1\n"); return 0; }
3,400
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int f[maxn], pos[maxn]; int main() { int n, m, tmp; bool ambs, psble; vector<int> ans; while (cin >> n >> m) { ans.clear(); ambs = psble = false; memset(f, 0, sizeof(f)); for (int i = 0; i < n; i++) { scanf("%d", &tmp); f[tmp]++; pos[tmp] = i + 1; } for (int i = 0; i < m; i++) { scanf("%d", &tmp); if (f[tmp] > 1) ambs = true; else if (f[tmp] == 1) ans.push_back(pos[tmp]); else psble = true; } if (psble == true) puts("Impossible"); else if (ambs == true) puts("Ambiguity"); else { puts("Possible"); int size = ans.size(); for (int i = 0; i < size - 1; i++) printf("%d ", ans[i]); printf("%d\n", ans[size - 1]); } } return 0; }
1,500
CPP
#include <bits/stdc++.h> int n, m, k; int main() { scanf("%d%d%d", &n, &m, &k); if (n + k < m) printf("0\n"); else { double ans = 1; for (int i = 1; i <= k + 1; ++i) { ans *= ((double)(m - i + 1) / (n + i)); } ans = 1.0 - ans; printf("%.4f\n", ans); } }
2,400
CPP
#include <bits/stdc++.h> using namespace std; struct SETS { int fa[500005]; void init(int n) { for (int i = 1; i <= n; i++) fa[i] = i; } int find_father(int x) { return (fa[x] == x) ? x : fa[x] = find_father(fa[x]); } void merge(int x, int y) { x = find_father(x); y = find_father(y); if (x == y) return; fa[x] = y; } } st1, st2; set<int> bel, equ; int num[500005], ans[500005], maxn, n; int fir[500005], id[500005]; void change1(int x, bool v) { if (x == 1 || num[x] == num[x - 1]) equ.erase(x - 1); if (x == n || num[x] == num[x + 1]) equ.erase(x); num[x] = 1; if (x == 1 || num[x] == num[x - 1]) equ.insert(x - 1); if (x == n || num[x] == num[x + 1]) equ.insert(x); if (x == 1) num[x - 1] = 1; if (x == n) num[x + 1] = 1; if (x > 1 && num[x - 1]) { st1.merge(x, x - 1); st2.merge(x - 1, x); } if (x < n && num[x + 1]) { st2.merge(x, x + 1); st1.merge(x + 1, x); } } void change2(int x, bool v) { int l = st1.find_father(x), r = st2.find_father(x); if (l < r || x == 1 || x == n) { set<int>::iterator it = equ.lower_bound(l); if (l > 1 && it != equ.begin()) { it--; int t = (*it) + 1; if (num[t]) { if (v) maxn = max(maxn, (l - t) >> 1); l = t + 1; } else { if (v) maxn = max(maxn, (l - t) >> 1); l = ((t + l) >> 1) + 1; } } it = equ.upper_bound(r); if (r < n && it != equ.end()) { int t = (*it); if (num[t]) { if (v) maxn = max(maxn, (t - r) >> 1); r = t - 1; } else { if (v) maxn = max(maxn, (t - r) >> 1); r = ((t + r) >> 1); } } } else { set<int>::iterator it1 = equ.lower_bound(x); it1--; set<int>::iterator it2 = equ.upper_bound(x); int nl = (*it1) + 1, nr = (*it2); if (v) maxn = max(maxn, (nr - nl) >> 1); if (num[nl] && num[nr]) { l = nl + 1; r = nr - 1; } else if (num[nl]) { l = nl + 1; r = ((nl + nr) >> 1); } else if (num[nr]) { l = ((nl + nr) >> 1) + 1; r = nr - 1; } else { l = 1; r = 0; } } if (l <= r) { for (;;) { set<int>::iterator it = bel.lower_bound(l); if (it != bel.end() && (*it) <= r) { ans[*it] = fir[x]; bel.erase(it); } else break; } } } bool cmp(int x, int y) { return fir[x] < fir[y]; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &fir[i]); id[i] = i; bel.insert(i); } sort(id + 1, id + n + 1, cmp); st1.init(n); st2.init(n); for (int i = 0; i <= n; i++) equ.insert(i); int r; for (int i = 1; i <= n; i = r + 1) if (fir[id[i]] > fir[id[i - 1]]) { r = i; while (r < n && fir[id[r + 1]] == fir[id[r]]) r++; for (int j = i; j <= r; j++) change1(id[j], 1); for (int j = i; j <= r; j++) change2(id[j], 1); } printf("%d\n", maxn); for (int i = 1; i <= n; i++) printf("%d ", ans[i]); printf("\n"); return 0; }
3,300
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> T gcd(T a, T b) { if (a == 0) return b; return gcd(b % a, a); } template <typename T> T pow(T a, T b, long long m) { T ans = 1; a = a % m; while (b > 0) { if (b % 2 == 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans % m; } template <typename T> T modInv(T a, T m) { return pow(a, m - 2, m); } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; int factor[1500000]; for (int i = 1; i < 1500000; i++) factor[i] = i; for (int i = 2; i < 1500000; i++) { if (factor[i] != i) continue; for (int j = 2 * i; j < 1500000; j += i) factor[j] = i; } bool used[1500000]; for (int i = 2; i < 1500000; i++) used[i] = false; int lar = 2; bool flag = true; for (int i = 1; i <= n; i++) { int x; cin >> x; int out = 4; if (flag == true) { for (int j = x; j < 1500000; j++) { int p = j; bool coprime = true; while (p != 1) { if (used[factor[p]]) coprime = false; p /= factor[p]; } if (coprime) { if (j > x) flag = false; out = j; break; } } } else { for (int j = lar; j < 1500000; j++) { if (factor[j] != j) continue; if (!used[j]) { out = j; lar = j; break; } } } cout << out << " "; int p = out; while (p != 1) { used[factor[p]] = true; p /= factor[p]; } } return 0; }
1,900
CPP
x, y = list(map(int, input().split())) a = True d = 1 while a: n = x * 3 m = y * 2 if n > m: print(d) a = False elif m >= n: d += 1 x = n y = m
800
PYTHON3
def transform_string(string): final = "" vowels = 'aoyeuiAOYEUI' for char in string: if char not in vowels: final += "." final += char.lower() return final input_string = input() final_string = transform_string(input_string) print(final_string)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; char dp[100][100]; bool used[101][101] = {false}; bool check(int i, int j) { if (!used[i][j] && !used[i + 1][j] && !used[i][j + 1] && !used[i - 1][j] && !used[i][j - 1] && dp[i + 1][j] == '#' && dp[i][j + 1] == '#' && dp[i - 1][j] == '#' && dp[i][j - 1] == '#') { return true; } else return false; } int main() { int n; cin >> n; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { cin >> dp[i][j]; } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (dp[i][j] == '#' && check(i, j)) { used[i][j] = true; used[i + 1][j] = true; used[i][j + 1] = true; used[i - 1][j] = true; used[i][j - 1] = true; } } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (dp[i][j] == '#' && !used[i][j]) { cout << "NO" << endl; return 0; } } } cout << "YES" << endl; }
1,100
CPP
s = input()[1:-1] letters = set() for l in s: if l!=' ' and l!=',': letters.add(l) print(len(letters))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; long long check(vector<long long> &a, long long b, long long a1) { long long res = 0; long long ap = a1; for (int i = 0; i < a.size(); ++i) { if (abs(ap - a[i]) < 2) { res += abs(ap - a[i]); } else { return 1000000009; } ap += b; } return res; } int main() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } if (n == 1) { cout << "0\n"; return 0; } long long ans = 1000000009; ans = min(ans, check(a, a[1] - a[0], a[0])); ans = min(ans, check(a, a[1] - a[0] + 1, a[0])); ans = min(ans, check(a, a[1] - a[0] - 1, a[0])); ans = min(ans, check(a, a[1] - a[0] + 1, a[0] - 1)); ans = min(ans, check(a, a[1] - a[0] + 2, a[0] - 1)); ans = min(ans, check(a, a[1] - a[0], a[0] - 1)); ans = min(ans, check(a, a[1] - a[0] - 1, a[0] + 1)); ans = min(ans, check(a, a[1] - a[0], a[0] + 1)); ans = min(ans, check(a, a[1] - a[0] - 2, a[0] + 1)); if (ans == 1000000009) ans = -1; cout << ans << '\n'; return 0; }
1,500
CPP
t = int(input()) for q in range(t): res = 0 n, k = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) a.sort() a = a[::-1] b.sort() for i in range(k): b[i] -= 1 for i in range(k): res += a[i] if b[i] == 0: res += a[i] index = k - 1 for i in range(k): index += b[i] if b[i] != 0: res += a[index] print(res)
1,400
PYTHON3
n,m = map(int,input().strip().split(" ")) a = list(map(int,input().strip().split(" "))) b = list(map(int,input().strip().split(" "))) dp = [ [ 0 for i in range(len(b)) ] for j in range(len(a)) ] for i in range(len(a)): for j in range(len(b)): dp[i][j] = a[i]&b[j] s = set(dp[0]) for i in range(1,len(a)): ss = set() for j in s: for k in range(len(b)): ss.add(dp[i][k]|j) s = ss print(min(s))
1,600
PYTHON3
#include <bits/stdc++.h> using namespace std; int k; void solve(int v, int p, vector<vector<long long> > &up, vector<vector<long long> > &down, vector<long long> &ans, vector<vector<long long> > &graph, vector<long long> &ans_d) { if (p != -1) { for (int i = 1; i < up[p].size(); ++i) { up[v][i] += up[p][i - 1]; } up[v][0] += up[p].back(); up[v][min(1, k - 1)]++; ans[v] += ans[p]; ans[v] += up[p][0]; ans[v]++; for (int i = 1; i < down[p].size(); ++i) { up[v][i] += down[p][i - 1]; } up[v][0] += down[p].back(); if (down[p][min(k - 1, 1)]) { up[v][min(1, k - 1)] + down[p][0]; } ans[v] += ans_d[p]; ans[v] += down[p][0]; } for (int i = 0; i < graph[v].size(); ++i) { long long to = graph[v][i]; if (to == p) { continue; } solve(to, v, up, down, ans, graph, ans_d); ans_d[v] += ans_d[to]; ans_d[v] += down[to][0]; ans_d[v] += 1; for (int i = 1; i < down[to].size(); ++i) { down[v][i] += down[to][i - 1]; } down[v][0] += down[to].back(); down[v][min(k - 1, 1)]++; } } int main() { long long n; cin >> n >> k; vector<vector<long long> > graph(n); long long from, to; for (int i = 0; i < n - 1; ++i) { cin >> from >> to; graph[from - 1].push_back(to - 1); graph[to - 1].push_back(from - 1); } vector<vector<long long> > up(n, vector<long long>(k)), down(n, vector<long long>(k)); vector<long long> ans(n), ans_d(n); solve(0, -1, up, down, ans, graph, ans_d); long long result = 0; for (int i = 0; i < n; ++i) { result += ans[i]; } cout << result; }
2,100
CPP
n=int(input()) m25=0 m50=0 m100=0 a=[int(n) for n in input().split(" ")] for i in range(len(a)): if a[i]==25: m25=m25+1 elif a[i]==50: m50=m50+1 m25=m25-1 else: m100=m100+1 if m50==0: m25=m25-3 else: m50=m50-1 m25=m25-1 if m25<0 or m50<0: print("NO") break if m25>=0 and m50>=0: print("YES")
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; struct st { int neg, pos; int ans; st() { neg = pos = ans = 0; }; } tree[3 * 1000006], q[3 * 1000006]; void merge(int x, st T[]) { T[x].ans = T[2 * x].ans + T[2 * x + 1].ans; int omar = min(T[2 * x].pos, T[2 * x + 1].neg); T[x].ans += 2 * omar; T[x].pos = T[2 * x].pos - omar + T[2 * x + 1].pos; T[x].neg = T[2 * x + 1].neg - omar + T[2 * x].neg; } char ar[1000006]; void init(int x, int a, int b) { if (a == b) { tree[x] = st(); if (ar[a] == ')') tree[x].neg = 1; else tree[x].pos = 1; return; } init(2 * x, a, (a + b) / 2); init(2 * x + 1, (a + b) / 2 + 1, b); merge(x, tree); } int query(int x, int a, int b, int i, int j) { if (a > j || b < i) { q[x] = st(); return 0; } if (i <= a && j >= b) { q[x].ans = tree[x].ans; q[x].neg = tree[x].neg; q[x].pos = tree[x].pos; return q[x].ans; } query(2 * x, a, (a + b) / 2, i, j); query(2 * x + 1, (a + b) / 2 + 1, b, i, j); merge(x, q); return q[x].ans; } int main() { scanf("%s", ar); int n = strlen(ar); int Q; int u, v; init(1, 0, n - 1); scanf("%d", &Q); while (Q--) { scanf("%d%d", &u, &v); printf("%d\n", query(1, 0, n - 1, u - 1, v - 1)); } }
2,000
CPP
#include <bits/stdc++.h> using namespace std; vector<int> sol; int main(int argc, char** argv) { int n, t = 0; string s; cin >> n; cin >> s; for (int i = 0; i < n; i++) { if (s[i] == 'B') t++; else if (t > 0) { sol.push_back(t); t = 0; } } if (t != 0) sol.push_back(t); int k = sol.size(); cout << k << endl; for (int i = 0; i < sol.size(); i++) cout << sol[i] << " "; return 0; }
800
CPP
Turn = int(input()) m = int(input()) A = {2:((2,3),(4,5),(0,1)), 0:((5,0),(1,2),(3,4)), 1:((1,4),(3,0),(5,2))} Num = Turn%6 if Num in A[m][0]: print(0) elif Num in A[m][1]: print(1) elif Num in A[m][2]: print(2)
1,000
PYTHON3
for i in range(int(input())): n = int(input()) l = list(map(int, input().split())) c = 1 l1 = list(set(l)) val = min(l) for i in range(len(l1)): if val < l1[i]: c += 1 print(c)
1,200
PYTHON3
#include<bits/stdc++.h> using namespace std; #define int long long #define f first #define s second int inf=1000000007; void solve(); /*bool prime[1000001]={1}; void sieve() { int i; for(i=0;i<1000000;i++)prime[i]=1; for(i=2;i*i<=1000000;i++) { if(prime[i]==1){for(int j=i*i;j<=1000000;j+=i)prime[j]=0;} } }*/ bool pal(string s) { int n=s.length(); for(int i=0;i<n/2;i++) { if(s[i]!=s[n-1-i]) { return 0; } } return 1; } int digit(int n) { int count=0; while(n>0) { count+=n%10; n/=10; } return count; } bool pri(int n) { if(n==1)return 1; for(int i=2;i*i<=n;i++) { if(n%i==0)return 0; } return 1; } bool perf(int x) { if(x>=0) { int sq=sqrt(x); return sq*sq==x; } return 0; } signed main() { ios_base::sync_with_stdio(false);cin.tie(NULL); int t; //sieve(); cin>>t; while(t--) { solve(); cout<<"\n"; } return 0; } void solve() { int n,x,i,sum=0,ans=0; cin>>n; if(n%2050!=0) { cout<<-1; return; } else { n/=2050; cout<<digit(n); } }
800
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; string s; cin >> s; string number; for (int i = 0; i < s.length(); i++) { if (i != s.length() - 1) { if (s[i] == '.') number += '0'; else if (s[i] == '-' && s[i + 1] == '.') number += '1', i++; else if (s[i] == '-' && s[i + 1] == '-') number += '2', i++; else 1; } else number += '0'; } cout << number << endl; return 0; }
800
CPP
n = int(input()) a = [int(_) for _ in input().split()] kmin = 10000000000000 mn = 1000000000000 for i in range(n-1): mn = min(mn, a[i], a[n-1-i]) p = mn // (n-i-1) if p <= kmin: kmin = p print(kmin)
1,300
PYTHON3
for i in range(int(input())): n,k = map(int , input().split()) l = set(list(map(int , input().split()))) if(len(l) > k): print(-1) continue print(n*k) print(*(list(l) + [1]*(k-len(l))) *n)
1,400
PYTHON3
n,k,x=map(int,input().split()) a=sorted(list(map(int,input().split())));s=[] for i in range(n-1): if a[i+1]-a[i]>x:s.append(a[i+1]-a[i]) size=len(s)+1;s.sort() for i in s: f=(i-1)//x if f<=k:k-=f;size-=1 else:break print(size)
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; int a[maxn], b[maxn], s[maxn], n; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; int cnt = max(a[0], b[0]); for (int i = 1; i < n; i++) { if (max(a[i], b[i]) <= cnt) cnt = max(a[i], b[i]); else if (max(a[i], b[i]) > cnt && min(a[i], b[i]) <= cnt) cnt = min(a[i], b[i]); else { cout << "NO" << endl; return 0; } } cout << "YES" << endl; return 0; }
1,000
CPP
n = int(input()) a = list(map(int,input().split())) max_l = 1 l = 1 for i in range(n-1): if(a[i+1] > a[i]): l += 1 if(l > max_l): max_l=l else: l = 1 print(max_l)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; constexpr int nmax = 200005; int s[nmax]; int n; int dp[nmax]; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> s[i]; s[i] += s[i - 1]; } int maxdp = s[n]; dp[n] = s[n]; for (int i = n - 1; i >= 2; --i) { dp[i] = s[i] - maxdp; if (dp[i] > maxdp) maxdp = dp[i]; } int ans = dp[2]; for (int i = 3; i <= n; i++) ans = max(ans, dp[i]); cout << ans << '\n'; return 0; }
2,200
CPP
inp = lambda cast=int: [cast(x) for x in input().split()] printf = lambda s='', *args, **kwargs: print(str(s).format(*args), flush=True, **kwargs) t, = inp() for _ in range(t): n, = inp() if n%2: print(n//2) else: print(n//2-1)
800
PYTHON3
import sys input = sys.stdin.readline t=int(input()) for tests in range(t): n=int(input()) AA=[] for i in range((n+2)//2,n): AA.append((1,i)) for i in range((n+2)//2-1,0,-1): AA.append((i,n)) L=list(range(1,n+1)) sc=0 for x,y in AA[::-1]: L[x-1],L[y-1]=L[y-1],L[x-1] sc+=(x-y)**2 print(sc) print(*L) print(len(AA)) for x,y in AA: if L[x-1]==y: print(y,x) else: print(x,y) L[x-1],L[y-1]=L[y-1],L[x-1]
2,500
PYTHON3
#include <bits/stdc++.h> signed main() { double l3, l4, l5; std::cin >> l3 >> l4 >> l5; double r3, r4, r5; double h3, h4, h5; double s3, s4, s5; r3 = sqrt(l3 * l3 - l3 * l3 / 4) * 2 / 3; h3 = sqrt(l3 * l3 - r3 * r3); s3 = l3 * sqrt(l3 * l3 - l3 * l3 / 4) / 2 * h3 / 3; r4 = sqrt(2 * l4 * l4) / 2; h4 = sqrt(l4 * l4 - r4 * r4); s4 = l4 * l4 * h4 / 3; r5 = sqrt(50 + 10 * sqrt(5)) * l5 / 10; h5 = sqrt(l5 * l5 - r5 * r5); s5 = sqrt(25 + 10 * sqrt(5)) * l5 * l5 / 4 * h5 / 3; printf("%.9lf", s3 + s4 + s5); }
1,700
CPP
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { out << '(' << p.first << ',' << p.second << ')'; return out; } template <class T1, class T2> istream& operator>>(istream& in, pair<T1, T2>& p) { in >> p.first >> p.second; return in; } template <class T> istream& operator>>(istream& in, vector<T>& v) { for (auto& x : v) in >> x; return in; } template <class T> ostream& operator<<(ostream& out, vector<vector<T>>& v) { for (auto& x : v) out << x << '\n'; return out; } template <class T> ostream& operator<<(ostream& out, vector<T>& v) { for (auto x : v) out << x << ' '; return out; } long long gcd(long long a, long long b) { if (b > a) swap(a, b); return (b ? gcd(b, a % b) : a); } using ll = long long; using pii = pair<int, int>; using pll = pair<long long, long long>; using tiii = pair<pair<int, int>, int>; using vi = vector<int>; using vl = vector<long long>; using vvi = vector<vector<int>>; using vvl = vector<vector<long long>>; const int h = 1000000007; vi ins, l; vvi M; vi C, ans; void dfs1(int x) { l[x] = 1; for (int y : M[x]) dfs1(y), l[x] += l[y]; } void dfs(int x) { if (C[x] >= l[x]) { cout << "NO"; exit(0); } ans[x] = ins[C[x]]; ins.erase(ins.begin() + C[x]); for (int y : M[x]) dfs(y); } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cerr.setstate(ios::failbit); int n; cin >> n; ans.resize(n + 1); M.resize(n + 1); ins.resize(n); C.resize(n + 1); l.resize(n + 1); for (int i = 0; i < n; i++) ins[i] = i + 1; for (int i = 1; i < n + 1; i++) { int p, c; cin >> p >> c; M[p].push_back(i); C[i] = c; } dfs1(M[0][0]); dfs(M[0][0]); cout << "YES\n"; for (int i = 1; i < n + 1; i++) cout << ans[i] << ' '; }
1,800
CPP
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split(" "))) a.sort() x,y = 0,0 for i in range(n): if i in a: a.remove(i) if i==n-1: x = i+1 else: x = i break m = len(a) for j in range(m): if j in a: a.remove(j) if j==m-1: y = j+1 else: y = j break print(x+y)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; string b[2][105], a[4][105], r; long long int ans = INT_MAX, n; void fun(long long int fir, long long int sec, long long int thi, long long int fou) { long long int i, j, val = 0; for (i = 0; i < n; ++i) for (j = 0; j < n; ++j) if (b[fir][i][j] != a[0][i][j]) ++val; for (i = 0; i < n; ++i) for (j = 0; j < n; ++j) if (b[sec][i][j] != a[1][i][j]) ++val; for (i = 0; i < n; ++i) for (j = 0; j < n; ++j) if (b[thi][i][j] != a[2][i][j]) ++val; for (i = 0; i < n; ++i) for (j = 0; j < n; ++j) if (b[fou][i][j] != a[3][i][j]) ++val; ans = min(ans, val); } int main() { long long int i, j, k; cin >> n; for (k = 0; k < 4; ++k) for (i = 0; i < n; ++i) cin >> a[k][i]; for (i = 0; i < n; ++i) r.push_back('0'); for (i = 0; i < n; ++i) { b[0][i] = r; b[1][i] = r; } for (i = 0; i < n; ++i) for (j = 0; j < n; ++j) if ((i + j) % 2 == 1) b[0][i][j] = '1'; else b[1][i][j] = '1'; fun(0, 0, 1, 1); fun(0, 1, 0, 1); fun(0, 1, 1, 0); fun(1, 0, 0, 1); fun(1, 0, 1, 0); fun(1, 1, 0, 0); cout << ans; return 0; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1 << 20, MOD = (int)1e9 + 9277; const long long base = 31; int n, m, l, y[N], h[N], pw[N], hp[N]; char p[N], s[N]; void HASH() { pw[0] = 1; for (int i = (1); i <= (N - 1); i++) { pw[i] = pw[i - 1] * base % MOD; } } int getHash(int l, int r) { return (h[r] + MOD - 1LL * h[l - 1] * pw[r - l + 1] % MOD) % MOD; } int main() { scanf("%d %d", &n, &m); scanf("%s", p); l = strlen(p); HASH(); for (int i = (1); i <= (l); i++) hp[i] = (hp[i - 1] * base + p[i - 1] - 'a' + 1) % MOD; for (int i = 0; i < int(m); i++) scanf("%d", y + i); int r = 0; for (int i = 0; i < int(m); i++) { for (int j = max(r + 1, y[i]); j < y[i] + l; j++) { s[j] = p[j - y[i]]; h[j] = (h[j - 1] * base + s[j] - 'a' + 1) % MOD; } if (r >= y[i]) if (getHash(y[i], r) != hp[r - y[i] + 1]) { printf("0"); return 0; } r = max(r, y[i] + l - 1); } int ans = 1; for (int i = (1); i <= (n); i++) if (!s[i]) ans = 1LL * ans * 26 % 1000000007; printf("%d", ans); return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j; string s; cin >> n; cin >> s; for (i = 0; i < n; i++) { if ((s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' || s[i] == 'y') && (s[i + 1] == 'a' || s[i + 1] == 'e' || s[i + 1] == 'i' || s[i + 1] == 'o' || s[i + 1] == 'u' || s[i + 1] == 'y')) { strcpy(&s[i + 1], &s[i + 2]); i--; } } i = 0; while (1) { if (s[i] == '\0') { break; } cout << s[i]; i++; } }
800
CPP
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const int INF = 1e9; const long long INF64 = 7e18; const long double EPS = 1e-9; const long double PI = 3.14159265358979310000; const long long MD = 535355424; const int T = 1e5 * 3; const int N = 400101; const int M = 61; int n, m, r, a[M][M][M], d[M][M][M]; int main() { cin >> n >> m >> r; for (int u = 0; u < int(m); u++) for (int i = 0; i < int(n); i++) for (int j = 0; j < int(n); j++) scanf("%d", &a[u][i][j]); for (int u = 0; u < int(M); u++) for (int i = 0; i < int(M); i++) for (int j = 0; j < int(M); j++) d[u][i][j] = INF; for (int u = 0; u < int(m); u++) for (int k = 0; k < int(n); k++) for (int i = 0; i < int(n); i++) for (int j = 0; j < int(n); j++) a[u][i][j] = min(a[u][i][j], a[u][i][k] + a[u][k][j]); for (int u = 0; u < int(m); u++) for (int i = 0; i < int(n); i++) for (int j = 0; j < int(n); j++) d[0][i][j] = min(d[0][i][j], a[u][i][j]); for (int u = 0; u < int(n); u++) for (int k = 0; k < int(n); k++) for (int i = 0; i < int(n); i++) for (int j = 0; j < int(n); j++) { if (d[u + 1][i][j] >= d[u][i][k] + d[0][k][j]) d[u + 1][i][j] = d[u][i][k] + d[0][k][j]; if (d[u + 1][i][j] >= d[0][i][k] + d[u][k][j]) d[u + 1][i][j] = d[0][i][k] + d[u][k][j]; } for (int i = 0; i < int(r); i++) { int a, b, k; scanf("%d %d %d", &a, &b, &k); a--; b--; k = min(k, n - 1); printf("%d\n", d[k][a][b]); } return 0; }
1,800
CPP
k=input() l={'a','e','i','o','u','y','Y','A','E','I','O','U'} for b in k: if(b not in l): print('.',end="") print(b.lower(),end="")
1,000
PYTHON3
import collections # 011011 # 11000 # # 0:[] # 1:[1,2] def solve(lst): stck=[] lst=[int(i) for i in lst] prev=-1 mx1,mx2=1,1 mps={0:[],1:[]} for i in range(len(lst)): if lst[i]: if mps[lst[i]-1]: p=mps[lst[i]-1].pop() mps[lst[i]].append(p) stck.append(p) if mx1<p: mx1=p else: if mps[lst[i]]: mx1+=1 stck.append(mx1) mps[lst[i]].append(mx1) else: mps[lst[i]].append(mx1) stck.append(mx1) else: if mps[lst[i]+1]: p=mps[lst[i]+1].pop() mps[lst[i]].append(p) stck.append(p) if mx2 < p: mx2=p else: if mps[lst[i]]: mx2+=1 stck.append(mx2) mps[lst[i]].append(mx2) else: mps[lst[i]].append(mx2) stck.append(mx2) return stck def sort(key): return key[0] ans=[] n = int(input()) for i in range(n): k = int(input()) lst= input() k = [str(i) for i in solve(lst)] print(max(solve(lst))) print(' '.join(k)) for i in ans: print(i)
1,500
PYTHON3
import math def solve(): n, = map(int, input().split()) top = 1 while top <= n: top *= 2 top -= 1 ans = [0] * (n + 1) i = n cur = n while i > 0: if top - i > cur: cur = i top = 1 while top <= cur: top *= 2 top -= 1 ans[i] = top - i ans[top - i] = i i -= 1 score = 0 if n % 2 == 1: ans[top] = 0 ans[0] = top for i in range(n + 1): score += ans[i] ^ i print(score) print(*ans) if __name__ == "__main__": t = 1 # t = int(input()) for _ in range(t): solve()
1,700
PYTHON3
n=int(input()) l=[] for _ in range(n): l.append([int(i) for i in input().split()]) x=l[0][1] m=l[0][1] for i in range(1,n): x=x-l[i][0]+l[i][1] if x>m: m=x; print(m)
800
PYTHON3
for _ in range(int(input())): n=int(input()) a=list(map(int,input().split())) dp=[[float('inf') for _ in ' '*2] for _ in range(n+1)] dp[0][1]=0 for i in range(n): for j in range(2): for fight in range(1,min(n-i,2)+1): hard = a[i] if fight>1: hard+=a[i+1] dp[i+fight][j]=min(dp[i+fight][j],dp[i][not j]+[0,1][j==0]*hard) print(min(dp[n][0],dp[n][1]))
1,500
PYTHON3
# from collections import defaultdict # import itertools # import statistics t = int(input()) for _ in range(t): # n,m = map(int,input().split()) # w = list(map(int,input().split())) r,g,b,w = map(int,input().split()) if r==b==g: print("YES") continue if r==0 and b!=0 and g!=0: if b%2==0 and g%2==0: print("YES") else: print("NO") continue if g==0 and b!=0 and r!=0: if b%2==0 and r%2==0: print("YES") else: print("NO") continue if b==0 and r!=0 and g!=0: if r%2==0 and g%2==0: print("YES") else: print("NO") continue x = min(r,g,b) if (r-x)%2==0 and (b-x)%2==0 and (g-x)%2==0: print("YES") continue if r==0 and b==0 and g%2==0: print("YES") continue if r==0 and b%2==0 and g==0: print("YES") continue if r%2==0 and b==0 and g==0: print("YES") continue if r%2==0 and g%2==0 and b%2==0: print("YES") continue if r%2==0 and g%2==1 and b%2==1 and w%2==1: print("YES") continue if r%2==1 and g%2==1 and b%2==0 and w%2==1: print("YES") continue if r%2==1 and g%2==0 and b%2==1 and w%2==1: print("YES") continue if r%2==1 and g%2==1 and b%2==1: print("YES") continue if r%2==1 and b%2==0 and g%2==0 and w%2==0: print("YES") continue if r%2==0 and b%2==1 and g%2==0 and w%2==0: print("YES") continue if r%2==0 and b%2==0 and g%2==1 and w%2==0: print("YES") continue if r%2==0 and b%2==0 and g%2==0 and w%2==1: print("YES") continue print("NO")
1,000
PYTHON3
n=int(input()) import math summ=int((n*(n+1))/2) j=summ-2 flag=0 key1=0 key2=0 for i in range(2,math.ceil(summ/2)): if math.gcd(i,j)>=2: flag=1 key1,key2=i,j break j-=1 if flag==1: print("Yes") keyf=min(key1,key2) print(1,keyf) print(n-1,end=" ") ite=1 while(True): if ite==n+1: break else: if ite!=keyf: print(ite,end=" ") ite+=1 print() else: print("No")
1,100
PYTHON3
t=int(input()) while(t>0): gift = int(input()) candy = list(map(int,input().split())) orange = list(map(int,input().split())) c_min = min(candy) o_min = min(orange) count=0 for i in range(gift): count+=max(abs(candy[i] - c_min),abs(orange[i] - o_min)) print(count) t-=1
800
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<unsigned long long> sub; vector<unsigned long long> pe; vector<vector<pair<unsigned long long, unsigned long long>>> v; void dfs(unsigned long long vr, unsigned long long p) { sub[vr] = 1; for (auto i : v[vr]) { if (i.first != p) { dfs(i.first, vr); pe[i.first] = i.second; sub[vr] += sub[i.first]; } } } int main() { int t; cin >> t; while (t--) { v.clear(); sub.clear(); pe.clear(); unsigned long long k; cin >> k; v.resize(2 * k); for (int i = 0; i < 2 * k - 1; i++) { unsigned long long a, b, c; cin >> a >> b >> c; a--; b--; v[a].push_back({b, c}); v[b].push_back({a, c}); } sub.resize(2 * k, 0); pe.resize(2 * k, 0); dfs(0, -1); unsigned long long mini = 0, maxi = 0; for (int i = 1; i < 2 * k; i++) { maxi += min(sub[i], 2 * k - sub[i]) * pe[i]; mini += (sub[i] % 2) * pe[i]; } cout << mini << " " << maxi << endl; } return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; int n, a[100005], sum, ans, mx = -1; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; mx = max(mx, a[i]); } sort(a + 1, a + n + 1); for (int i = 1; i <= n; i++) { sum += a[i - 1]; if (sum > mx) break; if (sum > a[i]) a[i] = 0; else ans++; } cout << ans; return 0; }
1,300
CPP
List = [] x = 0 y = 0 for i in range(5): row = list(map(int, input().split())) List.append(row) for i in range(5): for j in range(5): if List[i][j] == 1: x = j y = i break counterX = abs(x - 2) counterY = abs(y - 2) print(counterX + counterY)
800
PYTHON3
#include <bits/stdc++.h> int main() { puts("INTERCAL"); }
2,000
CPP
#include <bits/stdc++.h> using namespace std; int n, m, a[110], b[110], F[5010], size[1 << 16], cnt; unsigned long long t1[40010], t2[40010]; const int Z = 20002; inline int getsize(unsigned long long x) { return size[x & 65535] + size[(x >> 16) & 65535] + size[(x >> 32) & 65535] + size[(x >> 48) & 65535]; } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= m; i++) scanf("%d", &b[i]); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { t1[a[i] + b[j] + Z] |= (1ll << i - 1); t2[a[i] + b[j] + Z] |= (1ll << j - 1); } for (int i = 1; i < (1 << 16); i++) size[i] = size[i - (i & -i)] + 1; for (int i = Z - 20000; i <= Z + 20000; i++) if (t1[i]) F[++cnt] = i; int ans = 0; for (int i = 1; i <= cnt; i++) for (int j = 1; j <= cnt; j++) ans = max(ans, getsize(t1[F[i]] | t1[F[j]]) + getsize(t2[F[i]] | t2[F[j]])); cout << ans << endl; return 0; }
2,100
CPP
l=list(map(int,input().split())) l.sort() m=l[0] l[1]=l[1]-m n=l[1]//2 print(str(m)+' '+str(n))
800
PYTHON3
def digits(n): d = [] while n: d.append(n%2) n //= 2 return d s, x = map(int, input().split()) #for i in range(s+1): # if i^(s-i) == x: # print(i, s-i) flag = True if s-x < 0 or (s-x)%2 == 1: print(0) else: a = (s-x) >> 1 AND = digits(a) XOR = digits(x) for i in range(max(len(AND), len(XOR))):# comparing from reverse if i < len(XOR): xi = XOR[i] else: xi = 0 if i < len(AND): ai = AND[i] else: ai = 0 if xi == 1 and ai != 0: print(0) flag = False break if flag: data = XOR.count(1) ans = 2**data if a == 0: ans -= 2 print(ans)
1,700
PYTHON3
queries = int(input()) def checksides(list): for i in range(0, len(list), 2): if list[i] != list[i+1]: return False return True def sides(list): resp = [] for i in range(0, len(list), 2): resp.append(list[i]) return resp def check_areas(sides_list): if(len(sides_list) == 2): return True j = len(sides_list)-1 for i in range(len(sides_list)//2): # print(i, j, "!=", i+1, j-1) if(sides_list[i]*sides_list[j] != sides_list[i+1]*sides_list[j-1]): return False j -= 1 return True for query in range(queries): num_rect = int(input()) sticks = sorted(list(map(int, input().split()))) if checksides(sticks): sidesList = sides(sticks) # print(sidesList) if check_areas(sidesList): print("YES") else: print("NO") else: print("NO")
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long n; long long q; cin >> n >> q; long long nd = (n * n + 1) / 2; long long ans; for (long long i = 0; i < q; i++) { long long x, y; cin >> x >> y; long long cells_before = ((x - 1) / 2) * n; if ((x + y) % 2 == 0) { if (x % 2 == 1) { cells_before += y / 2; } else { cells_before += (y - 1) / 2 + (n + 1) / 2; } ans = cells_before + 1; } else { if (x % 2 == 1) { cells_before += (y - 1) / 2; } else { cells_before += y / 2 + n / 2; } ans = nd + cells_before + 1; } cout << ans << endl; } }
1,200
CPP
#include <bits/stdc++.h> using namespace std; vector<long long int> v[200005], diam[200005], tour, path; long long int n, h[200005], first[200005], stmin[2 * 200005][19], root; long long int vis1[200005], vis2[200005], vis3[200005], vis4[200005]; long long int parent[200005], dist[200005], root1; void height_initialise(long long int x, long long int y) { h[x] = y; vis1[x] = 1; for (int i = 0; i < v[x].size(); i++) { int node = v[x][i]; if (vis1[node] == 0) { height_initialise(node, y + 1); } } } void distanc(long long int x, long long int y) { dist[x] = y; vis3[x] = 1; for (int i = 0; i < v[x].size(); i++) { int node = v[x][i]; if (vis3[node] == 0) { distanc(node, y + 1); } } } void euler(long long int x, long long int y) { vis2[x] = 1; tour.push_back(x); for (int i = 0; i < v[x].size(); i++) { int node = v[x][i]; if (vis2[node] == 0) { euler(node, x); } } if (y != 0) tour.push_back(y); } void euler_tour() { euler(root, 0); for (int i = 0; i < tour.size(); i++) { if (first[tour[i]] == 0) { first[tour[i]] = i; } } } void sparsemin() { long long int i, j, x, y; for (i = 0; i < tour.size(); i++) { stmin[i][0] = tour[i]; } for (j = 1; (1 << j) <= tour.size(); j++) { for (i = 0; i <= tour.size() - (1 << j); i++) { x = stmin[i][j - 1]; y = stmin[i + (1 << (j - 1))][j - 1]; if (h[x] > h[y]) { stmin[i][j] = y; } else { stmin[i][j] = x; } } } } long long int spminquery(long long int l, long long int r) { long long int length, k, x, y; length = r - l + 1; k = log2(length); x = stmin[l][k]; y = stmin[l + length - (1 << k)][k]; if (h[x] < h[y]) { return x; } return y; } int bfs(int x, int flag) { int i, y; queue<int> q; q.push(x); int vis[200005] = {0}; while (q.size()) { x = q.front(); q.pop(); vis[x] = 1; for (int i = 0; i < v[x].size(); i++) { int node = v[x][i]; if (vis[node] == 0) { q.push(node); if (flag == 1) { parent[node] = x; } } } y = x; } return y; } void diameter() { int x, y, z, i, j; x = bfs(1, 0); y = bfs(x, 1); root1 = y; z = y; while (parent[z] != 0) { path.push_back(z); z = parent[z]; } path.push_back(z); reverse(path.begin(), path.end()); for (i = 0; i < path.size(); i++) { vis4[path[i]] = 1; diam[i].push_back(path[i]); } root = x; for (i = 0; i < path.size(); i++) { queue<int> q; q.push(path[i]); while (q.size()) { x = q.front(); q.pop(); vis4[x] = 1; for (j = 0; j < v[x].size(); j++) { int node = v[x][j]; if (vis4[node] == 0) { q.push(node); diam[i].push_back(node); } } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; long long int i, j, x, y, z, l, r, q, ans = 0; vector<pair<long long int, long long int> > an; for (i = 0; i < n - 1; i++) { cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } diameter(); height_initialise(root, 0); euler_tour(); sparsemin(); distanc(root, 0); for (i = 0; i < path.size() / 2; i++) { for (j = diam[i].size() - 1; j > 0; j--) { x = root1; y = diam[i][j]; an.push_back(make_pair(x, y)); l = first[x]; r = first[y]; z = spminquery(min(l, r), max(l, r)); z = dist[x] + dist[y] - 2 * dist[z]; ans += z; } } x = i; for (i = x; i < path.size(); i++) { for (j = diam[i].size() - 1; j > 0; j--) { x = root; y = diam[i][j]; an.push_back(make_pair(x, y)); l = first[x]; r = first[y]; z = spminquery(min(l, r), max(l, r)); z = dist[x] + dist[y] - 2 * dist[z]; ans += z; } } for (i = path.size() - 1; i > 0; i--) { x = root; y = diam[i][0]; an.push_back(make_pair(x, y)); l = first[x]; r = first[y]; z = spminquery(min(l, r), max(l, r)); z = dist[x] + dist[y] - 2 * dist[z]; ans += z; } cout << ans << endl; for (i = 0; i < an.size(); i++) { cout << an[i].first << " " << an[i].second << " " << an[i].second << "\n"; } return 0; }
2,400
CPP
for _ in range(int(input())): n=int(input()) k=list(map(int,input().split())) k.remove(max(k)) l=max(k) print(min(l-1,len(k)-1))
900
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 500000 + 123; const int INF = 1e9 + 123; int n; vector<pair<int, int> > V[maxn]; void ReadData() { cin >> n; for (int i = 2, _b = n; i <= _b; ++i) { int p; char c; cin >> p >> c; int x = c - 'a'; V[p].push_back(pair<int, int>(i, x)); } } int mask[maxn], tin[maxn], tout[maxn], sta[maxn]; int level[maxn], chil[maxn], timer; void preDfs(int u) { tin[u] = ++timer; sta[timer] = u; chil[u] = 1; for (auto x : V[u]) { int v = x.first, c = x.second; mask[v] = mask[u] ^ (1 << c); level[v] = level[u] + 1; preDfs(v); chil[u] += chil[v]; } tout[u] = timer; } int res[maxn]; int dp[1 << 24]; vector<int> rem; void pushMask(int mask, int val) { if (dp[mask] == -INF) rem.push_back(mask); dp[mask] = max(dp[mask], val); } void resetRem() { for (int x : rem) dp[x] = -INF; rem.clear(); } void dfs(int u, bool keep) { int bigChil = -1; for (auto x : V[u]) { if (bigChil == -1 || chil[bigChil] < chil[x.first]) bigChil = x.first; } res[u] = 0; for (auto x : V[u]) { int v = x.first; if (v == bigChil) continue; dfs(v, 0); res[u] = max(res[u], res[v]); } if (bigChil != -1) dfs(bigChil, 1); if (bigChil != -1) res[u] = max(res[u], res[bigChil]); for (auto x : V[u]) { int v = x.first; if (v == bigChil) continue; for (int i = tin[v], _b = tout[v]; i <= _b; ++i) { int z = sta[i]; res[u] = max(res[u], level[z] + dp[mask[z]] - 2 * level[u]); for (int p = 0, _n = 23; p < _n; ++p) { res[u] = max(res[u], level[z] + dp[mask[z] ^ (1 << p)] - 2 * level[u]); } } for (int i = tin[v], _b = tout[v]; i <= _b; ++i) { int z = sta[i]; pushMask(mask[z], level[z]); } } res[u] = max(res[u], dp[mask[u]] - level[u]); for (int p = 0, _n = 23; p < _n; ++p) res[u] = max(res[u], dp[mask[u] ^ (1 << p)] - level[u]); pushMask(mask[u], level[u]); if (!keep) resetRem(); } void Process() { preDfs(1); for (int x = 0, _n = 1 << 23; x < _n; ++x) dp[x] = -INF; mask[1] = 0; dfs(1, 0); for (int i = 1, _b = n; i <= _b; ++i) cout << res[i] << " "; cout << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ReadData(); Process(); return 0; }
2,900
CPP
n=int(input()) l=list(map(int,input().split())) r=list(map(int,input().split())) a=[[l[i]+r[i],i] for i in range(n)] a.sort() #print(a) candies=[0 for i in range(n)] if(a[0][0] != 0): print('NO') exit() else: candies[a[0][1]] = n - a[0][0] for i in range(1,n): if(a[i][0] != a[i-1][0] and a[i][0]!=i): print('NO') # print(i,a[i]) exit() # print(a[i][1],n-a[i][0],i) candies[a[i][1]] = n - a[i][0] #print(candies) for i in range(n): l1=0 r1=0 for j in range(i): if(candies[j]>candies[i]): l1+=1 for j in range(i+1,n): if(candies[j]>candies[i]): r1+=1 # print(l1,r1) if(l1 != l[i] or r1 != r[i]): print('NO') exit() print('YES') print(*candies)
1,500
PYTHON3
a=input() s=a.count('4')+a.count('7') if s==4 or s==7: print('YES') else: print("NO")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int nums[n]; for (int i = 0; i < n; i++) { cin >> nums[i]; } for (int i = 0; i < n - 1; i++) { if (nums[i] < 0) { cout << "NO"; return 0; } if (nums[i] % 2 == 1) { nums[i + 1] -= 1; } } if (nums[n - 1] >= 0 && nums[n - 1] % 2 == 0) { cout << "YES"; } else { cout << "NO"; } return 0; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 300000; const int MAXM = 200000; const int INF = 1000000010; const long long int MOD = 1000000007; const long long int P = 31; const long double EPS = 1e-6; long long int N, K; long long int ans = 0; int main() { ios_base::sync_with_stdio(false); scanf("%I64d %I64d", &N, &K); if (2 * K >= N) { ans = N * (N - 1) / 2; } else { for (long long int i = 1; i <= K; i++) ans += 2 * (N - 2 * i) + 1; } printf("%I64d", ans); return 0; }
1,200
CPP
number = int(input()) for i in range(1,41): term = int((i*i*i + 3*i*i + 2*i)/6) if term>number: break print(i-1)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; if (n == 1) { cout << -1 << "\n"; return 0; } cout << n << " " << (n + 1) << " " << (n * (n + 1)) << "\n"; return 0; }
1,500
CPP
x = int(input()) if (3*x) & (3*x+5) < 5: print(1) else: print(0)
2,400
PYTHON3
t=int(input()) x=0 while(t>0): t=t-1 k=input() if(k=='++X' or k=='X++'): x=x+1 if(k=='--X' or k=='X--'): x=x-1 print(x)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; bool isMatch(string s1, string s2) { int pos = 0; for (int i = 0; i < (int)s2.length(); i++) if (s1[pos] == s2[i]) ++pos; if (pos == s1.length()) return true; return false; } int main() { string s; cin >> s; if (isMatch("hello", s)) cout << "YES"; else cout << "NO"; return 0; }
1,000
CPP
import itertools sum_to_sockets = [ (0, 1), (2, 3), (0, 2), (1, 3), (0, 3), (1, 2) ] sums = [] for _ in range(3): sums += list(map(int, input().split())) for seq in itertools.permutations(range(1, 10), r=4): if all([seq[sum_to_sockets[i][0]] + seq[sum_to_sockets[i][1]] == sums[i] for i in range(6)]): print(str(seq[0]) + " " + str(seq[1])) print(str(seq[2]) + " " + str(seq[3])) break else: print(-1)
1,000
PYTHON3