solution
stringlengths
10
983k
difficulty
int64
0
25
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; int n, pos, F = 1; long long b[200000 + 1], mx, a[200000 + 1], c[200000 + 1]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lld", &b[i]); c[i] = b[i]; F &= (b[i] == 0); } for (int i = 0; i < n; i++) if (b[(i - 1 + n) % n] != b[i] && mx < b[i]) mx = b[i], pos = i; if (F) { puts("YES"); for (int i = 0; i < n; i++) printf("1 "); return 0; } a[pos] = b[pos]; if (!b[(pos - 1 + n) % n]) c[(pos - 1 + n) % n] = c[pos]; for (int i = pos - 1; i >= pos - n + 1; i--) a[(i + n) % n] = a[(i + n + 1) % n] + c[(i + n) % n]; for (int i = 0; i < n; i++) if (!a[(i + 1) % n] || a[i] % a[(i + 1) % n] != b[i]) return 0 * puts("NO"); puts("YES"); for (int i = 0; i < n; i++) printf("%lld ", a[i]); }
11
CPP
a=int(input()) for i in range(a): b=int(input()) print((b-1)//2)
7
PYTHON3
A, B, K = list(map(int, input().split())) k = min(A, K) print(A-k, max(0,B-(K-k)))
0
PYTHON3
#include <iostream> using namespace std; int main(){ int i,j,k,n,m; cin>>n>>m; for(i=0;i<=n;i++) for(j=0;j<=n;j++) for(k=0;k<=n,i+j+k<=n;k++) if(i*1000+j*5000+k*10000==m&&i+j+k==n){ cout<<k<<" "<<j<<" "<<i<<endl; return 0; } cout<<"-1 -1 -1"<<endl; return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; unordered_map<string, vector<string> > m; set<string> q; string SS(char C) { string p = ""; p += C; return p; } void dfs(string s, long n) { if (n == 0) { q.insert(s); return; } for (auto i : m[SS(s[0])]) dfs(i + s.substr(1, s.size() - 1), n - 1); } int main() { ios::sync_with_stdio(0); cin.tie(0); long n, t; cin >> n >> t; string s, p; for (long i = long(0); i < long(t); i++) { cin >> s >> p; m[p].push_back(s); } for (auto i : m["a"]) dfs(i, n - 2); cout << q.size(); }
8
CPP
#include <bits/stdc++.h> int main() { int n, len; char x[100]; scanf("%d", &n); while (n--) { scanf("%s", &x); len = strlen(x); if (len <= 10) printf("%s\n", x); else printf("%c%d%c\n", x[0], len - 2, x[len - 1]); } }
7
CPP
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(int argc, char const *argv[]) { int r, c; while (cin>>r>>c, r||c) { if (r % 2 == 0 || c % 2 == 0) cout<<"yes"<<endl; else cout<<"no"<<endl; } return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL), cout.tie(NULL); int tc; cin >> tc; while (tc--) { long long l, r, ans; cin >> l >> r; ans = l * 2; cout << l << " " << ans << "\n"; } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; bool isPrime[1000005] = {false}; vector<long long> prm; vector<long long> SPF(1000005); void sieve() { isPrime[1] = isPrime[0] = 0; long long i; for (i = 2; i < 1000005; ++i) isPrime[i] = 1; for (i = 2; i * i < 1000005; ++i) { if (isPrime[i]) { for (long long j = i * i; j < 1000005; j += i) isPrime[j] = 0; } } if (!prm.empty()) prm.clear(); for (i = 2; i < 1000005; ++i) { if (isPrime[i]) prm.push_back(i); } } bool flip(bool f) { if (f) return 0; return 1; } long long fastPower(long long a, long long b) { if (b == 0) return 1; long long prod = 1; while (b > 0) { if (b & 1) prod = prod * a; b = b >> 1; a = a * a; } return prod; } long long fact(long long n) { long long res = 1; for (long long i = 2; i <= n; i++) { res *= i; } return res; } long long nCr(long long n, long long r) { return (long double)fact(n) / (long double)(fact(r) * fact(n - r)); } void printa(bool f) { if (f) cout << "YES" << "\n"; else cout << "NO" << "\n"; } long long bs(vector<long long> &a, long long l, long long r, long long x) { long long m; while (l <= r) { m = l - (r + l) / 2; if (x * 2 <= a[m]) return m; else r = m - 1; } return -1; } void solve() { long long n, i; cin >> n; vector<long long> a(n); for (i = 0; i < n; ++i) cin >> a[i]; vector<long long> dp(n, -1); for (i = n - 1; i >= 0; --i) { long long j = i + 1, t = 0, tmp = a[i]; while (j < n) { if (tmp * 2 >= a[j]) { if (dp[j] != -1) { t += dp[j]; break; } else { ++t; tmp = a[j]; } } else { break; } ++j; } dp[i] = t + 1; } cout << *max_element(dp.begin(), dp.end()) << "\n"; } int main() { int tc = 1; while (tc--) { solve(); } return 0; }
8
CPP
from functools import cmp_to_key from math import * import sys def main(): n, k = [int(x) for x in input().split(' ')] s = input().strip() stack = [] draw = [False for i in range(len(s))] drawn = 0 for i, val in enumerate(s): if val == '(': stack.append(i) else: prev_open = stack.pop() draw[prev_open] = True draw[i] = True drawn += 2 if drawn == k: break for i in range(len(s)): if draw[i]: print(s[i], end='') if __name__ == "__main__": main()
9
PYTHON3
#include<cstdio> #include<vector> using namespace std; using vi=vector<int>; using vvi=vector<vi>; using ll=long long; const int white=-1; const int black=100000000; vi gx, gy; void push(int x, int y) { gx.push_back(x); gy.push_back(y); } int pop(int& x, int& y) { if(gx.size()<=0) return 0; x=gx.back(); y=gy.back(); gx.pop_back(); gy.pop_back(); return 1; } void eng_fill(vvi& c, int x, int y, int g) { int vx[]={ 1, 0,-1, 0}; int vy[]={ 0,-1, 0, 1}; int nx, ny; int w, h; w=7; h=c.size(); push(x, y); c[y][x]=g; while(pop(x, y)) { for(int i=0;i<4;i++) { nx=x+vx[i]; ny=y+vy[i]; if(nx<0 || w<=nx || ny<0 || h<=ny) continue; if(c[ny][nx]!=white) continue; c[ny][nx]=g; push(nx, ny); } } } int engroup(vvi& c) { int w, h, x, y; int g=0; w=7; h=c.size(); for(y=0;y<h;y++) { for(x=0;x<w;x++) { if(c[y][x]!=white) continue; eng_fill(c, x, y, g); g++; } } return g; } int gcd(int a, int b) { int c; while((c=a%b)) { a=b; b=c; } return b; } int lcm(int a, int b) { return a/gcd(a, b)*b; } int main(void) { int m, q, g, uh, gc; ll n, h; vvi c, f; while(scanf("%lld%d%d", &n, &m, &q)==3) { uh=lcm(m, 7)/7; h=n/uh; c.clear(); c.resize(uh); for(auto& ce: c) ce.resize(7, white); for(int i=0;i<q;i++) { int a; scanf("%d", &a); for(int j=a;j<uh*7;j+=m) { c[j/7][j%7]=black; } } g=engroup(c); f.clear(); for(int i=0;i<7;i++) { int u=c[0][i]; int l=c[uh-1][i]; int j; if(u==black || l==black) continue; if(u>l) swap(u, l); vi ft={u,l}; for(j=f.size()-1;j>=0;j--) { if(ft==f[j]) break; } if(j<0) { f.push_back(ft); // printf("%d %d\n", ft[0], ft[1]); } } gc=f.size(); # if 0 for(auto ce:c) { for(int x:ce) { if(x==black) printf(" X "); else printf("%2d ", x); } printf("\n"); } printf("g=%d h=%d gc=%d\n", g, h, gc); #endif ll result=g*h-gc*(h-1); printf("%lld\n", result); } return 0; }
0
CPP
#include <bits/stdc++.h> using namespace std; #ifdef DEBUG_MODE #define DBG(n) n; #else #define DBG(n) ; #endif #define REP(i,n) for(ll (i) = (0);(i) < (n);++i) #define rep(i,s,g) for(ll (i) = (s);(i) < (g);++i) #define rrep(i,s,g) for(ll (i) = (s);i >= (g);--(i)) #define PB push_back #define MP make_pair #define FI first #define SE second #define SHOW1d(v,n) {for(int WWW = 0;WWW < (n);WWW++)cerr << v[WWW] << ' ';cerr << endl << endl;} #define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;} #define ALL(v) v.begin(),v.end() #define Decimal fixed<<setprecision(20) #define INF 1000000000 #define LLINF 1000000000000000000LL #define MOD 1000000007 //--------geometry original ------------------ #define curr(PP, i) PP[i] #define next(PP, i) PP[(i+1)%PP.size()] #define diff(PP, i) (next(PP, i) - curr(PP, i)) #define X real() #define Y imag() typedef long long ll; typedef pair<ll, ll> P; const double EPS = 1e-12; const double PI = acos(-1.0); typedef complex<double> point; namespace std { bool operator < (const point& a, const point& b) { return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b); } bool operator == (const point& a,const point& b) { return (abs(a.real() - b.real()) < EPS && abs(a.imag() - b.imag()) < EPS); } } struct circle { point p; double r; circle(){} circle(const point &p, double r) : p(p), r(r) { } }; struct segment : public vector<point> { segment(const point &a, const point &b) { push_back(a); push_back(b); } }; double cross(const point& a, const point& b) { return imag(conj(a)*b); } double dot(const point& a, const point& b) { return real(conj(a)*b); } /* a → b で時計方向に折れて b → c a → b で半時計方向に折れて b → c a → b で逆を向いて a を通り越して b → c a → b でそのまま b → c a → b で逆を向いて b → c ( または b == c ) */ int ccw(point a, point b, point c) { b -= a; c -= a; if (cross(b, c) > 0) return +1; // counter clockwise if (cross(b, c) < 0) return -1; // clockwise if (dot(b, c) < 0) return +2; // c--a--b on line if (norm(b) < norm(c)) return -2; // a--b--c on line return 0; } bool intersectLL(const segment &l, const segment &m) { return abs(cross(l[1] - l[0], m[1] - m[0])) > EPS || // non-parallel abs(cross(l[1] - l[0], m[0] - l[0])) < EPS; // same line } bool intersectLS(const segment &l, const segment &s) { return cross(l[1] - l[0], s[0] - l[0])* // s[0] is left of l cross(l[1] - l[0], s[1] - l[0]) < EPS; // s[1] is right of l } bool intersectLP(const segment &l, const point &p) { return abs(cross(l[1] - p, l[0] - p)) < EPS; } bool intersectSP(const segment &s, const point &p) { return abs(s[0] - p) + abs(s[1] - p) - abs(s[1] - s[0]) < EPS; // triangle inequality } //端点の交差も考える bool intersectSS(const segment &s, const segment &t) { if(intersectSP(s,t[0]) || intersectSP(s,t[1]) || intersectSP(t,s[0]) || intersectSP(t,s[1]))return true; return ccw(s[0], s[1], t[0])*ccw(s[0], s[1], t[1]) <= 0 && ccw(t[0], t[1], s[0])*ccw(t[0], t[1], s[1]) <= 0; } point projection(const segment &l, const point &p) { double t = dot(p - l[0], l[0] - l[1]) / norm(l[0] - l[1]); return l[0] + t*(l[0] - l[1]); } point reflection(const segment &l, const point &p) { return p + 2. * (projection(l, p) - p); } double distanceLP(const segment &l, const point &p) { return abs(p - projection(l, p)); } double distanceLL(const segment &l, const segment &m) { return intersectLL(l, m) ? 0 : distanceLP(l, m[0]); } double distanceLS(const segment &l, const segment &s) { if (intersectLS(l, s)) return 0; return min(distanceLP(l, s[0]), distanceLP(l, s[1])); } double distanceSP(const segment &s, const point &p) { const point r = projection(s, p); if (intersectSP(s, r)) return abs(r - p); return min(abs(s[0] - p), abs(s[1] - p)); } double distanceSS(const segment &s, const segment &t) { if (intersectSS(s, t)) return 0; return min(min(distanceSP(s, t[0]), distanceSP(s, t[1])), min(distanceSP(t, s[0]), distanceSP(t, s[1]))); } double distancePP(const point &a,const point &b){ return abs(a-b); } //交点 point crosspoint(const segment &l, const segment &m) { double A = cross(l[1] - l[0], m[1] - m[0]); double B = cross(l[1] - l[0], l[1] - m[0]); if (abs(A) < EPS && abs(B) < EPS) return m[0]; // same line if (abs(A) < EPS) return point(INF,INF); // !!!PRECONDITION NOT SATISFIED!!! return m[0] + B / A * (m[1] - m[0]); } //凸包 vector<point> convex_hull(vector<point> ps) { int n = ps.size(), k = 0; sort(ps.begin(), ps.end()); vector<point> ch(2*n); for (int i = 0; i < n; ch[k++] = ps[i++]) // lower-hull while (k >= 2 && ccw(ch[k-2], ch[k-1], ps[i]) == -1) --k; for (int i = n-2, t = k+1;i >= 0; ch[k++] = ps[i--]) // upper-hull while (k >= t && ccw(ch[k-2], ch[k-1], ps[i]) == -1) --k; ch.resize(k - 1); return ch; } /*多角形内包判定 OUT:0 ON:1 IN:2 */ int contains(const vector<point>& Poly, const point& p) { bool in = false; for (int i = 0; i < Poly.size(); ++i) { point a = curr(Poly, i) - p, b = next(Poly, i) - p; if (imag(a) > imag(b)) swap(a, b); if (imag(a) <= 0 && 0 < imag(b)) if (cross(a, b) < 0) in = !in; if (cross(a, b) == 0 && dot(a, b) <= 0) return 1; } return in ? 2 : 0; } //見えるか(可視グラフ用) bool block_off(const point &a, const point &b, const vector<point> &obj) { point m = (a+b)/2.0; bool on = false, in = false; for (int j = 0; j < obj.size(); ++j) { point c = curr(obj,j), d = next(obj,j); if (imag(d) < imag(c)) swap(c, d); if (cross(a-c,b-c) * cross(a-d,b-d) < 0 && // strictly intersect. cross(c-a,d-a) * cross(c-b,d-b) < 0) return true; if (cross(a-c,b-c) == 0 && dot(a-c,b-c) < 0) return true; if (imag(c) <= imag(m) && imag(m) < imag(d)) // strictly contain. if (cross(c-m,d-m) < 0) in = !in; if (cross(c-m,d-m) == 0 && dot(c-m,d-m) <= 0) on = true; } return !on && in; } //面積 double area(const vector<point>& p) { double A = 0; for (int i = 0; i < p.size(); ++i) A += cross(curr(p, i), next(p, i)); return A / 2.; } //角度足し算 double add_rad(double a,double b){ double ret = a + b; if(ret > PI)ret -= 2 * PI; return ret; } //なす角(vector) double angle(const point &a,const point &b) { auto tmp = abs(arg(a) - arg(b)); return min(tmp, 2 * PI - tmp); } double angle(const segment &s1,const segment &s2) { return angle(s1[1] - s1[0], s2[1] - s2[0]); } //点の回転 point rotate(const point &p, double rad) { double x = p.real() * cos(rad) - p.imag() * sin(rad); double y = p.imag() * cos(rad) + p.real() * sin(rad); return point(x, y); } //並行 bool isParallel(const point &a, const point &b){ return abs(cross(a,b)) < EPS; } bool isParallel(const segment &a, const segment &b){ return isParallel(a[1]-a[0], b[1]-b[0]); } //直行 bool isOrthogonal(const point &a,const point &b){ return abs(angle(a,b) - PI / 2) < EPS; } bool isOrthogonal(const segment &a,const segment &b){ return isOrthogonal(a[1]-a[0],b[1]-b[0]); } //凸包判定 bool isConvex(vector<point> poly){ int sz = poly.size(); REP(i,sz){ int tmp = ccw(poly[i],poly[(i+1)%sz],poly[(i+2)%sz]); if(tmp == -1){ return false; } } return true; } double convex_diameter(const vector<point> &pt) { const int n = pt.size(); int is = 0, js = 0; for (int i = 1; i < n; ++i) { if (imag(pt[i]) > imag(pt[is])) is = i; if (imag(pt[i]) < imag(pt[js])) js = i; } double maxd = norm(pt[is]-pt[js]); int i, maxi, j, maxj; i = maxi = is; j = maxj = js; do { if (cross(diff(pt,i), diff(pt,j)) >= 0) j = (j+1) % n; else i = (i+1) % n; if (norm(pt[i]-pt[j]) > maxd) { maxd = norm(pt[i]-pt[j]); maxi = i; maxj = j; } } while (i != is || j != js); return sqrt(maxd); /* farthest pair is (maxi, maxj). */ } vector<point> convex_cut(const vector<point> P, const segment& l) { vector<point> Q; for (int i = 0; i < P.size(); ++i) { point A = curr(P, i), B = next(P, i); if (ccw(l[0], l[1], A) != -1) Q.push_back(A); if (ccw(l[0], l[1], A)*ccw(l[0], l[1], B) < 0) Q.push_back(crosspoint(segment(A, B), l)); } return Q; } int main() { int n;cin >> n; vector<point> v; REP(i,n){ double a,b;cin >> a >> b; v.PB(point(a,b)); } cin >> n; REP(i,n){ double a,b,c,d;cin >> a >> b >> c >> d; segment now = segment(point(a,b),point(c,d)); vector<point> ret = convex_cut(v,now); cout << Decimal << area(ret) << endl; } return 0; }
0
CPP
/*Aswathama*/ #include<bits/stdc++.h> void intl() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } #define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL); #define mod 1000000007 #define INF 10000000000000001 #define F first #define S second #define LB lower_bound #define UB upper_bound #define vc vector #define vll vector<long long> #define pll pair<long long,long long> #define pb push_back #define all(v) v.begin(),v.end() #define forn(i,a,n) for(ll i=a;i<(long long)n;++i) #define backn(i,n,a) for(ll i=n;i>(long long)a;--i) #define sz(x) (ll)x.size() #define ins insert #define mp make_pair #define int long long int #define cy cout<<"Yes\n"; #define cno cout<<"No\n"; typedef long long ll; typedef long double ld; using namespace std; ll round_up(ll a, ll b) {return (a + b - 1) / b;} // round_up(a/b) ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);} ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;} ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;} ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;} void solve() { int n, a; cin >> n; int counter[2 * n + 1]; forn(i, 0, 2 * n + 1)counter[i] = 1e6; for (int i = 0; i < n; i++) { cin >> a; counter[a] = i + 1; } int ans = 0; for (int i = 3; i < 2 * n; i++) { for (int j = 1; j * j <= i; j++) { if (i % j == 0 && i != j * j) { if (counter[j] + counter[i / j] == i)ans++; } } } cout << ans << endl; } signed main() { intl(); FASTIO; int t = 1; cin >> t; while (t-- > 0) { solve(); } return 0; }
8
CPP
for _ in range(int(input())): n, k = [int(x) for x in input().split()] arr = [int(x) for x in input().split()] prev = 0 for i in range(k): j = 0 while(j < n-1): if(arr[j] < arr[j+1]): arr[j] += 1 break j += 1 prev = j+1 if(prev == n): print(-1) break if(prev != n) : print(prev)
8
PYTHON3
n = int(input()) def divisible(a,b): n = a%b if n ==0 : return 0 return (b-n) for i in range(n): a = list(map(int,input().split())) print(divisible(a[0],a[1]))
10
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf("%d", &t); while (t--) { long long int a, b; long long int ans; scanf("%lld %lld", &a, &b); if (a > b) { ans = (b + 1) * b; ans = ans / 2; } else { ans = a * (a - 1); ans = ans / 2; ans = ans + 1; } printf("%lld\n", ans); } }
8
CPP
#include <bits/stdc++.h> using namespace std; class BinaryIndexedTree1 { public: int N; vector<int> bit; int sum(int i) { int s = 0; for (; i > 0; i -= i & -i) s += bit[i]; return s; } int sum(int l, int r) { return sum(r) - sum(l - 1); } void add(int i, int x) { for (; i <= N; i += i & -i) bit[i] += x; } }; void Main() { int N; scanf("%d", &N); vector<int> A(N), T(N), X(N); set<int> xs; map<int, vector<int>> ts; map<int, BinaryIndexedTree1> bits; map<int, map<int, int>> zs; for (int i = 0; i < (N); i++) { int a, t, x; scanf("%d %d %d", &a, &t, &x); A[i] = a; T[i] = t; X[i] = x; ts[x].push_back(t); xs.insert(x); } for (auto&& x : xs) { bits[x].N = ((int)(ts[x]).size()) + 1; bits[x].bit.resize(bits[x].N + 1); sort((ts[x]).begin(), (ts[x]).end()); for (int i = 0; i < (((int)(ts[x]).size())); i++) { int t = ts[x][i]; zs[x][t] = i + 1; } } for (int i = 0; i < (N); i++) { int a, t, x; a = A[i]; t = T[i]; x = X[i]; int t2 = zs[x][t]; if (a == 1) bits[x].add(t2, 1); else if (a == 2) bits[x].add(t2, -1); else (void)printf("%d\n", bits[x].sum(t2)); } } int main() { cin.tie(0); ios::sync_with_stdio(false); Main(); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; long long n, dis1, dis2 = 0x3f3f3f3f, x, y; pair<long long, long long> foun[2]; struct flower { long long id, fdis1, fdis2; } flow[2005]; int cmp(const struct flower& a, const struct flower& b) { return a.fdis2 > b.fdis2; } int main() { scanf("%lld", &n); scanf("%lld%lld%lld%lld", &foun[0].first, &foun[0].second, &foun[1].first, &foun[1].second); for (int i = 0; i < n; i++) { scanf("%lld%lld", &x, &y); flow[i].fdis1 = (x - foun[0].first) * (x - foun[0].first) + (y - foun[0].second) * (y - foun[0].second); flow[i].fdis2 = (x - foun[1].first) * (x - foun[1].first) + (y - foun[1].second) * (y - foun[1].second); } sort(flow, flow + n, cmp); dis2 = flow[0].fdis2; for (int i = 1; i < n; i++) { dis1 = 0; for (int j = 0; j < i; j++) { if (dis1 < flow[j].fdis1) { dis1 = flow[j].fdis1; } } dis2 = min(dis2, dis1 + flow[i].fdis2); } if (dis1 < flow[n - 1].fdis1) dis1 = flow[n - 1].fdis1; dis2 = min(dis2, dis1); printf("%lld", dis2); }
9
CPP
n=int(input()) d=list(map(int, input().split())) p, res=0,0 for i in d: if i ==-1 and p>0: p-=1 elif i ==-1 and p==0: res+=1 else: p+=i print(res)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long n, r, avg, i, sum1, sum2 = 0, ans, x, sum = 0; cin >> n >> r >> avg; pair<long long, long long> pi[n]; sum1 = avg * n; for (i = 0; i < n; i++) { cin >> pi[i].second >> pi[i].first; sum2 = sum2 + pi[i].second; } sort(pi, pi + n); ans = sum1 - sum2; if (ans <= 0) cout << "0" << endl; else { for (i = 0; i < n; i++) { if (ans == 0) break; x = r - pi[i].second; if (x > 0) { if (x >= ans) { sum = sum + ans * pi[i].first; ans = 0; } else { ans = ans - x; sum = sum + x * pi[i].first; } } } cout << sum << endl; } }
9
CPP
def solve(a): bad = [0,0] for i in range(len(a)): if i%2 != a[i]%2: bad[i%2] += 1 if (bad[0] != bad[1]): print (-1) else: print (bad[0]) t = int(input()) for i in range(t): n = int(input()) a = list(map(int, input().split())) solve(a)
8
PYTHON3
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define INF 1<<30 #define LINF 1LL<<60 /* <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2239> ?????????============================================================ ????????????????????????????????±?????????n ????????£?????????. ?????±????????????????????????0 ??????n ??? 1 ?????§???????????????????????????, k ???????????±?????????????????¨, p???a^k + q???b^k ?§????????????¨?????§??????. ??????????????????????§???????m ?§???????????§???????????????????????£??????????????????????????????????,???????????????????????¢??????????????????. ?§????????????????????????§??????. ?????±?????????????????????????????§???????????¨???????????¨????????¨???, ????????????????°?????§???????????????§??????????????????. 1 ??? n, m, a, b, p, q ??? 10^12 (??´??°) ================================================================= ?§£??¬============================================================= ================================================================ */ ll powmod(ll a, ll b) { ll res = 1; assert(b >= 0); for (;b;b >>= 1) { if (b & 1)res = res*a;a = a*a; }return res; } int main(void) { cin.tie(0); ios::sync_with_stdio(false); ll n, m, a, b, p, q; cin >> n >> m >> a >> b >> p >> q; ll res = m; if (a == 1 && b == 1) { ll k = m / (p + q); res = min({ m,abs(m - min(k,n)*(p + q)),abs(min((k + 1),n)*(p + q) - m )}); }else { ll N = min(40LL, n); vector<ll> dist; for (int i = 0; i < N;i++) { ll d = p * powmod(a, i) + q*powmod(b, i); if (d > 2 * m)break; dist.push_back(d); } int n1 = (int)dist.size() / 2; int n2 = (int)dist.size() - n1; vector<ll> L; for (int i = 0; i < (1 << n1);i++) { ll T = 0; for (int j = 0; j < n1;j++) { if ((i >> j) & 1) { T += dist[j]; } } if (T > 2 * m)continue; L.push_back(T); } sort(L.begin(), L.end()); for (int i = 0; i < (1 << n2);i++) { ll T = 0; for (int j = 0; j < n2;j++) { if ((i >> j) & 1) { T += dist[n1 + j]; } } if (T > 2 * m)continue; auto it = lower_bound(L.begin(), L.end(), m - T); if(it != L.end())res = min(res, abs(m - T - *it)); if (it != L.begin()) res = min(res, abs(m - T - *(it - 1))); } } cout << res << endl; return 0; }
0
CPP
import collections def solve(f,ff,fff): if collections.Counter(f+ff) == collections.Counter(fff): return "YES" return "NO" if __name__ == "__main__": f = input() ff = input() fff = input() print(solve(f,ff,fff))
7
PYTHON3
t = int(input()) for i in range(t): n = int(input()) k = 0 x = 0 while k != (n / 2) - 1: k += 1 x += 2 ** k x += 2 ** n while k != n - 1: k += 1 x -= 2 ** k print(x)
7
PYTHON3
import math A,B=list(map(int, input().split())) print((A+B)%24)
0
PYTHON3
a, b = input().split() a = int(a) b = int(b) days = 0 m = 0 while a > 0: days += 1 a -= 1 m += 1 if m % b == 0: m = 0 a += 1 print(days)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int size = 66; int a[size][size][size], dp[size][size][size]; int min(int x, int y) { return x > y ? y : x; } int main() { int n, m, r; while (scanf("%d%d%d", &n, &m, &r) != EOF) { for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) scanf("%d", &a[i][j][k]); for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) for (int l = 1; l <= n; l++) a[i][k][l] = min(a[i][k][l], a[i][k][j] + a[i][j][l]); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { dp[i][j][0] = a[1][i][j]; for (int k = 1; k <= m; k++) dp[i][j][0] = min(dp[i][j][0], a[k][i][j]); } } for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { dp[i][j][k] = dp[i][j][k - 1]; for (int t = 1; t <= n; t++) dp[i][j][k] = min(dp[i][j][k], dp[i][t][k - 1] + dp[t][j][0]); } } } while (r--) { int i, j, k; scanf("%d%d%d", &i, &j, &k); k = min(k, n); printf("%d\n", dp[i][j][k]); } } return 0; }
8
CPP
#include <bits/stdc++.h> int faster_in() { int r = 0, c; for (c = getchar(); c <= 32; c = getchar()) ; if (c == '-') return -faster_in(); for (; c > 32; r = (r << 1) + (r << 3) + c - '0', c = getchar()) ; return r; } using namespace std; const int INF = int(1e9 + 7); const int tam = 500010; vector<int> G[tam]; int L[tam], R[tam]; int n; int minL[tam]; void dfs(int v, int p, int minVal) { int children = G[v].size() - (p != -1); L[v] = minVal - children; minL[v] = L[v]; int cnt = 0; for (int u : G[v]) { if (u == p) continue; R[u] = L[v] + (++cnt); dfs(u, v, minL[v] - 1); minL[v] = min(minL[v], minL[u]); } } int main() { std::ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 0; i < n - 1; ++i) { int a, b; cin >> a >> b; a--; b--; G[a].emplace_back(b); G[b].emplace_back(a); } R[0] = 2 * n; dfs(0, -1, 2 * n - 1); for (int i = 0; i < n; ++i) { cout << L[i] << ' ' << R[i] << '\n'; } return 0; }
11
CPP
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> #include<queue> #include<vector> #include<ctime> #include<map> #include<bitset> #include<set> #include<assert.h> #include<chrono> #include<random> #define LL long long #define mp(x,y) make_pair(x,y) #define pll pair<long long,long long> #define pii pair<int,int> #define SZ(x) ((int)x.size()) using namespace std; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); inline LL read() { LL f=1,x=0;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int stack[20]; template<typename T>inline void write(T x) { if(x<0){putchar('-');x=-x;} if(!x){putchar('0');return;} int top=0; while(x)stack[++top]=x%10,x/=10; while(top)putchar(stack[top--]+'0'); } template<typename T>inline void pr1(T x){write(x);putchar(' ');} template<typename T>inline void pr2(T x){write(x);putchar('\n');} template<typename T>inline void chkmin(T &x,T y){x=x<y?x:y;} template<typename T>inline void chkmax(T &x,T y){x=x>y?x:y;} const int MAXMASK=(1<<19); const int mod=1e9+7; const int MAXN=25; int pow_mod(int a,int b) { int ret=1; for(;b;b>>=1,a=1LL*a*a%mod)if(b&1)ret=1LL*ret*a%mod; return ret; } void ad(int &x,int y){x+=y;if(x>=mod)x-=mod;} vector<int> nxt[MAXN]; void ins(int x,int y){nxt[x].emplace_back(y);} int f[MAXMASK],g[MAXMASK],E[MAXMASK]; int n,m,bin[25],ex[MAXN*MAXN],ey[MAXN*MAXN],mask[MAXN*MAXN],Log[MAXMASK]; int rt[MAXN]; int findrt(int x){return rt[x]==x?rt[x]:rt[x]=findrt(rt[x]);} int lowbit(int x){return x&-x;} int pre[MAXN*MAXN],inv[MAXN*MAXN]; map<int,int> mp[MAXN]; int get_edge(int x,int fa,int ed) { if(x==ed)return 0; for(auto y:nxt[x])if(y^fa) { int t=get_edge(y,x,ed); if(t!=-1)return t|bin[mp[x][y]-1]; }return -1; } int H[MAXMASK]; inline void FMT(int *y,int len){for(int i=1;i<len;i<<=1)for(int x=0;x<len;x++)if(!(x&i))ad(y[x|i],y[x]);} int main() { pre[0]=1;for(int i=1;i<MAXN*MAXN;i++)pre[i]=1LL*pre[i-1]*i%mod; inv[MAXN*MAXN-1]=pow_mod(pre[MAXN*MAXN-1],mod-2); for(int i=MAXN*MAXN-2;i>=0;i--)inv[i]=1LL*inv[i+1]*(i+1)%mod; bin[0]=1;for(int i=1;i<=20;i++)bin[i]=bin[i-1]<<1; for(int i=0;(1<<i)<MAXMASK;i++)Log[1<<i]=i; n=read();m=read(); for(int i=1;i<=m;i++) { ex[i]=read(),ey[i]=read(); if(i<n)ins(ex[i],ey[i]),ins(ey[i],ex[i]),mp[ex[i]][ey[i]]=mp[ey[i]][ex[i]]=i; } for(int i=n;i<=m;i++)mask[i]=get_edge(ex[i],0,ey[i]),++H[mask[i]]; FMT(H,bin[n-1]); for(int i=0;i<bin[n-1];i++) E[i]=(m-n+1)-H[(bin[n-1]-1)^i]; // for(int j=n;j<=m;j++)E[i]+=(mask[j]&i)>0; g[0]=1; for(int i=0;i<bin[n-1];i++)if(g[i]) { int t=__builtin_popcount(i); for(int S=(bin[n-1]-1)^i;S;S^=lowbit(S)) { int u=lowbit(S),newe=E[i|u]-E[i],pree=E[i]+t; int newg=1LL*g[i]*pre[pree+newe]%mod*inv[pree]%mod; int newf=1LL*f[i]*pre[pree+newe+1]%mod*inv[pree+1]%mod; ad(newf,1LL*(t+1)*newg%mod); ad(g[i|u],newg);ad(f[i|u],newf); } } pr2(f[bin[n-1]-1]); return 0; }
0
CPP
import math for test in range(int(input())): n=int(input()) A=list(map(int,input('').split())) A.sort() flag=0 for i in range(1,n): if abs(A[i]-A[i-1])>1: flag=1 break if flag: print('NO') else: print("YES")
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long inf = 1e18; const long long mod = 1e9 + 7; const long long MOD = 998244353; const double EPS = 1e-8; const double PI = acos(-1.0); void _print(long long t) { cerr << t; } void _print(int t) { cerr << t; } void _print(string t) { cerr << t; } void _print(char t) { cerr << t; } void _print(long double t) { cerr << t; } void _print(double t) { cerr << t; } void _print(unsigned long long t) { cerr << t; } template <class T, class V> void _print(pair<T, V> p); template <class T> void _print(vector<T> v); template <class T> void _print(set<T> v); template <class T, class V> void _print(map<T, V> v); template <class T> void _print(multiset<T> v); template <class T, class V> void _print(pair<T, V> p) { cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}"; } template <class T> void _print(vector<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void _print(set<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void _print(multiset<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T, class V> void _print(map<T, V> v) { cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]"; } void solve() { int n; cin >> n; vector<long long> a(n), b(n); for (long long i = 0; i < n; i++) cin >> a[i]; for (long long i = 0; i < n; i++) cin >> b[i]; bool pos = 0, neg = 0; for (long long i = 0; i < n; i++) { if (a[i] < b[i]) { if (!pos) { cout << "NO\n"; return; } } else if (a[i] > b[i]) { if (!neg) { cout << "NO\n"; return; } } if (a[i] == 1) pos = 1; if (a[i] == -1) neg = 1; } cout << "YES\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; cin >> t; while (t--) { solve(); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; const long long mo = 1000000007; long long i, n, a[1000], m; long long d(long long c, long long b) { if (b == 0) return c; else return d(b, c % b); } int main() { cin >> n >> m; for (i = 0; i < n; i++) { if (2 * i + 1 + 2 * n <= m) cout << 2 * i + 1 + 2 * n << " "; if (2 * i + 1 <= m) cout << 2 * i + 1 << " "; if (2 * i + 2 + 2 * n <= m) cout << 2 * i + 2 + 2 * n << " "; if (2 * i + 2 <= m) cout << 2 * i + 2 << " "; } return 0; }
8
CPP
for _ in range(int(input())): n=int(input()) x=input() a=list(x) #r=a.reverse() #print(a) j=n-1 b=[] b.append(0) z=[] o=[] for i in range(n): if(a[i]=='0'): z.append(a[i]) else: break #a.reverse() while(j>=0): if(a[j]=='1'): o.append(a[j]) else: break j=j-1 #print(z) #print(o) c=z+o y=[] y.extend(z) y.extend(b) y.extend(o) if(len(c)!=n): print("".join(map(str, y))) else: print("".join(map(str, c)))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; long long arr[(1 << 20)]; int swp[20]; int n, q; struct TreeNode { long long sum; TreeNode *left, *right; TreeNode() : sum(0), left(nullptr), right(nullptr) {} TreeNode(long long val) : sum(val), left(nullptr), right(nullptr) {} }; TreeNode* Create(int l, int r) { if (l == r) { TreeNode* node = new TreeNode(arr[l]); return node; } TreeNode* node = new TreeNode(); int mid = (l + r) / 2; node->left = Create(l, mid); node->right = Create(mid + 1, r); node->sum = node->left->sum + node->right->sum; return node; } inline void reverseLevel(int k) { for (int i = k; i < 20; i++) swp[i] ^= 1; } inline void swapLevel(int k) { swp[k] ^= 1; } void updateValue(TreeNode* node, int start, int end, int lev, int idx, long long val) { if (start == end) { node->sum = val; return; } int mid = (start + end) / 2; if (start <= idx && idx <= mid) { if (swp[lev]) updateValue(node->right, start, mid, lev + 1, idx, val); else updateValue(node->left, start, mid, lev + 1, idx, val); } else { if (swp[lev]) updateValue(node->left, mid + 1, end, lev + 1, idx, val); else updateValue(node->right, mid + 1, end, lev + 1, idx, val); } node->sum = node->left->sum + node->right->sum; } long long query(TreeNode* node, int start, int end, int l, int r, int lev) { if (start > r or end < l or l > r or start > end) return 0; if (start >= l && end <= r) return node->sum; int mid = (start + end) / 2; long long val; if (!swp[lev]) val = query(node->left, start, mid, l, r, lev + 1) + query(node->right, mid + 1, end, l, r, lev + 1); else val = query(node->right, start, mid, l, r, lev + 1) + query(node->left, mid + 1, end, l, r, lev + 1); return val; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> q; for (int i = 0; i < (1 << n); i++) cin >> arr[i]; TreeNode* root = Create(0, (1 << n) - 1); while (q--) { int type; int l, r; int x, k; cin >> type; switch (type) { case 1: cin >> x >> k; x--; updateValue(root, 0, (1 << n) - 1, 0, x, k); break; case 2: cin >> k; reverseLevel(n - k); break; case 3: cin >> k; swapLevel(n - k - 1); break; case 4: cin >> l >> r; l--; r--; long long ans = query(root, 0, (1 << n) - 1, l, r, 0); cout << ans << "\n"; break; } } }
12
CPP
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; cout << (count(s.begin(), s.end(), '7') ? "Yes" : "No") << endl; }
0
CPP
#include <bits/stdc++.h> using namespace std; const long long sz = 1e6 + 10; long long car[sz], n, m; struct info { long long id, gap; }; struct cmp1 { const bool operator()(const info &a, const info &b) { if (a.gap == b.gap) return a.id < b.id; return a.gap > b.gap; } }; struct cmp2 { const bool operator()(const info &a, const info &b) { return a.id < b.id; } }; set<info, cmp1> lst1; set<info, cmp2> lst2; inline long long calc_gap(long long l, long long r) { if (l == 0) return r - 1; if (r == n + 1) return n - l; long long d = r - l - 1; return d / 2 + (d & 1); } int main() { scanf("%lld", &n), scanf("%lld", &m); lst1.insert({0, n}), lst2.insert({0, n}); lst1.insert({n + 1, 0}), lst2.insert({n + 1, 0}); for (long long i = 1; i <= m; ++i) { long long op, c; scanf("%lld", &op), scanf("%lld", &c); if (op == 1) { info got = *lst1.begin(); if (got.id != 0) car[c] = got.id + got.gap; else car[c] = 1; printf("%lld\n", car[c]); lst1.erase(got), lst2.erase(got); long long d = calc_gap(got.id, car[c]); lst1.insert({got.id, d}), lst2.insert({got.id, d}); auto it = lst2.upper_bound(got); d = calc_gap(car[c], it->id); lst1.insert({car[c], d}), lst2.insert({car[c], d}); } else { long long id = car[c]; info got = *lst2.find({id, 0LL}); lst1.erase(got), lst2.erase(got); auto it = lst2.lower_bound(got); info lft = *(--it); it = lst2.upper_bound(got); long long d = calc_gap(lft.id, it->id); lst1.erase(lft), lst2.erase(lft); lst1.insert({lft.id, d}), lst2.insert({lft.id, d}); } } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using db = double; int n, k, mod; ll f[250006][27]; const int N = 250001; ll fac[N]; ll inv[N]; ll ifac[N]; int main() { cin >> n >> k >> mod; inv[1] = fac[0] = fac[1] = ifac[0] = ifac[1] = 1; for (int i = 2; i < N; ++i) { inv[i] = (ll)mod / i * (mod - inv[mod % i]) % mod; fac[i] = (ll)fac[i - 1] * i % mod; ifac[i] = (ll)ifac[i - 1] * inv[i] % mod; } ll ans = 1; for (int i = 1; i <= n; ++i) (ans *= k) %= mod; if (n % 2 == 0) { f[0][0] = 1; for (int i = 1; i <= n; ++i) if ((n & i) == i) for (int msk = i - ((i) & -(i)), j = msk;; --j &= msk) { int len = min(k - 1, __builtin_popcount(j)); for (int t = 0; t <= len; ++t) (f[i][t + 1] += f[j][t] * ifac[i - j] % mod * (k - t)) %= mod; if (!j) break; } for (int i = 1; i <= k; ++i) (ans -= f[n][i] * fac[n]) %= mod; } cout << (ans + mod) % mod << endl; return 0; }
9
CPP
n = int(input()) lk = [] for i in range(n): l,k = map(int, input().split()) lk.append([l,k]) k = int(input()) minus = 0 for c in lk: if k > c[1]: minus += 1 else: break print(n-minus)
7
PYTHON3
#include <iostream> #include <string> #include <algorithm> #include <cmath> #include <vector> #include <set> #include <cstdlib> using namespace std; typedef long long LL; static const double EPS = 1e-9; #define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i) #define REP(i,n) FOR(i,0,n) int judge(double t1, double t2){ int j=1; if(abs(t2-t1)>M_PI) j*=-1; if(t2-t1<0) j*=-1; return j; } int main(void){ double x[4],y[4],t[4]; while(cin>>x[0]){ int s = 0; cin.ignore(); cin >> y[0]; for(int i=1;i<4;i++){ cin.ignore(); cin >> x[i]; cin.ignore(); cin >> y[i]; } REP(i,4) t[i] = atan2(y[(i+1)%4]-y[i], x[(i+1)%4]-x[i]); REP(i,4) s+=judge(t[i],t[(i+1)%4]); if(abs(s) == 4) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
0
CPP
from sys import stdin fi = stdin # fi = open("input.txt", "r") def read_line_ints(): return [int(v) for v in fi.readline().split()] n, m = read_line_ints() x, k, y = read_line_ints() a = read_line_ints() b = read_line_ints() def solve(): a_value_to_idx = {value: idx for idx, value in enumerate(a)} for bi in b: if bi not in a_value_to_idx: return -1 for bi, bj in zip(b[:-1], b[1:]): if a_value_to_idx[bi] > a_value_to_idx[bj]: return -1 segments = [] if not b: segments = [(0, n-1)] else: if a_value_to_idx[b[0]] != 0: segments.append((0, a_value_to_idx[b[0]]-1)) for bi, bj in zip(b[:-1], b[1:]): ai_idx = a_value_to_idx[bi] aj_idx = a_value_to_idx[bj] if aj_idx > ai_idx + 1: segments.append((ai_idx+1, aj_idx-1)) if a_value_to_idx[b[-1]] != n-1: segments.append((a_value_to_idx[b[-1]]+1, n-1)) cost = 0 a.append(-1) for l, r in segments: max_v = max(a[l:r+1]) count = r-l+1 if max_v < max(a[l-1], a[r+1]): if x > k * y: cost += y * count else: n_blast = count // k cost += x * n_blast cost += y * (count % k) else: if count < k: return -1 if x > k * y: cost += y * (count - k) + x else: n_blast = count // k cost += x * n_blast cost += y * (count % k) return cost print(solve())
10
PYTHON3
#coding=utf-8 # if __name__ == "__main__": # # 读取第一行的n # n = int(sys.stdin.readline().strip()) # for i in range(n): # # 读取每一行 # line = sys.stdin.readline().strip() # # 把每一行的数字分隔后转化成int列表 # values = map(int, line.split()) import sys def Q4(string): x = 0 if '++' in string: x += 1 elif '--' in string: x -= 1 return x if __name__ == "__main__": res = 0 n = int(sys.stdin.readline().strip()) for i in range(n): line = sys.stdin.readline().strip() res += Q4(line) print (res)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int maxn = 310; int perr[maxn][maxn]; int cmp[maxn]; int scmp[maxn]; int perrA[maxn]; int Arrep[maxn]; int ans[maxn]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { scanf("%d", &perr[i][j]); } } for (int i = 1; i <= n; i++) { scanf("%d", &perrA[i]); Arrep[perrA[i]] = i; } cmp[1] = 1; memset(scmp, -1, sizeof(scmp)); for (int i = 2; i <= n; i++) { if (Arrep[i] < Arrep[cmp[i - 1]]) { cmp[i] = i; scmp[i] = cmp[i - 1]; } else { cmp[i] = cmp[i - 1]; if (scmp[i - 1] == -1) scmp[i] = i; else if (Arrep[i] < Arrep[scmp[i - 1]]) { scmp[i] = i; } else { scmp[i] = scmp[i - 1]; } } } for (int i = 1; i <= n; i++) { int tmp = i; for (int j = 1; j <= n; j++) { if (perr[i][j] == i) continue; if (cmp[perr[i][j]] == perr[i][j]) { tmp = perr[i][j]; break; } else if (cmp[perr[i][j]] == i) { if (scmp[perr[i][j]] == perr[i][j]) { tmp = perr[i][j]; break; } } } ans[i] = tmp; } for (int i = 1; i <= n; i++) { printf("%d ", ans[i]); } printf("\n"); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main(){ int N; cin >> N; string S; cin >> S; int Q; cin >> Q; vector<long long> answer; for (int q=0; q<Q; q++){ int k; cin >> k; long long D=0, M=0, DM=0, ans=0; for (int i=0; i<N; i++){ if (S[i]=='D'){ D++; } else if (S[i]=='M'){ M++; DM += D; } else if (S[i]=='C'){ ans += DM; } if (i>=k-1){ if (S[i-k+1]=='D'){ DM -= M; D--; } else if (S[i-k+1]=='M'){ M--; } } } answer.push_back(ans); } for (auto k: answer) cout << k << endl; }
0
CPP
#calculating function n=int(input()) sum=0 if n%2!=0: n//=2 sum=-(n+1) else: n//=2 sum=n print(sum)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int N = 1005; int n, ans1, ans2, cnt1[15], cnt2[15]; char s[N], t[N]; int main() { scanf("%d%s%s", &n, s + 1, t + 1); for (int i = 1; i <= n; i++) cnt1[t[i] - '0']++, cnt2[t[i] - '0']++; for (int i = 1; i <= n; i++) { int j = s[i] - '0'; for (; j <= 9 && !cnt1[j]; j++) ; if (j == 10) { j = 0; ans1++; for (; !cnt1[j]; j++) ; cnt1[j]--; } else cnt1[j]--; j = s[i] - '0' + 1; for (; j <= 9 && !cnt2[j]; j++) ; if (j == 10) { j = 0; for (; !cnt2[j]; j++) ; cnt2[j]--; } else cnt2[j]--, ans2++; } cout << ans1 << endl << ans2 << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> inline string toString(T a) { ostringstream os(""); os << a; return os.str(); } template <typename T> inline long long toLong(T a) { long long res; istringstream os(a); os >> res; return res; } template <typename T> inline T SQ(T a) { return a * a; } template <typename T> inline unsigned long long bigmod(T a, T b, T m) { if (b == 0) return 1; else if (b % 2 == 0) return SQ(bigmod(a, b / 2, m)) % m; else return (a % m * bigmod(a, b - 1, m)) % m; } template <typename T> inline vector<string> parse(T str) { vector<string> res; string s; istringstream os(str); while (os >> s) res.push_back(s); return res; } template <typename T> inline unsigned long long dist(T A, T B) { unsigned long long res = (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y); return res; } template <typename T> inline double cosAngle(T a, T b, T c) { double res = a * a + b * b - c * c; res = res / (2 * a * b); res = acos(res); return res; } int mat[105][105]; int vis[105]; int main() { int i, j, k, n; cin >> n; for (int i = (1); i <= (n); i++) for (int j = (1); j <= (n); j++) cin >> mat[i][j]; for (int i = (1); i <= (n); i++) for (int j = (1); j <= (n); j++) { if (mat[i][j] == -1) continue; if (mat[i][j] == 1) vis[i] = 1; if (mat[i][j] == 2) vis[j] = 1; if (mat[i][j] == 3) vis[j] = 1, vis[j] = 1; } int res = 0; for (int i = (1); i <= (n); i++) res += (1 - vis[i]); cout << res << endl; for (int i = (1); i <= (n); i++) if (!vis[i]) cout << i << " "; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { int i, x, n, k, m = 0, f = 0, j = 1; int a[26]; string s; cin >> s; n = s.length(); cin >> k; k = k + n; for (i = 0; i < 26; i++) { cin >> a[i]; m = max(m, a[i]); } for (i = 0; i < n; i++) { f = f + a[s[i] - 'a'] * j; j++; } f = f + ((k * (k + 1)) / 2 - (n * (n + 1)) / 2) * m; cout << f << endl; }
8
CPP
#include <bits/stdc++.h> using namespace std; long long modF(long long n) { long long mod = 256; return (n % mod + mod) % mod; } long long FUNCREV(long long n) { vector<long long> dp(8, 0); long long dpC = 0; while (n > 1) { dp[dpC++] = n % 2; n = n / 2; } if (n != 0) dp[dpC++] = n; long long ans = 0, mul = 1; for (long long i = 7; i >= 0; i--) { ans = ans + dp[i] * mul; mul = mul << 1; } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string s; getline(cin, s); for (long long i = 0; i < s.length(); i++) { long long number = s[i]; long long rev; if (i == 0) { rev = 0; } else { rev = FUNCREV(s[i - 1]); } number = FUNCREV(number); number = (rev - number); number = modF(number); cout << number << endl; } }
7
CPP
#https://codeforces.com/problemset/problem/238/B n, h = map(int, input().split()) a = list(map(int, input().split())) b = [[x, i] for i, x in enumerate(a)] b = sorted(b, key=lambda x:x[0]) def solve(a, n, h): if n == 2: return 0, [1, 1] min_ = (a[-1][0] + a[-2][0]) - (a[0][0] + a[1][0]) # move a[0] to 2th-group min_2 = max(a[-1][0] + a[-2][0], a[-1][0] + a[0][0] + h) - min(a[0][0] + a[1][0] +h, a[1][0] + a[2][0]) ans = [1] * n if min_2 < min_: min_ = min_2 ans[a[0][1]] = 2 return min_, ans min_, ans = solve(b, n, h) print(min_) print(' '.join([str(x) for x in ans])) #5 10 #0 1 0 2 1 #3 2 #1 2 3
8
PYTHON3
#include<iostream> #include<string> using namespace std; int main(){ int n,cou=0; string str; cin >> n; while(n>cou){ int cou_out=0,ans=0,ining=0; while(cou_out < 3){ cin >> str; if(str == "HIT"){ ining++; if(ining == 4){ ans++; ining--; } } else if(str == "OUT"){ cou_out++; } else if(str == "HOMERUN"){ ans += ining+1; ining = 0; } } cou++; cout << ans << endl; } }
0
CPP
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int fx[] = {+0, +1, +0, -1, +1, +1, -1, -1, +0}; int fy[] = {+1, +0, -1, +0, +1, -1, +1, -1, +0}; template <typename T> inline T GCD(T a, T b) { a = abs(a); b = abs(b); if (a < b) swap(a, b); while (b) { a = a % b; swap(a, b); } return a; } template <typename T> inline T EGCD(T a, T b, T &x, T &y) { if (a == 0) { x = 0; y = 1; return b; } T x1, y1; T d = EGCD(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return d; } template <typename T> inline T LCM(T x, T y) { T tp = GCD(x, y); if ((x / tp) * 1. * y > 9e18) return 9e18; return (x / tp) * y; } template <typename T> inline T BigMod(T A, T B, T M = MOD) { T ret = 1; while (B) { if (B & 1) ret = (ret * A) % M; A = (A * A) % M; B = B >> 1; } return ret; } template <typename T> inline T InvMod(T A, T M = MOD) { return BigMod(A, M - 2, M); } template <typename T> T scani(T &n) { n = 0; bool negative = false; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') negative = true; c = getchar(); } while (c >= '0' && c <= '9') { n = n * 10 + c - 48; c = getchar(); } if (negative) n = ~(n - 1); return n; } template <typename T> void write(T n, int type = true) { if (n < 0) { putchar('-'); n = -n; } if (!n) { putchar('0'); if (type == 32) putchar(' '); else if (type) putchar('\n'); return; } char buff[22]; int len = 0; while (n) buff[len++] = n % 10 + 48, n /= 10; for (int i = len - 1; i >= 0; i--) putchar(buff[i]); if (type == 32) putchar(' '); else if (type) putchar('\n'); } int scans(char *a) { int i = 0; char c = 0; while (c < 33) c = getchar(); while (c > 33) { a[i++] = c; c = getchar(); } a[i] = 0; return i; } const int N = 105; long long n, m, b, limit; long long dp[(1 << 20) + 10]; struct data { long long money; long long monitor, Pn; int sMask; data() {} data(long long a, long long x, long long c) { money = a; monitor = x; Pn = c; sMask = 0; } bool operator<(const data &yoo) const { return monitor > yoo.monitor; } } in[N]; int main() { set<int> st; memset(dp, -1, sizeof(dp)); scani(n), scani(m), scani(b); for (int i = 0; i < n; i++) { long long x, y, z; scani(x), scani(y), scani(z); in[i] = data(x, y, z); int mask = 0; for (int j = 0; j < z; j++) { int a; scani(a); st.insert(a); mask |= (1 << (a - 1)); } in[i].sMask = mask; } if (st.size() < m) { printf("-1\n"); return 0; } sort(in, in + n); limit = (1 << m) - 1; long long ans = 2e18; int id = 0; int mx = 0; for (int i = 0; i <= limit; i++) dp[i] = 2e18; for (int i = 0; i < n; i++) { dp[in[i].sMask] = min(b * in[i].monitor + in[i].money, dp[in[i].sMask]); mx = max(mx, in[i].sMask); for (int j = 0; j <= mx; j++) { dp[j | in[i].sMask] = min(dp[j | in[i].sMask], in[i].money + dp[j]); mx = max(mx, j | in[i].sMask); } ans = min(ans, dp[limit]); } if (ans >= 2e18) ans = -1; write(ans); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; stack<int> sta; int main() { int n, t, s; scanf("%d", &n); int cnt = 0; int ans = 0; int speed = 0; for (int i = 0; i < n; i++) { scanf("%d", &t); if (t == 1) { scanf("%d", &s); speed = s; while (!sta.empty() && sta.top() < speed) { ans++; sta.pop(); } } if (t == 2) { ans += cnt; cnt = 0; } if (t == 3) { scanf("%d", &s); if (speed > s) ans++; else sta.push(s); } if (t == 4) cnt = 0; if (t == 5) { while (!sta.empty()) sta.pop(); } if (t == 6) cnt++; } printf("%d\n", ans); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; template <class T> inline T min(T &a, T &b) { return a < b ? a : b; } template <class T> inline T max(T &a, T &b) { return a > b ? a : b; } template <class T> void read(T &x) { char ch; while ((ch = getchar()) && !isdigit(ch)) ; x = ch - '0'; while ((ch = getchar()) && isdigit(ch)) x = x * 10 + ch - '0'; } struct point { int x, y; point() {} point(int _x, int _y) : x(_x), y(_y) {} }; const int N = 2000; stack<pair<int, int> > Ans; int val[N], pos[N], n; void change(int l, int r) { for (int i = l; i <= (l + r) >> 1; i++) swap(val[i], val[l + r - i]); for (int i = l; i <= r; i++) pos[val[i]] = i; } void sc() { printf("%d\n", Ans.size()); while (Ans.size()) { printf("%d %d\n", Ans.top().first, Ans.top().second); Ans.pop(); } exit(0); } int check() { for (int i = 1; i <= n; i++) if (val[i] != i) return 0; return 1; } void dfs(int l, int r, int c) { if (l > r && check()) { sc(); return; } if (c == 0) { if (check()) sc(); return; } if (val[l] == l && val[r] == r) { dfs(l + 1, r - 1, c); return; } if (val[l] == l) { dfs(l + 1, r, c); return; } if (val[r] == r) { dfs(l, r - 1, c); return; } if (val[l] == r && val[r] == l) { change(l, r); Ans.push(make_pair(l, r)); dfs(l + 1, r - 1, c - 1); change(l, r); Ans.pop(); return; } int T = pos[l]; Ans.push(make_pair(l, pos[l])); change(l, pos[l]); dfs(l + 1, r, c - 1); change(l, T); Ans.pop(); T = pos[r]; Ans.push(make_pair(pos[r], r)); change(pos[r], r); dfs(l, r - 1, c - 1); change(T, r); Ans.pop(); T = min(pos[l], pos[r]); int S = max(pos[l], pos[r]); Ans.push(make_pair(T, S)); change(T, S); dfs(l, r, c - 1); change(T, S); Ans.pop(); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &val[i]), pos[val[i]] = i; dfs(1, n, 3); }
11
CPP
c=''.join(set(input())) x=len(c) if x%2==0: print("CHAT WITH HER!") else: print("IGNORE HIM!")
7
PYTHON3
def is_subsequence(a, b, idx, top): i = 0 for j, c in enumerate(a): if c == b[i] and idx[j+1] > top: i += 1 if i == len(b): return True return False a = input() b = input() r = list(map(int, input().split())) idx = [0 for _ in range(len(r)+1)] for i in range(len(r)): idx[r[i]] = i lo = -1 hi = len(a) while hi - lo > 1: mi = (lo + hi) // 2 if is_subsequence(a, b, idx, mi): lo = mi else: hi = mi print(lo+1) #~ for i in range(-1, len(r)+1): #~ print(is_subsequence(a, b, idx, i))
7
PYTHON3
def main(): score = [] n, k = map(int , input('').split()) score = list(map(int , input('').split())) count = 0 for j in score: if j >= score[k-1] and j > 0: count = count+1 print(count) main()
7
PYTHON3
#include <algorithm> #include <climits> #include <cmath> #include <cstdlib> #include <functional> #include <iostream> #include <queue> #include <vector> using namespace std; constexpr double EPS = 1e-9; struct point { double x, y; point(double x_ = 0.0, double y_ = 0.0):x(x_), y(y_) {} point(const point &p):x(p.x), y(p.y) {} point operator+ (const point &p) const { return point(x + p.x, y + p.y); } point operator- (const point &p) const { return point(x - p.x, y - p.y); } point operator* (double s) const { return point(x * s, y * s); } point operator* (const point &p) const { return point(x * p.x - y * p.y, x * p.y + y * p.x); } point operator/ (double s) const { return point(x / s, y / s); } bool operator< (const point &p) const { return x + EPS < p.x || (abs(x - p.x) < EPS && y + EPS < p.y); } bool operator== (const point &p) const { return abs(x - p.x) < EPS && abs(y - p.y) < EPS; } }; istream &operator>>(istream &is, point &p) { return is >> p.x >> p.y; } point rotate90(const point &p) { return point(-p.y, p.x); } double norm(const point &p) { return p.x * p.x + p.y * p.y; } double abs(const point &p) { return sqrt(norm(p)); } double dot(const point &a, const point &b) { return a.x * b.x + a.y * b.y; } double cross(const point &a, const point &b) { return a.x * b.y - a.y * b.x; } struct segment { point a, b; segment(const point &a_, const point &b_):a(a_), b(b_){} }; struct circle { point c; double r; circle(const point &c_, const double &r_):c(c_), r(r_){} }; typedef vector<point> polygon; int ccw(const point &a, point b, point c) { b = b - a; c = c - a; const double tmp = cross(b, c); if(tmp > EPS) return 1; // ccw if(tmp < -EPS) return -1; // cw if(dot(b, c) < 0) return 2; // c, a, b 順に一直線上 if(norm(b) < norm(c)) return -2; // a, b, c 順に一直線上 return 0; //a, c, b 順で一直線上 } bool intersect(const segment &s, const segment &t) { return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0; } bool intersect(const segment &s, const point &p) { return ccw(s.a, s.b, p) == 0; } point crosspoint(const segment &s, const segment &t) { if(!intersect(s, t)) return s.a; // 交点を持たない const double tmp = cross(s.b - s.a, t.b - t.a); if(abs(tmp) < EPS) { // 一直線上 if(intersect(s, t.a)) return t.a; if(intersect(s, t.b)) return t.b; if(intersect(t, s.a)) return s.a; return s.b; } return t.a + (t.b - t.a) * cross(s.b - s.a, s.b - t.a) * (1.0 / tmp); } struct edge { int to; double cost; edge(int to_, double cost_):to(to_), cost(cost_) {} }; typedef vector<vector<edge>> graph; int get_index(const vector<point> &points, const point &p) { return lower_bound(points.begin(), points.end(), p) - points.begin(); } graph arrangement(const vector<segment> &segments, vector<point> &points) { vector<segment> streets, signs; for(int i = 0; i < static_cast<int>(segments.size()); ++i) { bool a_on = false, b_on = false; for(int j = 0; j < static_cast<int>(segments.size()); ++j) { if(i == j) continue; a_on |= intersect(segments[j], segments[i].a); b_on |= intersect(segments[j], segments[i].b); } (a_on && b_on ? streets : signs).emplace_back(segments[i]); } const int n = streets.size(); points.clear(); for(int i = 0; i < n; ++i) { const auto &s1 = streets[i]; points.emplace_back(s1.a); points.emplace_back(s1.b); for(int j = i + 1; j < n; ++j) { const auto &s2 = streets[j]; if(intersect(s1, s2)) { points.emplace_back(crosspoint(s1, s2)); } } } sort(points.begin(), points.end()); points.erase(unique(points.begin(), points.end()), points.end()); const int V = points.size(); graph G(V); for(const auto &s : streets) { vector<pair<double, int>> vs; for(int i = 0; i < V; ++i) { if(intersect(s, points[i])) { vs.emplace_back(abs(s.a - points[i]), i); } } sort(vs.begin(), vs.end()); for(int i = 1; i < static_cast<int>(vs.size()); ++i) { const int v = vs[i].second; const int u = vs[i - 1].second; const double d = vs[i].first - vs[i - 1].first; bool go = true; bool back = true; const segment street(points[v], points[u]); for(const auto & t : signs) { const bool a_on = intersect(street, t.a); const bool b_on = intersect(street, t.b); if(a_on || b_on) { const point &base = (a_on ? t.a : t.b); const point &tip = (a_on ? t.b : t.a); const int tmp = ccw(base, base + rotate90(points[u] - points[v]), tip); if(tmp != 1) go = false; if(tmp != -1) back = false; } } if(go) G[v].emplace_back(u, d); if(back) G[u].emplace_back(v, d); } } return G; } bool dijkstra(const graph &G, int s, int t, vector<int> &prev) { typedef pair<double, int> P; const int n = G.size(); vector<double> dist(n, INT_MAX); priority_queue<P, vector<P>, greater<P>> que; dist[s] = 0; que.push({0, s}); prev.assign(n, -1); while(!que.empty()) { const auto d = que.top().first; const auto v = que.top().second; que.pop(); if(dist[v] + EPS < d) continue; if(v == t) return true; for(const auto &e : G[v]) { const auto next_dist = d + e.cost; if(dist[e.to] > next_dist + EPS) { dist[e.to] = next_dist; que.push({next_dist, e.to}); prev[e.to] = v; } } } return false; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); for(int n; cin >> n && n;) { point start, goal; cin >> start >> goal; vector<segment> segments; segments.reserve(n); for(int i = 0; i < n; ++i) { point a, b; cin >> a >> b; segments.emplace_back(a, b); } vector<point> points; const auto G = arrangement(segments, points); const int start_index = get_index(points, start); const int goal_index = get_index(points, goal); vector<int> prev; if(!dijkstra(G, start_index, goal_index, prev)) { cout << -1 << '\n'; continue; } vector<int> path; for(int v = goal_index; v != -1; v = prev[v]) { path.emplace_back(v); } reverse(path.begin(), path.end()); for(const auto &idx : path) { cout << points[idx].x << ' ' << points[idx].y << '\n'; } cout << "0\n"; } return EXIT_SUCCESS; }
0
CPP
n,k=map(int,input().split()) H=[int(i) for i in input().split()] k=min(n,k) H.sort() print(sum(H[:n-k]))
0
PYTHON3
#include <bits/stdc++.h> using namespace std; int Pow[45], r[45], tmp[45]; vector<int> g; int main() { int n; long long a; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lld", &a); bool p = true; int t = 0; while (1) { if (a == 1) { if (p) Pow[t + 1]++; else r[t + 1]++; break; } if (a & 1 != 0) p = false; a /= 2; t++; } } bool ans = false, end = false; int last; int num = Pow[1]; int k = 0; while (num - k != 0) { Pow[1] = num - k; if (k) r[1]++; for (int i = 1; i < 44; i++) { if (Pow[i] < Pow[i + 1]) { r[i] += Pow[i + 1] - Pow[i]; Pow[i + 1] = Pow[i]; } } for (int i = 0; i <= 44; i++) tmp[i] = Pow[i]; for (int i = 1; i <= 44; i++) { if (tmp[i] == 0) { last = i - 1; for (int j = i; j <= 44; j++) if (r[j] != 0) { end = true; } break; } } if (end) break; int pre = 0; int sum = 0; for (int i = last; i >= 0; i--) { if (tmp[last] != tmp[i]) { pre = i; sum = 0; for (int j = pre + 1; j <= last; j++) { sum += r[j]; } if (sum > tmp[last]) end = true; for (int j = 1; j <= last; j++) tmp[j] -= sum; last = pre; } } if (end) break; g.push_back(num - k); ans = true; k++; } sort(g.begin(), g.end()); if (!ans) printf("-1\n"); else { for (int i = 0; i < g.size(); i++) printf("%d ", g[i]); } }
9
CPP
'''input 4 3 9099 ''' from collections import defaultdict as dd from collections import Counter as ccd from itertools import permutations as pp from itertools import combinations as cc from random import randint as rd from bisect import bisect_left as bl from bisect import bisect_right as br import heapq as hq from math import gcd ''' Author : dhanyaabhirami Hardwork beats talent if talent doesn't work hard ''' ''' Stuck? See github resources Derive Formula Kmcode blog CP Algorithms Emaxx ''' mod=pow(10,9) +7 def inp(flag=0): if flag==0: return list(map(int,input().strip().split(' '))) else: return int(input()) # Code credits # assert(debug()==true) # for _ in range(int(input())): n,k=inp() s=list(input().strip()) i=0 while i<n and k>0: if i==0: if n==1: s[i]='0' k-=1 elif s[i]!='1': s[i]='1' k-=1 elif s[i]!='0': s[i]='0' k-=1 i+=1 print("".join(s))
8
PYTHON3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:134217728") using namespace std; int n; vector<int> p; int u[1 << 17]; int a[1 << 17], b[1 << 17]; int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); a[i]--; b[a[i]] = i; } for (int i = 2; i < 1 << 17; ++i) { if (!u[i]) { p.push_back(i - 1); for (int j = 2 * i; j < 1 << 17; j += i) u[j] = 1; } } vector<int> A, B; for (int i = 0; i < n; ++i) { int pos = b[i]; while (pos != i) { int L = 0, R = p.size(); while (R - L > 1) { int M = (L + R) / 2; if (pos - p[M] < i) R = M; else L = M; } A.push_back(pos - p[L] + 1); B.push_back(pos + 1); b[a[pos - p[L]]] = pos; b[i] = pos - p[L]; swap(a[pos], a[pos - p[L]]); pos -= p[L]; } } printf("%d\n", A.size()); for (int i = 0; i < A.size(); ++i) printf("%d %d\n", A[i], B[i]); return 0; }
9
CPP
# In peace, in love, in war, everything is the same n = int(input()) for i in range(n) : a, b = map(int, input().split()) if a == b : print(0) elif a%2 != b%2 and a < b : print(1) elif a%2 == b%2 and a > b : print(1) else : print(2)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const long double PI = 3.141592653589793; const long long mod = 1e9 + 7; int CASE = 1; const int mxn = 1e5 + 1; const long long infll = 1e18; const int infi = 1e9; bool prime(long long n) { if (n <= 1) return false; if (n == 2 or n == 3) return true; if (n % 2 == 0 or n % 3 == 0) return false; for (long long i = 5; i * i <= n; i += 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } long long __gcd(long long a, long long b) { return !b ? a : __gcd(b, a % b); } long long power(long long a, long long b) { long long x = a, res = 1, p = b; while (p > 0) { if (p & 1) res *= x; x *= x; p >>= 1; } return res; } void solve() { int n, m; cin >> n >> m; string s; cin >> s; vector<pair<int, int> > pos; char pre = 'L'; if (s.find('W') == -1) { cout << m * 2 - 1 * (m > 0) << '\n'; return; } for (long long i = 0; i < n; i++) { if (s[i] == 'W') { if (pre == 'L') { pre = 'W'; pos.push_back(make_pair(i, i)); } else { pos.back().second++; } } else { pre = 'L'; } } vector<pair<int, int> > v; for (int i = 0; i < pos.size() - 1; i++) { v.push_back( make_pair(pos[i].second + 1, pos[i + 1].first - pos[i].second - 1)); } sort(v.begin(), v.end(), [](pair<int, int> a, pair<int, int> b) { return a.second < b.second; }); for (auto x : v) { if (m >= x.second) { for (int i = x.first; s[i] == 'L'; i++) s[i] = 'W', m--; } else { for (int i = x.first; m > 0; i++, m--) s[i] = 'W'; } } int index = s.find('W') - 1; while (index >= 0 && s[index] == 'L' && m > 0) { s[index] = 'W'; m--; index--; } index = s.find_last_of('W') + 1; while (index < n && s[index] == 'L' && m > 0) { s[index] = 'W'; m--; index++; } int ans = 0; pre = 'C'; for (int i = 0; i < n; i++) { if (s[i] == 'L') pre = 'L'; else { if (pre == 'W') ans += 2; else ans++, pre = 'W'; } } cout << ans << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int T; cin >> T; while (T--) { solve(); } return 0; }
8
CPP
a = input() b = input() for i in range(len(a) - len(b) + 1): f = True for j in range(len(b)): if a[i+j] == b[j]: pass else: if b[j] == '_': pass else: f = False break if f: print('Yes') exit() print('No')
0
PYTHON3
test = int(input()) isPossible = 'NO' for i in range(1, test): another = test - i if i % 2 == 0 and another % 2 == 0: isPossible = 'YES' break print(isPossible)
7
PYTHON3
#include<cstdio> #include<iostream> using namespace std; int n; int a[100005]; int main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } for(int i=n;i>=1;i--){ if(a[i]<a[i-1]){ a[i-1]-=1; if(a[i]<a[i-1]){ cout<<"No"; return 0; } } } cout<<"Yes"; return 0; }
0
CPP
n=int(input()) for i in range (n): a=int(input()) print(int((a//7)+1))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { string line; string codes[10]; getline(cin, line); for (int i = 0; i < 10; i++) { getline(cin, codes[i]); } int start = 0; string ans = ""; while (1) { if (start == 80) break; string tmp = line.substr(start, 10); for (int i = 0; i < 10; i++) { if (tmp == codes[i]) { ans += (char)(i + '0'); break; } } start = start + 10; } cout << ans << endl; return 0; }
7
CPP
from sys import stdin,stdout song = stdin.readline() song= song.split('WUB') print(' '.join(song).strip())
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const int Maxd = 60; const int Maxn = 100005; const int Maxm = 524288; int n; int a[Maxn]; int st[Maxm][Maxd]; int q; void Union(int v) { for (int j = 0; j < Maxd; j++) { int t1 = st[2 * v][j]; int t2 = st[2 * v + 1][(j + t1) % Maxd]; st[v][j] = t1 + t2; } } void Create(int v, int l, int r) { if (l == r) for (int j = 0; j < Maxd; j++) st[v][j] = j % a[l] == 0 ? 2 : 1; else { int m = l + r >> 1; Create(2 * v, l, m); Create(2 * v + 1, m + 1, r); Union(v); } } void Update(int v, int l, int r, int x) { if (l == r) for (int j = 0; j < Maxd; j++) st[v][j] = j % a[l] == 0 ? 2 : 1; else { int m = l + r >> 1; if (x <= m) Update(2 * v, l, m, x); else Update(2 * v + 1, m + 1, r, x); Union(v); } } int Calc(int v, int l, int r, int a, int b, int cur = 0) { if (l == a && r == b) return st[v][cur]; else { int m = l + r >> 1; if (b <= m) return Calc(2 * v, l, m, a, b, cur); if (m + 1 <= a) return Calc(2 * v + 1, m + 1, r, a, b, cur); int t1 = Calc(2 * v, l, m, a, m, cur); cur = (cur + t1) % Maxd; int t2 = Calc(2 * v + 1, m + 1, r, m + 1, b, cur); return t1 + t2; } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); Create(1, 1, n); scanf("%d", &q); char ch; int x, y; while (q--) { scanf(" %c %d %d", &ch, &x, &y); if (ch == 'C') { a[x] = y; Update(1, 1, n, x); } else { y--; printf("%d\n", Calc(1, 1, n, x, y)); } } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n], b[n], c = 0, d = 0; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; if (a[i] == 1 && b[i] == 0) c++; if (a[i] == 0 && b[i] == 1) d++; } if (c > d) cout << 1; else if (c == 0) cout << -1; else { cout << (d / c) + 1; } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { int q, i, j, n, ar[105]; cin >> q; while (q--) { cin >> n; for (i = 0; i < n; i++) { cin >> ar[i]; } sort(ar, ar + n); bool yes = false; for (i = 1; i < n; i++) { if (ar[i - 1] + 1 == ar[i]) yes = true; } if (yes) cout << "2\n"; else cout << "1\n"; } return 0; }
7
CPP
#include <bits/stdc++.h> using namespace std; const long long int inf = 1e9 + 7; const long long int mod = 1e9 + 9; long long int mini(long long int a, long long int b) { if (a >= b) return b; return a; } long long int maxi(long long int a, long long int b) { if (a >= b) return a; return b; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); long long int n; cin >> n; map<long long int, long long int> cnt; long long int ans = 0; vector<long long int> A; for (long long int i = 0; i <= n - 1; i++) { string s; cin >> s; long long int x = 0; for (long long int j = 0; j <= s.length() - 1; j++) { long long int mask = (1LL << (s[j] - 'a')); x ^= mask; } ans += cnt[x]; cnt[x]++; A.push_back(x); } for (long long int i = 0; i <= n - 1; i++) { long long int x = A[i]; for (long long int j = 0; j <= 25; j++) { long long int mask = (1LL << j); if (mask & x) { x ^= mask; ans += cnt[x]; x ^= mask; } } } cout << ans; return 0; }
15
CPP
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main(void) { ll N,M,V,P; cin>>N>>M>>V>>P; vector<ll> A(N); for(int i=0;i<N;i++){ cin>>A[i]; } sort(A.begin(),A.end()); reverse(A.begin(),A.end()); ll res = P; ll sum = A[P-1]; for(ll i=P;i<N;i++){ if(A[P-1]<= A[i]+M){ if((A[i]+M)*(i+1-P)-sum >= M*(V-P-(N-1-i))){ //cout<<(A[i]+M)*(i+1-P)-sum<<" "<<M*(V-P-(N-1-i))<<endl; res++; } else{ break; } } sum += A[i]; } cout<<res<<endl; }
0
CPP
#include <bits/stdc++.h> using namespace std; struct Circle { int x, r, num, n; } cir[10001]; bool cmpx(Circle a, Circle b) { return a.x < b.x; } bool cmpnum(Circle a, Circle b) { return a.num < b.num; } int m, n; bool incir(int x, int y, int nc) { return (x - cir[nc].x) * (x - cir[nc].x) + y * y <= cir[nc].r * cir[nc].r; } int bins(int x) { int l = 0, h = m; while (1 < h - l) { int mid = (l + h) / 2; if (cir[mid].x > x) h = mid; else l = mid; } return l; } int main() { cin >> m; for (int i = 0; i < m; i++) { cin >> cir[i].x >> cir[i].r; cir[i].num = i; cir[i].n = -1; } sort(cir, cir + m, cmpx); int x, y; cin >> n; for (int i = 0; i < n; i++) { cin >> x >> y; int p = bins(x); for (int j = p; j < p + 2 && j < m; j++) if (cir[j].n == -1 && incir(x, y, j)) cir[j].n = i + 1; } int cnt = 0; for (int i = 0; i < m; i++) if (cir[i].n != -1) cnt++; cout << cnt << endl; sort(cir, cir + m, cmpnum); for (int i = 0; i < m; i++) cout << cir[i].n << " "; cout << endl; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; template <typename A, typename B> ostream& operator<<(ostream& out, const pair<A, B>& a) { out << "(" << a.first << "," << a.second << ")"; return out; } template <typename T, size_t N> ostream& operator<<(ostream& out, const array<T, N>& a) { out << "["; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0; } out << "]"; return out; } template <typename T> ostream& operator<<(ostream& out, const vector<T>& a) { out << "["; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0; } out << "]"; return out; } template <typename T, class Cmp> ostream& operator<<(ostream& out, const set<T, Cmp>& a) { out << "{"; bool first = true; for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0; } out << "}"; return out; } template <typename U, typename T, class Cmp> ostream& operator<<(ostream& out, const map<U, T, Cmp>& a) { out << "{"; bool first = true; for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0; } out << "}"; return out; } template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << ": " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << ": " << arg1 << " |"; __f(comma + 1, args...); } void test() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } int s, t; cin >> s >> t; s--; t--; if (s > t) swap(s, t); int ans = 0; for (int i = s; i < t; i++) { ans += a[i]; } int nin = 0; for (int i = 0; i < s; i++) { nin += a[i]; } for (int i = t; i < n; i++) { nin += a[i]; } cout << min(ans, nin) << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t = 1; while (t--) { test(); } return 0; }
7
CPP
def B(): n = int(input()) lst = list(input()) for i in range(len(lst)): if lst[i] == "r": lst[i] = 1 else: lst[i] = -1 rCo = 0 bCo = 0 co = 0 cur = lst[0] if n == 1: print("0") return 0 for i in range(len(lst)): if lst[i] == cur: cur *= -1 continue else: if lst[i] == 1: if bCo != 0: bCo -= 1 else: rCo += 1 co += 1 else: if rCo != 0: rCo -= 1 else: bCo += 1 co += 1 cur *= -1 first = co rCo = 0 bCo = 0 co = 0 cur = lst[0] * -1 for i in range(len(lst)): if lst[i] == cur: cur *= -1 continue else: if lst[i] == 1: if bCo != 0: bCo -= 1 else: rCo += 1 co += 1 else: if rCo != 0: rCo -= 1 else: bCo += 1 co += 1 cur *= -1 print(min(co, first)) return 0 if __name__ == "__main__": B()
8
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int m, n, l; cin >> m >> n; l = m * n; cout << l / 2; }
7
CPP
a, b = map(int, input().split()) if (a == b and a != 0) or (a == b-1) or (a == b + 1) or (b == 1 and a == 0): print("YES") else: print('NO')
7
PYTHON3
#include <stdio.h> #include <bits/stdc++.h> #define mod 1000000007 typedef long long ll; using namespace std; int n; char s[2][60]; int main() { cin>>n>>s[0]>>s[1]; ll ans=3; int k=0,j=1; if(s[0][0]==s[0][1]){ k=1;j=2; ans=6; } for(;j<n;j++){ if(s[0][j]==s[0][j+1]){ if(k==1) ans*=3; else ans*=2; k=1;j++; }else{ if(k==0) ans*=2; k=0; } ans%=mod; } cout<<ans<<endl; return 0; } /* 1 23 2 1 3 1 1 3 1 2 2 1 */
0
CPP
n = int(input()) s = list(map(int, input().rstrip().split())) dp = [0] for x in s: dp += [dp[-1] + x] q = int(input()) for _ in range(q): l, r = map(int, input().split()) sums = dp[r] - dp[l - 1] print(sums // 10)
9
PYTHON3
n=int(input()) a=list(map(int,input())) ans=[]; oof=[0,0,[2],[3],[2,2,3],[5],[3,5],[7],[2,2,2,7],[2,3,3,7]] for item in a: if item==1 or item==0: continue ans+=oof[item] ans.sort(reverse=True) print(''.join(str(item) for item in ans))
7
PYTHON3
from collections import Counter # from collections import OrderedDict nn, i = map(int, input().strip().split(' ')) a = [int(x) for x in input().strip().split(' ')] ad = dict(Counter(a)) # print(ad) a = sorted(ad) # print(ad) k = 8*i // nn storage = 2 ** min(20, k) if len(Counter(a).keys()) <= storage: print(0) else: n = len(a) sm = 0 i = 0 j = storage while i < j: sm = sm + ad[a[i]] i = i+1 msum = sm i = 0 while j < n: sm = sm - ad[a[i]] + ad[a[j]] i = i+1 j = j+1 msum = max(sm, msum) print(nn-msum)
7
PYTHON3
p = int(input()) seq = list(map(int,input().split())) total = 0 to_check = set(x+1 for x in range(p)) ind = 0 checked = [False for _ in range(p*2)] while to_check: current = seq[ind] checked[ind] = True for x in range(ind+1,p*2): if seq[x] == current: checked[x] = True break elif not checked[x]: total+=1 to_check.remove(current) for other in range(ind+1,p*2): if not checked[other]: ind = other break print (total)
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAXN = 1510; const long long MOD = 1e9 + 7; inline long long in() { long long x = 0, flag = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') flag = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar(); return x * flag; } long long n, m; struct Matrix { int R, C; long long val[110][110]; Matrix() {} Matrix(int _R, int _C) { R = _R, C = _C; memset(val, 0, sizeof val); } Matrix(int _R, int _C, int flag) { R = _R, C = _C; memset(val, 0, sizeof val); for (int i = 1; i <= 101; i++) val[i][i] = 1LL; } Matrix operator*(const Matrix b) const { Matrix ret(R, b.C); for (int i = 1; i <= R; i++) for (int j = 1; j <= b.C; j++) for (int k = 1; k <= C; k++) (ret.val[i][j] += val[i][k] * b.val[k][j]) %= MOD; return ret; } void print() { puts("---"); for (int i = 1; i <= R; i++, puts("")) for (int j = 1; j <= C; printf("%lld", val[i][j]), j++) ; } } Dp, Tr; Matrix fpow(Matrix A, long long B) { if (!B) return A; Matrix ret(A.R, A.C, 1); while (B) { if (B % 2 == 1) ret = A * ret, B--; else A = A * A, B /= 2; } return ret; } long long ans = 1; long long cnt[100010]; int main() { n = in(); m = in(); for (int i = 1; i <= n; i++) cnt[in()]++; Dp.val[100][1] = Dp.val[101][1] = 1LL; long long d = min(m, 100 * 1ll); d = 100; Dp.R = d + 1, Dp.C = 1; Tr.R = d + 1, Tr.C = d + 1; for (int i = 1; i < d; i++) Tr.val[i][i + 1] = 1LL; for (int i = 1; i <= d; i++) { Tr.val[d][d - i + 1] = Tr.val[d + 1][d - i + 1] = cnt[i]; } Tr.val[d + 1][d + 1] = 1LL; Tr = fpow(Tr, m); if (m) Dp = Tr * Dp; printf("%lld\n", Dp.val[101][1]); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; const int MAX = 3010; int n, m, p[MAX]; bool odw[MAX]; void wczytaj_dane() { cin >> n; for (int i = 0; i < (n); i++) { cin >> p[i]; p[i]--; } cin >> m; } int zlicz_cykle() { fill(odw, odw + n, false); int licz = 0; for (int i = 0; i < (n); i++) if (!odw[i]) { odw[i] = true; while (!odw[p[i]]) odw[i = p[i]] = true; i = p[i]; licz++; } return licz; } void wyznacz_minima(vector<int> &w) { fill(odw, odw + n, false); for (int i = 0; i < (n); i++) if (!odw[i]) { odw[i] = true; int mini = i; while (!odw[p[i]]) { mini = min(mini, i = p[i]); odw[i] = true; } i = p[i]; w.push_back(mini); } } void zlacz_cykle(int ile) { cout << ile << '\n'; vector<int> w; wyznacz_minima(w); sort((w).begin(), (w).end()); assert(ile < int((w).size())); for (int i = (1); i <= (ile); i++) cout << "1 " << w[i] + 1 << ' '; } int najmniejszy_niestaly() { for (int i = 0; i < (n); i++) if (p[i] != i) return i; return 0; assert(false); } int najmniejszy_w_cyklu(int a) { int b = n, c = p[a]; while (c != a) { b = min(b, c); c = p[c]; } assert(a < b); return b; } void rozdziel_cykle(int ile) { cout << ile << '\n'; while (ile--) { int a = najmniejszy_niestaly(); int b = najmniejszy_w_cyklu(a); cout << a + 1 << ' ' << b + 1 << ' '; swap(p[a], p[b]); } } void rozwiaz() { int c = zlicz_cykle(); if (c == n - m) cout << "0\n\n"; else if (c > n - m) zlacz_cykle(c - (n - m)); else rozdziel_cykle((n - m) - c); } void zrob_test() { wczytaj_dane(); rozwiaz(); } int main() { ios_base::sync_with_stdio(0); zrob_test(); return 0; }
10
CPP
#include<bits/stdc++.h> using namespace std; struct node { int l,r; }a[100003]; int n,minn=2e9+3,x,l,ans,ans1,maxx=-2e9; int cmp(node nx,node ny) { return nx.r<ny.r; } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d%d",&x,&l); a[i].l=x-l,a[i].r=x+l; } sort(a+1,a+n+1,cmp); for(int i=1;i<=n;i++) if(a[i].l>=maxx) ans++,maxx=max(a[i].r,maxx); cout<<ans; return 0; }
0
CPP
t = input() re_t = t.replace("?", "D") print(re_t)
0
PYTHON3
t = int(input()) while t != 0: n = int(input()) a = list(map(int, input().split()))[:n] b = list(map(int, input().split()))[:n] net1 = sorted(a) net2 = sorted(b) print(*net1) print(*net2) t -= 1
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long N = 555; long long n, res[N][N], c = 1; void snake() { for (long long i = n - 1; i; i--) { if (i & 1) { for (long long j = n - 1; j >= 0; j--) res[i][j] = c++; } else { for (long long j = 0; j < n; ++j) res[i][j] = c++; } if (i == n - 2) c--; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(40); cin >> n; if (n <= 2) { cout << -1; return 0; } if (n == 3) { cout << "5 6 8\n2 3 4\n1 9 7"; return 0; } bool f = 0; if (n % 2) { for (long long i = 0; i < n; ++i) { cout << c << ' '; c++; } cout << '\n'; c = 2 * n; f = 1; n--; } snake(); for (long long i = 1; i < n; ++i) res[0][i] = c++; res[0][0] = c++; res[n - 2][n - 1] = c; for (long long i = 0; i < n; ++i) { for (long long j = 0; j < n; ++j) cout << res[i][j] << ' '; if (f) cout << (n + 1) + i + 1; cout << '\n'; } }
11
CPP
#include <bits/stdc++.h> using namespace std; int GCD(int a, int b) { if (!a) return b; return GCD(b % a, a); } long long A(long long a) { return a > 0 ? a : -a; } vector<long long> h; map<long long, int> compx; vector<int> ans, par; struct node { int mx = 0; int idx = -1; void merge(const node &l, const node &r) { if (l.mx < r.mx) { mx = r.mx; idx = r.idx; } else { mx = l.mx; idx = l.idx; } } }; node tree[(100100 * 3) * 4 + 1]; void update(int root, int st, int en, int pos, int val) { if (st == en) { assert(st == pos); tree[root].mx = val; tree[root].idx = pos; return; } int mid = (st + en) / 2; if (pos <= mid) { update(root * 2, st, mid, pos, val); } else { update(root * 2 + 1, mid + 1, en, pos, val); } tree[root].merge(tree[root * 2], tree[root * 2 + 1]); } node FAIL; node query(int root, int st, int en, int l, int r) { if (st > r || en < l) return FAIL; if (l <= st && en <= r) { return tree[root]; } int mid = (st + en) / 2; node left = query(root * 2, st, mid, l, r); node right = query(root * 2 + 1, mid + 1, en, l, r); node ret; ret.merge(left, right); return ret; } int main() { clock_t startTime = clock(); ios_base::sync_with_stdio(false); int n, d; cin >> n >> d; h.reserve(n); ans.resize(n); par.resize(n); for (int i = (0); i < (int)(n); i++) { long long z; cin >> z; h.push_back(z); compx[z] = 0; compx[z - d] = 0; compx[z + d] = 0; } int cnt = 0; for (__typeof(compx.begin()) it = compx.begin(); it != compx.end(); it++) { it->second = cnt++; } ans[0] = 1; par[0] = 0; update(1, 0, cnt - 1, compx[h[0]], 1); for (int i = (1); i < (int)(n); i++) { long long curval = h[i]; int mapval = compx[curval]; int left = 0; int right = compx[curval - d]; node mn = query(1, 0, cnt - 1, left, right); int cur = 1, p = i; if (mn.idx != -1) { if (cur < mn.mx + 1) { cur = mn.mx + 1; p = mn.idx; } } left = compx[curval + d]; right = cnt - 1; mn = query(1, 0, cnt - 1, left, right); if (mn.idx != -1) { if (cur < mn.mx + 1) { cur = mn.mx + 1; p = mn.idx; } } ans[i] = cur; par[i] = p; update(1, 0, cnt - 1, compx[h[i]], cur); } int imx = max_element(ans.begin(), ans.end()) - ans.begin(); int mx = ans[imx]; cout << mx << "\n"; stack<int> out; long long last = (1LL << 62); for (int i = n - 1; i >= 0; i--) { if (ans[i] == mx && abs(h[i] - last) >= d) { last = h[i]; mx -= 1; out.push(i + 1); } } while (!out.empty()) { cout << out.top() << " "; out.pop(); } clock_t endTime = clock(); cerr << "\nTime:" << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds\n"; return 0; }
11
CPP
from fractions import gcd n, a, v = int(input()), list(map(int, input().split())), [] for i, ai in enumerate(a): v.append(ai) if i < n - 1 and gcd(ai, a[i + 1]) > 1: v.append(1) print(len(v) - len(a)) print(' '.join(map(str, v)))
7
PYTHON3
#include <bits/stdc++.h> using namespace std; struct arr { int l, r; double p; } a[5005]; int f[100005][18], mx[100005], used[100005], n, i, j, T, m, k; double ans[5005][5005], tmp[5005], ANS; inline int ask(int x, int y) { int len = (int)log2(y - x + 1); return max(f[x][len], f[y - (1 << len) + 1][len]); } inline int cmp(const arr &a, const arr &b) { return a.r - a.l < b.r - b.l; } int main() { scanf("%d%d", &n, &m); for (i = 1; i <= n; i++) scanf("%d", &f[i][0]); for (j = 1; j <= 17; j++) for (i = 1; i <= n; i++) T = i + (1 << (j - 1)), f[i][j] = max(f[i][j - 1], (T <= n) ? f[T][j - 1] : 0); for (i = 1; i <= m; i++) scanf("%d%d%lf", &a[i].l, &a[i].r, &a[i].p); a[++m] = (arr){1, n, 0}; sort(a + 1, a + m + 1, cmp); for (i = 1; i <= m; i++) { mx[i] = ask(a[i].l, a[i].r); for (k = 0; k <= m; k++) tmp[k] = 1.0; for (j = 1; j < i; j++) if (a[j].l >= a[i].l && a[j].r <= a[i].r && !used[j]) { used[j] = 1; for (k = 0; k <= m; k++) if (mx[i] + k - mx[j] <= m) tmp[k] *= ans[j][mx[i] + k - mx[j]]; } for (k = m; k; k--) tmp[k] -= tmp[k - 1]; ans[i][0] = (1 - a[i].p) * tmp[0]; for (k = 1; k <= m; k++) ans[i][k] = ans[i][k - 1] + tmp[k - 1] * a[i].p + tmp[k] * (1 - a[i].p); } ANS = ans[m][0] * mx[m]; for (i = 1; i <= m; i++) ANS += (ans[m][i] - ans[m][i - 1]) * (mx[m] + i); printf("%.10lf", ANS); }
9
CPP
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { return b ? gcd(b, a % b) : a; } long long int lcm(long long int a, long long int b) { return a * b / gcd(a, b); } int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; vector<int> ad[33]; vector<pair<long long int, long long int>> ans; int n; void dfs(int v, int pa, int dir, long long int x, long long int y, long long int step) { ans[v] = {x, y}; for (auto u : ad[v]) { if (u == pa) continue; dfs(u, v, (dir + 3) % 4, x + dx[dir] * step, y + dy[dir] * step, step >> 1); dir = (dir + 1) % 4; } } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int x, y; scanf("%d%d", &x, &y); ad[x].push_back(y); ad[y].push_back(x); } for (int i = 1; i <= n; i++) if (ad[i].size() > 4) return 0 * puts("NO"); ans.resize(n + 1); puts("YES"); dfs(1, 0, 0, 0, 0, 1ll << 40); for (int i = 1; i <= n; i++) printf("%lld %lld\n", ans[i].first, ans[i].second); return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; int a[1000010], t[5000000], n, m; int create(int u, int l, int h, int& ch) { if (l == h) { ch = 2; t[u] = a[l]; return t[u]; } int z, zz; int c = create(2 * u, l, (l + h) / 2, z); int d = create(2 * u + 1, (l + h) / 2 + 1, h, zz); if (z == 1) { ch = 2; return t[u] = c ^ d; } ch = 1; return t[u] = c | d; } int query(int u, int l, int h, int y, int val, int& ch) { if (y > h || y < l) { return t[u]; } if (l == h) { ch = 2; t[u] = val; return t[u]; } int z; int c = query(2 * u, l, (l + h) / 2, y, val, z); int d = query(2 * u + 1, (l + h) / 2 + 1, h, y, val, z); if (z == 1) { ch = 2; t[u] = c ^ d; return t[u]; } ch = 1; t[u] = c | d; return t[u]; } int main() { scanf("%d%d", &n, &m); int zz, x, y; for (int i = 1; i <= (1 << n); i++) scanf("%d", &a[i]); create(1, 1, 1 << n, zz); while (m--) { scanf("%d%d", &x, &y); int zz; printf("%d\n", query(1, 1, 1 << n, x, y, zz)); } return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; struct pt { int x, y; int id; pt() : x(0), y(0) {} bool operator<(const pt &p2) const { if (y != p2.y) return y > p2.y; return x < p2.x; } }; int main() { int n, m; while (scanf("%d%d", &n, &m) >= 1) { vector<pt> pts(m); for (int i = 0; i < m; i++) scanf("%d%d", &pts[i].x, &pts[i].y), pts[i].x--, pts[i].y--, pts[i].id = i; sort(pts.begin(), pts.end()); vector<int> res; int ptr = 0; set<pair<pair<int, int>, int> > xs; for (int y = n - 1; y >= 0; y--) { int x = n - 1 - y; while (ptr < ((int)(pts).size()) && pts[ptr].y >= y) xs.insert(make_pair(make_pair(pts[ptr].x, -pts[ptr].y), ptr)), ptr++; set<pair<pair<int, int>, int> >::iterator it = xs.lower_bound(make_pair(make_pair(x, -1e9), -1)); if (it == xs.end()) continue; int i = it->second; xs.erase(it); res.push_back(pts[i].id); } printf("%d\n", ((int)(res).size())); for (int i = 0; i < ((int)(res).size()); i++) printf("%d%c", res[i] + 1, "\n "[i + 1 < ((int)(res).size())]); } return 0; }
11
CPP
#include <bits/stdc++.h> using namespace std; long long dp[1 << 20]; const long long inf = 4000000000LL * 1000000000LL; struct triple { long long a, b, c; triple() {} triple(int a, int b, int c) : a(a), b(b), c(c) {} bool operator<(const triple& B) const { return b < B.b; } }; triple T[100]; int main() { int n, m; long long b; scanf("%d%d%lld", &n, &m, &b); for (int i = 0; i < n; i++) { int k; scanf("%lld%lld%d", &T[i].a, &T[i].b, &k); while (k--) { int a; scanf("%d", &a); T[i].c += (1 << (a - 1)); } } long long ans = inf; for (int i = 1; i < (1 << m); i++) dp[i] = inf; sort(T, T + n); for (int i = 0; i < n; i++) { for (int j = 0; j < (1 << m); j++) dp[j | T[i].c] = min(dp[j | T[i].c], dp[j] + T[i].a); ans = min(ans, dp[(1 << m) - 1] + T[i].b * b); } printf("%lld\n", ans >= inf ? -1LL : ans); return 0; }
8
CPP
N = int(input()) S = [input() for _ in range(N)] dic = {} X = sorted(list(set(S[0]))) for i in X: dic[i] = 51 for i in range(N): for x in X: dic[x] = min(dic[x],S[i].count(x)) for i in X: print(i * dic[i],end="")
0
PYTHON3
#include <bits/stdc++.h> using namespace std; bool bs(vector<int> &v, int l, int r) { int low = 0, high = v.size() - 1; while (low <= high) { int mid = (low + high) / 2; if (v[mid] >= l and v[mid] <= r) return true; else if (v[mid] > l and v[mid] > r) high = mid - 1; else low = mid + 1; } return false; } int main() { string s; cin >> s; vector<set<int> > v(26); for (int i = 0; i < s.length(); i++) { int p = s[i] % 97; v[p].insert(i); } int q; cin >> q; while (q--) { int x; cin >> x; if (x == 1) { int pos; char ch; cin >> pos >> ch; pos -= 1; char c = s[pos]; int p = c % 97; auto it = v[p].find(pos); if (it != v[p].end()) v[p].erase(it); p = ch % 97; v[p].insert(pos); s[pos] = ch; } else { int l, r; cin >> l >> r; l -= 1; r -= 1; int c = 0; for (int j = 0; j < 26; j++) { auto it = v[j].lower_bound(l); if (it != v[j].end() and *it <= r) c++; } cout << c << endl; } } }
10
CPP
i=0 while 1: a=int(input()) if(a==0): break i+=1 print("Case {0}: {1}".format(i,a))
0
PYTHON3