solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
#!/usr/bin/env python3 def double_ended(a, b): matrix = [[0 for i in range(len(a))] for i in range(len(b))] maximum = 0 for i in range(len(b)): for j in range(len(a)): if a[j] == b[i]: if i-1 >= 0 and j-1 >= 0: matrix[i][j] = 1 + matrix[i-1][j-1] else: matrix[i][j] = 1 if matrix[i][j] > maximum: maximum = matrix[i][j] count = len(b)+len(a) - 2*maximum print(count) if __name__ == '__main__': tc = int(input().strip()) for t in range(tc): a = input() b = input() double_ended(a, b)
9
PYTHON3
word=input() m=0 c='a' for each in word: t=abs(ord(c)-ord(each)) m+=min(t,26-t) c=each print(m)
7
PYTHON3
#include<iostream> using namespace std; int main(){ int n, d, x, temp; cin>>n>>d>>x; for(int i=0; i<n; i++){ cin>>temp; x+=(1+(d-1)/temp); }cout<<x<<"\n"; }
0
CPP
#include <bits/stdc++.h> using namespace std; map<string, int> m; string s, a, b; int n; int main() { m["void"]; m["errtype"] = -1; cin >> n; while (n--) { cin >> s; if (s == "typedef") { string t = ""; int cur = 0; cin >> a >> b; for (int i = 0; i < a.size(); i++) { if (isalpha(a[i])) t += a[i]; else if (a[i] == '&') cur--; else cur++; } if (m.find(t) == m.end() || m[t] < 0 || m[t] + cur < 0) m[b] = -1; else m[b] = m[t] + cur; } else { cin >> a; string t = ""; int cur = 0; for (int i = 0; i < a.size(); i++) { if (isalpha(a[i])) t += a[i]; else if (a[i] == '&') cur--; else cur++; } if (m.find(t) == m.end() || m[t] < 0 || m[t] + cur < 0) puts("errtype"); else cout << "void" << string(m[t] + cur, '*') << endl; } } return 0; }
8
CPP
for i in range(int(input())): n = int(input()) h = [] s1 = input() s2 = input() for j in range(n): if s1[j]!=s2[j]: h.append([s1[j],s2[j]]) if len(h)!=2: print('No') elif h[0]==h[1]: print("Yes") else: print("No")
8
PYTHON3
t=int(input()) for i in range(t): n,m=list(map(int,input().split())) arr=[] ans=0 for i in range(n): a=list(i for i in input()) arr.append(a) for i in range(n-1): if arr[i][m-1]=="R": ans+=1 for j in arr[n-1]: if j =="D": ans+=1 print(ans)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; long long exgcd(long long a, long long b, long long &x, long long &y) { long long d = a; if (b) d = exgcd(b, a % b, y, x), y -= (a / b) * x; else x = 1, y = 0; return d; } bool solve(long long a, long long b, long long c, long long &x, long long &y) { long long x0, y0; long long d = exgcd(a, b, x0, y0); if (c % d) return false; long long dx = b / d; long long dy = a / d; if (dx < 0) { dx = -dx; dy = -dy; } x = (((x0 * c / d) % (dx) + (dx)) % (dx)); y = (c - a * x) / b; return true; } int cnt[1000005]; int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int ansx, ansy, num = 0; int n, m, dx, dy; cin >> n >> m >> dx >> dy; while (m--) { int x, y; cin >> x >> y; long long k, tmp; solve(dx, n, -x, k, tmp); int yy = (y + k * dy) % n; cnt[yy]++; if (cnt[yy] > num) { num = cnt[yy]; ansx = x; ansy = y; } } cout << ansx << " " << ansy << "\n"; return 0; }
11
CPP
import sys, math import heapq from collections import deque input = sys.stdin.readline #input def ip(): return int(input()) def sp(): return str(input().rstrip()) def mip(): return map(int, input().split()) def msp(): return map(str, input().split()) def lmip(): return list(map(int, input().split())) def lmsp(): return list(map(str, input().split())) #gcd, lcm def gcd(x, y): while y: x, y = y, x % y return x def lcm(x, y): return x * y // gcd(x, y) #prime def isPrime(x): if x==1: return False for i in range(2, int(x**0.5)+1): if x%i==0: return False return True # Union Find # p = {i:i for i in range(1, n+1)} def find(x): if x == p[x]: return x q = find(p[x]) p[x] = q return q def union(x, y): global n x = find(x) y = find(y) if x != y: p[y] = x def getPow(a, x): ret =1 while x: if x&1: ret = (ret*a) % MOD a = (a*a)%MOD x>>=1 return ret def getInv(x): return getPow(x, MOD-2) def lowerBound(start, end, key): while start < end: mid = (start + end) // 2 if lst[mid] == key: end = mid elif key < lst[mid]: end = mid elif lst[mid] < key: start = mid + 1 return end def upperBound(start, end, key): while start < end: mid = (start + end) // 2 if lst[mid] == key: start = mid + 1 elif lst[mid] < key: start = mid + 1 elif key < lst[mid]: end = mid if end == len(lst): return end-1 return end ############### Main! ############### t = ip() while t: t -= 1 n = ip() s = sp() l = 0 a = [] b = [] for i in range(n): if s[i]=="M": b.append(i) else: a.append(i) if len(b)*2!=len(a): print("no") continue fl = False for i in range(n//3): if a[i] > b[i] or a[i+n//3] < b[i]: fl = True print("no") break if fl: continue print("yes") ######## Priest W_NotFoundGD ########
8
PYTHON3
s = input() t = input() x = ['a', 'e', 'i', 'o', 'u'] if len(s) != len(t): print('No') else: c = True for i in range(len(s)): a = s[i] in x b = t[i] in x if a != b: print('No') c = False break if c: print('Yes')
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); string s; cin >> s; const string cf = "CODEFORCES"; int n = int((s).size()); for (int i = 0; i < n; ++i) { for (int j = 0; j < n - i + 1; ++j) { string t = s; t.erase(i, j); if (t == cf) { cout << "YES"; return 0; } } } cout << "NO"; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; const int inf = INT_MAX; const long long infll = LONG_LONG_MAX; int gcd(int x, int y) { return y ? gcd(y, x % y) : x; } priority_queue<int, vector<int>, greater<int> > pq; int n, k, cnt, pos = 1, d[101010], p[101010]; int main() { scanf("%d %d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d", &d[i]); p[i] = d[i]; } sort(d + 1, d + n + 1); sort(p + 1, p + n + 1); while (cnt < k) { if (pos > n) { puts("0"); cnt++; continue; } if (d[pos] - d[pos - 1] == 0) { pos++; continue; } printf("%d\n", d[pos] - d[pos - 1]); pos++; cnt++; } }
8
CPP
coins = input() values = list(reversed(sorted(int(x) for x in input().split()))) values.append(0) for i in range(len(values)): if sum(values[:i]) > sum(values[i:]): print(i) break
7
PYTHON3
#include<bits/stdc++.h> using namespace std; int main() { string at; int ct=0; cin>>at; for(int i=0;i<at.size()-1;i++) if(at[i]!=at[i+1]) ct++; cout<<ct<<endl; }
0
CPP
#include <bits/stdc++.h> using namespace std; int cnt[107]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); ; string ss; cin >> ss; for (int i = 1; i < ss.size() - 1; i++) { if (ss[i] == 'A') { if (ss[i - 1] == 'B' && ss[i + 1] == 'C' || ss[i - 1] == 'C' && ss[i + 1] == 'B') { cout << "Yes\n"; return 0; } } else if (ss[i] == 'B') { if (ss[i - 1] == 'A' && ss[i + 1] == 'C' || ss[i - 1] == 'C' && ss[i + 1] == 'A') { cout << "Yes\n"; return 0; } } else if (ss[i] == 'C') { if (ss[i - 1] == 'A' && ss[i + 1] == 'B' || ss[i - 1] == 'B' && ss[i + 1] == 'A') { cout << "Yes\n"; return 0; } } } cout << "No\n"; return 0; }
7
CPP
n,m = [int(x) for x in input().split()] a = sorted([int(x) for x in input().split()]) l = [] if len(a) == 1: print(max(a[0],m-a[0])) else: for i in range(n-1): l.append(a[i+1]-a[i]) l1 = sorted(l) print(max(l1[-1]/2,m-a[-1],a[0]))
8
PYTHON3
b, k = map(int, input().split()) A = list(map(int, input().split())) if b % 2 == 1: cnt = 0 for i in A: if i % 2 == 1: cnt += 1 if cnt % 2: print('odd') else: print('even') else: if A[-1] % 2: print('odd') else: print('even')
7
PYTHON3
x,y,w,h,r=[int(i) for i in input().split()] if r<=w<=x-r and r<=h<=y-r : print("Yes") else: print("No")
0
PYTHON3
n = int(input()) nways = 0 if n % 2 != 0: print(0) else: nways = pow(2, n / 2) nways = int(nways) print(nways)
7
PYTHON3
print(''.join(map(lambda x: str(10 - int(x)), input())))
0
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; const long long mod = 1e9 + 7; long long a[1005][1005]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) cin >> a[i][j]; } vector<int> V; long long now = a[2][1] * a[3][1] / a[3][2]; now = sqrt(now); V.push_back(now); for (int i = 2; i <= n; i++) { V.push_back(a[i][1] / V[0]); } for (auto &x : V) { cout << x << ' '; } cout << '\n'; }
8
CPP
n=int(input()) a=n*[0] b=n*[0] for i in range(n): a[i],b[i]=[int(i) for i in input().split()] t=0 for i in range(n): z=a[i] for j in range(n): if z==b[j]: t=t+1 print(t)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<int> a(n + 1); for (int i = 1; i <= n; i++) cin >> a[i]; int three2n = 1; for (int i = 1; i <= n; i++) three2n *= 3; for (int k = 1; k < three2n; k++) { int k_cp = k; int sum = 0; for (int i = 1; i <= n; i++) { int s = k_cp % 3; k_cp /= 3; if (s == 2) s = -1; sum += s * a[i]; } if (sum == 0) { cout << "YES" << endl; return; } } cout << "NO" << endl; } int main() { int t; cin >> t; for (int i = 1; i <= t; i++) solve(); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b, x, y; cin >> n >> a >> x >> b >> y; while (a != x && b != y) { if (a == n) a = 1; else a++; if (b == 1) b = n; else b--; if (a == b) { cout << "YES" << endl; return 0; } if (a == x && b == y) { break; } } cout << "NO" << endl; }
7
CPP
# dp ==> subsequence sum n = int(input()) a = list(map(int,input().split())) b = [[-1,1][i==0] for i in a] S = a.count(1) best = 0 SUM = 0 for i in b: SUM = max(i, SUM+i) best = max(best, SUM) print(S+best if S != n else n-1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; int ans[501][501], now; int main() { cin >> n; if (n < 3) { cout << -1; return 0; } ans[1][1] = 7; ans[1][2] = 6, ans[1][3] = 9; ans[2][1] = 8; ans[2][2] = 2, ans[2][3] = 5; ans[3][1] = 1; ans[3][2] = 3; ans[3][3] = 4; for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { ans[i][j] += n * n - 9; } } for (int i = n; i >= 4; i--) { ans[i][i] = i + now; for (int j = i - 1; j >= 1; j--) { ans[j][i] = ans[j + 1][i]; ans[i][j] = ans[i][j + 1]; if (i & 1) ans[j][i]++, ans[i][j]--; else ans[j][i]--, ans[i][j]++; } now += 2 * i - 1; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cout << ans[i][j] << ' '; } cout << endl; } return 0; }
11
CPP
n = int(input()) dic = {} for i in range(n): x = str(input()) try: dic[x]+=1 except: dic[x] = 1 lis = list(dic.keys()) if len(lis) is 1: print (lis[0]) elif dic[lis[0]]>dic[lis[1]]: print (lis[0]) else: print (lis[1])
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { string one, two, three; cin >> one >> two >> three; if ((one == two && two == three) || (one != two && two != three && one != three)) { cout << "?"; } else if (two == three) { if ((one == "paper" && two == "rock") || (one == "scissors" && two == "paper") || (one == "rock" && two == "scissors")) { cout << "F"; } else if ((one == "paper" && two == "scissors") || (one == "rock" && two == "paper") || (one == "scissors" && two == "rock")) { cout << "?"; } } else if (one == two) { if ((three == "paper" && two == "rock") || (three == "scissors" && two == "paper") || (three == "rock" && two == "scissors")) { cout << "S"; } else if ((three == "paper" && two == "scissors") || (three == "rock" && two == "paper") || (three == "scissors" && two == "rock")) { cout << "?"; } } else if (one == three) { if ((two == "paper" && one == "rock") || (two == "scissors" && one == "paper") || (two == "rock" && one == "scissors")) { cout << "M"; } else if ((two == "paper" && one == "scissors") || (two == "rock" && one == "paper") || (two == "scissors" && one == "rock")) { cout << "?"; } } return 0; }
7
CPP
def main(): from collections import deque from itertools import chain import sys input = sys.stdin.readline N = int(input()) graph = tuple(set() for _ in range(N)) for _ in range(N - 1): a, b = (int(x) - 1 for x in input().split()) graph[a].add(b) graph[b].add(a) bipartite = [-1] * N bipartite[0] = 0 dq = deque([0]) while dq: v = dq.popleft() nb = bipartite[v] ^ 1 for u in graph[v]: if ~bipartite[u]: continue bipartite[u] = nb dq.append(u) s0 = bipartite.count(0) s1 = N - s0 it1 = iter(range(1, N + 1, 3)) it2 = iter(range(2, N + 1, 3)) it3 = iter(range(3, N + 1, 3)) it123 = chain(it1, it2, it3) it13 = chain(it1, it3) it23 = chain(it2, it3) ans = [-1] * N K = N // 3 if s0 <= K: for v in range(N): if bipartite[v] == 0: ans[v] = next(it3) else: ans[v] = next(it123) # 余った3をこちらで消費するので12ではなく123 elif s1 <= K: for v in range(N): if bipartite[v] == 1: ans[v] = next(it3) else: ans[v] = next(it123) else: for v in range(N): if bipartite[v] == 0: ans[v] = next(it13) else: ans[v] = next(it23) print(*ans) if __name__ == '__main__': main()
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int len[250]; int main() { int n,sum=0; cin >>n; for (int i=1;i<=2*n;i++) cin >>len[i]; sort(len+1,len+n*2+1); for (int i=1;i<=n;i++) sum=sum+len[2*i-1]; cout<<sum; }
0
CPP
#include <bits/stdc++.h> struct shark { int x; int y; double n; }; int main() { int n, p; scanf("%d%d", &n, &p); struct shark a[n]; for (int i = 0; i < n; i++) { scanf("%d%d", &a[i].x, &a[i].y); if (a[i].x % p != 0) a[i].n = (double)(a[i].y / p - a[i].x / p) / (a[i].y - a[i].x + 1); else a[i].n = (double)(a[i].y / p - a[i].x / p + 1) / (a[i].y - a[i].x + 1); } double sum = 0; for (int i = 0; i < n; i++) { if (i != n - 1) { sum += (1 - (1 - a[i].n) * (1 - a[i + 1].n)) * 2000; } else { sum += (1 - (1 - a[i].n) * (1 - a[0].n)) * 2000; } } printf("%.6lf", sum); return 0; }
9
CPP
import sys input = sys.stdin.readline sys.setrecursionlimit(10000) N, T = (int(i) for i in input().split()) A = [int(i) for i in input().split()] ma = A[-1] mas = 1 resma = 0 rescc = 0 for i in range(N-2, -1 ,-1): if A[i] > ma: ma = A[i] mas = 1 elif A[i] == ma: mas += 1 else: if ma - A[i] > resma: resma = ma - A[i] rescc = 1 elif ma - A[i] == resma: rescc += 1 else: pass print(rescc)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 10; const int inf = 0x3f3f3f3f; const int mod = 1000007; set<long long> leng, wide; map<long long, long long> len, wid; map<long long, long long>::iterator it; set<long long>::iterator se; char op[3]; int main() { long long H, W, n; scanf("%lld%lld%lld", &H, &W, &n); leng.insert(0); leng.insert(W); wide.insert(0); wide.insert(H); len[W]++; wid[H]++; for (long long i = 0; i < n; i++) { long long temp, index = -1; scanf("%s%lld", op, &temp); if (op[0] == 'H') { leng.insert(temp); se = leng.find(temp); se++; long long up = *se; se--, se--; long long down = *se; long long del = up - down; if (len[del] == 1) len.erase(del); else len[del]--; long long Min = min(up - temp, temp - down); long long Max = max(up - temp, temp - down); len[Min]++; len[Max]++; } else { wide.insert(temp); se = wide.find(temp); se++; long long up = *se; se--, se--; long long down = *se; long long del = up - down; if (wid[del] == 1) wid.erase(del); else wid[del]--; long long Min = min(up - temp, temp - down); long long Max = max(up - temp, temp - down); wid[Min]++; wid[Max]++; } it = len.end(), it--; long long length = it->first; it = wid.end(), it--; long long width = it->first; printf("%lld\n", length * width); } return 0; }
7
CPP
#include<bits/stdc++.h> using namespace std; string s; int w; int main(){ cin>>s>>w; for (int i=0;i<s.length();i+=w)cout<<s[i]; return 0; }
0
CPP
n = int(input()) coins = tuple(map(int, input().split())) result = 0 for i in coins: if coins.count(i) > result: result = coins.count(i) print(result)
7
PYTHON3
#include <iostream> #include <cstdio> using namespace std; int main() { double a, b; cin >> a >> b; printf("%.6f\n", (a * b) / (a + b)); return 0; }
0
CPP
def f(a, b): if b == 1: return a if b % 2 == 0: x = f(a, b // 2) % m return (x * x) % m else: return (f(a, b - 1) * a) % m n, m = map(int, input().split()) print((f(3, n) - 1) % m)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class T> inline void smn(T &a, const T &b) { if (b < a) a = b; } template <class T> inline void smx(T &a, const T &b) { if (b > a) a = b; } template <class T> inline T rev(const T &a) { T _ = a; reverse(_.begin(), _.end()); return _; } const double eps = 1e-9; const long double leps = 1e-14; vector<int> lucky; int p1, p2, v1, v2, k; const int INF = (1u << 31) - 1; void find(int d, int now) { if (d > 9) return; if (d) lucky.push_back(now); find(d + 1, now * 10 + 4); find(d + 1, now * 10 + 7); } double res; int main(int argc, char *argv[]) { ios_base::sync_with_stdio(false); cin >> p1 >> p2 >> v1 >> v2 >> k; find(0, 0); sort(lucky.begin(), lucky.end()); for (int i = 0; i <= (int)lucky.size() - k; i++) { int last = (i == 0) ? 1 : lucky[i - 1] + 1; int next = (i + k == lucky.size() ? INF : lucky[i + k] - 1); if (p1 <= lucky[i] && v2 >= lucky[i + k - 1] && p2 >= last && v1 <= next) res += ((min(lucky[i], p2) - max(last, p1) + 1) / double(p2 - p1 + 1)) * ((min(next, v2) - max(lucky[i + k - 1], v1) + 1) / double(v2 - v1 + 1)); if (v1 <= lucky[i] && p2 >= lucky[i + k - 1] && v2 >= last && p1 <= next) res += ((min(lucky[i], v2) - max(last, v1) + 1) / double(v2 - v1 + 1)) * ((min(next, p2) - max(lucky[i + k - 1], p1) + 1) / double(p2 - p1 + 1)); if (k == 1 && lucky[i] >= p1 && lucky[i] <= p2 && lucky[i] >= v1 && lucky[i] <= v2) res -= 1. / (p2 - p1 + 1) * 1. / (v2 - v1 + 1); } cout << fixed << setprecision(9) << res << '\n'; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int row[5005][2]; int col[5005][2]; int main(void) { int n, i, j; int a, b, g; int y, z; int c1 = 0, c2 = 0, c3 = 0; int diff1, diff2, diff3, diff4, diff5, diff6; int l, m; long long sum = 0; string s1, s2; int p, q, r, k; c1 = 0; int minn; long long s; scanf("%d%d%d", &n, &m, &k); for (i = 0; i < k; i++) { scanf("%d%d%d", &p, &q, &r); if (p == 1) { row[q][0] = r; row[q][1] = sum; } else if (p == 2) { col[q][0] = r; col[q][1] = sum; } sum++; } for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (row[i][0] > 0 && col[j][0] > 0) { if (row[i][1] > col[j][1]) { printf("%d ", row[i][0]); } else printf("%d ", col[j][0]); } else { if (row[i][0] > 0) printf("%d ", row[i][0]); else printf("%d ", col[j][0]); } } printf("\n"); } }
8
CPP
#include <stdio.h> #include <sys/time.h> #define N 200000 #define Q 200000 #define N_ (1 << 18) /* N_ = pow2(ceil(log2(N))) */ unsigned int X; void srand_() { struct timeval tv; gettimeofday(&tv, NULL); X = tv.tv_sec ^ tv.tv_usec; if (X % 2 == 0) X++; } int rand_() { return (X *= 3) >> 1; } int zz[1 + Q + 2], ll[1 + Q + 2], rr[1 + Q + 2], kk[1 + Q + 2], u_, l_, r_; int node(int k) { static int _ = 1; zz[_] = rand_(); kk[_] = k; return _++; } void split(int u, int k) { if (u == 0) { u_ = l_ = r_ = 0; return; } if (kk[u] < k) { split(rr[u], k); rr[u] = l_, l_ = u; } else if (kk[u] > k) { split(ll[u], k); ll[u] = r_, r_ = u; } else { u_ = u, l_ = ll[u], r_ = rr[u]; ll[u] = rr[u] = 0; } } int merge(int u, int v) { if (u == 0) return v; if (v == 0) return u; if (zz[u] < zz[v]) { rr[u] = merge(rr[u], v); return u; } else { ll[v] = merge(u, ll[v]); return v; } } int first(int u) { return ll[u] == 0 ? u : first(ll[u]); } int last(int u) { return rr[u] == 0 ? u : last(rr[u]); } int ii[Q * 3], jj[Q * 3]; void sort(int *hh, int l, int r) { while (l < r) { int i = l, j = l, k = r, h = hh[l + rand_() % (r - l)], tmp; while (j < k) if (ii[hh[j]] == ii[h]) j++; else if (ii[hh[j]] < ii[h]) { tmp = hh[i], hh[i] = hh[j], hh[j] = tmp; i++, j++; } else { k--; tmp = hh[j], hh[j] = hh[k], hh[k] = tmp; } sort(hh, l, i); l = k; } } long long st[N_ * 2]; int n_; void update(int l, int r, long long x) { for (l += n_, r += n_; l <= r; l >>= 1, r >>= 1) { if ((l & 1) == 1) st[l++] += x; if ((r & 1) == 0) st[r--] += x; } } long long query(int i) { long long x; x = 0; for (i += n_; i > 0; i >>= 1) x += st[i]; return x; } int main() { static int pp[N + 1], qu[N + 1], tt[Q], hh[Q * 3]; static long long aa[N + 1], bb[N], ans[Q]; int n, q, q_, h, i, cnt; srand_(); scanf("%d%d", &n, &q); for (i = 0; i < n; i++) scanf("%d", &pp[i]), pp[i]--; pp[n] = n; for (i = 1; i <= n; i++) { scanf("%lld", &aa[i]); aa[i] += aa[i - 1]; } for (i = 0; i < n; i++) scanf("%lld", &bb[i]); u_ = merge(node(0), node(n)); q_ = 0; for (h = 0; h < q; h++) { scanf("%d", &i), i--; if (i != 0) { int p, q; split(u_, i); p = kk[last(l_)], q = kk[first(r_)]; if (u_ == 0) u_ = node(i), tt[h] = 1; else u_ = 0, tt[h] = 2; ii[h * 3 + 0] = p, jj[h * 3 + 0] = q, hh[q_++] = h * 3 + 0; ii[h * 3 + 1] = p, jj[h * 3 + 1] = i, hh[q_++] = h * 3 + 1; ii[h * 3 + 2] = i, jj[h * 3 + 2] = q, hh[q_++] = h * 3 + 2; u_ = merge(merge(l_, u_), r_); } } sort(hh, 0, q_); n_ = 1; while (n_ < n) n_ <<= 1; cnt = 0; qu[cnt++] = n; for (i = n - 1, h = q_ - 1; i >= 0; i--) { int j; long long x; while (cnt && pp[qu[cnt - 1]] < pp[i]) cnt--; j = qu[cnt - 1]; qu[cnt++] = i; bb[i] = aa[j] - aa[i] - bb[i]; if ((x = query(j)) < bb[i]) update(j, n, bb[i] - x); while (h >= 0 && ii[hh[h]] == i) { ans[hh[h] / 3] -= query(jj[hh[h]]) * ((hh[h] % 3 == 0) == (tt[hh[h] / 3] == 2) ? 1 : -1); h--; } } ans[0] += aa[n] - query(n); for (h = 1; h < q; h++) ans[h] += ans[h - 1]; for (h = 0; h < q; h++) printf("%lld\n", ans[h]); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; int maxN(int n, int x) { int s = 0, mx = 0; for (int j = 0; j < n; j++) { if (x & (1 << j)) s++; else s--; mx = max(mx, s); if (s < 0) break; } return s < 0 ? -1 : mx; } int main() { int n; string s; cin >> n >> s; int c = 0; vector<int> cp(n), l; set<int> o; for (int i = 0; i < n; i++) { if (s[i] == '(') l.push_back(i), o.insert(i); else { c--, cp[l.back()] = i; l.pop_back(); } } string ans(n, '0'); int x = 0; while (!o.empty()) { auto it = begin(o); while (it != end(o)) { int p = *it; o.erase(it); ans[p] = x + '0'; ans[cp[p]] = x + '0'; it = o.upper_bound(cp[p]); } x ^= 1; } cout << ans << endl; }
10
CPP
n = int(input()) lst = [] sec = 0 for i in range(n): lst.clear() lst = list(map(int, input().split())) if abs(lst[2] - lst[0]) == 1 and lst[1] == lst[3]: sec = 1 elif abs(lst[1] - lst[3]) == 1 and lst[0] == lst[2]: sec = 1 elif abs(lst[2] - lst[0] == 0) or abs(lst[3] - lst[1] == 0): sec = abs(lst[0] - lst[2])+abs(lst[1]-lst[3]) else: sec = abs(lst[0] - lst[2])+abs(lst[1]-lst[3]) +2 print(sec)
7
PYTHON3
n = int(input()) for i in range(n): if i % 2 == 0: if n == i + 1: print('I hate it') break else: print('I hate that', end=' ') else: if n == i + 1: print('I love it') break else: print('I love that', end=' ')
7
PYTHON3
def solve(): n = int(input()) a = list(map(int, input().split())) st = set(a) if(len(st) != len(a)): print("YES") else: print("NO") _ = int(input()) while _ > 0: solve() _ -= 1
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int a, b, c, d, i; int digits(int n) { int d = 0; while (n) { n /= 10; d++; } return d; } int main() { cin >> a >> b; c = a; d = digits(b); for (i = 1; b; i++) { c += (b % 10) * (int)floor(pow((double)10, (double)d - i)); b /= 10; } cout << c; }
7
CPP
#include<iostream> #include<cmath> #include<stdio.h> using namespace std; struct point{ int x,y; }; struct line{ point s,t; }; struct bec{ int x,y; }; struct circle{ point p; int r; }; void solve(circle c1,circle c2){ bec p; double dp,cost,sint,qx, qy,ansx1,ansx2,ansy1,ansy2,cosa,sina; p.x = c2.p.x-c1.p.x; p.y = c2.p.y-c1.p.y; dp = sqrt((double)p.x*(double)p.x+(double)p.y*(double)p.y); cost = p.x/dp; sint = p.y/dp; cosa = (-c2.r*c2.r+c1.r*c1.r+dp*dp)/(2*dp*c1.r); sina = sqrt(1-cosa*cosa); ansx1 = c1.p.x + (double)c1.r*(cosa*cost - sint*sina); ansy1 = c1.p.y + (double)c1.r*(sint*cosa + cost*sina); ansx2 = c1.p.x + (double)c1.r*(cosa*cost + sint*sina); ansy2 = c1.p.y + (double)c1.r*(sint*cosa - cost*sina); if(ansx1<ansx2) printf("%.8f %.8f %.8f %.8f\n",ansx1,ansy1,ansx2,ansy2); else if(ansx1==ansx2){ if(ansy1<ansy2) printf("%.8f %.8f %.8f %.8f\n",ansx1,ansy1,ansx2,ansy2); else printf("%.8f %.8f %.8f %.8f\n",ansx2,ansy2,ansx1,ansy1); } else printf("%.8f %.8f %.8f %.8f\n",ansx2,ansy2,ansx1,ansy1); } int main(){ circle c1,c2; int c1x,c2x,c1y,c2y,c1r,c2r; cin>>c1x>>c1y>>c1r; c1.p.x =c1x; c1.p.y = c1y; c1.r = c1r; cin>>c2x>>c2y>>c2r; c2.p.x =c2x; c2.p.y = c2y; c2.r = c2r; solve(c1,c2); return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; using ll = int64_t; using ull = uint64_t; using ii = pair<int, int>; template <typename T1, typename T2> string print_iterable(T1 begin_iter, T2 end_iter, int counter) { bool done_something = false; stringstream res; res << "["; for (; begin_iter != end_iter and counter; ++begin_iter) { done_something = true; counter--; res << *begin_iter << ", "; } string str = res.str(); if (done_something) { str.pop_back(); str.pop_back(); } str += "]"; return str; } vector<int> SortIndex(int size, std::function<bool(int, int)> compare) { vector<int> ord(size); for (int i = 0; i < size; i++) ord[i] = i; sort(ord.begin(), ord.end(), compare); return ord; } template <typename T> bool MinPlace(T& a, const T& b) { if (a > b) { a = b; return true; } return false; } template <typename T> bool MaxPlace(T& a, const T& b) { if (a < b) { a = b; return true; } return false; } template <typename S, typename T> ostream& operator<<(ostream& out, const pair<S, T>& p) { out << "{" << p.first << ", " << p.second << "}"; return out; } template <typename T> ostream& operator<<(ostream& out, const vector<T>& v) { out << "["; for (int i = 0; i < (int)v.size(); i++) { out << v[i]; if (i != (int)v.size() - 1) out << ", "; } out << "]"; return out; } template <class TH> void _dbg(const char* name, TH val) { clog << name << ": " << val << endl; } template <class TH, class... TA> void _dbg(const char* names, TH curr_val, TA... vals) { while (*names != ',') clog << *names++; clog << ": " << curr_val << ", "; _dbg(names + 1, vals...); } int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { string s; int n; cin >> n >> s; int ans = 0; bool hmin = false; bool hmax = false; for (int i = 0; i < n; i++) { if (s[i] == '<') hmin = true; if (s[i] == '>') hmax = true; } if (hmin && hmax) { for (int i = 0; i < s.size(); i++) { if (s[(i + 1) % n] == '-' || s[i] == '-') ans++; } } else { ans = n; } cout << ans << '\n'; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main(){ int t; cin >> t; while(t--){ int n; cin >> n; map<int, int> m, mi; int index = -1; for(int i = 0;i < n;i++){ int temp; cin >> temp; m[temp]++; mi[temp] = i+1; } for(auto i : m){ if(i.second == 1){ index = mi[i.first]; } } cout << index << endl; } return 0; }
7
CPP
#include<bits/stdc++.h> using namespace std; int main() { string s; long long n; cin>>n; while(n--) { s=char(96+n%26+1)+s; n/=26; } cout<<s; }
0
CPP
#include <bits/stdc++.h> #define rep(i, a, b) for (int i = (a); i < (b); i++) #define all(x) x.begin(), x.end() #define sz(x) (int) (x.size()) using namespace std; using ll = long long; using vi = vector<int>; const int MAXN = 3001, INF = 1e9; int N; int A[MAXN]; int dp[MAXN][MAXN]; multiset<int> currRemove; void solve() { cin >> N; rep(i, 0, N) cin >> A[i]; rep(i, 0, N+1) { rep(j, 0, N+1) { dp[i][j] = INF; } } dp[N-1][N] = 0; for (int i = N-2; i >= 0; i--) { // Propagate mins for (int j = N-1; j >= 0; j--) { dp[i+1][j] = min(dp[i+1][j+1], dp[i+1][j]); } currRemove.clear(); for (int j = i+1; j < N; j++) { if (j > i+A[i]) continue; // Cannot reach dp[i][j] = dp[j][i+A[i]+1] + currRemove.size(); // Update currRemove currRemove.erase(j); // Add current to minRemove if (A[j] != 0) currRemove.insert(j+A[j]); } } int answer = INF; rep(j, 1, N+1) answer = min(dp[0][j], answer); cout << answer << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); int T; cin >> T; rep(i, 0, T) solve(); return 0; }
12
CPP
#include <bits/stdc++.h> typedef long long ll; typedef long double ldb; const int md = 1e9 + 7; const ll inf = 3e18; const int OO = 0; using namespace std; const int N = 1505; struct item { int i, j, d, col; item() {} item(int ii, int jj, int dd, int cc) { i = ii; j = jj; d = dd; col = cc; } }; int n, q; int a[N][N]; vector<pair<int, int>> b[N][N]; bool found(int i, int j, int col) { for (const auto &x : b[i][j]) { if (x.second == col) return true; } return false; } int aux[N * N] = {}; void addvector(vector<pair<int, int>> &A, const vector<pair<int, int>> &B) { for (const auto &x : B) A.emplace_back(x.first + 1, x.second); } void work(int i, int j) { vector<pair<int, int>> opt; opt.reserve(35); opt.emplace_back(0, a[i][j]); if (i + 1 < n) addvector(opt, b[i + 1][j]); if (j + 1 < n) addvector(opt, b[i][j + 1]); if (i + 1 < n && j + 1 < n) addvector(opt, b[i + 1][j + 1]); sort(opt.begin(), opt.end()); for (const auto &x : opt) { if (aux[x.second]) continue; aux[x.second] = 1; b[i][j].push_back(x); if (b[i][j].size() == q) break; } for (const auto &x : opt) aux[x.second] = 0; } void solve() { cin >> n >> q; q++; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> a[i][j]; for (int i = n - 1; i >= 0; i--) for (int j = n - 1; j >= 0; j--) work(i, j); /* queue<item> qu; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { qu.push(item(i, j, 0, a[i][j])); } while (qu.size()) { item x = qu.front(); if (OO) { cout << "in queue: " << x.i << " " << x.j << " " << x.d << " " << x.col << '\n'; } qu.pop(); if (b[x.i][x.j].size() == q || found(x.i, x.j, x.col)) continue; b[x.i][x.j].emplace_back(x.d, x.col); if (x.i > 0) { if (b[x.i - 1][x.j].size() < q && !found(x.i - 1, x.j, x.col)) qu.push(item(x.i - 1, x.j, x.d + 1, x.col)); if (x.j > 0) { if (b[x.i - 1][x.j - 1].size() < q && !found(x.i - 1, x.j - 1, x.col)) qu.push(item(x.i - 1, x.j - 1, x.d + 1, x.col)); } } if (x.j > 0) { if (b[x.i][x.j - 1].size() < q && !found(x.i, x.j - 1, x.col)) qu.push(item(x.i, x.j - 1, x.d + 1, x.col)); } } */ if (OO) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { cout << i << " " << j << ":\n"; //for (const auto &x : b[i][j]) cout << x.d << " " << x.col << '\n'; } } vector<int> ans(n + 1, 0); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { int best = min(n - i, n - j); if (b[i][j].size() == q) best = min(best, b[i][j][q - 1].first); ans[best]++; } for (int i = n - 1; i >= 0; i--) ans[i] += ans[i + 1]; for (int i = 1; i <= n; i++) cout << ans[i] << '\n'; } int main() { ios::sync_with_stdio(0), cin.tie(0); int tst = 1; // cin >> tst; while (tst--) solve(); }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { int t, x[100000], count = 1; cin >> t; for (int i = 0; i < t; i++) { cin >> x[i]; } for (int i = 0; i < t - 1; i++) { if (x[i] != x[i + 1]) count++; } cout << count << endl; return 0; }
7
CPP
#include <iostream> using namespace std; int main() { int n,m,l; long a[100][100]={},b[100][100]={},c[100][100]={}; cin>>n>>m>>l; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cin>>a[i][j]; } } for(int i=0;i<m;i++){ for(int j=0;j<l;j++){ cin>>b[i][j]; } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ for(int k=0;k<l;k++){ c[i][k]+=a[i][m-1-j]*b[m-1-j][k]; } } } for(int i=0;i<n;i++){ for(int j=0;j<l;j++){ if(j<l-1){cout<<c[i][j]<<" "; }else{cout<<c[i][j]<<endl;} } } return 0; }
0
CPP
c = input() h = input() if c[0] in h or c[1] in h: print("YES") else: print("NO")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; void mian() { long long x; scanf("%lld", &x); printf("%lld\n", (((x / 4 + 1) % mod * ((x / 2 + 1) % mod) - (x / 4) % mod * ((x / 4 + 1) % mod)) % mod + mod) % mod); } int main() { int testnum; cin >> testnum; while (testnum--) mian(); return 0; }
15
CPP
#include <bits/stdc++.h> using namespace std; map<int, int> M; int st[3002 + 1][3002 + 1]; int dr[3002 + 1][3002 + 1]; int cost[3002 + 1]; int val[3002 + 1]; int x[3002 + 1]; int N, K; int main() { int i, j, res = 300000001; scanf("%d", &N); for (i = 1; i <= N; ++i) scanf("%d", &val[i]); for (i = 1; i <= N; ++i) scanf("%d", &cost[i]); for (i = 1; i <= N; ++i) x[i] = val[i]; sort(val + 1, val + N + 1); for (i = 1; i <= N; ++i) if (!M[val[i]]) M[val[i]] = ++K; for (i = 1; i <= N; ++i) x[i] = M[x[i]]; for (i = 0; i <= N + 1; ++i) for (j = 0; j <= N + 1; ++j) st[i][j] = dr[i][j] = 300000001; for (i = 1; i <= N; ++i) { for (j = 1; j <= K; ++j) st[i][j] = min(st[i][j], st[i - 1][j]); for (j = x[i]; j <= K; ++j) st[i][j] = min(st[i][j], cost[i]); } for (i = N; i >= 1; --i) { for (j = 1; j <= K; ++j) dr[i][j] = min(dr[i][j], dr[i + 1][j]); for (j = x[i]; j >= 1; --j) dr[i][j] = min(dr[i][j], cost[i]); } for (i = 2; i < N; ++i) res = min(res, cost[i] + st[i - 1][x[i] - 1] + dr[i + 1][x[i] + 1]); if (res == 300000001) printf("-1\n"); else printf("%d\n", res); return 0; }
9
CPP
n=int(input()) a=list(input()) if "1" in a: print("HARD") else: print("EASY")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1005, C = 1e6 + 5; int n, m, s[N], t[N]; inline bool cmp(const int i, const int j) { return s[i] > s[j]; } pair<int, int> ans[C]; void reduce(int a, int b, int c) { while (1) { if (s[a] > s[b]) swap(a, b); if (s[b] > s[c]) swap(b, c); if (s[a] > s[b]) swap(a, b); if (!s[a]) return; for (int q = s[b] / s[a]; q; s[a] *= 2, q >>= 1) if (q & 1) s[b] -= s[a], ans[m++] = pair<int, int>(a, b); else s[c] -= s[a], ans[m++] = pair<int, int>(a, c); } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", s + i), t[i] = i; sort(t + 1, t + n + 1, cmp); while (n && !s[t[n]]) n--; if (n < 2) { puts("-1"); return 0; } for (; n > 2; n--) { reduce(t[n - 2], t[n - 1], t[n]); if (!s[t[n - 2]]) t[n - 2] = t[n]; if (!s[t[n - 1]]) t[n - 1] = t[n]; } printf("%d\n", m); for (int i = 0; i < m; i++) printf("%d %d\n", ans[i].first, ans[i].second); }
11
CPP
t=int(input()) while t>0: a=input() l=len(a) if l<=10: print(a) else: print(a[0],end="") print(l-2,end="") print(a[-1]) t-=1
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); long long a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, w, x, y, z, cnt, sum, total, ar[100005], br[100005], row, col, nx, ny, hi, lo, mid, ans, tc; cin >> n >> k; for (i = 1; i <= n; i++) { cin >> ar[i]; s = ((i + 1) * i) / 2; br[i] = s; } for (i = 1; i <= n; i++) { if (k > br[i] && k < br[i + 1]) { d = i + 1; f = 1; break; } else if (k == br[i]) { d = i; f = 0; break; } } if (f) { s = k - br[i]; } else { s = i; } cout << ar[s]; }
8
CPP
#include <iostream> using namespace std; int n, x, nr; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> x; while (x % 2 == 0) { nr++; x /= 2; } } cout << nr; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; string s; int main() { cin >> s; if (s.substr(0, 4) == "http") { cout << "http://"; s = s.substr(4); } else { cout << "ftp://"; s = s.substr(3); } int v = s.substr(1).find("ru"); cout << s.substr(0, v + 1) << ".ru"; if (v != s.size() - 3) cout << "/" << s.substr(v + 3); cout << endl; return 0; }
8
CPP
import sys def input(): return sys.stdin.readline().strip() def ints(): return map(int, input().split()) T = int(input()) for _ in range(T): n = int(input()) a = list(ints()) sa = sorted(a) if sa[::2] == sorted(a[::2]) and sa[1::2] == sorted(a[1::2]): print('yes') else: print('no')
9
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { char s[9][9]; int sumA, sumB, tem; while (gets(s[0])) { for (int i = 1; i < 8; i++) gets(s[i]); sumA = sumB = tem = 0; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { tem++; if (isupper(s[i][j])) { switch (s[i][j]) { case 'Q': { sumA += 9; break; } case 'R': { sumA += 5; break; } case 'B': { sumA += 3; break; } case 'N': { sumA += 3; break; } case 'P': { sumA += 1; break; } default: break; } } if (islower(s[i][j])) { switch (s[i][j]) { case 'q': { sumB += 9; break; } case 'r': { sumB += 5; break; } case 'b': { sumB += 3; break; } case 'n': { sumB += 3; break; } case 'p': { sumB += 1; break; } default: break; } } } } if (sumA > sumB) { printf("White\n"); } else if (sumA < sumB) { printf("Black\n"); } else { printf("Draw\n"); } } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int m; cin >> m; for (int j = 0; j < m; j++) { unsigned int l, r, k; cin >> l >> r >> k; l--; vector<int> new_pos(r - l); for (int i = 0; i < r - l; i++) new_pos[i] = (k % (r - l) + i) % (r - l); string s_t(s); for (int i = 0; i < r - l; i++) s_t[l + new_pos[i]] = s[l + i]; s = s_t; } cout << s << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100500; const long long inf = 1ll << 60; long long s[N], ans = -inf; int n, a[N], L, R; map<int, int> first, last; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", a + i); for (int i = 0; i < n; i++) if (!first[a[i]]) first[a[i]] = i + 1; for (int i = n - 1; i >= 0; i--) if (!last[a[i]]) last[a[i]] = i + 1; for (int i = 0; i < n; i++) s[i + 1] = s[i] + max(a[i], 0); for (int i = 0; i < n; i++) { int l = first[a[i]]; int r = last[a[i]]; if (l && r && l != r) { long long cur = s[r - 1] - s[l] + a[i] * 2; if (cur > ans) { ans = cur; L = l; R = r; } } } int k = 0; for (int i = 1; i <= n; i++) if (i < L || i > R || (L < i && a[i - 1] < 0 && i < R)) k++; printf( "%lld" " %d\n", ans, k); for (int i = 1; i <= n; i++) if (i < L || i > R || (L < i && a[i - 1] < 0 && i < R)) printf("%d ", i); return 0; }
7
CPP
l=[] for i in range(5): l.append(list(map(int,input().strip().split()))) for i in range(5): if 1 in l[i]: c=l[i].index(1)+1 r=i+1 print(abs(3-r)+abs(3-c))
7
PYTHON3
def nbTriangle(cote): total=0 for i in range(1, cote+1, 1): total+=2*i-1 return total lenght=input().split(" ") for i in range(6): lenght[i]=int(lenght[i]) coteGrandTriangle=lenght[0]+lenght[1]+lenght[2] totalTriangle=nbTriangle(coteGrandTriangle) ans=totalTriangle-nbTriangle(lenght[0])-nbTriangle(lenght[2])-nbTriangle(lenght[4]) print(ans)
7
PYTHON3
def read_int(): inp = map(int, input().strip().split()) return inp def test(): n, = read_int() s = input().strip() count = {0: {0: 0, 1: 0}, 1: {0: 0, 1: 0}} for i, v in enumerate(s, 1): v = int(v) count[i % 2][v % 2] += 1 if n % 2: if count[1][1]: print(1) else: print(2) else: if count[0][0]: print(2) else: print(1) t, = read_int() for _ in range(t): test()
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, k, ans = 0; cin >> n >> k; while (n > 0 && k / n < 3) { k -= 2; n -= 1; ans += 1; } cout << ans; return 0; }
7
CPP
n=int(input()) count = 0 while(n!=0): val = list(input().split(" ")) n1= val.count('1') if (n1 >= 2): count=count+1 n1=0 n=n-1 print(count)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; double f[2000100], p[50], x[50]; int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) scanf("%lf", &p[i]); f[0] = 1; for (int j = 1; j < (1 << n); j++) { double opp = 0; int cou = n; for (int i = 0; i < n; i++) if ((j & (1 << i)) == 0) { opp += p[i]; cou--; } if (cou > k) continue; for (int i = 0; i < n; i++) { if (j & (1 << i) && p[i] > 1e-12) { double inc = f[j - (1 << i)] * (p[i] / (opp + p[i])); f[j] += inc; x[i] += inc; } } } printf("%lf", x[0]); for (int i = 1; i < n; i++) printf(" %lf", x[i]); printf("\n"); return 0; }
11
CPP
n = int(input()) arr = list(map(int,input().split(' '))) arr = sorted(arr) temp1 = 0 temp2 = 0 i = 0 for i in range(0,int(n/2)): temp1 = temp1 + abs(int(arr[i])-(2*i+1)) temp2 = temp2 + abs(int(arr[i])-(2*i+2)) print(min(temp1,temp2))
7
PYTHON3
from math import sqrt, ceil, floor t = int(input()) for _ in range(t): n,d=map(int,input().split()) x = floor(sqrt(d))-1 print("YES" if x+ceil(d/(x+1))<=n else "NO")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; struct BIT { vector<int> sum; int len; void Init(int _len) { len = _len + 5; sum.resize(len, 0); } void Add(int pos, int how) { pos += 1; for (; pos < len; pos += (pos & (-pos))) sum[pos] += how; } int Get(int pos) { pos += 1; int res = 0; for (; pos > 0; pos -= (pos & (-pos))) res += sum[pos]; return res; } }; int n; vector<pair<int, pair<int, int> > > in; map<int, vector<int> > touch; map<int, BIT> vals; int GetIdx(int x, int t) { vector<int>& tim = touch[x]; return lower_bound(tim.begin(), tim.end(), t) - tim.begin(); } int main() { scanf("%d", &n); in.resize(n); for (int i = 0; i < n; ++i) { scanf("%d%d%d", &in[i].first, &in[i].second.first, &in[i].second.second); touch[in[i].second.second].push_back(in[i].second.first); } for (auto& el : touch) { sort(el.second.begin(), el.second.end()); el.second.erase(unique(el.second.begin(), el.second.end()), el.second.end()); BIT& b = vals[el.first]; b.Init(((int)(el.second).size()) + 1); } for (int i = 0; i < n; ++i) { int t = in[i].second.first; int x = in[i].second.second; if (in[i].first == 1) { int idx = GetIdx(x, t); vals[x].Add(idx, +1); } if (in[i].first == 2) { int idx = GetIdx(x, t); vals[x].Add(idx, -1); } if (in[i].first == 3) { int idx = GetIdx(x, t); int cnt = vals[x].Get(idx); (cnt) = max((cnt), (0)); printf("%d\n", cnt); } } return 0; }
11
CPP
for _ in range(int(input())): n = int(input()) p = list(map(int,input().split())) low = 1 possible = True i = n-1 while possible and i>=0: start = (i-(p[i]-low)) end = i+1 if start<0: print("No") break if p[start:end]!=list(range(low,p[i]+1)): print("No") break low = p[i]+1 i = start-1 else: print("Yes")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; long long a, b; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } int main() { scanf("%lld%lld", &a, &b); long long lm = lcm(a, b); long long mx = max(lm / a, lm / b); long long mn = min(lm / a, lm / b); if (mn + 1 == mx) { puts("Equal"); } else if (lm / a < lm / b) { puts("Masha"); } else { puts("Dasha"); } return 0; }
9
CPP
#include<iostream> #include<queue> #include<algorithm> #include<vector> using namespace std; #define REP(i,b,n) for(int i=b;i<n;i++) #define rep(i,n) REP(i,0,n) const int N = 1000; const int M = 100; char m[N][N+20]; char in[M][M+20]; char *ptr[N]; int atm[N][N+20]; struct PMA{ PMA *next[3];//next[0] is for fail, 0x100 = 256 int id; vector<int> ac; PMA(){fill(next,next+3,(PMA*)0);id=-1;} }; PMA *buildPMA(char *p[M],int size,int *acdata){ PMA* root = new PMA; int cnt=0; rep(i,size){ PMA *t = root; for(int j=0;p[i][j] != '\0';j++){ char c=p[i][j]-'0'+1; if (t->next[c] == NULL)t->next[c]=new PMA; t=t->next[c]; } if (t->id == -1)t->id=cnt++; acdata[i]=t->id; t->ac.push_back(t->id); } queue<PMA*> Q; REP(i,1,3){ char c=i; if (root->next[c]){ root->next[c]->next[0]=root; Q.push(root->next[c]); }else root->next[c]=root; } while(!Q.empty()){ PMA *t = Q.front();Q.pop(); for(int c='0'-'0'+1;c <= '1'-'0'+1;c++){ if (t->next[c]){ Q.push(t->next[c]); PMA *r = t->next[0]; while(!r->next[c])r=r->next[0]; t->next[c]->next[0]=r->next[c]; copy(r->next[c]->ac.begin(),r->next[c]->ac.end(), back_inserter(t->next[c]->ac)); } } } return root; } void match(PMA *r,char *tar,int *res){ for(int i=0;tar[i] != '\0';i++){ char c=tar[i]-'0'+1; while(!r->next[c])r=r->next[0]; r=r->next[c]; rep(j,r->ac.size()){ //res[r->ac[j]]++; //res[r->ac[j]]=i; res[i]=r->id;//ac[j]; } } } int kmp(int p1,int* text,int p2,int* word){ static int table[N]; int i=2,j=0;//i:searching word j:backtracking point table[0]=-1;// table[1]=0;// while(i <= p2){//make table if ( word[i-1] == word[j]){ table[i]=j+1; i++; j++; }else if ( j > 0){ j = table[j]; }else { table[i]=0; i++; } } //the main part of KMP algorithm int pos;// int m=0;// int cnt=0; i = 0;// while(m+i <p1){ if (word[i] == text[m+i]){ i++; if ( i == p2){ //return m; cnt++; m=m+i-table[i]; if (i>0)i=table[i]; } }else { m = m+i-table[i]; if (i > 0)i = table[i]; } } return cnt; } int compute(int r,int c,int p){ int ret=0; static int ac[M+10],tres[N+10]; rep(i,p)ptr[i]=in[i]; PMA *root = buildPMA(ptr,p,ac); rep(i,r){ rep(j,c)tres[j]=-1; match(root,m[i],tres); rep(j,c)atm[i][j]=tres[j]; } rep(j,c){ rep(i,r)tres[i]=atm[i][j]; ret+=kmp(r,tres,p,ac); } return ret; } char pat[8][M][M+2];//8 pattern void decode(int c,string &in,char *pt){ int p=in.size()*6-1; for(int i=in.size()-1;i>=0;i--){ int val; if (isupper(in[i]))val=in[i]-'A'; else if (islower(in[i]))val=in[i]-'a' + 26; else if (isdigit(in[i]))val=in[i]-'0' + 52; else if (in[i] == '+')val=62; else if (in[i] == '/')val=63; rep(j,6){ pt[p--]=val%2?'1':'0'; val/=2; } } pt[c]='\0'; } void rotate(int n,int cur){ static char tmp[M][M]; rep(i,n)rep(j,n)tmp[i][j]=pat[cur-1][i][j]; rep(i,n){ rep(j,n){ pat[cur][n-1 -j][i]=tmp[i][j]; } } } void mirror(int n,int cur){ rep(i,n){ rep(j,n/2)swap(pat[cur][i][j],pat[cur][i][n-1-j]); } } bool issame(int n,int cur,int tar){ rep(i,n){ rep(j,n)if (pat[cur][i][j] != pat[tar][i][j])return false; } return true; } int solve(int r,int c,int p){ int ret=0; bool check[8]={false}; fill(check,check+8,true); rep(k,8){ rep(i,p)pat[k][i][p]='\0'; } rep(i,p){ rep(j,p)pat[0][i][j]=pat[4][i][j]=in[i][j]; } REP(i,1,4)rotate(p,i); mirror(p,4); REP(i,5,8)rotate(p,i); rep(i,8){ if (!check[i])continue; REP(j,i+1,8){ if (issame(p,i,j))check[j]=false; } } rep(k,8){ if (check[k]){ rep(i,p)rep(j,p+1)in[i][j]=pat[k][i][j]; ret+=compute(r,c,p); } } return ret; } int main(){ int c,r,p; while(cin>>c>>r>>p && c){ rep(i,r){ string tmp; cin>>tmp; decode(c,tmp,m[i]); } rep(i,p){ string tmp; cin>>tmp; decode(p,tmp,in[i]); } cout << solve(r,c,p) << endl; } return false; }
0
CPP
#include <bits/stdc++.h> #pragma GCC optimize("unroll-loops") #pragma GCC optimize("Ofast") #pragma GCC optimize("O3") using namespace std; int n, k; int val[10] = {119, 18, 93, 91, 58, 107, 111, 82, 127, 123}; vector<int> a; inline int cnt(int mask) { int ans = 0; for (int i = 0; i < 8; i++) { if (mask >> i & 1) { ans++; } } return ans; } int to_val(string& s) { reverse((s).begin(), (s).end()); int ans = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '1') { ans += (1 << i); } } return ans; } void solve() { vector<vector<int> > dp(n + 1, vector<int>(k + 1, -1)); vector<vector<int> > prev(n + 1, vector<int>(k + 1, -1)); dp[0][0] = 0; for (int i = 0; i < n; i++) { for (int j = 0; j <= k; j++) { if (dp[i][j] == -1) { continue; } for (int z = 0; z < 10; z++) { if ((a[n - 1 - i] & val[z]) == a[n - 1 - i]) { int need = a[n - 1 - i] ^ val[z]; int add = cnt(need); if (j + add > k) { continue; } if (z > dp[i + 1][j + add]) { dp[i + 1][j + add] = z; prev[i + 1][j + add] = j; } else if (z == dp[i + 1][j + add]) { assert(0 == 1); int y1 = j; int y2 = prev[i + 1][j + add]; int x = i; while (x > 0 && dp[x][y1] == dp[x][y2]) { y1 = prev[x][y1]; y2 = prev[x][y2]; x--; } if (dp[x][y1] > dp[x][y2]) { dp[i + 1][j + add] = z; prev[i + 1][j + add] = j; } } } } } } if (dp[n][k] == -1) { cout << "-1\n"; } else { int x = n; int y = k; while (prev[x][y] != -1) { assert(x >= 0 && y >= 0); cout << dp[x][y]; assert(prev[x][y] != -1); y = prev[x][y]; x--; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; cin >> n >> k; a.resize(n); for (int i = 0; i < n; i++) { string s; cin >> s; a[i] = to_val(s); } solve(); return 0; }
10
CPP
if __name__ == "__main__": data = input().split() n, a, b = map(int, data) best = 0 for i in range(1, n): x = i y = n - i if a < x: continue if b < y: continue ma = int(a / x) mb = int(b / y) m = min(ma, mb) if m > best: best = m print(best) # print("Finished")
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 111; int n, k; int a[MAX], b[MAX]; int id[MAX]; bool was[MAX]; vector<int> ans; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> k; for (int i = 0; i < n; ++i) { cin >> a[i]; was[a[i]] = true; } for (int i = 0; i < n; ++i) { cin >> b[i]; } memset(id, -1, sizeof id); for (int i = 0; i < n; ++i) { if (id[a[i]] == -1 || b[id[a[i]]] < b[i]) id[a[i]] = i; } for (int i = 0; i < n; ++i) { if (id[a[i]] != i) { ans.push_back(b[i]); } } sort(ans.begin(), ans.end()); int dd = 0; long long res = 0; for (int i = 1; i <= k; ++i) { if (!was[i]) { res += ans[dd++]; } } cout << res; return 0; }
19
CPP
from math import ceil n, t, k, d = tuple(list(map(int, input().split(' ')))) if k>n: print ("NO") else: iters = ceil(n/float(k)) time = iters*t num2 = int(float(d)/t)*k time2 = d while num2<n: num2 += 2*k time2 += t if time2<time: print ("YES") else: print ("NO")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; signed long long gcd(signed long long a, signed long long b) { return b ? gcd(b, a % b) : a; } int main() { signed long long x, y, a, b, q; cin >> x >> y >> a >> b; q = x * y / gcd(x, y); cout << b / q - (a - 1) / q << endl; }
7
CPP
if __name__ == '__main__': t = int(input()) while t: n, k1, k2 = map(int, input().split(' ')) max1 = max(map(int, input().split(' '))) max2 = max(map(int, input().split(' '))) if max1 > max2: print('YES') else: print('NO') t -= 1
7
PYTHON3
n=int(input());a=list(map(int,input().split())) p=s=a[0];s1=0;v=[1] for i in range(1,n): if p>=a[i]*2:s+=a[i];v+=i+1, if s>sum(a)//2:print(len(v));print(*v) else:print(0)
7
PYTHON3
#include<bits/stdc++.h> using namespace std; int main(){ int y,z; cin>>y>>z; if(y<=z) cout<<y<<endl; else cout<<y-1<<endl; }
0
CPP
from collections import deque n, who = map(int, input().split()) who -= 1 rating = list(map(int, input().split())) score = deque(map(int, input().split())) rating[who] += score.popleft() bestWho = rating[who] for i in range(n): if i == who: continue if rating[i] + score[-1] > bestWho: rating[i] += score.popleft() else: rating[i] += score.pop() print(sorted(rating, reverse=True).index(bestWho) + 1)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 4400; int dp[N][N]; int t[N], d[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> t[i] >> d[i]; for (int i = 1; i < N; i++) for (int j = 0; j < N; j++) dp[i][j] = INT_MAX; for (int i = 1; i <= n; i++) { for (int j = 0; j <= k; j++) { if (j == 0) { dp[i][j] = max(dp[i - 1][j] + 1, t[i]) + d[i] - 1; continue; } dp[i][j] = min(dp[i][j], max(dp[i - 1][j] + 1, t[i]) + d[i] - 1); dp[i][j] = min(dp[i][j], dp[i - 1][j - 1]); } } int ans = 0; for (int i = 0; i <= n; i++) { for (int j = 0; j <= k; j++) { int st = dp[i][j] + 1; int en = ((i + k - j + 1) > n) ? 86400 : t[i + k - j + 1] - 1; if (en >= st) ans = max(ans, en - st + 1); } } cout << ans << endl; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int a[110]; int main() { int n, q, ct, num, ft; cin >> n >> q; for (int i = 0; i < q; i++) { cin >> ct >> num >> ft; int sum = 0; int numm = num; for (int j = 1; j <= n; j++) { if (numm == 0) break; if (a[j] <= ct) { numm--; } } if (numm) cout << -1 << endl; else { for (int j = 1; j <= n; j++) { if (num == 0) break; if (a[j] <= ct) { sum += j; a[j] = ct + ft; num--; } } cout << sum << endl; } } return 0; }
9
CPP
import math t = int(input()) for case in range(1, t+1): n, k = list(map(int, input().split())) a_vals = list(map(int, input().split())) min_a = min(a_vals) max_a = max(a_vals) if max_a > k: print(0) continue # First, get everything as close as possible. first_equal = True casts = 0 for val in a_vals: if first_equal and val == min_a: first_equal = False continue if (val <= k): casts += math.floor((k - val) / min_a) print(casts)
7
PYTHON3
#include <iostream> #include <algorithm> #include <iomanip> #include <map> #include <set> #include <queue> #include <stack> #include <numeric> #include <bitset> #include <cmath> static const int MOD = 1000000007; using ll = long long; using u32 = unsigned; using u64 = unsigned long long; using namespace std; template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208; int main() { vector<int> v(4); for (auto &&i : v) scanf("%d", &i); sort(v.begin(),v.end()); int ans = INF<int>; do { ans = min(ans, abs(v[0]+v[1]-v[2]-v[3])); }while(next_permutation(v.begin(),v.end())); cout << ans << "\n"; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; struct node { int best = INT_MAX; int a[9]; node() { fill(a, a + 9, INT_MAX); } node(int x) { int temp = x; for (int i = 0; i < 9; ++i) { a[i] = (temp % 10) ? x : INT_MAX; temp /= 10; } } node combine(node o) { node ret; ret.best = min(best, o.best); for (int i = 0; i < 9; ++i) { int x = a[i], y = o.a[i]; ret.a[i] = min(x, y); if (x < INT_MAX && y < INT_MAX) ret.best = min(ret.best, x + y); } return ret; } }; struct seg_tree { int n; vector<node> seg; seg_tree(int _n) : n(_n) { seg.assign(4 * n, node()); } void pull(int u) { seg[u] = seg[2 * u].combine(seg[2 * u + 1]); } void build(int a[], int u, int l, int r) { if (l == r) { seg[u] = node(a[l]); return; } int mid = (l + r) / 2; build(a, 2 * u, l, mid), build(a, 2 * u + 1, mid + 1, r); pull(u); } void build(int a[]) { build(a, 1, 0, n - 1); } void upd(int x, int val, int u, int l, int r) { if (l > x || r < x) return; if (l == r) { seg[u] = node(val); return; } int mid = (l + r) / 2; upd(x, val, 2 * u, l, mid), upd(x, val, 2 * u + 1, mid + 1, r); pull(u); } void upd(int x, int val) { upd(x, val, 1, 0, n - 1); } node qry(int x, int y, int u, int l, int r) { if (l > y || r < x) return node(); if (x <= l && r <= y) return seg[u]; int mid = (l + r) / 2; node a = qry(x, y, 2 * u, l, mid), b = qry(x, y, 2 * u + 1, mid + 1, r); return a.combine(b); } int qry(int x, int y) { node temp = qry(x, y, 1, 0, n - 1); return temp.best == INT_MAX ? -1 : temp.best; } }; int n, m, a[200000]; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 0; i < n; ++i) cin >> a[i]; seg_tree tree(n); tree.build(a); while (m--) { int type, x, y; cin >> type >> x >> y; if (type == 1) { tree.upd(x - 1, y); } if (type == 2) { cout << tree.qry(x - 1, y - 1) << '\n'; } } return 0; }
11
CPP
n=int(input()) x=[int(x) for x in input().split()] list=[None]*n for i in range(1,n+1): list[x[i-1]-1]=i for item in list: print(item,end=" ")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; pair<int, int> can[20010]; pair<int, int> a[10010]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cerr.tie(0); int n, l, r; cin >> n >> l >> r; int mx = 0; int cnt = 0; for (int i = 0; i < n; i++) { cin >> a[i].second; mx = max(mx, a[i].second); a[i].second *= -1; } for (int i = 0; i < n; i++) { cin >> a[i].first; cnt += a[i].first; } sort(a, a + n); pair<int, int> pp = {-n - 10, cnt}; fill(can, can + r + mx + 1, pp); can[0] = {0, cnt}; for (int i = 0; i < n; i++) { int x = -a[i].second; for (int j = r + mx; j >= x; j--) { auto p = can[j - x]; if (l <= j - x && j - x <= r && a[i].first) p.first++; if (a[i].first) p.second--; can[j] = max(can[j], p); } } int ans = 0; for (int i = 0; i <= r + mx; i++) { ans = max(ans, can[i].first + (int)(l <= i && i <= r && can[i].second > 0)); } cout << ans << endl; return 0; }
11
CPP
x = int(input()) Getlist = input().split(" ") Numlist = [int(i) for i in Getlist] citylist = [] for k in range(len(Numlist)): if k == 0: citylist.append([Numlist[1]-Numlist[0],Numlist[-1]-Numlist[0]]) elif k == len(Numlist)-1: citylist.append([Numlist[k]-Numlist[k-1],Numlist[k]- Numlist[0]]) else: citylist.append([Numlist[k+1]-Numlist[k],Numlist[k]-Numlist[k-1],Numlist[k]-Numlist[0],Numlist[-1]-Numlist[k]]) for i in range(x): print(min(citylist[i]),max(citylist[i]))
7
PYTHON3
n,m,z=map(int, input().split()) a=set(range(n,z+1,n)) b=set(range(m,z+1,m)) print(len(a&b))
7
PYTHON3
n =int(input()) text =input() arr =text.split() max=-100 for string in arr: count =0 for char in string: if ord(char) >= ord('A') and ord(char) <= ord('Z'): count+=1 if max < count: max =count print (max)
7
PYTHON3
def main() : a,b= map(int,input().split()) cnt = 0 while 1 : cnt = cnt + 1 a *= 3 b *= 2 if a > b : print(cnt) break if __name__ == '__main__' : main()
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MaxN = 3e3; int flat[MaxN + 5], a[MaxN + 5]; int main() { int n; int tot = 0, cot = 0; scanf("%d", &n); for (int i = 2; i <= n; i++) { bool ok = 0; for (int j = 2; j < i; j++) if (i % j == 0) ok = 1; if (!ok) flat[++tot] = i; } for (int i = 1; i <= n; i++) { int j = i; int cont = 0, k = 0; while (k != tot) { k++; bool flag = 0; while (j % flat[k] == 0) { j /= flat[k]; flag = 1; } if (flag) cont++; } if (cont == 2) a[i] = 1; } for (int i = 1; i <= n; i++) if (a[i] == 1) cot++; printf("%d\n", cot); }
7
CPP
#include <bits/stdc++.h> #pragma GCC optimize("unroll-loops") #pragma GCC optimize("O3") #pragma GCC optimize("fast-math") using namespace std; const int INF = 1e9; const long long INFll = 2e18; const int BASE1 = 179; const int BASE2 = 653; const long long MOD = 1e9 + 7; const int MAXN = 1e5 + 10; const long double PI = 3.1415926535; const long double EPS = 1e-10; vector<int> gener_z(string& s) { vector<int> zf(((int)(s).size()) + 1); zf[0] = 0; int l = 0, r = 0; for (int i = (1); i < (((int)(s).size())); i++) { if (i < r) zf[i] = min(zf[i - l], r - i); while (i + zf[i] < ((int)(s).size()) && s[zf[i]] == s[i + zf[i]]) zf[i]++; if (i + zf[i] > r) l = i, r = i + zf[i]; } return zf; } void solve() { int n, k; cin >> n >> k; string s; cin >> s; if (k == 1) { for (int i = 0; i < (n); i++) cout << 1; cout << "\n"; return; } vector<int> z = gener_z(s); vector<int> res(n); int r = -1; for (int i = 0; i < (n); i++) { if (i <= r) res[i] = 1; int len = i + 1; if (len % k != 0) continue; int p = len / k; if (z[p] >= len - p) { res[i] = 1; if (i + 1 < n) r = max(r, min(i + z[i + 1], i + p)); } } for (int i = 0; i < (n); i++) cout << res[i]; cout << "\n"; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << fixed << setprecision(15); int t = 1; while (t--) solve(); return 0; }
10
CPP
n, x, y = map(int, input().split()) coords = [list(map(int, input().split())) for _ in range(n)] points = lasers = 0 slopes = [] for i in range(n): if coords[i][0] == x: if "x" not in slopes:slopes+=['x'] elif(((y-coords[i][1]) / (x-coords[i][0])) not in slopes): slopes+=[((y - coords[i][1]) / (x - coords[i][0]))] print(len(slopes))
8
PYTHON3