solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j, x = 1, y = 1, ans = 0; cin >> n; for (i = 2; x < n; x += i, i++, y = 1) { for (j = 2; y < n - x; y += j, j++) { } if (x + y == n) ans = 1; } ans ? cout << "YES" : cout << "NO"; }
7
CPP
n = int(input()) out = 0 for i in range(n): out += 1 / n n -= 1 print(out)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, t; int a[200005]; int s[200005]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> n; int pos = 0; for (int i = 1; i <= n; i++) cin >> a[i]; s[1] = 1; int k = 0; for (int i = 2; i <= n; i++) { if (a[i] == a[i - 1]) s[i] = s[i - 1], pos = i; else s[i] = 3 - s[i - 1]; k = max(s[i], k); } if (a[1] != a[n] && s[1] == s[n]) { if (pos > 0) { for (int i = pos; i <= n; i++) s[i] = 3 - s[i]; } else { s[n] = 3, k = 3; } } cout << k << endl; for (int i = 1; i <= n; i++) cout << s[i] << " "; cout << endl; } return 0; }
10
CPP
n, d, res, c, count = int(input()), {}, 0, 0, 0 for i in range(-10, 11): d[i] = 0 for i in input().split(): if i == '0': count += 1 c += count - 1 else: d[int(i)] += 1 for i in range(1, 11): res += d[i] * d[-i] res += c print(res)
8
PYTHON3
a,b = input().split() a = int(a) b = int(b) f = 0 while 1: f+=1 a*=3 b*=2 if a>b: break print(f)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; #define REP(i, n) FOR(i, 0, n) #define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i) #define ALL(obj) (obj).begin(), (obj).end() #define ALLR(obj) (obj).rbegin(), (obj).rend() int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> a(n), b(n); REP(i, n) cin >> a[i] >> b[i]; int maxx = *max_element(ALL(a)); int minn = *min_element(ALL(b)); int ans = 0; vector<pair<int, int>> p(n); REP(i, n) p[i] = {minn - a[i] + 1, b[i] - maxx + 1}; sort(ALL(p)); int t = INT_MAX; REP(i, n - 1) { t = min(t, p[i].second); ans = max(ans, p[i + 1].first + t); } t = max(0, minn - maxx + 1); REP(i, n) { int cur = b[i] - a[i] + t + 1; ans = max(ans, cur); } cout << ans << '\n'; }
0
CPP
import math t=int(input()) for j in range(t): n=int(input()) sum=0 temp=input() temp=temp.split() for i in temp: sum+=int(i) if sum/n != sum//n: print(sum//n + 1) else: print(sum//n) # print(math.ceil(sum/n))
7
PYTHON3
example = input().split('+') example.sort() print('+'.join(example))
7
PYTHON3
def maths(n): r1 = [] p = 0 for m in range(2, 100000): if n%m == 0: p = m break if p == 0: p = n r1.append(str(n//p)) r1.append(str(n - (n//p))) r2 = ' '.join(r1) return r2 t = int(input()) for i in range(t): n = int(input()) print(maths(n))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1005; int n; int ans[N]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) ans[i] = int(1e9); for (int k = 0; k < 10; k++) for (int f = 0; f <= 1; f++) { vector<int> q; for (int i = 0; i < n; i++) if ((i >> k & 1) == f) q.push_back(i + 1); if (q.empty()) continue; printf("%d\n", q.size()); for (int i = 0; i < int(q.size()); i++) printf("%d ", q[i]); printf("\n"); fflush(stdout); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); if ((i >> k & 1) != f) ans[i] = min(ans[i], x); } } puts("-1"); for (int i = 0; i < n; i++) printf("%d ", ans[i]); printf("\n"); }
8
CPP
for _ in range(int(input())): a,b,c,d = map(int,input().split()) n = input() ans = 0 for i in n: if i=="0": ans+=min(d+c,b) elif i=="1": ans+=min(d+b,c) print(ans)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int N = 2e5 + 7; int qpow(int x, int y) { if (x == 0) return 0; int res = 1; for (; y; x = 1ll * x * x % mod, y >>= 1) if (y & 1) res = 1ll * res * x % mod; return res; } int ny(int x) { return qpow(x, mod - 2); } long long T; int n, t[N], tpow[N], jc[N], njc[N], ans, wz = -1, now; int C(int x, int y) { return 1ll * jc[x] * njc[y] % mod * njc[x - y] % mod; } int main() { scanf("%d%lld", &n, &T); tpow[0] = jc[0] = njc[0] = 1; for (int i = (1), iE = (n); i <= iE; i++) tpow[i] = 2 * tpow[i - 1] % mod; for (int i = (1), iE = (n); i <= iE; i++) jc[i] = 1ll * jc[i - 1] * i % mod, njc[i] = ny(jc[i]); for (int i = (1), iE = (n); i <= iE; i++) scanf("%d", &t[i]); for (int i = (1), iE = (n); i <= iE; i++) { T -= t[i]; if (T < 0) break; else if (T > i) ans++; else { if (wz == -1) wz = i, now = tpow[i]; else now = (now * 2 % mod + mod - C(i - 1, wz)) % mod; while (wz != T) (now += mod - C(i, wz)) %= mod, wz--; (ans += 1ll * now * ny(qpow(2, i)) % mod) %= mod; } } printf("%d\n", ans); return 0; }
12
CPP
n , k = map(int,input().split()) s=input()+'#' lis=[i for i in input().split()] #print(s,lis) prev=ans=0 for i in range(n+1): if s[i] not in lis: a=i-prev # print(prev,i,a) prev=i+1 ans+=(a*(a+1))//2 print(ans)
9
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long T; cin >> T; while (T--) { long long a, b; cin >> a >> b; if (a == b) cout << 0 << "\n"; else if (a > b) { if ((a - b) % 2 == 1) cout << 2 << "\n"; else { cout << 1 << "\n"; } } else { if ((b - a) % 2 == 1) { cout << 1 << "\n"; } else { cout << 2 << "\n"; } } } return 0; }
7
CPP
val=input(); print("Yes" if int(val)>2 and int(val)%2==0 else "NO")
7
PYTHON3
import sys import datetime # sys.stdin = open('input.txt') # sys.stdout = open('output.txt', 'w') def main(): n = int(input()) + 1 ans = (3 * (n - 1)) * n + 1 print(ans) main()
10
PYTHON3
n = int(input()) balance = list(map(int, input().split())) q = int(input()) log = [list(map(int, input().split())) for i in range(q)] ind_of_last_operation = [None] * n max_pay = 0 ind_of_max_payment = None payments = [] payments_id = [] for i in range(q): if log[i][0] == 1: ind_of_last_operation[log[i][1] - 1] = i for i in range(q): if log[i][0] == 2: ind_of_last_payment = i last_payment = log[i][1] if log[i][1] > max_pay: max_pay = log[i][1] max_pay_for_moment = [0] * (q + 1) for j in range(q - 1, -1, -1): if log[j][0] == 2: max_pay_for_moment[j] = max(log[j][1], max_pay_for_moment[j + 1]) else: max_pay_for_moment[j] = max_pay_for_moment[j + 1] for i in range(n): if ind_of_last_operation[i] == None: if balance[i] < max_pay: balance[i] = max_pay else: if max_pay_for_moment[ind_of_last_operation[i] + 1] > log[ind_of_last_operation[i]][2]: balance[i] = max_pay_for_moment[ind_of_last_operation[i] + 1] else: balance[i] = log[ind_of_last_operation[i]][2] print(*balance)
8
PYTHON3
T = int(input()) for _ in range(T): n = int(input()) a = [] a = list(map(int, input().split())) # print(a) h = 1 nwd = 0 dead = 0 for i in range(n): if a[i] == 1: if nwd == 0 and i: h += 5 else: h += 1 nwd = 0 else: nwd += 1 if nwd == 2: dead = 1 if dead: print(-1) else: print(h)
7
PYTHON3
def decrement(a): return chr(ord(a)-1) def increment(a): return chr(ord(a)+1) t=int(input()) for i in range(t): n=int(input()) string=input() stringrev=string[::-1] flag=1 n=len(string) for j in range(0,len(string)//2): if string[j]!=stringrev[j] and increment(string[j])!=decrement(stringrev[j]) and decrement(string[j])!=increment(stringrev[j]) : flag=0 break if flag==1: print("YES") else: print("NO")
7
PYTHON3
def rindex(mylist: list, myvalue): return len(mylist) - mylist[::-1].index(myvalue) - 1 def main(): t = int(input()) for s in range(t): n = int(input()) lst = list(int(i) for i in input().split()) f_element_index = lst.index(1) l_element_index = rindex(lst, 1) # print(f_element_index, l_element_index) cnt_zero = lst[f_element_index:l_element_index].count(0) if (f_element_index == l_element_index or cnt_zero == 0) and ( f_element_index == 0 or l_element_index == len(lst) - 1): print(0) else: # left_shift = cnt_zero + f_element_index # right_shift = cnt_zero + (len(lst) - 1 - l_element_index) # print(min(left_shift, right_shift)) print(cnt_zero) if __name__ == '__main__': main()
8
PYTHON3
#Codeforces 570 C - Replacement from sys import stdin, stdout n,m=map(int,stdin.readline().split()) s = list(stdin.readline()) dots = 0 groups = 0 prev = "x" #whatever char distinct of . for l in s: if l == ".": dots += 1 if prev != ".": groups += 1 prev = l #print(dots,groups) lines = stdin.readlines() for line in lines: x,c = line.split() x = int(x) - 1 a = x-1>=0 and s[x-1]=="." b = x+1<n and s[x+1]=="." if s[x] == "." and c != ".": dots -= 1 if a and b: groups += 1 elif not a and not b: groups -= 1 elif s[x] != "." and c==".": dots += 1 if a and b: groups -= 1 elif not a and not b: groups += 1 s[x] = c #print(s) #print(dots,groups) stdout.write(str(dots-groups)+"\n")
9
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1010; const long long INF = 1e18; int lcp[N][N], n, m, tot; struct S { int l, r; } a[N * N]; char s[N]; long long dp[N], k; bool cmp(S a, S b) { int l = min(lcp[a.l][b.l], min(a.r - a.l + 1, b.r - b.l + 1)); if (l < a.r - a.l + 1 && l < b.r - b.l + 1) return s[a.l + l] < s[b.l + l]; else return a.r - a.l + 1 < b.r - b.l + 1; } long long cal(int x) { int l = a[x].l, r = a[x].r; dp[n + 1] = 1; for (int i = 1; i <= n; ++i) dp[i] = 0; for (int o = 1; o <= m; ++o) { for (int i = n; i >= 1; --i) dp[i] = min(dp[i] + dp[i + 1], INF); for (int i = 1, j; i <= n; ++i) { j = min(lcp[l][i], r - l + 1); if (j == r - l + 1) dp[i] = dp[i + j]; else if (s[l + j] <= s[i + j]) dp[i] = dp[i + j + 1]; else dp[i] = 0; } dp[n + 1] = 0; } return dp[1]; } int main() { scanf("%d %d %lld", &n, &m, &k); scanf("%s", s + 1); for (int i = n; i >= 1; --i) for (int j = n; j >= 1; --j) if (s[i] == s[j]) lcp[i][j] = lcp[i + 1][j + 1] + 1; for (int i = 1; i <= n; ++i) for (int j = i; j <= n; ++j) a[++tot] = (S){i, j}; sort(a + 1, a + 1 + tot, cmp); int l = 0, r = tot + 1, mid; while (l + 1 < r) { mid = (l + r) >> 1; if (cal(mid) >= k) l = mid; else r = mid; } for (int i = a[l].l; i <= a[l].r; ++i) putchar(s[i]); }
9
CPP
#include <algorithm> #include <bitset> #include <cassert> #include <cmath> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <unordered_map> #include <vector> #include <string.h> #include <set> #include <stack> using namespace std; #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl template<class T> void printvec(const vector<T>& v) { for (auto x : v) { cout << x << " "; } cout << endl; } template<class T> void printtree(const vector< vector<T> >& tree) { for (long long i = 0; i < tree.size(); ++i) { cout << i + 1 << ": "; printvec(tree[i]); } } template<class T, class U> void printmap(const map<T, U>& mp) { for (auto x : mp) { cout << x.first << "=>" << x.second << endl; } } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } #define rep(i, n) for(ll i = 0; i < n; ++i) #define all(s) s.begin(), s.end() #define sz(x) (ll)(x).size() #define fr first #define sc second #define mp make_pair #define pb push_back #define eb emplace_back typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; typedef tuple<ll, ll, ll> triple; typedef double D; typedef vector<ll> vl; typedef vector<P> vp; const ll INF = 1e15; const ll MOD = 1000000007; // 1e9 + 7 // Dinic's algorithm. typedef ll FLOW; // Type of flow. int here. const ll MAX_V = 2005; // Maximum number of nodes in a graph. struct Edge { ll rev, from, to; FLOW cap, icap; // icap: initial cap Edge(ll r, ll f, ll t, FLOW c) : rev(r), from(f), to(t), cap(c), icap(c) {} friend ostream& operator << (ostream& s, const Edge& E) { if (E.cap > 0) { return s << E.from << "->" << E.to << '(' << E.cap << ')'; } else { return s; } } }; class Graph { public: Graph(ll n = 0) : V(n) { rep(i, MAX_V) { list[i].clear(); } } void init(ll n = 0) { V = n; rep(i, MAX_V) { list[i].clear(); } } void resize(ll n = 0) { V = n; } void reset() { rep(i, V) { rep(j, list[i].size()) { list[i][j].cap = list[i][j].icap; } } } inline vector<Edge>& operator [] (ll i) { return list[i]; } Edge &redge(Edge e) { if (e.from != e.to) { return list[e.to][e.rev]; } else { return list[e.to][e.rev + 1]; } } void addedge(ll from, ll to, FLOW cap) { list[from].push_back(Edge((ll)list[to].size(), from, to, cap)); list[to].push_back(Edge((ll)list[from].size() - 1, to, from, 0)); } private: ll V; vector<Edge> list[MAX_V]; }; static ll level[MAX_V]; static ll iter[MAX_V]; void dibfs(Graph &G, ll s) { rep(i, MAX_V) { level[i] = -1; } level[s] = 0; queue<ll> que; que.push(s); while (!que.empty()) { ll v = que.front(); que.pop(); rep(i, G[v].size()) { Edge &e = G[v][i]; if (level[e.to] < 0 && e.cap > 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } FLOW didfs(Graph &G, ll v, ll t, FLOW f) { if (v == t) { return f; } for (ll &i = iter[v]; i < G[v].size(); ++i) { Edge &e = G[v][i], &re = G.redge(e); if (level[v] < level[e.to] && e.cap > 0) { FLOW d = didfs(G, e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; re.cap += d; return d; } } } return 0; } // Main function for calculatig maximum flow FLOW Dinic(Graph &G, int s, int t) { FLOW res = 0; while (true) { dibfs(G, s); if (level[t] < 0) { return res; } memset(iter, 0, sizeof(iter)); FLOW flow; while ((flow = didfs(G, s, t, INF)) > 0) { res += flow; } } } // int main(int argc, char** argv) { // int V, E; // cin >> V >> E; // Graph g(V); // rep(i, E) { // ll u, v, c; // cin >> u >> v >> c; // g.addedge(u, v, c); // } // // ll r = Dinic(g, 0, V - 1); // cout << r << endl; // } int main(int argc, char** argv) { cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false); //cout << setprecision(10) << fixed; while (true) { ll n,m,l; cin >> n >> m >> l; if (n == 0 && m == 0 && l == 0) { break; } // initializatio memset(level, 0, sizeof(level)); memset(iter, 0, sizeof(iter)); vector<vector<ll>> d1(n, vector<ll>(n,INF)); rep(i,n) { d1[i][i] = 0; } while (m--) { ll u,v,d; cin >> u >> v >> d; d1[u][v] = d; d1[v][u] = d; } // Warshall-Floyd rep(k,n) { rep(u,n)rep(v,n) { chmin(d1[u][v], d1[u][k] + d1[k][v]); } } // printtree(d1); vector<ll> p(l); vector<ll> t(l); rep(i,l) { cin >> p[i] >> t[i]; } Graph g(2*l+2); // DAG rep(i,l)rep(j,l) { if (i==j) { continue; } if (t[i]+d1[p[i]][p[j]] <= t[j]) { // i->j is ok. g.addedge(i,l+j,1); // cout << "(i,j):" << i << "," << j << endl; // cout << "(i,j+l):" << i << "," << j+l << endl; } } // Add source and target ll src = 2*l, tgt = 2*l+1; rep(i,l) { g.addedge(src,i,1); g.addedge(l+i,tgt,1); } // cout << "src: " << src << endl; // cout << "tgt: " << tgt << endl; // rep(i,2*l+2) { // for (auto& e : g[i]) { // cout << e; // } // cout << endl; // } ll r = Dinic(g,src,tgt); // maximum matching // cout << "r: " << r << endl; cout << l-r << endl; } }
0
CPP
n, k = map(int, input().split()) a = [int(i) for i in input().split()] d = [0 for i in range(n)] d[0] = 1 + min(n - 1, k) for i in range(1, n): if a[i] == 0: d[i] = 1 + min(i, k) + min(n - 1 - i, k) else: j = a[i]-1 d[i] = d[j] + min(n - 1, i+k) - min(n - 1, j+k) if i - k > j + k: d[i] -= i - k - (j + k) - 1 for i in range(len(d)): print(d[i], end=" ")
8
PYTHON3
for i in range(5): x=list(map(int,input().split())) for k in range(5): if(x[k]==1): print(abs(k-2)+abs(i-2)) break
7
PYTHON3
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <climits> #include <cfloat> #include <ctime> #include <cassert> #include <map> #include <utility> #include <set> #include <iostream> #include <memory> #include <string> #include <vector> #include <algorithm> #include <functional> #include <sstream> #include <complex> #include <stack> #include <queue> #include <numeric> #include <list> #include <iomanip> #include <fstream> #include <bitset> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); ++i) #define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it) #define rforeach(it, c) for (__typeof__((c).rbegin()) it=(c).rbegin(); it != (c).rend(); ++it) #define all(c) (c).begin(), (c).end() #define rall(c) (c).rbegin(), (c).rend() #define CL(arr, val) memset(arr, val, sizeof(arr)) #define COPY(dest, src) memcpy(dest, src, sizeof(dest)) #define ten(n) ((long long)(1e##n)) #define bin(n) (1LL << (n)) #define erep(i, n) for (int i = 0; i <= (int)(n); ++i) #define revrep(i, n) for (int i = (n); i >= 0; --i) #define pb push_back template <class T> void chmax(T& a, const T& b) { a = max(a, b); } template <class T> void chmin(T& a, const T& b) { a = min(a, b); } template <class T> void uniq(T& c) { sort(c.begin(), c.end()); c.erase(unique(c.begin(), c.end()), c.end()); } template <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); } template <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; } template <typename T> void print_container(ostream& os, const T& c) { const char* _s = " "; if (!c.empty()) { __typeof__(c.begin()) last = --c.end(); foreach (it, c) { os << *it; if (it != last) cout << _s; } } } template <typename T> ostream& operator<<(ostream& os, const vector<T>& c) { print_container(os, c); return os; } template <typename T> ostream& operator<<(ostream& os, const set<T>& c) { print_container(os, c); return os; } template <typename T> ostream& operator<<(ostream& os, const multiset<T>& c) { print_container(os, c); return os; } template <typename T> ostream& operator<<(ostream& os, const deque<T>& c) { print_container(os, c); return os; } template <typename T, typename U> ostream& operator<<(ostream& os, const map<T, U>& c) { print_container(os, c); return os; } template <typename T, typename U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << "( " << p.first << ", " << p.second << " )"; return os; } template <class T> void print(T a, int n, const string& deli = " ", int br = 1) { for (int i = 0; i < n; ++i) { cout << a[i]; if (i + 1 != n) cout << deli; } while (br--) cout << endl; } template <class T> void print2d(T a, int w, int h, int width = -1, int br = 1) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (width != -1) cout.width(width); cout << a[i][j] << ' '; } cout << endl; } while (br--) cout << endl; } template <class T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; } template <class T> void input(T* a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; } void fix_pre(int n) { cout.setf(ios::fixed, ios::floatfield); cout.precision(10); } void fast_io() { cin.tie(0); ios::sync_with_stdio(false); } #define trace(x) (cout << #x << ": " << (x) << endl) bool in_rect(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; } typedef long long ll; typedef pair<int, int> pint; // y(v): v>^< y(^): ^>v< const int dx[] = { 0, 1, 0, -1 }; const int dy[] = { 1, 0, -1, 0 }; const double PI = acos(-1.0); #define mp make_pair int main() { int n, a[22]; cin >> n; input(a, n); const int m = 2 * ten(5) + 100; static double dp[22][m + 100]; erep(i, n) rep(j, m) dp[i][j] = 1e60; dp[0][1] = 0; rep(i, n) for (int j = 1; j < m; ++j) for (int nj = j; nj < m; nj += j) chmin(dp[i + 1][nj], max(dp[i][j], (double)abs(nj - a[i]) / a[i])); printf("%.10f\n", *min_element(dp[n], dp[n] + m)); }
0
CPP
#include <bits/stdc++.h> using namespace std; long long n, a[1000005]; int main() { cin >> n; for (int i = 1, _i = (n); i <= _i; i++) cin >> a[i]; cout << a[2] - a[1] << " " << a[n] - a[1] << endl; for (int i = 2, _i = (n - 1); i <= _i; i++) { cout << min(a[i + 1] - a[i], a[i] - a[i - 1]) << " " << max(a[n] - a[i], a[i] - a[1]) << endl; } cout << a[n] - a[n - 1] << " " << a[n] - a[1] << endl; }
7
CPP
#include <bits/stdc++.h> using namespace std; int dp[5005][5005]; int ans[5005][5005]; int solve(int l, int r) { if (dp[l][r]) { return dp[l][r]; } if (l == r) { return dp[l][r] = ans[l][r]; } else { return dp[l][r] = max({ans[l][r], solve(l, r - 1), solve(l + 1, r)}); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i < n + 1; i++) { cin >> ans[i][i]; } for (int i = 1; i < n; i++) { for (int j = 1; j <= n - i; j++) { ans[j][j + i] = ans[j][j + i - 1] ^ ans[j + 1][j + i]; } } for (int i = 1; i < n + 1; i++) { dp[i][i] = ans[i][i]; } for (int i = 1; i < n; i++) { for (int j = 1; j <= n - i; j++) { dp[j][j + i] = max({dp[j][j + i - 1], dp[j + 1][j + i], ans[j][j + i]}); } } int q; cin >> q; while (q--) { int l, r; cin >> l >> r; cout << dp[l][r] << '\n'; } }
10
CPP
(n,m), *ka = [list(map(int, s.split())) for s in open(0)] c = [0]*(m+1) for k, *a in ka: for elm in a: c[elm] += 1 print(c.count(n))
0
PYTHON3
#include <iostream> using namespace std; string s; int a[27]; int main() { cin >> s; for(int i = 0; i < s.length(); i++) a[s[i] - 'a']++; for(int i = 0; i < 27; i++) if (a[i] % 2) { cout << "No"; return 0; } cout << "Yes"; return 0; }
0
CPP
n=int(input()) s=[] for i in range(n): l=input().split() if int(l[1])>=2400 and int(l[2])>int(l[1]): s.append('YES') if 'YES' in s: print('YES') else: print('NO')
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; int a[6006]; int sum; bool is_prime(int x) { if (x == 1) return 0; for (int i = 2; i * i <= x; i++) { if (x % i == 0) return 0; } return 1; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { sum += i; } if (is_prime(sum)) { for (int i = 1; i <= n; i++) { printf("%d ", 1); } puts(""); return 0; } if (sum % 2 == 1 && !is_prime(sum - 2)) { sum -= 3; a[3] = 2; } for (int i = 2; i <= sum; i++) { if (is_prime(i) && is_prime(sum - i)) { a[i] = 1; break; } } for (int i = 1; i <= n; i++) { printf("%d ", a[i] + 1); } puts(""); return 0; }
13
CPP
a,b=map(int,input().split()) if(a>b): k=a-b l=k//2 print(b,l,sep=" ") else: k=b-a l=k//2 print(a,l,sep=" ")
7
PYTHON3
# ------------------- fast io -------------------- import os import sys from io import BytesIO, IOBase BUFSIZE = 8192 class FastIO(IOBase): newlines = 0 def __init__(self, file): self._fd = file.fileno() self.buffer = BytesIO() self.writable = "x" in file.mode or "r" not in file.mode self.write = self.buffer.write if self.writable else None def read(self): while True: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) if not b: break ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines = 0 return self.buffer.read() def readline(self): while self.newlines == 0: b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE)) self.newlines = b.count(b"\n") + (not b) ptr = self.buffer.tell() self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr) self.newlines -= 1 return self.buffer.readline() def flush(self): if self.writable: os.write(self._fd, self.buffer.getvalue()) self.buffer.truncate(0), self.buffer.seek(0) class IOWrapper(IOBase): def __init__(self, file): self.buffer = FastIO(file) self.flush = self.buffer.flush self.writable = self.buffer.writable self.write = lambda s: self.buffer.write(s.encode("ascii")) self.read = lambda: self.buffer.read().decode("ascii") self.readline = lambda: self.buffer.readline().decode("ascii") sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout) input = lambda: sys.stdin.readline().rstrip("\r\n") # ------------------- fast io -------------------- from math import gcd, ceil def pre(s): n = len(s) pi = [0] * n for i in range(1, n): j = pi[i - 1] while j and s[i] != s[j]: j = pi[j - 1] if s[i] == s[j]: j += 1 pi[i] = j return pi def prod(a): ans = 1 for each in a: ans = (ans * each) return ans def lcm(a, b): return a * b // gcd(a, b) def binary(x, length=16): y = bin(x)[2:] return y if len(y) >= length else "0" * (length - len(y)) + y for _ in range(int(input()) if True else 1): n, k = map(int, input().split()) #a, b = map(int, input().split()) #c, d = map(int, input().split()) #a = list(map(int, input().split())) #b = list(map(int, input().split())) #s = input() #print("YES" if s else "NO") ans = 1 for i in range(1,int(n**0.5)+69): if i > k:break if n % i == 0: ans = max(ans, i) if n // i <= k: ans = max(ans, n // i) print(n//ans)
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int factorial(int a) { return a < 1 ? 1 : a * factorial(a - 1); } int main() { int A, B; cin >> A >> B; cout << factorial(min(A, B)) << endl; return 0; }
7
CPP
n, m = map(int, input().split()) L = sorted(map(int, input().split())) d = [L[i + 1] - L[i] for i in range(m - 1)] d.sort(reverse=True) print(sum(d[(n - 1):]))
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[100005], b[100005], c[100005]; bool cmp(int a, int b) { return a > b; } int main() { int n; scanf("%d", &n); if (n <= 2) { cout << -1 << endl; return 0; } int j, i; for (i = 0; i < n; i++) { scanf("%d", &a[i]); b[i] = c[i] = a[i]; } sort(b, b + n); if (b[0] == b[n - 1]) { cout << -1 << endl; return 0; } sort(c, c + n, cmp); for (i = 0; i < n; i++) { if (a[i] == a[i + 1]) continue; for (j = i + 1; j < n; j++) { if (a[j] == a[j - 1]) continue; if (a[i] == a[j]) continue; if ((a[j] != b[i] || a[i] != b[j]) && (a[i] != c[j] || a[j] != c[i])) { cout << i + 1 << " " << j + 1 << endl; return 0; } } } cout << -1 << endl; return 0; }
8
CPP
#include<bits/stdc++.h> using namespace std; typedef long long int ll; int days[12]={31,28,31,30,31,30,31,31,30,31,30,31}; int main() { while(true){ string S; cin >> S; if(S=="#")break; int cou=0; for(int i=0;i<(int)S.length();i++){ if(S[i]=='.')cou++; } if(cou==2){ int y=0,m=0,d=0;cou=0; for(int i=0;i<(int)S.length();i++){ if(S[i]=='.'){ cou++; continue; } if(cou==0){ y*=10; y+=S[i]-'0'; } else if(cou==1){ m*=10; m+=S[i]-'0'; } else{ d*=10; d+=S[i]-'0'; } } ll sumday=0;int y1=2013;int m1=1; if(y==2012){ sumday=(ll)(d-21); } else{ sumday=11; while(y1<y-400){ y1+=400; sumday+=(ll)(366*97+365*303); } while(y1<y){ if(y1%400==0)sumday+=(ll)366; else if(y1%100==0)sumday+=(ll)365; else if(y1%4==0)sumday+=(ll)366; else sumday+=(ll)365; y1++; } while(m1<m){ if(m1==2){ if(y1%400==0)sumday+=(ll)29; else if(y1%100==0)sumday+=(ll)28; else if(y1%4==0)sumday+=(ll)29; else sumday+=(ll)28; } else{ sumday+=(ll)days[m1-1]; } m1++; } sumday+=(ll)(d-1); } ll b1,ka1,t1,w1,ki1; ki1=sumday%(ll)20; sumday/=(ll)20; w1=sumday%(ll)18; sumday/=(ll)18; t1=sumday%(ll)20; sumday/=(ll)20; ka1=sumday%(ll)20; sumday/=(ll)20; b1=sumday%(ll)13; printf("%lld.%lld.%lld.%lld.%lld\n",b1,ka1,t1,w1,ki1); } else{ int b=0,ka=0,t=0,w=0,ki=0;cou=0; for(int i=0;i<(int)S.length();i++){ if(S[i]=='.'){ cou++; continue; } if(cou==0){ b*=10; b+=S[i]-'0'; } else if(cou==1){ ka*=10; ka+=S[i]-'0'; } else if(cou==2){ t*=10; t+=S[i]-'0'; } else if(cou==3){ w*=10; w+=S[i]-'0'; } else{ ki*=10; ki+=S[i]-'0'; } } int y=2012;int m=12;int d=21; d+=b*(20*20*18*20)+ka*(20*18*20)+t*(18*20)+w*20+ki; while(d>days[m-1]){ d-=days[m-1]; m++; if(m==13){ y++;m=1; if(y%400==0)days[1]=29; else if(y%100==0)days[1]=28; else if(y%4==0)days[1]=29; else days[1]=28; } } days[1]=28; printf("%d.%d.%d\n",y,m,d); } } return 0; }
0
CPP
#include<stdio.h> int main(void) { int w,h,n,x[100000],y[100000],Xmax,cnt,flg[100000],kyori,ymax,i; scanf("%d%d%d",&w,&h,&n); Xmax=0; for(i=0;i<n;i++){ scanf("%d%d",&x[i],&y[i]); if(Xmax<x[i]) Xmax=x[i]; } for(i=0;i<=Xmax;i++) flg[i]=-1; flg[0]=0; for(i=0;i<n;i++){ if(flg[x[i]]<y[i]) flg[x[i]]=y[i]; } //for(i=0;i<=Xmax;i++) printf("flg[%d]=%d\n",i,flg[i]); kyori=1000000; ymax=0; for(i=Xmax;i>=0;i--){ if(flg[i]!=-1){ if(kyori>ymax+i) kyori=ymax+i; if(ymax<flg[i]) ymax=flg[i]; } //printf("i=%d ymax=%d\n",i,ymax); } printf("%d\n",kyori); return 0; }
0
CPP
length = int(input().split(" ")[1]) lanterns = list(int(x) for x in input().split(" ")) lanterns.sort() maximum = 0 for i in range(1, len(lanterns)): diff = lanterns[i] - lanterns[i - 1] if diff > maximum: maximum = diff maximum /= 2 if lanterns[0] > maximum: maximum = lanterns[0] if length - lanterns[-1] > maximum: maximum = length - lanterns[-1] print(maximum)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int inf = 2000000000; const int md = 1000003; class matrix { public: int n, m; vector<vector<int> > a; matrix() {} matrix(int _n, int _m) { n = _n; m = _m; a.resize(n, vector<int>(m, 0)); } matrix operator*(matrix B) { int m2 = B.m; if (m != B.n) { cout << "Can't multiply two matrix"; exit(1); } matrix A(n, m2); for (int i = 0; i < n; i++) { for (int k = 0; k < m; k++) { for (int j = 0; j < m2; j++) { A.a[i][j] = (A.a[i][j] + 1ll * a[i][k] * B.a[k][j]) % md; } } } return A; } }; matrix binpow(matrix a, int k) { if (k == 1) return a; if (k % 2) { return a * (binpow(a, k - 1)); } matrix f = binpow(a, k / 2); return f * f; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); int c, w, h; cin >> c >> w >> h; matrix A(w + 1, w + 1); for (int i = 0; i <= w; i++) { A.a[0][i] = (i ? 1ll * A.a[0][i - 1] * h % md : 1); } for (int i = 1; i <= w; i++) { A.a[i][i - 1] = 1; } matrix B(w + 1, 1); B.a[0][0] = 1; A = binpow(A, c + 1) * B; cout << A.a[0][0] << "\n"; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; struct node { int to, nxt; } e[maxn * 6]; int head[maxn]; int du[maxn]; int via[maxn]; int cnt; int n; void addage(int u, int v) { e[cnt].to = v; e[cnt].nxt = head[u]; head[u] = cnt++; e[cnt].to = u; e[cnt].nxt = head[v]; head[v] = cnt++; } void topu(int node) { queue<int> Q; Q.push(node); via[node] = 1; while (!Q.empty()) { int t = Q.front(); Q.pop(); cout << t << " "; for (int i = head[t]; i != -1; i = e[i].nxt) { int x = e[i].to; if (via[x] == 0) { du[x]--; if (du[x] == 1) { Q.push(x); via[x] = 1; } } } } } int main() { int x, y, z; cin >> n; memset(head, -1, sizeof(head)); memset(du, 0, sizeof(du)); memset(via, 0, sizeof(via)); for (int i = 0; i < n - 2; i++) { scanf("%d %d %d", &x, &y, &z); addage(x, y); addage(x, z); addage(y, z); du[x]++; du[y]++; du[z]++; } int isp1 = 0, isp2 = 0; for (int i = 1; i <= n; i++) { if (du[i] == 1) { if (isp1 == 0) isp1 = i; else du[i]++; } } for (int i = head[isp1]; i != -1; i = e[i].nxt) { int x = e[i].to; if (du[x] == 2) isp2 = x; } for (int i = 1; i <= n; i++) { if (du[i] == 2 && i != isp2) { du[i]++; } } topu(isp1); return 0; }
9
CPP
a=list(map(int,input().split())); a.sort() #print(a) f1=0; f2=0; #f1=a[len(a)-1] l=len(a) if l==1: print("NO") else: for i in range(len(a)-1,-1,-1): if(f1>=f2): f2=f2+a[i]; else: f1+=a[i]; if f1==f2: print("YES") else: print("NO")
7
PYTHON3
n,l,k = [int(i) for i in input().split()] k -= 1 s = sorted(input(),reverse=True) #print(s) m = [[-1 for j in range(l)] for i in range(n)] i = 0 j = 0 min_i = 0 last_c = '0' done = -1 while len(s)>0: c = s.pop() #print(i,j,c) m[i][j] = c if c != last_c: min_i = i last_c = c if i<k: i += 1 else: if j==l-1: break j += 1 i = min_i for i in range(n): for j in range(l): if m[i][j] == -1: m[i][j] = s.pop() for i in m: print(''.join(i))
19
PYTHON3
#include <bits/stdc++.h> using namespace std; long long manhattan(long long x, long long y, long long x2, long long y2) { return (abs(x - x2) ? 1 : 0) + (abs(y - y2) ? 1 : 0); } int main() { ios_base::sync_with_stdio(false); long long X[3], Y[3]; for (int i = 0; i < (int)3; ++i) { cin >> X[i] >> Y[i]; } if (X[0] == X[1] && X[1] == X[2] || Y[0] == Y[1] && Y[1] == Y[2]) { cout << 1 << endl; return 0; } int ans = 3; if (X[0] == X[1] && (Y[2] >= max(Y[0], Y[1]) || Y[2] <= min(Y[0], Y[1]))) ans = 2; if (X[1] == X[2] && (Y[0] >= max(Y[2], Y[1]) || Y[0] <= min(Y[2], Y[1]))) ans = 2; if (X[0] == X[2] && (Y[1] >= max(Y[2], Y[0]) || Y[1] <= min(Y[2], Y[0]))) ans = 2; if (Y[0] == Y[1] && (X[2] >= max(X[0], X[1]) || X[2] <= min(X[0], X[1]))) ans = 2; if (Y[1] == Y[2] && (X[0] >= max(X[2], X[1]) || X[0] <= min(X[2], X[1]))) ans = 2; if (Y[0] == Y[2] && (X[1] >= max(X[2], X[0]) || X[1] <= min(X[2], X[0]))) ans = 2; cout << ans << endl; }
10
CPP
#include <bits/stdc++.h> inline void read(int &x) { x = 0; int f = 1; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s >= '0' && s <= '9') { x = (x << 3) + (x << 1) + (s ^ 48); s = getchar(); } x *= f; } int main() { int n, m; read(n); read(m); for (register int i = 1; i <= n / 2; ++i) { for (register int j = 1; j <= m; ++j) { printf("%d %d\n", i, j); printf("%d %d\n", n - i + 1, m - j + 1); } } if (n & 1) { for (register int i = 1; i <= m / 2; ++i) { printf("%d %d\n", n / 2 + 1, i); printf("%d %d\n", n / 2 + 1, m - i + 1); } if (m & 1) printf("%d %d\n", n / 2 + 1, m / 2 + 1); } return 0; }
8
CPP
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int,int> pii; #define rep(i,n) for(ll i=0;i<(ll)(n);i++) struct st{ int first,second,ident; bool operator < (st obj) const{ return this->second < obj.second; } }; signed main(){ int n; while(cin>>n&&n){ set<pii> free; set<st> used; free.insert(pii(0,1e9+1)); rep(i,n){ char ch; cin>>ch; if(ch=='W'){ int num,len; cin>>num>>len; while(len>0){ pii tmp = *(free.begin()); free.erase(free.begin()); if(tmp.second-tmp.first<=len){ //???????????? len -= tmp.second-tmp.first; used.insert(st{tmp.first,tmp.second,num}); } else{ //???????????????????????? used.insert(st{tmp.first,tmp.first+len,num}); free.insert(pii(tmp.first+len,tmp.second)); len = 0; } } } if(ch=='D'){ int num; cin>>num; auto itr = used.begin(); while (itr != used.end()) { if (itr->ident==num) { st tmp = *itr; free.insert(pii(tmp.first,tmp.second)); used.erase(itr++); } else itr++; } } if(ch=='R'){ int pos; cin>>pos; auto res = used.upper_bound(st{0,pos,0}); if(res==used.end() || res->first>pos)cout<<"-1"<<endl; else{ st tmp = *(res); cout<<tmp.ident<<endl; } } } cout<<endl; } }
0
CPP
def main(): n,k,m,t = map(int,input().split()) multi = [0]*n multi[k-1] = 1 for i in range(t): op,index = map(int,input().split()) if op == 1: if k >= index: k += 1 multi.insert(index-1,0) else: if k > index: k -= index multi = multi[index:] else: multi = multi[:index] #print(multi) print(len(multi),k) main()
10
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long INF = 9223372036854775807; const long long N = 500005; const long long mod = 998244353; void ipgragh(long long n, long long m); void start(); double epsilon = 2.718281828459045235360; template <typename T> void read(T &x) { cin >> x; } template <typename T, typename T0> void read(T &x, T0 &y) { cin >> x >> y; } template <typename T, typename T0, typename T1> void read(T &x, T0 &y, T1 &z) { cin >> x >> y >> z; } template <typename T, typename T0, typename T1, typename T2> void read(T &x, T0 &y, T1 &z, T2 &w) { cin >> x >> y >> z >> w; } template <typename T> void read(T oneD[], long long n) { for (long long i = 0; i < n; i++) { read(oneD[i]); } } long long gcd(long long a, long long b) { if (b == 0) return a; gcd(b, a % b); } double logvalue(double b, double n) { b = log(b); n = log(n); double p = (1.0 * n) / (1.0 * b); return p; } vector<long long> adj[N]; struct p { long long number, cost, index; }; long long cases; struct CompareHeight { bool operator()(struct p const &p1, struct p const &p2) { if (p1.cost == p2.cost) { return (p1.index > p2.index); } return (p1.cost > p2.cost); } }; priority_queue<p, vector<p>, CompareHeight> Q; void solve() { long long n; read(n); long long a[n]; read(a, n); sort(a, a + n); long long ans = 0; for (long long(i) = 0; (i) <= (n / 2 - 1); (i) += (1)) { ans += (a[i] + a[n - i - 1]) * (a[i] + a[n - i - 1]); } cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; start(); solve(); } void start() {} void ipgraph(long long n, long long m) { for (long long(i) = 0; (i) <= (m - 1); (i) += (1)) { long long l, r; read(l, r); adj[l].push_back(r); adj[r].push_back(l); } }
9
CPP
a=[0]*26 d={'p':[],'s':[],'m':[]} for i in input().split(): d[i[1]]+=int(i[0]), a[ord(i[1])-97]+=1 if max(a)==1:print(2) else: x=chr(a.index(max(a))+97);v=[] if len(d[x])==2: g=abs(d[x][0]-d[x][1]) if g==1 or g==0 or g==2:print(1) else:print(2) else: v+=abs(d[x][0]-d[x][1]), v+=abs(d[x][0]-d[x][2]), v+=abs(d[x][1]-d[x][2]), if v.count(0)>1:print(0) elif v.count(0)==1:print(1) elif v.count(1)==2:print(0) elif v.count(1)==1 or v.count(2):print(1) else:print(2)
8
PYTHON3
def heapify(a, i): left = 2 * i + 1 right = 2 * i + 2 largest = i if left < len(a) and a[left] > a[largest]: largest = left if right < len(a) and a[right] > a[largest]: largest = right if largest != i: a[largest], a[i] = a[i], a[largest] heapify(a, largest) def build_heap(a): heap = a[::] for i in range(len(a) // 2)[::-1]: heapify(heap, i) return heap def decrease_root(a, value): a[0] = abs(abs(a[0]) - value) heapify(a, 0) if __name__ == '__main__': n, k1, k2 = [int(x) for x in input().split()] a = [int(x) for x in input().split()] b = [int(x) for x in input().split()] delta_heap = build_heap([abs(a[i] - b[i]) for i in range(len(a))]) k = k1 + k2 while k > 0: decrease_root(delta_heap, 1) k -= 1 print(sum([delta_heap[i] ** 2 for i in range(len(delta_heap))]))
8
PYTHON3
n = int(input()) listt = list(map(int, input().split( ))) crime = 0 plus = 0 for i in listt: if(plus == 0): if(i < 0): crime += 1 else: plus = 1 police = i else: if(i > 0): police +=i elif(i < 0): if(police > 0): police -=1 else: crime +=1 print(crime)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, l, sum, i; char str[2005]; cin >> n; cin >> str; l = strlen(str); if (l < 4) cout << "0" << endl; else { i = 0; sum = 0; while (i + n < l) { i = i + n; if (str[i - 1] == str[i - 2] && str[i - 2] == str[i - 3]) sum += 1; } cout << sum << endl; } return 0; }
7
CPP
import sys input = sys.stdin.readline ############ ---- Input Functions ---- ############ def inp(): return(int(input())) def inlt(): return(list(map(int,input().split()))) def insr(): s = input() return(list(s[:len(s) - 1]))#might need to remove the -1 def invr(): return(map(int,input().split())) n = inp() s = inlt() s = sorted(s, reverse=True) t = sum(s) g = 0 o = 0 for i in s: g += i o += 1 if g > (t-g): break print(o)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; long long *A = new long long[n]; for (int i = 0; i < n; i++) { cin >> A[i]; } long long a, b, c, d, e; long long a1 = 0, b1 = 0, c1 = 0, d1 = 0, e1 = 0; cin >> a >> b >> c >> d >> e; unsigned long long sum = 0; long long sum1; for (int i = 0; i < n; i++) { sum += A[i]; if (sum >= e) { sum1 = sum / e; sum = sum % e; e1 += sum1; } if (sum >= d) { sum1 = sum / d; sum = sum % d; d1 += sum1; } if (sum >= c) { sum1 = sum / c; sum = sum % c; c1 += sum1; } if (sum >= b) { sum1 = sum / b; sum = sum % b; b1 += sum1; } if (sum >= a) { sum1 = sum / a; sum = sum % a; a1 += sum1; } } cout << a1 << " " << b1 << " " << c1 << " " << d1 << " " << e1 << endl; cout << sum << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int n, d, b; int arr[100005]; int sz = 1; long long tree[400005]; void update(int idx, long long val) { idx += sz; while (idx) { tree[idx] += val; idx /= 2; } } long long query(int le, int ri) { le += sz; ri += sz; long long ret = 0; while (le <= ri) { if (le & 1) ret += tree[le++]; if (!(ri & 1)) ret += tree[ri--]; le /= 2; ri /= 2; } return ret; } int main() { scanf("%d%d%d", &n, &d, &b); sz = 1; while (sz < n) sz *= 2; for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); update(i, (long long)arr[i]); } int x1, x2; x1 = x2 = 0; long long le, ri; le = 0, ri = n - 1; long long r = 1; int dle, dri; dle = 0; dri = n - 1; while (le <= ri) { if (le == ri) { long long ls = query(max(0ll, le - d * r), min(le + d * r, n - 1ll)); long long diff = max(0ll, b - ls); if (diff) x1++; break; } long long ls = query(max(0ll, le - d * r), min(le + d * r, n - 1ll)); long long diff = max(0ll, b - ls); if (diff) x1++; long long up = diff ? 0 : b; while (up) { long long temp = query(dle, dle); if (temp <= up) { up -= temp; update(dle, -temp); dle++; } else { update(dle, -up); up = 0; } } long long rs = query(max(0ll, ri - d * r), min(ri + d * r, n - 1ll)); diff = max(0ll, b - rs); if (diff) x2++; up = diff ? 0 : b; while (up) { long long temp = query(dri, dri); if (temp <= up) { up -= temp; update(dri, -temp); dri--; } else { update(dri, -up); up = 0; } } le++; ri--; r++; } printf("%d\n", max(x1, x2)); return 0; }
10
CPP
#include<bits/stdc++.h> using namespace std; const int maxn = 1e2+7; int n,ar[maxn]; bool vis[maxn]; int ans[maxn]; int main() { scanf("%d",&n); int s; if(n & 1){ s = n; printf("%d\n",(n-1)*(n-1)/2); } else{ s = n+1; printf("%d\n",n*(n-2)/2); } for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++){ if(i+j != s)printf("%d %d\n",i,j); } } return 0; }
0
CPP
num=[] for i in range(10): num.append([]) num[i].append(1) num[i].append(0) n=int(input()) for kk in range(n): s=input() num[ord(s[0])-97][0]=0 for i in range(len(s)): num[ord(s[i])-97][1]+=pow(10,len(s)-i-1) num=sorted(num,key=lambda s:s[1],reverse=True) boo=True ans=0 now=1 for i in range(10): if boo and num[i][0]==1: ans+=0 boo=False else: ans+=num[i][1]*now now+=1 print(ans)
9
PYTHON3
f,r,c = list(map(int,input().split())) ans = min(f//3,min(r//2,c//2)) if(ans > 0): f = f - ans*3 r = r - ans*2 c = c - ans*2 ans = ans*7 #print(ans) '''f = f%3 r = r%2 c = c%2''' maxa = 0 fish = f rabbit = r chicken = c for i in range(7): count = 0 flag = 0 for j in range(7): a = (i+j)%7 if(a == 0 or a==1 or a==4): if(f == 0): flag = 1 f -= 1 if(a==2 or a==6): if(r == 0): flag = 1 r -= 1 if(a==3 or a==5): if(c == 0): flag = 1 c -= 1 if(flag == 0): count += 1 else: break maxa = max(count,maxa) f= fish r = rabbit c = chicken #print(maxa) print(ans+maxa)
9
PYTHON3
n = int(input()) if n>0: x1 = n//10 div = str(n%10) x2 = str(n//100) x2 = int(x2+div) else: p = -1*n x1 = p//10 div = str(p%10) x2 = str(p//100) x2 = int(x2+div) x2 *=-1 x1 *= -1 print(max(x1,x2,n))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1); const double eps = 1e-8; const int maxn = 1e6 + 5; const int maxm = 3e4 + 5; const long long mod = 1e9 + 7; const long long inf = 0x3f3f3f3f; const int _inf = -1e9 + 7; inline int scan() { int m = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') m = m * 10 + c - '0', c = getchar(); return m; } int N, Q, S, M, K; int num[200]; int dp[200][30000]; int main() { cin >> N >> K >> S; for (int i = 1; i <= N; i++) { scanf("%d", num + i); } memset(dp, inf, sizeof dp); dp[0][0] = 0; for (int i = N; i >= 1; i--) { for (int j = K - 1; j >= 0; j--) { for (int k = N * j; k >= 0; k--) { dp[j + 1][k + i - j - 1] = min(dp[j + 1][k + i - j - 1], dp[j][k] + num[i]); } } } int ans = inf; for (int i = 0; i <= min(S, N * N); i++) ans = min(ans, dp[K][i]); cout << ans; return 0; }
10
CPP
n,k=map(int,input().split()) t=input() i=max(0,0,*(i for i in range(n)if t[:i]==t[-i:])) print(t[:i]+t[i:]*k)
7
PYTHON3
#include<stdio.h> #include<string.h> #include<vector> #include<algorithm> using namespace std; int memo[10][2001]; vector<int>v[10]; int F(int n,int K) { if(n>=10||!K)return 0; int&r=memo[n][K]; if(r>=0)return r; r=F(n+1,K); for(int i=0;i<K&&i<v[n].size();++i) r=max(r,F(n+1,K-i-1)+v[n][i]+(i+1)*i); return r; } int main() { memset(memo,-1,sizeof(memo)); int N,K,C,G,i,j; for(scanf("%d%d",&N,&K);N--;) { scanf("%d%d",&C,&G); v[G-1].push_back(C); } for(i=0;i<10;++i) { sort(v[i].rbegin(),v[i].rend()); for(j=1;j<v[i].size();++j)v[i][j]+=v[i][j-1]; } printf("%d\n",F(0,K)); return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long n, d; cin >> n >> d; long long x = (n + 1) * (n + 1) / (4 * d) - 1; if (x >= 0) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
7
CPP
import sys readline = sys.stdin.readline def solve(): N = int(readline()) ans = 0 while True: for i in range(1, N + 2): if i * (3*i + 1) // 2 > N: break if i == 1: break ans += 1 N -= (i-1) * (3*(i-1) + 1) // 2 print(ans) T = int(readline()) for i in range(T): solve()
8
PYTHON3
row = 1 col = 1 while True: line = input() line = line.replace(' ', '') if '1' in line: col = line.find('1') + 1 break row += 1 if row == 6: break print(abs(row-3) + abs(col-3))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int cont[11]; int main() { string linha; int n, charc; cin >> n; for (int i = 0; i < 4; i++) { cin >> linha; for (int j = 0; j < 4; j++) { charc = linha[j] - 48; if (charc >= 1 && charc <= 9) cont[charc - 1]++; } } int aperta = 1; for (int i = 0; i < 9; i++) { if (cont[i] > 2 * n) aperta = 0; } if (aperta == 0) cout << "NO" << endl; else cout << "YES" << endl; }
7
CPP
#include <bits/stdc++.h> using namespace std; double presum[200005]; int main() { long long n, k; cin >> n >> k; for (long long i = 1; i <= n; i++) { long long x; cin >> x; presum[i] = presum[i - 1] + x; } double res = 0; for (long long i = k; i <= n; i++) { long long gt = i; for (long long j = i; j <= n; j++) { res = max(res, (presum[j] - presum[j - gt]) / gt); } } printf("%.15lf", res); }
9
CPP
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> pll; int main(){ while(1){ vector<vector<ll>> A(10000); ll an,bn,r; cin>>an>>bn>>r; if(an==0 && bn==0){break;} r*=4; vector<pll> w(an); for(int i=0;i<an;i++){ ll x,y; cin>>x>>y; w[i]={x,y}; } for(int i=0;i<bn;i++){ ll x,y; cin>>x>>y; A[y].push_back(x); } for(int i=0;i<A.size();i++){ sort(A[i].begin(),A[i].end()); } ll count=0; for(int i=0;i<an;i++){ pll &W=w[i]; ll d=0; for(ll y=r;y>=0;y--){ while((d+1)*(d+1)+y*y<=r*r){d++;} //cout<<y<<" "<<r<<" "<<d<<endl; if(y+W.second<A.size()){ count+=(ll)(upper_bound(A[y+W.second].begin(),A[y+W.second].end(),W.first+d)-lower_bound(A[y+W.second].begin(),A[y+W.second].end(),W.first-d)); } if(W.second-y>=0 && y!=0){ count+=(ll)(upper_bound(A[W.second-y].begin(),A[W.second-y].end(),W.first+d)-lower_bound(A[W.second-y].begin(),A[W.second-y].end(),W.first-d)); } } } cout<<count<<endl; } return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; int mins = 0; while (a > 0 && b > 0) { if (a < b) { a += 1; if (b < 2) break; b -= 2; } else { if (a < 2) break; a -= 2; b += 1; } mins += 1; } cout << mins << endl; return 0; }
7
CPP
L=int(input()) print(pow(L/3,3))
0
PYTHON3
#include<bits/stdc++.h> #define pb push_back using namespace std; const int maxn=3e6+10; char s[maxn]; int n,m; vector<int> ans,a; void print(){ printf("%d\n",(int)ans.size()); for (auto x:ans) printf("%d ",x);puts(""); } bool noprofile(){ for (int i=1;i<=n;i++) if (s[i]=='#') return 0; return 1; } void add1(int x){ if (x&1) ans.pb(2),x-=3; while (x) ans.pb(1),x-=2; } bool work0(){ if (s[1]!='#'||s[n]!='#') return 0; int last=0; for (int i=2;i<=n;i++){ if (s[i-1]=='_'&&s[i]=='_') return 0; if (s[i]=='_'){ ans.pb(i-last-1); last=i; } } ans.pb(n-last); print(); return 1; } bool work1(){ if (a[0]%2==0||a[m]%2==0) return 0; for (int i=2;i<=m-2;i+=2) if (a[i]%2==1) return 0; add1(a[0]-1); for (int i=1;i<=m-1;i++){ if (i%2==0){ add1(a[i]-2); } else { ans.pb(a[i]+1); } } add1(a[m]-1); print(); return 1; } bool work2(){ if (!(a[0]==2||a[0]>=4)) return 0; if (!(a[m]==2||a[m]>=4)) return 0; for (int i=2;i<=m-2;i+=2) if (!(a[i]==3||a[i]>=5)) return 0; add1(a[0]-2); for (int i=1;i<=m-1;i++){ if (i%2==0){ add1(a[i]-3); } else { ans.pb(a[i]+2); } } add1(a[m]-2); print(); return 1; } bool work3(){ if (!(a[0]==3||a[0]>=5)) return 0; if (!(a[m]==3||a[m]>=5)) return 0; for (int i=2;i<=m-2;i+=2) if (!(a[i]==4||a[i]>=6)) return 0; add1(a[0]-3); for (int i=1;i<=m-1;i++){ if (i%2==0){ add1(a[i]-4); } else { ans.pb(a[i]+3); } } add1(a[m]-3); print(); return 1; } int main(){ scanf("%s",s+1); n=strlen(s+1); if (s[1]=='#'||s[n]=='#'){ if (!work0()){ puts("-1"); } } else if (noprofile()){ puts("0"); } else { int last=0; for (int i=1;i<=n;i++) if (i==n||s[i]!=s[i+1]) a.pb(i-last),last=i; m=a.size()-1; if (!work1()&&!work2()&&!work3()){ puts("-1"); } } return 0; }
16
CPP
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + c - '0'; c = getchar(); } return x * f; } int n, len[100010]; char s[100010]; string ans[100010]; int main() { scanf("%s", s + 1); n = strlen(s + 1); for (int i = n, ok = 0; i >= 1; --i) { static char st[100010]; static char pre[100010]; static int top; if (ok && s[i] == st[top] && (st[top - 1] < s[i] || (s[i] == st[top - 1] && pre[top - 1] < s[i]))) --top, ok = 0; else { st[++top] = s[i]; if (st[top] == st[top - 1]) pre[top] = pre[top - 1]; else pre[top] = st[top - 1]; ok = 1; } if (top <= 10) { for (int j = top; j >= 1; --j) ans[i] += st[j]; } else { for (int j = top; j >= top - 4; --j) ans[i] += st[j]; ans[i] += "..."; for (int j = 2; j >= 1; --j) ans[i] += st[j]; } len[i] = top; } for (int i = 1; i <= n; ++i) { cout << len[i] << " " << ans[i] << endl; } return 0; }
11
CPP
#include <cstdio> #include <algorithm> #define mp(x,y) make_pair(x,y) #define ft first #define sd second #define rep(i,l,r) for (int i=l;i<=r;++i) #define P pair<int ,int > using namespace std; const int N =2e5+10; void read(int &x) { x=0; char ch=getchar();int f=1; while (ch<'0'||ch>'9') ch=='-'?f=-1:0,ch=getchar(); while ('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=getchar(); x*=f; } int n,bo[N],tot,m,a[N],top,v[233],l[N][20],r[N][20],f[1<<20],g[1<<20],cnt,tmp; P b[N<<1]; void _mx(int &x,int y) { x=max(x,y); } void _mn(int &x,int y) { x=min(x,y); } int main() { read(n);read(m); rep(i,1,n) read(a[i]); for (;v[top++]=m;m>>=1); rep(i,0,top) { int lst=1; rep(j,2,n) if (a[j]-a[j-1]>v[i]) { rep(k,lst,j-1) l[k][i]=lst,r[k][i]=j-1; lst=j; } rep(k,lst,n) l[k][i]=lst,r[k][i]=n; } tot=1<<top;--tot;rep(i,0,tot) g[i]=n+1; rep(i,0,tot) rep(j,0,top) if (!(i&1<<j)) _mx(f[i|1<<j] ,r[f[i]+1][j]),_mn(g[i|1<<j] ,l[g[i]-1][j]); rep(i,0,tot) if (!(i&1)) b[++cnt]=mp(g[i],i); rep(i,1,n) b[++cnt]=mp(r[i][0]+1,(1<<top)+i); sort(b+1,b+1+cnt);tmp=-1; rep(i,1,cnt) if (b[i].sd>(1<<top)) { b[i].sd-=(1<<top); if (tmp>=l[b[i].sd][0]-1) bo[b[i].sd]=1; }else _mx(tmp,f[((1<<top)-2)^b[i].sd]); rep(i,1,n) printf("%s\n",bo[i]?"Possible":"Impossible"); return 0; }
0
CPP
#include <bits/stdc++.h> int inp() { char c = getchar(); while (c < '0' || c > '9') c = getchar(); int sum = 0; while (c >= '0' && c <= '9') { sum = sum * 10 + c - '0'; c = getchar(); } return sum; } struct Node { int op; long long a; long long b; int id; }; long long num[100010]; std::vector<Node> vec1[100010]; std::vector<Node> vec; bool cmp1(Node a, Node b) { return a.a > b.a; } bool cmp2(Node a, Node b) { double d1 = (double)(a.a) / (double)(a.b); double d2 = (double)(b.a) / (double)(b.b); return d1 > d2; } bool cmp3(Node a, Node b) { return a.op < b.op; } long long max[100010]; int mid[100010]; int main() { int k = inp(); int n = inp(); int m = inp(); for (int i = 1; i <= k; i++) num[i] = inp(); for (int i = 1; i <= n; i++) { int op = inp(); int x = inp(); long long b = inp(); if (op == 1) { if (b > max[x]) { max[x] = b; mid[x] = i; } } else if (op == 2) { vec1[x].push_back((Node){op, b, -1, i}); } else { vec.push_back((Node){op, b, 1, i}); } } for (int i = 1; i <= k; i++) { if (max[i] > num[i]) vec1[i].push_back((Node){1, max[i] - num[i], -1, mid[i]}); if (!vec1[i].size()) continue; std::sort(vec1[i].begin(), vec1[i].end(), cmp1); long long sum = num[i]; for (int j = 0; j < vec1[i].size(); j++) { vec.push_back( (Node){vec1[i][j].op, sum + vec1[i][j].a, sum, vec1[i][j].id}); sum += vec1[i][j].a; } } std::sort(vec.begin(), vec.end(), cmp2); std::vector<Node>::iterator it = vec.begin(); int cnt = std::min((int)(vec.size()), m); for (int i = 1; i <= cnt; i++) it++; std::sort(vec.begin(), it, cmp3); printf("%d\n", cnt); for (int i = 0; i < cnt; i++) printf("%d ", vec[i].id); }
10
CPP
#include <bits/stdc++.h> using namespace std; const int MX = 100009; const long long INF = (1ll << 60); const int inf = (1 << 30); const long long mod = 1e9 + 7; int n, a, b, subtree[MX], par[MX], blocked[MX]; char ans[MX]; vector<int> v[MX]; void dfs(int x, int p) { subtree[x] = 1; par[x] = p; for (auto pp : v[x]) { if (pp == p || blocked[pp]) continue; dfs(pp, x); subtree[x] += subtree[pp]; } } void create(int x, char c) { int second = subtree[x], idx; queue<int> q; q.push(x); dfs(x, -1); while (!q.empty()) { int nxt = q.front(); q.pop(); int s = subtree[x] - subtree[nxt]; for (auto pp : v[nxt]) { if (pp == par[nxt] || blocked[pp]) continue; s = max(s, subtree[pp]); q.push(pp); } if (second > s) { second = s; idx = nxt; } } int HHH = 0; blocked[idx] = 1; ans[idx] = c; for (auto pp : v[idx]) { if (blocked[pp]) continue; create(pp, c + 1); } } int main() { scanf("%d", &n); for (int i = 0; i < n - 1; i++) { scanf("%d%d", &a, &b); v[a].push_back(b); v[b].push_back(a); } dfs(1, -1); create(1, 'A'); for (int i = 1; i <= n; i++) printf("%c ", ans[i]); }
11
CPP
for _ in range(int(input())): a, b = map(int, input().split()) d = max(min(a, b) * 2, max(a, b)) print(d*d)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int father[2005]; int c[2005]; int N, M; int sum[2005]; int getfather(int x) { return father[x] == x ? x : father[x] = getfather(father[x]); } int main() { scanf("%d%d", &N, &M); for (int i = 1; i <= N; i++) father[i] = i; for (int i = 1; i <= M; i++) { int a, b; scanf("%d%d", &a, &b); a = getfather(a); b = getfather(b); if (a != b) father[a] = b; } scanf("%d", &M); for (int i = 1; i <= M; i++) { int a, b; scanf("%d%d", &a, &b); a = getfather(a); b = getfather(b); if (a == b) c[a] |= 1; } memset(sum, 0, sizeof(sum)); for (int i = 1; i <= N; i++) if (!c[getfather(i)]) sum[getfather(i)]++; int ret = 0; for (int i = 1; i <= N; i++) ret = max(ret, sum[i]); cout << ret << endl; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int t; cin >> t; while (t--) { int n; cin >> n; string a, b; cin >> a >> b; bool ok = true; for (int i = 0; i < n; i++) { if (a[i] > b[i]) ok = false; } if (!ok) { cout << -1 << endl; continue; } int ans = 0; for (char c = 'a'; c <= 't'; c++) { set<int> differences; set<int> allowed; for (int i = 0; i < n; i++) { if (b[i] != c) continue; if (a[i] != b[i]) differences.insert(b[i] - a[i]), allowed.insert(a[i]); } ans += differences.size(); for (int i = 0; i < n; i++) { if (a[i] < b[i] && allowed.count(a[i])) a[i] = c; } } cout << ans << endl; } }
7
CPP
n, c = [int(x) for x in input().split()] a = list(map(int, input().split())) b = list(map(int, input().split())) dpst = [0] * (n + 1) dplift = [0] * (n + 1) dpst[2] = a[0] dplift[2] = c + b[0] for i in range(3, n + 1): dpst[i] = a[i - 2] + min(dpst[i - 1], dplift[i - 1]) dplift[i] = b[i - 2] + min(c + dpst[i - 1], dplift[i - 1]) for i in range(1, n + 1): print(min(dpst[i], dplift[i]), end = " ")
11
PYTHON3
h=input() a=list(map(int,input().split(' '))) b=list(map(int,input().split(' '))) for x in a: if(x in b): print(x," ",end="")
7
PYTHON3
n = int(input()) a = list(map(int, input().split())) ans = (sum(a) // (n - 1)) + int(sum(a) % (n - 1) != 0) print(max(max(a), ans))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[100007], n; bool ok = false; int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); sort(a, a + n); if (a[n - 1] == 1) a[n - 1] = 2; else a[n - 1] = 1; sort(a, a + n); for (int i = 0; i < n; ++i) printf("%d ", a[i]); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int MAX = 205; char input[MAX], s[MAX]; vector<int> v; map<char, char> mp; int main() { mp['W'] = 'B'; mp['B'] = 'W'; int n; scanf("%d%s", &n, input); strcpy(s, input); int tot = 0; for (int i = 0; i < n - 1; i++) if (s[i] == 'W') { s[i] = 'B'; s[i + 1] = mp[s[i + 1]]; tot++; v.push_back(i + 1); } if (s[n - 1] == 'B') { printf("%d\n", tot); for (int i = 0; i < v.size(); i++) printf("%d ", v[i]); printf("\n"); return 0; } tot = 0; v.clear(); strcpy(s, input); for (int i = 0; i < n - 1; i++) if (s[i] == 'B') { s[i] = 'W'; s[i + 1] = mp[s[i + 1]]; tot++; v.push_back(i + 1); } if (s[n - 1] == 'W') { printf("%d\n", tot); for (int i = 0; i < v.size(); i++) printf("%d ", v[i]); printf("\n"); return 0; } printf("-1\n"); }
8
CPP
t=int(input()) for i in range(t): a1,b1,c1,d1=input().split(" ") a=int(a1) b=int(b1) c=int(c1) d=int(d1) s=k=b if (b>=a): print(b) else: if (d>=c): print(-1) else: if ((a-b) % (c-d)==0): k=int((a-b)/(c-d)) else: k=int((a-b)/(c-d))+1 s=s+k*c print(s)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 500010; int main() { int n; scanf("%d", &n); char ch; int x; int n0 = 0, n1 = (1 << 10) - 1; for (int i = (1); i <= (n); i++) { while (ch = getchar(), ch != '|' && ch != '&' && ch != '^') ; scanf("%d", &x); if (ch == '|') n0 |= x, n1 |= x; else if (ch == '&') n0 &= x, n1 &= x; else if (ch == '^') n0 ^= x, n1 ^= x; } int ans0 = 0, ans1 = 0, ans2 = 0; for (int i = (0); i <= (9); i++) { if ((n0 & (1LL << (i))) || (n1 & (1LL << (i)))) ans0 |= (1LL << (i)); if ((n0 & (1LL << (i))) && (n1 & (1LL << (i)))) ans1 |= (1LL << (i)); if ((n0 & (1LL << (i))) && !(n1 & (1LL << (i)))) ans2 |= (1LL << (i)); } printf("3\n& %d\n| %d\n^ %d\n", ans0, ans1, ans2); return 0; }
7
CPP
def gcd (a, b) : while (b) : a %= b a, b = b, a return a; t, w, b = map(int, input().split()) g = w * b // gcd(w, b) res = 0 minh = min(w, b) res += (t // g + 1) * minh - 1 correct = (t // g) * g + minh - 1 if (correct > t) : res -= correct - t y = gcd(res, t) print(res // y, "/", t // y, sep = "")
9
PYTHON3
a,b,c = input().split() print(('NO','YES')[a[-1]==b[0] and b[-1]==c[0]])
0
PYTHON3
q=int(input()) for i in range(q): k=0 n=int(input()) while(n%5==0): n=4*n//5 k+=1 while(n%3==0): n=2*n//3 k+=1 while(n%2==0): n=n//2 k+=1 if(n!=1): print(-1) else: print(k)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int64_t maxn = 1.5e6; vector<int64_t> qui[maxn], quo[maxn]; signed main() { ios::sync_with_stdio(0); cin.tie(0); int64_t n, m, k; cin >> n >> m >> k; int64_t d[m], f[m], t[m], c[m]; set<pair<int64_t, int64_t>> in[n + 1], out[n + 1]; for (int64_t i = 0; i < m; i++) { cin >> d[i] >> f[i] >> t[i] >> c[i]; if (f[i] != 0 && t[i] != 0) continue; if (t[i] == 0) qui[d[i]].push_back(i); else if (d[i] >= k) out[t[i]].insert({c[i], d[i]}), quo[d[i]].push_back(i); } int64_t ans = 1e18; int64_t sumo = 0, sumi = 0; int64_t empty_o = 0; int64_t empty_i = n; for (int64_t i = 1; i <= n; i++) { if (out[i].empty()) empty_o++; else sumo += out[i].begin()->first; } for (int64_t i = 0; i < maxn; i++) { if (i + k - 1 < maxn) for (auto it : quo[i + k - 1]) { sumo -= out[t[it]].begin()->first; out[t[it]].erase({c[it], d[it]}); if (out[t[it]].empty()) empty_o++; else sumo += out[t[it]].begin()->first; } if (empty_o == 0 && empty_i == 0) ans = min(ans, sumo + sumi); for (auto it : qui[i]) { if (in[f[it]].empty()) empty_i--; else sumi -= in[f[it]].begin()->first; in[f[it]].insert({c[it], d[it]}); sumi += in[f[it]].begin()->first; } } if (ans == 1e18) cout << -1 << endl; else cout << ans << endl; return 0; }
8
CPP
#include<bits/stdc++.h> using namespace std; int main(){ while(1){ long e; cin >> e; if(e==0) break; long ans=1000000; long tz,x; for(long z=0;e>=z*z*z;z++){ tz=e-z*z*z; for(long y=0;tz>=y*y;y++){ x=tz-y*y; ans=min(ans,x+y+z); } } cout << ans << endl; } return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int val[26] = {0, 0, 1, 1, 5, 1, 21, 1, 85, 73, 341, 89, 1365, 1, 5461, 4681, 21845, 1, 87381, 1, 349525, 299593, 1398101, 178481, 5592405, 1082401}; inline int solve(int x) { for (int i = 2; i <= 25; i++) { if (x == (1 << i) - 1) { return val[i]; } } for (int i = 25; i >= 0; i--) if (x & (1 << i)) return (1 << (i + 1)) - 1; return -1; } int main() { int q, x; scanf("%d", &q); while (q--) { scanf("%d", &x); printf("%d\n", solve(x)); } return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int n, m, q, u1, u2; const int N = 3005; vector<int> G[N], v; map<pair<pair<int, int>, int>, bool> vis; int parent[N][N]; bool q1; void bfs() { queue<pair<int, int> > q; q.push(make_pair(1, 1)); while (!q.empty()) { u1 = q.front().first; u2 = q.front().second; q.pop(); if (u2 == n) { q1 = true; return; } for (int i = 0; i < G[u2].size(); i++) { int u3 = G[u2][i]; if (vis[make_pair(make_pair(u1, u2), u3)] == 1 || parent[u2][u3] != 0) continue; parent[u2][u3] = u1; q.push(make_pair(u2, u3)); } } } int main() { cin >> n >> m >> q; while (m--) { int x, y; cin >> x >> y; G[x].push_back(y); G[y].push_back(x); } while (q--) { int a, b, c; cin >> a >> b >> c; vis[make_pair(make_pair(a, b), c)] = 1; } bfs(); if (!q1) { cout << -1; return 0; } int new1 = u1; int new2 = u2; while (new1 != 1) { int x = new2; new2 = new1; new1 = parent[new2][x]; v.push_back(new2); } cout << v.size() + 1 << "\n"; reverse(v.begin(), v.end()); cout << "1" << " "; for (int i = 0; i < v.size(); i++) cout << v[i] << ' '; cout << n; }
11
CPP
from heapq import * import sys if __name__ == '__main__': q, n = [int(__) for __ in input().strip().split()] input = sys.stdin.readline arr = list(range(n)) heap = [] op = [] for x in arr: heappush(heap, x) for _ in range(q): inp = int(input()) % n arr[inp] += n heappush(heap, arr[inp]) sm = heappop(heap) while arr[sm % n] != sm: sm = heappop(heap) op.append(sm) heappush(heap, sm) print("\n".join([str(x) for x in op]))
10
PYTHON3
#include <bits/stdc++.h> #pragma warning(disable : 4996) #pragma comment(linker, "/STACK:336777216") using namespace std; inline long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } inline long long power(long long a, long long n, long long m) { if (n == 0) return 1; long long p = power(a, n / 2, m); p = (p * p) % m; if (n % 2) return (p * a) % m; else return p; } const long long MOD = 998244353; const int INF = 0x3f3f3f3f; const long long LL_INF = 0x3f3f3f3f3f3f3f3f; int main() { int n, m, k; cin >> n >> m >> k; long long f[n + 1][k + 1]; memset(f, (long long)0, sizeof(f)); f[1][0] = m; for (int i = 2; i <= n; i++) { for (int j = 0; j <= k; j++) { f[i][j] = f[i - 1][j]; if (j >= 1) { f[i][j] = (f[i][j] + (f[i - 1][j - 1] * (m - 1)) % MOD) % MOD; } } } cout << f[n][k] << endl; return 0; }
9
CPP
s=input() k=int(input()) ans="" cane=0 flake=0 l=0 for i in s: if i=="*": flake+=1 elif i=="?": cane+=1 else: l+=1 if l==k: for i in s: if i!="*" and i!="?": ans+=i print(ans) elif l<k: t=k-l flag=0 if flake!=0: for i in range(len(s)): if s[i]=="*" and flag==0: flag=1 ans+=s[i-1]*t if s[i]!="*" and s[i]!="?": ans+=s[i] print(ans) else: print("Impossible") else: if l-(flake+cane)<=k: t=abs(l-k) a=[] for i in range(len(s)): if (s[i]=="?" or s[i]=="*") and t>0: t-=1 del a[len(a)-1] elif s[i]!="?" and s[i]!="*": a.append(s[i]) ans="" for i in a: ans+=i print(ans) else: print("Impossible")
9
PYTHON3
def solution(n,m): if n == 1: print(0) elif n == 2: print(m) else: print(m*2) for _ in range(int(input())): n, m = map(int, input().split()) solution(n, m)
7
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target( \ "avx,avx2,fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; long long MOD = 1e9 + 7; const int N = 1000000; long long n; long long fl(long long x, long long y) { return 1 <= x && x <= n && 1 <= y && y <= n; } vector<long long> xx; map<long long, vector<long long> > keko; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout << fixed << setprecision(20); long long m; cin >> n >> m; for (int i = 0; i < m; i++) { long long x, y; cin >> x >> y; xx.push_back(x); keko[x].push_back(y); } keko[n].push_back(n + 1); xx.push_back(n); sort(xx.begin(), xx.end()); xx.resize(unique(xx.begin(), xx.end()) - xx.begin()); for (auto x : xx) { keko[x].push_back(0); keko[x].push_back(n + 1); sort(keko[x].begin(), keko[x].end()); } set<pair<long long, long long> > otr; long long lst = 1; otr.insert({1, n}); if (keko[1].size()) { otr.clear(); otr.insert({1, keko[1][1] - 1}); } for (auto x : xx) { if (!otr.size()) { cout << -1; return 0; } if (x <= lst) { continue; } if (x != lst + 1) { long long mn = (*(otr.begin())).first; otr.clear(); otr.insert({mn, n}); } lst = x; auto it = otr.begin(); set<pair<long long, long long> > otr2; for (auto hv : keko[x]) { long long mn = MOD; while (it != otr.end() && it->second < hv) { mn = min(mn, it->first); it++; } if (mn != MOD) otr2.insert({mn, hv - 1}); if (it != otr.end() && it->first <= hv) { if (it->first != hv) { otr2.insert({it->first, hv - 1}); } long long sc = it->second; otr.erase(it); if (sc > hv) otr.insert({hv + 1, sc}); it = otr.lower_bound({hv + 1, sc}); } } otr.clear(); otr = otr2; } if (!otr.size()) { cout << -1; return 0; } auto it = otr.end(); it--; if (it->second < n) { cout << -1; return 0; } cout << 2 * (n - 1); }
8
CPP
#include <iostream> #include <queue> #include <vector> #include <map> #include <cstdio> #include <cstring> #include <cmath> using namespace std; const int INF = 1e+9 * 2; typedef pair<int, int> P; struct edge { int to, c; edge(int _to, int _c) : to(_to), c(_c){} }; int N, M, d[3001]; vector<edge> G[3001]; int main() { int k, shop, a, b, l; cin >> N >> M >> k; for(int i = 0; i < M; ++i){ scanf("%d %d %d", &a, &b, &l); G[a].push_back(edge(b, l)); G[b].push_back(edge(a, l)); } priority_queue<P, vector<P>, greater<P> > que; fill(d, d + N + 1, INF); for(int i = 0; i < k; ++i){ scanf("%d", &shop); d[shop] = 0; que.push(P(0, shop)); } while(!que.empty()){ P p = que.top(); que.pop(); int v = p.second; if(d[v] < p.first) continue; for(int i = 0; i < G[v].size(); ++i){ edge e = G[v][i]; if(d[e.to] > d[v] + e.c){ d[e.to] = d[v] + e.c; que.push(P(d[e.to], e.to)); } } } double ans = 0; for(int i = 1; i <= N; ++i){ for(int j = 0; j < G[i].size(); ++j){ ans = max(ans, (G[i][j].c + d[i] + d[G[i][j].to]) / 2.0); } } cout << round(ans) << endl; return 0; }
0
CPP
ax, ay, bx, by, cx, cy = map(int, input().split()) distAB = ((ax - bx) ** 2) + ((ay - by) ** 2) distBC = ((bx - cx) ** 2) + ((by - cy) ** 2) proj1 = (cx - bx)*(by - ay) proj2 = (bx - ax)*(cy - by) if (distAB == distBC and proj1 != proj2): print("Yes") else: print("No")
8
PYTHON3