solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; int main() { vector<int> g1, g2; int n, i, sum_A = 0, sum_B = 0, count1_A, count1_B, count2_A, count2_B, count3_A, count3_B, count4_A, count4_B, count5_A, count5_B, input1; cin >> n; for (i = 0; i < n; i++) { cin >> input1; g1.push_back(input1); } for (i = 0; i < n; i++) { cin >> input1; g2.push_back(input1); } count1_A = count(g1.begin(), g1.end(), 1); count1_B = count(g2.begin(), g2.end(), 1); count2_A = count(g1.begin(), g1.end(), 2); count2_B = count(g2.begin(), g2.end(), 2); count3_A = count(g1.begin(), g1.end(), 3); count3_B = count(g2.begin(), g2.end(), 3); count4_A = count(g1.begin(), g1.end(), 4); count4_B = count(g2.begin(), g2.end(), 4); count5_A = count(g1.begin(), g1.end(), 5); count5_B = count(g2.begin(), g2.end(), 5); if (count1_A > count1_B) sum_A = sum_A + (count1_A - count1_B); else sum_B = sum_B + (count1_B - count1_A); if (count2_A > count2_B) sum_A = sum_A + (count2_A - count2_B); else sum_B = sum_B + (count2_B - count2_A); if (count3_A > count3_B) sum_A = sum_A + (count3_A - count3_B); else sum_B = sum_B + (count3_B - count3_A); if (count4_A > count4_B) sum_A = sum_A + (count4_A - count4_B); else sum_B = sum_B + (count4_B - count4_A); if (count5_A > count5_B) sum_A = sum_A + (count5_A - count5_B); else sum_B = sum_B + (count5_B - count5_A); if (abs(count1_A - count1_B) % 2 != 0 || abs(count2_A - count2_B) % 2 != 0 || abs(count3_A - count3_B) % 2 != 0 || abs(count4_A - count4_B) % 2 != 0 || abs(count5_A - count5_B) % 2 != 0) { cout << -1 << endl; } else if (sum_A == sum_B && sum_A % 2 == 0 && sum_B % 2 == 0) cout << sum_B / 2 << endl; }
7
CPP
#include <bits/stdc++.h> using namespace std; vector<int> v[2]; long long mind[2], minc[2]; int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; v[x].push_back(y); } for (int i = 0; i < 2; i++) sort(v[i].begin(), v[i].end()); long long cnt = 0; for (int i = 0; i < 2; i++) { for (auto k : v[i]) { long long pl = lower_bound(v[i ^ 1].begin(), v[i ^ 1].end(), k) - v[i ^ 1].begin(); long long pr = upper_bound(v[i ^ 1].begin(), v[i ^ 1].end(), k) - v[i ^ 1].begin(); cnt += pl * ((long long)v[i ^ 1].size() - pr); cnt += (pr - pl) * ((long long)v[i ^ 1].size() - pr + pl) + (pr - pl) * (pr - pl - 1) / 2; } } if (cnt) { cout << cnt << '\n'; return 0; } for (int i = 0; i < 2; i++) { if ((int)v[i].size() == 0) continue; long long c1 = 0, c2 = 0; for (auto k : v[i]) { if (k == v[i][0]) c1++; if (k == v[i][(int)v[i].size() - 1]) c2++; } mind[i] = v[i][(int)v[i].size() - 1] - v[i][0]; if (mind[i] == 0) minc[i] = c1 * (c1 - 1) * (c1 - 2) / 6; else minc[i] = c1 * c2 * (m - c1 - c2) + c1 * c2 * (c1 + c2 - 2) / 2; } if (!v[0].size() || !v[1].size()) { cout << minc[0] + minc[1] << endl; return 0; } int i = 0; if (v[0][0] < v[1][0]) i = 1; long long mn = v[i][0], mnc = 0, mx = v[1 - i][(int)v[1 - i].size() - 1], mxc = 0; for (auto k : v[i]) if (k == mn) mnc++; for (auto k : v[1 - i]) if (k == mx) mxc++; cout << mnc * mxc * (m - mnc - mxc) + mnc * mxc * (mnc + mxc - 2) / 2 << endl; }
11
CPP
#include <iostream> using namespace std; int main(){ double v; while(cin>>v){ double y,t; t = v/9.8; y = 4.9*t*t; cout<<(int)(y/5+2)<<endl; } }
0
CPP
row=0 col=0 for i in range(5): s = input() if s!='0 0 0 0 0': row=i+1 for x in range(0,9,2): if s[x]=='1': col=x/2+1 break print(int(abs(row-3)+abs(col-3)))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-12; int main() { double x0, y0, v, t; scanf("%lf %lf %lf %lf", &x0, &y0, &v, &t); double r0 = v * t; int n; scanf("%d", &n); vector<pair<double, double> > list; while (n--) { double x, y, r; scanf("%lf %lf %lf", &x, &y, &r); double d = sqrt((x - x0) * (x - x0) + (y - y0) * (y - y0)); if (d < r + eps) { printf("%lf\n", 1.0); return 0; } if (r + r0 < d) continue; double mid = atan2(y - y0, x - x0); if (mid < 0) mid += 2 * pi; double range; double tan = sqrt(d * d - r * r); if (tan < r0) range = asin(r / d); else range = acos((r0 * r0 - r * r + d * d) / (2 * d * r0)); double begin = mid - range; double end = mid + range; if (begin < 0) list.push_back(pair<double, double>(begin + 2 * pi, 2 * pi)), begin = 0; if (end > 2 * pi) list.push_back(pair<double, double>(0, end - 2 * pi)), end = 2 * pi; list.push_back(pair<double, double>(begin, end)); } double sum = 0; double begin = -1; double end = -1; sort(list.begin(), list.end()); for (auto p : list) { if (p.first < end) end = max(end, p.second); else { sum += (end - begin); begin = p.first; end = p.second; } } sum += (end - begin); printf("%lf\n", sum / (2 * pi)); }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e7 + 5; const int M = 1e9 + 7; inline int add(int a, int b) { return (a + b < M) ? (a + b) : (a + b - M); } inline int sub(int a, int b) { return (a >= b) ? (a - b) : (a - b + M); } inline int mul(int a, int b) { return (int((a * 1LL * b) % M) + M) % M; } inline int expo(int a, int b) { int ret = 1; b = (b == -1) ? (M - 2) : b; while (b != 0) { if (b & 1) { ret = mul(ret, a); } a = mul(a, a); b >>= 1; } return ret; } vector<int> prime; bitset<N> vis; int mu[N], spf[N]; void sieve() { mu[1] = 1; spf[1] = 1; for (int i = 2; i < N; ++i) { if (!vis[i]) { prime.push_back(i); mu[i] = -1; spf[i] = i; } for (int j = 0; j < (int)prime.size() && i * prime[j] < N; ++j) { vis[i * prime[j]] = true; spf[i * prime[j]] = prime[j]; if (i % prime[j] == 0) { mu[i * prime[j]] = 0; break; } mu[i * prime[j]] = mu[i] * mu[prime[j]]; } } } vector<int> divisors(int n) { vector<int> pdivs; while (n > 1) { pdivs.push_back(spf[n]); while (pdivs.back() == spf[n]) { n /= spf[n]; } } int l = (int)pdivs.size(); vector<int> ret; for (int mask = 1; mask < (1 << l); mask++) { int num = 1; for (int i = 0; i < l; i++) { if ((mask >> i) & 1) { num *= pdivs[i]; } } ret.push_back(num); } return ret; } int main() { sieve(); int n; scanf("%d", &n); vector<int> a(n), cnt(N); for (int &v : a) { scanf("%d", &v); for (int x : divisors(v)) { ++cnt[x]; } } vector<int> pow2(n + 1, 1); for (int i = 1; i <= n; i++) { pow2[i] = (pow2[i - 1] << 1); if (pow2[i] >= M) { pow2[i] -= M; } } for (int &v : pow2) { v = sub(v, 1); } vector<int> dp(N); for (int i = 2; i < N; i++) { dp[i] = mu[i] == 1 ? pow2[cnt[i]] : (M - pow2[cnt[i]]); } int tot = 0; for (int i = 2; i < N; i++) { tot = sub(tot, dp[i]); } int ans = 0; for (int x : a) { ans = add(ans, tot); for (int v : divisors(x)) { ans = add(ans, dp[v]); } } printf("%d\n", ans); return 0; }
11
CPP
#include <bits/stdc++.h> int x[100010]; int y[100010]; int main() { int i; int n; long long Sqsum = 0, sumX = 0, sumY = 0; scanf("%d", &n); if (n == 1) { printf("0\n"); return 0; } for (i = 0; i < n; i++) { scanf("%d%d", &x[i], &y[i]); Sqsum += x[i] * x[i] + y[i] * y[i]; sumX += x[i]; sumY += y[i]; } long long ans = n * Sqsum - sumX * sumX - sumY * sumY; printf("%I64d\n", ans); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool cmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template <typename T> inline bool cmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <typename L, typename R> ostream &operator<<(ostream &out, pair<L, R> t) { return out << "(" << t.first << "," << t.second << ")"; } template <typename T> ostream &operator<<(ostream &out, vector<T> t) { for (unsigned i = 0; i < t.size(); i++) out << (" ("[!i]) << t[i] << (",)"[i == t.size() - 1]); return out; } const int NX = 1001234 << 1, KX = 26 + 1; const int mod = 1e9 + 7; int n, m, k, last[KX], f[KX], g[KX]; char s[NX]; int main() { scanf("%d%d%s", &n, &k, s + 1); m = strlen(s + 1); for (int i = 1; i <= m; i++) { int sum = 1; for (int j = 0; j < k; j++) sum = (sum + f[j]) % mod; f[s[i] - 'a'] = sum; } for (int i = 1; i <= m; i++) g[s[i] - 'a'] = i; for (int i = 1; i <= n; i++) { int t = min_element(g, g + k) - g, u = 1; for (int j = 0; j < k; j++) (u += f[j]) %= mod; f[t] = u; g[t] = i + m; } int ans = 1; for (int i = 0; i < k; i++) (ans += f[i]) %= mod; cout << ans << "\n"; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 1010; struct Edge { int s, e, nx; long long c; } E[1000000]; int head[MAXN], cnt; void add(int s, int e, long long c) { E[cnt].s = s; E[cnt].e = e; E[cnt].c = c; E[cnt].nx = head[s]; head[s] = cnt++; E[cnt].s = e; E[cnt].e = s; E[cnt].c = 0; E[cnt].nx = head[e]; head[e] = cnt++; } int flow_d[MAXN], flow_num[MAXN]; int flow_Stack[MAXN], flow_Top; int flow_H[MAXN]; int flow_Q[MAXN]; long long isap(int N, int s, int t) { for (int i = 0; i <= N + 1; i++) flow_H[i] = head[i]; memset(flow_d, -1, sizeof(flow_d)); memset(flow_num, 0, sizeof(flow_num)); flow_d[t] = 0; flow_num[0] = 1; int l = 0, r = 0; flow_Q[r++] = t; int x; while (l < r) { x = flow_Q[l++]; for (int i = head[x]; ~i; i = E[i].nx) if (E[i ^ 1].c && flow_d[E[i].e] == -1) { flow_d[E[i].e] = flow_d[x] + 1; ++flow_num[flow_d[x] + 1]; flow_Q[r++] = E[i].e; } } long long ans = 0; flow_Top = 0; while (flow_d[s] < N) { if (!flow_Top) x = s; else x = E[flow_Stack[flow_Top - 1]].e; if (x == t) { long long p = E[flow_Stack[0]].c; for (int i = 1; i < flow_Top; i++) p = min(p, E[flow_Stack[i]].c); ans += p; for (int i = flow_Top - 1; i >= 0; i--) { E[flow_Stack[i]].c -= p; if (E[flow_Stack[i]].c == 0) flow_Top = i; E[flow_Stack[i] ^ 1].c += p; } } else { bool ok = 0; for (int i = flow_H[x]; ~i; i = E[i].nx) if (E[i].c > 0 && flow_d[E[i].e] == flow_d[x] - 1) { flow_Stack[flow_Top++] = i; flow_H[x] = i; ok = 1; break; } if (ok) continue; if (--flow_num[flow_d[x]] == 0) return ans; int mi = N; for (int i = head[x]; i != -1; i = E[i].nx) if (E[i].c > 0 && mi > flow_d[E[i].e]) { mi = flow_d[E[i].e]; flow_H[x] = i; } flow_d[x] = mi + 1; ++flow_num[flow_d[x]]; if (x != s) --flow_Top; } } return ans; } const long long M = 1LL << 50; int n; bool can[MAXN][MAXN]; int main() { while (~scanf("%d", &n)) { cnt = 0; memset(head, -1, sizeof(head)); memset(can, 0, sizeof(can)); int s = 0, t = 2 * n + 1; for (int i = 1; i <= n; i++) { int m; scanf("%d", &m); for (int j = 1; j <= m; j++) { int x; scanf("%d", &x); can[i][x] = 1; } } long long sum = 0; for (int i = 1; i <= n; i++) { int p; scanf("%d", &p); add(s, i, -p + M); sum -= p; } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (can[i][j]) { add(i, j + n, M * 100000); } for (int i = 1; i <= n; i++) add(i + n, t, M); long long ans = isap(t + 1, s, t); printf("%I64d\n", ans - sum - n * M); } }
11
CPP
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; int solve(const string& A, const string& B){ const int N = A.size(); int s = B.find('1'); if(s == string::npos) return (A.find('1') == string::npos ? 0 : -1); vector<int> nextL(N), nextR(N); int p = 0; for(int i=0;i<N;i++){ if(B[(s+i)%N] == '1') p = (s+i)%N; nextL[(s+i)%N] = p; } for(int i=0;i<N;i++){ if(B[(s+N-i)%N] == '1') p = (s+N-i)%N; nextR[(s+N-i)%N] = p; } int res = 10 * N; for(int shift=0;shift<N;shift++){ int cur = 0; vector<pair<int, int>> vp; for(int i=0;i<N;i++){ if(A[i] == B[(i+shift)%N]) continue; ++cur; if(A[i] == '0') continue; if(i+shift < N && i <= nextR[i] && nextR[i] <= i+shift) continue; if(i+shift >= N && (nextR[i] <= (i+shift)%N || i <= nextR[i])) continue; int left = nextL[i] - i; if(left > 0) left -= N; int right = nextR[(i+shift)%N] - (i+shift)%N; if(right < 0) right += N; vp.emplace_back(left, right); } sort(vp.begin(), vp.end()); int addMove = 3*N; int maxR = 0; for(auto& v : vp){ addMove = min(addMove, -v.first + maxR); maxR = max(maxR, v.second); } addMove = min(addMove, maxR); res = min(res, cur + shift + 2 * addMove); } return res; } int main(){ string A, B; while(cin >> A >> B){ int a = solve(A, B); reverse(A.begin(), A.end()); reverse(B.begin(), B.end()); int b = solve(A, B); cout << min(a, b) << endl; } }
0
CPP
#include <iostream> #include <cstdio> #include <map> #define REP(i,n) for(int i=0; i<(int)(n); i++) #define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++) using namespace std; char d[20][30]; int dx[8] = {-1, 0, 1,-1, 1,-1, 0, 1}; int dy[8] = {-1,-1,-1, 0, 0, 1, 1, 1}; int main(){ int h, w; while(scanf("%d%d", &h, &w), h + w){ map<string, int> m; REP(i,h) scanf("%s", d[i]); REP(i,h) REP(j,w){ REP(k,8){ int x = j; int y = i; string tmp = ""; do{ tmp += d[y][x]; x = (x + w + dx[k]) % w; y = (y + h + dy[k]) % h; m[tmp]++; }while(i != y || j != x); } } string ans = ""; FOR(it, m) if(it->second > 1){ const string &s = it->first; if(s.size() > ans.size()) ans = s; else if(s.size() == ans.size() && s < ans) ans = s; } if(ans.size() > 1) puts(ans.c_str()); else puts("0"); } return 0; }
0
CPP
n = int(input()) l = list(map(int, input().split())) l.sort() if n % 2 == 0: print(l[n // 2 - 1]) else: print(l[n // 2])
7
PYTHON3
n = int(input()) nameLst = [] dic = {} for i in range(n): name = input() nameLst.append(name) for i in range(len(nameLst)): if nameLst[i] in dic: dic[nameLst[i]] += 1 print(nameLst[i] + str(dic[nameLst[i]]-1)) else: dic[nameLst[i]] = 1 print("OK")
9
PYTHON3
a = list(input().split()) b = str (a[0][0]) d = b.upper() c=[] c.append(d) for i in range(1,len(a[0])): c.append(a[0][i]) for i in range(len(c)): print(c[i],end='')
7
PYTHON3
r = 0; S = [] for s in range(int(input())): t = [int(y) for y in input().split()] r += t[-1] - t[0]; S.append(r) print(max(S))
7
PYTHON3
t = int(input()) while t: t-=1 n = int(input()) odd = 2*(n/2)-1 if (odd + int(n/2)+2)%2 ==0 : print("NO") else: print("YES") ans = [i for i in range(2, n+1, 2)] ans.extend([i for i in range(1, n-1, 2)]) ans.append(int(n/2+ans[-1]+2)) for a in ans: print(a, end = ' ') print()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int INF = 10000000; const int MAX_V = 51; int d[MAX_V][MAX_V]; int V; void warshall_floyd() { for ( int k = 0; k < V; k++ ) { for (int i = 0; i < V; i++ ) { for ( int j = 0; j < V; j++ ) { d[i][j] = min(d[i][j], d[i][k] + d[k][j] ); } } } } int main() { cin >> V; for ( int i = 0; i < 51; i++ ) { for ( int j = 0; j < 51; j++ ) { if ( i == j ) d[i][j] = 0; else d[i][j] = INF; } } int m; cin >> m; int a,b,c,dd; char ch1,ch2,ch3; for ( int i = 0; i < m; i++ ) { cin >> a >> ch1 >> b >> ch2 >> c >> ch3 >> dd; a--; b--; d[a][b] = c; d[b][a] = dd; } int x1,x2,y1,y2; cin >> x1 >> ch1 >> x2 >> ch2 >> y1 >> ch3 >> y2; x1--; x2--; warshall_floyd(); int ans = y1 - d[x1][x2] - d[x2][x1] - y2; cout << ans << endl; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int n, s, k; int a[10010]; bool ima(int t) { for (int i = 0; i < k; i++) if (a[i] == t) return false; return true; } int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { cin >> n >> s >> k; for (int i = 0; i < k; i++) cin >> a[i]; int sol = 1e9; for (int i = s; i <= n; i++) if (ima(i)) { sol = i - s; break; } for (int i = s; i > 0; i--) if (ima(i)) { sol = min(sol, s - i); break; } cout << sol << endl; } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int n, m, A, B, S, sl, fh, a[200010], w[200010], inv[3010 << 1], f[3010][3010], g[3010][3010]; int rd() { sl = 0; fh = 1; char ch = getchar(); while (ch < '0' || '9' < ch) { if (ch == '-') fh = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') sl = sl * 10 + ch - '0', ch = getchar(); return sl * fh; } int _pow(int k, int i) { int t = 1; while (i) { if (i & 1) t = 1ll * t * k % 998244353; k = 1ll * k * k % 998244353; i >>= 1; } return t; } int main() { n = rd(); m = rd(); for (int i = 1; i <= n; ++i) a[i] = rd(); for (int i = 1; i <= n; ++i) { w[i] = rd(); S += w[i]; if (a[i]) A += w[i]; else B += w[i]; } for (int i = 0; i <= m * 2; ++i) inv[i] = _pow(S + i - m, 998244353 - 2); for (int i = m; ~i; --i) { f[i][m - i] = g[i][m - i] = 1; for (int j = m - i - 1; ~j; --j) f[i][j] = (1ll * (A + i + 1) * f[i + 1][j] + 1ll * (B - j) * f[i][j + 1]) % 998244353 * inv[i - j + m] % 998244353, g[i][j] = (1ll * (B - j - 1) * g[i][j + 1] + 1ll * (A + i) * g[i + 1][j]) % 998244353 * inv[i - j + m] % 998244353; } A = f[0][0]; B = g[0][0]; for (int i = 1; i <= n; ++i) if (a[i]) printf("%lld\n", 1ll * A * w[i] % 998244353); else printf("%lld\n", 1ll * B * w[i] % 998244353); return 0; }
11
CPP
T=int(input()) for t in range(T): n,k1,k2=map(int,input().split()) a=list(map(int,input().split())) b=list(map(int,input().split())) x=max(a) y=max(b) if(x>y): print("YES") else: print("NO")
7
PYTHON3
#include "bits/stdc++.h" #include<unordered_map> #include<unordered_set> #pragma warning(disable:4996) using namespace std; using ld = long double; const ld eps = 1e-9; //// < "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\a.txt" > "d:\d_download\visual studio 2015\projects\programing_contest_c++\debug\b.txt" int main() { while (true) { int N;cin>>N; if(!N)break; string st;cin>>st; vector<int>v; for (auto c : st) { v.push_back(c-'a'); } vector<pair<int,int>>ps; for (int i = 0; i < N; ++i) { int a,b;cin>>a>>b; a--;b--; ps.push_back(make_pair(a,b)); } for (int i = N - 1; i >= 0; --i) { int a=ps[i].first; int b=ps[i].second; int plus=abs(a-b); v[a]+=plus; v[a]%=26; v[b]+=plus; v[b]%=26; swap(v[a],v[b]); } for (int i = 0; i < st.size(); ++i) { cout<<char(v[i]+'a'); } cout<<endl; } return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; inline long long Readll() { long long ret = 0; int f = 1; char ch; do { ch = getchar(); if (ch == '-') f = -1; } while (ch >= 0 && (ch < '0' || ch > '9')); while (ch >= '0' && ch <= '9') { ret = ret * 10 + ch - '0'; ch = getchar(); } return ret * f; } void open() { freopen("E.in", "r", stdin); freopen("E.out", "w", stdout); } void close() { fclose(stdin); fclose(stdout); } const int MAXN = 333; const int MAXM = 101010; const int inf = 1e9; int match[MAXN]; int ans; int n; struct bi_graph { int tot; int to[MAXM]; int nxt[MAXM]; int hd[MAXN]; bool vis[MAXN]; inline void add(const int &x, const int &y) { tot++; to[tot] = y; nxt[tot] = hd[x]; hd[x] = tot; } bool find(int p) { for (int i = hd[p]; i; i = nxt[i]) if (vis[to[i]] == false) { vis[to[i]] = true; if (match[to[i]] == 0 || find(match[to[i]])) { match[to[i]] = p; return true; } } return false; } void work() { for (int i = (1), _END_ = (n); i <= _END_; i++) { memset(vis, 0, sizeof(vis)); find(i); } } } B; struct flow_graph { int src, snk; int tot; int to[MAXM]; int nxt[MAXM]; int fl[MAXM]; int hd[MAXN]; int que[MAXN + 1]; bool in[MAXN]; int dis[MAXN]; inline void clear() { tot = -1; memset(hd, -1, sizeof(hd)); } inline void add(const int &x, const int &y, const int &FL) { tot++; to[tot] = y; fl[tot] = FL; nxt[tot] = hd[x]; hd[x] = tot; tot++; to[tot] = x; fl[tot] = 0; nxt[tot] = hd[y]; hd[y] = tot; } inline int Nxt(int &T) { int ret = T; if (T == MAXN) T = 0; else T++; return ret; } bool SPFA() { int L = 0, R = 0; for (int i = (src), _END_ = (snk); i <= _END_; i++) dis[i] = inf; que[Nxt(R)] = src; dis[src] = 0; int now; while (L != R) { now = que[Nxt(L)]; in[now] = false; for (int i = hd[now]; i != -1; i = nxt[i]) if (fl[i]) { if (dis[to[i]] > dis[now] + 1) { dis[to[i]] = dis[now] + 1; if (!in[to[i]]) { in[to[i]] = true; que[Nxt(R)] = to[i]; } } } } return dis[snk] < inf; } int dfs(int p, int flow) { if (p == snk) return flow; int ret = 0, tmp; for (int i = hd[p]; i != -1; i = nxt[i]) if (fl[i] && dis[p] + 1 == dis[to[i]]) { tmp = dfs(to[i], min(flow, fl[i])); fl[i] -= tmp; fl[i ^ 1] += tmp; flow -= tmp; ret += tmp; if (flow == 0) break; } return ret; } int maxflow() { int ret = 0; while (SPFA()) ret += dfs(src, inf); return ret; } } F; vector<int> son[MAXN]; void init() { scanf("%d", &n); int sz; int x; for (int i = (1), _END_ = (n); i <= _END_; i++) { scanf("%d", &sz); while (sz) { sz--; scanf("%d", &x); son[i].push_back(x); B.add(i, x); } } } void build() { int x; F.clear(); F.src = 0; F.snk = n + 1; for (int i = (1), _END_ = (n); i <= _END_; i++) { scanf("%d", &x); x = -x; if (x > 0) { ans += x; F.add(F.src, i, x); } else F.add(i, F.snk, -x); for (int j = (0), _END_ = (son[i].size()); j < _END_; j++) if (match[son[i][j]] != i) { F.add(i, match[son[i][j]], inf); } } } int main() { int _ = 0; init(); B.work(); build(); printf("%d\n", F.maxflow() - ans); close(); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int n, m, f[300005], v[600005], o[600005], w[600005], d[300005], l[300005], t, g, in[300005], S, T, p; bool b[600005], u[300005], h[300005]; void bd(int a, int b, int c) { v[++p] = b; o[p] = f[a]; f[a] = p; w[p] = c; } void dfs(int x, int y) { d[x] = l[x] = ++t; for (int i = (f[x]); i; i = o[i]) if (i != (y ^ 1)) { if (!d[v[i]]) { dfs(v[i], i); if (l[v[i]] > d[x]) b[i] = b[i ^ 1] = 1; l[x] = min(l[x], l[v[i]]); } else l[x] = min(l[x], d[v[i]]); } } void dfs2(int x, int y) { in[x] = y; for (int i = (f[x]); i; i = o[i]) if (!b[i]) { if (w[i]) h[y] = 1; if (!in[v[i]]) dfs2(v[i], y); } } bool dfs3(int x, int y) { u[x] = 1; if (h[in[x]]) y = 1; if (x == T) return y; for (int i = (f[x]); i; i = o[i]) if (!u[v[i]] && dfs3(v[i], w[i] ? 1 : y)) return 1; return 0; } int main() { p = 1; scanf("%d %d", &n, &m); for (int i = (0); i < (m); i++) { int a, b, c; scanf("%d %d %d", &a, &b, &c); bd(a, b, c); bd(b, a, c); } scanf("%d %d", &S, &T); dfs(1, 0); for (int i = (1); i <= (n); i++) if (!in[i]) dfs2(i, ++g); puts(dfs3(S, 0) ? "YES" : "NO"); }
11
CPP
#include <bits/stdc++.h> using namespace std; inline int RD() { int x; scanf("%d", &x); return x; } inline int RD(int& x) { return x = RD(); } inline int RD(int& x, int& y) { RD(x); RD(y); } inline int RD(int& x, int& y, int& z) { RD(x, y); RD(z); } const static int N = 60; int a[N][N]; int c[N]; pair<int, int> co[N * N]; int n, s; int main() { RD(n); for (int i = (1); i <= (n); i++) RD(c[i]), s += c[i]; for (int i = (1); i <= (n); i++) for (int j = (1); j <= (c[i]); j++) RD(a[i][j]), co[a[i][j]].first = i, co[a[i][j]].second = j; int x, y, xl = n, yl = c[n]; queue<pair<pair<int, int>, pair<int, int> > > q; for (int i = (s); i >= (1); i--) { x = co[i].first, y = co[i].second; if (xl != x || yl != y) { q.push(make_pair(make_pair(xl, yl), make_pair(x, y))); swap(co[a[xl][yl]], co[a[x][y]]); swap(a[xl][yl], a[x][y]); } yl--; if (yl == 0) { xl--; yl = c[xl]; } } cout << q.size() << endl; while (!q.empty()) { pair<pair<int, int>, pair<int, int> > x = q.front(); q.pop(); cout << x.first.first << ' ' << x.first.second << ' ' << x.second.first << ' ' << x.second.second << endl; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; struct _ { _() { ios_base::sync_with_stdio(0); } } _; template <class A, class B> ostream& operator<<(ostream& o, pair<A, B> t) { o << "(" << t.first << ", " << t.second << ")"; return o; } template <class T> void PV(T a, T b) { while (a != b) cout << *a++, cout << (a != b ? " " : "\n"); } template <class T> inline bool chmin(T& a, T b) { return a > b ? a = b, 1 : 0; } template <class T> inline bool chmax(T& a, T b) { return a < b ? a = b, 1 : 0; } template <class T> string tostring(T first, int len = 0) { stringstream ss; ss << first; string r = ss.str(); if (r.length() < len) r = string(len - r.length(), '0') + r; return r; } template <class T> void convert(string first, T& r) { stringstream ss(first); ss >> r; } const int inf = 0x3f3f3f3f; const long long linf = 0x3f3f3f3f3f3f3f3fLL; const int mod = int(1e9) + 7; const int N = 1111; int n, m, k, w; string s[N]; int d[N][N]; struct MST { static pair<int, vector<int> > primMST( const vector<vector<pair<int, int> > >& G, int src) { int n = G.size(); vector<int> used(n, 0), lowcost(n, inf), prev(n, -1); priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > PQ; int mst = 0; lowcost[src] = 0; prev[src] = -1; PQ.push(make_pair(0, src)); while (!PQ.empty()) { int u = PQ.top().second; PQ.pop(); if (used[u]) continue; used[u] = 1; mst += lowcost[u]; for (int i = 0; i < G[u].size(); i++) { int v = G[u][i].first; int c = G[u][i].second; if (!used[v] && chmin(lowcost[v], c)) { PQ.push(make_pair(lowcost[v], v)); prev[v] = u; } } } return make_pair(mst, prev); } }; int main() { cin >> n >> m >> k >> w; for (int i = 1; i <= k; i++) { for (int j = 0; j < n; j++) { string t; cin >> t; s[i] += t; } } for (int i = 1; i <= k; i++) for (int j = i + 1; j <= k; j++) { int cost = 0; for (int first = 0; first < n * m; first++) if (s[i][first] != s[j][first]) cost++; d[i][j] = d[j][i] = cost; } vector<vector<pair<int, int> > > G; G.resize(k + 1); for (int i = 1; i <= k; i++) { G[0].push_back(make_pair(i, n * m)); G[i].push_back(make_pair(0, n * m)); } for (int i = 1; i <= k; i++) for (int j = 1; j <= k; j++) { G[i].push_back(make_pair(j, d[i][j] * w)); G[j].push_back(make_pair(i, d[i][j] * w)); } auto res = MST::primMST(G, 0); int mst = res.first; vector<int> pre = res.second; cout << mst << endl; vector<int> used(k + 1, 0); vector<int> seq; while (1) { int updated = 0; for (int i = 0; i <= k; i++) { if (!used[i] && (pre[i] == -1 || used[pre[i]] == 1)) { seq.push_back(i); used[i] = 1; updated = 1; break; } } if (!updated) break; } for (int i = 1; i < seq.size(); i++) { int u = seq[i], v = pre[u]; if (v == -1) cout << u << " " << 0 << endl; else cout << u << " " << pre[u] << endl; } return 0; }
9
CPP
n,k=map(int,input().split()) d={} a=[] import sys input=sys.stdin.readline for i in range(n): s=input().rstrip() a.append(list(s)) for i in range(n): u="".join(a[i]) if u in d: continue d[u]=[0 for x in range(n)] d[u][i]=1 for j in range(i+1,n): if a[i]==a[j]: d[u][j]=d[u][j-1]+1 else: d[u][j]=d[u][j-1] #a.append(list(s)) c=0 for i in range(n): for j in range(i+1,n): z="S"*k z=list(z) l=['S','E','T'] for p in range(k): if a[i][p]==a[j][p]: z[p]=a[i][p] else: for y in l: if y!=a[i][p] and y!=a[j][p]: z[p]=y break #print(z) z="".join(z) if d.get(z)!=None: c+=d[z][n-1]-d[z][j] print(c)
8
PYTHON3
def calc_loop_cost(loop, min_w): if len(loop) == 1: return 0 elif len(loop) == 2: return loop[0] + loop[1] else: cost1 = sum(loop) + (len(loop) - 2) * min(loop) cost2 = sum(loop) + min(loop) + (len(loop) + 1) * min_w return min(cost1, cost2) n = int(input()) org_list = [int(s) for s in input().split()] sorted_list = sorted(org_list) check_list = [False] * n loop_list = [] for i in range(n): if check_list[i]: continue loop = [] check_list[i] = True loop.append(org_list[i]) next_idx = sorted_list.index(org_list[i]) while next_idx != i: check_list[next_idx] = True loop.append(org_list[next_idx]) next_idx = sorted_list.index(org_list[next_idx]) loop_list.append(loop) min_w = sorted_list[0] cost_sum = 0 for lp in loop_list: cost_sum += calc_loop_cost(lp, min_w) print(cost_sum)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; long long a[1000000]; int main() { long long n, ans = 0; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; sort(a, a + n); for (long long i = n - 1; i > -1; --i) ans += a[i] * min(i + 2, n); cout << ans; return 0; }
7
CPP
#include <iostream> #include <bits/stdc++.h> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> #include <vector> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <queue> #include <ctime> #include <cassert> #include <complex> #include <string> #include <cstring> #include <chrono> #include <random> #include <bitset> using namespace std; typedef long long ll; typedef long double ld; ll temp1,temp2,temp; char tempc; #define forn(i,n) for(int i=0;i<int(n);i++) #define FOR(i,a,b) for(int i=int(a);i<int(b);i++) #define N 1000000007 #define mp make_pair #define pb push_back #define F first #define S second #define pi pair<int,int> #define pl pair<ll,ll> #define int ll #define fast ios_base::sync_with_stdio(false); cin.tie(NULL) #define vi vector<int> ll binpow(ll a, ll b) { a%=N;b=b%(N-1); ll res=1; while(b>0) { if(b&1) res=res*a%N; a=a*a%N; b>>=1; } return res%N; } int simpow(int a,int b) { // b=b%(N-1); ll res=1; while(b>0) { if(b&1) res=res*a; a=a*a; b>>=1; } return res; } int gcd(int a,int b) { if (b == 0) return a; return gcd(b, a % b); } int mini(int a,int b) { if(a<b) return a; return b; } int maxi(int a,int b) { if(a>b) return a; return b; } struct DSU { vector<int> sz; vector<int> parent; void make_set(int v) { parent[v] = v; sz[v] = 1; } int find_set(int v) { if (v == parent[v]) return v; return parent[v] = find_set(parent[v]); } void union_sets(int a, int b) { a = find_set(a); b = find_set(b); if (a != b) { if (sz[a] < sz[b]) swap(a, b); parent[b] = a; sz[a] += sz[b]; } } DSU(int n) { sz.resize(n+1); parent.resize(n+1); for (int i = 1; i <= n; i++) make_set(i); } }; signed main() { #ifndef ONLINE_JUDGE // for getting input from input.txt freopen("inputf.in", "r", stdin); // for writing output to output.txt freopen("outputf.in", "w", stdout); #endif int t; cin >> t; while(t--) { int n; cin >> n; int count1=0; if(n%2==1) { forn(i,n) { for(int j=i+1;j<n;j++) { if(j-i<=(n-1)/2) cout << 1 << " "; else cout << -1 << " "; } } cout << endl; } else { forn(i,n) { if(i%2==0) { cout << 0 << " "; forn(j,(n-i-1)/2) cout << 1 << " "; forn(j,(n-i-1)/2) cout << -1 << " "; } else { forn(j,(n-i-1)/2) cout << -1 << " "; forn(j,(n-i-1)/2) cout << 1 << " "; } } cout << endl; } } }
9
CPP
import math, sys from collections import defaultdict, Counter, deque INF = float('inf') def gcd(a, b): while b: a, b = b, a%b return a def isPrime(n): if (n <= 1): return False i = 2 while i ** 2 <= n: if n % i == 0: return False i += 1 return True def primeFactors(n): factors = [] i = 2 while i ** 2 <= n: while n % i == 0: factors.append(i) n //= i i += 1 if n > 1: factors.append(n) return factors def vars(): return map(int, input().split()) def array(): return list(map(int, input().split())) def main(): n, r = vars() if n > r: ans = (r + 1) * r // 2 else: ans = (n - 1) * n // 2 ans += 1 print(ans) if __name__ == "__main__": t = int(input()) # t = 1 for _ in range(t): main()
8
PYTHON3
#include <bits/stdc++.h> bool is_checked[70][70][70]; int states[60000]; int parent[60000]; int next[60000]; int front = 0; int back = 0; char map[71][71]; int n; inline bool checked(int stones[3]); inline bool checked(int state); inline void state2stones(int state, int stones[3]); inline int stones2state(int stones[3]); inline void set_checked(int stones[3]); bool find_it(int stones[3]); int main() { scanf("%d", &n); int stones[3]; for (int i = 0; i < 3; i++) { scanf("%d", &stones[i]); stones[i]--; } for (int i = 0; i < n; i++) { gets(map[i]); if (strlen(map[i]) == 0) { i--; } } bool found = find_it(stones); if (!found) { printf("-1"); } else { int steps = 0; int s = back - 1; while (parent[s] != -1) { next[parent[s]] = s; s = parent[s]; steps++; } printf("%d\n", steps); s = next[0]; int stones_last[3]; state2stones(states[0], stones_last); for (int i = 0; i < steps; i++) { state2stones(states[s], stones); for (int j = 0; j < 3; j++) { if (stones[j] != stones_last[j]) { printf("%d %d\n", stones_last[j] + 1, stones[j] + 1); stones_last[j] = stones[j]; break; } } s = next[s]; } } return 0; } bool find_it(int stones[3]) { states[back] = stones2state(stones); parent[back++] = -1; if (stones[0] >= 0 && stones[0] <= 2 && stones[1] >= 0 && stones[1] <= 2 && stones[2] >= 0 && stones[2] <= 2) { return true; } while (front < back) { int state = states[front]; state2stones(state, stones); int stones_temp[3]; stones_temp[1] = stones[1]; stones_temp[2] = stones[2]; for (int i = 0; i < n; i++) { if (i == stones[0] || i == stones[1] || i == stones[2]) { continue; } if (map[i][stones[0]] == map[stones[1]][stones[2]]) { stones_temp[0] = i; if (!checked(stones_temp)) { states[back] = stones2state(stones_temp); parent[back++] = front; set_checked(stones_temp); if (stones_temp[0] >= 0 && stones_temp[0] <= 2 && stones_temp[1] >= 0 && stones_temp[1] <= 2 && stones_temp[2] >= 0 && stones_temp[2] <= 2) { return true; } } } } stones_temp[0] = stones[0]; stones_temp[2] = stones[2]; for (int i = 0; i < n; i++) { if (i == stones[0] || i == stones[1] || i == stones[2]) { continue; } if (map[i][stones[1]] == map[stones[0]][stones[2]]) { stones_temp[1] = i; if (!checked(stones_temp)) { states[back] = stones2state(stones_temp); parent[back++] = front; set_checked(stones_temp); if (stones_temp[0] >= 0 && stones_temp[0] <= 2 && stones_temp[1] >= 0 && stones_temp[1] <= 2 && stones_temp[2] >= 0 && stones_temp[2] <= 2) { return true; } } } } stones_temp[0] = stones[0]; stones_temp[1] = stones[1]; for (int i = 0; i < n; i++) { if (i == stones[0] || i == stones[1] || i == stones[2]) { continue; } if (map[i][stones[2]] == map[stones[0]][stones[1]]) { stones_temp[2] = i; if (!checked(stones_temp)) { states[back] = stones2state(stones_temp); parent[back++] = front; set_checked(stones_temp); if (stones_temp[0] >= 0 && stones_temp[0] <= 2 && stones_temp[1] >= 0 && stones_temp[1] <= 2 && stones_temp[2] >= 0 && stones_temp[2] <= 2) { return true; } } } } front++; } return false; } inline bool checked(int stones[3]) { return ( (stones[0] < stones[1]) ? (stones[1] < stones[2]) ? is_checked[stones[0]][stones[1]][stones[2]] : (stones[2] < stones[0]) ? is_checked[stones[2]][stones[0]][stones[1]] : is_checked[stones[0]][stones[2]][stones[1]] : (stones[2] < stones[1]) ? is_checked[stones[2]][stones[1]][stones[0]] : (stones[0] < stones[2]) ? is_checked[stones[1]][stones[0]][stones[2]] : is_checked[stones[1]][stones[2]][stones[0]]); } inline bool checked(int state) { int stones[3]; state2stones(state, stones); return checked(stones); } inline void set_checked(int stones[3]) { ((stones[0] < stones[1]) ? (stones[1] < stones[2]) ? is_checked[stones[0]][stones[1]][stones[2]] : (stones[2] < stones[0]) ? is_checked[stones[2]][stones[0]][stones[1]] : is_checked[stones[0]][stones[2]][stones[1]] : (stones[2] < stones[1]) ? is_checked[stones[2]][stones[1]][stones[0]] : (stones[0] < stones[2]) ? is_checked[stones[1]][stones[0]][stones[2]] : is_checked[stones[1]][stones[2]][stones[0]]) = true; } inline void state2stones(int state, int stones[3]) { stones[0] = state & 0x000000FF; stones[1] = (state >> 8) & 0x000000FF; stones[2] = (state >> 16) & 0x000000FF; } inline int stones2state(int stones[3]) { return ((stones[2] & 0x000000FF) << 16) | ((stones[1] & 0x000000FF) << 8) | (stones[0] & 0x000000FF); }
10
CPP
for case in range(int(input())): N = int(input()) arr = list(map(int, input().split())) arr.sort() answer = 9876543210 for n in range(N-1): answer = min(answer, arr[n+1]-arr[n]) print(answer)
8
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug& operator<<(const c&) { return *this; } }; const unsigned long long ONE = 1; int main() { int T = 30; int n; long long k; scanf("%d%lld", &n, &k); vector<unsigned long long> a(n), on(n); for (int i = 1; i < n; ++i) { int par; long long x; scanf("%d%llu", &par, &x); a[i] = a[par - 1] ^ x; } debug() << " [" << "a" ": " << (a) << "] "; long long count_smaller = 0; unsigned long long answer = 0; vector<unsigned long long> A(n); vector<pair<unsigned long long, unsigned long long>> B(n), tmp(n); for (int i = 0; i < n; ++i) { B[i] = make_pair(0ULL, a[i]); } sort(B.begin(), B.end()); vector<unsigned long long> sorted = a; sort(sorted.begin(), sorted.end()); for (int bit = 63; bit >= 0; --bit) { const unsigned long long ONLY = ONE << bit; answer |= ONLY; for (int i = 0; i < n; ++i) { if (a[i] & ONLY) { on[i] |= ONLY; } if (sorted[i] & ONLY) { A[i] |= ONLY; } } long long nowe = 0; for (int i = 0; i < n; ++i) { int type = T / 16 % 4; if (type == 3 || bool(B[i].second & ONLY) == type) { B[i].first ^= ONLY; } } auto ple = [&]() { for (int i = 0; i < n - 1; ++i) { if (B[i].first > B[i + 1].first) { int nxt = 0; int start = i; while (start - 1 >= 0 && B[start - 1].first == B[start].first) { --start; } int en = i + 1; while (en + 1 < n && B[en].first == B[en + 1].first) { ++en; } for (int j = start; j <= i; ++j) { tmp[nxt++] = B[j]; } nxt = start; for (int j = i + 1; j <= en; ++j) { B[nxt++] = B[j]; } for (int j = nxt; j <= en; ++j) { B[j] = tmp[j - nxt]; } } } }; ple(); int j = 0; for (int i = 0; i < n; ++i) { int cnt = 1; while (i + 1 < n && A[i] == A[i + 1]) { ++i; ++cnt; } while (j < n && B[j].first < A[i]) { ++j; } while (j < n && B[j].first == A[i]) { ++j; nowe += cnt; } } if (count_smaller + nowe < k) { count_smaller += nowe; for (int i = 0; i < n; ++i) { int type = T / 4 % 4; if (type == 3 || bool(B[i].second & ONLY) == type) { B[i].first ^= ONLY; } } } else { answer ^= ONLY; for (int i = 0; i < n; ++i) { int type = T % 4; if (type == 3 || bool(B[i].second & ONLY) == type) { B[i].first ^= ONLY; } } } ple(); } printf("%lld\n", answer); }
12
CPP
import sys def furui(n): l = [1 for i in range(n+1)] l[0] = 0 l[1] = 0 for i in range(2,n+1): if l[i] == 1: for j in range(2,n//i+1): l[i*j] = 0 l2 = [] for i in range (len(l)): if l[i] == 1: l2.append(i) return l2 for line in sys.stdin: n = int(line) l = furui(n) if max(l) == n: i1 = l[len(l)-2] else: i1 = max(l) i2 = n while True: i2 += 1 flag = 1 for i in l: if i2 % i == 0: flag = 0 break if flag: break print(str(i1) + " " + str(i2))
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[520], b[520], c[520]; int main() { int n, i, j; cin >> n; int x, d = 0, g = 0; x = 2 * n; for (i = 0; i < x; i++) { cin >> a[i]; if (i % 2 == 0) { b[g] = a[i]; g++; } else { c[d] = a[i]; d++; } } int count = 0; for (i = 0; i < g; i++) { for (j = 0; j < d; j++) { if (b[i] == c[j]) count++; } } cout << count << endl; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; const double eps = 1e-11; const double PI = acos(-1.0); const int MAX = 1e5 + 5; const int INF = 1e9; const int IINF = -1e9; struct node { int left, right, maxn, minn; } p[MAX << 2]; int father[MAX << 2]; int arr[MAX]; void Build_Tree(int l, int r, int x) { if (l == r) { p[x].left = l; p[x].right = r; p[x].minn = arr[l]; p[x].maxn = arr[l]; father[l] = x; return; } p[x].left = l; p[x].right = r; p[x].maxn = IINF; p[x].minn = INF; Build_Tree(l, (l + r) >> 1, x << 1); Build_Tree(((l + r) >> 1) + 1, r, (x << 1) + 1); p[x].maxn = max(p[x << 1].maxn, p[(x << 1) + 1].maxn); p[x].minn = min(p[x << 1].minn, p[(x << 1) + 1].minn); } int imax, imin; void Find(int l, int r, int x) { if (p[x].left == l && p[x].right == r) { imax = max(p[x].maxn, imax); imin = min(imin, p[x].minn); return; } x <<= 1; if (p[x].right >= l) { if (p[x].right >= r) { Find(l, r, x); } else Find(l, p[x].right, x); } x++; if (p[x].left <= r) { if (p[x].left <= l) { Find(l, r, x); } else Find(p[x].left, r, x); } } char s[MAX]; int main() { int n; while (~scanf("%d", &n)) { for (int i = 1; i < n + 1; i++) { scanf("%d", &arr[i]); } scanf("%s", s + 1); Build_Tree(1, n, 1); int l = IINF, r = INF; for (int i = 5; i < n + 1; i++) { if (s[i] == s[i - 1]) { int flag = 0; int mi = arr[i], ma = arr[i]; for (int j = i - 1; j >= i - 3; j--) { mi = min(arr[j], mi); ma = max(arr[j], ma); if (s[j] != s[j - 1]) { flag = 1; } } mi = min(arr[i - 4], mi); ma = max(arr[i - 4], ma); if (!flag) { if (s[i] == '1') { r = max(r, mi); } else { l = min(ma, l); } } } else { imax = IINF; imin = INF; if (s[i] == '1') { Find(i - 4, i, 1); l = max(imax + 1, l); } else { Find(i - 4, i, 1); r = min(r, imin - 1); } } } printf("%d %d\n", l, r); } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long int a, b, c, d, tmp1, tmp2; cin >> a >> b >> c >> d; tmp1 = max(3 * a / 10, a - a / 250 * c); tmp2 = max(3 * b / 10, b - b / 250 * d); if (tmp1 > tmp2) { cout << "Misha"; } else if (tmp1 < tmp2) { cout << "Vasya"; } else { cout << "Tie"; } return 0; }
7
CPP
n = int(input()) a = list(map(int, input().split())) k = 0 ch = 0 for i in a: if i % 2 == 0: ch += 1 print(min(ch, len(a) - ch))
7
PYTHON3
n = int(input()) if n > 0: print(n) else: n_neg = -n print(-((n_neg // 100) * 10 + min(n_neg % 10, (n_neg // 10) % 10)))
7
PYTHON3
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=2e5+5; ll n,a[maxn]; int main(){ cin>>n; for(int i=0;i<n;i++){ cin>>a[i]; } sort(a,a+n); ll sum=a[n-1],t=n-2,tmp=t/2; int i=n-2; for(;i>n-2-tmp;i--){ sum+=2*a[i]; } if(t%2)sum+=a[i]; cout<<sum; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 255; inline int read() { int x = 0, f = 1; char ch = 0; for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48; return x * f; } struct node { int l, r, val; } tr[maxn]; int dp[maxn][3][4], T, n, cz[maxn]; void dfs(int u) { if (tr[u].val != -1) return; dfs(tr[u].l); cz[u] += cz[tr[u].l]; dfs(tr[u].r); cz[u] += cz[tr[u].r]; cz[u]++; if (cz[tr[u].r] & 1) swap(tr[u].l, tr[u].r); } inline int cmp(int a, int b, bool c) { if (a == 0 || b == 0) return a + b; return (a > b) ^ c ? a : b; } inline int solve(int u, int player, int skip) { if (tr[u].val > 0) return tr[u].val; if (dp[u][player][skip]) return dp[u][player][skip]; if (cz[u] == 1) return cmp(tr[tr[u].l].val, tr[tr[u].r].val, player ^ (skip == 2)); int res = 0; if (skip) { if (cz[u] & 1) { if (skip == 1) res = solve(u, !player, 0); if (cz[tr[u].l] & 1) { res = cmp( res, cmp(solve(tr[u].l, player, 0), solve(tr[u].r, !player, cz[tr[u].l] == 1 && skip == 1 ? 2 : 1), !player), player); res = cmp( res, cmp(solve(tr[u].r, player, 0), solve(tr[u].l, !player, cz[tr[u].r] == 1 && skip == 1 ? 2 : 1), !player), player); } else { if (cz[tr[u].r]) res = cmp(res, cmp(solve(tr[u].l, !player, 0), solve(tr[u].r, player, !cz[tr[u].l] && skip == 1 ? 2 : 1), !player), player); if (cz[tr[u].l]) res = cmp(res, cmp(solve(tr[u].r, !player, 0), solve(tr[u].l, player, !cz[tr[u].r] && skip == 1 ? 2 : 1), !player), player); } } else res = cmp(solve(tr[u].r, player, 0), solve(tr[u].l, player, !cz[tr[u].r] && skip == 1 ? 2 : 1), player); } else { if (cz[u] & 1) { if (cz[tr[u].l] & 1) res = cmp(solve(tr[u].l, player, 1), solve(tr[u].r, player, 1), player); else res = cmp(solve(tr[u].l, player, 0), solve(tr[u].r, player, 0), player); } else { int p = cmp(solve(tr[u].l, player, 0), solve(tr[u].r, !player, 0), !player); int q = cmp(solve(tr[u].l, !player, 1), solve(tr[u].r, player, 1), !player); res = cmp(p, q, player); } } return dp[u][player][skip] = res; } int main() { T = read(); while (T--) { n = read(); for (int i = 0; i <= n; ++i) tr[i].val = 0, tr[i].l = 0, tr[i].r = 0; memset(cz, 0, sizeof(cz)); memset(dp, 0, sizeof(dp)); for (int i = 0; i < n; ++i) { int opt = read(), l, r; if (opt == -1) tr[i].val = -1, tr[i].l = read(), tr[i].r = read(); else tr[i].val = opt + 1; } dfs(0); printf("%d\n", solve(0, 0, 0) - 1); } }
12
CPP
#include <bits/stdc++.h> #define rep(i,n)for(int i=0;i<(n);i++) using namespace std; int w,h; int dp[2000][2000][2],s[2000][2000]; int sum[2000][2000]; int dx[]{1,0},dy[]{0,1}; int rec(int x,int y,int a){ if(dp[x][y][a]!=-1)return dp[x][y][a]; if(y==w)return 0; int res=(a==0?INT_MIN:INT_MAX); rep(i,2){ int nx=x+dx[i],ny=y+dy[i]; if(nx>h)continue; int b=rec(nx,ny,!a)+(i==1?sum[x][y]:0); if(a==0)res=max(res,b); else res=min(res,b); } return dp[x][y][a]=res; } int main(){ scanf("%d%d",&w,&h); rep(i,h)rep(j,w)scanf("%d",&s[i][j]); int a=0; rep(i,w)rep(j,h){ sum[j+1][i]=sum[j][i]+s[j][i]; a+=s[j][i]; } memset(dp,-1,sizeof(dp)); cout<<abs(a-rec(0,0,0)*2)<<endl; }
0
CPP
n=int(input()) arr = list(map(int, input().rstrip().split())) arr.sort(reverse=True) if n%2 == 1 : print(arr[n//2]) else : print(arr[n//2])
7
PYTHON3
n = int(input()) names = {} for i in range(n): name = input() if name in names: k = names[name] names[name] = k + 1 print(name + str(k + 1)) else: names[name] = 0 print('OK')
9
PYTHON3
n=int(input()) s=[] for i in range(n): s.append(input()) for i in range(n): if len(s[i]) > 10 : print(s[i][0],end='') print(len(s[i])-2,end='') print(s[i][len(s[i])-1]) else : print(s[i])
7
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimize(3) using namespace std; const int maxn = 1000005; int nxt[maxn]; char a[maxn], b[maxn]; int main() { scanf("%s", a + 1); int n = strlen(a + 1); int m = n; for (register int i = 1; i <= n; i++) { b[i] = a[i]; } for (register int i = 2, j; i <= n; i++) { j = nxt[i - 1]; while (j && a[i] != a[j + 1]) { j = nxt[j]; } if (a[i] == a[j + 1]) { j++; } nxt[i] = j; } if (!nxt[n]) { printf("Just a legend"); return 0; } while (1) { n = nxt[n]; if (n == 0) { break; } for (int i = nxt[n]; i <= m - 1; i++) { if (nxt[i] == n) { for (register int k = 1; k <= n; k++) { printf("%c", b[k]); } return 0; } } } printf("Just a legend"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; vector<int> l, r; cin >> n; for (int i = 0; i < n; i++) { int temp; cin >> temp; l.push_back(temp); } for (int i = 0; i < n; i++) { int temp; cin >> temp; r.push_back(temp); } if (l[0] != 0 || r[n - 1] != 0) { cout << "NO" << endl; return 0; } vector<int> ret(n); int candies = n; for (int i = 0; i < n; i++) { ret[i] = candies - (r[i] + l[i]); if (ret[i] < 0) { cout << "NO" << endl; return 0; } } for (int i = 0; i < n; i++) { int count = 0; for (int j = 0; j < i; j++) { if (ret[j] > ret[i]) { count++; } } if (l[i] == count) { continue; } else { cout << "NO" << endl; return 0; } } for (int i = 0; i < n; i++) { int count = 0; for (int j = i + 1; j < n; j++) { if (ret[j] > ret[i]) { count++; } } if (r[i] == count) { continue; } else { cout << "NO" << endl; return 0; } } cout << "YES" << endl; int mn = *min_element(ret.begin(), ret.end()); for (int i = 0; i < n; i++) { cout << ret[i] << " "; } cout << endl; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int func(int x) { if (x <= 4) { if (x == 4) { return 2; } else { return x % 2; } } else if (x % 2 == 1) { return 0; } else { int prev = func(x / 2); if (prev == 1) { return 2; } else { return 1; } } } int main() { int n, k; scanf("%d%d", &n, &k); int res = 0; for (int i = 0; i < n; i++) { int x; scanf("%d", &x); if (k % 2 == 0) { if (x <= 2) { res ^= x; } else if (x % 2 == 0) { res ^= 1; } } else { res ^= func(x); } } if (res) { printf("Kevin\n"); } else { printf("Nicky\n"); } return 0; }
9
CPP
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("avx2") using namespace std; vector<long long> vec; long long t; long long cnt[10000][5]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n, m; cin >> n; vec.resize(n); for (int i = 0; i < n; i++) cin >> vec[i]; long long ch = 0, nech = 0, sum = 0, maxn = 0; for (int i = 0; i < n; i++) { if (vec[i] % 2 == 0) ch++; else nech++; sum += vec[i]; maxn = max(maxn, vec[i]); } if (sum % 2 == 0 && maxn <= sum - maxn) { cout << "YES"; } else { cout << "NO"; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 100005; int n, d[N], c[N], p[N], q, t; vector<int> e[N], r, s; void f(int x, int y){ int u = int(e[x].size()) - d[x]; if(d[x] == 1){ if(!q){ p[1] = q++; for(int i = t + 2; i <= t + u; i++) p[i] = q++; p[2] = q++; t += (u + 1); } else{ for(int i = t + 1; i < t + u; i++) p[i] = q++; p[t] = q++; p[n] = q++; } } else{ for(int i = t + 1; i <= t + u; i++) p[i] = q++; p[t] = q++; t += (u + 1); } for(int i : e[x]) if(i != y && d[i]) f(i, x); } int main(){ scanf("%d", &n); if(n == 1){ puts("1"); return 0; } for(int i = 0, x, y; i < n - 1; i++){ scanf("%d%d", &x, &y); e[x].push_back(y); e[y].push_back(x); } for(int i = 1; i <= n; i++) c[i] = (e[i].size() > 1); for(int i = 1; i <= n; i++) for(int j : e[i]) if(c[i] && c[j]) d[i]++; if(count(d + 1, d + n + 1, 1) > 2){ puts("-1"); return 0; } if(count(d + 1, d + n + 1, 1) == 0){ printf("1 "); for(int i = 3; i <= n - 1; i++) printf("%d ", i); printf("2 %d", n); return 0; } q = 0; t = 1; for(int i = 1; i <= n; i++) if(d[i] == 1){ f(i, 0); break; } r.resize(n); for(int i = 1; i <= n; i++) r[p[i]] = i; q = 0; t = 1; for(int i = n; i >= 1; i--) if(d[i] == 1){ f(i, 0); break; } s.resize(n); for(int i = 1; i <= n; i++) s[p[i]] = i; r = min(r, s); for(int i : r) printf("%d ", i); puts(""); }
0
CPP
for _ in range(int(input())): n=int(input()) ar=list(map(int,input().split())) for i in range(0,n,2): ar[i]=-abs(ar[i]) for i in range(1,n,2): ar[i]=abs(ar[i]) print(*ar)
7
PYTHON3
n,m = map(int,input().split()) print(m+n-1) for i in range(1,n+1): if i == 1: for j in range(1,m+1): print(i," ",j) else: print(i, " ", 1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { char c; cin>>c; if(c<'a') cout<<"A"; else cout<<"a"; }
0
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; vector<int> adj[N]; bool vis[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } list<int> v(n); for (int& i : v) cin >> i; bool flag = true; if (v.front() != 1) flag = false; queue<int> Q; Q.push(v.front()), v.pop_front(); while (!Q.empty()) { int top = Q.front(); Q.pop(), vis[top] = true; set<int> s1, s2; list<int> x; for (int i : adj[top]) { if (vis[i]) continue; s1.insert(i); } for (int i = 0; i < s1.size(); i++) { x.push_back(v.front()); s2.insert(v.front()); v.pop_front(); } if (s1 != s2) { flag = false; break; } while (x.size()) Q.push(x.front()), x.pop_front(); } cout << (flag ? "Yes" : "No") << endl; }
10
CPP
n = int(input()) ans = [] for i in range(n): lst = list(map(int,input().split())) if lst[0] == 0: ans.append(lst[1]) elif lst[0] == 1: print(ans[lst[1]]) else: del ans[-1]
0
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int N = 1000 + 10; int k; int dp[N][N], p10[N]; void init() { int i, j; p10[0] = 1; for (i = 1; i < N; i++) p10[i] = (p10[i - 1] * 10LL) % MOD; memset(dp[0], 0, sizeof(dp[0])); for (i = 1; i < N; i++) for (j = 1; j <= k + 1; j++) { int &r = dp[i][j]; r = (dp[i - 1][min(k + 1, j + 1)] * 8LL) % MOD; if (j <= k) r += (p10[i - 1] * 2) % MOD; else r += (dp[i - 1][1] * 2) % MOD; r %= MOD; } } char a[N], b[N]; int getCnt(char s[], int t) { int i, n = strlen(s), last = k + 1, res = 0, e, x, good = 0; for (i = 0; i < n; i++) { e = 0; x = s[i] - '0' - 1; if (x >= 4) e++; if (x >= 7) e++; x++; if (good == 1) { res += (((long long)x) * p10[n - i - 1]) % MOD; res %= MOD; continue; } res += (((long long)x - e) * dp[n - i - 1][min(last + 1, k + 1)]) % MOD; res %= MOD; if (last <= k) res += (p10[n - i - 1] * e) % MOD; else res += (dp[n - i - 1][1] * e) % MOD; res %= MOD; if (s[i] == '4' || s[i] == '7') { if (last <= k) good = 1; else last = 1; } else last = min(last + 1, k + 1); } if (good && t) res = (res + 1) % MOD; return res; } int main() { int t, i; scanf("%d%d", &t, &k); init(); for (i = 0; i < t; i++) { scanf("%s%s", a, b); printf("%d\n", (((getCnt(b, 1) - getCnt(a, 0)) + MOD) % MOD)); } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { string S; cin >> S; int N = S.size(); cout << S.substr(0, N-8) << "\n"; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; long long inf = 1e18; long long mx = -1; long long mn = 1e18; long long calpow(long long a, long long b, long long md) { long long mul = 1; while (b > 0) { if (b & 1) { mul = ((a % md) * (mul % md)) % md; b--; } else { a = ((a % md) * (a % md)) % md; b /= 2; } } return mul; } void solve() { long long i, j, n, t, q, a, b, m, k, ct = 0, sum = 0, flag = 0; string s; cin >> s; if (s.size() == 1) cout << 0; else { n = s.size(); long long dp[n + 1]; dp[n] = 0; for (i = n - 1; i >= 1; i--) { dp[i] = ((((n - i) % mod) * calpow(10, n - i - 1, mod)) % mod + dp[i + 1]) % mod; } for (i = 1; i <= n; i++) { j = (long long)(s[i - 1] - '0'); sum = (sum + ((((i * (i - 1)) / 2 % mod * (calpow(10, n - i, mod))) % mod + dp[i]) % mod * j) % mod) % mod; } cout << sum; } } signed main() { ios::sync_with_stdio(false); cin.tie(NULL); long long t = 1; while (t--) { solve(); cout << "\n"; } cerr << clock() * 1000.0 / CLOCKS_PER_SEC << 'm' << 's'; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; inline int change_pixel(int v, string &str) { int nv = v; const int d[7][7] = { { 8, 9, 3, 4, 5, 0, 1}, { 9, 10, 4, 5, 6, 1, 2}, {12, 13, 7, 8, 9, 3, 4}, {13, 14, 8, 9, 10, 4, 5}, {14, 15, 9, 10, 11, 5, 6}, {16, 17, 12, 13, 14, 8, 9}, {17, 18, 13, 14, 15, 9, 10} }; for (int i = 0; i < 7; i++) { int bit = 0; nv &= ~(1 << d[i][3]); for (int j = 0; j < 7; j++) { bit |= (v >> d[i][j] & 1) ? (1<<j) : 0; } nv |= (1 << d[i][3])*(str[bit] - '0'); } return nv; } bool is_idempotent(string &str) { for (int i = 0; i < (1<<19); i++) { int before = i, after; before = change_pixel(before, str); after = change_pixel(before, str); if ((before >> 9 & 1) != (after >> 9 & 1)) { return false; } } return true; } int main() { string str; while (cin >> str, str != "#") { cout << (is_idempotent(str) ? "yes" : "no") << endl; } return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5; int n, mx[maxn + 10], fa[maxn + 10]; int f[maxn + 10]; vector<int> g[maxn + 10]; map<int, int> mp[maxn + 10]; int ask(int x, int y) { if (mp[x].count(y)) return mp[x][y]; int ans = 1; for (int i = 0; i < (int)g[x].size(); ++i) { int e = g[x][i]; if (e != fa[x] && e < y) ans += ask(e, y); } return mp[x][y] = ans; } void dfsfa(int p) { mx[p] = max(mx[fa[p]], fa[p]); for (int i = 0; i < (int)g[p].size(); ++i) { int e = g[p][i]; if (e != fa[p]) { fa[e] = p; dfsfa(e); } } } void dfs(int p) { if (p > 1) f[p] = f[fa[p]] + (p > mx[fa[p]] ? ask(p, mx[p]) : ask(p, mx[p]) - ask(p, mx[fa[p]])); for (int i = 0; i < (int)g[p].size(); ++i) { int e = g[p][i]; if (e != fa[p]) dfs(e); } } int main() { scanf("%d", &n); for (int i = 1; i < n; ++i) { int l, r; scanf("%d%d", &l, &r); g[l].push_back(r); g[r].push_back(l); } dfsfa(1); dfs(1); for (int i = 2; i <= n; ++i) printf("%d ", f[i]); }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { char *a, *a1, *a2; int n = 100; a = new char[n]; a1 = new char[n]; cin >> a >> a1; int p = strlen(a); a2 = new char[p + 1]; for (int i = 0; i < p; i++) { if (a[i] == a1[i]) a2[i] = '0'; else a2[i] = '1'; } a2[p] = '\0'; cout << a2; return 0; }
7
CPP
for _ in range(int(input())): n = int(input()) arr = list(map(int,input().split())) t = [] for i in range(n): t.append(arr[i]-i) map_val = {} for i in range(n): if(t[i] not in map_val): map_val[t[i]] = 1 else: map_val[t[i]]+=1 res = 0 for i in map_val: if(map_val[i]>1): res += (abs(map_val[i])*(abs(map_val[i]-1)))//(1+1) print(res)
10
PYTHON3
n,p,w,d = list(map(int, input().split())) W = p//w flag = 0 for i in range(min(W+1,1000000)): win_cost = i*w draw_cost = p - win_cost if draw_cost%d == 0: if (draw_cost//d + i) <= n: flag = 1 print(i, draw_cost//d, n-(i + draw_cost//d)) break D = p//d if flag == 0: for i in range(min(D+1,1000000)): draw_cost = i*d win_cost = p - draw_cost if win_cost%w == 0: if (win_cost//w + i) <= n: flag = 1 print(win_cost//w,i , n-(i + win_cost//w)) break if flag == 0: print(-1)
9
PYTHON3
for doors in range(int(input())): a, k = [], -1 num = int(input()) stamps = map(int, input().split()) for i in stamps: a += [i * k] k *= -1 print(*a[::-1])
7
PYTHON3
n = int(input()) arr = [int(i) for i in input().split()] for i in range(n): tmp = arr[i] while tmp > 0: print('P', end='') tmp -= 1 if tmp: if i == 0:print('RL', end='') else:print('LR', end='') if i < n-1:print('R', end='')
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, d, x, y; void solve() { cin >> n >> x >> y >> d; x--, y--; if (abs(y - x) % d == 0) { cout << abs(y - x) / d << endl; return; } int res = 2e9; if (y % d == 0) { res = min(res, (x - 1) / d + 1 + y / d); } if ((n - 1 - y) % d == 0) { res = min(res, ((n - 1 - x - 1) / d + 1 + (n - 1 - y) / d)); } if (res == 2e9) cout << "-1\n"; else cout << res << endl; } int main() { int t; cin >> t; while (t--) { solve(); } }
7
CPP
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; const long long MOD = 1000000007; long long a[300005]; long long sum[300005]; long long block[300005]; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, m, k; cin >> n >> m >> k; vector<long long> a(n); for (long long i = 1; i <= n; i++) { cin >> a[i]; sum[i] = a[i] + sum[i - 1]; } long long ans = 0; for (long long i = m; i <= n; i++) { block[i] = (block[i - m] + sum[i] - sum[i - m] - k > 0 ? block[i - m] + sum[i] - sum[i - m] - k : 0); ans = max(ans, block[i]); } for (long long i = 1; i <= n; i++) { long long cum = 0; for (long long j = 0; j < m && i + j <= n; j++) { cum += a[i + j]; ans = max(ans, block[i - 1] + cum - k); } } cout << ans << '\n'; }
10
CPP
#include <bits/stdc++.h> using namespace std; vector<int> adj[26]; stack<char> st; bool vis[26]; int c[26]; long long par[26]; void toposort(int v) { vis[v] = true; par[v] = 1; for (auto i : adj[v]) { if (!vis[i]) { toposort(i); } else if (par[i]) { cout << "Impossible"; exit(0); } } par[v] = 0; st.push(char(v + 97)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, i, j; cin >> n; string s[n]; for (i = 0; i < n; i++) cin >> s[i]; for (i = 1; i < n; i++) { string s1 = s[i - 1]; string s2 = s[i]; int f = 0; for (j = 0; j < min(s1.length(), s2.length()); j++) { if (s1[j] != s2[j]) { adj[s1[j] - 97].push_back(s2[j] - 97); f = 1; break; } } if (f == 0 && s2.size() < s1.size()) return cout << "Impossible", 0; } for (i = 0; i < 26; i++) { if (!vis[i]) toposort(i); } while (!st.empty()) { cout << st.top(); c[st.top() - 97] = 1; st.pop(); } for (i = 0; i < 26; i++) { if (!c[i]) cout << char(i + 97); } }
9
CPP
#include <bits/stdc++.h> using namespace std; void solve() { string s, t; cin >> s >> t; int i = 0, j = 0, n1 = s.length(), n2 = t.length(); vector<int> v; for (i = 0; i <= n1 - n2; i++) { if (s[i] == t[0]) v.push_back(i); } for (int k = 0; k < v.size(); k++) { i = v[k]; j = 0; while (i < n1) { if (s[i] == t[j]) { i++; j++; } else i = i + 2; } if (j == n2 && i == n1) { cout << "YES\n"; return; } } cout << "NO\n"; return; } int main() { cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false); int k; cin >> k; while (k--) solve(); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { string s; getline(cin, s); int current; int len = s.length(); for (int i = len; i >= 0; i--) { current = tolower(s[i]); if (!(current >= 'a' && current <= 'z')) { continue; } if (current == 'a' || current == 'e' || current == 'i' || current == 'o' || current == 'u' || current == 'y') { cout << "YES" << endl; } else { cout << "NO" << endl; } break; } }
7
CPP
#include <bits/stdc++.h> using namespace std; char s[10]; map<char, int> mp; void Init() { mp['R'] = 0; mp['O'] = 1; mp['Y'] = 2; mp['G'] = 3; mp['B'] = 4; mp['V'] = 5; } int num[10]; int cmp(int a, int b) { return a > b; } int main() { Init(); memset(num, 0, sizeof(num)); scanf("%s", s); for (int i = 0; i < 6; i++) { num[mp[s[i]]]++; } sort(num, num + 6, cmp); if (num[0] == 6 || num[0] == 5) { printf("1\n"); } else if (num[0] == 4 || (num[0] == 3 && num[1] == 3)) { printf("2\n"); } else if ((num[0] == 3 && num[1] == 2)) { printf("3\n"); } else if ((num[0] == 3 && num[1] == 1)) { printf("5\n"); } else if (num[0] == 2 && num[1] == 2 && num[2] == 2) { printf("6\n"); } else if (num[0] == 2 && num[1] == 2 && num[2] == 1) { printf("8\n"); } else if (num[0] == 2 && num[1] == 1) { printf("15\n"); } else printf("30\n"); return 0; }
7
CPP
t = int(input()) for s in range(t) : ans = [] x = str(input()) if len(x) < 3 : print(x) else : ans.append(x[0]) for i in range(1,len(x)-1) : if i % 2 != 0 : ans.append(x[i]) else : continue ans.append(x[len(x)-1]) print(''.join(map(str,ans)))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 7; const int INF = 1e9 + 7; int n, m; char str[N][N], tmp[N][N]; int ans = INF; pair<int, int> w; bool dfs(pair<int, int> cur) { if (cur.first + w.first < n && str[cur.first + w.first][cur.second] == 'X') { for (int i = (0); i < (w.second); i++) if (str[cur.first + w.first][cur.second + i] != 'X') return false; else str[cur.first + w.first][cur.second + i] = '.'; return dfs({cur.first + 1, cur.second}); } if (cur.second + w.second < m && str[cur.first][cur.second + w.second] == 'X') { for (int i = (0); i < (w.first); i++) if (str[cur.first + i][cur.second + w.second] != 'X') return false; else str[cur.first + i][cur.second + w.second] = '.'; return dfs({cur.first, cur.second + 1}); } for (int i = (0); i < (n); i++) for (int j = (0); j < (m); j++) if (str[i][j] == 'X') return false; return true; } void gao() { pair<int, int> cur = {-1, -1}, beg = {-1, -1}; for (int i = (0); i < (n); i++) if (!~cur.first) for (int j = (0); j < (m); j++) if (str[i][j] == 'X') { beg = cur = {i, j}; break; } w = {1, 1}; while (cur.first + w.first < n && str[cur.first + w.first][cur.second] == 'X') w.first++; if (cur.first + w.first < n) { while (cur.second < m && str[cur.first + w.first][cur.second] != 'X') cur.second++; while (cur.second + w.second < m && str[cur.first + w.first][cur.second + w.second] == 'X' && str[cur.first][cur.second + w.second] == 'X') w.second++; } for (int i = (0); i < (w.first); i++) for (int j = (0); j < (w.second); j++) if (str[beg.first + i][beg.second + j] != 'X') return; else { str[beg.first + i][beg.second + j] = '.'; } if (dfs(beg)) ans = min(ans, w.first * w.second); } int main() { cin >> n >> m; for (int i = (0); i < (n); i++) scanf(" %s", str[i]); for (int i = (0); i < (n); i++) for (int j = (0); j < (m); j++) tmp[i][j] = str[i][j]; gao(); swap(n, m); for (int i = (0); i < (n); i++) for (int j = (0); j < (m); j++) str[i][j] = tmp[j][i]; gao(); if (ans < INF) printf("%d", ans); else printf("-1"); }
9
CPP
weight = int(input()) if weight % 2 or weight == 2: print("NO") else : print("YES")
7
PYTHON3
#include<bits/stdc++.h> using namespace std; #define int long long const int MOD = 1000000007LL; int m, n; int po[13] = {}; void init(){ po[0] = 1; for(int i = 1; i < 13; i++) po[i] = po[i - 1] * 3; } int dfs(vector<string> &data, vector<int> &dp, int S){ int ret = dp[S]; if(ret != MOD) return ret; string state = ""; int temp_S = S; for(int i = 0; i < m; i++){ string add = ""; if(S % 3 == 0) add = "0"; else if(S % 3 == 1) add = "1"; else add = "2"; state = add + state; S /= 3; } //cout << "state is " << state << endl; //残っている data の数 int cnt = 0; for(int i = 0; i < n; i++){ bool ok = true; for(int j = 0; j < m; j++){ if(state[j] == '2') continue; if(data[i][j] != state[j]) ok = false; } cnt += ok; } if(cnt <= 1){ ret = 0; dp[S] = ret; //cout << "dp[" + state + "] = " << dp[S] << endl; return ret; } // 未決定 (2) のところを 0 or 1 に変更して遷移する S = temp_S; for(int i = 0; i < m; i++){ if(state[i] == '2'){ int T1 = S + po[m - i - 1] * (0 - 2); int T2 = S + po[m - i - 1] * (1 - 2); //cout << S << " " << state << " " << T1 << " " << T2 << endl; int temp = max(dfs(data, dp, T1), dfs(data, dp, T2)) + 1; ret = min(ret, temp); } } dp[S] = ret; //cout << "dp[" + state + "] = " << dp[S] << endl; return ret; } signed main(){ init(); while(1){ cin >> m >> n; if(n == 0) break; vector<string> data(n); for(int i = 0; i < n; i++) cin >> data[i]; int S = (int)po[m] - 1; // 状態 S を特定するために必要な最大の質問回数 vector<int> dp(S + 1, MOD); cout << dfs(data, dp, S) << endl; } return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100010; const int S = 230; const int nbits = 20; const int inf = 1e9; struct Query { int l, r, p, i; Query() {} Query(int l, int r, int i) : l(l), r(r), i(i) { p = l / S; } bool operator<(const Query &other) const { return (p < other.p || (p == other.p && r < other.r)); } }; struct State { int minval, maxval; int next[2]; State() { next[0] = -1; next[1] = -1; minval = inf; maxval = -inf; } }; int n, m, sz1, sz2, tmpsz1, tmpsz2, a[N], f1[N], f2[N], ans[N]; Query q[N]; vector<State> st1, st2, tmpst1, tmpst2; void Clear(vector<State> &st, int &sz) { st.clear(); sz = 1; st.push_back(State()); } int f(int x) { if (x % 4 == 0) return x; if (x % 4 == 1) return 1; if (x % 4 == 2) return x + 1; return 0; } int getbit(int x, int i) { if (x & (1 << i)) return 1; return 0; } void add(int x, int val, vector<State> &st, int &sz) { int cur = 0; st[cur].minval = min(st[cur].minval, x); st[cur].maxval = max(st[cur].maxval, x); for (int i = nbits - 1; i >= 0; i--) { int b = getbit(val, i); if (st[cur].next[b] == -1) { st[cur].next[b] = sz++; st.push_back(State()); } cur = st[cur].next[b]; st[cur].minval = min(st[cur].minval, x); st[cur].maxval = max(st[cur].maxval, x); } } int get1(int x, int val, const vector<State> &st, const int &sz) { int cur = 0; int res = 0; if (st[cur].minval > x) { res = -inf; return res; } for (int i = nbits - 1; i >= 0; i--) { int b = getbit(val, i); for (int j = 0; j < 2; j++) { int tmp = st[cur].next[b ^ 1]; if (tmp != -1 && st[tmp].minval <= x) { if (j == 0) res += 1 << i; cur = tmp; break; } b ^= 1; } } return res; } int get2(int x, int val, const vector<State> &st, const int &sz) { int cur = 0; int res = 0; if (st[cur].maxval < x) { res = -inf; return res; } for (int i = nbits - 1; i >= 0; i--) { int b = getbit(val, i); for (int j = 0; j < 2; j++) { int tmp = st[cur].next[b ^ 1]; if (tmp != -1 && st[tmp].maxval >= x) { if (j == 0) res += 1 << i; cur = tmp; break; } b ^= 1; } } return res; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < m; i++) { int l, r; scanf("%d%d", &l, &r); l--; r--; q[i] = Query(l, r, i); } sort(q, q + m); for (int i = 0; i < n; i++) { f1[i] = f(a[i]); f2[i] = f(a[i] - 1); } int prev = 0; for (int i = 0; i < m; i++) if (i == m - 1 || q[i].p != q[i + 1].p) { int lb = q[prev].l; for (int j = prev + 1; j <= i; j++) { lb = max(lb, q[j].l); } Clear(st1, sz1); Clear(st2, sz2); int res = 0; int rb = lb - 1; for (int j = prev; j <= i; j++) { int l = q[j].l; int r = q[j].r; Clear(tmpst1, tmpsz1); Clear(tmpst2, tmpsz2); while (rb < r) { rb++; add(a[rb], f1[rb], st1, sz1); add(a[rb] - 1, f2[rb], st2, sz2); res = max(res, get1(a[rb], f1[rb], st2, sz2)); res = max(res, get2(a[rb] - 1, f2[rb], st1, sz1)); } int curres; if (r >= lb) curres = res; else curres = 0; int curl = lb; while (curl > l) { curl--; if (curl <= r) { add(a[curl], f1[curl], tmpst1, tmpsz1); add(a[curl] - 1, f2[curl], tmpst2, tmpsz2); if (lb <= r) curres = max(curres, get1(a[curl], f1[curl], st2, sz2)); curres = max(curres, get1(a[curl], f1[curl], tmpst2, tmpsz2)); if (lb <= r) curres = max(curres, get2(a[curl] - 1, f2[curl], st1, sz1)); curres = max(curres, get2(a[curl] - 1, f2[curl], tmpst1, tmpsz1)); } } ans[q[j].i] = curres; } prev = i + 1; } for (int i = 0; i < m; i++) printf("%d\n", ans[i]); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int A[n]; for (int i = 0; i < n; i++) { cin >> A[i]; } int s[10000] = {0}; sort(A, A + n); int mx = A[n - 1]; s[mx] = 1; A[n - 1] = 0; for (int i = 0; i < n - 1; i++) { if ((s[A[i]] == 0) && (mx % A[i] == 0)) { s[A[i]] = 1; A[i] = 0; } } sort(A, A + n); cout << mx << " " << A[n - 1] << "\n"; }
8
CPP
#include <bits/stdc++.h> using namespace std; long long t, n, m, a1, a2, b1, b2, u1, u2, v1, v2; long long num(long long x, long long y, long long a, long long b, long long col) { if (x > a || y > b) return 0; long long ans = (a - x + 1) * (b - y + 1); if ((x + y) % 2 == col) ans++; ans /= 2; return ans; } int main() { scanf("%lld", &t); while (t-- > 0) { long long ans = 0; scanf("%lld%lld", &m, &n); ans += num(1, 1, n, m, 0); scanf("%lld%lld%lld%lld", &a1, &a2, &b1, &b2); ans += num(a1, a2, b1, b2, 1); scanf("%lld%lld%lld%lld", &u1, &u2, &v1, &v2); ans -= num(u1, u2, v1, v2, 0); ans -= num(max(a1, u1), max(a2, u2), min(b1, v1), min(b2, v2), 1); printf("%lld %lld\n", ans, n * m - ans); } return 0; }
9
CPP
from sys import stdin,stdout nmbr = lambda: int(input()) lst = lambda: list(map(int, input().split())) PI=float('inf') for _ in range(1):#nmbr()): n=nmbr() a=lst() ones=[i for i in range(n) if a[i]==1] zeros=[i for i in range(n) if a[i]!=1] na=len(ones) nb=len(zeros) dp=[[PI for i in range(nb+1)] for j in range(na+1)] for i in range(nb+1): dp[0][i]=0 for i in range(1,na+1): for j in range(1,nb+1): dp[i][j]=min(dp[i][j-1], dp[i-1][j-1] + abs(ones[i-1]-zeros[j-1])) print(dp[na][nb])
10
PYTHON3
import sys # infilename = 'input.txt' # sys.stdin = open(infilename, 'r') # outfilename = 'output.txt' # sys.stdout = open(outfilename, 'w') def main(): n = int(input()) for _ in range(n): a, b, x, y = map(int, input().split()) xp = max(a-x-1, x) yp = max(b-y-1, y) print(max(xp*b, yp*a)) if __name__ == '__main__': main()
7
PYTHON3
#include <bits/stdc++.h> using namespace std; long long MOD = 1e9 + 7; const long double E = 1e-12; unsigned char ccc; bool _minus = false; template <typename T> inline T sqr(T t) { return (t * t); } inline void read(long long &n) { n = 0; _minus = false; while (true) { ccc = getchar(); if (ccc == ' ' || ccc == '\n') break; if (ccc == '-') { _minus = true; continue; } n = n * 10 + ccc - '0'; } if (_minus) n *= -1; } inline void read(int &n) { n = 0; _minus = false; while (true) { ccc = getchar(); if (ccc == ' ' || ccc == '\n') break; if (ccc == '-') { _minus = true; continue; } n = n * 10 + ccc - '0'; } if (_minus) n *= -1; } char wwww[19]; int kkkk; inline void write(long long y) { long long x = y; kkkk = 0; if (!x) ++kkkk, wwww[kkkk] = '0'; else while (x) { ++kkkk; wwww[kkkk] = char(x % 10 + '0'); x /= 10; } for (int i = kkkk; i >= 1; --i) putchar(wwww[i]); putchar(' '); } const int MAX = 1e5 + 10; int ar[MAX]; int t1[MAX << 2]; void build1(int v, int l, int r) { if (l == r) { t1[v] = ar[l]; return; } int x = (l + r) >> 1; build1(v << 1, l, x); build1(v << 1 | 1, x + 1, r); t1[v] = min(t1[v << 1], t1[v << 1 | 1]); } int n, k; struct segment_tree { private: unordered_map<int, int> t, y; inline void push(int v) { if (y[v]) { t[v << 1] = t[v << 1 | 1] = y[v << 1] = y[v << 1 | 1] = y[v]; y[v] = 0; } } int upd(int v, int tl, int tr, int l, int r, int val) { if (l <= tl && tr <= r) { t[v] = val; y[v] = val; return val; } if (tr < l || r < tl) return (t[v] == 0 ? t1[v] : t[v]); push(v); int x = (tl + tr) >> 1; return t[v] = min(upd(v << 1, tl, x, l, r, val), upd(v << 1 | 1, x + 1, tr, l, r, val)); } int get(int v, int tl, int tr, int l, int r) { if (l <= tl && tr <= r) { return (t[v] == 0 ? t1[v] : t[v]); } if (tr < l || r < tl) return INT_MAX; push(v); int x = (tl + tr) >> 1; return min(get(v << 1, tl, x, l, r), get(v << 1 | 1, x + 1, tr, l, r)); } public: void upd(int l, int r, int val) { upd(1, 1, n, l, r, val); } int get(int l, int r) { return get(1, 1, n, l, r); } } st[MAX]; int t2[MAX], y2[MAX]; void build2(int v, int l, int r) { if (l == r) { t2[v] = st[l].get(1, n); return; } int x = (l + r) >> 1; build2(v << 1, l, x); build2(v << 1 | 1, x + 1, r); t2[v] = min(t2[v << 1], t2[v << 1 | 1]); } inline void push(int v) { if (y2[v]) { t2[v << 1] = t2[v << 1 | 1] = y2[v << 1] = y2[v << 1 | 1] = y2[v]; y2[v] = 0; } } void upd(int v, int tl, int tr, int l, int r, int val) { if (l <= tl && tr <= r) { t2[v] = y2[v] = val; return; } if (tr < l || r < tl) return; push(v); int x = (tl + tr) >> 1; upd(v << 1, tl, x, l, r, val); upd(v << 1 | 1, x + 1, tr, l, r, val); t2[v] = min(t2[v << 1], t2[v << 1 | 1]); } int get(int v, int tl, int tr, int l, int r) { if (l <= tl && tr <= r) { return t2[v]; } if (tr < l || r < tl) return INT_MAX; push(v); int x = (tl + tr) >> 1; return min(get(v << 1, tl, x, l, r), get(v << 1 | 1, x + 1, tr, l, r)); } void upd2(int v, int tl, int tr, int pos, int l, int r, int val) { if (tl == tr) { if (y2[v]) st[tl].upd(1, n, y2[v]); y2[v] = 0; st[tl].upd(l, r, val); t2[v] = st[tl].get(1, n); return; } int x = (tl + tr) >> 1; push(v); if (pos <= x) upd2(v << 1, tl, x, pos, l, r, val); else upd2(v << 1 | 1, x + 1, tr, pos, l, r, val); t2[v] = min(t2[v << 1], t2[v << 1 | 1]); } int get2(int v, int tl, int tr, int pos, int l, int r) { if (tl == tr) { if (y2[v]) st[tl].upd(1, n, y2[v]); y2[v] = 0; return st[tl].get(l, r); } int x = (tl + tr) >> 1; push(v); return (pos <= x ? get2(v << 1, tl, x, pos, l, r) : get2(v << 1 | 1, x + 1, tr, pos, l, r)); } inline int get_block(long long x) { return (x - 1) / n + 1; } inline int get_pos_in_block(long long x) { return x - (get_block(x) - 1) * n; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; srand(time(NULL)); cout.precision(12); cout << fixed; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> ar[i]; } build1(1, 1, n); build2(1, 1, k); int q; cin >> q; while (q--) { int t; long long l, r; cin >> t >> l >> r; int l1 = get_block(l); int r1 = get_block(r); int L1 = get_pos_in_block(l); int R1 = get_pos_in_block(r); if (t == 1) { int x; cin >> x; if (l1 == r1) { upd2(1, 1, k, l1, L1, R1, x); } else { upd2(1, 1, k, l1, L1, n, x); upd2(1, 1, k, r1, 1, R1, x); if (l1 + 1 < r1) { upd(1, 1, k, l1 + 1, r1 - 1, x); } } } else { int ans = INT_MAX; if (l1 == r1) { ans = get2(1, 1, k, l1, L1, R1); } else { ans = min(ans, get2(1, 1, k, l1, L1, n)); ans = min(ans, get2(1, 1, k, r1, 1, R1)); if (l1 + 1 < r1) { ans = min(ans, get(1, 1, k, l1 + 1, r1 - 1)); } } cout << ans << "\n"; } } }
13
CPP
def getC(n, k): r = 1 for i in range(n, n - k, -1): r *= i for i in range(1, k + 1): r //= i return r n = int(input()) print(getC(n, 5) + getC(n, 6) + getC(n, 7))
12
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; int n; bool is_prime[MAXN]; vector<int> primes; void sieve(int n) { fill(is_prime + 2, is_prime + n + 1, true); for (int i = 2; i <= n; i++) { if (is_prime[i]) primes.push_back(i); for (auto p : primes) { if (p * i > n) break; is_prime[p * i] = false; if (i % p == 0) break; } } } bool vis[MAXN]; int main() { ios::sync_with_stdio(false); cin >> n; if (n == 1) { cout << "C 1" << endl; cout.flush(); return 0; } sieve(n); int left = n, ans = 1, sqrt_n = sqrt(n); bool min_prim_flag = false; for (int i = 0; i < primes.size(); i++) { int p = primes[i]; cout << "B " << p << endl; cout.flush(); int mul_cnt = 0, real_mul_cnt; cin >> real_mul_cnt; for (int i = p; i <= n; i += p) { if (!vis[i]) { vis[i] = true; left--; mul_cnt++; } } if (mul_cnt != real_mul_cnt) { int res; for (long long j = p; j <= n; j *= p) { cout << "A " << j << endl; cout.flush(); cin >> res; if (res != 1) break; ans *= p; } } if (((i + 1) % sqrt_n == 0 || i == primes.size() - 1) && !min_prim_flag) { cout << "A 1" << endl; cout.flush(); int res; cin >> res; if (res != left) { for (int j = i - sqrt_n + 1; j <= i; j++) { cout << "A " << primes[j] << endl; cout.flush(); cin >> res; if (res == 1) { for (long long k = primes[j]; k <= n; k *= primes[j]) { cout << "A " << k << endl; cout.flush(); cin >> res; if (res != 1) break; ans *= primes[j]; } min_prim_flag = true; break; } } } } } cout << "C " << ans << endl; cout.flush(); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long INF = 0x3f3f3f3f3f3f3f3f; char buf[1 << 21], *p1 = buf, *p2 = buf; inline int gc() { return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++; } inline int read() { int ret = 0, f = 0; char c = gc(); while (!isdigit(c)) { if (c == '-') f = 1; c = gc(); } while (isdigit(c)) { ret = ret * 10 + c - 48; c = gc(); } if (f) return -ret; return ret; } int vis[30]; int c[30]; inline int lowbit(int x) { return x & (-x); } inline int query(int x) { int sum = 0; for (; x; x -= lowbit(x)) { sum += c[x]; } return sum; } inline void add(int x) { for (; x <= 26; x += lowbit(x)) { c[x]++; } } int main() { int T; cin >> T; while (T--) { int n; cin >> n; string a, b; cin >> a >> b; string s1 = a, s2 = b; sort((s1).begin(), (s1).end()); sort((s2).begin(), (s2).end()); if (s1 != s2) { cout << "NO" << endl; continue; } memset(vis, 0, sizeof(vis)); bool flag = false; for (auto x : a) { vis[x - 'a']++; if (vis[x - 'a'] > 1) { flag = true; break; } } if (flag) { cout << "YES" << endl; continue; } int sum1 = 0, sum2 = 0; reverse((a).begin(), (a).end()); reverse((b).begin(), (b).end()); memset(c, 0, sizeof(c)); for (auto x : a) { sum1 += query(x - 'a' + 1); add(x - 'a' + 1); } memset(c, 0, sizeof(c)); for (auto y : b) { sum2 += query(y - 'a' + 1); add(y - 'a' + 1); } if ((sum1 ^ sum2) & 1) { cout << "NO" << endl; } else { cout << "YES" << endl; } } return 0; }
12
CPP
t= int(input()) cnt = 0 for _ in range(t): n, k = [int(x) for x in input().split()] if k == 0: continue if k-n>=2: cnt+=1 else: continue print(cnt)
7
PYTHON3
from collections import defaultdict def solve(): dp = defaultdict(int) n = int(input()) l = list(map(int,input().split())) l.sort() le = [i for i in l if i%2] ce = len(le) co = n-ce if ce%2==0 and co%2==0: print("YES") return else: for i in range(0,n-1): if l[i+1]-l[i]==1: print("YES") return print("NO") return for _ in range(int(input())): solve()
9
PYTHON3
g = int(input()) j = list(map(int, input().split())) if sum(j): print('HARD') else: print('EASY')
7
PYTHON3
#include <bits/stdc++.h> using namespace std; long long read() { long long a = 0, fh = 1; char c = getchar(); while (c > '9' || c < '0') { if (c == '-') fh = -1; c = getchar(); } while ('0' <= c && c <= '9') { a = a * 10 + c - 48; c = getchar(); } return a * fh; } long long n, k, vis[200005], ans; long long TOT = 0; void chk(vector<long long>& p, long long pos) { for (long long j = 0; j < p.size(); ++j) { long long i = p[j]; cout << "? " << i << endl; TOT++; char c; cin >> c; if (c == 'Y' && j >= pos) vis[i] = 1; } } void solve() { long long cnt = 0; for (long long len = k; len <= n; len += k) { vector<long long> p(0); for (long long i = 1; i <= len; i += k) { long long j = i + len; if (j + k - 1 > n) continue; vector<long long> p(0); for (long long j = i; j < i + k; ++j) p.push_back(j); chk(p, 0); for (long long j = i + len; j + k - 1 <= n; j += len) { p.clear(); cnt++; for (long long l = j; l < j + k; ++l) p.push_back(l); chk(p, 0); } cout << "R" << endl; } } } signed main() { ans = n = read(); k = read(); k = (k + 1) >> 1; solve(); for (long long i = 1; i <= n; ++i) if (vis[i]) ans--; cout << "! " << ans << endl; return 0; }
10
CPP
t = input() for _ in range(int(t)): n,x,m = map(int,input().split()) left = x right = x for _ in range(m): l,r = map(int,input().split()) if l>right or r<left: continue else: left = min(l,left) right = max(r,right) print(right-left+1)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; priority_queue<pair<int, int> > q; const int N = 100010; const int M = 200010 << 1; int st[N], e[N], a[N]; int b[M], nex[M], uid[M]; int tot = 0; void add(int x, int y, int z) { b[++tot] = y; uid[tot] = z; e[x]++; nex[tot] = st[x]; st[x] = tot; } int n, m; vector<int> ans; void ex() { if ((int)ans.size() != m) { puts("DEAD"); exit(0); } puts("ALIVE"); for (int i = (int)ans.size() - 1; i >= 0; --i) printf("%d%c", ans[i], " \n"[i == 0]); exit(0); } bool vis[N]; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", a + i); for (int i = 1; i <= m; ++i) { int u, v; scanf("%d%d", &u, &v); add(u, v, i); add(v, u, i); } for (int i = 1; i <= n; ++i) q.push({a[i] - e[i], i}); while (!q.empty()) { pair<int, int> top = q.top(); q.pop(); if (vis[top.second]) continue; if (top.first < 0) ex(); int x = top.second; if (0) printf("deal node %d\n", x); for (int i = st[x]; i; i = nex[i]) { int y = b[i]; if (vis[y]) continue; e[x]--; e[y]--; ans.push_back(uid[i]); if (0) printf("append person %d to ans\n", uid[i]); } for (int i = st[x]; i; i = nex[i]) { int y = b[i]; if (vis[y]) continue; q.push({a[y] - e[y], y}); if (0) printf("push node %d to queue\n", y); } vis[x] = true; } ex(); return 0; }
11
CPP
import math inp=input("") tup=inp.split() k=int(tup[0]) #Number of people n=int(tup[1]) #Number of planes per person s=int(tup[2]) #Number of planes per sheet p=int(tup[3]) #Number of sheets per pack assert 1 <= k <= 10000 assert 1 <= n <= 10000 assert 1 <= s <= 10000 assert 1 <= p <= 10000 a=s b=p step=math.ceil(n/s) totsheets=step*k packs=math.ceil(totsheets/p) print(packs)
7
PYTHON3
s = str(input()) target = "heidi" i, j = 0, 0 while(True): if s[i] == target[j]: i += 1 j += 1 else: i += 1 if j == len(target): break elif i == len(s): break else: pass print("YES") if j == len(target) else print("NO")
13
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int N, K; cin >> N >> K; for (int i = 1; i <= N; ++i) { for (int j = 1; j <= N; ++j) { if (i == j) cout << K << " "; else cout << 0 << " "; } cout << endl; } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; void read(int &x) { char ch; bool ok; for (ok = 0, ch = getchar(); !isdigit(ch); ch = getchar()) if (ch == '-') ok = 1; for (x = 0; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; if (ok) x = -x; } const int maxn = 2e5 + 10; int cnt, pre[maxn * 20], nxt[maxn * 20], h[maxn * 10], val[maxn * 20], pos[maxn * 10]; int id, n, m, s; long long dis[maxn * 10]; bool vis[maxn * 10]; struct oo { int x; long long y; }; priority_queue<oo> q; bool operator<(oo a, oo b) { return a.y > b.y; } void add(int x, int y, int z) { pre[++cnt] = y, nxt[cnt] = h[x], h[x] = cnt, val[cnt] = z; } void build_in(int x, int l, int r) { int mid = (l + r) >> 1; id = max(id, x); if (l == r) return pos[l] = x, void(); build_in(x << 1, l, mid), build_in(x << 1 | 1, mid + 1, r); add(x, x << 1, 0), add(x, x << 1 | 1, 0); } void build_out(int x, int l, int r) { add(x, x + id, 0); int mid = (l + r) >> 1; if (x != 1) add(x + id, (x >> 1) + id, 0); if (l == r) return; build_out(x << 1, l, mid), build_out(x << 1 | 1, mid + 1, r); } void change_in(int x, int l, int r, int a, int b, int c, int d) { if (a <= l && b >= r) return add(c, x, d), void(); int mid = (l + r) >> 1; if (a <= mid) change_in(x << 1, l, mid, a, b, c, d); if (b > mid) change_in(x << 1 | 1, mid + 1, r, a, b, c, d); } void change_out(int x, int l, int r, int a, int b, int c, int d) { if (a <= l && b >= r) return add(x + id, c, d), void(); int mid = (l + r) >> 1; if (a <= mid) change_out(x << 1, l, mid, a, b, c, d); if (b > mid) change_out(x << 1 | 1, mid + 1, r, a, b, c, d); } void dijkstra(int s) { memset(dis, 63, sizeof dis); q.push((oo){s, dis[s] = 0}); while (!q.empty()) { int x = q.top().x; q.pop(); if (vis[x]) continue; vis[x] = 1; for (register int i = h[x]; i; i = nxt[i]) if (!vis[pre[i]] && dis[pre[i]] > dis[x] + val[i]) { dis[pre[i]] = dis[x] + val[i]; q.push((oo){pre[i], dis[pre[i]]}); } } } int main() { read(n), read(m), read(s); build_in(1, 1, n), build_out(1, 1, n); for (register int i = 1, t, x, y, u, v; i <= m; i++) { read(t); if (t == 1) { read(y), read(x), read(v); add(pos[y] + id, pos[x], v); } if (t == 2) { read(u), read(x), read(y), read(v); change_in(1, 1, n, x, y, pos[u] + id, v); } if (t == 3) { read(u), read(x), read(y), read(v); change_out(1, 1, n, x, y, pos[u], v); } } dijkstra(pos[s]); for (register int i = 1; i <= n; i++) printf("%lld ", dis[pos[i]] > 1e16 ? -1 : dis[pos[i]]); }
10
CPP
#!/usr/bin/env python3 NUM_BUG_PENN = 5 NUM_FEAT_PENN = 3 def solve(n): ''' *n* is the number of tables in the company ''' return comb(n + NUM_BUG_PENN - 1, NUM_BUG_PENN) * \ comb(n + NUM_FEAT_PENN - 1, NUM_FEAT_PENN) def comb(n, k): ''' ''' return factorial(n) // (factorial(n - k) * factorial(k)) def factorial(n): ''' ''' prod = 1; for i in range(2, n + 1): prod *= i return prod if __name__ == "__main__": print(solve(int(input())))
13
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const long long int mod = 1000000007; const long long int MAX = 100008; vector<long long int> v[200005]; long long int in[200005]; long long int out[200005]; long long int value[200005]; long long int level[200005]; int vis[200005]; long long int timer = 0; struct node { long long int odd, even; }; void dfs(long long int node) { vis[node] = 1; timer++; in[node] = timer; for (int i = 0; i < v[node].size(); i++) { if (vis[v[node][i]] == 0) { level[v[node][i]] = level[node] + 1; dfs(v[node][i]); } } out[node] = timer; } struct node lazy[900009]; void initialize() { for (int i = 0; i < 900009; i++) { lazy[i].odd = 0; lazy[i].even = 0; } } long long int ans = 0; void query(long long int start, long long int end, long long int node, long long int idx, long long int lev) { long long int mid = (start + end) >> 1; if (lazy[node].even != 0 && lazy[node].odd != 0 && start != end) { lazy[2 * node].even += lazy[node].even; lazy[2 * node + 1].even += lazy[node].even; lazy[2 * node].odd += lazy[node].odd; lazy[2 * node + 1].odd += lazy[node].odd; lazy[node].even = 0; lazy[node].odd = 0; } if (start > idx || idx > end) { return; } else if (start == end && idx == end) { if (lev & 1) ans += lazy[node].odd; else ans += lazy[node].even; } else { query(start, mid, 2 * node, idx, lev); query(mid + 1, end, (2 * node) + 1, idx, lev); } } void update(long long int start, long long int end, long long int node, long long int l, long long int r, long long int val, long long int lev) { long long int mid = (start + end) >> 1; if (lazy[node].even != 0 && lazy[node].odd != 0 && start != end) { lazy[2 * node].even += lazy[node].even; lazy[2 * node + 1].even += lazy[node].even; lazy[2 * node].odd += lazy[node].odd; lazy[2 * node + 1].odd += lazy[node].odd; lazy[node].even = 0; lazy[node].odd = 0; } if (start > r || l > end) { return; } else if (l <= start && r >= end) { if (lev % 2 == 0) { lazy[node].even += val; lazy[node].odd -= val; } else { lazy[node].even -= val; lazy[node].odd += val; } return; } else { update(start, mid, 2 * node, l, r, val, lev); update(mid + 1, end, (2 * node) + 1, l, r, val, lev); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int n, q; cin >> n >> q; for (int i = 1; i <= n; i++) cin >> value[i]; for (int i = 1; i <= n - 1; i++) { long long int a, b; cin >> a >> b; v[a].push_back(b); v[b].push_back(a); } level[1] = 1; dfs(1); initialize(); while (q--) { int ch; cin >> ch; if (ch == 1) { long long int node, x; cin >> node >> x; long long int idx = in[node]; update(1LL, n, 1LL, in[node], out[node], x, level[node]); } else { long long int node; cin >> node; ans = value[node]; query(1, n, 1, in[node], level[node]); cout << ans << '\n'; } } }
11
CPP
import os import heapq import sys,threading import math import bisect import operator from collections import defaultdict sys.setrecursionlimit(10**5) from io import BytesIO, IOBase def gcd(a,b): if b==0: return a else: return gcd(b,a%b) def power(x, p,m): res = 1 while p: if p & 1: res = (res * x) % m x = (x * x) % m p >>= 1 return res def inar(): return [int(k) for k in input().split()] def lcm(num1,num2): return (num1*num2)//gcd(num1,num2) def main(): #t=int(input()) t=1 for _ in range(t): # n=int(input()) # n,k=inar() # arr=inar() n,m,x,y=inar() res=[] i=x count1=0 take=y ok=0 while i<=n and count1<n: count2=0 if ok==0: ok=1 j=y else: j=take while j<=m and count2<m: #print(i,j) res.append([i,j]) if j==m: j=1 count2+=1 else: count2+=1 j+=1 if j==1: take=m else: take=j-1 if i==n: count1+=1 i=1 else: i+=1 count1+=1 for i in range(len(res)): print(*res[i]) 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") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") if __name__ == "__main__": main() #threadin.Thread(target=main).start()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; double const pi = 3.1415926536; string f(string s) { string b; for (int i = 0; i < s.length(); i++) { if ((s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= 'a' && s[i] <= 'z')) b += s[i]; } for (int i = 0; i < b.length(); i++) { if (b[i] >= 'A' && b[i] <= 'Z') b[i] += 32; } return (b); } int main() { string a, b, c; cin >> a >> b >> c; int n; cin >> n; string x, y, z; x = f(a); y = f(b); z = f(c); string v1, v2, v3, v4, v5, v6; v1 = x + y + z; v2 = x + z + y; v3 = y + x + z; v4 = y + z + x; v5 = z + y + x; v6 = z + x + y; while (n--) { string u, t; cin >> u; t = f(u); if (t == v1 || t == v2 || t == v3 || t == v4 || t == v5 || t == v6) cout << "ACC\n"; else cout << "WA\n"; } }
8
CPP
#include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <cmath> #include <stack> #include <vector> #include <map> #include <set> #include <queue> #include <list> #include <iostream> #include <sstream> #include <climits> #include <cfloat> #include <complex> typedef long long ll; const double Eps = 1e-6; using namespace std; int t[256][256]; int main() { int n; while (scanf("%d", &n), n) { for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) scanf("%1d", &t[i][j]); int maxLen = 0; for (int i = 0; i < n; ++i) { int v, h; v = h = 0; for (int j = 0; j < n; ++j) { if (t[i][j]) maxLen = max(maxLen, ++v); else v = 0; if (t[j][i]) maxLen = max(maxLen, ++h); else h = 0; } int a, b, c, d; a = b = c = d = 0; for (int j = 0; i+j < n; ++j) { if (t[j][i+j]) maxLen = max(maxLen, ++a); else a = 0; if (t[i+j][j]) maxLen = max(maxLen, ++b); else b = 0; if (t[j][n-1-i-j]) maxLen = max(maxLen, ++c); else c = 0; if (t[i+j][n-1-j]) maxLen = max(maxLen, ++d); else d = 0; } } printf("%d\n", maxLen); } return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100005; const int M = 1000005; int n, m, q, pre[N], mcnt; struct edge { int x, w, next; } e[N]; long long d[N]; int g[N]; bool v[N]; queue<int> que[N]; int main() { scanf("%d%d%d", &n, &m, &q); memset(pre, -1, sizeof(pre)); for (int i = 0; i < m; ++i) { int x, y, z; scanf("%d%d%d", &x, &y, &z); e[mcnt] = (edge){y, z, pre[x]}, pre[x] = mcnt++; } priority_queue<pair<long long, int> > Q; memset(d, 0x3f, sizeof(d)); d[1] = 0; Q.push({-d[1], 1}); while (!Q.empty()) { auto it = Q.top(); Q.pop(); int x = it.second; if (-it.first > d[x]) continue; for (int i = pre[x]; ~i; i = e[i].next) { int y = e[i].x; if (d[x] + e[i].w < d[y]) { d[y] = d[x] + e[i].w; Q.push({-d[y], y}); } } } while (q--) { int o, k; scanf("%d%d", &o, &k); if (o == 1) { if (d[k] >= d[0]) { puts("-1"); } else { printf("%lld\n", d[k]); } } else { int w; for (int i = 0; i < k; ++i) { scanf("%d", &w); e[w - 1].w++; } memset(g, 0x3f, sizeof(g)); g[1] = 0; que[0].push(1); int maxDis = 0; for (int dis = 0; dis <= maxDis; ++dis) { while (!que[dis].empty()) { int x = que[dis].front(); que[dis].pop(); if (dis > g[x]) continue; for (int i = pre[x]; ~i; i = e[i].next) { int y = e[i].x, z = d[x] + e[i].w - d[y]; if (g[x] + z < g[y]) { g[y] = g[x] + z; if (g[y] <= min(k, n - 1)) { maxDis = max(maxDis, g[y]); que[g[y]].push(y); } } } } } for (int i = 1; i <= n; ++i) { d[i] = min(d[0], d[i] + g[i]); } } } }
10
CPP