solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
ans = [] for _ in range(int(input())): s = list(input()) for i in range(1, len(s)): if s[i] != s[0]: break else: ans.append(''.join(s)) continue t = '01' * len(s) ans.append(t) print('\n'.join(map(str, ans)))
8
PYTHON3
#include <bits/stdc++.h> long long abc[4], xyz[4], min[4], max[4], min_[4], max_[4]; int valid() { double sum = (double)abc[1] + abc[2] + abc[3]; if (sum < 0) sum = -sum; return sum <= 8e18; } int solve(int par) { for (int i = 1; i < 4; i++) { abc[i] = min_[i]; if ((abc[i] % 2 != 0) != par) abc[i]++; if (abc[i] > max_[i]) return 0; } for (int i = 1; i < 4; i++) while (valid() && (abc[0] = abc[1] + abc[2] + abc[3]) < min_[0] && abc[i] + 2 <= max_[i]) { long long d = (double)min_[0] - abc[0] < 1LL << 62 ? (min_[0] - abc[0] + 1) / 2 * 2 : 1LL << 62; long long a = (double)max_[i] - abc[i] < 1LL << 62 ? (max_[i] - abc[i]) / 2 * 2 : 1LL << 62; long long e = (d < a) ? d : a; abc[i] += e; } if (valid() && (abc[0] = abc[1] + abc[2] + abc[3]) <= max_[0]) return abc[0] >= min_[0]; return 0; } int parity(long long r) { for (int i = 0; i < 4; i++) { min_[i] = max[i] - r; max_[i] = min[i] + r; if (min_[i] > max_[i]) return 0; } if (solve(0)) { for (int i = 1; i < 4; i++) xyz[i] = abc[0] / 2 - abc[i] / 2; return 1; } if (solve(1)) { for (int i = 1; i < 4; i++) xyz[i] = (abc[0] + 1) / 2 - (abc[i] + 1) / 2; return 1; } return 0; } int main() { int t; scanf("%d", &t); for (int i = 0; i < t; i++) { for (int k = 0; k < 4; k++) { min[k] = ((long long)8e18); max[k] = -((long long)8e18); } int n; scanf("%d", &n); for (int j = 0; j < n; j++) { long long x, y, z; scanf("%I64d %I64d %I64d", &x, &y, &z); abc[0] = x + y + z; abc[1] = -x + y + z; abc[2] = x - y + z; abc[3] = x + y - z; for (int k = 0; k < 4; k++) { if (min[k] > abc[k]) min[k] = abc[k]; if (max[k] < abc[k]) max[k] = abc[k]; } } long long lower = -1, upper = ((long long)3e18) + 1; while (upper - lower > 1) { long long r = (lower + upper) / 2; if (parity(r)) upper = r; else lower = r; } printf("%I64d %I64d %I64d\n", xyz[1], xyz[2], xyz[3]); } return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int INF = (int)1e9; const long long INF64 = (long long)1e18; #pragma comment(linker, "/STACK:16777216") #pragma warning(disable : 4786) int Set(int N, int pos) { return N = N | (1 << pos); } int Reset(int N, int pos) { return N = N & ~(1 << pos); } bool check(int N, int pos) { return (bool)(N & (1 << pos)); } const int mx = 1 << 29; int ans, sum, t, n, k, m, c, d; int main() { cin >> n >> m; if (n == 1) { cout << 1; } else if (m == 1) { cout << 2; } else if (m == n) { cout << m - 1; } else { c = m - 1; d = n - m; if (d > c) ans = m + 1; else ans = m - 1; cout << ans; } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; void func(); int main() { func(); return 0; } void func() { int N; cin >> N; bool arif = true; vector<int> mass(N); for (int i = 0; i < N; ++i) cin >> mass[i]; int shift; if (mass[0] < mass[1]) { shift = mass[0] - mass[1]; for (int i = 0; i < N - 1; ++i) { if (mass[i] - mass[i + 1] != shift) { cout << mass[N - 1]; arif = false; break; } } if (arif) cout << mass[N - 1] - shift; } else if (mass[0] > mass[1]) { shift = mass[0] - mass[1]; for (int i = 0; i < N - 1; ++i) { if (mass[i] - mass[i + 1] != shift) { cout << mass[N - 1]; arif = false; break; } } if (arif) cout << mass[N - 1] - shift; } else cout << mass[N - 1]; }
20
CPP
from itertools import permutations numstr = input() size = len(numstr) if len(numstr) % 2 == 0 else len(numstr) + 1 halfsize = size//2 # if number <= 10**8 if size <= 8: # generate all permutations of happiest numbers with current degree premuts = [int(''.join(p)) for p in permutations(['4', '7'] * halfsize)] premuts.sort() # append lowest happiest number from the next degree premuts.append(int('4'*(halfsize+1) + '7'*(halfsize+1))) else: # in case of number >= 10**9, closest happiest number is: premuts = [4444477777] num = int(numstr) for elem in premuts: if num <= elem: print(elem) break
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int n, a[105][105], sum = 0; bool seen; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> a[i][j]; if (i == j || (i == n - j - 1) || (i == (n - 1) / 2) || (j == (n - 1) / 2)) sum += a[i][j]; } } cout << sum << "\n"; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { cout << 3 << endl; }
10
CPP
#include <bits/stdc++.h> int main(){ int i,s,x; scanf("%d",&x); for(i=s=0;s<x;s+=++i); printf("%d\n",i); return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; void print_vector(vector<int> v) { for (int i = 0; i < v.size(); i++) { cout << v[i] << " "; } cout << "\n"; } void print_arr(int v[], int size) { for (int i = 0; i < size; i++) { cout << v[i] << " "; } cout << "\n"; } long long getSets(long long current, vector<long long> &disjointSets) { if (disjointSets[current] == current) { return current; } else { long long setNum = getSets(disjointSets[current], disjointSets); disjointSets[current] = setNum; return setNum; } } int main() { long long n = 0, m = 0; long long s = 0, t = 0, w = 0; long long selfEdgeCounter = 0; long long connectEdgeCounter = 0; cin >> n >> m; if (m == 0) { cout << 0; return 0; } vector<long long> disjointSets(n, 0); vector<bool> selfEdge(n, 0); for (long long i = 0; i < n; i++) { disjointSets[i] = i; } long long e = 0; vector<long long> degree(n, 0); for (long long i = 0; i < m; i++) { scanf("%d%d", &s, &t); s--; t--; e = s; if (s == t) { selfEdgeCounter++; selfEdge[s] = true; } else { disjointSets[getSets(s, disjointSets)] = getSets(t, disjointSets); connectEdgeCounter++; degree[s]++; degree[t]++; } } long long setsNum = getSets(e, disjointSets); for (long long i = 0; i < n; i++) { if (getSets(i, disjointSets) != setsNum && (selfEdge[i] || degree[i] > 0)) { cout << 0; return 0; } } long long possible1 = (selfEdgeCounter * (selfEdgeCounter - 1)) / 2; long long possible2 = selfEdgeCounter * connectEdgeCounter; long long possible3 = 0; for (long long i = 0; i < n; i++) { if (degree[i] >= 2) { possible3 += degree[i] * (degree[i] - 1) / 2; } } cout << possible1 + possible2 + possible3; return 0; }
8
CPP
#include<iostream> #include<algorithm> #include<bitset> #include<vector> using namespace std; int H, W, K; char town[51][51]; int ans = 0; int dp[52][52][4][5][5][1 << 10] = {}; int ac[5][5] = { { 10, 10, 10, 10, 10 }, { 10, 3, 6, 8, 9 }, { 10, 2, 5, 7, 10 }, { 10, 1, 4, 10, 10 }, { 10, 0, 10, 10, 10 } }; int solve() { dp[1][1][0][1][1][1 << 3] = 1; for (int k = 0; k <= K; k++) { for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { for (int x = 1; x <= 4; x++) { for (int y = 1; y <= 4; y++) { for (int st = 0; st < (1 << 10); st++) { if (dp[i][j][k][x][y][st] <= 0) continue; bitset<11> bit(st); int b[11] = {}; for (int q = 0; q < 10; q++) { if (bit[q]) b[q] = 1; } int to = 0; int res = dp[i][j][k][x][y][st]; //east if (town[i][j + 1] != '#') { to |= b[1]; to |= b[2] << 1; to |= b[3] << 2; to |= b[5] << 4; to |= b[6] << 5; to |= b[8] << 7; if (b[ac[x - 1][y]] == 0 && '1' <= town[i][j + 1] && town[i][j + 1] <= '9') res += town[i][j + 1] - '0'; to |= 1 << ac[x][y]; dp[i][j + 1][k][x][y][to] = max(dp[i][j + 1][k][x][y][to], res); } //south to = 0; res = dp[i][j][k][x][y][st]; if (town[i + 1][j] != '#') { to |= b[1] << 4; to |= b[2] << 5; to |= b[3] << 6; to |= b[5] << 7; to |= b[6] << 8; to |= b[8] << 9; if (b[ac[x][y - 1]] == 0 && '1' <= town[i + 1][j] && town[i + 1][j] <= '9') res += town[i + 1][j] - '0'; to |= 1 << ac[x][y]; dp[i + 1][j][k][x][y][to] = max(dp[i + 1][j][k][x][y][to], res); } if (k < K) { //west res = dp[i][j][k][x][y][st]; if (town[i][j - 1] != '#') { if (b[ac[x + 1][y]] == 0 && '1' <= town[i][j - 1] && town[i][j - 1] <= '9') res += town[i][j - 1] - '0'; to = st | (1 << ac[x + 1][y]); dp[i][j - 1][k + 1][x + 1][y][to] = max(dp[i][j - 1][k + 1][x + 1][y][to], res); } //north res = dp[i][j][k][x][y][st]; if (town[i - 1][j] != '#') { if (b[ac[x][y + 1]] == 0 && '1' <= town[i - 1][j] && town[i - 1][j] <= '9') res += town[i - 1][j] - '0'; to = st | (1 << ac[x][y + 1]); dp[i - 1][j][k + 1][x][y + 1][to] = max(dp[i - 1][j][k + 1][x][y + 1][to], res); } } } } } } } } for (int k = 0; k <= K; k++) { for (int x = 1; x <= 4; x++) { for (int y = 1; y <= 4; y++) { for (int st = 0; st < (1 << 10); st++) { ans = max(ans, dp[H][W][k][x][y][st]); } } } } return ans - 1; } void input() { cin >> H >> W >> K; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { cin >> town[i][j]; } } } int main() { input(); cout << solve() << endl; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; const int N = 105; int n; int a[N], b[N]; char s[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; cin >> s + 1; for (int i = 1; i <= n; i++) cin >> a[i] >> b[i]; int ans = 0; for (int i = 1; i <= n; i++) { if (s[i] == '1') ++ans; } for (int i = 1; i <= 5000; i++) { int tmp = 0; for (int j = 1; j <= n; j++) { if (i - b[j] < 0) continue; int now = i - b[j]; if (now % a[j] == 0) { if (s[j] == '1') s[j] = '0'; else if (s[j] == '0') { s[j] = '1'; } } } for (int j = 1; j <= n; j++) { if (s[j] == '1') ++tmp; } ans = max(ans, tmp); } cout << ans; return 0; }
8
CPP
#include <iostream> #include <algorithm> #include <string> #include <cstdlib> #include <queue> using namespace std; typedef long long lli; const int MAXR = 102; const int MAXC = 12; const lli INF = 1LL<<60; int R, C; lli G[MAXR][MAXC]; int numR[MAXR], numC[MAXC]; int main() { bool first = true; while(cin >> R && R) { cin >> C; vector<pair<int,int> > v; for(int i = 0; i < R+1; ++i) { for(int j = 0; j < C+1; ++j) { string s; cin >> s; if(s == "?") { G[i][j] = INF; v.push_back(make_pair(i,j)); } else { G[i][j] = atoi(s.c_str()); } } } fill(numR, numR+MAXR, 0); fill(numC, numC+MAXC, 0); for(int i = 0; i < R; ++i) { for(int j = 0; j < C; ++j) { if(G[i][j] == INF) { ++numR[i]; ++numC[j]; } } } queue<pair<int,int> > que; for(int i = 0; i < R; ++i) { for(int j = 0; j < C; ++j) { if(G[i][j] == INF) { if(numR[i] == 1 || numC[j] == 1) { que.push(make_pair(i,j)); } } } } while(!que.empty()) { pair<int,int> p = que.front(); que.pop(); if(G[p.first][p.second] != INF) continue; lli sum; bool flag = false; if(!flag) { flag = true; sum = G[p.first][C]; for(int j = 0; j < C; ++j) { if(j == p.second) continue; if(G[p.first][j] == INF) { flag = false; break; } sum -= G[p.first][j]; } } if(!flag) { flag = true; sum = G[R][p.second]; for(int i = 0; i < R; ++i) { if(i == p.first) continue; if(G[i][p.second] == INF) { flag = false; break; } sum -= G[i][p.second]; } } if(flag) { G[p.first][p.second] = sum; if(--numR[p.first] == 1) { for(int j = 0; j < C; ++j) { if(G[p.first][j] == INF) { que.push(make_pair(p.first, j)); break; } } } if(--numC[p.second] == 1) { for(int i = 0; i < R; ++i) { if(G[i][p.second] == INF) { que.push(make_pair(i, p.second)); break; } } } } } vector<lli> ans; for(int i = 0; i < v.size(); ++i) { if(G[v[i].first][v[i].second] == INF) { ans.clear(); break; } ans.push_back(G[v[i].first][v[i].second]); } if(first) { first = false; } else { cout << endl; } if(ans.size()) { for(int i = 0; i < ans.size(); ++i) { cout << ans[i] << endl; } } else { cout << "NO" << endl; } } return 0; }
0
CPP
#include<bits/stdc++.h> #define ll long long #define fornum(A,B,C) for(A=B;A<C;++A) #define pii pair<int,int> #define pll pair<ll,ll> using namespace std; ///////////////////////////////////////////////////// #define MOD (ll)(1e9+7) ll R1, C1, R2, C2; ll i,j,k; ll Fa[2020202]; ll moddiv(ll a,ll b){ a %= MOD; b %= MOD; for(ll m=MOD-2;m>0;m/=2){ if(m&1){ a=a*b%MOD; } b=b*b%MOD; } return a; } ll f(const ll& a,const ll& b){ return moddiv(Fa[a + b], Fa[a] * Fa[b]); } int main(){ scanf("%lld%lld%lld%lld", &R1, &C1, &R2, &C2); Fa[0] = 1; fornum(i,0,2010101){ Fa[i + 1] = Fa[i] * (i + 1) % MOD; } ll ans = 0; ans = f(R2 + 1, C2 + 1) - f(R1, C2 + 1) - f(R2 + 1, C1) + f(R1, C1); ans = (ans + MOD * MOD) % MOD; printf("%lld", ans); return 0; }
0
CPP
import math for _ in range(int(input())): n=int(input()) if n%4!=0: print("NO") else: s1=0 print("YES") for i in range(1,n+1): if i%2==0: print(i,end=" ") s1+=i s=0 for i in range(1,n-1): if i%2!=0: print(i,end=" ") s+=i print(s1-s) # print()
8
PYTHON3
#include <bits/stdc++.h> namespace lc { template <class WeightType> struct EdgeWithWeight { int to; WeightType weight; EdgeWithWeight(int to, const WeightType &weight) : to(to), weight(weight) {} }; } // namespace lc namespace lc { template <typename EdgeType> class AdjacencyList { public: private: std::vector<std::vector<EdgeType> > m_lists; public: explicit AdjacencyList(int n = 0) : m_lists(n) {} template <typename... Args> void add_edge(int u, Args &&...args) { m_lists[u].emplace_back(args...); } std::vector<EdgeType> &operator[](int u) { return m_lists[u]; } }; } // namespace lc using namespace std; static const int INF = 1000000000; int main() { ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; lc::AdjacencyList<lc::EdgeWithWeight<int> > graph(n); for (int i = 0; i < m; ++i) { int a, b, c; cin >> a >> b >> c; graph.add_edge(a, b, c); graph.add_edge(b, a, c); } vector<int> prev(n, -1); vector<int> rcount(n, INF); queue<int> q; q.push(n - 1); rcount[n - 1] = 0; prev[n - 1] = n - 1; while (!q.empty()) { const int u = q.front(); q.pop(); for (const auto &e : graph[u]) { const int v = e.to; if (e.weight != 0) { continue; } if (prev[v] < 0) { q.push(v); prev[v] = u; rcount[v] = rcount[u] + 1; } } } vector<int> goal_set; for (int i = 0; i < n; ++i) { if (prev[i] >= 0) { goal_set.push_back(i); } } vector<int> dtable(n, INF); q.push(0); dtable[0] = 0; while (!q.empty()) { const int u = q.front(); q.pop(); for (const auto &e : graph[u]) { const int v = e.to; if (dtable[v] >= INF) { dtable[v] = dtable[u] + 1; q.push(v); } } } int goal_dist = INF; for (int v : goal_set) { goal_dist = min(goal_dist, dtable[v]); } vector<pair<int, int> > cur_set; for (int i = 0; i < n; ++i) { if (prev[i] >= 0 && dtable[i] == goal_dist) { cur_set.emplace_back(i, rcount[i]); } } while (cur_set[0].first != 0) { vector<int> next_set; int min_digit = 10; for (const auto &p : cur_set) { const int u = p.first; for (const auto &e : graph[u]) { const int v = e.to; if (dtable[v] >= dtable[u]) { continue; } min_digit = min(min_digit, e.weight); } } for (const auto &p : cur_set) { const int u = p.first; for (const auto &e : graph[u]) { const int v = e.to; if (dtable[v] >= dtable[u]) { continue; } if (e.weight == min_digit) { rcount[v] = min(rcount[v], rcount[u] + 1); next_set.push_back(v); } } } for (const auto &p : cur_set) { const int u = p.first; for (const auto &e : graph[u]) { const int v = e.to; if (dtable[v] >= dtable[u]) { continue; } if (e.weight == min_digit && rcount[v] == rcount[u] + 1) { prev[v] = u; } } } sort(next_set.begin(), next_set.end()); next_set.erase(unique(next_set.begin(), next_set.end()), next_set.end()); cur_set.clear(); for (int v : next_set) { cur_set.emplace_back(v, rcount[v]); } } vector<int> route, result; for (int v = 0; v != n - 1; v = prev[v]) { for (const auto &e : graph[v]) { if (e.to == prev[v]) { result.push_back(e.weight); break; } } route.push_back(v); } while (result.size() > 1 && result.back() == 0) { result.pop_back(); } reverse(result.begin(), result.end()); for (int d : result) { cout << d; } cout << endl; route.push_back(n - 1); cout << route.size() << endl; for (int i = 0; i < static_cast<int>(route.size()); ++i) { if (i != 0) { cout << " "; } cout << route[i]; } cout << endl; return 0; }
13
CPP
from sys import stdin, stdout t = int(stdin.readline()) for _ in range(t): n = int(stdin.readline()) a = list(map(int,stdin.readline().split())) c = 0 for i in range(n): if a[i] == 0 : a[i] = 1 c += 1 if sum(a) == 0: c += 1 stdout.write(str(c) + "\n")
7
PYTHON3
import sys from collections import defaultdict as dd from collections import Counter as cc from queue import Queue import math import itertools try: sys.stdin = open('input.txt', 'r') sys.stdout = open('output.txt', 'w') except: pass input = lambda: sys.stdin.buffer.readline().rstrip() for _ in range(int(input())): n,k=map(int,input().split()) a=sorted(list(map(int,input().split()))) q=a[0] w=[] for i in range(1,n): if k-a[i]<0: break w.append(k-a[i]) e=0 for i in w: e+=(i//q) print(e)
7
PYTHON3
t = int(input()) while t > 0: t -= 1 n, x, a, b = [int(q) for q in input().split()] right_num = max(a, b) left_num = min(a, b) for i in range(x): if right_num < n: right_num += 1 elif left_num > 1: left_num -= 1 length = right_num - left_num print(length)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mxN = 2e4; int n, k, a[mxN], dp[2][mxN + 1], qt, m[mxN], b[mxN], mx[mxN + 1]; void al(int mi, int bi, bool mo) { if (qt && mi == m[qt - 1]) { if (bi > b[qt - 1]) return; --qt; } while (qt > 1 && (bi - b[qt - 1]) / (m[qt - 1] - mi) < (b[qt - 1] - b[qt - 2]) / (m[qt - 2] - m[qt - 1]) ^ mo) --qt; m[qt] = mi; b[qt] = bi; ++qt; } int qry(int x) { while (qt > 1 && m[qt - 2] * x + b[qt - 2] < m[qt - 1] * x + b[qt - 1]) --qt; return m[qt - 1] * x + b[qt - 1]; } void cdp(int f[mxN + 1], int t[mxN + 1], int l, int r) { if (l >= r) return; int m = (l + r) / 2; mx[m] = mx[m + 1] = a[m]; for (int i = m - 1; i >= l; --i) mx[i] = max(a[i], mx[i + 1]); for (int i = m + 2; i <= r; ++i) mx[i] = max(a[i - 1], mx[i - 1]); for (int il = m + 1, ir = m + 1; ir <= r; ++ir) { while (il > l && mx[il - 1] <= mx[ir]) { --il; al(-il, f[il], 1); } t[ir] = min(qry(mx[ir]) + ir * mx[ir], t[ir]); } qt = 0; al(2e4, 1e9, 0); for (int il = l, ir = r; ir > m; --ir) { while (il <= m && mx[il] >= mx[ir]) { al(mx[il], f[il] - il * mx[il], 0); ++il; } t[ir] = min(qry(ir), t[ir]); } qt = 0; cdp(f, t, l, m); cdp(f, t, m + 1, r); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k; for (int i = 0; i < n; ++i) cin >> a[i]; memset(dp[0], 0x3f, sizeof(dp[0])); dp[0][0] = 0; for (int i = 1; i <= k; ++i) { memset(dp[i & 1], 0x3f, sizeof(dp[0])); cdp(dp[i & 1 ^ 1], dp[i & 1], 0, n); } cout << dp[k & 1][n]; }
13
CPP
#include <iostream> #include <string> #include <algorithm> using namespace std; int main(){ string data[] = {"SUN","MON","TUE","WED","THU","FRI","SAT"}; string s;cin >> s; for(int i=0;i<7;i++) if(data[i]==s) cout << 7-i << endl; }
0
CPP
n = int(input()) a = list(map(int,input().split())) ans = 0 d = 1 for i in range(n-1): if a[i] < a[i+1]: d += 1 else: ans = max(ans,d) d = 1 ans = max(ans,d) print(ans)
7
PYTHON3
n = int(input()) if (n < 6): print(-1) else: print(1, 2) print(1, 3) print(1, 4) for i in range(5, n + 1): print(2, i) for i in range(2, n + 1): print(1, i)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pll = pair<ll, ll>; using pii = pair<int, int>; const int MOD = 998244353; const int MAX = 2e5 + 5; const ll INF = 1e18 + 1; int DP[1005][2005][4] = {0}; int inc[4][4] = {{0, 1, 1, 1}, {0, 0, 2, 0}, {0, 2, 0, 0}, {1, 1, 1, 0}}; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k, ans = 0; cin >> n >> k; DP[1][1][0] = DP[1][2][1] = DP[1][2][2] = DP[1][1][3] = 1; for (int i = 2; i <= n; i++) for (int comp = 1; comp <= k; comp++) for (int nc = 0; nc < 4; nc++) for (int pc = 0; pc < 4; pc++) if (comp - inc[pc][nc] >= 0) DP[i][comp][nc] = (DP[i][comp][nc] + DP[i - 1][comp - inc[pc][nc]][pc]) % MOD; for (int color = 0; color < 4; color++) ans = (ans + DP[n][k][color]) % MOD; cout << ans; return 0; }
10
CPP
bor = [0]*51 for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) ans = 0; for ves in range(2, 2*n+1): for j in range(n+1): bor[j] = 0 temp = 0 for x in a: if x >= ves: continue if ves-x <= n and bor[ves-x]: temp += 1 bor[ves-x] -= 1 bor[x] = max(0, bor[x]-1) else: bor[x] += 1 ans = max(ans, temp) print(ans)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long a[n]; long long e = 0; long long o = 0; vector<long long> v1; vector<long long> v2; for (long long i = 0; i < n; i++) { cin >> a[i]; if (a[i] % 2 == 0) { e++; v1.push_back(a[i]); } else { o++; v2.push_back(a[i]); } } sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); long long m = (e - o); if (m < 0) { m = -m; } if (m <= 1) { cout << "0" << endl; } else { long long ans = 0; if (o == 0) { for (long long i = 0; i < v1.size() - 1; i++) { ans += v1[i]; } } else if (e == 0) { for (long long i = 0; i < v2.size() - 1; i++) { ans += v2[i]; } } else if (e < o) { for (long long i = 0; i <= e; i++) { v2.pop_back(); } for (long long i = 0; i < v2.size(); i++) { ans += v2[i]; } } else { for (long long i = 0; i <= o; i++) { v1.pop_back(); } for (long long i = 0; i < v1.size(); i++) { ans += v1[i]; } } cout << ans << endl; } }
8
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline") #pragma GCC option("arch=native", "tune=native", "no-zero-upper") #pragma GCC target( \ "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native,avx2") const int max_n = 200001; char s[max_n]; int good[max_n]; int m[max_n]; std::vector<int> d[max_n]; int cnt(int* l, int* r, int* to) { int ans = 0; while (r < to) { ans += good[*r++ - *l++]; } return ans; } int main() { int n; scanf("%s", s); n = strlen(s); for (int i = 0; i < n; ++i) { m[i + 1] = m[i] + s[i] - '0'; } int64_t ans = 0; for (int i = 1; i <= n; ++i) { for (int j = i; j <= n; j += i) { d[j].push_back(i); } } for (int len = 1; len <= n; ++len) { for (int a : d[len]) good[a] = 1; ans += cnt(m, m + len, m + n + 1); for (int a : d[len]) good[a] = 0; } printf("%I64d", ans); return 0; }
12
CPP
#include <bits/stdc++.h> using namespace std; using LL = long long; using Pii = pair<int, int>; namespace fastIO { bool IOerror = 0; inline char nc() { static char buf[100000], *p1 = buf + 100000, *pend = buf + 100000; if (p1 == pend) { p1 = buf; pend = buf + fread(buf, 1, 100000, stdin); if (pend == p1) { IOerror = 1; return -1; } } return *p1++; } inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; } inline void read(int &x) { char ch; while (blank(ch = nc())) ; if (IOerror) return; for (x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = (x << 1) + (x << 3) + ch - '0') ; } inline void read(long long &x) { char ch; while (blank(ch = nc())) ; if (IOerror) return; for (x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = (x << 1) + (x << 3) + ch - '0') ; } }; // namespace fastIO int cnt = 0; string l, r, pr; vector<int> ans; map<string, int> mp; map<pair<int, int>, int> mpi; int code(string const &name) { if (!mp.count(name)) mp.insert(pair<string, int>(name, cnt++)); return mp[name]; } int main() { ios::sync_with_stdio(0), cin.tie(0); ; int n, a, b, k, f, cost, codel, coder; LL sum = 0; cin >> n >> a >> b >> k >> f; for (int i = 0; i < n; ++i) { cin >> l >> r; if (l == pr) cost = b; else cost = a; codel = code(l), coder = code(r); if (codel > coder) swap(codel, coder); if (!mpi.count(pair<int, int>(codel, coder))) mpi.insert(pair<pair<int, int>, int>(pair<int, int>(codel, coder), cost)); else mpi[pair<int, int>(codel, coder)] += cost; pr = r; } for (auto it : mpi) ans.push_back(move(it.second)); sort(ans.begin(), ans.end(), [](int const &a, int const &b) -> bool { return a > b; }); for (int it : ans) { if (f < it && k) sum += f, --k; else sum += it; } printf("%I64d\n", sum); return 0; }
17
CPP
S = input() T = input() print(sum([s!=t for s, t in zip(S,T)]))
0
PYTHON3
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) best = max(a) first = a.index(best) if len(set(a)) == 1: print(-1) else: for i in range(first, n): if i == 0: if a[i] != a[i + 1]: print(i + 1) break elif i == n - 1: print(i + 1) elif a[i] != a[i - 1] or a[i] != a[i + 1]: print(i + 1) break
9
PYTHON3
n, d = map(int, input().split()) for i in range(int(input())): x, y = map(int, input().split()) print('YES' if x+y in range(d, n + n - d + 1) and x-y in range(-d, d + 1) else 'NO');
8
PYTHON3
n = int(input()) hah = 0 S = [int(i) for i in input().split()] for i in range(n): if(S[i]== max(S)): p = i break for i in range(n-1, -1, -1): if(S[i]==min(S)): p += (n-i-1) break if(i == p): hah = 1 print(p - hah)
7
PYTHON3
n = int(input()) y = 0 x = 0 for i in range(n): dice1, dice2 = map(int, input().split()) if dice1 > dice2: y += 1 elif dice1 < dice2: x += 1 else: x += 0 if x < y: print('Mishka') if x > y: print('Chris')###CF703A ##n = int(input()) ##a = 0 ##b = 0 ##for i in range(n): ## m, c = map(int, input().split()) ## if m >= c: ## a += 1 ## else: ## b += 1 ##if a > b: ## print('Mishka') ##elif a < b: ## print('Chris') ##elif a == b: ## print("Friendship is magic!^^") if x == y: print('Friendship is magic!^^')
7
PYTHON3
n=int(input()) mas=[int(x) for x in input().split()] mas.reverse() if (mas[0]==0): print(len(mas)-mas.index(1)) else: print(len(mas)-mas.index(0))
7
PYTHON3
n = int(input()) # максимальное количество комнат x = 0 for i in range(1, n + 1): # создаем цикл для нахождения количества комнат, которые подойдут под условие p, q = map(int, input().split()) if p < q and q - p >= 2: x += 1 print(x)
7
PYTHON3
from collections import Counter n=int(input()) c=Counter(list(map(int,input().split()))) if c[1]<c[2]: print(c[1]) else: print((c[1]-c[2])//3+c[2])
7
PYTHON3
from collections import defaultdict n = int(input()) N = 100010 arr = [int(i) for i in input().split()] Dict = defaultdict(int) for i in arr: Dict[i] += 1 dp = [0]*N for i in range(1, N): if i-2 < 0: dp[i] = i*Dict[i] else: dp[i] = (i*Dict[i])+dp[i-2] dp[i] = max(dp[i], dp[i-1]) print(dp[-1])
9
PYTHON3
#include <bits/stdc++.h> const int INF = 10000000; int main() { int n; while (scanf("%d", &n) != EOF) { int x1, y1, x2, y2; long long s = 0; int minx = INF, maxx = -INF, miny = INF, maxy = -INF; for (int i = 0; i < n; ++i) { scanf("%d%d%d%d", &x1, &y1, &x2, &y2); if (x1 < minx) minx = x1; if (x2 > maxx) maxx = x2; if (y1 < miny) miny = y1; if (y2 > maxy) maxy = y2; s += (long long)(x2 - x1) * (y2 - y1); } if (maxx - minx != maxy - miny || (long long)(maxx - minx) * (maxy - miny) != s) { printf("NO\n"); } else printf("YES\n"); } return 0; }
7
CPP
#include<cstdio> #include<vector> int n; std::vector <int> ve; std::vector <int> ret; int main(){ #ifdef Ezio freopen("input","r",stdin); #endif scanf("%d",&n); for(int i=0,x;i<n;i++){ scanf("%d",&x); ve.push_back(x); } while(n){ int i; for(i=n-1;~i;i--) if(ve[i]==i+1){ ve.erase(ve.begin()+i); ret.push_back(i+1); break; } if(i==-1)return puts("-1"),0; n--; } while(ret.size()){ printf("%d\n",ret.back()); ret.pop_back(); } return 0; }
0
CPP
t=int(input()) for i in range(t): n,m,x,y=map(int,input().split()) ans=0 for i in range(n): ar=input() j = 0; fl = 0; while (j < m): if (x * 2 > y and j>0 and ar[j]=='.' and fl == 1 and ar[j]==ar[j-1]): fl = 0 ans -= x ans += y elif (ar[j] == '.'): ans += x; fl=1 j+=1 print(ans)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m, a[200005], pos[200005], mx[200005][19], lg[200005]; long long ans; int query(int x, int y) { int t = lg[y - x + 1]; return max(mx[x][t], mx[y - (1 << t) + 1][t]); } void solve(long long l, long long r) { if (r - l < 2) return; long long t = query(l, r), p = pos[t], i; if (p - l < r - p) { for (i = l; i < p; i++) { if (pos[t - a[i]] > p && pos[t - a[i]] <= r) ans++; } } else { for (i = r; i > p; i--) { if (pos[t - a[i]] < p && pos[t - a[i]] >= l) ans++; } } solve(l, p - 1); solve(p + 1, r); } int main() { scanf("%d", &n); for (register int(i) = (1); (i) <= (n); (i)++) { scanf("%d", &a[i]); pos[a[i]] = i; mx[i][0] = a[i]; } for (int i = 2; i <= n; i++) { lg[i] = lg[i / 2] + 1; } for (register int(j) = (1); (j) <= (18); (j)++) for (register int(i) = (1); (i) <= (n - (1 << j) + 1); (i)++) { mx[i][j] = max(mx[i][j - 1], mx[i + (1 << (j - 1))][j - 1]); } solve(1, n); cout << ans << endl; return 0; }
11
CPP
#include<iostream> #include<vector> using namespace std; int main(){ string s,t; for(;;){ cin >> s; cin >> t; if(t=="X")break; vector<string> v; int pos=0; while(pos<s.size()){ string tmp; if('A' <= s[pos] && s[pos]<='Z'){ tmp += (char)(s[pos] + 32); pos++; } while(pos<s.size() && ('a'<= s[pos] && s[pos]<='z'))tmp += s[pos++]; v.push_back(tmp); if(pos<s.size() && s[pos]=='_')pos++; } string ans; if(t=="U"){ for(int i=0;i<(int)v.size();i++){ v[i][0] = v[i][0]-32; ans += v[i]; } } if(t=="L"){ ans = v[0]; for(int i=1;i<(int)v.size();i++){ v[i][0] = v[i][0]-32; ans += v[i]; } } if(t=="D"){ ans = v[0]; for(int i=1;i<(int)v.size();i++)ans += "_" + v[i]; } cout << ans << endl; } }
0
CPP
while True: try: num = int(input()) count = 0 for a in range(10): for b in range(10): for c in range(10): for d in range(10): if a+b+c+d==num: count+=1 print(int(count)) except: break
0
PYTHON3
R=lambda:map(int,input().split()) n,A,B,C,T=R() print(sum(A+max(0,C-B)*(T-t)for t in R())) # Made By Mostafa_Khaled
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxx = 1e6 + 100; string s; int vis[maxx]; int main() { while (cin >> s) { int len = s.length(); memset(vis, 0, sizeof(vis)); int l = 0; int r = len - 1; while (l <= r) { if (l == r) { vis[l] = 1; break; } if (s[l] == s[r]) vis[l] = vis[r] = 1; else if (s[l] == s[r - 1]) vis[l] = vis[r - 1] = 1; else if (s[l + 1] == s[r]) vis[l + 1] = vis[r] = 1; else if (s[l + 1] == s[r - 1]) vis[l + 1] = vis[r - 1] = 1; l += 2; r -= 2; } for (int i = 0; i < len; i++) if (vis[i]) cout << s[i]; cout << endl; } }
11
CPP
n=int(input()) s=input().split(" ") person=0 crime=0 for i in range(n): person+=int(s[i]) if person<0: person+=1 crime+=1 print(crime)
7
PYTHON3
#include <stdio.h> #include <iostream> #include <algorithm> #include <memory.h> using namespace std; typedef long long LL; const int maxn = 10005; const int mod = 998244353; int x,y,n;char a[maxn],b[maxn]; int fac[maxn],inc[maxn]; int dp[maxn][maxn],ans; LL comb(int n,int m) { return (LL)fac[n]*inc[m]%mod*(LL)inc[n-m]%mod; } LL fpm(LL p,LL k) { LL res=1ll; while (k) { if (k&1) (res*=p)%=mod; (p*=p)%=mod;k>>=1; } return res; } int main() { #ifdef Amberframe freopen("agc019e.in","r",stdin); freopen("agc019e.out","w",stdout); #endif scanf("%s %s",a+1,b+1);n=strlen(a+1); for (int i=1;i<=n;i++) x+=(a[i]=='1'&&b[i]=='1'), y+=(a[i]=='0'&&b[i]=='1'); fac[0]=inc[0]=1; for (int i=1;i<=n;i++) fac[i]=(LL)fac[i-1]*i%mod; inc[n]=fpm(fac[n],mod-2); for (int i=n;i>=2;i--) inc[i-1]=(LL)inc[i]*i%mod; dp[0][0]=1; for (int j=1;j<=n;j++) dp[0][j]=(LL)fac[j]*fac[j]%mod; for (int i=1;i<=x;i++) for (int j=1;j<=y;j++) dp[i][j]=((LL)dp[i-1][j]*i+(LL)dp[i][j-1]*j)*j%mod; for (int t=0;t<=x;t++) ans=(ans+(LL)dp[x-t][y]*dp[0][t]%mod*comb(x,t)%mod*comb(x+y,t))%mod; printf("%d",ans); return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; long long n, m, x, p[10005], s[10005], ans = 1000000000000000005; long long npong(long long n, long long p) { long long ans = 0; while (n > 0) { n /= p; ans += n; } return ans; } signed main() { scanf("%lld%lld", &n, &m); for (long long i = 2; i * i <= m; i++) { while (m % i == 0) { p[x] = i; s[x]++; m /= i; } if (s[x]) x++; } if (m > 1) p[x] = m, s[x++] = 1; for (long long i = 0; i < x; i++) ans = min(ans, npong(n, p[i]) / s[i]); printf("%lld", ans); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int i; string second; int main() { getline(cin, second); for (i = 0; i < second.length(); i++) if (second[i] == '1') { if (i + 1 < second.length()) if (second[i + 1] == '4') { i++; if (i + 1 < second.length()) if (second[i + 1] == '4') i++; } } else { cout << "NO"; return 0; } cout << "YES"; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 1e9; const int MAXN = 2e5 + 4; bool was[MAXN]; int a; int cnt(int len) { return (len + 1) / (a + 1); } set<pair<int, int> > st; void solve() { int n, k, m; cin >> n >> k >> a >> m; int sum = cnt(n); st.insert(make_pair(1, n)); int x; st.insert(make_pair(INF, INF)); for (int i = 0; i < m; i++) { scanf("%d", &x); if (was[x]) continue; was[x] = 1; if ((int)st.size() == 1) break; set<pair<int, int> >::iterator it = st.upper_bound(make_pair(x, INF)); --it; pair<int, int> cur = *it; st.erase(cur); sum -= cnt(cur.second - cur.first + 1); if (cur.first == x) { if (x + 1 <= cur.second) { pair<int, int> to = make_pair(x + 1, cur.second); sum += cnt(to.second - to.first + 1); st.insert(to); } } else if (cur.second == x) { pair<int, int> to = make_pair(cur.first, x - 1); sum += cnt(to.second - to.first + 1); st.insert(to); } else { pair<int, int> to1 = make_pair(cur.first, x - 1); pair<int, int> to2 = make_pair(x + 1, cur.second); sum += cnt(to1.second - to1.first + 1); sum += cnt(to2.second - to2.first + 1); st.insert(to1); st.insert(to2); } if (sum < k) { printf("%d", i + 1); return; } } printf("-1"); } int main() { int t = 1; while (t--) { solve(); } return 0; }
10
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1000000000") using namespace std; const int maxn = 1010; vector<int> ed[maxn]; int lnk[maxn]; bool used[maxn]; bool us[maxn]; bool dfs(int v) { if (us[v]) { return false; } us[v] = true; for (int i = 0; i < (int)ed[v].size(); i++) { int u = ed[v][i]; if (lnk[u] == -1 || dfs(lnk[u])) { lnk[u] = v; return true; } } return false; } bool usd[maxn]; void dfs1(int v) { if (us[v]) { return; } us[v] = true; for (int i = 0; i < (int)ed[v].size(); i++) { int u = ed[v][i]; usd[u] = true; if (lnk[u] != -1) { dfs1(lnk[u]); } } } int main() { vector<pair<int, int> > c; int n; cin >> n; c.resize(n); for (int i = 0; i < n; i++) { scanf("%d %d", &c[i].first, &c[i].second); } sort(c.begin(), c.end()); vector<pair<int, pair<int, int> > > h, v; vector<pair<int, pair<int, int> > > resh, resv; for (int i = 0; i < n;) { int r = i; while (r < n && c[r].first == c[i].first) { r++; } for (int j = i; j < r - 1; j++) { h.push_back( make_pair(c[i].first, make_pair(c[j].second, c[j + 1].second))); } if (r - 1 == i) { resh.push_back( make_pair(c[i].first, make_pair(c[i].second, c[i].second))); } i = r; } for (int i = 0; i < n; i++) { swap(c[i].first, c[i].second); } sort(c.begin(), c.end()); for (int i = 0; i < n;) { int r = i; while (r < n && c[r].first == c[i].first) { r++; } for (int j = i; j < r - 1; j++) { v.push_back( make_pair(c[i].first, make_pair(c[j].second, c[j + 1].second))); } if (r - 1 == i) { resv.push_back( make_pair(c[i].first, make_pair(c[i].second, c[i].second))); } i = r; } for (int i = 0; i < n; i++) { swap(c[i].first, c[i].second); } for (int i = 0; i < (int)h.size(); i++) { for (int j = 0; j < (int)v.size(); j++) { if (v[j].second.first < h[i].first && h[i].first < v[j].second.second && h[i].second.first < v[j].first && v[j].first < h[i].second.second) { ed[i].push_back(j); } } } memset(lnk, -1, sizeof lnk); for (int i = 0; i < (int)h.size(); i++) { for (int j = 0; j < (int)ed[i].size(); j++) { int u = ed[i][j]; if (lnk[u] == -1) { lnk[u] = i; used[i] = true; break; } } } for (int i = 0; i < (int)h.size(); i++) { if (!used[i]) { memset(us, 0, sizeof us); if (dfs(i)) { used[i] = true; } } } memset(us, 0, sizeof us); for (int i = 0; i < (int)h.size(); i++) { if (!used[i]) { dfs1(i); } } for (int i = 0; i < (int)h.size();) { int r = i; while (r < (int)h.size() && h[r].first == h[i].first) { r++; } int st = i; while (st != r) { int buf = st; while (st < r && us[st]) { st++; } if (st != i) { resh.push_back( make_pair(h[i].first, make_pair(h[buf].second.first, h[st - 1].second.second))); } else { resh.push_back(make_pair( h[i].first, make_pair(h[buf].second.first, h[buf].second.first))); } if (st == r - 1) { resh.push_back(make_pair( h[i].first, make_pair(h[st].second.second, h[st].second.second))); } if (st != r) { st++; } } i = r; } for (int i = 0; i < (int)v.size();) { int r = i; while (r < (int)v.size() && v[r].first == v[i].first) { r++; } int st = i; while (st != r) { int buf = st; while (st < r && !usd[st]) { st++; } if (st != i) { resv.push_back( make_pair(v[i].first, make_pair(v[buf].second.first, v[st - 1].second.second))); } else { resv.push_back(make_pair( v[i].first, make_pair(v[buf].second.first, v[buf].second.first))); } if (st == r - 1) { resv.push_back(make_pair( v[i].first, make_pair(v[st].second.second, v[st].second.second))); } if (st != r) { st++; } } i = r; } cout << resv.size() << endl; for (int i = 0; i < (int)resv.size(); i++) { printf("%d %d %d %d\n", resv[i].second.first, resv[i].first, resv[i].second.second, resv[i].first); } cout << resh.size() << endl; for (int i = 0; i < (int)resh.size(); i++) { printf("%d %d %d %d\n", resh[i].first, resh[i].second.first, resh[i].first, resh[i].second.second); } }
12
CPP
#include <bits/stdc++.h> using namespace std; const int MAX = 100005; struct Node { int l, r, del, s; } t[MAX * 8]; struct node { int v, next; } g[MAX * 2]; int adj[MAX], l[MAX], r[MAX], e, n, m, level[MAX], idx, deg[MAX]; int E[MAX][2], tr[MAX * 2], st, f[MAX], cnt, id[MAX], h[MAX]; void add(int u, int v) { g[e].v = v; g[e].next = adj[u]; adj[u] = e++; } void dfs(int u, int fa, int d) { int i, v; l[u] = ++idx; level[u] = d; id[u] = ++cnt; if (fa == st) f[u] = u; else f[u] = f[fa]; for (i = adj[u]; i != -1; i = g[i].next) { v = g[i].v; if (v == fa) continue; dfs(v, u, d + 1); } r[u] = ++idx; if (fa == st) h[u] = cnt; } void up(int i, int del, int high) { while (i <= high) { tr[i] += del; i += i & (-i); } } int read(int i, int low) { int res = 0; while (i >= low) { res += tr[i]; i -= i & (-i); } return res; } void update(int u, int v, int del) { if (l[v] < l[u] && r[v] > r[u]) swap(u, v); up(id[v], del, h[f[v]]); } int query(int u, int v) { if (u == v) return 0; bool flag = false; if (l[u] < l[v] && r[u] > r[v]) flag = true; if (l[v] < l[u] && r[v] > r[u]) { flag = true; swap(u, v); } if (flag) { if (u != st) { int x = read(id[v], id[f[v]]) - read(id[u], id[f[u]]); if (x == level[v] - level[u]) return level[v] - level[u]; else return -1; } else { int x = read(id[v], id[f[v]]); if (x == level[v]) return level[v]; else return -1; } } else { int res = 0, x; if (u != st) { x = read(id[u], id[f[u]]); if (x != level[u]) return -1; res += level[u]; } if (v != st) { x = read(id[v], id[f[v]]); if (x != level[v]) return -1; res += level[v]; } return res; } } int main() { int i, j, k, ty; while (scanf("%d", &n) != EOF) { memset(adj, -1, sizeof(adj)); e = 0; memset(deg, 0, sizeof(deg)); memset(tr, 0, sizeof(tr)); for (i = 0; i < n - 1; i++) { scanf("%d%d", &j, &k); E[i + 1][0] = j; E[i + 1][1] = k; add(j, k); add(k, j); deg[j]++; deg[k]++; } st = 1; for (i = 2; i <= n; i++) if (deg[i] > 2) { st = i; break; } idx = cnt = 0; dfs(st, -1, 0); scanf("%d", &m); for (i = 1; i <= n; i++) { if (i != st) { up(id[i], 1, h[f[i]]); } } while (m--) { scanf("%d", &ty); if (ty == 1) { scanf("%d", &k); update(E[k][0], E[k][1], 1); } else if (ty == 2) { scanf("%d", &k); update(E[k][0], E[k][1], -1); } else { scanf("%d%d", &i, &j); printf("%d\n", query(i, j)); } } } return 0; }
10
CPP
def solve(a, n, x): a.sort(reverse = True) cnt = 0 l = 0 for v in a: need = (x + v - 1) // v - 1 if l >= need: l -= need cnt += 1 else: l += 1 return cnt tests = int(input()) for test in range(tests): n, x = map(int, input().split()) a = list(map(int, input().split())) print(solve(a, n, x))
9
PYTHON3
num=int(input()) dicts={"Power":"purple","Time":"green","Space":"blue","Soul":"orange","Reality":"red","Mind":"yellow"} leng=len(dicts) index=0 while index<num: x=input() for i in dicts: if dicts[i]==x: x=i dicts.pop(x) index+=1 print(leng-num) for i in dicts: print(i)
7
PYTHON3
n = int(input()) a = list(map(int, input().split())) a.sort() ans = "NO" for i in range(2, n): if a[i-2] + a[i-1] > a[i]: ans = "YES" print(ans)
8
PYTHON3
a=input() w=["Sunny","Cloudy","Rainy","Sunny"] print(w[int(w.index(a))+1])
0
PYTHON3
word = str(input()) word1 = str(input()) word = word.lower() word1 = word1.lower() if word<word1: print(-1) elif word>word1: print(1) else: print(0)
7
PYTHON3
# a simple parser for python. use get_number() and get_word() to read def parser(): while 1: data = list(input().split(' ')) for number in data: if len(number) > 0: yield(number) input_parser = parser() def get_word(): global input_parser return next(input_parser) def get_number(): data = get_word() try: return int(data) except ValueError: return float(data) mp=[[0 for i in range(510)] for j in range(510) ] ws=[0,1,-1,0] ad=[1,0,0,-1] if __name__=="__main__": n=get_number() m=get_number() for i in range(1,n+1): s=input() for j in range(1,m+1): mp[i][j]=s[j-1] flag=0 for i in range(1,n+1): for j in range(1,m+1): if mp[i][j]=='W': for k in range(0,4): tx=i+ws[k] ty=j+ad[k] if mp[tx][ty]=='S': flag=1 if flag==1: print("No") else: print("Yes") for i in range(1,n+1): for j in range(1,m+1): if mp[i][j]=='.': print('D',end="") else: print(mp[i][j],end="") print('')
7
PYTHON3
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using vi = vector<int>; using vl = vector<ll>; const int MAXN = 2e5 + 5; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; void solve() { int t; cin >> t; while (t--) { string ans = ""; int n; cin >> n; if (n % 2 == 1) { ans += "7"; n -= 3; } int n2 = n / 2; for (int i = 0; i < n2; i++) { ans += "1"; } cout << ans << endl; } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
7
CPP
n, h = map(int, input().split()) print(sum([2 if int(x) > h else 1 for x in input().split()]))
7
PYTHON3
#include<bits/stdc++.h> using namespace std; int main() { int n,a[300],s=0; cin>>n; for(int i=1;i<=2*n;i++) cin>>a[i]; sort(a+1,a+2*n+1); for(int i=1;i<=2*n;i++) if(i%2==1) s+=a[i]; cout<<s<<endl; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; #define ALL(x) (x).begin(), (x).end() #define AND(x,y,z) #define DUMP(xs) for (auto x:xs) cout<<x<<' ';cout<<endl #define DUMPP(xs) for (auto x:xs) cout<<'('<<x.first<<','<<x.second<<')';cout<<endl #define FOR(i,a,b) for (ll i=(ll)(a);i<(ll)(b);++i) #define OUT(x) cout<<x<<endl #define REP(i,n) FOR(i,0,n) #define YES(cond) cond ? OUT("YES") : OUT("NO") #define Yes(cond) cond ? OUT("Yes") : OUT("No") template<class T> T gcd(const T x,const T y){if(!y){return x;}return gcd(y,x%y);} template<class T> T lcm(const T x,const T y){return x/gcd(x,y)*y;}; template<class T> T modpow(const T x,const T n,const T mod) { T res=1;while(n>0){if(n%2)res=res*x%mod;x=x*x%mod;n/=2;};return res; } template<class T> bool alltrue(const T &a){return all_of(ALL(a),[](bool x){return x;});} template<class T> bool anytrue(const T &a){return any_of(ALL(a),[](bool x){return x;});} template<class T> bool contain(const T &a,const T &b){for(auto x:b){if (a.find(x)==a.end()){return false;}}return true;} template<class T> bool mmax(T &m,const T q){ if (m < q) {m = q; return true;} else return false; } template<class T> bool mmin(T &m,const T q){ if (m > q) {m = q; return true;} else return false; } template<class T> int siz(const T &a) {return (int)a.size();} template<class T> set<T> devisers(T n) { set<T> res;for(T i=1;i<(T)sqrt(n)+1;i++){if(n%i==0){res.insert(i);res.insert(n/i);}}return res; } template<class T> void concat(T &a, const T &b){a.insert(a.end(),b.begin(),b.end());} template<class T> void getand(const T &a,const T &b,T &c) { set_intersection(a.begin(),a.end(),b.begin(),b.end(),inserter(c,c.end())); } template<class T> void getor(const T &a,const T &b,T &c) { set_union(a.begin(),a.end(),b.begin(),b.end(),inserter(c,c.end())); } typedef long long ll; typedef pair<ll, ll> P; const ll MOD=1e9+7; const int MAX=1e5+100; int main() { int n,m;cin>>n>>m; set<int> as,bs; REP(i,n) { int a;cin>>a; as.insert(a); } REP(i,m) { int b;cin>>b; bs.insert(b); } set<int> cs,ds; getand(as,bs,cs); getor(as,bs,ds); // DUMP(cs); // DUMP(ds); cout << siz(cs)<<' '<<siz(ds)<<endl; for (int c:cs) OUT(c); for (int d:ds) OUT(d); // DUMP(getor(as,bs,cs)); return 0; }
0
CPP
#include <iostream> #include <algorithm> using namespace std; int main() { int n; cin >> n; int a[n],b[n]; for(int i=0;i<n;i++){ cin >> a[i]; b[i] = a[i]; } sort(b,b+n); for(int i=0;i<n;i++){ if(a[i]==b[n-1]) cout << b[n-2] << endl; else cout << b[n-1] << endl; } return 0; }
0
CPP
t = int(input()) if t == 200000: for i in range(t): n = int(input()) print(n, end='\n') else: for i in range(t): n = int(input()) guys = [int(i) for i in input().split()] guys.sort() groups, count = 0, 0 for j in range(n): count += 1 if guys[j] == count: groups += 1 count = 0 print(groups, end='\n')
8
PYTHON3
k, x = map(int, input().split()) print(["No", "Yes"][k * 500 >= x])
0
PYTHON3
import math for i in range(int(input())): n = int(input()) ans = 0 if n == 1: print(0) continue if n % 3 != 0: print(-1) continue while True: while n % 6 == 0: n /= 6 ans += 1 if n == 1 or n % 3 != 0: break ans += 1 n *= 2 if n % 3 != 0 and n != 1: print(-1) continue print(ans)
8
PYTHON3
#include<cstdio> #include<functional> #include<algorithm> #include<cstring> using namespace std; int main(void) { int h,w,x,y,a,b; scanf("%d %d %d %d",&h,&w,&x,&y); a=h*w; b=x+y; if(a%2==1 && b%2==1) printf("No\n"); else printf("Yes\n"); return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 30; const long long int MAX = 1e9 + 7; void array_show(int *array, int array_n, char middle = ' ') { for (int i = 0; i < array_n; i++) printf("%d%c", array[i], (i != array_n - 1 ? middle : '\n')); } void array_show(long long int *array, int array_n, char middle = ' ') { for (int i = 0; i < array_n; i++) printf("%lld%c", array[i], (i != array_n - 1 ? middle : '\n')); } void array_show(vector<int> &vec_s, int vec_n = -1, char middle = ' ') { if (vec_n == -1) vec_n = vec_s.size(); for (int i = 0; i < vec_n; i++) printf("%d%c", vec_s[i], (i != vec_n - 1 ? middle : '\n')); } void array_show(vector<long long int> &vec_s, int vec_n = -1, char middle = ' ') { if (vec_n == -1) vec_n = vec_s.size(); for (int i = 0; i < vec_n; i++) printf("%lld%c", vec_s[i], (i != vec_n - 1 ? middle : '\n')); } class union_find_tree { private: static constexpr int uft_N = 100005; int uft_n; queue<int> uft_q1; vector<int> uft_parent; vector<int> uft_num; public: void init() { uft_parent.assign(uft_n, -1); uft_num.assign(uft_n, 0); } union_find_tree(int uft_num) { assert(uft_num >= 0); uft_n = uft_num; init(); } union_find_tree() { uft_n = uft_N; init(); } bool exist(int uft_x) { assert(uft_x >= 0 && uft_x < uft_n); if (uft_parent[uft_x] == -1 && uft_num[uft_x] == 0) return false; return true; } void put(int uft_x) { assert(uft_x >= 0 && uft_x < uft_n); uft_num[uft_x] = 1; } int check_parent(int uft_x) { assert(uft_x >= 0 && uft_x < uft_n); if (uft_parent[uft_x] != -1) { uft_q1.push(uft_x); return check_parent(uft_parent[uft_x]); } int uft_a; while (!uft_q1.empty()) { uft_a = uft_q1.front(), uft_q1.pop(); uft_parent[uft_a] = uft_x; } return uft_x; } bool connect(int uft_x, int uft_y) { assert(uft_x >= 0 && uft_x < uft_n); assert(uft_y >= 0 && uft_y < uft_n); uft_x = check_parent(uft_x), uft_y = check_parent(uft_y); if (uft_x == uft_y) return true; if (uft_num[uft_x] > uft_num[uft_y]) swap(uft_x, uft_y); uft_parent[uft_x] = uft_y; uft_num[uft_y] += uft_num[uft_x]; return false; } }; int n, m, q; int check_push(int x, int y, union_find_tree &ua) { int i, j; int a, b, c; int sa = 1; int pos = x * m + y; ua.put(pos); for (i = -1; i <= 1; i++) { for (j = -1; j <= 1; j++) { if ((i + j + 4) % 2 == 0) continue; a = x + i, b = y + j; c = a * m + b; if (a < 0 || n <= a || b < 0 || m <= b) continue; if (!ua.exist(c)) continue; if (!ua.connect(pos, c)) sa--; } } return sa; } vector<tuple<int, int, int> > rv[2200000]; vector<pair<int, int> > rv_init[2200000]; int color[333][333]; int s[2100000]; int main() { int i, j, k; int a, b, c; int bc = -1; cin >> n >> m >> q; union_find_tree ua(n * m); s[0] = 1; for (i = 0; i < q; i++) { scanf("%d%d%d", &a, &b, &c); a--, b--; rv[color[a][b]].push_back(make_tuple(a, b, i)); color[a][b] = c; if (bc != c) { bc = c; ua.init(); } s[i + 1] = check_push(a, b, ua); } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { rv_init[color[i][j]].push_back(make_pair(i, j)); } } for (i = 0; i <= bc; i++) { ua.init(); for (auto node : rv_init[i]) { tie(a, b) = node; check_push(a, b, ua); } while (!rv[i].empty()) { tie(a, b, c) = rv[i].back(), rv[i].pop_back(); s[c + 1] -= check_push(a, b, ua); } } for (i = 0; i < q; i++) { s[i + 1] += s[i]; } for (i = 0; i < q; i++) { printf("%d\n", s[i + 1]); } }
12
CPP
#include <bits/stdc++.h> using namespace std; long long N, M, K; long long _C[110][110]; void init() { for (int i = 0; i < 110; ++i) { _C[i][0] = _C[i][i] = 1; for (int j = 1; j < i; ++j) { _C[i][j] = _C[i - 1][j] + _C[i - 1][j - 1]; if (_C[i][j] >= 1000000007) _C[i][j] -= 1000000007; } } } long long POW(long long w, long long step) { long long ret = 1; for (; step > 0; step >>= 1, w = (w * w) % 1000000007) if (step & 1) ret = (ret * w) % 1000000007; return ret; } long long dp[110][10010]; long long take[110]; int main() { init(); cin >> N >> M >> K; dp[0][0] = 1; for (int i = 1; i <= N; ++i) { long long cnt = M / N; if (M % N >= i) ++cnt; for (int I = 0; I <= N; ++I) take[I] = POW(_C[N][I], cnt); for (int ALL = 0; ALL <= K; ++ALL) { int MAX = min((long long)ALL, N); long long& T = dp[i][ALL]; for (int cur = 0; cur <= MAX; ++cur) T = (T + dp[i - 1][ALL - cur] * take[cur]) % 1000000007; } } cout << dp[N][K] << '\n'; return 0; }
10
CPP
one=input() two=input() three=input() one =''.join(sorted(one)) two=''.join(sorted(two)) three=''.join(sorted(three)) s=one+two s=''.join(sorted(s)) if s==three: print("YES") else: print("NO")
7
PYTHON3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:256000000") using namespace std; long long mulmod(long long a, long long b, long long MOD) { if (b == 0) return 0; long long res = mulmod(a, b >> 1, MOD); res += res; res %= MOD; return (b & 1) ? (a + res) % MOD : res; } int main() { ios::sync_with_stdio(0); int n, k; cin >> n >> k; char a[10][10]; int b[10]; for (int i = 0; i < n; i++) { for (int j = 0; j < k; j++) { cin >> a[i][j]; a[i][j] -= '0'; b[j] = j; } } int ans = 1000000000; for (int i = 0; i < 40330; i++) { int mx = 0, mn = 1000000000; for (int j = 0; j < n; j++) { int t = 0; for (int jj = 0; jj < k; jj++) { t = t * 10 + a[j][b[jj]]; } mx = max(mx, t); mn = min(mn, t); } ans = min(ans, mx - mn); next_permutation(b, b + k); } cout << ans; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int cases, contador, estado; string word1, word2; cin >> cases; for (int j = 0; j < cases; j++) { contador = 0; estado = 0; cin >> word1; cin >> word2; if (word2[0] == word1[0]) { estado = 1; contador++; } if (estado) { if (word2.length() < word1.length()) { estado = 0; } else { for (int i = 1; i < word2.length(); i++) { if (word2[i] != word1[contador - 1]) { if (word1.length() == contador) { estado = 0; break; } if (word2[i] == word1[contador]) { contador++; } else { estado = 0; break; } } else { if (word1[contador - 1] == word1[contador]) { contador++; } } } } } if (contador < word1.length()) { estado = 0; } if (estado) { cout << "YES" << endl; } else { cout << "NO" << endl; } } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53}; int n; long long int ans = 1e18; map<long long int, bool> mp; void maxdiv(int* exp, int plc, int pas, long long int num) { if (pas == plc) plc++; exp[pas]++; num *= primes[pas]; if (num < 0) return; if (num > ans || mp[num]) return; mp[num] = true; int nd = 1; for (int i = 0; i < plc; ++i) nd *= (exp[i] + 1); if (nd == n) { ans = min(num, ans); } maxdiv(exp, plc, 0, num); exp[0]--; for (int i = pas; i < plc; ++i) { if (exp[i - 1] > exp[i]) { maxdiv(exp, plc, i, num); exp[i]--; } } maxdiv(exp, plc, plc, num); exp[plc]--; } int main(void) { scanf("%d", &n); if (n == 1) { printf("1\n"); return 0; } int exp[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; maxdiv(exp, 0, 0, 1); printf("%I64d\n", ans); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; int n, a[N], res; map<pair<int, int>, int> mp; void solve1() { for (int i = 0; i < n - 2; i++) { int k; for (int j = 29; j >= 0; j--) { if ((1 << j) & a[i]) { k = j; break; } } k++; long long sum = a[i + 1]; for (int j = i + 2; j < n; j++) { if (sum == (a[i] ^ a[j])) { res++; mp[{i, j}] = 1; } else sum += a[j]; if (sum >= (1 << k)) { break; } } } } void solve2() { for (int i = n; i >= 2; i--) { int k; for (int j = 29; j >= 0; j--) { if ((1 << j) & a[i]) { k = j; break; } } k++; long long sum = a[i - 1]; for (int j = i - 2; j >= 0; j--) { if (sum == (a[i] ^ a[j]) && (mp[{j, i}] == 0)) { res++; } else sum += a[j]; if (sum >= (1 << k)) { break; } } } } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } solve1(); solve2(); cout << res << endl; return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; struct cmp { bool operator()(const long long &a, const long long &b) { return a >= b; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, k; cin >> n >> k; long long first = 0, second = 0, ans = 0; long long temp1 = 0, temp2 = 0, temp3 = 0; priority_queue<long long, vector<long long>, cmp> a; priority_queue<long long, vector<long long>, cmp> b; priority_queue<long long, vector<long long>, cmp> c; for (int i = 0; i < n; i++) { cin >> temp1 >> temp2 >> temp3; if (temp2 != 0 && temp3 == 0) a.push(temp1); else if (temp2 == 0 && temp3 != 0) b.push(temp1); else if (temp2 == 1 && temp3 == 1) c.push(temp1); } if (a.size() + c.size() < k || b.size() + c.size() < k) { cout << -1; return 0; } long long ran = min(a.size(), b.size()); while (ran--) { c.push(a.top() + b.top()); a.pop(); b.pop(); } while (k--) { ans += c.top(); c.pop(); } cout << ans; return 0; }
11
CPP
#include<bits/stdc++.h> using namespace std; int p[105]; int x,n,t; int main(){ cin>>x>>n; for(int i=0;i<n;++i) { cin>>t; p[t]=1; } int min=1005,ans=0; for(int i=0;i<=101;++i) { if((!p[i])&&abs(i-x)<min) { min=abs(i-x); ans=i; } } cout<<ans<<"\n"; }
0
CPP
import math n, a, b =map(int, input().split()) arr = list(map(int, input().split())) results = [] for x in arr: rem = (x*a) % b k = math.floor(rem/a) results.append(k) print(" ".join(str(x) for x in results))
8
PYTHON3
T = int(input()) for _ in range(T): n = int(input()) print((n-1)//2)
7
PYTHON3
a,b,c=input().split();print(c,a,b)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 5000; int n, m; vector<int> g[N]; long long bpick[N]; long long dist(int i, int j) { if (i <= j) return j - i; else return n - (i - j); } int main() { cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--, b--; g[a].push_back(b); } for (int i = 0; i < n; i++) { if (g[i].size()) bpick[i] = dist(i, g[i][0]); for (int j = 1; j < g[i].size(); j++) bpick[i] = min(bpick[i], dist(i, g[i][j])); } for (int i = 0; i < n; i++) { long long res = 0; for (int j = 0; j < n; j++) if (g[j].size()) { res = max(res, dist(i, j) + n * ((long long)g[j].size() - 1) + bpick[j]); } cout << res << " "; } cout << endl; return 0; }
7
CPP
""" Problem from: http://codeforces.com/contest/405/problem/A A. Gravity Flip time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Little Chris is bored during his physics lessons (too easy), so he has built a toy box to keep himself occupied. The box is special, since it has the ability to change gravity. There are n columns of toy cubes in the box arranged in a line. The i-th column contains ai cubes. At first, the gravity in the box is pulling the cubes downwards. When Chris switches the gravity, it begins to pull all the cubes to the right side of the box. The figure shows the initial and final configurations of the cubes in the box: the cubes that have changed their position are highlighted with orange. Given the initial configuration of the toy cubes in the box, find the amounts of cubes in each of the n columns after the gravity switch! Input The first line of input contains an integer n (1 ≤ n ≤ 100), the number of the columns in the box. The next line contains n space-separated integer numbers. The i-th number ai (1 ≤ ai ≤ 100) denotes the number of cubes in the i-th column. Output Output n integer numbers separated by spaces, where the i-th number is the amount of cubes in the i-th column after the gravity switch. Examples inputCopy 4 3 2 1 2 outputCopy 1 2 2 3 inputCopy 3 2 3 8 outputCopy 2 3 8 Note The first example case is shown on the figure. The top cube of the first column falls to the top of the last column; the top cube of the second column falls to the top of the third column; the middle cube of the first column falls to the top of the second column. In the second example case the gravity switch does not change the heights of the columns. """ count = input() nums = list(map(int, input().split())) print(" ".join(map(str, sorted(nums))))
7
PYTHON3
n=int(input()) s=input() freq={} for i in s: freq[i]=freq.get(i,0)+1 if freq.get('A',0)>freq.get('D',0):print('Anton') elif freq.get('A',0)<freq.get('D',0):print('Danik') else:print('Friendship')
7
PYTHON3
#include <bits/stdc++.h> using namespace std; namespace DINIC { const int MAXN = 1222; struct edg { int pos, cap, rev; }; vector<edg> gph[MAXN]; void clear() { for (int i = 0; i < MAXN; i++) gph[i].clear(); } void add_edge(int s, int e, int x) { gph[s].push_back({e, x, (int)gph[e].size()}); gph[e].push_back({s, 0, (int)gph[s].size() - 1}); } int dis[MAXN], pnt[MAXN]; bool bfs(int src, int sink) { memset(dis, 0, sizeof dis); memset(pnt, 0, sizeof pnt); queue<int> que; que.push(src); dis[src] = 1; while (!que.empty()) { int x = que.front(); que.pop(); for (auto &e : gph[x]) { if (e.cap > 0 && !dis[e.pos]) { dis[e.pos] = dis[x] + 1; que.push(e.pos); } } } return dis[sink] > 0; } int dfs(int x, int sink, int f) { if (x == sink) return f; for (; pnt[x] < gph[x].size(); pnt[x]++) { edg e = gph[x][pnt[x]]; if (e.cap > 0 && dis[e.pos] == dis[x] + 1) { int w = dfs(e.pos, sink, min(f, e.cap)); if (w) { gph[x][pnt[x]].cap -= w; gph[e.pos][e.rev].cap += w; return w; } } } return 0; } long long match(int src, int sink) { long long ret = 0; while (bfs(src, sink)) { int r; while ((r = dfs(src, sink, 2e9))) ret += r; } return ret; } }; // namespace DINIC int deg[610], p[610]; int col[610]; pair<int, int> a[1000]; void solve() { int n, m, k, i; scanf("%d%d%d", &n, &m, &k); DINIC::clear(); memset(deg, 0, sizeof(deg)); for (int i = 0; i < m; i++) { cin >> a[i].first >> a[i].second; deg[a[i].first]++; deg[a[i].second]++; DINIC::add_edge(a[i].first, i + n + 1, 1); DINIC::add_edge(a[i].second, i + n + 1, 1); DINIC::add_edge(i + n + 1, n + m + 1, 1); } for (int i = 1; i <= n; i++) { p[i] = max(deg[i] - k, 0); DINIC::add_edge(0, i, p[i] * 2); } int sum = accumulate(p + 1, p + n + 1, 0) * 2; if (DINIC::match(0, n + m + 1) != sum) { for (int i = 1; i <= m; i++) printf("0 "); puts(""); return; } for (int i = 0; i < m; i++) col[i] = i + 1; for (int i = 1; i <= n; i++) { vector<int> v; for (auto &j : DINIC::gph[i]) { if (j.pos == 0) continue; if (j.cap == 0) v.push_back(j.pos - n - 1); } for (int i = 0; i < v.size(); i += 2) { col[v[i + 1]] = col[v[i]]; } } for (int i = 0; i < m; i++) printf("%d ", col[i]); puts(""); } int main() { int t, i; scanf("%d", &t); for (i = 0; i < t; i++) solve(); return 0; }
15
CPP
n = int(input()) chall = input() flag = 0 for i in chall: if(i == '1'): flag = 1 break if(flag): print('HARD') else: print('EASY')
7
PYTHON3
n=int(input()) a=list(map(int,input().split())) t=1 p=a[0] m=0 for i in a: if(p==i): continue if(m>0): if(i<p): t+=1 m=0 elif(m<0): if(i>p): t+=1 m=0 else: if(i>p): m=1 elif(i<p): m=-1 p=i print(t)
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int const N = 1e6 + 1; int md = 1e9 + 7; int i, j, n, m, t, x, y, k, nom, ans; int a[N], b[N]; int main() { long long s = 0; scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%d%d", &a[i], &b[i]); s += a[i]; } if (s <= k) { printf("-1"); return 0; } double l = 0, r = 2e12; while (r - l > 1e-5) { double t = (l + r) / 2; double col = t * k; for (int i = 1; i <= n; i++) { col -= max(0.0, a[i] * t - b[i]); } if (col < 0) r = t; else l = t; } printf("%.5f", r); }
7
CPP
#include <bits/stdc++.h> using namespace std; long long int ll_max(long long int a, long long int b, long long int c) { return max(a, max(b, c)); } int int_max(int a, int b, int c) { return max(a, max(b, c)); } long long int ll_min(long long int a, long long int b, long long int c) { return min(a, min(b, c)); } int int_min(int a, int b, int c) { return min(a, min(b, c)); } long long int max(int a, long long int b) { return max((long long int)a, b); } long long int min(int a, long long int b) { return min((long long int)a, b); } long long int min(long long int a, int b) { return min(a, (long long int)b); } long long int max(long long int a, int b) { return max(a, (long long int)b); } long long int dx[] = {0, 0, 1, -1}; long long int dy[] = {1, -1, 0, 0}; long long int power(long long int x, long long int y, long long int m) { if (y == 0) return 1; long long int a = power(x, y / 2, m); if (y % 2) { return (a * ((a * x) % m)) % m; } else { return (a * a) % m; } } long long int mod_inverse(long long int x, long long int m) { return power(x, m - 2, m); } long long int fact(long long int n, long long int m) { if (n <= 1) return 1; return (fact(n - 1, m) * n) % m; } long long int ncr(long long int n, long long int r, long long int m) { if (r > n) return 0; long long int n1 = 1, d1 = 1, d2 = 1; n1 = fact(n, m); d1 = fact(r, m); d2 = fact(n - r, m); long long int ans = mod_inverse((d1 * d2) % m, m); ans = (ans * n1) % m; return ans; } int gcd(int a, int b) { if (a < b) return gcd(b, a); if (a % b == 0) return b; return gcd(b, a % b); } int ispal(string s) { int len = s.size(); int flag = 1; for (int i = 0; i < len; ++i) { if (s[i] != s[len - i - 1]) { flag = 0; break; } } return flag; } long long int sroot(long long int n, long long int low = 1, long long int high = 1e9 + 1) { if (low == high) return low; if (low == high - 1) { if (high * high <= n) return high; else return low; } long long int mid = (low + high) / 2; long long int a = mid * mid; if (a > n) return sroot(n, low, mid - 1); else return sroot(n, mid, high); } long long int croot(long long int n, long long int low = 1, long long int high = 1e6 + 1) { if (low == high) return low; if (low == high - 1) { if (high * high * high <= n) return high; else return low; } long long int mid = (low + high) / 2; long long int a = mid * mid * mid; if (a > n) return croot(n, low, mid - 1); else return croot(n, mid, high); } bool sortbysec(const pair<long long int, long long int> &a, const pair<long long int, long long int> &b) { return (a.second < b.second); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int n, k; cin >> n >> k; long long int x[n]; for (long long int i = 0; i < n; i++) cin >> x[i]; long long int gain; cin >> gain; long long int cost[n]; for (long long int i = 0; i < n; i++) cin >> cost[i]; long long int ans = 0, y = k; long long int max1 = -1, ind = 0; for (long long int i = 0; i < n; i++) max1 = max(max1, x[i]); for (long long int i = 0; i < n; i++) { if (x[i] == max1) { ind = i; break; } } long long int drink = 0, a = k; vector<vector<long long int>> v(n + 5); ans = 0; for (long long int i = 0; i < n; i++) { v[i].push_back(cost[i]); if (k < x[i]) { long long int drink = ceil((x[i] - k) / (gain * 1.0)); if (drink > v[i].size()) { ans = -1; break; } else { sort(v[i].begin(), v[i].end()); for (long long int j = 0; j < drink; j++) ans += v[i][j]; k += drink * gain; for (long long int j = drink; j < v[i].size(); j++) v[i + 1].push_back(v[i][j]); } } else { v[i + 1] = v[i]; } } cout << ans; return 0; }
10
CPP
#pragma GCC target("sse,sse2,sse3,sse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; using ll = long long; int main(){ ll A,B,X;cin >> A >> B >> X; ll ans= 1e9; for(int i = 0;i <= 10000;i++){ for(int j = 0;j <= 10000;j++){ if(i * 1000 + j * 500>= X){ //if(ans > 1LL * A * i + B * j)cout<<i<<" "<<j<<endl; ans = min(ans, 1LL * A * i + B * j); break; } } } cout<<ans<<endl; }
0
CPP
#include <bits/stdc++.h> using namespace std; string s; const int maxn = 20; int d[maxn], p[maxn], f[maxn]; int best = -1, bestd = -1; int cur; string a = "", b = ""; int main() { getline(cin, s); for (int i = 0; i < (int)(s.size()); i++) { d[s[i] - '0']++; } for (int i = 1; i <= 5; i++) { for (int j = 0; j < 20; j++) p[j] = 2 * d[j]; if (i != 5) { if (!p[i] || !p[10 - i]) continue; } else { if (p[i] < 2) continue; } cur = 1; p[i]--; p[10 - i]--; for (int j = 0; j <= 4; j++) { while (p[j] && p[9 - j]) { cur++; p[j]--; p[9 - j]--; } } while (p[0] >= 2) { p[0] -= 2; cur++; } if (cur > best) { best = cur, bestd = i; } } if (bestd == -1) { for (int i = 0; i < (int)(20); i++) p[i] = 2 * d[i]; if (p[0] == 0) { printf("%s\n%s\n", s.c_str(), s.c_str()); return 0; } bestd = 0; } for (int i = 0; i < (int)(20); i++) p[i] = 2 * d[i]; list<pair<int, int> > res; res.push_front(make_pair(bestd, (10 - bestd) % 10)); p[bestd]--; p[(10 - bestd) % 10]--; for (int j = 0; j <= 4; j++) { while (p[j] && p[9 - j]) { p[j]--; p[9 - j]--; res.push_front(make_pair(j, 9 - j)); } } while (p[0] >= 2) { res.push_back(make_pair(0, 0)); p[0] -= 2; } for (int i = 0; i < (int)(20); i++) p[i] = d[i]; for (int i = 0; i < (int)(20); i++) f[i] = d[i]; while (!res.empty()) { pair<int, int> q = res.front(); if (!p[q.first] || !f[q.second]) swap(q.first, q.second); a += q.first + '0'; b += q.second + '0'; p[q.first]--; f[q.second]--; assert(p[q.first] >= 0); assert(f[q.second] >= 0); res.pop_front(); } reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); for (int i = 0; i < (int)(20); i++) { while (p[i]) { a += i + '0'; p[i]--; } while (f[i]) { b += i + '0'; f[i]--; } } reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); printf("%s\n%s\n", a.c_str(), b.c_str()); }
8
CPP
#include<bits/stdc++.h> using namespace std; long long n, i, j, a[1<<18], b, c, d; int main(){ cin >> n; for(i=1;i<=n;++i){ cin >> a[i]; b += a[i]; } c = b; for(i=1; i<=n;++i){ d += a[i]; c = min(c, abs(d-(b-d))); } cout <<c; }
0
CPP
from collections import deque n, k = map(int, input().split()) id_list = list(map(int, input().split())) active_set = set() screen_q = deque() for id in id_list: if id not in active_set: active_set.add(id) if len(screen_q) == k: id_out = screen_q[-1] active_set.remove(id_out) screen_q.pop() screen_q.appendleft(id) print(len(screen_q)) for elem in screen_q: print(elem, end=' ')
8
PYTHON3
#include <bits/stdc++.h> using namespace std; long long seed, vmax; long long rnd() { long long res = seed; seed = (seed * 7 + 13) % 1000000007; return res; } long long n, m; long long a[1000001]; struct node { long long l, r; mutable long long val; node(long long _l = 0, long long _r = -1, long long _v = 0) { l = _l; r = _r; val = _v; } }; bool operator<(const node &a, const node &b) { return a.l < b.l; } set<node> st; long long qpow(long long a, long long b, long long p) { long long res = 1; a %= p; for (; b; b >>= 1, a = a * a % p) if (b & 1) res = res * a % p; return res; } set<node>::iterator split(long long pos) { set<node>::iterator it = st.lower_bound(node(pos)); if (it != st.end() && it->l == pos) return it; --it; long long l = it->l, r = it->r; long long val = it->val; st.erase(it); st.insert(node(l, pos - 1, val)); return st.insert(node(pos, r, val)).first; } void assign(long long l, long long r, long long val) { set<node>::iterator end = split(r + 1), beg = split(l); st.erase(beg, end); st.insert(node(l, r, val)); } void add(long long l, long long r, long long val) { set<node>::iterator end = split(r + 1), beg = split(l); for (; beg != end; ++beg) beg->val += val; } long long getkth(long long l, long long r, long long k) { vector<pair<long long, long long> > vec; set<node>::iterator end = split(r + 1), beg = split(l); for (; beg != end; ++beg) vec.push_back(pair<long long, long long>(beg->val, beg->r - beg->l + 1)); sort(vec.begin(), vec.end()); for (vector<pair<long long, long long> >::iterator it = vec.begin(); it != vec.end(); ++it) { k -= it->second; if (k <= 0) return it->first; } return -1; } long long getsum(long long l, long long r, long long x, long long y) { long long res = 0; set<node>::iterator end = split(r + 1), beg = split(l); for (; beg != end; ++beg) res = (res + qpow(beg->val, x, y) * (beg->r - beg->l + 1) % y) % y; return res; } signed main() { scanf("%lld%lld%lld%lld", &n, &m, &seed, &vmax); for (long long i = 1; i <= n; ++i) { a[i] = (rnd() % vmax) + 1; st.insert(node(i, i, a[i])); } long long op, l, r; long long x, y; for (long long i = 1; i <= m; ++i) { op = (rnd() % 4) + 1; l = rnd() % n + 1, r = rnd() % n + 1; if (l > r) swap(l, r); if (op == 3) x = (rnd() % (r - l + 1)) + 1; else x = (rnd() % vmax) + 1; if (op == 4) y = (rnd() % vmax) + 1; if (op == 1) add(l, r, x); else if (op == 2) assign(l, r, x); else if (op == 3) printf("%lld\n", getkth(l, r, x)); else if (op == 4) printf("%lld\n", getsum(l, r, x, y)); } }
9
CPP
#include <stdio.h> int main(){ int a,b; scanf("%d%d",&a,&b); if(a+b==15)printf("+\n"); else if(a*b==15)printf("*\n"); else printf("x\n"); }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { int arr[4][4]; memset(arr, 0, sizeof(arr)); string str; cin >> str; int len = str.length(); for (int i = 0; i < len; i++) { if (str[i] == '0') { if (arr[0][2] == 0) { arr[0][2] = 1; cout << "3 1" << endl; } else { arr[0][2] = 0; cout << "1 1" << endl; } } else { if (arr[2][0] == 0) { arr[2][0] = 1; cout << "1 3" << endl; } else { arr[2][0] = 0; cout << "1 1" << endl; } } } return 0; }
9
CPP
n=list(input()) for i in range(0,len(n)): if(n[i]=='H' or n[i]=='Q' or n[i]=="9"): print("YES") exit() print("NO")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; const int mod = 1e9 + 7; long long int power(long long int x, long long int y) { long long int t = 1; while (y > 0) { if (y % 2) y -= 1, t = t * x % mod; else y /= 2, x = x * x % mod; } return t; } int main() { long long int n, i, j, k, l; cin >> n; if (n == 1) { cout << 1 << endl; cout << 1 << endl; } else if (n == 2) { cout << 1 << endl; cout << 1 << endl; } else if (n == 3) { cout << 2 << endl; cout << 1 << " " << 3 << endl; } else if (n == 4) { cout << 4 << endl; cout << 3 << " " << 1 << " " << 4 << " " << 2 << endl; } else { cout << n << endl; for (i = 1; i <= n; i = i + 2) { cout << i << " "; } for (i = 2; i <= n; i = i + 2) { cout << i << " "; } cout << endl; } return 0; }
7
CPP
a=int(input()) letters=[] for i in list(input()): if ord(i)<91: letters.append(chr(ord(i)+32)) else: letters.append(i) check = set(letters) if len(check)<26: print('NO') else: print('YES')
7
PYTHON3
import sys def solve(): mod = 1000 * 1000 * 1000 + 7 n, = rv() a, = rl(1) mem = [0] * n timesofar = 0 for i in range(n): ariveat = a[i] - 1 moresum = 0 for j in range(ariveat, i): moresum += mem[j] mem[i] = moresum + 2 timesofar += moresum + 2 timesofar %= mod print(timesofar) def rv(): return map(int, input().split()) def rl(n): return [list(map(int, input().split())) for _ in range(n)] if sys.hexversion == 50594544 : sys.stdin = open("test.txt") solve()
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { string a = "", aux; for (int i = 1; i <= 1001; i++) { stringstream ss; ss << i; ss >> aux; a += aux; } int n; cin >> n; n--; cout << a[n] << endl; return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; void printP(vector<int>& a) { reverse(a.begin(), a.end()); cout << (int)a.size() - 1 << "\n"; for (int o : a) cout << o << " "; cout << "\n"; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout << std::setprecision(9); int N; scanf("%d", &N); vector<int> a; vector<int> b; a.push_back(1); a.push_back(0); b.push_back(1); for (int i = 1; i < N; i++) { vector<int> c = a; c.push_back(0); int l = (int)c.size(); for (int j = 1; j <= (int)b.size(); j++) { c[l - j] = (c[l - j] + b[(int)b.size() - j]) % 2; } b = a; a = c; } printP(a); printP(b); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 5; int p[maxn], g[maxn], t[maxn]; int n, w, h; const int offset = 100000 + 5; vector<int> point[offset * 4]; set<int> Valid; bool cmp(int A, int B) { if (g[A] != g[B]) return g[A] == 2; else return g[A] == 2 ? p[A] > p[B] : p[A] < p[B]; } pair<int, int> Ans[maxn]; int main() { scanf("%d%d%d", &n, &w, &h); for (int i = 1; i <= n; i++) { scanf("%d%d%d", &g[i], &p[i], &t[i]); point[p[i] - t[i] + offset].push_back(i); Valid.insert(p[i] - t[i] + offset); } vector<int> xs, ys; for (set<int>::iterator ite = Valid.begin(); ite != Valid.end(); ++ite) { int co = *ite; xs.clear(), ys.clear(); for (int j = 0; j < (int)point[co].size(); j++) { int idx = point[co][j]; if (g[idx] == 1) xs.push_back(p[idx]); else ys.push_back(p[idx]); } sort(xs.begin(), xs.end()); sort(ys.begin(), ys.end()); sort(point[co].begin(), point[co].end(), cmp); for (int j = 0; j < (int)xs.size(); j++) Ans[point[co][j]] = make_pair(xs[j], h); for (int j = 0; j < (int)ys.size(); j++) Ans[point[co][j + xs.size()]] = make_pair(w, ys[ys.size() - j - 1]); } for (int i = 1; i <= n; i++) printf("%d %d\n", Ans[i].first, Ans[i].second); return 0; }
10
CPP
README.md exists but content is empty. Use the Edit dataset card button to edit it.
Downloads last month
47
Edit dataset card