solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
import string from collections import defaultdict,Counter from math import sqrt, log10, log2, log, gcd, ceil, floor,factorial from bisect import bisect_left, bisect_right from itertools import permutations,combinations_with_replacement import sys,io,os; input=sys.stdin.readline # input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline # print=sys.stdout.write sys.setrecursionlimit(10000) mod=int(pow(10,7)+9) inf = float('inf') def get_list(): return [int(i) for i in input().split()] def yn(a): print("YES" if a else "NO") n=20000 l=[[] for i in range(n+1)] for i in range(1,n+1): for j in range(i,n+1,i): l[i].append(j) t = 1 t=int(input()) def update(a,b,c,i,j,k): return abs(i-a)+abs(j-b)+abs(k-c) for _ in range(t): a,b,c=get_list() if c%b==0 and b%a==0: print(0) print(a,b,c) continue mina=inf ans=[a,b,c] for iter1 in range(len(l)): i = iter1; if i-a>mina: break; if a-i>mina: continue for iter2 in range(len(l[iter1])): j=l[iter1][iter2] if j-b>mina: break; k=((c)//j)*j; if k<j: k=j; version=update(a,b,c,i,j,k) if version<mina: mina=version answer=[i,j,k] k+=j; version = update(a,b,c,i,j,k) if version < mina: mina = version answer = [i, j, k] print(mina) print(*answer)
10
PYTHON3
#include <bits/stdc++.h> using namespace std; template <class T> inline T sqr(T x) { return x * x; } template <class T> inline bool isSquare(T x) { T y = sqrt(x + 0.5); return (y * y) == x; } template <class T1, class T2> inline T1 gcd(T1 a, T2 b) { return b ? gcd(b, a % b) : a; } template <class T1, class T2> inline T1 eqMin(T1& x, const T2& y) { if (T1(y) < x) return x = y; return x; } template <class T1, class T2> inline T1 eqMax(T1& x, const T2& y) { if (T1(y) > x) return x = y; return x; } template <class T1, class T2> inline T1 min(const T1& x, const T2& y) { return x < (T1)y ? x : (T1)y; } template <class T1, class T2> inline T1 max(T1& x, const T2& y) { return x > (T1)y ? x : (T1)y; } template <typename T> inline T getint() { T x = 0, p = 1; char ch; do { ch = getchar(); } while (ch <= ' '); if (ch == '-') p = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * p; } struct _flag_t { string val; } const _1d{", "}, _2d{"\n "}; _flag_t _flag = _1d; ostream& operator<<(ostream& os, _flag_t flag) { _flag = flag; return os; } template <class CharT, class Traits, class It> basic_ostream<CharT, Traits>& _out(basic_ostream<CharT, Traits>& os, It f, It l) { if (f == l) return os << "{}"; _flag_t cur_flag = _flag; os << _1d << "{ " << *f; for (; ++f != l; os << cur_flag.val << *f) ; return os << " }"; } template <class CharT, class Traits, class C> auto operator<<(basic_ostream<CharT, Traits>& os, C const& cont) -> decltype(begin(cont), end(cont), cont.size(), os) { return _out(os, begin(cont), end(cont)); } template <class X, class Y> ostream& operator<<(ostream& os, pair<X, Y> const& p) { return os << "[" << p.first << ", " << p.second << "]"; } const double PI = acos(-1); const double EPS = 1e-8; const int INF = (int)2e9; const int MOD = (int)1e9 + 7; const int MAXN = (int)1e5 + 10; int n; vector<int> g[MAXN]; int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 1; i < n; i++) { int x, y; cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } for (int i = 1; i <= n; i++) { if (g[i].size() == 2) { return cout << "NO", 0; } } cout << "YES"; }
7
CPP
n=int(input()) k=0 for i in range(n): list=input().split() sum=int(list[0])+int(list[1])+int(list[2]) if(sum>=2): k+=1 print(k)
7
PYTHON3
n = int(input()) for _ in range(n): s = sum(map(int, input().split())) print(s//2)
7
PYTHON3
s=(input()) c=0 while len(s)!=1: n=0 for i in s: n=n+int(i) s=str(n) c=c+1 print(c)
8
PYTHON3
#include "bits/stdc++.h" using namespace std; const int N_ = 200100; vector<int> V[N_], G[N_], E[N_]; int N, Co[N_], Res[N_]; void Haku(int s, int e) { Co[s] = !Co[e]; G[Co[s]].push_back(s); for (auto &u : V[s]) if (u != e) Haku(u, s); } int main() { int i, p = 0; scanf("%d", &N); for (i = 1; i <= N; ++i) E[i % 3].push_back(i); for (i = 1; i + 1 <= N; ++i) { int a, b; scanf("%d%d", &a, &b); V[a].push_back(b); V[b].push_back(a); } Haku(1, 1); if (G[1].size() < G[0].size()) p = !p; if (G[p].size() <= E[0].size()) { for (i = 1; i <= N; ++i) if (Co[i] == p) { Res[i] = E[0].back(); E[0].pop_back(); } for (i = 1; i <= N && E[1].size(); ++i) if (!Res[i]) { Res[i] = E[1].back(); E[1].pop_back(); } for (i = 1; i <= N && E[2].size(); ++i) if (!Res[i]) { Res[i] = E[2].back(); E[2].pop_back(); } } else { for (i = 1; i <= N && E[1].size(); ++i) if (Co[i]) { Res[i] = E[1].back(); E[1].pop_back(); } for (i = 1; i <= N && E[2].size(); ++i) if (!Co[i]) { Res[i] = E[2].back(); E[2].pop_back(); } } for (i = 1; i <= N; ++i) if (!Res[i]) { Res[i] = E[0].back(); E[0].pop_back(); } for (i = 1; i <= N; ++i) printf("%d ", Res[i]); puts(""); }
0
CPP
n=int(input()) ls=[int(x) for x in input().split()] l=[int(x) for x in input().split()] a=ls.count(1) b=l.count(1) d1=dict() d2=dict() c=0 for i in range(n): if ls[i]==1: d1[i]=1 if l[i]==1: d2[i]=1 for i in range(n): if i in d1 and i in d2: c+=1 res=a-c if res==0: print(-1) else: ans=(b-c)//res+1 print(ans)
7
PYTHON3
#include<bits/stdc++.h> using namespace std; int n,sum,cnt; bool used[100000]; int gcd(int x,int y){return y==0?x:gcd(y,x%y);} int main(){ scanf("%d",&n); int i; for (i=1;i<=30000&&cnt<n-1;++i) if (i%2==0||i%3==0){ ++cnt; printf("%d ",i); used[i]=1; sum+=i; } for (int i=1;i<=30000;++i) if (!used[i]&&(sum+i)%6==0&&gcd(sum,i)!=1){ ++cnt; printf("%d\n",i); break; } cerr<<cnt<<endl; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int ch[15100000][2], sum[15100000], cnt, root[65], n; long long b[301000], bin[65], c[301000]; void insert(int &, long long); long long query(int, long long); int main() { scanf("%d", &n); for (int i = 0; i <= 62; i++) bin[i] = 1ll << i; for (int i = 1; i <= n; i++) { scanf("%lld", &b[i]); for (int j = 60; j >= 0; j--) { if (b[i] & bin[j]) { insert(root[j], b[i]); break; } } } long long zz = 0; int t = 1; for (; t <= n; t++) { for (int i = 0; i <= 60; i++) { if ((!(zz & bin[i])) && sum[root[i]] != 0) { c[t] = query(root[i], zz); zz ^= c[t]; break; } } if (!c[t]) break; } if (t <= n) printf("No"); else { printf("Yes\n"); for (int i = 1; i <= n; i++) printf("%lld ", c[i]); } return 0; } void insert(int &rt, long long y) { if (!rt) rt = ++cnt; sum[rt]++; int now = rt; for (int i = 60; i >= 0; i--) { if (y & bin[i]) { if (!ch[now][1]) ch[now][1] = ++cnt; now = ch[now][1]; } else { if (!ch[now][0]) ch[now][0] = ++cnt; now = ch[now][0]; } sum[now]++; } } long long query(int x, long long y) { sum[x]--; int now = x; long long ans = 0; for (int i = 60; i >= 0; i--) { int d = ((y & bin[i]) != 0); if (sum[ch[now][d]]) { now = ch[now][d]; if (d) ans += bin[i]; } else { now = ch[now][d ^ 1]; if (!d) ans += bin[i]; } sum[now]--; } return ans; }
9
CPP
def findFirstGreaterOrEqual(nums, target): lo = int(0) hi = int(len(nums)-1) if target >= nums[hi]: return hi while (lo < hi): # print(nums[lo:hi+1]) mid = int((lo+hi)/2) if(target <= nums[mid]): hi = mid else: lo = mid +1 return lo n, k = map(int,input().split()) #print(n) #print(q) a = list(map(int,input().split())) q = list(map(int,input().split())) #print(q) # cumulative array for i in range (1, n): a[i] = a[i-1] + a[i] # create a copy of the array a_copy = a[:] #print(a_copy) #print(findFirstGreaterOrEqual(a_copy,8)) arrows = 0 for i in range (0,k): arrows = arrows + q[i] index = findFirstGreaterOrEqual(a_copy,arrows) if(a_copy[index] > arrows): print(len(a_copy) - index) elif(a_copy[index] <= arrows): if index == len(a_copy) - 1: print(len(a_copy)) arrows = 0 else: print(len(a_copy) - index -1)
9
PYTHON3
def unique(sentence): seen = set() return [x for x in sentence if not (x in seen or seen.add(x))] str1 = input() str2 = int(str1) + 1 #print("Value of str2 is : ", str2) while True: str3 = unique(str(str2)) if len(str3) == len(str1): print("".join(str3)) break else: #print("Value of str2 is : ", str2) str2 = str2 + 1
7
PYTHON3
for t in range (int(input())) : n,p,k=map(int,input().split()) a=list(map(int,input().split())) a.sort() sum1=[0,a[0]] for i in range (1,n) : sum1.append(sum1[-2]+a[i]) for i in range (n+1) : if sum1[i]<=p : ans=i print(ans)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, a[5005], h[5005], s[5005], b[5005]; void Nhap() { cin >> n; for (int i = 1; i <= n; i++) cin >> a[i], s[i] = s[i - 1] + a[i]; } void process() { h[1] = a[1]; int res = 0; for (int i = 2; i <= n; i++) { int j = i - 1; while (j > 0 && s[i] - s[j] < h[j]) j--; b[i] = i - j - 1 + b[j]; h[i] = s[i] - s[j]; } cout << b[n]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); Nhap(); process(); }
10
CPP
#include <bits/stdc++.h> using namespace std; int n; int k; int ans[300005]; string a; string b; int main() { int T; scanf("%d", &T); while (T--) { k = 0; int n; scanf("%d", &n); cin >> a >> b; int i = n - 1; ; while (i >= 0) { if (a[i] == b[i]) { i--; continue; } if (a[0] == b[i]) ans[k++] = 1; ans[k++] = i + 1; int ii = 0, jj = i; for (int l = 0; l <= jj; ++l) { if (a[l] == '1') a[l] = '0'; else a[l] = '1'; } while (ii < jj) swap(a[ii++], a[jj--]); i--; } printf("%d ", k); for (int i = 0; i < k; ++i) printf("%d ", ans[i]); putchar('\n'); } }
7
CPP
dice = input().split() roll = input() for s in roll: if s == "N": next = list("263415") elif s == "E": next = list("421653") elif s == "S": next = list("513462") else: next = list("326154") tmp = [0] * 6 for i, n in enumerate(next): tmp[i] = dice[int(n) - 1] dice = tmp print(dice[0])
0
PYTHON3
from collections import deque from sys import stdin, stdout import math input = stdin.readline #print = stdout.write for _ in range(int(input())): n, m = map(int, input().split()) arr = [list(input().strip()) for i in range(n)] good = [] bad = [] for i in range(n): for g in range(m): if arr[i][g] == 'G': good.append((i, g)) elif arr[i][g] == 'B': bad.append((i, g)) dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)] can = True for i in bad: for g in dirs: newx = i[0] + g[0] newy = i[1] + g[1] if not (0 <= newx < n and 0 <= newy < m): continue if arr[newx][newy] == 'G': can = False if arr[newx][newy] == '.': arr[newx][newy] = '#' visited = [[0 for g in range(m)] for i in range(n)] buff = deque() if arr[n - 1][m - 1] != '#': visited[n - 1][m - 1] = 1 buff = deque([(n - 1, m - 1)]) while len(buff): i = buff.popleft() for g in dirs: newx = i[0] + g[0] newy = i[1] + g[1] if not (0 <= newx < n and 0 <= newy < m) or visited[newx][newy] or arr[newx][newy] == '#': continue visited[newx][newy] = 1 buff.append((newx, newy)) for i in good: if not visited[i[0]][i[1]]: can = False for i in bad: if visited[i[0]][i[1]]: can = False print('Yes' if can else 'No')
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int st(int x, int pos) { return x = x | (1 << pos); } int Reset(int x, int pos) { return x = x & ~(1 << pos); } vector<long long> graph[300001]; long long dp[300001], ans[300001], val[300001]; void dfs(long long u, long long p) { dp[u] = val[u]; for (long long v : graph[u]) { if (v != p) { dfs(v, u); dp[u] += max(0LL, dp[v]); } } } void dfs1(long long u, long long p) { ans[u] = dp[u]; for (long long v : graph[u]) { if (v != p) { dp[u] -= max(0LL, dp[v]); dp[v] += max(0LL, dp[u]); dfs1(v, u); dp[v] -= max(0LL, dp[u]); dp[u] += max(0LL, dp[v]); } } } int main() { long long n; cin >> n; for (int i = 1; i <= n; i++) scanf("%lld", &val[i]); for (int i = 1; i <= n; i++) { if (val[i] == 0) { val[i] = -1; } } for (int i = 1; i <= n - 1; i++) { long long a, b; scanf("%lld%lld", &a, &b); graph[a].push_back(b); graph[b].push_back(a); } dfs(1, -1); dfs1(1, -1); for (int i = 1; i <= n; i++) cout << ans[i] << " "; printf("\n"); }
12
CPP
#include <bits/stdc++.h> class Cvor { public: int node, level, odd, even; Cvor(int n, int l, int o, int e) { node = n; level = l; odd = o; even = e; } }; using namespace std; vector<vector<int> > graf; int init[100000], goal[100000], bio[100000]; int n, u, v, odd, even, t; vector<int> sols; stack<Cvor*> stek; Cvor* p; int main(void) { scanf("%d", &n); vector<int> vi; graf.insert(graf.begin(), n, vi); for (int i = 0; i < n - 1; ++i) { scanf("%d %d", &u, &v); graf[u - 1].push_back(v - 1); graf[v - 1].push_back(u - 1); } for (int i = 0; i < n; ++i) scanf("%d", &init[i]); for (int i = 0; i < n; ++i) scanf("%d", &goal[i]); stek.push(new Cvor(0, 1, 0, 0)); while (!stek.empty()) { p = stek.top(); stek.pop(); if (bio[p->node]) continue; bio[p->node] = 1; if (p->level % 2 && p->odd % 2) init[p->node] = (init[p->node]) ? 0 : 1; if (p->level % 2 == 0 && p->even % 2) init[p->node] = (init[p->node]) ? 0 : 1; if (init[p->node] != goal[p->node]) { sols.push_back(p->node + 1); t = 1; } else t = 0; for (int i = 0; i < graf[p->node].size(); ++i) stek.push(new Cvor(graf[p->node][i], p->level + 1, p->odd + ((t && p->level % 2) ? 1 : 0), p->even + ((t && p->level % 2 == 0) ? 1 : 0))); delete p; } printf("%d\n", sols.size()); for (int i = 0; i < sols.size(); ++i) printf("%d\n", sols[i]); return 0; }
7
CPP
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define ri register int const int N=500005,M=1000000007; int n,u[N],v[N],i,q,a[N],g[3005][3005],tmp[3005][3005],j; long long inv,ans; long long qpow(long long a,long long b) { long long ans=1,s=a; while(b) { if(b&1) ans=ans*s%M; s=s*s%M; b>>=1; } return ans; } int main() { scanf("%d %d",&n,&q); for(i=1;i<=n;++i) scanf("%d",&a[i]); for(i=1;i<=q;++i) scanf("%d %d",&u[i],&v[i]); for(i=1;i<=n;++i) for(j=1;j<=n;++j) if(a[i]>a[j]) g[i][j]=1; inv=qpow(2,M-2); for(i=1;i<=q;++i) { for(j=1;j<=n;++j) if(j!=u[i]&&j!=v[i]) { tmp[u[i]][j]=tmp[v[i]][j]=(g[v[i]][j]+g[u[i]][j])*inv%M; tmp[j][u[i]]=tmp[j][v[i]]=(g[j][u[i]]+g[j][v[i]])*inv%M; } tmp[v[i]][u[i]]=tmp[u[i]][v[i]]=(g[u[i]][v[i]]+g[v[i]][u[i]])*inv%M; for(j=1;j<=n;++j) { g[u[i]][j]=tmp[u[i]][j]; g[v[i]][j]=tmp[v[i]][j]; g[j][u[i]]=tmp[j][u[i]]; g[j][v[i]]=tmp[j][v[i]]; } } for(i=1;i<=n;++i) for(j=i+1;j<=n;++j) ans+=g[i][j]; ans%=M; cout<<(ans*qpow(2,q)%M+M)%M; }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long t, n, i, j, k; cin >> t; while (t--) { cin >> n; long long a[n]; for (i = 0; i < n; i++) cin >> a[i]; long long b[n]; memset(b, 0, sizeof(b)); b[0] = 1; long long o = -1; long long cnt = 1; for (i = 1; i < n - 1; i++) { if (a[i - 1] == a[i]) { b[i] = b[i - 1]; o = i; } else { if (b[i - 1] == 1) b[i] = 2; else b[i] = 1; } cnt = max(cnt, b[i]); } if (a[n - 1] == a[n - 2] && a[n - 1] == a[0]) b[n - 1] = 1; else if (a[n - 1] != a[n - 2]) { if (a[n - 1] != a[0]) { if (b[n - 2] + b[0] == 3) { if (o == -1) b[n - 1] = 3; else { for (j = o; j < n - 1; j++) { if (b[j] == 1) b[j] = 2; else b[j] = 1; } if (b[n - 2] == 2) b[n - 1] = 1; else b[n - 1] = 2; } } else b[n - 1] = 2; } else { if (b[n - 2] == 1) b[n - 1] = 2; else b[n - 1] = 1; } } else b[n - 1] = 2; cnt = max(cnt, b[n - 1]); cout << cnt << endl; for (i = 0; i < n; i++) cout << b[i] << " "; cout << endl; } }
10
CPP
# import sys # sys.stdin=open('input.in','r') # sys.stdout=open('output.out','w') n,m=input().strip().split()[:2] n=int(n) m=int(m) x=1 s=0 while s+m<=240 and x<=n: s=s+x*5 if s+m>240: x=x-1 p=x x+=1 print(p)
7
PYTHON3
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=305,p=998244353; int read(){ int f=1,g=0; char ch=getchar(); for (;!isdigit(ch);ch=getchar()) if (ch=='-') f=-1; for (;isdigit(ch);ch=getchar()) g=g*10+ch-'0'; return f*g; } int qpow (int x,int k){ int t=1; for (;k;k>>=1){ if (k&1) t=(ll)t*x%p; x=(ll)x*x%p; } return t; } int n,ans,f[N][N][N],g[N][N][N]; char ch[N]; void check(int &x,int y){if((x+=y)>=p) x-=p;} void chkmax(int &x,int y){if (x<y) x=y;} int main(){ // freopen("a.in","r",stdin); scanf(" %s",ch+1); n=strlen(ch+1); if (n==1){puts("1");return 0;} memset(f,0xC0,sizeof(f)); f[0][0][0]=0; for (int i=0;i<n;i++) for (int j=0;j<=i;j++) for (int k=0;k<=i;k++){ chkmax(f[i+1][j][k],f[i][j][k]); if (j){ if (ch[i+1]=='0') chkmax(f[i+1][j-1][k+1],f[i][j][k]); else chkmax(f[i+1][j-1][k],f[i][j][k]+1); } if (i+2<=n){ if ((ch[i+1]=='0')||(ch[i+2]=='0')) chkmax(f[i+2][j][k+1],f[i][j][k]); if ((ch[i+1]=='1')||(ch[i+2]=='1')) chkmax(f[i+2][j][k],f[i][j][k]+1); chkmax(f[i+2][j+1][k],f[i][j][k]); } } for (int i=0;i<=n;i++) for (int j=0;j<=i;j++) for (int k=0;k<=i;k++) chkmax(f[i][0][k],f[i][j][k]); g[n+1][0][0]=1;ans=p-1; for (int i=n+1;i;i--) for (int j=0;j<i;j++) for (int k=0;j+k<i;k++){ if (f[i-1][0][j]>=k) check(ans,g[i][j][k]); if (i==1) continue; check(g[i-1][j][k],g[i][j][k]); if (ch[i-1]=='0') check(g[i][j][k+1],g[i][j][k]); else check(g[i][j+1][k],g[i][j][k]); } printf("%d\n",ans); return 0; }
0
CPP
#include <iostream> #include <string> #include <list> using namespace std; #define REP(i,n) for(int i = 0; i < (int)n; ++i) int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int n; string st; int x; cin >> n; list<int> L; list<int>::iterator it, endit; while(n--) { cin >> st; if(st == "insert") { cin >> x; L.push_front(x); } else if(st == "delete") { cin >> x; for(it = L.begin(), endit = L.end(); it != endit;++it) { if(*it == x) { L.erase(it); break; } } } else if(st == "deleteFirst") { L.pop_front(); } else { // deleteLast L.pop_back(); } } for(it = L.begin(), endit = --L.end(); it != endit; ++it) { cout << *it << ' '; } cout << *it << endl; return 0; }
0
CPP
d = {i: j for i, j in zip(range(4), map(int, input().split()))} print(sum(d[i - 1] for i in map(int, list(input()))))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int M, N, sr, sc, er, ec; int dr[4] = {1, 0, -1, 0}; int dc[4] = {0, 1, 0, -1}; string grid[555]; bool vis[555][555]; inline bool valid(int r, int c) { return r >= 0 && r < M && c >= 0 && c < N; } void solve() { cin >> M >> N; vector<vector<pair<int, int> > > p( M + 1, vector<pair<int, int> >(N + 1, make_pair(-1, -1))); for (int i = 0; i < M; i++) { cin >> grid[i]; } cin >> sr >> sc >> er >> ec; sr--, sc--, er--, ec--; if (sr == er && sc == ec) { int cnt = 0; for (int i = 0; i < 4; i++) if (valid(er + dr[i], ec + dc[i])) cnt += (grid[er + dr[i]][ec + dc[i]] != 'X' && grid[er + dr[i]][ec + dc[i]] != '*'); cout << (cnt > 0 ? "YES\n" : "NO\n"); return; } memset(vis, false, sizeof(vis)); vis[sr][sc] = true; queue<pair<int, int> > Q; Q.push({sr, sc}); while (!Q.empty()) { pair<int, int> top = Q.front(); Q.pop(); ; if (top.first == er && top.second == ec) break; for (int i = 0; i < 4; i++) { int nr = top.first + dr[i], nc = top.second + dc[i]; if (!valid(nr, nc) || vis[nr][nc]) continue; ; if (grid[nr][nc] == 'X' && (nr != er || nc != ec)) continue; p[nr][nc] = top; vis[nr][nc] = true; Q.push({nr, nc}); } }; ; bool ok = true; if (!vis[er][ec]) ok = false; else if (vis[er][ec] && grid[er][ec] != 'X') { pair<int, int> curr = make_pair(er, ec); while (curr.first != -1 && curr.second != -1) { grid[curr.first][curr.second] = '*'; ; ; curr = p[curr.first][curr.second]; ; } int cnt = 0; for (int i = 0; i < 4; i++) if (valid(er + dr[i], ec + dc[i])) cnt += (grid[er + dr[i]][ec + dc[i]] != 'X' && grid[er + dr[i]][ec + dc[i]] != '*'); ok = (cnt > 0); } cout << (ok ? "YES\n" : "NO\n"); } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); solve(); return 0; }
9
CPP
b=[int(b) for b in input().split()] list=[] for i in range(b[0]): c=[int(c) for c in input().split()] for j in range(1,len(c)): if c[j]<b[1]: list.append(i+1) break print(len(list)) for i in list: print(i,end=" ")
7
PYTHON3
days, kilos = map(int, input().split()) prices = list(map(int, input().split())) profit = 0 for i in range(days-1): profit = max(profit, prices[i] - prices[i+1] - kilos) print(profit)
7
PYTHON3
if __name__ == '__main__': n = int(input()) y = input() inlist = y.split(' ') for ii in range(n): inlist[ii] = int(inlist[ii]) n_n = max(inlist)+300000 qq = 1 print(n+1) print(str(1)+' '+str(n)+' '+str(n_n)) for i in range(n): if (n_n+inlist[i]) % (n_n+inlist[i] - qq) == 0: x = n_n+inlist[i]-qq-1 qq = qq+2 else: x = n_n + inlist[i] - qq qq = qq+1 print(str(2) + ' ' + str(i+1) + ' ' + str(x))
9
PYTHON3
#include <bits/stdc++.h> using namespace std; struct node { long long m, v; long long num; bool operator<(const node& other) const { if (m == other.m) return v < other.v; else return m < other.m; } }; long long v[100005]; long long m[100005]; vector<node> vn; vector<long long> ans; long long n, k, h; long long ok(double t) { long long now = 0; for (long long i = 1; i <= k; i++) { long long tr = 0; while (now < vn.size()) { if ((double)(vn[now].v) * t >= (double)(i * h)) { tr = 1; now++; break; } now++; } if (tr == 0) return 0; } return 1; } void solve(double t) { long long now = 0; for (long long i = 1; i <= k; i++) { long long tr = 0; while (now < vn.size()) { if ((double)(vn[now].v) * t >= (double)(i * h)) { ans.push_back(vn[now].num); tr = 1; now++; break; } now++; } } } int main() { memset(m, 0, sizeof(m)); memset(v, 0, sizeof(v)); cin >> n >> k >> h; for (long long i = 1; i <= n; i++) { scanf("%I64d", &m[i]); } for (long long i = 1; i <= n; i++) { scanf("%I64d", &v[i]); } for (long long i = 1; i <= n; i++) { node h; h.m = m[i]; h.v = v[i]; h.num = i; vn.push_back(h); } sort(vn.begin(), vn.end()); double left = 0; double right = 1000000008; double mid = (right + left) / (double)2.0; for (long long i = 1; i <= 100; i++) { if (ok(mid)) { right = mid; } else left = mid; mid = (right + left) / (double)2.0; } solve(right); for (long long i = 0; i < ans.size(); i++) { if (i != ans.size() - 1) printf("%d ", ans[i]); else printf("%d\n", ans[i]); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxlongint = 2147483647; const int M = 2000011; int last[M]; char st[M], Ans[M]; void getKMP(int n) { int j = 0; last[1] = 0; for (int i = 2; i <= n; i++) { while (j && st[j + 1] != st[i]) j = last[j]; if (st[j + 1] == st[i]) j++; else j = 0; last[i] = j; } } int main() { int n; scanf("%d", &n); int totlen = 0; for (int i = 1; i <= n; i++) { scanf("%s", st + 1); int l = strlen(st + 1); getKMP(l); int j = 0; for (int i = max(1, totlen - l + 1); i <= totlen; i++) { if (j == l) j = last[j]; while (st[j + 1] != Ans[i] && j) j = last[j]; if (st[j + 1] == Ans[i]) j++; } for (int i = j + 1; i <= l; i++) Ans[++totlen] = st[i]; } for (int i = 1; i <= totlen; i++) { putchar(Ans[i]); } fclose(stdin); fclose(stdout); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 5; int n; pair<int, int> a[N]; int val[N]; int ask(pair<int, int> a) { cout << "? " << a.first << ' ' << a.second << endl; int sum; cin >> sum; return sum; } void solve() { for (int i = (2); i <= (int)(n); i += (1)) { int sum = ask({i - 1, i}); a[i].first = -a[i - 1].first; a[i].second = sum - a[i - 1].second; } int sumfs = 0, sumse = 0; for (int i = (1); i <= (int)(n); i += (1)) { sumfs += a[i].first; sumse += a[i].second; } int total = ask({1, n}); val[1] = (total - sumse) / sumfs; for (int i = (2); i <= (int)(n); i += (1)) val[i] = a[i].first * val[1] + a[i].second; } int main() { cin >> n; a[1] = {1, 0}; if (n & 1) solve(); else { n--; solve(); n++; int lstsum = ask({n - 1, n}); val[n] = lstsum - val[n - 1]; } cout << "!"; for (int i = (1); i <= (int)(n); i += (1)) cout << " " << val[i]; cout << endl; return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; const ll INF = 4e18; const ll MOD = 1e9+7; int main(){ // ios::sync_with_stdio(false); // cin.tie(0); double a[3], x[3], y[3], S, R; for (int i = 0; i < 3; i++) { cin >> x[i] >> y[i]; } for (int i = 1; i < 3; i++) { x[i] -= x[0]; y[i] -= y[0]; } x[0] = 0; y[0] = 0; for (int i = 0; i < 3; i++) { a[i] = sqrt((x[i] - x[(i+1)%3]) * (x[i] - x[(i+1)%3]) + (y[i] - y[(i+1)%3]) * (y[i] - y[(i+1)%3])); } R = abs(x[1] * y[2] - x[2] * y[1]) / (a[0] + a[1] + a[2]); double l = 0, mx = max(a[0], max(a[1], a[2])), r = mx; for (int i = 0; i < 40; i++) { double mid = (l+r)/2; if (mid <= mx * R / (2 * R + mx)) { l = mid; } else { r = mid; } } cout << setprecision(20) << l << endl; return 0; }
0
CPP
ch=input() ch1="" ch=ch.lower() l1=["a", "o", "y", "e", "u", "i"] for i in ch : if i not in l1 : ch1=ch1+'.'+i print(ch1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t, n, k, x, a, b, c; string s, ans; cin >> t; while (t--) { cin >> a >> b >> c; if (c / a == b && c % a == 0) { cout << b - 1 << " -1" << endl; } else if (a >= c) { cout << "-1 " << b << endl; } else if (c / a >= b) { cout << "1 -1" << endl; } else { cout << "1 " << b << endl; } } }
7
CPP
#include <bits/stdc++.h> using namespace std; char a[20]; vector<int> v; int main() { int t, tc; int i, j, k, l; int res, u, w, p, n; cin >> tc; while (tc--) { scanf("%s", &a); res = 0; int flag = 0, fl = 0; for (i = 1; i <= 12; i++) { flag = 1; fl = 1; k = 12 / i; if (i * k == 12) { fl = 1; for (u = 0; u < i && fl; u++) { flag = 1; for (j = u; j < 12 && flag; j += i) { if (a[j] != 'X') { flag = 0; break; } } if (flag) fl = 0; } if (fl == 0) v.push_back(i); } } printf("%d", v.size()); for (i = v.size() - 1; i >= 0; i--) { k = 12 / v[i]; printf(" %dx%d", k, v[i]); } printf("\n"); v.clear(); } return 0; }
7
CPP
#include <iostream> #include <map> using namespace std; map<int,long long> sl; int order[4000]; int main() { int n; while(1) { cin>>n;if(!n)return 0; int count=0; int od=0; sl.clear(); for(int i=0;i<n;++i){ int sn; long long t,v; cin>>sn>>t>>v; map<int,long long>::iterator it=sl.find(sn); if(it==sl.end()){ order[od++]=sn; sl.insert(make_pair(sn,t*v)); } else it->second+=t*v; } for(int i=0;i<od;++i){ if(sl[order[i]]>=1000000){cout<<order[i]<<endl;++count;} } if(!count)cout<<"NA"<<endl; } }
0
CPP
#include <bits/stdc++.h> int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int32_t a, b; std::cin >> a >> b; std::vector<int32_t> ax; while (a != 0) { ax.push_back(a % 10); a /= 10; } std::vector<int32_t> bx; while (b != 0) { bx.push_back(b % 10); b /= 10; } const int32_t base = std::max(*std::max_element(ax.begin(), ax.end()), *std::max_element(bx.begin(), bx.end())) + 1; std::vector<int32_t> sum; int32_t carry = 0; for (size_t i = 0; i < std::max(ax.size(), bx.size()) || carry; ++i) { int32_t x = carry; if (i < ax.size()) { x += ax[i]; } if (i < bx.size()) { x += bx[i]; } sum.push_back(x % base); carry = x / base; } std::cout << sum.size() << '\n'; }
8
CPP
t = int(input()) for _ in range(t): n, s, t = [int(x) for x in input().split()] larger = max(s, t) overlap = s + t - n print(larger - overlap +1)
7
PYTHON3
c =0 a = [] while c < 3: b = input() l = b.split() a.append(l) c +=1 if (int(a[0][0]) + int(a[0][1]) + int(a[1][0])) % 2 == 0 : print(1,end='') else: print(0,end='') if (int(a[0][1]) + int(a[0][0]) + int(a[0][2]) + int(a[1][1])) % 2 == 0 : print(1,end='') else: print(0,end='') if (int(a[0][1]) + int(a[0][2]) + int(a[1][2])) % 2 == 0 : print(1) else: print(0) if (int(a[0][0]) + int(a[1][0]) + int(a[1][1]) + int(a[2][0])) % 2 == 0 : print(1,end='') else: print(0,end='') if (int(a[1][1]) + int(a[1][0]) + int(a[2][1]) + int(a[1][2]) + int(a[0][1])) % 2 == 0 : print(1,end='') else: print(0,end='') if (int(a[1][2]) + int(a[0][2]) + int(a[1][1]) + int(a[2][2])) % 2 == 0 : print(1) else: print(0) if (int(a[2][0]) + int(a[1][0]) + int(a[2][1])) % 2 == 0 : print(1,end='') else: print(0,end='') if (int(a[2][1]) + int(a[1][1]) + int(a[2][0]) + int(a[2][2])) % 2 == 0 : print(1,end='') else: print(0,end='') if (int(a[2][2]) + int(a[1][2]) + int(a[2][1])) % 2 == 0 : print(1) else: print(0)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 17 + 3; long long ans, dp[1 << N][N]; vector<int> adj[N]; bool e[N][N]; int n, m; void input() { cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; --u, --v; e[u][v] = e[v][u] = true; adj[u].push_back(v); adj[v].push_back(u); dp[(1 << u) | (1 << v)][max(u, v)] = 1; } } void make() { for (int i = 0; i < n; i++) dp[1 << i][i] = 1; for (int mask = 0; mask < (1 << n); mask++) { int s = __builtin_ctz(mask); for (int i = s + 1; i < n; i++) { if (mask >> i & 1) for (auto x : adj[i]) if (mask >> x & 1) dp[mask][i] += dp[mask ^ (1 << i)][x]; if (__builtin_popcount(mask) > 2 && mask >> i & 1 && e[s][i]) ans += dp[mask][i]; } } } int32_t main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); input(), make(); cout << ans / 4 << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; clock_t time_p = clock(); void time_taken() { time_p = clock() - time_p; cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n"; } const long long mod = 1e9 + 7; const long long INF = 1e18; int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); string s; cin >> s; int n = (int)(s).size(); bool ok = 1; for (int i = 2; i <= n - 1; i++) { ok &= (s[i] == 'A' + (s[i - 1] + s[i - 2] - 2 * 'A') % 26); } cout << (ok ? "YES" : "NO") << '\n'; time_taken(); return 0; }
12
CPP
#include <bits/stdc++.h> const double PI = acos(-1.0); const double e = exp(1.0); template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } using namespace std; int n; int ans[55]; int mp[55][55]; int flag[55]; int main() { int q; while (~scanf("%d", &n)) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) scanf("%d", &mp[i][j]); memset(ans, 0, sizeof(ans)); memset(flag, 0, sizeof(flag)); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) for (int k = 1; k <= n && k != j; k++) if (mp[i][j] == mp[i][k] && j != i && k != i) { ans[i] = mp[i][j]; flag[ans[i]] = 1; } int numm = 100; while (numm--) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { if (ans[i] != 0) { if (mp[i][j] != ans[i] && j != i) { if (!ans[j]) ans[j] = mp[i][j]; flag[ans[j]] = 1; } } else if (!ans[i]) { if (j != i && ans[j] && mp[i][j] != ans[j]) { if (!ans[i]) ans[i] = mp[i][j]; flag[ans[i]] = 1; } } } } for (int i = 1; i <= n; i++) { if (ans[i] > 0) { printf("%d ", ans[i]); } else { for (int j = 1; j <= n; j++) if (!flag[j]) { printf("%d ", j); flag[j] = 1; break; } } } printf("\n"); } return 0; }
8
CPP
n, l = map(int, input().split()) a = sorted(list(map(int, input().split()))) ans = 0 if len(a) == 1: ans = max([ans, a[0], l - a[0]]) else: for i in range(n): if i == 0: ans = max(ans, a[i]) else: gap = (a[i] - a[i - 1]) / 2 ans = max(ans, gap) if i == n - 1: ans = max(ans, l - a[i]) print(ans)
8
PYTHON3
n=int(input()) a=list(map(int,input().split())) c=0 for i in range(1,n-1): if(a[i-1]==1 and a[i+1]==1 and a[i]==0): c+=1 if(n>3): if(a[i-2]==0 and a[i-3]==1): c-=1 a[i-1]=0 print(c)
8
PYTHON3
s = input() string = "hello" cur = 0 for i in s: if i == string[cur]: cur += 1 if cur > 4: print("YES") break if cur <= 4: print("NO")
7
PYTHON3
t=int(input()) while t: t-=1 a,b,n=map(int,input().split()) c=max(a,b) i=min(a,b) a=c b=i c=i=0 while a<=n and b<=n: if a+b>n: c+=1 break if i%2==0: b+=a i+=1 else: a+=b i+=1 c+=1 print(c)
7
PYTHON3
#include<stdio.h> const int MAX_V = 10000; int prime[MAX_V+1]; int main() { int i, k, v; for(i = 2; i <= MAX_V; i++) { prime[i] = 1; } for(i = 2; i*i <= MAX_V; i++) { if(prime[i]) { for(k = 2 * i; k <= MAX_V; k += i) { prime[k] = 0; } } } while(scanf("%d", &v) != EOF) { if(v == 0){ break; } while(1){ if(prime[v] == 0 || prime[v - 2] == 0){ v--; }else{ printf("%d %d\n", v - 2, v); break; } } } return 0; }
0
CPP
n = int(input()); arr = [list(map(int,input().split())) for _ in range(n-1)] graph = [[] for _ in range(n)] for a,b in arr: graph[a-1].append(b-1);graph[b-1].append(a-1) mod = 10**9+7 fac = [1]*(n+1) inv = [1]*(n+1) ifac = [1]*(n+1) for i in range(2,n+1): fac[i] = fac[i-1] * i % mod inv[i] = (- (mod//i)*inv[(mod%i)]) % mod ifac[i] = ifac[i-1] * inv[i] % mod parents = [-1]*n children = [[] for _ in range(n)] orders = [] stack = [0] flags = [True] * n; flags[0] = False while stack: tmp = stack.pop() orders.append(tmp) for node in graph[tmp]: if flags[node]: flags[node] = False children[tmp].append(node) stack.append(node) parents[node] = tmp size = [1] * (n+1) dp = [1] * (n+1) for child in orders[::-1]: parent = parents[child] size[parent] += size[child] dp[child] = dp[child] * fac[size[child]-1] % mod dp[parent] = dp[parent] * dp[child] * ifac[size[child]] % mod _ = size.pop();_ = dp.pop() size2 = [n-i+1 for i in size] dp2 = [1] * n for parent in orders[1:]: child = parents[parent] dp2[parent] = dp2[parent] * fac[size2[parent]-2] * dp2[child] * ifac[size2[child]-1] * dp[child] * ifac[size[child]-1] * pow(dp[parent], mod-2, mod) * fac[size[parent]] % mod for i in range(n): print(fac[n-1]*dp[i]*ifac[size[i]-1]*dp2[i]*ifac[size2[i]-1] % mod)
0
PYTHON3
s=input() t=input() a = [] b = [] for i in range(len(s)): a += s[i] for i in range(len(t)): b += t[i] a.reverse() print('YES' if a==b else 'NO')
7
PYTHON3
n = int(input()) s = input() cont = 0 while "xxx" in s: s = s.replace("xxx", "xx", 1) cont += 1 print(cont)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 10005; struct edge { int from, to, next, v, v2; } e[N * 2]; int head[N], tot, tp[N]; int n, m, K, S1, S2, T; bool vis[N]; long long dis[N]; priority_queue<pair<long long, long long> > Q; void add(int x, int y, int v, int v2) { e[++tot] = (edge){x, y, head[x], v, v2}; head[x] = tot; } bool upd(int x, long long d, int fl) { if (d > dis[x] || (d == dis[x] && (fl | tp[x]) == tp[x])) return 0; if (d == dis[x]) tp[x] |= fl; else dis[x] = d, tp[x] = fl; Q.push(pair<long long, long long>(-dis[x], x)); return 1; } int main() { scanf("%d%d%d%d%d%d", &n, &m, &K, &S1, &S2, &T); for (int i = (1); i <= (int)m; i++) { int x, y, v; scanf("%d%d%d", &x, &y, &v); add(x, y, v, 0); } for (int i = (1); i <= (int)K; i++) { int x, y, v1, v2; scanf("%d%d%d%d", &x, &y, &v1, &v2); add(x, y, v1, v2); } for (int i = (1); i <= (int)n; i++) dis[i] = 1e18; upd(S1, 0, 1); upd(S2, 0, 2); while (!Q.empty()) { int x = Q.top().second; Q.pop(); if (vis[x]) continue; vis[x] = 1; for (int i = head[x]; i; i = e[i].next) { long long trans = dis[x] + (i > m && tp[x] & 2 ? e[i].v2 : e[i].v); upd(e[i].to, trans, tp[x]); } } if (tp[T] == 1) puts("WIN"); if (tp[T] == 2) puts("LOSE"); if (tp[T] == 3) puts("DRAW"); if (tp[T] != 2) { for (int i = (m + 1); i <= (int)m + K; i++) if (tp[e[i].from] / 2) printf("%d ", e[i].v2); else printf("%d ", e[i].v); } }
11
CPP
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("fast-math") #pragma GCC target("avx2,tune=native") using namespace std; vector<int> mult(vector<int> a, vector<int> b) { vector<int> c((int)a.size()); for (int i = 0; i < (int)a.size(); i++) c[i] = b[a[i]]; return c; } vector<int> binpow(vector<int> base, int st) { vector<int> res((int)base.size()); iota(res.begin(), res.end(), 0); while (st) { if (st & 1) res = mult(base, res); base = mult(base, base); st >>= 1; } return res; } string s; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> s; int q; vector<int> ds((int)s.size()), sd; sd.resize((int)s.size()); iota(sd.begin(), sd.end(), 1); sd[sd.size() - 1] = 0; cin >> q; while (q--) { int k, d; cin >> k >> d; int l = 0; for (int i = 0; i < d; i++) for (int j = i; j < k; j += d) ds[l++] = j; for (int i = k; i < (int)s.size(); i++) ds[l++] = i; int cnt = (int)s.size() - k + 1; vector<int> res = mult(sd, ds); res = binpow(res, (int)s.size() - k + 1); res = mult(binpow(sd, (int)s.size() - cnt), res); string t; for (auto u : res) t += s[u]; cout << (s = t) << "\n"; } return 0; }
9
CPP
#include <iostream> #include <queue> #include <algorithm> #include <vector> using namespace std; using ll = long long; const ll MAX = 10000; vector<vector<pair<ll, ll>>> graph; bool finished[MAX] = {}; ll d[MAX]; ll n; void dijkstra() { priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq; pq.push(make_pair(0, 0)); while (!pq.empty()) { auto p = pq.top(); pq.pop(); if (finished[p.second]) continue; for (auto e : graph[p.second]) { if (!finished[e.second]) { pq.push(make_pair(p.first + e.first, e.second)); } } finished[p.second] = true; d[p.second] = p.first; } } int main() { cin >> n; graph.resize(n); for (ll i = 0; i < n; i++) { ll u, k; cin >> u >> k; for (ll j = 0; j < k; j++) { ll v, c; cin >> v >> c; graph[u].push_back(make_pair(c, v)); } } dijkstra(); for (ll i = 0; i < n; i++) { cout << i << " " << d[i] << endl; } }
0
CPP
y = [0]*256 x = input() for i in x: y[ord(i)] = 1 y = y.count(1) if y % 2 == 0: print("CHAT WITH HER!") else: print("IGNORE HIM!")
7
PYTHON3
#include<bits/stdc++.h> #define maxn 300005 using namespace std; int cnt[maxn], sir[maxn], answer[maxn], tests, length; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> tests; while (tests--) { cin >> length; for (int i = 1; i <= length; i++) answer[i] = cnt[i] = 0; for (int i = 1; i <= length; i++) { cin >> sir[i]; cnt[sir[i]]++; } bool is_good = cnt[1] > 0; answer[1] = is_good; int l = 1, r = length + 1; for (int i = 2; i <= length; i++) { if(is_good) { if (cnt[i - 1] != 1) is_good = false; else if (cnt[i] == 0) is_good = false; else if (i - 1 == sir[l]) l++; else if (i - 1 == sir[r - 1]) --r; else is_good = false; } answer[i] = is_good; } is_good = 1; for (int i = 1; i <= length; i++) if (!cnt[i]) is_good = 0; answer[length] = is_good; for (int i = length; i > 0; i--) cout << answer[i]; cout << "\n"; } return 0; }
10
CPP
n=int(input()) ptr=0 overtake=0 di=dict() enter=list(map(int,input().split())) exit=list(map(int,input().split())) enter.reverse(); exit.reverse(); for i in range(n): while exit[ptr] in di: ptr+=1 if(enter[i]!=exit[ptr]): overtake+=1; di[enter[i]]=True else: ptr+=1 print(overtake)
8
PYTHON3
#include <iostream> #include <sstream> #include <cstdio> #include <cstdlib> #include <cmath> #include <ctime> #include <cstring> #include <string> #include <vector> #include <stack> #include <queue> #include <deque> #include <map> #include <set> #include <bitset> #include <numeric> #include <utility> #include <iomanip> #include <algorithm> #include <functional> using namespace std; #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl #define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i) template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) { return s << '<' << P.first << ", " << P.second << '>'; } template<class T> ostream& operator << (ostream &s, vector<T> P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template<class T> ostream& operator << (ostream &s, vector<vector<T> > P) { for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; } template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) { EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; } const long long INF = 1LL<<60; typedef long long DELAY; struct VAL { long long minv; long long maxv; long long minnum; long long maxnum; VAL() { minv = maxv = 0; minnum = maxnum = 1; } VAL(long long a, long long b, long long c, long long d) { minv = a; maxv = b; minnum = c; maxnum = d; } friend ostream& operator << (ostream& s, const VAL& v) { return s << "(<" << v.minv << "," << v.minnum << ">, <" << v.maxv << ", " << v.maxnum << ">)"; } }; template<class Monoid, class Action> struct SegTree { int size_seg = 1; vector<Monoid> val; vector<Action> delay; Monoid UNITY_MONOID = VAL(INF, -INF, 0, 0); // to be set Action UNITY_ACTION = 0; // to be set SegTree(int n = 1) { init(n); } SegTree(int n, const Monoid &init_monoid, const Action &init_action) { init(n, init_monoid, init_action); } void init(int n = 1) { size_seg = 1; while (size_seg < n) size_seg <<= 1; val.resize(size_seg * 2 - 1); delay.resize(size_seg * 2 - 1); for (int i = 0; i < size_seg * 2 - 1; ++i) { val[i] = UNITY_MONOID; delay[i] = UNITY_ACTION; } } void init(int n, const Monoid &init_monoid, const Action &init_action) { size_seg = 1; while (size_seg < n) size_seg <<= 1; val.resize(size_seg * 2 - 1); delay.resize(size_seg * 2 - 1); for (int i = 0; i < size_seg * 2 - 1; ++i) { val[i] = init_monoid; delay[i] = init_action; } } inline void init_set(int a, const Monoid &x) { a += size_seg - 1; val[a] = x; } inline void init_tree(int k = 0, int l = 0, int r = -1) { if (r == -1) r = size_seg; if (r - l > 1) { init_tree(k*2+1, l, (l+r)/2); init_tree(k*2+2, (l+r)/2, r); val[k] = merge(val[k*2+1], val[k*2+2]); } } inline Monoid merge(const Monoid &x, const Monoid &y) { Monoid res = UNITY_MONOID; if (x.minv > y.minv) { res.minv = y.minv; res.minnum = y.minnum; } else if (x.minv == y.minv) { res.minv = x.minv; res.minnum = x.minnum + y.minnum; } else { res.minv = x.minv; res.minnum = x.minnum; } if (x.maxv < y.maxv) { res.maxv = y.maxv; res.maxnum = y.maxnum; } else if (x.maxv == y.maxv) { res.maxv = x.maxv; res.maxnum = x.maxnum + y.maxnum; } else { res.maxv = x.maxv; res.maxnum = x.maxnum; } return res; } inline void add(Action &d, const Action &e) { // d += e; d += e; } inline void reflect(Monoid &x, const Action &d, int l, int r) { // x += d; x.minv += d; x.maxv += d; } inline void redelay(int k, int l, int r) { reflect(val[k], delay[k], l, r); if (k < size_seg-1) { add(delay[k*2+1], delay[k]); add(delay[k*2+2], delay[k]); } delay[k] = UNITY_ACTION; } inline void renode(int k, int l, int r) { val[k] = merge(val[k*2+1], val[k*2+2]); } inline void set(int a, int b, DELAY x, int k = 0, int l = 0, int r = -1) { if (r == -1) r = size_seg; if (a <= l && r <= b) { // included add(delay[k], x); redelay(k, l, r); } else if (a < r && l < b) { // intersected redelay(k, l, r); set(a, b, x, k*2+1, l, (l+r)/2); set(a, b, x, k*2+2, (l+r)/2, r); renode(k, l, r); } else { // no-intersected redelay(k, l, r); } } inline Monoid get(int a, int b, int k = 0, int l = 0, int r = -1) { if (r == -1) r = size_seg; redelay(k, l, r); if (a <= l && r <= b) { // included return val[k]; } else if (a < r && l < b) { // intersected Monoid t1 = get(a, b, k*2+1, l, (l+r)/2); Monoid t2 = get(a, b, k*2+2, (l+r)/2, r); renode(k, l, r); return merge(t1, t2); } else { // no-intersected return UNITY_MONOID; } } inline Monoid operator [] (int i) {return get(i, i+1);} void print() { long long t = 2; for (int i = 0; i < size_seg*2-1; ++i) { cout << make_pair(val[i], delay[i]) << ","; if (i == t-2) {cout << endl; t *= 2;} } for (int i = 0; i < size_seg; ++i) { cout << get(i, i+1) << ", "; } cout << endl << endl; } }; SegTree<VAL,int> xseg, yseg; long long H, W, Q; int main() { while (cin >> H >> W >> Q) { xseg.init((int)max(H, W)+2, VAL(0, 0, 1, 1), 0); yseg.init((int)max(H, W)+2, VAL(0, 0, 1, 1), 0); for (int i = 0; i < H; ++i) { int x; cin >> x; xseg.set(i, i+1, x); } for (int i = 0; i < W; ++i) { int y; cin >> y; yseg.set(i, i+1, y); } for (int q = 0; q < Q; ++q) { int t; cin >> t; if (t == 1) { int a, b; long long v; cin >> a >> b >> v; --a, --b; xseg.set(a, b+1, v); } else if (t == 2) { int c, d; long long v; cin >> c >> d >> v; --c, --d; yseg.set(c, d+1, v); } else if (t == 3 || t == 4) { int a, b, c, d; cin >> a >> b >> c >> d; --a, --b, --c, --d; VAL x = xseg.get(a, b+1); VAL y = yseg.get(c, d+1); long long res = INF; if (t == 4) res = -INF; long long resnum = 0; if (t == 3) { chmin(res, x.minv * y.minv); chmin(res, x.minv * y.maxv); chmin(res, x.maxv * y.minv); chmin(res, x.maxv * y.maxv); } else { chmax(res, x.minv * y.minv); chmax(res, x.minv * y.maxv); chmax(res, x.maxv * y.minv); chmax(res, x.maxv * y.maxv); } if (res == 0) { long long xnonzero = b - a + 1; if (x.minv == 0) xnonzero -= x.minnum; else if (x.maxv == 0) xnonzero -= x.maxnum; long long ynonzero = d - c + 1; if (y.minv == 0) ynonzero -= y.minnum; else if (y.maxv == 0) ynonzero -= y.maxnum; resnum = (b-a+1) * (d-c+1) - xnonzero * ynonzero; } else { if (res == x.minv * y.minv) resnum += x.minnum * y.minnum; if (res == x.maxv * y.minv && x.maxv != x.minv) resnum += x.maxnum * y.minnum; if (res == x.minv * y.maxv && y.maxv != y.minv) resnum += x.minnum * y.maxnum; if (res == x.maxv * y.maxv && x.maxv != x.minv && y.maxv != y.minv) resnum += x.maxnum * y.maxnum; } cout << res << " " << resnum << endl; } /* COUT("----------"); COUT(q); xseg.print(); yseg.print(); */ } } }
0
CPP
days,bowl,plate = map(int,(input().split())) #print(days) #print(bowl) #print(plate) count1 = 0 count2 = 0 menu = (input().split()) #menu.append(inp) for i in range(days): #print(menu[i]) if(menu[i]=='1'): count1 = count1+1 elif(menu[i]=='2'): count2 = count2+1 bowl = bowl-count1 if(bowl>0): left = bowl+plate plate = left - count2 if(plate>=0): print("0") else: print(abs(plate)) if(bowl<=0): plate = plate - count2 if(plate<0): print(abs(plate+bowl)) else: print(abs(bowl))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<(n);i++) stack<int> s; int a,b; void op() { a = s.top(); s.pop(); b = s.top(); s.pop(); } int main() { string w; while (cin >> w) { if(w == "+") { op(); s.push(a + b); } else if (w == "-") { op(); s.push(b - a); } else if (w == "*") { op(); s.push(b * a); } else { s.push(atoi(w.c_str())); } } cout << s.top() << endl; return 0; }
0
CPP
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; long long n, m, a, b, c, k, temp, x, y; const int MAXN = 1e5 + 11, mod = 1e9 + 7; long long max(long long a, long long b) { return ((a > b) ? a : b); } long long min(long long a, long long b) { return ((a > b) ? b : a); } vector<long long> read(int n) { vector<long long> v(n); for (int i = 0; i < v.size(); i++) cin >> v[i]; return v; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); vector<long long> v[5000]; int total[5000]; bool vis[3001][3001]; int down[5000], parentmi[5000], up[5000]; int mi = 5000, pr; void dfsfortotal(int u, int p = -1) { for (auto x : v[u]) { if (x == p) continue; dfsfortotal(x, u); total[u] += total[x]; if (!vis[u][x]) total[u]++; } } void dfsforans(int u, int p = -1) { if (p != -1) { up[u] = up[p]; down[u] = down[p]; if (!vis[p][u]) up[u]++; if (vis[p][u]) down[u]++; parentmi[u] = min(parentmi[p], up[u] - down[u]); mi = min(total[pr] - up[u] + down[u] + parentmi[u], mi); } for (auto x : v[u]) { if (x == p) continue; dfsforans(x, u); } } void sol(void) { cin >> n; m = n - 1; while (m--) { cin >> a >> b; vis[a][b] = true; v[a].push_back(b); v[b].push_back(a); } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) down[j] = total[j] = up[j] = 0, parentmi[j] = 5000; parentmi[i] = 0; dfsfortotal(i); mi = min(total[i], mi); pr = i; dfsforans(i); } cout << mi; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int test = 1; for (int i = 1; i <= test; i++) sol(); }
9
CPP
n = int(input()) l = [int(x) for x in input().split()] ans, c = 0, 0 for i in range(2*n): if l[i%n] == 1: c += 1 ans = max(ans, c) else: c = 0 print(ans)
8
PYTHON3
n,t=map(int,input().split()) seq=list(map(int,input().split())) head=0 tail=0 s=[] x=0 count=1 ans=0 cursum=seq[0] for i in range(n): x+=seq[i] s.append(x) while(head!=n): if head!=0: if tail==n: break cursum=s[tail] - s[head-1] else: if tail==n: break cursum=s[tail] while cursum<=t: tail+=1 count+=1 if head!=0: if tail==n: break cursum=s[tail]-s[head-1] else: if tail==n: break cursum=s[tail] head+=1 count-=1 ans=max(ans,count) print(ans)
8
PYTHON3
#include<bits/stdc++.h> using namespace std; int a[200005],n; double ans; int main() { scanf("%d",&n); for (int i=0;i<2*n;i++)scanf("%d",&a[i]); sort(a,a+2*n); for (int i=0;i<2*n;i++) ans+=(double)i*a[i]/(2*n-1); printf("%.10lf",ans); }
0
CPP
#include <iostream> #include <iomanip> #include <complex> #include <vector> #include <algorithm> #include <cmath> #include <array> #include <map> #include <queue> using namespace std; const double EPS = 1e-6; const double INF = 1e12; #define EQ(n,m) (abs((n)-(m)) < EPS) #define X real() #define Y imag() typedef complex<double> P; typedef vector<P> VP; struct L : array<P, 2>{ L(const P& a, const P& b){ at(0)=a; at(1)=b; } L(){} }; namespace std{ bool operator < (const P& a, const P& b){ return !EQ(a.X,b.X) ? a.X<b.X : a.Y+EPS<b.Y; } bool operator == (const P& a, const P& b){ return abs(a-b) < EPS; } } double dot(P a, P b){ return (conj(a)*b).X; } double cross(P a, P b){ return (conj(a)*b).Y; } int ccw(P a, P b, P c){ b -= a; c -= a; if(cross(b,c) > EPS) return +1; //ccw if(cross(b,c) < -EPS) return -1; //cw if(dot(b,c) < -EPS) return +2; //c-a-b if(abs(c)-abs(b) > EPS) return -2; //a-b-c return 0; //a-c-b } bool intersectSS(const L& a, const L& b){ return ( ccw(a[0],a[1],b[0]) *ccw(a[0],a[1],b[1]) <= 0 ) && ( ccw(b[0],b[1],a[0]) *ccw(b[0],b[1],a[1]) <= 0 ); } bool intersectSP(const L& s, const P &p){ return abs(cross(s[0]-p, s[1]-p))<EPS && dot(s[0]-p, s[1]-p)<EPS; } P crosspointLL(const L &l, const L &m) { double A = cross(l[1]-l[0], m[1]-m[0]); double B = cross(l[1]-l[0], l[1]-m[0]); return m[0] + B/A *(m[1]-m[0]); } bool isParallel(const P &a, const P &b){ return abs(cross(a,b)) < EPS; } bool isParallel(const L &a, const L &b){ return isParallel(a[1]-a[0], b[1]-b[0]); } int in_poly(const P &p, const VP &poly){ int n = poly.size(); int ret = -1; for(int i=0; i<n; i++){ P a = poly[i]-p; P b = poly[(i+1)%n]-p; if(a.Y > b.Y) swap(a,b); if(intersectSP(L(a,b), P(0,0))) return 0; if(a.Y<=0 && b.Y>0 && cross(a,b)<0) ret = -ret; } return ret; } vector<vector<vector<int> > > make_graph(vector<L>& lines, VP &poly, P s, P g){ VP plist; int ls = lines.size(); vector<VP> cp(ls); for(int i=0; i<ls; i++){ for(int j=i+1; j<ls; j++){ if(!isParallel(lines[i], lines[j])){ P cpij = crosspointLL(lines[i], lines[j]); cp[i].push_back(cpij); cp[j].push_back(cpij); plist.push_back(cpij); } } sort(cp[i].begin(), cp[i].end()); cp[i].erase(unique(cp[i].begin(), cp[i].end()), cp[i].end()); } sort(plist.begin(), plist.end()); plist.erase(unique(plist.begin(), plist.end()), plist.end()); for(int i=0; i<(int)plist.size(); i++){ if(plist[i] == s) swap(plist[i], plist[0]); if(plist[i] == g) swap(plist[i], plist[1]); } //make convmap (普通にdoubleをキーにすると事故るので、比較関数は誤差ありを使う) map<P, int> conv; for(int i=0; i<(int)plist.size(); i++){ conv[plist[i]] = i; } P dir8[8] = {P(1,0), P(1,1), P(0,1), P(-1,1), P(-1,0), P(-1,-1), P(0,-1), P(1,-1)}; int n = plist.size(); //graph[点][向き] = 到達可能点リスト vector<vector<vector<int> > > graph(n, vector<vector<int> >(8)); for(int i=0; i<ls; i++){ int d=-1; //cp[i][0] から cp[i][1] に進む方向 for(int j=0; j<8; j++){ if(intersectSP(L(0, 1e5*dir8[j]), cp[i][1] -cp[i][0])){ d = j; break; } } for(int j=0; j<(int)cp[i].size()-1; j++){ if(in_poly((cp[i][j] +cp[i][j+1])/2.0, poly) == -1) continue; graph[conv[cp[i][j]]][d].push_back(conv[cp[i][j+1]]); graph[conv[cp[i][j+1]]][(d+4)%8].push_back(conv[cp[i][j]]); } } //壁伝いの移動 for(int i=0; i<n; i++){ for(int j=1; j<8; j+=2){ //壁にぶつかる if(in_poly(plist[i]+(0.1*dir8[j]), poly) != -1) continue; //かつ壁づたいに動ける if(!graph[i][(j+1)%8].empty()){ graph[i][j].push_back(graph[i][(j+1)%8][0]); } if(!graph[i][(j+7)%8].empty()){ graph[i][j].push_back(graph[i][(j+7)%8][0]); } } } return graph; } int dijkstra(vector<vector<vector<int> > > &graph){ struct info{ int v,d,c; info(int v, int d, int c):v(v),d(d),c(c){} info(){} bool operator<(const info &a) const{ return c > a.c; } }; const int inf = 1e9; int n = graph.size(); priority_queue<info> wait; vector<vector<int> > mincost(n, vector<int>(8, inf)); for(int d=0; d<8; d++){ wait.push(info(0, d, 1)); mincost[0][d] = 1; } int ans = inf; while(!wait.empty()){ int v = wait.top().v; int d = wait.top().d; int c = wait.top().c; wait.pop(); if(c > mincost[v][d]) continue; if(v == 1){ ans = c; break; } for(int nd=0; nd<8; nd++){ int nc = (d==nd)? c: c+1; for(int i=0; i<(int)graph[v][nd].size(); i++){ int nv = graph[v][nd][i]; if(nc < mincost[nv][nd]){ mincost[nv][nd] = nc; wait.push(info(nv, nd, nc)); } } } } return ans; } int main(){ while(1){ int n; cin >> n; if(n==0) break; //get input int sx,sy,gx,gy; cin >> sx >> sy >> gx >> gy; P s(sx, sy), g(gx, gy); VP poly(n); for(int i=0; i<n; i++){ int x,y; cin >> x >> y; poly[i] = P(x, y); } vector<L> lines; P dir4[4] = {P(0,1), P(1,1), P(1,0), P(1,-1)}; for(int i=0; i<n; i++){ for(int d=0; d<4; d++){ lines.push_back(L(poly[i], poly[i]+dir4[d])); } } for(int d=0; d<4; d++){ lines.push_back(L(s, s+dir4[d])); lines.push_back(L(g, g+dir4[d])); } vector<vector<vector<int> > > graph = make_graph(lines, poly, s, g); cout << dijkstra(graph) << endl; } return 0; }
0
CPP
def isSubSequence(str1,str2): m = len(str1) n = len(str2) j = 0 # Index of str1 i = 0 # Index of str2 while j<m and i<n: if str1[j] == str2[i]: j = j+1 i = i + 1 # If all characters of str1 matched, then j is equal to m return j==m # Driver Program #str1 = str(input("str1: ")) str1="hello" str2 = str(input()) #print ("Yes") if isSubSequence(str1,str2) else ("No") if isSubSequence(str1,str2): print("YES") else: print("NO")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; struct cube { int data[6]; void rotate() { int temp = data[0]; data[0] = data[3]; data[3] = data[4]; data[4] = data[1]; data[1] = temp; } void roll_left() { int temp = data[1]; data[1] = data[2]; data[2] = data[3]; data[3] = data[5]; data[5] = temp; } void roll_up() { int temp = data[0]; data[0] = data[2]; data[2] = data[4]; data[4] = data[5]; data[5] = temp; } int get_hash() { int result = 0; for (int i = 0; i < 6; i++) { result *= 6; result += data[i]; } return result; } }; void add_cube(const cube& a, set<int>& hashes) { bool wanna_add = true; cube b = a; for (int j = 0; j < 4; j++) { for (int i = 0; i < 4; i++) { int hash = b.get_hash(); if (hashes.find(hash) != hashes.end()) { wanna_add = false; break; } b.rotate(); } b.roll_up(); } for (int j = 0; j < 4; j++) { for (int i = 0; i < 4; i++) { int hash = b.get_hash(); if (hashes.find(hash) != hashes.end()) { wanna_add = false; break; } b.rotate(); } b.roll_left(); } if (wanna_add) { hashes.insert(b.get_hash()); } } int main() { cube a; for (int i = 0; i < 6; i++) { char c; cin >> c; int code; if (c == 'R') { code = 0; } else if (c == 'O') { code = 1; } else if (c == 'Y') { code = 2; } else if (c == 'G') { code = 3; } else if (c == 'B') { code = 4; } else if (c == 'V') { code = 5; } a.data[i] = code; } set<int> hashes; sort(a.data, a.data + 6); do { add_cube(a, hashes); } while (next_permutation(a.data, a.data + 6)); cout << hashes.size() << endl; return 0; }
7
CPP
#include<stdio.h> int main(){ int a, b; scanf("%d", &a); b = ((1+a)*a)/2; printf("%d\n", b); return 0; }
0
CPP
n,m = list(map(int,input().split(" "))) Meow =[] for i in range(101): Meow.append([]) for j in range(n+1): Meow[i].append(j) def root(N,C): while(Meow[C][N]!=N): Meow[C][N] = Meow[C][Meow[C][N]]; N=Meow[C][N] return N def union(A,B,C): p = root(A,C) q = root(B,C) Meow[C][p]=q for i in range(m): a,b,c = list(map(int,input().split(" "))) union(a,b,c) q = int(input()) for i in range(q): a,b = list(map(int,input().split(" "))) cnt=0; for i in range(1,101): if(root(a,i)==root(b,i)): cnt+=1 print(cnt)
8
PYTHON3
import sys, math import io, os #data = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline from bisect import bisect_left as bl, bisect_right as br, insort from heapq import heapify, heappush, heappop from collections import defaultdict as dd, deque, Counter #from itertools import permutations,combinations def data(): return sys.stdin.readline().strip() def mdata(): return list(map(int, data().split())) def outl(var) : sys.stdout.write('\n'.join(map(str, var))+'\n') def out(var) : sys.stdout.write(str(var)+'\n') #from decimal import Decimal from fractions import Fraction #sys.setrecursionlimit(100000) INF = float('inf') mod = int(1e9)+7 for t in range(int(data())): n=int(data()) a=sorted(mdata(),reverse=True) x,y=0,0 s=0 l=[0]*32 for i in range(n): s = s ^ a[i] for j in range(32): if (1<<j)&a[i]: l[j]+=1 if s==0: out("DRAW") continue if n%2==0: out("WIN") else: for i in range(31,-1,-1): if l[i]%2!=0: if l[i]%4==1: out("WIN") else: out("LOSE") break
8
PYTHON3
def suff(a, b): if a == b: return False else: return len(b) > len(a) and b[len(b) - len(a):] == a n = int(input()) a = {} for i in range(n): s = input().split() name = s[0] nums = s[2:] if name in a.keys(): a[name].extend(nums) else: a[name] = nums for name in a.keys(): nums = [] for i in a[name]: if i not in nums: fl = False for j in a[name]: if suff(i, j): fl = True if not fl: nums.append(i) a[name] = nums print(len(a.keys())) for name in a.keys(): print(name, len(a[name]), end=' ') for i in a[name]: print(i, end=' ') print()
9
PYTHON3
n = int(input()) a = input() if a.count('1') >= 1: print('1'+a.count('0')*'0') else: print(0)
7
PYTHON3
from collections import deque # https://www.geeksforgeeks.org/detect-cycle-in-the-graph-using-degrees-of-nodes-of-graph/ def findCycle(grafo): n = len(grafo) cantidad_conexiones = [len(destinos) for destinos in grafo] visitados = [False for i in range(n)] cola = deque(i for i in range(n) if cantidad_conexiones[i] == 1) while cola: while cola: nodo = cola.popleft() visitados[nodo] = True for destino in grafo[nodo]: cantidad_conexiones[destino] -= 1 cola = deque(i for i in range(n) if cantidad_conexiones[i] == 1 and not visitados[i]) nodos_ciclo = [] for i in range(n): if not visitados[i]: nodos_ciclo.append(i) return nodos_ciclo def distanciasAlCiclo(grafo, ciclo, numero_grande=float("inf")): distancias = [numero_grande for i in range(n)] for nodo in ciclo: distancias[nodo] = 0 cola = deque(ciclo) while len(cola): nodo = cola.popleft() for conectado in grafo[nodo]: if distancias[conectado] == numero_grande: distancias[conectado] = distancias[nodo] + 1 if conectado not in ciclo: cola.append(conectado) return distancias n = int(input()) grafo = [list() for i in range(n)] for i in range(n): x, y = map(lambda z: int(z)-1, input().split()) grafo[x].append(y) grafo[y].append(x) nodos_ciclo = findCycle(grafo) print(" ".join(map(str, distanciasAlCiclo(grafo, nodos_ciclo))))
10
PYTHON3
from sys import stdin def main(): n = int(stdin.readline().strip()) l, c = [0, 0], [0, 0] for i, x in enumerate(map(int, stdin.readline().strip().split())): x &= 1 l[x] = i c[x] += 1 return l[c[0] > c[1]] + 1 print(main())
7
PYTHON3
# include <bits/stdc++.h> using namespace std; const int maxn = 500000 + 10; char s[maxn]; short int a[maxn]; int len; bool check(int val) { int sum = val * 9, s = 0; for(int i = 0; i < len; ++i) { sum = sum + a[i]; s += sum % 10; sum /= 10; } while(sum) { s += sum % 10; sum /= 10; } return s <= val * 9; } int main() { scanf("%s", s); len = strlen(s); reverse(s, s + len); for(int i = 0; i < len; ++i) a[i] = (s[i] - '0') * 9; for(int i = 0; i < len; ++i) a[i + 1] += a[i] / 10, a[i] %= 10; if(a[len]) ++len; int l = 0, r = len * 2; while(r - l > 1) { int mid = (l + r) >> 1; check(mid) ? r = mid : l = mid; } printf("%d\n", r); return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, c = 0, c1 = 0; string str = ""; cin >> n; for (int i = 4; i <= n; i++) { if (n % i == 0) { str = to_string(i); for (int j = 0; str[j] == '4' || str[j] == '7' && j < str.length(); j++) { c1++; } if (c1 == str.length()) { cout << "YES"; c++; break; } } } if (c != 1) cout << "NO"; return 0; }
7
CPP
calories = list(map(int, input().split())) game = input() ans = 0 for sq in game: ans += calories[int(sq) - 1] print(ans)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { double n, m, k; cin >> n >> m >> k; int r = ceil(k / (2.0 * m)); char c; if (int(k) % 2 == 1) c = 'L'; else c = 'R'; cout << r << " " << ceil((k - (r - 1.0) * 2.0 * m) / 2.0) << " " << c; }
7
CPP
n = int(input()) members = input().split(' ') one = 0 two = 0 three = 0 four = 0 for i, num in enumerate(members): if num == '1': one += 1 elif num == '2': two += 1 elif num == '3': three +=1 else: four += 1 count = four + three if two % 2 == 0: count += two // 2 if one > three: if (one - three) % 4 == 0: count += (one - three) // 4 else: count += (one - three) // 4 + 1 else: count += two // 2 + 1 one -= 2 if one > three: if (one - three) % 4 == 0: count += (one - three) // 4 else: count += (one - three) // 4 + 1 print(count)
8
PYTHON3
from sys import stdin inf=stdin for qw in range(int(inf.readline())): n=int(inf.readline()) arr=list(map(int,inf.readline().split(" "))) print(len(set(arr))) #n=int(inf.readline()) #s=inf.readline() # s="bacabcab" # changed=False # while not changed: # print(s) # changed=True # i=0 # ll=len(s) # while(i+1<ll and ord(s[i])!=ord(s[i+1])+1): # i+=1 # j=i+1 # while(j+1<ll and ord(s[j])==ord(s[j+1])+1): # j+=1 # if(i+1!=ll): # s=s[:i]+s[j+1:] # changed=False # print(i,j,s[:i],s[j+1:]) # i=0 # ll=len(s) # while(i+1<ll and ord(s[i])!=ord(s[i+1])-1): # i+=1 # j=i+1 # while(j+1<ll and ord(s[j])==ord(s[j+1])-1): # j+=1 # if(i+1!=ll): # s=s[:i+1]+s[j+2:] # changed=False # print(i,j,s[:i+1],s[j+2:]) # print(len(s)) # for i in dicti: # temp=0 # for j in helper: # if(j[0]<=i[0] and j[1]<=i[1]): # temp+=(i[0]-j[0]+1)*(i[1]-j[1]+1) # if(j[1]<=i[0] and j[0]<=i[1] and j[0]!=j[1]): # temp+=(i[0]-j[1]+1)*(i[1]-j[0]+1) # count+=temp*dicti[i] # def all_partitions(string): # for cutpoints in range(1 << (len(string)-1)): # result = [] # lastcut = 0 # for i in range(len(string)-1): # if (1<<i) & cutpoints != 0: # result.append(string[lastcut:(i+1)]) # lastcut = i+1 # result.append(string[lastcut:]) # yield result # maxyet=0 # store={'h': -6, 'e': -7, 'l': -8, 'o': 3, 'he': 3, 'hel': -3, 'el': 0, 'hell': 6, 'ell': -5, 'll': 10, 'hello': -3, 'ello': -8, 'llo': 9, 'lo': -6} # def quality(stri): # return store[stri] # for partition in all_partitions("hel"): # temp=0 # for i in partition: # temp+=quality(i) # if(temp>maxyet): # print(partition) # maxyet=temp # print(maxyet)
8
PYTHON3
a,b=map(int,input().split()) c=list(map(int,input().split())) d=0 for i in c: if i>b: d+=2 else: d+=1 print(d)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 2005; const int mod = 1e9 + 7; int n, k, memo[N][N]; int dp(int pos, int val) { if (val > n) return 0; if (pos == k - 1) { return 1; } if (memo[pos][val] != -1) return memo[pos][val]; int ans = 0; for (int i = val; i <= n; i += val) { ans += dp(pos + 1, i); ans %= mod; } return memo[pos][val] = ans; } int main() { scanf("%d %d", &n, &k); memset(memo, -1, sizeof memo); int ANS = 0; for (int i = 1; i <= n; i++) { ANS += dp(0, i); ANS %= mod; } printf("%d\n", ANS); return 0; }
8
CPP
from sys import stdin tt = int(stdin.readline()) for loop in range(tt): n = int(stdin.readline()) a = list(map(int,stdin.readline().split())) l = [0] * (n+1) ans = 0 for j in range(n): r = [0] * (n+1) for k in range(n-1,j,-1): ans += l[a[k]] * r[a[j]] r[a[k]] += 1 l[a[j]] += 1 print (ans)
10
PYTHON3
#include<bits/stdc++.h> using namespace std; #define int long long int sum=0; int a[510],ans,n,k; int sum1[510],sum2[510],p[510]; bool check(int x){ int cnt=0; for(int i=1;i<=n;i++)p[i]=a[i]%x; sort(p+1,p+n+1); for(int i=1;i<=n;i++)sum1[i]=sum1[i-1]+p[i]; for(int i=n;i>=1;i--)sum2[i]=sum2[i+1]+x-p[i]; int mi=k; for(int i=1;i<=n;i++){ if(sum1[i]==sum2[i+1]){ if(sum1[i]<=k)return 1; else return 0; } } return 0; } signed main(){ scanf("%lld%lld",&n,&k); for(int i=1;i<=n;i++)scanf("%lld",&a[i]),sum+=a[i]; for(int i=1;i*i<=sum;i++){ if(sum%i==0){ if(check(i))ans=max(ans,i); if(i!=(sum/i)){ if(check(sum/i))ans=max(ans,sum/i); } } } cout<<ans; }
0
CPP
t = int(input()) for _ in range(t): n,m = map(int, input().split()) a = list(map(int, input().split())) b = list(map(int, input().split())) found = False for i in range(n): if (a[i] in b): print('YES') print(1, a[i]) found = True break if (not found): print('NO')
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 500 + 10; int d[maxn][maxn]; bool mark[maxn]; int n, x[maxn]; long long ans[maxn]; int main() { while (scanf("%d", &n) != EOF) { for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) scanf("%d", &d[i][j]); memset(mark, 0, sizeof(mark)); for (int i = 1; i <= n; i++) scanf("%d", &x[i]); int k; for (int t = 1; t <= n; t++) { k = x[n - t + 1]; mark[k] = true; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (i != k && j != k && i != j && d[i][k] > 0 && d[k][j] > 0) d[i][j] = min(d[i][j], d[i][k] + d[k][j]); ans[t] = 0; for (int i = 1; i <= n; i++) if (mark[i]) for (int j = 1; j <= n; j++) if (mark[j]) ans[t] += d[i][j]; } for (int i = n; i >= 1; i--) printf("%I64d ", ans[i]); printf("\n"); } return 0; }
10
CPP
x=eval(input()) y=list(map(int,input().split())) i=0 c1=0 c2=0 while(i<x): if(y[i]%2==0): c1+=1 m=i+1 else: c2+=1 n=i+1 i+=1 if(c1>c2): print(n) else: print(m)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<int> adj[105]; bool visited[105]; int df[1005]; int ans = 0, k = 0; void dfs(int i, int parent) { visited[i] = true; for (int j = 0; j < adj[i].size(); j++) { if (!visited[adj[i][j]]) dfs(adj[i][j], i); } } int main() { int n, m; cin >> n >> m; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } for (int i = 1; i <= n; i++) { if (!visited[i]) k++, dfs(i, 0); } if (n == m && k == 1) cout << "FHTAGN!\n"; else cout << "NO\n"; }
8
CPP
from sys import stdin, stdout, setrecursionlimit import threading # tail-recursion optimization # In case of tail-recusion optimized code, have to use python compiler. # Otherwise, memory limit may exceed. # declare the class Tail_Recursion_Optimization class Tail_Recursion_Optimization: def __init__(self, RECURSION_LIMIT, STACK_SIZE): setrecursionlimit(RECURSION_LIMIT) threading.stack_size(STACK_SIZE) return None class SOLVE: def solve(self): R = stdin.readline #f = open('input.txt');R = f.readline W = stdout.write ans = [] for i in range(int(R())): n, x = [int(x) for x in R().split()] if n <= 2: ans.append('1') continue floor = 2 cnt = 1 while floor < n: floor += x cnt += 1 ans.append(str(cnt)) W('\n'.join(ans)) return 0 def main(): s = SOLVE() s.solve() #Tail_Recursion_Optimization(10**7, 100*1024**2) # recursion-call size, stack-size in byte (MB*1024**2) #threading.Thread(target=main).start() main()
7
PYTHON3
s=input() if len(s)==1 and s.islower(): p=s.upper() print(p) elif len(s)==1 and s.isupper(): n=s.lower() print(n) else: if s[1:].isupper() and s[0].islower(): print(s.capitalize()) elif s.isupper(): b=s.lower() print(b) else: print(s)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; long long int a[2000006]; int main() { long long int n, t, l, i, c, p; double ans; scanf("%lld%lld%lld", &n, &l, &t); c = t / l; p = t % l; for (i = 0; i < n; i++) { scanf("%lld", &a[i]); a[n + i] = a[i] + l; } ans = c * n * (n - 1) / 2; for (i = 0; i < n; i++) ans += (upper_bound(a, a + 2 * n, a[i] + 2 * p) - (a + i + 1)) * 0.25; printf("%lf\n", ans); return 0; }
7
CPP
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; using ll = long long; using VI = vector<ll>; using VV = vector<VI>; using VS = vector<string>; using PII = pair<ll, ll>; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string& s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(char c) { string s = {c}; return s; } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto& x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << '\n'; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } template <class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } void yes() { cout << ("Yes") << '\n'; exit(0); } void no() { cout << ("No") << '\n'; exit(0); } void possible() { cout << ("Possible") << '\n'; exit(0); } void impossible() { cout << ("Impossible") << '\n'; exit(0); } ll SUM(VI& V) { return accumulate((V).begin(), (V).end(), 0LL); } ll MIN(VI& V) { return *min_element((V).begin(), (V).end()); } ll MAX(VI& V) { return *max_element((V).begin(), (V).end()); } void print_vector(VI& V, ll offset = 0) { ll n = V.size(); for (ll i = (0); i < (n); ++i) { if (i) cout << ' '; cout << V[i] + offset; } cout << endl; } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; } using ld = long double; constexpr ld EPS = 1e-14; constexpr bool equals(ld a, ld b) { return fabs((a) - (b)) < EPS; } using inverse_priority_queue = priority_queue<ll, vector<ll>, greater<ll> >; int popcount(ll t) { return __builtin_popcountll(t); } const ll mod = 1e9 + 7; const ll inf = 1e18; const double PI = acos(-1); ll ceil_div(ll a, ll b) { return (a + b - 1) / b; } ll ll_pow(ll a, ll n) { ll ans = 1; for (ll i = (0); i < (n); ++i) { ans *= a; } return ans; } struct mint { ll x; mint(ll x = 0) : x((x % mod + mod) % mod) {} mint operator-() const { return mint(-x); } mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } mint inv() const { return pow(mod - 2); } mint& operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; VV load_graph(ll N, ll M) { VV G(N); for (ll i = (0); i < (M); ++i) { ll a, b; cin >> a >> b; a--; b--; G[a].push_back(b); G[b].push_back(a); } return G; } VV load_tree(ll N) { return load_graph(N, N - 1); } void solve() { ll N; cin >> N; VI A(N); for (ll i = (0); i < (N); ++i) { cin >> A[i]; } } int main() { cin.tie(0); ios::sync_with_stdio(false); ll N, M; cin >> N >> M; VI A(M); for (ll i = (0); i < (M); ++i) { cin >> A[i]; } 42; ll base = 0; for (ll i = (0); i < (M - 1); ++i) { base += abs(A[i] - A[i + 1]); } 42; set<ll> se; for (ll a : A) se.insert(a); map<ll, VI> index_map; for (ll i = (0); i < (M); ++i) { ll a = A[i]; index_map[a].push_back(i); } ll mi = base; map<ll, VI> mp; for (ll a : se) { for (ll i : index_map[a]) { if (i < M - 1) { if (A[i + 1] != a) mp[a].push_back(A[i + 1]); } if (i > 0) { if (A[i - 1] != a) mp[a].push_back(A[i - 1]); } } ll before = 0; for (ll v : mp[a]) { before += abs(a - v); } sort((mp[a]).begin(), (mp[a]).end()); ll sz = mp[a].size(); if (sz == 0) continue; ll med = mp[a][sz / 2]; ll after = 0; for (ll v : mp[a]) { after += abs(med - v); } ll diff = before - after; ll candidate = base - diff; chmin(mi, candidate); } cout << (mi) << '\n'; return 0; }
7
CPP
q,n=input().split() x=input() x=list(x.strip()) for i in range(int(n)): m=list(input().split()) l=int(m[0]) r=int(m[1]) for i in range(l-1,r): if x[i]==m[2]: x[i]=m[3] if l==r: if x[l-1]==m[2]: x[i]=m[3] ans="" for i in x: ans=ans+i print(ans)
7
PYTHON3
n = int(input()); horiz = [0] * n; vert = [0] * n; realDays = [False] * n**2; for i in range( 0, n**2): line = [int(n) for n in input().split()]; if( horiz[line[0]-1]==0 and vert[line[1]-1]==0 ): horiz[line[0]-1] = 1; vert[line[1]-1] = 1; realDays[i] = True; out = ""; for i in range( 0, len(realDays) ): if( realDays[i] ): out += str(i+1) + " "; print(out);
7
PYTHON3
#include <iostream> using namespace std; int H,W,h,w; int main() { cin >> H >> W >> h >> w; if (H%h != 0 || W%w != 0) { cout << "Yes\n"; int k=(H/h)*(W/w)+1; for (int i=0; i<H; i++) { for (int j=0; j<W; j++) { if (i%h == h-1 && j%w == w-1) cout << -k-1 << " "; else if (i%h == 0 && j%w == 0) cout << k << " "; else cout << "0 "; } cout << "\n"; } } else { cout << "No\n"; } }
0
CPP
#include <bits/stdc++.h> using namespace std; int ans = 0; int i, j, n, m, k, z, t, x, y; int a[211111]; int const d = -1e9; int main() { memset(a, -1, sizeof a); scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d%d", &x, &y); if (a[y] + 1 < x) { printf("NO"); return 0; } else a[y] = max(a[y], x); } printf("YES"); }
8
CPP
for _ in range(int(input())): a, b, c, d, k = map(int, input().split()) x, y = (a + c - 1) // c, (b + d - 1) // d if x + y <= k: print(x, y) else: print(-1)
7
PYTHON3
a, b, x, y = [int(x) for x in input().split()] m = x n = y count = 0 if m < n: m, n = n, m while m % n != 0: m, n = n, m % n x = x // n y = y // n count = min(a//x, b//y) print(count)
8
PYTHON3
def sortSum(sum): sumList = sum.split('+') sortedSumList = sorted(sumList) return '+'.join(sortedSumList) sum = input() print(sortSum(sum))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("O3") const int mods[10] = {1000000007, 999999937, 999999929, 999999893, 999999883, 999999797, 999999761, 999999757, 999999751, 999999739}; const int mod = 1000000007; int rand_mods[2]; const long double eps = 1e-10; const int siz = 1e5 + 5, siz2 = (1 << 20) + 5, lg = 21, block = 448, block2 = 1000, mxv = 5e5, sqrt_mod = 31630; const int alpha_sz = 26; int n, p; pair<int, int> arr[siz]; long long ans[siz]; priority_queue<int, vector<int>, greater<int>> pq; bool done[siz]; struct qu { stack<pair<int, int>> s1, s2; int getmin() { return !s1.size() ? s2.top().second : (!s2.size() ? s1.top().second : min(s1.top().second, s2.top().second)); } void pretop() { if (s2.empty()) { while (!s1.empty()) { pair<int, int> v = s1.top(); s2.push( {v.first, !s2.size() ? v.first : min(v.first, s2.top().second)}); s1.pop(); } } } void pop() { pretop(); s2.pop(); } int top() { pretop(); return s2.top().first; } void add(int v) { s1.push({v, !s1.size() ? v : min(v, s1.top().second)}); } void clear() { while (!s1.empty()) { s1.pop(); } while (!s2.empty()) { s2.pop(); } } int size() { return s1.size() + s2.size(); } }; qu q; pair<int, int> tree[siz * 4]; pair<int, int> merge(pair<int, int> a, pair<int, int> b) { if (a.first < b.first) { return a; } if (b.first < a.first) { return b; } if (a.second < b.second) { return a; } return b; } void build(int node, int s, int e) { if (s == e) { tree[node] = {arr[s].first, s}; return; } int mid = (s + e) / 2; build(node * 2, s, mid); build(node * 2 + 1, mid + 1, e); tree[node] = merge(tree[node * 2], tree[node * 2 + 1]); } pair<int, int> query(int node, int s, int e, int l, int r) { if (s > r || e < l) { return {INT_MAX, 0}; } if (s >= l && e <= r) { return tree[node]; } int mid = (s + e) / 2; return merge(query(node * 2, s, mid, l, r), query(node * 2 + 1, mid + 1, e, l, r)); } void update(int node, int s, int e, int i) { if (s == e) { tree[node] = {INT_MAX, 0}; return; } int mid = (s + e) / 2; if (i <= mid) { update(node * 2, s, mid, i); } else { update(node * 2 + 1, mid + 1, e, i); } tree[node] = merge(tree[node * 2], tree[node * 2 + 1]); } int main() { scanf("%d%d", &n, &p); arr[0] = {INT_MAX, 0}; for (int i = 1; i <= n; i++) { scanf("%d", &arr[i]); arr[i].second = i; } build(1, 0, n); sort(arr + 1, arr + 1 + n); int ind = 1; long long cur = 0; for (int srvd = 0; srvd < n; srvd++) { int nxt; if (!q.size()) { while (!pq.empty() && done[pq.top()]) { pq.pop(); } if (!pq.empty()) { nxt = pq.top(); pq.pop(); cur += p; } else { pair<int, int> mnp = query(1, 0, n, 1, n); nxt = mnp.second; cur = mnp.first + p; } update(1, 0, n, nxt); } else { nxt = q.top(); q.pop(); cur += p; } while (ind <= n && arr[ind].first <= cur) { pq.push(arr[ind].second); ind++; } done[nxt] = 1; ans[nxt] = cur; int mn = nxt; if (q.size()) { mn = min(nxt, q.getmin()); } while (1) { pair<int, int> mnp = query(1, 0, n, 0, mn - 1); if (mnp.first == INT_MAX || mnp.first >= cur) { break; } q.add(mnp.second); update(1, 0, n, mnp.second); mn = mnp.second; } } for (int i = 1; i <= n; i++) { printf("%I64d ", ans[i]); } printf("\n"); return 0; }
9
CPP
for s in[*open(0)][1:]:n,k=map(int,s.split());k-=1;print(k//(n-1)*n+k%(n-1)+1)
9
PYTHON3