solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; int main() { int large, i, small; long long int ans = 0; cin >> large >> small; if (small > large) { int temp = small; small = large; large = temp; } for (i = 1; i <= large; i++) { int rem = i % 5; int first = 5 - rem; int num = (small - first) / 5; if (small - first >= 0) num++; ans = ans + num; } cout << ans << endl; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; const int k[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; map<string, int> f; string s; int main() { int i, j, n = 0, y, m, d; string t, x; cin >> s; for (i = 0; i + 10 <= (int)s.length(); i++) { x = s.substr(i, 10); if (sscanf((x + "*1").c_str(), "%2d-%2d-%4d*%d", &d, &m, &y, &j) != 4) continue; if (m > 12 || m < 1) continue; if (d > k[m] || d < 1) continue; if (y < 2013 || y > 2015) continue; if (n < ++f[x]) { n = f[x]; t = x; } } cout << t; return 0; }
1,600
CPP
n,m,k = map(int, input().split()) starts = [] ends = [] for _ in range(k): sx, sy = map(int, input().split()) starts.append((sx, sy)) for _ in range(k): sx, sy = map(int, input().split()) ends.append((sx, sy)) if n == 1 and m == 2: print("2\nLR") else: res = "U"*(n-1) + "L"*(m-1) + (("R"*(m-1) + "D" + "L"*(m-1)+"D")*(n//2))[:-1] + ("" if n % 2 == 0 else "D" + "R"*(m-1)) print(len(res)) print(res)
1,600
PYTHON3
from sys import stdin s=stdin.readline().strip().split() def smas(x,mas): for i in range(len(s)): if s[i]==1 and (i==0 or s[i-1]=="+"): if x>0: y=min(n-1,x) s[i]+=y x-=y def smen(x,men): for i in range(len(s)): if s[i]==1 and i>0 and s[i-1]=="-": if x>0: y=min(n-1,x) s[i]+=y x-=y n=int(s[-1]) ans=0 y=0 if s[0]=="-": y=1 ind=-1 men=0 mas=0 for i in range(y,len(s)-1,2): if i!=0 and s[i-1]=="-" : ans-=1 men+=1 else: mas+=1 if ind==-1: ind=i else: ans+=1 s[i]=1 l=[-(n*men),-men] t=True for i in range(mas,n*mas+1): if i>=n: x=n-i else: continue if x>=l[0] and x<=l[1]: t=False smas(i-mas,mas) smen((-x)-men,men) break if t: print("Impossible") else: print("Possible") print(*s)
1,800
PYTHON3
n = int(input()) misha = 0 chris = 0 for i in range((n)): a = list(map(int, input().split())) if a[0] > a[1]: misha += 1 elif a[1] > a[0]: chris += 1 if misha > chris: print("Mishka") elif chris > misha: print("Chris") else: print("Friendship is magic!^^")
800
PYTHON3
#include <bits/stdc++.h> int main() { int n = 0; double s = 0, xm = 0, ym = 0, xl = 31400, yl = 31400; scanf("%d", &n); while (n--) { double x1, y1, x2, y2; scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2); s = (y2 - y1) * (x2 - x1) + s; if (x1 <= xl) xl = x1; if (y1 <= yl) yl = y1; if (x2 >= xm) xm = x2; if (y2 >= ym) ym = y2; } if ((xm - xl) == (ym - yl) && s == (xm - xl) * (ym - yl)) { printf("YES\n"); } else { printf("NO\n"); } return 0; }
1,500
CPP
#include <bits/stdc++.h> int x, y; int is_ok(int a, int b) { if (a < 0 || b < 0 || a >= x || b >= y) return 0; return 1; } int mp[200][200]; char res[200][200]; int chk[200]; void nul(int i, int j, int from, int to, int fg) { if (!is_ok(i, j)) return; if (fg == -1) { if (res[i][j]) chk[res[i][j] - 'a'] = 1; } if (mp[i][j] != from) return; mp[i][j] = to; if (fg >= 0) { res[i][j] = fg + 'a'; } nul(i - 1, j, from, to, fg); nul(i + 1, j, from, to, fg); nul(i, j - 1, from, to, fg); nul(i, j + 1, from, to, fg); } int main() { int i, j, k, l, m, n; int a, b, c, dame; scanf("%d%d%d%d%d", &x, &y, &a, &b, &c); for (i = 0; i < x; i++) for (j = 0; j < y; j++) mp[i][j] = -1; k = 0; dame = 0; if (x % 2 == 1) { for (j = 0; j < y; j++) if (mp[0][j] == -1 && is_ok(0, j + 1) && mp[0][j + 1] == -1) { a--; mp[0][j] = mp[0][j + 1] = k++; } } if (y % 2 == 1) { for (i = 0; i < x; i++) if (mp[i][0] == -1 && is_ok(i + 1, 0) && mp[i][0] == -1) { b--; mp[i][0] = mp[i + 1][0] = k++; } } for (i = 0; i < x; i++) for (j = 0; j < y; j++) if (mp[i][j] == -1) { if (is_ok(i + 1, j + 1) && mp[i][j] == -1 && c > 0) { c--; mp[i][j] = mp[i + 1][j] = mp[i][j + 1] = mp[i + 1][j + 1] = k++; continue; } if (is_ok(i + 1, j + 1) && mp[i][j] == -1 && a >= 2) { a -= 2; mp[i][j] = mp[i][j + 1] = k++; mp[i + 1][j] = mp[i + 1][j + 1] = k++; continue; } if (is_ok(i + 1, j + 1) && mp[i][j] == -1 && b >= 2) { b -= 2; mp[i][j] = mp[i + 1][j] = k++; mp[i][j + 1] = mp[i + 1][j + 1] = k++; continue; } dame = 1; } if (a < 0 || b < 0 || c < 0) dame = 1; if (dame) puts("IMPOSSIBLE"); else { for (i = 0; i < x; i++) for (j = 0; j < y; j++) res[i][j] = 0; for (i = 0; i < x; i++) for (j = 0; j < y; j++) if (!res[i][j]) { for (k = 0; k < 26; k++) chk[k] = 0; nul(i, j, mp[i][j], -5, -1); for (k = 0; k < 26; k++) if (!chk[k]) break; nul(i, j, -5, -1, k); } for (i = 0; i < x; i++) res[i][y] = '\0'; for (i = 0; i < x; i++) puts(res[i]); } return 0; }
2,000
CPP
from math import * a=list(map(int,input().split())) m=max(3*a[0]/10,a[0]-a[0]/250*a[2]) v=max(3*a[1]/10,a[1]-a[1]/250*a[3]) if m>v: print("Misha") elif m<v: print("Vasya") else: print("Tie")
900
PYTHON3
n = int(input()) l = [int(i) for i in input().split()] a = max(l) b = min(l) if a == b: print(0, int(n * (n - 1) / 2)) else: print(a - b, l.count(a) * l.count(b))
1,300
PYTHON3
#1092B Team forming n=int(input()) a=[int(x) for x in input().split()] a.sort() #print(a) count=0 for i in range(0,n-1,2): count=count+(a[i+1]-a[i]) print(count)
800
PYTHON3
# ------------------- fast io -------------------- import os import sys from io import BytesIO, IOBase 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") # ------------------- fast io -------------------- import math vals=list(map(int,input().split())) n=vals[0] s=vals[1] initials=s #im thinking if n*(n+1)//2 >=s then not possible if n>s//2: print("NO") else: print("YES") outy=[] kval=0 for j in range(1,s): if n>1: outy.append(str(1)) kval+=j s-=1 n-=1 else: break outy.append(str(s)) print(" ".join(outy)) print(s-1)
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; const int Mod = 1e9 + 7; int C[4005][4005], dp[4005][4005]; void calc(int &x, int y) { x += y; if (x > Mod) x -= Mod; } int main() { int n, ans = 1; scanf("%d", &n); C[0][0] = 1; for (int i = 1; i <= n; i++) { C[i][0] = 1; for (int j = 1; j < i; j++) calc(C[i][j], (C[i - 1][j - 1] + C[i - 1][j]) % Mod); C[i][i] = 1; } for (int i = 1; i <= n; i++) { dp[i][1] = 1; for (int j = 1; j < i; j++) dp[i][j] = (dp[i - 1][j - 1] + 1LL * dp[i - 1][j] * j % Mod) % Mod; dp[i][i] = 1; } for (int i = 1; i < n; i++) { int sum = 0; for (int j = 1; j <= i; j++) calc(sum, dp[i][j]); calc(ans, 1LL * sum * C[n][i] % Mod); } printf("%d\n", ans); return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; int n, k, a[200], b[200]; int s[200]; int ans; int main() { scanf("%d %d", &n, &k); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); s[i] = a[i]; if (i > 0) s[i] += s[i - 1]; } ans = a[0]; for (int i = 0; i < n; ++i) for (int j = i; j < n; ++j) { for (int x = 0; x < n; ++x) b[x] = a[x]; int ss = s[j]; if (i > 0) ss -= s[i - 1]; for (int x = 0; x < k; ++x) { int smin = i; for (int q = i; q <= j; ++q) { if (b[q] < b[smin]) smin = q; } int omax = -1; for (int q = 0; q < i; ++q) if (omax == -1) omax = q; else if (b[q] > b[omax]) omax = q; for (int q = j + 1; q < n; ++q) if (omax == -1) omax = q; else if (b[q] > b[omax]) omax = q; if ((omax != -1) && (b[omax] > b[smin])) { ss += b[omax] - b[smin]; swap(b[omax], b[smin]); } } if (ss > ans) ans = ss; } printf("%d\n", ans); return 0; }
1,500
CPP
T = int(input()) for i in range(T): print(int(input()))
900
PYTHON3
n,k,t = map(int, input().split()) print(t if t<=k else k if t<=n else k-(t%n))
800
PYTHON3
#include <bits/stdc++.h> int main() { int MIN(int a, int b); int tc, d, m; scanf("%d", &tc); while (tc--) { scanf("%d%d", &d, &m); int h = 0; for (int i = 0; i <= 31; i++) { if (d & (1 << i)) h = i; } int ans = 1; for (int i = 0; i <= h; i++) { int amount = MIN((1 << (i + 1)) - 1, d) - (1 << i) + 2; ans = (1LL * ans * amount) % m; } ans--; if (ans < 0) ans += m; printf("%d\n", ans); } return 0; } int MIN(int a, int b) { return a < b ? a : b; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; template <class T> T By(T x, T y, T P) { T F1 = 0; while (y) { if (y & 1) { F1 += x; if (F1 < 0 || F1 >= P) F1 -= P; } x <<= 1; if (x < 0 || x >= P) x -= P; y >>= 1; } return F1; } template <class T> T Mul(T x, T y, T P) { T F1 = 1; x %= P; while (y) { if (y & 1) { F1 = By(F1, x, P); } x = By(x, x, P); y >>= 1; } return F1; } template <class T> T Gcd(T x, T y) { if (y == 0) return x; T z; while (z = x % y) { x = y, y = z; } return y; } struct EDGE { int T; EDGE *Nxt; }; template <class T> void UpdataMin(T &x, T y) { if (y < x) { x = y; } } template <class T> void UpdataMax(T &x, T y) { if (x < y) { x = y; } } int Dp[61][61][61]; int Map[61][61][61]; int NM[61][61]; int N, M, R, I, J, K; int main() { memset(Dp, 63, sizeof(Dp)); memset(NM, 63, sizeof(NM)); scanf("%d%d%d", &N, &M, &R); for (int i = 0; i < (M); ++i) { for (int j = 1; j <= (N); ++j) for (int k = 1; k <= (N); ++k) scanf("%d", &Map[i][j][k]); for (int l = 1; l <= (N); ++l) for (int j = 1; j <= (N); ++j) for (int k = 1; k <= (N); ++k) if (Map[i][j][l] + Map[i][l][k] < Map[i][j][k]) Map[i][j][k] = Map[i][j][l] + Map[i][l][k]; for (int j = 1; j <= (N); ++j) for (int k = 1; k <= (N); ++k) UpdataMin(NM[j][k], Map[i][j][k]); } for (int i = 1; i <= (N); ++i) for (int j = 1; j <= (N); ++j) Dp[0][i][j] = NM[i][j]; for (int i = 1; i <= (60); ++i) for (int j = 1; j <= (N); ++j) for (int k = 1; k <= (N); ++k) for (int l = 1; l <= (N); ++l) UpdataMin(Dp[i][j][l], Dp[i - 1][j][k] + NM[k][l]); while (R--) { scanf("%d%d%d", &I, &J, &K); if (K > 60) K = 60; printf("%d\n", Dp[K][I][J]); } scanf("%d", &N); return 0; }
1,800
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fll; long long query(vector<int>& v) { if (!v.size()) return 0; cout << "? " << v.size() << " "; for (int i : v) cout << i + 1 << " "; cout << endl; long long ans; cin >> ans; return ans; } int main() { int n; cin >> n; vector<int> mask; vector<vector<int>> subset(13); for (int i = 0; i < (1 << 13); i++) { if (__builtin_popcount(i) != 6) continue; for (int j = 0; j < 13; j++) if (i >> j & 1) subset[j].push_back(mask.size()); mask.push_back(i); if (mask.size() == n) break; } vector<long long> ou(13); for (int i = 0; i < 13; i++) ou[i] = query(subset[i]); vector<long long> ans(n); for (int i = 0; i < n; i++) for (int j = 0; j < 13; j++) if (!(mask[i] >> j & 1)) ans[i] |= ou[j]; cout << "! "; for (long long i : ans) cout << i << " "; cout << endl; exit(0); }
2,800
CPP
#include <bits/stdc++.h> int main() { int a; scanf("%d", &a); char* v = NULL; int n = 0; char ch; do { ch = getchar(); if (n == 0 && ch == '\n') { continue; } if (ch == '\n' || ch == EOF) { break; } ++n; v = (char*)realloc(v, sizeof(char) * n); v[n - 1] = ch; } while (1); int nc = 26; char* vc = NULL; vc = (char*)malloc(sizeof(char) * nc); int i = 0; for (;;) { if (i >= nc) { break; } vc[i] = 'A' + i; ++i; } int num = 0; int max = 0; int i5; i = 0; for (;;) { if (i >= n) { break; } i5 = 0; for (;;) { if (i5 >= nc) { break; } if (v[i] == vc[i5]) { ++num; } ++i5; } if (num > max) { max = num; } if (v[i] == ' ') { num = 0; } ++i; } printf("%d", max); free(v); free(vc); }
800
CPP
t=int(input()) result=0 for i in range(t): st=input() if st.find("+")!=-1: result+=1 else: result-=1 print(result)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int t, n; cin >> t; while (t--) { string str = " "; cin >> n; while (n >= 4) str += "1", n -= 2; if (n == 3) str += "7", n -= 3; else if (n == 2) str += "1", n -= 2; reverse(str.begin(), str.end()); cout << str << endl; } return 0; }
900
CPP
for _ in range(int(input())): a,b=map(int,input().split()) dif=abs(a-b) ans=0 ans+=dif//5 dif%=5 ans+=dif//2 dif%=2 ans+=dif print(ans)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[1005]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + n + 1); if (n % 2 == 1) { cout << a[(n + 1) / 2] << endl; } else { cout << a[n / 2] << endl; } return 0; }
800
CPP
x=input() y=input() x=x.split() n=int(x[0]) t=int(x[1]) y=y.split() d=[] for i in (y): d.append(86400-int(i)) flag=0 for i in range(n): if d[i]<t: t=t-d[i] else: flag=i+1 break print(flag)
800
PYTHON3
#include <bits/stdc++.h> const long long N = 2 * 1e5 + 10; const long long INF = 1e18; const long long inf = -1e18; const long long MOD = 1e9 + 7; const long long xesh = 31; using namespace std; long long n, a[211111], mx[211111], q, w, m, l, ans = 1, sh, i, o; int main() { cin >> o; while (o--) { cin >> n; sh = 0; for (i = 1; i <= n; ++i) { cin >> a[i]; mx[i] = 0; } cin >> m; for (i = 1; i <= m; ++i) { cin >> q >> w; mx[w] = max(q, mx[w]); } for (i = n - 1; i >= 1; --i) { mx[i] = max(mx[i], mx[i + 1]); } for (i = 1, m = 0, l = 0; i <= n; ++i) { m = max(a[i], m); if (mx[i - l] < m) { l = i - 1; ans++; m = a[i]; } if (mx[1] < m) { sh = 1; break; } } if (sh == 1) cout << -1 << '\n'; else cout << ans << '\n'; ans = 1; } }
1,700
CPP
from os import path import sys,time, collections as c , math , pprint as p , itertools as it , operator as op maxx , localsys , mod = float('inf'), 0 , int(1e9 + 7) if (path.exists('input.txt')): sys.stdin=open('input.txt','r') ; sys.stdout=open('output.txt','w') input = sys.stdin.readline n = int(input()) ; s = list(map(int , input().split())) ; c = list(map(int , input().split())) ans = maxx for mid in range(1 , n - 1): l = [maxx] + [c[i] for i in range(mid) if s[i] < s[mid]] r = [maxx] + [c[i] for i in range(mid+1 , n) if s[i] > s[mid]] ans = min(ans , min(l) + c[mid] + min(r)) print(ans if ans != float('inf') else -1) # for _ in range(int(input())): # n = int(input()) # a , i , j ,ok = [[int(i) for i in input().rstrip('\n')] for _ in range(2)] , 0 , 0 , True # while j < n : # if a[i][j] > 2: # if a[i^1][j] < 3: # break # else: # i^=1 # j+=1 # print('YES' if i==1 and j == n else 'NO') #for example suppose if you are at row 1 and standing on the curled tile which will obviously lead to another row #and if this row has ( | or - ) then obviously you have no other way to move forward #all other combinations are viable # n = int(input()) ; g = c.defaultdict(list) # for _ in range(n-1): # u , v = map(int , input().split()) # g[u].append(v) # g[v].append(u) # v , q , ans = [0]*(n+1) , [(1 ,1 ,0)] , 0 #p , cnt , height # while q : # p , cnt , h = q.pop() # v[p] , c = 1 , 0 # for i in g[p]: # if not v[i]: # c+=1 # if c == 0 : # ans+= cnt*h # else: # for i in g[p]: # if not v[i]: # q.append((i , cnt/c , h+1)) # v[i] = 1 # print(q,ans , p, cnt , h) # print('%.14f'%(ans)) # #probability of the horse taking the route to each of the child of the parent * length of the journey = expected value for that vertex # def ok(p , s): # cnt , need =0 , 0 # for i in s: # if p[need] == i: # cnt+=1 ; need ^= 1 # if cnt % 2 and p[0] != p[1]: # cnt-=1 # return cnt # for _ in range(int(input())): # s = input().rstrip('\n') ; n , ans = len(s) , maxx # for i in range(10): # for j in range(10): # ans = min(ans , n - ok((str(i) , str(j)), s)) # print(ans) #This problem was so easy oh gawd , so you can only make left cyclic shift = right cyclic shift , when there are at most #2 characters #(incase of 1 ch) at the same time if total string has only character it is valid no matter how you see it #(incase of 2 ch) all the characters at odd positions must be equal , and all the characters at even position must be equal # n , k = map(int , input().split()) ; s = input().rstrip('\n') # ans = a = b = j = 0 # for i in range(n): # a , b = (a+1 , b) if s[i] == 'a' else (a , b+1 ) # if min(a, b) > k : # a , b = (a -1 , b) if s[j] == 'a' else (a , b -1) ; j+=1 # else: # ans+=1 # print(ans) # #two - pointer method , if at any point min(a , b )> k then keep on decreasing from the beginning untill and unless you get min(a , b) # #less than k
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int x = 0; while (n--) { string s; cin >> s; if (s[1] == '+') x++; else x--; } cout << x; return 0; }
800
CPP
#include <bits/stdc++.h> int calculate(int n, int num); int main() { int n, m, count, t; scanf("%d %d", &n, &m); if (n >= m) printf("%d\n", n - m); else { printf("%d\n", calculate(n, m)); } return 0; } int calculate(int n, int num) { if (n >= num) { return n - num; } else { if (num % 2 == 0) { num = num / 2; if (n == num) return 1; else return 1 + calculate(n, num); } else { num = (num + 1) / 2; return 2 + calculate(n, num); } } }
1,400
CPP
#include <bits/stdc++.h> using namespace std; struct Task { int solve(string filePrefix) { cin.sync_with_stdio(false); cout.sync_with_stdio(false); long long n = -1; char ch = 'x'; cin >> n >> ch; ; ; const vector<int> inrowdelay = {4, 5, 6, 3, 2, 1}; const vector<int> interrowdelay = {0, 7, 0, 7}; --n; long long sol = 16 * (n / 4) + interrowdelay[n % 4] + inrowdelay[ch - 'a']; cout << sol << endl; return 0; } }; int solve(std::string s) { Task t; return t.solve(s); } int main(int argc, char *argv[]) { int ret = 0; ret = solve(""); return ret; }
1,200
CPP
h, l = input().split() h = int(h) l = int(l) ans = ((l*l) - (h*h)) / (2*h) print('%.13f' % ans)
1,000
PYTHON3
from sys import stdout, stdin, setrecursionlimit from bisect import insort,bisect_right,bisect_left from io import BytesIO, IOBase from collections import * from itertools import * from random import * from string import * from queue import * from heapq import * from math import * from re import * from os import * ####################################---fast-input-output----######################################### 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 = read(self._fd, max(fstat(self._fd).st_size, 8192)) 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 = read(self._fd, max(fstat(self._fd).st_size, 8192)) 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: 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") stdin, stdout = IOWrapper(stdin), IOWrapper(stdout) graph, mod, szzz = {}, 10**9 + 7, lambda: sorted(zzz()) def getStr(): return input() def getInt(): return int(input()) def listStr(): return list(input()) def getStrs(): return input().split() def isInt(s): return '0' <= s[0] <= '9' def input(): return stdin.readline().strip() def zzz(): return [int(i) for i in input().split()] def output(answer, end='\n'): stdout.write(str(answer) + end) def lcd(xnum1, xnum2): return (xnum1 * xnum2 // gcd(xnum1, xnum2)) dx = [-1, 1, 0, 0, 1, -1, 1, -1] dy = [0, 0, 1, -1, 1, -1, -1, 1] daysInMounth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] #################################################---Some Rule For Me To Follow---################################# """ --instants of Reading problem continuously try to understand them. --If you Know some-one , Then you probably don't know him ! --Try & again try, maybe you're just one statement away! """ ##################################################---START-CODING---############################################### a,b=zzz() if (a<b):print(-1) else:print(((a+b)/2)/((a+b)//(2*b)))
1,700
PYTHON3
tc = 1 while tc: tc-=1 n = int(input()) ls = list(map(int, input().split())) new = ls+ls res = 0 maxtill = 0 for item in new: if item == 1: maxtill+=1 else: maxtill = 0 if maxtill>res: res = maxtill print(res)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class T = long long int> T nxt() { T x; cin >> x; return x; } long long int pw(long long int a, long long int b, long long int mod) { if (!b) return 1; if (b & 1) return a * pw(a * a % mod, b / 2, mod) % mod; return pw(a * a % mod, b / 2, mod) % mod; } const long long int N = 1e5 + 10; const long long int MOD = 1e9 + 7; const long long int INF = 1e18; long long int w, h, n, m, mx[4] = {-INF, -INF, -INF, -INF}, now[4]; pair<long long int, long long int> A[N], B[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> w >> h >> n; for (int i = 0; i < n; i++) { cin >> A[i].first >> A[i].second; mx[0] = max(mx[0], A[i].first + A[i].second); mx[1] = max(mx[1], A[i].first - A[i].second); mx[2] = max(mx[2], -A[i].first + A[i].second); mx[3] = max(mx[3], -A[i].first - A[i].second); } cin >> m; long long int ans = INF, ind = -1; for (int j = 0; j < m; j++) { cin >> B[j].first >> B[j].second; long long int now = -INF; now = max(now, mx[0] - B[j].first - B[j].second); now = max(now, mx[1] - B[j].first + B[j].second); now = max(now, mx[2] + B[j].first - B[j].second); now = max(now, mx[3] + B[j].first + B[j].second); if (now < ans) { ans = now; ind = j; } } cout << ans << '\n' << ind + 1 << '\n'; return 0; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100005; set<int> indexs[maxn]; int n, a[maxn], b[maxn]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); indexs[a[i]].insert(i); b[i] = a[i]; } sort(b, b + n); int sz = n, now = 0; long long ans = n; for (int i = 0; i < n; i++) { int nowNum = b[i]; set<int>::iterator it = indexs[nowNum].lower_bound(now); if (it == indexs[nowNum].end()) { ans += sz; now = 0; it = indexs[nowNum].lower_bound(now); } now = *it; indexs[nowNum].erase(it); sz--; } printf("%lld\n", ans); return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; bool mp[517][517]; int in[517], n; string ans = ""; priority_queue<int, vector<int>, greater<int> > q; void toposort(int n) { for (int i = 0; i < 26; i++) { if (in[i] == 0) q.push(i); } int c = 1; while (!q.empty()) { int v = q.top(); q.pop(); if (c != n) { ans += (v + 'a'); c++; } else ans += (v + 'a'); for (int i = 0; i < 26; i++) { if (!mp[v][i]) continue; in[i]--; if (!in[i]) q.push(i); } } } string s[105]; int main() { scanf("%d", &n); int lena, lenb, pos; for (int i = 0; i < n; ++i) cin >> s[i]; for (int i = 0; i < n - 1; ++i) { lena = s[i].length(); for (int j = i + 1; j < n; ++j) { lenb = s[j].length(); for (pos = 0; pos < lena && pos < lenb; ++pos) { if (s[i][pos] != s[j][pos]) { break; } } if (pos == lena) continue; else if (pos == lenb) { puts("Impossible"); return 0; } else { if (mp[s[i][pos] - 'a'][s[j][pos] - 'a']) continue; mp[s[i][pos] - 'a'][s[j][pos] - 'a'] = 1; in[s[j][pos] - 'a']++; } } } toposort(26); if (ans.length() != 26) puts("Impossible"); else cout << ans << endl; return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; bool codejam = 0; void solve(); int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(20); long long t = 1; cin >> t; long long tt = t; while (t--) { if (codejam) cout << "Case #" << tt - t << ": "; solve(); } return 0; } void solve() { string s[3]; for (long long i = 0; i < 3; ++i) cin >> s[i]; long long j = 0; for (long long i = 0; i < (long long)s[1].size(); ++i) { if (j == (long long)s[0].size()) break; if (s[1][i] == s[0][j]) ++j; } if (j != (long long)s[0].size()) { cout << "NO\n"; return; } long long cnt[3][26]; memset(cnt, 0, sizeof cnt); for (long long i = 0; i < 3; ++i) { for (auto j : s[i]) { cnt[i][j - 'a'] += 1; } } for (long long i = 0; i < 26; ++i) { if (cnt[0][i] + cnt[2][i] < cnt[1][i]) { cout << "NO\n"; return; } } cout << "YES\n"; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; using ii = long long; int main() { int t; cin >> t; while (t--) { int n, x, y; cin >> n >> x; vector<pair<int, int>> v(n); for (int i = 0; i < n; ++i) { int a, b; cin >> a >> b; v[i] = {b - a, a}; } int o = 0; for (int i = 0; i < n; ++i) { o = max(o, v[i].second); } if (o >= x) { cout << 1 << "\n"; continue; } sort(v.begin(), v.end()); y = -v[0].first; if (y <= 0) { cout << -1 << "\n"; continue; } int f = 0; if (o > y) { f++; x -= o; } f += x / y; f += !!(x % y); cout << f << "\n"; } }
1,600
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, m, k, c = 0, i; map<int, int> mp; map<int, int>::iterator it; scanf("%d %d %d", &n, &m, &k); for (i = 0; i < n; i++) { int x; scanf("%d", &x); mp[-x]++; } for (i = 0; i < m; i++) { int x; scanf("%d", &x); mp[-x]--; } for (it = mp.begin(); it != mp.end(); it++) { c += it->second; if (c > 0) { puts("YES"); return 0; } } puts("NO"); return 0; }
1,600
CPP
#include <bits/stdc++.h> #define fast ios::sync_with_stdio(0), cin.tie(nullptr) #define all(v) v.begin(), v.end() #define x first #define y second using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int S = 3 * 100005, MOD = (int)1e9 + 7; const int mem_max = 0x3f; ll dp[S][2][2]; // 0 = L / 1 = R string load; int n; ll s(int v, int t, int from) { if (from == 2) return s(v, t, 0) + s(v, t, 1); if (dp[v][t][from] != -1) return dp[v][t][from]; if (t) { if (from) { if (load[v - 1] == 'R') return dp[v][t][from] = s(v - 1, !t, from) + 1; else return dp[v][t][from] = 0; } else { if (load[v] == 'L') return dp[v][t][from] = s(v + 1, !t, from) + 1; else return dp[v][t][from] = 0; } } else { if (from) { if (load[v - 1] == 'L') return dp[v][t][from] = s(v - 1, !t, from) + 1; else return dp[v][t][from] = 0; } else { if (load[v] == 'R') return dp[v][t][from] = s(v + 1, !t, from) + 1; else return dp[v][t][from] = 0; } } } void solve() { cin >> n >> load; memset(dp, -1, sizeof(dp[0]) * (n + 2)); dp[0][0][1] = dp[0][1][1] = 0; dp[n][0][0] = dp[n][1][0] = 0; for (int i = 0; i <= n; i++) cout << s(i, 0, 2) + 1 << ' '; cout << '\n'; } int main() { fast; int t; cin >> t; while (t--) solve(); return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int mod1 = 1e9 + 7; const int mod2 = 998244353; const int mod3 = 1e9; const double PI = acos(-1); const double eps = 1e-3; const int sq5 = 616991993; int t; const int maxn = 1e6 + 10; char s[maxn]; int pre[maxn]; int main() { cin >> t; while (t--) { scanf("%s", s + 1); int n = strlen(s + 1); for (int i = 1; i <= n; ++i) { pre[i] = pre[i - 1] + ((s[i] == '+') ? (1) : (-1)); } cout << endl; int last = -1; long long res = 0; for (int i = 1; i <= n; ++i) { if ((last == -1 || pre[i] < pre[last]) && (pre[i] < 0)) { res = res + (long long)i * (pre[last] - pre[i]); last = i; } } printf("%lld\n", res + n); } return 0; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; const int LOGN = 23; const int NMAX = 1e5 + 7; long long n, q, id[NMAX], tin[NMAX], we[NMAX], h[NMAX], par[LOGN][NMAX]; long long res = 0; vector<pair<long long int, long long int> > g[NMAX]; int Time = 0; void Dfs(int node, int p) { h[node] = h[p] + 1; par[0][node] = p; tin[node] = ++Time; id[Time] = node; for (pair<long long int, long long int> i : g[node]) { if (i.first != p) { we[i.first] = we[node] + i.second; Dfs(i.first, node); } } } int LCA(int u, int v) { if (h[u] < h[v]) swap(u, v); int d = h[u] - h[v]; for (int i = 0; i < LOGN; i++) if (d & (1 << i)) u = par[i][u]; if (u == v) return u; for (int i = LOGN - 1; i >= 0; i--) { if (par[i][u] != par[i][v]) { u = par[i][u]; v = par[i][v]; } } return par[0][u]; } long long dist(int u, int v) { return we[u] + we[v] - 2 * we[LCA(u, v)]; } set<long long> second; int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 1; i <= n - 1; i++) { int u, v, w; cin >> u >> v >> w; g[u].push_back({v, w}); g[v].push_back({u, w}); } Dfs(1, 0); for (int i = 1; i < LOGN; i++) for (int j = 1; j <= n; j++) par[i][j] = par[i - 1][par[i - 1][j]]; cin >> q; while (q--) { char c; cin >> c; if (c == '+') { int x; cin >> x; if (second.size() == 0) { second.insert(tin[x]); res = 0; continue; } set<long long>::iterator it = second.upper_bound(tin[x]); int posle = it == second.end() ? *second.begin() : *it; ; int pre = it == second.begin() ? *second.rbegin() : *--it; long long dodaj = (dist(x, id[posle]) + dist(x, id[pre]) - dist(id[pre], id[posle])) / 2; res += dodaj; second.insert(tin[x]); } if (c == '-') { int x; cin >> x; if (second.size() == 1) { second.erase(tin[x]); res = 0; continue; } second.erase(tin[x]); set<long long>::iterator it = second.upper_bound(tin[x]); int posle = it == second.end() ? *second.begin() : *it; int pre = it == second.begin() ? *second.rbegin() : *--it; long long oduzmi = (dist(x, id[posle]) + dist(x, id[pre]) - dist(id[pre], id[posle])) / 2; res -= oduzmi; } if (c == '?') cout << res << endl; } return 0; }
3,100
CPP
# -*- coding: utf-8 -*- """ Created on Wed Jun 13 22:28:12 2018 @author: Arsanuos """ def main(): n = int(input()) arr = [int(t) for t in input().split()] dollar = arr[0] energy = 0 start = 0 while start < len(arr) - 1: if arr[start] < arr[start + 1]: if energy >= (arr[start + 1] - arr[start]) : energy -= (arr[start + 1] - arr[start]) else: dollar += (arr[start + 1] - arr[start] - energy) energy = 0 else: energy += (arr[start] - arr[start + 1]) start += 1 print(dollar) if __name__ == "__main__": main()
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; long long pre[4000 + 10]; map<long long, long long> _map; int main() { long long a; cin >> a; string sh; cin >> sh; long long n = sh.length(); sh = " " + sh; for (long long i = 1; i <= n; i++) { pre[i] = pre[i - 1] + (sh[i] - '0'); } for (long long i = 1; i <= n; i++) { for (long long j = i; j <= n; j++) { _map[pre[j] - pre[i - 1]]++; } } long long res = 0; if (a == 0) { res = (n * (n + 1)) / 2 * _map[0] * 2 - _map[0] * _map[0]; cout << res << endl; return 0; } for (long long i = 1; i <= n; i++) { for (long long j = i; j <= n; j++) { long long tmp = pre[j] - pre[i - 1]; if (tmp == 0) continue; if (a % tmp == 0) { res += _map[a / tmp]; } } } cout << res << endl; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; const int iinf = 0x3f3f3f3f; const long long linf = 0x3f3f3f3f3f3f3f3f; const int N = 1e6; long long a, b, ns; int n, m, g, cnt[N][26], sum[N]; string sn, sm; int gcd(int a, int b) { return a ? gcd(b % a, a) : b; } int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> a >> b >> sn >> sm, n = int((sn).size()), m = int((sm).size()), g = gcd(n, m); for (int i(0); i < (n); ++i) cnt[i % g][sn[i] - 'a']++, sum[i % g]++; for (int i(0); i < (m); ++i) ns += sum[i % g] - cnt[i % g][sm[i] - 'a']; ns *= a / (m / g); cout << ns << '\n'; return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 0; i < t; i++) { long long int n; cin >> n; long long int answer; answer = floor(sqrt(n)) + floor(cbrt(n)) - floor(cbrt(sqrt(n))); cout << answer << endl; } return 0; }
800
CPP
n,x,y=map(int,input().split()) s=input() cou=0 j=0 for i in range(n-1,-1,-1): #print(s[i]) if(j==x): #print(s[i],j,x) break if(j==y and s[i]=='0'): cou+=1 elif(j!=y and s[i]=='1'): cou+=1 j+=1 print(cou)
1,100
PYTHON3
import os import sys from io import BytesIO, IOBase 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") ########################################################## from collections import Counter #'%.9f'%ans def nCk(n, k): res = 1 for i in range(1, k + 1): res = res * (n - i + 1) // i return res import math import bisect for _ in range(int(input())): n = int(input()) a=[] b=[] c=[] #n=int(input()) for i in range(n): l,r=map(int, input().split()) a.append(l) b.append(r) c.append([l,r]) a.sort() b.sort() ans=n for i in range(n): v1=bisect.bisect_left(b,c[i][0]) v2=n-bisect.bisect_right(a,c[i][1]) ans=min(ans,v1+v2) print(ans) #arr=list(map(int, input().split()))
1,800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; const int inf = 0x3f3f3f3f; int n, m, mx, ans = inf; char s[N]; int a[N], tmp[1 << 17], f[1 << 17]; int cst[1 << 17], dp[1 << 17]; int g[17][17]; void dfs(int x) { if (tmp[x]) return; tmp[x] = 1; for (int y = x; y; y ^= y & -y) dfs(x ^ y & -y); } void dfs1(int x) { if (f[x] || tmp[x]) return; tmp[x] = 1; ans = min(ans, cst[x]); for (int y = x; y; y ^= y & -y) dfs1(x ^ y & -y); } int main() { scanf("%d%d%s", &n, &m, s + 1); mx = (1 << m) - 1; for (int i = 1; i <= n; i++) cst[1 << (a[i] = s[i] - 'a')]++; for (int i = 1; i <= mx; i++) cst[i] = cst[i ^ i & -i] + cst[i & -i]; for (int i = 0; i < m; i++) for (int j = 0; j < m; j++) scanf("%d", &g[i][j]); for (int i = 0; i < m; i++) for (int j = 0; j < m; j++) if (!g[i][j]) { int now = 0, pre = -1; for (int k = 1; k <= n; k++) { if (a[k] == i || a[k] == j) { if (a[k] == i && pre == j || a[k] == j && pre == i) dfs(mx ^ now); now = 0, pre = a[k]; } else now |= 1 << a[k]; } int t = 1 << i | 1 << j; for (int k = 0; k <= mx; k++) { if ((k & t) == t) f[k] |= tmp[k]; tmp[k] = 0; } } dfs1(mx); printf("%d\n", ans); }
2,500
CPP
n,m,k=map(int,input().split()) if n%k!=0: n=n//k+1 else: n=n//k if m%k!=0: m=m//k+1 else: m=m//k print(m*n)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; long long expo(long long base, long long exponent, long long mod) { long long ans = 1; while (exponent != 0) { if ((exponent & 1) == 1) { ans = ans * base; ans = ans % mod; } base = base * base; base %= mod; exponent >>= 1; } return ans % mod; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int TESTS = 1; while (TESTS--) { long long h, w; cin >> h >> w; vector<long long int> r(h), c(w); for (long long int i = 0; i < h; i++) cin >> r[i]; for (long long int i = 0; i < w; i++) cin >> c[i]; set<pair<long long, long long> > s; long long ans = 0; for (long long int i = 0; i < h; i++) { if (r[i] != w && c[r[i]] > i) { cout << 0; return 0; } } for (long long int i = 0; i < w; i++) { if (c[i] != h && r[c[i]] > i) { cout << 0; return 0; } } for (long long int i = 0; i < h; i++) { for (long long int j = 0; j < w; j++) { if ((j > r[i]) && (i > c[j])) ans++; } } cout << expo(2, ans, 1000000007); } return 0; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c; scanf("%d %d %d", &a, &b, &c); printf("%d\n", b * c + (a - 1) * (b + c - 1)); return 0; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; long long n, a, b, i; int main() { cin >> n >> a >> b; for (i = 0; i <= n; i += a) { if ((n - i) % b == 0) { cout << "YES\n"; cout << i / a << " " << (n - i) / b; return 0; } } cout << "NO\n"; return 0; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; const int dl = 1e9 + 7; const int M = 1e5 + 10; pair<pair<int, int>, int> lme[M]; int n, k, h; bool check(double x) { int sel = 1; for (int i = 0; i < n && sel <= k; i++) { if (double(sel) * h <= x * lme[i].first.second) sel++; } return sel > k; } int main() { cin >> n >> k >> h; for (int i = 0; i < n; i++) { lme[i].second = i; cin >> lme[i].first.first; } for (int i = 0; i < n; i++) { cin >> lme[i].first.second; } sort(lme, lme + n); double low = -1, hi = double(h) * k; for (int i = 0; i < 1000; i++) { double mid = (hi + low) / 2; if (check(mid)) hi = mid; else low = mid; } vector<int> ans; int sel = 1; for (int i = 0; i < n && sel <= k; i++) { if (double(sel) * h <= hi * lme[i].first.second) { sel++; ans.push_back(lme[i].second + 1); } } for (int x : ans) { cout << x << " "; } return 0; }
2,000
CPP
w = int(input()) print("YES" if (w % 2 == 0 and w > 2) else "NO")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n, m, k; long long a[1005][1005]; vector<pair<long long, pair<long long, long long>>> b; pair<long long, long long> p[1005][1005]; long long r[1005][1005]; void create(long long x, long long y) { p[x][y] = {x, y}; r[x][y] = 1; } pair<long long, long long> find(pair<long long, long long> x) { if (p[x.first][x.second] != x) p[x.first][x.second] = find(p[x.first][x.second]); return p[x.first][x.second]; } void merge(pair<long long, long long> x, pair<long long, long long> y) { if (a[x.first][x.second] > a[y.first][y.second]) return; pair<long long, long long> px = find(x); pair<long long, long long> py = find(y); if (px == py) return; if (r[px.first][px.second] >= r[py.first][py.second]) { p[py.first][py.second] = p[px.first][px.second]; r[px.first][px.second] += r[py.first][py.second]; } else { p[px.first][px.second] = p[py.first][py.second]; r[py.first][py.second] += r[px.first][px.second]; } } long long cnt; long long ans[1005][1005]; long long vis[1005][1005]; void dfs(long long xx, long long yy, long long v) { if (vis[xx][yy]) return; vis[xx][yy] = 1; if (a[xx][yy] < v || cnt <= 0) { a[xx][yy] = 0; return; } ans[xx][yy] = v; cnt--; dfs(xx - 1, yy, v); dfs(xx, yy - 1, v); dfs(xx + 1, yy, v); dfs(xx, yy + 1, v); } void solve() { cin >> n >> m >> k; for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= m; j++) { cin >> a[i][j]; create(i, j); b.push_back({a[i][j], {i, j}}); } } sort(b.begin(), b.end()); reverse(b.begin(), b.end()); long long val = 0, nx = 1, ny = 1; cnt = -1; pair<long long, long long> pk; for (auto now : b) { nx = now.second.first; ny = now.second.second; merge({nx, ny}, {nx + 1, ny}); merge({nx, ny}, {nx - 1, ny}); merge({nx, ny}, {nx, ny + 1}); merge({nx, ny}, {nx, ny - 1}); if (k % now.first == 0) { pk = find({nx, ny}); if (k / now.first <= r[pk.first][pk.second]) { cnt = k / now.first; val = now.first; break; } } } if (cnt == -1) { cout << "NO"; return; } cout << "YES\n"; dfs(nx, ny, val); for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= m; j++) cout << ans[i][j] << " "; cout << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long tests = 1; while (tests--) { solve(); } return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; vector<int> s[maxn]; vector<int> g[maxn]; queue<int> q; int in[maxn]; int n, m; int mark = 0; int cap[maxn]; vector<int> ans; void toposort() { for (int i = 1; i <= m; i++) { if (!in[i]) q.push(i); } while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (v > u) { if (cap[u]) mark = 1; else cap[v] = 1; } else if (cap[u]) cap[v] = 1; in[v]--; if (in[v] == 0) { q.push(v); } } } for (int i = 1; i <= m; i++) { if (in[i]) { mark = 1; break; } } } int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) { int l; scanf("%d", &l); for (int j = 1; j <= l; j++) { int x; scanf("%d", &x); s[i].push_back(x); } } memset(in, 0, sizeof(in)); for (int i = 1; i < n; i++) { int j; int same = 1; for (j = 0; j < s[i].size() && j < s[i + 1].size(); j++) { if (s[i][j] == s[i + 1][j]) continue; same = 0; g[s[i + 1][j]].push_back(s[i][j]); in[s[i][j]]++; break; } if (same && s[i].size() > s[i + 1].size()) mark = 1; } toposort(); if (mark) { printf("No\n"); } else { printf("Yes\n"); for (int i = 1; i <= m; i++) { if (cap[i]) ans.push_back(i); } printf("%d\n", ans.size()); for (int i = 0; i < ans.size(); i++) { printf("%d", ans[i]); if (i != ans.size()) printf(" "); } printf("\n"); } return 0; }
2,100
CPP
def maxProduct(arr, n): arr.sort() return max([arr[0] * arr[1] * arr[2] * arr[3] * arr[4], arr[0] * arr[1] * arr[2] * arr[3] * arr[n-1], arr[0] * arr[1] * arr[2] * arr[n-2] * arr[n-1], arr[0] * arr[1] * arr[n-3] * arr[n-2] * arr[n-1], arr[0] * arr[n-4] * arr[n-3] * arr[n-2] * arr[n-1], arr[n-1] * arr[n-2] * arr[n-3] * arr[n-4] * arr[n-5]]) for _ in range(int(input())): n = int(input()) arr = list(map(int,input().split())) if n==5: prod = 1 for i in arr: prod*=i print(prod) else: print(maxProduct(arr,n))
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; char *a = new char[3000000]; int y = 0, q, s[500][500], d[500][500], f[500][500]; bool d1[500][500]; void l(int w, int e) { s[w][e]++; if (!d[w][e]) { d[w][e] = 1; a[y] = '1'; y++; } if (f[w][e] & 1) if (!s[w][e - 1]) { s[w][e]++; a[y] = 'L'; y++; l(w, e - 1); a[y] = 'R'; y++; } if (f[w][e] & 2) if (!s[w - 1][e]) { s[w][e]++; a[y] = 'U'; y++; l(w - 1, e); a[y] = 'D'; y++; } if (f[w][e] & 4) if (!s[w][e + 1]) { s[w][e]++; a[y] = 'R'; y++; l(w, e + 1); a[y] = 'L'; y++; } if (f[w][e] & 8) if (!s[w + 1][e]) { s[w][e]++; a[y] = 'D'; y++; l(w + 1, e); a[y] = 'U'; y++; } a[y] = '2'; y++; d[w][e] = 0; if (d1[w][e]) { int r; if ((f[w][e] & 1) == 0) for (r = e + 1; r < q; r++) { f[w][r] -= 1; if ((d[w][r]) && (d1[w][r])) break; } if ((f[w][e] & 2) == 0) for (r = w + 1; r < q; r++) { f[r][e] -= 2; if ((d[r][e]) && (d1[r][e])) break; } if ((f[w][e] & 4) == 0) for (r = e - 1; r >= 0; r--) { f[w][r] -= 4; if ((d[w][r]) && (d1[w][r])) break; } if ((f[w][e] & 8) == 0) for (r = w - 1; r >= 0; r--) { f[r][e] -= 8; if ((d[r][e]) && (d1[r][e])) break; } } } int main() { int w, e, r, t, c, v; scanf("%d%d%d", &q, &c, &v); c--; v--; for (w = 0; w < q; w++) for (e = 0; e < q; e++) { scanf("%d", &d[w][e]); d1[w][e] = d[w][e]; s[w][e] = f[w][e] = 0; } for (w = 0; w < q; w++) { r = 0; for (e = 0; e < q; e++) { f[w][e] += r; if (d[w][e]) r = 1; } r = 0; for (e = q - 1; e >= 0; e--) { f[w][e] += r; if (d[w][e]) r = 4; } } for (e = 0; e < q; e++) { r = 0; for (w = 0; w < q; w++) { f[w][e] += r; if (d[w][e]) r = 2; } r = 0; for (w = q - 1; w >= 0; w--) { f[w][e] += r; if (d[w][e]) r = 8; } } l(c, v); for (w = 0; w < q; w++) for (e = 0; e < q; e++) if ((d[w][e]) && (!s[w][e])) { printf("NO"); return 0; } a[y] = 0; printf("YES\n%s", a); return 0; }
2,400
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> x(n); vector<int> y(n); vector<vector<bool>> t(2001, vector<bool>(2001)); for (int i = 0; i < n; i++) { int xp, yp; cin >> xp; cin >> yp; xp += 1000; yp += 1000; t[xp][yp] = true; x[i] = xp; y[i] = yp; } int cont = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { bool sirve = true; int fst = x[i] + x[j]; if (fst % 2 != 0) { sirve = false; } else { fst /= 2; } int snd = y[i] + y[j]; if (snd % 2 != 0) { sirve = false; } else { snd /= 2; } if (sirve && t[fst][snd]) { cont++; } } } cout << cont; return 0; }
1,300
CPP
import sys t = int(sys.stdin.readline().strip()) for h in range(t): n = int(sys.stdin.readline().strip()) arr = [[0 for j in range(n)] for i in range(n)] tl = [0, 0] bl = [n-1, 0] tr = [0, n-1] br = [n-1, n-1] pt = [tl, bl, tr, br] for k in range(n//2): for j in range(len(pt)): arr[pt[j][0]][pt[j][1]] = 1 pt[0][0] += 1 pt[0][1] += 1 pt[1][0] -= 1 pt[1][1] += 1 pt[2][0] += 1 pt[2][1] -= 1 pt[3][0] -= 1 pt[3][1] -= 1 if n % 2 == 1: arr[n//2][0] = 1 arr[0][n//2] = 1 arr[n-1][n // 2] = 1 arr[n//2][n-1] = 1 for j in arr: for k in j: print(k, end=' ') print('')
900
PYTHON3
s=input() lc,up=0,0 for i in s: if i.isupper(): up+=1 else: lc+=1 print(s.upper() if up>lc else s.lower())
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { string s; getline(cin, s); long long int a[s.size()]; vector<long long int> v; for (long long int i = 0; i < s.size(); i++) { if (i == 0) { vector<long long int> tp(8, 0); long long int j = 0; long long int k = s[i]; while (k) { tp[j] = (k >> 0) & 1; k = k >> 1; j++; } long long int ans = 0; reverse(tp.begin(), tp.end()); for (long long int l = 0; l < 8; l++) { ans = ans + tp[l] * pow(2, l); } v.push_back(ans); a[i] = 256 - ans; continue; } vector<long long int> tp(8, 0); long long int j = 0; long long int k = s[i]; while (k) { tp[j] = (k >> 0) & 1; k = k >> 1; j++; } long long int ans = 0; reverse(tp.begin(), tp.end()); for (long long int l = 0; l < 8; l++) { ans = ans + tp[l] * pow(2, l); } a[i] = (v[i - 1] - ans + 256) % 256; v.push_back(ans); } for (long long int i = 0; i < s.size(); i++) cout << a[i] << "\n "; return 0; }
1,300
CPP
t=int(input()) for _ in range(t): n=int(input()) a=list(map(int,input().split())) c=0 st=[] for i in range(n): if a[i]%3==0: c=c+1 else: st.append(a[i]) for i in range(len(st)): st[i]=st[i]%3 st.sort() c1=st.count(1) c2=st.count(2) if c1>=c2: c=c+c2 c=c+(c1-c2)//3 else: c=c+c1 c=c+(c2-c1)//3 print(c)
1,100
PYTHON3
n=list(map(int,input().split())) n1=list(map(int,input().split())) lst=[] lst1=[] s=0 for i in n1: lst.append(i) lst.sort() for j in lst: s=s+j if(s>n[1]): break lst1.append(n1.index(j)+1) n1.insert(n1.index(j),'') n1.remove(n1[n1.index(j)]) if(lst1==[]): print(0) else: print(len(lst1)) for k in lst1: print(k,end=' ')
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m, k; int arr[55][20005]; inline int query(int i, int l, int r) { return arr[i][r] - (l == 1 ? 0 : arr[i][l - 1]); } int dp[55][20005], suf[55][20005]; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> arr[i][j], arr[i][j] += arr[i][j - 1]; for (int i = 2; i <= n + 1; i++) { int now = 0; for (int j = k; j <= m; j++) { int cover = query(i - 1, j - k + 1, j) + query(i, j - k + 1, j); dp[i][j] = now + cover; if (j + k <= m) dp[i][j] = max(dp[i][j], suf[i - 1][j + k] + cover); int l = max(k, j - k + 1), r = min(m, j + k - 1); for (int pre = l; pre < j; pre++) dp[i][j] = max(dp[i][j], dp[i - 1][pre] + cover - query(i - 1, j - k + 1, pre)); for (int pre = j; pre <= r; pre++) dp[i][j] = max(dp[i][j], dp[i - 1][pre] + cover - query(i - 1, pre - k + 1, j)); if (j - k + 1 >= k) now = max(now, dp[i - 1][j - k + 1]); } for (int j = m; j >= k; j--) suf[i][j] = max(dp[i][j], suf[i][j + 1]); } cout << suf[n + 1][k] << endl; return 0; }
2,300
CPP
n=int(input()) lst=[] upper=0 lower=0 c=0 for i in range(n): a=list(map(int,input().split())) upper+=a[0] lower+=a[1] lst.append(a) for i in range(len(lst)): upper=lower=0 for j in lst: upper+=j[0] lower+=j[1] if upper%2==0 and lower%2==0: break else: lst[i][1],lst[i][0]=lst[i][0],lst[i][1] c+=1 if upper%2==0 and lower%2==0: if c>=1: print(1) else: print(0) else: print(-1)
1,200
PYTHON3
n=input() n=int(n) s=input() a=[int(i) for i in s.split()] b=[int(i) for i in range(n)] for i in range(n): if(a==b): print("Yes") break for j in range(int((n+1)/2)): if(a[2*j]==n-1): a[2*j]=0 else: a[2*j]=a[2*j]+1 for j in range(int(n/2)): if(a[2*j+1]==0): a[2*j+1]=n-1 else: a[2*j+1]=a[2*j+1]-1 else: print("No")
1,100
PYTHON3
import sys import math from collections import Counter,defaultdict try: sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') except: pass def takeInput(): return [int(x) for x in input().strip().split()] t=int(input()) while t!=0: t-=1 n=int(input()) rounds=[] pow10=1 while n: d=n%10 if(d!=0): rounds.append(pow10*d) pow10*=10 n//=10 print(len(rounds)) print(*rounds)
800
PYTHON3
h, n = map(int, input().rstrip().split(" ")) print((n*n-h*h)/(2*h))
1,000
PYTHON3
a = input().lower() b = input().lower() for i in range(len(a)): if a[i] != b[i]: if a[i]<b[i]: print(-1) else: print(1) exit() print(0)
800
PYTHON3
v= int(input()) l=[] for _ in range(v): temp= list(map(int, input().split())) l.append(temp) count=0 flag=0 for i in range(3): for j in range(v): count+=l[j][i] if count==0: flag+=1 continue else: break if flag==3: print('YES') else: print('NO')
1,000
PYTHON3
#!/usr/bin/env python3 import re try: while True: s = input() n = int(s[s.rfind(' '):]) pos = s.count('+') + 1 neg = s.count('-') if n * pos - neg < n or pos - n * neg > n: print("Impossible") else: print("Possible") need = n - (pos - neg) prev = '+' first = True for m in re.finditer(r"[+-]", s): if first: first = False else: print(prev, end=' ') if prev == '+' and need > 0: x = min(need + 1, n) need -= x - 1 elif prev == '-' and need < 0: x = min(-need + 1, n) need += x - 1 else: x = 1 print(x, end=' ') prev = m.group() if not first: print(prev, end=' ') if prev == '+' and need > 0: x = min(need + 1, n) need -= x - 1 elif prev == '-' and need < 0: x = min(-need + 1, n) need += x - 1 else: x = 1 print(x, '=', n) except EOFError: pass
1,800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main(void) { int b, k, a, ans = 0; scanf("%d %d", &b, &k); if (b % 2 == 1) { for (int i = 0; i < k; i++) { scanf("%d", &a); ans += a; } } else { for (int i = 0; i < k; i++) scanf("%d", &ans); } if (ans % 2 == 1) printf("odd"); else printf("even"); return 0; }
900
CPP
#dummy n = int(input()) a = list(map(int, input().split())) N = 100001 d = [-1] * N p = [-1] * N for i in range(n): if p[a[i]] == -1: d[a[i]] = 0 else: if i - p[a[i]] != d[a[i]]: if d[a[i]] == 0: d[a[i]] = i - p[a[i]] else: d[a[i]] = -1 p[a[i]] = i count = 0 for i in range(N): if d[i] != -1: count += 1 print(count) print('\n'.join("{0} {1}".format(i, d[i]) for i in range(N) if d[i] != -1))
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; string kb = "qwertyuiopasdfghjkl;zxcvbnm,./"; char dir; string m; int main() { ios_base::sync_with_stdio(false); cin >> dir >> m; int del = (dir == 'R') ? -1 : 1; for (int i = 0; i < m.length(); i++) { for (int j = 0; j < kb.length(); j++) { if (m[i] == kb[j]) cout << kb[j + del]; } } return 0; }
900
CPP
t = int(input()) for _ in range(t): a = int(input()) h = list(map(int, input().split())) mi = 1e9 + 1 ma = 0 ans = 0 f = 0 if a > 2: for i in range(1, a - 1): if h[i - 1] == -1 and h[i] != -1: mi = min(h[i], mi) ma = max(h[i], ma) f = 1 elif h[i - 1] != -1 and h[i] == -1: mi = min(h[i - 1], mi) ma = max(h[i - 1], ma) f = 1 else: ans = max(ans, abs(h[i - 1] - h[i])) if h[i + 1] == -1 and h[i] != -1: mi = min(h[i], mi) ma = max(h[i], ma) f = 1 elif h[i + 1] != -1 and h[i] == -1: mi = min(h[i + 1], mi) ma = max(h[i + 1], ma) f = 1 else: ans = max(ans, abs(h[i + 1] - h[i])) else: if h[0] == -1 and h[1] != -1: mi = min(h[1], mi) ma = max(h[1], ma) f = 1 elif h[0] != -1 and h[1] == -1: mi = min(h[0], mi) ma = max(h[0], ma) f = 1 else: ans = max(ans, abs(h[0] - h[1])) if f == 1: print(max(ans, ma - (ma + mi)//2), (ma + mi)//2) else: print(0, 0)
1,500
PYTHON3
import math j = [a for a in input().split(' ')] j[0] = int(j[0]) j[1] = int(j[1]) j[2] = int(j[2]) temp = j[2] temp2 =0 while temp>0: temp2 = math.gcd(j[0],temp) temp -= temp2 if temp == 0: print('0') temp2 = math.gcd(j[1],temp) temp -= temp2 if temp == 0: print('1')
800
PYTHON3
#include <bits/stdc++.h> double a, d, pret = 0.0, v, t, t1, pred; int main() { int n; scanf("%d%lf%lf", &n, &a, &d); for (int i = 0; i < n; i++) { scanf("%lf%lf", &t, &v); t1 = v / (double)a; if (a * t1 * t1 > d * 2) t1 = sqrt((2 * d) / a); else { pred = (a * t1 * t1) / 2; t1 += (d - pred) / (double)v; } pret = (((pret) > (t + t1)) ? (pret) : (t + t1)); printf("%lf\n", pret); } }
1,600
CPP
for t in range(int(input())): n = int(input()) a = list(map(int, input().split())) sum = a[0] max_digit = a[0] positive = True if a[0] > 0 else False for i in range(1,n): if a[i] > 0: if positive == True: if a[i] > max_digit: sum += (a[i]-max_digit) max_digit = a[i] else: sum += a[i] positive = True max_digit = a[i] else: if positive == False: if a[i] > max_digit: sum += (a[i]-max_digit) max_digit = a[i] else: sum += a[i] max_digit = a[i] positive = False print(sum)
1,200
PYTHON3
p, q = map(int, input().split()) n = int(input()) a = list(map(int, input().split())) p0 = a[-1] q0 = 1 for i in range(n - 2, -1, -1): p0, q0 = q0 + p0 * a[i], p0 if (p * q0 == q * p0): print('YES') else: print('NO')
1,700
PYTHON3
#include <bits/stdc++.h> using namespace std; int const N = 3e5 + 10, oo = 1e9; long long const OO = 2e18; double const eps = 1e-8, PI = acos(-1); int mod = oo + 7; int n; string s; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> s; int cnt = 0; for (int i = 0; i < n - 10; i++) cnt += s[i] == '8'; int er = n - 10; if (cnt > er / 2) cout << "YES"; else cout << "NO"; return 0; }
1,200
CPP
def dist(v): ans = 0 if (v[1] == v[2]): return 0 for i in range(3): ans += (v[i][0] - v[i + 1][0]) ** 2 + (v[i][1] - v[i + 1][1]) ** 2 return ans now = [0] * 4 n, m = map(int, input().split()) best = 0 if n == 0: print(0, 1) print(0, m) print(0, 0) print(0, m - 1) elif m == 0: print(1, 0) print(n, 0) print(0, 0) print(n - 1, 0) else: v = [[(0, 0), (n, m), (0, m), (n, 0)], [(0, 0), (n, m), (n, 0), (0, m)], [(0, 0), (n, m), (0, 1), (n, m - 1)], [(0, 0), (n, m), (1, 0), (n - 1, m)], [(0, 0), (n, m), (0, 1), (n - 1, m)], [(0, 0), (n, m), (1, 0), (n, m - 1)], [(0, 1), (n, m), (0, 0), (n, m - 1)], [(1, 0), (n, m), (0, 0), (n - 1, m)], [(0, 1), (n, m), (0, 0), (n - 1, m)], [(1, 0), (n, m), (0, 0), (n, m - 1)], [(0, 1), (n, m - 1), (0, 0), (n, m)], [(1, 0), (n - 1, m), (0, 0), (n, m)], [(0, 1), (n - 1, m), (0, 0), (n, m)], [(1, 0), (n, m - 1), (0, 0), (n, m)], [(0, 1), (n, m - 1), (1, 0), (n, m)], [(1, 0), (n - 1, m), (0, 1), (n, m)], [(0, 1), (n - 1, m), (1, 0), (n, m)], [(1, 0), (n, m - 1), (0, 1), (n, m)], [(0, 1), (n, m - 1), (1, 0), (n - 1, m)], [(1, 0), (n - 1, m), (0, 1), (n, m - 1)], [(0, 1), (n - 1, m), (1, 0), (n, m - 1)], [(1, 0), (n, m - 1), (0, 1), (n - 1, m)], [(0, 0), (n, m - 1), (0, 1), (n, m)], [(0, 0), (n - 1, m), (1, 0), (n, m)], [(0, 0), (n - 1, m), (0, 1), (n, m)], [(0, 0), (n, m - 1), (1, 0), (n, m)]] for i in range(len(v)): now = dist(v[i]) if now > best: best = now ans = i for i in range(4): print(v[ans][i][0], v[ans][i][1])
1,800
PYTHON3
import sys input = sys.stdin.readline rInt = lambda: int(input()) mInt = lambda: map(int, input().split()) rLis = lambda: list(map(int, input().split())) t = int(input()) for _ in range(t): n, k = mInt() a = rLis() b = rLis() a.sort() b.sort(reverse = True) for i in range(k): if a[i] < b[i]: a[i] = b[i] print(sum(a))
800
PYTHON3
t=int(input()) for _ in range(t): n=int(input()) l1=list(map(int,input().split())) swap = 0 even =0 odd = 0 for i in range(n): x = i%2 y = l1[i]%2 if x!=y : if y == 0: even+=1 else: odd+=1 if even == odd: print(even) else: print(-1)
800
PYTHON3
for i in range(int(input())): n,m=map(int,input().split()) if m%(n-1)==0: s=(m//(n-1))*n-1 print(s) if m%(n-1)!=0: s=(m//(n-1))*n+(m%(n-1)) print(s)
1,200
PYTHON3
# cook your dish here n=int(input()) for _ in range(n): a=int(input()) lis=list(map(int,input().split())) if(a==1): print("YES") else: lis.sort() k=0 for i in range(a-1): if(lis[i+1]-lis[i]>1): k=1 break if(k==1): print("NO") else: print("YES")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long LINF = 0x3f3f3f3f3f3f3f3f; const int INF = 0x3f3f3f3f; const long long mod = 998244353; const double PI = acos(-1.0); const double eps = 1e-7; const int maxn = 1e5 + 7; const int maxm = 1e6 + 7; long long la, ra, ta, l, r, lena; long long lb, rb, tb, lenb; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } int main() { scanf("%lld%lld%lld%lld%lld%lld", &la, &ra, &ta, &lb, &rb, &tb); if (ta < tb) { swap(ta, tb); swap(la, lb); swap(ra, rb); } lena = ra - la; lenb = rb - lb; long long g = gcd(ta, tb); long long u = rb % g, v = ra + ta; long long tmp1 = ((v - u) / g) * g + u, tmp2 = ((v - u - 1) / g + 1) * g + u; long long ans = (min(v, tmp1) - max(tmp1 - lenb, v - lena) + 1); ans = max(ans, (min(v, tmp2) - max(tmp2 - lenb, v - lena) + 1)); printf("%lld\n", max(ans, 0ll)); return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; long long inf = 1e18; int __FAST_IO__ = []() { std::ios::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0); return 0; }(); int main() { int t; cin >> t; while (t--) { int N; cin >> N; vector<int> v(N); for (int i = 0; i < (N); ++i) cin >> v[i]; int ans = 1e9; for (int i = 0; i < (5); ++i) for (int j = 0; j < (5); ++j) { int tot = i + j * 2; vector<int> mo(tot + 1, -1); for (int x = 0; x < (i + 1); ++x) for (int y = 0; y < (j + 1); ++y) { mo[x + y * 2] = x + y; } int ans2 = 0; for (int k = 0; k < (N); ++k) { bool flag = false; for (int x = (tot); x >= 0; --x) { if (mo[x] >= 0 && v[k] >= x && (v[k] - x) % 3 == 0) { ans2 = max(ans2, (v[k] - x) / 3); flag = true; break; } } if (!flag) { ans2 = 1e9; break; } } ans = min(ans, ans2 + i + j); } printf("%d\n", ans); } return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; int main() { double y1, y2, yw, xb, yb, r; while (~scanf("%lf%lf%lf%lf%lf%lf", &y1, &y2, &yw, &xb, &yb, &r)) { double k = (yw - 2 * r - y1) / (yw - r - yb); double xw = (k * xb) / (1 + k); if (y2 < y1 + r + r / (sin(atan(xw / (yw - 2 * r - y1))))) printf("-1\n"); else printf("%.10f\n", xw); } return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; int main() { int w, h; cin >> w >> h; vector<string> matriz; vector<string> matriz2; string word; for (int i = 0; i < h; i++) { cin >> word; matriz.push_back(word); } for (int i = 0; i < w; i++) { string empty(h, ' '); matriz2.push_back(empty); } for (int fila = 0; fila < h; fila++) { for (int col = 0; col < w; col++) { matriz2[col][fila] = matriz[fila][col]; } } vector<string> matriz3; for (int i = 0; i < w * 2; i++) { string empty(h * 2, ' '); matriz3.push_back(empty); } for (int fila = 0; fila < w; fila++) { int colnew = 0; for (int cola = 0; cola < h; cola++) { matriz3[fila][colnew] = matriz2[fila][cola]; matriz3[fila][colnew + 1] = matriz2[fila][cola]; colnew += 2; } } for (int fila = 0; fila < w; fila++) { for (int col = 0; col < h * 2; col++) { cout << matriz3[fila][col]; } cout << endl; for (int col = 0; col < h * 2; col++) { cout << matriz3[fila][col]; } cout << endl; } return 0; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; struct SPT { vector<vector<long long>> dp; vector<int> log; int n; SPT(int n) : n(n) { log.resize(n + 1); for (int i = 2; i <= n; i++) log[i] = log[i / 2] + 1; dp.resize(20); for (int i = 0; i < 20; i++) { dp[i].resize(n + 1); fill(dp[i].begin(), dp[i].end(), 1e9); } } void init(vector<int> &a) { for (int i = 1; i <= n; i++) dp[0][i] = a[i]; for (int k = 1; (1 << k) <= n; k++) for (int i = 1; i + (1 << k) <= n + 1; i++) { int lval = dp[k - 1][i], rval = dp[k - 1][i + (1 << (k - 1))]; dp[k][i] = min(lval, rval); } } int minv(int l, int r) { assert(l <= r); int k = log[r - l + 1]; int lval = dp[k][l], rval = dp[k][r - (1 << k) + 1]; return min(lval, rval); } }; int main() { int n; cin >> n; vector<int> a(n + 1); for (int i = 1; i <= n; i++) cin >> a[i]; auto S = SPT(n); S.init(a); vector<int> la(n + 1); auto LS = SPT(n); for (int i = 1; i <= n; i++) la[i] = a[i] - i; LS.init(la); auto RS = SPT(n); for (int i = 1; i <= n; i++) la[i] = a[i] + i; RS.init(la); int ans = 1e9, L = 1; for (int T = 1; T <= n; T++) { while (L <= T && T >= 2 * L - a[L]) L++; int j = 0; if (L <= T) j = L; else { int lx = T, rx = n; int idx = 0; while (lx <= rx) { int mx = (lx + rx) >> 1; if (S.minv(T, mx) >= T) lx = mx + 1; else idx = mx, rx = mx - 1; } if (!idx) continue; j = idx; } int lmin, rmin; if (j <= T) { lmin = LS.minv(1, j - 1) + T; rmin = min(LS.minv(j, T) + T, RS.minv(T, n) - T); } else { lmin = min(LS.minv(1, T) + T, RS.minv(T, j - 1) - T); rmin = RS.minv(j, n) - T; } ans = min(ans, max(lmin, rmin)); } if (ans != 1e9) cout << ans; else cout << -1; puts(""); return 0; }
2,900
CPP
for _ in range(int(input())): n,k=map(int,input().split()) h=list(map(int,input().split())) flag=False lo=h[0]-k+1 hi=h[0]+k-1 for i in range(1,n): if h[i]>hi or h[i]+k<=lo: flag=True break lo=max(lo-k+1,h[i]-k+1) hi=max(hi+k-1,h[i]+k-1) lo=h[-1]-k+1 hi=h[-1]+k-1 for i in range(n-2,-1,-1): if h[i]>hi or h[i]+k<=lo: flag=True break lo=max(lo-k+1,h[i]-k+1) hi=max(hi+k-1,h[i]+k-1) if flag: print('No') else: print('Yes')
1,600
PYTHON3
s = input() retl = [] for char in s: if char in 'QA': retl += [char] s = ''.join(retl).strip('A') ans = 0 for i,char in enumerate(s): if char == 'A': ans += s[:i].count('Q') * s[i:].count('Q') print(ans)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int t, n, d, x; cin >> t; while (t--) { cin >> n; n = 180 - n; cout << (360 % n == 0 ? "YES" : "NO") << endl; } return 0; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 7; map<int, int> l, r; int a[maxn << 1]; int sum[maxn << 1]; int main() { int t; cin >> t; while (t--) { l.clear(); r.clear(); int n; cin >> n; for (int i = 1; i <= 2 * n; i++) cin >> a[i]; for (int i = 1; i <= 2 * n; i++) { a[i] = (a[i] == 2 ? 1 : -1); } for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i]; sum[2 * n + 1] = 0; for (int i = 2 * n; i >= n + 1; i--) sum[i] = sum[i + 1] + a[i]; r[0] = 2 * n + 1; l[0] = 0; for (int i = 1; i <= n; i++) l[sum[i]] = max(l[sum[i]], i); for (int i = 2 * n; i >= n + 1; i--) r[sum[i]] ? r[sum[i]] = min(r[sum[i]], i) : r[sum[i]] = i; int ans = 2 * n; for (int i = 0; i <= n; i++) { if (!r[-sum[i]]) continue; ans = min(r[-sum[i]] - l[sum[i]] - 1, ans); } for (int i = 2 * n + 1; i >= n + 1; i--) { if (!l[-sum[i]] && sum[i]) continue; ans = min(ans, r[sum[i]] - l[-sum[i]] - 1); } cout << ans << endl; } return 0; ; }
1,700
CPP
import sys input = sys.stdin.readline def main(): n = int(input()) S = list(input().strip()) bef = "" rg = {"B":"R", "R":"B"} for i, s in enumerate(S): if s == "?": if bef != "": S[i] = rg[bef] bef = rg[bef] else: bef = s if S[-1] == "?": S[-1] = "B" bef = "" for i in range(n - 1, -1, -1): s = S[i] if s == "?": if bef != "": S[i] = rg[bef] bef = rg[bef] else: bef = s print("".join(S)) for _ in range(int(input())): main()
900
PYTHON3
n, a, b = map(int, input().split()) gr = list(map(int, input().split())) cnt = 0 d = 0 for i in range(n): c = gr[i] if c == 1: if a == 0: if b == 0: if d == 0: cnt += 1 else: d -= 1 else: b -= 1 d += 1 else: a -= 1 else: if b == 0: cnt += 2 else: b -= 1 print(cnt)
1,200
PYTHON3
n = int(input()) li1 = [int(x) for x in input().split()] li2 = [int(x) for x in input().split()] li3 = [int(x) for x in input().split()] li1.sort() li2.sort() li3.sort() for i in range(n-1): if li1[i] != li2[i]: break else: i += 1 print(li1[i]) for i in range(n-2): if li2[i] != li3[i]: break else: i += 1 print(li2[i])
1,100
PYTHON3
#include <bits/stdc++.h> int main() { int n, i, c = 0, p = 0, count = 0, small = 1, big = 1, temp; scanf("%d", &n); int ara[n]; for (i = 1; i <= n; i++) { scanf("%d", &ara[i]); if (ara[i] < ara[i - 1] && i != 1 && c == 0) { small = i - 1; p = 1; c++; } if (ara[i] >= ara[i - 1] && i != 1 && c == 1 && p == 1) { big = i - 1; p = 0; } if (ara[i] > ara[i - 1] && i != 1) count = 1; } if (count == 0) { printf("yes\n"); printf("%d %d", small, n); } else { if (small != 1 && big == 1) { if (ara[small - 1] <= ara[n]) { printf("yes\n"); printf("%d %d", small, n); return 0; } printf("no"); return 0; } for (i = small; i <= (big - small) / 2 + small; i++) { temp = ara[i]; ara[i] = ara[big - i + small]; ara[big - i + small] = temp; } count = 0; for (i = 1; i <= n; i++) { if (ara[i] < ara[i - 1] && i != 1) count = 1; } if (count == 1 && big != 1) printf("no"); else { printf("yes\n"); printf("%d %d", small, big); } } }
1,300
CPP
#include <bits/stdc++.h> using namespace std; int n, f[1007], cnt; bitset<1007> in[1007], one[1007], c, is; int read() { int num = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') num = num * 10 + c - '0', c = getchar(); return num; } int find(int x) { if (f[x] == x) return x; return f[x] = find(f[x]); } int main() { n = read(); for (int i = 1; i <= n; i++) { int k = read(); f[i] = i; for (int j = 1; j <= k; j++) in[i][read()] = 1; } for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) { c = (in[i] & in[j]); if (c.count() == 2) { int a = 0, b = 0; for (int k = 1; k <= n; k++) if (c[k]) if (a) b = k; else a = k; if (find(a) != find(b)) { cout << a << ' ' << b << endl; f[find(a)] = find(b); cnt++; one[a][b] = one[b][a] = one[a][a] = one[b][b] = is[a] = is[b] = 1; } } } if (cnt == 0) { for (int i = 2; i <= n; i++) cout << 1 << ' ' << i << endl; return 0; } else if (cnt == 1) { int a = 0, b = 0; for (int k = 1; k <= n; k++) if (is[k]) if (a) b = k; else a = k; for (int i = 1; i <= n; i++) if (in[i].count() < n) { for (int j = 1; j <= n; j++) if (!is[j]) cout << (in[i][j] ? a : b) << ' ' << j << endl; break; } return 0; } for (int i = 1; i <= n; i++) if (!is[i]) { int mn = 1000; for (int j = 1; j <= n; j++) if (in[j][i] && (in[j] & is).count() < mn) { c = in[j] & is; mn = c.count(); } for (int j = 1; j <= n; j++) if (c[j] && one[j] == c) { cout << i << ' ' << j << endl; break; } } return 0; }
3,200
CPP