solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
q = int(input()) for i in range(q): n, k = map(int, input().split()) a = list(map(int, input().split())) if (max(a) - min(a)) <= k*2: print(min(a) + k) else: print(-1)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5, inf = 1e9 + 7; const long double eps = 1e-12; const long long linf = (long long)1e18 + 7, mod = 1e14; int main() { long long n; cin >> n; long long f[n + 5]; f[1] = 1; f[2] = 2; for (int i = 3; i <= n; i++) { f[i] = f[i - 1] + f[i - 2]; } for (int i = 1; i <= n; i++) { bool ok = true; for (int j = 1; j <= n; j++) { if (f[j] == i) { ok = false; cout << "O"; break; } } if (ok) { cout << "o"; } } return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1e2 + 2; int n; int x[2][4]; char table; int ans; inline int getans(int now, int i, int j) { if (now >= 4) return 0; int rtn = INT_MAX; if (i != 2) rtn = min(rtn, getans(now + 1, i + 1, j) + x[0][now]); if (j != 2) rtn = min(rtn, getans(now + 1, i, j + 1) + x[1][now]); return rtn; } int main() { scanf("%d", &n); for (int k = 0; k < 4; k++) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { scanf("%1s", &table); if (((i + j) & 1) == table - '0') x[0][k]++; } x[1][k] = n * n - x[0][k]; } ans = getans(0, 0, 0); printf("%d\n", ans); return 0; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; long double PI = acosl(-1); bool compare_int(int a, int b) { return (a > b); } bool compare_string(string a, string b) { return a.size() < b.size(); } bool compare_pair(const pair<int, int> &a, const pair<int, int> &b) { if (a.second == b.second) return a.first > b.first; else return (a.second > b.second); } long long int fact(long long int n) { if (n == 0 || n == 1) return 1; else return n * fact(n - 1); } int cnt[200005]; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s, s1; s1 = "."; int n, q, x; cin >> s >> n; s1 += s; int l = s1.size(); for (int i = 0; i < n; i++) { cin >> x; cnt[x]++; cnt[l - x + 1]++; } for (int i = 1; i < l; i++) cnt[i] += cnt[i - 1]; for (int i = 1; i < l; i++) { if (cnt[i] % 2) cout << s1[l - i]; else cout << s1[i]; } }
1,400
CPP
#include <bits/stdc++.h> using namespace std; string a, b; int x[200000]; bool can(int f) { string g = a; for (int i = 0; i < f; i++) { g[x[i] - 1] = ' '; } int w = 0; for (int i = 0; i < g.size(); i++) { if (g[i] == b[w]) w++; if (w == b.size()) break; } if (w == b.size()) return 1; else return 0; } int main() { cin >> a >> b; for (int i = 0; i < a.size(); i++) cin >> x[i]; int l = 0, mid, k = 0; ; int r = a.size() - 1; while (l <= r) { mid = (l + r) / 2; if (can(mid)) { k = mid; l = mid + 1; } else { r = mid - 1; } } cout << k << endl; return 0; }
1,700
CPP
d=int(input()) m=int(input()) a=list(map(int,input().split()))[:m] count=0 for i in range(len(a)-1): count+=d-a[i] print(count)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; string S; char tablica[2][14]; int t1, t2; bool check() { for (int i = 0; i < 27; i++) { for (int j = i + 1; j < 27; j++) { if (S[i] == S[j]) { t1 = i; t2 = j; } } } if (abs(t1 - t2) == 1) return true; else return false; } int main(void) { cin >> S; if (check()) { printf("Impossible"); } else { tablica[0][(t2 - t1 - 1) / 2] = S[t1]; int it = (t2 - t1 - 1) / 2 - 1, x = t1 + 1; for (; x < S.size() && it >= 0; it--, x++) { tablica[0][it] = S[x]; } it = 0, x = (t1 + t2 + 1) / 2; for (; x < t2; x++, it++) { tablica[1][it] = S[x]; } int k = 1; x = t2 + 1; for (; x < S.size() && it < 13; it++, x++) { tablica[k][it] = S[x]; } if (it == 13) { it--; k = 0; } for (; x < S.size() && it >= 0; x++, it--) { tablica[k][it] = S[x]; } x = 0; if (k == 1) { for (; x < t1 && it < 13; it++, x++) { tablica[k][it] = S[x]; } it--; k = 0; } for (; it >= 0 && x < t1; it--, x++) { tablica[k][it] = S[x]; } for (int i = 0; i < 2; i++) { for (int j = 0; j < 13; j++) { cout << tablica[i][j]; } cout << "\n"; } } return 0; }
1,600
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; double avg, sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum = sum + a[i]; } avg = sum / n; cout << avg; }
800
CPP
weight = int(input()) print("YES" if weight > 3 and weight % 2 == 0 else "NO")
800
PYTHON3
s=input() z=sorted(set(s), key=s.index); n=len(z) x = 'YES' for i in range(n): if ord(z[i])-97 != i: x = 'NO' print(x)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; const int md = 1000000007; const int maxn = 201010; const int maxn4 = 4 * maxn; const long long inf = 2020202020202020202LL; vector<pair<int, int>> t[4 * maxn]; vector<int> maxpot[4 * maxn]; pair<int, int> a[maxn], ar[maxn], xx[maxn]; int maxri[maxn], minle[maxn]; int n, m; void build(pair<int, int> a[], int v, int tl, int tr) { if (tl == tr) t[v] = vector<pair<int, int>>( 1, make_pair(a[tl].second, a[tl].second - a[tl].first)); else { int tm = (tl + tr) / 2; build(a, v * 2, tl, tm); build(a, v * 2 + 1, tm + 1, tr); merge(t[v * 2].begin(), t[v * 2].end(), t[v * 2 + 1].begin(), t[v * 2 + 1].end(), back_inserter(t[v])); } maxpot[v].resize(t[v].size()); maxpot[v][0] = t[v][0].second; for (int i = 1; i < t[v].size(); i++) maxpot[v][i] = max(maxpot[v][i - 1], t[v][i].second); } long long query(int v, int tl, int tr, int l, int r, int y) { if (l > r) return 0; if (l == tl && tr == r) { vector<pair<int, int>>::iterator pos = lower_bound((t[v]).begin(), (t[v]).end(), make_pair(y, md)); if (pos == t[v].begin()) return 0; int ps = pos - t[v].begin(); ps--; return maxpot[v][ps]; } int tm = (tl + tr) / 2; return max(query(v * 2, tl, tm, l, min(r, tm), y), query(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, y)); } int main() { cin >> n >> m; for (int i = 0; i < n; i++) { int u, v; scanf("%d %d", &u, &v); a[i] = make_pair(u, v); xx[i].first = a[i].first; xx[i].second = a[i].second; ar[i].first = a[i].second; ar[i].second = a[i].first; } sort(a, a + n); sort(ar, ar + n); for (int i = 0; i < n; i++) { if (i == 0) { maxri[i] = a[i].second; continue; } maxri[i] = max(maxri[i - 1], a[i].second); } for (int i = n - 1; i >= 0; i--) { if (i == n - 1) { minle[i] = ar[i].second; continue; } minle[i] = min(minle[i + 1], ar[i].second); } build(a, 1, 0, n - 1); long long ans = 0; int ansi, ansj, ansx, ansy, ansc; for (int i = 0; i < m; i++) { int x, y, c; scanf("%d %d %d", &x, &y, &c); long long res = 0; int lb = lower_bound(a, a + n, make_pair(x, md)) - a; if (lb != 0) { lb--; int ri = maxri[lb]; ri = min(ri, y); res = ri - x; res = (long long)res * c; lb++; } if (ans < res) { ansj = i + 1; ansx = x; ansy = y; ansc = c; } ans = max(ans, res); res = 0; int rb = lower_bound(ar, ar + n, make_pair(y, -1)) - ar; if (rb != n) { int le = minle[rb]; le = max(le, x); res = y - le; res = (long long)res * c; } if (ans < res) { ansj = i + 1; ansx = x; ansy = y; ansc = c; } ans = max(res, ans); res = 0; res = query(1, 0, n - 1, lb, n - 1, y); res = (long long)res * c; if (ans < res) { ansj = i + 1; ansx = x; ansy = y; ansc = c; } ans = max(res, ans); } cout << ans << endl; if (ans) { for (int i = 0; i < n; i++) { long long d, res = 0; int a = xx[i].first; int b = xx[i].second; int x = ansx; int y = ansy; if (a > x) { swap(a, x); swap(b, y); } int v = min(b, y); v = v - x; res = v; res = (long long)res * ansc; if (res == ans) { ansi = i + 1; break; } } cout << ansi << " " << ansj; } return 0; }
2,400
CPP
#include <bits/stdc++.h> using namespace std; inline long long gcd(long long a, long long b) { return (b ? gcd(b, a % b) : a); } inline long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; } const int MAXN = 100005; int n, m; int pai[MAXN], desejo[MAXN], nivel[MAXN], dadoA[MAXN]; vector<int> filho[MAXN]; vector<int> ans; void GeraNivel(int node, int nvl) { nivel[node] = nvl; for (int i = 0; i < filho[node].size(); i++) { GeraNivel(filho[node][i], nvl + 1); } } void dfs(int node, int man) { if (dadoA[node] != -1) return; dadoA[node] = man; for (int i = 0; i < filho[node].size(); i++) { dfs(filho[node][i], man); } } int main() { cin >> n >> m; for (int i = 0; i < MAXN; i++) pai[i] = i; for (int i = 0, x, y; i < m; i++) { cin >> x >> y; pai[y] = x; filho[x].push_back(y); } for (int i = 1; i <= n; i++) { if (pai[i] == i) GeraNivel(i, 0); } vector<pair<int, int> > v; for (int i = 1; i <= n; i++) { cin >> desejo[i]; v.push_back(make_pair(nivel[desejo[i]], desejo[i])); } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); memset(dadoA, -1, sizeof dadoA); for (int i = 0; i < v.size(); i++) { if (dadoA[v[i].second] == -1) { ans.push_back(v[i].second); dfs(v[i].second, v[i].second); } } bool ok = true; for (int i = 1; i <= n; i++) { if (desejo[i] != dadoA[i]) { ok = false; } } if (ok) { cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) cout << ans[i] << "\n"; } else { cout << "-1\n"; } }
2,000
CPP
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:1024000000,1024000000") const int N = 30005, M = 100005; const int inf = 0x3f3f3f3f; const long long INF = 1000000000000000000ll; const long long mod = 998244353; const double pi = acos(-1.0); const double eps = 1e-6; long long vis[4000 * 9 + 5]; long long sum[4005]; int main() { std::ios::sync_with_stdio(false); cin.tie(0); long long a; cin >> a; string str; cin >> str; int siz = str.size(); sum[1] = str[0] - '0'; for (int i = 2; i <= siz; i++) { sum[i] = str[i - 1] - '0' + sum[i - 1]; } long long all = 0; for (int len = 1; len <= siz; len++) { for (int i = 1, j; i + len - 1 <= siz; i++) { j = i + len - 1; long long tmp = sum[j] - sum[i - 1]; vis[tmp]++; all++; } } if (a == 0) { cout << 2 * vis[0] * (all - vis[0]) + vis[0] * vis[0] << endl; return 0; } long long ans = 0; for (int i = 1; i * i <= a; i++) { if (vis[i] && a % i == 0 && a / i <= 36000 && vis[a / i]) { ans += vis[i] * vis[a / i] * (i * i == a ? 1 : 2); } } cout << ans << endl; }
1,600
CPP
n=int(input()) arr=list(map(int,input().split())) for i in range(n): if arr[i]==1: print("HARD") exit() print("EASY")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int modA[1000001], modB[1000001], digit[1000001], i, j, n, mod, mult, a, b; char c; int main() { i = 0; while (true) { scanf("%c", &c); if (c == '\n') break; digit[i++] = c - '0'; } n = i; scanf("%d %d", &a, &b); mod = 0; for (i = 0; i < n; i++) { modA[i] = (mod * 10 + digit[i]) % a; mod = modA[i]; } mod = 0; mult = 1; for (i = n - 1; i > 0; i--) { mult %= b; modB[i] = (digit[i] * mult + mod) % b; if (modB[i] == 0 && modA[i - 1] == 0 && digit[i] != 0) break; mult *= 10; mod = modB[i]; } if (i == 0) printf("NO\n"); else { printf("YES\n"); for (j = 0; j < i; j++) printf("%d", digit[j]); printf("\n"); while (i < n) printf("%d", digit[i++]); printf("\n"); } return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while (T--) { long long int s, a, b, c; cin >> s >> a >> b >> c; long long int sum = 0, r = 0; sum = (s / c); r = (sum / a) * b + sum; cout << r << endl; } }
800
CPP
#include <bits/stdc++.h> using namespace std; vector<int> m(200001); void Print(int pick, int ans) { if (m[pick] == 0) { Print(pick + 1, ans); return; } if (pick == ans) { for (int i = 0; i < m[pick]; ++i) cout << pick << ' '; } else { cout << pick << ' '; Print(pick + 1, ans); for (int i = 1; i < m[pick]; ++i) cout << pick << ' '; } } int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { int x; cin >> x; m[x]++; } int ans = 0; pair<int, int> value = {0, 0}; int cur_shit = 0; int cur_fuck = 0; for (int i = 0; i <= 200001; ++i) { if (m[i] == 0) { cur_shit = 0; } else if (m[i] == 1) { cur_shit++; if (ans < cur_shit) { ans = cur_shit; cur_fuck = i; } cur_shit = 1; } else if (m[i] >= 2) { cur_shit += m[i]; if (ans < cur_shit) { ans = cur_shit; cur_fuck = i; } } } cout << ans << endl; cur_shit = cur_fuck; do { cur_fuck--; } while (m[cur_fuck] != 0 && m[cur_fuck] != 1); Print(cur_fuck, cur_shit); }
2,000
CPP
# 3/2/2020, 8:23 PM # edited to just edit the parent array import math import sys import operator def raise_d(parent): # find the deepest depth that has more than one vertex just_parent = [i for i, j in parent] for deep in range(n): if depth_track[-i - 1] >= 2: break deep_nodes = [] c = 0 for p, depth in parent[::-1]: if depth == deep: deep_nodes.append(n - c) if len(deep_nodes) == 2: break c += 1 # deep_nodes contains vertices at depth = deep for NC in deep_nodes: if just_parent.count(NC) < 2: C ,= [x for x in deep_nodes if x != NC] # now we have NC with at most one child and C the other node # we just have to move C to be a child of NC parent[C] = NC, deep + 1 depth_track[deep + 1] += 1 depth_track[deep] -= 1 return parent T = int(input()) for _ in range(T): # 2 cases: one where we use as little vertices and # another where we fill us levels are much as we can n, d = map(int, input().split()) # upper bound ub = n*(n - 1)//2 # calculating the lower bound count = n depth = 0 lb = 0 while count > 0: depth_count = 2**depth fill = min(count, depth_count) count -= fill lb += fill*depth depth += 1 # testing d against bounds if not (lb <= d <= ub): print ('NO') continue # create an array to keep track of how many nodes are at each depth depth_track = [0]*(n) # create the parent array for lower bound parent = [(-1, -1) for i in range(n + 1)] for i in range(1, n + 1): if i % 2 == 1: p = (i - 1)//2 depth = len(bin(i)) - 3 parent[i] = (p, depth) depth_track[depth] += 1 else: p = i//2 depth = len(bin(i)) - 3 parent[i] = (p, depth) depth_track[depth] += 1 # note that we will only increase the depth of a node, never decrease def raise_d(parent): # find the deepest depth that has more than one vertex just_parent = [i for i, j in parent] for deep in range(n - 1, -1, -1): if depth_track[deep] >= 2: break deep_nodes = [] c = 0 for p, depth in parent[::-1]: if depth == deep: deep_nodes.append(n - c) if len(deep_nodes) == 2: break c += 1 # deep_nodes contains vertices at depth = deep for NC in deep_nodes: if just_parent.count(NC) == 0: C ,= [x for x in deep_nodes if x != NC] break # now we have NC with at most one child and C the other node # we just have to move NC to be a child of C parent[NC] = C, deep + 1 depth_track[deep + 1] += 1 depth_track[deep] -= 1 return parent for i in range(d - lb): parent = raise_d(parent) print ('YES') # making an array so that vertex v at index v - 1 has its parent node there parent = [p for p, depth in parent if depth != 0 and depth != -1] print (*parent)
2,200
PYTHON3
import math n, x = map(int, input().split(' ')) limit = int(math.sqrt(x)) + 1 res = 0 for div in range(1, limit): if x % div == 0 and div <= n and x // div <= n: res += 2 #if it is a perfect square if int(math.sqrt(x)) == math.sqrt(x) and math.sqrt(x) <= n: res -= 1 print(res)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int a[N], sum[N]; int ans1[N], ans2[N]; int main() { int h; scanf("%d", &h), ++h; for (int i = 1; i <= h; ++i) scanf("%d", &a[i]); int flg = 1; for (int i = 1; i < h; ++i) if (a[i] > 1 && a[i + 1] > 1) { flg = 0; break; } if (flg) return puts("perfect"), 0; puts("ambiguous"); for (int i = 1; i <= h; ++i) sum[i] = sum[i - 1] + a[i]; ans1[1] = 0; for (int i = 2; i <= h; ++i) for (int k = sum[i - 1] + 1; k <= sum[i]; ++k) ans1[k] = sum[i - 2] + 1; ans2[1] = 0; for (int i = 2; i <= h; ++i) { ans2[sum[i - 1] + 1] = sum[i - 2] + 1; for (int k = sum[i - 1] + 2; k <= sum[i]; ++k) ans2[k] = sum[i - 1]; } int n = sum[h]; for (int i = 1; i <= n; ++i) printf("%d%c", ans1[i], i == n ? '\n' : ' '); for (int i = 1; i <= n; ++i) printf("%d%c", ans2[i], i == n ? '\n' : ' '); return 0; }
1,500
CPP
#include <bits/stdc++.h> using namespace std; void solve() { int n, m; cin >> n >> m; vector<int> A(n); vector<int> B(m); for (auto &x : A) cin >> x; for (auto &x : B) cin >> x; for (int b = 0; b < (1 << 9); b++) { int flag2 = 1; for (int i = 0; i < n; i++) { int flag = 1; for (int j = 0; j < m; j++) { if (((A[i] & B[j]) & b) == (A[i] & B[j])) { flag = 0; break; } } if (flag) { flag2 = 0; break; } } if (flag2) { cout << b << "\n"; return; } } } signed main() { ios::sync_with_stdio(0); cin.tie(0); solve(); }
1,600
CPP
n=int(input()) d=1 l=[1,1] i=2 a=l[1] while(True): a=l[i-2]+l[i-1] if(a<=1000): l.append(a) else: break i=i+1 for i in range(1,n+1): if i in l: print("O",end="") else: print("o",end="")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; inline long long mod(long long n, long long m) { long long ret = n % m; if (ret < 0) ret += m; return ret; } long long gcd(long long a, long long b) { return (b == 0LL ? a : gcd(b, a % b)); } long long exp(long long a, long long b, long long m) { if (b == 0LL) return 1LL; if (b == 1LL) return mod(a, m); long long k = mod(exp(a, b / 2, m), m); if (b & 1LL) { return mod(a * mod(k * k, m), m); } else return mod(k * k, m); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; string s; cin >> s; int n = s.size(); vector<int> d1(n), d2(n), res(n); for (int i = 0, l = 0, r = -1; i < n; i++) { int k = (i > r) ? 1 : min(d1[l + r - i], r - i + 1); while (0 <= i - k && i + k < n && s[i - k] == s[i + k]) { k++; } d1[i] = k--; if (i + k > r) { l = i - k; r = i + k; } } for (int i = 0, l = 0, r = -1; i < n; i++) { int k = (i > r) ? 0 : min(d2[l + r - i + 1], r - i + 1); while (0 <= i - k - 1 && i + k < n && s[i - k - 1] == s[i + k]) { k++; } d2[i] = k--; if (i + k > r) { r = i + k; l = i - k - 1; } } int ans = 0; for (int i = 0; i < n; i++) { if (i & 1) { int c = (i + 1) / 2; int t = d2[c]; if (2 * t == i + 1) { res[i] = 1 + res[c - 1]; } else res[i] = 0; } else { int c = i / 2; int t = d1[c]; if (2 * t - 1 == i + 1) { res[i] = 1 + (c == 0 ? 0 : res[c - 1]); } else res[i] = 0; } ans += res[i]; } cout << ans << "\n"; }
2,200
CPP
n=list(map(int,input().split())) r1=min(n[0],n[1]) d=max(n[0],n[1])-min(n[0],n[1]) c=0 while d>1: d=d-2 c=c+1 print(r1,end=" ") print(c)
800
PYTHON3
import sys input = sys.stdin.readline def main(): Q = int(input()) data = [] for _ in range(Q): N, X = map(int, input().split()) D = [] A = [] for _ in range(N): d, h = map(int, input().split()) D.append(d-h) A.append(d) data.append((N, X, D, A)) for N, X, D, A in data: d = max(D) b = max(A) score = X-b if score <= 0: print(1) elif d <= 0: print(-1) else: c = score//d + 1 if score % d != 0: c += 1 print(c) if __name__ == "__main__": main()
1,600
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n, m, dx[] = {0, 0, 1, 1, 1, -1, -1, -1}, dy[] = {1, -1, 1, 0, -1, 1, 0, -1}; vector<string> a; vector<vector<long long>> w; vector<vector<long long>> dist; bool check(long long x, long long y) { return x >= 0 && y >= 0 && x <= n + 1 && y <= m + 1; } bool checkT(long long T) { queue<pair<long long, long long>> q; for (long long i = 0; i < n + 2; ++i) { fill(dist[i].begin(), dist[i].end(), 1e9); for (long long j = 0; j < m + 2; ++j) { if (w[i][j] > T) { dist[i][j] = 0; q.push({i, j}); } } } while (!q.empty()) { long long x = q.front().first, y = q.front().second; q.pop(); for (long long i = 0; i < 8; ++i) { long long nx = x + dx[i], ny = y + dy[i]; if (check(nx, ny) && dist[nx][ny] > 1 + dist[x][y]) { dist[nx][ny] = dist[x][y] + 1; q.push({nx, ny}); } } } bool f = 1; for (long long i = 0; i < n + 2; ++i) { for (long long j = 0; j < m + 2; ++j) { if (a[i][j] == 'X' && dist[i][j] > T) f = 0; } } return f; } signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m; a.resize(n + 2); w.resize(n + 2); dist.resize(n + 2); queue<pair<long long, long long>> q; string g; for (long long i = 0; i < m + 2; ++i) g += '.'; for (long long i = 1; i <= n; ++i) { w[i].resize(m + 2); dist[i].resize(m + 2); fill(w[i].begin(), w[i].end(), 1e9); cin >> a[i]; a[i] = '.' + a[i] + '.'; } w[0].resize(m + 2); w[n + 1].resize(m + 2); dist[0].resize(m + 2); dist[n + 1].resize(m + 2); a[0] = g; a[n + 1] = g; for (long long i = 0; i < n + 2; ++i) { for (long long j = 0; j < m + 2; ++j) { if (a[i][j] != 'X') { w[i][j] = 0; q.push({i, j}); } } } while (!q.empty()) { long long x = q.front().first, y = q.front().second; q.pop(); for (long long i = 0; i < 8; ++i) { long long nx = x + dx[i], ny = y + dy[i]; if (check(nx, ny) && w[x][y] + 1 < w[nx][ny]) { w[nx][ny] = w[x][y] + 1; q.push({nx, ny}); } } } long long l = 0, r = max(n + 2, m + 2), med; while (r - l > 1) { med = (r + l) / 2; if (checkT(med)) l = med; else r = med; } long long res = l; if (checkT(r)) res = r; cout << res << '\n'; for (long long i = 1; i <= n; ++i) { for (long long j = 1; j <= m; ++j) { if (w[i][j] > res) cout << 'X'; else cout << '.'; } cout << '\n'; } }
2,200
CPP
#include <bits/stdc++.h> using namespace std; const int MAXn = 200 + 10, MOD = 1e9 + 7; vector<int> g[MAXn]; int n, cnt[MAXn], h[MAXn], p[MAXn]; long long dp[MAXn][MAXn], res, invN, inv2; vector<int> dfsStack; void dfs(int v, int par, int height) { cnt[v] = 1; h[v] = height; p[v] = par; for (int u : g[v]) { if (u != par) { dfs(u, v, height+1); cnt[v] += cnt[u]; } } } void dfs2(int v, int origin) { dfsStack.push_back(v); if (origin > v) { for (int i = 0; i < (int)(dfsStack.size() - 1); i++) { long long tmp = cnt[dfsStack[i]] - cnt[dfsStack[i+1]]; tmp = (tmp * invN) % MOD; tmp = (tmp * dp[i][dfsStack.size() - 1 - i]) % MOD; res = (tmp + res) % MOD; } } for (int u : g[v]) { if (u != p[v]) { dfs2(u, origin); } } dfsStack.pop_back(); } long long pw(long long a, long long b) { long long res = 1; while(b) { if (b & 1) { res = (res * a) % MOD; } a = (a * a)%MOD; b /= 2; } return res; } int main() { cin >> n; for (int i = 0; i < n - 1; i++) { int u, v; cin >> u >> v;u--;v--; g[u].push_back(v); g[v].push_back(u); } inv2=pw(2, MOD-2), invN = pw(n, MOD-2); for (int i = 1; i < MAXn; i++) dp[0][i] = 1; for (int i = 1; i < MAXn; i++) for (int j = 1; j < MAXn; j++) { dp[i][j] = (inv2 * ((dp[i-1][j] + dp[i][j-1]) % MOD)) % MOD; } for (int i = 0; i < n; i++) { dfs(i, -1, 0); dfs2(i, i); } cout << res; }
2,300
CPP
n, k = map(int, input().split()) trees = map(int, input().split()) deltas = [tree-i*k-1 for i, tree in enumerate(trees)] freqs = [0]*1000 for delta in deltas: if delta >= 0: freqs[delta] += 1 d = freqs.index(max(freqs)) print(sum(((delta != d) for delta in deltas))) for i, delta in enumerate(deltas): if delta > d: print('- %d %d' % (i+1, delta-d)) elif delta < d: print('+ %d %d' % (i+1, d-delta))
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long N = 100005; const long long mod = 1e9 + 7; long long mx = -1e18, mn = 1e18; vector<long long> g[N]; pair<long long, long long> edge; vector<pair<long long, long long> > v; long long n; long long dfs(long long cur, long long par = 0) { long long sum = 1; bool isleaf = 1; long long mx = -1e18; for (auto ele : g[cur]) { if (ele != par) { isleaf = 0; long long child_components = dfs(ele, cur); sum += child_components; mx = max(mx, child_components); } } mx = max(mx, n - sum); v.push_back({mx, cur}); if (isleaf) { edge = {cur, par}; } return sum; } long long neglect; pair<long long, long long> e; void dfs1(long long cur, long long par = 0) { bool isleaf = 1; for (auto ele : g[cur]) { if (ele != par && ele != neglect) { isleaf = 0; dfs1(ele, cur); } } if (isleaf) { e = {cur, par}; } } void inp_out() {} int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); inp_out(); long long t; cin >> t; while (t--) { long long i, j, k, m, ans = 0, cnt = 0, sum = 0; cin >> n; for (i = 0; i <= n; i++) { g[i].clear(); } v.clear(); for (i = 0; i < n - 1; i++) { long long x, y; cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } dfs(1); sort(v.begin(), v.end()); if (v[0].first != v[1].first) { cout << edge.first << " " << edge.second << "\n"; cout << edge.first << " " << edge.second << "\n"; } else { neglect = v[0].second; dfs1(v[1].second); cout << e.first << " " << e.second << "\n"; cout << e.first << " " << v[0].second << "\n"; } } return 0; }
1,700
CPP
import math x = int(input()) txt = list(input()) txt1 = txt[::-1] l1 = [] l1.insert(x-1,txt1[0]) if(x!=1): l1.insert(0,txt1[1]) i=2 while(i!=x): if(len(l1)%2==0): n = math.floor(len(l1)/2) l1.insert(n,txt1[i]) else: m = math.floor(len(l1)/2) l1.insert(m,txt1[i]) i=i+1 print (''.join(l1))
900
PYTHON3
from sys import stdin, stdout k = int(stdin.readline().rstrip()) n = int(stdin.readline().rstrip()) nList = list(map(int,list(str(n)))) nList.sort() currentSum = sum(nList) i=0 while currentSum<k: currentSum+=(9-nList[i]) i+=1 print(i)
1,100
PYTHON3
def Soldier_and_Bananas(k, n, w): cost = 0 for i in range(1, w+1): cost += i*k if cost <= n: print(0) else: print(cost - n) if __name__ == '__main__': k,n,w = map(int, input().split()) Soldier_and_Bananas(k, n, w)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<int> v[200005]; int vis[200005]; int ans[200005]; void dfs(int node, int f) { vis[node] = 1; for (int i = 0; i < v[node].size(); i++) { if (vis[v[node][i]]) continue; ans[v[node][i]] += f; dfs(v[node][i], f); } } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int t; cin >> t; while (t--) { int n, m, a, b; cin >> n >> m >> a >> b; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } vis[a] = 1; vis[b] = 1; dfs(a, 1); for (int i = 0; i <= n; i++) { vis[i] = 0; } vis[a] = 1; vis[b] = 1; dfs(b, -1); long long pos = 0, neg = 0; for (int i = 0; i <= n; i++) { if (ans[i] > 0) pos++; else if (ans[i] < 0) { neg++; } } cout << pos * neg << '\n'; for (int i = 0; i <= n; i++) { v[i].clear(); vis[i] = 0; ans[i] = 0; } } return 0; }
1,900
CPP
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const long long INF = 2e9; const long long MOD = 1e9 + 7; const long long MB = 20; void solve() { long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < n; i++) { long long x; cin >> x; a[x - 1] = i; } long long ans = 0; for (long long i = 1; i < n; i++) { ans += abs(a[i] - a[i - 1]); } cout << ans; } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout.precision(12); solve(); }
1,200
CPP
p=input() a=ord('a') z=ord('z') l=0; #aa=ord(p[0]) #if((aa>=a)&(aa<=z)): # kk=0; #else: # kk=1; for i in range (1,len(p),1): aa=ord(p[i]) if((aa>=a)&(aa<=z)): l=l+1; ass=[] if((l==0)): for i in range (0,len(p),1): aa=ord(p[i]) if(((aa>=a)&(aa<=z))): bb=chr(aa-32); ass.append(bb) else: aa=aa+32; bb=chr(aa); ass.append(bb) else: for i in range (0,len(p),1): aa=ord(p[i]) bb=chr(aa); ass.append(bb) print(''.join([str(ass[i]) for i in range (0,len(ass))]))
1,000
PYTHON3
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int,input().split())) for i in range(n): if i % 2 == 0: print(abs(a[i]), end = ' ') else: print(-(abs(a[i])), end = ' ')
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 100000; long long v[N]; long long t[N]; void solve() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> v[i]; } for (int i = 0; i < n; i++) { cin >> t[i]; } priority_queue<long long, vector<long long>, greater<long long>> pq; long long sum = t[0]; pq.push(v[0]); for (int i = 1; i < n; i++) { v[i] += sum; pq.push(v[i]); sum += t[i]; } int cnt = 0; long long sm = 0; for (int i = 0; i < n; i++) { cnt++; long long ans = 0; while (!pq.empty() && pq.top() - sm <= t[i]) { long long xx = pq.top(); pq.pop(); ans += xx - sm; cnt--; } ans += cnt * t[i]; sm += t[i]; cout << ans << " "; } } int main() { solve(); getchar(); getchar(); }
1,600
CPP
#include <bits/stdc++.h> using namespace std; const int RT = 100; int n, a[300030], k, aa[300030]; int main() { scanf("%d %d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", &aa[i]); sort(aa + 1, aa + n + 1); int e = 1; a[1] = aa[1]; for (int i = 2; i <= n; i++) if (aa[i] > aa[i - 1]) a[++e] = aa[i]; if (a[1] <= k + 1) { printf("%d\n", a[1]); return 0; } n = e; a[n + 1] = 1e9; if (k > RT) { int ans = a[1] + 1, j; bool tag = 1; while (tag) { ans--; tag = 0; for (int now = 2 * ans; now <= a[n] + ans && !tag; now += ans) { j = lower_bound(a, a + n + 2, now) - a - 1; if (j && (a[j] % ans > k)) tag = 1; } } printf("%d\n", ans); } else { int ans = a[1] + 1; bool tag = 1; while (tag) { ans--; tag = 0; for (int i = 1; i <= n && !tag; i++) if (a[i] % ans > k) tag = 1; } printf("%d\n", ans); } return 0; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n = (int)s.size(); int jav = 0; int a = 0; int b = 0; for (int i = 0; i < n; i++) { if (s[i] == 'U') { a++; } if (s[i] == 'D') { a--; } if (s[i] == 'R') { b++; } if (s[i] == 'L') { b--; } } if (abs(a) % 2 == 0 && abs(b) % 2 == 0) { jav += (abs(a) + abs(b)) / 2; } if (abs(a) % 2 == 1 && abs(b) % 2 == 1) { jav += (abs(a) + abs(b)) / 2; } if (abs(a) % 2 == 0 && abs(b) % 2 == 1) { jav = -1; } if (abs(a) % 2 == 1 && abs(b) % 2 == 0) { jav = -1; } cout << jav; return 0; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; struct FenwickTree { int counter[101000]; FenwickTree() { memset(counter, 0, sizeof(counter)); } void add(int k, int v) { int idx = k; while (idx <= 100000) { counter[idx] += v; idx += idx & (-idx); } } int cnt(int k) { int ret = 0; while (k) { ret += counter[k]; k -= k & (-k); } return ret; } int cntAt(int k) { return cnt(k) - cnt(k - 1); } int cntUp(int k) { return cnt(100000) - cnt(k); } int cntDown(int k) { return cnt(k - 1); } void remove(int k) { int l = cntAt(k); while (k <= 100000) { counter[k] -= l; k += k & (-k); } } long long findNext(int k) { int l = cnt(k); int s = k + 1, e = 100000; long long r = 1LL * 1000000005 * 1000000005; while (s <= e) { int m = (s + e) / 2; if (cnt(m) > l) { e = m - 1; r = m; } else s = m + 1; } return r; } long long findBef(int k) { int l = cnt(k); int s = 0, e = k - 1; long long r = 1LL * -1000000005 * 1000000005; while (s <= e) { int m = (s + e) / 2; if (cnt(m) < l) { s = m + 1; r = m; } else e = m - 1; } return r; } int sum() { int l = 100000; int ret = 0; while (l) { ret += counter[l]; l -= l & (-l); } return ret; } }; int idno = 0; struct person { int t, s, f; int id; void input() { id = idno++; scanf("%d %d %d", &t, &s, &f); } bool operator<(const person& o) const { return t < o.t; } }; vector<int> waiting[101000]; vector<int> outing[101000]; long long answer[101000]; int main() { FenwickTree inLiftT, waitingT; long long T = 0; int F = 1; vector<person> v; int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { person p; p.input(); v.push_back(p); } sort(v.begin(), v.end()); long long nxtTime = 1LL * 1000000005 * 1000000005; int dir = 0; int i = 0; while (1) { if (i >= n || nxtTime < v[i].t) { F = F + dir * (nxtTime - T); T = nxtTime; for (int k = 0; k < outing[F].size(); k++) { answer[v[outing[F][k]].id] = T; } inLiftT.remove(F); outing[F].clear(); waitingT.remove(F); for (int k = 0; k < waiting[F].size(); k++) { outing[v[waiting[F][k]].f].push_back(waiting[F][k]); inLiftT.add(v[waiting[F][k]].f, 1); } waiting[F].clear(); int sF = waitingT.cntUp(F) + inLiftT.cntUp(F); int dF = waitingT.cntDown(F) + inLiftT.cntDown(F); if (sF == 0 && dF == 0 && i >= n) { break; } else if (sF == 0 && dF == 0) { dir = 0; nxtTime = 1000000005; } else if (sF >= dF) { dir = 1; nxtTime = T + min(inLiftT.findNext(F), waitingT.findNext(F)) - F; } else { dir = -1; nxtTime = T + F - max(inLiftT.findBef(F) + 1, waitingT.findBef(F) + 1); } } while (i < n && v[i].t <= nxtTime) { F = F + dir * (v[i].t - T); T = v[i].t; while (i < n && v[i].t == T) { waitingT.add(v[i].s, 1); waiting[v[i].s].push_back(i); i++; } for (int k = 0; k < outing[F].size(); k++) { answer[v[outing[F][k]].id] = T; } inLiftT.remove(F); outing[F].clear(); waitingT.remove(F); for (int k = 0; k < waiting[F].size(); k++) { outing[v[waiting[F][k]].f].push_back(waiting[F][k]); inLiftT.add(v[waiting[F][k]].f, 1); } waiting[F].clear(); int sF = waitingT.cntUp(F) + inLiftT.cntUp(F); int dF = waitingT.cntDown(F) + inLiftT.cntDown(F); if (sF == 0 && dF == 0 && i >= n) break; if (sF == 0 && dF == 0) { dir = 0; nxtTime = 1000000005; } else if (sF >= dF) { dir = 1; nxtTime = T + min(inLiftT.findNext(F), waitingT.findNext(F)) - F; } else { dir = -1; nxtTime = T + F - max(inLiftT.findBef(F) + 1, waitingT.findBef(F) + 1); } } } for (int i = 0; i < n; i++) printf("%I64d\n", answer[i]); }
2,200
CPP
#include <bits/stdc++.h> using namespace std; map<string, int> sp; int main() { string s; cin >> s; for (int i = 0; i < s.size(); i++) { for (int j = 1; j <= s.size() - i; j++) { sp[s.substr(i, j)]++; } } string ans; for (auto it : sp) { if (it.first.size() > ans.size() && it.second >= 2) { ans = it.first; } } cout << ans.size() << endl; }
1,200
CPP
t = int(input()) for i in range(t): a,b = [int(x) for x in input().split()] c = a % b if c == 0 : print(c) else : print(b-c)
1,500
PYTHON3
s=str(input()) t=str(input()) x=str(input()) ans="" for c in x: if ord(c)<=90 and ord(c)>=65: a=chr(ord(c)+32) j=s.index(a) ans+=chr(ord(t[j])-32) elif ord(c)>=97 and ord(c)<=122: j=s.index(c) ans+=t[j] else: ans+=c print(ans)
800
PYTHON3
import math l=int(input('')) s=input() l1=s.split(' ') l1=list(map(int,l1)) l2=l1 l3=[] while(True): l1=l2 l2=sorted(l2) x=len(l1)-1 while(x>0): if(l2[x]>l2[x-1]): l2[x]=l2[x]-l2[x-1] x-=1 l3.append(sum(l2)) if(l1==l2): break # print(l2) print(min(l3))
1,000
PYTHON3
def main(): x = input() if x.count("0000000") > 0 or x.count("1111111") > 0 : print("YES") else: print("NO") if __name__ == '__main__': main()
900
PYTHON3
t=int(input()) import math for _ in range(t): a,b,c,d,k=map(int,input().split(" ")) x=math.ceil(a/c) y=math.ceil(b/d) if (x+y<=k): print(" ".join([str(x),str(y)])) else: print(-1)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int a[1000]; for (int i = 0; i < 4; i++) { cin >> a[i]; } sort(a, a + 4); if (a[0] + a[3] == a[1] + a[2]) { cout << "YES" << endl; } else if (a[0] + a[1] + a[2] == a[3]) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; long long read() { long long res = 0, flag = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') flag = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { res = (res << 3) + (res << 1) + (ch ^ 48); ch = getchar(); } return res * flag; } const long long maxn = 2e5 + 5; const long long mod = 1e9 + 7; const double pi = acos(-1); const double eps = 1e-8; long long n, m, k; pair<long long, long long> st, cur, a[maxn]; set<pair<long long, long long>> s; signed main() { n = read(), m = read(); m %= 2 * n; scanf("%lld%lld", &st.first, &st.second); for (long long i = 0; i < n; i++) scanf("%lld%lld", &a[i].first, &a[i].second); s.insert(st); for (long long i = 1; i <= m; i++) { pair<long long, long long> tmp; tmp.first = 2 * a[k].first - st.first; tmp.second = 2 * a[k].second - st.second; st = tmp; k = (k + 1) % n; } cout << st.first << " " << st.second << endl; return 0; }
1,800
CPP
#include <bits/stdc++.h> using namespace std; vector<int> huruf[300]; string a, b; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> a >> b; for (int i = 0; i < a.size(); i++) { huruf[a[i]].push_back(i); } int last = INT_MAX; int ans = 0; bool ok = 1; for (int i = 0; i < b.size(); i++) { if (huruf[b[i]].size() == 0) { ok = 0; break; } int pos = lower_bound(huruf[b[i]].begin(), huruf[b[i]].end(), last) - huruf[b[i]].begin(); if (pos == huruf[b[i]].size()) { ans++; last = huruf[b[i]][0] + 1; } else { last = huruf[b[i]][pos] + 1; } } if (ok) cout << ans << '\n'; else cout << -1 << '\n'; return 0; }
1,500
CPP
from collections import defaultdict d=defaultdict(list) for _ in range(int(input())): a = list((input().split())) s=a[0] x=a[1] ind=2 for i in range(int(x)): d[s].append((a[ind])) ind+=1 print(len(d)) for keys in d: s = set() for j in sorted(d[keys],key=len): if j not in s: s.add(str(j)) s1 = "" j=str(j) for i in range(len(j) - 1 , 0 , -1): s1 = j[i]+s1 if s1 in s: s.remove(s1) print(keys,len(s),*s)
1,400
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int h, m; cin >> h >> m; cout << (23 - h) * 60 + 60 - m << "\n"; } }
800
CPP
import sys, os from io import BytesIO, IOBase from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log from collections import defaultdict as dd, deque from heapq import merge, heapify, heappop, heappush, nsmallest from bisect import bisect_left as bl, bisect_right as br, bisect if os.path.exists('input.txt'): sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') # region fastio BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") stdin, stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) mod = pow(10, 9) + 7 mod2 = 998244353 def inp(): return stdin.readline().strip() def iinp(): return int(inp()) def out(var, end="\n"): stdout.write(str(var)+"\n") def outa(*var, end="\n"): stdout.write(' '.join(map(str, var)) + end) def lmp(): return list(mp()) def mp(): return map(int, inp().split()) def l1d(n, val=0): return [val for i in range(n)] def l2d(n, m, val=0): return [l1d(m, val) for j in range(n)] def ceil(a, b): return (a+b-1)//b S1 = 'abcdefghijklmnopqrstuvwxyz' S2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' def isprime(x): if x<=1: return False if x in (2, 3): return True if x%2 == 0: return False for i in range(3, int(sqrt(x))+1, 2): if x%i == 0: return False return True T=iinp() for _ in range(T): n=iinp() if isprime(n): for i in range(n): for j in range(n): print(1,end=" ") print() else: ans=[] for i in range(n-1): ans.append(4) s=sum(ans) ele=1 while True: s+=ele if isprime(s) and not isprime(ele): ans.append(ele) break else: s-=ele ele+=1 for i in range(n): print(*ans) temp=[] temp.append(ans[-1]) ans.pop() ans=temp+ans
900
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int test, number, low = -2e9, high = 2e9; string str; char ch; cin >> test; while (test-- && cin >> str >> number >> ch) { if (ch == 'N') { if (str == ">") str = "<="; else if (str == "<") str = ">="; else if (str == "<=") str = ">"; else str = "<"; } if (str == ">") low = max(low, number + 1); else if (str == "<") high = min(high, number - 1); else if (str == ">=") low = max(low, number); else high = min(high, number); } if (high < low) cout << "Impossible" << endl; else cout << high << endl; return 0; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; char arr[n][m]; bool check[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> arr[i][j]; check[i][j] = true; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (check[i][j]) { for (int k = 0; k < n; k++) { if (arr[i][j] == arr[k][j] && i != k) { check[i][j] = false; check[k][j] = false; } } for (int k = 0; k < m; k++) { if (arr[i][j] == arr[i][k] && j != k) { check[i][j] = false; check[i][k] = false; } } } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (check[i][j]) cout << arr[i][j]; } } }
1,100
CPP
#include <bits/stdc++.h> const long long way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; using namespace std; const long long inf = 0x7fffffff; const double PI = acos(-1.0); const double eps = 1e-6; const long long mod = 1e9 + 7; const long long N = 2e5 + 5; long long n, ans, sum; long long a[N]; signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n; for (long long i = 0; i < n; i++) cin >> a[i]; for (long long maxx = 1; maxx <= 30; maxx++) { sum = 0; for (long long i = 0; i < n; i++) { if (a[i] > maxx) { sum = 0; continue; } sum += a[i]; if (sum < 0) sum = 0; ans = max(ans, sum - maxx); } } cout << ans << '\n'; return 0; }
2,000
CPP
n = int(input()) a = n%10 b = int(str(n)[:-1] + str('0')) #print(n, b, abs(n-b)) if a == 0: print(n) elif abs(n-b) > 5: print((10-abs(n-b))+n) else: print(b)
800
PYTHON3
a=input() h=j=k=0 for i in range (0, len(a), 2): if a[i]=='1': h+=1 elif a[i]=='2': j+=1 else: k+=1 a="1+"*h+"2+"*j+"3+"*k print(a[:-1])
800
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); } template <class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); } template <class S, class T> ostream &operator<<(ostream &os, pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } const long long inf = 1000000001; const long long INF = (long long)1e18 + 1; const long double pi = 3.1415926535897932384626433832795028841971L; vector<long long> dx = {1, 0, -1, 0}, dy = {0, 1, 0, -1}; vector<long long> dx2 = {1, 1, 0, -1, -1, -1, 0, 1}, dy2 = {0, 1, 1, 1, 0, -1, -1, -1}; const long long MOD = 998244353; template <typename T1, typename T2> class LazySegmentTree { public: using F = function<T1(T1 &, T1 &)>; using G = function<T2(T2 &, T2 &)>; using H = function<T1(T1 &, T2 &, long long, long long)>; long long n; vector<T1> node; vector<T2> lazy; T1 e1; T2 e2; F query_func; G update_func; H eval_func; LazySegmentTree(vector<T1> a, F query_func, G update_func, H eval_func, T1 e1, T2 e2) : query_func(query_func), update_func(update_func), eval_func(eval_func), e1(e1), e2(e2) { long long _n = a.size(); n = 1; while (n < _n) n *= 2; node.resize(2 * n - 1, e1); lazy.resize(2 * n - 1, e2); for (long long i = 0; i < _n; i++) node[i + n - 1] = a[i]; for (long long i = n - 2; i >= 0; i--) { node[i] = query_func(node[i * 2 + 1], node[i * 2 + 2]); } } inline void eval(long long k, long long l, long long r) { if (lazy[k] != e2) { node[k] = eval_func(node[k], lazy[k], l, r); if (r - l > 1) { lazy[2 * k + 1] = update_func(lazy[2 * k + 1], lazy[k]); lazy[2 * k + 2] = update_func(lazy[2 * k + 2], lazy[k]); } lazy[k] = e2; } } void update(long long a, long long b, T2 x, long long k, long long l, long long r) { eval(k, l, r); if (b <= l || r <= a) return; if (a <= l && r <= b) { lazy[k] = update_func(lazy[k], x); eval(k, l, r); } else { update(a, b, x, 2 * k + 1, l, (l + r) / 2); update(a, b, x, 2 * k + 2, (l + r) / 2, r); node[k] = query_func(node[2 * k + 1], node[2 * k + 2]); } } T1 query(long long a, long long b, long long k, long long l, long long r) { eval(k, l, r); if (b <= l || r <= a) return e1; if (a <= l && r <= b) return node[k]; T1 resl = query(a, b, 2 * k + 1, l, (l + r) / 2); T1 resr = query(a, b, 2 * k + 2, (l + r) / 2, r); return query_func(resl, resr); } }; long long f(long long a, long long b) { return max(a, b); } long long g(long long a, long long b) { return a + b; } long long h(long long a, long long b, long long l, long long r) { return a + b; } signed main() { cin.tie(0); ios::sync_with_stdio(false); ; long long n; cin >> n; vector<long long> p(n), q(n); vector<long long> p2idx(n + 1); for (long long i = 0; i < (n); i++) { cin >> p[i]; p2idx[p[i]] = i; } for (long long i = 0; i < (n); i++) { cin >> q[i]; q[i]--; } vector<long long> a(n); long long x = n; for (long long i = 0; i < (p2idx[x] + 1); i++) { a[i]++; } LazySegmentTree<long long, long long> seg(a, f, g, h, -inf, 0); for (long long i = 0; i < (n); i++) { while (true) { long long ma = seg.query(0, n, 0, 0, seg.n); if (ma > 0) { cout << x << " "; break; } x--; seg.update(0, p2idx[x] + 1, 1, 0, 0, seg.n); } seg.update(0, q[i] + 1, -1, 0, 0, seg.n); } cout << endl; }
2,400
CPP
#include <bits/stdc++.h> using namespace std; int n, l; double a[203]; int b[203]; double memo[205][205][405]; double go(int pos, int win, int capa) { if (pos >= n) { if (win >= l && capa >= 200) return 1.0; return 0.0; } if (memo[pos][win][capa] >= 0.0) return memo[pos][win][capa]; double res = 0.0; if (b[pos] == -1) { if (capa) res += a[pos] * go(pos + 1, win + 1, capa - 1); } if (b[pos] != -1) { res += a[pos] * go(pos + 1, win + 1, min(400, capa + b[pos])); } res += (1.0 - a[pos]) * go(pos + 1, win, capa); return memo[pos][win][capa] = res; } int main() { int k; cin >> n >> l >> k; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) cin >> b[i]; for (int i = 0; i < n; i++) a[i] /= 100.0; for (int i = 0; i < 203; i++) for (int j = 0; j < 203; j++) for (int kk = 0; kk < 403; kk++) memo[i][j][kk] = -1.0; double res = go(0, 0, 200 + k); printf("%.11lf\n", res); return 0; }
1,800
CPP
a,b=input().split() n=int(input()) print(a,b) s=[a,b] for i in range(n): x,y=input().split() s.remove(x) s.append(y) print(*s)
900
PYTHON3
import sys def inp(): return sys.stdin.readline().rstrip("\r\n") # for fast input def N(): return int(inp()) def lis(): return list(map(int, inp().split())) def stringlis(): return list(map(str, inp().split())) def take(): return map(int, inp().split()) def strsep(): return map(str, inp().split()) def fsep(): return map(float, inp().split()) def testcase(t): for p in range(t): solve() def solve(): n=N() ar=lis() t=sorted(ar) ans=[] for i in ar: ind=t.index(i) ind=(ind+1)%n ans.append(t[ind]) print(*ans) solve() #testcase(N())
2,000
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<int> v; void izbaci(int l, int r) { for (int i = 0; i < v.size(); i++) { if (v[i] >= l && v[i] <= r) { v.erase(v.begin() + i); izbaci(l, r); } } } int main() { int n, m; cin >> n >> m; int l, r; for (int i = 1; i <= m; i++) v.push_back(i); for (int i = 0; i < n; i++) { cin >> l >> r; izbaci(l, r); } cout << v.size() << endl; for (int i = 0; i < v.size(); i++) { cout << v[i] << ' '; } }
800
CPP
inp = input() num = int(inp) while True: num+=1 string = str(num) if string.count(string[0])==1 and string.count(string[1])==1 and string.count(string[3])==1 and string.count(string[2])==1: break print(num)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; template <typename T> struct RBNode { enum Color { RED, BLACK }; RBNode *right = nullptr, *left = nullptr, *parent = nullptr; T value; int size = 0; int color = RED; inline bool isBlack() const { return color == BLACK; } inline bool isRed() const { return color == RED; } inline bool isLeftBlack() const { return left == nullptr || left->color == BLACK; } inline bool isLeftRed() const { return left && left->color == RED; } inline bool isRightBlack() const { return right == nullptr || right->color == BLACK; } inline bool isRightRed() const { return right && right->color == RED; } inline int leftSize() const { return left ? left->size : 0; } inline int rightSize() const { return right ? right->size : 0; } inline void paintBlack() { color = BLACK; } inline void paintRed() { color = RED; } }; template <typename T> struct RBTree { RBNode<T> *root = nullptr; inline int size() const { return root ? root->size : 0; } void insert(const T &value) { RBNode<T> *parent; bool left; if (!find_insertion_point(value, parent, left)) return; auto node = new RBNode<T>; node->value = value; node->parent = parent; if (parent) { if (left) parent->left = node; else parent->right = node; } else { root = node; } on_insert(node); start: parent = node->parent; if (!parent) { node->paintBlack(); return; } if (parent->isBlack()) return; auto g = parent->parent; auto uncle = g->left == parent ? g->right : g->left; if (uncle && uncle->isRed()) { parent->paintBlack(); uncle->paintBlack(); g->paintRed(); node = g; goto start; } if (node == parent->right && parent == g->left) { rotate_left(parent); swap(parent, node); } if (node == parent->left && parent == g->right) { rotate_right(parent); swap(parent, node); } parent->paintBlack(); g->paintRed(); if (node == parent->left) rotate_right(g); else rotate_left(g); } RBNode<T> *successor(RBNode<T> *node) const { if (!node) return nullptr; auto current = node; if (current->right) { current = current->right; while (current->left) current = current->left; } else { while (current) { auto stop = !current->parent || current == current->parent->left; current = current->parent; if (stop) break; } } return current; } int n_geq(const T &value) const { RBNode<T> *parent, *node; bool left; if (find_insertion_point(value, parent, left)) { node = left ? parent : successor(parent); } else { if (parent) { node = left ? parent->left : parent->right; } else { node = root; } } if (!node) return 0; auto count = 1 + node->rightSize(); auto previous = node; for (auto current = node->parent; current; current = current->parent) { if (current->left == previous) count += 1 + current->rightSize(); previous = current; } return count; } inline int count_in_range(const T &from, const T &to) const { return from < to ? n_geq(from) - n_geq(to) : 0; } bool find_insertion_point(const T &value, RBNode<T> *&parent, bool &left) const { auto current = root; parent = nullptr; while (current) { if (current->value == value) return false; parent = current; left = !(current->value < value); current = left ? current->left : current->right; } return true; } void rotate_left(RBNode<T> *node) { on_rotate_left(node); auto p = node->parent; auto c = node->right; node->right = c->left; node->parent = c; if (c->left) c->left->parent = node; c->left = node; c->parent = p; if (p) { if (p->left == node) p->left = c; else p->right = c; } else { root = c; } } void rotate_right(RBNode<T> *node) { on_rotate_right(node); auto p = node->parent; auto c = node->left; node->left = c->right; node->parent = c; if (c->right) c->right->parent = node; c->right = node; c->parent = p; if (p) { if (p->left == node) p->left = c; else p->right = c; } else { root = c; } } void on_rotate_left(RBNode<T> *node) { auto t = node->leftSize() + node->right->leftSize() + 1; node->right->size = node->size; node->size = t; } void on_rotate_right(RBNode<T> *node) { auto t = node->rightSize() + node->left->rightSize() + 1; node->left->size = node->size; node->size = t; } void on_insert(RBNode<T> *node) { for (auto current = node; current; current = current->parent) current->size++; } }; int scan() { auto c = getchar(); while (c < '0' || c > '9') c = getchar(); int n; for (n = 0; '0' <= c && c <= '9'; c = getchar()) n = n * 10 + c - '0'; return n; } struct Op { int time; int value; }; bool operator<(const Op &a, const Op &b) { if (a.value == b.value) return a.time < b.time; return a.value < b.value; } bool operator==(const Op &a, const Op &b) { return a.value == b.value && a.time == b.time; } int main() { int N = scan(); RBTree<Op> inserts, removals; for (auto i = 0; i < N; i++) { auto type = scan(); Op op = {scan(), scan()}; switch (type) { case 1: inserts.insert(op); break; case 2: removals.insert(op); break; case 3: Op from = {1, op.value}; auto a = inserts.count_in_range(from, op); auto b = removals.count_in_range(from, op); printf("%d\n", a - b); break; } } }
2,000
CPP
c = int(input()) d = {} t = {} for i in range(c): s = input().split() if s[0] not in t: t[s[0]] = [] for m in s[2:]: t[s[0]].append(m) for i in t: d[i] = [] u = set(t[i]) q = [] for p in u: q.append(p) for j in range(len(q)): a = q[j][::-1] z = 0 for k in range(len(q)): b = q[k][::-1] if j!=k and b.find(a)!=-1: if b.find(a) == 0: z = 1 break if z == 0: d[i].append(a[::-1]) print(len(d)) for i in d: print(i,len(d[i]),end = ' ') for j in d[i]: print((j),end = ' ') print('\n')
1,400
PYTHON3
n, k = map(int,input().split()) s = list(input().split()) a = n // k d = 0 for x in range(k): b = "" c = x for y in range(a): b += s[c] c += k if b.count("1") >= b.count("2"):d += b.count("2") else:d += b.count("1") print(d)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int t, i, j, k; string str; cin >> t; while (t--) { cin >> str; bool isok = false; string str1 = str; reverse(str1.begin(), str1.end()); set<char> s; for (i = 0; i < str.size(); i++) { if (str[i] != str1[i]) { cout << str << endl; isok = true; break; } s.insert(str[i]); } if (isok) continue; if (s.size() == 1) { cout << -1 << endl; isok = true; continue; } for (i = 0; i < str.size(); i++) { if (str[i] != str[i + 1]) { swap(str[i], str[i + 1]); break; ; } } cout << str << endl; } return 0; }
900
CPP
def isALucky(n): if n % 4 == 0: return 'YES' elif n % 7 == 0: return 'YES' elif n % 44 == 0: return 'YES' elif n % 444 == 0: return 'YES' elif n % 77 == 0: return 'YES' elif n % 777 == 0: return 'YES' elif n % 47 == 0: return 'YES' elif n % 74 == 0: return 'YES' elif n % 447 == 0: return 'YES' elif n % 474 == 0: return 'YES' elif n % 744 == 0: return 'YES' elif n % 477 == 0: return 'YES' elif n % 747 == 0: return 'YES' elif n % 774 == 0: return 'YES' else: return 'NO' n = int(input()) print(isALucky(n))
1,000
PYTHON3
t=int(input()) for i in range(t): n=int(input()) s=input() b=[] od=0 l1=0 ev=0 l2=0 for j in range(n): if j%2==0: if int(s[j])%2!=0: od+=1 else: l2+=1 if int(s[j]) % 2 == 0: ev += 1 f=0 if n==1 and int(s)%2==0: f=1 if n%2==0: if ev!=0: f=1 else: if od==0: f=1 if f==0: print(1) else: print(2)
900
PYTHON3
#include <bits/stdc++.h> using namespace std; long long n, k; long long l, r, mmax = 0; long long a[2000 + 5], dp[2000 + 5]; bool judge(long long x) { for (int i = 1; i <= n; i++) { dp[i] = i - 1; for (int j = i - 1; j >= 1; j--) { if (abs(a[i] - a[j]) <= (i - j) * x) dp[i] = min(dp[i], dp[j] + i - j - 1); } if (dp[i] + n - i <= k) return true; } return false; } int main() { scanf("%lld%lld", &n, &k); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); for (int i = 2; i <= n; i++) mmax = max(mmax, abs(a[i] - a[i - 1])); l = 0; r = mmax; while (l < r) { long long mid = (l + r) >> 1; if (judge(mid)) r = mid; else l = mid + 1; } printf("%lld\n", l); return 0; }
2,000
CPP
import sys, itertools, math T = int(sys.stdin.readline()) for _ in range(T): n, m = [int(i) for i in sys.stdin.readline().strip().split(" ")] ans = True G = {} for _ in range(n): t, l, h = [int(i) for i in sys.stdin.readline().strip().split(" ")] if t in G: gl, gh = G[t] if h < gl or l > gh: ans = False else: G[t] = (max(l, gl), min(h, gh)) else: G[t] = (l, h) G = sorted(G.items()) min_t = m max_t = m last_t = 0 for t, (l, h) in G: min_t -= t - last_t max_t += t - last_t if h < min_t or l > max_t: ans = False break min_t = max(l, min_t) max_t = min(h, max_t) last_t = t if ans: print("YES") else: print("NO")
1,500
PYTHON3
n=int(input()) countm=0 countc=0 for _ in range(n): m,c=map(int,input().split()) if m>c: countm+=1 elif c>m: countc+=1 if (countm>countc): print("Mishka") elif (countm<countc): print("Chris") else: print("Friendship is magic!^^")
800
PYTHON3
for _ in range(int(input())): l, r = map(int, input().split()) x = l y = x * 2 if y > r: print(-1, -1) continue else: print(x, y)
800
PYTHON3
n=int(input()) m=int(0) while(n!=0): a=[int(i) for i in input().split()] diff=int(abs(a[1]-a[0])) m=int (diff/5) diff%=5 if diff<5 and diff>0 : if diff == 1 or diff == 2: print(m+1) elif diff==3 or diff==4 : print(m+2) elif diff==0: print(m) n-=1
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, i, s = 0; cin >> n; int a[8]; for (i = 0; i < 7; i++) cin >> a[i]; i = 0; while (n > s) { s += a[i % 7]; i++; } if (i % 7 == 0) cout << 7; else cout << i % 7; return 0; }
1,000
CPP
# -*- coding: utf-8 -*- """ Created on Tue Mar 31 08:36:04 2020 @author: CronuS """ k = list(map(int, input().split())) n = k[0]; s = '' for i in range(n): k = list(map(int, input().split())) a = k[0] b = k[1] c = k[2] d = k[3] k = list(map(int, input().split())) x = k[0] y = k[1] x1 = k[2] y1 = k[3] x2 = k[4] y2 = k[5] x = x - a + b y = y - c + d s = 'no' if ((x >= x1) and (x <= x2) and (y >= y1) and (y <= y2)): if (((x1 == x2) and ((a + b) > 0)) or ((y1 == y2) and ((c + d) > 0))): s = 'no' else: s = 'yes' print(s)
1,100
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("no-stack-protector") #pragma GCC target("sse,sse2,ssse3,sse3,sse4,popcnt,avx,mmx,abm,tune=native") using namespace std; namespace output { void __(short x) { cout << x; } void __(unsigned x) { cout << x; } void __(int x) { cout << x; } void __(long long x) { cout << x; } void __(unsigned long long x) { cout << x; } void __(double x) { cout << x; } void __(long double x) { cout << x; } void __(char x) { cout << x; } void __(const char* x) { cout << x; } void __(const string& x) { cout << x; } void __(bool x) { cout << (x ? "true" : "false"); } template <class S, class T> void __(const pair<S, T>& x) { __(1 ? "(" : ""), __(x.first), __(1 ? ", " : " "), __(x.second), __(1 ? ")" : ""); } template <class T> void __(const vector<T>& x) { __(1 ? "{" : ""); bool _ = 0; for (const auto& v : x) __(_ ? 1 ? ", " : " " : ""), __(v), _ = 1; __(1 ? "}" : ""); } template <class T> void __(const set<T>& x) { __(1 ? "{" : ""); bool _ = 0; for (const auto& v : x) __(_ ? 1 ? ", " : " " : ""), __(v), _ = 1; __(1 ? "}" : ""); } template <class T> void __(const multiset<T>& x) { __(1 ? "{" : ""); bool _ = 0; for (const auto& v : x) __(_ ? 1 ? ", " : " " : ""), __(v), _ = 1; __(1 ? "}" : ""); } template <class S, class T> void __(const map<S, T>& x) { __(1 ? "{" : ""); bool _ = 0; for (const auto& v : x) __(_ ? 1 ? ", " : " " : ""), __(v), _ = 1; __(1 ? "}" : ""); } void pr() { cout << "\n"; } template <class S, class... T> void pr(const S& a, const T&... b) { __(a); if (sizeof...(b)) __(' '); pr(b...); } } // namespace output using namespace output; const int MN = 404, MM = 125; int n, i, j, x, y, ds[5 * MM * MM], dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1}, dis[2 * MN][2 * MN], block[2 * MN][2 * MN]; pair<int, int> s, e, tmp; char dir[] = {'L', 'R', 'D', 'U'}; vector<char> ans; int fnd(int x) { return ds[x] = ds[x] == x ? x : fnd(ds[x]); } inline void mv(int id, int dx, int dy) { int nx = (!id ? s.first : e.first) + dx, ny = (!id ? s.second : e.second) + dy; if (nx >= 0 && nx < 2 * MM && ny >= 0 && ny < 2 * MM && block[nx][ny]) return; if (!id) s = {nx, ny}; else e = {nx, ny}; } inline void gen(pair<int, int> src) { queue<pair<int, int> > q; memset(dis, -1, sizeof(dis)); dis[src.first][src.second] = 0; q.push(src); while (q.size()) { pair<int, int> cur = q.front(); q.pop(); for (int k = 0; k < 4; k++) { int nx = cur.first + dx[k], ny = cur.second + dy[k]; if (nx < 0 || nx > 2 * MM || ny < 0 || ny > 2 * MM) continue; if (!block[nx][ny] && dis[nx][ny] == -1) { dis[nx][ny] = dis[cur.first][cur.second] + 1; q.push({nx, ny}); } } } } inline void trans(int d, int steps) { while (steps--) { mv(0, dx[d], dy[d]); mv(1, dx[d], dy[d]); ans.push_back(dir[d]); } } int main() { scanf("%d%d%d%d%d", &s.first, &s.second, &e.first, &e.second, &n); s.first += MM, s.second += MM, e.first += MM, e.second += MM; for (i = 1; i <= n; i++) { scanf("%d%d", &x, &y); x += MM, y += MM; block[x][y] = 1; } for (i = 0; i < 2 * MM; i++) { for (j = 0; j < 2 * MM; j++) ds[i * (2 * MM) + j] = i * (2 * MM) + j; } for (i = 0; i < 2 * MM; i++) { for (j = 0; j < 2 * MM; j++) { if (block[i][j]) continue; int id = i * 2 * MM + j; for (int k = 0; k < 4; k++) { int nx = i + dx[k], ny = j + dy[k]; if (nx < 0 || nx > 2 * MM || ny < 0 || ny > 2 * MM) continue; if (!block[nx][ny]) { int ot = nx * 2 * MM + ny; if (fnd(id) != fnd(ot)) ds[fnd(id)] = fnd(ot); } } } } int hm = 2 * MM - 1; const int INF = 1500; if (fnd(s.first * 2 * MM + s.second) != fnd(e.first * 2 * MM + e.second) || !n) printf("-1\n"); else if (fnd(s.first * 2 * MM + s.second) == fnd(hm * 2 * MM + hm)) { gen(s); vector<int> op; pair<int, int> cur = {hm, hm}; while (cur != s) { for (int k = 0; k < 4; k++) { int nx = cur.first + dx[k], ny = cur.second + dy[k]; if (nx < 0 || nx > 2 * MM || ny < 0 || ny > 2 * MM) continue; if (dis[nx][ny] + 1 == dis[cur.first][cur.second]) { op.push_back(k ^ 1); cur = {nx, ny}; break; } } } reverse(op.begin(), op.end()); for (auto v : op) { mv(0, dx[v], dy[v]); mv(1, dx[v], dy[v]); ans.push_back(dir[v]); } op.clear(); for (i = 0; i < 2 * MM + 10; i++) { mv(0, dx[1], dy[1]); mv(1, dx[1], dy[1]); ans.push_back(dir[1]); mv(0, dx[3], dy[3]); mv(1, dx[3], dy[3]); ans.push_back(dir[3]); } if (e.first >= 0 && e.first < hm && e.second >= 0 && e.second < hm) { gen(e); op.clear(); pair<int, int> cur = {hm, hm}; while (cur != e) { for (int k = 0; k < 4; k++) { int nx = cur.first + dx[k], ny = cur.second + dy[k]; if (nx < 0 || nx > 2 * MM || ny < 0 || ny > 2 * MM) continue; if (dis[nx][ny] + 1 == dis[cur.first][cur.second]) { op.push_back(k ^ 1); cur = {nx, ny}; break; } } } reverse(op.begin(), op.end()); for (auto v : op) { mv(0, dx[v], dy[v]); mv(1, dx[v], dy[v]); ans.push_back(dir[v]); } } pair<int, int> a = {min(s.first, e.first), min(s.second, e.second)}; pair<int, int> b = {max(s.first, e.first), max(s.second, e.second)}; if (e.first < s.first) { pair<int, int> cell(-1, -1); for (i = 0; i < 2 * MM; i++) { for (j = 0; j < 2 * MM; j++) { if (block[i][j] && i > cell.first) cell = {i, j}; } } int DX = e.first - cell.first, DY = e.second - cell.second, DC = s.first - e.first - 1; trans(2, DY); trans(0, DX + DC); trans(1, DX + DC); trans(3, DY); } else if (e.first > s.first) { pair<int, int> cell(10000, 10000); for (i = 0; i < 2 * MM; i++) { for (j = 0; j < 2 * MM; j++) { if (block[i][j] && i < cell.first) cell = {i, j}; } } int IDX = INF; trans(0, IDX); int DX = cell.first - e.first, DY = e.second - cell.second, DC = e.first - s.first - 1; trans(2, DY); trans(1, DX + DC); trans(0, DX + DC); trans(3, DY); trans(1, IDX); } if (e.second < s.second) { pair<int, int> cell(-1, -1); for (i = 0; i < 2 * MM; i++) { for (j = 0; j < 2 * MM; j++) { if (block[i][j] && i > cell.second) cell = {i, j}; } } int DX = e.first - cell.first, DY = e.second - cell.second, DC = s.second - e.second - 1; trans(0, DX); trans(2, DY + DC); trans(3, DY + DC); trans(1, DX); } else if (s.second < e.second) { pair<int, int> cell(10000, 10000); for (i = 0; i < 2 * MM; i++) { for (j = 0; j < 2 * MM; j++) { if (block[i][j] && i < cell.second) cell = {i, j}; } } int IDY = INF; trans(2, IDY); int DX = e.first - cell.first, DY = cell.second - e.second, DC = e.second - s.second - 1; trans(0, DX); trans(3, DY + DC); trans(2, DY + DC); trans(1, DX); trans(2, IDY); } for (auto v : ans) printf("%c", v); pr(); } else { queue<pair<int, int> > q; while (s != e) { gen(e); for (int k = 0; k < 4; k++) { int nx = s.first + dx[k], ny = s.second + dy[k]; if (nx < 0 || nx > 2 * MM || ny < 0 || ny > 2 * MM) continue; if (dis[nx][ny] + 1 == dis[s.first][s.second]) { mv(1, dx[k], dy[k]); mv(0, dx[k], dy[k]); ans.push_back(dir[k]); break; } } } for (auto v : ans) printf("%c", v); pr(); } }
3,100
CPP
#JMD #Nagendra Jha-4096 import sys import math #import fractions #import numpy ###File Operations### fileoperation=0 if(fileoperation): orig_stdout = sys.stdout orig_stdin = sys.stdin inputfile = open('W:/Competitive Programming/input.txt', 'r') outputfile = open('W:/Competitive Programming/output.txt', 'w') sys.stdin = inputfile sys.stdout = outputfile ###Defines...### mod=1000000007 ###FUF's...### def nospace(l): ans=''.join(str(i) for i in l) return ans ##### Main #### t=int(input()) for tt in range(t): #n=int(input()) a,b= map(int, sys.stdin.readline().split(' ')) ans=0 if(a>b): temp=b b=a a=temp diff=b-a ans+=(diff//5) diff%=5 ans+=(diff//2) diff%=2 ans+=diff print(ans) #a=list(map(int,sys.stdin.readline().split(' '))) #####File Operations##### if(fileoperation): sys.stdout = orig_stdout sys.stdin = orig_stdin inputfile.close() outputfile.close()
800
PYTHON3
n=int(input()) a=[int(x) for x in input().split()][:n] for i in a: if i%2==0: print(i-1,end=" ") else: print(i,end=" ")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m, k, it, it1, line; bool g[307][307], mark[307], vis[307]; int main() { scanf("%d%d%d", &n, &m, &k); if ((k == n) || (m > (n - 1) * (n - 2) / 2 + n - k)) { printf("-1"); return 0; } for (int i = 1; i <= k; ++i) { int in; scanf("%d", &in); mark[in] = 1; it = in; } if (m == n - 1 && k == 1) { for (int i = 1; i <= n; ++i) if (mark[i]) { it == i; break; } int last; for (int i = 1; i < n; ++i) { if (mark[i]) continue; printf("%d %d\n", i, it); last = i; } printf("%d %d\n", n, last); return 0; } vis[it] = 1; for (int i = 1; i <= n; ++i) g[i][i] = 1; for (int i = 1; i <= n; ++i) { if (i == it || mark[i]) continue; printf("%d %d\n", i, it); ++line; vis[i] = 1; g[i][it] = g[it][i] = 1; it1 = i; if (line == m) return 0; } for (int i = 1; i <= n; ++i) { if (line == m) return 0; if (vis[i]) continue; printf("%d %d\n", i, it1); ++line; g[i][it1] = g[it1][i] = 1; vis[i] = 1; } for (int i = 1; i <= n; ++i) { if (line == m) return 0; if (i == it) continue; for (int j = 1; j <= n; ++j) { if (j == it || g[i][j]) continue; if (line == m) return 0; printf("%d %d\n", i, j); g[i][j] = g[j][i] = 1; ++line; } } return 0; }
2,200
CPP
#include <bits/stdc++.h> #pragma warning(disable : 4996) #pragma comment(linker, "/STACK:336777216") using namespace std; int IT_MAX = 1 << 19; const long long MOD = 998244353; const int INF = 0x3f3f3f3f; const long long LL_INF = 0x3f3f3f3f3f3f3f3f; const double PI = acos(-1); const double ERR = 1e-10; int main() { long long n, a[100009], ans = 0, mx = -1, cnt = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; mx = max(a[i], mx); } for (int i = 0; i < n; i++) { if (a[i] == mx) { cnt++; } else { if (ans < cnt) { ans = cnt; } cnt = 0; } } if (ans < cnt) { ans = cnt; } cout << ans << endl; return 0; }
1,100
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long l1, r1, l2, r2, k, ans = 0; cin >> l1 >> r1 >> l2 >> r2 >> k; if (l2 > r1 || l1 > r2) { cout << "0"; return 0; } else { ans = min(r1, r2) - max(l1, l2) + 1; if (k >= max(l2, l1) && k <= min(r1, r2)) ans = ans - 1; } cout << ans; return 0; }
1,100
CPP
t = int(input()) for i in range(t): x = int(input()) print(x);
900
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, b, t; int nom = 0; cin >> n >> b >> t; while (n / 2 >= 1) { if (n % 2) nom++; nom += n / 2; n = n / 2; } cout << nom * ((b * 2) + 1) << " " << (nom + 1) * t; return 0; }
1,000
CPP
def solve(): s = input() m = set() a = [] for c in s[::-1]: if c not in m: a.append(c) m.add(c) a = a[::-1] d = {c:0 for c in a} for c in s: d[c] += 1 for i, c in enumerate(a, 1): if d[c] % i: print(-1) return d[c] //= i i = 0 ans = s[:sum(d.values())] ans1 = ans for j in range(len(a)): if s[i:i+len(ans)] != ans: print(-1) return i += len(ans) ans = ans.replace(a[j], "") print(ans1, "".join(a)) import sys input = lambda: sys.stdin.readline().rstrip() t = int(input()) for i in range(t): solve()
1,800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 100100, oo = 1000000000; int i, j, k, n, m, ch, t, nm, nc, En, rt, mi, Size; int l[N], r[N], h[N], ans[N], sz[N], z[N], fg[N]; vector<int> B[N]; struct edge { int s, n; } E[N << 1]; struct cc { int a, b, x; } A[N], C[N << 1]; void R(int &x) { x = 0; ch = getchar(); while (ch < '0' || '9' < ch) ch = getchar(); while ('0' <= ch && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); } bool cmp(const cc &a, const cc &b) { return a.x < b.x; } bool cmp1(const int &a, const int &b) { return a > b; } bool cmp2(const cc &a, const cc &b) { if (a.a == b.a) return a.b < b.b; return a.a < b.a; } void E_add(int x, int y) { E[++En].s = y; E[En].n = h[x]; h[x] = En; E[++En].s = x; E[En].n = h[y]; h[y] = En; } void get_rt(int x, int F) { int ma = 0; sz[x] = 1; for (int k = h[x]; k; k = E[k].n) if (E[k].s != F && !z[E[k].s]) { get_rt(E[k].s, x); sz[x] += sz[E[k].s]; if (sz[E[k].s] > ma) ma = sz[E[k].s]; } if (Size - sz[x] > ma) ma = Size - sz[x]; if (ma < mi) mi = ma, rt = x; } void get_sz(int x, int F) { sz[x] = 1; for (int k = h[x]; k; k = E[k].n) if (E[k].s != F && !z[E[k].s]) { get_sz(E[k].s, x); sz[x] += sz[E[k].s]; } } void Df(int x, int dp) { z[x] = 1; ans[x] = dp; get_sz(x, 0); for (int k = h[x]; k; k = E[k].n) if (!z[E[k].s]) { Size = sz[E[k].s]; mi = oo; get_rt(E[k].s, 0); Df(rt, dp + 1); } } int main() { R(n); R(m); for (i = 1; i <= m; i++) { R(A[i].a); R(A[i].b); if (A[i].a > A[i].b) { t = A[i].a; A[i].a = A[i].b; A[i].b = t; } A[i].x = min(A[i].b - A[i].a + 1, n - A[i].b + A[i].a + 1); } sort(A + 1, A + m + 1, cmp); for (i = 1; i < n; i++) r[i] = i + 1, l[i + 1] = i; r[n] = 1; l[1] = n; for (i = 1; i <= m; i++) { if (A[i].x == A[i].b - A[i].a + 1) { if (r[A[i].a] == A[i].b) continue; nm++; B[nm].push_back(A[i].a); for (j = r[A[i].a]; j != A[i].b; j = r[j]) { B[nm].push_back(j); r[l[j]] = r[j]; l[r[j]] = l[j]; fg[j] = 1; } B[nm].push_back(A[i].b); sort(B[nm].begin(), B[nm].end(), cmp1); } else { if (r[A[i].b] == A[i].a) continue; nm++; B[nm].push_back(A[i].b); for (j = r[A[i].b]; j != A[i].a; j = r[j]) { B[nm].push_back(j); r[l[j]] = r[j]; l[r[j]] = l[j]; fg[j] = 1; } B[nm].push_back(A[i].a); sort(B[nm].begin(), B[nm].end(), cmp1); } } for (i = 1; i <= n; i++) if (!fg[i]) break; nm++; B[nm].push_back(i); for (j = r[i]; j != i; j = r[j]) B[nm].push_back(j); sort(B[nm].begin(), B[nm].end(), cmp1); sort(B + 1, B + nm + 1); for (i = 1; i <= nm; i++) { for (j = 0; j < B[i].size() - 1; j++) { if (B[i][j] == B[i][j + 1] + 1) continue; nc++; C[nc].a = B[i][j + 1]; C[nc].b = B[i][j]; C[nc].x = i; } if (B[i][j] == 1 && B[i][0] == n) continue; nc++; C[nc].a = B[i][j]; C[nc].b = B[i][0]; C[nc].x = i; } puts(""); sort(C + 1, C + nc + 1, cmp2); for (i = 1; i <= nc; i += 2) E_add(C[i].x, C[i + 1].x); Size = nm; mi = oo; get_rt(1, 0); Df(rt, 1); for (i = 1; i <= nm; i++) printf("%d ", ans[i]); puts(""); }
2,800
CPP
#!/usr/bin/env python3 from typing import Dict, List, Tuple def input_lst() -> List[int]: return [int(x) for x in input().split()] def print_out(res: List[int]): print(' '.join([str(x) for x in res])) def main(): k, n, s, p = (int(x) for x in input().split()) lists_needed_for_each = (n) // (s) lists_needed_for_each += (0 if (n) % (s) == 0 else 1) x = (lists_needed_for_each * k) // p x += (0 if (lists_needed_for_each*k) % (p) == 0 else 1) #x = x * k #res = [0] #times = input_lst() #s = list(input()) print(x) #print_out(res) if __name__ == '__main__': main()
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const double PI = 3.14159265358979323846; const long long MODUL = 1000000007; const long long MAXINT = 2e9 + 2; const long long MAXLL = 2e18 + 2; const double eps = 1e-11; const int MAXN = 5e5 + 10; int n, m, b, mod, progs[502]; long long dp[502][502], ans(0); int main() { ios_base::sync_with_stdio(0); cin >> n >> m >> b >> mod; for (int i(0); i < n; ++i) cin >> progs[i]; for (int i(0); i <= m; ++i) for (int j(0); j <= n; ++j) dp[i][j] = 0; dp[0][0] = 1; for (int i(0); i < n; ++i) { for (int j(0); j <= m - 1; ++j) { for (int k(0); k <= b - progs[i]; ++k) dp[j][k] %= mod, dp[j + 1][k + progs[i]] += dp[j][k]; if (b >= progs[i] - 1) for (int k(b - progs[i] + 1); k <= b; ++k) dp[j][k] %= mod; } } for (int i(0); i <= b; ++i) ans += dp[m][i]; cout << ans % mod; return 0; }
1,800
CPP
#include <bits/stdc++.h> using namespace std; int n, m, k; vector<int> fo[1000000]; vector<int> br; bool used[1000000]; void dfs(int vr) { used[vr] = true; ++br.back(); for (int i = 0; i < fo[vr].size(); ++i) if (!used[fo[vr][i]]) dfs(fo[vr][i]); } int main() { int i, j, x, y; scanf("%d%d%d", &n, &m, &k); for (i = 0; i < m; ++i) { scanf("%d%d", &x, &y); --x; --y; fo[x].push_back(y); fo[y].push_back(x); } for (i = 0; i < n; ++i) if (!used[i]) { br.push_back(0); dfs(i); } if (k == 0) { printf("%d\n", br.size() - 1); return 0; } if (k == 1) { printf("%d\n", max(0, (int)(br.size()) - 2)); return 0; } int LEFT = 0; for (i = 0; i < br.size(); ++i) LEFT += min(k, br[i]); printf("%d\n", max(0, (int)(br.size() - 1 - floor(LEFT / 2.0)))); return 0; }
2,200
CPP
#include <bits/stdc++.h> using namespace std; const int M = 505050; const int N = 2020; const int INF = 1 << 30; inline char get(void) { static char buf[100000], *S = buf, *T = buf; if (S == T) { T = (S = buf) + fread(buf, 1, 100000, stdin); if (S == T) return EOF; } return *S++; } template <typename T> inline void read(T &x) { static char c; x = 0; int sgn = 0; for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1; for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0'; if (sgn) x = -x; } bitset<N> A[N], iA[N]; int n, q, pos, det; int u[M], v[M]; int main(void) { read(n); read(q); for (int i = 0; i < q; i++) { read(u[i]); read(v[i]); A[u[i]][v[i]] = 1; } for (int i = 1; i <= n; i++) iA[i][i] = 1; for (int i = 1; i <= n; i++) { pos = i; for (int j = i + 1; j <= n; j++) if (A[j][i]) pos = j; swap(A[i], A[pos]); swap(iA[i], iA[pos]); for (int j = 1; j <= n; j++) if (j != i && A[j][i]) { A[j] ^= A[i]; iA[j] ^= iA[i]; } } for (int i = 0; i < q; i++) if (iA[v[i]][u[i]]) puts("NO"); else puts("YES"); return 0; }
2,800
CPP
#include <bits/stdc++.h> using namespace std; int a[200500]; vector<int> r; vector<int> o; vector<int> w; vector<pair<int, char> > ro; vector<pair<int, char> > wo; int main() { int n; cin >> n; int k; cin >> k; if (k == 1) { cout << -1; return 0; } for (int i = 0; i < n; i++) { cin >> a[i]; } string s; cin >> s; for (int i = 0; i < s.length(); i++) { if (s[i] == 'R') { r.push_back(a[i]); } if (s[i] == 'O') { o.push_back(a[i]); } if (s[i] == 'W') { w.push_back(a[i]); } if (s[i] == 'R' || s[i] == 'O') ro.push_back({a[i], s[i]}); if (s[i] == 'W' || s[i] == 'O') wo.push_back({a[i], s[i]}); } sort(r.rbegin(), r.rend()); sort(o.rbegin(), o.rend()); sort(w.rbegin(), w.rend()); sort(ro.rbegin(), ro.rend()); sort(wo.rbegin(), wo.rend()); if (o.empty()) { cout << -1; return 0; } int res = -1; if (ro.size() >= k && r.size()) { set<char> have; int sum = 0; for (int i = 0; i < k - 1; i++) { sum += ro[i].first; have.insert(ro[i].second); } if (have.size() == 2) { sum += ro[k - 1].first; } else { for (int i = k - 1; i < ro.size(); i++) { if (ro[i].second != *have.begin()) { sum += ro[i].first; break; } } } res = max(res, sum); } if (wo.size() >= k && w.size()) { set<char> have; int sum = 0; for (int i = 0; i < k - 1; i++) { sum += wo[i].first; have.insert(wo[i].second); } if (have.size() == 2) { sum += wo[k - 1].first; } else { for (int i = k - 1; i < wo.size(); i++) { if (wo[i].second != *have.begin()) { sum += wo[i].first; break; } } } res = max(res, sum); } cout << res; return 0; }
2,200
CPP
#include <bits/stdc++.h> using namespace std; double a[100005]; double avg[100005]; double s[100005]; int n, k, m; int main() { scanf("%d%d%d", &n, &k, &m); for (int i = 1; i <= n; i++) scanf("%lf", &a[i]); sort(a + 1, a + n + 1); s[n] = a[n]; for (int i = n - 1; i; i--) s[i] = s[i + 1] + a[i]; for (int i = 1; i <= n; i++) avg[i] = s[i] / (n - i + 1); double ans = 0; for (int i = 1; i <= min(n, m + 1); i++) ans = max(ans, avg[i] + min((double)(m - i + 1) / (n - i + 1), (double)k)); printf("%.10lf\n", ans); }
1,700
CPP
#include <bits/stdc++.h> using namespace std; int main() { char a[11][11]; int i, j, k, l, m, n, o, t = 0; scanf("%d %d", &n, &m); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { cin >> a[i][j]; } } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (a[i][j] == 'P') { if ((a[i - 1][j] == 'W') && (i - 1 >= 0) && (i - 1 <= n - 1)) { a[i][j] = '.'; a[i - 1][j] = '.'; t++; } else if ((a[i + 1][j] == 'W') && (i + 1 >= 0) && (i + 1 <= n - 1)) { a[i][j] = 'o'; a[i + 1][j] = 'o'; t++; } else if ((a[i][j + 1] == 'W') && (j + 1 >= 0) && (j + 1 <= m - 1)) { a[i][j] = '.'; a[i][j + 1] = '.'; t++; } else if ((a[i][j - 1] == 'W') && (j - 1 >= 0) && (j - 1 <= m - 1)) { a[i][j] = '.'; a[i][j - 1] = '.'; t++; } } } } printf("%d\n", t); }
1,100
CPP
n=int(input()) a=16*n b=15*n print(str(a)+" "+str(b))
800
PYTHON3
a, b, x = [int(x) for x in input().split()] now1 = 0 now2 = 0 n = a + b if ((x == 2) and (a == 1)): print(1, end = "") print(0, end = "") for i in range(b - 1): print(1, end = "") exit(0) if ((x % 2) == 0): if (a != x // 2): for i in range(a - (x // 2)): print(0, end = "") for i in range(b - (x // 2) + 1): print(1, end = "") for i in range(x - 1): if (i % 2 == 0): print(0, end="") else: print(1, end="") else: for i in range(b - (x // 2)): print(1, end = "") for i in range(a - (x // 2) + 1): print(0, end = "") for i in range(x - 1): if (i % 2 == 0): print(1, end="") else: print(0, end="") else: if (a != x // 2): for i in range(a - x // 2): print(0, end = "") for i in range(b - x // 2): print(1, end = "") for i in range(x - 1): if (i % 2 == 0): print(0, end = "") else: print(1, end = "") else: for i in range(b - x // 2): print(1, end = "") for i in range(a - x // 2): print(0, end = "") for i in range(x - 1): if (i % 2 == 0): print(1, end = "") else: print(0, end = "")
1,300
PYTHON3
#include <bits/stdc++.h> int stack[105], top_index; void pop() { if (top_index == 0) return; stack[top_index - 1] = -1; top_index--; } void push(int num) { stack[top_index++] = num; } int top() { if (top_index == 0) return 0; return stack[top_index - 1]; } int main() { int nChild, eliminate_index, leader_index; int i, j, nK, kstep, buffer[105], buffer_count, eliminate_num; while (scanf("%d%d", &nChild, &nK) == 2) { leader_index = 0; top_index = 0; for (i = 0; i < nChild; i++) push(i + 1); for (i = 0; i < nK; i++) { scanf("%d", &kstep); eliminate_index = (leader_index + kstep) % top_index; for (j = top_index - 1, buffer_count = 0; j > eliminate_index; j--) { buffer[buffer_count++] = top(); pop(); } eliminate_num = top(); pop(); leader_index = top_index; printf("%d ", eliminate_num); for (j = buffer_count - 1; j >= 0; j--) push(buffer[j]); } } return 0; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > V; vector<int> R1, R2, R3; int main() { int n, i; scanf("%d", &n); for (i = 1; i <= n; i++) { int a; scanf("%d", &a); V.push_back(make_pair(a, i)); } sort(V.begin(), V.end()); for (i = 0; i < n; i++) R1.push_back(V[i].second); int flag = 0; for (i = n - 1; i >= 0; i--) { if (i > 0 && V[i - 1].first == V[i].first && flag != 1) { flag++; R2.push_back(V[i - 1].second); R2.push_back(V[i].second); i--; } else R2.push_back(V[i].second); } flag = 0; for (i = n - 1; i >= 0; i--) { if (i > 0 && V[i - 1].first == V[i].first && flag != 2) { flag++; if (flag == 2 || ((i - 2) >= 0 && V[i - 2].first != V[i - 1].first)) { R3.push_back(V[i - 1].second); R3.push_back(V[i].second); i--; } else if ((i - 2) >= 0) { flag = 2; R3.push_back(V[i - 2].second); R3.push_back(V[i - 1].second); R3.push_back(V[i].second); i -= 2; } } else R3.push_back(V[i].second); } if (flag != 2) { printf("NO"); return 0; } printf("YES\n"); for (i = 0; i < n; i++) printf("%d ", R1[i]); printf("\n"); for (i = n - 1; i >= 0; i--) printf("%d ", R2[i]); printf("\n"); for (i = n - 1; i >= 0; i--) printf("%d ", R3[i]); printf("\n"); return 0; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; int maps[1001][1001]; int biao[101]; int cal(int x) { int len = 0; while (x > 0) { biao[len++] = x % 3; x /= 3; } return len; } int main() { int n, i, j, k; int ans; scanf("%d", &n); int st = 3; vector<int> vec; int lens = cal(n); for (i = 0; i < lens; i++) { if (biao[i] > 0) { vec.clear(); if (biao[i] == 1) { maps[1][st] = 1; vec.push_back(st++); } else { maps[1][st] = 1; vec.push_back(st++); maps[1][st] = 1; vec.push_back(st++); } for (int j = 0; j <= i; ++j) { for (int k = 0; k < vec.size(); ++k) { maps[vec[k]][st] = maps[vec[k]][st + 1] = maps[vec[k]][st + 2] = 1; } vec.clear(); vec.push_back(st), vec.push_back(st + 1), vec.push_back(st + 2); st += 3; } st -= 3; for (int j = 1; j <= 1000; ++j) { maps[j][st + 1] = maps[j][st + 2] = 0; } int j = i + 1; while (j < lens) { ++j; maps[st][st + 1] = 1; st++; } maps[st++][2] = 1; } } st--; for (i = 1; i <= st; i++) { for (j = 1; j <= st; j++) { if (maps[i][j]) maps[j][i] = 1; } } cout << st << endl; for (i = 1; i <= st; i++) { for (j = 1; j <= st; j++) { if (maps[i][j]) cout << "Y"; else cout << "N"; } cout << endl; } return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; long double x1, x2, Y1, y2, r1, r2, dis, a1, a2, s1, s2; signed main() { cin >> x1 >> Y1 >> r1 >> x2 >> y2 >> r2; dis = sqrt((x2 - x1) * (x2 - x1) + (y2 - Y1) * (y2 - Y1)); cout.precision(9); if (r1 + r2 <= dis) { cout << 0; return 0; } if (abs(r1 - r2) >= dis && r1 >= r2) { cout << r2 * r2 * 3.1415926535897932384; return 0; } if (abs(r1 - r2) >= dis && r2 >= r1) { cout << r1 * r1 * 3.1415926535897932384; return 0; } a1 = acos((r1 * r1 + dis * dis - r2 * r2) / (2 * r1 * dis)) * 2; a2 = acos((r2 * r2 + dis * dis - r1 * r1) / (2 * r2 * dis)) * 2; s1 = (long double)0.5 * a1 * r1 * r1 - r1 * r1 * sin(a1) * 0.5; s2 = (long double)0.5 * a2 * r2 * r2 - r2 * r2 * sin(a2) * 0.5; cout << s1 + s2; }
2,000
CPP
for _ in range(int(input())): n,k=map(int,input().split()) p=n-k if(p%2==0 and n>=k*k): print("YES") else: print("NO")
1,100
PYTHON3