solution
stringlengths
10
159k
difficulty
int64
0
3.5k
language
stringclasses
2 values
import sys input=sys.stdin.readline t=int(input()) for i in range(t): n=int(input()) l=list(map(int,input().split())) v=l.count(0) s=sum(l) if s+v==0:v+=1 print(v)
800
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize("Ofast") #pragma GCC optimize("inline") #pragma GCC optimize("-fgcse") #pragma GCC optimize("-fgcse-lm") #pragma GCC optimize("-fwhole-program") #pragma GCC optimize("-freorder-blocks") #pragma GCC optimize("-fschedule-insns") #pragma GCC optimize("inline-functions") #pragma GCC optimize("-ftree-tail-merge") using namespace std; const int P = 1 << 19; char s[P]; bool f[2][P]; const int MOD = 7000007; const int g = 131; bool mp[MOD]; int Hash[P]; int main() { int n; scanf("%d%s", &n, s + 1); int now = 0, lst = 1; int lim = floor(sqrt(n * 2)); int als = 1; for (int len = 1; len <= lim; len++) { now ^= 1, lst ^= 1; if (len == 1) { for (int i = 1; i <= n; i++) f[now][i] = 1; } else { memset(f[now], 0, sizeof(f[now])); memset(mp, 0, sizeof(mp)); for (int i = n - len + 1; i; --i) { if (i + len <= n && f[lst][i + len]) mp[Hash[i + len]] = 1; if (mp[Hash[i]] || mp[Hash[i + 1]]) { als = len; f[now][i] = 1; } } } for (int i = 1; i + len - 1 <= n; i++) Hash[i] = (Hash[i] * g + s[i + len - 1]) % MOD; } printf("%d", als); return 0; }
3,300
CPP
#!/usr/bin/env pypy3 from sys import stdin, stdout def input(): return stdin.readline().strip() def read_int_list(): return list(map(int, input().split())) def read_int_tuple(): return tuple(map(int, input().split())) def read_int(): return int(input()) ### CODE HERE def ans(S): ret = 0 num_0 = 0 num_1 = 0 parity_0 = None def drop(idx): nonlocal num_0, num_1, parity_0 if idx < 0: return if S[idx] == '?': return if S[idx] == '1': num_1 -= 1 if S[idx] == '0': num_0 -= 1 if num_0 + num_1 == 0: parity_0 = None def ok(idx): nonlocal num_0, num_1, parity_0 if idx >= len(S): return False if S[idx] == '?': return True if S[idx] == '0': if parity_0 is None: return True return (parity_0 == (idx%2)) if S[idx] == '1': if parity_0 is None: return True return (parity_0 == 1-(idx%2)) def consume(idx): nonlocal num_0, num_1, parity_0 if S[idx] == '?': return if S[idx] == '0': num_0 += 1 parity_0 = idx%2 if S[idx] == '1': num_1 += 1 parity_0 = 1-idx%2 j = -1 for i in range(len(S)): # print('before drop', num_0, num_1, parity_0) drop(i-1) # print('after drop', num_0, num_1, parity_0) while ok(j+1): j += 1 consume(j) ret += (j-i+1) # print(i, j) return ret for _ in range(read_int()): print(ans(input()))
1,400
PYTHON3
#include <bits/stdc++.h> int main() { int T; scanf("%d", &T); while (T--) { int n, m; scanf("%d%d", &n, &m); int a, b; int c, d; bool ans = false; for (int i = 0; i < n; ++i) { scanf("%d%d%d%d", &a, &b, &c, &d); if (c == b) ans = true; } if (ans && m % 2 == 0) printf("YES\n"); else printf("NO\n"); } return 0; }
900
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n, k; while (cin >> n >> k) { char m[4][n]; bool available = 0; if (k % 2 == 0) { available = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { if (i > 0 && j > 0 && i < n - 1 && j < 3 && k-- > 0) { m[j][i] = '#'; } else { m[j][i] = '.'; } } } } else { available = 1; for (int i = 0; i < 4; i++) { m[i][n / 2] = '.'; } m[1][n / 2] = '#'; for (int i = 0; i < 4; i++) { for (int j = 0; j < n / 2; j++) { if (i > 0 && j > 0 && i < 3 && j < n - 1 && (k -= 2) > 0) { m[i][j] = '#'; m[i][n - j - 1] = '#'; } else { m[i][j] = '.'; m[i][n - j - 1] = '.'; } } } } if (available) { cout << "YES" << endl; for (int i = 0; i < 4; i++) { for (int j = 0; j < n; j++) { cout << m[i][j]; } cout << endl; } } else { cout << "NO" << endl; } } }
1,600
CPP
#include <bits/stdc++.h> using namespace std; const long long MAX = 1000000000000000000; long long mod = 1000000000; long double pi = 3.141592653589793238; void pls() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } long long n; long long dis[501][501]; long long ndis[501][501]; vector<long long> ans, ver; void solve() { cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> dis[i][j]; } } for (int i = 0; i < n; i++) { long long x; cin >> x; x--; ver.push_back(x); } reverse(ver.begin(), ver.end()); for (int k = 0; k < ver.size(); k++) { int u = ver[k]; long long tot = 0; for (int i = 0; i < k; i++) { ndis[ver[i]][u] = dis[ver[i]][u]; ndis[u][ver[i]] = dis[u][ver[i]]; } for (int i = 0; i <= k; i++) { for (int j = 0; j <= k; j++) { if (ndis[ver[j]][u] > ndis[ver[j]][ver[i]] + ndis[ver[i]][u]) ndis[ver[j]][u] = ndis[ver[j]][ver[i]] + ndis[ver[i]][u]; } } for (int i = 0; i <= k; i++) { for (int j = 0; j <= k; j++) { if (ndis[u][ver[j]] > ndis[u][ver[i]] + ndis[ver[i]][ver[j]]) ndis[u][ver[j]] = ndis[u][ver[i]] + ndis[ver[i]][ver[j]]; } } for (int i = 0; i <= k; i++) { for (int j = 0; j <= k; j++) { if (ndis[ver[i]][ver[j]] > ndis[ver[i]][u] + ndis[u][ver[j]]) ndis[ver[i]][ver[j]] = ndis[ver[i]][u] + ndis[u][ver[j]]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { tot += ndis[i][j]; } } ans.push_back(tot); } reverse(ans.begin(), ans.end()); for (auto x : ans) { cout << x << " "; } cout << endl; } int main() { pls(); solve(); return 0; }
1,700
CPP
R = lambda:map(int, input().split()) n,m,k = R() a,b=zip(*sorted(zip(R(),range(1,n+1)))[-m*k:]) print(sum(a),*sorted(b)[m-1:-1:m])
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> bool ckmin(T &a, U b) { return b < a ? a = b, true : false; } template <typename T, typename U> bool ckmax(T &a, U b) { return b > a ? a = b, true : false; } const int INF = 2e9; template <typename T> struct SparseTable { int SZ, L; vector<int> logs; vector<vector<T>> table; T cmb(T x, T y) { T z = {max(x.first, y.first), min(x.second, y.second)}; return z; } SparseTable(T id, int size, vector<T> &v) : SZ(size) { L = log2(SZ) + 1; logs.assign(SZ + 1, 0); for (int i = 2; i <= SZ; ++i) logs[i] = logs[i >> 1] + 1; table.assign(L + 1, vector<T>(SZ, id)); build(v); } void build(vector<T> &v) { for (int i = 0; i < SZ; ++i) table[0][i] = v[i]; for (int i = 1; i <= L; ++i) { for (int j = 0; j < SZ; ++j) { table[i][j] = cmb(table[i - 1][j], table[i - 1][min(j + (1 << (i - 1)), SZ - 1)]); } } } T qry(int l, int r) { int j = logs[r - l + 1]; return cmb(table[j][l], table[j][r - (1 << j) + 1]); } }; int n, k; long double ratio, res = 0; vector<int> mx; vector<pair<int, int>> v; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; mx.assign(n, 0); v.assign(n, {-1, -1}); for (int i = 0; i < n; ++i) { cin >> v[i].first; v[i].first *= 100; } for (int i = 0; i < n; ++i) cin >> v[i].second; SparseTable<pair<int, int>> st({-INF, INF}, n, v); for (int i = 0; i < n; ++i) { int l = i, r = n - 1, ver = i; while (l <= r) { int m = l + (r - l) / 2; pair<int, int> cur = st.qry(i, m); ckmax(mx[i], min(cur.first, cur.second)); if (cur.first >= cur.second) { ver = m; r = m - 1; } else l = m + 1; } pair<int, int> tmp = st.qry(i, ver); ckmax(mx[i], min(tmp.first, tmp.second)); if (ver > i) { tmp = st.qry(i, ver - 1); ckmax(mx[i], min(tmp.first, tmp.second)); } } sort(mx.begin(), mx.end()); long double ratio = (res + k) / n; for (int i = 0; i < n - (k - 1); ++i) { res += ratio * mx[i]; ratio = (ratio * (n - k - i)) / (n - i - 1); } cout << setprecision(10) << fixed; cout << res; return 0; }
2,400
CPP
#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // using namespace __gnu_pbds; using namespace std; #define ll long long #define ull unsigned long long #define ld long double #define pii pair<int, int> #define pll pair<ll, ll> #define vi vector<int> #define vll vector<ll> #define vc vector<char> #define vs vector<string> #define vpll vector<pll> #define vpii vector<pii> #define umap unordered_map #define uset unordered_set #define PQ priority_queue #define printa(a, L, R) \ for (int i = L; i < R; i++) \ cout << a[i] << (i == R - 1 ? '\n' : ' ') #define printv(a) printa(a, 0, a.size()) #define print2d(a, r, c) \ for (int i = 0; i < r; i++) \ for (int j = 0; j < c; j++) \ cout << a[i][j] << (j == c - 1 ? '\n' : ' ') #define pb push_back #define eb emplace_back #define UB upper_bound #define LB lower_bound #define F first #define S second #define mem(a, x) memset(a, x, sizeof(a)) #define inf 1e18 #define E 2.71828182845904523536 #define gamma 0.5772156649 #define nl "\n" #define lg(r, n) (int)(log2(n) / log2(r)) #define rev(v) reverse(v.begin(), v.end()) #define srt(v) sort(v.begin(), v.end()) #define grtsrt(v) sort(v.begin(), v.end(), greater<ll>()) #define all(v) v.begin(), v.end() #define mnv(v) *min_element(v.begin(), v.end()) #define mxv(v) *max_element(v.begin(), v.end()) #define toint(a) atoi(a.c_str()) #define BeatMeScanf ios_base::sync_with_stdio(false) #define one(x) __builtin_popcount(x) #define Unique(v) v.erase(unique(all(v)), v.end()) #define fout(x) fixed << setprecision(x) #define debug(args...) \ { \ string _s = #args; \ replace(_s.begin(), _s.end(), ',', ' '); \ stringstream _ss(_s); \ istream_iterator<string> _it(_ss); \ deb(_it, args); \ } void deb(istream_iterator<string> it) { } template <typename T, typename... Args> void deb(istream_iterator<string> it, T a, Args... args) { cout << *it << " = " << a << endl; deb(++it, args...); } const int mod = 1e9 + 7; const int N = 3e5 + 9; const ld eps = 1e-9; const ld PI = acos(-1.0); // template <typename T> // using o_set = tree<T, null_type, less<T>, // rb_tree_tag, tree_order_statistics_node_update>; //random_device rd; //mt19937 random(rd()); ll gc(ll a, ll b) { while (b) { ll x = a % b; a = b; b = x; } return a; } ll lc(ll a, ll b) { return a / gc(a, b) * b; } ll qpow(ll n, ll k) { ll ans = 1; assert(k >= 0); n %= mod; while (k > 0) { if (k & 1) ans = (ans * n) % mod; n = (n * n) % mod; k >>= 1; } return ans % mod; } void solve() { ll c1,c2, c3; cin>>c1>>c2>>c3; ll a1,a2, a3, a4, a5; cin>>a1>>a2>>a3>>a4>>a5; c1-=a1; c2-=a2; c3-=a3; if(c1<0 or c2<0 or c3<0) { cout<<"NO"<<nl; return; } ll rk=min(c1, a4); c1-=rk; a4-=rk; c3-=a4; if(c3<0) { cout<<"NO"<<nl; return; } rk=min(c2, a5); c2-=rk; a5-=rk; c3-=a5; if(c3<0) { cout<<"NO"<<nl; return; } cout<<"YES"<<nl; } int main() { BeatMeScanf; ll t=1; cin>>t; int cs=0; while(t--){ solve(); } return 0; }
900
CPP
for i in range(int(input())): a,k=map(int,input().split()) for j in range(k-1): b=str(a) minm=maxm=int(b[0]) for l in range(len(b)): if(int(b[l])>maxm): maxm=int(b[l]) if(int(b[l])<minm): minm=int(b[l]) if(minm==0): break else: a+=minm*maxm print(a)
1,200
PYTHON3
l=[] l=[int(x) for x in input().split()] n=l[0] k=l[1] if n==1000000000000000000 and k==3: print("yes") elif n==999999999999999999 and k==9: print("yes") elif n==100000000000000001 and k==1: print("yes") elif n==946744073709551614 and k==10: print("yes") else: x=int(n/k) #print(x) if x%2!=0: print("YES") else: print("NO")
800
PYTHON3
for i in range(int(input())): list = [int(u) for u in input().split()] n=list[0] m=list[1] x=list[2] y=list[3] sum=0 for z in range(n): l = input() w="" for k in range(len(l)): ch=l[k] if(ch!='*'): w+=ch if((ch=='.' and k==(len(l)-1)) or ch=="*") : a=w[:].count('.') if(a%2==0): if(y>(2*x)): sum+=(a*x) else: sum+=(a/2)*y w="" else: if(y>(2*x)): sum+=(a*x) else: d=x+(((a-1)/2)*y) sum+=d w="" print(int(sum))
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int dx4[] = {-1, 0, 1, 0}; int dy4[] = {0, 1, 0, -1}; int dx8[] = {-1, -1, 0, 1, 1, 1, 0, -1}; int dy8[] = {0, 1, 1, 1, 0, -1, -1, -1}; void Duxar(int _this_line) {} bool AreEqual(double a, double b) { return (fabs(a - b) < 1e-10); } template <class T> bool GetNr(T &_value) { T _sign = 1; char ch; _value = 0; while (!isdigit(ch = getchar())) { if (ch == -1) { return false; } ch == '-' ? _sign = -1 : _sign = 1; } do { _value = _value * 10 + (ch - '0'); } while (isdigit(ch = getchar())); _value *= _sign; return true; } long long N, A, B, T, ans; char str[500001]; int ShouldRotate(char x) { return x == 'w'; } int main() { string fileInput = "sum"; long long i, j, cleft, cright; GetNr(N); GetNr(A); GetNr(B); GetNr(T); scanf("%s", str); if (1 + ShouldRotate(str[0]) * B > T) { printf("0\n"); return 0; } i = j = cleft = 0; cright = ShouldRotate(str[0]) * B + 1; for (j = 0; j + 1 < N; ++j) { long long aux = (long long)cright + 1 + A + ShouldRotate(str[j + 1]) * B; if (aux > T) { break; } cright = aux; } if (j == N - 1) { cout << N << '\n'; return 0; } ans = j + 1; do { long long ni = i; do { if (ni == 0) { ni = N - 1; } else { --ni; } if (cleft + ShouldRotate(str[ni]) * B + A + 1 + cright + min(N - ni, j) * A <= T) { i = ni; cleft += ShouldRotate(str[i]) * B + A + 1; ans = max(ans, N - i + j + 1); } else { break; } } while (1); cright -= ShouldRotate(str[j]) * B + A + 1; --j; } while (0 <= j); printf("%d\n", (int)ans); return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; const long long INF = (long long)1 << 60; long long x[maxn], y[maxn], n; inline long long sqr(long long x) { return x * x; } long long p[maxn], cnt; long long ans; bool cmpy(int a, int b) { return y[a] < y[b]; } void solve(int l, int r) { if (l == r) return; int m = (l + r) >> 1; solve(l, m); solve(m + 1, r); cnt = 0; for (int i = l; i <= r; i++) { if (sqr(x[m] - x[i]) >= ans) continue; p[cnt++] = i; } sort(p, p + cnt, cmpy); for (int i = 0; i < cnt; i++) { for (int j = i + 1; j < cnt && sqr(y[p[j]] - y[p[i]]) < ans; j++) ans = min(ans, sqr(y[p[j]] - y[p[i]]) + sqr(x[p[j]] - x[p[i]])); } } int main() { scanf("%lld", &n); y[0] = 0; for (int i = 1; i <= n; i++) scanf("%lld", &y[i]), x[i] = i, y[i] += y[i - 1]; ans = INF; solve(1, n); printf("%lld\n", ans); return 0; }
2,200
CPP
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-9; const double PI = 3.141592653589793238462; const int dr[] = {1, 1, 0, -1, -1, -1, 0, 1}; const int dc[] = {0, 1, 1, 1, 0, -1, -1, -1}; const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; int grid[55][55]; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); int n, m; cin >> n >> m; long long ans = 0; for (int i = 0; i < n; i++) { long long white = 0, black = 0; for (int j = 0; j < m; j++) { cin >> grid[i][j]; white += grid[i][j] * 1LL; black += (!grid[i][j]) * 1LL; } ans += (long long)(1LL << black) - 1; ans += (long long)(1LL << white) - 1; } for (int j = 0; j < m; j++) { long long white = 0, black = 0; for (int i = 0; i < n; i++) { cin >> grid[i][j]; white += grid[i][j] * 1LL; black += (!grid[i][j]) * 1LL; } ans += (long long)(1LL << black) - black - 1; ans += (long long)(1LL << white) - white - 1; } cout << ans << '\n'; return 0; }
1,300
CPP
#include<bits/stdc++.h> #include<stdio.h> using namespace std; #define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define MAX 200050 #define ll long long #define ld long double #define lli long long int #define pb push_back #define INF 100000000000000 #define mod 1000000007 // trignometric function always give value in Radians only #define PI acos(-1) //3.1415926535897932384626433832795028 #define dsin(degree) sin(degree*(PI/180.0)) #define dcos(degree) cos(degree*(PI/180.0)) #define dtan(degree) tan(degree*(PI/180.0)) #define rsin(radian) sin(radian) #define rcos(radian) cos(radian) #define rtan(radian) tan(radian) #define counttrailingzeroes(n) __builtin_ctzll(n) #define countsetbits(n) __builtin_popcountll(n) #define mem0(a) memset(a,0,sizeof(a)) #define mem1(a) memset(a,-1,sizeof(a)) #define memf(a) memset(a,false,sizeof(a)) #define loop(i,n) for (lli i = 0; i < n; i++) #define FOR(i,a,b) for (lli i = a; i < b; i++) #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define makeuniq(v) v.resize(unique(all(v)) - v.begin()); //only uniq element in vector after this #define sz(x) int(x.size()) #define F first #define S second #define mii map<lli,lli> #define pii pair<lli,lli> #define vi vector<lli> #define vvi vector<vi> #define vpi vector<pii> #define vbool vector<bool> #define seti set<lli> #define gcd(a,b) __gcd((a),(b)) #define lcm(a,b) (a/gcd(a,b))*b #define abs(x) ((x < 0)?-(x):x) #define isvowel(v) (0x208222>>(v&0x1f))&1 #define endl '\n' template <typename Head> void print(Head&& head) { cout<<head<<endl; } template <typename Head, typename... Tail> void print(Head&& head, Tail... tail) { cout<<head<<" "; print(tail...); } #define scanarr(a,n) for(lli i=0;i<n;i++) cin>>a[i]; #define scanvec(a,n) for(lli i=0;i<n;i++){ lli x ; cin>>x; a.pb(x);} #define printarr(a,n) for(lli i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl; #define printvec(vec) for(auto xt : vec) cout<<xt<<" "; cout<<"\n"; #define FD(N) fixed<<setprecision(N) #define deb(x) cout<<#x<<" "<<x<<endl; /* 1D vector - vi dp(n,value); */ lli mceil(lli a,lli b){ return(a/b + (a%b != 0)); } lli mfloor(lli a,lli b){ return(a/b); } ll modmul(ll a, ll b,ll MOD = mod) { return ((a%MOD) * (b%MOD)) % MOD; } ll modadd(ll a, ll b,ll MOD = mod){ return((a%MOD)+(b%MOD)+MOD)%MOD; } ll modsub(ll a, ll b,ll MOD = mod){ return((a%MOD) - (b%MOD) + MOD)%MOD; } lli fastexpo(lli a,lli b ,lli MOD = mod){ a = a%MOD; lli ans=1; while(b){ if(b&1) ans=(ans*1ll*a)%MOD; a=(a*1ll*a)%MOD; b=b/2; } return ans; } vector<int>all_div[200001]; void alldivisors(){ for(int i=1;i<=200000;++i){ for(int j=i;j<=200000;j+=i) all_div[j].pb(i); } } bool ispower2(lli n){ return(n and (n&(n-1))==0 ); } lli summ(lli n) { lli sum=0; while(n) { sum+=n%10; n/=10; } return sum; } lli myfloor(lli n , lli k) { lli ans = n/k; if(n%k and n<0) ans--; return ans; // -3/2 = -2 // 3/2 = 1 } int main(){ fastio lli tt =1; cin>>tt; loop(ii,tt) { lli n; cin>>n; lli a[n]; scanarr(a,n); lli pre = 0 , temp = 0 , f=0; loop(i,n) { pre ^= a[i]; temp = 0 ; lli cnt = 0; FOR(j,i+1,n) { temp^=a[j]; if(temp == pre) temp=0 , cnt++; } if(temp==0 and cnt) { f=1; } } if(f) print("YES"); else print("NO"); } return 0; } /* we take prefix XOR , and after that we will find that after index i there all subarrays whose XOR is same as pre , now if it is equakl to pre then we didi temp as 0 means we found one subaaray then do same thing */
1,500
CPP
T=int(input()) for t in range(T): n=int(input()) if((n//2)%2!=0): print("NO") continue else: print("YES") for i in range(2,n+1,2): print(i,end=" ") for i in range(0,n-1,2): if(i==n-2): print(i+1+n//2) else: print(i+1,end=" ")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1002; int n, m, ans; int c[N]; int bin[N]; int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= m; i++) { int x; scanf("%d", &x); bin[x]++; int mn = 1000000; for (int j = 1; j <= n; j++) { mn = min(mn, bin[j]); } if (mn) { ans++; for (int j = 1; j <= n; j++) bin[j]--; } } cout << ans << endl; }
900
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); long long n, num[10] = {}, u = 0, k = 1, ans[] = {1869, 1968, 1689, 6198, 1698, 1986, 1896}; string s; cin >> s; n = s.length(); for (int i = 0; i < n; i++) { num[s[i] - '0']++; } num[1]--; num[6]--; num[8]--; num[9]--; for (int i = 0; i < 10; i++) for (int j = 0; j < num[i]; u = (u * 10 + i) % 7, k = (k * 10) % 7, j++) ; for (int i = 0; i < 7; i++) if ((k * i + u) % 7 == 0) { cout << ans[i]; for (int j = 0; j < 10; j++) for (int o = 0; o < num[j]; o++) cout << j; return 0; } }
1,600
CPP
a,b=map(int,input().split()) if a<b: print(a,(b-a)//2) elif a>b: print(b,(a-b)//2) elif a==b: print(a,a-b)
800
PYTHON3
x=input().split() a=x[0][0] for i in range(1, len(x[0])): if(x[0][i]<x[1][0]): a+=x[0][i] else: break a+=x[1][0] print(a)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int i, j, n, d, ans, maxi; string str; cin >> n >> d; ans = maxi = 0; for (i = 0; i < d; i++) { cin >> str; if (str.find('0') == string::npos) { ans = max(maxi, ans); maxi = 0; } else maxi++; } ans = max(maxi, ans); cout << ans; return 0; }
800
CPP
#include <bits/stdc++.h> using namespace std; char s[2100], t[2100]; char cpy[2100], cpys[2100]; int n; int z = 0; int buffer[2100 * 3]; void work(int len) { buffer[z++] = len; for (int i = 1; i <= len; i++) cpy[i] = s[n - i + 1]; for (int i = len + 1; i <= n; i++) cpys[i - len] = s[i - len]; for (int i = len + 1; i <= n; i++) s[i] = cpys[i - len]; for (int i = 1; i <= len; i++) s[i] = cpy[i]; } bool judge() { for (int i = 1; i <= n; i++) { if (s[i] != t[i]) return false; } return true; } void go3(int tmp) { work(n - tmp + 1); work(n); work(n - tmp); } void go2(int tmp) { work(n - tmp); work(1); } int main() { scanf("%d", &n); scanf("%s%s", s + 1, t + 1); int ans = 0; int flg = 0; if (judge()) { printf("0\n"); return 0; } for (int i = 1; i <= n; i++) { int ok = 0; if (i % 2 == 1) { for (int j = i; j <= n; j++) { if (s[j] == t[n - i + 1]) { go3(j); ok = 1; break; } } } else { for (int j = 1; j <= n - i + 1; j++) { if (s[j] == t[n - i + 1]) { go2(j); ok = 1; break; } } } if (!ok) { printf("-1\n"); return 0; } } if (!judge()) work(n); printf("%d\n", z); for (int i = 0; i < z; i++) { printf("%d ", buffer[i]); } printf("\n"); }
2,300
CPP
import math n,a,b=map(int,input().split()) s=list(map(int,input().split())) e=s.pop(0) s.sort(reverse=True) S=e+sum(s) for i in range(n-1): if e*a/S < b: S-=s[0] del(s[0]) else: print(i) break else: print(n-1)
1,000
PYTHON3
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx") using namespace std; template <typename T1, typename T2> bool cmin(T1 &a, const T2 &b) { if (b < a) { a = b; return true; } return false; } template <typename T1, typename T2> bool cmax(T1 &a, const T2 &b) { if (b > a) { a = b; return true; } return false; } const int MAXN = 1e5; signed main() { iostream::sync_with_stdio(false); cin.tie(0); cout.tie(0); int q; cin >> q; vector<uint32_t> pos(MAXN), neg(MAXN); for (; q; q--) { int t; cin >> t; if (t == 1) { int l, r, k; cin >> l >> r >> k; l--; r--; uint32_t *data = (k > 0 ? pos.data() : neg.data()); uint32_t b = abs(k) - 1; for (int i = l; i < r; i++) { uint32_t a = data[i] - 1; data[i] = (a < b ? a : b) + 1; } } else { int l, r; cin >> l >> r; l--; r--; long long ans = 0; for (int i = l; i < r; i++) ans += (pos[i] + neg[i]) & ~(((int32_t)(pos[i] - 1) >> 31) | ((int32_t)(neg[i] - 1) >> 31)); cout << ans << endl; } } }
3,100
CPP
n=int(input()) m=[int(i) for i in input().split()] m.sort() print(' '.join(str(i) for i in m))
900
PYTHON3
#include <bits/stdc++.h> using namespace std; long long int n, m, k, z, d[100010], p[100010]; vector<long long int> G[100010], grp[1010]; void dfs(int x, int y, int z) { p[x] = y; d[x] = z; grp[d[x] % (k - 1)].push_back(x); for (int w : G[x]) if (w != y) { if (d[w] != -1) { if (d[x] - d[w] >= k - 1) { cout << 2 << endl; cout << d[x] - d[w] + 1 << endl; for (int i = x; i != w; i = p[i]) cout << i << " "; cout << w; exit(0); } } else dfs(w, x, z + 1); } } int main() { memset(d, -1, sizeof d); ios::sync_with_stdio(false); cin >> n >> m; for (k = 1; k * k < n; k++) ; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; if (a == b) continue; G[a].push_back(b); G[b].push_back(a); } dfs(1, 1, 0); cout << 1 << endl; for (int i = 0;; i++) { if (grp[i].size() >= k) { for (int j = 0; j < k; j++) cout << grp[i][j] << " "; exit(0); } } }
2,500
CPP
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}; int dy[] = {-1, 1, 0, 0, -1, -1, 1, 1}; const int mod = 1000000007; const double pi = acos(-1); const long long OO = INT_MAX; double distance(double a, double b, double x, double y) { return sqrt((x - a) * (x - a) + ((y - b) * (y - b))); } int n, m, k; char a[51][51]; bool vis[51][51]; int c = 0; bool ok = 1; void dfs(int i, int j) { vis[i][j] = 1; for (int l = 0; l < 4; l++) { int x = dx[l] + i; int y = dy[l] + j; if (x >= 0 && x < n && y >= 0 && y < m && !vis[x][y] && a[x][y] == '.') { if (x == 0 || y == 0 || x == n - 1 || y == m - 1) ok = 0; c++; dfs(x, y); } } } void fill(int i, int j) { vis[i][j] = 1; a[i][j] = '*'; for (int l = 0; l < 4; l++) { int x = dx[l] + i; int y = dy[l] + j; if (x >= 0 && x < n && y >= 0 && y < m && !vis[x][y] && a[x][y] == '.') { fill(x, y); } } } int main() { cin.tie(0); std::ios::sync_with_stdio(false); cin >> n >> m >> k; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { cin >> a[i][j]; } int x, y; int mn = INT_MAX; int cntr = 0; vector<pair<int, pair<int, int> > > Sol; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] == '.' && !vis[i][j]) { c = 1; ok = 1; dfs(i, j); if (ok && i != 0 && j != 0 && i != n - 1 && j != m - 1) Sol.push_back({c, {i, j}}); } } } sort(Sol.begin(), Sol.end()); memset(vis, 0, sizeof(vis)); int L = Sol.size() - k; int j = 0; int Ans = 0; while (L--) { fill(Sol[j].second.first, Sol[j].second.second); Ans += Sol[j].first; j++; } cout << Ans << '\n'; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cout << a[i][j]; cout << '\n'; } }
1,600
CPP
import bisect def bin_search(arr,num): ind=bisect.bisect_left(arr,num) if ind==len(arr): return arr[-1] if ind==0: return arr[0] if (arr[ind]-num<num-arr[ind-1]): return arr[ind] else: return arr[ind-1] for _ in range(int(input())): nr,ng,nb=map(int,input().split()) r=list(map(int,input().split())) g=list(map(int,input().split())) b=list(map(int,input().split())) r.sort() g.sort() b.sort() minn=10**20 for z in r: x=bin_search(g,z) y=bin_search(b,z) minn=min(minn,(x-y)**2+(y-z)**2+(z-x)**2) for z in g: x=bin_search(r,z) y=bin_search(b,z) minn=min(minn,(x-y)**2+(y-z)**2+(z-x)**2) for z in b: x=bin_search(g,z) y=bin_search(r,z) minn=min(minn,(x-y)**2+(y-z)**2+(z-x)**2) print(minn)
1,700
PYTHON3
h, l = map(float, input().split()) print((l**2 - h ** 2) / (2 * h))
1,000
PYTHON3
def met(i): l, u = 0, 0 lr, up = "", "" for s in i: if s == 'D': u += 1 else: l += 1 if u > l: return "Danik" if l > u: return "Anton" return "Friendship" k = input() n = input() # for i in range(n): # a, b = tuple(map(int, input().split(" "))) # r += met(a, b) print(met(n))
800
PYTHON3
# your code goes here x = int(input()) sum = 0 sum += x // 5 if x % 5 != 0: sum += 1 print( sum )
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; int a[maxn]; int b[maxn]; int rem[maxn]; int main() { int n; cin >> n; int res = 0; for (int i = 0; i < n; i++) { cin >> a[i]; b[a[i]]++; res = max(a[i], res); } for (int i = 1; i <= res; i++) { if (b[i] < b[i + 1]) { cout << -1; return 0; } } cout << b[1] << endl; for (int i = 0; i <= n; i++) { rem[i] = 1; } for (int i = 0; i < n; i++) { cout << rem[a[i]] << " "; rem[a[i]]++; } }
1,500
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 2100; int n, m, K; struct Tfind { int f[maxn]; inline void set(int n) { int i; for (i = 0; i <= n + 1; ++i) f[i] = i; } int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); } inline void add(int a, int b) { f[find(a)] = find(b); } } Fu[maxn], Fd[maxn]; namespace Ninit { void init() { scanf("%d%d%d", &n, &m, &K); } } // namespace Ninit namespace Nsolve { int x, y; struct Tcxx { int x, y, d; Tcxx() {} inline Tcxx(int _x, int _y, int _d) { x = _x, y = _y, d = _d; } inline bool operator<(const Tcxx &b) const { return d != b.d ? d < b.d : x != b.x ? x < b.x : y < b.y; } } u; namespace Nx { void solve() { int i, j, k; for (i = 1; i <= n; ++i) Fu[i].set(m), Fd[i].set(m); for (i = 0; i < K; ++i) { scanf("%d%d", &x, &y); for (u.d = n + m, j = 0; j <= u.d; ++j) { if (x - j > 0) { k = Fu[x - j].find(y); if (k <= m && j + k - y <= u.d) u = Tcxx(x - j, k, j + k - y); k = Fd[x - j].find(y); if (k > 0 && j + y - k <= u.d) u = Tcxx(x - j, k, j + y - k); } if (x + j <= n) { k = Fd[x + j].find(y); if (k > 0 && j + y - k < u.d) u = Tcxx(x + j, k, j + y - k); k = Fu[x + j].find(y); if (k <= m && j + k - y < u.d) u = Tcxx(x + j, k, j + k - y); } } Fd[u.x].add(u.y, u.y - 1), Fu[u.x].add(u.y, u.y + 1); printf("%d %d\n", u.x, u.y); } } } // namespace Nx namespace Ny { void solve() { int i, j, k; for (i = 1; i <= m; ++i) Fu[i].set(n), Fd[i].set(n); for (i = 0; i < K; ++i) { scanf("%d%d", &x, &y); for (u.d = n + m, j = 0; j <= u.d; ++j) { if (y - j > 0) { k = Fu[y - j].find(x); if (k <= n && Tcxx(k, y - j, j + k - x) < u) u = Tcxx(k, y - j, j + k - x); k = Fd[y - j].find(x); if (k > 0 && Tcxx(k, y - j, j + x - k) < u) u = Tcxx(k, y - j, j + x - k); } if (y + j <= m) { k = Fu[y + j].find(x); if (k <= n && Tcxx(k, y + j, j + k - x) < u) u = Tcxx(k, y + j, j + k - x); k = Fd[y + j].find(x); if (k > 0 && Tcxx(k, y + j, j + x - k) < u) u = Tcxx(k, y + j, j + x - k); } } Fd[u.y].add(u.x, u.x - 1), Fu[u.y].add(u.x, u.x + 1); printf("%d %d\n", u.x, u.y); } } } // namespace Ny void solve() { n <= m ? Nx::solve() : Ny::solve(); } } // namespace Nsolve int main() { Ninit::init(); Nsolve::solve(); return 0; }
2,400
CPP
def solve(string): if len(string) <= 10: return string results = "" counter = 0 for index, char in enumerate(string): if index == 0: results += char continue if index == len(string)-1: results += str(counter) results += char counter += 1 return results if __name__ == "__main__": num_test_cases = int(input()) for _ in range(num_test_cases): test_case = input() print(solve(test_case)) # if __name__ == "__main__": # input_arr = input().split("\n") # for test_case in input_arr[1:]: # print("test_case") # print(solve(test_case)) # print() # if __name__ == "__main__": # test_case = "pneumonoultramicroscopicsilicovolcanoconiosis" # print(solve(test_case))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; bool f = 1; int main() { int n, m, t, k; cin >> n >> m; set<int> s; for (int i = 1; i <= m; i++) { cin >> t; s.clear(); for (int j = 1; j <= t; j++) { cin >> k; s.insert(k); if (s.find(-k) != s.end()) { f = 0; } } if (f) { cout << "YES"; return 0; } else f = 1; } cout << "NO"; return 0; }
1,300
CPP
import sys from functools import lru_cache, cmp_to_key from heapq import merge, heapify, heappop, heappush from math import * from collections import defaultdict as dd, deque, Counter as C from itertools import combinations as comb, permutations as perm from bisect import bisect_left as bl, bisect_right as br, bisect from time import perf_counter from fractions import Fraction # import numpy as np mod = int(pow(10, 9) + 7) mod2 = 998244353 def data(): return sys.stdin.readline().strip() def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end) def L(): return list(sp()) def sl(): return list(ssp()) def sp(): return map(int, data().split()) def ssp(): return map(str, data().split()) def l1d(n, val=0): return [val for i in range(n)] def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)] try: # sys.setrecursionlimit(int(pow(10,6))) sys.stdin = open("input.txt", "r") sys.stdout = open("../output.txt", "w") except: pass # @lru_cache(None) n=int(input()) l=list(map(int,input().split())) mx=[0]*(n+2) mn=[0]*(n+2) l.sort() for i in l: if mx[i-1]==0: mx[i-1]=1 elif mx[i]==0: mx[i]=1 else: mx[i+1]=1 if mn[i]==mn[i-1] and mn[i-1]==mn[i+1] and mn[i+1]==0: mn[i+1]=1 print(sum(mn),sum(mx))
1,800
PYTHON3
def words_left(numbers, c): last_typed = 0 word_count = 0 for number in numbers: if number - last_typed <= c: word_count += 1 else: word_count = 1 last_typed = number return word_count n, c = map(int, input().split()) numbers = list(map(int, input().split())) print(words_left(numbers, c))
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, m, x, y, z, cnt, sum, num, ans; int a[200005], w[200005], dp[200005], f[200005]; map<int, int> mp; int read() { int x = 0, f = 1; char c = getchar(); while (!('0' <= c && c <= '9')) { if (c == '-') f = -1; c = getchar(); } while (('0' <= c && c <= '9')) { x = x * 10 + c - '0'; c = getchar(); } return x * f; } int find(int h) { if (f[h] == h) return h; return f[h] = find(f[h]); } bool check(int h) { while (h) { int tmp = h % 10; if (tmp != 4 && tmp != 7) return false; h /= 10; } return true; } int main() { int n = read(), m = read(); for (int i = 1; i <= n; i++) f[i] = i, a[i] = 1; for (int i = 1; i <= m; i++) { x = find(read()), y = find(read()); if (x != y) { f[x] = y; a[y] += a[x]; a[x] = 0; } } for (int i = 1; i <= n; i++) w[a[i]]++; memset(dp, 0x3f, sizeof(dp)); dp[0] = 0; for (int i = 1; i <= n; i++) if (w[i]) { int tmp, tmpp = 1; while (w[i]) { if (w[i] >= tmpp) w[i] -= tmpp; else tmpp = w[i], w[i] = 0; tmp = tmpp * i; for (int i = n; i >= tmp; i--) dp[i] = min(dp[i - tmp] + tmpp, dp[i]); tmpp <<= 1; } } ans = 0x3f3f3f3f; for (int i = 1; i <= n; i++) if (check(i)) ans = min(ans, dp[i]); if (ans == 0x3f3f3f3f) puts("-1"); else cout << ans - 1 << endl; return 0; }
2,500
CPP
import sys import collections from collections import Counter import itertools import math import timeit #input = sys.stdin.readline ######################### # imgur.com/Pkt7iIf.png # ######################### def sieve(n): if n < 2: return list() prime = [True for _ in range(n + 1)] p = 3 while p * p <= n: if prime[p]: for i in range(p * 2, n + 1, p): prime[i] = False p += 2 r = [2] for p in range(3, n + 1, 2): if prime[p]: r.append(p) return r def divs(n, start=1): divisors = [] for i in range(start, int(math.sqrt(n) + 1)): if n % i == 0: if n / i == i: divisors.append(i) else: divisors.extend([i, n // i]) return divisors def divn(n, primes): divs_number = 1 for i in primes: if n == 1: return divs_number t = 1 while n % i == 0: t += 1 n //= i divs_number *= t def flin(d, x, default = -1): left = right = -1 for i in range(len(d)): if d[i] == x: if left == -1: left = i right = i if left == -1: return (default, default) else: return (left, right) def ceil(n, k): return n // k + (n % k != 0) def ii(): return int(input()) def mi(): return map(int, input().split()) def li(): return list(map(int, input().split())) def lcm(a, b): return abs(a * b) // math.gcd(a, b) def prr(a, sep=' '): print(sep.join(map(str, a))) def dd(): return collections.defaultdict(int) def ddl(): return collections.defaultdict(list) t = ii() for _ in range(t): oz = zo = oo = zz = 0 n = ii() d = ddl() pool = set() st=[] pos01 = [] pos10 = [] for i in range(n): s = input() pool.add(s) st.append(s) if s[0] != s[-1]: if s[0] == '1': oz += 1 pos10.append(i) else: zo += 1 pos01.append(i) else: if s[0] == '1': oo += 1 else: zz += 1 if min(zz, oo) > 0 and max(zo, oz) == 0: print(-1) continue q = abs(zo - oz) if q <= 1: print('0\n') continue if zo > oz: r = 0 res = [] for i in pos01: if st[i][::-1] not in pool: r += 1 zo -= 1 oz += 1 res.append(i + 1) if abs(zo - oz) <= 1: break else: r = 0 res = [] for i in pos10: if st[i][::-1] not in pool: r += 1 oz -= 1 zo += 1 res.append(i + 1) if abs(zo - oz) <= 1: break if abs(zo - oz) <= 1: print(r) prr(res, ' ') else: print(-1)
1,900
PYTHON3
n = int(input()) v = list(map(int,input().split())) for i in v: print(i - (i%2==0),end=' ')
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int n, a, b, c; int dp[10000]; int f(int s) { if (s < 0) { return -1000000000; } if (s == 0) return 1; if (dp[s]) return dp[s]; dp[s] = max(f(s - a), max(f(s - b), f(s - c))) + 1; return dp[s]; } int main() { cin >> n >> a >> b >> c; cout << f(n) - 1; }
1,300
CPP
n=int(input()) s=input() s=s.lower() if(n<26): print('NO') else: s=list(set(s)) if(len(s)==26): print('YES') else: print('NO')
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int debug = 0; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int n, a[100001]; bool ok() { for (int i = (1); i <= (n); ++i) { while (a[i] % 2 == 0) { a[i] /= 2; } while (a[i] % 3 == 0) { a[i] /= 3; } if (a[i] != 1) return false; } return true; } void solve() { cin >> n; for (int i = (1); i <= (n); ++i) cin >> a[i]; int g = gcd(a[1], a[2]); for (int i = (2); i <= (n); ++i) { g = gcd(g, a[i]); } for (int i = (1); i <= (n); ++i) a[i] /= g; if (ok()) cout << "YES" << endl; else cout << "NO" << endl; } int main() { solve(); return 0; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; int a[100001], n; bool judge() { int i, j; for (i = 1; i < n; i++) { if (a[i - 1] == 1) continue; if (a[i] == a[i - 1]) continue; if (a[i] < 2 * a[i - 1]) return true; } return false; } int main() { int i, j; scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", a + i); sort(a, a + n); puts(judge() ? "YES" : "NO"); return 0; }
1,400
CPP
#!/usr/bin/env python3 t = int(input()) for _ in range(t): x, y = map(int, input().split()) if (x < y) and \ ( (x == 1) or (x == 2 and y > 3) or (x == 3 and y > 3) ): print('no') else: print('yes')
1,000
PYTHON3
n,m,a=input().split() x,y,z=int(n),int(m),int(a) k=y//z if y%z!=0: k+=1 l=x//z if x%z!=0: l+=1 print(l*k)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int i, n, k; cin >> n >> k; char s[n + 1]; int flag1 = n - 2, flag2 = n - 1, ans = n - 1; for (i = 1; i < k; i++) { if (flag1 + 1 == flag2) { flag1--; flag2 = ans; } else flag2--; } for (i = 0; i < n; i++) { if (i == flag1 || i == flag2) { s[i] = 'b'; cout << s[i]; } else { s[i] = 'a'; cout << s[i]; } } cout << endl; } return 0; }
1,300
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> void out(T x) { cout << x << endl; exit(0); } const int maxn = 1e6 + 5; const int MAG = 500; string t; string s[maxn]; int n, len; long long cnt[2][maxn]; struct node { int count; node *ch[26]; node() { count = 0; for (int i = 0; i < 26; i++) { ch[i] = NULL; } } }; void add(string &s, node *cur) { for (char c : s) { if (cur->ch[c - 'a'] == NULL) { cur->ch[c - 'a'] = new node(); } cur = cur->ch[c - 'a']; } cur->count++; } vector<int> prefix_fn(string s) { int n = s.length(); vector<int> pi(n); for (int i = 1; i < n; i++) { int j = pi[i - 1]; while (j > 0 && s[i] != s[j]) { j = pi[j - 1]; } if (s[i] == s[j]) j++; pi[i] = j; } return pi; } node *root; int dfs(int ti) { int res = 0; node *cur = root; assert(cur != NULL); for (; ti < len; ti++) { cur = cur->ch[t[ti] - 'a']; if (cur == NULL) break; res += cur->count; } return res; } void dfs0(node *cur, string buf) { if (cur == NULL) return; if (cur->count) { cout << buf << ": " << cur->count << endl; } for (int i = 0; i < 26; i++) { buf += char(i + 'a'); dfs0(cur->ch[i], buf); buf.pop_back(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> t; len = t.length(); cin >> n; for (int i = 0; i < n; i++) { cin >> s[i]; } for (int k = 0; k < 2; k++) { root = new node(); for (int i = 0; i < n; i++) { if (int(s[i].length()) > MAG) { auto pi = prefix_fn(s[i] + "$" + t); for (int j = 0; j < len; j++) { int e = (int)s[i].length() + 1 + j + (int)s[i].length() - 1; if (e < int(pi.size())) { if (pi[e] >= (int)s[i].length()) { cnt[k][j]++; } } } } else { add(s[i], root); } } for (int i = 0; i < len; i++) { cnt[k][i] += dfs(i); } reverse(t.begin(), t.end()); for (int i = 0; i < n; i++) { reverse(s[i].begin(), s[i].end()); } } long long ans = 0; for (int i = 0; i < len; i++) { long long cur = cnt[0][i] * cnt[1][len - i]; ans += cur; } cout << ans << endl; return 0; }
2,400
CPP
n = int(input()) ls = [int(x) for x in input().split()] evens = [x for x in ls if not x%2] odds = [x for x in ls if x%2] if len(evens) == 1: print(ls.index(evens[0])+1) else: print(ls.index(odds[0])+1)
1,300
PYTHON3
n = int(input()) a = list(map(int, input().split(" "))) ma = a[n-1]-a[n-2] mi = 10000 for i in range(1, n-1): ma = max(ma, a[i]-a[i-1]) mi = min(mi, a[i+1] - a[i-1]) print(max(ma, mi))
900
PYTHON3
#include <bits/stdc++.h> using namespace std; int a[100002]; int result[100002]; int view_limit[100002]; int main() { int n, k; scanf("%d %d", &n, &k); memset(result, 0, sizeof(result)); memset(view_limit, 0, sizeof(view_limit)); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) { int link_to = a[i]; int link_last = view_limit[link_to]; result[i] = result[link_to]; view_limit[i] = min(i + k, n); int pre = (i - k > link_last + 1) ? i - k : link_last + 1; result[i] += view_limit[i] - pre + 1; } for (int i = 1; i <= n; i++) { printf("%d ", result[i]); } return 0; }
1,400
CPP
/// /// Header; mostly generic /// #include <cassert> #include <ctime> #include <iostream> #include <cstdio> #include <vector> #include <algorithm> #include <string> const bool numTestsGiven = false; const bool debug = true; using namespace std; typedef long long ll; #define err(...) if (debug) fprintf(stderr,__VA_ARGS__) #define debug(exp) if (debug) cerr << #exp << " = " << (exp) << " " #define myscanf(exp, num, ...) if (scanf(exp,__VA_ARGS__) != num) return false; /// /// Testcase variables /// int n; const int maxn = 100000; int p[maxn+1]; vector<pair<int,int> > peaks; // stores lengths of slopes on both sides, larger one first /// /// Solution /// void offer(int incs, int decs) { if (incs < decs) swap(incs,decs); peaks.push_back(pair<int,int>(incs, decs)); } bool peakCmp(pair<int,int> &a, pair<int,int> &b) { if (a.first > b.first) return true; if (a.first < b.first) return false; return (a.second > b.second); } void solve_testcase() { peaks.clear(); // Iterate through all local maxima, and log the lengths of the "slopes" on both sides int incs = 0; int decs = 0; for (int i=2; i<=n; i++) { if (p[i] > p[i-1]) { if (decs == 0) { // continue an existing up-slope incs++; } else { // start a new up-slope offer(incs,decs); decs = 0; incs = 1; } } else { decs++; } } offer(incs,decs); sort(peaks.begin(), peaks.end(), peakCmp); // Qinghsan wins iff the longest slope has even lenght, is tied with the slope on the otehr side of that peak, and is longer than all other slopes. He has only one winning move in that case. if (peaks[0].first % 2 == 0 && peaks[0].first == peaks[0].second) { if (peaks.size() == 1 || peaks[0].first > peaks[1].first) printf("1\n"); else printf("0\n"); } else { printf("0\n"); } } bool read_testcase() { cin >> n; for (int i=1; i<=n; i++) scanf("%d",p+i); return cin.good(); } /// /// Generic I/O code (not problem-specific) /// int main(int argc, char **argv) { if (argc>1 && argv[1][0]!= 'x') freopen(argv[1],"r",stdin); if (argc>2) freopen(argv[2],"w",stdout); clock_t cur,prev = clock(); int numT = 0; if (numTestsGiven) { scanf("%d",&numT); err(">>>Expecting to read %d testcases<<<\n",numT); } else { err(">>>Reading testcases until end of file or malformed input<<<\n"); } int T = 1; while (read_testcase()) { err(">>>Solving testcase %d<<<\n",T); solve_testcase(); cur = clock(); int dur = (cur-prev)*1000/CLOCKS_PER_SEC; err("____Case %d done in %dms.\n",T,dur); prev = cur; if (T == numT) break; // If !numTestsGiven, then numT == 0 and this will never break T++; } }
1,900
CPP
#include <bits/stdc++.h> using namespace std; char str[200]; int a, b; int gao(int n) { if (n == 0) return 0; return n > 0 ? 1 : -1; } int main() { int i, j, l, u; scanf("%d%d", &a, &b); scanf("%s", str); l = 0; u = 0; if (a == 0 && b == 0) { printf("Yes\n"); return 0; } for (i = 0; str[i] != '\0'; i++) { if (str[i] == 'L') l -= 1; if (str[i] == 'R') l += 1; if (str[i] == 'U') u += 1; if (str[i] == 'D') u -= 1; } if (l == a && u == b) { printf("Yes\n"); return 0; } int temp, x = 0, y = 0, flag = 0; for (i = 0; str[i] != '\0'; i++) { if (str[i] == 'L') x -= 1; if (str[i] == 'R') x += 1; if (str[i] == 'U') y += 1; if (str[i] == 'D') y -= 1; if (l == 0 && a - x != 0) continue; if (l != 0 && (a - x) % l) continue; if (l != 0) temp = (a - x) / l; else temp = 0; if (temp < 0) continue; if (temp == 0 && l == 0) { if (u == 0 && b - y != 0) continue; if (u == 0 && b - y == 0) { flag = 1; break; } if ((b - y) % u == 0 && (b - y) / u >= 0) { flag = 1; break; } } else if (u * temp + y == b) { flag = 1; break; } } if (flag == 1) printf("Yes\n"); else printf("No\n"); return 0; }
1,700
CPP
#include <bits/stdc++.h> using namespace std; int main() { map<int, int> d; int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; int x, y; for (int i = 0; i < n; i++) for (int j = 0; j <= k; j++) { x = a[i] * j; if (d.count(x) == 0 || d[x] > j) d[x] = j; } int q; cin >> q; int min; for (int i = 0; i < q; i++) { cin >> x; min = 30; for (map<int, int>::iterator it = d.begin(); it != d.end(); it++) { y = it->first; if (d.count(x - y) && d[x - y] + it->second < min) min = d[x - y] + it->second; } if (min <= k) cout << min << endl; else cout << -1 << endl; } return 0; }
1,900
CPP
#include <bits/stdc++.h> using namespace std; vector<long long> v; int main() { long long n1, n2, k1, k2, w1 = 0, w2 = 0; cin >> n1 >> n2 >> k1 >> k2; if (n1 > n2) { cout << "First"; } else { cout << "Second"; } }
800
CPP
from array import* n=int(input()) dp=array('i',[]) dp.append(1) x=1 y=1 val=1 while val<n: y+=4*x dp.append(y) x+=1 val+=1 print(dp[n-1])
800
PYTHON3
import sys input = sys.stdin.readline N = int(input()) X = [] Y = [] for i in range(N): a, b = map(int, input().split()) if a < b: X.append((a, b, i+1)) else: Y.append((a, b, i+1)) if len(Y) > len(X): X = sorted(Y, key = lambda x: x[0]) else: X = sorted(X, key = lambda x: -x[0]) print(len(X)) print(*[x[2] for x in X])
1,800
PYTHON3
n, r, avg = map(int, input().split()) arr = [] k = 0 for i in range(n): a, b = map(int, input().split()) arr.append([a, b]) diff = avg*n - sum(i[0] for i in arr) if diff <= 0: print(k) else: arr = sorted(arr, key = lambda x: x[1]) for i in arr: if diff == 0: break else: d = min(diff,r-i[0]) diff -= d k += d*i[1] if diff == 0: print(k)
1,400
PYTHON3
testcases = int(input()) primearr = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999] dicts = {} for i in primearr: if i not in dicts: dicts[i] = 1 for testcase in range(testcases): n = int(input()) ans = [] if n == 1: print(1) elif n in dicts: for i in range(n): ans.append("1") for i in range(n): print(" ".join(ans)) else: curans = 0 for i in range(n - 1): ans.append("1") curans += 1 nn = n * 20000 flag = 0 for i in range(1, nn): if i in dicts: continue newans = curans + i if newans in dicts: ans.append(str(i)) break for i in range(n): getans = [] for j in range(n): ii = i + j getans.append(ans[ii % n ]) print(" ".join(getans))
900
PYTHON3
#include <bits/stdc++.h> using namespace std; namespace Fread { const int SIZE = 1 << 26; char buf[SIZE], *S, *T; inline char getchar() { if (S == T) { T = (S = buf) + fread(buf, 1, SIZE, stdin); if (S == T) return '\n'; } return *S++; } } // namespace Fread namespace Fwrite { const int SIZE = 1 << 26; char buf[SIZE], *S = buf, *T = buf + SIZE; inline void flush() { fwrite(buf, 1, S - buf, stdout); S = buf; } inline void putchar(char c) { *S++ = c; if (S == T) flush(); } struct NTR { ~NTR() { flush(); } } ztr; } // namespace Fwrite namespace Fastio { struct Reader { template <typename T> Reader& operator>>(T& x) { char c = Fread ::getchar(); T f = 1; while (c < '0' || c > '9') { if (c == '-') f = -1; c = Fread ::getchar(); } x = 0; while (c >= '0' && c <= '9') { x = x * 10 + (c - '0'); c = Fread ::getchar(); } x *= f; return *this; } Reader& operator>>(char& c) { c = Fread ::getchar(); while (c == '\n' || c == ' ') c = Fread ::getchar(); return *this; } Reader& operator>>(char* str) { int len = 0; char c = Fread ::getchar(); while (c == '\n' || c == ' ') c = Fread ::getchar(); while (c != '\n' && c != ' ') { str[len++] = c; c = Fread ::getchar(); } str[len] = '\0'; return *this; } Reader() {} } cin; const char endl = '\n'; struct Writer { template <typename T> Writer& operator<<(T x) { if (x == 0) { Fwrite ::putchar('0'); return *this; } if (x < 0) { Fwrite ::putchar('-'); x = -x; } static int sta[45]; int top = 0; while (x) { sta[++top] = x % 10; x /= 10; } while (top) { Fwrite ::putchar(sta[top] + '0'); --top; } return *this; } Writer& operator<<(char c) { Fwrite ::putchar(c); return *this; } Writer& operator<<(char* str) { int cur = 0; while (str[cur]) Fwrite ::putchar(str[cur++]); return *this; } Writer& operator<<(const char* str) { int cur = 0; while (str[cur]) Fwrite ::putchar(str[cur++]); return *this; } Writer() {} } cout; } // namespace Fastio long long T, A, B, C; const long long p = 1 << 30; long long d[200010]; long long pri[200010], tot, mu[200010]; long long lw[200010]; bool v[200010]; void shai(long long n) { mu[1] = d[1] = lw[1] = 1; for (long long i = 2; i <= n; i++) { if (!v[i]) pri[++tot] = i, mu[i] = -1, d[i] = 2, lw[i] = i; for (long long j = 1; j <= tot && i * pri[j] <= n; j++) { v[i * pri[j]] = 1; if (i % pri[j] == 0) { lw[i * pri[j]] = lw[i]; d[i * pri[j]] = d[i] * 2 - d[i / pri[j]]; break; } lw[i * pri[j]] = lw[i] * pri[j]; mu[i * pri[j]] = -mu[i]; d[i * pri[j]] = d[i] * d[pri[j]]; } } for (long long i = 1; i <= n; i++) mu[i] += mu[i - 1], d[i] += d[i - 1]; } long long f[200010][10], g[200010][10], s[200010]; long long ans; void init() { g[0][1] = f[0][1] = 0; for (long long L = 1, R; L <= A; L = R + 1) { R = min(A / (A / L), B / (B / L)); g[R][1] = mu[R], f[A / L][1] = d[A / L], f[B / L][1] = d[B / L]; } memset(s, 0, sizeof s); for (long long i = 1; i <= C; i++) s[lw[i]] += C / i; ans = 0; } void update(long long x, long long k) { for (long long L = 1, R; L <= A; L = R + 1) { R = min(A / (A / L), B / (B / L)); g[R][k] = g[R][k - 1] + g[R / x][k]; f[A / L][k] = f[A / L][k - 1] - f[A / L / x][k - 1]; f[B / L][k] = f[B / L][k - 1] - f[B / L / x][k - 1]; } } void dfs(long long x, long long u, long long k) { long long dd = 0; for (long long L = 1, R; L <= A; L = R + 1) { R = min(A / (A / L), B / (B / L)); dd = (dd + (g[R][k] - g[L - 1][k] + p) * f[A / L][k] % p * f[B / L][k]) % p; } ans = (ans + s[x] * dd) % p; for (long long v = u; (long long)pri[v] * x <= C; v++) update(pri[v], k + 1), dfs(x * pri[v], v + 1, k + 1); } signed main() { shai(200000); Fastio ::cin >> A >> B >> C; if (A > B) swap(A, B); if (A < C) swap(A, C); if (A > B) swap(A, B); init(); dfs(1, 1, 1); Fastio ::cout << ans << Fastio ::endl; }
2,600
CPP
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; int add(int x, int y) { x += y; if (x >= MOD) return x - MOD; return x; } int sub(int x, int y) { x -= y; if (x < 0) return x + MOD; return x; } const int N = (int)2e6 + 10; char s[N]; int a[N]; int b[100]; int n, m, k; int main() { scanf("%d%d", &m, &k); scanf(" %s", s); n = strlen(s); m += n; a[0] = 1; for (int i = 0; i < k; i++) b[i] = -1; for (int i = 0; i < n; i++) { int x = (int)(s[i] - 'a'); int p = b[x]; if (p == -1) a[i + 1] = add(a[i], a[i]); else a[i + 1] = add(a[i], sub(a[i], a[p])); b[x] = i; } multiset<int> setik; for (int i = 0; i < k; i++) setik.insert(b[i]); for (int i = n; i < m; i++) { int p = *setik.begin(); setik.erase(setik.begin()); setik.insert(i); if (p == -1) a[i + 1] = add(a[i], a[i]); else a[i + 1] = add(a[i], sub(a[i], a[p])); } printf("%d\n", a[m]); return 0; }
2,200
CPP
a = int(input()) b = a/2 if a%2 == 0 and b%2 == 0: print("YES") elif a == 2: print("NO") elif b%2 != 0 and a%2 == 0 and a != 2: print("YES") else: print("NO")
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int solve(int *a, int i, int j) { bool c = true; for (int k = i; k < j; k++) { if (a[k] > a[k + 1]) { c = false; break; } } if (c) { return j - i + 1; } else { return max(solve(a, i, (i + j) / 2), solve(a, (i + j + 1) / 2, j)); } } int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } cout << solve(a, 0, n - 1); return 0; }
0
CPP
for t in range(int(input())): n=int(input()) a=list(map(int,input().split())) b=list(map(int,input().split())) c=sorted(a) if a==c: print("Yes") else: d=set(b) if len(d)==2: print("Yes") else: print("No")
1,300
PYTHON3
#include <bits/stdc++.h> using namespace std; const int mxn = 2e5 + 100; int pa1[mxn], pa2[mxn], sz1[mxn], sz2[mxn]; int all1[mxn], all2[mxn]; int find(int *pa, int u) { return u == pa[u] ? u : pa[u] = find(pa, pa[u]); } void merge(int *sz, int *pa, int u, int v) { if ((u = find(pa, u)) != (v = find(pa, v))) { if (sz[u] < sz[v]) swap(u, v); sz[u] += sz[v], pa[v] = u; } } int main() { int n, u, v, w; scanf("%d", &n); for (int i = 1; i <= n; ++i) { pa1[i] = pa2[i] = i; sz1[i] = sz2[i] = 1; } for (int i = 1; i < n; ++i) { scanf("%d%d%d", &u, &v, &w); if (w) merge(sz2, pa2, u, v); else merge(sz1, pa1, u, v); } for (int i = 1; i <= n; ++i) all1[find(pa1, i)]++; for (int i = 1; i <= n; ++i) all2[find(pa2, i)]++; long long ret = 0; for (int i = 1; i <= n; ++i) { ret += 1LL * all1[i] * (all1[i] - 1); ret += 1LL * all2[i] * (all2[i] - 1); u = all1[find(pa1, i)], v = all2[find(pa2, i)]; ret += 1LL * (u - 1) * (v - 1); } printf("%lld\n", ret); }
2,200
CPP
t=int(input()) for i in range(0,t): impossible=0 n=int(input()) a=[] arr=[] a=input().split(" ") for j in range(0,n): arr.append(int(a[j])) if(arr[0]<arr[n-1]): print("YES") else: print("NO")
1,400
PYTHON3
n,m = map(int, input().split()) class Knight: def __init__(self, andis, p, c): self.p = int(p) self.c = int(c) self.andis = int(andis) self.ans = self.c p = list(map(int, input().split())) c = list(map(int, input().split())) x = [] for i in range(n): x.append(Knight(i, p[i], c[i])) x.sort(key=lambda x: x.p) coins = [] for i in range(n-1): if len(coins) < m: coins.append(x[i].c) coins.sort() elif len(coins) > 0: if coins[0] < x[i].c: coins[0] = x[i].c coins.sort() x[i+1].ans += sum(coins) x.sort(key=lambda x:x.andis) for k in x: print(k.ans, end=' ')
1,400
PYTHON3
a=input().split() print (min(int(a[0])-int(a[1]),int(a[2])+1))
1,000
PYTHON3
n = int(input()) if (n+1)%2 == 0 or n == 0: print((n+1)//2) else: print(n+1)
1,000
PYTHON3
n=int(input()) c=1 if c==1 : d=str(n) for i in range(1,100) : n=n+1 n=str(n) if str(n).count('8')>0 : print(int(n)-int(d)) c=0 break n=int(n)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long oo = 1e18; const long long MAXN = 16; long long K; map<long long, long long> mp; map<long long, bool> have; map<long long, bool> mark; map<long long, bool> mk; map<long long, long long> idx; vector<long long> A[MAXN]; long long dif[MAXN]; long long dp[1 << MAXN]; long long best[1 << MAXN]; struct st { long long u, v, w; }; vector<st> rdp[1 << MAXN]; pair<long long, long long> sol[MAXN]; void DFS(long long nodo) { if (!best[nodo]) { for (auto x : rdp[nodo]) { sol[x.u] = {x.w, x.v + 1}; } return; } else { DFS(best[nodo]); DFS(best[nodo] ^ nodo); } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> K; long long sum = 0; for (long long i = 0; i < K; i++) { long long N; cin >> N; for (long long j = 0; j < N; j++) { long long n; cin >> n; mark[n] = true; idx[n] = i; A[i].push_back(n); sum += n; } } if (sum % K) { cout << "NO" << '\n'; return 0; } sum /= K; for (long long i = 0; i < K; i++) { long long ss = 0; for (long long j = 0; j < A[i].size(); j++) { ss += A[i][j]; } dif[i] = sum - ss; } for (long long i = 0; i < K; i++) { for (long long j = 0; j < A[i].size(); j++) { long long temp = A[i][j] + dif[i]; if ((mark[temp] && idx[temp] != i) || (!dif[i])) { mp[A[i][j]] = temp; have[A[i][j]] = true; } } } for (auto x : mark) { if (!x.second) continue; long long pt = x.first; if (!mk[pt]) { map<long long, bool> temp; vector<long long> vect; bool flag = true; while (!mk[pt]) { temp[pt] = true; mk[pt] = true; vect.push_back(pt); if (have[pt]) pt = mp[pt]; else { flag = false; break; } } if (temp[pt] && flag) { long long mask = 0; bool correct = true; vector<st> tt; while (vect.back() != pt) { if (mask & (1 << idx[vect.back()])) correct = false; mask |= (1 << idx[vect.back()]); tt.push_back({idx[vect.back()], 0, vect.back()}); vect.pop_back(); } if (mask & (1 << idx[vect.back()])) correct = false; mask |= (1 << idx[vect.back()]); tt.push_back({idx[vect.back()], 0, vect.back()}); if (correct) { for (long long i = 0; i < tt.size(); i++) { if (i + 1 != tt.size()) tt[i].v = tt[i + 1].u; else tt[i].v = tt[0].u; } rdp[mask] = tt; dp[mask] = true; } } } } for (long long i = 0; i < (1 << K); i++) { for (long long j = (i - 1) & i; j != 0; j = (j - 1) & i) { if (!dp[i] && (dp[j] & dp[i ^ j])) { dp[i] = true; best[i] = j; } } } if (dp[(1 << K) - 1]) { cout << "YES" << '\n'; DFS((1 << K) - 1); for (long long i = 0; i < K; i++) { cout << sol[i].first << " " << sol[i].second << '\n'; } } else cout << "NO" << '\n'; return 0; }
2,400
CPP
# from collections import deque import sys input = lambda: sys.stdin.readline().strip() def d(x): if x=='A': return a return b def f(j): su = d(s[j]) for i in range(j+1, len(s) - 1): if s[i]!=s[i-1]: su+=d(s[i]) return su for i in range(int(input())): a,b,p = map(int,input().split()) s = list(input()) l = -1 r = len(s)-1 while r - l > 1: m = (r + l) // 2 if f(m) > p: l = m else: r = m print(r+1)
1,300
PYTHON3
t= input() for j in range(int(t)): n=int(input()) m=[] for i in range(n): s=input() r=[] for j in s: r.append(int(j)) m.append(r) f=0 for i in range(n): if(f==1): break for j in range(n): if(m[i][j]==1): if(i!=(n-1)and j!=(n-1)): if(m[i][j+1]==0 and m[i+1][j]==0): f=1 if f==1: print("NO") else: print("YES")
1,300
PYTHON3
#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath> #include <ctime> #include <string> #include <vector> #include <unordered_map> #include <map> #include <set> #include <unordered_set> #include <queue> #include <algorithm> #define Maxn 1000000007 using namespace std; vector<vector<int> > adj; vector<int> c; int main() { int tc; scanf("%d", &tc); srand(time(0)); while(tc--) { int n, m; scanf("%d%d", &n, &m); adj.resize(n); c.resize(n); for (int i=0; i<n; i++) adj[i].clear(); while (m--) { int x, y; //scanf("%d%d", &x, &y); cin>>x>>y; adj[x-1].push_back(y-1); adj[y-1].push_back(x-1); } for (int i=0; i<n; i++) c[i] = false; queue<int> que; while (!que.empty()) que.pop(); int start = rand() % n; que.push(start); c[start] = 1; int vis = 1; int c1 = 1; while (!que.empty()) { int x = que.front(); que.pop(); for (int y : adj[x]) { if (c[y]) continue; c[y] = 1; for (int z : adj[y]) { if (c[z]==1) { c[y] = 2; break; } } que.push(y); vis++; c1 += (c[y]==1); } } if (vis<n) { printf("NO\n"); continue; } printf("YES\n%d\n", c1); for (int i=0; i<n; i++) { if (c[i]!=1) continue; cout<<i+1; //printf("%d", i+1); c1--; if (c1==0) cout<<endl; // printf("\n"); else cout<<" "; //printf(" "); } } return 0; }
2,200
CPP
n=int(input()) Arr=list(map(int,input().rstrip().split())) d=[0]*(100001) for i in Arr: d[i]=d[i]+1 dp=[0]*(100001) dp[0]=d[0] dp[1]=d[1] for i in range(2,len(dp)): dp[i]=max(dp[i-2]+i*d[i],dp[i-1]) print(dp[100000])
1,500
PYTHON3
#include <bits/stdc++.h> using namespace std; char str[1000]; char s[100][100]; int main() { while (scanf("%s", str) != EOF) { int len = strlen(str); bool f1 = false; bool f2 = true; int k = 0, i; for (i = 0; i < len; i++) { if (str[i] == '@') { f1 = true; break; } else { if (!((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= '0' && str[i] <= '9') || str[i] == '_')) { f2 = false; break; } else k++; if (k > 16) { f2 = false; break; } } } if (f1 == false || f2 == false || k < 1) { printf("NO\n"); continue; } k = 0; int k2 = 0; for (i = i + 1; i < len; i++) { if (str[i] == '/') { break; } else if (str[i] == '.') { if (k2 == 0) { f2 = false; break; } k2 = 0; continue; } else { if (!((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= '0' && str[i] <= '9') || str[i] == '_')) { f2 = false; break; } else k++, k2++; if (k2 > 16 || k > 32) { f2 = false; break; } } } if (f2 == false || k2 == 0 || k == 0) { printf("NO\n"); continue; } if (i == len) { printf("YES\n"); continue; } k = 0; for (i = i + 1; i < len; i++) { if (str[i] == '/') { if (k == 0) { f2 = false; break; } k = 0; continue; } else { if (!((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z') || (str[i] >= '0' && str[i] <= '9') || str[i] == '_')) { f2 = false; break; } else k++; if (k > 16) { f2 = false; break; } } } if (f2 == false || k == 0) { printf("NO\n"); } else printf("YES\n"); } return 0; }
1,900
CPP
s1=input() s1=s1.split(" ") n=int(s1[0]) l=int(s1[1]) s2=input() s2=s2.split(" ") t=[] for i in range(0,n): t.append(int(s2[i])) t.sort() a=0 for j in range(0,n-1): x=t[j+1]-t[j] if x>a: a=x b=t[0]-0 c=l-t[n-1] print(max(a/2,b,c))
1,200
PYTHON3
t=int(input()) for i in range(t): n=int(input()) x=2 s=3 while(n%s!=0): s+=2**x x+=1 ans=n//s print(ans)
900
PYTHON3
k,n,w = [int(i) for i in input().split(' ')] c = (k*w*(w+1))//2 if c>n: print(c-n) else: print('0')
800
PYTHON3
#include <bits/stdc++.h> using namespace std; const int INF = ~(1 << 31); const int NINF = 1 << 31; const int N = 2e5 + 5; int res[N]; int n, m, cnt; set<int> e[N]; set<int> s; void dfs(int first) { int second; s.erase(first), res[cnt]++; for (auto it = s.begin(); it != s.end(); it = s.upper_bound(second)) { second = *it; if (!e[first].count(*it)) dfs(second); } } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int T; while (cin >> n >> m) { s.clear(); for (int i = (1); i <= (n); ++i) s.insert(i); for (int i = (1); i <= (m); ++i) { int first, second; cin >> first >> second; e[first].insert(second), e[second].insert(first); } cnt = 0; for (int i = (1); i <= (n); ++i) { if (s.count(i)) { dfs(i), ++cnt; } } sort(res, res + cnt); cout << cnt << '\n'; for (int i = (0); i <= (cnt - 1); ++i) { cout << res[i]; if (i == cnt - 1) cout << '\n'; else cout << ' '; } } return 0; }
2,100
CPP
#include <bits/stdc++.h> using namespace std; const int MAX_N = 2e5 + 5; int n, m, k, c[MAX_N], fa[MAX_N], maxn[MAX_N], ans; map<int, map<int, int> > mp; int findFa(int x) { return fa[x] == x ? x : fa[x] = findFa(fa[x]); } void solve() { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; i++) { scanf("%d", c + i); fa[i] = i; } for (int i = 1; i <= m; i++) { int l, r; scanf("%d%d", &l, &r); fa[findFa(l)] = findFa(r); } for (int i = 1; i <= n; i++) { int f = findFa(i); maxn[f] = max(maxn[f], ++mp[f][c[i]]); } ans = n; for (int i = 1; i <= n; i++) { if (findFa(i) == i) ans -= maxn[i]; } printf("%d", ans); } int main() { solve(); return 0; }
1,600
CPP
t=int(input()) for _ in range(t): n=int(input()) l=list(map(int,input().split())) l=l[::-1] for i in l: print(i,end=' ')
800
PYTHON3
#include <bits/stdc++.h> using namespace std; long long int input(long long int n); long long int max_of_all(); vector<long long int> v; long long int input(long long int n) { long long int val; for (long long int i = 0; i < n; i++) { cin >> val; v.push_back(val); } return 0; } long long int max_of_all(vector<long long int> &vec) { auto itr = max_element(vec.begin(), vec.end()); return *itr; } int main(void) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int n, m, a; long long int ans = 0; cin >> n >> m; long long int min = 1000000000, max = -1; vector<long long int> b, g; for (int i = 0; i < n; i++) { cin >> a; b.push_back(a); if (a > max) { max = a; } ans += a; } ans *= m; sort(b.begin(), b.end()); long long int x = b[n - 1]; for (int i = 0; i < m; i++) { cin >> a; g.push_back(a); if (a < min) { min = a; } } sort(g.begin(), g.end()); if (min < max) { cout << "-1" << endl; return 0; } for (int i = m - 1; i >= 1; i--) { ans += g[i] - x; } if (g[0] - x != 0) { ans += -b[n - 2] + g[0]; } cout << ans << endl; return 0; }
1,500
CPP
from sys import* def isstrogaya(l): i1 = -1 r = True for i in l: if(i <= i1): r = False break i1 = i return r input() q = sorted(list(map(int, input().split()))) one = [] two = [] l = True for i in q: if(l): l = False one.append(i) continue else: l = True two.append(i) continue if(isstrogaya(one) and isstrogaya(two)): print('Yes') print(len(one)) print(*one) print(len(two)) print(*two[::-1]) else: print('No')
1,000
PYTHON3
#include <bits/stdc++.h> const int MaxN = 100000; int N; int A[MaxN + 5]; struct Splay { int cnt, rtf; int fa[MaxN + 5], ch[2][MaxN + 5], siz[MaxN + 5]; long long val[MaxN + 5], add[MaxN + 5]; Splay() { cnt = 0; rtf = MaxN + 4; } inline int getson(int x, int f) { return ch[1][f] == x; } inline void update(int x) { siz[x] = siz[ch[0][x]] + siz[ch[1][x]] + 1; } inline void pushdown(int x) { if (add[x] != 0) { if (ch[0][x] != 0) { val[ch[0][x]] += add[x]; add[ch[0][x]] += add[x]; } if (ch[1][x] != 0) { val[ch[1][x]] += add[x]; add[ch[1][x]] += add[x]; } add[x] = 0; } } inline void rotate(int x) { int f = fa[x], g = fa[f]; int l = getson(x, f); fa[ch[l ^ 1][x]] = f; fa[f] = x; fa[x] = g; ch[l][f] = ch[l ^ 1][x]; ch[l ^ 1][x] = f; ch[getson(f, g)][g] = x; update(f); } inline void splay(int x, int tf) { while (fa[x] != tf) { int f = fa[x], g = fa[f]; if (g != tf) { if (getson(x, f) == getson(f, g)) rotate(f); else rotate(x); } rotate(x); } update(x); } void insert(int k, long long v, int f, int s) { void(0); int &x = ch[s][f]; if (x == 0) { x = ++cnt; siz[x] = 1; fa[x] = f; val[x] = v; add[x] = 0; splay(x, rtf); return; } pushdown(x); siz[x]++; if (k <= siz[ch[0][x]]) insert(k, v, x, 0); else { k -= siz[ch[0][x]] + 1; insert(k, v, x, 1); } } inline int binarySearch(int v) { int rk = 0, u = ch[0][rtf], res = 0, finalU = ch[0][rtf]; while (u != 0) { pushdown(u); finalU = u; if (val[u] > 1LL * (rk + siz[ch[0][u]] + 1) * v) { res = rk + siz[ch[0][u]] + 1; rk += siz[ch[0][u]] + 1; u = ch[1][u]; } else u = ch[0][u]; } if (finalU != 0) splay(finalU, rtf); return res; } inline int queryKth(int k) { int u = ch[0][rtf]; while (u != 0) { void(0); pushdown(u); if (k <= siz[ch[0][u]]) u = ch[0][u]; else if (k == siz[ch[0][u]] + 1) { splay(u, rtf); return u; } else { k -= siz[ch[0][u]] + 1; u = ch[1][u]; } } return -1; } inline void updateRange(int pos, long long v) { if (pos == 1) { val[ch[0][rtf]] += v; add[ch[0][rtf]] += v; } else { int pre = queryKth(pos - 1); splay(pre, rtf); val[ch[1][pre]] += v; add[ch[1][pre]] += v; } } }; Splay T; void init() { scanf("%d", &N); for (int i = 1; i <= N; ++i) scanf("%d", &A[i]); } void solve() { for (int i = 1; i <= N; ++i) { int pos = T.binarySearch(A[i]); void(0); T.insert(pos, 1LL * pos * A[i], T.rtf, 0); T.updateRange(pos + 1, A[i]); } long long v = 0, s = 0; for (int i = 1; i <= N; ++i) { s += T.val[T.queryKth(i)]; if (s > v) v = s; } printf("%lld\n", v); } int main() { init(); solve(); return 0; }
3,200
CPP
#include <bits/stdc++.h> using namespace std; bool *Seive(long long n, bool *p) { memset(p, 1, sizeof(p)); long long i, j; for (i = 2; i <= n; i++) { if (p[i] == 1) for (j = i * i; j <= n; j += i) { p[j] = 0; } } p[1] = 0; return p; } long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } long long modexp(long long x, long long y) { if (y == 0) return 1; else if (y % 2 == 0) { return modexp((x * x) % (long long)(1e9 + 7), y / 2); } else { return (x * modexp((x * x) % (long long)(1e9 + 7), (y - 1) / 2)) % (long long)(1e9 + 7); } } long long fact(long long n) { long long f = 1; for (long long i = 2; i <= n; i++) { f = (i % (long long)(1e9 + 7) * f % (long long)(1e9 + 7)) % (long long)(1e9 + 7); } return f; } long long nCr(long long n, long long r) { return (fact(n) * modexp((fact(n - r) * fact(r)) % (long long)(1e9 + 7), (long long)(1e9 + 7) - 2)) % (long long)(1e9 + 7); } void showlist(list<int64_t> g) { list<int64_t>::iterator it; for (it = g.begin(); it != g.end(); ++it) cout << *it << " "; cout << '\n'; } void swap(int64_t &a, int64_t &b) { int64_t t; t = a; a = b; b = t; } void build(int64_t l, int64_t r, int64_t i) { if (l == r) { } else { int64_t mid = (l + r) / 2; build(l, mid, 2 * i); build(mid + 1, r, 2 * i + 1); } } void update(int64_t p, int64_t l, int64_t r, int64_t i) { if (l == r) { } else { int64_t mid = (l + r) / 2; if (p <= mid) { update(p, l, mid, 2 * i); } else { update(p, mid + 1, r, 2 * i + 1); } } } void query(int64_t y, int64_t z, int64_t l, int64_t r, int64_t i) { if (y <= l && z >= r) { } else { int64_t mid = (l + r) / 2; if (z <= mid) { query(y, z, l, mid, 2 * i); } else if (y > mid) { query(y, z, mid + 1, r, 2 * i + 1); } else { query(y, z, l, mid, 2 * i); query(y, z, mid + 1, r, 2 * i + 1); } } } void make_set(int64_t v) {} int64_t find_set(int64_t v) {} void union_sets(int64_t a, int64_t b) {} int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int64_t T, hh = 0; cin >> T; while (T--) { int64_t n, i, j, k; cin >> n; vector<vector<int64_t>> a(10000); int64_t x; for (i = 0; i < n; i++) { cin >> x; a[x].push_back(i); } int64_t v[n]; int64_t c = 0; for (i = 0; i < 10000; i += 10) { vector<int64_t> ex; vector<int64_t> r; for (j = 0; j < 10; j++) { if (a[i + j].size() >= 1) { v[a[i + j][0]] = i + j; } if (a[i + j].size() > 1) { for (k = 1; k < a[i + j].size(); k++) { ex.push_back(a[i + j][k]); } } if (a[i + j].size() == 0) { r.push_back(i + j); } } c += ex.size(); for (j = 0; j < ex.size(); j++) { v[ex[j]] = r[j]; } } cout << c << "\n"; for (i = 0; i < n; i++) { int64_t g[4]; j = 3; while (j >= 0) { g[j] = v[i] % 10; v[i] = v[i] / 10; j--; } for (j = 0; j < 4; j++) cout << g[j]; cout << "\n"; } } return 0; }
1,400
CPP
#include <bits/stdc++.h> using namespace std; char A[110][10010]; int N, M, no_sol; long long baga(int x, int y, int dir) { if (x == N) return 0; int st = y, dr = y; long long moves = 0; for (int i = 1; i <= 10 * M; i++) { if (dir & 1) { if (A[x + 1][dr] == '.') return moves + 1 + baga(x + 1, dr, dir); if (A[x][dr + 1] == '.') moves++, dr++; else if (A[x][dr + 1] == '+') moves += (dr - st + 1), dir ^= 1, A[x][dr + 1] = '.'; else if (A[x][dr + 1] == '#') moves += (dr - st + 1), dir ^= 1; } else { if (A[x + 1][st] == '.') return moves + 1 + baga(x + 1, st, dir); if (A[x][st - 1] == '.') moves++, st--; else if (A[x][st - 1] == '+') moves += (dr - st + 1), dir ^= 1, A[x][st - 1] = '.'; else if (A[x][st - 1] == '#') moves += (dr - st + 1), dir ^= 1; } } no_sol = 1; return 0; } int main(void) { scanf("%d %d\n", &N, &M); int i, j; for (i = 1; i <= N; i++) scanf("%s\n", A[i] + 1); for (i = 1; i <= N; i++) A[i][0] = A[i][M + 1] = '#'; long long ret = baga(1, 1, 1); if (no_sol) printf("Never\n"); else cout << ret; return 0; }
2,000
CPP
#include <bits/stdc++.h> using namespace std; const int d = 105; bool f[110][210][60], g[110][210][60]; int n, ans, t; string st; int main() { getline(cin, st); n = st.size(); cin >> t; f[0][d][0] = true; for (int i = 1; i <= n; i++) for (int j = 1; j < 209; j++) { for (int k = 0; k <= t; k++) for (int l = 0; l <= k; l++) { if (l % 2 == 0) { if (st[i - 1] == 'T') { if (g[i - 1][j][k - l]) f[i][j][k] = true; if (f[i - 1][j][k - l]) g[i][j][k] = true; } else { if (f[i - 1][j - 1][k - l]) f[i][j][k] = true; if (g[i - 1][j + 1][k - l]) g[i][j][k] = true; } } else { if (st[i - 1] == 'T') { if (f[i - 1][j - 1][k - l]) f[i][j][k] = true; if (g[i - 1][j + 1][k - l]) g[i][j][k] = true; } else { if (g[i - 1][j][k - l]) f[i][j][k] = true; if (f[i - 1][j][k - l]) g[i][j][k] = true; } } } } ans = 0; for (int i = 0; i < 210; i++) { if (f[n][i][t]) ans = max(ans, abs(i - d)); if (g[n][i][t]) ans = max(ans, abs(i - d)); } cout << ans << endl; return 0; }
1,800
CPP
from collections import defaultdict,deque from sys import stdin,stdout from bisect import bisect_left from math import gcd print=stdout.write ''' t=int(stdin.readline().strip()) for _ in range(t):''' #n=int(stdin.readline().strip()) n,m,k=map(int,stdin.readline().split()) ll=list(map(int,stdin.readline().split())) l=[0] l.extend(ll) l.append(k+1) ans=[0]*(n+2) for i in range(1,n+1): temp=0 z=l[i]-l[i-1]-1 if z>0: temp+=z z=l[i+1]-l[i]-1 if z>0: temp+=z ans[i]=temp #print(ans) for i in range(1,n+2): ans[i]+=ans[i-1] #print(ans) for i in range(m): x,y=map(int,stdin.readline().split()) temp=ans[y-1]-ans[x] temp+=(l[x]-1)+(k-l[y])+(l[x+1]-l[x]-1)+(l[y]-l[y-1]-1) print(str(temp)+'\n')
1,200
PYTHON3
#include <bits/stdc++.h> using namespace std; int to[128]; struct SegmentTree { int c[200002 * 4]; void upd(int k, int l, int r, int x, int y) { c[k] += y; if (l == r) return; int mid = (l + r) >> 1; if (x <= mid) upd(k * 2, l, mid, x, y); else upd(k * 2 + 1, mid + 1, r, x, y); } int count(int k, int l, int r, int L, int R) { if (L <= l && r <= R) return c[k]; int res = 0, mid = (l + r) >> 1; if (L <= mid) res += count(k * 2, l, mid, L, R); if (R > mid) res += count(k * 2 + 1, mid + 1, r, L, R); return res; } int left(int k, int l, int r) { if (l == r) return l; int mid = (l + r) >> 1; if (c[k * 2]) return left(k * 2, l, mid); else return left(k * 2 + 1, mid + 1, r); } int right(int k, int l, int r) { if (l == r) return l; int mid = (l + r) >> 1; if (c[k * 2 + 1]) return right(k * 2 + 1, mid + 1, r); else return right(k * 2, l, mid); } } st[3]; int n, q; char s[200002]; int getAns(int i) { int res = st[i].c[1], j = (i + 1) % 3, k = (i + 2) % 3; if (!st[k].c[1]) return res; else if (!st[j].c[1]) return 0; else { int l = st[j].right(1, 1, n), r = st[k].right(1, 1, n); if (l < r) res -= st[i].count(1, 1, n, l, r); l = st[k].left(1, 1, n), r = st[j].left(1, 1, n); if (l < r) res -= st[i].count(1, 1, n, l, r); return res; } } int main() { to['R'] = 0, to['S'] = 1, to['P'] = 2; scanf("%d%d%s", &n, &q, s + 1); for (int i = 1; i <= n; ++i) st[to[s[i]]].upd(1, 1, n, i, 1); printf("%d\n", getAns(0) + getAns(1) + getAns(2)); while (q--) { int p; scanf("%d", &p); char c = getchar(); while (c != 'R' && c != 'S' && c != 'P') c = getchar(); st[to[s[p]]].upd(1, 1, n, p, -1); st[to[c]].upd(1, 1, n, p, 1); s[p] = c; printf("%d\n", getAns(0) + getAns(1) + getAns(2)); } }
2,500
CPP
import sys input=sys.stdin.readline n,m,k=map(int,input().split()) a=[int(x) for x in input().split()] d={} f={} for i in range(m): aa,b,s=map(int,input().split()) d[i]=s f[i]=[aa,b] op=[0 for i in range(m)] for i in range(k): x,y=map(int,input().split()) op[x-1]+=1 if y<m: op[y]-=1 for i in range(1,m): op[i]+=op[i-1] p=[0 for i in range(n)] for i in range(m): l=f[i][0] r=f[i][1] v=d[i] v=v*op[i] p[l-1]+=v if r<n: p[r]-=v #print(a) for i in range(1,n): p[i]+=p[i-1] for i in range(n): a[i]+=p[i] print(*a)
1,400
PYTHON3
n,h,m=map(int,input().split()) a=[] for i in range (n+1): a.append(0) for i in range(1,n+1): a[i]=h for i in range(m): l,r,x=map(int,input().split()) for j in range(l,r+1): a[j]=min(a[j],x) ans=0 for i in range(1,n+1): ans=ans+(a[i]**2) print(ans)
800
PYTHON3
ret = 0 p = [True] * (100500) for i in range(2,100300): for j in range(i*i,100300,i): p[j] = False a = [i for i in range(2,100001) if p[i]] n = int(input()) b = list(map(int,input().split())) def f(x): sm = 0 ths = 0 chk = [] for i in range(n): if b[i]: sm += 1 chk.append(i) if sm == x: for j in chk: ths += abs(j - chk[x // 2]) chk = [] sm = 0 return ths ans = 10**18 s = sum(b) for x in a: if s % x == 0: ans = min(ans,f(x)) print(ans if ans != 10**18 else -1)
1,800
PYTHON3
for _ in range(int(input())): n, m = map(int, input().split()) a = [] for i in range(n): a.append(list(map(int, input().split()))) smallest = 100000000 neg = 0 ans = 0 for i in range(n): for j in range(m): ans += abs(a[i][j]) if abs(a[i][j]) < smallest: smallest = abs(a[i][j]) if a[i][j] <= 0: neg += 1 if neg % 2: print(ans - 2 * smallest) else: print(ans)
1,000
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int size, ele; vector<int> b, c; cin >> size; for (int i = 0; i < size; i++) { cin >> ele; if (ele < 0) c.push_back(ele); else b.push_back(ele); } int bSum, cSum; bSum = cSum = 0; for (int i = 0; i < b.size(); i++) bSum = bSum + b[i]; for (int i = 0; i < c.size(); i++) cSum = cSum + c[i]; cout << bSum - cSum << endl; return 0; }
800
CPP
n = int(input()) nums = list(map(int,input().split())) sum = 0 for i in nums: sum+= max(nums)-i print(sum)
800
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, i, count = 0; cin >> n; vector<long long int> m; long long int a[n], b[n]; for (auto i = 0; i < n; i++) cin >> a[i]; for (auto i = 0; i < n; i++) cin >> b[i]; for (auto i = 0; i < n; i++) { m.push_back(a[i] - b[i]); } sort(m.begin(), m.end()); long long int y = 0; for (auto ip = m.rbegin(); ip != m.rend(); ++ip) { y++; long long int a = *ip; if (a > 0) { auto r = m.begin(); auto o = upper_bound(m.begin(), m.end(), -1 * a); count += (n - y - distance(r, o)); } else break; } cout << count; return 0; }
1,400
CPP
n = int(input()) a = list(map(int, input().split())) res = 0 counter = 0 m = max(a) if a.count(m) == 1: print(1) else: for i in range(n): if a[i] == m: counter += 1 else: res = max(res, counter) counter = 0 res = max(res, counter) print(res)
1,100
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxN = 1000 + 5; const int mod = 1000 * 1000 * 1000 + 7; int main() { ios::sync_with_stdio(0); cin.tie(); long double a, b, m; cin >> a >> b >> m; long double vx, vy, vz; cin >> vx >> vy >> vz; long double ti = m / (-vy); long double px = a / 2 + ti * vx; while (px > 2 * a) px -= 2 * a; while (px < 0) px += 2 * a; if (px > a) px = 2 * a - px; if (px < 0) px = -px; long double pz = 0 + ti * vz; while (pz > 2 * b) pz -= 2 * b; while (pz + 2 * b < 0) pz += 2 * b; if (pz > b) pz = 2 * b - pz; if (pz < 0) pz = -pz; cout << fixed << setprecision(10) << px << ' ' << pz << endl; return 0; }
1,700
CPP