solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
t=int(input()) for i in range(t): x,n,m=[int(x) for x in input().split()] while(n!=0 and x>20): x= int(x/2)+10 n-=1 while(m!=0 and x>0): x-=10 m-=1 if x<=0: print("YES") else: print("NO")
900
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; const double Exp = 1e-9; int sgn(double x) { if (fabs(x) < Exp) return 0; return x > 0 ? 1 : -1; } struct Point { double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} friend Point operator+(const Point &a, const Point &b) { return Point(a.x + b.x, a.y + b.y); } friend Point operator-(const Point &a, const Point &b) { return Point(a.x - b.x, a.y - b.y); } friend Point operator*(const Point &a, double b) { return Point(a.x * b, a.y * b); } friend Point operator/(const Point &a, double b) { return Point(a.x / b, a.y / b); } Point rotate(double del) { return Point(x * cos(del) - y * sin(del), x * sin(del) + y * cos(del)); } } s, t, island[40]; vector<Point> vec; double Dot(const Point &a, const Point &b) { return a.x * b.x + a.y * b.y; } double Cross(const Point &a, const Point &b) { return a.x * b.y - a.y * b.x; } double Length(const Point &a) { return sqrt(Dot(a, a)); } struct Line { Point p, v; Line(Point p, Point v) : p(p), v(v) {} Point get_point(double t) { return p + v * t; } }; Point get_line_intersection(const Line &l1, const Line &l2) { Point a = l1.p, b = l1.p + l1.v; Point c = l2.p, d = l2.p + l2.v; double u = Cross(d - a, c - a), v = Cross(c - b, d - b); double t = u / (u + v); return a + (b - a) * t; } int if_point_on_line(const Point &p, const Point &st, const Point &ed) { return sgn(Cross(p - st, ed - st)) == 0; } int if_point_on_ray(const Point &p, const Point &st, const Point &ed) { if (sgn(Cross(p - st, ed - st)) != 0) return 0; if (sgn(Dot(p - st, ed - st)) < 0) return 0; return 1; } int if_point_on_segment(const Point &p, const Point &st, const Point &ed) { return if_point_on_ray(p, st, ed) && if_point_on_ray(p, ed, st); } int main() { cin >> s.x >> s.y >> t.x >> t.y; cin >> n; for (int i = 0; i < n; i++) { cin >> island[i].x >> island[i].y; } double sum = 0; int flag = 0; int cnt = 0; for (int i = 0; i < n; i++) { int l = i, r = i + 1; r %= n; sum += Length(island[r] - island[l]); } for (int i = 0; i < n; i++) { int l = i, r = i + 1; r %= n; if (flag == 1 && cnt < 2) vec.push_back(island[l]); Point now; if (sgn(Cross(s - t, island[r] - island[l])) != 0) { now = get_line_intersection(Line(s, t - s), Line(island[r], island[l] - island[r])); if (if_point_on_segment(now, island[l], island[r])) { if ((vec.size() > 0 && sgn(Length(vec[vec.size() - 1] - now)) != 0) || vec.size() == 0) { if (if_point_on_segment(now, s, t)) { cnt++; flag = 1; vec.push_back(now); if (cnt == 2) break; } } } } else if (if_point_on_line(island[l], s, t)) { flag = 2; break; } } if (flag == 2 || cnt == 0) { double ans = Length(s - t); printf("%.9lf\n", ans); return 0; } double edge_len = 0; for (int i = 1; i < vec.size(); i++) { edge_len += Length(vec[i] - vec[i - 1]); } edge_len = min(edge_len, sum - edge_len); double s_len = min(Length(s - vec[0]), Length(s - vec[vec.size() - 1])); double t_len = min(Length(t - vec[0]), Length(t - vec[vec.size() - 1])); double ans1 = edge_len + s_len + t_len; double rest_len = Length(s - t); rest_len -= s_len + t_len; double ans2 = s_len + t_len + 2 * rest_len; double ans = min(ans1, ans2); printf("%.9lf\n", ans); return 0; }
2,400
CPP
#include <bits/stdc++.h> using namespace std; const long long hmod = 1791791791; const long long hp = 179; vector<long long> hppows = {1}; long long hppow(int i) { while ((int)hppows.size() < i + 1) { hppows.push_back((hppows.back() * hp) % hmod); } return hppows[i]; } struct hashed_string { string s; vector<long long> h; hashed_string() {} hashed_string(string _s) { s = _s; h.resize(s.size() + 1); for (int i = 0; i < (int)s.size(); i++) { h[i + 1] = (h[i] * hp + (s[i] - 'a' + 1)) % hmod; } } long long substr_hash(int l, int r) { return ((((h[r] - h[l] * hppow(r - l)) % (hmod)) + (hmod)) % (hmod)); } }; int main() { int n, m; cin >> n >> m; string s; cin >> s; hashed_string hs[26]; for (int c = 0; c < (26); c++) { string t; for (int i = 0; i < (n); i++) { t.push_back('a' + (s[i] - 'a' == c)); } hs[c] = hashed_string(t); } for (int i = 0; i < m; i++) { int l, r, len; cin >> l >> r >> len; l--; r--; vector<long long> h1, h2; for (int c = 0; c < (26); c++) { h1.push_back(hs[c].substr_hash(l, l + len)); h2.push_back(hs[c].substr_hash(r, r + len)); } sort(h1.begin(), h1.end()); sort(h2.begin(), h2.end()); if (h1 == h2) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
2,300
CPP
#include <bits/stdc++.h> using namespace std; int N, K; const int NMAX = 100000 + 5; int v[NMAX]; int aib[NMAX]; inline int lsb(int node) { return node & (-node); } void init() { for (int i = 1; i <= N; ++i) { aib[i] = 1; for (int j = i - lsb(i) + 1; j <= i; ++j) aib[i] = (1LL * aib[i] * v[j]) % K; } } int query(int l, int r) { int ans = 1; while (r >= l) { if (r - lsb(r) + 1 >= l) { ans = (1LL * ans * aib[r]) % K; r -= lsb(r); } else { ans = (1LL * ans * v[r]) % K; r--; } } return ans; } int main() { ios_base ::sync_with_stdio(false); cin.tie(0); cin >> N >> K; for (int i = 1; i <= N; ++i) cin >> v[i]; init(); long long int ans = 0; int l = 0; for (int i = 1; i <= N; ++i) { while (l + 1 <= i && query(l + 1, i) == 0) ++l; ans += l; } cout << ans << '\n'; return 0; }
1,900
CPP
from collections import deque m = int(input()) b = [int(i) for i in input().split()] b.sort(reverse = True) ans = deque() ans.append(b[0]) for i in b[1:]: if i < ans[-1]: ans.append(i) elif i < ans[0]: ans.appendleft(i) print(len(ans)) print(' '.join(map(str, ans)))
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m, lognm; vector<string> c, s; vector<vector<int>> used, nxt; void getnext(int x, int y, int &nx, int &ny) { if (s[x][y] == 'U') nx = x - 1, ny = y; if (s[x][y] == 'R') nx = x, ny = y + 1; if (s[x][y] == 'D') nx = x + 1, ny = y; if (s[x][y] == 'L') nx = x, ny = y - 1; } void dfs(int x, int y) { used[x][y] = 1; int nx = -1, ny = -1; getnext(x, y, nx, ny); assert(0 <= nx && nx < n && 0 <= ny && ny < m); int v = x * m + y, to = nx * m + ny; if (!used[nx][ny]) dfs(nx, ny); nxt[v][0] = to; } int main() { int t; cin >> t; while (t--) { cin >> n >> m; lognm = 0; int nm = n * m; while ((1 << lognm) <= nm) ++lognm; c = s = vector<string>(n); for (auto &it : c) cin >> it; for (auto &it : s) cin >> it; used = vector<vector<int>>(n, vector<int>(m)); nxt = vector<vector<int>>(n * m, vector<int>(lognm)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (!used[i][j]) dfs(i, j); } } for (int deg = 1; deg < lognm; ++deg) { for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { int id = i * m + j; nxt[id][deg] = nxt[nxt[id][deg - 1]][deg - 1]; } } } vector<vector<int>> black, white; black = white = vector<vector<int>>(n, vector<int>(m)); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { int v = i * m + j, to = v; for (int deg = lognm - 1; deg >= 0; --deg) { if ((nm >> deg) & 1) to = nxt[to][deg]; } if (c[i][j] == '0') ++black[to / m][to % m]; else ++white[to / m][to % m]; } } int all = 0, good = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (black[i][j]) { ++all; ++good; } else if (white[i][j]) { ++all; } } } cout << all << " " << good << endl; } return 0; }
2,200
CPP
if __name__ == '__main__': n,m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) for i in a: if i in b: print(i,end = " ") print()
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, u, v; long long c[1000006] = {}, z = 0; cin >> n >> m; while (m--) cin >> u >> v, c[u]++, c[v]++; for (int i = 1; i <= n; i++) z += c[i] * (n - c[i] - 1); cout << (long long)n * (n - 1) * (n - 2) / 6 - z / 2; }
1,900
CPP
t=int(input()) for e in range(t): a,b=map(int,input().split()) if a%b==0: print(0) else: print(b-a%b)
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; bool mario(string a, string b) { if (a.size() != b.size()) return a.size() < b.size(); for (int i = 0; i < a.size(); i++) { if (a[i] != b[i]) return a[i] < b[i]; } return true; } int main() { string b = "127"; string s = "32767"; string i = "2147483647"; string l = "9223372036854775807"; string n; cin >> n; if (mario(n, b)) { cout << "byte" << endl; } else if (mario(n, s)) { cout << "short" << endl; } else if (mario(n, i)) { cout << "int" << endl; } else if (mario(n, l)) { cout << "long" << endl; } else { cout << "BigInteger" << endl; } return 0; }
1,300
CPP
t=int(input()) q=t c=0 for i in range(q): s=[int(i) for i in input().split()][:3] if(s.count(1)>=2): c+=1 print(c)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m, k; short field[550][550]; int dx[4] = {0, 1, 0, -1}; int dy[4] = {1, 0, -1, 0}; bool onf(int i, int j) { return (i >= 1 && i <= n && j >= 1 && j <= m); } void setwall(int y, int x) { k--; field[y][x] = 0; for (int i = 0; i < 4; i++) { if (field[y + dy[i]][x + dx[i]] == 2 && k > 0) setwall(y + dy[i], x + dx[i]); } } int main() { scanf("%d %d %d", &n, &m, &k); int kol = 0; char c = getchar(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { c = getchar(); if (c == '.') { field[i][j] = 2; kol++; } if (c == '#') field[i][j] = 1; } c = getchar(); } k = kol - k; for (int i = 1; i <= n && k; i++) { for (int j = 1; j <= m && k; j++) { if (field[i][j] == 2 && k > 0) setwall(i, j); } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (field[i][j] == 1) printf("#"); if (field[i][j] == 0) printf("."); if (field[i][j] == 2) printf("X"); } printf("\n"); } return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int n, fa, h[N]; vector<int> G[N], p, q; int main() { scanf("%d", &n); for (int i = (1); i <= (n - 1); ++i) scanf("%d", &fa), G[fa].push_back(i); function<void(int)> dfs = [&](int u) { for (int v : G[u]) dfs(v), h[u] = max(h[u], h[v] + 1); }; dfs(0); function<int(int, int)> solve = [&](int u, int nd) { sort(G[u].begin(), G[u].end(), [&](int x, int y) { return h[x] < h[y]; }); p.push_back(u); for (int i = (1); i <= (nd); ++i) q.push_back(u); nd = 0; for (int v : G[u]) nd = solve(v, nd); return nd + 1; }; solve(0, 0); for (int x : p) printf("%d ", x); printf("\n%d\n", q.size()); for (int x : q) printf("%d ", x); return 0; }
2,500
CPP
#include <bits/stdc++.h> using namespace std; void files(); int N; pair<int, pair<int, int> > V[2 * 100000 + 5]; char S[3][100000 + 5]; int main() { int i, k = 0, a, b, c; scanf("%d", &N); for (i = 1; i <= N; i++) { scanf("%d%d", &a, &b); V[++k] = make_pair(a, make_pair(i, 1)); V[++k] = make_pair(b, make_pair(i, 2)); S[1][i] = S[2][i] = '0'; } sort(V + 1, V + k + 1); for (i = 1; i <= N; i++) { a = V[i].first; b = V[i].second.first; c = V[i].second.second; S[c][b] = '1'; S[1][i / 2] = '1'; S[2][i / 2] = '1'; } printf("%s\n%s\n", S[1] + 1, S[2] + 1); return 0; } void files() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); }
1,300
CPP
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const int maxn = 5 * 100000; pair<pair<int, int>, int> inv[maxn]; int a[maxn], n, m; long long s[maxn], f[maxn]; struct Node { int l, r; long long maxi, cover; } tree[maxn * 4]; const int root = 1; void buildTree(int l, int r, int p) { tree[p].l = l; tree[p].r = r; tree[p].cover = tree[p].maxi = 0; int mid = (l + r) >> 1; if (l < r) { buildTree(l, mid, p * 2), buildTree(mid + 1, r, p * 2 + 1); } } void down(int p) { tree[p * 2].cover += tree[p].cover; tree[p * 2 + 1].cover += tree[p].cover; tree[p * 2].maxi += tree[p].cover; tree[p * 2 + 1].maxi += tree[p].cover; tree[p].cover = 0; } void up(int p) { tree[p].maxi = max(tree[p * 2].maxi, tree[p * 2 + 1].maxi); } void add(int l, int r, int p, long long delta) { if (l <= tree[p].l && r >= tree[p].r) { tree[p].cover += delta; tree[p].maxi += delta; return; } down(p); int mid = (tree[p].l + tree[p].r) >> 1; if (l <= mid) add(l, r, p * 2, delta); if (r > mid) add(l, r, p * 2 + 1, delta); up(p); } int main() { cin >> n >> m; s[0] = f[0] = 0; for (int i = 0; i < n; i++) cin >> s[i + 1]; for (int i = 0; i < n; i++) s[i + 1] += s[i]; for (int i = 0; i < m; i++) cin >> inv[i].first.second >> inv[i].first.first >> inv[i].second; sort(inv, inv + m); int now = 0; buildTree(0, n + 10, root); long long ans = 0; for (int i = 1; i <= n; ++i) { while (now < m && inv[now].first.first <= i) { add(0, --inv[now].first.second, root, inv[now].second); ++now; } f[i] = max(f[i - 1], tree[root].maxi - s[i]); ans = max(ans, f[i]); add(i, i, root, f[i] + s[i]); } cout << ans << endl; return 0; }
2,400
CPP
#include <bits/stdc++.h> using namespace std; vector<int> graph[101]; int n, m; int ans = 0; vector<int> temp; void remove_(int x) { int child = graph[x][0]; for (int i = 0; i < graph[child].size(); i++) { if (graph[child][i] == x) { graph[child].erase(graph[child].begin() + i); break; } } graph[x].clear(); } int main() { cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; graph[x].push_back(y); graph[y].push_back(x); } bool finished = true; ans = -1; while (finished) { finished = false; ans++; for (int i = 1; i <= n; i++) { int length = graph[i].size(); if (length == 1) { finished = true; temp.push_back(i); } } if (finished) { for (int i = 0; i < temp.size(); i++) { remove_(temp[i]); } } } cout << ans; return 0; }
1,200
CPP
#include <bits/stdc++.h> using namespace std; int n; char ss[100005]; bool check() { for (int i = 0; i < n; i++) { if (ss[i] == '0') { return 0; } } return 1; } int main() { scanf("%s", ss); n = strlen(ss); if (check()) { for (int i = 0; i < n - 1; i++) { putchar(ss[i]); } puts(""); } else { bool flag = 0; for (int i = 0; i < n; i++) { if (ss[i] == '0' && !flag) { flag = 1; continue; } putchar(ss[i]); } puts(""); } return 0; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; void gameend(int n) { if (n == 0) cout << "once again"; if (n == 1) cout << "tokitsukaze"; if (n == 2) cout << "quailty"; exit(0); } int main() { ios::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; string s; cin >> s; int pre = s[0] - '0', suf = s[n - 1] - '0', prel = 0, sufl = 0; for (int i = 0; i < n; i++) { if (s[i] - '0' != pre) break; prel++; } for (int i = n - 1; i >= 0; i--) { if (s[i] - '0' != suf) break; sufl++; } int L = n - prel - sufl; int a = max(prel, sufl), b = min(prel, sufl); if (pre == suf && L <= k) gameend(1); if (pre != suf && L + b <= k) gameend(1); if (pre == suf) gameend(0); if (a >= k || k <= L - 1) gameend(0); if (k <= L + a - 2) gameend(0); gameend(2); }
2,300
CPP
#include <bits/stdc++.h> using namespace std; const int iinf = 0x7fffffff; const long long linf = ~(1LL << 63); template <typename T> inline T gcd(T a, T b) { if (a < b) return gcd(b, a); if (b == 0) return a; return gcd(b, a % b); } template <typename C> inline void sort(C& v) { sort(v.begin(), v.end()); } inline void println(int u) { printf("%d\n", u); } inline void printsp(int u) { printf("%d ", u); } const int maxn = 100050; int d[maxn]; vector<int> E[maxn]; int order[maxn]; vector<int> ord; vector<int> ord2; bool vis[maxn]; void dfs2(int x) { vis[x] = true; for (int i = E[x].size() - 1; i >= 0; i--) { int to = E[x][i]; if (!vis[to]) dfs2(to); } ord2.push_back(x); } void dfs(int x) { vis[x] = true; for (int i = 0; i < E[x].size(); i++) { int to = E[x][i]; if (!vis[to]) dfs(to); } ord.push_back(x); } bool topological_sort(int x) { memset(vis, 0, sizeof vis); dfs(x); memset(vis, 0, sizeof vis); dfs2(x); for (int i = 0; i < ord.size(); i++) { if (ord[i] != ord2[i]) return false; order[ord[i]] = i; } return true; } int edu[maxn], edv[maxn]; int main() { ios::sync_with_stdio(0); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; E[u].push_back(v); edu[i] = u; edv[i] = v; d[v]++; } int u, cnt = 0; for (int i = 1; i <= n; i++) if (d[i] == 0) { u = i; cnt++; } bool can = true; if (cnt != 1) can = false; else { can = topological_sort(u); } if (!can) { cout << -1 << endl; } else { int ans = m; for (int i = ans - 1; i >= 0; i--) { if (abs(order[edu[i]] - order[edv[i]]) == 1) { cout << i + 1 << endl; break; } } } return 0; }
1,800
CPP
def solve(n): t, f, s = n // 3, 0, 0 if n % (3 * t) == 1: if t < 2: return -1, -1, -1 else: return t - 2, 0, 1 elif n % (3 * t) == 2: if t < 1: return -1, -1, -1 else: return t - 1, 1, 0 else: return t, 0, 0 t = int(input()) for i in range(t): n = int(input()) if n in [1, 2, 4]: print(-1) else: k,m,n = solve(n) if k == -1: print(-1) else: print(f"{k} {m} {n}")
900
PYTHON3
def f(graf): for i in graf: if i > 0: return False return True n = int(input()) graf = [[]] for i in range(n): graf.append([]) maspred = [0] for i in range(1, n + 1): p, c = map(int, input().split()) maspred.append(p) if c == 0: c = 1 else: c = -1 if p == -1: continue graf[p].append(i * c) graf[i].append(p * c) ch = 0 for i in range(1, len(graf)): if f(graf[i]) and maspred[i] != -1: print(i) ch += 1 if ch == 0: print(-1)
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; int dp[1 << 20][20], n, m, a[20][10005], tp1[20][20], tp2[20][20]; int calc(int mask, int v) { if (dp[mask][v] != -1) return dp[mask][v]; dp[mask][v] = 0; for (int u = 0; u < n; u++) if (v != u && ((mask >> u) & 1)) dp[mask][v] = max(dp[mask][v], min(tp1[u][v], calc(mask ^ (1 << v), u))); return dp[mask][v]; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) scanf("%d", &a[i][j]); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { int val = 1000000000; for (int k = 0; k < m; k++) val = min(val, abs(a[i][k] - a[j][k])); tp1[i][j] = val; val = 1000000000; for (int k = 0; k < m - 1; k++) val = min(val, abs(a[i][k] - a[j][k + 1])); tp2[i][j] = val; } int ans = 0; for (int i = 0; i < n; i++) { memset(dp, -1, sizeof(dp)); for (int j = 0; j < n; j++) dp[1 << j][j] = (j == i ? 1000000000 : 0); for (int j = 0; j < n; j++) ans = max(ans, min(tp2[j][i], calc((1 << n) - 1, j))); } printf("%d", ans); return 0; }
2,000
CPP
from collections import Counter print(4-len(Counter(input().split())))
800
PYTHON3
s = input() l = len(s) t = '' if len(s) > 1: if s[0].islower(): for i in range(1,l): f = 0 if s[i].isupper(): pass else: f = 1 print(s) break if f == 0: t += s[0].upper() for i in range(1,l): t += s[i].lower() print(t) elif s[0].isupper(): f = 0 for i in range(1,l): if s[i].isupper(): t += s[i-1].lower() else: f = 1 print(s) break t += s[l-1].lower() if f == 0: print(t) else: print(s) else: if s.isupper(): print(s.lower()) else: print(s.upper())
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int boys[367] = {0}; int girls[367] = {0}; while (n--) { char g; int l, r; cin >> g >> l >> r; int *ref; if (g == 'M') { ref = boys; } else { ref = girls; } ref[l] += 1; ref[r + 1] -= 1; } int sumb = 0, sumg = 0; for (int i = 1; i <= 366; ++i) { sumb += boys[i]; sumg += girls[i]; boys[i] = sumb; girls[i] = sumg; } int res = 0; for (int i = 1; i <= 366; i++) { res = max(res, 2 * min(boys[i], girls[i])); } cout << res << '\n'; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T &x) { x = 0; char ch = getchar(); bool f = false; while (!isdigit(ch)) { if (ch == '-') { f = true; } ch = getchar(); } while (isdigit(ch)) { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } x = f ? -x : x; return; } template <typename T> inline void write(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 ^ 48); return; } const int N = 2e5 + 5, M = 4e5 + 5; long long ans; int n, m, qq; int head[N], nex[M], to[M], num[M], idx = 1; long long val[M]; long long disS[N], disT[N], minn[N << 4]; int from[M], arr[M]; long long len[M]; int las[N], ind[M], cnt, l[N], r[N]; void add(int id, int u, int v, long long w) { nex[++idx] = head[u]; num[idx] = id; to[idx] = v; val[idx] = w; head[u] = idx; return; } priority_queue<pair<long long, int>, vector<pair<long long, int> >, greater<pair<long long, int> > > q; bool vis[N], path[N]; void dijkstra(int s, long long dis[], int f = 0) { for (int i = 1; i <= n; i++) dis[i] = 1e18; dis[s] = 0; q.push({0, s}); while (!q.empty()) { pair<long long, int> t = q.top(); q.pop(); int x = t.second; long long dist = t.first; if (dist > dis[x]) continue; for (int i = head[x]; i; i = nex[i]) { int y = to[i]; if (dist + val[i] < dis[y]) { dis[y] = dist + val[i]; las[y] = num[i]; if (f == 1 && !path[y]) l[y] = l[x]; if (f == 2 && !path[y]) r[y] = r[x]; q.push({dis[y], y}); } } } return; } void Trace() { memset(ind, -1, sizeof(ind)); path[1] = true; l[1] = r[1] = 0; int cur = 1; for (int i = 1; cur != n; i++) { int id = las[cur]; ind[id] = i; cnt++; cur = from[id] ^ arr[id] ^ cur; path[cur] = true; l[cur] = r[cur] = i; } return; } void build(int x, int l, int r) { minn[x] = 1e18; if (l == r) return; int mid = (l + r) >> 1; build(x << 1, l, mid); build(x << 1 | 1, mid + 1, r); return; } void modify(int x, int l, int r, int ql, int qr, long long v) { if (ql > qr) return; if (ql <= l && r <= qr) { minn[x] = min(minn[x], v); return; } int mid = (l + r) >> 1; if (ql <= mid) modify(x << 1, l, mid, ql, qr, v); if (mid < qr) modify(x << 1 | 1, mid + 1, r, ql, qr, v); } long long query(int x, int l, int r, int v) { if (l == r) return minn[x]; int mid = (l + r) >> 1; long long res = minn[x]; if (v <= mid) res = min(res, query(x << 1, l, mid, v)); else res = min(res, query(x << 1 | 1, mid + 1, r, v)); return res; } int main() { read(n), read(m), read(qq); for (int i = 1; i <= m; i++) { read(from[i]), read(arr[i]), read(len[i]); add(i, from[i], arr[i], len[i]); add(i, arr[i], from[i], len[i]); } dijkstra(n, disT); Trace(); dijkstra(1, disS, 1); dijkstra(n, disT, 2); build(1, 1, cnt); for (int i = 1; i <= m; i++) { if (ind[i] == -1) { modify(1, 1, cnt, l[from[i]] + 1, r[arr[i]], disS[from[i]] + len[i] + disT[arr[i]]); modify(1, 1, cnt, l[arr[i]] + 1, r[from[i]], disS[arr[i]] + len[i] + disT[from[i]]); } } for (int i = 1; i <= qq; i++) { int id; long long lenth; ans = disS[n]; read(id), read(lenth); if (ind[id] != -1) { ans = disS[n] - len[id] + lenth; if (lenth > len[id]) ans = min(ans, query(1, 1, cnt, ind[id])); } else { if (lenth < len[id]) { ans = min(ans, disS[from[id]] + disT[arr[id]] + lenth); ans = min(ans, disS[arr[id]] + disT[from[id]] + lenth); } } write(ans); putchar('\n'); } return 0; }
3,000
CPP
n=int(input()) a=[] for _ in range(n): a.append(int(input())) a.sort() c0=a.count(a[0]) c1=a.count(a[n-1]) if c0==c1 and c0+c1==n: print('YES') print(' '.join(map(str,[a[0],a[n-1]]))) else: print('NO')
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int N, M; cin >> N >> M; char bo[501][501]; int xa = -1; int ya = -1; int xb = -1; int yb = -1; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { cin >> bo[i][j]; if (xa == -1 && bo[i][j] == 'X') { xa = i; ya = j; } } } bool flag = true; for (int k = xa; k < N; k++) { if (bo[k][ya] == 'X') { xb = k; } } for (int l = ya; l < M; l++) { if (bo[xa][l] == 'X') { yb = l; } } for (int i = xa; i <= xb; i++) { for (int j = ya; j <= yb; j++) { if (bo[i][j] != 'X') { flag = false; break; } else { bo[i][j] = '.'; } } } if (!flag) { cout << "NO" << endl; return 0; } for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (bo[i][j] == 'X') { flag = false; break; } } } if (flag) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; }
1,400
CPP
read = lambda: map(int, input().split()) n, a = read() t = list(read()) ans = 0 for d in range(n): i, j = a - d - 1, a + d - 1 if i == j: ans += t[i] elif i >= 0 and j < n and t[i] == t[j]: ans += t[i] + t[j] elif i < 0 and j < n: ans += t[j] elif i >= 0 and j >= n: ans += t[i] print(ans)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; inline int read() { int sum = 0, ff = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') ff = -1; ch = getchar(); } while (isdigit(ch)) sum = sum * 10 + (ch ^ 48), ch = getchar(); return sum * ff; } const int mo = 1e9 + 7; long long n, m, k, p, cnt, fl; long long ans; int mp[1005][1005]; long long q_pow(long long x, long long y) { long long res = 1; while (y) { if (y & 1) res = res * x % p; y >>= 1; x = x * x % p; } return res; } int main() { n = read(), m = read(), k = read(); cnt = (n - 1) * (m - 1) - k; for (int i = 1; i <= k; i++) { int x = read(), y = read(), z = read(); if (n < m) swap(x, y); mp[x][y] = z; } if (n < m) swap(n, m); p = read(); if (n % 2 != m % 2) { cout << 0; return 0; } for (int i = 1; i <= n; i++) { int cheak = 1; int num = 0; for (int j = 1; j <= m; j++) { if (mp[i][j]) num++, cheak *= mp[i][j]; } if (num == m) { if (cheak != -1) { fl = 1; break; } cnt++; } } if (fl) { cout << 0; return 0; } else { cout << q_pow(2, cnt); return 0; } }
2,500
CPP
#x, y = map(int, input().split()) #girdi = list(input()) m, h, w = map(int, input().split()) total = 0 for _ in range(1,w+1): total += (m*(_)) money = total-h if money < 0: print(0) else: print(total-h)
800
PYTHON3
import math as mt import sys, string from collections import Counter, defaultdict input = sys.stdin.readline # input functions I = lambda : int(input()) M = lambda : map(int, input().split()) ARR = lambda: list(map(int, input().split())) def printARR(arr): for e in arr: print(e, end=" ") print() def main(): n, l, r = M() num1 = {1} i = 1 while len(num1) < l: num1.add(2 * i) i = i * 2 num2 = set(list(num1).copy()) while len(num2) < r: num2.add(2 * i) i = i*2 a, b = 0, 0 num1, num2 = list(num1), list(num2) num1.sort() num2.sort() # print(num1,num2) a = sum(num1) b = sum(num2) a += ((n - len(num1)) * num1[0]) b += ((n - len(num2)) * num2[-1]) print(a,b) tc = 1 # tc = I() for _ in range(tc): main()
900
PYTHON3
#include <bits/stdc++.h> using namespace std; struct str { string name; int taxi = 0; int pizza = 0; int girls = 0; }; int main() { { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int t; cin >> t; struct str st[t]; for (int i = 0; i < t; i++) { int n; cin >> n; string s; cin >> s; st[i].name = s; for (int j = 0; j < n; j++) { string tem; cin >> tem; string tt = ""; set<char> se; for (int k = 0; k < (int)tem.size(); k++) { if (isdigit(tem[k])) { tt += tem[k]; se.insert(tem[k]); } } if (se.size() == 1) st[i].taxi++; else { bool ok = true; for (int k = 0; k < (int)tt.size() - 1; k++) { if (tt[k] <= tt[k + 1]) { ok = false; break; } } if (ok) st[i].pizza++; else st[i].girls++; } } } int maxt = st[0].taxi; vector<string> ans; for (int i = 0; i < t; i++) { if (st[i].taxi > maxt) { ans.clear(); maxt = st[i].taxi; ans.push_back(st[i].name); } else if (st[i].taxi == maxt) ans.push_back(st[i].name); } cout << "If you want to call a taxi, you should call: "; int c = 1; for (auto i : ans) { if (c == (int)ans.size()) cout << i; else cout << i << ", "; c++; } cout << ".\n"; maxt = st[0].pizza; ans.clear(); for (int i = 0; i < t; i++) { if (st[i].pizza > maxt) { ans.clear(); maxt = st[i].pizza; ans.push_back(st[i].name); } else if (st[i].pizza == maxt) ans.push_back(st[i].name); } cout << "If you want to order a pizza, you should call: "; c = 1; for (auto i : ans) { if (c == (int)ans.size()) cout << i; else cout << i << ", "; c++; } cout << ".\n"; maxt = st[0].girls; ans.clear(); for (int i = 0; i < t; i++) { if (st[i].girls > maxt) { ans.clear(); maxt = st[i].girls; ans.push_back(st[i].name); } else if (st[i].girls == maxt) ans.push_back(st[i].name); } cout << "If you want to go to a cafe with a wonderful girl, you should call: "; c = 1; for (auto i : ans) { if (c == (int)ans.size()) cout << i; else cout << i << ", "; c++; } cout << ".\n"; }
1,200
CPP
def solve(a, n): s = sum(a) if s % n == 0: return int(s / n) else: return int(s / n) + 1 if __name__ == '__main__': t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split())) print(solve(a, n))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { double x, y, z; cin >> x >> y >> z; double arr[12]; fill(arr, arr + 12, INT_MIN); bool flag = false; if (x <= 1 and y <= 1 and z <= 1) flag = 1; if (x >= 1) { if (x == 1) arr[0] = arr[1] = arr[2] = arr[3] = INT_MIN; else { arr[0] = log(log(x)) + z * log(y); arr[1] = log(log(x)) + y * log(z); arr[2] = log(log(x)) + log(y) + log(z); arr[3] = log(log(x)) + log(y) + log(z); } } if (x < 1 and flag) { arr[0] = log(log(1 / x)) + z * log(y); arr[1] = log(log(1 / x)) + y * log(z); arr[2] = log(log(1 / x)) + log(y) + log(z); arr[3] = log(log(1 / x)) + log(y) + log(z); } if (y >= 1) { if (y == 1) arr[4] = arr[5] = arr[6] = arr[7] = INT_MIN; else { arr[4] = log(log(y)) + z * log(x); arr[5] = log(log(y)) + x * log(z); arr[6] = log(log(y)) + log(z) + log(x); arr[7] = log(log(y)) + log(z) + log(x); } } if (y < 1 and flag) { arr[4] = log(log(1 / y)) + z * log(x); arr[5] = log(log(1 / y)) + x * log(z); arr[6] = log(log(1 / y)) + log(z) + log(x); arr[7] = log(log(1 / y)) + log(z) + log(x); } if (z >= 1) { if (z == 1) arr[8] = arr[9] = arr[10] = arr[11] = INT_MIN; else { arr[8] = log(log(z)) + y * log(x); arr[9] = log(log(z)) + x * log(y); arr[10] = log(log(z)) + log(x) + log(y); arr[11] = log(log(z)) + log(x) + log(y); } } if (z < 1 and flag) { arr[8] = log(log(1 / z)) + y * log(x); arr[9] = log(log(1 / z)) + x * log(y); arr[10] = log(log(1 / z)) + log(x) + log(y); arr[11] = log(log(1 / z)) + log(x) + log(y); } map<double, string> m; m[0] = "x^y^z"; m[1] = "x^z^y"; m[2] = "(x^y)^z"; m[3] = "(x^z)^y"; m[4] = "y^x^z"; m[5] = "y^z^x"; m[6] = "(y^x)^z"; m[7] = "(y^z)^x"; m[8] = "z^x^y"; m[9] = "z^y^x"; m[10] = "(z^x)^y"; m[11] = "(z^y)^x"; int ans = -1; double maximum = -(1e12); double minimum = 1e12; for (int i = 0; i < 12; ++i) if (flag) { if (arr[i] < minimum) { minimum = arr[i]; ans = i; } } else { if (arr[i] > maximum) { maximum = arr[i]; ans = i; } } cout << m[ans] << endl; }
2,400
CPP
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; struct fastio { char s[100000]; int it, len; fastio() { it = len = 0; } inline char get() { if (it < len) return s[it++]; it = 0; len = fread(s, 1, 100000, stdin); if (len == 0) return EOF; else return s[it++]; } bool notend() { char c = get(); while (c == ' ' || c == '\n') c = get(); if (it > 0) it--; return c != EOF; } } _buff; inline long long getnum() { long long r = 0; bool ng = 0; char c; c = _buff.get(); while (c != '-' && (c < '0' || c > '9')) c = _buff.get(); if (c == '-') ng = 1, c = _buff.get(); while (c >= '0' && c <= '9') r = r * 10 + c - '0', c = _buff.get(); return ng ? -r : r; } template <class T> inline void putnum(T x) { if (x < 0) putchar('-'), x = -x; register short a[20] = {}, sz = 0; while (x) a[sz++] = x % 10, x /= 10; if (sz == 0) putchar('0'); for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]); } inline char getreal() { char c = _buff.get(); while (c <= 32) c = _buff.get(); return c; } long long qpow(long long x, long long k) { return k == 0 ? 1 : 1ll * qpow(1ll * x * x % mod, k >> 1) * (k & 1 ? x : 1) % mod; } const double eps = 1e-7; const double pi = acos(-1); bool is0(double x) { return x > -eps && x < eps; } struct point { double x, y; point(double _x = 0, double _y = 0) { x = _x; y = _y; } point operator+(const point &t) const { return point(x + t.x, y + t.y); } point operator-(const point &t) const { return point(x - t.x, y - t.y); } point operator*(const point &t) const { return point(x * t.x - y * t.y, x * t.y + y * t.x); } point operator*(const double &t) const { return point(x * t, y * t); } point operator/(const double &t) const { return point(x / t, y / t); } point operator-() const { return point(-x, -y); } double len() const { return sqrt(x * x + y * y); } double sqrl() const { return x * x + y * y; } point operator/(const point &t) const { return point(x * t.x + y * t.y, -x * t.y + y * t.x) / t.len() / t.len(); } double det(const point &t) const { return x * t.y - y * t.x; } double dot(const point &t) const { return x * t.x + y * t.y; } double len2() const { return x * x + y * y; } point unit() const { return (*this) / len(); } bool operator<(const point &t) const { return is0(x - t.x) ? (is0(y - t.y) ? 0 : y < t.y) : x < t.x; } bool operator==(const point &t) const { return is0(x - t.x) && is0(y - t.y); } bool operator!=(const point &t) const { return !is0(x - t.x) || !is0(y - t.y); } void out() const { if (0) cout << x << "," << y << endl; } double rad() const { return atan2(y, x); } point conj() const { return point(x, -y); } }; bool cmpwithangle(point x, point y) { return x.rad() < y.rad(); } bool parallel(const point &px, const point &py, const point &qx, const point &qy) { return is0((px - py).det(qx - qy)); } point intersection(const point &px, const point &py, const point &qx, const point &qy) { double t = -(px - qx).det(qy - qx) / (py - px).det(qy - qx); return px + (py - px) * t; } point intersection(const pair<point, point> &p, const pair<point, point> &q) { return intersection(p.first, p.second, q.first, q.second); } vector<int> construct_ch(const vector<point> &a, int coef) { vector<int> s; for (int i = 0; i < a.size(); i++) { int sz = s.size(); while (sz >= 2 && (a[i] - a[s[sz - 2]]).det(a[s[sz - 1]] - a[s[sz - 2]]) * coef < eps) { s.pop_back(); sz--; } s.push_back(i); } return s; } vector<int> construct_chull(const vector<point> &a) { vector<int> h1 = construct_ch(a, -1), h2 = construct_ch(a, 1), h; for (int i = 0; i < h1.size(); i++) h.push_back(h1[i]); for (int i = int(h2.size()) - 2; i > 0; i--) h.push_back(h2[i]); return h; } bool inseg_in(const point &px, const point &py, const point &p) { return is0((py - px).det(p - px)) && (px - p).dot(py - p) < eps; } struct solution { bool emp; point p, q; solution() { emp = 1; } void init(point P, point Q) { p = P; q = Q; emp = 0; } void intersect(point a, point b) { if (emp) return; if (a == b) { if (p == q) { if (a != p) emp = 1; } else { if (inseg_in(p, q, a)) p = q = a; else emp = 1; } } else { bool tp = (b - a).det(p - a) > -eps; bool tq = (b - a).det(q - a) > -eps; if (p == q) { if (!tp) emp = 1; } else { if (parallel(a, b, p, q)) { if (!tp) emp = 1; } else { if (!tp && !tq) emp = 1; else if (!tp && tq) p = intersection(p, q, a, b); else if (tp && !tq) q = intersection(p, q, a, b); } } } } void limit(int L) { intersect(point(0, 0), point(L, 0)); intersect(point(L, 0), point(L, L)); intersect(point(L, L), point(0, L)); intersect(point(0, L), point(0, 0)); } void debug() { if (0) cout << "emp=" << emp << " p=" << p.x << "," << p.y << " q=" << q.x << "," << q.y << endl; } }; const int maxn = 100111; int n; int L, H; bool type[maxn]; pair<int, point> P[maxn]; solution getseg(point x) { point S = point(0, x.x / x.y * H); point T = point(L, L - (L - x.x) / x.y * H); solution ret; ret.init(S, T); ret.limit(L); return ret; } solution getnxt(solution a, solution b, double r) { vector<point> v; for (int i = -1; i <= 1; i += 2) { for (int j = -1; j <= 2; j += 2) { point dlt(i * r, j * r); v.push_back(a.p + dlt); v.push_back(a.q + dlt); } } sort(v.begin(), v.end()); vector<int> h = construct_chull(v); assert(h.size() > 0); h.push_back(h[0]); for (int i = 0; i + 1 < h.size(); i++) b.intersect(v[h[i]], v[h[i + 1]]); b.limit(L); return b; } solution pp[maxn]; bool check(double v) { solution cur; cur.init(P[0].second, P[0].second); for (int i = 1; i <= n; i++) { int dlt = P[i].first - P[i - 1].first; if (pp[i].emp) return false; cur = getnxt(cur, pp[i], dlt * v); if (cur.emp) return false; } return true; } int main() { n = getnum(), L = getnum(), H = getnum(); int Sx, Sy; Sx = getnum(), Sy = getnum(); P[0] = make_pair(0, point(Sx, Sy)); for (int i = 1; i <= n; i++) { int t, x, y; t = getnum(), x = getnum(), y = getnum(); P[i] = make_pair(t, point(x, y)); pp[i] = getseg(P[i].second); } double l = 0, r = 1001; for (int i = 0; i < 30; i++) { double mid = (l + r) / 2; if (check(mid)) r = mid; else l = mid; } if (l > 1000.5) puts("-1"); else cout << fixed << setprecision(6) << l << endl; return 0; }
3,500
CPP
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): inputs = [int(x) for x in input().split()] mod2 = [x%2 for x in inputs] n_moves = min(inputs[0:3]) if_work = False if sum(mod2) <= 1: if_work = True if (n_moves >= 1) and (sum(mod2) >= 3): if_work = True print("Yes" if if_work else "No")
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 4e5 + 5; const long long inf = 1e9 + 7; long long n, m; int a[maxn]; vector<int> t; int num[maxn]; inline int getId(int x) { return lower_bound(t.begin(), t.end(), x) - t.begin() + 1; } int main() { cin >> n >> m; long long p = (m * 8LL) / n; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); t.push_back(a[i]); } sort(t.begin(), t.end()); t.erase(unique(t.begin(), t.end()), t.end()); int len = t.size(); if (p >= 20 || (1 << p) >= len) { puts("0"); return 0; } long long ans = 0; for (int i = 1; i <= n; i++) { num[getId(a[i])]++; } for (int i = 2; i <= len; i++) { num[i] = num[i] + num[i - 1]; } long long pp = (1 << p); for (int i = pp; i <= len; i++) { ans = max(ans, (long long)num[i] - num[i - pp]); } cout << n - ans << endl; return 0; }
1,600
CPP
#include <bits/stdc++.h> char s[1000001]; int tran(char c) { if (c == '1') return 1; if (c == '2') return 2; if (c == '3') return 3; if (c == '4') return 4; if (c == '5') return 5; if (c == '6') return 6; if (c == '7') return 7; if (c == '8') return 8; if (c == '9') return 9; } int main() { scanf("%s", s); int len = strlen(s); long long r = 0, l = 0, p = 0; for (int i = 0; i < len; i++) { if (s[i] == '^') { p = i; break; } } for (int i = 0; i < len; i++) { if (i == p) continue; if (s[i] != '=') { if (i < p) { l += (long long)tran(s[i]) * (p - i); } else { r += (long long)tran(s[i]) * (i - p); } } } if (l == r) printf("balance\n"); else if (l < r) printf("right\n"); else printf("left\n"); return 0; }
900
CPP
n=int(input()) ans=0 for a in range(1,n+1): for b in range(a,n+1): c=a^b if a+b>c>=b and c<=n: ans+=1 print(ans)
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 200005; long long mod = 1000000007; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } bool is_vowel(char c) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') return 1; return 0; } double getDistance(double x1, double y1, double x2, double y2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } long long extended_euclidean(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return a; } long long g = extended_euclidean(b, a % b, y, x); y -= (a / b) * x; return g; } long long power(long long base, long long p) { if (p == 1) return base; if (!p) return 1ll; long long ret = power(base, p / 2); ret *= ret; ret %= mod; if (p & 1) ret *= base; return ret % mod; } pair<long long, long long> vec(pair<long long, long long> a, pair<long long, long long> b) { pair<long long, long long> ret = pair<long long, long long>(a.first - b.first, a.second - b.second); return ret; } long long getArea(pair<long long, long long> a, pair<long long, long long> b, pair<long long, long long> c) { pair<long long, long long> x = vec(a, b); pair<long long, long long> y = vec(c, b); return abs(x.first * y.second - x.second * y.first); } int main() { int a, b, c, n; cin >> a >> b >> c >> n; int mn = a + b - c; if (mn < 0 || mn >= n || (c > min(a, b))) cout << -1; else { cout << n - mn; } return 0; }
1,000
CPP
#include <bits/stdc++.h> using namespace std; int n, m, i, j, x, h; char s[1001]; char c = '<'; int main() { scanf("%s", &s); h = 0; i = 0; while (i < strlen(s)) { if (s[i + 1] != '/') { printf("%*c", 1 + 2 * h, c); printf("%c>\n", s[i + 1]); h++; i += 3; } else { h--; printf("%*c", 1 + 2 * h, c); printf("/%c>\n", s[i + 2]); i += 4; } } return 0; }
1,000
CPP
n, m, r = map(int, input().split(' ')) sarr = list(map(int, input().split(' '))) barr = list(map(int, input().split(' '))) smin = min(sarr) bmax = max(barr) if(smin < bmax): ans = (r//smin)*bmax + r % smin else: ans = r print(ans)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n; cin >> n; if (n == 1) { cout << "0\n"; } else { long long num = (n - 1) / 2; long long ans = 0; for (long long i = num; i >= 1; i--) { ans += (i * (4 * (n - 1))); n -= 2; } cout << ans << endl; } } return 0; }
1,000
CPP
T=int(input()) for t in range (T): n,x=input().split(' ') n=int(n) x=int(x) print(2*x)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; struct A { int a; int t; }; A s[100]; int ans[101][20005]; int main() { int n, k, b; cin >> n >> k; for (int i = 0; i < n; i++) cin >> s[i].a; for (int i = 0; i < n; i++) { cin >> b; s[i].t = b * k - s[i].a; } for (int i = 0; i <= n; i++) for (int j = 0; j < 20005; j++) ans[i][j] = -1; ans[0][0 + 10002] = 0; int r; for (int i = 1; i <= n; i++) { for (int j = 0; j < 20005; j++) { if (ans[i - 1][j] < 0) continue; ans[i][j] = max(ans[i][j], ans[i - 1][j]); r = j + s[i - 1].t; if (r >= 0 && r < 20005) ans[i][r] = max(ans[i][r], ans[i - 1][j] + s[i - 1].a); } } cout << (ans[n][0 + 10002] == 0 ? -1 : ans[n][0 + 10002]); return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; int fastread() { int t; scanf("%d", &t); return t; } int common_base(int x1, int x2) { while (x1 != x2) { if (x1 > x2) x1 /= 2; else x2 /= 2; } return x1; } vector<int> v; int main() { int n = fastread(); int base = 0; for (int i = 0; i < n; i++) { int t; cin >> t; v.push_back(t); while (t % 2 == 0) t /= 2; if (base == 0) base = t; else if (base != t && base != 1) base = common_base(base, t); } int result = 2000000000; vector<int> x; int base_t = base; while (base_t <= 100000) { x.push_back(0); base_t *= 2; } for (int i = 0; i < n; i++) { int zeros_to_base = 0; int divs_required = 0; while (v[i] != base) { if (v[i] % 2 == 0) zeros_to_base++; else zeros_to_base = 0; v[i] /= 2; divs_required++; } int divs_anyway = (divs_required - zeros_to_base); for (int j = zeros_to_base, k = 0; j >= 0; j--, k++) x[j] += divs_anyway + k; for (int j = zeros_to_base + 1, k = 1; j < x.size(); j++, k++) x[j] += divs_anyway + k; } for (int i = 0; i < x.size(); i++) result = min(result, x[i]); printf("%d", result); return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; const long long OO = 1e8; const double EPS = (1e-7); int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; } int n; string s; int dp[100009][2][3]; int solveDP(int i, int prev, int op) { if (i == n) return op == 3; int& ret = dp[i][prev][op]; if (ret != -1) return ret; ret = solveDP(i + 1, prev, op); if (op != 1 && (s[i] - '0') != prev) ret = max(ret, 1 + solveDP(i + 1, (s[i] - '0'), op)); if (op == 1 && (s[i] - '0') == prev) ret = max(ret, 1 + solveDP(i + 1, prev ^ 1, op)); if (op <= 1) ret = max(ret, solveDP(i, prev, op + 1)); return ret; } int main() { cin >> n; cin >> s; memset(dp, -1, sizeof(dp)); cout << max(solveDP(0, 0, 0), solveDP(0, 1, 0)) << endl; }
1,600
CPP
n = int(input()) if((n%2)==0 and n>2): print("YES") else: print("NO")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; int a[3005]; int b[305][305]; vector<int> v; vector<int> v2; bool used[305]; void dfs(int x) { used[x] = true; v.push_back(x); v2.push_back(a[x]); for (int j = 1; j <= n; ++j) if (b[x][j] == 1 && used[j] == false) dfs(j); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { char c; cin >> c; b[i][j] = c - '0'; } } for (int i = 1; i <= n; ++i) { if (used[i] == false) { v.clear(); v2.clear(); dfs(i); sort(v.begin(), v.end()); sort(v2.begin(), v2.end()); for (int i = 0; i < v.size(); ++i) a[v[i]] = v2[i]; } } for (int i = 1; i <= n; ++i) cout << a[i] << " "; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; const int dx[] = {0, 0, 1, 1}; const int dy[] = {0, 1, 0, 1}; int res = 0; vector<int> ans; vector<pair<int, int> > sq, can; vector<pair<pair<int, int>, pair<int, int> > > d; vector<int> now; int b[40][40]; void Check() { for (int i = 0; i < 14; i++) for (int k = 0; k < 4; k++) b[sq[i].first + dx[k]][sq[i].second + dy[k]] = now[i]; vector<pair<int, int> > t; for (int i = 0; i < d.size(); i++) t.push_back(make_pair(b[d[i].first.first][d[i].first.second], b[d[i].second.first][d[i].second.second])); for (int i = 0; i < t.size(); i++) if (t[i].first > t[i].second) swap(t[i].first, t[i].second); sort(t.begin(), t.end()); if (t == can) { res++; if (ans.empty()) ans = now; } } void Func(int n) { if (n == 7) { Check(); return; } int beg = 0; for (int i = 0; i < 14; i++) if (now[i] == -1) { now[i] = n; beg = i; break; } for (int i = beg; i < 14; i++) if (now[i] == -1) { now[i] = n; Func(n + 1); now[i] = -1; } now[beg] = -1; } int main() { int n, m; cin >> n >> m; vector<string> arr(n); for (int i = 0; i < n; i++) cin >> arr[i]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (isalpha(arr[i][j])) { if (i + 1 < n && arr[i][j] == arr[i + 1][j]) d.push_back(make_pair(make_pair(i, j), make_pair(i + 1, j))); if (j + 1 < m && arr[i][j] == arr[i][j + 1]) d.push_back(make_pair(make_pair(i, j), make_pair(i, j + 1))); } vector<vector<bool> > was(n, vector<bool>(m, false)); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (!was[i][j] && isalpha(arr[i][j])) { for (int k = 0; k < 4; k++) was[i + dx[k]][j + dy[k]] = true; sq.push_back(make_pair(i, j)); } now.assign(14, -1); for (int i = 0; i < 7; i++) for (int j = i; j < 7; j++) can.push_back(make_pair(i, j)); Func(0); cout << (long long)res * 5040 << endl; vector<string> p(n, string(m, '.')); for (int i = 0; i < sq.size(); i++) for (int k = 0; k < 4; k++) p[sq[i].first + dx[k]][sq[i].second + dy[k]] = '0' + ans[i]; for (int i = 0; i < n; i++) cout << p[i] << endl; return 0; }
2,400
CPP
'''Author- Akshit Monga''' t=int(input()) for _ in range(t): n=int(input()) s=input() arr=[] val=s[0] c=0 for i in s: if i==val: c+=1 else: arr.append(c) c=1 val=i if c>0: arr.append(c) ans=0 q=[] for i in range(len(arr)): if arr[i]!=1: q.append(i) k=0 while 1: if arr==[]: break if arr[0]>1: ans+=1 q.pop(0) arr.pop(0) else: if len(q)>0: arr[q[0]-k]-=1 if arr[q[0]-k]==1: q.pop(0) ans+=1 else: ans+= (len(arr))//2 +(len(arr))%2 break arr.pop(0) k+=1 print(ans)
1,700
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long maxn = 10005, maxm = 100005, INF = 0x3f3f3f3f; struct Edge { long long next, to, f; } edge[maxm * 2]; long long n, m, head[maxn], tot; void INIT() { memset(head, -1, sizeof(head)); tot = 0; } void insert(long long u, long long v, long long w) { edge[tot].to = v, edge[tot].f = w, edge[tot].next = head[u], head[u] = tot; tot++; edge[tot].to = u, edge[tot].f = 0, edge[tot].next = head[v], head[v] = tot; tot++; } long long level[maxn]; bool makelevel(long long s, long long t) { memset(level, 0, sizeof(level)); level[s] = 1; long long que[maxn]; long long iq = 0; que[iq++] = s; long long i, k; long long top; for (i = 0; i < iq; i++) { top = que[i]; if (top == t) return true; for (k = head[top]; k != -1; k = edge[k].next) { if (!level[edge[k].to] && edge[k].f) { que[iq++] = edge[k].to; level[edge[k].to] = level[top] + 1; } } } return false; } long long DFS(long long now, long long maxf, long long t) { if (now == t) return maxf; long long ret = 0, f; long long k; for (k = head[now]; k != -1; k = edge[k].next) { if (edge[k].f && level[edge[k].to] == level[now] + 1) { f = DFS(edge[k].to, min(maxf - ret, edge[k].f), t); edge[k].f -= f; edge[k ^ 1].f += f; ret += f; if (ret == maxf) return ret; } } return ret; } long long DINIC(long long s, long long t) { long long ans = 0; while (makelevel(s, t)) ans += DFS(s, INF, t); return ans; } int main() { INIT(); int n, a, b; scanf("%d%d", &n, &a); int s = 0, t; for (int i = 2; i <= a; i++) { int u; scanf("%d", &u); insert(n + u, n + i, INF); insert(s, n + i, 1); } for (int i = 1; i <= n; i++) { int u; scanf("%d", &u); insert(n + u, i, INF); } scanf("%d", &b); t = n + a + b + 1; for (int i = 2; i <= b; i++) { int u; scanf("%d", &u); insert(n + a + i, n + a + u, INF); insert(n + a + i, t, 1); } for (int i = 1; i <= n; i++) { int u; scanf("%d", &u); insert(i, n + a + u, INF); } printf("%lld\n", a + b - 2 - DINIC(s, t)); return 0; }
2,400
CPP
#include <bits/stdc++.h> using namespace std; const long long MAXVAL = 3000000000000000000LL; const long long MINVAL = -1000000000000000000LL; double sqr(double a) { return a * a; } int n, n1, n2; vector<int> v; double res = 0.0; int main() { cin >> n >> n1 >> n2; v.resize(n); for (int i = 0; i < n; ++i) cin >> v[i]; sort(v.begin(), v.end()); reverse(v.begin(), v.end()); if (n2 < n1) swap(n1, n2); for (int i = 0; i < n1; ++i) { res += (double)v[i] / (double)n1; } for (int i = n1; i < n1 + n2; ++i) res += (double)v[i] / (double)n2; cout.precision(15); cout << fixed << res << endl; return 0; }
1,100
CPP
from sys import stdin, stdout from sys import maxsize #input = stdin.readline().strip def solve(a, n): v1, v2 = 0, 0 for i in range(0, n, 2): if(a[i] % 2 == 1): v1 += 1 for i in range(1, n, 2): if(a[i] % 2 == 0): v2 += 1 if(v1 == v2): print(v1) else: print(-1) pass test = 1 test = int(input()) for t in range(0, test): # brr = [list(map(int,input().split())) for i in range(rows)] # 2D array row-wise input n = int(input()) # s = list(input()) # String Input, converted to mutable list. # n, x = list(map(int, input().split())) arr = [int(x) for x in input().split()] ans = solve(arr, n) ''' rows, cols = (5, 5) arr = [[0]*cols for j in range(rows)] # 2D array initialization b=input().split() # list created by spliting about spaces brr = [[int(b[cols*i+j]) for j in range(cols)] for i in range(rows)] # 2D array Linear Input rows,cols=len(brr),len(brr[0]) # no of rows/cols for 2D array arr.sort(key = lambda x : x[1]) # sort list of tuples by 2nd element, Default priority - 1st Element then 2nd Element s=set() # empty set a=maxsize # initializing infinity b=-maxsize # initializing -infinity mapped=list(map(function,input)) # to apply function to list element-wise try: # Error handling #code 1 except: # ex. to stop at EOF #code 2 , if error occurs '''
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 100; int sonb[N], sonr[N], fath[N]; vector<int> e[N]; int col[N]; void dfs(int now, int fa) { fath[now] = fa; if (col[now] == 0) { sonb[now] = sonr[now] = 0; } if (col[now] == 1) { sonr[now] = 1; sonb[now] = 0; } if (col[now] == 2) { sonb[now] = 1; sonr[now] = 0; } for (int i = 0; i < e[now].size(); i++) { int to = e[now][i]; if (to == fa) continue; dfs(to, now); sonr[now] += sonr[to]; sonb[now] += sonb[to]; } } int nr = 0; int nb = 0; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> col[i]; if (col[i] == 1) nr++; if (col[i] == 2) nb++; } int u, v; for (int i = 1; i <= n - 1; i++) { cin >> u >> v; e[u].push_back(v); e[v].push_back(u); } dfs(1, 0); int ans = 0; for (int a = 1; a <= n; a++) { for (int j = 0; j < e[a].size(); j++) { int b = e[a][j]; if (b != fath[a]) { if (col[a] == 2) { if (sonr[b] == nr && sonb[b] == 0) ans++; } else if (col[a] == 1) { if (sonb[b] == nb && sonr[b] == 0) ans++; } else { if ((sonr[b] == 0 && sonb[b] == nb) || (sonr[b] == nr && sonb[b] == 0)) ans++; } } } } cout << ans << endl; return 0; }
1,800
CPP
from collections import Counter t = int(input()) for _ in range(t): n = int(input()) s = list(input()) t = list(input()) flag = True for i in range(n): if ord(t[i])<ord(s[i]): flag = False break if not flag: print(-1) continue steps = 0 arr = [chr(i) for i in range(ord('a'),ord('a')+21)] for char in arr: mn = float('inf') for i in range(n): if s[i]==char and t[i]!=char: mn = min(ord(t[i]),mn) if mn!=float('inf'): steps+=1 for i in range(n): if s[i]==char and t[i]!=char: s[i]=chr(mn) print(steps) # cnt = Counter() # adj = {chr(i):[] for i in range(ord('a'),ord('z')+1)} # flag = True # for i in range(n): # if ord(t[i])<ord(s[i]): # flag = False # break # if cnt[s[i]+" "+t[i]]==0: # adj[s[i]].append(t[i]) # cnt[s[i]+" "+t[i]]+=1 # if not flag: # print(-1) # continue # visited = set() # def dfs(i,depth,child): # if (i,depth) in visited: # return False # visited.add((i,depth)) # if depth>21: # return False # if depth!=1 and i == child: # return True # children = adj[i] # for c in children: # if dfs(c,depth+1,child): # return True # return False # ans = len(cnt.keys()) # for i in range(n): # if dfs(s[i],0,t[i]): # ans-=1 # visited = set() # print(ans)
1,700
PYTHON3
n = int(input()) p = input().split(" ") q = input().split(" ") del p[0] del q[0] a = p+q k = 0 for i in range(1,n+1): if str(i) in a: k+=1 if k == n: print("I become the guy.") else: print("Oh, my keyboard!")
800
PYTHON3
t = int(input()) for _ in range(t): x, y = list(map(int, input().split())) if (min(x, y) * 2 < max(x, y)): print(max(x, y)**2) else: print((min(x, y) * 2)**2)
800
PYTHON3
#include <bits/stdc++.h> char str[100005][30]; int ns[100005]; int intr[30]; int rem; int n; int find_first() { int i; int bst = -1; for (i = 0; i < n; i++) { if (ns[i] > 0 && intr[str[i][0] - 'a'] == 0 && (bst == -1 || str[i][0] < str[bst][0])) bst = i; } return bst; } int main() { int i, j, k; char inp[100005]; int seen[30]; char out[30]; char used[30]; int o, m; for (i = 0; i < 100005; i++) ns[i] = 0; for (i = 0; i < 30; i++) intr[i] = used[i] = 0; scanf("%d", &n); rem = n; for (i = 0; i < n; i++) { scanf("%s", inp); for (j = 0; j < 30; j++) seen[j] = 0; for (j = 0; inp[j] != '\0'; j++) { if (seen[inp[j] - 'a']) { printf("NO\n"); return 0; } if (j > 0) intr[inp[j] - 'a'] = 1; seen[inp[j] - 'a'] = 1; str[i][j] = inp[j]; ns[i]++; } str[i][ns[i]] = '\0'; } for (o = 0, m = 0; rem > 0; o++) { if (m == o) { int c = find_first(); if (c == -1) { printf("NO\n"); return 0; } if (used[str[c][0] - 'a']) { printf("NO\n"); return 0; } used[str[c][0] - 'a'] = 1; out[m] = str[c][0]; m++; } for (i = 0; i < n; i++) { if (str[i][0] != out[o] || ns[i] == 0) continue; for (j = 0; j < ns[i]; j++) { if (j < m - o) { if (str[i][j] != out[o + j]) { printf("NO\n"); return 0; } } else { if (used[str[i][j] - 'a']) { printf("NO\n"); return 0; } used[str[i][j] - 'a'] = 1; out[m] = str[i][j]; m++; } } ns[i] = 0; rem--; } } out[m] = '\0'; printf("%s\n", out); return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; string sln; cin >> sln; bool fl = 0; int mx = 0; if (sln[0] == '?') mx = 2; for (int i = 1; i < n; ++i) { if (sln[i] == sln[i - 1] && sln[i] != '?') fl = 1; if (i < n - 1 && sln[i] == '?') { int t = sln[i - 1] == sln[i + 1] ? 2 : 1; if (sln[i - 1] == '?' || sln[i + 1] == '?') mx = max(2, mx); mx = max(t, mx); } } if (sln[n - 1] == '?') mx = max(2, mx); if (fl || mx < 2) cout << "No" << endl; else cout << "Yes" << endl; return 0; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n == 0) { puts("a"); return 0; } char c = 'a'; while (n) { int sum = 0; int cur = 1; while (sum <= n) { sum += cur; cur++; } cur--; for (int i = 1; i <= cur; i++) { printf("%c", c); } cur--; for (int i = 1; i <= cur; i++) n -= i; c++; } return 0; }
1,600
CPP
import sys import math import heapq from collections import defaultdict, deque input = sys.stdin.readline def r(): return int(input()) def rm(): return map(int,input().split()) def rl(): return list(map(int,input().split())) '''A Buy a shovel''' k,r=rm() cnt=1;i=0;j=k while j%10!=r and j%10!=0: cnt+=1 j+=k print(cnt)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int xx[4] = {0, 0, -1, 1}; int yy[4] = {-1, 1, 0, 0}; int N; long long int mini; vector<long long int> ve; long long int cal(long long int large, long long int value) { long long int ret = 0; long long int small = large - 1; if (value >= (large - 2) * (large - 1)) { ret = (value + large - 1) / large; } else { if (value / small < value % small) { ret = -1; } else { ret = (value + large - 1) / large; } } return ret; } int main(void) { scanf("%d", &N); ; mini = 2000000000; for (int i = 1; i <= N; i++) { long long int temp; scanf("%lld", &temp); ; ve.push_back(temp); mini = min(mini, temp); } long long int ind = 1; while (1) { long long int cur = mini / ind; long long int cou = 0; cur++; for (int i = 0; i < ve.size(); i++) { long long int temp = cal(cur, ve[i]); if (temp == -1) { break; } cou += temp; if (i == ve.size() - 1) { printf("%lld", cou); return 0; } } cou = 0; cur--; for (int i = 0; i < ve.size(); i++) { long long int temp = cal(cur, ve[i]); if (temp == -1) { break; } cou += temp; if (i == ve.size() - 1) { printf("%lld", cou); return 0; } } cou = 0; cur--; for (int i = 0; i < ve.size(); i++) { long long int temp = cal(cur, ve[i]); if (temp == -1) { break; } cou += temp; if (i == ve.size() - 1) { printf("%lld", cou); return 0; } } ind++; } return 0; }
2,500
CPP
n = input().strip() n = int(n) stew = list(map(int, input().strip().split(' '))) stew.sort() min_val = stew[0] max_val= stew[len(stew)-1] val = n-stew.count(min_val)-stew.count(max_val) if(val<1) :val=0 print(val)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 1000; char a[maxn][maxn]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; string s; cin >> n >> s; memset(a, ' ', sizeof(a)); int h = 0, w = n, d = 0; for (int i = 0; i < n; i++) { if (i > 0 && s[i] == ']' && s[i - 1] == '[') w += 3; if (s[i] == '[') d++; else d--; h = max(h, d); } int x = 0; for (int i = 0; i < n; i++) { if (s[i] == ']') { if (i > 0 && s[i - 1] == '[') x += 3; d--; a[x - 1][d] = a[x - 1][2 * h - d] = '-'; } a[x][d] = a[x][2 * h - d] = '+'; for (int j = d + 1; j < 2 * h - d; j++) a[x][j] = '|'; if (s[i] == '[') { a[x + 1][d] = a[x + 1][2 * h - d] = '-'; d++; } x++; } for (int i = 0; i <= 2 * h; i++) { for (int j = 0; j < w; j++) cout << a[j][i]; cout << "\n"; } return 0; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(NULL); ios_base::sync_with_stdio(false); long long t = 1; while (t--) { string s; cin >> s; long long n = s.length(); long long arr[1000001] = {}; long long x = 1; long long md[n]; for (long long i = n - 1; i >= 0; i--) { md[i] = x % 1000000007; x = md[i] * 10; } long long z = n - 1; long long ans = 0; for (long long i = n - 1; i >= 1; i--) { x = (z * (z + 1)) / 2; x %= 1000000007; x = (x * (s[i] - '0')) % 1000000007; x = (x * md[i]) % 1000000007; z--; ans = (ans + x) % 1000000007; } long long k = 0; x = 1; for (long long i = n - 1; i >= 0; i--) { k = (k + (md[i] * x) % 1000000007) % 1000000007; md[i] = k; x++; } for (long long i = 0; i <= n - 2; i++) { long long x = ((s[i] - '0') * md[i + 1]) % 1000000007; ans = (ans + x) % 1000000007; } cout << ans << "\n"; } return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; long long dp[1000003]; map<pair<long long, long long>, long long> m; long long a[100][100]; long long b[1003][1002]; long long neg[1000003]; long long other; int main() { long long i, j, k, x, y; cin >> k >> x >> y; if (x > y) dp[1] = 1; else if (x == y) { dp[1] = 0; } else { neg[1]++; } if (x == 1 && y == 3) { dp[1] = 1; neg[1]--; } if (x == 3 && y == 1) { dp[1] = 0; neg[1]++; } m.insert(make_pair(make_pair(x, y), 1)); for (i = 1; i <= 3; i++) { for (j = 1; j <= 3; j++) { cin >> a[i][j]; } } for (i = 1; i <= 3; i++) { for (j = 1; j <= 3; j++) { cin >> b[i][j]; } } long long cnt = 2; long long f = x; x = a[x][y]; y = b[f][y]; while (m.find(make_pair(x, y)) == m.end() && cnt <= k) { m.insert(make_pair(make_pair(x, y), cnt)); if (x > y) dp[cnt] = 1; else if (x == y) { dp[cnt] = 0; } else { neg[cnt]++; } if (x == 1 && y == 3) { dp[cnt] = 1; neg[cnt]--; } if (x == 3 && y == 1) { dp[cnt] = 0; neg[cnt]++; } dp[cnt] += dp[cnt - 1]; neg[cnt] += neg[cnt - 1]; long long f = x; x = a[x][y]; y = b[f][y]; cnt++; } if (cnt == k + 1) { cout << dp[cnt - 1] << " " << neg[cnt - 1] << endl; return 0; } long long sum = 0; sum = dp[m[make_pair(x, y)] - 1]; long long other1 = neg[m[make_pair(x, y)] - 1]; long long g = dp[cnt - 1] - sum; other = neg[cnt - 1] - other1; long long len = cnt - 1 - m[make_pair(x, y)] + 1; g = (g * ((k + 1 - (m[make_pair(x, y)])) / len)); other = (other * ((k + 1 - (m[make_pair(x, y)])) / len)); long long gh = (k + 1 - (m[make_pair(x, y)])) % len; for (i = m[make_pair(x, y)]; i <= m[make_pair(x, y)] + gh - 1; i++) { g += dp[i] - dp[i - 1]; other += neg[i] - neg[i - 1]; } long long jeet = sum + g; long long haar = other; cout << jeet << " " << haar + other1 << endl; }
1,800
CPP
from bisect import bisect_left t = int(input()) for _ in range(t): n,s,k = map(int,input().split()) a = list(map(int,input().split())) a.sort() now = bisect_left(a,s) #print(a) #print(s,now) if now >= k or now <= -1 or a[now] != s: print(0) continue else: tmp1,tmp2 = 10**10,10**10 i = 1 check = True while check: if now-i < 0 and a[0] > 1: tmp1 = s - (a[0]-1) check = False else: if k-1>now - i >= 0 and a[now-i] != a[now]-i: tmp1 = s - (a[now-i+1]-1) check = False if now + i >= k and a[k-1] < n: tmp2 = a[k-1]+1 - s check = False else: if 0 < now + i < k and a[now+i] != a[now] + i: tmp2 = a[now+i-1] + 1 - s check = False if check: i += 1 print(min(tmp1,tmp2))
1,100
PYTHON3
maxp = 1000 np = [] for i in range(2, maxp): j = 2 while j * j <= i: if i % j == 0: np.append(i) break j += 1 def isprime(i): j = 2 while j * j <= i: if i % j == 0: return False j += 1 return True n = int(input()) for i in np: if not isprime(n - i): print(i, n - i) break
800
PYTHON3
#include <bits/stdc++.h> using namespace std; ostream_iterator<int> output(cout, " "); int remove_zero(int n) { string str; stringstream s; s << n; s >> str; str.erase(remove(str.begin(), str.end(), '0'), str.end()); return atoi(str.c_str()); } int main() { int a, b; cin >> a >> b; remove_zero(a) + remove_zero(b) == remove_zero(a + b) ? cout << "YES" << endl : cout << "NO" << endl; return 0; }
1,000
CPP
n = int(input()) a = list(map(int,input().split())) a = sorted(a) c = int(a[-1] ** (1/(n-1)) ) res1 = sum(abs((c**i -a[i])) for i in range(n)) res2 = sum(abs((c+1)**i -a[i]) for i in range(n)) print(res1 if res1<res2 else res2)
1,500
PYTHON3
N = int(input()) m = list(map(int, input().split())) ans = 0 def sol(k): ret = l = r = m[k] for i in range(k+1, N): if m[i] < r: r = m[i] ret += r for i in range(k-1, -1, -1): if m[i] < l: l = m[i] ret += l return ret ans_idx = -1 for i in range(N): X = sol(i) if ans < X: ans = X ans_idx = i l = r = m[ans_idx] for i in range(ans_idx+1, N): if m[i] < r: r = m[i] else: m[i] = r for i in range(ans_idx-1, -1, -1): if m[i] < l: l = m[i] else: m[i] = l print(*m)
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, x1, y1, x2, y2, k1, k2, k11, k12, k21, k22, a1, b1, a2, b2; while (scanf("%I64d%I64d%I64d%I64d%I64d%I64d", &a, &b, &x1, &y1, &x2, &y2) != EOF) { a1 = x1 + y1; b1 = x1 - y1; k11 = a1 / (2 * a); if (a1 < 0) k11--; k12 = b1 / (2 * b); if (b1 < 0) k12--; a2 = x2 + y2; b2 = x2 - y2; k21 = a2 / (2 * a); if (a2 < 0) k21--; k22 = b2 / (2 * b); if (b2 < 0) k22--; k1 = abs(k11 - k21); k2 = abs(k12 - k22); printf("%I64d\n", max(k1, k2)); } return 0; }
1,800
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100007; int n, k, parent[N], a[N], root; vector<int> v[N]; int kaglkahkjga[N], qkjohguogqkjgq[N], leftest[N], rightest[N]; int cnt[N]; long long sum[N]; pair<int, int> queries[N]; double answer[N]; bool cmp_value(int x, int y) { return a[x] < a[y]; } void update(int down, int up, int value) { int left_index = (int)(lower_bound(queries + 1, queries + 1 + k, make_pair(down, 0)) - queries); int right_index = (int)(upper_bound(queries + 1, queries + 1 + k, make_pair(up, 10000000)) - queries) - 1; ++cnt[left_index]; --cnt[right_index + 1]; sum[left_index] += value; sum[right_index + 1] -= value; } void dfs(int node, int from, int to) { if (v[node].empty()) { leftest[node] = node; rightest[node] = node; return; } dfs(kaglkahkjga[node], from, a[node] - 1); dfs(qkjohguogqkjgq[node], a[node] + 1, to); leftest[node] = leftest[kaglkahkjga[node]]; rightest[node] = rightest[qkjohguogqkjgq[node]]; update(from, a[node] - 1, a[leftest[qkjohguogqkjgq[node]]]); update(a[node] + 1, to, a[rightest[kaglkahkjga[node]]]); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int i; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d %d", &parent[i], &a[i]); if (parent[i] > 0) v[parent[i]].push_back(i); else root = i; } for (i = 1; i <= n; i++) if (!v[i].empty()) { sort(v[i].begin(), v[i].end(), cmp_value); kaglkahkjga[i] = v[i][0]; qkjohguogqkjgq[i] = v[i][1]; } scanf("%d", &k); for (i = 1; i <= k; i++) { scanf("%d", &queries[i].first); queries[i].second = i; } sort(queries + 1, queries + 1 + k); dfs(root, 1, 1000000000); for (i = 1; i <= k; i++) { sum[i] += sum[i - 1]; cnt[i] += cnt[i - 1]; answer[queries[i].second] = (double)(sum[i]) / (double)(cnt[i]); } for (i = 1; i <= k; i++) { printf("%.15lf\n", answer[i]); } return 0; }
2,200
CPP
for _ in range(int(input())): n,a,b,c,d=map(int,input().split()) x=(c+d) y=(c-d) f=0 tmin=(a-b)*n tmax=(a+b)*n if(y<=tmin<=x): print("YES") elif(y<=tmax<=x): print("YES") elif(tmin<=y and tmax>=x): print("YES") else: print("NO")
900
PYTHON3
a = int(input()) b = list(input().split()) k = "".join(b) print(k.count("101"))
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int TC, N; string S; array<int, 4> solve() { int pos = find(S.begin() + N / 2, S.end(), '0') - S.begin() + 1; if (pos <= N) return {pos - N / 2, pos, pos - N / 2, pos - 1}; if (S[N - 1 - N / 2] == '0') return {N - N / 2, N, N - N / 2 + 1, N}; return {N - N / 2, N - 1, N - N / 2 + 1, N}; } int main() { ios_base::sync_with_stdio(0); cin >> TC; while (TC--) { cin >> N >> S; auto ans = solve(); for (int i = (0); i < (4); i++) cout << ans[i] << " \n"[i == 3]; } }
1,500
CPP
#include <bits/stdc++.h> #define ll long long using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); ll k, l, r, t, x, y; cin >> k >> l >> r >> t >> x >> y; r -= l; k -= l; l = 0; if (x > y) { ll last = k / (x - y); if (last < t) { cout << "No"; return 0; } } if (r >= x + y) { cout << "Yes"; return 0; } // cout << "IDK" << endl; ll needed_steps = k / x; if (t <= needed_steps) { cout << "Yes"; return 0; } t -= needed_steps; vector <int> used(x + 1); k %= x; while (t) { if (used[k]) { cout << "Yes"; return 0; } used[k] = 1; if (k + y > r || k + y < x) { cout << "No"; return 0; } ll needed_steps = (k + y) / x; if (t <= needed_steps) { cout << "Yes"; return 0; } t -= needed_steps; k = (k + y) % x; } }
2,200
CPP
#include <bits/stdc++.h> using namespace std; int n, m, k, i = 0, data, compara, cant, cont0 = 0, amigo = 0; int bit(int n) { int cont = 0; while (n != 0) { n = n / 2; cont++; } return cont; } int main() { vector<int> datos; cin >> n >> m >> k; n = pow(2, n); while (i <= m) { cin >> data; datos.push_back(data); i++; } i = 0; while (i < m) { if (datos[i] < n) { cont0 = 0; if (datos[i] > datos[m]) { cant = bit(datos[i]); compara = ((datos[i]) ^ (datos[m])); for (int i = 0; i < cant; i++) { if (compara & (1 << i)) cont0++; } } else { cant = bit(datos[m]); compara = ((datos[i]) ^ (datos[m])); for (int i = 0; i < cant; i++) { if (compara & (1 << i)) cont0++; } } } if (cont0 <= k) amigo++; i++; } cout << amigo << endl; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; vector<int> months = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int MAX = 1e5 + 55; const int inf = 1e9 + 77; const int MOD = 1e9 + 7; const double PI = acos(-1.0); const double eps = 1e-7; int a[MAX]; vector<int> v; void GenerateDivisors(int n) { int i; v.push_back(1); for (i = 2; i * i < n; ++i) { if (n % i == 0) { v.push_back(i); if (i != 2) v.push_back(n / i); } } if (i * i == n) v.push_back(i); } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); GenerateDivisors(n); int mx = -inf; for (auto f : v) { if (f == n / 2 + (n & 1)) continue; for (int st = 0; st < f; ++st) { int sum = 0; for (int i = st; i < n; i += f) sum += a[i]; mx = max(mx, sum); } } printf("%d", mx); return 0; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; int m, n; double f[1005][1005]; int vis[1005][1005]; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') f = -1; ch = getchar(); if (ch == -1) return 0; } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } inline void write(int x) { if (x < 0) { putchar('-'); x = -x; } if (x >= 10) write(x / 10); putchar(x % 10 + '0'); } double dfs(int m, int n) { if (!m) return 1.0 / (n + 1); if (!n) return 1; if (vis[m][n]) return f[m][n]; vis[m][n] = 1; return f[m][n] = 1 / (1 + 1.0 / (dfs(n, m - 1) * (n + 1))) * n / (n + 1) * (1 - dfs(n - 1, m)) + 1 - 1 / (1 + 1.0 / (dfs(n, m - 1) * (n + 1))); } signed main() { m = read(), n = read(); printf("%.10lf %.10lf\n", dfs(m, n), 1 - dfs(m, n)); return 0; }
2,700
CPP
from collections import * import sys # "". join(strings) def ri(): return int(input()) def rl(): return list(map(int, input().split())) n = ri() aa = rl() zero_cnt = 0 aa = [value for value in aa if value != 0] n = len(aa) edges = set() graph = defaultdict(list) bit_shared = defaultdict(list) #2^60 > 10^18 >= aa[i]. So we have 60 bits to check, between 0 and 59. #Also note that for a given bit, if 3 numbers share it, then the answer is 3 for k in range(60): for i in range(n): if (1 << k) & aa[i]: bit_shared[k].append(i) if len(bit_shared[k]) >= 3: print(3) sys.exit() # print("bit: ", bit_shared) #we build the graph for i in range(n): for j in range(i + 1, n): if aa[i] & aa[j]: if (i,j) not in edges: graph[i].append(j) graph[j].append(i) edges.add((i,j)) # we need a set, because sometimes many edges between same 2 vertices, but they dont count as cycle according to: #ANNOUCEMENT : Note that the graph does not contain parallel edges. Therefore, one edge can not be a cycle. A cycle should contain at least three distinct vertices. # print("graph: ",graph) # print("edges: ",edges) #if we havent exited yet, it means for all bit 2^k, it is shared at most between two elements of aa. #We have to find the shortest length cycle of this graph. #I think we can chose to iterate over edges (there are less than 60 of them) #Or we can iterate over vertices (there are less of 60*3 + 1 = 183 of them because of pigeonhole principle) #let's iterate of vertices v and use BFS to find the shortest cycle containing v ans = 10**5 # For all vertices for i in range(n): # Make distance maximum dist = [int(1e5)] * n # Take a imaginary parent par = [-1] * n # Distance of source to source is 0 dist[i] = 0 q = deque() # Push the source element q.append(i) # Continue until queue is not empty while q: # Take the first element x = q[0] q.popleft() # Traverse for all it's childs for child in graph[x]: # If it is not visited yet if dist[child] == int(1e5): # Increase distance by 1 dist[child] = 1 + dist[x] # Change parent par[child] = x # Push into the queue q.append(child) # If it is already visited elif par[x] != child and par[child] != x: ans = min(ans, dist[x] + dist[child] + 1) if ans == 10**5: print(-1) else: print(ans)
1,900
PYTHON3
b_cnt, res = 0, 0 mod = 10 ** 9 + 7 for c in input()[::-1]: if c == 'b': b_cnt += 1 else: res += b_cnt b_cnt *= 2 res %= mod b_cnt %= mod print(res)
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int res; string s; cin >> s; int n = s.length(); res = 2 * (pow(2, n - 1) - 1); for (int i = 0; i < 9; i++) { if (s[i] == '4') { } else { res += pow(2, n - i - 1); } } cout << res + 1; }
1,100
CPP
def solve(n, a, b): need = n * 6 orig_a, orig_b = a, b if a < b: a, b = b, a y = b best_prod = None best = (None, None) while True: x, modulo = divmod(need, y) if modulo: x += 1 if x < a: x = a prod = x * y if not best_prod or best_prod > prod: best_prod = prod best = (x, y) if best_prod == need: break y += 1 if x < y: break if x < a or y < b: break x, y = best if x < orig_a or y < orig_b: best = (y, x) return best if __name__ == '__main__': n, a, b = list(map(int, input().split())) x, y = solve(n, a, b) print(x * y) print(x, y)
2,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[92][92]; int main() { int k, n = 92, i, j, l; scanf("%d", &k); for (i = 0; i < 29; i++) { int x = i * 3 + 2; a[x][x + 3] = a[x + 3][x] = 1; for (j = 1; j < 3; j++) { for (l = 4; l < 6; l++) { a[x + j][x + l] = a[x + l][x + j] = 1; } } } for (i = 0; i < 30; i++) { if ((k >> i) & 1) a[(29 - i) * 3 + 2][(29 - i) * 3 + 3] = a[(29 - i) * 3 + 3][(29 - i) * 3 + 2] = 1; } a[0][2] = a[2][0] = 1; a[1][90] = a[90][1] = a[1][91] = a[91][1] = 1; printf("%d\n", n); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (a[i][j] == 1) { putchar('Y'); } else { putchar('N'); } } puts(""); } return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; double PI = 3.141592653589793; const double EPS = 1e-6; const int N = 1e6 + 5; const long long mod = 1e9 + 7; const int oo = 1e9; int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}; int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; int n; int t[N]; int l, r; struct node { int g, mn; }; node seg[N]; int GCD(int a, int b) { if (b == 0) return a; return GCD(b, a % b); } void build(int p, int s, int e) { if (s == e) { seg[p].g = seg[p].mn = t[s]; return; } build(2 * p, s, (s + e) / 2); build(2 * p + 1, (s + e) / 2 + 1, e); seg[p].g = GCD(seg[2 * p].g, seg[2 * p + 1].g); seg[p].mn = min(seg[2 * p].mn, seg[2 * p + 1].mn); } node get(int p, int s, int e) { node ret; ret.mn = -1; ret.g = -1; if (s > r || e < l) return ret; if (s >= l && e <= r) return seg[p]; node L = get(2 * p, s, (s + e) / 2); node R = get(2 * p + 1, (s + e) / 2 + 1, e); if (L.mn == -1) ret = R; else if (R.mn == -1) ret = L; else { ret.g = GCD(L.g, R.g); ret.mn = min(L.mn, R.mn); } return ret; } bool can(int len) { for (int i = 0; i < n && i + len < n; i++) { l = i, r = i + len; node x = get(1, 0, n - 1); if (x.g == x.mn) return 1; } return 0; } int main() { scanf("%d", &n); for (int i = 0; i <= n - 1; i++) scanf("%d", &t[i]); build(1, 0, n - 1); int len = 0; int L = 0, R = n - 1; while (L <= R) { int m = (L + R) / 2; if (can(m)) { L = m + 1; len = m; } else R = m - 1; } vector<int> ans; for (int i = 0; i < n && i + len < n; i++) { l = i, r = i + len; node x = get(1, 0, n - 1); if (x.g == x.mn) ans.push_back(i + 1); } printf("%d %d\n", ans.size(), len); for (int i = 0; i < ans.size(); i++) printf("%d ", ans[i]); }
2,000
CPP
#include <bits/stdc++.h> using namespace std; int n, m; pair<int, int> ar[500010]; int mr[500010]; vector<pair<int, int> > v; int up[20][500010]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> ar[i].first >> ar[i].second; mr[ar[i].first] = max(mr[ar[i].first], ar[i].second); } for (int i = 1; i < 500010; i++) { mr[i] = max(mr[i], mr[i - 1]); } for (int i = 0; i < 500010; i++) { if (mr[i] <= i) continue; up[0][i] = mr[i]; } for (int i = 1; i < 20; i++) { for (int j = 0; j < 500010; j++) { if (up[i - 1][j] <= j) continue; up[i][j] = up[i - 1][up[i - 1][j]]; } } while (m--) { int a, b; cin >> a >> b; int ans = 0; for (int i = 19; i >= 0; i--) { if (up[i][a] <= a) continue; if (up[i][a] <= b) { ans += (1 << i); a = up[i][a]; } } if (up[0][a] >= b && a < b) cout << ans + 1 << '\n'; else if (a < b) cout << -1 << '\n'; else cout << ans << '\n'; } return 0; }
2,200
CPP
from bisect import bisect_left as bl from bisect import bisect_right as br import heapq import math from collections import * from functools import reduce,cmp_to_key import sys input = sys.stdin.readline M = mod = 998244353 def factors(n):return sorted(list(set(reduce(list.__add__,([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))) def inv_mod(n):return pow(n, mod - 2, mod) def li():return [int(i) for i in input().rstrip('\n').split(' ')] def st():return input().rstrip('\n') def val():return int(input().rstrip('\n')) def li2():return [i for i in input().rstrip('\n').split(' ')] def li3():return [int(i) for i in input().rstrip('\n')] # print(inv_mod(2)) # for i in range(1,100): # if (inv_mod(i)*3)%mod == 124780545:print(i) # for _ in range(val()): # n, m = li() # a = li() # b = li() # s = set() # ans = j = 0 # for i in b: # if i not in s: # while a[j]!= i: # s.add(a[j]) # j += 1 # ans += 2*len(s) # ans += 1 # j += 1 # else: # s.remove(i) # ans += 1 # print(ans) for _ in range(val()): n, s = li() l = li() su = 0 currmax = -float('inf') i = 0 d = {} while i<n: if s - l[i] >= 0: s -= l[i] else:break currmax = max(currmax,l[i]) d[l[i]] = i i+=1 if i == n: print(0) continue currlen = i currmax = max(currmax,l[i]) s += currmax ans = currmax d[l[i]] = i i += 1 while i<n: if s - l[i] >= 0: s -= l[i] else:break i += 1 print(d[ans] + 1)
1,300
PYTHON3
for _ in range(int(input())): n = int(input()) l = list(map(int,input().split())) s = set() for i in range(n): if (i+l[i%n])%n not in s: s.add((i+l[i%n])%n) else: print('NO') break else: print('YES')
1,600
PYTHON3
#include <bits/stdc++.h> using namespace std; char sochuoi(int k) { return k + 48; } int main() { int n, d, i, k, j; string s = "123456789", sa; for (i = 10; i <= 400; i++) { k = i; sa = ""; while (k != 0) { int du = k % 10; k = k / 10; sa += sochuoi(du); } for (j = sa.size() - 1; j >= 0; j--) s += sa[j]; } cin >> n; cout << s[n - 1]; return 0; }
800
CPP
from __future__ import division, print_function import os import sys from io import BytesIO, IOBase def main(): # every point must have unique x and unique y # (they can't be on the same row or column) # they must not fit into pitagoras equation # (no alternate coordinate in a pair of points can be multiple of 3 and 4) n, m = map(lambda x: int(x), input().split()) x, y = n, 0 k = 0 points = [] while 0 <= x - k <= n and 0 <= y + k <= m: points.append((x - k, y + k)) k += 1 print(len(points)) print('\n'.join( map(lambda point: f'{point[0]} {point[1]}', points) )) BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") input = lambda: sys.stdin.readline().rstrip("\r\n") def print(*args, **kwargs): """Prints the values to a stream, or to sys.stdout by default.""" sep = kwargs.pop("sep", " ") file = kwargs.pop("file", sys.stdout) atStart = True for x in args: if not atStart: file.write(sep) file.write(str(x)) atStart = False file.write(kwargs.pop("end", "\n")) if kwargs.pop("flush", False): file.flush() sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) main()
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; struct CHT { struct Line { long long slope, yi; Line(long long slope, long long yi) : slope(slope), yi(yi) {} long long calc(long long x) { return slope * x + yi; } long long intersect(Line b) { return ceil((1.0 * b.yi - yi) / (slope - b.slope)); } }; deque<pair<Line, long long>> dq; void insert(long long slope, long long yi) { Line l(slope, yi); while (dq.size() > 1 && dq.back().second <= l.intersect(dq.back().first)) { dq.pop_back(); } if (dq.empty()) { dq.push_back({l, yi}); } else { dq.push_back({l, l.intersect(dq.back().first)}); } } long long query(long long x) { while (dq.size() > 1) { if (dq[0].first.calc(x) <= dq[1].first.calc(x)) dq.pop_front(); else break; } return dq[0].first.calc(x); } }; const int maxN = 1e6 + 5; long long table[maxN]; CHT cht; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; tuple<long long, long long, long long> data[n]; for (int x = 0; x < n; x++) { long long a, b, c; cin >> a >> b >> c; data[x] = make_tuple(a, b, c); } sort(data, data + n); cht.insert(0, 0); long long ans = 0; for (int x = 0; x < n; x++) { long long a = get<0>(data[x]), b = get<1>(data[x]), c = get<2>(data[x]); table[x] = cht.query(b) + (a * b) - c; ans = max(ans, table[x]); cht.insert(-a, table[x]); } cout << ans << "\n"; return 0; }
2,400
CPP
def main(): k = int(input()) s = list(reversed(input())) t = list(reversed(input())) st = [0] * (k+1) carry = 0 for pos in range(0, k): s_pos = ord(s[pos]) - ord('a') t_pos = ord(t[pos]) - ord('a') total = s_pos + t_pos + carry carry = total // 26 total %= 26 st[pos] = total st[k] = carry st = list(reversed(st)) for pos in range(0, len(st)): rem = st[pos] % 2 st[pos] //= 2 if pos < len(st)-1: st[pos+1] += rem * 26 else: assert(rem == 0) print(''.join([chr(st[pos]+ord('a')) for pos in range(1, len(st))])) if __name__ == '__main__': main()
1,900
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, M; int a[100020]; bool check(long long m) { int c = 0; long long t = 0; for (int i = 1; i <= n; i++) { t--; if (t < 0) { t = 0; } int u = a[i]; while (u > t) { u -= t; c++; if (c > M) { return false; } t = m - i; if (t < 0) { return false; } } t -= u; } return true; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> M; for (int i = 1; i <= n; i++) { cin >> a[i]; } long long l = 0, r = 1e15; while (l < r - 1) { long long m = (l + r) / 2; if (check(m)) r = m; else l = m; } cout << r << endl; return 0; }
2,200
CPP
#include <bits/stdc++.h> using namespace std; void yash() { long long n; cin >> n; long long arr[n + 1]; for (long long i = 1; i <= n; i++) cin >> arr[i]; if (n == 1) { cout << "YES" << endl; return; } long long pos[n + 1], vis[n + 1]; for (long long i = 1; i <= n; i++) { pos[arr[i]] = i; vis[i] = 0; } vis[0] = 0; vis[pos[1]] = 1; for (long long i = 2; i <= n; i++) { if (pos[i - 1] + 1 > n) { vis[pos[i]] = 1; } else { if (pos[i - 1] + 1 != pos[i] && vis[pos[i - 1] + 1] == 0) { cout << "NO" << endl; return; } else { vis[pos[i]] = 1; } } } cout << "YES" << endl; return; } int32_t main() { long long t; cin >> t; while (t--) yash(); return 0; }
1,500
CPP
t=int(input()) while t: t=t-1 s,a,b,c = map(int,input().split()) k=s//c ans=k+(k//a)*b print(ans)
800
PYTHON3
q = int(input()) for qq in range(q): n = int(input()) if n <= 4: print(4 - n) else: print(((n+1)//2)*2-n)
800
PYTHON3
n = [int(i) for i in input().split()] div_1 = 0 div_2 = 0 if n[0] == n[1] == n[2] or (n[2]>n[0] and n[2] > n[1]): print (1) elif n[2] > n[1] or n[2] > n[0]: if n[2] > n[0]: div_1 = n[1]/n[2] if int(div_1) == div_1: print(int(div_1)) else: div_1 = int(div_1) div_1 += 1 print(div_1) else: div_2 = n[0]/n[2] if int(div_2) == div_2: print(int(div_2)) else: div_2 = int(div_2) div_2 += 1 print(div_2) else: div_1 = n[1] / n[2] div_2 = n[0] / n[2] if div_1 != int(div_1): div_1 = int(div_1) div_1 += 1 if div_2 != int(div_2): div_2 = int(div_2) div_2 += 1 print(int(div_1*div_2))
1,000
PYTHON3