solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
#include <stdio.h> #include <iostream> #include <string> #include <sstream> #include <stack> #include <algorithm> #include <cmath> #include <queue> #include <map> #include <set> #include <cstdlib> #include <bitset> #include <tuple> #include <assert.h> #include <deque> #include <bitset> #include <iomanip> #include <limits> #include <chrono> #include <random> #include <array> #include <unordered_map> #include <functional> #include <complex> #include <numeric> #include <cctype> 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; } //constexpr long long MAX = 5100000; constexpr long long INF = 1LL << 60; constexpr int inf = 1000000007; constexpr long long mod = 1000000007LL; //constexpr long long mod = 998244353LL; const long double PI = acos((long double)(-1)); using namespace std; typedef unsigned long long ull; typedef long long ll; typedef long double ld; vector<int> topological(vector<vector<int>>& g) { vector<int> topo; int n = g.size(); vector<int> deg(n); for (int i = 0; i < n; i++) for (auto next : g[i]) deg[next]++; queue<int> q; for (int i = 0; i < n; i++) if (deg[i] == 0) q.push(i), topo.push_back(i); while (!q.empty()) { int cur = q.front(); q.pop(); for (auto next : g[cur]) { deg[next]--; if (deg[next] == 0) { q.emplace(next); topo.push_back(next); } } } return topo; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int m, n; cin >> m >> n; vector<vector<int>> g(m); vector<vector<int>> l(m, vector<int>(n)), r(m, vector<int>(n, inf)); for (int i = 0; i < m; i++) { int k; cin >> k; for (int j = 0; j < k; j++) { int s, t; string str; cin >> s >> str >> t; s--; if (str == ">=") { chmax(l[i][s], t); } else { chmin(r[i][s], t); } } for (int j = 0; j < n; j++) { if (l[i][j] > r[i][j]) { cout << "No" << "\n"; return 0; } } } for (int i = 0; i < m; i++) { for (int j = i + 1; j < m; j++) { bool lr = false; bool rl = false; for (int k = 0; k < n; k++) { if (r[i][k] < l[j][k]) { lr = true; } if (r[j][k] < l[i][k]) { rl = true; } } if (lr and rl) { cout << "No" << "\n"; return 0; } if (lr) g[i].emplace_back(j); if (rl) g[j].emplace_back(i); } } auto t = topological(g); if (t.size() == m) cout << "Yes" << "\n"; else cout << "No" << "\n"; }
0
CPP
p=[int(i) for i in input().split()] n,m=p[0],p[1] q=[int(j) for j in input().split()] if q[0]!=0: z=q[m-1] s=0 for k in range(n): if q[k]>=z and q[k]!=0: s+=1 print(s) else: print(0)
7
PYTHON3
N = int(input()) XL = [[int(i) for i in input().split()] for _ in range(N)] R = [(X - L, X + L) for X, L in XL] R.sort(key=lambda x: x[1]) ret = 0 rmax = -float('inf') for l, r in R : if l >= rmax : ret += 1 rmax = r print(ret)
0
PYTHON3
#include <bits/stdc++.h> int in[10100]; int main() { int n, c = 0, i, s = 0; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &in[i]); s += in[i]; } for (i = 0; i < n; i++) if ((s - in[i]) % 2 == 0) c++; printf("%d\n", c); }
7
CPP
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; struct Trie { const static int ALPHA_SIZE = 2; char base = 'a'; vector<vector<int> > g; vector<int> pref, words; int nxt = 1; int move_to(int u, int c) { if (g[u][c] == -1) { g[u][c] = nxt++; g.push_back(vector<int>(ALPHA_SIZE, -1)); pref.push_back(0); words.push_back(0); } pref[g[u][c]]++; return g[u][c]; } public: Trie() { g.push_back(vector<int>(ALPHA_SIZE, -1)); pref.push_back(0); words.push_back(0); } void addBit(int a) { int u = 0; for (int i = 31; i >= 0; i--) u = move_to(u, bool(a & (1ll << i))); words[u]++; } int minXor(int u) { int ans = 0, v = 0; for (int i = 31; i >= 0; i--) { ans *= 2; if (u & (1ll << i) && g[v][1] != -1) v = g[v][1]; else if (!(u & (1ll << i)) && g[v][0] != -1) v = g[v][0]; else if (~g[v][0]) v = g[v][0], ans++; else v = g[v][1], ans++; } return ans; } int calc(int u = 0) { if (u == -1) return 0; int ans1 = calc(g[u][0]), ans2 = calc(g[u][1]); return max(max(ans1, ans2) + min(1, min(ans1, ans2)), 1); } }; int main() { ios::sync_with_stdio(false); cin.tie(0); int T = 1, n; while (T--) { cin >> n; Trie trie = Trie(); for (int i = 0; i < n; ++i) { int aux; cin >> aux; trie.addBit(aux); } cout << n - trie.calc() << "\n"; } }
9
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; template <class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; } template <class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k; cin >> n >> k; string s; while (int((s).size()) < n) { for (auto i = (0); i < ((n - k) / 2); ++i) s += "1"; s += "0"; } while (int((s).size()) > n) s.pop_back(); cout << s << '\n'; }
8
CPP
#include <bits/stdc++.h> int main() { int n = 0, x = 0, c = 0, i = 0; scanf("%d%d", &n, &x); for (i = 1; i <= n; ++i) { if (x % i == 0 && x / i <= n) { c += 1; } } printf("%d\n", c); return 0; }
7
CPP
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; int main(){ int n; cin>>n; int ans=0,cnt1=0,cnt2=0,cnt3=0; rep(i,n){ string s; cin>>s; int len=s.length(); rep(j,len-1) if(s[j]=='A' && s[j+1]=='B') ans++; if(s[0]=='B' && s[len-1]=='A') cnt1++; else if(s[ 0 ]=='B') cnt2++; else if(s[len-1]=='A') cnt3++; } ans+=min(cnt2,cnt3); if(cnt2+cnt3>0) ans+=cnt1; else ans+=max(cnt1-1,0); printf("%d\n",ans); return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int n, k; char l[100005], r[100005]; bool vis_left[100005], vis_right[100005]; bool bfs(int u, int cw, bool lf) { queue<pair<int, pair<int, bool> > > q; q.push(make_pair(u, make_pair(cw, lf))); vis_left[1] = 1; while (!q.empty()) { int x = q.front().first; int y = q.front().second.first; bool f = q.front().second.second; q.pop(); if (x + k > n || x + 1 > n) { return 1; } if (f) { if (!vis_right[x + 1] && r[x + 1] == '-' && x + 1 > y + 1) { q.push(make_pair(x + 1, make_pair(y + 1, f))), vis_right[x + 1] = 1; } if (x > 1 && x - 1 > y + 1 && !vis_right[x - 1] && r[x - 1] == '-') { q.push(make_pair(x - 1, make_pair(y + 1, f))), vis_right[x - 1] = 1; } if (x + k <= n && !vis_left[x + k] && l[x + k] == '-' && x + k > y + 1) { q.push(make_pair(x + k, make_pair(y + 1, 0))), vis_left[x + k] = 1; } } else { if (!vis_left[x + 1] && l[x + 1] == '-' && x + 1 > y + 1) { q.push(make_pair(x + 1, make_pair(y + 1, f))), vis_left[x + 1] = 1; } if (x > 1 && x - 1 > y + 1 && !vis_left[x - 1] && l[x - 1] == '-') { q.push(make_pair(x - 1, make_pair(y + 1, f))), vis_left[x - 1] = 1; } if (x + k <= n && !vis_right[x + k] && r[x + k] == '-' && x + k > y + 1) { q.push(make_pair(x + k, make_pair(y + 1, 1))), vis_right[x + k] = 1; } } } return 0; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> l[i]; } for (int i = 1; i <= n; ++i) { cin >> r[i]; } cout << (bfs(1, 0, 0) ? "YES" : "NO"); return 0; }
8
CPP
# Bear and Big Brother(405.2) a,b=map(int,input().split()) c=0 while True: a*=3 b*=2 c+=1 if a>b: break print(c)
7
PYTHON3
import sys input = sys.stdin.readline def main(): n,m,k=map(int,input().split()) ab=[list(map(int,input().split())) for _ in range(m)] cd=[list(map(int,input().split())) for _ in range(k)] gf=[[]for _ in range(n)] for a,b in ab: gf[a-1].append(b-1) gf[b-1].append(a-1) gb=[[]for _ in range(n)] for c,d in cd: gb[c-1].append(d-1) gb[d-1].append(c-1) seen=[0]*n ans=[0]*n for i in range(n): c=set([i]) if seen[i]==0: todo=[i] while todo: e=todo.pop() l=gf[e] for li in l: if seen[li]==0: todo.append(li) seen[li]=1 c.add(li) base=len(c)-1 for ci in c: ans[ci]=base for fb in gb[ci]+gf[ci]: if fb in c: ans[ci]-=1 print(' '.join(map(str,ans))) if __name__=='__main__': main()
0
PYTHON3
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; const int INF = 1 << 25; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, m, query; while (cin >> n >> m , n) { int cost[101][101], time[101][101]; for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { cost[i][j] = (i == j) ? 0 : INF; time[i][j] = (i == j) ? 0 : INF; } } while (n--) { int a, b, c, t; cin >> a >> b >> c >> t; cost[a - 1][b - 1] = cost[b - 1][a - 1] = c; time[a - 1][b - 1] = time[b - 1][a - 1] = t; } for (int k = 0; k < m; k++) { for (int i = 0; i < m; i++) { for (int j = 0; j < m; j++) { cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]); time[i][j] = min(time[i][j], time[i][k] + time[k][j]); } } } cin >> query; while (query--) { int p, q, r; cin >> p >> q >> r; if (r == 0) { cout << cost[p - 1][q - 1] << endl; } else { cout << time[p - 1][q - 1] << endl; } } } }
0
CPP
#include <bits/stdc++.h> using namespace std; long long read() { long long sum = 0; char c = getchar(); bool f = 0; while (c < '0' || c > '9') { if (c == '-') f = 1; c = getchar(); } while (c >= '0' && c <= '9') { sum = sum * 10 + c - '0'; c = getchar(); } if (f) return -sum; return sum; } const int N = 200005; int n, m, P[N], Q[N], pos[N], L[N], bel[N]; char ans[N]; int main() { int i, j, k; n = read(); m = read(); for (i = 1; i <= n; i++) P[i] = read(), pos[P[i]] = i; for (i = 1; i <= n; i++) Q[i] = read(); for (k = n, i = n; i >= 1; i--) j = pos[Q[i]], k = min(k, j), L[j] = k; for (j = 1, k = n, i = n; i >= 1; i--) { k = min(k, L[i]); bel[i] = j; if (k == i) j++; } if (bel[1] < m) { puts("NO"); return 0; } puts("YES"); for (j = -1, i = 1; i <= n; i++) { if (bel[i] != bel[i - 1]) j++; ans[P[i]] = 'a' + min(25, j); } for (i = 1; i <= n; i++) printf("%c", ans[i]); return 0; }
12
CPP
n=int(input()) l=[int(num) for num in input().split()] m,i,p,c=0,0,0,0 while(i<n-1): if(l[i]!=l[i+1]): j,k,c,p=i-1,i+2,1,1 while(j>=0): if(l[j]==l[i]): c,j=c+1,j-1 else: break while(k<n): if(l[k]==l[i+1]): p,k=p+1,k+1 else: break m=max(m,min(p,c)) i=i+1 print(2*m)
7
PYTHON3
#include<stdio.h> #include<iostream> #include<algorithm> #include<stdlib.h> using namespace std; const int maxn=500100; int t[maxn],a[maxn]; int c=0; void merge_sort(int *a,int x,int y,int *t){ if(y-x>1){ int m=(x+y)/2; int p=x,q=m,i=x; merge_sort(a,x,m,t); merge_sort(a,m,y,t); while(p<m || q<y){ if(q>=y || (p<m && a[p]<=a[q])) { c++; t[i++]=a[p++]; } else { c++; t[i++]=a[q++]; } } for(i = x;i<y;i++) a[i]=t[i]; } } int main() { int n; while(scanf("%d",&n)!=EOF){ for(int i=0;i<n;i++) { scanf("%d",&a[i]); } int l=0,r=n; merge_sort(a,l,r,t); for(int i=0;i<n;i++) { if(i!=0) printf(" "); printf("%d",a[i]); } printf("\n"); printf("%d\n",c); } return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int a,b,c; signed main(){ scanf("%d%d%d",&a,&b,&c); printf("%d",c/a*b); }
0
CPP
import math import heapq def main(): n, m = map(int,input().split()) adj = [[] for i in range(n)] for i in range(m): a,b,w = map(int,input().split()) adj[a-1].append((b-1,w)) adj[b-1].append((a-1,w)) def djikstra(x,end): n=len(adj) visited = [False] * n path = [] distances = [math.inf] * n distances[x] = 0 pre=[-1]*n pq = list() heapq.heapify(pq) heapq.heappush(pq,(0,x)) # (-d,x) where d is distances while(pq): distance,node = heapq.heappop(pq) #take the node not the distance for next in adj[node]: el = next[0] w = next[1] if distances[node] + w < distances[el]: pre[el]=node distances[el] = distances[node] + w heapq.heappush(pq,(distances[el],el)) if pre[end]==-1: print("-1") else: v=end while(v != -1): path.append(pre[v]) v = pre[v] path.reverse() path.append(end) path = [str(el+1) for el in path if el!= -1] print(" ".join(path)) djikstra(0,n-1) if __name__ == '__main__': main()
9
PYTHON3
def ras(n): if(n%2==0): if(n%4==0): return(4) elif(n%4==1): return(1) elif(n%4==2): return(0) else: return(3) else: return(0) n=int(input()) r=ras(n) print(r)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; template <typename T> using V = vector<T>; int gcdex(int a, int b, int &x, int &y) { if (a == 0) { x = 0; y = 1; return b; } int x1, y1; int g = gcdex(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return g; } inline int ADD_MOD(int a, int b) { return (a + b) % 1000000007; } inline int MUL_MOD(int a, int b) { return (int64_t(a) * b) % 1000000007; } inline int SUB_MOD(int a, int b) { return a >= b ? a - b : a + 1000000007 - b; } int DIV_MOD(int a, int b) { int x, y; gcdex(b, 1000000007, x, y); int b1 = (x % 1000000007 + 1000000007) % 1000000007; return MUL_MOD(a, b1); } const long double EPS = 1. / 1e6; inline bool EPS_EQUAL(long double a, long double b) { return abs(a - b) <= EPS; } inline bool EPS_LESS(long double a, long double b) { return b - a > EPS; } inline bool EPS_GREATER(long double a, long double b) { return a - b > EPS; } const int INF = 1e9; int dp[2][150][150 * 150 / 2 + 1] = {0}; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.precision(15); int n, k, s; cin >> n >> k >> s; V<int> a(n); for (auto &i : a) { cin >> i; } int cur = 0; int next = 1; s = min(s + 1, n * n / 2 + 1); for (int i = 0; i < k; ++i) { for (int j = 0; j < s; ++j) { dp[next][i][j] = INF; } dp[next][i][0] = (i > 0 ? dp[cur][i - 1][0] + a[i] : a[i]); for (int j = i + 1; j < n; ++j) { int cost = j - i; for (int l = 0; l < s; ++l) { int prevSum = (l >= cost ? dp[cur][j - 1][l - cost] + a[j] : INF); dp[next][j][l] = min(dp[next][j - 1][l], prevSum); } } swap(cur, next); } int minSum = INF; for (int i = k - 1; i < n; ++i) { for (int j = 0; j < s; ++j) { minSum = min(minSum, dp[cur][i][j]); } } cout << minSum << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int c[30]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s; int len, l, r, ds, qs; cin >> s, len = s.size(); ds = qs = 0; if (len < 26) cout << -1 << endl; else { for (int i = 0; i < 25; i++) { if (s[i] == '?') qs++; else { if (!c[s[i] - 'A']) ds++; c[s[i] - 'A']++; } } l = 0, r = 25; bool f = 0; while (l <= r && r < len) { if (s[r] == '?') qs++; else { if (!c[s[r] - 'A']) ds++; c[s[r] - 'A']++; } if (qs + ds == 26) { f = 1; break; } if (s[l] == '?') qs--; else { if (c[s[l] - 'A'] == 1) ds--; c[s[l] - 'A']--; } l++, r++; } if (f) { for (int i = l; i <= r; i++) { if (s[i] == '?') { for (int j = 0; j < 26; j++) { if (!c[j]) { c[j]++, s[i] = (j + 'A'); break; } } } } for (int i = 0; i < len; i++) { if (s[i] == '?') s[i] = 'A'; } } if (f) cout << s << endl; else cout << -1 << endl; } return 0; }
8
CPP
N, K = list(map(int, input().split())) A = list(map(int, input().split())) C = 10**9+7 dp = [[0]*(K+1) for _ in range(N)] dp[0] = [1]*(A[0]+1)+[0]*(K-A[0]) csum = [[0]*(K+2) for _ in range(N)] # l=max(0,j[A[i]])ใฎใจใ # dp[i][j]=sum(dp[i][l,j+1])ใจใ—ใŸใ„ใŒO(NK**2)ใง้–“ใซๅˆใ‚ใชใ„ # ->ๅ’Œใ‚’ๅ–ใ‚‹ๆ“ไฝœใ‚’้ซ˜้€ŸๅŒ–ใ™ใ‚‹ใŸใ‚ใซ็ดฏ็ฉๅ’Œใฎใƒ†ใƒผใƒ–ใƒซใ‚’ๆบ–ๅ‚™ใ™ใ‚‹ for i in range(1, N): for j in range(K+1): csum[i-1][j+1] = (csum[i-1][j]+dp[i-1][j]) % C l = max(0, j-A[i]) dp[i][j] = (csum[i-1][j+1]-csum[i-1][l]) % C print(dp[-1][-1])
0
PYTHON3
def main(): a, b, c, d = map(int, input().split()) print(max(a + b, c + d)) if __name__ == '__main__': t = int(input()) for i in range(t): main()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; string uni = "abcdefghijklmnopqrstuvwxyz"; bool vis[100000] = {0}; bool check(long long int arr[], long long int k, long long int h) { long long int sum = 0; for (long long int i = 0; i <= k; i += 2) sum += arr[i]; return sum <= h; } signed main() { long long int n; cin >> n; long long int arr[n]; for (long long int i = 0; i < n; i++) cin >> arr[i]; long long int t = INT_MAX; for (long long int i = 0; i < n; i++) t = min(t, arr[i] / n); for (long long int i = 0; i < n; i++) { arr[i] -= t * n; } for (long long int i = 0; i < 2 * n; i++) { if (arr[i % n] - (i) <= 0) { cout << i % n + 1; return 0; } } }
8
CPP
x1,y1 = map(int,input().split()) x2,y2 = map(int,input().split()) if (x1 == x2) or (y1 == y2):r=2 else:r=0 print(((abs(x1-x2) + 1)*2) + ((abs(y1-y2) + 1)*2) + r)
20
PYTHON3
da,db=map(int, input().split()) if da == db: print(da*10+1, db*10+2) elif db-da == 1: print(da*10+9,db*10) elif da == 9 and db == 1: print(99, 100) else: print(-1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 100100; int a[N]; int dp[N]; vector <int> adj[N]; int dfs(int a) { for (int u: adj[a]) { dfs(u); } vector <int> v; if(adj[a].size() == 0) return dp[a] = 0; for (int u: adj[a]) { v.push_back(dp[u]); } sort(v.begin(),v.end()); reverse(v.begin(), v.end()); dp[a] = 0; for (int i = 0; i < adj[a].size(); ++i) { dp[a] = max(dp[a],v[i]+i+1); } return dp[a]; } int main() { int n; cin >> n; for (int i = 1; i < n; ++i) { cin >> a[i]; a[i]--; adj[a[i]].push_back(i); } printf("%d\n",dfs(0)); }
0
CPP
# n = int(input()) # arr = list(map(int,input().split())) # k,d = map(int,input().split()) # n = int(input()) import sys import math def fun(): n = int(input()) arr = list(map(int, input().split())) enter_list = input() width_list = [[i+1,arr[i]] for i in range(n)] width_list.sort(key=lambda x:x[-1]) j = 0 seated_list = [width_list[0]] all_list = [width_list[0][0]] # print(width_list[j][0], end='') j += 1 for c in enter_list[1:]: if c == '0': seated_list.append(width_list[j]) # print("", width_list[j][0], end='') all_list.append(width_list[j][0]) j += 1 else: # print("", seated_list[-1][0], end='') all_list.append(seated_list[-1][0]) seated_list.pop() all_list = [str(i) for i in all_list] print(' '.join(all_list)) if __name__ == "__main__": fun() # print(str)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAX = 30; vector<int> g[MAX], rg[MAX]; bool vis[MAX], cycle, possible = true; vector<int> ans; int col[MAX]; void dfs(int u) { vis[u] = true; for (int v : g[u]) { if (!vis[v]) dfs(v); } ans.push_back(u); } void dfs1(int u) { col[u] = 1; for (int v : g[u]) { if (col[v] == 2) continue; if (!col[v]) dfs1(v); else cycle = true; } col[u] = 2; } void addEdge(string a, string b) { int l = min((int)a.size(), (int)b.size()); int i = 0; while (i < l && a[i] == b[i]) i++; if (i == l) { if ((int)a.size() > (int)b.size()) possible = false; return; } g[a[i] - 'a'].push_back(b[i] - 'a'); rg[b[i] - 'a'].push_back(a[i] - 'a'); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; vector<string> mp[n]; for (int i = 0; i < n; i++) { int x; cin >> x; vector<string> v(k); for (auto &y : v) cin >> y; mp[x] = v; } if (n == 1 && k == 1) { vector<bool> used(26, false); for (char c : mp[0][0]) { if (used[c - 'a']) continue; cout << c; used[c - 'a'] = true; } return 0; } for (int i = 0; i < n; i++) { if (i < n - 1) addEdge(mp[i][k - 1], mp[i + 1][0]); for (int x = 0; x < k - 1; x++) addEdge(mp[i][x], mp[i][x + 1]); } for (int i = 0; i < 26; i++) { if (!col[i]) dfs1(i); } if (cycle || !possible) return cout << "IMPOSSIBLE", 0; for (int i = 0; i < 26; i++) { if ((int)g[i].size() == 0 && (int)rg[i].size() == 0) continue; if (!vis[i]) dfs(i); } reverse(ans.begin(), ans.end()); for (int x : ans) cout << char('a' + x); return 0; }
20
CPP
t = int(input()) while t: a,b=map(int,input().split()) c, d = map(int, input().split()) if a==b or c==d: print("NO") elif a==c or a==d or b==c or b==d: flag=0 if a==c and b+d==c: flag=1 if a==d and b+c==d: flag=1 if b==c and a+d==c: flag=1 if b==d and a+c==d: flag=1 if flag==1: print("YES") else: print("NO") else: print("NO") t-=1
8
PYTHON3
import bisect n,k,q = map(int,input().split()) a = list(map(int,input().split())) if q == 1: print(0) exit() ls = [-1,n] b = sorted(((a[i],i) for i in range(n))) ansls = [b[q-1][0]-b[0][0]] space = [n] qc = q for i,x in enumerate(b): for j in range(i,n): idx = bisect.bisect_left(ls,b[j][1]) if space[idx-1] < k: continue qc -= 1 space[idx-1] -= 1 if qc == 0: ansls.append(b[j][0]-x[0]) break ins = bisect.bisect_left(ls,x[1]) ls.insert(ins,x[1]) space = [] qc = q for j in range(1,i+3): space.append(ls[j]-ls[j-1]-1) print(min(ansls))
0
PYTHON3
#include <bits/stdc++.h> using namespace std; long long int MOD = 998244353; long long int mod = 1e9 + 7; long long int INF = 1e18; long long int dx[] = {1, -1, 0, 0}; long long int dy[] = {0, 0, 1, -1}; long long int a[100009] = {0}; long long int sum[100009]; long long int par[100009]; bool dfs(vector<long long int> adj[], long long int x, long long int h, long long int s) { if (h % 2 == 1) { a[x] = sum[x] - s; s = sum[x]; } if (a[x] < 0) return false; long long int m = INF; for (auto j : adj[x]) { if (j != par[x]) { par[j] = x; bool flag = dfs(adj, j, h + 1, s); if (!flag) return false; m = min(a[j], m); } } if (m != INF && h % 2 == 0) { a[x] += m; for (auto j : adj[x]) { if (j != par[x]) a[j] -= m; } } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n; cin >> n; vector<long long int> adj[n + 1]; for (int i = 2; i <= n; i++) { long long int x; cin >> x; adj[x].push_back(i); adj[i].push_back(x); } for (int i = 1; i <= n; i++) cin >> sum[i]; bool flag = dfs(adj, 1, 1, 0); if (!flag) cout << -1; else { long long int ans = 0; for (int i = 1; i <= n; i++) ans += a[i]; cout << ans; } return 0; }
7
CPP
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <cmath> #include <map> #include <queue> #include <iomanip> #include <set> #include <tuple> #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) using namespace std; typedef long long ll; const ll MOD=998244353; ll mod_pow(ll x,ll n){ x%=MOD; ll res=1; while(n>0){ if(n&1) res=res*x%MOD; x=x*x%MOD; n/=2; } return res; } void add(ll &a,ll b){ a=(a+b)%MOD; } void mul(ll &a,ll b){ a=a*b%MOD; } int N; vector<ll> X; int main(){ cin>>N; X.resize(N); rep(i,N) cin>>X[i]; ll ans=0; for(int i=0;i<N;i++){ ll res=0; res=mod_pow(X[i]+1,i); mul(res,X[i]); mul(res,mod_pow(2ll,N-(i+1))); add(ans,res); } cout<<ans<<endl; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; struct point { long double x, y; point() { x = 0; y = 0; } point(long long a, long long b) { x = a; y = b; } bool operator==(const point &b) { return x == b.x and y == b.y; } bool operator>(const point &b) { if (x > b.x) return true; if (x < b.x) return false; if (x == b.x) { if (y < b.y) return false; if (y > b.y) return true; return true; } } bool operator<(point &b) { return (b > point(x, y)); } point(long double x1, long double y1) { x = x1; y = y1; } long double dist(point b) { return sqrt((b.x - x) * (b.x - x) + (y - b.y) * (y - b.y)); } long double dist_kv(point b) { return ((b.x - x) * (b.x - x) + (y - b.y) * (y - b.y)); } bool point_on_sig(point &a, point &b) { return abs(a.dist(b) - (dist(a) + dist(b))) <= 1e-6; } long double polar_angle() { long double alpha = atan2(y, x); long double pi = 3.141592654; if (alpha < 0) alpha += 2 * pi; return alpha; } }; struct line { long double A, B, C; line() { A = 0; B = 0; C = 0; } line(point &a, point &b) { A = a.y - b.y; B = b.x - a.x; C = -A * a.x - B * a.y; } line(long double a, long double b, long double c) { A = a; B = b; C = c; } line line_perp_dan_cherez_point(point a) { return line(-B, A, -(A * a.y) + B * a.x); } bool onli(point a) { return A * a.x + B * a.y + C == 0; } point intersect_lines(line &a) { point p; p.x = -(((C * a.B) - (a.C * B)) / ((A * a.B) - (a.A * B))); p.y = -(((A * a.C) - (a.A * C)) / ((A * a.B) - (a.A * B))); return p; } bool intersect(line &b) { return A * b.B - B * b.A != 0; } bool operator==(line &a) { return A == a.A && B == a.B && C == a.C; } long double distance_perpendicular(point &d) { return abs(A * d.x + B * d.y + C) / (sqrt(A * A + B * B)); } }; struct vc { long double x, y; vc() { x = 0; y = 0; } vc(long double x_, long double y_) { x = x_; y = y_; } vc(const point &a) { x = a.x; y = a.y; } vc minus(const vc &b) { return vc(x - b.x, y - b.y); } long double len() { return sqrt(x * x + y * y); } long double scalar(const vc &b) { return x * b.x + y * b.y; } long double vector(const vc &b) { return x * b.y - y * b.x; } long double angle(const vc &b) { return atan2(vector(b), scalar(b)); } }; vector<int> top; void top_sort(int v, vector<int> nodes[], int used[], int colors[]) { if (colors[v] == 1) { cout << -1; exit(0); } else if (colors[v] == 0) colors[v] = 1; for (int i = 0; i < nodes[v].size(); ++i) { if (used[nodes[v][i]] == 0) top_sort(nodes[v][i], nodes, used, colors); } top.push_back(v + 1); used[v] = 1; colors[v] = 2; } vector<int> z_function(string s) { vector<int> z(s.size()); for (int i = 1, l = 0, r = 0; i < s.size(); ++i) { if (i <= r) z[i] = min(r - i + 1, z[i - l]); while (i + z[i] < s.size() && s[z[i]] == s[i + z[i]]) ++z[i]; if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; } return z; } vector<int> p_function(string s) { vector<int> p(s.size()); for (int i = 1; i < s.size(); ++i) { int j = p[i - 1]; while (j > 0 && s[i] != s[j]) j = p[j - 1]; if (s[i] == s[j]) ++j; p[i] = j; } return p; } vector<int> now_ans; void dfsik(int v, vector<int> nodes[], int colors[], int turn) { if (colors[v] == 1) { now_ans.push_back(0); return; } else if (colors[v] == 0) colors[v] = 1; for (int i = 0; i < nodes[v].size(); ++i) { if (turn == 1) dfsik(nodes[v][i], nodes, colors, 2); else dfsik(nodes[v][i], nodes, colors, 1); } if (nodes[v].empty()) { now_ans.push_back(turn); return; } colors[v] = 2; } long long INF = (long long)1e20; int int_INF = (int)1e20; long long t[8 * 100000]; long long ad[8 * 100000]; bool us[8 * 100000]; void build(long long v, long long l, long long r, vector<long long> &a) { if (l == r - 1) { t[v] = a[l]; return; } long long mid = (l + r) / 2; build(v * 2, l, mid, a); build(v * 2 + 1, mid, r, a); t[v] = min(t[2 * v], t[2 * v + 1]); } long long mini(long long v, long long l, long long r, long long lf, long long rf) { if (lf <= l and rf >= r) { return t[v]; } if (l >= rf or r <= lf) { return INF; } long long mid = (l + r) / 2; return min(mini(v * 2, l, mid, lf, rf), mini(v * 2 + 1, mid, r, lf, rf)); } void push(long long v, long long l, long long r) { if (l == r - 1) { ad[2 * v] += ad[v]; ad[2 * v + 1] += ad[v]; } t[v] += ad[v]; ad[v] = 0; } void push_s(long long v, long long l, long long r) { if (!us[v]) return; if (l == r - 1) { ad[2 * v] += ad[v]; ad[2 * v + 1] += ad[v]; } t[v] = ad[v]; ad[v] = 0; } void add(long long v, long long l, long long r, long long lf, long long rf, long long x) { push(v, l, r); if (l >= lf and r <= rf) { ad[v] += x; push(v, l, r); return; } if (l >= rf or r <= lf) return; long long mid = (l + r) / 2; add(2 * v, l, mid, lf, rf, x); add(2 * v + 1, mid, r, lf, rf, x); t[v] = min(t[2 * v], t[2 * v + 1]); } void sett(long long v, long long l, long long r, long long lf, long long rf, long long x) { push_s(v, l, r); if (l >= lf and r <= rf) { ad[v] += x; push_s(v, l, r); return; } if (l >= rf or r <= lf) return; long long mid = (l + r) / 2; add(2 * v, l, mid, lf, rf, x); add(2 * v + 1, mid, r, lf, rf, x); t[v] = min(t[2 * v], t[2 * v + 1]); } int parents[500000]; void create(int v) { parents[v] = v; } int find_parent(int v) { if (v == parents[v]) return v; return parents[v] = find_parent(parents[v]); } void join(int a, int b) { int x = find_parent(a); int y = find_parent(b); if (x != y) { parents[x] = y; } } vector<long long> manac(string s, int shift) { long long l = 0; long long r = -1; long long n = s.size(); vector<long long> ans(n); for (int i = 1; i < n; i++) { long long k = 0; if (i <= r) { k = min(r - i + shift, ans[r - i + l + shift]); } while (i - k - 1 >= 0 && i + k + 1 - shift < n && s[i - k - 1] == s[i + k + 1 - shift]) { k++; } if (i + k - shift > r) { l = i - k; r = i + k - shift; } ans[i] = k; } return ans; } vector<int> dividers(int x) { vector<int> ans; ans.push_back(x); for (int i = 2; i <= sqrt(x); ++i) { if (x % i == 0) { ans.push_back(i); if (i != sqrt(x)) ans.push_back(x / i); } } return ans; } long long max_arr(long long arr[], long long n) { long long maxi = -INF; for (int i = 0; i < n; ++i) maxi = max(maxi, arr[i]); return maxi; } long long min_arr(long long arr[], long long n) { long long maxi = INF; for (int i = 0; i < n; ++i) maxi = min(maxi, arr[i]); return maxi; } struct triple { long long a, b, c; triple(long long x, long long y, long long z) { a = x; b = y; c = z; } triple() { a = 0; b = 0; c = 0; } bool operator<(const triple *x) { return a < x->a; } bool operator==(const triple *x) { return a == x->a and b == x->b and c == x->c; } bool operator>(const triple *x) { return a > x->a; } }; bool f(int a, int i) { return ((a >> i) & 1); } long long mex(vector<long long> &kek) { vector<long long> cnt(kek.size() + 100); for (int i = 0; i < kek.size(); ++i) { ++cnt[kek[i]]; } for (int i = 0; i < cnt.size(); ++i) { if (cnt[i] == 0) return i; } } int main() { long long a, b; cin >> a >> b; long long l = 0, r = a + b, ans = -1; while (l <= r) { long long m = (l + r) / 2; long long now1 = a, now2 = b, now = 0; bool can = true; for (long long i = m; i > 0; --i) { if (max(now1, now2) < i) { can = false; break; } if (now1 > now2) { now1 -= i; } else now2 -= i; ++now; } if (can) { ans = max(ans, now); l = m + 1; } else r = m - 1; } long long now1 = a, now2 = b, now = 0; vector<long long> f, s; bool can = true; for (long long i = ans; i > 0; --i) { if (max(now1, now2) < i) { can = false; break; } if (now1 > now2) { f.push_back(i); now1 -= i; } else { s.push_back(i); now2 -= i; } ++now; } cout << f.size() << '\n'; for (int i = 0; i < f.size(); ++i) cout << f[i] << ' '; cout << '\n'; cout << s.size() << '\n'; for (int i = 0; i < s.size(); ++i) cout << s[i] << ' '; }
9
CPP
dp=[0]*4001 for i in range(4001): if i<1000: dp[i]=i+1 elif i>2000: dp[i]=0 elif i>=1000: dp[i]=2001-i while 1: try: ans=0 n=int(input()) for i in range(n+1): ans+=dp[i]*dp[n-i] print(ans) except:break
0
PYTHON3
n = int(input()) for i in range(n): t = int(input()) t = str(bin(t)) t = t[t.index('b') + 1:] answer = 1 for j in t: if(j == '1'): answer *= 2 print(answer) #print(t)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 2e6 + 20; long long a[N], now, ans, s[N]; vector<long long> gr[N]; void root(int v = 1, int h = 0, int par = -1) { now += h * a[v]; s[v] = a[v]; for (int u : gr[v]) if (u != par) root(u, h + 1, v), s[v] += s[u]; } void upd(int v = 1, int par = -1) { ans = max(ans, now); for (int u : gr[v]) { long long tmp = now; if (u != par) { now += s[1] - 2 * s[u], upd(u, v); } now = tmp; } } int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n - 1; i++) { int x, y; cin >> x >> y; gr[--x].push_back(--y); gr[y].push_back(x); } root(); upd(); cout << ans; }
12
CPP
a, b = map(int, input().split()) y = 0 while a <= b: a, b = 3*a, 2*b y += 1 print(y)
7
PYTHON3
n = int(input()) k=(n*2)-1 o=[] z=[] q=-1 for i in range(0,n+1): for j in range(0,i): o.append(j) for w in range(i-2,-1,-1): o.append(w) if len(o)>0: if k>0: print(k*" ",*o) k-=2 o=[] else: print(*o) for p in range(n+1,0,-1): for m in range(0,p): z.append(m) for w in range(p-2,-1,-1): z.append(w) if q==-1: print(*z) q+=2 z=[] else: print(q*" ",*z) q+=2 z=[]
8
PYTHON3
n = int(input()) solved = 0 for i in range(n): inp = list(map(int, input().strip().split(' '))) count = 0 for j in range(len(inp)): count += inp[j] if count >= 2: solved += 1 print(solved)
7
PYTHON3
""" Greedy solution where if we are looking at a, then move b to front as much as possible """ def solve(s): res = [0 for _ in range(len(s))] for i in range(1, len(s)): if s[i] == "a": res[i] = 1 res[i-1] = 1 - res[i-1] return res lst = list((input())) print ((" ").join(list(map(str, solve(lst)))))
9
PYTHON3
#include <bits/stdc++.h> using namespace std; int l, r; int main() { scanf("%d%d", &l, &r); if (l == r) printf("%d\n", l); else puts("2"); return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string x; int pnt = 0; int a[100000 + 10]; for (int i = 0; i < n; i++) { cin >> x; int id = (x[0] - '0') * 10 + x[1] - '0'; id = id * 60; int id2 = (x[3] - '0') * 10 + x[4] - '0'; id += id2; a[i] = id; } sort(a, a + n); int ans = 0; for (int i = 0; i < n - 1; i++) { ans = max(ans, a[i + 1] - a[i] - 1); } ans = max(ans, 24 * 60 - (a[n - 1] - a[0] + 1)); int h = ans / 60; int m = ans % 60; if (h < 10) cout << "0"; cout << h; cout << ":"; if (m < 10) cout << "0"; cout << m; }
15
CPP
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy sys.setrecursionlimit(10**7) inf=10**20 mod=10**9+7 dd=[(-1,0),(0,1),(1,0),(0,-1)] ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)] def LI(): return [int(x) for x in sys.stdin.readline().split()] # def LF(): return [float(x) for x in sys.stdin.readline().split()] def I(): return int(sys.stdin.readline()) def F(): return float(sys.stdin.readline()) def LS(): return sys.stdin.readline().split() def S(): return input() # aใ€œz -- START -- alf=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] # aใ€œz --- END --- def main(): n,k=LI() d={} for x in alf: d[x]=x s=S() for _ in range(k): a,b=LS() for k,v in d.items(): if v==a: d[k]=b if v==b: d[k]=a ans='' for x in s: ans+=d[x] return ans # main() print(main())
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int b[1000005]; int main() { int i, x, n, mx = 0; scanf("%d", &n); memset(b, 0, sizeof b); for (i = 0; i < n; i++) { scanf("%d", &x); mx = max(mx, x); b[x]++; } int r, c = 0; for (i = 0; i <= mx; i++) { r = b[i] / 2; b[i] %= 2; b[i + 1] += r; if (b[i] == 1) c++; } r = b[mx + 1]; for (i = 0; i < 31; i++) if (r & (1 << i)) c++; printf("%d\n", c); }
9
CPP
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, st, lt, v, ele, scost, lcost, lows, lowl; vector<long long> stair, lift; vector<long long>::iterator it; cin >> n >> m >> st >> lt >> v; for (int i = 0; i < st; i++) { cin >> ele; stair.push_back(ele); } for (int i = 0; i < lt; i++) { cin >> ele; lift.push_back(ele); } sort(stair.begin(), stair.end()); sort(lift.begin(), lift.end()); long long q, a, b, c, d; cin >> q; while (q--) { cin >> a >> b >> c >> d; if (a == c) { cout << abs(b - d) << '\n'; continue; } scost = INT_MAX; if (stair.size()) { it = lower_bound(stair.begin(), stair.end(), b); lows = it - stair.begin(); if (lows) lows--; if (lows < stair.size() - 1) { scost = min(abs(b - stair[lows]) + abs(d - stair[lows]), abs(b - stair[lows + 1]) + abs(d - stair[lows + 1])) + abs(c - a); } else { scost = abs(b - stair[lows]) + abs(d - stair[lows]) + abs(c - a); } } lcost = INT_MAX; if (lift.size()) { it = lower_bound(lift.begin(), lift.end(), b); lowl = it - lift.begin(); if (lowl) lowl--; if (lowl < lift.size() - 1) { lcost = min(abs(b - lift[lowl]) + abs(d - lift[lowl]), abs(b - lift[lowl + 1]) + abs(d - lift[lowl + 1])) + (int)(ceil((double)abs(c - a) / v)); } else { lcost = abs(b - lift[lowl]) + abs(d - lift[lowl]) + (int)(ceil((double)abs(c - a) / v)); } } cout << min(lcost, scost) << "\n"; } return 0; }
9
CPP
# https://codeforces.com/contest/363/problem/B def single_integer(): return int(input()) def multi_integer(): return map(int, input().split()) def string(): return input() def multi_string(): return input().split() from collections import deque n, k = multi_integer() d = deque() fence = tuple(multi_integer()) min_sum = 0 for i in range(k): min_sum += fence[i] d.append((fence[i], i + 1)) min_index = 1 width_sum = min_sum for i in range(k, n): left_element, left_index = d.popleft() width_sum = width_sum - left_element + fence[i] if width_sum <= min_sum: min_sum = width_sum min_index = left_index + 1 d.append((fence[i], i + 1)) print(min_index)
8
PYTHON3
inputs = list(map(int,input().strip().split())) print(4-len(set(inputs)))
7
PYTHON3
def sum2(board, x, y, dx, dy): s = sum([board[x][y], board[x][y+dy], board[x+dx][y], board[x+dx][y+dy]]) return s rows, cols, moves = map(int, input().split()) if rows == 1 or cols == 1: print(0) else: board = [[1 for col in range(cols)] for row in range(rows)] for move in range(moves): row, col = map(lambda x: int(x)-1, input().split()) board[row][col] = 0 ok = True if row < rows - 1: if col < cols - 1 and sum2(board, row, col, 1, 1) == 0: ok = False if col > 0 and sum2(board, row, col, 1, -1) == 0: ok = False if row > 0: if col < cols - 1 and sum2(board, row, col, -1, 1) == 0: ok = False if col > 0 and sum2(board, row, col, -1, -1) == 0: ok = False if not ok: print(move + 1) break else: print(0)
7
PYTHON3
from collections import deque import sys input = sys.stdin.readline n, k = map(int, input().split()) pow2 = [1] for _ in range(35): pow2.append(2 * pow2[-1]) x = [] for i in pow2: if i & n: x.append(i) ans = "YES" if len(x) <= k <= n else "NO" print(ans) if ans == "YES": c = k - len(x) q = deque() y = [] while c: if not q: q.append(x.pop()) i = q.popleft() if i == 1: y.append(i) else: q.append(i // 2) q.append(i // 2) c -= 1 for i in y: x.append(i) for i in q: x.append(i) print(*x)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long int b, d, s, a; cin >> b >> d >> s; a = max(b, max(d, s)) - 1; cout << max(0LL, a - b) + max(0LL, a - d) + max(0LL, a - s) << '\n'; }
9
CPP
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long exp(long long x, long long n, long long mod) { long long result = 1; while (n) { if (n & 1) result = (result * x) % mod; x = (x * x) % mod; n >>= 1; } return result; } const long long M = 1e9 + 7; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s, t; cin >> s >> t; int n1 = s.length(), n2 = t.length(); long long dp[n1 + 1][n2 + 1]; memset(dp, 0LL, sizeof(dp)); for (int i = n1 - 1; i >= 0; i--) { for (int j = n2 - 1; j >= 0; j--) { dp[i][j] = dp[i][j + 1]; if (s[i] == t[j]) dp[i][j] = (dp[i][j] + (dp[i + 1][j + 1] + 1LL) % M) % M; } } long long ans = 0; for (int i = 0; i < n1; i++) ans = (ans + dp[i][0]) % M; cout << ans << "\n"; return 0; }
7
CPP
n=int(input()) a=list(map(int,input().split())) even=0 odd=0 for i in range(n): if(a[i]%2==0): even=even+1 else: odd=odd+1 a.sort() count=0 if(even==odd): print('0') elif(even>odd): even=even-odd-1 for i in range(n): if(a[i]%2==0 and even>0): even=even-1 count=count+a[i] print(count) else: odd=odd-even-1 for i in range(n): if(a[i]%2!=0 and odd>0): odd=odd-1 count=count+a[i] print(count)
8
PYTHON3
M = int(input()) x = 24 - M + 24 print(x)
0
PYTHON3
for _ in range(int(input())): n = int(input()) ar = list(map(int, input().split())) aSortedReversed = sorted(ar, reverse=True) ans = "No" for i in range(n-1): if aSortedReversed[i] == aSortedReversed[i+1]: ans = "Yes" break if ar != aSortedReversed: print("Yes") else: print(ans)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; long long calc(long long n, long long x) { long long y = x / 2; long long z = min(x - 1, n); if (z <= y) return 0; return z - y; } int main() { long long n; cin >> n; long long x = 500000000LL; while (n < x) { x /= 10; } if (x == 0) { cout << (n - 1) * n / 2 << endl; return 0; } long long y = x * 2; long long ans = 0; for (int i = 0; i < 9; i++) { ans += calc(n, i * y + 2 * x - 1); } cout << ans << endl; return 0; }
10
CPP
n=list(map(str,input())) z=" " z=n[0] n.pop(0) c=True for i in n: if i==i.lower(): c=False if c: for i in range(len(n)): n[i]=n[i].lower() if z==z.lower(): z=z.capitalize() else: z=z.lower() print(z,*n,sep="")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1005; int n, q, len[N]; char name[N][20], ip[N][20]; bool dy(char ip1[], char ip2[], int len) { for (int i = 0; i < len; i++) if (ip1[i] != ip2[i]) return 0; return 1; } int main() { scanf("%d%d", &n, &q); for (int i = 1; i <= n; i++) scanf("%s%s", name[i], ip[i]), len[i] = strlen(ip[i]); while (q--) { char na[20], IP[20]; scanf("%s%s", na, IP); int L = strlen(IP) - 1; IP[L] = '\0'; for (int i = 1; i <= n; i++) if (len[i] == L && dy(IP, ip[i], L)) { printf("%s %s; #%s\n", na, IP, name[i]); break; } } return 0; }
8
CPP
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; #define F first #define S second const int MAXN = 1e5 + 10; int n, h, c1[MAXN], c0[MAXN]; bool vis[MAXN]; vector<int> adj[MAXN]; bool found = 0; void dfs(int v){ vis[v] = 1; if (c0[v] ^ c1[v]) found = 1; if ((v > h && c1[v] > c0[v]) || (v <= h && c0[v] > c1[v])){ cout << "NO\n"; exit(0); } for (int u:adj[v]) if (!vis[u]) dfs(u); } int main(){ ios::sync_with_stdio(false); cin.tie(0); cin >> n >> h; while (n--){ int a, b, c, d; cin >> a >> b >> c >> d; int u = -1, v = -1; if (c == 0) u = a; else u = h+c; if (d) v = d; else v = h+b; adj[u].push_back(v); c1[u]++; adj[v].push_back(u); c0[v]++; } for (int i = 1; i <= h+h; i++) if (!vis[i] && adj[i].size()){ found = 0; dfs(i); if (!found){ cout << "NO\n"; return 0; } } cout << "YES\n"; return 0; }
0
CPP
n = int(input()) pack = [0]*n s = input() s = s.split() for i in range(n): pack[i] = int(s[i]) k = int(input()) for i in range(k): t = input() t = t.split() Pn = int(t[0])-1 Bn = int(t[1]) r = pack[Pn] - Bn l = Bn - 1 if Pn>0 and Pn<(len(pack)-1): pack[Pn] = 0 pack[Pn+1] += r pack[Pn-1] += l elif (Pn<=0 and Pn>=(len(pack)-1)): pack[Pn] = 0 elif Pn<=0: pack[Pn] = 0 pack[Pn+1] += r elif Pn>=(len(pack)-1): pack[Pn] = 0 pack[Pn-1] += l for i in range(n): print(pack[i])
7
PYTHON3
m, n = input().split() mul = int(m)*int(n) d = mul//2 if m == 1 and n == 1: print(0) else: print(d)
7
PYTHON3
o=ord;d=[0]*128 for t in open(0).read().split():d[o(t[0])]+=1 print(sum(d[o(p)]*d[o(q)]*d[o(r)]for p,q,r in __import__('itertools').combinations('MARCH',3)))
0
PYTHON3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; const int inf = 1e9 + 7; const long long linf = 1e18; const int N = 1111111; const int M = 22; int n; int d[N]; int anc[N][M]; inline void upd(int cur, int fa) { anc[cur][0] = fa; d[cur] = d[fa] + 1; for (int i = 1; i < M; i++) anc[cur][i] = anc[anc[cur][i - 1]][i - 1]; } inline int lca(int x, int y) { if (d[x] > d[y]) swap(x, y); if (d[x] < d[y]) { int t = d[y] - d[x]; for (int i = 0; i < M; i++) if (t & (1 << i)) y = anc[y][i]; } if (x != y) { for (int i = M - 1; i >= 0; i--) if (anc[x][i] != anc[y][i]) { x = anc[x][i]; y = anc[y][i]; } x = anc[x][0]; y = anc[y][0]; } return x; } inline int dist(int x, int y) { int t = lca(x, y); return d[x] - d[t] + d[y] - d[t]; } int main() { ios::sync_with_stdio(false); int q; cin >> q; int total = 4; for (int i = 1; i <= 4; i++) upd(i, 1); int now1 = 2, now2 = 3; int ans = 2; for (int i = 0; i < q; i++) { int temp; cin >> temp; upd(++total, temp); upd(++total, temp); int t1 = dist(now1, total); int t2 = dist(now2, total); if (t1 > ans) { if (t2 > t1) now1 = total; else now2 = total; } else if (t2 > ans) now1 = total; ans = max(ans, max(t1, t2)); cout << ans << endl; } return 0; }
12
CPP
#include<bits/stdc++.h> using namespace std; const int maxn=1e6+5; typedef long long ll; const ll mod=1e9+7; ll a[maxn]; int num[maxn]; ll qPow(ll a,ll b){ ll ans=1; while(b){ if(b&1) ans=(ans*a)%mod; a=(a*a)%mod; b>>=1; } return ans; } int main(){ int n; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%lld",a+i); } for(int i=0;i<n;i++){ int x=a[i]; for(int j=2;j*j<=x;j++){ if(x%j==0){ int cnt=0; while(x%j==0){ x/=j; cnt++; } num[j]=max(cnt,num[j]); } } if(x>1){ num[x]=max(num[x],1); } } ll tmp=1; for(int i=0;i<maxn;i++){ for(int j=0;j<num[i];j++){ tmp=(tmp*i)%mod; } } // printf("%lld\n",tmp); ll ans=0; for(int i=0;i<n;i++){ ans=(ans+tmp*qPow(a[i],mod-2))%mod; } printf("%lld",ans%mod); return 0; }
0
CPP
def main(): n = int(input()) nums = list(map(int, input().split())) m = {} for i in range(n): if m.get(nums[i]): m[nums[i]] = (m[nums[i]][0] + 1, m[nums[i]][1], i) else: m[nums[i]] = (1, i, i) for k in m: if m[k][0] > 2: return 'YES' elif m[k][0] == 2 and m[k][1] + 1 < m[k][2]: return 'YES' return 'NO' if __name__ == "__main__": t = int(input()) for _ in range(t): print(main())
8
PYTHON3
t=int(input()) import math for t1 in range(0,t): #n=int(input()) x,y=map(int,input().split()) #l=list(map(int,input().split())) z=x z1=x*2 if z1<=y: print(z,z1) else: print(-1,-1)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; signed main() { ios::sync_with_stdio(0); long long i, j, k, n; cin >> n >> k; vector<long long> have(k), suf(k), a(n); for (i = 0; i < n; ++i) { cin >> j; a[i] = j; have[j - 1]++; } suf[k - 1] = have[k - 1]; for (i = k - 2; i >= 0; --i) suf[i] = suf[i + 1] + have[i]; long long tot = 1; for (i = 0; i < k; ++i) { long long lim; cin >> lim; tot = max(tot, (suf[i] + lim - 1) / lim); } vector<vector<long long>> ans(tot); sort(a.rbegin(), a.rend()); for (i = 0; i < n; ++i) ans[i % tot].push_back(a[i]); cout << tot << "\n"; for (auto x : ans) { cout << (long long)x.size() << " "; for (auto y : x) cout << y << " "; cout << "\n"; } return 0; }
10
CPP
for _ in range(int(input())): n=int(input()) s=input() count=0 from collections import deque d=deque() for i in range(n): if s[i]=="(": d.append(s[i]) else: if len(d)>0: if d[-1]=="(": d.pop() else: d.append(s[i]) count+=1 else: d.append(s[i]) count+=1 print(count)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<int> answer; vector<bool> did; vector<int> portals; int n; int main() { cin >> n; for (long long int i = 0; i < n; i++) { int d; cin >> d; d--; portals.push_back(d); } answer = vector<int>(n, 1000000); did = vector<bool>(n, false); answer[0] = 0; queue<int> que; que.push(0); while (que.size()) { int cur = que.front(); que.pop(); answer[portals[cur]] = min(answer[portals[cur]], answer[cur] + 1); if (!did[portals[cur]]) { que.push(portals[cur]); did[portals[cur]] = true; } if (cur > 0) { answer[cur - 1] = min(answer[cur - 1], answer[cur] + 1); if (!did[cur - 1]) { que.push(cur - 1); did[cur - 1] = true; } } if (cur < n - 1) { answer[cur + 1] = min(answer[cur + 1], answer[cur] + 1); if (!did[cur + 1]) { que.push(cur + 1); did[cur + 1] = true; } } } for (int i = 0; i < n; i++) { if (i != 0) cout << " "; cout << setw(2); cout << answer[i]; } }
8
CPP
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { int a, b, c, temp, s, total, ss; cin >> a >> b >> c; temp = max(a, b); temp = max(temp, c); s = min(a, b); s = min(s, c); total = a + b + c; ss = total - temp - s; if (temp == (s + ss)) cout << "YES" << endl; else if ((temp == ss) && (s % 2 == 0)) cout << "YES" << endl; else if ((s == ss) && (temp % 2 == 0)) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
7
CPP
n, k, x = map(int, input().split()) c = list(map(int, input().split())) ans = k * x for i in range(0, n - k): ans += c[i] print(ans)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mx = (int)1e6 + 2; int lo[mx], fact[3][mx]; vector<int> pr, po; long long int ppow(int x, int e) { if (e == 0) return 1; if (e == 1) return x; long long int root = ppow(x, e / 2); root *= root; if ((e & 1)) root *= x; return root; } int recur(long long int n, int ind, long long int sofar) { if (sofar > n) return 0; if (ind == pr.size()) { if (sofar <= n) return 1; return 0; } int ret = 0; int i = 0; long long int x = 1; for (; i <= po[ind]; i++) { if (sofar > (long long int)2e18 / x) break; ret += recur(n, ind + 1, sofar * x); x *= pr[ind]; } return ret; } int main() { int i, j, k, l, p, q, c; long long int a, b, ans = 0, n, m, s; for (i = 1; i < mx; i++) lo[i] = i; for (j = 2; j < mx; j++) { if (lo[j] == j) { for (i = 2 * j; i < mx; i += j) lo[i] = min(lo[i], j); } } ios::sync_with_stdio(false); cin.tie(0); cin >> l; while (l--) { int nums[3][3]; for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) cin >> nums[i][j]; set<int> primes[3]; for (j = 0; j < 3; j++) { for (i = 0; i < 3; i++) { c = nums[j][i]; while (lo[c] > 1) { k = lo[c]; p = 0; while (k == lo[c]) { c /= lo[c]; p++; } primes[j].insert(k); fact[j][k] += p; } } } pr.clear(); po.clear(); primes[2].insert(2); fact[2][2]++; for (int x : primes[2]) { pr.emplace_back(x); po.emplace_back(fact[2][x]); } n = (long long int)nums[0][0] * (long long int)nums[0][1] * nums[0][2]; m = (long long int)nums[1][0] * (long long int)nums[1][1] * nums[1][2]; ans = recur(n, 0, 1); vector<long long int> bad; for (int x : primes[0]) { if (fact[0][x] > fact[2][x]) { b = ppow(x, fact[2][x] + 1); bad.push_back(b); } } int sgn = +1; int limit = 1 << bad.size(); for (i = 0; i < limit; i++, sgn *= -1) { int conf = (i ^ (i >> 1)); long long int act = 1; for (j = 0; j < bad.size(); j++) if ((conf & (1 << j)) > 0) act = min(m + 1, act * bad[j]); ans += sgn * m / act; } cout << ans << '\n'; for (j = 0; j < 3; j++) { for (int x : primes[j]) fact[j][x] = 0; } } return 0; }
9
CPP
/* ID: htc9811 LANG: C++ TASK: */ #include<stdio.h> #include<string> #include<math.h> #include<stdlib.h> #include<set> #include<bitset> #include<map> #include<vector> #include<string.h> #include<algorithm> #include<iostream> #include<queue> #include<deque> #include<stack> #include<cmath> #include<ctime> #include<complex> #include<list> #include<limits.h> using namespace std; const int N=2e5+5; int n,cur; long long a[N]; long long d,ans; struct Seg{ long long length; int x,y; Seg(){}; Seg(long long length,int x,int y):length(length),x(x),y(y){}; }; struct UF{ int p[N],rnk[N]; void init(int n){ memset(p,0,sizeof(p)); memset(rnk,0,sizeof(rnk)); for(int i=0;i<n;i++) p[i]=i; for(int i=0;i<n;i++) rnk[i]=0; } int fp(int x){ return x==p[x]?x:p[x]=fp(p[x]); } void un(int x,int y){ if(rnk[x]>rnk[y]) p[y]=x; if(rnk[x]<rnk[y]) p[x]=y; if(rnk[x]==rnk[y]) p[x]=y,rnk[y]++; } }; inline bool comp(Seg &x,Seg &y){ return x.length<y.length; } vector<Seg> seg; UF uf; void solve(int left,int right){ if(left==right) return; int middle=(left+right)>>1; int pl=left; long long vl=a[left]-d*left; for(int i=left+1;i<=middle;i++){ long long cv=a[i]-d*i; if(cv<vl){ vl=cv; pl=i; } } int pr=middle+1; long long vr=a[middle+1]+d*(middle+1); for(int i=middle+2;i<=right;i++){ long long cv=a[i]+d*i; if(cv<vr){ vr=cv; pr=i; } } for(int i=left;i<=middle;i++) seg.push_back(Seg(a[i]-d*i+vr,i,pr)); for(int i=middle+1;i<=right;i++) seg.push_back(Seg(a[i]+d*i+vl,pl,i)); solve(left,middle); solve(middle+1,right); } int main(){ // freopen("cc3.in","r",stdin); // freopen("cc1.out","w",stdout); scanf("%d%lld",&n,&d); uf.init(n); for(int i=0;i<n;i++) scanf("%lld",a+i); solve(0,n-1); sort(seg.begin(),seg.end(),comp); for(int i=1;i<n;i++){ while(true){ int nx=seg[cur].x; int ny=seg[cur].y; long long len=seg[cur].length; nx=uf.fp(nx); ny=uf.fp(ny); cur++; if(nx!=ny){ uf.un(nx,ny); // printf("%lld\n",len); ans+=len; break; } } } printf("%lld\n",ans); return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 0x7f7f7f7f; const long long int INFL = 0x7f7f7f7f7f7f7f7f; const int MAXN = 1005; int main() { int n, total = 0; scanf("%d", &n); int arr[n]; scanf("%d", arr); for (int i = 1; i < n; i++) { scanf("%d", arr + i); arr[i] += arr[i - 1]; } total = arr[n - 1]; if (total % 2) { cout << "0" << endl; } else { int t = 0; for (int i = 0; i < n - 1; i++) { if (arr[i] == total / 2) { t++; } } cout << t << endl; } }
9
CPP
#include <iostream> using namespace std; int dx[] = {0,0,0,3,2,1,0,0,0,-1,-2,-3},dy[] = {3,2,1,0,0,0,-1,-2,-3,0,0,0}; void chain(int sx,int sy,int bom[][8]){ bom[sx][sy] = 0; int x,y; for(int i = 0 ; i < 12 ; i++){ x = dx[i] + sx; y = dy[i] + sy; if(bom[x][y] == 1 && (0 <= x && x < 8) && (0 <= y && y < 8 )){ chain(x,y,bom); } } } int main(){ string bo; int bom[8][8]; int n,k; cin >> n; for(k = 1 ; k <= n ; k++ ){ for(int i = 0 ; i < 8; i++){ cin >> bo; for(int j = 0; j < 8 ; j++){ bom[i][j] = bo[j] - '0'; } } int sx,sy; cin >> sy >> sx; sx--;sy--; chain(sx,sy,bom); cout << "Data " << k << ":" << endl; for(int i = 0; i < 8 ; i++){ for(int j = 0; j < 8 ; j++){ cout << bom[i][j]; } cout << endl; } } } //http://www9.plala.or.jp/sgwr-t/c/sec11-2.html //ไบŒๆฌกๅ…ƒ้…ๅˆ—ใ‚’้–ขๆ•ฐใธๆธกใ™ๅ ดๅˆ
0
CPP
n=int(input()) s=input() l=list(s) for i in range(n-1): if ord(s[i])>ord(s[i+1]): l.pop(i) break if len(l)==n: print(s[:-1]) else: print(''.join(l))
7
PYTHON3
N = int(input()) x = -(-N // 2) print(x/N)
0
PYTHON3
t=int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) a.sort() A = [] for i in range((n//2)): A+= [a[n-1-i],a[i]] if(n&1): A.append(a[n//2]) print(*reversed(A))
8
PYTHON3
#n = int(input()) #n, m = map(int, input().split()) s = input() t = input() #c = list(map(int, input().split())) a = [0] * 123 l = 0 for i in range(len(s)): x = ord(s[i]) y = ord(t[i]) if s[i] == t[i]: if a[x] == x or a[x] == 0: a[x] = x else: print(-1) break elif s[i] != t[i]: if a[x] == 0 and a[y] == 0: a[x] = y a[y] = x l += 1 elif a[x] != y and a[y] != x: print(-1) break else: print(l) for i in range(len(a)): if a[i] != i and a[i] != 0: k = a[i] print(chr(i), chr(a[i])) a[i], a[k] = a[k], a[i]
8
PYTHON3
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<int(n);++i) #define all(a) a.begin(),a.end() #define INF 1e9 using P = pair<int,int>; int dx[4] = {1,0,-1,0}; int dy[4] = {0,1,0,-1}; vector<vector<int>> cost_map( int Y,int X,int sy,int sx, vector<string> &s){ vector<vector<int>> cost(Y, vector<int>(X,INF)); cost[sy][sx] = 0; queue<P> qu; qu.push(P(sy,sx)); while(qu.size()){ P p = qu.front(); qu.pop(); int y = p.first; int x = p.second; rep(i,4){ int ny = y+dy[i]; int nx = x+dx[i]; if(nx<0 || ny<0 || nx>=X || ny>=Y) continue; if(s[ny][nx] == 'x') continue; if( cost[ny][nx] > cost[y][x] + 1){ cost[ny][nx] = cost[y][x] + 1; qu.push(P(ny, nx)); } } } return cost; } int main(void){ int Y,X; while(cin>>X>>Y, Y|X){ vector<string> s(Y); rep(y,Y) cin>>s[y]; vector<P> pos; rep(y,Y)rep(x,X){ if(s[y][x]=='o' || s[y][x]=='*'){ pos.push_back(P(y,x)); } } int n = pos.size(); int d[11][11]; bool ng = false; rep(i,n){ int y = pos[i].first; int x = pos[i].second; auto cost = cost_map(Y,X,y,x,s); rep(j,n){ int ny = pos[j].first; int nx = pos[j].second; d[i][j] = cost[ny][nx]; if(d[i][j] == INF) ng = true; } } if(ng){ cout<<-1<<endl; continue; } vector<int> perm(n); rep(i,n) perm[i] = i; int res = INF; do{ int sy = pos[perm[0]].first; int sx = pos[perm[0]].second; if(s[sy][sx] != 'o') continue; int cost = 0; rep(i,n-1){ cost += d[perm[i]][perm[i+1]]; } res = min(res, cost); }while(next_permutation(all(perm))); cout<<res<<endl; } return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; inline long long read() { long long Hashimoto = 0, Kanna = 1; char I_Love = getchar(); while (I_Love < '0' || I_Love > '9') { if (I_Love == '-') Kanna = -1; I_Love = getchar(); } while (I_Love >= '0' && I_Love <= '9') { Hashimoto = Hashimoto * 10 + I_Love - '0'; I_Love = getchar(); } return Hashimoto * Kanna; } template <typename T1, typename T2> inline void Umax(T1 &a, T2 b) { if (a < b) a = b; } template <typename T1, typename T2> inline void Umin(T1 &a, T2 b) { if (a > b) a = b; } inline int FUCK(string s) { if (s == "1J") return 101; if (s == "2J") return 102; int ans; if (s[0] == 'C') ans = 0; if (s[0] == 'D') ans = 13; if (s[0] == 'H') ans = 26; if (s[0] == 'S') ans = 39; if (s[1] == 'T') ans += 8; else if (s[1] == 'J') ans += 9; else if (s[1] == 'Q') ans += 10; else if (s[1] == 'K') ans += 11; else if (s[1] == 'A') ans += 12; else ans += s[1] - '2'; return ans; } inline string SHIT(int x) { string ans = ""; if (x / 13 == 0) ans = 'C'; if (x / 13 == 1) ans = 'D'; if (x / 13 == 2) ans = 'H'; if (x / 13 == 3) ans = 'S'; if (x % 13 < 8) ans += x % 13 + '2'; else if (x % 13 == 8) ans += 'T'; else if (x % 13 == 9) ans += 'J'; else if (x % 13 == 10) ans += 'Q'; else if (x % 13 == 11) ans += 'K'; else if (x % 13 == 12) ans += 'A'; reverse(ans.begin(), ans.end()); ; return ans; } bool used[55]; vector<int> able; int n, m; int t[22][22]; pair<int, int> J1, J2; int vj1 = -1, vj2 = -1; bool intersect(int i1, int I_Love_Hashimoto_Kanna_J1, int i2, int I_Love_Hashimoto_Kanna_J2) { for (int i = i2; i < (i2 + 3); ++i) for (int j = I_Love_Hashimoto_Kanna_J2; j < (I_Love_Hashimoto_Kanna_J2 + 3); ++j) if (i1 <= i && i < i1 + 3 && I_Love_Hashimoto_Kanna_J1 <= j && j < I_Love_Hashimoto_Kanna_J1 + 3) return 1; return 0; } bool debug = 0; bool check(int i, int j) { int a = -1, b = 0; for (int n = 0; n < (3); ++n) { for (int m = 0; m < (3); ++m) { if (a == -1) { a = t[i + n][j + m] / 13; } else if (a != t[i + n][j + m] / 13) { a = -2; } b |= (1 << (t[i + n][j + m] % 13)); } } if (a != -2 || __builtin_popcount(b) == 9) return 1; return 0; } void check() { for (int i1 = 0; i1 < (n - 2); ++i1) { for (int I_Love_Hashimoto_Kanna_J1 = 0; I_Love_Hashimoto_Kanna_J1 < (m - 2); ++I_Love_Hashimoto_Kanna_J1) { for (int i2 = 0; i2 < (n - 2); ++i2) { for (int I_Love_Hashimoto_Kanna_J2 = 0; I_Love_Hashimoto_Kanna_J2 < (m - 2); ++I_Love_Hashimoto_Kanna_J2) { if (intersect(i1, I_Love_Hashimoto_Kanna_J1, i2, I_Love_Hashimoto_Kanna_J2)) continue; if (check(i1, I_Love_Hashimoto_Kanna_J1)) { if (check(i2, I_Love_Hashimoto_Kanna_J2)) { cout << "Solution exists.\n"; int t = 0; if (J1.first != -1) t |= 1; if (J2.first != -1) t |= 2; if (t == 0) { cout << "There are no jokers.\n"; } if (t == 1) { cout << "Replace J1 with " << SHIT(vj1) << ".\n"; } if (t == 2) { cout << "Replace J2 with " << SHIT(vj2) << ".\n"; } if (t == 3) { cout << "Replace J1 with " << SHIT(vj1) << " and J2 with " << SHIT(vj2) << ".\n"; } cout << "Put the first square to (" << i1 + 1 << ", " << I_Love_Hashimoto_Kanna_J1 + 1 << ").\n"; cout << "Put the second square to (" << i2 + 1 << ", " << I_Love_Hashimoto_Kanna_J2 + 1 << ").\n"; exit(0); } } } } } } } void dfs(int fuck) { if (fuck == 0) { if (J1.first == -1) { dfs(fuck + 1); } else { for (int i = 0; i < (able.size()); ++i) { vj1 = able[i]; t[J1.first][J1.second] = vj1; dfs(fuck + 1); } } } else if (fuck == 1) { if (J2.first == -1) { dfs(fuck + 1); } else { for (int i = 0; i < (able.size()); ++i) { if (able[i] == vj1) continue; vj2 = able[i]; t[J2.first][J2.second] = vj2; dfs(fuck + 1); } } } else { if (vj1 == 22) debug = 1; check(); debug = 0; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; J1 = J2 = {-1, -1}; cin >> n >> m; for (int i = 0; i < (n); ++i) for (int j = 0; j < (m); ++j) { string s; cin >> s; reverse(s.begin(), s.end()); ; t[i][j] = FUCK(s); if (t[i][j] < 100) { used[t[i][j]] = 1; } if (t[i][j] == 101) J1 = {i, j}; if (t[i][j] == 102) J2 = {i, j}; } for (int i = 0; i < (52); ++i) { if (!used[i]) { able.push_back(i); } } dfs(0); cout << "No solution." << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int nax = 1e6 + 5; const int inf = 1e9 + 5; int m[3]; multiset<int> enemies; void greedy(int atLeast, int extra, int &ans) { while (!enemies.empty()) { auto it = enemies.end(); --it; if (*it < atLeast) break; enemies.erase(it); ++ans; it = enemies.lower_bound(extra); if (it != enemies.begin()) { --it; enemies.erase(it); } } } int main() { int tc = 1; while (tc--) { int n; scanf("%d", &n); for (int i = 0; i < 3; ++i) scanf("%d", &m[i]); sort(m, m + 3); for (int i = 0; i < n; ++i) { int x; scanf("%d", &x); --x; enemies.insert(x); } int all = m[0] + min(inf, m[1] + m[2]); auto it = enemies.end(); --it; if (*it >= all) { puts("-1"); return 0; } int ans = 0; greedy(m[1] + m[2], 0, ans); greedy(m[0] + m[2], m[0], ans); greedy(max(m[0] + m[1], m[2]), m[1], ans); int one = 0, two = 0; for (int x : enemies) { if (x < m[0] + m[1]) ++one; if (x < m[2]) ++two; } int best = inf; for (int rep = 0; rep < n + 5; ++rep) { if (max(one, two) == (int)enemies.size()) { if (2 * min(one, two) >= (int)enemies.size()) best = min(best, rep + ((int)enemies.size() + 1) / 2); else best = min(best, rep + (int)enemies.size() - min(one, two)); } for (int i = 0; i < 3; ++i) { auto it = enemies.lower_bound(m[i]); if (it != enemies.begin()) { --it; if (*it < m[0] + m[1]) --one; if (*it < m[2]) --two; enemies.erase(it); } } } printf("%d\n", ans + best); } return 0; }
11
CPP
#include<cstdio> #include<cstring> #include<cctype> #include<algorithm> #define reg register #define reset(a) memset(a,0,sizeof(a)) using namespace std; typedef pair<int,int> par; const int N=1e5+5; struct E { int to,nxt; }edge[N*50]; struct node { int x,id; inline friend bool operator < (node a,node b) {return a.x<b.x;} }c[N]; int n,m,num,head[N],a[N],dfn[N],low[N],bel[N]; int cnt,tim,tot,stack[N],top,idx[N]; bool exist[N]; inline int read() { int x=0,w=1; char c=getchar(); while (!isdigit(c)&&c!='-') c=getchar(); if (c=='-') c=getchar(),w=-1; while (isdigit(c)) { x=(x<<1)+(x<<3)+c-'0'; c=getchar(); } return x*w; } inline void add_edge(int from,int to) { edge[++num]=(E){to,head[from]}; head[from]=num; } inline void clear() { reset(dfn); reset(low); reset(bel); reset(exist); reset(head); reset(idx); num=cnt=tot=top=0; tim=n<<1; } void tarjan(int k) { int temp; dfn[k]=low[k]=++cnt; stack[++top]=k; exist[k]=1; for (reg int i=head[k];i;i=edge[i].nxt) { int v=edge[i].to; if (!dfn[v]) { tarjan(v); low[k]=min(low[k],low[v]); } else if (exist[v]) low[k]=min(low[k],dfn[v]); } if (dfn[k]==low[k]) { ++tot; do { temp=stack[top--]; exist[temp]=0; bel[temp]=tot; }while (temp!=k); } } void build(int l,int r,int now) { idx[now]=++tim; if (now>1) add_edge(idx[now>>1],tim); if (l==r) {add_edge(idx[now],c[l].id>n?c[l].id-n:c[l].id+n); return;} int mid=(l+r)>>1; build(l,mid,now<<1); build(mid+1,r,now<<1|1); } void change(int L,int R,int l,int r,int now,int x) { if (l>R||r<L) return; if (l>=L&&r<=R) {add_edge(x,idx[now]); return;} int mid=(l+r)>>1; if (mid>=R) change(L,R,l,mid,now<<1,x); else if (mid<L) change(L,R,mid+1,r,now<<1|1,x); else { change(L,mid,l,mid,now<<1,x); change(mid+1,R,mid+1,r,now<<1|1,x); } } inline par query(int p,int k) { par ret; ret.first=ret.second=p; int l=1,r=p,pos=p; while (l<=r) { int mid=(l+r)>>1; if (c[p].x-c[mid].x<k) pos=mid,r=mid-1; else l=mid+1; } ret.first=pos; l=p,r=m,pos=p; while (l<=r) { int mid=(l+r)>>1; if (c[mid].x-c[p].x<k) pos=mid,l=mid+1; else r=mid-1; } ret.second=pos; return ret; } inline bool check(int k) { clear(); build(1,m,1); for (reg int i=1;i<=m;i++) { par now=query(i,k); change(now.first,i-1,1,m,1,c[i].id); change(i+1,now.second,1,m,1,c[i].id); } for (reg int i=1;i<=m;i++) if (!dfn[i]) tarjan(i); for (reg int i=1;i<=n;i++) if (bel[i]==bel[i+n]) return 0; return 1; } int main() { n=read(); for (reg int i=1;i<=n;i++) { a[i]=read(),a[i+n]=read(); c[++m]=(node){a[i],i}; c[++m]=(node){a[i+n],i+n}; } sort(c+1,c+m+1); int l=0,r=1e9,ans=0; while (l<=r) { int mid=(l+r)>>1; if (check(mid)) ans=mid,l=mid+1; else r=mid-1; } printf("%d\n",ans); return 0; }
0
CPP
# -*- coding: utf-8 -*- """ Created on Sun Aug 16 20:13:45 2020 @author: The Wonder Land """ for _ in range(int(input())): pass_length = int(input()) pwd = list(map(int, input().strip().split())) if len(set(pwd)) == 1: print(len(pwd), end="\n") else: print("1", end="\n") """i = 0 while((pass_length > 1) and i < (pass_length-1)): if pwd[i] != pwd[i+1]: pwd[i] += pwd[i+1] pwd.remove(pwd[i+1]) i = 0 pass_length = len(pwd) continue else: i += 1 continue print(len(pwd), end="\n")"""
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[1000005]; int b[1000005]; int c[1000005]; int d[1000005]; int main() { int n, k; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); scanf("%d", &k); for (int i = 0; i < k; i++) scanf("%d", &b[i]); int sum = 0; int h = 0; int ma = 0; for (int i = 0; i < n; i++) { sum += a[i]; d[i] = h; if (sum > b[h]) { cout << "NO" << endl; return 0; } else { if (a[i] > ma) { if (i < n - 1 && sum != b[h] && a[i + 1] < a[i] || i > 0 && d[i - 1] == h && a[i - 1] < a[i]) { ma = a[i]; c[h] = i; } } if (sum == b[h]) { if (a[i] == b[h]) c[h] = i; else if (ma == 0) { cout << "NO" << endl; return 0; } h++; ma = 0; sum = 0; } } } if (h != k) { cout << "NO" << endl; return 0; } cout << "YES" << endl; for (int i = k - 1; i >= 0; i--) { if (a[c[i]] == b[i]) continue; if (c[i] != n - 1 && d[c[i] + 1] == i && a[c[i] + 1] < a[c[i]]) { for (int j = c[i] + 1; j < n && d[j] == i; j++) printf("%d R\n", c[i] + 1); for (int j = c[i] - 1; j >= 0 && d[j] == i; j--) printf("%d L\n", j + 2); } else { printf("%d L\n", c[i] + 1); for (int j = c[i] + 1; j < n && d[j] == i; j++) printf("%d R\n", c[i]); for (int j = c[i] - 2; j >= 0 && d[j] == i; j--) printf("%d L\n", j + 2); } } return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2 * 1e5 + 10, mod = 1e9 + 7; int p[N]; bool mark[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, c = 0, cyc = 0, ans = 0; cin >> n; for (int i = 1; i <= n; i++) cin >> p[i]; for (int i = 0; i < n; i++) { int t; cin >> t; if (t == 1) c++; } if (c % 2 == 0) ans++; for (int i = 1; i <= n; i++) { if (mark[i]) continue; int x = i; cyc++; while (!mark[x]) { mark[x] = true; x = p[x]; } } ans += cyc; if (cyc == 1) ans--; cout << ans << '\n'; }
7
CPP
n, m, z = input().split() ans = 0 for i in range(int(n), int(z) + 1, int(n)): if i % int(m) == 0: ans += 1 print(ans)
7
PYTHON3
a = int(input()) for i in range(a): numbers = (input()) list = [] for j in range(len(numbers)): if numbers[j] != "0": number = numbers[j] + "0" * (len(numbers) - j - 1) list.append(number) print(len(list)) print(" ".join(list))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { setlocale(LC_ALL, "Ru"); string str; getline(cin, str); int a[4]; for (int i = 0; i < 4; i++) { a[i] = atoi(str.c_str()); if (a[i] >= 0 && a[i] < 10) str.erase(0, 2); else if (a[i] >= 10 && a[i] < 100) str.erase(0, 3); if (a[i] == 100) str.erase(0, 4); if (a[i] > 100 || a[i] < 0) { printf("-1"); exit(0); } } if ((a[0] - a[2]) + (a[1] - a[2]) + a[2] > a[3]) { printf("-1"); exit(0); } int nez = a[3] - ((a[0] - a[2]) + (a[1] - a[2]) + a[2]); if (nez == 0 || a[3] == 0 || nez > a[3] || a[0] < a[2] || a[1] < a[2]) { printf("-1"); exit(0); } else printf("%d", nez); return 0; }
7
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:32000000") using namespace std; inline bool read(int& val) { return scanf("%d", &val) != -1; } inline bool read(long long& val) { return scanf("%I64d", &val) != -1; } inline bool read(double& val) { return scanf("%lf", &val) != -1; } inline bool read(char* val) { return scanf("%s", val) != -1; } template <class T1, class T2> inline bool read(T1& a, T2& b) { return read(a) && read(b); } template <class T1, class T2, class T3> inline bool read(T1& a, T2& b, T3& c) { return read(a) && read(b) && read(c); } template <class T1, class T2, class T3, class T4> inline bool read(T1& a, T2& b, T3& c, T4& d) { return read(a) && read(b) && read(c) && read(d); } template <class T1, class T2, class T3, class T4, class T5> inline bool read(T1& a, T2& b, T3& c, T4& d, T5& e) { return read(a) && read(b) && read(c) && read(d) && read(e); } const int N = 11, M = 16; long long dp[M + 1][N + 1]; long long C[N + 1][N + 1]; int ans[N + 1], cnt[M]; template <int n, int m> void calC(long long (&C)[n][m]) { for (int j = 0, _ = (m); j < _; ++j) C[0][j] = 0; for (int i = 0, _ = (n); i < _; ++i) C[i][0] = 1; for (int i = (1), _ = (n); i < _; ++i) for (int j = (1), _ = (m); j < _; ++j) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]); } long long getnum(int n, int cnt[M]) { for (int i = 0, _ = (M + 1); i < _; ++i) for (int j = 0, _ = (N); j < _; ++j) dp[i][j] = 0; dp[0][0] = 1; for (int i = 0, _ = (M); i < _; ++i) for (int j = 0, _ = (n + 1); j < _; ++j) for (int d = 0, _ = (cnt[i]); d < _; ++d) { if (j + d <= n) dp[i + 1][j + d] += C[n - j][d] * dp[i][j]; } return dp[M][n]; } void get(int ans[N], long long k, int cnt[M]) { long long cur = 0; int n = 0; bool found = false; while (!found) { for (int i = (1), _ = (M); i < _; ++i) { ans[n] = i; cnt[i]--; long long add = getnum(n, cnt); if (k < cur + add) { found = true; break; } cur += add; cnt[i]++; } if (!found) n++; } for (int j = (n)-1; j >= 0; --j) { for (int i = 0, _ = (M); i < _; ++i) { ans[j] = i; cnt[i]--; long long add = getnum(j, cnt); if (k < cur + add) break; cnt[i]++; cur += add; } } } int main() { calC(C); long long k; int t; read(k, t); for (int i = 0, _ = (M); i < _; ++i) for (int j = 0, _ = (N); j < _; ++j) for (int d = 0, _ = (t); d < _; ++d) if (j + d < N) dp[i + 1][j + d] += dp[i][j]; for (int i = 0, _ = (M); i < _; ++i) cnt[i] = t + 1; get(ans, k - 1, cnt); int n = N - 1; while (n >= 0 && ans[n] == 0) n--; for (int i = (n + 1) - 1; i >= 0; --i) printf("%c", (char)(ans[i] < 10 ? '0' + ans[i] : 'a' + ans[i] - 10)); printf("\n"); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; void fastIO() { std::ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } template <typename T, typename U> inline void mnze(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> inline void mxze(T &x, U y) { if (x < y) x = y; } void _scan(int &x) { scanf("%d", &x); } void _scan(long long &x) { scanf("%lld", &x); } void _scan(double &x) { scanf("%lf", &x); } void _scan(char &x) { scanf(" %c", &x); } void _scan(char *x) { scanf("%s", x); } void scan() {} template <typename T, typename... U> void scan(T &head, U &...tail) { _scan(head); scan(tail...); } template <typename T> void _dbg(const char *sdbg, T h) { cerr << sdbg << "=" << h << "\n"; } template <typename T, typename... U> void _dbg(const char *sdbg, T h, U... t) { while (*sdbg != ',') cerr << *sdbg++; cerr << "=" << h << ","; _dbg(sdbg + 1, t...); } const long long MOD = 998244353; const int MAXN = 505; long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } long long ans = 1; void mans(long long x) { ans = ans * (x % MOD) % MOD; } map<long long, int> pcnt; map<long long, int> occ; int n; long long a[MAXN]; long long is_square(long long x) { long long lo = 2, hi = (1 << 30) + 500000000; while (lo < hi) { long long mi = (lo + hi) / 2; if (mi * mi >= x) hi = mi; else lo = mi + 1; } if (lo * lo == x) return lo; return -1; } long long is_cube(long long x) { long long lo = 2, hi = (1 << 20) + 500000; while (lo < hi) { long long mi = (lo + hi) / 2; if (mi * mi * mi >= x) hi = mi; else lo = mi + 1; } if (lo * lo * lo == x) return lo; return -1; } long long is_forth(long long x) { long long lo = 2, hi = (1 << 8) + 40000; while (lo < hi) { long long mi = (lo + hi) / 2; if (mi * mi * mi * mi >= x) hi = mi; else lo = mi + 1; } if (lo * lo * lo * lo == x) return lo; return -1; } vector<long long> nfact; bool fact(long long x) { long long p; if ((p = is_forth(x)) != -1) { pcnt[p] += 4; return true; } if ((p = is_square(x)) != -1) { pcnt[p] += 2; return true; } if ((p = is_cube(x)) != -1) { pcnt[p] += 3; return true; } return false; } int main() { scan(n); for (int i = 0; i < (int)n; i++) { scan(a[i]); occ[a[i]]++; if (!fact(a[i])) { nfact.push_back(a[i]); } } sort(nfact.begin(), nfact.end()); nfact.resize(distance(nfact.begin(), unique(nfact.begin(), nfact.end()))); for (long long x : nfact) { bool done = false; int c = occ[x]; for (int i = 0; i < (int)n; i++) { if (a[i] == x) continue; long long g = gcd(x, a[i]); if (g == 1) continue; pcnt[g] += c; pcnt[x / g] += c; done = true; break; } if (!done) { mans(c + 1); mans(c + 1); } } for (auto &it : pcnt) { int c = it.second; mans(c + 1); } printf("%lld\n", ans); fflush(stdout); }
10
CPP
limit = input() limit = int(limit) x = input() x = x.split() x.sort(reverse=True) x = [int(i)for i in x ] j = limit-1 ans = 0 i = 0 while i<=j: ans+=1 four = 4-x[i] while x[j]<=four and j>=i: four-=x[j] j-=1 i+=1 print(ans)
8
PYTHON3
for _ in range(int(input())): a,b=map(int,input().split()) #a=[int(i) for i in input().split()] c=0 if a>b: if (a-b)%2==0: c=1 else: c=2 elif a<b: if (b-a)%2==0: c=2 else: c=1 else: c=0 print(c)
7
PYTHON3
n = int(input()) skills = list(map(int, input().split())) subjects = [list() for i in range(3)] for i in range(len(skills)): subjects[skills[i]-1].append(i+1) teams = min(len(i) for i in subjects) if teams == 0: print(0) else: print(teams) for i in range(teams): print(subjects[0][i], subjects[1][i], subjects[2][i])
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, k; int a[35010], pre[35010], last[35010]; int f[55][35010]; struct node { int lc, rc, tag, f; } t[35010 << 1]; int ptr = 0; int newnode() { return ++ptr; } int root; void pushup(int rt) { t[rt].f = max(t[t[rt].lc].f, t[t[rt].rc].f); return; } void pushdown(int rt) { if (t[rt].tag == 0) return; t[t[rt].lc].tag += t[rt].tag; t[t[rt].rc].tag += t[rt].tag; t[t[rt].lc].f += t[rt].tag; t[t[rt].rc].f += t[rt].tag; t[rt].tag = 0; return; } void build(int &rt, int l, int r, int cur) { rt = newnode(); t[rt].tag = 0; if (l == r) { t[rt].f = f[cur][l - 1]; t[rt].lc = t[rt].rc = 0; return; } int mid = ((l + r) >> 1); build(t[rt].lc, l, mid, cur); build(t[rt].rc, mid + 1, r, cur); pushup(rt); return; } void init() { memset(t, 0, sizeof(t)); ptr = 0; return; } void pt(int rt, int l, int r) { if (l == r) { cout << t[rt].f << " "; return; } pushdown(rt); int mid = ((l + r) >> 1); pt(t[rt].lc, l, mid); pt(t[rt].rc, mid + 1, r); return; } int query(int rt, int L, int R, int l, int r) { if (L <= l && r <= R) { return t[rt].f; } pushdown(rt); int mid = ((l + r) >> 1); int res = 0; if (L <= mid) res = max(res, query(t[rt].lc, L, R, l, mid)); if (R >= mid + 1) res = max(res, query(t[rt].rc, L, R, mid + 1, r)); return res; } void add(int rt, int L, int R, int l, int r) { if (L <= l && r <= R) { t[rt].f += 1; t[rt].tag += 1; return; } pushdown(rt); int mid = ((l + r) >> 1); if (L <= mid) add(t[rt].lc, L, R, l, mid); if (R >= mid + 1) add(t[rt].rc, L, R, mid + 1, r); pushup(rt); return; } int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); pre[i] = last[a[i]]; last[a[i]] = i; } memset(f, 0, sizeof(f)); for (int i = 1; i <= k; ++i) { init(); build(root, 1, n, i - 1); for (int j = 1; j <= n; ++j) { add(root, pre[j] + 1, j, 1, n); f[i][j] = query(root, 1, j, 1, n); } } cout << f[k][n] << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<n;++i) int main(void){ int N,sum=0; int L[200]; cin>>N; rep(i,2*N)cin>>L[i]; sort(L,L+2*N); rep(i,N)sum+=L[i*2]; cout<<sum<<endl; return 0; }
0
CPP
N, M, *ab = map(int, open(0).read().split()) ans = [0] * N for a, b in zip(*[iter(ab)] * 2): ans[a - 1] += 1 ans[b - 1] -= 1 print("NO") if any(a % 2 for a in ans) else print("YES")
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, m; cin >> n >> m; int a[n][m]; set<int> r, c; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; if (a[i][j] == 1) { r.insert(i); c.insert(j); } } } int r1 = n - r.size(); int c1 = m - c.size(); if (r1 == 0 || c1 == 0) cout << "Vivek\n"; else if ((r1 & 1) && (c1 & 1)) cout << "Ashish\n"; else if (!(r1 & 1) && !(c1 & 1)) cout << "Vivek\n"; else { int o, e; if (r1 & 1) o = r1, e = c1; else o = c1, e = r1; if (o > e) cout << "Vivek\n"; else cout << "Ashish\n"; } } return 0; }
7
CPP
#include <iostream> #include <stdio.h> #include <string.h> #define MAX_N 100005 #define int long long using namespace std; int n,m; int par[MAX_N]; int dis[MAX_N]; int find(int x) { if(par[x]==x) return x; int p=par[x]; par[x]=find(par[x]),dis[x]+=dis[p]; return par[x]; } bool unite(int x,int y,int z) { int px=find(x),py=find(y); if(px==py) return dis[x]-dis[y]==z; dis[px]=-dis[x]+z+dis[y],par[px]=py; return true; } signed main() { scanf("%lld%lld",&n,&m); for(int i=1;i<=n;i++) par[i]=i; int l,r,d; for(int i=1;i<=m;i++) { scanf("%lld%lld%lld",&l,&r,&d); if(!unite(l,r,d)) { printf("No\n"); return 0; } } printf("Yes\n"); }
0
CPP
#include <bits/stdc++.h> using namespace std; long long n, t, var, ans, x, y, a[1000000]; bool f; string s; char c; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> c >> s; x = 1; var = 0; for (long long i = s.length() - 1; i + 1; --i) { if (((int)s[i] - 48) % 2) var += x; x *= 2; } if (c == '+') ++a[var]; else if (c == '-') --a[var]; else cout << a[var] << endl; } }
7
CPP
#include <iostream> using namespace std; bool isprime(int p) { if (p == 1) return false; for (int i = 2; i < p; i++) { if (p%i == 0) return false; } return true; } int n; int main() { cin >> n; for (int i = 11; i <= 55555; i += 5) { if (isprime(i) == true) { if (i != 11) cout << " "; cout << i; n--; } if (n == 0) break; } cout << endl; return 0; }
0
CPP