solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
def prime(n): for i in range(2,int(n**(1.0/2.0))+1): if n%i==0: return False return True n = int(input()) for i in range(1, 1001): if prime(n*i+1)==False: print(i) break
800
PYTHON3
n, k = map(int, input().split()) a = list(map(int, input().split())) mn = min(a) if (max(a) - mn > k): print('NO') else: print('YES') for i in range(n): arr = [1]*mn arr += [1+i for i in range(a[i]-mn)] print(' '.join(map(str, arr)))
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; long double n, a[100004], mx, sum; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i], mx = max(mx, a[i]), sum += a[i]; cout << (long long int)max(ceil(sum / (n - 1)), mx) << endl; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; long long n, k, x; int main(void) { cin >> n >> k; long long cont = 0; vector<long long> v; for (int i = 0; i < n; ++i) { cin >> x; v.push_back(x); if (cont + v.size() >= k) { for (int j = 0; j < v.size(); ++j) { cont++; if (cont == k) { cout << v[j] << "\n"; return 0; } } } else cont += v.size(); } return 0; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; int v[105]; int aux[105]; int leftmost[105]; int path[105][105]; int askEdge(int a, int b) { cout << 1 << ' ' << a << ' ' << b << endl; int ans; cin >> ans; return ans; } int askPath(int a, int l, int r) { if (l > r) return 0; cout << 2 << ' ' << a << ' ' << r - l + 1 << ' '; for (int i = l; i <= r; i++) { cout << v[i] << ' '; } cout << endl; int ans; cin >> ans; return ans; } void printAnswer(int n) { cout << 3 << endl; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << path[i][j]; } cout << endl; } cout << endl; } void mergeSort(int L, int R) { if (L >= R) return; int m = (L + R) >> 1; mergeSort(L, m); mergeSort(m + 1, R); int l = L, r = m + 1, ptr = L; while (l <= m && r <= R) { if (askEdge(v[l], v[r])) aux[ptr++] = v[l++]; else aux[ptr++] = v[r++]; } while (l <= m) aux[ptr++] = v[l++]; while (r <= R) aux[ptr++] = v[r++]; for (int i = L; i <= R; i++) { v[i] = aux[i]; } } void solve() { memset(v, 0, sizeof v); memset(aux, 0, sizeof aux); memset(path, 0, sizeof path); memset(leftmost, -1, sizeof leftmost); int n; cin >> n; iota(v, v + n, 0); mergeSort(0, n - 1); int j = n - 1; for (int i = n - 1; i >= 0; i--) { j = min(j, i); while (askPath(v[i], 0, j - 1)) { j--; } if (j == i) { // todo mundo depois de i pode ir pra todo mundo for (int k = j; k < n; k++) { if (leftmost[k] != -1) break; leftmost[k] = j; } } } for (int i = 0; i < n; i++) { for (int j = leftmost[i]; j < n; j++) { path[v[i]][v[j]] = 1; } } printAnswer(n); int ok; cin >> ok; if (ok == -1) exit(0); } int main() { int tt; cin >> tt; while (tt--) { solve(); } return 0; }
2,700
CPP
#include <bits/stdc++.h> using namespace std; string FILE_NAME = "testcase.436A"; string NAME; string itos(int n) { stringstream ss; ss << n; return ss.str(); } vector<pair<pair<int, int>, int> > candy; bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) { if (a.second != b.second) { return a.second > b.second; } if (a.first.second != b.first.second) return a.first.second < b.first.second; return a.first.first < b.first.first; } bool eat[2002]; int eat_candy(int prev, int n, int curr_h) { memset(eat, false, sizeof(eat)); while (true) { bool update = false; for (int i = 0; i < (n); i++) { int t = candy[i].first.first; int h = candy[i].first.second; int m = candy[i].second; if (t != prev && !eat[i] && curr_h >= h) { eat[i] |= true; prev = t; curr_h += m; update |= true; break; } } if (!update) break; } int res = (int)count(eat, eat + n, true); return res; } int main() { ios_base::sync_with_stdio(0); int n, x; cin >> n >> x; candy.clear(); candy.resize(n); for (int i = 0; i < (n); i++) { int t, h, m; cin >> t >> h >> m; candy[i].first.first = t; candy[i].first.second = h; candy[i].second = m; } sort(candy.begin(), candy.end(), cmp); int res = eat_candy(0, n, x); res = max(res, eat_candy(1, n, x)); cout << res << endl; return 0; }
1,500
CPP
#include <bits/stdc++.h> using namespace std; const int MAX = 70005; unordered_map<string, bool> ok; unordered_map<string, int> m; vector<string> subs[MAX]; bool comp(string a, string b) { return a.length() < b.length(); } char s2[15]; int main() { int n; cin >> n; m.reserve(1024); m.max_load_factor(0.25); ok.reserve(1024); ok.max_load_factor(0.25); for (int i = 0; i < n; i++) { scanf("%s", s2); string second = s2; int l = 9; ok.clear(); for (int j = 0; j < l; j++) { for (int k = 1; k <= l - j; k++) { string sub = second.substr(j, k); if (!ok[sub]) { ok[sub] = true; subs[i].push_back(second.substr(j, k)); m[second.substr(j, k)] += 1; } } } } for (int i = 0; i < n; i++) { sort(subs[i].begin(), subs[i].end(), comp); } for (int i = 0; i < n; i++) { int minlen = 900; string ans = ""; bool ok = false; for (string j : subs[i]) { if (ok) { break; } if (m[j] == 1) { for (char i : j) { printf("%c", i); } printf("\n"); ok = true; } } } }
1,600
CPP
n,k=map(int,input().split()) a=list(map(int,input().split())) a.sort() a.append(a[-1]+1) if k==0: if a[0]==1: print(-1) else: print(1) elif a[k]-a[k-1]<1: print(-1) else: print(a[k-1])
1,200
PYTHON3
n = int(input()) for i in range(500): if i * (i + 1) // 2 == n: print('YES') exit(0) print('NO')
800
PYTHON3
n=int(input()) aans=0 for i in range(n): l=list(map(int,input().split())) if l.count(1)>1: aans+=1 print(aans)
800
PYTHON3
try: for _ in range(int(input())): n=int(input()) a=[int(i) for i in input().split()] b=[int(i) for i in input().split()] a1=min(a) b1=min(b) m=0 for i in range(n): d1=a[i]-a1 d2=b[i]-b1 m+=max(d1,d2) print(m) except: pass
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int read() { int x = 0, f = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { x = (x << 3) + (x << 1) + (c ^ 48); c = getchar(); } return x * f; } const int maxn = 3e6 + 10, mod = 998244353; int Pow(int x, int p) { int r = 1; while (p) { if (p & 1) r = 1LL * x * r % mod; x = 1LL * x * x % mod; p >>= 1; } return r; } int rev[maxn], g[maxn], inv; void init(int len) { int z = __builtin_ctz(len); for (int i = 0; i < len; ++i) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << z - 1); int G = Pow(3, (mod - 1) / len); g[0] = 1; for (int i = 1; i <= len; ++i) g[i] = 1LL * g[i - 1] * G % mod; inv = Pow(len, mod - 2); } void DFT(int *a, int len, int fg) { for (int i = 0; i < len; ++i) if (rev[i] > i) swap(a[rev[i]], a[i]); for (int i = 2; i <= len; i <<= 1) for (int j = 0; j < len; j += i) for (int k = 0; k < (i >> 1); ++k) { int y = 1LL * (fg ? g[len / i * k] : g[len - len / i * k]) * a[j + k + (i >> 1)] % mod; int x = a[j + k]; a[j + k] = (x + y) % mod; a[j + k + (i >> 1)] = (x - y) % mod; } if (!fg) for (int i = 0; i < len; ++i) a[i] = 1LL * a[i] * inv % mod; } int a[maxn], A[maxn], ans[maxn]; int main() { int n = read(), m = read(); for (int i = 1; i <= n; ++i) { int x = read(); a[x]++; A[x]++; } a[0] = 1; int len = 1; while (len <= m + m) len <<= 1; init(len); DFT(a, len, 1); for (int i = 0; i < len; ++i) a[i] = 1LL * a[i] * a[i] % mod; DFT(a, len, 0); for (int i = 0; i < len; ++i) a[i] = (a[i] + mod) % mod; int tot = 0; int fg = 1; for (int i = 1; i <= m; ++i) if (A[i] && a[i] == 2) ans[++tot] = i; else if (!A[i] && a[i]) fg = 0; if (!fg) { printf("NO\n"); return 0; } printf("YES\n"); cout << tot << endl; for (int i = 1; i <= tot; ++i) printf("%d ", ans[i]); return 0; }
2,800
CPP
import math '''A=int(input()) B=int(input()) if A%2==0: a=A else: a=A+1 if B%2==0: b=B else: b=B+1 print(int((a-b)/2))''' '''a=int(input()) b=int(input()) c=int(input()) d=[a,b,c] def qsort1(list): if list == []: return [] else: pivot = list[0] lesser = qsort1([x for x in list[1:] if x < pivot]) greater = qsort1([x for x in list[1:] if x >= pivot]) return lesser + [pivot] + greater d=qsort1(d) print(d[1])''' '''a=int(input()) nums=[] for i in range(1,a+1): if a% i==0 and (math.sqrt(i)).is_integer(): nums.append(i) print(nums[-1])''' '''n=int(input()) a='-++-' b='--+' if n%4 == 0: print(a*int(n//4)) if n% 4 == 3: print(b+(a*int(n//4))) else: print("IMPOSSIBLE")''' '''s = input() svet = ['N','W','E','S'] ch ='' ns = 0 we = 0 for i in range(len(s)): if s[i] not in svet: ch += s[i] else: if s[i] == 'N': ns += int(ch) if s[i] == 'S': ns -= int(ch) if s[i] == 'W': we -= int(ch) if s[i] == 'E': we += int(ch) ch = '' print(abs(ns), 'N' if ns > 0 else 'S', abs(we), 'E' if we > 0 else 'W', sep='')''' '''n,m = map(int,input().split()) a=n*m b=[] c=[] s=0 for i in range (n): i=input().split() if len(i)>m: break else: b.append(i) z=input() for i in range (n): i=input().split() if len(i)>m: break else: c.append(i) for i in range(n): for j in range(m): if b[i][j]==c[i][j]: s+=1''' '''otv=open('buildingin.txt').read().splitlines() n=int(otv[0][0]) k=int(otv[0][2]) x=int(otv[0][4]) y=int(otv[0][6]) q=int(otv[1]) g=otv[2] a=g.split() for i in range(len(a)): a[i]=int(a[i])''' '''x1=int(input()) y1=int(input()) x2=int(input()) y2=int(input()) x=int(input()) y=int(input()) x3=x1 y3=y2 x4=x2 y4=y1 b=[x,y] c=[[x,y]] a = [[x1,y1],[x2,y2],[x3,y3],[x4,y4]] for i in range(len(a)-1): if b[0]-a[i][0]and b[1]-a[i][1]>b[0]-a[i+1][0]and b[1]-a[i+1][1]: c.pop(0) c.append(i) print(c)''' '''m,n,a=map(int,input().split()) s=m//a k=n//a if m%a != 0: s+=1 if n%a !=0: k+=1 g=s*k print(g)''' '''n=int(input()) for i in range(n): a=input() if len(a)>10: f=a[0] h=len(a)-2 end=a[len(a)-1] print(f+str(h)+end) else: print(a)''' '''n,k=map(int,input().split()) a=list(map(int, input().split(maxsplit=n))) s=0 for i in a: if i>0: if i >= a[k-1]: s+=1 print(s)''' '''a=input().lower() b=["a","e","o","u","y","i"] a=filter(lambda x: x not in b,a) a=".".join(a) print("."+a)''' '''n=int(input()) b=[] c=[1] s=0 for i in range(n): a=input().split() b.append(a) for i in range(len(b)): print(b) b[i]=list(filter(lambda x: x in c,b[i])) print(b[i]) if len(b[i])>=2: s+=1 print(s)''' '''a=int(input()) b=0 c=0 while b<a: if input().count("1")>1: c+=1 b+=1 print(c)''' '''m,n=list(map(int,input().split())) e=m*n w=2*1 print(e//w)''' n=int(input()) x=0 b=[] for i in range(n): a=input() b.append(a) for i in b: if i == "X++" or i== "++X": x+=1 if i == "--X" or i=="X--": x-=1 print(x)
800
PYTHON3
g=0 s=input().lower() k=input().lower() for i in range(len(s)): if (s[i]==k[i]): g=0 elif(s[i]>k[i]): g=1 break elif(s[i]<k[i]): g=-1 break print(g)
800
PYTHON3
n=int(input()) ans=0 if n%2==0: k=n//2 i=0 ans=15*k+5+4+5*k+1 print(ans) while i<k: t=i*5 print(t,0) print(t+1,0) print(t+2,0) print(t+3,0) print(t+4,0) print(t+1,-1) print(t+3,-1) print(t+1,1) print(t+3,1) print(t+1,2) print(t+2,2) print(t+3,2) print(t+1,-2) print(t+2,-2) print(t+3,-2) i+=1 print(-1,0) print(-1,1) print(-1,2) print(-1,3) print(-1,4) for i in range(0,k*5+1): print(i,4) print(k*5,2) print(k*5,1) print(k*5,0) print(k*5,3) else: k=n//2 i=0 ans=15*k+19+k*5+1+5 print(ans) while i<k: t=i*5 print(t,0) print(t+1,0) print(t+2,0) print(t+3,0) print(t+4,0) print(t+1,-1) print(t+3,-1) print(t+1,1) print(t+3,1) print(t+1,2) print(t+2,2) print(t+3,2) print(t+1,-2) print(t+2,-2) print(t+3,-2) i+=1 print(-1,0) print(-1,1) print(-1,2) print(-1,3) print(-1,4) print(-1,5) print(-1,6) print(-1,7) print(0,7) print(1,7) print(1,6) print(1,5) print(-2,7) print(-3,7) print(-3,8) print(-3,9) print(-2,9) print(-1,9) print(-1,8) for i in range(1,k*5+2): print(i,4) print(k*5+1,2) print(k*5+1,1) print(k*5+1,0) print(k*5+1,3) if(k==0): print(0,0) else: print(5*k,0)
1,500
PYTHON3
s=input() a=[] t='' for i in range(len(s)): if s[i]=='h': if a.count(s[i])==0: a.append(s[i]) elif s[i]=='e': if a.count(s[i])==0 and a.count('h')==1: a.append(s[i]) elif s[i]=='l': if (a.count(s[i])==0 or a.count(s[i])==1) and a.count('h')==1 and a.count('e')==1: a.append(s[i]) elif s[i]=='o': if a.count(s[i])==0 and a.count('h')==1 and a.count('e')==1 and a.count('l')==2: a.append(s[i]) for i in range(len(a)): t+=a[i] if t=='hello': print ("YES") else: print ("NO")
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[500005], b[500005], cnt[500005], p[500005]; vector<int> v[500005]; int main() { int m, k, n, s, ed, cntt; bool f; scanf("%d%d%d%d", &m, &k, &n, &s); for (int i = 0; i < m; i++) scanf("%d", &a[i]); for (int i = 0; i < s; i++) scanf("%d", &b[i]); for (int i = 0; i < s; i++) cnt[b[i]]++; for (int i = 0; i < m; i++) if (cnt[a[i]]) v[a[i]].push_back(i); ed = -1; f = true; for (int i = 0; i < s; i++) if (v[b[i]].size() < cnt[b[i]]) { f = false; break; } else { p[b[i]] = cnt[b[i]] - 1; ed = max(ed, v[b[i]][p[b[i]]]); } if (!f) printf("-1\n"); else { f = false; for (int i = 0; i < m; i++) { if (i / k + 1 + (m - 1 - ed - max(0, k - (ed - i + 1))) / k >= n) { f = true; printf("%d\n", i % k + max(0, (ed - i + 1 - k))); for (int j = 0; j < i % k; j++) printf("%d ", j + 1); cntt = s; for (int j = i; j <= ed; j++) if ((!cnt[a[j]] || j > v[a[j]][p[a[j]]]) && cntt == k) printf("%d ", j + 1); else if (!cnt[a[j]] || j > v[a[j]][p[a[j]]]) cntt++; printf("\n"); break; } if (cnt[a[i]]) { p[a[i]]++; if (p[a[i]] >= v[a[i]].size()) break; ed = max(ed, v[a[i]][p[a[i]]]); } } if (!f) printf("-1\n"); } return 0; }
1,900
CPP
#include <bits/stdc++.h> #pragma warning(disable : 4996) #pragma comment(linker, "/STACK:1048576") using namespace std; int IT_MAX = 1 << 18; const long long MOD = 1000000009; const int INF = 0x3f3f3f3f; const long long LL_INF = 1234567890123456789ll; const double PI = acos(-1); const double EPS = 1e-8; char u[10005]; int main() { int N; scanf("%d", &N); int ans = 0; while (scanf("%s", u) != EOF) { int c = 0, i; for (i = 0; u[i] != 0; i++) if (u[i] >= 'A' && u[i] <= 'Z') c++; ans = max(ans, c); } return !printf("%d\n", ans); }
800
CPP
import sys def input(): return sys.stdin.readline().rstrip() t = int(input()) answers = [] for _ in range(t): n = int(input()) n = n+1 divide_by = 1 ans = 0 for i in range(100): # if divide_by > n: # break # ans += max((n)//divide_by - 1, 0) if divide_by > n: break if n%divide_by == 0: ans += (n//divide_by - 1) else: ans += n//divide_by divide_by = 2*divide_by answers.append(ans) print(*answers, sep = '\n')
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; const int oo = 0x3f3f3f3f; const long long ooo = 9223372036854775807ll; const int _cnt = 1000 * 1000 + 7; const int _p = 1000 * 1000 * 1000 + 7; const int N = 1001005; const double PI = acos(-1.0); const double eps = 1e-9; int o(int x) { return x % _p; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int lcm(int a, int b) { return a / gcd(a, b) * b; } void file_put() { freopen("filename.in", "r", stdin); freopen("filename.out", "w", stdout); } int p[N], prime[N], phi[N], top, T, X, P, K, pp[N]; long long l, r, mid, ans; void init_prime(int N) { top = 0, phi[1] = 1; for (int i = 2; i <= N; ++i) { if (!p[i]) prime[++top] = i, phi[i] = i - 1; for (int j = 1; j <= top; ++j) { if ((long long)i * prime[j] > N) break; p[i * prime[j]] = 1; if (i % prime[j] == 0) { phi[i * prime[j]] = phi[i] * prime[j]; break; } phi[i * prime[j]] = phi[i] * phi[prime[j]]; } } } void init(int x) { pp[0] = 0; for (int i = 1; i <= top && (long long)prime[i] * prime[i] <= x; i++) if (x % prime[i] == 0) { pp[++pp[0]] = prime[i]; while (x % prime[i] == 0) x /= prime[i]; if (x == 1) return; if (!p[x]) { pp[++pp[0]] = x; return; } } if (x > 1) pp[++pp[0]] = x; } void dfs(int k, int op, long long s, long long n, long long &ans) { if (k > pp[0]) { ans += n / s * op; return; } dfs(k + 1, op, s, n, ans); if (s * pp[k] <= n) dfs(k + 1, -op, s * pp[k], n, ans); } long long Count(long long x) { long long ans = 0; dfs(1, 1, 1, x % P, ans); return x / P * phi[P] + ans; } int main() { scanf("%d", &T); init_prime(N - 5); while (T--) { scanf("%d%d%d", &X, &P, &K), init(P); K += Count(X), l = 1, r = 1e7; while (l <= r) { mid = (l + r) >> 1; if (Count(mid) >= K) ans = mid, r = mid - 1; else l = mid + 1; } printf("%I64d\n", ans); } return 0; }
2,200
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 5; int n, k; int a[maxn], dem[maxn], ans = 0; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; dem[a[i]]++; } int x = (n + 1) / 2; for (int i = 1; i <= k; i++) while (dem[i] >= 2 and x > 0) { x--; dem[i] -= 2; } for (int i = 1; i <= k; i++) if (dem[i] == 1 and x > 0) { x--; dem[i] -= 1; } for (int i = 1; i <= k; i++) ans += dem[i]; cout << n - ans; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; int a[1000][1000]; int w, h; int fastMax(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ y; } int fastMin(int x, int y) { return (((y - x) >> (32 - 1)) & (x ^ y)) ^ x; } int main() { scanf("%d%d", &h, &w); for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) scanf("%d", &a[y][x]); int ans = -1; for (int y1 = 0; y1 < h; y1++) for (int y2 = y1 + 1; y2 < h; y2++) { int m1 = -1, m2 = -1; for (int x = 0; x < w; x++) { const int v = fastMin(a[y1][x], a[y2][x]); if (m1 < v) { m2 = m1; m1 = v; } else if (m2 < v) { m2 = v; } } ans = max(ans, m2); } printf("%d\n", ans); return 0; }
2,100
CPP
# your code goes here st=[] s=input().strip() for i in s: if len(st) and st[-1]==i: st.pop() else: st.append(i) print(''.join(st))
1,400
PYTHON3
a=input() b=input() c=input() a=a+b for i in a: if (i in c)==True: a=a.replace(i,"",1) c=c.replace(i,"",1) if (len(c)==0) and (len(a)==0): print("YES") else: print("NO")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; int a[100]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); if (a[n - 1] == 15) puts("DOWN"); else if (a[n - 1] == 0) puts("UP"); else { if (n == 1) { puts("-1"); return 0; } if (a[n - 1] > a[n - 2]) puts("UP"); else puts("DOWN"); } }
1,100
CPP
#include <bits/stdc++.h> using namespace std; struct cell { int t, w, num; bool operator<(const cell& A) const { return w < A.w; } } a[111111]; int n; long long ans[111111]; long long curm; void doit(int l, int r) { curm = max(curm, (long long)a[r].t); sort(a + l, a + r + 1); long long pos = 0; for (int i = l; i <= r;) { int k = 1; while (i + k <= r && a[i + k].w == a[i].w) ++k; curm += a[i].w - pos; for (int j = 0; j < k; ++j) { ans[a[i + j].num] = curm; } curm += 1 + k / 2; pos = a[i].w; i += k; } curm += a[r].w; } int m; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) { scanf("%d%d", &a[i].t, &a[i].w); a[i].num = i; } for (int i = 0; i < n; i += m) { int r = i + m - 1; if (r >= n) { doit(i, n - 1); break; } doit(i, r); } for (int i = 0; i < n; ++i) { printf("%I64d ", ans[i]); } cout << endl; return 0; }
1,500
CPP
#import sys #sys.stdin = open("input.in","r") #sys.stdout = open("test.out","w") n=int(input()) c=list(map(int,input().split())) c.sort() l=c[-1]-c[0]+1 print(l-n)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; void solve() { long long int n = ({ long long int tito; cin >> tito; tito; }); vector<vector<long long int>> v; map<long long int, vector<long long int>> mm; long long int pos = 0; for (long long int i = 0; i < n; i++) { long long int a = ({ long long int tito; cin >> tito; tito; }); long long int b, c; if (a == 1) { b = ({ long long int tito; cin >> tito; tito; }); c = 0; } else { b = ({ long long int tito; cin >> tito; tito; }); c = ({ long long int tito; cin >> tito; tito; }); } vector<long long int> vv; vv.push_back(a); vv.push_back(b); vv.push_back(c); v.push_back(vv); } vector<long long int> ans; vector<long long int> aa; for (long long int i = 0; i < 5 * 1e5 + 5; i++) { aa.push_back(i); } for (long long int i = 0; i < n; i++) { vector<long long int> vv = v[n - 1 - i]; long long int a = vv[0]; long long int b = vv[1]; long long int c = vv[2]; if (a == 1) { ans.push_back(aa[b]); } else { aa[b] = aa[c]; } } long long int nn = ans.size(); for (long long int i = 0; i < nn; i++) { cout << ans[nn - 1 - i] << " "; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t = 1; while (t--) solve(); return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; template <class T> inline void chkmin(T &a, T b) { if (a > b) a = b; } template <class T> inline void chkmax(T &a, T b) { if (a < b) a = b; } const double pi = 4 * atan(1); struct point { double x, y; point(double _x = 0, double _y = 0) : x(_x), y(_y) {} point operator+(point p) { return point(x + p.x, y + p.y); } point operator-(point p) { return point(x - p.x, y - p.y); } point operator*(double t) { return point(x * t, y * t); } point operator/(double t) { return point(x / t, y / t); } }; inline int sign(double x) { return x < -1e-8 ? -1 : x > 1e-8; } inline double dot(point a, point b) { return a.x * b.x + a.y * b.y; } inline double multi(point a, point b) { return a.x * b.y - a.y * b.x; } inline double norm(point a) { return dot(a, a); } inline double abs(point a) { return sqrt(norm(a)); } inline double arg(point a) { return atan2(a.y, a.x); } inline double dist(point a, point b) { return abs(a - b); } inline point polar(double len, double al) { return point(len * cos(al), len * sin(al)); } inline double angle(point a, point b) { double al; al = dot(a, b) / abs(a) / abs(b); chkmax(al, -1.0); chkmin(al, 1.0); al = acos(al); return al; } double seg(point a, point b, point c) { if (sign(a.y - b.y) == 0) return (c.x - a.x) / (b.x - a.x); else return (c.y - a.y) / (b.y - a.y); } point p[555][5]; pair<double, int> b[5555]; int main() { int N, i, j, ii, jj, cnt, aa, bb, co; double tot, area, s1, s2, s, last, now; scanf("%d", &N); for (i = 0; i < N; i++) for (j = 0; j < 4; j++) scanf("%lf%lf", &p[i][j].x, &p[i][j].y); tot = 0; for (i = 0; i < N; i++) { area = 0; p[i][4] = p[i][0]; for (j = 0; j < 4; j++) area += multi(p[i][j], p[i][j + 1]); if (area < 0) { reverse(p[i], p[i] + 4); p[i][4] = p[i][0]; area = -area; } tot += area; } area = 0; for (i = 0; i < N; i++) for (ii = 0; ii < 4; ii++) { cnt = 0; b[cnt++] = make_pair(0.0, 0); b[cnt++] = make_pair(1.0, 0); for (j = 0; j < N; j++) { if (i == j) continue; for (jj = 0; jj < 4; jj++) { aa = sign(multi(p[i][ii + 1] - p[i][ii], p[j][jj] - p[i][ii])); bb = sign(multi(p[i][ii + 1] - p[i][ii], p[j][jj + 1] - p[i][ii])); if (aa == 0 && bb == 0) { if (dot(p[i][ii + 1] - p[i][ii], p[j][jj + 1] - p[j][jj]) > 0 && j < i) { b[cnt++] = make_pair(seg(p[i][ii], p[i][ii + 1], p[j][jj]), 1); b[cnt++] = make_pair(seg(p[i][ii], p[i][ii + 1], p[j][jj + 1]), -1); } } else if (aa < 0 && bb >= 0) { s1 = multi(p[i][ii] - p[j][jj], p[j][jj + 1] - p[j][jj]); s2 = s1 + multi(p[j][jj + 1] - p[j][jj], p[i][ii + 1] - p[j][jj]); b[cnt++] = make_pair(s1 / s2, -1); } else if (aa >= 0 && bb < 0) { s1 = multi(p[i][ii] - p[j][jj], p[j][jj + 1] - p[j][jj]); s2 = s1 + multi(p[j][jj + 1] - p[j][jj], p[i][ii + 1] - p[j][jj]); b[cnt++] = make_pair(s1 / s2, 1); } } } sort(b, b + cnt); s = 0; last = min(max(b[0].first, 0.0), 1.0); co = b[0].second; for (j = 1; j < cnt; ++j) { now = min(max(b[j].first, 0.0), 1.0); if (!co) s += now - last; co += b[j].second; last = now; } area += multi(p[i][ii], p[i][ii + 1]) * s; } area = fabs(area); printf("%.15lf\n", tot / area); return 0; }
2,700
CPP
for _ in range(int(input())): n, k = map(int, input().split()) if k < n: print(k) else: if (n * k) % (n - 1) == 0: print((n * k) // (n - 1) - 1) else: print((n * k) // (n - 1))
1,200
PYTHON3
#new theatre square import sys def input(): return sys.stdin.readline().rstrip() t = int(input()) answers = [] for _ in range(t): n, m,x ,y = [int(i) for i in input().split()] grid = [] for _ in range(n): grid.append([1 if i == '*' else 0 for i in input()]) a = grid # grid[i][j] == 1 means black ==0 means white freqs = [0 for i in range(m)] #freqs[i-1] contains the number of white segments of length i for i in range(n): current = 0 last = None for j in range(m): if a[i][j] == 1: #black implies break if current!=0: freqs[current-1] += 1 current = 0 else: current += 1 if j == m-1: if current!=0: freqs[current-1] += 1 if 2*x <= y: #sabko single se ans = 0 for i, f in enumerate(freqs): ans += x*f*(i+1) else: ans = 0 for i, f in enumerate(freqs): ans += x*f*((i+1)%2) ans += y*f*((i+1)//2) answers.append(ans) print(*answers, sep = '\n')
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i, j; unordered_map<int, int> mapper; int start = 1, end; vector<int> a; scanf("%d %d", &n, &m); for (i = 1; i <= n; i++) { int banyakMain, durasi; scanf("%d %d", &banyakMain, &durasi); int selisih = (banyakMain * durasi); end = start + selisih - 1; a.push_back(start); a.push_back(end); mapper[start] = i; mapper[end] = i; start = end + 1; } for (i = 1; i <= m; i++) { int time; scanf("%d", &time); int idx = lower_bound(a.begin(), a.end(), time) - a.begin(); int waktu = a[idx]; printf("%d\n", mapper[waktu]); } return 0; };
1,200
CPP
#include <bits/stdc++.h> using namespace std; void kkk() { int n; cin >> n; vector<int> nums(n); int even = 0, odd = 0; for (int i = 0; i < n; i++) { cin >> nums[i]; if (nums[i] % 2) odd++; else even++; } if (even % 2 != odd % 2) cout << "NO\n"; else { if (even % 2 == 0) { cout << "YES\n"; } else { int possible = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int cond1 = abs(nums[j] - nums[i]); int cond2 = abs(nums[j] - nums[i]) % 2; if ((cond1 == 1) && nums[i] % 2 != nums[j] % 2) { cout << "YES\n"; possible = 1; return; } } } cout << "NO\n"; } } } int main() { int t; cin >> t; while (t--) { kkk(); } }
1,100
CPP
import sys import re c = re.compile("\s").split(sys.stdin.readline()) del c[len(c)-1] c = list(map(lambda x: int(x), c)) c[0] = c[0] % 10 vals = re.compile("\s").split(sys.stdin.readline()) del vals[len(vals)-1] vals = list(map(lambda x: int(x), vals)) vals.reverse() sum, pow = 0 , 1 for i in range(c[1]): sum = (sum%10 + (pow*(vals[i]%10))%10)%10 pow = ((pow%10)*c[0])%10 if sum % 2 == 0 : print("even") else: print("odd")
900
PYTHON3
for _ in range(int(input())): n=int(input()) a=list(map(int,input().split())) l=list(map(int,input().split())) s=[] for j in range(n): if l[j]==0: s.append(a[j]) s=sorted(s,reverse=True) k=0 for j in range(n): if l[j]==0: a[j]=s[k] k+=1 print(*a)
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; template <typename T, typename S> inline bool REMIN(T &a, const S &b) { return a > b ? a = b, 1 : 0; } template <typename T, typename S> inline bool REMAX(T &a, const S &b) { return a < b ? a = b, 1 : 0; } long long toint(const string &s) { stringstream ss; ss << s; long long x; ss >> x; return x; } string tostring(int number) { stringstream ss; ss << number; return ss.str(); } int bit(long long x, int pos) { return ((1ll << pos) & x) ? 1 : 0; } long long power(long long base, long long exp, long long c = 1e9 + 7) { if (!exp) return 1; long long r = power(base, exp / 2, c); r = (r * r) % c; if (exp & 1) r = (r * base) % c; return r; } void nope(int dec = 0) { if (!dec) cout << "NO"; else cout << dec; exit(0); } const long double PI = 2 * acos(0); const int INF = 1e9; const int NMAX = 1e5 + 5; const long long MOD = 1000000007; int T, N, M; int a, b, c; string s1, s2; int obst; int vis[int(2e6) + 100]; int cv(int ix) { return ix + 1e6; } int tryout(int gots) { memset(vis, 0, sizeof(vis)); int id = cv(0); for (int(i) = int(0); (i) <= int(s1.length() - 1); ++(i)) { vis[id] = 1; int nexid = id + ((s1[i] == 'L') ? -1 : 1); if (gots && nexid == cv(obst)) continue; id = nexid; } return !vis[id]; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> s1; if (s1.back() == 'R') for (int(i) = int(0); (i) <= int(s1.length() - 1); ++(i)) { if (s1[i] == 'R') s1[i] = 'L'; else s1[i] = 'R'; } if (tryout(0)) { cout << 1; return 0; } int l = 1, r = 1e6, ans = 0; while (l <= r) { int m = (l + r) >> 1; obst = m; if (tryout(1)) { ans = m; l = m + 1; } else r = m - 1; } cout << ans; return 0; }
2,200
CPP
n = int(input()) sum1 = 0 max1 = 0 for i in range(0,n): l = [int(x) for x in input().split(' ')] a = l[0] b = l[1] sum1 = sum1 - a + b if(max1 < sum1): max1 = sum1 print(max1)
800
PYTHON3
import math N=int(input()) for i in range(N): a=list(map(int,input().strip().split())) a.sort(reverse=True) l=a[0] b=a[1] ar=2*(l*b) num=math.floor(ar**0.5) if num>=l and num>=2*b: print(num**2) elif num>=l and num<=2*b: num=2*b print(num**2) elif num<l and num>=2*b: num=l print(num**2) elif num<=l and num<=2*b: if l>2*b: num=l print(num**2) elif l<2*b: num=2*b print(num**2)
800
PYTHON3
for _ in " "*int(input()): p=input;a=int(p());b=[];z=[*range(a)];z1=[*range(0,a+1)];c=list(map(int,p().split())) while(len(sorted(set(z).difference(set(c))))!=0):r=sorted(set(z).difference(set(c)))[0];c[r]=r;b+=[r+1] for i in range(a): if c[i]!=i: r=sorted(set(z1).difference(set(c)))[0] while(i in c):b+=[c.index(i)+1];c[c.index(i)]=r c[i]=i;b+=[i+1] print(len(b));print(*b)
1,900
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m, x, y, z, k, w; char S[10005]; int dp[10005]; int ok[20005]; bool check(int x, int y, int pos) { for (int i = (0); i < (pos); i++) if (S[i + x] != S[i + y]) return 1; return 0; } struct node { char S[4]; } ANS[20005]; bool cmp(const node& a, const node& b) { return strcmp(a.S, b.S) < 0; } int main() { scanf("%s", S); n = strlen(S); dp[n - 2] = 2; dp[n] = -1; dp[n - 3] = 1; for (int i = (n - 4); i >= (5); i--) { if (dp[i + 3] == 1 && check(i, i + 3, 3)) dp[i] += 1; else if (dp[i + 3] == 2 || dp[i + 3] == 3) dp[i] += 1; if (dp[i + 2] == 2 && check(i, i + 2, 2)) dp[i] += 2; else if (dp[i + 2] == 1 || dp[i + 2] == 3) dp[i] += 2; } for (int i = (5); i < (n); i++) { if (dp[i] >= 2) { dp[i] -= 2; ANS[w].S[0] = S[i]; ANS[w].S[1] = S[i + 1]; w++; } if (dp[i] >= 1) { ANS[w].S[0] = S[i]; ANS[w].S[1] = S[i + 1]; ANS[w].S[2] = S[i + 2]; w++; } } sort(ANS, ANS + w, cmp); for (int i = (1); i < (w); i++) if (strcmp(ANS[i].S, ANS[i - 1].S) == 0) ok[i] = 1; x = 0; for (int i = (0); i < (w); i++) if (ok[i] == 0) x++; printf("%d\n", x); for (int i = (0); i < (w); i++) if (ok[i] == 0) printf("%s\n", ANS[i].S); return 0; }
1,800
CPP
from sys import stdin t = int(input()) while t: t += -1 x, y, n = map(int, input().split()) tmp = x * (n // x) ans = -1 if tmp + y <= n: ans = tmp + y else: ans = tmp - (x - y) print(ans)
800
PYTHON3
// Author :: <Hitesh_Saini> #include<bits/stdc++.h> #define __speed() ios_base::sync_with_stdio(false), cin.tie(nullptr); #define dbg(x) cout << "(" << __LINE__ << ": "<< #x << " = " << x << ")\n" #define Yes(x) print((x) ? "Yes" : "No") #define tt int t; for (cin >> t; t--; ) #define f0(i, n) for (i = 0; i < (int)(n); i++) #define f1(i, n) for (i = 1; i <=(int)(n); i++) #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define sz(x) (int)(x.size()) #define EB emplace_back #define PB push_back #define endl "\n" #define S second #define F first using namespace std; using mii = map<int, int>; using pii = pair<int, int>; using ll = int64_t; using vi = vector<int>; using vvi = vector<vi>; using vl = vector<ll>; const int mod = 1e9+7, mxN = 5e6+5, INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3f; template <typename... T> void print(T... args) { ((cout << args << " "), ...), cout << endl; } template <typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& p) { in >> p.F >> p.S; return in; } template <typename T1, typename T2> ostream& operator<<(ostream& ot, pair<T1, T2>& p) { ot << p.F << ' ' << p.S; return ot; } template <typename T1, typename T2> bool cmax(T1& a, T2 b) { if (b > a) { a = b; return true;} return false; } template <typename T1, typename T2> bool cmin(T1& a, T2 b) { if (b < a) { a = b; return true;} return false; } template <typename T> istream& operator>>(istream& in, vector<T>& v) { for (T& x:v) in >> x; return in; } template <typename T> ostream& operator<<(ostream& ot, vector<T>& v) { for (T& x:v) ot << x << ' '; return ot; } void solve() { ll n, m, i, required, sum = 0; cin >> n >> m; vl A(n), pref_max(n); f0(i, n) { cin >> A[i]; sum += A[i]; if (i==0) pref_max[i] = sum; else pref_max[i] = max(sum, pref_max[i-1]); } while (m--) { cin >> required; if (required > pref_max.back() && sum <= 0) { cout << "-1 "; continue; } ll cycles = 0, c = 0; if (required > pref_max.back()) { // cycles exist cycles = (required - pref_max.back() + sum - 1) / sum; c = cycles * n; required -= cycles * sum; } c += lower_bound(all(pref_max), required) - pref_max.begin(); cout << c << ' '; } cout << endl; } signed main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif __speed() tt solve(); }
1,900
CPP
#include <bits/stdc++.h> using namespace std; const int inf = (1LL << 31) - 1; int n, m; int c, h; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; cin >> c; int xmin = inf, ymin = inf; int xmax = -inf, ymax = -inf; for (int i = 1; i <= c; i++) { int x, y; cin >> x >> y; xmin = min(xmin, x - y); xmax = max(xmax, x - y); ymin = min(ymin, x + y); ymax = max(ymax, x + y); } cin >> h; int mn = inf, id; for (int i = 1; i <= h; i++) { int x, y; cin >> x >> y; int cur = -inf; cur = max(cur, abs(x - y - xmin)); cur = max(cur, abs(x + y - ymin)); cur = max(cur, abs(x - y - xmax)); cur = max(cur, abs(x + y - ymax)); if (mn > cur) { mn = cur; id = i; } } cout << mn << "\n" << id; return 0; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int kk; kk = 1; while (kk--) { int n; cin >> n; cin.ignore(); vector<pair<int, int> > par; for (int i = 0; i < n; i++) { int a, b; cin >> a; cin.ignore(); cin >> b; cin.ignore(); par.push_back(make_pair(a, b)); } if (n & 1) { cout << "NO"; continue; } bool flag = true; par.push_back(make_pair(par[0].first, par[0].second)); for (int i = 0; i < n / 2; i++) if ((par[i].first - par[i + 1].first) != (par[i + 1 + n / 2].first - par[i + n / 2].first) || (par[i].second - par[i + 1].second) != (par[i + 1 + n / 2].second - par[i + n / 2].second)) { cout << "NO"; flag = false; break; } if (flag) cout << "YES"; continue; } return 0; }
1,800
CPP
n = int(input()) arr = list(map(int, input().split())) ans = 0; for i in range(1, n): if arr[i-1] == arr[i]: print("Infinite") exit() elif arr[i-1] == 1 and arr[i] == 2: ans += 3 elif arr[i-1] == 1 and arr[i] == 3: ans += 4 elif arr[i-1] == 2 and arr[i] == 1: ans += 3 elif arr[i-1] == 2 and arr[i] == 3: print("Infinite") exit() elif arr[i-1] == 3 and arr[i] == 1: ans += 4 elif arr[i-1] == 3 and arr[i] == 2: print("Infinite") exit() if arr[i] == 2 and arr[i-1] == 1 and i - 2 >= 0 and arr[i-2] == 3: ans -= 1 print("Finite") print(ans);
1,400
PYTHON3
n = int(input()) values = [([None] * (n+1)) for i in range(n+1)] def count(i, j): global values i, j= min(i,j), max(i,j) if values[i][j] is not None: return values[i][j] else: values[i][j] = count(i-1, j) + count(i, j-1) return values[i][j] for i in range(n + 1): values[1][i] = values[i][1] = 1 print(count(n,n))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; long long MOD = pow(10, 9) + 7; int main() { ios::sync_with_stdio(0); cin.tie(0); int test = 1; while (test--) { int r, c; cin >> r >> c; string cake[r]; for (int i = 0; i < r; i++) cin >> cake[i]; int eat = 0; int count_row = 0; for (int i = 0; i < r; i++) { int flag = 0; for (int j = 0; j < c; j++) { if (cake[i][j] == 'S') { flag = 1; break; } } if (!flag) { eat += c; count_row++; } } int count_column = 0; for (int i = 0; i < c; i++) { int flag = 0; for (int j = 0; j < r; j++) { if (cake[j][i] == 'S') { flag = 1; break; } } if (!flag) { eat += r; count_column++; } } cout << eat - count_column * count_row << endl; } }
800
CPP
#include <bits/stdc++.h> using namespace std; int read() { int res = 0, w = 1; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') c = getchar(), w = -1; while (isdigit(c)) res = (res << 1) + (res << 3) + c - '0', c = getchar(); return res * w; } const int N = 3e5 + 10; const long long INF = 1e16; long long tag[N << 2]; void build(int t, int l, int r) { tag[t] = (l == 0) ? 0 : INF; if (l == r) return; int mid = (l + r) >> 1; build((t << 1), l, mid); build((t << 1 | 1), mid + 1, r); } void modify(int t, int l, int r, int pos, long long vl) { tag[t] = min(tag[t], vl); if (l == r) return; int mid = (l + r) >> 1; if (pos <= mid) modify((t << 1), l, mid, pos, vl); else modify((t << 1 | 1), mid + 1, r, pos, vl); } long long query(int t, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) { return tag[t]; } int mid = (l + r) >> 1; long long res = INF; if (ql <= mid) res = min(res, query((t << 1), l, mid, ql, qr)); if (qr > mid) res = min(res, query((t << 1 | 1), mid + 1, r, ql, qr)); return res; } char s[N]; int n, k; int main() { n = read(), k = read(); scanf("%s", s + 1); build(1, 0, n); for (int i = 1; i <= n; i++) { if (s[i] == '1') { modify(1, 0, n, min(n, i + k), query(1, 0, n, max(1, i - k) - 1, n) + i); } modify(1, 0, n, i, query(1, 0, n, i - 1, i - 1) + i); } cout << query(1, 0, n, n, n) << endl; return 0; }
2,100
CPP
t = int(input()) for i in range(t): k = int(input()) k = (k+1)/2 print(int(4*(k-1)*(k)*(2*k-1)/3))
1,000
PYTHON3
import sys input = sys.stdin.readline n=int(input()) print(2*n**2-2*n+1)
800
PYTHON3
def main(): n = int(input()) for i in range(n): k, n, a, b = map(int, input().split()) if k // b < n or k // b == n and k % b == 0: print(-1) else: l = -1 r = n + 1 for w in range(30): copy = k bet = (l + r) // 2 copy -= bet * a if b * (n - bet) >= copy: r = bet else: l = bet if l < 0: l = 0 print(l) main()
1,400
PYTHON3
n, k = map(int, input().split()) t = list(input()) i, m = 0, n // 2 if k > m: k = m + ((m + k) & 1) while k and i < n - 1: if t[i] == '4' and t[i + 1] == '7': k -= 1 if i & 1 == 0: t[i + 1] = '4' else: t[i] = '7' i -= 2 i += 1 print(''.join(t))
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 410; const int M = 200100; const int inf = 1000000009; int binx[N], biny[N], topx = 0, topy = 0, n, m; class Rectangle { public: Rectangle() {} int x1, x2, y1, y2; } rec[N]; class Network_Flow { public: Network_Flow() { tot = 1; } void add(int x, int y, int fl) { e[++tot].ne = he[x]; he[x] = tot; e[tot].to = y; e[tot].fl = fl; } void addedge(int x, int y, int fl) { add(x, y, fl); add(y, x, 0); } bool bfs() { int i, j, x, y, lp = 1, rp = 0; q[++rp] = S; for (i = 1; i <= n; i++) { dep[i] = 0; } dep[S] = 1; while (lp <= rp) { x = q[lp++]; for (i = he[x]; i; i = e[i].ne) { y = e[i].to; if ((e[i].fl) && (!dep[y])) { dep[y] = dep[x] + 1; q[++rp] = y; } } } return dep[T] != 0; } int dfs(int x, int flow) { int i, y, cur = 0, used = 0; if ((x == T) || (flow == 0)) { return flow; } for (i = he[x]; i; i = e[i].ne) { y = e[i].to; if ((e[i].fl) && (dep[y] == dep[x] + 1)) { cur = dfs(y, min(e[i].fl, flow - used)); used += cur; e[i].fl -= cur; e[i ^ 1].fl += cur; if (used == flow) { break; } } } if (!used) { dep[x] = -1; } return used; } int dinic() { int ans = 0; while (bfs()) { ans += dfs(S, inf); } return ans; } int S, T, n; private: class Edge { public: int ne, to, fl; } e[M]; int he[N], tot, dep[N], q[N]; } solver; int main() { int i, j, k; scanf("%d%d", &n, &m); if (m == 0) { printf("0\n"); return 0; } topx = 0; topy = 0; for (i = 1; i <= m; i++) { scanf("%d%d%d%d", &rec[i].x1, &rec[i].y1, &rec[i].x2, &rec[i].y2); binx[++topx] = rec[i].x1; binx[++topx] = rec[i].x2 + 1; biny[++topy] = rec[i].y1; biny[++topy] = rec[i].y2 + 1; } sort(binx + 1, binx + topx + 1); sort(biny + 1, biny + topy + 1); solver.S = topx + topy + 2 * m + 1; solver.T = solver.S + 1; solver.n = solver.T; for (i = 1; i < topx; i++) { solver.addedge(solver.S, i, binx[i + 1] - binx[i]); } for (i = 1; i < topy; i++) { solver.addedge(topx + i, solver.T, biny[i + 1] - biny[i]); } for (i = 1; i <= m; i++) { solver.addedge(topx + topy + i * 2 - 1, topx + topy + i * 2, inf); for (j = 1; j <= topx; j++) { if ((binx[j] >= rec[i].x1) && (binx[j] <= rec[i].x2)) { solver.addedge(j, topx + topy + i * 2 - 1, inf); } } for (j = 1; j <= topx; j++) { if ((biny[j] >= rec[i].y1) && (biny[j] <= rec[i].y2)) { solver.addedge(topx + topy + i * 2, topx + j, inf); } } } printf("%d\n", solver.dinic()); return 0; }
2,500
CPP
t = int(input()) while t>0: n = int(input()) map = list() for i in range(n): x, y = input().split() p = [int(x), int(y)] map.append(p) map.sort(key=lambda p : (p[0], p[1])) p=[0,0] i=0 ch='' ok=True while i<n: if map[i][0] > p[0]: for j in range(map[i][0]-p[0]): ch += 'R' p[0]= map[i][0] if map[i][0]== p[0] and map[i][1]>p[1]: for j in range(map[i][1]-p[1]): ch += 'U' p[1] = map[i][1] if map[i][0] == p[0] and map[i][1] == p[1]: i += 1 else : print('NO') ok=False break if ok: print('YES') print(ch) t -= 1
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); vector<int> x(3); for (int i = 0; i < 4; i++) cin >> x[i]; vector<int> c = {x[0] + x[2], x[1] + x[3]}; int n = c[0] + c[1]; for (int p = 0; p < 2; p++) { int diff = c[1 ^ p] - c[p]; if (diff != 0 && diff != -1) continue; int r = x[3 * p]; int s = x[3 * (1 ^ p)]; vector<bool> used(n, false); bool works = true; int next_use = 1; for (int i = 0; i < r; i++) { if (2 * i >= n) { works = false; break; } used[2 * i] = true; next_use = 2 * i + 3; } if (!works) continue; for (int i = 0; i < s; i++) { int j = next_use + 2 * i; if (j >= n) { works = false; break; } used[j] = true; } if (!works) continue; cout << "YES" << '\n'; for (int i = 0; i < n; i++) { int which = (1 ^ p ^ i) & 1; cout << ((which + 1) ^ (2 * (int)used[i])) << ' '; } cout << '\n'; return 0; } cout << "NO" << '\n'; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-10; const double PI = acos(-1.0); template <class T> T gcd(T a, T b) { for (T c; b; c = a, a = b, b = c % b) ; return a; } template <class T> void out(const vector<T> &a) { for (int i = 0; i < ((int)(a).size()); ++i) cout << a[i] << " "; cout << endl; } int countbit(int n) { return n == 0 ? 0 : 1 + countbit(n & (n - 1)); } const int d8[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1}}; const int d4[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; inline int dcmp(double x) { return (x > EPS) - (x < -EPS); } inline double cross(const complex<double> &a, const complex<double> &b) { return (conj(a) * b).imag(); } inline double dot(const complex<double> &a, const complex<double> &b) { return (conj(a) * b).real(); } const int N = 100000; const int MOD = 1000000000 + 9; int f[N]; int find(int x) { if (f[x] != f[f[x]]) f[x] = find(f[x]); return f[x]; } int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; ++i) f[i] = i; int ret = 0; for (int i = 0; i < m; ++i) { int x, y; scanf("%d %d", &x, &y); --x; --y; int rx = find(x), ry = find(y); if (rx != ry) { f[ry] = rx; } else { ret = (ret * 2 + 1) % MOD; } printf("%d\n", ret); } return 0; }
2,300
CPP
#include <bits/stdc++.h> using namespace std; inline int read(int f = 1, int x = 0, char ch = ' ') { while (!isdigit(ch = getchar())) if (ch == '-') f = -1; while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); return f * x; } const int N = 2e3 + 5, P = 998244353; int n, a[N], b[N], vis[N << 1], to[N << 1], cnt[4], d[N << 1]; long long S[N][N], A[N][N], C[N][N], f[N], g[N], h[N]; void dfs(int x, int y) { vis[x] = 1; if (to[x]) { if (!vis[to[x]]) dfs(to[x], y); else ++cnt[0]; } else if (x > n || y > n) ++cnt[(x > n) * 2 + (y > n)]; } void solve(long long *f, int n) { for (int i = 0; i <= n; ++i) { f[i] = 0; for (int j = i; j <= n; ++j) f[i] = (f[i] + C[n][j] * S[j][i] % P * A[n + cnt[3] - j][n - j] % P) % P; } for (int i = 0; i <= n; ++i) { long long w = 0; for (int j = i, k = 1; j <= n; ++j, k = P - k) w = (w + k * C[j][i] % P * f[j] % P) % P; f[i] = w; } } int main() { n = read(); for (int i = 1; i <= n; ++i) a[i] = read(), a[i] = a[i] ? a[i] : i + n, vis[i] = 1; for (int i = 1; i <= n; ++i) b[i] = read(), b[i] = b[i] ? b[i] : i + n, vis[i + n] = 1; for (int i = 1; i <= n; ++i) { vis[a[i]] = vis[b[i]] = 0; if (a[i] <= n || b[i] <= n) to[a[i]] = b[i], ++d[b[i]]; } for (int i = 1; i <= n << 1; ++i) if (!vis[i] && !d[i]) dfs(i, i); for (int i = 1; i <= n << 1; ++i) if (!vis[i]) dfs(i, i); for (int i = 0; i <= n; ++i) { S[i][0] = i == 0, C[i][0] = 1, A[i][0] = 1; for (int j = 1; j <= i; ++j) S[i][j] = (S[i - 1][j - 1] + (i - 1) * S[i - 1][j] % P) % P, C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % P, A[i][j] = A[i][j - 1] * (i - j + 1) % P; } solve(f, cnt[1]), solve(g, cnt[2]); for (int i = 0; i <= n; ++i) for (int j = 0; j <= i; ++j) h[i] = (h[i] + f[j] * g[i - j] % P) % P; for (int i = 0; i <= n; ++i) { f[i] = 0; for (int j = 0; j <= i; ++j) f[i] = (f[i] + S[cnt[3]][j] * h[i - j] % P) % P; f[i] = f[i] * A[cnt[3]][cnt[3]] % P; } for (int i = 0; i < n; ++i) printf("%lld ", n - cnt[0] - i >= 0 ? f[n - cnt[0] - i] : 0); puts(""); return 0; }
3,400
CPP
#include <bits/stdc++.h> using namespace std; struct star { int ci, cj, len; }; class E1StarsDrawingEasyEdition { public: int N, M; vector<vector<int>> used; vector<vector<char>> grid; void input(std::istream& in, std::ostream& out) { ios_base::sync_with_stdio(false); in >> N >> M; used = vector<vector<int>>(N, vector<int>(M, 0)); grid = vector<vector<char>>(N, vector<char>(M)); for (int(i) = (0); (i) < (N); ++(i)) for (int(j) = (0); (j) < (M); ++(j)) in >> grid[i][j]; } bool is_star(int ci, int cj, int k) { if (ci - k < 0 or grid[ci - k][cj] == '.') return false; if (ci + k >= N or grid[ci + k][cj] == '.') return false; if (cj - k < 0 or grid[ci][cj - k] == '.') return false; if (cj + k >= M or grid[ci][cj + k] == '.') return false; return true; } void solve(std::istream& in, std::ostream& out) { input(in, out); vector<star> ans; for (int(i) = (0); (i) < (N); ++(i)) for (int(j) = (0); (j) < (M); ++(j)) { if (grid[i][j] == '.') { used[i][j] = 1; continue; } int len = 0; while (is_star(i, j, len + 1)) len++; if (not len) continue; for (int(k) = (0); (k) < (len + 1); ++(k)) { used[i - k][j] = used[i + k][j] = 1; used[i][j - k] = used[i][j + k] = 1; } ans.push_back({i + 1, j + 1, len}); } for (int(i) = (0); (i) < (N); ++(i)) for (int(j) = (0); (j) < (M); ++(j)) { if (not used[i][j]) { out << "-1\n"; return; } } out << (int)(ans).size() << '\n'; for (auto u : ans) { out << u.ci << ' ' << u.cj << ' ' << u.len << '\n'; } } }; int main() { E1StarsDrawingEasyEdition solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; long long fastpower(long long b, long long p) { long long ans = 1; while (p) { if (p % 2) { ans = (ans * b); } b = (b * b); p /= 2; } return ans; } string makeitstring(long long n) { string ans; while (n) { long long mod = n % 10; mod += 48; char m = mod; ans = m + ans; n /= 10; } return ans; } long long makeitnumber(string s) { long long ans = 0; for (long long i = 0; i < s.size(); i++) { long long num = s[i] - '0'; ans += (num * fastpower(10, (long long)s.size() - i - 1)); } return ans; } int arr[(int)1e3]; int main() { int n; scanf("%d", &n); n *= 2; vector<int> save; multiset<int> f, s; for (int i = 0; i < n; i++) { int x; scanf("%d", &x); arr[x]++; save.push_back(x); } int cnt1 = 0; for (int i = 0; i < 1000; i++) { if (arr[i] > 1) { for (int j = 0; j < arr[i]; j++) { if (cnt1 % 2) { f.insert(i); } else { s.insert(i); } cnt1++; } arr[i] = 0; } } for (int i = 0; i < 1000; i++) { if (arr[i] == 1) { if (cnt1 % 2) { f.insert(i); } else { s.insert(i); } cnt1++; } } set<string> ans; for (auto it : f) { for (auto it2 : s) { string f = makeitstring(it); string s = makeitstring(it2); string h = s + f; ans.insert(h); } } printf("%d\n", ans.size()); for (auto it : save) { if (f.count(it)) { printf("1 "); f.erase(f.find(it)); } else { printf("2 "); s.erase(s.find(it)); } } return 0; }
1,900
CPP
rd = lambda: list(map(int, input().split())) def root(x): if f[x]!=x: f[x] = root(f[x]) return f[x] n, m = rd() N = range(n) f = list(N) lang = [0]*n for i in N: lang[i] = set(rd()[1:]) for i in N: for j in N[:i]: rj = root(j) if lang[rj].intersection(lang[i]): f[rj] = i lang[i] = lang[i].union(lang[rj]) print(sum(1 for i in N if i==root(i)) - (sum(map(len, lang))>0))
1,400
PYTHON3
import sys import math import bisect import itertools def main(): n = int(input()) A = list(map(int, input().split())) m = int(input()) A.sort(reverse=True) total = sum(A) for q in list(map(int, input().split())): print(total - A[q-1]) if __name__ == "__main__": main()
900
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = int(1e6) + 10, mod = int(1e9) + 7; vector<int> g[N]; int n, m; long long first[N + 10], ans; int main() { scanf("%d%d", &n, &m); for (int i = 1, k, x; i <= n; i++) { scanf("%d", &k); for (int j = 1; j <= k; j++) { scanf("%d", &x); g[x].push_back(i); } } first[0] = 1; for (int i = 1; i < N; i++) { first[i] = first[i - 1] * i % mod; } ans = 1; sort(g + 1, g + m + 1); int cur = 1; for (int i = 2; i <= m; i++) { if (g[i] == g[i - 1]) cur++; else { ans = ans * first[cur] % mod; cur = 1; } } ans = ans * first[cur] % mod; printf("%d", int(ans)); return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1000010; long long v[N]; int main() { long long m, h1, a1, x1, y1, h2, a2, x2, y2; scanf("%lld", &m); scanf("%lld%lld", &h1, &a1); scanf("%lld%lld", &x1, &y1); scanf("%lld%lld", &h2, &a2); scanf("%lld%lld", &x2, &y2); long long t; for (t = 1; !v[h1]; ++t, h1 = (h1 * x1 + y1) % m) v[h1] = t; if (!v[a1]) { puts("-1"); return 0; } long long k1, b1 = v[a1] - 1; if (v[a1] >= v[h1]) k1 = t - v[h1]; else k1 = 0; memset(v, 0, sizeof(v)); for (t = 1; !v[h2]; ++t, h2 = (h2 * x2 + y2) % m) v[h2] = t; if (!v[a2]) { puts("-1"); return 0; } long long k2, b2 = v[a2] - 1; if (v[a2] >= v[h2]) k2 = t - v[h2]; else k2 = 0; if (!k2) { if (!k1) { if (b1 != b2) puts("-1"); else printf("%lld\n", b1); } else { if (b2 >= b1 && !((b2 - b1) % k1)) printf("%lld\n", b2); else puts("-1"); } } else { if (!k1) { if (b1 >= b2 && !((b1 - b2) % k2)) printf("%lld\n", b1); else puts("-1"); } else { if (k1 != k2) { for (long long i = 0; i <= k2; ++i) if (k1 * i + b1 >= b2 && !(((k1 * i + b1 - b2)) % k2)) { printf("%lld\n", k1 * i + b1); return 0; } puts("-1"); } else { if (b1 > b2) { if (!((b1 - b2) % k2)) printf("%lld\n", b1); else puts("-1"); } else { if (!((b2 - b1) % k1)) printf("%lld\n", b2); else puts("-1"); } } } } return 0; }
2,200
CPP
import sys input = sys.stdin.readline def main(): n, m = map(int, input().split()) alst = list(map(int, input().split())) if sum(alst) == m: print("YES") else: print("NO") for _ in range(int(input())): main()
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mx = 1111; double ans[mx]; long long xs[mx]; int n; long long r; int main() { cin >> n >> r; for (int i = (0); i < (n); i++) cin >> xs[i]; ans[0] = r; for (int i = (1); i < (n); i++) { double cur = -1; long long dif = -1; ans[i] = r; for (int j = (0); j < (i); j++) { if (abs(xs[i] - xs[j]) <= 2 * r) { cur = ans[j]; dif = abs(xs[i] - xs[j]); double an = (4 * r * r) - (dif * dif); an = sqrt(an); ans[i] = max(ans[i], cur + an); } } } for (int i = (0); i < (n); i++) printf("%0.8lf ", ans[i]); cout << endl; return 0; }
1,500
CPP
#include <bits/stdc++.h> using namespace std; int m, n; int B[105][105], A[105][105], ones[105][105]; void check(int x, int y) { register int i, j; for (i = 0; i < m; i++) A[i][y] = 0; for (i = 0; i < n; i++) A[x][i] = 0; } int serve(int x, int y) { register int i, j; for (i = 0; i < m; i++) if (A[i][y] == -1) return 1; for (i = 0; i < n; i++) if (A[x][i] == -1) return 1; return 0; } int main() { register int i, j; memset(A, -1, sizeof(A)); cin >> m >> n; for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { cin >> B[i][j]; if (B[i][j] == 1) ones[i][j] = 1; else check(i, j); } } for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { if (ones[i][j] == 1) { int x = serve(i, j); if (x == 0) { cout << "NO"; return 0; } } } } cout << "YES\n"; for (i = 0; i < m; i++) { for (j = 0; j < n; j++) { cout << -A[i][j] << " "; } cout << "\n"; } }
1,300
CPP
t= int(input()) for i in range(t): n,x = map(int,input().split()) numE = [0]*n for i in range(n-1): u,v = map(int,input().split()) numE[u-1]+=1 numE[v-1]+=1 if(n<=2): print("Ayush") continue if(numE[x-1]==1): print("Ayush") continue else: if((n-2)%2==0): print("Ayush") else: print("Ashish")
1,600
PYTHON3
#include <bits/stdc++.h> using namespace std; const double pi = 3.14159265358979323846264338327950288419716939937511; const double eps = 1e-9; char ch_ch_ch[1 << 20]; inline string gs() { scanf("%s", ch_ch_ch); return string(ch_ch_ch); } inline string gl() { gets(ch_ch_ch); return string(ch_ch_ch); } inline int gi() { int x; scanf("%d", &x); return x; } const int inf = 1000000000; int n, q, t; int a[333]; vector<vector<int> > G; vector<int> tops; int was[333]; int initcnts[333]; int sums[333]; const int mod = 1000000007; int ruk[100100]; int dfstop(int v) { if (was[v] == 1) return 1; if (was[v] == 2) return 0; was[v] = 1; for (int i = 0; i < (G[v].size()); ++i) if (dfstop(G[v][i])) return 1; tops.push_back(v); was[v] = 2; return 0; } void solution() { n = gi(); q = gi(); t = gi(); for (int i = 0; i < (n); ++i) scanf("%d", &a[i]); G.resize(n); for (int i = 0; i < (q); ++i) { int b, c; scanf("%d%d", &b, &c); --c, --b; G[c].push_back(b); } for (int i = 0; i < (n); ++i) if (!was[i]) { int cycle = dfstop(i); if (cycle) { printf("0\n"); return; } } reverse(tops.begin(), tops.end()); for (int i = 0; i < (n); ++i) { int v = tops[i]; for (int j = 0; j < (G[v].size()); ++j) initcnts[G[v][j]] = max(initcnts[G[v][j]], initcnts[v] + 1); } long long initSum = 0; for (int i = 0; i < (n); ++i) initSum += initcnts[i] * a[i]; if (initSum > (long long)t) { printf("0\n"); return; } for (int i = n - 1; i >= 0; --i) { int v = tops[i]; sums[v] = a[v]; for (int i = 0; i < (G[v].size()); ++i) sums[v] += sums[G[v][i]]; } ruk[initSum] = 1; for (int it = 0; it < (n); ++it) { int v = tops[it]; for (int val = sums[v], pre = 0; val <= t; ++val, ++pre) if (ruk[pre]) { ruk[val] += ruk[pre]; if (ruk[val] >= mod) ruk[val] -= mod; } } printf("%d\n", ruk[t]); } int main(int argc, char** argv) { solution(); return 0; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; const long long mod = 1e9 + 7; const int alphabet = 26; const int inf = INT_MAX; const long long linf = 1e18 + 9; namespace util { inline long long cnm(char ch) { return (ch - '0'); } inline long long cap(char ch) { return (ch - 'a'); } inline void instr(string& s, long long& n) { cin >> s; n = s.length(); } inline void instr(string& s) { cin >> s; } template <typename T> T gcd(T a, T b) { if (a == 0) return b; return gcd(b % a, a); } template <typename T> bool min(T a, T b, T c) { return std::min(a, std::min(b, c)); } } // namespace util using namespace util; long long g[10][10]; void solve() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long i, j, k, m, n; cin >> n >> m; long long ans, cur, c; for (i = 0; i < m; i++) { long long x, y; cin >> x >> y; g[x][y] = 1; g[y][x] = 1; } if (n <= 6) { cout << m; return; } ans = 0; for (i = 1; i <= 7; i++) { cur = m; for (j = 1; j <= 7; j++) { if (g[i][j]) cur -= 1; } for (j = 1; j <= 7; j++) { if (i == j) continue; c = 0; for (k = 0; k <= 7; k++) { if (g[i][k] and !g[k][j]) c++; } ans = max(ans, cur + c); } } cout << ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) { solve(); } return 0; }
1,700
CPP
def fn(a,b): (a,b)=((3-len(a))*'0'+a),((3-len(b))*'0'+b) for i in range(3): if a[i]!='0' and b[i]!='0':return False return True n=int(input()) a=list(map(str, input().split())) ans1=[] temp=[] ans2=0 for i in range(n): temp=[a[i]] for j in range(n): if fn(a[i],a[j]) and a[i]!=a[j]: c=0 for k in temp: if fn(k,a[j]):c+=1 if c==len(temp):temp.append(a[j]) if len(temp)>ans2: ans1=temp ans2=len(temp) print(ans2) print(' '.join(ans1))
1,600
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); vector<int> v; vector<int> getmin; vector<pair<int, int> > answer; int m; cin >> m; make_heap(v.begin(), v.end()); while (m--) { string s; int no; cin >> s; if (s == "removeMin") { if (v.empty() == true) { answer.push_back(pair<int, int>(1, 0)); } else { pop_heap(v.begin(), v.end()); v.pop_back(); } answer.push_back(pair<int, int>(-1, 0)); } else { cin >> no; if (s == "insert") { v.push_back(-1 * no); push_heap(v.begin(), v.end()); answer.push_back(pair<int, int>(1, no)); } else { while (v.empty() != true && (-1 * v.front()) < no) { answer.push_back(pair<int, int>(-1, 0)); pop_heap(v.begin(), v.end()); v.pop_back(); } if (v.empty() == true || (-1 * v.front()) > no) { answer.push_back(pair<int, int>(1, no)); v.push_back(-1 * no); push_heap(v.begin(), v.end()); } answer.push_back(pair<int, int>(0, no)); } } } cout << answer.size() << endl; for (int i = 0; i < answer.size(); i++) { switch (answer[i].first) { case -1: cout << "removeMin" << endl; break; case 0: cout << "getMin " << answer[i].second << endl; break; case 1: cout << "insert " << answer[i].second << endl; break; } } }
1,600
CPP
n = int( input() ) for _ in range(n): a = int( input() ) i = -1 ans = [] while a!=0: d = a % 10 a //= 10 i += 1 if d==0: continue ans.append(d * (10**i)) print(len(ans)) for i in ans: print(i,sep=" ")
800
PYTHON3
x, y, l, r = list(map(int, input().split())) xs = [] ys = [] ans = [] for i in range(61): xs.append(x ** i) for i in range(61): ys.append(y ** i) for i in range(61): for k in range(61): if l<= xs[i] + ys[k] <= r: ans.append(xs[i] + ys[k]) pref = l ans.append(l - 1) ans.append(r + 1) ans.sort() a = 0 for i in range(len(ans)): a = max(a, ans[i] - pref - 1) pref = ans[i] print(a)
1,800
PYTHON3
#include <bits/stdc++.h> using namespace std; struct P { int x; int y; }; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; bool visit[1005][1005]; bool grid[1005][1005]; int n, m; int sx, sy; int allarea = 0; vector<P> l, r; void bfs(int sx, int sy) { int countt = 0; P sp; sp.x = sx, sp.y = sy; queue<P> q; q.push(sp); while (!q.empty()) { P p; p = q.front(); q.pop(); for (int i = 0; i < 4; i += 1) { int x = p.x + dx[i], y = p.y + dy[i]; if (x >= 0 && x < n && y >= 0 && y < m && grid[x][y] && !visit[x][y]) { allarea += 1; visit[x][y] = true; P tmpp; tmpp.x = x, tmpp.y = y; q.push(tmpp); } } } } bool check1() { int flag = -1; for (int i = 0; i < n; i += 1) { for (int j = 0; j < m; j += 1) { if (grid[i][j] && !visit[i][j]) { if (flag == -1) { allarea += 1; sx = i, sy = j; flag = 0; visit[i][j] = true; bfs(i, j); continue; } else return false; } } } return true; } bool check2(int dx, int dy) { int countt = 0; for (int i = sx; i < sx + dx; i += 1) { for (int j = sy; j < sy + dy; j += 1) { if (!grid[i][j]) return false; } } countt = dx * dy; int tmpx = sx, tmpy = sy; while (1) { bool flag1 = true; for (int i = tmpy; i < tmpy + dy; i += 1) { if (!grid[tmpx + dx][i]) flag1 = false; } if (flag1) tmpx += 1, countt += dy; bool flag2 = true; for (int i = tmpx; i < tmpx + dx; i += 1) { if (!grid[i][tmpy + dy]) flag2 = false; } if (flag2) tmpy += 1, countt += dx; if (!flag1 && !flag2) break; } if (countt == allarea) return true; else return false; } int solve() { int flag = 0; int ansx = 99999999, ansy = 99999999; for (int i = 0; i < n; i += 1) { int countt = 0; for (int j = 0; j < m; j += 1) { if (grid[i][j]) countt += 1; } if (countt) { ansy = min(ansy, countt); } } for (int i = 0; i < m; i += 1) { int countt = 0; for (int j = 0; j < n; j += 1) { if (grid[j][i]) countt += 1; } if (countt) { ansx = min(ansx, countt); } } int ans = 999999999; for (int i = 1; i <= ansy; i += 1) { if (check2(ansx, i)) ans = min(ans, ansx * i), flag = 1; } for (int i = 1; i <= ansx; i += 1) { if (check2(i, ansy)) ans = min(ans, ansy * i), flag = 1; } if (flag) return ans; else return -1; } int main() { cin >> n >> m; for (int i = 0; i < n; i += 1) { for (int j = 0; j < m; j += 1) { char c; cin >> c; if (c == '.') grid[i][j] = false; else grid[i][j] = true; } } memset(visit, false, sizeof(visit)); if (check1()) { printf("%d\n", solve()); } else { printf("-1\n"); } return 0; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; int t, n, k, fac[500005], inv[500005]; inline int read() { int ans = 0, f = 1; char c = getchar(); while (c > '9' || c < '0') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') ans = (ans << 1) + (ans << 3) + (c ^ 48), c = getchar(); return ans * f; } int ksm(int a, int b) { int ans = 1; while (b) { if (b & 1) ans = (long long)ans * a % 998244353; a = (long long)a * a % 998244353; b >>= 1; } return ans; } inline int C(int n, int m) { return n < m ? 0 : (long long)fac[n] * inv[m] % 998244353 * inv[n - m] % 998244353; } int main() { n = read(), k = read(); fac[0] = inv[0] = 1; for (int i = 1; i <= n; ++i) fac[i] = (long long)fac[i - 1] * i % 998244353; inv[n] = ksm(fac[n], 998244353 - 2); for (int i = n - 1; i; i--) inv[i] = (long long)inv[i + 1] * (i + 1) % 998244353; int ans = 0; for (int i = 1; i <= n; ++i) ans = (ans + C(n / i - 1, k - 1)) % 998244353; printf("%d\n", ans); }
2,000
CPP
n=int(input()) li=list(map(int,input().split())) ans=0 for i in range(1,n-1): if li[i]==0 and li[i-1]==1 and li[i+1]==1: li[i+1]=0 ans+=1 print(ans)
1,000
PYTHON3
""" Author - Satwik Tiwari . 17th Oct , 2020 - Saturday """ #=============================================================================================== #importing some useful libraries. from __future__ import division, print_function from fractions import Fraction import sys import os from io import BytesIO, IOBase # from itertools import * from heapq import * from math import gcd, factorial,floor,ceil from copy import deepcopy from collections import deque # from collections import Counter as counter # Counter(list) return a dict with {key: count} # from itertools import combinations as comb # if a = [1,2,3] then print(list(comb(a,2))) -----> [(1, 2), (1, 3), (2, 3)] # from itertools import permutations as permutate from bisect import bisect_left as bl from bisect import bisect_right as br from bisect import bisect #============================================================================================== #fast I/O region BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep, file = kwargs.pop("sep", " "), kwargs.pop("file", sys.stdout) at_start = True for x in args: if not at_start: file.write(sep) file.write(str(x)) at_start = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() if sys.version_info[0] < 3: sys.stdin, sys.stdout = FastIO(sys.stdin), FastIO(sys.stdout) else: sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) # inp = lambda: sys.stdin.readline().rstrip("\r\n") #=============================================================================================== ### START ITERATE RECURSION ### from types import GeneratorType def iterative(f, stack=[]): def wrapped_func(*args, **kwargs): if stack: return f(*args, **kwargs) to = f(*args, **kwargs) while True: if type(to) is GeneratorType: stack.append(to) to = next(to) continue stack.pop() if not stack: break to = stack[-1].send(to) return to return wrapped_func #### END ITERATE RECURSION #### #=============================================================================================== #some shortcuts mod = 10**9+7 def inp(): return sys.stdin.readline().rstrip("\r\n") #for fast input def out(var): sys.stdout.write(str(var)) #for fast output, always take string def lis(): return list(map(int, inp().split())) def stringlis(): return list(map(str, inp().split())) def sep(): return map(int, inp().split()) def strsep(): return map(str, inp().split()) # def graph(vertex): return [[] for i in range(0,vertex+1)] def zerolist(n): return [0]*n def nextline(): out("\n") #as stdout.write always print sring. def testcase(t): for pp in range(t): solve(pp) def printlist(a) : for p in range(0,len(a)): out(str(a[p]) + ' ') def google(p): print('Case #'+str(p)+': ',end='') def lcm(a,b): return (a*b)//gcd(a,b) def power(x, y, p) : res = 1 # Initialize result x = x % p # Update x if it is more , than or equal to p if (x == 0) : return 0 while (y > 0) : if ((y & 1) == 1) : # If y is odd, multiply, x with result res = (res * x) % p y = y >> 1 # y = y/2 x = (x * x) % p return res def ncr(n,r): return factorial(n) // (factorial(r) * factorial(max(n - r, 1))) def isPrime(n) : if (n <= 1) : return False if (n <= 3) : return True if (n % 2 == 0 or n % 3 == 0) : return False i = 5 while(i * i <= n) : if (n % i == 0 or n % (i + 2) == 0) : return False i = i + 6 return True #=============================================================================================== # code here ;)) def solve(case): s = list(inp()) n = len(s) que = deque([]) for i in range(n): if(len(que) == 0): que.append(s[i]) else: que.append(s[i]) f = True while(f): # print(que) temp = que.pop() t2 = que.pop() # print(temp+t2) if(t2+temp == 'AB' or t2+temp == 'BB'): pass else: f = False que.append(temp) que.append(t2) if(len(que) < 2): f = False print(len(que)) # testcase(1) testcase(int(inp()))
1,100
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimize("O2") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("no-stack-protector,fast-math") using namespace std; const int MAXN = 500010; long long n, m, k, p, q, r, ans; long long A[MAXN], B[MAXN], C[MAXN]; long long maxb[MAXN], maxc[MAXN]; long long seg[MAXN << 2]; int lazy[MAXN << 2], Mn[MAXN << 2], Mx[MAXN << 2]; vector<int> vec[MAXN]; void add_lazy(int id, int len, long long lz) { seg[id] = len * lz; lazy[id] = Mn[id] = Mx[id] = lz; } void shift(int id, int tl, int tr) { if (!lazy[id]) return; int mid = (tl + tr) >> 1; add_lazy(id << 1, mid - tl, lazy[id]); add_lazy(id << 1 | 1, tr - mid, lazy[id]); lazy[id] = 0; } void update(int id, int tl, int tr, int l, int r, int val) { if (tr <= l || r <= tl || Mn[id] >= val) return; if (l <= tl && tr <= r && Mx[id] <= val) { add_lazy(id, tr - tl, val); return; } shift(id, tl, tr); int mid = (tl + tr) >> 1; update(id << 1, tl, mid, l, r, val); update(id << 1 | 1, mid, tr, l, r, val); seg[id] = seg[id << 1] + seg[id << 1 | 1]; Mn[id] = min(Mn[id << 1], Mn[id << 1 | 1]); Mx[id] = max(Mx[id << 1], Mx[id << 1 | 1]); } long long get(int id, int tl, int tr, int l, int r, long long val) { if (r <= tl || tr <= l || Mx[id] <= val) return 0; if (l <= tl && tr <= r && Mn[id] >= val) return seg[id] - val * (tr - tl); shift(id, tl, tr); int mid = (tl + tr) >> 1; return get(id << 1, tl, mid, l, r, val) + get(id << 1 | 1, mid, tr, l, r, val); } void Add(int x, int y) { update(1, 1, MAXN, 1, x + 1, y); } long long Get(int x, int y) { return get(1, 1, MAXN, x + 1, MAXN, y); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> p >> q >> r; for (int i = 1; i <= n; i++) cin >> A[i] >> B[i] >> C[i], vec[A[i]].push_back(i); for (int a = p; a; a--) { maxb[a] = maxb[a + 1]; maxc[a] = maxc[a + 1]; for (int id : vec[a]) { maxb[a] = max(maxb[a], B[id]); maxc[a] = max(maxc[a], C[id]); } } for (int a = 1; a <= p; a++) { ans += (q - maxb[a]) * (r - maxc[a]) - Get(maxb[a], maxc[a]); for (int id : vec[a]) Add(B[id], C[id]); } cout << ans << '\n'; return 0; }
2,800
CPP
# A. Minutes Before the New Year # -*- coding: utf-8 -*- # @Date : 2019-12-29 07:20:55 # @Author : raj lath ([email protected]) # @Link : link # @Version : 1.0.0 import sys sys.setrecursionlimit(10**5+1) inf = int(10 ** 20) max_val = inf min_val = -inf RW = lambda : sys.stdin.readline().strip() RI = lambda : int(RW()) RMI = lambda : [int(x) for x in sys.stdin.readline().strip().split()] RWI = lambda : [x for x in sys.stdin.readline().strip().split()] zero_minutes = 24 * 60 for _ in [0] * RI(): hour, mins = RMI() print( zero_minutes - (hour * 60 + mins))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxN = 200005; struct Node { int sum; Node *left, *right; Node() { left = NULL; right = NULL; sum = 0; } }; typedef Node *PNode; class Tree { public: PNode root; int size; PNode bild(int rt, int l, int r) { if (l == r) { PNode cur = new Node; return cur; } int m = (l + r) >> 1; PNode cur = new Node; cur->left = bild(rt << 1, l, m); cur->right = bild((rt << 1) + 1, m + 1, r); cur->sum = 0; return cur; } public: Tree() {} Tree(int _size) { size = _size; root = bild(1, 1, size); } int suma(PNode rt, int l, int r, int L, int R) { if (r < l || R < L) return 0; if (L == l && R == r) { return rt->sum; } int m = (l + r) >> 1; int ans = 0; if (L <= m) ans += suma(rt->left, l, m, L, min(m, R)); if (R > m) ans += suma(rt->right, m + 1, r, max(m + 1, L), R); return ans; } int ins(PNode rt, int l, int r, int L, int R) { if (L == l && R == r && rt->sum == r - l + 1) { return 0; } if (l == r) { rt->sum += 1; return l; } int m = (l + r) >> 1; int st = max(l, L); int fn = min(m, R); int ans = 0; if (st <= fn) ans = ins(rt->left, l, m, st, fn); if (ans != 0) { ++(rt->sum); return ans; } st = max(m + 1, L); fn = min(r, R); if (st <= fn) ans = ins(rt->right, m + 1, r, st, fn); if (ans != 0) { rt->sum++; } return ans; } int add(int num) { int sum = suma(root, 1, size, num, size); if (sum < size - num + 1) { return ins(root, 1, size, num, size); } else { num--; return ins(root, 1, size, 1, num); } } void del(PNode rt, int l, int r, int &num) { if (l == r) { rt->sum = 0; return; } int m = (l + r) >> 1; if (num <= m) del(rt->left, l, m, num); else del(rt->right, m + 1, r, num); rt->sum = rt->left->sum + rt->right->sum; return; } }; int h, t, n; int cnt; int tr[maxN], numb[maxN]; int size[maxN]; map<int, pair<int, int> > data; Tree *der[maxN]; long long ans = 0; int main() { scanf("%d%d%d\n", &h, &t, &n); cnt = 0; for (int i = 0; i < n; ++i) if (tr[i] == 0) { cnt++; int j = i, kil = 0; while (tr[j] == 0) { numb[j] = ++kil; tr[j] = cnt; j = (j + t) % h; } size[cnt] = kil; } for (int i = 1; i <= cnt; ++i) { der[i] = new Tree(size[i]); } for (int i = (0); (i) < (n); ++(i)) { char q; cin.get(q); if ('+' == q) { int ch, hsh; scanf("%d %d\n", &ch, &hsh); int dernum = tr[hsh]; int num = numb[hsh]; int cur_index = der[dernum]->add(num); data[ch] = make_pair(tr[hsh], cur_index); if (cur_index < num) cur_index += size[dernum]; if (num < cur_index) { ans += cur_index - num; } } else { int ch; scanf("%d\n", &ch); map<int, pair<int, int> >::iterator it = data.find(ch); int dernum = (*it).second.first; int num = (*it).second.second; der[dernum]->del(der[dernum]->root, 1, size[dernum], num); data.erase(ch); } } cout << ans << endl; return 0; }
1,600
CPP
def f(n, s): L = 0 R = 0 for (a,b) in s: L += a R += b beauty = abs(L-R) ind = -1 for i in range(n): (a,b) = s[i] new = abs((L-a+b) - (R-b+a)) if new > beauty: ind = i beauty = new return ind+1 def run(): s = [] i = 0 for line in iter(input, ''): if i == 0: n = int(line) else: k = line.split(' ') m = (int(k[0]), int(k[1])) s.append(m) if i == n: break i += 1 print(f(n, s)) run()
1,100
PYTHON3
import math for _ in range(int(input())): n, k = map(int, input().split()) ans = (k//(n-1))+k if ans%n==0: ans-=1 print(ans)
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; bool cmp(const pair<string, int>& a, const pair<string, int>& b) { if (a.second != b.second) return a.second > b.second; return a.first < b.first; } int main() { string me; cin >> me; int n; cin >> n; string x, y; string tp; unordered_map<string, int> fact; for (int i = 0; i < n; i++) { cin >> x >> tp; if (fact.find(x) == fact.end()) fact[x] = 0; if (tp == "commented") { cin >> tp >> y >> tp; y = y.substr(0, y.size() - 2); if (fact.find(y) == fact.end()) fact[y] = 0; if (x == me) fact[y] += 10; if (y == me) fact[x] += 10; } else if (tp == "likes") { cin >> y >> tp; y = y.substr(0, y.size() - 2); if (fact.find(y) == fact.end()) fact[y] = 0; if (y == me) fact[x] += 5; if (x == me) fact[y] += 5; } else { cin >> tp >> y >> tp; y = y.substr(0, y.size() - 2); if (fact.find(y) == fact.end()) fact[y] = 0; if (y == me) fact[x] += 15; if (x == me) fact[y] += 15; } } vector<pair<string, int>> v; for (auto el : fact) { if (el.first == me) continue; v.push_back({el.first, el.second}); } sort(v.begin(), v.end(), cmp); for (auto el : v) { cout << el.first << endl; } return 0; }
1,500
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T &x) { T f = 1; x = 0; char ch = getchar(); while (0 == isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (0 != isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar(); x *= f; } template <typename T> inline void write(T x) { if (x < 0) { x = ~(x - 1); putchar('-'); } if (x > 9) write(x / 10); putchar(x % 10 + '0'); } const int inf = 0x3f3f3f3f; const int N = 1e6 + 100; char s[N]; int sum[N]; map<int, vector<int>> node; int cal(int l, int r) { int x = sum[r] + sum[l - 1]; return *lower_bound(node[x].begin(), node[x].end(), l); } int main() { int w; cin >> w; while (w--) { node.clear(); int n, m; read(n), read(m); scanf("%s", s + 1); for (int i = 1; i <= n; i++) { sum[i] = sum[i - 1] + (i % 2 ? 1 : -1) * (s[i] == '+' ? 1 : -1); node[sum[i] + sum[i - 1]].push_back(i); } while (m--) { int l, r; read(l), read(r); if (sum[r] - sum[l - 1] == 0) puts("0"); else if ((r - l + 1) & 1) { puts("1"); printf("%d\n", cal(l, r)); } else { puts("2"); printf("%d %d\n", l, cal(l + 1, r)); } } } return 0; }
2,200
CPP
x=int(input()) l=list(map(int,input().split())) l=list(set(l)) if len(l)==1: print(0) elif len(l)==2: ans=abs(l[0]-l[1]) if ans%2==0: print(ans//2) else: print(ans) elif len(l)==3: su=0 for i in range(len(l)): su+=l[i] su//=len(l) a1=su-min(l) a2=max(l)-su if a1==a2: print(a1) else: print(-1) else: print(-1)
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const long long mod = 1e9 + 7; const long long N = 1e5 + 10; inline long long add(long long x, long long y) { x += y; if (x >= mod) x -= mod; return x; } inline long long mul(long long x, long long y) { x = (1LL * x * y) % mod; return x; } long long get_val(long long i, long long a, long long b) { return a + b * (i - 1); } long long get_sum(long long i, long long a, long long b) { return b * (((i + 1) * i) / 2) - b * i + a * i; } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); long long a, b, n; cin >> a >> b >> n; while (n--) { long long l, t, m; cin >> l >> t >> m; long long l_ = l; long long x = get_sum(l - 1, a, b); long long r_ = 1e9; while (l_ != r_) { long long m_ = (l_ - r_) / 2 + r_; if (get_val(m_, a, b) <= t && get_sum(m_, a, b) - x <= t * m) l_ = m_; else r_ = m_ - 1; } if (get_val(l, a, b) > t || get_val(l, a, b) > m * t) cout << "-1\n"; else cout << l_ << "\n"; } return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; pair<long int, long int> seg[int(4e6)]; int k = 1; void upd(int R, int L, int i, int p = 0, int l = 0, int r = k) { if (R > r || R < l) return; if (l == r) { seg[p] = max(seg[p], pair<long int, long int>(L, i)); return; } int m = (l + r) / 2; upd(R, L, i, p + p + 1, l, m); upd(R, L, i, p + p + 2, 1 + m, r); seg[p] = max(seg[p + p + 1], seg[p + p + 2]); } pair<long int, long int> query(int L, int R, int p = 0, int l = 0, int r = k) { if (R < l) return pair<long int, long int>(-1, -1); if (r <= R) return seg[p]; if (l == r) { return seg[p]; } int m = l + r; m >>= 1; return max(query(L, R, p + p + 1, l, m), query(L, R, p + p + 2, 1 + m, r)); } int a[int(1e6)][2]; map<int, int> comp; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { scanf("%d%d", &a[i][0], &a[i][1]); comp[a[i][1]]; } for (auto x : comp) { comp[x.first] = k++; } for (int i = 0; i < 4 * k + 100; i++) seg[i] = pair<long int, long int>(-1, -1); for (int i = 0; i < n; i++) { auto x = query(a[i][0], comp[a[i][1]]); if (x != pair<long int, long int>(-1, -1) && x.first >= a[i][0]) { cout << x.second + 1 << " " << i + 1 << endl; return 0; } upd(comp[a[i][1]], a[i][0], i); } for (int i = 0; i < 4 * k + 100; i++) seg[i] = pair<long int, long int>(-1, -1); for (int I = 0; I < n; I++) { int i = n - 1 - I; auto x = query(a[i][0], comp[a[i][1]]); if (x != pair<long int, long int>(-1, -1) && x.first >= a[i][0]) { cout << x.second + 1 << " " << i + 1 << endl; return 0; } upd(comp[a[i][1]], a[i][0], i); } cout << "-1 -1" << endl; }
1,500
CPP
#!/usr/bin/env python import os import sys from io import BytesIO, IOBase #from bisect import bisect_left as bl #c++ lowerbound bl(array,element) #from bisect import bisect_right as br #c++ upperbound br(array,element) import math def main(): n,k=map(int,input().split(" ")) print(len(list(filter(lambda x:x>=k,list(map(lambda x:5-int(x),input().split(" "))))))//3) #-----------------------------BOSS-------------------------------------! # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # endregion if __name__ == "__main__": main()
800
PYTHON3
#include <bits/stdc++.h> using namespace std; using ll = long long; const int maxn = 2e5 + 5; int tt, n, lf, bd; bool tk[maxn]; vector<int> E[maxn]; void dfs(int nd, int pr) { int cnt = 0; for (auto i : E[nd]) { if (i != pr) { dfs(i, nd); if (!tk[i]) cnt++; } } if (!cnt) lf++; else { tk[nd] = 1; if (nd != 1) bd++; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> tt; while (tt--) { cin >> n; lf = bd = 0; for (int i = 1; i <= n; i++) E[i].clear(), tk[i] = 0; for (int i = 1; i < n; i++) { int x, y; cin >> x >> y; E[x].push_back(y); E[y].push_back(x); } dfs(1, 0); cout << lf - bd << '\n'; } }
2,000
CPP
#include <bits/stdc++.h> using namespace std; float percentage(map<string, int> mapping, string str) { int score = mapping[str]; int count = 0; int total = 0; for (auto i : mapping) { if (i.second <= score) count++; total++; } return ((float)count / total * (100.0)); } int main() { int n; cin >> n; vector<pair<string, int>> arr; map<string, int> mapping; for (int i = 0; i < n; i++) { string str; cin >> str; int temp; cin >> temp; arr.push_back(make_pair(str, temp)); if (mapping.find(str) == mapping.end()) { mapping[str] = temp; } else { if (mapping[str] < temp) mapping[str] = temp; } } cout << mapping.size() << endl; for (auto i : mapping) { string s = ""; float percent = percentage(mapping, i.first); if (percent < 50) { s = "noob"; } else if (percent >= 50 && percent < 80) { s = "random"; } else if (percent >= 80 && percent < 90) { s = "average"; } else if (percent >= 90 && percent < 99) { s = "hardcore"; } else { s = "pro"; } cout << i.first << " " << s << endl; } }
1,400
CPP
def median(n,m,li): count = 0 li.sort() while True: if li[(n+1)//2 -1]== m: return count li.append(m) li.sort() n+=1 count+=1 n,m = input().split() li = [int(x) for x in input().split()] print(median(int(n),int(m),li))
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mx[] = {1, -1, 0, 0}; const int my[] = {0, 0, 1, -1}; struct RECT { int upb; int leb; int rib; int dob; int col; }; vector<RECT> rectangulos; int corr[55][55], act = 1, an, al, color[55][55], n, dir = 0, sec = 0, nxt; bool mrk[55][55]; void exp(int xi, int yi) { int up, le, ri, dow; queue<int> x, y; x.push(xi); y.push(yi); up = yi; dow = yi; le = xi; ri = xi; mrk[xi][yi] = 1; corr[xi][yi] = act; while (x.size()) { if (y.front() < up) { up = y.front(); } if (y.front() > dow) { dow = y.front(); } if (x.front() > ri) { ri = x.front(); } if (x.front() < le) { le = x.front(); } for (int i = 0; i < 4; i++) { if (x.front() + mx[i] >= 0 && y.front() + my[i] >= 0 && x.front() + mx[i] < an && y.front() + my[i] < al && mrk[x.front() + mx[i]][y.front() + my[i]] == 0 && color[x.front() + mx[i]][y.front() + my[i]] == color[x.front()][y.front()]) { mrk[x.front() + mx[i]][y.front() + my[i]] = 1; corr[x.front() + mx[i]][y.front() + my[i]] = act; x.push(x.front() + mx[i]); y.push(y.front() + my[i]); } } x.pop(); y.pop(); } RECT aux; aux.upb = up; aux.dob = dow; aux.leb = le; aux.rib = ri; aux.col = color[xi][yi]; rectangulos.push_back(aux); } void gen() { for (int i = 0; i < an; i++) { for (int j = 0; j < al; j++) { if (mrk[i][j] == 0 && color[i][j] != 0) { exp(i, j); act++; } } } } void changedir() { sec++; sec %= 2; if (sec == 0) { dir++; dir %= 4; } } int main(void) { string line; RECT aux; rectangulos.push_back(aux); scanf("%d%d", &al, &n); for (int i = 0; i < al; i++) { cin >> line; for (int j = 0; j < line.size(); j++) { color[j][i] = line[j] - '0'; } } an = line.size(); gen(); act = 1; for (int i = 0; i < n; i++) { if (dir == 0) { if (rectangulos[act].rib == an - 1) { changedir(); continue; } if (sec == 0) { nxt = corr[rectangulos[act].rib + 1][rectangulos[act].upb]; if (nxt == 0) { changedir(); continue; } } else { nxt = corr[rectangulos[act].rib + 1][rectangulos[act].dob]; if (nxt == 0) { changedir(); continue; } } } if (dir == 1) { if (rectangulos[act].dob == al - 1) { changedir(); continue; } if (sec == 0) { nxt = corr[rectangulos[act].rib][rectangulos[act].dob + 1]; if (nxt == 0) { changedir(); continue; } } else { nxt = corr[rectangulos[act].leb][rectangulos[act].dob + 1]; if (nxt == 0) { changedir(); continue; } } } if (dir == 2) { if (rectangulos[act].leb == 0) { changedir(); continue; } if (sec == 0) { nxt = corr[rectangulos[act].leb - 1][rectangulos[act].dob]; if (nxt == 0) { changedir(); continue; } } else { nxt = corr[rectangulos[act].leb - 1][rectangulos[act].upb]; if (nxt == 0) { changedir(); continue; } } } if (dir == 3) { if (rectangulos[act].upb == 0) { changedir(); continue; } if (sec == 0) { nxt = corr[rectangulos[act].leb][rectangulos[act].upb - 1]; if (nxt == 0) { changedir(); continue; } } else { nxt = corr[rectangulos[act].rib][rectangulos[act].upb - 1]; if (nxt == 0) { changedir(); continue; } } } act = nxt; } printf("%d\n", rectangulos[act].col); }
2,100
CPP
#include <bits/stdc++.h> using namespace std; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count() * ((uint64_t) new char | 1)); int K, N; vector<int> A, B, Z; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> K; N = 1 << K; Z.resize(N); int all_xor = 0; for (auto &z : Z) { cin >> z; all_xor ^= z; } if (all_xor != 0) { cout << "Fou" << '\n'; return 0; } A.resize(N); B.resize(N); vector<int> location(N); for (int i = 0; i < N; i++) A[i] = B[i] = i; shuffle(A.begin(), A.end(), rng); shuffle(B.begin(), B.end(), rng); for (int i = 0; i < N; i++) location[B[i]] = i; for (int i = 0; i < N; i++) { int u = i; while (Z[u] != (A[u] ^ B[u])) { int v = location[A[u] ^ Z[u]]; swap(B[u], B[v]); swap(location[B[u]], location[B[v]]); if (v > i) { break; } else { swap(A[v], A[i + 1]); u = v; } } } cout << "Shi" << '\n'; for (int i = 0; i < N; i++) cout << A[i] << (i < N - 1 ? ' ' : '\n'); for (int i = 0; i < N; i++) cout << B[i] << (i < N - 1 ? ' ' : '\n'); }
3,100
CPP
#include <bits/stdc++.h> #pragma GCC diagnostic ignored "-Wunused-result" #pragma GCC diagnostic ignored "-Wunused-function" using namespace std; namespace { namespace shik { template <class T> void _R(T &x) { cin >> x; } void _R(int &x) { scanf("%d", &x); } void _R(int64_t &x) { scanf("%" SCNd64, &x); } void _R(double &x) { scanf("%lf", &x); } void _R(char &x) { scanf(" %c", &x); } void _R(char *x) { scanf("%s", x); } void R() {} template <class T, class... U> void R(T &head, U &...tail) { _R(head); R(tail...); } template <class T> void _W(const T &x) { cout << x; } void _W(const int &x) { printf("%d", x); } void _W(const int64_t &x) { printf("%" PRId64, x); } void _W(const double &x) { printf("%.16f", x); } void _W(const char &x) { putchar(x); } void _W(const char *x) { printf("%s", x); } template <class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); } void W() {} template <class T, class... U> void W(const T &head, const U &...tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); } template <class T, class F = less<T>> void sort_uniq(vector<T> &v, F f = F()) { sort(begin(v), end(v), f); v.resize(unique(begin(v), end(v)) - begin(v)); } template <class T> inline T bit(T x, int i) { return (x >> i) & 1; } template <class T> inline bool chkmax(T &a, const T &b) { return b > a ? a = b, true : false; } template <class T> inline bool chkmin(T &a, const T &b) { return b < a ? a = b, true : false; } template <class T> using MaxHeap = priority_queue<T>; template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>; const int N = 5010; char s[N]; int z[N]; void main() { scanf("%s", s + 1); int n = strlen(s + 1); for (int i = (1); i <= int(n); i++) z[i] = z[i - 1] + (s[i] == 'a'); int ans = 0; for (int i = (0); i <= int(n); i++) for (int j = (i); j <= int(n); j++) { int c1 = z[i]; int c2 = (j - i) - (z[j] - z[i]); int c3 = z[n] - z[j]; int now = c1 + c2 + c3; chkmax(ans, now); } W(ans); } } // namespace shik } // namespace int main() { shik::main(); return 0; }
1,500
CPP
n, k = map(int, input().split()) a = n * (k - 1) + 1 sm = n * (2 * a + (n - 1) * (n - k + 1)) // 2 t = [ [0] * n for i in range(n) ] x = 1 for j in range(k-1): for i in range(n): t[i][j] = x x += 1 for i in range(n): for j in range(k-1, n): t[i][j] = x x += 1 print(sm) for row in t: print(" ".join(map(str, row)))
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int w = 0, n, i, j, p, q; cin >> n; cin >> p; int b[p]; for (i = 0; i < p; i++) cin >> b[i]; cin >> q; int u[q]; for (j = 0; j < q; j++) cin >> u[j]; int c[p + q]; i = 0; j = 0; while (i != p) { c[w] = b[i]; i++; w++; } while (j != q) { c[w] = u[j]; j++; w++; } int y; y = p + q; set<int> s(c, c + y); if (s.size() == n) cout << "I become the guy."; else cout << "Oh, my keyboard!"; return 0; }
800
CPP
n = int(input()) footprints = input() firstR = n+1 lastR = 0 firstL = n+1 lastL=0 for i in range(n): if(footprints[i]=='.'): continue if(footprints[i]=='R'): firstR = min(firstR,i+1) lastR = i+1 else: firstL = min(firstL,i+1) lastL=i+1 if(firstR!=n+1): s = firstR else : s = lastL print(s,end=" ") if(firstL!=n+1): t = firstL-1 else : t = lastR+1; print(t)
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; int N, M; string A; set<int> second; int main() { cin >> N >> M; while (N--) { cin >> A; pair<int, int> pos; for (__typeof((__typeof(M))0) j((__typeof(M))0); j < M; j++) { if (A[j] == 'G') pos.first = j; if (A[j] == 'S') pos.second = j; } if (pos.second < pos.first) { cout << -1 << endl; return 0; } second.insert(pos.second - pos.first); } cout << (int)(second.size()) << endl; return 0; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { int n, m, k; int i; cin >> n >> m >> k; vector<long long> A(n + 1, 0); vector<long long> B(n, 0); for (i = 0; i < n; i++) cin >> B[i]; vector<int> l(m, 0); vector<int> r(m, 0); vector<int> d(m, 0); vector<long long> M(m + 1, 0); for (i = 0; i < m; i++) cin >> l[i] >> r[i] >> d[i]; for (i = 0; i < k; i++) { int x, y; cin >> x >> y; x--; M[x] += 1; M[y] -= 1; } long long count = 0; for (i = 0; i < m; i++) { count += M[i]; A[l[i] - 1] += count * d[i]; A[r[i]] -= count * d[i]; } long long num = 0; for (i = 0; i < n; i++) { num += A[i]; cout << num + B[i] << " "; } cout << endl; return 0; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; int tree[maxn]; int xx[maxn]; int n, q; inline int lowbit(int &xx) { return xx & -xx; } void utr(int ii, int xx) { while (ii < maxn) { tree[ii] += xx; ii += lowbit(ii); } } int rtr(int ii) { int res = 0; while (ii) { res += tree[ii]; ii -= lowbit(ii); } return res; } bool chk(int mid, int K) { if (rtr(mid) >= K) return true; else return false; } int find2(int L, int R, int K) { int res = 0; while (L <= R) { int mid = (L + R) >> 1; if (chk(mid, K)) { res = mid; R = mid - 1; } else L = mid + 1; } return res; } void deal1() { scanf("%d%d", &n, &q); for (int i = 1; i <= n; i++) { int temp; scanf("%d", &temp); utr(temp, 1); } for (int i = 1; i <= q; i++) { int k; scanf("%d", &k); if (k > 0) { utr(k, 1); } else { int ii = find2(1, n, -k); utr(ii, -1); } } int ii = find2(1, n, 1); printf("%d\n", ii); } int aa[maxn]; int bb[maxn]; bool chk2(int mid, int n, int q) { int cnt = 0; for (int i = 1; i <= n; i++) if (aa[i] <= mid) cnt++; for (int i = 1; i <= q; i++) { if (bb[i] > 0 && bb[i] <= mid) cnt++; else if (bb[i] < 0 && -bb[i] <= cnt) cnt--; } return cnt; } void deal2() { int n, q; scanf("%d%d", &n, &q); for (int i = 1; i <= n; i++) scanf("%d", aa + i); for (int i = 1; i <= q; i++) scanf("%d", bb + i); int L = 1, R = n; int res = 0; while (L <= R) { int mid = (L + R) >> 1; if (chk2(mid, n, q)) { res = mid; R = mid - 1; } else L = mid + 1; } printf("%d\n", res); } int main() { deal2(); }
1,900
CPP