solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
l=[] r=[] t=0 n=int(input()) for i in range(n): a,b=list(map(int,input().strip().split())) l +=[a] r +=[b] m=max(l.count(1),l.count(0)) t +=(n-m) s=max(r.count(1),r.count(0)) t +=(n-s) print(t)
7
PYTHON3
n = int(input()) l = list(map(int, input().split())) l.sort() l.reverse() i = 1 while (not (sum(l[:i]) > sum(l[i:]))): i += 1 print(i)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using vi = vector<int>; using ld = long double; using pll = pair<ll, ll>; using vll = vector<ll>; using pll = pair<ll, ll>; void buff() { ios::sync_with_stdio(false); cin.tie(nullptr); } constexpr ll MOD = 998244353; inline ll pow_mod(ll a, ll b, ll mod = MOD) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; } return res; } constexpr int ms = 5e5 + 13; int main() { buff(); long long res = 0ll; long long n, k; cin >> n >> k; long long start = 1; long long fat[ms]; long long inv_fat[ms]; fat[0] = 1ll; inv_fat[0] = 1ll; for (int i = 1; i < ms; ++i) fat[i] = (fat[i - 1] * i) % MOD; for (int i = 1; i < ms; ++i) inv_fat[i] = pow_mod(fat[i], MOD - 2); while (k * start <= n) { long long cand = n / start - 1; long long g = fat[cand]; g = (g * inv_fat[k - 1]) % MOD; g = (g * inv_fat[cand - (k - 1)]) % MOD; res = (res + g) % MOD; start++; } cout << res << '\n'; return 0; }
11
CPP
for _ in range(int(input())): n=int(input()) ar=(list(map(int,input().split()))) ar1=(list(map(int,input().split()))) t=min(ar) u=min(ar1) sub=0 count=0 for i in range(n): if ar[i]>t and ar1[i]>u: sub=min(ar[i],ar1[i])-min(t,u) count+=sub ar[i]-=sub ar1[i]-=sub for i in range(n): if ar[i]>t: count+=ar[i]-t for i in range(n): if ar1[i]>u: count+=ar1[i]-u print(count)
8
PYTHON3
import sys def main(): n, k, t = map(int, sys.stdin.read().strip().split()) if t > n: return n + k - t if t < k: return t return k print(main())
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main(){ char a;cin>>a; if(a=='A')a='T'; else if(a=='T')a='A'; else if(a=='C')a='G'; else a='C'; cout<<a<<endl; }
0
CPP
s, v1, v2, t1, t2 = map(int, input().split(' ')) f1 = v1 * s + 2 * t1 f2 = v2 * s + 2 * t2 if f1 < f2: print("First") elif f1 > f2: print("Second") else: print("Friendship")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int binarySearch(int arr[], int l, int r, int x) { while (l <= r) { int m = l + (r - l) / 2; if (arr[m] == x) return m; if (arr[m] < x) l = m + 1; else r = m - 1; } return -1; } int main() { int n; cin >> n; int arr[500]; for (int i = 0; i < 500; i++) { arr[i] = ((i + 1) * (i + 2)) / 2; } if (binarySearch(arr, 0, 499, n) != -1) { cout << "YES" << endl; } else { cout << "NO" << endl; } }
7
CPP
#include <bits/stdc++.h> unsigned long long ans[64] = { 205420232704ull, 4300726075392ull, 9943141842944ull, 102772080771072ull, 234260485308416ull, 723091532955648ull, 1929901949378560ull, 7962145521006592ull, 16163718647774208ull, 36001995886165504ull, 72057594037927680ull, 144115188075855744ull, 144115188075855744ull, 288230376151711680ull, 576460752303423456ull, 576460752303423456ull, 1152921504606846960ull, 2305843009213693944ull, 2305843009213693920ull, 576412923518779264ull, 71777768313191948ull, 3466203809432795196ull, 9079473177587753214ull, 9206058851405956094ull, 9220848794270097406ull, 9197581204444447742ull, 18414869574287814655ull, 18420741636752033791ull, 18426746848929380351ull, 18415823286090401791ull, 18437647845276217343ull, 18413224286464100351ull, 18420404737790451711ull, 18444146654698651647ull, 18438754119921373183ull, 18444712607746252799ull, 18442905914206388223ull, 18446482373293113343ull, 9222888249038077950ull, 9223372004642521086ull, 9223372024775180286ull, 9223372034908618750ull, 4611686010441433084ull, 4611686017353644540ull, 2296835806200851448ull, 2271494263009059832ull, 2292441381606829048ull, 1150432316586299376ull, 575754606495084512ull, 575406797197246432ull, 287974233452855232ull, 144099394269413248ull, 144050217612541824ull, 72048513284964096ull, 36027165100998144ull, 18010923323096064ull, 9005072994269184ull, 2251791223742464ull, 1125899906826240ull, 281474976645120ull, 140737488224256ull, 17592184995840ull, 4398042316800ull, 274810798080ull}; using namespace std; int main() { int n, m; cin >> n >> m; puts(ans[n] >> m & 1 ? "IN" : "OUT"); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int l, r, a, b, dif, t, x, lit, poz; bool uz[27]; char s[1001]; int main() { cin >> a >> b >> l >> r; dif = min(500, r - l); l = l % (a + b); if (l == 0) l = a + b; r = l + dif; if (l <= a) { for (int i = l; i <= min(a, r); i++) s[++poz] = i; l = a + 1; t = 1; } else { for (int i = l; i <= min(a + b, r); i++) s[++poz] = 1; l = a + b + 1; memset(uz, 0, sizeof(uz)); for (int i = poz; i >= max(1, poz - a + 1); i--) uz[s[i]] = 1; lit = 1; t = 2; } x = 0; while (l <= r) { if (t == 1) { x++; s[++poz] = s[poz - 1]; l++; if (x == b) { memset(uz, 0, sizeof(uz)); for (int i = poz; i >= max(1, poz - a + 1); i--) uz[s[i]] = 1; lit = 1; t = 2; x = 0; } } else { x++; while (uz[lit] == 1) lit++; s[++poz] = lit; l++; uz[lit] = 1; if (x == a) { t = 1; x = 0; } } } memset(uz, 0, sizeof(uz)); for (int i = 1; i <= poz; i++) { uz[s[i]] = 1; } int nr = 0; for (int i = 1; i <= 26; i++) nr += uz[i]; cout << nr; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; inline int read() { int s = 0; bool f = 0; char ch = ' '; while (!isdigit(ch)) { f |= (ch == '-'); ch = getchar(); } while (isdigit(ch)) { s = (s << 3) + (s << 1) + (ch ^ 48); ch = getchar(); } return (f) ? (-s) : (s); } inline void write(int x) { if (x < 0) { putchar('-'); x = -x; } if (x < 10) { putchar(x + '0'); return; } write(x / 10); putchar((x % 10) + '0'); } const int N = 25; int n, m, a[N], b[N], bin[N], f[1 << 5]; bool bo[N]; vector<int> wwx[N]; short dp[21][1 << 5][11][11][11][11][11]; char jyl[N][N]; inline bool dfs(int now, int zt) { int i, j, oo; bool flg; if (now == m + 1) { for (i = 1; i <= n; i++) if (a[i] != 0) return false; return true; } if (dp[now][zt][a[1]][a[2]][a[3]][a[4]][a[5]] != -1) return dp[now][zt][a[1]][a[2]][a[3]][a[4]][a[5]]; for (i = 0; i < wwx[b[now]].size(); i++) { oo = wwx[b[now]][i]; for (j = 1; j <= n; j++) if ((!(zt & bin[j - 1])) && (oo & bin[j - 1])) a[j]--; flg = 1; for (j = 1; j <= n && flg; j++) if ((a[j] < 0) || (m - now + 1) / 2 < a[j]) flg = 0; if (flg && dfs(now + 1, oo)) { for (j = 1; j <= n; j++) jyl[j][now] = (oo & bin[j - 1]) ? '*' : '.'; return dp[now][zt][a[1]][a[2]][a[3]][a[4]][a[5]] = 1; } for (j = 1; j <= n; j++) if ((!(zt & bin[j - 1])) && (oo & bin[j - 1])) a[j]++; } return dp[now][zt][a[1]][a[2]][a[3]][a[4]][a[5]] = 0; } int main() { int i, j; n = read(); m = read(); for (i = 1; i <= n; i++) a[i] = read(); for (i = 1; i <= m; i++) b[i] = read(); bin[0] = 1; for (i = 1; i <= 20; i++) bin[i] = bin[i - 1] << 1; for (i = 0; i < bin[n]; i++) { memset(bo, 0, sizeof bo); for (j = 1; j <= n; j++) if (i & bin[j - 1]) bo[j] = 1; f[i] = 0; for (j = 1; j <= n; j++) if (bo[j] && (!bo[j - 1])) f[i]++; wwx[f[i]].push_back(i); } memset(dp, -1, sizeof dp); dp[0][0][a[1]][a[2]][a[3]][a[4]][a[5]] = 1; dfs(1, 0); for (i = 1; i <= n; i++, putchar('\n')) { for (j = 1; j <= m; j++) putchar(jyl[i][j]); } return 0; }
12
CPP
import math n,k=[int(x) for x in input().split()] odd=math.ceil(n/2) if k<=odd: print(2*k-1) else: print((k-odd)*2)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> P; typedef pair<ll ,P> P3; const ll MOD = ll(1e9+7); const int IINF = INT_MAX; const ll LLINF = LLONG_MAX; const int MAX_N = int(1e5 + 5); const double EPS = 1e-6; const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0}; #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define SORT(v) sort((v).begin(), (v).end()) #define ALL(v) (v).begin(), (v).end() struct UnionFindTree { vector<int> par; vector<int> rank; vector<int> siz; void init(int n) { par.resize(n); rank.resize(n); siz.resize(n); for (int i = 0; i < n; i++) { par[i] = i; rank[i] = 0; siz[i] = 1; } } int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) swap(x, y); if (rank[x] == rank[y]) rank[x]++; par[y] = x; siz[x] += siz[y]; } bool is_same(int x, int y) { return find(x) == find(y); } int size(int x) { x = find(x); return siz[x]; } }; int main() { int n; vector<P3> es; cin >> n; REP(i,n-1){ ll a, b, c; cin >> a >> b >> c; es.push_back({c,{a,b}}); } sort(ALL(es)); UnionFindTree g; g.init(n); ll ans = 0; REP(i,n){ ll a, b, c; a = es[i].second.first; b = es[i].second.second; c = es[i].first; ans += c*ll(g.size(a))*ll(g.size(b)); g.unite(a,b); } cout << ans << endl; return 0; }
0
CPP
n=int(input()) count=0 for i in range(n): l=list(map(int,input().strip().split())) if l.count(1)>1: count=count+1 print(count)
7
PYTHON3
#include <iostream> #include <string> #include <vector> #include <set> #include <algorithm> using namespace std; int main() { for (;;) { string g; cin >> g; if ("#" == g) { break; } int y, m, d; cin >> y >> m >> d; if (y > 31 || y == 31 && m >= 5) { cout << "? " << y - 30 << " " << m << " " << d << endl; } else { cout << g << " " << y << " " << m << " " << d << endl; } } return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; long long mod_pow(long long a, long long p) { if (a == 0) return 0; if (p == 0) return 1; if (a == 1) return 1; long long tmp = mod_pow(a, p / 2); tmp = (tmp * tmp) % 1000000007; if (p % 2) tmp = (tmp % 1000000007) * (a % 1000000007); return tmp % 1000000007; } int main() { long long n; cin >> n; if (n == 0) cout << "1" << endl; else { long long f = mod_pow(4, n); long long t = mod_pow(2, n - 1); long long t2 = (t * 2) % 1000000007; long long tmp = (((t2 - 1) % 1000000007) * t) % 1000000007; long long ans; if (f >= tmp) ans = (f - tmp) % 1000000007; else ans = (f - tmp) % 1000000007 + 1000000007; cout << ans << endl; } return 0; }
9
CPP
n = input() listOfInputs = list(input().split()) if "1" in listOfInputs: print("HARD") else: print("EASY")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int getbit(long long x) { int cnt = 0; while (x) { cnt += x % 10; x /= 10; } return cnt; } int main() { long long n; cin >> n; long long res = 0; long long cnt = 1; for (long long i = sqrt(n) + 1; i >= 1; i--) { if (cnt >= 4000000) break; if (i * i + getbit(i) * i == n) { res = i; } cnt++; } if (res == 0) puts("-1"); else cout << res << endl; }
8
CPP
from collections import Counter in_n = input() in_nums= input() n = int(in_n) nums_counter = Counter([int(x) for x in in_nums.split()]) def getMaxScore(keys): saved = [0] cur_key_chain = [] last_num = None for key in keys: cur_key_chain.append(key) if last_num is not None and last_num + 1 == key: saved.append(max((key * nums_counter[key]) + saved[len(saved) - 2], saved[len(saved) - 1])) else: saved.append(key * nums_counter[key] + saved[len(saved) - 1]) last_num = key return(saved[len(saved) - 1]) print(getMaxScore(sorted(list(nums_counter.keys()))))
7
PYTHON3
#include<bits/stdc++.h> using namespace std; int main(){ int n,k,x=0;cin>>n>>k; vector<int>a(n);for(int i=0;i<n;i++)cin>>a[i]; sort(a.begin(),a.end()); for(int i=0;i<k;i++)x+=a[i]; cout<<x<<endl; }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, k, i, j; char s[200]; scanf("%d%d ", &n, &k); scanf("%s", s); if (k <= n / 2) { for (i = 1; i < k; i++) { printf("LEFT\n"); } printf("PRINT %c\n", s[0]); for (i = 1; i < n; i++) { printf("RIGHT\n"); printf("PRINT %c\n", s[i]); } } else { for (i = k; i < n; i++) { printf("RIGHT\n"); } printf("PRINT %c\n", s[n - 1]); for (i = n - 2; i >= 0; i--) { printf("LEFT\n"); printf("PRINT %c\n", s[i]); } } return 0; }
7
CPP
def enumeration(a): y = 0 res = 0 while 2 ** y <= a: res += -1 * (2 ** y) y += 1 return res * 2 t = int(input()) for i in range(t): n = int(input()) sum = n * (n + 1) // 2 print(sum + enumeration(n))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; template <typename A> ostream &operator<<(ostream &cout, vector<A> const &v) { cout << "["; for (int i = 0; i < v.size(); i++) { if (i) cout << ", "; cout << v[i]; } return cout << "]"; } template <typename A, typename B> ostream &operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.first << ", " << p.second << ")"; } template <typename A, typename B> istream &operator>>(istream &cin, pair<A, B> &p) { cin >> p.first; return cin >> p.second; } long long int const mod = 998244353; long long int const maxn = 1e6 + 9; void solve() { int n, m; cin >> n >> m; int lol[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cin >> lol[i][j]; } int c = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (c == 0) { if (j % 2 == 0) { if (lol[i][j] % 2 == 1) lol[i][j]++; } else { if (lol[i][j] % 2 == 0) lol[i][j]++; } } else { if (j % 2 == 0) { if (lol[i][j] % 2 == 0) lol[i][j]++; } else { if (lol[i][j] % 2 == 1) lol[i][j]++; } } cout << lol[i][j] << " "; } c ^= 1; cout << endl; } cout << endl; } int main() { int tc; cin >> tc; while (tc--) solve(); }
9
CPP
#include <cstdio> #include <cstring> #include <algorithm> const int Mod = 1000000007; const int MN = 300005; inline void Add(int &x, int y) { x -= (x += y) >= Mod ? Mod : 0; } int N; char S[MN]; int t0[7] = {2, 4, 0, 0, 6, 5, 4}, t1[7] = {3, 5, 0, 1, 1, 5, 4}; int f[7], g[7]; int main() { scanf("%s", S + 1), N = strlen(S + 1); f[0] = S[1] != '1', f[1] = S[1] != '0'; for (int i = 2; i <= N; ++i) { for (int j = 0; j < 7; ++j) g[j] = 0; for (int j = 0; j < 7; ++j) { if (S[i] != '1') Add(g[t0[j]], f[j]); if (S[i] != '0') Add(g[t1[j]], f[j]); } std::swap(f, g); } printf("%d\n", (f[1] + f[5]) % Mod); return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; #define int long long bool isPalin(string s) { string srev(s.rbegin(), s.rend()); return s == srev; } void solve() { string s; cin >> s; if (!isPalin(s + "a")) { cout << "YES\n"; cout << s + "a" << "\n"; } else if (!isPalin("a" + s)) { cout << "YES\n"; cout << "a" + s << "\n"; } else { cout << "NO\n"; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); int32_t t; cin >> t; while (t--) solve(); return 0; }
7
CPP
# -*- coding: utf-8 -*- if __name__ == '__main__': layers = int(input()) feeling = str() for layer in range(1, layers + 1): if layer % 2 == 1: feeling += "that I hate " else: feeling += "that I love " feeling += "it" feeling = feeling.split(" ", 1)[-1].strip() print(feeling)
7
PYTHON3
n,k=map(int,input().split()) if n==0: print(0) exit(1) arr = list(map(int,input().split())) players = 0 for x in range(n): if arr[x]>=arr[k-1] and arr[x]>0: players+=1 print(players)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 2e2 + 5; int main() { int n, k; scanf("%d %d", &n, &k); priority_queue<pair<int, int> > pq; vector<int> vc; map<pair<int, int>, multiset<int> > mp; for (int i = 0; i < n; i++) { int x; scanf("%d", &x); vc.push_back(x); pq.push({x, i}); } for (int pt1 = 0; pt1 < n; pt1++) { for (int pt2 = 0; pt2 < n; pt2++) { for (int x = pt1; x <= pt2; x++) mp[{pt1, pt2}].insert(vc[x]); } } int ans = INT_MIN; for (auto it = mp.begin(); it != mp.end(); it++) { int l = it->first.first; int r = it->first.second; multiset<int> st = it->second; priority_queue<pair<int, int> > tmp = pq; int val = k; int sum = 0; for (auto &i : st) { sum += i; if (val > 0) while (tmp.size()) { int number = tmp.top().first; int index = tmp.top().second; tmp.pop(); if (number < i) break; if (index < l || index > r) { sum -= i; sum += number; break; } } val--; } ans = max(ans, sum); } cout << ans << endl; return 0; }
7
CPP
n = int(input()) l = [int(i) for i in input().split()] l.sort(reverse = True) if len(l) == 1: print(l[0]) elif len(l) == 2: print(2 * sum(l)) else: t = - l[0] for i in l: t += i * (n + 1) n -= 1 print(t)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("O3") const int MAX_MEM = 2e8; int mpos = 0; char mem[MAX_MEM]; inline void *operator new(size_t n) { char *res = mem + mpos; mpos += n; assert(mpos <= MAX_MEM); return (void *)res; } inline void operator delete(void *) {} int i, j, k, l, n, m, mn = ((int)1e9 + 9ll); pair<int, int> a[1000009], b[1000009]; int suff[1000009], pos[1000009]; vector<int> nums; inline bool cmp(pair<int, int> a, pair<int, int> b) { if (a.first != b.first) return a.first < b.first; return a.second < b.second; } vector<int> vs[1000009]; priority_queue<pair<int, int> > q; int res[1000009]; vector<pair<int, int> > dp[1000009]; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> m; for (i = 1; i <= n; i++) cin >> a[i].first >> a[i].second; for (i = 1; i <= m; i++) cin >> b[i].first >> b[i].second; sort(b + 1, b + 1 + m, cmp); sort(a + 1, a + 1 + n, cmp); for (i = m; i > 0; i--) { suff[i] = max(suff[i + 1], b[i].second); } suff[m + 1] = -((int)1e9 + 9ll); for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (b[j].first >= a[i].first) { pos[i] = j; q.push({suff[j] - a[i].second + 1, i}); res[i] = suff[j] - a[i].second + 1; break; } } if (pos[i] == 0) { pos[i] = m + 1; q.push({-((int)1e9 + 9ll) - a[i].second + 1, i}); res[i] = -((int)1e9 + 9ll) - a[i].second + 1; } } long long kilk = 0; for (i = 1; i <= n; i++) { for (j = pos[i]; j <= m; j++) { if (b[j].first == b[j - 1].first) continue; dp[b[j].first - a[i].first + 1].push_back( {suff[j + 1] - a[i].second + 1, i}); } } int ans = max(0, q.top().first); for (i = 1; i <= 1000001; i++) { for (auto to : dp[i]) { res[to.second] = to.first; q.push({res[to.second], to.second}); } while (!q.empty() && q.top().first != res[q.top().second]) q.pop(); ans = min(ans, i + max(0, q.top().first)); } cout << ans << '\n'; exit(0); }
10
CPP
import sys input = sys.stdin.readline def I(): return int(input()) def MI(): return map(int, input().split()) def LI(): return list(map(int, input().split())) def main(): mod=10**9+7 inf=10**6 N,K=MI() P=LI() inf=10**6 class SegTree: def __init__(self,N,ide,segfunc=min): self.ide_ele = ide """ ex) 最小値のセグ木 → +inf 和のセグ木 → 0 積のセグ木 → 1 gcdのセグ木 → 0 """ self.segfunc=segfunc #num:N以上の最小の2のべき乗 self.num =2**(N-1).bit_length() self.seg=[self.ide_ele]*2*self.num #リストで初期化する def setL(self,init_val): #init_valは操作する数列 for i in range(N): self.seg[i+self.num-1]=init_val[i] #built for i in range(self.num-2,-1,-1) : self.seg[i]=self.segfunc(self.seg[2*i+1],self.seg[2*i+2]) #k番目の値をxに更新 def update(self,k,x): k += self.num-1 self.seg[k] = x while k: k = (k-1)//2 self.seg[k] = self.segfunc(self.seg[k*2+1],self.seg[k*2+2]) #[p,q)の区間に対するクエリへの応答 def query(self,p,q): if q<=p: return self.ide_ele p += self.num-1 q += self.num-2 res=self.ide_ele while q-p>1: if p&1 == 0: res = self.segfunc(res,self.seg[p]) if q&1 == 1: res = self.segfunc(res,self.seg[q]) q -= 1 p = p//2 q = (q-1)//2 if p == q: res = self.segfunc(res,self.seg[p]) else: res = self.segfunc(self.segfunc(res,self.seg[p]),self.seg[q]) return res """ 長さKの尺取りぽく見るか. 最小値が抜けて最大値が入る場合は不変. ただし,並び替え後も元の物と変わらない場合を何度かカウントしそう. 事前に(元から昇順になっている長さKの列)の個数を数えておこうか. 後からこれを引くのはダメ,事前に計算しておかないと,すでに照準になっているものを数えているのか数えていないのか不明になる. これは簡単に求まる.次が自分以上なら1をつけて,長さKの尺取りで和がKならおk. 累積和の方が実装が楽かなあ """ ans=1#最初のK個を並び替えたものは無条件で数える mseg=SegTree(N,inf,min) Mseg=SegTree(N,-1,max) mseg.setL(P) Mseg.setL(P) #累積和パート S=[0]*(N+1) for i in range(N-1): if P[i]<P[i+1]: S[i+1]=1 #print(S) for i in range(N): S[i+1]+=S[i] m=min(P[:K]) M=max(P[:K]) cnt=0#最初と同じ配列のものを何回数えたか if S[K]==K:#最初のKこを並び替えたものを1回とすでに数えてしまっているのでチェック cnt+=1 for i in range(K,N): if m==P[i-K] and P[i]>M:#最小値が抜けて,新しく入るのが最大値を更新するなら m=mseg.query(i-K+1,i+1) M=P[i] else: m=mseg.query(i-K+1,i+1) M=Mseg.query(i-K+1,i+1) #print(i,S[i]-S[i-K+1],S[i],S[i-K+1]) if cnt==0: ans+=1 elif S[i]-S[i-K+1]!=(K-1): ans+=1 if S[i]-S[i-K+1]==(K-1): cnt+=1 #print(i,m,M,ans,cnt) print(ans) #print(S) main()
0
PYTHON3
#include <bits/stdc++.h> using namespace std; bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = "RDLU"; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? "Yes" : "No"); exit(0); } void addmod(int &x, int y, int mod = 1000000007) { assert(y >= 0); x += y; if (x >= mod) x -= mod; assert(x >= 0 && x < mod); } void et() { puts("-1"); exit(0); } bool isLft(long long tar) { pair<long long, long long> mid = {2, ln - 1}; map<long long, long long> L, R; long long cnt = lk; L[0] = 0; R[0] = 0; while (cnt) { long long midlen = mid.second - mid.first + 1; long long lLen = (*L.rbegin()).first; long long rLen = R.rbegin()->first; long long MM = max({midlen, lLen, rLen}); if ((MM + 1) / 2 == (lLen + 1) / 2) { if (L.rbegin()->second >= cnt) return 1; cnt -= L.rbegin()->second; L[L.rbegin()->first / 2] += L.rbegin()->second; L[(L.rbegin()->first - 1) / 2] += L.rbegin()->second; L.erase(prev(L.end())); } else if ((MM + 1) / 2 == (midlen + 1) / 2) { long long C = (mid.first + mid.second) / 2; if (cnt == 1) return C <= tar; cnt--; long long x = mid.first, y = mid.second; mid = {0, -1}; if (C - 1 <= tar) { L[C - x]++; } else { mid = {x, C - 1}; } if (tar < C + 1) { R[y - C]++; } else mid = {C + 1, y}; } else { if (R.rbegin()->second >= cnt) return 0; cnt -= R.rbegin()->second; R[R.rbegin()->first / 2] += R.rbegin()->second; R[(R.rbegin()->first - 1) / 2] += R.rbegin()->second; R.erase(prev(R.end())); } } return 0; } void fmain(int tid) { scanf("%lld%lld", &ln, &lk); if (lk == 1) puts("1"); else if (lk == 2) { printf("%lld\n", ln); } else { lk -= 2; long long l = 2, r = ln - 1, ans = ln - 1; while (l <= r) { long long mid = (l + r) / 2; if (isLft(mid)) { ans = mid; r = mid - 1; } else l = mid + 1; } printf("%lld\n", ans); } } int main() { int t = 1; for (int(i) = 1; (i) <= (int)(t); (i)++) { fmain(i); } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<int,int>; #define rep(i, n) for(int i=0; i<(int)(n); ++i) const int D = 80*160 + 10; using bs = bitset<2*D>; bs dp[85][85]; int a[85][85]; int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int h, w; cin >> h >> w; rep(i, h) rep(j, w) cin >> a[i][j]; rep(i, h) rep(j, w) { int x; cin >> x; a[i][j] = abs(x-a[i][j]); } dp[0][0][D-a[0][0]] = 1; dp[0][0][D+a[0][0]] = 1; rep(i, h) rep(j, w) { if (i) { dp[i][j] |= dp[i-1][j]<<a[i][j]; dp[i][j] |= dp[i-1][j]>>a[i][j]; } if (j) { dp[i][j] |= dp[i][j-1]<<a[i][j]; dp[i][j] |= dp[i][j-1]>>a[i][j]; } } rep(i, D) { if (dp[h-1][w-1][D-i] || dp[h-1][w-1][D+i]) { cout << i << endl; return 0; } } }
0
CPP
#include <bits/stdc++.h> using namespace std; void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> a(n + 1); for (int i = 1; i <= n; i++) cin >> a[i]; vector<vector<int>> dp(n + 1, vector<int>(4, 0)); dp[1][0] = dp[1][1] = a[1]; dp[1][2] = dp[1][3] = 1e9; for (int i = 2; i <= n; i++) { dp[i][0] = dp[i - 1][3] + a[i]; dp[i][1] = min(dp[i - 1][3], dp[i - 1][0]) + a[i]; dp[i][2] = dp[i - 1][1]; dp[i][3] = min(dp[i - 1][2], dp[i - 1][1]); } cout << min({dp[n][0], dp[n][1], dp[n][2], dp[n][3]}); cout << "\n"; } return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxn = 1e5 + 10; const int inf = 0x3f3f3f3f; const double eps = 1e-14; const double pi = acos(-1.0); int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; int a[maxn]; int main() { int n, d; while (~scanf("%d%d", &n, &d)) { for (int i = 0; i < n; i++) scanf("%d", &a[i]); int minn = 0, maxx = 0; int res = 0; for (int i = 0; i < n; i++) { if (!a[i]) { if (minn < 0) minn = 0; if (maxx < 0) { maxx = d, res++; } } else { minn += a[i]; maxx += a[i]; if (minn > d) { printf("-1\n"); return 0; } if (maxx > d) maxx = d; } } printf("%d\n", res); } return 0; }
10
CPP
N, K = map(int, input().split()) if K==0: print(N**2) exit() res = 0 for b in range(1,N+1): res += (N//b)*max(b-K,0) + max(N%b-K+1, 0) print(res)
0
PYTHON3
#include <iostream> #include <queue> #include <stdio.h> using namespace std; int n,k,ans,cnt[30],tmp[30],jum; string x; priority_queue < int > pq; int main() { scanf("%d %d",&n,&k); for(int i=0;i<n;i++) { cin>>x; cnt[x[0]-'A']++; } for(int i=0;i<26;i++) { if(cnt[i]) pq.push(cnt[i]); } while(pq.size()>=k) { jum = 0; for(int i=0;i<k;i++) { tmp[jum++] = pq.top()-1; pq.pop(); } ans+=1; for(int i=0;i<jum;i++) { if(tmp[i]) pq.push(tmp[i]); } } printf("%d\n",ans); return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { int k, n, w, sum = 0; cin >> k; cin >> n; cin >> w; for (int i = 1; i <= w; i++) { sum += (k * i); } if (sum >= n) { cout << (sum - n); return 0; } cout << 0; return 0; }
7
CPP
t=int(input()) for q in range(t): n,x=map(int,input().split()) a=list(map(int,input().split())) if x in a: print(1) else: if x%max(a)!=0: print(max(2,x//(max(a))+1)) else: print(max(2,x//max(a)))
8
PYTHON3
n = int(input()) ln = input() tmp_str = ln if len(tmp_str.replace('4', '').replace('7', '')) > 0: print('NO') else: sum_1 = sum(map(int, list(ln[:n//2]))) sum_2 = sum(map(int, list(ln[n//2:]))) if sum_1 == sum_2: print('YES') else: print('NO')
7
PYTHON3
x,a,b=map(int,input().split()) a=abs(x-a) b=abs(x-b) print('A' if a<b else 'B')
0
PYTHON3
#code y = [int(x) for x in input().split()] n=y[0] l=y[1] a=[int(x) for x in input().split()] a.sort() ans=a[0] if(n==2): ans=max(a[0],(a[1]-a[0])/2) ans=max(ans,l-a[1]) print(ans) exit(0) for i in range(1,n-1): ans=max(ans,max((a[i]-a[i-1])/2,(a[i+1]-a[i])/2)); ans=max(ans,(l-a[n-1])) print(ans)
8
PYTHON3
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> P; const ll mod = 1e9+7; #define p_ary(ary,a,b,i) do { cout << "["; for (int (i) = (a);(i) < (b);++(i)) cout << ary[(i)] << ((b)-1 == (i) ? "" : ", "); cout << "]\n"; } while(0) #define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0) const int MAX_N = 1000010; ll fact[MAX_N],fact_inv[MAX_N],inv[MAX_N]; ll pow_mod(ll a,ll b) { ll ret; if (b < 0) ret = pow_mod(a,mod+b-1); else if (b == 0) ret = 1; else if (b == 1) ret = a; else { ll c = pow_mod(a,b/2); if (b%2) ret = (c*c)%mod*a%mod; else ret = c*c%mod; } return ret; } void create_table(int n) { fact[0] = 1;fact[1] = 1; for (int i = 2;i <= n;++i) fact[i] = fact[i-1]*i%mod; fact_inv[n] = pow_mod(fact[n],-1); for (int i = n;i > 0;--i) fact_inv[i-1] = fact_inv[i]*i%mod; for (int i = 1;i <= n;++i) inv[i] = fact_inv[i]*fact[i-1]%mod; } ll combi(ll a, ll b) { return fact[a]*fact_inv[b]%mod*fact_inv[a-b]%mod; } int main() { int n,h,d; cin >> n >> h >> d; vector<ll> dp(h+1,0); create_table(n); dp[0] = fact[n]; ll sum = 0,a = 0; for (int i = 1;i < n+1;++i) (a += fact[i]) %= mod; for (int i = 0;i < h;++i) { (sum += dp[i]) %= mod; if (i >= d) (sum -= dp[i-d]) %= mod; dp[i+1] = sum*a%mod; } cout << (sum+mod)%mod << endl; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 300; bool ispr[MAX + 305]; vector<int> primes; void sieve() { memset(ispr, 1, sizeof ispr); for (int i = 4; i <= MAX; i += 2) ispr[i] = 0; ispr[0] = ispr[1] = 0; primes.push_back(2); for (int i = 3; i <= MAX; i++) { if (ispr[i]) { primes.push_back(i); for (long long j = (long long)i * i; j <= MAX; j += i) ispr[j] = 0; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); sieve(); int n; cin >> n; vector<int> all(n), pos(n); for (int i = 0; i < n; i++) { cin >> all[i]; } vector<pair<int, int> > ans; for (int i = 0; i < n;) { int num = all[i]; int need = num - 1; if (i != need) { int j = i; int diff = abs(j - need) + 1; while (diff > 1) { auto it = lower_bound(primes.begin(), primes.end(), diff); if (*it > diff) it--; ans.push_back({j + 1, j + *it}); swap(all[j], all[j + *it - 1]); j += (*it) - 1; diff = abs(j - need) + 1; } } else i++; } cout << ans.size() << '\n'; for (pair<int, int> x : ans) cout << x.first << " " << x.second << '\n'; return 0; }
9
CPP
n,m = map(int, input().split()) s = input().split() t = input().split() tests = int(input()) for i in range(tests): num = int(input()) print(s[(num-1)%n] + t[(num-1)%m])
7
PYTHON3
#include <bits/stdc++.h> using namespace std; template <typename T, typename V> ostream &operator<<(ostream &out, const pair<T, V> x) { out << "{" << x.first << " : " << x.second << "}"; return out; } template <typename T> ostream &operator<<(ostream &out, const set<T> x) { for (auto &it : x) { out << it << " "; } return out; } template <typename T> ostream &operator<<(ostream &out, const multiset<T> x) { for (auto &it : x) { out << it << " "; } return out; } template <typename T, typename V> ostream &operator<<(ostream &out, const map<T, V> x) { for (auto &it : x) { out << "[" << it.first << "]" << " = " << it.second << "\n"; } return out; } template <typename T> ostream &operator<<(ostream &out, const vector<T> x) { for (int i = 0; i < x.size() - 1; ++i) { out << x[i] << " "; } out << x.back(); return out; } template <typename T> ostream &operator<<(ostream &out, const vector<vector<T> > x) { for (int i = 0; i < x.size() - 1; ++i) { out << "[" << i << "]" << " = {" << x[i] << "}\n"; } out << "[" << x.size() - 1 << "]" << " = {" << x.back() << "}\n"; return out; } const int N = 1e6 + 5; const int MOD = 1e9 + 7; const int INF = 1e9; pair<long long, long long> x[1005][1005]; char ans[1005][1005]; int n; void bfs(int i, int j) { if (!ans[i + 1][j] && x[i][j] == x[i + 1][j]) { ans[i + 1][j] = 'U'; bfs(i + 1, j); } if (!ans[i - 1][j] && x[i][j] == x[i - 1][j]) { ans[i - 1][j] = 'D'; bfs(i - 1, j); } if (!ans[i][j + 1] && x[i][j] == x[i][j + 1]) { ans[i][j + 1] = 'L'; bfs(i, j + 1); } if (!ans[i][j - 1] && x[i][j] == x[i][j - 1]) { ans[i][j - 1] = 'R'; bfs(i, j - 1); } } bool check(int i, int j) { if (x[i - 1][j] == (pair<long long, long long>){-1, -1}) { ans[i][j] = 'U'; return true; } if (x[i][j - 1] == (pair<long long, long long>){-1, -1}) { ans[i][j] = 'L'; return true; } if (x[i + 1][j] == (pair<long long, long long>){-1, -1}) { ans[i][j] = 'D'; return true; } if (x[i][j + 1] == (pair<long long, long long>){-1, -1}) { ans[i][j] = 'R'; return true; } return false; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < 1005; ++i) { for (int j = 0; j < 1005; ++j) { x[i][j] = {-2, -2}; } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { cin >> x[i][j].first >> x[i][j].second; } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (i == x[i][j].first && j == x[i][j].second) { ans[i][j] = 'X'; bfs(i, j); } } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (x[i][j] == (pair<long long, long long>){-1, -1}) { if (!check(i, j)) { cout << "INVALID"; return 0; } } } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (ans[i][j] == 0) { cout << "INVALID"; return 0; } } } cout << "VALID\n"; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { cout << ans[i][j]; } cout << "\n"; } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 2e3 + 5; int x[maxn], p[maxn]; int a, n, m, l, r; int dp[maxn][maxn]; vector<int> List[maxn]; bool rain[maxn]; int update(int a, int b) { if (a == -1 && b == -1) return -1; if (a == -1) return b; if (b == -1) return a; return min(a, b); } bool cmp(int a, int b) { return p[a] < p[b]; } int main() { scanf("%d%d%d", &a, &n, &m); a++; for (int i = 0; i < n; i++) { scanf("%d%d", &l, &r); l++, r++; for (int j = l; j < r; j++) rain[j] = true; } for (int i = 1; i <= m; i++) { scanf("%d%d", &x[i], &p[i]); x[i]++; List[x[i]].push_back(i); } for (int i = 1; i <= a; i++) sort(List[i].begin(), List[i].end(), cmp); memset(dp, -1, sizeof(dp)); dp[0][0] = 0; p[0] = 0; for (int i = 0; i < a; i++) for (int j = 0; j <= m; j++) { if (dp[i][j] < 0) continue; if (rain[i + 1]) { if (j != 0) dp[i + 1][j] = update(dp[i + 1][j], dp[i][j] + p[j]); if (List[i + 1].size()) dp[i + 1][List[i + 1][0]] = update(dp[i + 1][List[i + 1][0]], dp[i][j] + p[List[i + 1][0]]); } else { dp[i + 1][0] = update(dp[i + 1][0], dp[i][j]); if (j != 0) dp[i + 1][j] = update(dp[i + 1][j], dp[i][j] + p[j]); if (List[i + 1].size()) dp[i + 1][List[i + 1][0]] = update(dp[i + 1][List[i + 1][0]], dp[i][j] + p[List[i + 1][0]]); } } int ans = -1; for (int j = 0; j <= m; j++) ans = update(ans, dp[a][j]); printf("%d\n", ans); return 0; }
12
CPP
t = int(input()) for i in range(t): first = list(map(int, input().split())) second = list(map(int, input().split())) if max(first) == max(second) and min(first)+ min(second)==max(first): print("Yes") else: print("No")
8
PYTHON3
import sys, string def convert(ch): return ord(ch) - ord('a') tc = int(sys.stdin.readline()) for _ in range(tc): n, m = map(int, sys.stdin.readline().split()) arr = list(sys.stdin.readline().rstrip()) res = [[0] * 26 for _ in range(n)] temp = list(map(int, sys.stdin.readline().split())) ans = [0] * 26 res[0][convert(arr[0])] = 1 for i in range(1, n): for j in range(26): res[i][j] = res[i - 1][j] res[i][convert(arr[i])] += 1 for i in temp: for j in range(26): ans[j] += res[i - 1][j] for i in arr: ans[convert(i)] += 1 print(' '.join(map(str, ans)))
9
PYTHON3
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n; cin >> n; vector<long long> tpa[6], tpb[4]; long long amt[6]; fill(amt, amt + 6, 0); for (long long i = 0; i < n; ++i) { long long a, b, c; cin >> a >> b >> c; --a; --c; if (c < a) { swap(a, c); } if (a == 0 && c == 0) { tpb[0].push_back(b); } else if (a == 0 && c == 1) { tpa[0].push_back(b); ++amt[0]; } else if (a == 0 && c == 2) { tpa[1].push_back(b); ++amt[1]; } else if (a == 0 && c == 3) { tpa[2].push_back(b); ++amt[2]; } else if (a == 1 && c == 1) { tpb[1].push_back(b); } else if (a == 1 && c == 2) { tpa[3].push_back(b); ++amt[3]; } else if (a == 1 && c == 3) { tpa[4].push_back(b); ++amt[4]; } else if (a == 2 && c == 2) { tpb[2].push_back(b); } else if (a == 2 && c == 3) { tpa[5].push_back(b); ++amt[5]; } else { tpb[3].push_back(b); } } for (long long i = 0; i < 6; ++i) { if (i < 4) sort((tpb[i]).begin(), (tpb[i]).end(), greater<long long>()); sort((tpa[i]).begin(), (tpa[i]).end(), greater<long long>()); } long long ans = 0; for (long long i = 0; i < 4; ++i) { long long cr = 0; for (long long j : tpb[i]) { cr += j; } ans = max(ans, cr); } long long val[amt[0] + 1][amt[1] + 1][amt[2] + 1][amt[3] + 1][amt[4] + 1] [amt[5] + 1]; memset(val, 0, sizeof val); for (long long a = 0; a <= amt[0]; ++a) { for (long long b = 0; b <= amt[1]; ++b) { for (long long c = 0; c <= amt[2]; ++c) { for (long long d = 0; d <= amt[3]; ++d) { for (long long e = 0; e <= amt[4]; ++e) { for (long long f = 0; f <= amt[5]; ++f) { if (!a && !b && !c && !d && !e && !f) continue; if (a) { val[a][b][c][d][e][f] = max(val[a][b][c][d][e][f], val[a - 1][b][c][d][e][f] + tpa[0][a - 1]); } if (b) { val[a][b][c][d][e][f] = max(val[a][b][c][d][e][f], val[a][b - 1][c][d][e][f] + tpa[1][b - 1]); } if (c) { val[a][b][c][d][e][f] = max(val[a][b][c][d][e][f], val[a][b][c - 1][d][e][f] + tpa[2][c - 1]); } if (d) { val[a][b][c][d][e][f] = max(val[a][b][c][d][e][f], val[a][b][c][d - 1][e][f] + tpa[3][d - 1]); } if (e) { val[a][b][c][d][e][f] = max(val[a][b][c][d][e][f], val[a][b][c][d][e - 1][f] + tpa[4][e - 1]); } if (f) { val[a][b][c][d][e][f] = max(val[a][b][c][d][e][f], val[a][b][c][d][e][f - 1] + tpa[5][f - 1]); } } } } } } } for (long long i = 0; i < 16; ++i) { for (long long j = 0; j < 6; ++j) { amt[j] = tpa[j].size(); } long long cur = 0; bool adj[4]; fill(adj, adj + 4, false); for (long long j = 0; j < 4; ++j) { if (i & (1 << j)) { for (long long k : tpb[j]) { cur += k; } } else { adj[j] = 1; if (j == 0) { amt[0] = amt[1] = amt[2] = 0; } else if (j == 1) { amt[0] = amt[3] = amt[4] = 0; } else if (j == 2) { amt[1] = amt[3] = amt[5] = 0; } else if (j == 3) { amt[2] = amt[4] = amt[5] = 0; } } } if (!adj[0] && !amt[0] && !amt[1] && !amt[2]) continue; if (!adj[1] && !amt[0] && !amt[3] && !amt[4]) continue; if (!adj[2] && !amt[1] && !amt[3] && !amt[5]) continue; if (!adj[3] && !amt[2] && !amt[4] && !amt[5]) continue; bitset<4> dp[amt[0] + 1][amt[1] + 1][amt[2] + 1][amt[3] + 1][amt[4] + 1] [amt[5] + 1]; for (long long j = 0; j < 4; ++j) { dp[amt[0]][amt[1]][amt[2]][amt[3]][amt[4]][amt[5]][j] = 1; } for (long long a = amt[0]; a >= 0; --a) { for (long long b = amt[1]; b >= 0; --b) { for (long long c = amt[2]; c >= 0; --c) { for (long long d = amt[3]; d >= 0; --d) { for (long long e = amt[4]; e >= 0; --e) { for (long long f = amt[5]; f >= 0; --f) { if (a != amt[0]) { if (dp[a + 1][b][c][d][e][f][0]) { dp[a][b][c][d][e][f][1] = 1; } if (dp[a + 1][b][c][d][e][f][1]) { dp[a][b][c][d][e][f][0] = 1; } } if (b != amt[1]) { if (dp[a][b + 1][c][d][e][f][0]) { dp[a][b][c][d][e][f][2] = 1; } if (dp[a][b + 1][c][d][e][f][2]) { dp[a][b][c][d][e][f][0] = 1; } } if (c != amt[2]) { if (dp[a][b][c + 1][d][e][f][0]) { dp[a][b][c][d][e][f][3] = 1; } if (dp[a][b][c + 1][d][e][f][3]) { dp[a][b][c][d][e][f][0] = 1; } } if (d != amt[3]) { if (dp[a][b][c][d + 1][e][f][2]) { dp[a][b][c][d][e][f][1] = 1; } if (dp[a][b][c][d + 1][e][f][1]) { dp[a][b][c][d][e][f][2] = 1; } } if (e != amt[4]) { if (dp[a][b][c][d][e + 1][f][3]) { dp[a][b][c][d][e][f][1] = 1; } if (dp[a][b][c][d][e + 1][f][1]) { dp[a][b][c][d][e][f][3] = 1; } } if (f != amt[5]) { if (dp[a][b][c][d][e][f + 1][2]) { dp[a][b][c][d][e][f][3] = 1; } if (dp[a][b][c][d][e][f + 1][3]) { dp[a][b][c][d][e][f][2] = 1; } } } } } } } } long long cans = 0; for (long long a = max(0LL, amt[0]); a >= 0; --a) { for (long long b = max(0LL, amt[1]); b >= 0; --b) { for (long long c = max(0LL, amt[2]); c >= 0; --c) { for (long long d = max(0LL, amt[3]); d >= 0; --d) { for (long long e = max(0LL, amt[4]); e >= 0; --e) { for (long long f = max(0LL, amt[5]); f >= 0; --f) { bool usd[4]; fill(usd, usd + 4, 0); if (a < amt[0] || b < amt[1] || c < amt[2]) usd[0] = 1; if (a < amt[0] || d < amt[3] || e < amt[4]) usd[1] = 1; if (b < amt[1] || d < amt[3] || f < amt[5]) usd[2] = 1; if (c < amt[2] || e < amt[4] || f < amt[5]) usd[3] = 1; bool ok = 0; for (long long j = 0; j < 4; ++j) { if (dp[a][b][c][d][e][f][j]) ok = 1; if (i & (1 << j)) { if (!usd[j]) { ok = false; break; } } } if (!ok) continue; cans = max(cans, cur + val[amt[0] - a][amt[1] - b][amt[2] - c] [amt[3] - d][amt[4] - e][amt[5] - f]); } } } } } } for (long long j = 0; j < 6; ++j) { amt[j] = tpa[j].size(); } ans = max(ans, cans); } cout << ans << "\n"; }
11
CPP
# Reach Median (manthan-18) n,s=input().split() n,s=int(n),int(s) a=[int(i) for i in input().split()] c=0 a.sort() for i in range(n//2): if a[i]>s: c+=a[i]-s if a[n-1-i]<s: c+=s-a[n-1-i] c+=abs(s-a[n//2]) print(c)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; long long i, n, a, b, o; pair<long long, long long> s[1001000]; void solve(long long f) { long long t = 0; vector<long long> ansa, ansb; for (i = 1; i <= n; i++) { if (1.0 * a / i <= s[i].first) { ansa.push_back(s[i].second); break; } ansa.push_back(s[i].second); } t = i; if (t > n) return; for (i = t + 1; i <= n; i++) { if (1.0 * b / (i - t) <= s[i].first) { ansb.push_back(s[i].second); break; } ansb.push_back(s[i].second); } if (i > n) return; if (f) swap(ansa, ansb); printf("Yes\n"); printf("%lld\n", (long long)ansa.size()); printf("%lld\n", (long long)ansb.size()); for (auto i : ansa) printf("%lld ", i); printf("\n"); for (auto i : ansb) printf("%lld ", i); printf("\n"); exit(0); } int main() { scanf("%lld %lld %lld", &n, &a, &b); for (i = 1; i <= n; i++) { scanf("%lld", &o); s[i] = make_pair(o, i); } sort(s + 1, s + 1 + n, greater<pair<long long, long long> >()); solve(0); swap(a, b); solve(1); printf("No\n"); }
10
CPP
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] n, m = list(map(int, input().split())) if n == 47: print("NO") else: if m == primes[primes.index(n)+1]: print("YES") else: print("NO")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const double PI = 3.1415926535897932384626433832795; const double EPS = 1e-9; const int MAXN = 1000 * 1000; bool isPrime[MAXN]; int primes[100 * 1000]; int amountPrimes; bool testPrime(int x) { for (int i = 2; i * i <= x; ++i) { if (x % i == 0) { return false; } } return x > 1; } void precalc() { amountPrimes = 0; memset(isPrime, true, sizeof(isPrime)); isPrime[0] = isPrime[1] = false; for (int i = 2; i < MAXN; ++i) { if (isPrime[i]) { primes[amountPrimes++] = i; for (int j = i + i; j < MAXN; j += i) { isPrime[j] = false; } } } cerr << amountPrimes << endl; } vector<int> collect(int remain, int border) { vector<int> res; for (int cur = border; cur > 0; --cur) { if (cur <= remain) { res.push_back(cur); remain -= cur; } } assert(remain == 0); return res; } vector<int> solve(int n) { int sumAll = n * (n + 1) / 2; int minAbleColor = 0; if (testPrime(sumAll)) { return vector<int>(n, minAbleColor); } int border = n; vector<int> res(n, -1); if (sumAll & 1) { if (testPrime(sumAll - 2)) { res.assign(n, 0); res[2 - 1] = 1; return res; } bool ok = false; for (int p = n; p > 1; --p) { if (testPrime(p)) { border = p - 1; sumAll -= p; res[p - 1] = minAbleColor++; ok = true; break; } } assert(ok); } bool ok = false; for (int primeId = 0; primeId < amountPrimes; ++primeId) { int p = primes[primeId]; if (2 * p <= sumAll && testPrime(sumAll - p)) { assert(border * (border + 1) / 2 >= p); vector<int> marked = collect(p, border); for (int i = 0; i < marked.size(); ++i) { int number = marked[i]; res[number - 1] = minAbleColor; } ++minAbleColor; ok = true; break; } } assert(ok); for (int i = 0; i < res.size(); ++i) { if (res[i] == -1) { res[i] = minAbleColor; } } return res; } int main() { precalc(); int n; while (cin >> n) { cerr << "n = " << n << endl; vector<int> res = solve(n); for (int i = 0; i < res.size(); ++i) { cout << res[i] + 1 << " "; } cout << endl; } return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; string s; cin >> n >> k; cin >> s; long long t = 0; for (long long i = 0, cur = 1; i < n; i++, cur++) { if (s[i] != 'N') cur = 0; t = max(t, cur); } if (t > k) { cout << "NO"; return 0; } s = "Y" + s; s = s + "Y"; for (int l = 1, r = l + k - 1; r <= n; l++, r++) { if (s[l - 1] != 'N' && s[r + 1] != 'N') { bool ans = 1; for (int i = l; i <= r; i++) ans = min(ans, !(s[i] == 'Y')); if (ans) { cout << "YES"; return 0; } } } cout << "NO"; return 0; }
16
CPP
from itertools import accumulate import math R = lambda: map(int, input().split()) n, m = R() a, b = list(accumulate([0] + list(R()))), list(R()) i = 0 for j in range(m): while i < n - 1 and a[i + 1] < b[j]: i += 1 print(i + 1, b[j] - a[i])
9
PYTHON3
t=int(input()) for i in range(t): s=list(input()) move=0 while True: indey=0 indfer=0 t=0 for k in range(len(s)-1): if s[k]!=s[k+1]: t=1 if (k==0 or k==len(s)-2): indfer=k elif s[k-1]==s[k+2]: indey=k elif s[k-1]!=s[k+2]: indfer=k if t==0: break elif indey!=0: del s[indey],s[indey] else: del s[indfer],s[indfer] move+=1 if move%2!=0: print("DA") else: print("NET")
8
PYTHON3
#include <bits/stdc++.h> using namespace std; struct node { int cnt; node* child[2]; node() { cnt = 0; for (int i = 0; i < 2; i++) { child[i] = nullptr; } } }; void addNum(int x, node* cur) { for (int i = 31; i >= 0; i--) { bool bit = x & (1 << i); if (cur->child[bit] == nullptr) { cur->child[bit] = new node(); } cur = cur->child[bit]; cur->cnt++; } } void delNum(int x, node* root, node* cur, int i) { if (i < 0) { return; } bool bit = x & (1 << i); delNum(x, root, cur->child[bit], i - 1); if (cur->child[bit]->cnt == 1) { delete cur->child[bit]; cur->child[bit] = nullptr; } else { cur->child[bit]->cnt--; } } int query(int x, node* cur) { int res = 0; for (int i = 31; i >= 0; i--) { bool bit = x & (1 << i); if (cur->child[!bit] != nullptr) { res |= (!bit) << i; cur = cur->child[!bit]; } else { res |= bit << i; cur = cur->child[bit]; } } return res; } void delTrie(node* cur) { for (int i = 0; i < 2; i++) { if (cur->child[i] != nullptr) { delTrie(cur->child[i]); } } delete cur; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int q; cin >> q; node* root = new node(); addNum(0, root); while (q--) { char c; cin >> c; int x; cin >> x; if (c == '+') { addNum(x, root); } else if (c == '-') { delNum(x, root, root, 31); } else { int res = x ^ query(x, root); cout << res << '\n'; } } delTrie(root); return 0; }
10
CPP
x = input() l = list(map(str,input().split())) f=0 for i in range(len(l)): if x[0]==l[i][0] or x[1]==l[i][1]: print("YES") f=1 break if f==0: print("NO")
7
PYTHON3
S,W=map(int,input().split()) if W>=S: print('unsafe') exit() print('safe')
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int par[150008], sz[150008]; int n; deque<int> dq[150008]; int find(int u) { return (par[u] == u) ? u : find(par[u]); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { sz[i] = 1; par[i] = i; dq[i].push_back(i); } for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; int x = find(u); int y = find(v); int mn = min(x, y); if (sz[y] > sz[x]) { sz[y] += sz[x]; par[x] = y; for (int i = 0; i < dq[x].size(); i++) { dq[y].push_back(dq[x][i]); par[dq[x][i]] = y; } } else { if (sz[x] == sz[y]) { if (x > y) { swap(x, y); } } sz[x] += sz[y]; par[y] = x; for (int i = 0; i < dq[y].size(); i++) { dq[x].push_back(dq[y][i]); par[dq[y][i]] = x; } } } int pos = -1; for (int i = 1; i <= n; i++) { if (dq[i].size() == n) { pos = i; break; } } for (int i = 0; i < dq[pos].size(); i++) { cout << dq[pos][i] << " "; } cout << endl; return 0; }
12
CPP
n = int(input()) h = list(map(int,input().split())) m = 0 c = 0 l = h[0] for x in h[1:]: c += x-l l = x m = max(m,c) print (h[0]+m)
8
PYTHON3
T = int(input()) for i in range(0,T): l = list(map(int,input().split())) A ,B = l[0],l[1] b_piece = list(str(B)) if(B==int("9"*len(b_piece))): result = len(b_piece) * A else: result = A * (len(b_piece)-1) print(result)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int t; t = 1; while (t--) { stack<int> st; int n, x, ans = 0; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &x); while (!st.empty()) { ans = max(ans, st.top() ^ x); if (st.top() > x) break; st.pop(); } st.push(x); } printf("%d\n", ans); } }
8
CPP
#include <iostream> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <set> #include <string> #include <iomanip> using namespace std; long long mod=1e9+7; long long c[8]={1,1,2,3,5,8,13,21}; int main(){ int h,w,k; cin>>h>>w>>k; long long dp[101][8]={}; dp[0][0]=1; for(int i=1;i<=h;i++){ for(int j=0;j<w;j++){ if(j==0){ dp[i][0]+=dp[i-1][0]*c[w-1]; if(w>=2){ dp[i][0]+=dp[i-1][1]*c[w-2]; } }else if(j==w-1){ dp[i][w-1]+=dp[i-1][w-1]*c[w-1]; if(w>=2){ dp[i][w-1]+=dp[i-1][w-2]*c[w-2]; } }else{ dp[i][j]+=dp[i-1][j]*c[j]*c[w-j-1]; dp[i][j]+=dp[i-1][j-1]*c[j-1]*c[w-j-1]; dp[i][j]+=dp[i-1][j+1]*c[j]*c[w-j-2]; } dp[i][j]%=mod; } } cout<<dp[h][k-1]<<endl; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n + 1]; for (int j = 1; j <= n; j++) cin >> a[j]; long long m = 0; for (int i = 1; i <= n - 1; i++) { int t = log2(n - i); m += a[i]; cout << m << endl; a[i + (int)pow(2, t)] += a[i]; } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; long long h, q, lev, l, r, ans; vector<pair<long long, long long> > vec, interv; vector<pair<long long, long long> >::iterator it; long long getl(long long num, long long level) { long long ret = num; for (int i = level + 1; i <= h; i++) ret = ret * 2; return ret; } long long getr(long long num, long long level) { long long ret = num; for (int i = level + 1; i <= h; i++) ret = ret * 2 + 1; return ret; } bool cmp(const pair<long long, long long> &a, const pair<long long, long long> &b) { if (a.first < b.first) return true; else if (a.first > b.first) return false; return a.second > b.second; } void merg(vector<pair<long long, long long> > v) { sort(v.begin(), v.end(), cmp); long long pre = 0, cnt = 0; for (it = v.begin(); it != v.end(); it++) { if (pre == 0) pre = it->first; cnt = cnt + it->second; if (cnt == 0) { long long ed = it->first; interv.push_back(make_pair(pre, ed)); pre = 0; } } } pair<long long, long long> intersection(vector<pair<long long, long long> > v) { long long ri = 1000000000, le = 0; ri = ri * ri; for (it = v.begin(); it != v.end(); it++) { if (it->first > le) le = it->first; if (it->second < ri) ri = it->second; } return make_pair(le, ri); } void getans(pair<long long, long long> inte, vector<pair<long long, long long> > v) { long long intecnt = 0, ans = 0; for (it = v.begin(); it != v.end(); it++) if (inte.first <= inte.second) { if (it->first <= inte.first && it->second >= inte.first) { inte.first = it->second + 1; continue; } if (it->first > inte.first) { intecnt += min(it->first - inte.first, inte.second - inte.first + 1); ans = inte.first; inte.first = it->second + 1; } } if (intecnt == 0) { if (inte.first == inte.second) cout << inte.first << endl; else if (inte.first < inte.second) cout << "Data not sufficient!" << endl; else cout << "Game cheated!" << endl; } else if (intecnt == 1) { cout << ans << endl; } else cout << "Data not sufficient!" << endl; } int main() { cin >> h >> q; for (int i = 1; i <= q; i++) { cin >> lev >> l >> r >> ans; if (ans == 1) interv.push_back(make_pair(getl(l, lev), getr(r, lev))); else { vec.push_back(make_pair(getl(l, lev), 1)); vec.push_back(make_pair(getr(r, lev), -1)); } } interv.push_back(make_pair(getl(1LL, 1LL), getr(1LL, 1LL))); pair<long long, long long> tmp = intersection(interv); interv.clear(); merg(vec); getans(tmp, interv); return 0; }
10
CPP
for _ in range(int(input())): p = ''.join(sorted(list(input()))) plen = len(p) h = input() hlen = len(h) hl = list(h) flag = 0 for i in range(hlen - plen + 1): if ''.join(sorted(hl[i : i + plen])) == p: print("YES") flag = 1 break if flag == 0: print("NO")
7
PYTHON3
str = input() s2 = "" c = 0 for i in str: if(i >= 'a' and i<= 'z'): s2 += i li1 = list(s2) li2 = [] for i in li1: if i not in li2: li2.append(i) print(len(li2))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int swap_count = 0; int n; cin >> n; int array1[n]; for (int i = 0; i < n; i++) { cin >> array1[i]; } int array2[n]; for (int i = 0; i < n; ++i) array2[i] = array1[i]; sort(array2, array2 + n); for (int i = 0; i < n; i++) { if (array1[i] != array2[i]) swap_count++; } if (swap_count <= 2) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long n, k; cin >> n >> k; long long a, i, j, d, c = 0; vector<long long> vec; cin >> a; vec.push_back(a); for (i = 2; i <= n; i++) { cin >> a; d = 0; for (j = 1; j <= vec.size(); j++) { d += (vec[j - 1] * (j - 1) - (n - (vec.size() + 1 + c)) * a); } if (d < k) { cout << i << endl; c++; } else { vec.push_back(a); } } return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int x[1013][5]; bool chk(int a, int b, int c) { int s = 0; for (int i = int(0), __border_righti = int(5); i < __border_righti; i++) { s += (x[b][i] - x[a][i]) * (x[c][i] - x[a][i]); } return s <= 0; } int main() { int N = 0; scanf("%d", &N); for (int i = int(0), __border_righti = int(N); i < __border_righti; i++) { for (int j = int(0), __border_rightj = int(5); j < __border_rightj; j++) { scanf("%d", &x[i][j]); } } if (N > 32) { printf("%d\n", 0); } else { int ans[40]; int n = 0; for (int i = int(0), __border_righti = int(N); i < __border_righti; i++) { bool flag = true; for (int j = int(0), __border_rightj = int(N); j < __border_rightj; j++) { if (j == i) { continue; } for (int k = int(0), __border_rightk = int(j); k < __border_rightk; k++) { if (k == i) { continue; } if (!chk(i, j, k)) { flag = false; } } } if (flag) { ans[n++] = i + 1; } } printf("%d\n", n); for (int i = int(0), __border_righti = int(n); i < __border_righti; i++) { printf("%d\n", ans[i]); } } return 0; }
7
CPP
n, k = map(int,input().split()) a = list(map(int,input().strip().split())) c = 0 for i in a: if(i>=a[k-1] and i>0): c+=1 print(c)
7
PYTHON3
t = int(input()) for _ in range(0,t): a,b,c,n = map(int,input().split()) ans = 'YES' aa,bb,cc = sorted([a,b,c]) n -= cc-aa if n < 0: ans = 'NO' n -= cc-bb if n < 0: ans = 'NO' if n%3 != 0: ans = 'NO' print(ans)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { char input[101]; scanf("%s", input); char temp[11]; map<string, int> mp; for (int i = 0; i < 10; ++i) { scanf("%s", temp); mp[temp] = i; } string s; for (int i = 0; input[i]; ++i) { s += input[i]; if (s.size() == 10) { printf("%c", mp[s] + '0'); s = ""; } } return 0; }
7
CPP
n = int(input()) A = [0]*5 B = [0]*5 for a in map(int, input().split()): A[a - 1] += 1 for a in map(int, input().split()): B[a - 1] += 1 if any([(x + y) % 2 != 0 for x, y in zip(A, B)]): print(-1) else: print(sum([abs(x - y) // 2 for x, y in zip(A, B)]) // 2)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; using cd = complex<double>; mt19937 rnd(chrono::system_clock::now().time_since_epoch().count()); long long n, m, k, mod, d[105][105][105], c[105][105]; int main() { cin >> n >> m >> k >> mod; for (long long i = 0; i <= 100; i++) { c[i][0] = c[i][i] = 1; for (long long j = 1; j < i; j++) { c[i][j] = (c[i - 1][j] + c[i - 1][j - 1]) % mod; } } for (long long i = 0; i <= m; i++) { d[0][i][0] = 1; } for (long long i = 1; i <= n; i++) { d[i][1][1] = (d[i - 1][1][1] * i) % mod; if (i == 1) d[i][1][1] = 1; } for (long long i = 1; i < m; i++) { for (long long l = 0; l < n; l++) { for (long long x = 0; x <= l; x++) { if (d[l][i][x]) { for (long long r = 0; l + r < n; r++) { for (long long y = 0; y <= r; y++) { if (d[r][i][y]) { d[l + r + 1][i + 1][x + y] += c[l + r][l] % mod * d[l][i][x] % mod * d[r][i][y] % mod; d[l + r + 1][i + 1][x + y] %= mod; } } } } } } } cout << d[n][m][k] << endl; }
8
CPP
n = int(input()) i = 1 result = 0 while i**2 <= n: result += 2*(n%i==0)-(i**2==n) i += 1 print(result-1)
7
PYTHON3
s = int(input()) k = -(-s//10**9) a = 10**9*k-s print(0,0,1,k,10**9,a)
0
PYTHON3
#include<bits/stdc++.h> using namespace std; #define inf 1e+9 int main(){ int n,m,a[50],b[50],ab[51][51],tmp[51][51]; for(int i=0;i<51;i++) for(int j=0;j<51;j++) ab[i][j]=inf; cin>>n>>m; for(int i=0;i<m;i++){ cin>>a[i]>>b[i]; ab[a[i]][b[i]]=1;ab[b[i]][a[i]]=1; } int ans=0; for(int l=0;l<m;l++){ for(int ti=0;ti<=n;ti++) for(int tj=0;tj<=n;tj++) tmp[ti][tj]=ab[ti][tj]; tmp[a[l]][b[l]]=inf;tmp[b[l]][a[l]]=inf; for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) tmp[i][j]=min(tmp[i][j],tmp[i][k]+tmp[k][j]); if(tmp[a[l]][b[l]]==inf||tmp[b[l]][a[l]]==inf)ans++; } cout<<ans<<endl; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; struct Node { int is, id, x; Node() {} Node(int is, int id, int x) : is(is), id(id), x(x) {} bool operator<(const Node &a) const { if (x != a.x) return x < a.x; else return is < a.is; } }; int a[maxn * 4], s[maxn], p[maxn], inc[maxn], b[maxn], treea[maxn * 4], treeb[maxn * 4], push_back[maxn]; int ans[maxn]; map<int, int> ma; int cnt = 0, tt = 0; Node que[maxn * 3]; int getid(int x) { return ma[x]; } void add1(int x, int y) { for (int i = x; i < 4 * maxn; i += (i & -i)) treea[i] += y; } void add2(int x, int y) { for (int i = x; i < 4 * maxn; i += (i & -i)) treeb[i] += y; } int quer1(int x) { int res = 0; for (int i = x; i >= 1; i -= (i & -i)) res += treea[i]; return res; } int quer2(int x) { int res = 0; for (int i = x; i >= 1; i -= (i & -i)) res += treeb[i]; return res; } vector<Node> ft; vector<int> st; map<int, int> idx; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &p[i]); for (int i = 1; i <= n; i++) scanf("%d", &s[i]); for (int i = 1; i <= n; i++) scanf("%d", &b[i]); for (int i = 1; i <= m; i++) scanf("%d", &inc[i]); for (int i = 1; i <= m; i++) scanf("%d", &push_back[i]); for (int i = 1; i <= n; i++) { ft.push_back(Node(0, i, p[i])); ft.push_back(Node(2, i, s[i])); st.push_back(-(b[i] + p[i] - 1)); st.push_back(-(b[i] - p[i])); } for (int i = 1; i <= m; i++) { ft.push_back(Node(1, i, inc[i])); st.push_back(-(push_back[i] - inc[i])); st.push_back(-(inc[i] + push_back[i])); } auto cmp = [&](const Node &a, const Node &b) { return a.x < b.x || (a.x == b.x && a.is < b.is); }; sort(ft.begin(), ft.end(), cmp); sort(st.begin(), st.end()); int pos = unique(st.begin(), st.end()) - st.begin(); for (int i = 0; i < pos; ++i) { ma[-st[i]] = i + 1; } for (int i = 0; i < ft.size(); i++) { Node now = ft[i]; if (now.is == 0) { add1(getid(b[now.id] - p[now.id]), 1); add2(getid(p[now.id] + b[now.id] - 1), -1); } else if (now.is == 1) { ans[now.id] = quer1(getid(push_back[now.id] - inc[now.id])) + quer2(getid(inc[now.id] + push_back[now.id])); } else if (now.is == 2) { add1(getid(b[now.id] - p[now.id]), -1); add2(getid(p[now.id] + b[now.id] - 1), 1); } } for (int i = 1; i <= m; i++) { printf("%d ", ans[i]); } return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; const int N = 500001; const int oo = 1e9; const int LOGN = 20; const int MOD = 1000000007; const double PI = acos(-1); int n, f[41]; long long dp[41][41][41][2]; string t; void fail() { int len = 0; for (int i = 1; i < t.size(); ++i) { while (len > 0 && t[i] != t[len]) len = f[len - 1]; if (t[i] == t[len]) len++; f[i] = len; } } int match(int cur, char nx) { if (cur == t.size()) cur = f[cur - 1]; while (cur > 0 && t[cur] != nx) cur = f[cur - 1]; if (t[cur] == nx) cur++; return cur; } long long calc(long long idx, long long pre, long long suf, bool done) { if (pre == t.size()) done = true; if (idx == n) return (pre == suf) && done; long long &ret = dp[idx][pre][suf][done]; if (ret != -1) return ret; ret = 0; for (int i = '0'; i <= '1'; ++i) ret = ret + calc(idx + 1, match(pre, i), suf, done); return ret; } int main() { memset(dp, -1, sizeof dp); cin >> n >> t; fail(); long long ans = 0; for (int i = 0; i <= t.size(); ++i) { ans += calc(0, i, i, 0); } cout << ans << endl; return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; int main() { char ab[200]; string s; cin.getline(ab, 150); s = ab; int h = s.length(), l = 0, b, pr = 0; for (int i = 0; i < h; ++i) { int a[10] = {}; l = s[i]; b = 0; while (l) { a[++a[0]] = l % 2; l /= 2; } for (int j = 1; j < 9; ++j) b = b * 2 + a[j]; cout << (pr - b + 512) % 256 << '\n'; pr = b; } }
7
CPP
ans=[] a,b=map(int,input().split()) c,d=map(int,input().split()) l=[b,b+a] ll=[d,d+c] for i in range(100): l+=[b+i*a] for i in range(100): ll+=[d+i*c] for i in l: if i in ll: ans+=[i] if len(ans)==0: print(-1) else: print(ans[0])
7
PYTHON3
import math s = input() count_a = s.count('a') count_b = s.count('b') count_c = s.count('c') flag = True if count_a == 0 or count_b == 0 or count_c == 0: print("NO") flag = False if flag: for i in range(count_a): if s[i] == 'b' or s[i] == 'c': print("NO") flag = False break if flag: for i in range(count_a,count_b + count_a): if s[i] == 'a' or s[i] == 'c': flag = False print("NO") break if flag: if count_a == count_c or count_b == count_c: print("YES") else: print("NO")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[310][310]; int n; int dp[620][310][310]; int solve() { dp[0][0][0] = a[0][0]; for (int k = 1; k <= 2 * n - 2; k++) { int st = max(0, k - n + 1); int en = min(n - 1, k); for (int i1 = st; i1 <= en; i1++) { for (int i2 = st; i2 <= en; i2++) { int j1 = k - i1, j2 = k - i2; dp[k][i1][i2] = a[i1][j1]; if (i1 != i2) dp[k][i1][i2] += a[i2][j2]; int add = -1e9; if (i1 != k && i2 != k) add = max(add, dp[k - 1][i1][i2]); if (i1 != k && i2 != 0) add = max(add, dp[k - 1][i1][i2 - 1]); if (i2 != k && i1 != 0) add = max(add, dp[k - 1][i1 - 1][i2]); if (i1 != 0 && i2 != 0) add = max(add, dp[k - 1][i1 - 1][i2 - 1]); dp[k][i1][i2] += add; } } } return dp[2 * n - 2][n - 1][n - 1]; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; } } int ans = solve(); cout << ans << endl; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; using vs = vector<string>; using vi = vector<int>; using vl = vector<ll>; using vb = vector<bool>; using pi = pair<int, int>; using pl = pair<ll, ll>; using vpi = vector<pair<int, int>>; using vpl = vector<pair<ll, ll>>; using ld = double; const ll MOD = 998244353; ll add(ll x, ll y) { x += y; if (x >= MOD) return x - MOD; return x; } ll sub(ll x, ll y) { x -= y; if (x < 0) return x + MOD; return x; } ll mult(ll x, ll y) { return (x * y) % MOD; } template <typename T> T signum(T a) { return (a > 0) - (a < 0); } template <typename T> T cmp(T a, T b) { return signum(a - b); } template <typename T> bool mins(T& lhs, T& rhs) { if (rhs < lhs) { lhs = rhs; return true; } return false; } template <typename T> bool maxs(T& lhs, T& rhs) { if (rhs > lhs) { lhs = rhs; return true; } return false; } const int MAX = INT_MAX; void gks(int tc) { printf("Case #%d: ", tc); }; int main() { int t; cin >> t; while (t--) { vector<pair<int, int>> like; vector<pair<int, int>> dislike; int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } string s; cin >> s; for (int i = 0; i < n; ++i) { if (s[i] == '0') { dislike.push_back({a[i], i}); } else { like.push_back({a[i], i}); } } sort(like.begin(), like.end()); sort(dislike.begin(), dislike.end()); vector<int> ans(n); vector<int> b(dislike.size()); vector<int> c(like.size()); iota(b.begin(), b.end(), 1); iota(c.begin(), c.end(), dislike.size() + 1); for (int i = 0; i < (int)dislike.size(); ++i) { ans[dislike[i].second] = b[i]; } for (int i = 0; i < (int)like.size(); ++i) { ans[like[i].second] = c[i]; } for (auto x : ans) { cout << x << " "; } cout << "\n"; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; string s; vector<char> v; int main() { cin >> s; for (int i = 0; i < s.size(); i++) { if (s[i] == 'h' && v.size() == 0) { v.push_back('h'); } if (s[i] == 'e' && v.size() == 1) { v.push_back('e'); } if (s[i] == 'i' && (v.size() == 2 || v.size() == 4)) { v.push_back('i'); } if (s[i] == 'd' && v.size() == 3) { v.push_back('d'); } } if (v.size() >= 5) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; }
13
CPP
n=int(input()) ar=list(map(int, input().split())) ar.sort() s=sum(ar)//2 c=1 t=ar[n-1] while t<=s: c+=1 t+=ar[n-c] print(c)
7
PYTHON3
#include <bits/stdc++.h> const int maxn = 1000000 + 50; const int maxm = 4e5 + 10; const int inf_max = 0x3f3f3f; using namespace std; long long n, k; const long long MAX = 2000000000; long long check(long long cur) { long long group = cur / n; cur -= group; return cur; } int main() { int t; cin >> t; while (t--) { cin >> n >> k; long long l = 1, r = 3000000000; while (l <= r) { long long mid = (l + r) >> 1; if (mid % n == 0) { long long tt = mid / n, tmp = mid; tmp -= tt; if (tmp == k) { printf("%lld\n", mid - 1); break; } else if (tmp > k) r = mid - 1; else l = mid + 1; } else { long long cur = check(mid); if (cur == k) { printf("%lld\n", mid); break; } else if (cur > k) r = mid - 1; else l = mid + 1; } } } return 0; }
9
CPP
#include <bits/stdc++.h> int N; int H[5000 + 7]; int F[5000 + 7][5000 + 7]; int S[5000 + 7]; int Calc(int X, int Y) { return S[Y] - S[X - 1]; } int main() { scanf("%d", &N); for (int i = 1; i <= N; i++) { scanf("%d", H + i); S[i] = S[i - 1] + H[i]; } for (int i = 1; i <= N; i++) F[1][i] = 1; for (int i = 2; i <= N; i++) for (int j = i, k = i, temp = 0, record = -1; j <= N; j++) { for (; k > 1 && Calc(i, j) >= Calc(k - 1, i - 1); k--) { if (F[k - 1][i - 1] > temp) record = k - 1, temp = F[k - 1][i - 1]; } if (record == -1) temp = -9999; F[i][j] = temp + 1; } int Ans = 0; for (int k = 1; k <= N; k++) if (Ans < F[k][N]) Ans = F[k][N]; printf("%d\n", N - Ans); return 0; }
10
CPP
#[n participants, k-th place finishers score] n, k = [int(s) for s in input().split()] scores = [int(s) for s in input().split()] count = 0 for index in scores: if index >= scores[k-1] and index > 0: count+=1 print(count)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; long long mod = (998244353LL); inline long long poww(long long a, long long b) { long long res = 1; while (b > 0) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; } inline pair<long long, long long> operator+(pair<long long, long long> A, pair<long long, long long> B) { return pair<long long, long long>(A.first + B.first, A.second + B.second); } inline pair<long long, long long> operator-(pair<long long, long long> A, pair<long long, long long> B) { return pair<long long, long long>(A.first - B.first, A.second - B.second); } inline long long mult(long long a, long long b) { return (a * b) % mod; } long long gcd(long long a, long long b) { while (b) { a %= b; swap(a, b); } return a; } long long n, m, q, k, v[500050], ans, dp[500050], opt[500050]; long long sum[500050]; long long l[500050], r[500050], x[500050], pref[500050]; pair<long long, long long> TT, w[500050]; string second; int32_t main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> k >> q; for (long long i = 1; i <= q; i++) cin >> l[i] >> r[i] >> x[i]; long long ans = 1LL; for (long long j = 0; j < k; j++) { vector<long long> en(n + 2); for (long long i = 0; i <= n + 1; i++) { pref[i] = dp[i] = sum[i] = 0; opt[i] = v[i] = 0; } for (long long i = 1; i <= q; i++) { if (x[i] & (1LL << j)) { pref[l[i]]++; pref[r[i] + 1]--; continue; } opt[r[i]] = max(opt[r[i]], l[i]); } long long cara = 0; for (long long i = 1; i <= n + 1; i++) { pref[i] += pref[i - 1]; en[i] = cara; if (opt[i] > 0) cara = max(opt[i], cara); } dp[0] = 1; sum[0] = 1; for (long long i = 1; i <= n + 1; i++) { if (!pref[i]) { long long t = en[i]; if (t > 0) dp[i] = sum[i - 1] - sum[t - 1]; else dp[i] = sum[i - 1]; } dp[i] %= mod; if (dp[i] < 0) dp[i] += mod; sum[i] = sum[i - 1] + dp[i]; sum[i] %= mod; } ans *= dp[n + 1]; ans %= mod; en.clear(); } cout << ans << "\n"; }
12
CPP
#include <bits/stdc++.h> long gcd(long a, long b) { long c = a % b; for (; c;) { a = b; b = c; c = a % b; } return b; } char name[2][15] = {"Dasha", "Masha"}; char opt[15] = {0}; int main() { long a, b; long c; long long d; scanf("%ld%ld", &a, &b); if (a > b) { a ^= b; b ^= a; a ^= b; strcpy(opt, name[0]); strcpy(name[0], name[1]); strcpy(name[1], opt); } c = gcd(a, b); a /= c; b /= c; d = (long long)a * (b - a) + (long long)a * (a - 1) / 2; if ((long long)a * b - d == d) printf("Equal\n"); else if (d > (long long)a * b - d) printf("%s\n", name[0]); else printf("%s\n", name[1]); return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; const long long logN = 20; const long long N = 100005; const long long M = 1000000007; const long long INF = 1e12; const long long dx[4] = {1, 0, -1, 0}; const long long dy[4] = {0, 1, 0, -1}; long long A[2005]; long long dp[2005][2005]; long long beg[2005], la[2005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; for (long long i = 1; i < n + 1; i++) cin >> A[i]; for (long long i = 1; i < n + 1; i++) { long long l1 = 0, l2 = 0; for (long long j = i; j < n + 1; j++) { if (A[j] == 1) { l1 = max(l1, l2) + 1; } else { l2++; } dp[i][j] = max(l1, l2); } } long long cnt = 0; for (long long i = 1; i < n + 1; i++) { if (A[i] == 1) cnt++; beg[i] = cnt; } cnt = 0; for (long long i = n; i >= 1; i--) { if (A[i] == 2) cnt++; la[i] = cnt; } long long ans = 0; for (long long i = 1; i < n + 1; i++) for (long long j = i; j < n + 1; j++) { ans = max(ans, beg[i - 1] + dp[i][j] + la[j + 1]); } cout << ans << "\n"; return 0; }
9
CPP
N, K = map(int, input().split()) S = input() print(N - 1 - max(0, len([0 for i in range(1, N) if S[i] != S[i-1]]) - 2 * K))
0
PYTHON3
n,d=list(map(int,input().split())) if n%2==1 and d==((n//2)+1): print(n) elif n==d: if n==10**12: print(n) else: print(n-1) elif d<=(n//2): print((d*2)-1) else: if n%2==1: d=d-1 f=n//2 d=d-f print((d*2))
7
PYTHON3
N = int(input()) plus = [] minus = [] for _ in range(N): x, y = map(int, input().split()) plus.append(x + y) minus.append(x - y) print(max(max(plus) - min(plus), max(minus) - min(minus)))
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[20][20]; vector<long long> dp[20010][15]; vector<int> e[15]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { char c; cin >> c; a[i][j] = c - '0'; } for (int i = 1; i < (1 << n); i++) { int x = i, cnt = 0; while (x) { cnt++; x -= (x & (-x)); } e[cnt].push_back(i); for (int k = 0; k < n; k++) dp[i][k].resize(1 << (cnt - 1)); } for (int i = 0; i < n; i++) dp[(1 << i)][i][0] = 1; for (int j = 1; j < n; j++) { for (auto i : e[j]) { for (int k = 0; k < n; k++) { if ((i & (1 << k)) == 0) continue; for (int l = 0; l < n; l++) { if (i & (1 << l)) continue; for (int p = 0; p < (1 << (j - 1)); p++) { dp[i | (1 << l)][l][(p << 1) + (a[l][k])] += dp[i][k][p]; } } } } } for (int p = 0; p < (1 << (n - 1)); p++) { long long ans = 0; for (int k = 0; k < n; k++) ans += dp[(1 << n) - 1][k][p]; cout << ans << " "; } return 0; }
12
CPP
n=int(input()) o=list(map(int,input().split())) a=sum(o)/n dif=1000000000 ans=-1 for i,k in enumerate(o): if abs(a-k)<dif: dif=abs(a-k) ans=i print(ans)
0
PYTHON3