solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
import sys, bisect input = sys.stdin.readline for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) printed = False for i in range(n - 1): if abs(a[i + 1] - a[i]) > 1: print('YES') print(i + 1, i + 2) printed = True break if not printed: print('NO')
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; int fa[100010]; vector<int> v[100010]; int dep; int cnt[100010]; void dfs(int x, int d) { cnt[d]++; dep = max(d, dep); for (auto y : v[x]) { dfs(y, d + 1); } } int main(int argc, char const *argv[]) { scanf("%d", &n); for (int i = 2; i <= n; i++) { int x; scanf("%d", &x); v[x].push_back(i); } dfs(1, 1); int ans = 0; for (int i = 1; i <= dep; i++) { ans += (cnt[i] & 1); } std::cout << ans << '\n'; return 0; }
1,500
CPP
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(15); string str; cin >> str; int l = 0; for (int i = 1; i < str.length(); ++i) { if (isupper(str[i])) l++; } if (l == str.length() - 1) { for (int i = 1; i < str.length(); ++i) { str[i] = tolower(str[i]); } if (isupper(str[0])) str[0] = tolower(str[0]); else str[0] = toupper(str[0]); } cout << str; return 0; }
1,000
CPP
n, m, q = map(int, input().split()) x = [0 for i in range(n+1)] b = input() s = input() for i in range(n-m+1): f = True for j in range(i, i+m): if b[j] != s[j-i]: f = False break if f: x[i+m] = 1 for i in range(1, n+1): x[i] = x[i] + x[i-1] x += [x[-1] for i in range(m)] for _ in range(q): l, r = map(int, input().split()) print(max(x[r]-x[l+m-2], 0))
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; const int NUM = 200100; int main() { int n; scanf("%d", &n); char loc[NUM]; scanf("%s", loc); unordered_map<int, int> m; int rooms = (n - 1) * 2; int ans = 0; for (int i = 0; i != rooms; i += 2) { m[loc[i] - 'a']++; if (m[loc[i + 1] - 'A'] == 0) { ans++; } else { m[loc[i + 1] - 'A']--; } } printf("%d\n", ans); return 0; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int equ[2 * maxn], cover[2 * maxn], a[maxn]; int main() { int T; cin >> T; while (T--) { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", a + i); fill(equ, equ + 2 * k + 5, 0); fill(cover, cover + 2 * k + 5, 0); for (int i = 1; i <= n / 2; i++) equ[a[i] + a[n - i + 1]]++; for (int i = 1; i <= n / 2; i++) { cover[min(a[i], a[n - i + 1]) + 1]++; cover[max(a[i], a[n - i + 1]) + k + 1]--; } for (int i = 3; i <= 2 * k; i++) cover[i] += cover[i - 1]; int ans = INT_MAX; for (int x = 2; x <= 2 * k; x++) ans = min(ans, cover[x] - equ[x] + (n / 2 - cover[x]) * 2); printf("%d\n", ans); } return 0; }
1,700
CPP
n = int(input()) def binarySearch(l, b): length = len(l) if length == 1: if l[0] + b > 0: return 1 return 0 if l[length // 2] + b > 0: return length - length // 2 + binarySearch(l[:length // 2], b) elif length //2 + 1 < length: return binarySearch(l[length//2 + 1 :], b) return 0 a = list(map(int, input().split())) b = list(map(int, input().split())) c = [] #d = [] for i in range(n): c.append(a[i] - b[i]) #d.append(b[i] - a[i]) count = 0 c.sort() ind = n for i in range(n): if c[i] > 0: ind = i break ind2 = n #x = 0 ''' if ind == 0: count += n * (n-1) // 2 else: for i in range(ind, n): if ind2 > 0: x = binarySearch(c[:ind2], c[i]) else: count += (n-i) * (2*i + n-i-1) // 2 break count += i - ind2 + x ind2 -= x ''' while n-1 >= ind: num = c[n - ind2] + c[n - 1] > 0 count += num * (ind2 - 1) n -= num ind2 -= 1 print(count)
1,400
PYTHON3
n = int(input()) d = {} for i in map(int, input().split()): d[i] = d.get(i, 0) + 1 a = list(sorted(d.keys())) ans = 0 s = set() for i in sorted(d): if d[i] == 1: if i in s: if i - 1 in s: s.add(i + 1) else: s.add(max(i - 1, 1)) else: if i - 1 in s: s.add(i) else: s.add(max(i - 1, 1)) elif d[i] == 2: if i - 1 in s: s.add(i) s.add(i + 1) else: s.add(max(i - 1, 1)) if i in s: s.add(i + 1) else: s.add(i) else: s.add(max(i - 1, 1)) s.add(i) s.add(i + 1) print(len(s))
1,500
PYTHON3
n = int(input()) coord = list(map(int,input().split())) ans = n for i in coord: s = 0 for j in coord: if abs(i-j) % 2 != 0: s += 1 if s < ans: ans = s print(ans)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; int k, n, m; int main() { cin >> k; while (k--) { string s; cin >> s; int l = s.size(); bool one = false; int ans = 0, tmp = 0; for (int i = 0; i < l; i++) { if (s[i] == '1' && !one) { one = true; } else if (s[i] == '0' && one) { tmp++; } else if (s[i] == '1' && one) { ans += tmp; tmp = 0; } } cout << ans << endl; } return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxn = 1005; int f1[maxn][15], f2[maxn][15]; int pre[maxn], suf[maxn]; int main() { int n, m; scanf("%d%d", &n, &m); f1[0][0] = 1; for (int len = 1; len <= m; len++) { for (int i = 1; i <= n; i++) { for (int j = 0; j <= i; j++) { f1[i][len] = ((long long)f1[i][len] + f1[j][len - 1]) % mod; } } } f2[n + 1][0] = 1; for (int len = 1; len <= m; len++) { for (int i = n; i >= 1; i--) { for (int j = n + 1; j >= i; j--) { f2[i][len] = ((long long)f2[i][len] + f2[j][len - 1]) % mod; } } } for (int i = n; i >= 1; i--) suf[i] = ((long long)suf[i + 1] + f2[i][m]) % mod; int ans = 0; for (int i = 1; i <= n; i++) { ans = (ans + (long long)f1[i][m] * suf[i]) % mod; } printf("%d\n", ans); return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; inline void read(int &x) { int v = 0, f = 1; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') f = -1; else v = (c & 15); while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15); x = v * f; } inline void read(long long &x) { long long v = 0ll, f = 1ll; char c = getchar(); while (!isdigit(c) && c != '-') c = getchar(); if (c == '-') f = -1; else v = (c & 15); while (isdigit(c = getchar())) v = (v << 1) + (v << 3) + (c & 15); x = v * f; } inline void readc(char &x) { char c; while (((c = getchar()) == ' ') || c == '\n') ; x = c; } inline void writes(string s) { puts(s.c_str()); } inline void writeln() { writes(""); } inline void writei(int x) { if (x < 0) { putchar('-'); x = abs(x); } if (!x) putchar('0'); char a[25]; int top = 0; while (x) { a[++top] = (x % 10) + '0'; x /= 10; } while (top) { putchar(a[top]); top--; } } inline void writell(long long x) { if (x < 0) { putchar('-'); x = abs(x); } if (!x) putchar('0'); char a[25]; int top = 0; while (x) { a[++top] = (x % 10) + '0'; x /= 10; } while (top) { putchar(a[top]); top--; } } const int dx[9] = {0, -2, -2, -1, -1, 1, 1, 2, 2}, dy[9] = {0, -1, 1, -2, 2, -2, 2, -1, 1}; int n, m, i, j, dis[1005][1005][2], x, y, xx, yy; bool inmap(int x, int y) { return 1 <= x && x <= n && 1 <= y && y <= m; } void bfs(int x, int y, int typ) { dis[x][y][typ] = 0; queue<int> qx, qy; qx.push(x); qy.push(y); while (!qx.empty()) { int x = qx.front(), y = qy.front(); int i; qx.pop(); qy.pop(); for (((i)) = (1); ((i)) <= ((8)); ((i))++) { int nx = x + dx[i], ny = y + dy[i]; if (inmap(nx, ny) && dis[nx][ny][typ] > dis[x][y][typ] + 1) { dis[nx][ny][typ] = dis[x][y][typ] + 1; qx.push(nx); qy.push(ny); } } } } void solve(int x, int y, int typ) { for (;;) { if (dis[x][y][typ] == 0) break; int i; for (((i)) = (1); ((i)) <= ((8)); ((i))++) { int nx = x + dx[i], ny = y + dy[i]; if (inmap(nx, ny) && dis[nx][ny][typ] == dis[x][y][typ] - 1) { printf("%d %d\n", nx, ny); x = nx; y = ny; break; } } } } int main() { scanf("%d%d", &n, &m); scanf("%d%d%d%d", &x, &y, &xx, &yy); memset((dis), (0x16), (sizeof((dis)))); bfs(n / 2, m / 2, 0); bfs(n / 2 + 1, m / 2, 1); double wg = dis[x][y][0]; double wb = dis[x][y][1]; double bg = dis[xx][yy][1] + 0.5; double bb = dis[xx][yy][0] + 0.5; if ((x + y) % 2 == (xx + yy) % 2) { if (bg < wg) { puts("BLACK"); solve(xx, yy, 1); return 0; } if (bb < wg + 1) { puts("BLACK"); solve(xx, yy, 0); solve(n / 2, m / 2, 1); return 0; } puts("WHITE"); solve(x, y, 0); } else { if (wg < bg) { puts("WHITE"); solve(x, y, 0); return 0; } if (wb < bg + 1) { puts("WHITE"); solve(x, y, 1); solve(n / 2 + 1, m / 2, 0); return 0; } puts("BLACK"); solve(xx, yy, 1); } return 0; }
2,900
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long int t; cin >> t; while (t--) { long long int n; cin >> n; long long int a[2 * n], bc = 0, sc = 0, d[n], x = 0, y = 0; long long int diffp[100005], diffn[100005]; memset(diffp, -1, sizeof(diffp)); memset(diffn, -1, sizeof(diffn)); diffp[0] = 0; for (long long int i = 0; i < 2 * n; i++) cin >> a[i]; for (long long int i = n - 1; i >= 0; i--) { if (a[i] == 1) sc++; else bc++; d[i] = sc - bc; } for (long long int i = n; i < 2 * n; i++) { if (a[i] == 1) { sc++; x++; } else { bc++; y++; } if (x - y < 0 && diffn[y - x] == -1) diffn[y - x] = i - n + 1; else if (x - y >= 0 && diffp[x - y] == -1) diffp[x - y] = i - n + 1; } if (sc == bc) { cout << "0" << endl; continue; } y = sc - bc; long long int ans = INT_MAX; if (sc - bc < 0 && diffn[bc - sc] != -1) ans = diffn[bc - sc]; else if (sc - bc >= 0 && diffp[y] != -1) ans = diffp[sc - bc]; for (long long int i = n - 1; i >= 0; i--) { y = sc - bc - d[i]; if (y < 0 && diffn[abs(y)] != -1) { ans = min(ans, diffn[abs(y)] + n - i); } else if (y >= 0 && diffp[y] != -1) { ans = min(ans, diffp[y] + n - i); } } cout << ans << endl; } }
1,700
CPP
from fractions import gcd a,b,n=map(int,input().split()) s1=0 s2=0 for i in range(0,n): n-=gcd(a,n) s1+=1 if n==0: break n-=gcd(b,n) s2+=1 if n==0: break if s1>s2: print(0) else: print(1)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, cnt, a[220000]; long long sum; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int l = 1; l <= n; l++) { sum = a[l + 1]; for (int r = l + 2; r <= n; r++) { if (a[l] < a[r]) { sum += a[r]; if (sum > (1 << ((int)log2(a[l]) + 1))) break; continue; } if ((a[l] ^ a[r]) == sum) cnt++; sum += a[r]; if (sum > (1 << ((int)log2(a[l]) + 1))) break; } } reverse(a + 1, a + n + 1); for (int l = 1; l <= n; l++) { sum = a[l + 1]; for (int r = l + 2; r <= n; r++) { if (a[l] <= a[r]) { sum += a[r]; if (sum > (1 << ((int)log2(a[l]) + 1))) break; continue; } if ((a[l] ^ a[r]) == sum) cnt++; sum += a[r]; if (sum > (1 << ((int)log2(a[l]) + 1))) break; } } printf("%d", cnt); return 0; }
2,500
CPP
t = int(input()) for _ in range(t): n, k, d = map(int, input().split()) a = list(map(int, input().split())) m = dict() for i in range(d): if a[i] not in m: m[a[i]] = 0 m[a[i]] += 1 ans = len(m) for i in range(n - d): m[a[i]] -= 1 if m[a[i]] == 0: m.pop(a[i]) if a[i + d] not in m: m[a[i + d]] = 0 m[a[i + d]] += 1 ans = min(ans, len(m)) print(ans)
1,000
PYTHON3
from collections import deque import math def main(): n = int(input()) t = input().strip() s_list = deque() for i in range(round(math.sqrt(2 * n + 1 / 4) - (1 / 2))): s_list.append(t[i*(i+1)//2]) print("".join(s_list)) main()
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 20, Mod = 1e9 + 7; int pl, pr, vl, vr, k; long double p, q; vector<int> v; void count(int t) { int L, R; L = min(v[t], pr) - max(v[t - 1] + 1, pl); R = min(v[t + k] - 1, vr) - max(v[t + k - 1], vl); p += max(L + 1, 0) * 1ll * max(R + 1, 0); L = min(v[t], vr) - max(v[t - 1] + 1, vl); R = min(v[t + k] - 1, pr) - max(v[t + k - 1], pl); p += max(L + 1, 0) * 1ll * max(R + 1, 0); if (k == 1) { if (vl <= v[t] && v[t] <= vr && pl <= v[t] && v[t] <= pr) p--; } } void add(int length) { for (int i = 0; i < (1 << length); i++) { int num = 0; for (int j = 0; j < length; j++) { num *= 10; if (i & (1 << j)) num += 4; else num += 7; } v.push_back(num); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> pl >> pr >> vl >> vr >> k; q = (pr - pl + 1) * 1ll * (vr - vl + 1); for (int i = 1; i <= 9; i++) add(i); v.push_back(0); v.push_back(Mod); v.push_back(Mod); sort(v.begin(), v.end()); int n = v.size(); for (int i = 1; i < n - k - 1; i++) { count(i); } cout << fixed << setprecision(10) << p / q; return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; int N, p[500001]; pair<int, int> tree1[500001], tree2[500001]; vector<pair<int, int> > g1[500001]; list<int> g2[500001]; list<int>::iterator edges[500001][2]; int parent(int x) { return (p[x] == x) ? x : (p[x] = parent(p[x])); } void merge(int x, int y) { p[parent(x)] = parent(y); } void dfs(int x, int p = 0) { for (auto e : g1[x]) { int y = e.first, e1 = e.second; if (y != p) { dfs(y, x); int yy = parent(y); int e2 = *g2[yy].begin(); printf("%d %d %d %d\n", tree1[e1].first, tree1[e1].second, tree2[e2].first, tree2[e2].second); int a = parent(tree2[e2].first), b = parent(tree2[e2].second); g2[a].erase(edges[e2][0]); g2[b].erase(edges[e2][1]); g2[a].splice(g2[a].begin(), g2[b]); merge(b, a); } } } int main() { scanf("%d", &N); iota(p + 1, p + N + 1, 1); for (int i = 0; i < (N - 1); ++i) { scanf("%d%d", &tree1[i].first, &tree1[i].second); if (tree1[i].first > tree1[i].second) swap(tree1[i].first, tree1[i].second); } for (int i = 0; i < (N - 1); ++i) { scanf("%d%d", &tree2[i].first, &tree2[i].second); if (tree2[i].first > tree2[i].second) swap(tree2[i].first, tree2[i].second); } sort(tree1, tree1 + N - 1); sort(tree2, tree2 + N - 1); int i1 = 0, i2 = 0, N1 = 0, N2 = 0; while (i1 < N - 1 || i2 < N - 1) { if (i1 < N - 1 && i2 < N - 1 && tree1[i1] == tree2[i2]) { merge(tree1[i1].first, tree1[i1].second); ++i1; ++i2; } else if (i2 == N - 1 || (i1 < N - 1 && i2 < N - 1 && tree1[i1] < tree2[i2])) { if (N1 != i1) tree1[N1] = tree1[i1]; ++N1; ++i1; } else { if (N2 != i2) tree2[N2] = tree2[i2]; ++N2; ++i2; } } for (int i = 0; i < (N1); ++i) { int a = parent(tree1[i].first), b = parent(tree1[i].second); g1[a].emplace_back(b, i); g1[b].emplace_back(a, i); } for (int i = 0; i < (N2); ++i) { int a = parent(tree2[i].first), b = parent(tree2[i].second); g2[a].push_front(i); edges[i][0] = g2[a].begin(); g2[b].push_front(i); edges[i][1] = g2[b].begin(); } printf("%d\n", N1); dfs(parent(1)); return 0; }
3,200
CPP
import sys input = sys.stdin.readline n=int(input()) P=[1]+list(map(int,input().split())) A=list(map(int,input().split())) for i in range(n): P[i]-=1 Y=[1]*n for i in range(n): Y[P[i]]=0 #print(Y) for i in range(n-1,0,-1): Y[P[i]]+=Y[i] S=[a for a in A] for i in range(n-1,0,-1): S[P[i]]+=S[i] OK=sum(A) NG=-1 while OK-NG>1: mid=(OK+NG)//2 for i in range(n): if Y[i]*mid<S[i]: NG=mid break else: OK=mid print(OK)
1,900
PYTHON3
from sys import stdin, exit inputs = map(int, next(stdin).split()) outputs = map(int, next(stdin).split()) extras = [x - y for x, y in zip(inputs, outputs)] for i in range(3): if extras[i] < 0: for x in range(3): while extras[i] < 0 and extras[x] > 1: extras[i] += 1 extras[x] -= 2 if extras[i] < 0: print("NO") exit(0) print("YES")
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAXNLAY = 1000010; const int MAXNCOL = 1000010; const int MAXLAYL = 5010; const int MAXSUML = 10000010; int nLay, nCol, prm; int layL[MAXNLAY], maxLayL, sumL[MAXNLAY]; int perm[MAXLAYL], fact[MAXLAYL]; int g[MAXLAYL][MAXLAYL]; int f[MAXSUML]; int inData() { scanf("%d %d %d", &nLay, &nCol, &prm); for (int i = 1; i <= nLay; i++) scanf("%d", &layL[i]); return 0; } inline int h(int i, int j) { return sumL[i - 1] + j; } int solve() { sumL[0] = 0; maxLayL = 0; for (int i = 1; i <= nLay; i++) { sumL[i] = (sumL[i - 1] + layL[i]) % prm; maxLayL = max(maxLayL, layL[i]); } g[1][1] = 1; for (int i = 2; i <= maxLayL; i++) { g[i][1] = 0; for (int j = 2; j < i; j++) g[i][j] = (g[i - 1][j - 1] + (long long)g[i - 1][j] * (j - 1)) % prm; g[i][i] = 1; } perm[0] = 1; for (int i = 1; i <= maxLayL; i++) perm[i] = (long long)perm[i - 1] * (nCol - i + 1) % prm; fact[0] = 1; for (int i = 1; i <= maxLayL; i++) fact[i] = (long long)fact[i - 1] * i % prm; for (int j = 1; j <= layL[1]; j++) f[h(1, j)] = (long long)g[layL[1]][j] * perm[j] % prm; for (int i = 2; i <= nLay; i++) { int sum = 0; for (int j = 1; j <= layL[i - 1]; j++) sum = (sum + f[h(i - 1, j)]) % prm; for (int j = 1; j <= layL[i]; j++) if (j <= layL[i - 1]) f[h(i, j)] = (long long)g[layL[i]][j] * ((long long)sum * perm[j] % prm - (long long)f[h(i - 1, j)] * fact[j] % prm + prm) % prm; else f[h(i, j)] = (long long)g[layL[i]][j] * sum % prm * perm[j] % prm; } int ans = 0; for (int j = 1; j <= layL[nLay]; j++) ans = (ans + f[h(nLay, j)]) % prm; printf("%d", ans); return 0; } int main() { inData(); solve(); return 0; }
2,600
CPP
x=sorted([int(x) for x in input().split( )]) if (x[0]+x[3]==x[1]+x[2] or x[0]+x[1]+x[2]==x[3]) : print("YES") else : print("NO")
800
PYTHON3
#include <bits/stdc++.h> int a, h, w; int extended_euclid(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return a; } int ans = extended_euclid(b, a % b, x, y); int p = y; y = x - a / b * y; x = p; if (x < 0) { y += (x / b - 1) * a; x -= (x / b - 1) * b; } if (x > b) { y += x / b * a; x %= b; } return ans; } int main() { scanf("%d%d%d", &a, &w, &h); if (h < a || w < a) { return printf("-1\n"), 0; } if (h < w) { std::swap(h, w); } int u = a + h, b = h - w, v = a + w, x, y; int gcd = extended_euclid(-u, v, x, y); if (b % gcd) { return printf("-1\n"), 0; } x *= b / gcd, y *= b / gcd; int c = std::abs(v / gcd); x = (x % c + c) % c; int max = w / a / c + 1; int p = max * c + x; while (1ll * p * a > w) { p -= c; } if (p > 0) { return printf("%.10lf\n", 1.0 * (w - 1ll * p * a) / (p + 1)), 0; } return printf("-1\n"), 0; }
2,700
CPP
for _ in range(int(input())): n=int(input()) arr=[int(x) for x in input().split()] arr.sort() left=0 right=n-1 ans=1 c=0 ans=max(arr[n-1]*arr[n-2]*arr[n-3]*arr[n-4]*arr[n-5],arr[0]*arr[1]*arr[n-1]*arr[n-2]*arr[n-3],arr[0]*arr[1]*arr[2]*arr[3]*arr[-1]) print(ans)
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 9; int n, m, k; vector<int> e[maxn]; int vis[maxn], ru[maxn]; queue<int> q; vector<int> s; bool serch(int x, int y) { int l = 0, r = e[x].size(); while (l <= r) { int mid = (l + r) >> 1; if (e[x][mid] < y) { l = mid + 1; } else if (e[x][mid] == y) { return true; } else { r = mid - 1; } } return false; } bool check() { for (int x = 0; x < s.size(); x++) { for (int y = 0; y < s.size(); y++) { if (s[x] == s[y]) continue; if (!binary_search(e[s[x]].begin(), e[s[x]].end(), s[y])) return false; } } return true; } bool solve() { for (int i = 1; i <= n; i++) { ru[i] = e[i].size(); if (ru[i] < k) { q.push(i); vis[i] = 1; } } while (!q.empty()) { int u = q.front(); vis[u] = 2; q.pop(); if (ru[u] == k - 1 && s.empty()) { s.push_back(u); for (int i = 0; i < e[u].size(); i++) { int v = e[u][i]; if (vis[v] != 2) s.push_back(v); } if (!check()) s.clear(); } for (int i = 0; i < e[u].size(); i++) { ru[e[u][i]]--; if (ru[e[u][i]] < k && vis[e[u][i]] == 0) { q.push(e[u][i]); vis[e[u][i]] = 1; } } } int ans = 0; for (int i = 1; i <= n; i++) if (!vis[i]) ans++; if (ans) { printf("1 %d\n", ans); for (int i = 1; i <= n; i++) { if (!vis[i]) { printf("%d ", i); } } puts(""); return true; } else if (s.size()) { printf("2\n"); for (int i = 0; i < s.size(); i++) { printf("%d ", s[i]); } puts(""); return true; } return false; } void init() { s.clear(); while (!q.empty()) q.pop(); for (int i = 1; i <= n; i++) e[i].clear(), vis[i] = 0; for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); e[u].push_back(v); e[v].push_back(u); } for (int i = 1; i <= n; i++) { sort(e[i].begin(), e[i].end()); } } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d%d%d", &n, &m, &k); init(); if (k * (k - 1) / 2 > m) { printf("-1\n"); continue; } if (solve()) continue; printf("-1\n"); } }
2,600
CPP
#include <bits/stdc++.h> using namespace std; const long long llINF = 9223372036854775807; const int INF = 2147483647; const int maxn = 2e5 + 7; const int maxm = 1e6 + 7; const int ALP = 26; const long long mod = 1e9 + 7; const double pi = acos(-1.0); const double eps = 1e-8; int T; int r, c, k; char s[107][107]; char ans[107][107]; set<char> table; void init() { table.clear(); memset(s, 0, sizeof(s)); memset(ans, 0, sizeof(ans)); for (int i = 0; i < 26; i++) table.insert('A' + i); for (int i = 0; i < 26; i++) table.insert('a' + i); for (int i = 0; i < 10; i++) table.insert('0' + i); } int main() { scanf("%d", &T); while (T--) { init(); scanf("%d%d%d", &r, &c, &k); for (int i = 1; i <= r; i++) scanf("%s", s[i] + 1); int sum = 0; for (int i = 1; i <= r; i++) for (int j = 1; j <= c; j++) sum += (s[i][j] == 'R'); int num = sum / k, res = sum % k; char idx = *table.begin(); table.erase(table.begin()); int cur = 0; int j = 1; int cnt = 0; for (int i = 1; i <= r; i++) { if (i % 2 == 1) { while (j <= c) { ans[i][j] = idx; cur += (s[i][j] == 'R'); if (s[i][j] == 'R') cnt++; if (cur == num + 1 && res > 0) { cur = 0; if (!table.empty() && sum - cnt > 0) { idx = *table.begin(); table.erase(table.begin()); } res--; } else if (cur == num && res == 0) { cur = 0; if (!table.empty() && sum - cnt > 0) { idx = *table.begin(); table.erase(table.begin()); } } j++; } if (j == c + 1) j = c; } else { while (j >= 1) { ans[i][j] = idx; cur += (s[i][j] == 'R'); if (s[i][j] == 'R') cnt++; if (cur == num + 1 && res > 0) { cur = 0; if (!table.empty() && sum - cnt > 0) { idx = *table.begin(); table.erase(table.begin()); } res--; } else if (cur == num && res == 0) { cur = 0; if (!table.empty() && sum - cnt > 0) { idx = *table.begin(); table.erase(table.begin()); } } j--; } if (j == 0) j = 1; } } for (int i = 1; i <= r; i++) printf("%s\n", ans[i] + 1); } return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int TC; cin >> TC; while (TC--) { int N; cin >> N; string s; cin >> s; int ans = 0; int cnt = 0; for (int i = N - 1; i >= 0; i--) { if (s[i] == 'P') cnt++; else { ans = max(ans, cnt); cnt = 0; } } cout << ans << "\n"; } }
800
CPP
n = int(input()) a = list(map(int,input().split())) i = 0 k = 0 while i < len(a) and k <= 1: if a[i] == 0: k += 1 i+=1 else: i+=1 if k == 1 and len(a) > 1: print("YES") elif k == 0 and len(a) == 1: print("YES") else: print("NO")
1,000
PYTHON3
t = int(input()) while (t): n = int(input()) a = [0] + list(map(int, input().split())) b = [0] + list(map(int, input().split())) k = 0 f = True for i in range(1, n + 1): if a[i] != b[i] and k == 0: k = b[i] - a[i] #print(k) if k < 0: f = False break elif (a[i] + k != b[i] and k != 0 and a[i] != b[i]) or (a[i - 1] == b[i - 1] and k != 0 and a[i] != b[i]): #print(a[i], b[i], a[i - 1] == b[i - 1] and k != 0 and a[i] != b[i]) f = False break if f: print('YES') else: print('NO') t -= 1
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<int> div1, div2; int main() { int n, m, a, b, i; cin >> n >> m; for (i = 1; i <= m; i++) { cin >> a >> b; if (a > b) swap(a, b); div2.push_back(a); div1.push_back(b); } if (m == 0) cout << n - 1; else { a = *max_element(div2.begin(), div2.end()); b = *min_element(div1.begin(), div1.end()); if (b <= a) cout << 0; else cout << b - a; } return 0; }
1,300
CPP
N = int(input()) List = [int(x) for x in input().split()] query = [] q = int(input()) for i in range(q): x = [int(x) for x in input().split()] query.append(x) second = [-1] * q if(query[q-1][0] == 2): second[q-1] = query[q-1][1] for i in range(q-2,-1,-1): if(query[i][0] == 2): second[i] = max(second[i+1],query[i][1]) else: second[i] = second[i+1] first = [-1] * N for i in range(q): if(query[i][0] == 1): first[query[i][1]-1] = i for i in range(N): if(first[i]!=-1): List[i] = query[first[i]][2] List[i] = max(List[i],second[first[i]]) else: if(second[0]!=-1): List[i] = max(List[i],second[0]) print(*List)
1,600
PYTHON3
#include <bits/stdc++.h> using namespace std; bool desc_sort(const int& lhs, const int& rhs) { return lhs > rhs; } bool desc_pii_sort(const pair<int, int>& lhs, const pair<int, int>& rhs) { return lhs > rhs; } bool second_sort(const pair<int, int>& lhs, const pair<int, int>& rhs) { pair<int, int> a, b; a.first = lhs.second; a.second = lhs.first; b.first = rhs.second; b.second = rhs.first; return a < b; } template <class T> ostream& operator<<(ostream& os, vector<T> V) { os << "[ "; for (auto v : V) os << v << " "; return os << "]"; } template <class T> ostream& operator<<(ostream& os, set<T> second) { os << "{ "; for (auto s : second) os << s << " "; return os << "}"; } template <class L, class R> ostream& operator<<(ostream& os, pair<L, R> P) { return os << "(" << P.first << "," << P.second << ")"; } template <class L, class R> ostream& operator<<(ostream& os, map<L, R> M) { os << "{ "; for (auto m : M) os << "(" << m.first << ":" << m.second << ") "; return os << "}"; } template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << "\n"; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } inline long long fast_expo(long long base, long long power, long long modulo = 1e0) { base %= modulo; if (base < 0LL) base += modulo; long long x = base, cnt = power, ans = 1LL; while (cnt) { if (cnt & 1LL) ans = (ans * x) % modulo; x = (x * x) % modulo; cnt >>= 1LL; } return ans; } inline long long inv(long long base, long long modulo = 1e0) { return fast_expo(base, modulo - 2LL, modulo); } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; int b[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) cin >> b[i]; int red = 0, blue = 0; int mymap[2 * n + 2]; vector<int> check(2 * n + 2, 0); mymap[n] = n; check[n] = 1; for (int i = n - 1; i >= 0; i--) { if (b[i] == 1) red++; else blue++; mymap[red - blue + n] = i; check[red - blue + n] = 1; } int ans = n + mymap[n]; red = 0; blue = 0; for (int i = 0; i < n; i++) { if (a[i] == 1) red++; else blue++; if (check[blue - red + n]) { ans = min((n - i - 1) + mymap[blue - red + n], ans); } } cout << ans << "\n"; } return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; int num[10005]; int main() { int t; cin >> t; while (t--) { int n, k; cin >> n >> k; if (n > k) { cout << k << endl; } else { int a = k / (n - 1), b = k % (n - 1); if (k % (n - 1) == 0) { cout << a * n - 1 << endl; } else cout << a * n + b << endl; } } }
1,200
CPP
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long t; cin >> t; while (t--) { long long m; cin >> m; vector<vector<long long>> res(2, vector<long long>(m)); string str1, str2; cin >> str1 >> str2; for (long long i = 0; i < m; i++) { res[0][i] = str1[i] - '0'; } for (long long i = 0; i < m; i++) { res[1][i] = str2[i] - '0'; } long long flag = 0; for (long long i = 0; i < m; i++) { if (res[0][i] == 1 && res[1][i] == 1) { cout << "NO" << endl; flag = 1; break; } } if (flag == 0) cout << "YES" << endl; } return 0; }
800
CPP
n=int(input()) a=[int(i) for i in input().split()] m=int(input()) b=[int(i) for i in input().split()] c=[0 for i in range(n)] d=[0 for i in range(m)] a.sort() b.sort() num=0 for i in range(n): for j in range(m): if (abs(a[i]-b[j])<=1)and(c[i]==0)and(d[j]==0): num+=1 c[i]=1 d[j]=1 print(num)
1,200
PYTHON3
from collections import Counter n = int(input()) a = list(map(int, input().split())) cnt = [0]*n for i, x in enumerate(a): while x % 2 == 0: cnt[i] += 1 x //= 2 ok = Counter(cnt).most_common(1)[0][0] ans = [] for i in range(n): if cnt[i] != ok: ans.append(a[i]) print(len(ans)) if ans: print(*ans)
1,900
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long n, k, x; cin >> n >> k; long long mn = 1000000000000000000; long long type, ans; for (int i = 0; i < k; i++) { cin >> x; long long p = n % x; if (p < mn) { mn = p; type = i + 1; ans = n / x; } } cout << type << " " << ans << endl; return 0; }
1,000
CPP
# cook your dish here from sys import stdin, stdout import math from itertools import permutations, combinations from collections import defaultdict from bisect import bisect_left from bisect import bisect_right def L(): return list(map(int, stdin.readline().split())) def In(): return map(int, stdin.readline().split()) def I(): return int(stdin.readline()) P = 1000000007 def maxCountSameSUM(arr, n): M = defaultdict(lambda:0) for i in range(n): for j in range(n): if i != j and (arr[i]+arr[j]) not in M.keys(): M[arr[i]+arr[j]] = 0 arr1 = list(M.keys()) #print(arr1) mx = 0 for val in arr1: lis = [1 for i in range(n)] for i in range(0, n-1): for j in range(i+1 , n): if lis[i] and lis[j] and (arr[i]+arr[j]) == val: lis[i] = 0 lis[j] = 0 M[arr[i] + arr[j]] += 1 #print(M) for key in M: mx = max(mx, M[key]) return mx def main(): for t in range(I()): n = I() arr = L() print(maxCountSameSUM(arr, n)) if __name__ == '__main__': main()
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class R> inline void Readin(R &K) { K = 0; bool F = false; char C = getchar(); while (C < '0' or C > '9') { if (C == '-') F = true; C = getchar(); } while (C <= '9' and C >= '0') K = (K << 1) + (K << 3) + C - '0', C = getchar(); if (F) K = -K; } const int MaxN = 3e5 + 10; int N; long long X, A[MaxN]; inline int P1() { long long Ans = 0, Sum = 0; for (register int i = 0; ++i <= N;) { Sum += A[i]; if (Sum < 0) Sum = 0; Ans = max(Ans, Sum); } return not printf("%lld\n", max(Ans, X * Ans)); } long long Al[MaxN], Br[MaxN], S[MaxN]; inline int P2() { long long Sum = 0; for (register int i = 0; ++i <= N;) { Sum += A[i]; if (Sum < 0) Sum = 0; Al[i] = Sum; } Sum = 0; for (register int i = N; i; --i) { Sum += A[i]; if (Sum < 0) Sum = 0; Br[i] = Sum; } for (register int i = 0; ++i <= N;) { S[i] = S[i - 1] + A[i] * X; Al[i] -= S[i]; Br[i] += S[i - 1]; } Br[N + 1] = S[N]; for (register int i = 1; ++i <= N;) Al[i] = max(Al[i - 1], Al[i]); for (register int i = N; i; --i) Br[i] = max(Br[i], Br[i + 1]); register long long Ans = 0; for (register int i = 0; i <= N; ++i) Ans = max(Ans, Al[i] + Br[i + 1]); return not printf("%lld\n", Ans); } int main() { Readin(N); Readin(X); for (register int i = 0; ++i <= N; Readin(A[i])) ; if (X > 0) return P1(); else return P2(); }
1,900
CPP
n, x, y = map(int, input().split()) a = input() b = str(10**x + 10**y) count = 0 for i in range(x + 1): if(a[-i] != b[-i]): count += 1 print(count)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9; const int mod = inf + 7; const double eps = 1e-9; const double pi = acos(-1.0); int n, k, m, used[2020][2020]; vector<int> g[2020]; bool c[2020]; vector<int> cur; void dfs(int v) { c[v] = 1; cur.push_back(v); for (__typeof(g[v].begin()) it = g[v].begin(); it != g[v].end(); it++) { int to = *it; if (!c[to]) dfs(to); } } int main() { scanf("%d\n", &n); scanf("%d\n", &k); for (int i = 0, x, y; i < k; i++) { scanf("%d%d", &x, &y); x--; y--; g[x].push_back(y); g[y].push_back(x); } scanf("%d\n", &m); for (int i = 0, x, y; i < m; i++) { scanf("%d%d\n", &x, &y); x--; y--; used[x][y] = used[y][x] = 1; } int ans = 0; for (int i = 0; i < n; i++) { if (!c[i]) { cur.clear(); dfs(i); bool ok = 1; for (int i = 0; i < cur.size(); i++) { for (int j = 0; j < cur.size(); j++) { if (used[cur[i]][cur[j]]) ok = 0; } } if (ok) ans = max(ans, (int)cur.size()); } } printf("%d\n", ans); return 0; }
1,500
CPP
n, k = (int(x) for x in input().split()) a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] cur_sum = 0 max_sum = 0 sum = 0 for i in range(n): if b[i] == 1: sum += a[i] if i < k: cur_sum += (1 - b[i]) * a[i] else: cur_sum += (1 - b[i]) * a[i] cur_sum -= (1 - b[i - k]) * a[i - k] if cur_sum > max_sum: max_sum = cur_sum # print(cur_sum) # print(max_th) print(sum + max_sum)
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAX = 150000; void pls() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int32_t main() { pls(); long long k, b, n, t; cin >> k >> b >> n >> t; if (t == 1) { cout << n << endl; } else if (k == 1) { long long at = max(double(0), ceil((1LL + b * n - t) / double(b))); cout << at << endl; } else { int x = 0; for (; x <= n + 1; x++) { long long p, q; p = powl(k, x + 1) + powl(k, x) * (b - 1); q = t * (k - 1) + b; if (p > q) { cout << n - x + 1 << endl; break; } } if (x == n + 2) cout << 0 << endl; } return 0; }
1,700
CPP
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx") using namespace std; const int INF = (int)1e9 + 123; const long long LINF = (long long)1e18 + 123; const long double EPS = (long double)1e-7; const long long MOD = (long long)1e9 + 7; long long power(long long x, long long y, long long mod = MOD) { if (y == 0) { return 1; } if (y & 1) { return power(x, y - 1, mod) * x % mod; } else { long long tmp = power(x, y / 2, mod); return tmp * tmp % mod; } } template <typename A, typename B> bool mini(A &x, const B &y) { if (y < x) { x = y; return true; } return false; } template <typename A, typename B> bool maxi(A &x, const B &y) { if (y > x) { x = y; return true; } return false; } void run(); int main() { if (strlen("")) { freopen( "" ".in", "r", stdin); freopen( "" ".out", "w", stdout); } ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout << fixed << setprecision(12); run(); return 0; } const int N = (int)2e5 + 123; template <typename T> struct Deque { int l, r; vector<T> q; Deque() { l = 0, r = 0; q.resize(4 * N + 100); } T &front() { return q[l]; } void pop_front() { l++; } T &back() { return q[r - 1]; } void pop_back() { r--; } int size() { return r - l; } void push_back(const T &x) { q[r++] = x; } void push_front(const T &x) { q[l--] = x; } void only_prefix() { if (size() > N) { r = l + N; } shift(); } void only_suffix() { if (size() > N) { l = r - N; } shift(); } void shift() { int len = size(); for (int i = 0; i < len; i++) { q[i] = q[l + i]; } l = 0; r = len; } }; int n, k, m; int a[N]; struct Shit { bool divided; long long rest; Deque<pair<int, int>> l, r; Shit() { divided = 0; rest = 0; } }; Shit init; void convert() { Deque<pair<int, int>> q; long long cnt = 0; for (int i = 0; i < n; i++) { cnt++; if (q.size() == 0) { q.push_back({a[i], 1}); } else { if (q.back().first == a[i]) { q.back().second++; } else { q.push_back({a[i], 1}); } } if (q.back().second >= k) { cnt -= k; q.pop_back(); } } init.rest = cnt; init.l = q; } Shit merge(Shit a, Shit b) { Shit res; if (a.divided && b.divided) { res.rest = a.rest + b.rest; while (a.r.size() && b.l.size() && a.r.back().first == b.l.front().first) { int cntL = a.r.back().second; int cntR = b.l.front().second; assert(cntL < k && cntR < k); if (cntL + cntR < k) { a.r.back().second += cntR; b.l.pop_front(); break; } a.r.pop_back(); if (cntL + cntR == k) { b.l.pop_front(); } else { b.l.front().second -= (k - cntL); } res.rest -= k; } res.divided = 1; res.l = a.l; res.r = b.r; } else if (a.divided) { res.rest = a.rest + b.rest; while (a.r.size() && b.l.size() && a.r.back().first == b.l.front().first) { int cntL = a.r.back().second; int cntR = b.l.front().second; assert(cntL < k && cntR < k); if (cntL + cntR < k) { a.r.back().second += cntR; b.l.pop_front(); break; } a.r.pop_back(); if (cntL + cntR == k) { b.l.pop_front(); } else { b.l.front().second -= (k - cntL); } res.rest -= k; } res.divided = 1; res.l = a.l; while (b.l.size()) { a.r.push_back(b.l.front()); b.l.pop_front(); } res.r = a.r; res.r.only_suffix(); } else if (b.divided) { res.rest = a.rest + b.rest; while (a.l.size() && b.l.size() && a.l.back().first == b.l.front().first) { int cntL = a.l.back().second; int cntR = b.l.front().second; assert(cntL < k && cntR < k); if (cntL + cntR < k) { a.l.back().second += cntR; b.l.pop_front(); break; } a.l.pop_back(); if (cntL + cntR == k) { b.l.pop_front(); } else { b.l.front().second -= (k - cntL); } res.rest -= k; } res.divided = 1; res.r = b.r; while (b.l.size()) { a.l.push_back(b.l.front()); b.l.pop_front(); } res.l = a.l; res.l.only_prefix(); } else { res.rest = a.rest + b.rest; while (a.l.size() && b.l.size() && a.l.back().first == b.l.front().first) { int cntL = a.l.back().second; int cntR = b.l.front().second; assert(cntL < k && cntR < k); if (cntL + cntR < k) { a.l.back().second += cntR; b.l.pop_front(); break; } a.l.pop_back(); if (cntL + cntR == k) { b.l.pop_front(); } else { b.l.front().second -= (k - cntL); } res.rest -= k; } while (b.l.size()) { a.l.push_back(b.l.front()); b.l.pop_front(); } if (a.l.size() >= 2 * N) { auto l = a.l; auto r = a.l; l.only_prefix(); r.only_suffix(); res.divided = 1; res.l = l; res.r = r; } else { res.l = a.l; } } return res; } Shit solve(int x) { if (x == 1) { return init; } Shit tmp = solve(x / 2); Shit shit = merge(tmp, tmp); if (x & 1) { shit = merge(shit, init); } return shit; } void run() { cin >> n >> k >> m; for (int i = 0; i < n; i++) { cin >> a[i]; } convert(); cout << solve(m).rest << "\n"; }
2,300
CPP
n = int(input()) string = input() + " " xs_in_a_row = 0 x_counts = [] for i in range(n + 1): if string[i] == 'x': xs_in_a_row += 1 else: x_counts.append(xs_in_a_row) xs_in_a_row = 0 min_num_of_chars = 0 for count in x_counts: if count >= 3: min_num_of_chars += count - 2 print(min_num_of_chars)
800
PYTHON3
import sys import bisect from bisect import bisect_left as lb input_=lambda: sys.stdin.readline().strip("\r\n") from math import log from math import gcd from math import atan2,acos from random import randint sa=lambda :input_() sb=lambda:int(input_()) sc=lambda:input_().split() sd=lambda:list(map(int,input_().split())) sflo=lambda:list(map(float,input_().split())) se=lambda:float(input_()) sf=lambda:list(input_()) flsh=lambda: sys.stdout.flush() #sys.setrecursionlimit(10**6) mod=10**9+7 gp=[] cost=[] dp=[] mx=[] ans1=[] ans2=[] special=[] specnode=[] a=0 kthpar=[] def dfs(root,par): if par!=-1: dp[root]=dp[par]+1 for i in range(1,20): if kthpar[root][i-1]!=-1: kthpar[root][i]=kthpar[kthpar[root][i-1]][i-1] for child in gp[root]: if child==par:continue kthpar[child][0]=root dfs(child,root) ans=0 def hnbhai(tc): n=sb() a=sd() d={} ans=0 for i in range(n): for j in range(32): if d.get((1<<j)-a[i]): ans+=d[(1<<j)-a[i]] d[a[i]]=d.get(a[i],0)+1 print(ans) for _ in range(1): hnbhai(_+1)
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; double pi = 3.1415926535; int main() { ios::sync_with_stdio(0); cin.tie(0); double d, h, v, e; cin >> d >> h >> v >> e; if ((4 * v) / (pi * d * d) < e) { cout << "NO\n"; return 0; } cout << "YES\n"; cout << h / ((4 * v) / (pi * d * d) - e) << "\n"; return 0; }
1,100
CPP
n=int(input()) result = [int(item) for item in input().split()] m=max(result);s=0 for i in result: if i<=m: s+=m-i print(s)
800
PYTHON3
import math def isPerfectSquare(x): s = int(math.sqrt(x)) return s * s == x def isFibonacci(n): return isPerfectSquare(5 * n * n + 4) or isPerfectSquare(5 * n * n - 4) n=int(input()) for i in range(n): if (isFibonacci(i+1) == True): print("O",end="") else: print("o",end="")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; const int MAXN = 110 * 26; const int MAXM = MAXN * 100; int read() { int kkk = 0, x = 1; char c = getchar(); while ((c < '0' || c > '9') && c != '-') c = getchar(); if (c == '-') c = getchar(), x = -1; while (c >= '0' && c <= '9') kkk = kkk * 10 + (c - '0'), c = getchar(); return kkk * x; } int n, m, S, T, dis[MAXN], dl[MAXN], h, t, pd[MAXN]; struct node { int head[MAXN], tot, st[MAXN]; int to[MAXM], nextn[MAXM], l[MAXM], cost[MAXM]; void ADD(int u, int v, int _l, int _cost) { to[++tot] = v, nextn[tot] = head[u], l[tot] = _l, cost[tot] = _cost; head[u] = tot; } void build(int u, int v, int _l, int _cost) { ADD(u, v, _l, _cost); ADD(v, u, 0, -_cost); } } Ed; int build(int N) { for (int i = 1; i <= N; ++i) Ed.st[i] = Ed.head[i]; memset(dis, -0x3f, sizeof(dis)); dis[S] = h = t = 0; dl[++t] = S; int loop = N + 1; while (h != t) { h = h % loop + 1; int u = dl[h]; pd[u] = 0; for (int i = Ed.head[u]; i != 0; i = Ed.nextn[i]) { int v = Ed.to[i]; if (dis[v] >= dis[u] + Ed.cost[i] || !Ed.l[i]) continue; dis[v] = dis[u] + Ed.cost[i]; if (!pd[v]) { pd[v] = 1; t = t % loop + 1; dl[t] = v; } } } return dis[T] != dis[0]; } int vis[MAXN]; pair<int, int> fun(int u, int flow) { if (u == T) return make_pair(flow, 0); int rest = flow, use = 0; vis[u] = 1; for (int i = Ed.st[u]; i != 0 && rest != 0; i = Ed.nextn[i]) { Ed.st[u] = i; int v = Ed.to[i]; if (dis[v] != dis[u] + Ed.cost[i] || vis[v] || !Ed.l[i]) continue; pair<int, int> K = fun(v, min(Ed.l[i], rest)); if (!K.first) dis[v] = inf; Ed.l[i] -= K.first; Ed.l[i ^ 1] += K.first; rest -= K.first; use += K.second + K.first * Ed.cost[i]; } vis[u] = 0; return make_pair(flow - rest, use); } void Dinic(int N) { int ans = 0; while (build(N)) while (1) { pair<int, int> K = fun(S, inf); if (!K.first) break; ans += K.second; } printf("%d\n", ans); } int ton[MAXN], val[MAXN]; char s[MAXN]; int main() { n = read(); Ed.tot = 1; scanf("%s", s + 1); for (int i = 1; i <= n; ++i) ++ton[s[i] - 'a']; for (int i = 1; i <= n; ++i) val[i] = read(); S = n + 26 + 26 * (n / 2) + 1, T = S + 1; for (int i = 1; i <= n; ++i) Ed.build(i, T, 1, 0); for (int i = 1; i <= 26; ++i) { Ed.build(S, i + n, ton[i - 1], 0); for (int j = 1; j <= n / 2; ++j) { Ed.build(i + n, n + 26 + (i - 1) * (n / 2) + j, 1, 0); Ed.build(n + 26 + (i - 1) * (n / 2) + j, j, 1, (s[j] - 'a' == i - 1 ? val[j] : 0)); Ed.build(n + 26 + (i - 1) * (n / 2) + j, n - j + 1, 1, (s[n - j + 1] - 'a' == i - 1 ? val[n - j + 1] : 0)); } } Dinic(T); return 0; }
2,500
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:256000000") using namespace std; const int INF = (int)1e9; const long double EPS = 1e-9; const long double PI = 3.1415926535897932384626433832795; int n, m; char a[1010][1010]; long long sI[1010], sJ[1010]; int main() { cin >> n >> m; memset(sI, 0, sizeof(sI)); memset(sJ, 0, sizeof(sJ)); for (int i = 0; i < int(n); ++i) for (int j = 0; j < int(m); ++j) { scanf(" %c", &a[i][j]); if (a[i][j] == '*') { sI[i]++; sJ[j]++; } } long long ans = 0; for (int i = 0; i < int(n); ++i) for (int j = 0; j < int(m); ++j) if (a[i][j] == '*') ans += (sI[i] - 1) * (sJ[j] - 1); cout << ans << endl; return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; int main(int argc, char **argv) { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> v(n + 2), tmp; for (int i = 1; i <= n; ++i) cin >> v[i]; vector<int> left(n + 2, 0); for (int i = 1; i <= n; ++i) left[i] = min(left[i - 1] + 1, v[i - 1] + 1); vector<int> right(n + 2, 0); for (int i = n; i >= 1; --i) right[i] = min(right[i + 1] + 1, v[i + 1] + 1); int ans = 0, now; for (int i = 1; i <= n; ++i) { now = min(v[i], min(left[i], right[i])); ans = max(ans, now); } cout << ans << '\n'; return 0; }
1,600
CPP
#include <bits/stdc++.h> struct pt { int x, y; }; pt operator+(pt a, pt b) { pt p = {std::min(a.x, b.x), std::max(a.y, b.y)}; return p; } int n, Q; int head[(1 << 18)], nxt[(1 << 18)], level[(1 << 18)], tree[(1 << 18) * 2], rev[(1 << 18)], tin[(1 << 18)]; pt pre[(1 << 18) * 2]; void dfs(int x) { static int timer = 1; tin[x] = timer; rev[timer] = x; pre[x + (1 << 18)].x = tin[x]; pre[x + (1 << 18)].y = tin[x]; tree[timer + (1 << 18)] = level[x]; ++timer; for (int h = head[x]; h; h = nxt[h]) { int y = h; level[y] = 1 + level[x]; dfs(y); tree[timer + (1 << 18)] = level[x]; ++timer; } } pt query(int l, int r) { pt res = {(1 << 18), -(1 << 18)}; ++r; for (l += (1 << 18), r += (1 << 18); l < r; l >>= 1, r >>= 1) { if (l & 1) res = res + pre[l++]; if (r & 1) res = res + pre[--r]; } return res; } pt get(int l, int r, int ng) { return query(l, ng - 1) + query(ng + 1, r); } int ask(int l, int r) { int res = (1 << 18); ++r; for (l += (1 << 18), r += (1 << 18); l < r; l >>= 1, r >>= 1) { if (l & 1) res = std::min(res, tree[l++]); if (r & 1) res = std::min(res, tree[--r]); } return res; } int main(int argc, char* argv[]) { scanf("%d%d", &n, &Q); memset(head, 0, sizeof head); memset(tree, 0x33, sizeof tree); for (int i = 2; i <= n; ++i) { int p; scanf("%d", &p); nxt[i] = head[p]; head[p] = i; } level[1] = 0; dfs(1); for (int i = (1 << 18) - 1; i >= 1; --i) { pre[i] = pre[i * 2] + pre[i * 2 + 1]; tree[i] = std::min(tree[i * 2], tree[i * 2 + 1]); } while (Q--) { int l, r; scanf("%d%d", &l, &r); pt t = query(l, r); pt a = get(l, r, rev[t.x]); pt b = get(l, r, rev[t.y]); int na = ask(a.x, a.y); int nb = ask(b.x, b.y); if (na > nb) { printf("%d %d\n", rev[t.x], na); } else { printf("%d %d\n", rev[t.y], nb); } } }
2,300
CPP
#include <bits/stdc++.h> using namespace std; int main() { std::ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<long long> l(m), suffix(m + 1); long long total_len = 0; for (int i = 0; i < m; i++) { cin >> l[i]; total_len += l[i]; } if (total_len < n || n - l[m - 1] + 1 < m) { cout << "-1\n"; return 0; } for (int i = m - 1; i >= 0; i--) { suffix[i] = suffix[i + 1] + l[i]; } int cur = -1; vector<int> ans; int prev; for (long long i = m - 1; i >= 0; i--) { int cur = max(n - suffix[i], i); if (cur + l[i] > n) { cout << "-1\n"; return 0; } ans.push_back(cur); } reverse(ans.begin(), ans.end()); for (int i = 0; i < ans.size(); i++) { cout << ans[i] + 1 << ' '; } }
1,800
CPP
#include <bits/stdc++.h> using namespace std; void add(long long &x, long long y) { x += y; if (x >= 1000000007) x -= 1000000007; } long long C[1010][1010]; long long dp[1010][1010][4]; long long F[1010]; int main() { int N, K; scanf("%d%d", &N, &K); for (int i = 0; i <= N; i++) for (int j = 0; j <= i; j++) C[i][j] = j ? (C[i - 1][j] + C[i - 1][j - 1]) % 1000000007 : 1; dp[0][0][2] = 1; for (int i = 1; i <= N; i++) { for (int j = 0; j < i; j++) { for (int k = 0; k < 4; k++) { if (k & 1) add(dp[i][j + 1][k / 2 + 2], dp[i - 1][j][k]); if (i < N) add(dp[i][j + 1][k / 2], dp[i - 1][j][k]); add(dp[i][j][k / 2 + 2], dp[i - 1][j][k]); } } } for (int j = 0; j <= N; j++) { for (int k = 0; k < 4; k++) add(F[j], dp[N][j][k]); for (int k = 2; k <= N - j; k++) F[j] = (F[j] * k) % 1000000007; } for (int i = N - 1; i >= 0; i--) for (int j = i + 1; j <= N; j++) add(F[i], 1000000007 - F[j] * C[j][i] % 1000000007); printf("%I64d\n", F[K]); return 0; }
2,600
CPP
def func(aa1, aa2, kk1, kk2, nn): if (nn <= aa1 * (kk1 - 1) + aa2 * (kk2 - 1)): print(0) elif (nn < aa1 * kk1 + aa2 * kk2): print(nn - aa1 * (kk1 - 1) - aa2 * (kk2 - 1)) else: print(aa1 + aa2) if (nn <= aa1 * kk1): print(nn // kk1) elif(nn <= aa1 * kk1 + aa2 * kk2): print((nn - aa1 * kk1) // kk2 + aa1) else: print(aa1 + aa2) a1 = int(input()) a2 = int(input()) k1 = int(input()) k2 = int(input()) n = int(input()) if (k1 <= k2): func(a1, a2, k1, k2, n) else: func(a2, a1, k2, k1, n)
1,000
PYTHON3
from math import inf t = int(input()) for q in range(t): n = int(input()) A = [int(i) for i in input().split()] mi = inf ma = -1 * inf for i in range(len(A)): if A[i] != -1: if i < len(A) - 1 and A[i + 1] == -1: mi = min(A[i], mi) ma = max(A[i], ma) if i > 0 and A[i - 1] == -1: mi = min(A[i], mi) ma = max(A[i], ma) if mi == inf: num = 0 else: num = (mi + ma) // 2 d = 0 if A[0] == -1: A[0] = num for i in range(1, len(A)): if A[i] == -1: A[i] = num d = max(d, abs(A[i] - A[i - 1])) print(d, num)
1,500
PYTHON3
n,a,b=map(int,input().split()) airport=input() if airport[a-1]==airport[b-1]: print(0) else: print(1)
1,200
PYTHON3
def solve(g, i, j): ans = 0 for ii in range(i): ans = max(ans, min(g[ii])) print(ans) i, j = tuple(map(int, input().split())) g = [] for _ in range(i): g.append(list(map(int, input().split()))) solve(g, i, j)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 305; map<string, int> mp; string s; int id[N], sum[N]; int n, cnt, ans; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { cin >> s; if (!mp.count(s)) mp[s] = ++cnt; id[i] = mp[s]; sum[i] = sum[i - 1] + s.size(); } ans = sum[n] + n - 1; for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) { int len = j - i, cnt = 0; for (int k = j + 1; k + len <= n; k++) { bool f = true; for (int x = 0; x <= len; x++) if (id[i + x] != id[k + x]) { f = false; break; } if (f) { cnt++; k += len; } } if (cnt) ans = min(ans, sum[n] - (cnt + 1) * (sum[j] - sum[i - 1]) + cnt + n); } } printf("%d\n", ans); return 0; }
2,200
CPP
a = input() flag = False num0 = 0 for i in range(0,len(a)): if a[i] == '1': flag = True if flag == True and a[i] == '0': num0 = num0 + 1; if num0 >= 6 : print("yes") else: print("no")
1,000
PYTHON3
t=int(input()) for i in range(0,t): a=input() b=input() a=a.lower() b=b.lower() f=0 c=len(b)-len(a) if(len(b)==len(a) and sorted(a)==sorted(b)): print("YES") elif(len(b)<len(a)): print("NO") else: for i in range(0,c+1): d=b[i:(i+len(a))] if(sorted(d)==sorted(a)): print("YES") f=1 break if(f==0): print("NO")
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; inline char read() { static const int IN_LEN = 1000000; static char buf[IN_LEN], *s, *t; return (s == t ? t = (s = buf) + fread(buf, 1, IN_LEN, stdin), (s == t ? -1 : *s++) : *s++); } template <class T> inline void read(T &x) { static bool iosig; static char c; for (iosig = false, c = read(); !isdigit(c); c = read()) { if (c == '-') iosig = true; if (c == -1) return; } for (x = 0; isdigit(c); c = read()) x = ((x + (x << 2)) << 1) + (c ^ '0'); if (iosig) x = -x; } const int OUT_LEN = 10000000; char obuf[OUT_LEN], *ooh = obuf; inline void print(char c) { if (ooh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), ooh = obuf; *ooh++ = c; } template <class T> inline void print(T x) { static int buf[30], cnt; if (x == 0) print('0'); else { if (x < 0) print('-'), x = -x; for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48; while (cnt) print((char)buf[cnt--]); } } inline void flush() { fwrite(obuf, 1, ooh - obuf, stdout); } long long n, l, r, a[233]; long long calc(long long x) { if (x < 2) return 1; return 1 + 2 * calc(x >> 1); } int solve(int s, long long l, long long r) { if ((n >> s) > 1) { int ans = 0; if (l <= a[s + 1]) ans += solve(s + 1, l, min(a[s + 1], r)); if (l <= a[s + 1] + 1 && a[s + 1] + 1 <= r) ans += (n >> s) & 1; if (r > a[s + 1] + 1) ans += solve(s + 1, max(1ll, l - a[s + 1] - 1), r - a[s + 1] - 1); return ans; } else return n >> s; } int main() { read(n), read(l), read(r); for (register int i = (0); i <= (50); ++i) if (n >> i) a[i] = calc(n >> i); else break; printf("%d", solve(0, l, r)); return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; inline int getSecsInYear(int year) { if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) return 60 * 60 * 24 * 366; else return 60 * 60 * 24 * 365; } int lm[12] = {31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline int getSecsInMonth(int year, int month) { if (month == 1) { if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) return 60 * 60 * 24 * 29; else return 60 * 60 * 24 * 28; } else return 60 * 60 * 24 * lm[month]; } inline void incrementDay(int &day, int &date, int &month, int &year, long long &t) { day = (day + 1) % 7; t += 60 * 60 * 24; if (month == 1) { if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) { if (date == 28) { date = 0; month++; } else date++; } else { if (date == 27) { date = 0; month++; } else date++; } } else { if (date + 1 == lm[month]) { date = 0; if (month == 11) { month = 0; year++; } else month++; } else date++; } } inline bool incrementTime(int &s, int &m, int &h, long long &t) { t++; if (s == 59) { s = 0; if (m == 59) { m = 0; if (h == 23) return false; else h++; } else m++; } else s++; return true; } inline bool check(int x, int y) { if (y == -1) return true; return x == y; } inline bool checkDay(int cday, int cdate, int day, int date) { if (day == -1) return check(cdate, date); if (date == -1) return check(cday, day); return check(cdate, date) || check(cday, day); } inline long long solveToday(int cs, int cm, int ch, int cday, int cdate, int cmonth, int cyear, long long ct, int s, int m, int h, int day, int date, int month) { if (!checkDay(cday, cdate, day, date) || !check(cmonth, month)) return -1; while (true) { if (check(cs, s) && check(cm, m) && check(ch, h)) return ct; if (!incrementTime(cs, cm, ch, ct)) break; } return -1; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int s, m, h, day, date, month; cin >> s >> m >> h >> day >> date >> month; if (day != -1) day--; if (date != -1) date--; if (month != -1) month--; int n; cin >> n; for (int q = 0; q < n; q++) { long long t; cin >> t; t++; int cs = 0, cm = 0, ch = 0, cday = 3, cdate = 0, cmonth = 0, cyear = 1970; long long ct = t; while (true) { int sy = getSecsInYear(cyear); if (t >= sy) { t -= sy; cday = (cday + sy / (60 * 60 * 24)) % 7; cyear++; } else break; } while (true) { int sm = getSecsInMonth(cyear, cmonth); if (t >= sm) { t -= sm; cday = (cday + sm / (60 * 60 * 24)) % 7; cmonth++; } else break; } while (t >= 60 * 60 * 24) { t -= 60 * 60 * 24; cday = (cday + 1) % 7; cdate++; } while (t >= 60 * 60) { t -= 60 * 60; ch++; } while (t >= 60) { t -= 60; cm++; } cs += t; long long ans = solveToday(cs, cm, ch, cday, cdate, cmonth, cyear, ct, s, m, h, day, date, month); if (ans != -1) { cout << ans << '\n'; continue; } ct -= cs + 60 * cm + 60 * 60 * ch; while (true) { incrementDay(cday, cdate, cmonth, cyear, ct); ans = solveToday(0, 0, 0, cday, cdate, cmonth, cyear, ct, s, m, h, day, date, month); if (ans != -1) break; } cout << ans << '\n'; } return 0; }
2,800
CPP
n=int(input()) for i in range(n-1): print(['I hate that ','I love that '][i%2],sep='',end='') print(['I love it','I hate it'][n%2])
800
PYTHON3
#include <bits/stdc++.h> using namespace std; long long Solver(int h, long long n) { if (h == 0) return 0; if (n > (1LL << (h - 1))) { return (1LL << h) + Solver(h - 1, n - (1LL << (h - 1))); } else { return (1) + Solver(h - 1, (1LL << (h - 1)) + 1 - n); } } int main() { ios_base::sync_with_stdio(0); long long h, n; cin >> h >> n; cout << Solver(h, n) << endl; }
1,700
CPP
import sys def main(): n = int(sys.stdin.readline().strip()) dirs, dists = [], [] earth_circum = 40000 lat = 0 north = "North" south = "South" for i in range(n): distance, direction = [s for s in sys.stdin.readline().split()] distance = int(distance) if direction == north: distance *= -1 dirs.append(direction) dists.append(distance) for distance, direction in zip(dists, dirs): if distance == 0: continue if lat == 0 and direction != south: print("NO") return if lat == earth_circum / 2 and direction != north: print("NO") return if direction in [south, north]: if abs(lat + distance) > earth_circum / 2: print("NO") return if lat + distance < 0: print("NO") return lat += distance print("YES" if lat == 0 else "NO") if __name__ == '__main__': main()
1,300
PYTHON3
t_count = int(input().strip()) while t_count > 0: n, k = list(map(int, input().strip().split(' '))) if n % 2 == 0: if k % 2 == 0: if n >= 2 * k: rem = n - 2 * (k - 1) print('YES') print('2 ' * (k - 1), rem, sep = '') elif n >= k: rem = n - k + 1 print('YES') print('1 ' * (k - 1), rem, sep = '') else: print('NO') else: if n >= 2 * k: rem = n - 2 * (k - 1) print('YES') print('2 ' * (k - 1), rem, sep = '') else: print('NO') else: if k % 2 == 0: print('NO') elif n >= k: rem = n - k + 1 print('YES') print('1 ' * (k - 1), rem, sep = '') else: print('NO') t_count -= 1
1,200
PYTHON3
n = int(input()) s = n // 2 if n % 2: s = -s - 1 print(s)
800
PYTHON3
t=int(input()) for i in range(t): n=int(input()) a=list(map(int,input().split())) c=0 for j in range(n-1): if a.count(a[j])>2: print('YES') break elif a[j]!=a[j+1] and a[j]!=a[j-1] and a.count(a[j])==2 and j>=1: print('YES') break elif a[j]!=a[j+1] and a.count(a[j])==2 and j==0: print('YES') break else: c+=1 continue if c==n-1: print('NO')
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n, i, ans, cnt, x; int main() { scanf("%I64d", &n); cnt = 1; x = 1; while (i < n) { if (i * 10 + 9 <= n) ans += 9 * x * cnt; else ans += (n - i) * cnt; x *= 10; cnt++; i = i * 10 + 9; } printf("%I64d\n", ans); return 0; }
1,200
CPP
#author: raghav_0901 from math import * mod = (10**9) + 7 def _in(): return [int(x) for x in input().split(' ')] def main(): n = input() ans = [] for i in range(len(n)): if n[i] != '0': ans.append(int(n[i]) * (10**(len(n) - i - 1))) print(len(ans)) for i in ans: print(i) if __name__ == "__main__": for _ in range(int(input())): main()
800
PYTHON3
n = int(input()) a5 = n % 10; n //= 10; a4 = n % 10; n //= 10; a3 = n % 10; n //= 10; a2 = n % 10; a1 = n // 10; k = a1 * 10000 + a3 * 1000 + a5 * 100 + a4 * 10 + a2; k = k * k * k * k * k; x5 = k % 10 k //= 10 x4 = k % 10 k //= 10 x3 = k % 10 k //= 10 x2 = k % 10 k //= 10 x1 = k % 10 print(repr(x1) + repr(x2) + repr(x3) + repr(x4) + repr(x5))
1,400
PYTHON3
n=int(input()) k=0 while(k<n): c=input() if((len(c))<=10): print(c) else: print(c[0]+str(len(c)-2)+c[len(c)-1]) k=k+1
800
PYTHON3
# import sys # sys.stdin=open("input.in","r") s=input() l=set() for i in range(len(s)): l.add(s[i:]+s[:i]) print(len(l))
900
PYTHON3
#include <bits/stdc++.h> using namespace std; long long sum(vector<long long> a) { long long n = a.size(), sol = 0; for (long long i = 1; i < n; ++i) { if (a[i] % 2 != a[i - 1] % 2) ++sol; } return sol; } bool cmp(pair<long long, long long> a, pair<long long, long long> b) { return ((a.second - a.first) < (b.second - b.first)); } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); long long n; cin >> n; if (n == 1) return cout << "0\n", 0; vector<long long> a(n); for (auto& i : a) cin >> i; long long k[2]; k[0] = k[1] = 0; for (long long j = 1; j <= n; ++j) { bool ok = 0; for (auto& i : a) { if (i == j) { ok = 1; break; } } if (!ok) { ++k[j % 2]; } } long long l = INT32_MAX, r = INT32_MIN; for (long long i = 0; i < n; ++i) { if (a[i]) { l = min(l, i); r = max(r, i); } } if (l > r) { return cout << "1\n", 0; } if (!k[0] && !k[1]) { return cout << sum(a) << '\n', 0; } vector<pair<long long, long long> > t; for (long long i = l; i <= r; ++i) { if (a[i]) continue; long long j = i + 1; while (j < n && !a[j]) ++j; if (a[i - 1] % 2 == a[j] % 2) t.push_back({i, j - 1}); i = j; } sort(t.begin(), t.end(), cmp); long long len = t.size(); for (long long e = 0; e < len; ++e) { long long le = t[e].first, re = t[e].second; long long p = (a[le - 1] + 22) % 2; if (k[p] < re - le + 1) { a[le] = (p + 3); k[(p + 3) % 2]--; continue; } for (long long i = le; i <= re; ++i) { a[i] = p; --k[p]; } } if (l > 0) { long long p = a[l] % 2; if (k[p] < l) { a[0] = p + 3; k[(p + 3) % 2]--; } else { for (long long i = 0; i < l; ++i) { a[i] = p; --k[p]; } } } if (r < n - 1) { long long p = a[r] % 2; if (k[p] < n - 1 - r) { a[n - 1] = p + 3; } else { for (long long i = r + 1; i < n; ++i) { a[i] = p; } } } cout << sum(a) << '\n'; }
1,800
CPP
def isAllOnes(st): for ch in st: if ch == "0": return False return True # print ("Input n and d") n,d = (int(j) for j in input().split()) currun = 0 maxrun = 0 for i in range(d): # print("Input the next string") nextone = input() if isAllOnes(nextone): currun = 0 else: currun += 1 if currun > maxrun: maxrun = currun print (maxrun)
800
PYTHON3
s=input() count=0 for i in range(len(s)): if(s[i]=="4" or s[i]=="7"): count=count+1 if(count==4 or count==7): print("YES") else: print("NO")
800
PYTHON3
from math import * n=int(input()) a=list(map(int,input().split())) b=list(map(int,input().split())) s=set() for i in range(1,len(a)): s.add(a[i]) for i in range(1,len(b)): s.add(b[i]) if len(s)==n: print('I become the guy.') else: print('Oh, my keyboard!')
800
PYTHON3
n = int(input()) max, curr = 0, 0 for i in range(n): a, b = [int(elem) for elem in input().split()] curr -=a curr +=b if curr > max: max = curr print(max)
800
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; vector<long long> vis(101, 0); long long l, r; cin >> l >> r; for (long long i = 2; i <= n; i++) { long long a, b; cin >> a >> b; for (long long i = a; i < b; i++) vis[i] = 1; } long long ans = 0; for (long long i = l; i < r; i++) ans += (1 - vis[i]); cout << ans; }
0
CPP
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; long long power(long long a, long long b) { long long ans = 1; while (b > 0) { if (b & 1) ans *= a; a = a * a; b >>= 1; } return ans; } long long powm(long long a, long long b) { a %= MOD; long long ans = 1; while (b > 0) { if (b & 1) ans = (ans * a) % MOD; a = (a * a) % MOD; b >>= 1; } return ans; } int main() { double n, m; cin >> n >> m; double ans = INT_MAX; for (int i = 0; i < n; i++) { double a, b; cin >> a >> b; ans = min(ans, a / b * m); } cout << fixed << setprecision(10) << ans << "\n"; return 0; }
800
CPP
#include <bits/stdc++.h> int findmax(int p, int t); int main() { int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d); if (findmax(a, c) > findmax(b, d)) { printf("Misha"); } else if (findmax(a, c) < findmax(b, d)) { printf("Vasya"); } else { printf("Tie"); } return 0; } int findmax(int p, int t) { int max = -1; if (((3 * p) / 10) > (p - ((p / 250) * t))) { max = ((3 * p) / 10); } else { max = (p - ((p / 250) * t)); } return max; }
900
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 5, maxa = 1e6 + 5; inline int gi() { char c = getchar(); while (c < '0' || c > '9') c = getchar(); int sum = 0; while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar(); return sum; } int n, q, a[maxn], A; pair<int, int> sum[maxa << 2]; int tag[maxa << 2]; pair<int, int> operator+(pair<int, int> a, int b) { a.first += b; return a; } pair<int, int> operator+(pair<int, int> a, pair<int, int> b) { if (a > b) swap(a, b); if (a.first == b.first) a.second += b.second; return a; } void modify1(int s, int l, int r, int ql, int qr, int v) { if (ql <= l && r <= qr) return sum[s].first += v, tag[s] += v, void(); if (ql <= ((l + r) >> 1)) modify1((s << 1), l, ((l + r) >> 1), ql, qr, v); if (qr >= ((l + r) >> 1) + 1) modify1((s << 1 | 1), ((l + r) >> 1) + 1, r, ql, qr, v); sum[s] = sum[(s << 1)] + sum[(s << 1 | 1)] + tag[s]; } void modify2(int s, int l, int r, int p, int v) { if (l == r) return sum[s].second += v, void(); if (p <= ((l + r) >> 1)) modify2((s << 1), l, ((l + r) >> 1), p, v); else modify2((s << 1 | 1), ((l + r) >> 1) + 1, r, p, v); sum[s] = sum[(s << 1)] + sum[(s << 1 | 1)] + tag[s]; } int query(int s, int l, int r, int ql, int qr, int v) { if (ql <= l && r <= qr) return sum[s].first + v == 1 ? sum[s].second : 0; int res = 0; if (ql <= ((l + r) >> 1)) res += query((s << 1), l, ((l + r) >> 1), ql, qr, v + tag[s]); if (qr >= ((l + r) >> 1) + 1) res += query((s << 1 | 1), ((l + r) >> 1) + 1, r, ql, qr, v + tag[s]); return res; } void mdf(int l, int r, int v) { if (l != r) modify1(1, 0, A, min(l, r), max(l, r) - 1, v); } int main() { n = gi(); q = gi(); for (int i = 1; i <= n; ++i) a[i] = gi(); a[0] = A = 1e6 + 1; for (int i = 0; i <= n; ++i) mdf(a[i], a[i + 1], 1), modify2(1, 0, A, a[i], 1); int x, v; while (q--) { x = gi(); v = gi(); mdf(a[x - 1], a[x], -1); mdf(a[x], a[x + 1], -1); modify2(1, 0, A, a[x], -1); a[x] = v; mdf(a[x - 1], a[x], 1); mdf(a[x], a[x + 1], 1); modify2(1, 0, A, a[x], 1); printf("%d\n", query(1, 0, A, 1, A - 1, 0)); } return 0; }
3,300
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 110005; int n, qq, fa[maxn], f[maxn], sz[maxn], up[maxn], edge[maxn], dep[maxn], son[maxn], q[maxn]; int tot = 0, V[maxn << 1], N[maxn << 1], F[maxn], ans[maxn]; struct event { int c, k, a, b; } events[maxn << 1]; struct info { int ls, rs, len, val; info() { ls = 0, rs = 0, len = 0, val = 0; } } tree[maxn << 2]; bool cmp(event x, event y) { if (x.c != y.c) return x.c > y.c; return x.k < y.k; } void add(int a, int b) { ++tot, V[tot] = b, N[tot] = F[a], F[a] = tot; } void decompose() { int hd = 0, tl = 1; memset(son, 0, sizeof son); memset(fa, 0, sizeof fa); memset(dep, 0, sizeof dep); memset(sz, 0, sizeof sz); q[1] = 1, dep[1] = 0, fa[1] = 0; while (hd != tl) { int x = q[++hd]; for (int p = F[x]; p > 0; p = N[p]) if (V[p] != fa[x]) { q[++tl] = V[p]; dep[V[p]] = dep[x] + 1; fa[V[p]] = x; } } for (int i = n, x; i > 0; i--) { sz[x = q[i]]++; if (fa[x]) { sz[fa[x]] += sz[x]; if (!son[fa[x]] || sz[x] > sz[son[fa[x]]]) son[fa[x]] = x; } } int num = 0; for (int i = n, x, y; i > 0; i--) { x = q[i]; if (son[x]) continue; while (fa[x] && son[fa[x]] == x) x = fa[x]; y = x; while (x > 0) up[x] = y, edge[x] = ++num, x = son[x]; } } info rev(info a) { swap(a.ls, a.rs); return a; } info operator+(const info &a, const info &b) { info ret; ret.len = a.len + b.len; ret.ls = a.ls; if (a.ls == a.len) ret.ls = a.len + b.ls; ret.rs = b.rs; if (b.rs == b.len) ret.rs = b.len + a.rs; ret.val = a.val + b.val - f[a.rs] - f[b.ls] + f[a.rs + b.ls]; return ret; } void build(int cur, int l, int r) { tree[cur].len = r - l + 1; tree[cur].val = tree[cur].ls = tree[cur].rs = 0; if (l != r) build((cur << 1), l, ((l + r) >> 1)), build((cur << 1 | 1), ((l + r) >> 1) + 1, r); } void modify(int cur, int l, int r, int pos) { if (l == r) { tree[cur].val = f[1]; tree[cur].ls = tree[cur].rs = 1; return; } if (pos <= ((l + r) >> 1)) modify((cur << 1), l, ((l + r) >> 1), pos); else modify((cur << 1 | 1), ((l + r) >> 1) + 1, r, pos); tree[cur] = tree[(cur << 1)] + tree[(cur << 1 | 1)]; } info query(int cur, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) return tree[cur]; info ret; if (ql <= ((l + r) >> 1)) ret = ret + query((cur << 1), l, ((l + r) >> 1), ql, qr); if (qr > ((l + r) >> 1)) ret = ret + query((cur << 1 | 1), ((l + r) >> 1) + 1, r, ql, qr); return ret; } void upd(int a, int b) { if (dep[a] < dep[b]) swap(a, b); modify(1, 1, n, edge[a]); } int ask(int x, int y) { info vx, vy; while (up[x] != up[y]) { if (dep[up[x]] < dep[up[y]]) swap(x, y), swap(vx, vy); vx = query(1, 1, n, edge[up[x]], edge[x]) + vx; x = fa[up[x]]; } if (dep[x] < dep[y]) swap(x, y), swap(vx, vy); info ans = rev(vy) + query(1, 1, n, edge[y] + 1, edge[x]) + vx; return ans.val; } int main() { scanf("%d%d", &n, &qq); f[0] = f[n] = 0; for (int i = 1; i < n; i++) scanf("%d", &f[i]); int all = 0; for (int i = 1; i < n; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); add(u, v), add(v, u); events[++all] = (event){w, 0, u, v}; } for (int i = 1; i <= qq; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); events[++all] = (event){w, i, u, v}; } sort(events + 1, events + 1 + all, cmp); decompose(); build(1, 1, n); for (int i = 1; i <= all; i++) if (events[i].k == 0) { upd(events[i].a, events[i].b); } else { ans[events[i].k] = ask(events[i].a, events[i].b); } for (int i = 1; i <= qq; i++) printf("%d\n", ans[i]); return 0; }
3,100
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 2e9; const int MOD = 1e9 + 7; vector<long long> vector_input(long long n) { vector<long long> v; for (long long i = 0; i < n; i++) { long long num; cin >> num; v.push_back(num); } return v; } void input() { string s; int q; cin >> s; cin >> q; long long l = s.length(); set<int> s1[26]; for (int i = 0; i < l; i++) { s1[s[i] - 'a'].insert(i); } while (q--) { int type; cin >> type; if (type == 1) { int pos; char c; cin >> pos >> c; s1[s[pos - 1] - 'a'].erase(pos - 1); s[pos - 1] = c; s1[c - 'a'].insert(pos - 1); } else { int l, r; cin >> l >> r; l--; r--; int ans = 0; for (int i = 0; i < 26; i++) { auto it = s1[i].lower_bound(l); if (it == s1[i].end()) continue; if (*it <= r) ans++; } cout << ans << '\n'; } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout.precision(12); input(); }
1,600
CPP
w=input() n=w.lower() l=len(n) for i in range(0,l,1): if n[i]!='a' and n[i]!='e' and n[i]!='i' and n[i]!='o' and n[i]!='u' and n[i]!='y': print('.',n[i],sep='',end='')
1,000
PYTHON3
nm = [int(i) for i in input().split(' ')] n = nm[0] m = nm[1] lists = [int(i) for i in input().split(' ')] lists.sort() result = 0 for j in range(m-n+1): if(j==0): result = lists[j+n-1] - lists[j] elif(lists[j+n-1] - lists[j] <= result): result = lists[j+n-1] - lists[j] print(result)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxN = 5005; int n, a[maxN], res, cnt; int big[maxN][maxN], small[maxN][maxN]; int main() { ios_base::sync_with_stdio(false); res = 1e9; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), ++a[i]; int old = 0; for (int i = 2; i <= n; ++i) for (int j = i - 1; j >= 1; --j) old += int(a[i] < a[j]); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) big[i][j] = big[i - 1][j] + (a[i - 1] > j); for (int i = n; i >= 1; --i) for (int j = 1; j <= n; ++j) small[i][j] = small[i + 1][j] + (a[i + 1] < j); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) if (a[i] >= a[j]) { int v1 = a[i], v2 = a[j]; int sum = old - big[i][v1] - small[i][v1] + 1 + big[j][v1] + small[j][v1] - big[j][v2] - small[j][v2] + big[i][v2] + small[i][v2]; if (sum < res) { res = sum; cnt = 1; } else if (sum == res) ++cnt; } cout << res << " " << cnt; return 0; }
1,900
CPP
n=int(input()) for _ in range(n): a=input() a=list(a) if len(a)>10: b=len(a)-2 b=str(b) a=a[0]+b+a[len(a)-1] print(a) else: print("".join(a))
800
PYTHON3
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("O3") #pragma target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; const long long inf = 0x3f3f3f3f3f3f3f3fll; inline int read() { register int x = 0, dp = 1; register char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') dp = -1; for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); return x * dp; } void write(int x) { if (x < 0) x = -x, putchar('-'); if (x >= 10) write(x / 10); putchar(x % 10 + '0'); } void writeln(int x) { write(x); puts(""); } double E[2][600]; int n, k; int main() { memset(E, 0, sizeof(E)); n = read(), k = read(); int now = 1; for (int i = n - 1; i >= 0; i--) { now ^= 1; for (int j = 1; j < 600; j++) E[now][j] = E[now ^ 1][j] * (j * 1. / (j + 1) / k + (k - 1) * 1. / k) + (j * 1. / 2 + j * 1. / (j + 1)) / k + 1. / (j + 1) * E[now ^ 1][j + 1] / k; } printf("%.9f\n", k * E[now][1]); return 0; }
2,700
CPP
#include <bits/stdc++.h> using namespace std; int n, k; vector<pair<int, int> > e[200011]; vector<int> ans; int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> k; int li, ri; for (int i = (0); i < (n); ++i) { cin >> li >> ri; --li, --ri; e[li].emplace_back(-ri, i); } set<pair<int, int> > st; for (int i = (0); i < (200005); ++i) { for (auto p : e[i]) st.insert(p); while (st.size() > k) { ans.emplace_back(st.begin()->second); st.erase(st.begin()); } auto it = st.lower_bound({-i, -1}); while (!st.empty() && it->first == -i) { it = st.erase(it); } } cout << ans.size() << endl; for (auto i : ans) cout << i + 1 << " "; cout << endl; return 0; }
1,800
CPP
import sys def fastio(): from io import StringIO from atexit import register global input sys.stdin = StringIO(sys.stdin.read()) input = lambda : sys.stdin.readline().rstrip('\r\n') sys.stdout = StringIO() register(lambda : sys.__stdout__.write(sys.stdout.getvalue())) fastio() INF = 10**20 MOD = 10**9 + 7 I = lambda:list(map(int,input().split())) from math import gcd from math import ceil from collections import defaultdict as dd, Counter from bisect import bisect_left as bl, bisect_right as br Q, = I() while Q: Q -= 1 s = input() t = input() n, m = len(s), len(t) s_set = set(s) t_set = set(t) for i in t_set: if i not in s_set: print(-1) break else: ans = 0 idx = [[] for i in range(26)] for i in range(n): idx[ord(s[i]) - 97].append(i) cur = -1 ok = 0 for i in range(m): x = ord(t[i]) - 97 k = br(idx[x], cur) if k == len(idx[x]): ans += 1 cur = idx[x][0] else: cur = idx[x][k] ans += 1 print(ans)
1,600
PYTHON3
/* * @Author: zhl * @LastEditTime: 2021-02-08 21:20:19 */ /* Author: zhl * Time: 2021-02-07 22:35:03 **/ #include<bits/stdc++.h> using namespace std; const int N = 2e5 + 10; typedef long long ll; int n, A[N], vis[N], B[N], pre[N], f[N], mx[N]; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> A[i]; } int cnt = 0; int ppre = 0; int ans = n; for (int i = 1; i <= n; i++) { if (A[i] == ppre) { vis[cnt] = 1; ans--; } else { B[++cnt] = A[i]; } ppre = A[i]; } // for(int i = 1;i <= cnt;i++){ // cout << B[i] << " " << vis[i] << endl; // } for (int i = 1;i <= cnt;i++) { if (pre[B[i]]) { int p = pre[B[i]]; f[i] = max(f[i],mx[p + 1] + 1); } pre[B[i]] = i; mx[i] = max(mx[i-1],f[i]); } ans -= mx[cnt]; cout << ans << endl; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 1; const long long INF = 1e9 + 7; const long long inf = 1e18; vector<long long> adj[N]; long long vis[N]; long long ans[N]; long long ct; void dfs(long long node) { vis[node] = ct; ans[ct]++; for (auto it : adj[node]) { if (!vis[it]) { dfs(it); } } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m; cin >> n >> m; for (long long i = 0; i < m; i++) { long long x; cin >> x; long long count = 0, temp; while (x--) { long long a; cin >> a; if (count == 0) { temp = a; } if (count > 0) { adj[temp].push_back(a); adj[a].push_back(temp); temp = a; } count++; } count = 0; } for (long long i = 1; i <= n; i++) { if (!vis[i]) { ct++; dfs(i); } cout << ans[vis[i]] << " "; } }
1,400
CPP
n, m=map(int, input().split()) i=0 while n!=0 and m!=0: n-=1 m-=1 i+=1 if i%2: print("Akshat") else: print("Malvika")
900
PYTHON3
#include <bits/stdc++.h> using namespace std; using LL = long long; const int B = 8, M = 1 << B; int ord[6][3] = {{0, 1, 2}, {0, 2, 1}, {1, 0, 2}, {1, 2, 0}, {2, 0, 1}, {2, 1, 0}}; struct P { int x, y, z; bool operator==(const P &rhs) const { return x == rhs.x && y == rhs.y && z == rhs.z; } P operator-(const P &rhs) { return P{x - rhs.x, y - rhs.y, z - rhs.z}; } } ps[8], pa[8], p[8]; P perm(const P &p, int id) { int c[3] = {p.x, p.y, p.z}; return {c[ord[id][0]], c[ord[id][1]], c[ord[id][2]]}; } LL inner_product(const P &p1, const P &p2) { return (LL)p1.x * p2.x + (LL)p1.y * p2.y + (LL)p1.z * p2.z; } bool found = false; void solve(int dep) { if (found) return; if (dep == 8) { LL l = 1ll << 60; for (int i = 0; i < 8; i++) for (int j = 0; j < i; j++) l = min(l, inner_product(p[i] - p[j], p[i] - p[j])); if (l == 0) return; int count[5] = {0}; for (int i = 0; i < 8; i++) for (int j = 0; j < i; j++) { LL d = inner_product(p[i] - p[j], p[i] - p[j]); if (d % l != 0) break; d /= l; if (1 <= d && d <= 3) count[d]++; } if (count[1] == 12 && count[2] == 12 && count[3] == 4) { memcpy(pa, p, sizeof(p)); found = true; } return; } for (int i = 0; i < 6; i++) { p[dep] = perm(ps[dep], i); solve(dep + 1); } } int main() { for (int i = 0; i < B; i++) scanf("%d%d%d", &ps[i].x, &ps[i].y, &ps[i].z); p[0] = ps[0]; solve(1); if (!found) puts("NO"); else { puts("YES"); for (int i = 0; i < 8; i++) { printf("%d %d %d\n", pa[i].x, pa[i].y, pa[i].z); } } }
2,000
CPP
l=input().split() a=int(l[0]) b=int(l[1]) print(min(a,b),end=' ') print((max(a,b)-min(a,b))//2)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class _T_> void read(_T_& d) { d = 0; int f = 1; char c = getchar(); for (; c < '0' || c > '9'; c = getchar()) if (c == '-') f *= -1; for (; c >= '0' && c <= '9'; c = getchar()) d = d * 10 + c - '0'; d *= f; } const double eps = 1e-9; struct Node { double x, y; } p[2205], rt, P; Node operator+(Node A, Node B) { return (Node){A.x + B.x, A.y + B.y}; } Node operator-(Node A, Node B) { return (Node){A.x - B.x, A.y - B.y}; } Node operator*(Node A, double B) { return (Node){A.x * B, A.y * B}; } Node operator/(Node A, double B) { return (Node){A.x / B, A.y / B}; } double operator*(Node A, Node B) { return A.x * B.x + A.y * B.y; } bool operator==(Node A, Node B) { return A.x == B.x && A.y == B.y; } int res, n, a; pair<double, int> PP[2205]; bool Doit(int x) { P = (p[a] + p[x]) * .5; Node var = rt - P; var = (Node){var.y, -var.x}; for (int i = 1; i <= n; ++i) { PP[i] = make_pair(var * p[i], i); } sort(PP + 1, PP + n + 1); int G = 0; for (int i = 1; i <= n; ++i) { if (PP[i].second == x) G += i; if (PP[i].second == a) G += i; } if (G != n + 1) return 0; for (int i = 1; i <= n; ++i) { if (abs(PP[i].first + PP[n - i + 1].first - PP[1].first - PP[n].first) > eps) return 0; } return 1; } int main() { read(n); res = 0; for (int i = 1; i <= n; ++i) { scanf("%lf%lf", &p[i].x, &p[i].y); rt = rt + p[i]; } rt = rt / n; for (int i = 1; i <= n; ++i) { int A = 1; for (int j = 1; j <= n; ++j) if (p[i] + p[j] == rt + rt) A = 0; if (A) { a = i; goto Turtle; } } return puts("-1"), 0; Turtle: for (int i = 1; i <= n; ++i) if (Doit(i)) ++res; printf("%d\n", res); return 0; }
2,900
CPP