solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:20000000") using namespace std; int ri() { int x; scanf("%d", &x); return x; } long long rll() { long long x; scanf("%lld", &x); return x; } int n, u, r; int best_res[35]; long long ans; int a[35]; int b[35]; int p[35]; int k[35]; void go(int pos, bool ok) { if (pos == u + 1 || (u - pos) % 2 == 1) { long long now = 0; for (int i = (int)(1); i <= (int)(n); i++) now += (long long)a[i] * k[i]; if (now > ans) ans = now; } if (pos == u + 1) return; if (!ok) { int *temp = new int[n + 1]; for (int i = (int)(1); i <= (int)(n); i++) temp[i] = a[i]; for (int i = (int)(1); i <= (int)(n); i++) a[i] ^= b[i]; go(pos + 1, true); for (int i = (int)(1); i <= (int)(n); i++) a[i] = temp[i]; delete[] temp; } int *temp = new int[n + 1]; for (int i = (int)(1); i <= (int)(n); i++) temp[i] = a[i]; for (int i = (int)(1); i <= (int)(n); i++) a[i] = temp[p[i]] + r; go(pos + 1, false); for (int i = (int)(1); i <= (int)(n); i++) a[i] = temp[i]; delete[] temp; } void solve() { ans = -2e16; n = ri(), u = ri(), r = ri(); for (int i = (int)(1); i <= (int)(n); i++) a[i] = ri(); for (int i = (int)(1); i <= (int)(n); i++) b[i] = ri(); for (int i = (int)(1); i <= (int)(n); i++) k[i] = ri(); for (int i = (int)(1); i <= (int)(n); i++) p[i] = ri(); int zero = 0; for (int i = (int)(1); i <= (int)(n); i++) zero += a[i] < 0; go(1, false); cout << ans << endl; } int main() { solve(); return 0; }
2
#include<bits/stdc++.h> using namespace std; using Int = long long; #define EPS (1e-10) #define equals(a,b) (fabs((a)-(b)) < EPS) #define PI 3.141592653589793238 // COUNTER CLOCKWISE static const int CCW_COUNTER_CLOCKWISE = 1; static const int CCW_CLOCKWISE = -1; static const int CCW_ONLINE_BACK = 2; static const int CCW_ONLINE_FRONT = -2; static const int CCW_ON_SEGMENT = 0; //Intercsect Circle & Circle static const int ICC_SEPERATE = 4; static const int ICC_CIRCUMSCRIBE = 3; static const int ICC_INTERSECT = 2; static const int ICC_INSCRIBE = 1; static const int ICC_CONTAIN = 0; struct Point{ double x,y; Point(){} Point(double x,double y) :x(x),y(y){} Point operator+(Point p) {return Point(x+p.x,y+p.y);} Point operator-(Point p) {return Point(x-p.x,y-p.y);} Point operator*(double k){return Point(x*k,y*k);} Point operator/(double k){return Point(x/k,y/k);} double norm(){return x*x+y*y;} double abs(){return sqrt(norm());} bool operator < (const Point &p) const{ return x!=p.x?x<p.x:y<p.y; //grid-point only //return !equals(x,p.x)?x<p.x:!equals(y,p.y)?y<p.y:0; } bool operator == (const Point &p) const{ return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS; } }; istream &operator >> (istream &is,Point &p){ is>>p.x>>p.y; return is; } ostream &operator << (ostream &os,Point p){ os<<fixed<<setprecision(12)<<p.x<<" "<<p.y; return os; } bool sort_x(Point a,Point b){ return a.x!=b.x?a.x<b.x:a.y<b.y; } bool sort_y(Point a,Point b){ return a.y!=b.y?a.y<b.y:a.x<b.x; } typedef Point Vector; typedef vector<Point> Polygon; struct Segment{ Point p1,p2; Segment(){} Segment(Point p1, Point p2):p1(p1),p2(p2){} }; typedef Segment Line; istream &operator >> (istream &is,Segment &s){ is>>s.p1>>s.p2; return is; } struct Circle{ Point c; double r; Circle(){} Circle(Point c,double r):c(c),r(r){} }; istream &operator >> (istream &is,Circle &c){ is>>c.c>>c.r; return is; } double norm(Vector a){ return a.x*a.x+a.y*a.y; } double abs(Vector a){ return sqrt(norm(a)); } double dot(Vector a,Vector b){ return a.x*b.x+a.y*b.y; } double cross(Vector a,Vector b){ return a.x*b.y-a.y*b.x; } bool isOrthogonal(Vector a,Vector b){ return equals(dot(a,b),0.0); } bool isOrthogonal(Point a1,Point a2,Point b1,Point b2){ return isOrthogonal(a1-a2,b1-b2); } bool isOrthogonal(Segment s1,Segment s2){ return equals(dot(s1.p2-s1.p1,s2.p2-s2.p1),0.0); } bool isParallel(Vector a,Vector b){ return equals(cross(a,b),0.0); } bool isParallel(Point a1,Point a2,Point b1,Point b2){ return isParallel(a1-a2,b1-b2); } bool isParallel(Segment s1,Segment s2){ return equals(cross(s1.p2-s1.p1,s2.p2-s2.p1),0.0); } Point project(Segment s,Point p){ Vector base=s.p2-s.p1; double r=dot(p-s.p1,base)/norm(base); return s.p1+base*r; } Point reflect(Segment s,Point p){ return p+(project(s,p)-p)*2.0; } double arg(Vector p){ return atan2(p.y,p.x); } Vector polar(double a,double r){ return Point(cos(r)*a,sin(r)*a); } int ccw(Point p0,Point p1,Point p2); bool intersectSS(Point p1,Point p2,Point p3,Point p4); bool intersectSS(Segment s1,Segment s2); int intersectCC(Circle c1,Circle c2); bool intersectSC(Segment s,Circle c); double getDistanceLP(Line l,Point p); double getDistanceSP(Segment s,Point p); double getDistanceSS(Segment s1,Segment s2); Point getCrossPointSS(Segment s1,Segment s2); Point getCrossPointLL(Line l1,Line l2); Polygon getCrossPointCL(Circle c,Line l); Polygon getCrossPointCC(Circle c1,Circle c2); int contains(Polygon g,Point p); Polygon andrewScan(Polygon s); Polygon convex_hull(Polygon ps); double diameter(Polygon s); bool isConvex(Polygon p); double area(Polygon s); Polygon convexCut(Polygon p,Line l); Line bisector(Point p1,Point p2); Vector translate(Vector v,double theta); vector<Line> corner(Line l1,Line l2); int ccw(Point p0,Point p1,Point p2){ Vector a = p1-p0; Vector b = p2-p0; if(cross(a,b) > EPS) return CCW_COUNTER_CLOCKWISE; if(cross(a,b) < -EPS) return CCW_CLOCKWISE; if(dot(a,b) < -EPS) return CCW_ONLINE_BACK; if(a.norm()<b.norm()) return CCW_ONLINE_FRONT; return CCW_ON_SEGMENT; } bool intersectSS(Point p1,Point p2,Point p3,Point p4){ return (ccw(p1,p2,p3)*ccw(p1,p2,p4) <= 0 && ccw(p3,p4,p1)*ccw(p3,p4,p2) <= 0 ); } bool intersectSS(Segment s1,Segment s2){ return intersectSS(s1.p1,s1.p2,s2.p1,s2.p2); } int intersectCC(Circle c1,Circle c2){ if(c1.r<c2.r) swap(c1,c2); double d=abs(c1.c-c2.c); double r=c1.r+c2.r; if(equals(d,r)) return ICC_CIRCUMSCRIBE; if(d>r) return ICC_SEPERATE; if(equals(d+c2.r,c1.r)) return ICC_INSCRIBE; if(d+c2.r<c1.r) return ICC_CONTAIN; return ICC_INTERSECT; } bool intersectSC(Segment s,Circle c){ double d=getDistanceSP(s,c.c); return d<=c.r; } double getDistanceLP(Line l,Point p){ return abs(cross(l.p2-l.p1,p-l.p1)/abs(l.p2-l.p1)); } double getDistanceSP(Segment s,Point p){ if(dot(s.p2-s.p1,p-s.p1) < 0.0 ) return abs(p-s.p1); if(dot(s.p1-s.p2,p-s.p2) < 0.0 ) return abs(p-s.p2); return getDistanceLP(s,p); } double getDistanceSS(Segment s1,Segment s2){ if(intersectSS(s1,s2)) return 0.0; return min(min(getDistanceSP(s1,s2.p1),getDistanceSP(s1,s2.p2)), min(getDistanceSP(s2,s1.p1),getDistanceSP(s2,s1.p2))); } Point getCrossPointSS(Segment s1,Segment s2){ Vector base=s2.p2-s2.p1; double d1=abs(cross(base,s1.p1-s2.p1)); double d2=abs(cross(base,s1.p2-s2.p1)); double t=d1/(d1+d2); return s1.p1+(s1.p2-s1.p1)*t; } Point getCrossPointLL(Line l1,Line l2){ double a=cross(l1.p2-l1.p1,l2.p2-l2.p1); double b=cross(l1.p2-l1.p1,l1.p2-l2.p1); if(abs(a)<EPS&&abs(b)<EPS) return l2.p1; return l2.p1+(l2.p2-l2.p1)*(b/a); } Polygon getCrossPointCL(Circle c,Line l){ Polygon p(2); Vector pr=project(l,c.c); Vector e=(l.p2-l.p1)/abs(l.p2-l.p1); double base=sqrt(c.r*c.r-norm(pr-c.c)); p[0]=pr+e*base; p[1]=pr-e*base; return p; } Polygon getCrossPointCC(Circle c1,Circle c2){ Polygon p(2); double d=abs(c1.c-c2.c); double a=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d)); double t=arg(c2.c-c1.c); p[0]=c1.c+polar(c1.r,t+a); p[1]=c1.c+polar(c1.r,t-a); return p; } // IN:2 ON:1 OUT:0 int contains(Polygon g,Point p){ int n=g.size(); bool x=false; for(int i=0;i<n;i++){ Point a=g[i]-p,b=g[(i+1)%n]-p; if(fabs(cross(a,b)) < EPS && dot(a,b) < EPS) return 1; if(a.y>b.y) swap(a,b); if(a.y < EPS && EPS < b.y && cross(a,b) > EPS ) x = !x; } return (x?2:0); } Polygon andrewScan(Polygon s){ Polygon u,l; if(s.size()<3) return s; sort(s.begin(),s.end()); u.push_back(s[0]); u.push_back(s[1]); l.push_back(s[s.size()-1]); l.push_back(s[s.size()-2]); for(int i=2;i<(int)s.size();i++){ for(int n=u.size();n>=2&&ccw(u[n-2],u[n-1],s[i])!=CCW_CLOCKWISE;n--){ u.pop_back(); } u.push_back(s[i]); } for(int i=s.size()-3;i>=0;i--){ for(int n=l.size();n>=2&&ccw(l[n-2],l[n-1],s[i])!=CCW_CLOCKWISE;n--){ l.pop_back(); } l.push_back(s[i]); } reverse(l.begin(),l.end()); for(int i=u.size()-2;i>=1;i--) l.push_back(u[i]); return l; } Polygon convex_hull(Polygon ps){ int n=ps.size(); sort(ps.begin(),ps.end(),sort_y); int k=0; Polygon qs(n*2); for(int i=0;i<n;i++){ while(k>1&&cross(qs[k-1]-qs[k-2],ps[i]-qs[k-1])<0) k--; qs[k++]=ps[i]; } for(int i=n-2,t=k;i>=0;i--){ while(k>t&&cross(qs[k-1]-qs[k-2],ps[i]-qs[k-1])<0) k--; qs[k++]=ps[i]; } qs.resize(k-1); return qs; } double diameter(Polygon s){ Polygon p=s; int n=p.size(); if(n==2) return abs(p[0]-p[1]); int i=0,j=0; for(int k=0;k<n;k++){ if(p[i]<p[k]) i=k; if(!(p[j]<p[k])) j=k; } double res=0; int si=i,sj=j; while(i!=sj||j!=si){ res=max(res,abs(p[i]-p[j])); if(cross(p[(i+1)%n]-p[i],p[(j+1)%n]-p[j])<0.0){ i=(i+1)%n; }else{ j=(j+1)%n; } } return res; } bool isConvex(Polygon p){ bool f=1; int n=p.size(); for(int i=0;i<n;i++){ int t=ccw(p[(i+n-1)%n],p[i],p[(i+1)%n]); f&=t!=CCW_CLOCKWISE; } return f; } double area(Polygon s){ double res=0; for(int i=0;i<(int)s.size();i++){ res+=cross(s[i],s[(i+1)%s.size()])/2.0; } return abs(res); } Polygon convexCut(Polygon p,Line l){ Polygon q; for(int i=0;i<(int)p.size();i++){ Point a=p[i],b=p[(i+1)%p.size()]; if(ccw(l.p1,l.p2,a)!=-1) q.push_back(a); if(ccw(l.p1,l.p2,a)*ccw(l.p1,l.p2,b)<0) q.push_back(getCrossPointLL(Line(a,b),l)); } return q; } Line bisector(Point p1,Point p2){ Circle c1=Circle(p1,abs(p1-p2)),c2=Circle(p2,abs(p1-p2)); Polygon p=getCrossPointCC(c1,c2); if(cross(p2-p1,p[0]-p1)>0) swap(p[0],p[1]); return Line(p[0],p[1]); } Vector translate(Vector v,double theta){ Vector res; res.x=cos(theta)*v.x-sin(theta)*v.y; res.y=sin(theta)*v.x+cos(theta)*v.y; return res; } vector<Line> corner(Line l1,Line l2){ vector<Line> res; if(isParallel(l1,l2)){ double d=getDistanceLP(l1,l2.p1)/2.0; Vector v1=l1.p2-l1.p1; v1=v1/v1.abs()*d; Point p=l2.p1+translate(v1,90.0*(PI/180.0)); double d1=getDistanceLP(l1,p); double d2=getDistanceLP(l2,p); if(abs(d1-d2)>d){ p=l2.p1+translate(v1,-90.0*(PI/180.0)); } res.push_back(Line(p,p+v1)); }else{ Point p=getCrossPointLL(l1,l2); Vector v1=l1.p2-l1.p1,v2=l2.p2-l2.p1; v1=v1/v1.abs(); v2=v2/v2.abs(); res.push_back(Line(p,p+(v1+v2))); res.push_back(Line(p,p+translate(v1+v2,90.0*(PI/180.0)))); } return res; } Polygon tangent(Circle c1,Point p2){ Circle c2=Circle(p2,sqrt(norm(c1.c-p2)-c1.r*c1.r)); Polygon p=getCrossPointCC(c1,c2); sort(p.begin(),p.end()); return p; } struct beet{ int l,r; double x,h; beet(int l,int r,double x,double h):l(l),r(r),x(x),h(h){} bool operator<(const beet&a)const{ return x<a.x; } }; signed main(){ int n; while(cin>>n,n){ vector<Circle> vc(n); for(int i=0;i<n;i++){ double x,r; cin>>x>>r; vc[i]=Circle(Point(x,0),r); } auto check=[&](double A){ double a=A/2.0; //cout<<a<<endl; map<double,int> m; for(int i=0;i<n;i++){ if(vc[i].r<a) continue; double d=sqrt(vc[i].r*vc[i].r-a*a); double l=vc[i].c.x-d,r=vc[i].c.x+d; m[l]++;m[r]--; } double res=0,tmp=0,pos=-1e9; int cur=0; for(auto p:m){ double x=p.first; if(cur) tmp+=x-pos; else tmp=0; pos=x; res=max(res,tmp); cur+=p.second; } return res>=A; }; double l=0,r=1e9; for(int i=0;i<50;i++){ double m=(l+r)/2; if(check(m)) l=m; else r=m; } cout<<fixed<<setprecision(15)<<l<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, q; int val[2 * 100100]; vector<int> adj[2 * 100100]; bool lev[2 * 100100]; int par[2 * 100100]; int st[2 * 100100], en[2 * 100100]; int fen[2 * 100100]; int num; void dfs(int u, int pa, bool l) { lev[u] = l; par[u] = pa; st[u] = ++num; for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; if (v == pa) continue; dfs(v, u, !l); } en[u] = num; } void up(int st, int val) { while (st <= n + 1) { fen[st] += val; st += (st & -st); } return; } int query(int e) { int sum = 0; while (e) { sum += fen[e]; e -= (e & -e); } return sum; } int main() { cin >> n >> q; for (int i = 1; i <= n; i++) { cin >> val[i]; } for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } dfs(1, 0, 0); while (q--) { int a, no, co; cin >> a >> no; if (a == 1) { cin >> co; int s, e; s = st[no]; e = en[no]; if (lev[no]) { up(s, -co); up(e + 1, co); } else { up(s, co); up(e + 1, -co); } } else { cout << val[no] + (lev[no] ? -1 : +1) * query(st[no]) << endl; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; int main() { int n; cin >> n; long long dp[n + 1][n + 1]; dp[0][0] = 1; for (int i = 1; i <= n; i++) { dp[i][0] = dp[i - 1][i - 1]; for (int j = 1; j <= i; j++) { dp[i][j] = (dp[i][j - 1] + dp[i - 1][j - 1]) % mod; } } cout << dp[n][n - 1] << endl; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 50; int n; double ABS(double x) { return x < 0 ? -x : x; } double a[maxn]; int A[maxn]; int main() { cin >> n; int sum = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; A[i] = int(a[i]); sum += A[i]; } if (sum > 0) { for (int i = 1; i <= n; i++) { if (ABS(1.0 * A[i] - a[i] - 1.0) < 1.0) A[i]--, sum--; if (sum == 0) break; } } else if (sum < 0) { for (int i = 1; i <= n; i++) { if (ABS(1.0 * A[i] - a[i] + 1.0) < 1.0) A[i]++, sum++; if (sum == 0) break; } } for (int i = 1; i <= n; i++) cout << A[i] << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; long double o = 1.0; pair<long long, long long> arr[100001]; long double pr[100001]; int main() { ios_base::sync_with_stdio(0); cin.tie(); long long n, p; cin >> n >> p; for (int i = 0; i < n; i++) cin >> arr[i].first >> arr[i].second; for (int i = 0; i < n; i++) { long long num = max(arr[i].first, arr[i].second) / p - min(arr[i].first, arr[i].second) / p; if (arr[i].first % p == 0) num++; long long den = max(arr[i].first, arr[i].second) - min(arr[i].first, arr[i].second) + 1; pr[i] = num * (o / den); } long double res = 0.0; for (int i = 0; i < n; i++) { long double p1 = pr[i], p2 = pr[(i + 1) % n]; res += p1 + p2 - p1 * p2; } cout << fixed << setprecision(20) << res * 2000; }
3
#include <bits/stdc++.h> using namespace std; bool mark; long long n, a[2000000], p, s, q, i, j, sum2, sum, pop; string t; int main() { cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; sum2 += a[i]; } cin >> t; for (i = 0; i < t.length(); i++) { if (t[i] == '1') { j = i; pop++; sum += a[i]; } } if (pop == t.length()) { cout << sum2 << endl; return 0; } for (i = 0; i < j; i++) { s += a[i]; } for (i = j; i >= 0; i--) { if (i >= 1 && t[i] == '1' && a[i - 1] > a[i]) { sum -= a[i]; for (j = 0; j < i; j++) { if (t[j] == '0') { sum += a[j]; } } break; } } cout << max(sum, s) << endl; return 0; }
3
#include <iostream> #include <string> #include <sstream> #include <map> #include <set> using namespace std; int main() { map<int, int> m; int n, cnt; char comma; while(true) { string str; getline(cin, str); if(str == "") break; istringstream is(str); is >> n >> comma >> cnt; m[n]++; } set<int> s; while(cin >> n >> comma >> cnt) { if(m.count(n) > 0) { m[n]++; s.insert(n); } } for(set<int>::iterator it = s.begin(); it != s.end(); ++it) cout << *it << ' ' << m[*it] << endl; return 0; }
0
#include<bits/stdc++.h> using namespace std; int n, m, sum, p, x, a[123]; int main() { cin >> n; for (int i = 1; i <= n; i++) { scanf("%d", a + i); sum += a[i]; } cin >> m; while(m--) { scanf("%d%d", &p, &x); printf("%d\n", sum - a[p] + x); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int n_max = 400005; int n, i, j, x, y, a[n_max], timer, Q; vector<int> g[n_max]; int l[n_max], r[n_max], f[n_max]; int h[n_max], k; void add(int x, int k) { for (int i = x; i < n_max; i = 2 * i - (i & (i - 1))) f[i] += k; } int sum(int x) { int s = 0; while (x) { s += f[x]; x &= x - 1; } return s; } void go(int u, int p) { int i, to; l[u] = ++timer; h[u] = h[p] + 1; for (i = 0; i < g[u].size(); i++) { to = g[u][i]; if (to == p) continue; go(to, u); } r[u] = ++timer; } int main() { scanf("%d%d", &n, &Q); for (i = 1; i <= n; i++) scanf("%d", &a[i]); for (i = 1; i < n; i++) { scanf("%d%d", &x, &y); g[x].push_back(y); g[y].push_back(x); } go(1, 0); while (Q--) { scanf("%d", &k); if (k == 1) { scanf("%d%d", &x, &y); if (h[x] % 2 == 0) y *= -1; add(l[x], y); add(r[x] + 1, -y); } else { scanf("%d", &x); y = sum(l[x]); if (h[x] % 2 == 0) y *= -1; printf("%d\n", y + a[x]); } } }
3
#include <bits/stdc++.h> using namespace std; #define int long long const int MAX = 510000; const int MOD = 998244353; const int Inv2 = (MOD+1)/2; signed main(){ int n,k,c; cin>>n>>k>>c; string s; cin>>s; int t=0; int a[n],b[n];a[0]=0;b[n-1]=0; for(int i=0;i<n-1;i++){ if(t>0||s[i]=='x'){a[i+1]=a[i];t--;} else {a[i+1]=a[i]+1;t=c;} } t=0; for(int i=n-1;i>0;i--){ if(t>0||s[i]=='x'){b[i-1]=b[i];t--;} else {b[i-1]=b[i]+1;t=c;} } for(int i=0;i<n;i++)if(a[i]+b[i]<k)cout<<i+1<<endl; }
0
///********************** Bismillahir Rahmanir Rahim *****************/// #include<bits/stdc++.h> using namespace std; ///*********************** Template Start Here ***********************/// ///************************ C o n t a i n e r ************************/// typedef long long ll; typedef unsigned long long ull; typedef vector<ll> vl; typedef vector<int> vi; typedef vector<char> vc; typedef vector<string> vs; typedef vector<int>::iterator vit; typedef set<int> si; typedef set<string> ss; typedef set<int>::iterator sit; typedef map<int, int> mii; typedef map<string, int> msi; typedef map<int, string> mis; typedef map<string, string> mss; typedef pair<ll, ll> pll; typedef pair<int,int> pii; typedef pair<double, double> pdd; typedef double dl; ///**************************** M a r c o ****************************/// #define f first #define s second #define endl '\n' #define sp <<" "<< #define pb push_back #define MP make_pair #define MOD 1000000007 #define sqr(a) ((a) * (a)) #define sz(x) (int)x.size() #define mid(l,r) ((l+r)/2) #define fora(a) for(auto u:a) #define gcd(a,b) __gcd(a,b) #define lcm(a,b) (a*(b/gcd(a,b))) #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() #define mem(a,b) memset(a, b, sizeof(a)) #define test int tc; cin>>tc; while(tc--) #define forn(i,n) for(auto i=0; i<n; i++) #define rforn(i,n) for(auto i=n-1; i>=0; i--) #define printv(a) {for(auto u:a) cout<<u<<" "; cout<<endl;} #define printm(a) {for(auto u:a) cout<<u.f sp u.s<<endl;} #define abid() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define ssd(a,b,c) set_symmetric_difference(all(a), all(b), c.begin()); #define fori(a,b,c) {for(a = c.begin(); a!=b; a++) cout<< *a<< " "; cout<<endl;} #define fraction(a) cout.unsetf(ios::floatfield); cout.precision(a); cout.setf(ios::fixed,ios::floatfield); #define file() freopen("input.txt","r",stdin);freopen("output.txt","w",stdout); ///**************************** C o n s t ****************************/// const double PI = acos(-1); const double eps = 1e-9; const int inf = 2000000000; const ll infLL = 9000000000000000000; ///************************ Template End Here ************************/// int main() { abid(); int n; cin>>n; cout<<2 - n*n<<endl; return 0; }
6
//17 #include<iostream> #include<algorithm> #include<map> #include<string> #include<queue> #include<vector> #include<set> using namespace std; struct E{ string t; int d; }; struct S{ string v; int r,d; bool operator<(S a)const{ return d>a.d; } }; int main(){ for(int n,mm,cap;cin>>n>>mm>>cap,n|mm|cap;){ cap*=10; string src,dest; cin>>src>>dest; map<string,vector<E> > m; for(int i=0;i<n;i++){ string c[2]; int d; cin>>c[0]>>c[1]>>d; for(int j=0;j<2;j++){ E ce={c[j^1],d}; m[c[j]].push_back(ce); } } set<string> gs; for(int i=0;i<mm;i++){ string s; cin>>s; gs.insert(s); } priority_queue<S> que; S is={src,cap,0}; que.push(is); map<string,map<int,int> > p; while(!que.empty()){ S c=que.top(); if(c.v==dest)break; que.pop(); map<int,int>::iterator it=p[c.v].lower_bound(c.r); if(it!=p[c.v].end()&&it->second<=c.d)continue; p[c.v][c.r]=c.d; for(int i=0;i<m[c.v].size();i++){ if(m[c.v][i].d<=c.r){ S n={m[c.v][i].t,c.r-m[c.v][i].d,c.d+m[c.v][i].d}; if(gs.count(n.v)){ n.r=cap; } que.push(n); } } } cout<<(que.empty()?-1:que.top().d)<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e5 + 100; long long n; vector<long long> pos; long long cou(long long k) { long long ans = 0; for (long long i = 1; i <= n; i += k) { long long mid = (i + i + k - 1) / 2; for (long long j = i; j <= i + k - 1; j++) { ans += abs(pos[mid] - pos[j]); } } return ans; } signed main() { cin >> n; pos.push_back(0); for (long long i = 1; i <= n; i++) { long long x; cin >> x; if (x) pos.push_back(i); } n = pos.size() - 1; long long k = -1, sum = 1e18; for (long long i = 2; i <= n; i++) { if (n % i == 0) { sum = min(sum, cou(i)); k = i; } } if (k == -1) { cout << "-1\n"; return 0; } cout << sum << endl; }
5
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const double PI = 3.14159265359; int main() { ios_base::sync_with_stdio(0); cin.tie(0); string s; cin >> s; int n = s.size(); set<int> zr, zl; vector<int> ones, zeros; for (int i = 0; i < n; i++) { if (s[i] == '0') { zl.insert(i); zr.insert(i); zeros.push_back(i); } else ones.push_back(i); } vector<vector<int>> graph(n); for (int i = 0; i < ones.size(); i++) { if (!zl.size() || !zr.size()) { cout << -1; return 0; } auto it1 = zl.lower_bound(ones[i]); if (it1 == zl.begin()) { cout << -1; return 0; } it1--; graph[*it1].push_back(ones[i]); zl.erase(it1); auto it2 = zr.lower_bound(ones[i]); if (it2 == zr.end()) { cout << -1; return 0; } graph[ones[i]].push_back(*it2); zr.erase(it2); } vector<bool> visited(n, false); vector<vector<int>> ans; for (int i : zeros) { if (!visited[i]) { vector<int> path; path.push_back(i); int cur = i; visited[i] = true; while (graph[cur].size()) { path.push_back(graph[cur][0]); cur = graph[cur][0]; visited[cur] = true; } ans.push_back(path); } } cout << ans.size() << endl; for (auto v : ans) { cout << v.size() << " "; for (int i : v) { cout << i + 1 << " "; } cout << endl; } return 0; }
3
#include<bits/stdc++.h> using namespace std; int main(void) { int i,j,k,name[1000],b[1000],c[1000],d[1000],e,f,g,h,kl[1000],n,ff; while(cin>>n&&n) { for(i=0;i<n;i++) { cin>>name[i]>>b[i]>>c[i]>>d[i]; kl[i]=b[i]*4+c[i]*9+d[i]*4; } cin>>e>>f>>g>>h; ff=0; for(i=0;i<n;i++) { if(b[i]<=e&&c[i]<=f&&d[i]<=g&&kl[i]<=h) { cout<<name[i]<<endl; ff=1; } } if(ff==0) cout<<"NA"<<endl; } }
0
#include <iostream> int main(){ char c,d; std::cin >> c >> d; std::cout << (c == d ? 'H' : 'D') << std::endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, m; string s; cin >> n >> s; int t = 0; for (int i = 0; i < n; i++) if (s[i] == 'X') t++; int k = 0; if (t < n / 2) { for (int i = 0; i < n; i++) if (s[i] == 'x') { t++; k++; s[i] = 'X'; if (t == n / 2) break; } } else if (t > n / 2) { for (int i = 0; i < n; i++) if (s[i] == 'X') { t--; k++; s[i] = 'x'; if (t == n / 2) break; } } cout << k << endl; cout << s << endl; return 0; }
1
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:64000000") using namespace std; void __never(int a) { printf("\nOPS %d", a); } int n, m; map<string, int> Map; vector<string> vec; vector<int> res; void sol() { m = (int)((Map).size()); for (map<string, int>::iterator it = Map.begin(); it != Map.end(); it++) { vec.push_back(it->first); res.push_back(it->second); } cout << m << "\n"; for (int a = (0); a <= (m - 1); a++) { int A = 0, B = 0; for (int b = (0); b <= (m - 1); b++) if (res[a] >= res[b]) A++; else B++; if (100 * B > m * 50) cout << vec[a] << " noob\n"; else if (100 * B > m * 20) cout << vec[a] << " random\n"; else if (100 * B > m * 10) cout << vec[a] << " average\n"; else if (100 * B > m) cout << vec[a] << " hardcore\n"; else cout << vec[a] << " pro\n"; } } int main() { cin >> n; for (int a = (1); a <= (n); a++) { string str; int x; cin >> str >> x; Map[str] = max(Map[str], x); } sol(); return 0; }
2
#include<bits/stdc++.h> using namespace std; int main(){ int a,b,c,d; cin>>a>>b>>c>>d; cout<<min(min(a+c,a+d),min(b+c,b+d))<<endl; }
0
#include <bits/stdc++.h> using namespace std; long long n, k; long long a[200005]; bool check(long long x, long long y) { long long ans = 0; for (int i = 0; i < n; i++) { if (!y) { ans++; y = y ^ 1; } else { if (a[i] <= x) { ans++; y = y ^ 1; } } } if (ans >= k) return true; else return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; } int low = 0; int high = 1e9; long long ans; while (low <= high) { long long mid = (low + (high - low) / 2); if (check(mid, 0) || check(mid, 1)) { ans = mid; high = mid - 1; } else { low = mid + 1; } } cout << ans; }
4
#include <bits/stdc++.h> using namespace std; int n, k, res = 0, a[100005], t[100005], sum[100005]; int main() { cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> t[i]; for (int i = 1; i <= n; i++) { sum[i] = sum[i - 1]; if (t[i] == 1) res += a[i]; else sum[i] += a[i]; } int maxn = 0; for (int i = k; i <= n; i++) maxn = max(maxn, sum[i] - sum[i - k]); cout << res + maxn; }
2
#include <bits/stdc++.h> using namespace std; int main() { int test = 1; while (test--) { int n, m; cin >> n; int a[n]; long long int sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } sort(a, a + n, greater<int>()); cin >> m; int q[m]; for (int i = 0; i < m; i++) cin >> q[i]; for (int i = 0; i < m; i++) { cout << sum - a[q[i] - 1] << endl; } } }
2
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int INF = 1e9; const int base = 1e9; const int MAX = 2e5; const long double EPS = 1e-10; const long double PI = acos(-1.); int n, k, p, second; int main() { ios_base::sync_with_stdio(0); cin >> k >> n >> second >> p; n = (n + second - 1) / second; int ans = (n * k + p - 1) / p; cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int le[4 * 200010], me[4 * 200010], re[4 * 200010]; void init(int id, int l, int r) { le[id] = me[id] = re[id] = r - l + 1; if (l == r) return; int m = (l + r) / 2; init(id * 2, l, m); init(id * 2 + 1, m + 1, r); } void add(int id, int l, int r, int p, int v) { if (l == r) { le[id] = re[id] = me[id] += v; return; } int m = (l + r) / 2; if (p <= m) add(id * 2, l, m, p, v); else add(id * 2 + 1, m + 1, r, p, v); me[id] = max(max(me[id * 2], me[id * 2 + 1]), re[id * 2] + le[id * 2 + 1]); le[id] = le[id * 2] + (le[id * 2] == m - l + 1 ? le[id * 2 + 1] : 0); re[id] = re[id * 2 + 1] + (re[id * 2 + 1] == r - m ? re[id * 2] : 0); } int st, ed; inline bool eq(int a, int b) { return (a + 1) / 2 == (b + 1) / 2; } int ask(int id, int l, int r) { if (l == r) return st = ed = l; int m = (l + r) / 2; if (eq(me[id], me[id * 2])) return ask(id * 2, l, m); if (eq(me[id], re[id * 2] + le[id * 2 + 1])) { st = m - re[id * 2] + 1; ed = m + le[id * 2 + 1]; return (st + ed) / 2; } return ask(id * 2 + 1, m + 1, r); } int pos[1000010]; set<int> in; int main() { int n, m, t, x; scanf("%d%d", &n, &m); init(1, 1, n); while (m--) { scanf("%d%d", &t, &x); if (t == 1) { int s1 = in.empty() ? n : *in.begin() - 1; int t2 = ask(1, 1, n), s2 = (ed - st) / 2 + 1; int s3 = in.empty() ? n : n - *in.rbegin(); int s = max(s1, max(s2, s3)); if (s1 == s && s1 >= s3) pos[x] = 1; else if (s2 == s) pos[x] = t2; else pos[x] = n; in.insert(pos[x]); add(1, 1, n, pos[x], -1); printf("%d\n", pos[x]); } else { in.erase(pos[x]); add(1, 1, n, pos[x], 1); } } return 0; }
5
#include <bits/stdc++.h> using namespace std; struct node { int a, b, c; } d[200005]; bool cmp(const node& p, const node& q) { return p.c > q.c; } int main() { int n, k, i, ans; scanf("%d%d", &n, &k); for (i = 1; i <= n; i++) scanf("%d", &d[i].a); for (i = 1; i <= n; i++) scanf("%d", &d[i].b), d[i].c = d[i].b - d[i].a; sort(d + 1, d + 1 + n, cmp); for (ans = 0, i = 1; i <= k; i++) ans += d[i].a; for (; i <= n; i++) ans += min(d[i].a, d[i].b); printf("%d\n", ans); }
3
#include <bits/stdc++.h> using namespace std; template <typename T> void pr(vector<T> &v) { for (int i = 0; i < (int)(v).size(); i++) cout << v[i] << " "; cout << '\n'; ; } template <typename T> void pr(vector<vector<T>> &v) { for (int i = 0; i < (int)(v).size(); i++) { pr(v[i]); } } template <typename T> void re(T &x) { cin >> x; } template <typename T> void re(vector<T> &a) { for (int i = 0; i < (int)(a).size(); i++) re(a[i]); } template <class Arg, class... Args> void re(Arg &first, Args &...rest) { re(first); re(rest...); } template <typename T> void pr(T x) { cout << x << '\n'; ; } template <class Arg, class... Args> void pr(const Arg &first, const Args &...rest) { cout << first << " "; pr(rest...); cout << '\n'; ; } void ps() { cout << '\n'; ; } template <class T, class... Ts> void ps(const T &t, const Ts &...ts) { cout << t; if (sizeof...(ts)) cout << " "; ps(ts...); } const long long MOD = 998244353; long double PI = 4 * atan(1); long double eps = 1e-12; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int q; cin >> q; vector<long long> a(n); re(a); vector<long long> diff(n + 1); for (int i = 0; i < n - 1; i++) { diff[i + 1] = abs(a[i + 1] - a[i]); } diff[0] = -1; diff[n] = MOD; vector<int> left(n + 1); vector<int> right(n + 1); stack<int> stk; for (int i = 0; i < n + 1; i++) { while (!stk.empty() && diff[stk.top()] <= diff[i]) { right[stk.top()] = i; stk.pop(); } if (!stk.empty()) { left[i] = stk.top(); } else { left[i] = 0; } stk.push(i); } for (int i = 0; i < q; i++) { int x, y; cin >> x >> y; long long ans = 0; for (int j = x; j < y; j++) { ans += diff[j] * (j - max(x - 1, left[j])) * (min(y, right[j]) - j); } cout << ans << '\n'; ; } }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 213245; int a[maxn], w[maxn][2]; int dp[maxn], sum[maxn]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m, k; cin >> n >> m >> k; for (int i = 1; i <= n; ++i) { cin >> a[i]; } sort(a + 1, a + n + 1); memset(sum, 0, sizeof(sum)); for (int i = 1; i <= k; ++i) { sum[i] = sum[i - 1] + a[i]; } w[0][0] = 1; w[0][1] = 0; for (int i = 1; i <= m; ++i) { cin >> w[i][0] >> w[i][1]; } for (int i = 0; i <= k; ++i) dp[i] = INT_MAX; dp[0] = 0; for (int i = 0; i < k; ++i) { for (int j = 0; j <= m; ++j) { int r = i + w[j][0], l = i + w[j][1]; if (r <= k) dp[r] = min(dp[i] + sum[r] - sum[l], dp[r]); } } cout << dp[k] << '\n'; return 0; }
6
#include <bits/stdc++.h> const int MAXN = 4e5 + 5; int a[MAXN], n; std::vector<int> S; inline int calc(int k) { S.clear(); for (int i = 1; i <= n; ++i) S.push_back(a[i] & ((1 << (k + 1)) - 1)); std::sort(S.begin(), S.end()); long long res = 0; for (int i = 0; i <= n - 1; ++i) { int l = 0, r = 0; if ((S[i] >> k) & 1) { l = 0; r = (1 << (k + 1)) - S[i] - 1; } else { l = (1 << k) - S[i]; r = (1 << (k + 1)) - S[i] - 1; } if (l <= S[i] && S[i] <= r) res--; l = std::lower_bound(S.begin(), S.end(), l) - S.begin(); r = std::upper_bound(S.begin(), S.end(), r) - S.begin() - 1; if (l > r) continue; res += r - l + 1; } for (int i = 0; i <= n - 1; ++i) { int l = 0, r = 0; if ((S[i] >> k) & 1) { l = (1 << (k + 1)) - S[i] + (1 << k); r = 1e7; } else { l = (1 << (k + 1)) - S[i] + (1 << k); r = 1e7; } if (l <= S[i] && S[i] <= r) res--; l = std::lower_bound(S.begin(), S.end(), l) - S.begin(); r = std::upper_bound(S.begin(), S.end(), r) - S.begin() - 1; if (l > r) continue; res += r - l + 1; } res /= 2; return res & 1; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", a + i); int ans = 0; for (int i = 0; i <= 24; ++i) { ans |= calc(i) << i; } printf("%d\n", ans); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, sum1 = 0, sum2 = 0; cin >> n; int *a = new int[n]; for (int i = 0; i < n; i++) cin >> a[i]; int x, y; cin >> x >> y; if (x > y) { x = x + y; y = x - y; x = x - y; } for (int i = x - 1; i < y - 1; i++) sum1 += a[i]; for (int i = 0; i < x - 1; i++) sum2 += a[i]; for (int i = n - 1; i >= y - 1; i--) sum2 += a[i]; if (sum1 > sum2) cout << sum2; else cout << sum1; cout << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { string a, b; cin >> a >> b; vector<pair<char, char>> res; map<char, int> chk; for (int i = 0; i < a.size(); i++) { if (a[i] != b[i]) { res.push_back(make_pair(a[i], b[i])); char c = b[i]; if (chk[c] || chk[a[i]]) { cout << -1 << endl; return 0; } chk[c] = chk[a[i]] = 1; for (int j = 0; j < a.size(); j++) { if (b[j] == a[i]) b[j] = c; else if (b[j] == c) b[j] = a[i]; } } } if (a != b) { cout << -1; return 0; } cout << res.size() << endl; for (int i = 0; i < res.size(); i++) cout << res[i].first << " " << res[i].second << endl; return 0; }
2
#include <bits/stdc++.h> const int maxi = 2000000000; const int maxq = 1000000000; const double eps = 1e-10; const double pi = 3.1415926535897932; const double inf = 1e+18; const int mo = 1000000007; using namespace std; int t, kolr, n, m, k, a, b, tin[333333], fup[333333], nom[111111], y[333333], p[333333], pred[333333]; bool used[333333], most[3333][3333]; void adde(int a, int b) { kolr++; y[kolr] = b; p[kolr] = nom[a]; nom[a] = kolr; } void dfs1(int x, int pr) { if (used[x]) return; pred[x] = pr; used[x] = true; int q = nom[x]; while (q) { dfs1(y[q], x); q = p[q]; } } void dfs(int x, int pr) { used[x] = true; t++; tin[x] = fup[x] = t; for (int q = nom[x]; q > 0; q = p[q]) { if (y[q] == pr) continue; if (used[y[q]]) fup[x] = min(fup[x], tin[y[q]]); else { dfs(y[q], x); fup[x] = min(fup[x], fup[y[q]]); if (fup[y[q]] > tin[x]) { most[x][y[q]] = 1; most[y[q]][x] = 1; } } } } int main() { scanf("%d%d\n", &n, &m); for (int i = 0; i < m; i++) { scanf("%d%d", &a, &b); adde(a, b); adde(b, a); } scanf("%d\n", &k); dfs(1, -1); for (int i = 0; i < k; i++) { scanf("%d%d", &a, &b); int kol = 0; for (int i = 1; i <= n; i++) pred[i] = 0, used[i] = 0; dfs1(a, 0); while (pred[b] != 0) { if (most[pred[b]][b]) kol++; b = pred[b]; } printf("%d\n", kol); } return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long MOD = (int)1e9 + 7; struct vct { long long v[2010]; } b, a; int c[2010]; int n, k; vct mul(vct x, vct y) { vct res; memset(res.v, 0LL, sizeof(res.v)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { res.v[i] = (res.v[i] + x.v[j] * y.v[i - j + 1]) % MOD; } } return res; } int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", &c[i]); for (int i = 1; i <= n; i++) b.v[i] = 1LL; memset(a.v, 0LL, sizeof(a.v)); a.v[1] = 1LL; while (k > 0) { if (k & 1) a = mul(a, b); vct temp = mul(b, b); b = temp; k >>= 1; } long long ans; for (int i = 1; i <= n; i++) { ans = 0LL; for (int j = i; j >= 1; j--) { ans = (ans + a.v[j] * (long long)c[i - j + 1]) % MOD; } printf("%d ", ans); } printf("\n"); return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, m; int a[5001]; int dp[5001][5001]; int sol(int i, int cur) { if (i == n) return 0; int &ret = dp[i][cur]; if (ret != -1) return ret; int take = 1 + sol(i + 1, a[i]); int dont = sol(i + 1, cur); if (a[i] < cur) return ret = dont; return ret = max(dont, take); } int main() { cin >> n >> m; memset(dp, -1, sizeof(dp)); for (int i = 0; i < n; i++) { double x; cin >> a[i] >> x; } cout << n - sol(0, 0) << endl; }
4
#include <bits/stdc++.h> #define rep(i,n) for (int i=0; i<(n); ++i) using namespace std; typedef long long ll; typedef pair<int,int> P; int main(){ string s; cin>>s; string t=s; reverse(t.begin(),t.end()); rep(i,t.size()){ if(s[i]=='b')s[i]='d'; else if(s[i]=='d')s[i]='b'; else if(s[i]=='p')s[i]='q'; else if(s[i]=='q')s[i]='p'; } cout<<(s==t?"Yes":"No")<<endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; long long x, y, a, b; cin >> t; for (int j = 0; j < t; ++j) { cin >> x >> y >> a >> b; cout << (max(x, y) - min(x, y)) * a + min(x, y) * min(b, 2 * a) << '\n'; } return 0; }
1
#include <bits/stdc++.h> using namespace std; using ll = long long; ll mod = (ll)1e9 + 7; ll MOD = mod; int64_t POW(int64_t a, int64_t b) { int64_t res = 1; while (b > 0) { if (b % 2 == 1) res = (res * a); a = (a * a); b /= 2; } return res; } ll sum_range2d(ll i, ll j, ll k, ll l, vector<vector<ll>>& sum) { return sum[k][l] - sum[k][j - 1] - sum[i - 1][l] + sum[i - 1][j - 1]; } ll GCDAC(ll a, ll b) { if (b == 0) return a; return GCDAC(b, a % b); } ll gcd(ll a, ll b) { if (a < b) swap(a, b); return GCDAC(a, b); } ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; } ll dist(ll X1, ll Y1, ll X2, ll Y2) { return sqrt(POW(X1 - X2, 2) + POW(Y1 - Y2, 2)); } bool intersect(pair<ll, ll> p1, pair<ll, ll> p2) { ll x1 = p1.first, x2 = p1.second, y1 = p2.first, y2 = p2.second; return (x1 >= y1 && x1 <= y2) || (x2 >= y1 && x2 <= y2) || (y1 >= x1 && y1 <= x2) || (y2 >= x1 && y2 <= x2); } bool isPrime(ll n) { for (ll i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } ll inverse(ll n) { return (POW(n % mod, mod - 2)); } bool isVowel(char ch) { ch = tolower(ch); return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'; } int main() { ios::sync_with_stdio(0); cout.tie(0); cin.tie(0); string s, t; cin >> s >> t; int freq[300] = {0}, v[(int)(2 * 1e5 + 5)] = {0}; int yay = 0, whoops = 0; for (int i = 0; i < (int)t.length(); i++) freq[t[i]]++; for (int i = 0; i < (int)s.length(); i++) { if (freq[s[i]] > 0) { v[i] = 1; ++yay; --freq[s[i]]; } } for (int i = 0; i < (int)s.length(); i++) { if (v[i]) continue; char ch; if (isupper(s[i])) ch = tolower(s[i]); else ch = toupper(s[i]); if (freq[ch] > 0) { ++whoops; --freq[ch]; } } cout << yay << ' ' << whoops; }
2
#include <bits/stdc++.h> using namespace std; string name; int n, fi; int f[155][155]; int from[155][155]; int lo[155][255]; char mask[155], mask2[155]; ; char ans[155]; bool gen() { memset(ans, 0, sizeof(ans)); memset(mask2, 0, sizeof(mask2)); for (int i = 0; i < name.size(); ++i) if (name[i] >= 'a' + fi) return false; for (int i = 0; i < n - name.size() + 1; ++i) mask2[i] = mask[i]; for (int i = 0; i < n - name.size() + 1; ++i) if (mask[i] == '1') for (int j = 0; j < name.size(); ++j) ans[i + j] = name[j]; for (int i = 0; i < n - name.size() + 1; ++i) if (mask[i] == '1') for (int j = 0; j < name.size(); ++j) if (ans[i + j] != name[j]) return false; char mask1[155] = {0}; for (int i = 0; i < n - name.size() + 1; ++i) if (mask[i] == '1') for (int j = 0; j < name.size(); ++j) mask1[i + j] = '1'; for (int i = 0; i < n - name.size() + 1; ++i) if (mask[i] == '0') { bool ouc = true; for (int j = 0; j < name.size(); ++j) if (!(mask1[i + j] && ans[i + j] == name[j])) { ouc = false; break; } if (ouc) return false; } for (int i = 0; i < n; ++i) mask[i] = mask1[i]; memset(f, 0, sizeof(f)); memset(lo, 0, sizeof(lo)); memset(from, 0, sizeof(from)); for (int i = 0; i <= name.size(); ++i) for (int j = 'a'; j < 'a' + fi; ++j) { string str1 = ""; for (int k = 0; k < i; ++k) str1 += name[k]; str1 += j; for (int k = min(str1.size(), name.size()); k >= 1; --k) { bool check = true; for (int r = 0; r < k; ++r) if (str1[str1.size() - k + r] != name[r]) { check = false; break; } if (check) { lo[i][j] = k; break; } } } for (int i = 'a'; i < 'a' + fi; ++i) if (name[0] == i) { if (mask[0] != '1' && name.size() == 1) continue; f[0][1] = name[0]; } else if (mask[0] != '1') { if (name[0] != 'a') f[0][0] = 'a'; else if (fi > 1) f[0][0] = 'b'; } for (int i = 1; i < n; ++i) for (int j = 0; j <= name.size(); ++j) { if (f[i - 1][j] > 0) { if (mask[i] == '1') { if (lo[j][ans[i]] == name.size() && '1' != mask2[i - name.size() + 1]) continue; f[i][lo[j][ans[i]]] = ans[i]; from[i][lo[j][ans[i]]] = j; continue; } for (int k = 'a'; k < 'a' + fi; ++k) { if (lo[j][k] == name.size()) continue; f[i][lo[j][k]] = k; from[i][lo[j][k]] = j; } } } int where = -1; for (int i = 0; i <= name.size(); ++i) if (f[n - 1][i] > 0) { where = i; break; } if (-1 == where) return false; for (int i = n - 1; i >= 0; --i) { ans[i] = f[i][where]; where = from[i][where]; } return true; } int main() { memset(mask, 0, sizeof(mask)); cin >> n >> fi; cin >> name; cin >> mask; if (gen()) { for (int i = 0; i < n; ++i) cout << ans[i]; cout << endl; } else cout << "No solution\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long int b, sum = 0; scanf("%lld", &b); long long int i, j; long long int c = sqrt(b); for (i = 1; i <= c; i++) { if (b % i == 0) { j = b / i; if (j == i) sum++; else sum += 2; } } printf("%lld\n", sum); return 0; }
2
#include<bits/stdc++.h> using namespace std; int check(int x){ int y=0; while(y*(y+1)/2 < x){ y++; } return y; } int main(){ int n; cin >> n; int i=check(n); if(n-i*(i+1)/2){ cout << "No" << endl; return 0; }else{ cout << "Yes" << endl; } vector<vector<int>> a(i+1); for(int p=1; p<=n; p++){ a[check(p)].push_back(p); a[p-1-(check(p)-1)*check(p)/2].push_back(p); } int tes = 0; cout << i+1 << endl; for(int cnt=0; cnt<i+1; cnt++){ cout << i << " "; for(const auto q: a[cnt]){ cout << q << " "; } cout << endl; } }
0
#include <bits/stdc++.h> using namespace std; int x[202333]; int p[202333]; pair<int, int> P[202333]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", x + i), P[i].first = x[i], P[i].second = i; sort(P + 1, P + n + 1); for (int i = 1; i <= n; ++i) p[P[i].second] = i; sort(x + 1, x + n + 1); x[0] = -2e9 - 10; x[n + 1] = 2e9 + 10; for (int i = 1; i <= m; ++i) { int a, l; int dir = 1; scanf("%d%d", &a, &l); a = p[a]; while (true) { int Mi = INT_MAX; if (a) Mi = min(Mi, x[a] - x[a - 1]); if (a < n) Mi = min(Mi, x[a + 1] - x[a]); if (l < Mi) break; if (dir) { if (a < n) { int L = a, R = n + 1, M; while (R - L > 1) { M = (L + R) >> 1; if (x[M] - x[a] <= l) L = M; else R = M; } if (L == a) dir ^= 1; else { int LL = x[L] - x[a]; if (l - LL * 2 < x[a] - x[a - 1]) { int r = l % LL, d = l / LL; if (d & 1) a = L, dir ^= 1; l = r; } else { l -= LL; a = L; dir ^= 1; } } } else dir ^= 1; } else { if (a) { int L = 0, R = a, M; while (R - L > 1) { M = (L + R) >> 1; if (x[a] - x[M] <= l) R = M; else L = M; } if (R == a) dir ^= 1; else { int LL = x[a] - x[R]; if (l - LL * 2 < x[a + 1] - x[a]) { int r = l % LL, d = l / LL; if (d & 1) a = R, dir ^= 1; l = r; } else { l -= LL; a = R; dir ^= 1; } } } else dir ^= 1; } } printf("%d\n", P[a].second); } return 0; }
4
#include<bits/stdc++.h> using namespace std; long long n; long long cnt[100]; long long ans; int main(){ cin>>n; for(long long i=1;i<=n;i++){ long long x; cin>>x; for(long long j=0;j<61;j++){ cnt[j]+=x&1; x>>=1; } } long long t=1; for(long long i=0;i<61;i++){ ans+=((cnt[i]*(n-cnt[i])%1000000007)*t)%1000000007; ans%=1000000007; t<<=1; t%=1000000007; } cout<<ans; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int ans = 0; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; if (a + b > ans) ans = a + b; } cout << ans; }
2
#include <iostream> #include <vector> #include <cmath> #include <algorithm> using namespace std; int main(){ int n,k; cin >> n >> k; vector<int> a(n); for(int i=0;i<n;i++){ cin >> a[i]; } for(int i=0;i<k;i++){ vector<int> nxt(n,0); for(int j=0;j<n;j++){ int l = max(0,j-a[j]); int r = min(n-1,j+a[j]); nxt[l]++; if(r<n-1){ nxt[r+1]--; } } for(int j=1;j<n;j++){ nxt[j] += nxt[j-1]; } if(a==nxt) break; else a = nxt; } for(int i=0;i<n;i++){ cout << a[i] << (i==n-1 ? "\n" : " "); } }
0
#include<iostream> using namespace std; int n,a; int main() { cin>>n>>a; if(n%500<=a) cout<<"Yes"; else cout<<"No"; return 0; }
0
#include <bits/stdc++.h> using namespace std; struct node { int x; int norm; node *next; } * h[300001]; struct poz { int left, right; } p[300001]; int t[300001], i, current, n, fi[300001]; long long f[300001], b[300001], total, s; int hash_search(unsigned int x) { int j = x % 300001; for (node *d = h[j]; d != NULL; d = d->next) { if (d->x == x) return d->norm; } return 0; } void hash_add(unsigned int x, int norm) { int j = x % 300001; node *d = new node; d->x = x; d->norm = norm; d->next = h[j]; h[j] = d; } void add(unsigned int x) { int val = hash_search(x); if (val) p[val].right = i; else { ++current; hash_add(x, current); p[current].left = i; } } int main() { cin >> n; for (i = 1; i <= n; i++) { cin >> t[i]; add(t[i]); if (t[i] > 0) total += t[i]; } for (i = 1; i <= n; i++) { if (t[i] > 0) f[i] = f[i - 1] + t[i]; else f[i] = f[i - 1]; } for (i = n; i >= 1; i--) { if (t[i] > 0) b[i] = b[i + 1] + t[i]; else b[i] = b[i + 1]; } long long bests = -3000000000000, besti = 1; for (i = 1; i <= current; i++) { if (p[i].right == 0) continue; s = (f[p[i].right] + b[p[i].left]) - total; if (t[p[i].left] < 0) s += t[p[i].left]; if (t[p[i].right] < 0) s += t[p[i].right]; if (s > bests) { bests = s; besti = i; } } int nr = 0; for (int i = 1; i < p[besti].left; i++) fi[++nr] = i; for (int i = p[besti].left + 1; i < p[besti].right; i++) { if (t[i] < 0) fi[++nr] = i; } for (int i = p[besti].right + 1; i <= n; i++) fi[++nr] = i; cout << bests << " " << nr << "\n"; for (int i = 1; i <= nr; i++) cout << fi[i] << " "; }
1
#include <iostream> #include <queue> #include <functional> #include <cstring> #include <utility> #include <cstdio> using namespace std; #define REP(i,b,n) for(int i=b;i<n;i++) #define rep(i,n) REP(i,0,n) struct edge { double dist; int limit; }; struct state { double cost; int pre, cur, v; state(double d, int a, int b, int c):cost(d),pre(a),cur(b),v(c){} bool operator<(const state& a) const { return cost > a.cost; } }; const int INF = 100000000, MAX_N = 31, MAX_V = 31; int n, m; edge e[1000]; int graph[MAX_N][MAX_N]; bool used[MAX_N][MAX_N][MAX_V]; void solve() { memset(graph, -1, sizeof graph); memset(used, false, sizeof used); int s, g; cin>>s>>g; rep(i,m) { int x, y; cin>>x>>y>>e[i].dist>>e[i].limit; graph[x][y] = i; graph[y][x] = i; } double d[MAX_N][MAX_N][MAX_V]; rep(i,MAX_N) rep(j,MAX_N) rep(k,MAX_V) d[i][j][k] = INF; priority_queue<state> que; REP(i,1,n+1) if (graph[s][i] != -1) { int en = graph[s][i]; if (e[en].limit < 1) continue; d[s][i][1] = e[en].dist; que.push(state(d[s][i][1],s,i,1)); } while (!que.empty()) { state si = que.top(); que.pop(); double cost = si.cost; int pre = si.pre, cur = si.cur, v = si.v; if (used[pre][cur][v]) continue; used[pre][cur][v] = true; if (cur == g && v == 1) { printf("%.4lf\n", cost); return; } REP(i,1,n+1) if (graph[cur][i] != -1) { if (i == pre || i == cur) continue; int en = graph[cur][i]; REP(j,-1,2) { if (v+j < 1 || v+j > e[en].limit) continue; double ne = cost + e[en].dist / (v+j); if (d[cur][i][v+j] > ne) { d[cur][i][v+j] = ne; que.push(state(ne, cur, i, v+j)); } } } } cout<<"unreachable"<<endl; } int main (int argc, char const* argv[]) { while (cin>>n>>m, n||m) { solve(); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; long long n; void pow(long long a[2][2], long long n) { if (n == 1) return; long long t[2][2]; memset(t, 0, sizeof(t)); for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) for (int k = 0; k < 2; k++) t[i][j] += (a[i][k] * a[k][j]) % mod; pow(t, n / 2); if (n % 2 == 1) { long long u[2][2]; memset(u, 0, sizeof(u)); for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) for (int k = 0; k < 2; k++) u[i][j] += (t[i][k] * a[k][j]) % mod; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) t[i][j] = u[i][j]; } for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) a[i][j] = t[i][j]; } int main() { cin >> n; if (n == 0) { cout << 1 << endl; return 0; } long long m[2][2], u[2][2]; memset(m, 0, sizeof(m)); memset(u, 0, sizeof(u)); m[0][0] = 1; long long s[2][2]; s[0][0] = 3; s[0][1] = 1; s[1][0] = 1; s[1][1] = 3; pow(s, n); for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) for (int k = 0; k < 2; k++) u[i][j] += (m[i][k] * s[k][j]) % mod; cout << u[0][0] << endl; }
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000010; const int offset = (1 << 20); struct tournament { int p[offset * 2]; tournament() { memset(p, 0, sizeof p); } void update(int cvor, int l, int r, int a, int b, int x) { if (b < a) return; if (l > b || r < a) return; if (l >= a && r <= b) { p[cvor] ^= x; return; } int mid = (l + r) / 2; update(cvor * 2, l, mid, a, b, x); update(cvor * 2 + 1, mid + 1, r, a, b, x); } int get(int cvor) { cvor += offset; int ret = 0; while (cvor) { ret ^= p[cvor]; cvor /= 2; } return ret; } }; tournament T; int n, m, gdje[MAXN], l, r, p[MAXN], pref[MAXN], sol[MAXN]; int p1[MAXN], koji[MAXN]; vector<pair<pair<int, int>, int> > v; vector<int> v1; int main() { scanf("%d", &n); for (int i = (0); i < int(n); i++) scanf("%d", &p[i]); memcpy(p1, p, sizeof p); for (int i = (0); i < int(n); i++) v1.push_back(p[i]); pref[0] = p[0]; for (int i = (1); i < int(n); i++) pref[i] = pref[i - 1] ^ p[i]; sort((v1).begin(), (v1).end()); v1.erase(unique((v1).begin(), (v1).end()), v1.end()); for (int i = (0); i < int(n); i++) p[i] = lower_bound((v1).begin(), (v1).end(), p[i]) - v1.begin(); for (int i = (0); i < int(n); i++) v.push_back(make_pair(make_pair(i, i), -(i + 1))); scanf("%d", &m); for (int i = (0); i < int(m); i++) { int a, b; scanf("%d %d", &a, &b); a--; b--; sol[i] = pref[b]; if (a) sol[i] ^= pref[a - 1]; v.push_back(make_pair(make_pair(a, -1), i)); v.push_back(make_pair(make_pair(b, MAXN), i)); } sort((v).begin(), (v).end()); l = -1; for (int i = (0); i < int((int)v.size()); i++) { if (v[i].first.first == v[i].first.second) { if (l >= gdje[p[v[i].first.first]]) { T.update(1, 0, offset - 1, gdje[p[v[i].first.first]], l, p1[v[i].first.first]); gdje[p[v[i].first.first]] = l + 1; } continue; } if (v[i].first.second == -1) { l++; koji[v[i].second] = l; } if (v[i].first.second == MAXN) sol[v[i].second] ^= T.get(koji[v[i].second]); } for (int i = (0); i < int(m); i++) printf("%d\n", sol[i]); return 0; }
4
#include <bits/stdc++.h> template <typename T> struct ScanfSpecifier {}; template <> struct ScanfSpecifier<char*> { static constexpr const char* value = "%s"; }; template <> struct ScanfSpecifier<int> { static constexpr const char* value = "%d"; }; template <> struct ScanfSpecifier<double> { static constexpr const char* value = "%lf"; }; template <> struct ScanfSpecifier<float> { static constexpr const char* value = "%f"; }; template <> struct ScanfSpecifier<char> { static constexpr const char* value = "%c"; }; template <> struct ScanfSpecifier<const char*> { static constexpr const char* value = "%s"; }; template <> struct ScanfSpecifier<unsigned long> { static constexpr const char* value = "%lu"; }; template <> struct ScanfSpecifier<unsigned int> { static constexpr const char* value = "%u"; }; template <> struct ScanfSpecifier<long long int> { static constexpr const char* value = "%lld"; }; template <typename T> int RD(T& arg) { return std::scanf(ScanfSpecifier<T>::value, &arg); } template <int S> int RD(char (&arg)[S]) { return std::scanf("%s", arg); } int RD(char* arg) { return std::scanf("%s", arg); } template <> int RD<char>(char& arg) { return std::scanf(" %c", &arg); } template <typename T, typename... Args> int RD(T& arg1, Args&... args) { return RD(arg1) + RD(args...); } template <typename T> T RD() { T ret; RD(ret); return ret; } template <typename It> void RDV(It begin, It end) { while (begin != end) RD(*begin++); } template <typename C> void RDV(C& c) { RDV(std::begin(c), std::end(c)); } template <typename T> void WT(T arg) { std::printf(ScanfSpecifier<T>::value, arg); } template <typename T, typename U> void WT(std::pair<T, U> arg) { std::printf("("); WT(arg.first); std::printf(", "); WT(arg.second); std::printf(")"); } template <typename... Args> void WT(Args... args) { int alc = 0; int dummy[] = {((alc++ ? std::printf(" ") : 0), WT(args), 0)...}; } template <typename... Args> void WTL(Args... args) { WT(args...); std::printf("\n"); } template <typename It> void WTV(It begin, It end) { int alc = 0; while (begin != end) (alc++ ? std::printf(" ") : 0), WT(*begin++); } template <typename C> void WTV(const C& c) { WTV(std::begin(c), std::end(c)); } template <typename It> void WTVL(It begin, It end) { WTV(begin, end); std::printf("\n"); } template <typename C> void WTVL(const C& c) { WTVL(std::begin(c), std::end(c)); } struct Range { struct It { int num, step; int operator*() { return num; } void operator++() { num += step; } bool operator!=(const It& other) { return num != other.num; } }; Range(int ee) : b(0), e(ee) {} Range(int bb, int ee) : b(bb), e(ee) {} It begin() { return {b, (b < e ? 1 : -1)}; } It end() { return {e, 0}; } int b, e; }; template <typename T> inline T& UMAX(T& x, T y) { if (x < y) x = y; return x; } template <typename T> inline T& UMIN(T& x, T y) { if (y < x) x = y; return x; } template <typename T, typename... Args> struct ArithmiticPromotion { typedef decltype(T() + typename ArithmiticPromotion<Args...>::type()) type; }; template <typename T, typename U> struct ArithmiticPromotion<T, U> { typedef decltype(T() + U()) type; }; template <typename T> struct ArithmiticPromotion<T, T> {}; template <typename T> struct ArithmiticPromotion<T> {}; template <typename T, typename U> typename ArithmiticPromotion<T, U>::T MAX(T a, U b) { return a < b ? b : a; } template <typename T, typename... Args> typename ArithmiticPromotion<T, Args...>::T MAX(T a, Args... args) { return MAX(a, MAX(args...)); } template <typename T, typename U> typename ArithmiticPromotion<T, U>::T MIN(T a, U b) { return a < b ? a : b; } template <typename T, typename... Args> typename ArithmiticPromotion<T, Args...>::T MIN(T a, Args... args) { return MIN(a, MIN(args...)); } using RG = Range; using namespace std; char L[32], R[32]; void pad(char* s) { int l = strlen(s); reverse(s, s + l); for (int i : RG(l, 19)) s[i] = '0'; reverse(s, s + 19); for (int i : RG(19)) s[i] -= '0'; } long long int ans; int num[10]; void dfs(int pos, int r, int total) { if (pos < 10) { for (int i = 0; i <= r; i++) { num[pos] = i; dfs(pos + 1, r - i, total + i); } } else { { bool check = true; int use = 0; bool b = false; int tmp[10] = {}; int mn = 1; for (int i = 0; i < 19; i++) { while (mn < 10 && tmp[mn] == num[mn]) mn++; if (!b) { for (int d = L[i] + 1; d < R[i]; d++) if ((!d || tmp[d] + 1 <= num[d]) && use + 19 - i - !d >= total) { ans++; return; } } else if (R[i]) { if (use + 19 - i - 1 >= total || (mn < R[i] && use + 19 - i >= total)) { ans++; return; } } if (R[i]) { ++use; if (++tmp[R[i]] > num[R[i]]) { check = false; break; } } if (L[i] < R[i]) b = true; } if (check && use == total) { ans++; return; } } { bool check = true; int use = 0; bool b = false; int tmp[10] = {}; int mx = 9; for (int i = 0; i < 19; i++) { while (mx > 0 && tmp[mx] == num[mx]) mx--; if (!b) { for (int d = L[i] + 1; d < R[i]; d++) if (tmp[d] + 1 <= num[d] && use + 19 - i - !d >= total) { ans++; return; } } else { if (mx > L[i] && use + 19 - i >= total) { ans++; return; } } if (L[i]) { ++use; if (++tmp[L[i]] > num[L[i]]) { check = false; break; } } if (L[i] < R[i]) b = true; } if (check && use == total) { ans++; return; } } } } int main() { RD(L, R); pad(L), pad(R); dfs(1, 18, 0); WTL(ans); }
3
#include <bits/stdc++.h> using namespace std; using cat = long long; void add_to_interval(int l, int r, cat val, vector<cat>& A) { int N = A.size() - 1; if (l > r) return; int start = l; while (start < 0) start += N; while (start >= N) start -= N; A[start] += val; if (start + r - l < N) A[start + r - l + 1] -= val; else { A[N] -= val; A[0] += val; A[start + r - l + 1 - N] -= val; } } int main() { cin.sync_with_stdio(0); cin.tie(0); int N; cat M; cin >> M >> N; if (M == 1) { cout << "0\n"; for (int i = 0; i < N; i++) cout << i + 1 << ((i == N - 1) ? "\n" : " "); return 0; } vector<pair<cat, int> > A(N), B(N); for (int i = 0; i < N; i++) cin >> A[i].first; for (int i = 0; i < N; i++) cin >> B[i].first; for (int i = 0; i < N; i++) { A[i].first--, B[i].first--; A[i].second = B[i].second = i + 1; } sort(begin(A), end(A)); sort(begin(B), end(B)); vector<cat> ans(N + 1, 0); for (int i = 0; i < N; i++) { cat l = B[i].first - M / 2, r = B[i].first + M / 2, c = B[i].first; pair<cat, int> p_l = {l, -1}, p_r = {r + 1, -1}, p_cl = {c, -1}, p_cr = {c + 1, -1}; auto it_l = lower_bound(begin(A), end(A), p_l); auto it_r = lower_bound(begin(A), end(A), p_r); auto it_cl = lower_bound(begin(A), end(A), p_cl); auto it_cr = lower_bound(begin(A), end(A), p_cr); add_to_interval(i - (it_l - begin(A)) + 1, i, -B[i].first + M, ans); add_to_interval(i - (it_cl - begin(A)) + 1, i - (it_l - begin(A)), B[i].first, ans); add_to_interval(i - (it_r - begin(A)) + 1, i - (it_cr - begin(A)), -B[i].first, ans); add_to_interval(i - (N - 1), i - (it_r - begin(A)), B[i].first + M, ans); } for (int i = 0; i < N; i++) { cat l = A[i].first - M / 2, r = A[i].first + M / 2, c = A[i].first; pair<cat, int> p_l = {l, -1}, p_r = {r + 1, -1}, p_cl = {c, -1}, p_cr = {c + 1, -1}; auto it_l = lower_bound(begin(B), end(B), p_l); auto it_r = lower_bound(begin(B), end(B), p_r); auto it_cl = lower_bound(begin(B), end(B), p_cl); auto it_cr = lower_bound(begin(B), end(B), p_cr); add_to_interval(-i, (it_l - begin(B)) - i - 1, -A[i].first, ans); add_to_interval((it_l - begin(B)) - i, (it_cl - begin(B)) - i - 1, A[i].first, ans); add_to_interval((it_cr - begin(B)) - i, (it_r - begin(B)) - i - 1, -A[i].first, ans); add_to_interval((it_r - begin(B)) - i, N - 1 - i, A[i].first, ans); } for (int i = 0; i < N; i++) ans[i + 1] += ans[i]; int opt_dif = 0; for (int i = 0; i < N; i++) if (ans[opt_dif] > ans[i]) opt_dif = i; cout << ans[opt_dif] << "\n"; vector<int> ans_seq(N + 1); for (int i = 0; i < N; i++) ans_seq[A[i].second] = B[(i + opt_dif) % N].second; for (int i = 0; i < N; i++) cout << ans_seq[i + 1] << ((i == N - 1) ? "\n" : " "); }
6
#include <bits/stdc++.h> using namespace std; int a[1505], sum[1505]; int dp[1505][1505]; int nt[1505]; int main() { int n, s, m, k; scanf("%d%d%d%d", &n, &s, &m, &k); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); nt[i] = -1; } for (int i = 1; i <= s; i++) { int l, r; scanf("%d%d", &l, &r); l--; for (int j = l; j < r; j++) { nt[j] = max(nt[j], r); } } int lo = 1, hi = 1e9, ans = -1; while (lo <= hi) { int mid = (lo + hi) / 2; memset(dp, 0, sizeof(dp)); for (int i = 1; i <= n; i++) { if (a[i] <= mid) sum[i] = 1; else sum[i] = 0; sum[i] += sum[i - 1]; } for (int i = 0; i <= n; i++) { for (int j = 0; j <= s; j++) { dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]); if (j < s) { dp[nt[i]][j + 1] = max(dp[nt[i]][j + 1], dp[i][j] + sum[nt[i]] - sum[i]); } } } if (dp[n][m] >= k) ans = mid, hi = mid - 1; else lo = mid + 1; } printf("%d\n", ans); }
5
// #pragma GCC target ("avx2") // #pragma GCC optimization ("O3") // #pragma GCC optimization ("unroll-loops") #pragma GCC optimize("Ofast") #pragma GCC target("avx,fma") #include <bits/stdc++.h> #include <iostream> using namespace std; #define fastio \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ cout.tie(NULL) #define f(i, a, b) for (ll i = a; i <= b; i++) #define fr(i, a, b) for (ll i = a; i >= b; i--) #define pb push_back #define sz(a) ll(a.size()) #define mp make_pair #define vi vector<ll> #define vvi vector<vi> #define pii pair<ll, ll> #define all(p) p.begin(), p.end() #define pvec(v, a, b) \ { \ f(i, a, b) cout << v[i] << " "; \ nl; \ } #define nl cout << "\n" #define mii map<ll, ll> #define deb(x) cout << #x << "=" << x << "\n" #define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl #define gout(x) cout << "Case #" << x << ": " typedef long long ll; const double EPS = 1e-9; const ll MAX = 2e5 + 7; const ll INF = 1e18; const ll mod = 1e9 + 7; ll x, y; ll gcdExtended(ll a, ll b, ll *x, ll *y) { if (a == 0) { *x = 0, *y = 1; return b; } ll x1, y1; ll gcd = gcdExtended(b % a, a, &x1, &y1); *x = y1 - (b / a) * x1; *y = x1; return gcd; } ll lcm(ll a, ll b) { ll g = gcdExtended(a,b,&x,&y); return (a*b)/g; } void solve() { ll n; cin >> n; ll c = 2; ll div = 1; ll ans = 0; while (div <= n) { ll l = lcm(div, c); ll add = n / div; add -= n / l; add %= mod; ans += c * add; ans %= mod; div = l; c++; } cout << ans; nl; } int main() { // auto start = chrono::high_resolution_clock::now(); fastio; ll tc; tc = 1; cin >> tc; f(i, 1, tc) solve(); // auto end = chrono::high_resolution_clock::now(); // // Calculating total time taken by the program. // double time_taken = // chrono::duration_cast<chrono::nanoseconds>(end - start).count(); // time_taken *= 1e-9; // cout << "Time taken by program is : " << fixed // << time_taken << setprecision(9); // cout << " sec" << endl; // return 0; }
3
#include <bits/stdc++.h> using namespace std; vector<long long> q; map<long long, pair<long long, long long> > mp; pair<long long, long long> solve(long long x) { if (x < 8) return pair<long long, long long>(x, x); for (int i = 0; i < 5; i++) { int tst = 0; tst++; tst--; tst++; tst--; } if (mp.find(x) != mp.end()) { return mp[x]; for (int i = 0; i < 5; i++) { int qaq = 0; qaq++; qaq--; qaq++; qaq--; } } for (int i = 0; i < 5; i++) { int qwq = 0; qwq++; qwq--; qwq++; qwq--; } long long q1 = *(--upper_bound(q.begin(), q.end(), x)); pair<long long, long long> q2 = solve(q1 - 1); pair<long long, long long> p3 = solve(x - q1); pair<long long, long long> ans; for (int i = 0; i < 5; i++) { int qwq = 0; qwq++; qwq--; qwq++; qwq--; } if (q2.first == p3.first + 1) { long long a = q2.second; long long b = p3.second + q1; if (a > b) { ans = pair<long long, long long>(q2.first, a); } else { ans = pair<long long, long long>(p3.first + 1, p3.second + q1); } for (int i = 0; i < 5; i++) { int qqq = 0; qqq++; qqq--; qqq++; qqq--; } } else if (q2.first < p3.first + 1) { ans = pair<long long, long long>(p3.first + 1, p3.second + q1); } else if (q2.first > p3.first + 1) { ans = q2; } for (int i = 0; i < 5; i++) { int pppp = 0; pppp++; pppp--; pppp++; pppp--; } return mp[x] = ans; } int main() { long long x; scanf("%I64d", &x); for (int i = 1; i <= 1e5 + 10; i++) q.push_back((long long)i * i * i); pair<long long, long long> ans = solve(x); printf("%I64d %I64d\n", ans.first, ans.second); return 0; }
2
#include <bits/stdc++.h> using namespace std; int n; int a[10][100]; int b[100]; int ans; int d[10][100]; int main(int argc, const char* argv[]) { cin >> n; for (int i = 0; i < 2; i++) { for (int j = 0; j < n - 1; j++) { cin >> a[i][j]; } } for (int i = 0; i < n; i++) cin >> b[i]; ans = (1 << 29); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int pre1 = 0; int suf1 = 0; for (int k = 0; k < i; k++) { pre1 += a[0][k]; } for (int k = i; k < n - 1; k++) { suf1 += a[1][k]; } int pre2 = 0; int suf2 = 0; for (int k = 0; k < j; k++) { pre2 += a[0][k]; } for (int k = j; k < n - 1; k++) { suf2 += a[1][k]; } int x = pre1 + suf1 + b[i]; int y = pre2 + suf2 + b[j]; ans = min(ans, x + y); } } cout << ans; return 0; }
2
#include <bits/stdc++.h> int st[5000005], top = 0, x, n, i, j; int main() { scanf("%d", &n); long long int sum = 0; for (i = 1; i <= n; i++) { scanf("%d", &x); while (top > 1 && st[top - 1] >= st[top] && x >= st[top]) { sum += std::min(x, st[top - 1]); top--; } st[++top] = x; } std::sort(&st[1], &st[1] + top); for (i = 1; i <= top - 2; i++) sum += st[i]; printf("%I64d\n", sum); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int i, e = 0, n, c = 0, d = 0, a, b; cin >> a >> b >> n; e = n % 3; if (e == 0) { cout << a << endl; } else if (e == 1) { cout << b << endl; } else { c = a ^ b; cout << c << endl; } } }
1
#include <bits/stdc++.h> using namespace std; string add(string s1) { string s; int c = 1, x; for (int j = s1.size() - 1; j >= 0; j--) { x = s1[j] - '0' + c; s += x % 10 + '0'; c = x / 10; } if (c) s += c + '0'; reverse(s.begin(), s.end()); return s; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, l = 0; string s, second; cin >> n >> s; second = s; sort(second.begin(), second.end()); if (s.size() % n || second[0] == second[s.size() - 1]) { l = s.size() / n + 1; for (int i = 0; i < l; i++) { cout << "1"; for (int i = 1; i < n; i++) cout << "0"; } cout << endl; } else { second.clear(); for (int i = 0; i < n; i++) second += s[i]; string res; for (int i = 1; i <= s.size() / n; i++) { res += second; } if (res > s) cout << res << endl; else { res = add(second); for (int i = 0; i < s.size() / n; i++) cout << res; cout << endl; } } }
3
#include<bits/stdc++.h> using namespace std; const int M=1e9+7,N=17; int n,m,i,fac[1<<N],inv[1<<N],f[1<<N],t,L,j,ans,a[N],k; void inc(int &x,int y){x+=y;if(x>=M)x-=M;if(x<0)x+=M;} int C(int n,int m){ if (n<m) return 0; return 1ll*fac[n]*inv[m]%M*inv[n-m]%M; } int main(){ scanf("%d%d",&n,&m),L=1<<n; fac[0]=fac[1]=inv[0]=inv[1]=1; for (i=2;i<=L;i++) fac[i]=1ll*fac[i-1]*i%M,inv[i]=M-1ll*M/i*inv[M%i]%M; for (i=2;i<=L;i++) inv[i]=1ll*inv[i-1]*inv[i]%M; for (i=1;i<=m;i++) scanf("%d",&a[i]); f[0]=1; for (i=m;i;i--) for (j=L-1;~j;j--) for (k=0;k<n;k++) if (j&(t=1<<k)) inc(f[j],1ll*f[j^t]*C(L-a[i]-j+t,t-1)%M*fac[t]%M); for (i=0;i<L;i++) inc(ans,1ll*(__builtin_popcount(i)&1?-1:1)*f[i]*fac[L-1-i]%M); printf("%lld",1ll*ans*L%M); }
0
#include <bits/stdc++.h> using namespace std; bool PIIfs(const pair<int, int>& a, const pair<int, int>& b) { return a.first < b.first || (a.first == b.first && a.second < b.second); } bool PIIsf(const pair<int, int>& a, const pair<int, int>& b) { return a.second < b.second || (a.second == b.second && a.first < b.first); } bool PIIFS(const pair<int, int>& a, const pair<int, int>& b) { return a.first > b.first || (a.first == b.first && a.second > b.second); } bool PIISF(const pair<int, int>& a, const pair<int, int>& b) { return a.second > b.second || (a.second == b.second && a.first > b.first); } bool PIIfS(const pair<int, int>& a, const pair<int, int>& b) { return a.first < b.first || (a.first == b.first && a.second > b.second); } bool PIIsF(const pair<int, int>& a, const pair<int, int>& b) { return a.second < b.second || (a.second == b.second && a.first > b.first); } bool PIIFs(const pair<int, int>& a, const pair<int, int>& b) { return a.first > b.first || (a.first == b.first && a.second < b.second); } bool PIISf(const pair<int, int>& a, const pair<int, int>& b) { return a.second > b.second || (a.second == b.second && a.first < b.first); } template <typename T> inline T max(T a, T b, T c) { return max(max(a, b), c); } template <typename T> inline T max(T a, T b, T c, T d) { return max(max(a, b, c), d); } template <typename T> inline T max(T a, T b, T c, T d, T e) { return max(max(a, b, c, d), e); } template <typename T> inline T min(T a, T b, T c) { return min(min(a, b), c); } template <typename T> inline T min(T a, T b, T c, T d) { return min(min(a, b, c), d); } template <typename T> inline T min(T a, T b, T c, T d, T e) { return min(min(a, b, c, d), e); } template <typename T> inline void RD(T& x) { cin >> x; } template <typename T, typename U> inline void RD(T& a, U& b) { return RD(a), RD(b); } template <typename T, typename U, typename V> inline void RD(T& a, U& b, V& c) { return RD(a), RD(b), RD(c); } template <typename T, typename U, typename V, typename W> inline void RD(T& a, U& b, V& c, W& d) { return RD(a), RD(b), RD(c), RD(d); } template <typename T, typename U, typename V, typename W, typename X> inline void RD(T& a, U& b, V& c, W& d, X& e) { return RD(a), RD(b), RD(c), RD(d), RD(e); } vector<int> optima; vector<int> dpoint; int main() { std::ios::sync_with_stdio(false); int N; cin >> N; optima.push_back(0); long long sum = 0; int max1 = 0; int max2 = 0; int oldval = 0; int val = 0; bool incing = false; for (int i = 1; i <= N; i++) { cin >> val; if (val > max1) { max2 = max1; max1 = val; } else { max2 = max(max2, val); } sum += val; if (incing) { if (val < oldval) { optima.push_back(oldval); incing = false; } else { } } else { if (val > oldval) { incing = true; optima.push_back(oldval); } else { } } oldval = val; } if (incing) { optima.push_back(oldval); } if (N <= 2) { cout << "0\n"; return 0; } int optima_size = optima.size(); int half_size = optima_size >> 1; while (optima_size > 3) { for (int i = 0; i < (half_size); ++i) { sum += min(optima[2 * i], optima[2 * i + 2]) - optima[2 * i + 1]; } vector<int> new_optima; new_optima.push_back(0); bool incing = false; int oldval = 0; int val = 0; for (int i = 0; i < optima_size; i += 2) { val = optima[i]; if (incing) { if (val < oldval) { new_optima.push_back(oldval); incing = false; } else { } } else { if (val > oldval) { incing = true; new_optima.push_back(oldval); } else { } } oldval = val; } if (incing) { new_optima.push_back(oldval); } optima.swap(new_optima); optima_size = optima.size(); half_size = optima_size >> 1; } sum -= max1; sum -= max2; cout << sum << '\n'; return 0; }
5
#include <bits/stdc++.h> using namespace std; vector<int> b; vector<int> good; bool vis[1024]; int find(int n) { int cnt = 0; int r = 0; while (n) { int a = n % 2; r += a * (int)pow(3, cnt); cnt++; n /= 2; } return r; } int bs(int v) { int i = 0, j = good.size(); while (i < j) { int m = (i + j) >> 1; if (v <= good[m]) j = m - 1; if (v > good[m]) i = m + 1; } return i; } int main() { for (int i = 1; i <= 1024; i++) { good.push_back(find(i)); } int q; cin >> q; while (q--) { int n; cin >> n; int k = 0; while (good[k] < n) { k++; } cout << good[k] << '\n'; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int inf = 2000000000; const int N = 500005; int a[N]; char t[N]; int main() { cin.tie(0); ios_base::sync_with_stdio(0); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } long long s1 = 0, s2 = 0; for (int i = 0; i < n; i++) { cin >> t[i]; if (t[i] == 'A') { s1 += a[i]; } else { s2 += a[i]; } } long long ans = s2; long long t1 = 0, t2 = 0; for (int i = 0; i < n; i++) { if (t[i] == 'A') { t1 += a[i]; } else { t2 += a[i]; } ans = max(ans, s2 - t2 + t1); } t1 = t2 = 0; for (int i = n - 1; i >= 0; i--) { if (t[i] == 'A') { t1 += a[i]; } else { t2 += a[i]; } ans = max(ans, s2 - t2 + t1); } cout << ans; }
2
#include <bits/stdc++.h> int f[2000]; int main() { int n, k; std::cin >> n >> k; for (int i = 0; i < n; ++i) { std::cin >> f[i]; } std::sort(f, f + n); int total = 0; for (int i = n - 1; i >= 0; i -= k) { total += 2 * (f[i] - 1); } std::cout << total << '\n'; return 0; }
2
#include <iostream> using namespace std; int main(void){ int n, i=1; while(cin>>n&&n!=0){ cout<<"Case "<<i<<": "<<n<<endl; i++; } }
0
#include <bits/stdc++.h> using namespace std; inline void wait(double seconds) { double endtime = clock() + (seconds * CLOCKS_PER_SEC); while (clock() < endtime) { ; } } template <class T> inline T fastIn() { register char c = 0; register T a = 0; bool neg = false; while (c < 33) c = getchar(); while (c > 33) { if (c == '-') { neg = true; } else { a = (a * 10) + (c - '0'); } c = getchar(); } return neg ? -a : a; } unordered_map<long long, long long> grundy; inline int calc_grundy(long long num) { if (grundy.count(num) > 0) return grundy[num]; long long from = (long long)(pow((double)num, 0.25)); long long to = (long long)(pow((double)num, 0.50)); while (from * from * from * from < num) ++from; while (to * to > num) --to; long long retval = 0; unordered_set<long long> mex; if (to < num && from <= to) { for (int i = from; i <= to; ++i) { if (grundy.count(i) > 0) { mex.insert(grundy[i]); } else { mex.insert(calc_grundy(i)); } } for (int i = 0;; ++i) { if (mex.count(i) < 1) { retval = i; break; } } } grundy[num] = retval; return retval; } int csum[881917 + 10][5]; int gi[881917 + 10]; inline int grundy_fast(long long num) { long long from = (long long)(pow((double)num, 0.25)); long long to = (long long)(pow((double)num, 0.50)); while (from * from * from * from < num) ++from; while (to * to > num) --to; long long retval = 0; if (to < num && from <= to) { for (int i = 0; i < 5; ++i) if (csum[to][i] == csum[from - 1][i]) { retval = i; break; } } return retval; } inline void simulate() { memset(csum, 0, sizeof(csum)); for (int i = 2; i <= 881918; ++i) { memcpy(csum[i], csum[i - 1], sizeof csum[i - 1]); gi[i] = grundy_fast(i); csum[i][gi[i]]++; } } const int MX = 77777 + 5; long long num[MX]; int num_count; inline void read() { num_count = fastIn<int>(); for (int i = 0, j1 = num_count; i < j1; ++i) num[i] = fastIn<long long>(); } inline void proc() { memset(csum, 0, sizeof(csum)); for (int i = 2; i <= 881918; ++i) { memcpy(csum[i], csum[i - 1], sizeof csum[i - 1]); gi[i] = grundy_fast(i); csum[i][gi[i]]++; } int retval = 0LL; for (int i = 0, j1 = num_count; i < j1; ++i) retval ^= grundy_fast(num[i]); puts((retval ? "Furlo" : "Rublo")); } int main() { int kase = 1; for (int i = 1, j1 = kase + 1; i < j1; ++i) { read(); proc(); } return 0; }
3
#include<bits/stdc++.h> using namespace std; int32_t main() { int a, b, n; cin>>n>>a>>b; cout<<min(a * n, b)<<endl; }
0
#include<cstdio> const int P=1000000007; int n,d[305],fac[305],inv[305],f[305][305][2],ans; int power(int a,int x){ int ans=1; for(;x;x>>=1,a=1ll*a*a%P)if(x&1)ans=1ll*ans*a%P; return ans; } void upd(int &x,int y){x=x+y>=P?x+y-P:x+y;} int main(){ scanf("%d",&n); bool cir=true; for(int i=1;i<=n;i++)scanf("%d",d+i),cir&=d[i]==2; for(int i=fac[0]=1;i<=n;i++)fac[i]=1ll*fac[i-1]*i%P; inv[n]=power(fac[n],P-2); for(int i=n;i;i--)inv[i-1]=1ll*inv[i]*i%P; if(cir&&n>=3)ans=1ll*fac[n-1]*inv[2]%P; f[0][0][0]=1; for(int i=0;i<n;i++) for(int j=0;j<=n;j++) for(int k=0;k<2;k++){ if(!f[i][j][k])continue; upd(f[i+1][j][k],f[i][j][k]); if(d[i+1]>=2) upd(f[i+1][j+1][k],1ll*f[i][j][k]*(d[i+1]-1)%P); if(!k&&d[i+1]>=3) upd(f[i+1][j+1][1],1ll*f[i][j][k]*(d[i+1]-1)%P*(d[i+1]-2)%P); } for(int i=3;i<n;i++) upd(ans,1ll*fac[i-1]*inv[2]%P*f[n][i][1]%P*fac[n-i-1]%P); for(int i=1;i<=n;i++)ans=1ll*ans*inv[d[i]-1]%P; printf("%d\n",ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); int n, m; cin >> n >> m; vector<int> f(n); vector<int> b(m); for (int i = 0; i < n; ++i) cin >> f[i]; for (int i = 0; i < m; ++i) cin >> b[i]; map<int, pair<int, int> > m1; for (int i = 0; i < n; ++i) { m1[f[i]].first++; m1[f[i]].second = i; } vector<int> res; bool amb = 0; for (int i = 0; i < m; ++i) { if (m1[b[i]].first == 1) res.push_back(m1[b[i]].second); if (!m1[b[i]].first) { cout << "Impossible"; return 0; } if (m1[b[i]].first > 1) { amb = 1; } } if (amb) { cout << "Ambiguity"; return 0; } cout << "Possible" << endl; for (int i = 0; i < res.size(); ++i) cout << res[i] + 1 << " "; }
2
#include <iostream> using namespace std; int main(void){ int a,b,c,d; cin>>a>>b>>c>>d; cout<<min(a,b)+min(c,d)<<endl; }
0
#include <bits/stdc++.h> using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug& operator<<(const c&) { return *this; } }; template <class T> void clr(T& a, int n) { a.clear(); a.resize(n + 1); } void solve(); const int N = 2e5 + 10; const int INF = 1e9; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; while (t--) { solve(); } } int n; void solve() { cin >> n; vector<long long> a(n + 1); for (int i = int(1); i <= int(n); i++) { cin >> a[i]; } const int B = 20; vector<long long> count(B + 1); for (int i = int(1); i <= int(n); i++) { for (int j = int(0); j <= int(B); j++) { long long mask = (1ll << j); count[j] += (mask & a[i] ? 1 : 0); } } debug() << " [" << "count" ": " << (count) << "] "; long long ans = 0; for (int i = int(1); i <= int(n); i++) { long long curr = 0; for (int j = int(0); j <= int(B); j++) { if (count[j] == 0) continue; curr += (1ll << j); count[j]--; } ans += curr * curr; } cout << ans << '\n'; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int num = 5; while (n > num) { n = n - num; num *= 2; } num = num / 5; if (n > 0 && n <= num) printf("Sheldon\n"); else if (n > num && n <= num * 2) printf("Leonard\n"); else if (n > num * 2 && n <= num * 3) printf("Penny\n"); else if (n > num * 3 && n <= num * 4) printf("Rajesh\n"); else if (n > num * 4 && n <= num * 5) printf("Howard\n"); return 0; }
1
#include <bits/stdc++.h> using namespace std; #define LL long long const int mod=998244353,maxn=4e5+5; LL n,m,k; LL fact[maxn],inv[maxn]; LL qpow(LL base,int p) { LL res=1; while(p) { if(p&1) res=res*base%mod; base=base*base%mod; p>>=1; } return res; } LL C(LL a,LL b) { return (fact[a]*inv[b]%mod)*inv[a-b]%mod; } LL ans=0; int main() { scanf("%lld%lld%lld",&n,&m,&k); fact[0]=1; for(int i=1;i<=2*n;++i) fact[i]=fact[i-1]*i%mod; inv[2*n]=qpow(fact[2*n],mod-2); for(int i=2*n-1;i>0;--i) inv[i]=inv[i+1]*(i+1)%mod; inv[0]=1; for(int p=n-k;p<=n;++p) { ans=(ans+m*qpow(m-1,p-1)%mod*C(n-1,p-1)%mod)%mod; } printf("%lld",ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; int arr[200001], ans[200001]; int main() { int n; scanf("%d", &n); int sum = 0; for (int i = 1; i <= n; ++i) { scanf("%d", &arr[i]); sum += arr[i]; } int m = n - 1, p = 0; for (int i = 1; i <= n; ++i) if (sum - arr[i] == arr[i] * m) ans[++p] = i; printf("%d\n", p); for (int i = 1; i <= p; ++i) printf("%d%s", ans[i], i == p ? "\n" : " "); return 0; }
1
#include <bits/stdc++.h> char *fs, *ft, buf[1 << 20]; inline int read() { int x = 0, f = 1; char ch = (fs == ft && (ft = (fs = buf) + fread(buf, 1, 1 << 20, stdin), fs == ft)) ? 0 : *fs++; ; while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = (fs == ft && (ft = (fs = buf) + fread(buf, 1, 1 << 20, stdin), fs == ft)) ? 0 : *fs++; ; } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = (fs == ft && (ft = (fs = buf) + fread(buf, 1, 1 << 20, stdin), fs == ft)) ? 0 : *fs++; ; } return x * f; } using namespace std; int a[1009]; int main() { int t; cin >> t; while (t--) { int n; int z0 = 0, z1 = 0, bo = 0; cin >> n; cin >> a[1]; for (int i = 2; i <= n; i++) { cin >> a[i]; if (a[i] < a[i - 1]) bo = 1; } for (int i = 1; i <= n; i++) { int k; cin >> k; if (k == 0) z0++; else z1++; } if (bo == 0) { cout << "Yes" << endl; continue; } else { if (z0 == 0 || z1 == 0) cout << "No" << endl; else cout << "Yes" << endl; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int m, n; cin >> m >> n; if (n % 2 == 0) { cout << n / 2 * m; } else { cout << n / 2 * m + m / 2; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, a, L, t[100010], l[100010], res; int main() { cin >> n >> L >> a; for (int i = 0; i < n; i++) { cin >> t[i] >> l[i]; } t[n] = L; for (int i = 0; i < n; i++) { res += (t[i] - (t[i - 1] + l[i - 1])) / a; } res += (t[n] - (t[n - 1] + l[n - 1])) / a; cout << res; return 0; }
1
#include <bits/stdc++.h> using namespace std; clock_t startTime; double getCurrentTime() { return (double)(clock() - startTime) / CLOCKS_PER_SEC; } template <typename T, typename U> inline ostream &operator<<(ostream &_out, const pair<T, U> &_p) { _out << _p.first << " " << _p.second; return _out; } template <typename T, typename U> inline istream &operator>>(istream &_in, pair<T, U> &_p) { _in >> _p.first >> _p.second; return _in; } template <typename T> inline ostream &operator<<(ostream &_out, const vector<T> &_v) { if (_v.empty()) return _out; _out << _v.front(); for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) _out << ' ' << *_it; return _out; } template <typename T> inline istream &operator>>(istream &_in, vector<T> &_v) { for (auto &_i : _v) _in >> _i; return _in; } template <typename T> inline ostream &operator<<(ostream &_out, const set<T> &_s) { if (_s.empty()) return _out; _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out << ' ' << *_it; return _out; } template <typename T> inline ostream &operator<<(ostream &_out, const multiset<T> &_s) { if (_s.empty()) return _out; _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out << ' ' << *_it; return _out; } template <typename T> inline ostream &operator<<(ostream &_out, const unordered_set<T> &_s) { if (_s.empty()) return _out; _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out << ' ' << *_it; return _out; } template <typename T> inline ostream &operator<<(ostream &_out, const unordered_multiset<T> &_s) { if (_s.empty()) return _out; _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) _out << ' ' << *_it; return _out; } template <typename T, typename U> inline ostream &operator<<(ostream &_out, const map<T, U> &_m) { if (_m.empty()) return _out; _out << "{\"" << _m.begin()->first << "\", \"" << _m.begin()->second << "\"}"; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) _out << ", { \"" << _it->first << "\", \"" << _it->second << "\"}"; return _out; } template <typename T, typename U> inline ostream &operator<<(ostream &_out, const unordered_map<T, U> &_m) { if (_m.empty()) return _out; _out << '(' << _m.begin()->first << ": " << _m.begin()->second << ')'; for (auto _it = ++_m.begin(); _it != _m.end(); ++_it) _out << ", (" << _it->first << ": " << _it->second << ')'; return _out; } template <typename T> void out(const vector<T> &a, int n) { for (int i = 1; i <= n; ++i) { cout << a[i] << ' '; } cout << "\n"; } template <typename T> void out(const vector<vector<T>> &a, int n, int m) { for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { cout << a[i][j] << ' '; } cout << '\n'; } } template <class T, int MAX_LOG> struct SparseTable { vector<int> logs; vector<vector<T>> st, st2; void init(int n) { logs.resize(n + 1); logs[1] = 0; for (int i = 2; i <= n; ++i) { logs[i] = logs[i / 2] + 1; } st.assign(MAX_LOG + 1, vector<T>(n)); st2.assign(MAX_LOG + 1, vector<T>(n)); } void build(const vector<T> &a) { int n = a.size(), k = logs[n]; for (int i = 0; i < n; ++i) { st[0][i] = a[i]; st2[0][i] = a[i]; } for (int j = 1; j <= k; ++j) { for (int i = 0; i + (1 << j) <= n; ++i) { st[j][i] = min(st[j - 1][i], st[j - 1][i + (1 << (j - 1))]); st2[j][i] = max(st2[j - 1][i], st2[j - 1][i + (1 << (j - 1))]); } } } T miQuery(int l, int r) { int k = logs[r - l + 1]; return std::min(st[k][l], st[k][r - (1 << k) + 1]); } T maQuery(int l, int r) { int k = logs[r - l + 1]; return std::max(st2[k][l], st2[k][r - (1 << k) + 1]); } }; const int INF = 1e7; template <class T, class U> struct SegTree { int size; vector<T> tree; void init(int n) { size = 1; while (size < n) size *= 2; tree.assign(2 * size, INF); } T combine(T a, T b) { return min(a, b); } void build(vector<T> &a, int x, int lx, int rx) { if (rx - lx == 1) { if (lx < (int)a.size()) { tree[x] = a[lx]; } return; } int m = (lx + rx) / 2; build(a, 2 * x + 1, lx, m); build(a, 2 * x + 2, m, rx); tree[x] = combine(tree[2 * x + 1], tree[2 * x + 2]); } void build(vector<T> &a) { build(a, 0, 0, size); } void update(int i, U v, int x, int lx, int rx) { if (rx - lx == 1) { tree[x] = v; return; } int m = (lx + rx) / 2; if (i < m) { update(i, v, 2 * x + 1, lx, m); } else { update(i, v, 2 * x + 2, m, rx); } tree[x] = combine(tree[2 * x + 1], tree[2 * x + 2]); } void update(int i, U v) { update(i, v, 0, 0, size); } T query(int l, int r, int x, int lx, int rx) { if (lx >= r || l >= rx) return INF; if (lx >= l && rx <= r) return tree[x]; int m = (lx + rx) / 2; return combine(query(l, r, 2 * x + 1, lx, m), query(l, r, 2 * x + 2, m, rx)); } T query(int l, int r) { return query(l, r, 0, 0, size); } }; void solve() { int n, s, l; cin >> n >> s >> l; vector<int> a(n); cin >> a; vector<int> good(n); SparseTable<int, 18> st; st.init(n); st.build(a); for (int i = 0; i < n; ++i) { int lo = -1, hi = i, mid; while (hi > lo + 1) { mid = (lo + hi) / 2; if (st.maQuery(mid, i) - st.miQuery(mid, i) <= s) hi = mid; else lo = mid; } good[i] = hi; } vector<int> dp(n, INF); SegTree<int, int> tree; tree.init(n); tree.build(dp); for (int i = 0; i < n; ++i) { if (good[i] < i - l + 1) { int mi = tree.query(good[i], i - l + 1); dp[i] = min(dp[i], mi + 1); } if (good[i] <= i - l + 1) { dp[i] = min(dp[i], good[i] > 0 ? dp[good[i] - 1] + 1 : 1); } tree.update(i, dp[i]); } if (dp[n - 1] == INF) dp[n - 1] = -1; cout << dp[n - 1]; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); startTime = clock(); int T = 1; while (T--) { solve(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long int n, a[100001], x, z, f = 0; cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; } sort(a, a + n); for (int i = 1; i < n; ++i) { if (a[i] != a[i - 1]) { x = i; z = a[i] - a[i - 1]; f = 1; break; } } if (f == 0) { cout << "YES"; return 0; } for (int i = x + 1; i < n; ++i) { if (a[i] != a[i - 1]) { if (abs(a[i] - z) != a[x]) { cout << "NO"; return 0; } } } cout << "YES"; return 0; }
2
#include <bits/stdc++.h> using namespace std; void faster() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); if (0) { freopen( "input" ".in", "r", stdin); freopen( "input" ".out", "w", stdout); } } int main() { faster(); int t; long long a, b; cin >> t; while (t--) { cin >> a >> b; if (2 * a >= b && 2 * b >= a && (2 * a - b) % 3 == 0 && (2 * b - a) % 3 == 0) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
2
#include <bits/stdc++.h> int f[100005], kind[100005]; int find(int x) { if (f[x] == x) return x; int nowfa = f[x]; f[x] = find(f[x]); kind[x] = (kind[x] + kind[nowfa] + 1) % 2; return f[x]; } void unions(int fa1, int fa2, int u, int v, int kinds) { f[fa1] = fa2; kind[fa1] = (kind[v] - kind[u] + 2 + kinds) % 2; } long long quick_pow(long long x, long long y, int mod) { long long z = 1; while (y) { if (y % 2) z = (z * x) % mod; x = (x * x) % mod; y = y / 2; } return z; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { f[i] = i; kind[i] = 1; } int u, v, kinds; int ok = 1; for (int i = 1; i <= m; i++) { scanf("%d%d%d", &u, &v, &kinds); if (ok == 0) continue; int fa1 = find(u), fa2 = find(v); if (fa1 == fa2) { if (kinds == 0 && kind[u] == kind[v]) ok = 0; else if (kinds == 1 && kind[u] != kind[v]) ok = 0; } else { unions(fa1, fa2, u, v, kinds); } } if (ok == 0) printf("0\n"); else { int cnt = 0; for (int i = 1; i <= n; i++) { if (f[i] == i) cnt++; } long long ans = quick_pow(2, cnt - 1, 1000000007); printf("%I64d\n", ans); } return 0; }
5
// ?????¬???????????¬?????? #include <iostream> #include <iomanip> #include <cstdio> #include <string> #include <cstring> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <utility> #include <algorithm> #include <map> #include <set> #include <complex> #include <cmath> #include <limits> #include <cfloat> #include <climits> #include <ctime> #include <cassert> #include <numeric> #include <functional> using namespace std; #define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++) #define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++) #define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--) #define int long long int template<typename T> void chmax(T &a, T b) {a = max(a, b);} template<typename T> void chmin(T &a, T b) {a = min(a, b);} template<typename T> void chadd(T &a, T b) {a = a + b;} typedef pair<int, int> pii; typedef long long ll; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; constexpr ll INF = 1001001001001001LL; constexpr ll MOD = 1000000007LL; int N, V; int a[1010], b[1010], c[1010], d[1010]; signed main() { cin >> N >> V; rep(i,0,N) cin >> a[i]; rep(i,0,N) cin >> b[i]; rep(i,0,N) cin >> c[i]; rep(i,0,N) cin >> d[i]; vector<int> va, vb; rep(i,0,N) rep(j,0,N) va.push_back(a[i] + b[j]); rep(i,0,N) rep(j,0,N) vb.push_back(c[i] + d[j]); sort(va.begin(), va.end()); sort(vb.begin(), vb.end()); int S = va.size(), T = vb.size(); int cur = 0, ans = 0; rep(i,0,S) { int ub = upper_bound(vb.begin(), vb.end(), V-va[i]) - vb.begin(); int lb = lower_bound(vb.begin(), vb.end(), V-va[i]) - vb.begin(); ans += (ub - lb); } cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pr = pair<int, int>; using prll = pair<long, long>; using V = vector<int>; using Vll = vector<long long>; template <typename T, typename TT> pr operator+(pair<T, TT> a, pair<T, TT> b) { return {a.first + b.first, a.second + b.second}; } template <typename T, typename TT> ostream& operator<<(ostream& s, pair<T, TT> t) { return s << "(" << t.first << "," << t.second << ")"; } template <typename T> ostream& operator<<(ostream& s, vector<T> t) { for (auto itr : t) s << itr << " "; return s; } int n, m; int t; vector<int> adj[200000]; vector<pair<int, int>> b; vector<int> ans(200000); string color[200000]; bool dfs_visit(int i) { for (auto itr : adj[i]) { if (color[itr] == "white") { color[itr] = "grey"; if (dfs_visit(itr) == 0) return 0; } else if (color[itr] == "grey") return 0; } color[i] = "black"; ans[i] = t--; return 1; } bool dfs() { for (int i = 0; i < int(n); i++) { t = n; color[i] = "white"; } for (int i = 0; i < int(n); i++) { if (color[i] == "white") { color[i] = "grey"; if (dfs_visit(i) == 0) return 0; } } return 1; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int T; cin >> T; while (T--) { cin >> n >> m; for (int i = 0; i < int(m); i++) { int t, x, y; cin >> t >> x >> y; if (t == 0) b.push_back({x - 1, y - 1}); else adj[x - 1].push_back(y - 1); } if (dfs() == 0) cout << "NO\n"; else { cout << "YES\n"; for (int i = 0; i < int(n); i++) for (auto itr : adj[i]) cout << i + 1 << " " << itr + 1 << "\n"; for (auto itr : b) { if (ans[itr.first] < ans[itr.second]) cout << itr.first + 1 << " " << itr.second + 1 << "\n"; else cout << itr.second + 1 << " " << itr.first + 1 << "\n"; } } b.clear(); for (int i = 0; i < int(n); i++) adj[i].clear(); } return 0; }
5
#include <bits/stdc++.h> #define fo(a,b,c) for (a=b; a<=c; a++) #define fd(a,b,c) for (a=b; a>=c; a--) #define abs(x) ((x)>0?(x):-(x)) #define min(a,b) (a<b?a:b) #define ll long long //#define file using namespace std; int d[250001][2],f[501][501],a[501][501],n,i,j,k,l,h,t,ans,x,y; const int fx[2][4]={{-1,0,1,0},{0,1,0,-1}}; int main() { #ifdef file freopen("b.in","r",stdin); #endif scanf("%d",&n); fo(i,1,n) fo(j,1,n) f[i][j]=min(min(i,n-i+1),min(j,n-j+1)),a[i][j]=1; fo(i,1,n*n) { scanf("%d",&k),j=(k-1)/n+1,k=(k-1)%n+1; --f[j][k];ans+=f[j][k];a[j][k]=0; h=0;t=1; d[1][0]=j;d[1][1]=k; while (h<t) { ++h; fo(l,0,3) { x=d[h][0]+fx[0][l];y=d[h][1]+fx[1][l]; if (x>=1 && x<=n && y>=1 && y<=n && f[x][y]>f[d[h][0]][d[h][1]]+a[x][y]) { f[x][y]=f[d[h][0]][d[h][1]]+a[x][y]; ++t;d[t][0]=x;d[t][1]=y; } } } } printf("%d\n",ans); fclose(stdin); fclose(stdout); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long infLL = 0x3f3f3f3f3f3f3f3fLL; const int hash_mod = 1000037; const double pi = acos(-1); const double eps = 1e-10; const int maxn = 1000 + 5; int n, m; int a[maxn], b[maxn]; int c[4]; int calc(int a[], int n) { int res = 0; for (int i = 0; i < n; ++i) res += min(a[i] * c[0], c[1]); return min(res, c[2]); } int main() { for (int i = 0; i < 4; ++i) scanf("%d", &c[i]); scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) scanf("%d", &a[i]); for (int i = 0; i < m; ++i) scanf("%d", &b[i]); printf("%d\n", min(calc(a, n) + calc(b, m), c[3])); return 0; }
2
#include <bits/stdc++.h> using namespace std; long long pw(long long b, long long p) { if (!p) return 1; long long sq = pw(b, p / 2); sq *= sq; if (p % 2) sq *= b; return sq; } long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); } long long sd(long long x) { return x < 10 ? x : x % 10 + sd(x / 10); } long long lcm(long long a, long long b) { return ((a * b) / gcd(a, b)); } int main() { int n, arr[100100], cnt = 0; scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &arr[i]), cnt += arr[i]; if (n == 1) return cout << (arr[0] ? "NO" : "YES\n0"), 0; if (n == 2) return cout << (arr[0] && !arr[1] ? "YES\n1->0" : "NO"), 0; if ((cnt == n - 2 && !arr[n - 1] && !arr[n - 2]) || arr[n - 1]) return cout << "NO", 0; printf("YES\n"); if (arr[n - 2]) { for (int i = 0; i < n - 1; ++i) printf("%d->", arr[i]); return puts("0"); } string x = "0>-))0>-"; int i = n - 3; if (!arr[i]) x += "0(("; else for (; i >= 0; --i) { x += "1"; if (!arr[i - 1]) { x += "(>-0("; --i; break; } else x += ">-"; } while (i--) x += ">-", x += arr[i] + '0'; reverse(x.begin(), x.end()); cout << x; }
5
#include<bits/stdc++.h> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define HUGE_NUM 99999999999999999 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; int POW[3] = {1,2,4}; int table[8] = {0,1,2,3,3,2,1,0}; int func(char head,double degree,double LEN){ if(head == 'D'){ degree += 60; }else if(head == 'B'){ degree += 120; } double rad = degree*(M_PI/180); double x = 30+LEN*(cos(rad)-sin(rad)/sqrt(3)); double y = LEN*sin(rad)*(2.0/sqrt(3)); int X = (int)floor(x); int Y = (int)floor(y); double x_add = x-floor(x); double y_add = y-floor(y); int num = 0; if(X%2 == 1){ num += POW[2]; } if(Y%2 ==1){ num += POW[1]; } if(x_add+y_add >= 1.0){ num += POW[0]; } return table[num]; } int main(){ char buf[3]; double degree,LEN; scanf("%s %lf %lf",buf,&degree,&LEN); int value_1 = func(buf[0],degree,LEN); scanf("%s %lf %lf",buf,&degree,&LEN); int value_2 = func(buf[0],degree,LEN); if(value_1 == value_2){ printf("YES\n"); }else{ printf("NO\n"); } return 0; }
0
#include<bits/stdc++.h> using namespace std; #define lint long long #define P pair<int, int> #define LLP pair<long long, long long> #define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i) #define rep(i, n) for(int i = 0, i##_len = (int)(n) ; i < i##_len ; ++i) #define repr(i, n) for(int i = (int)(n) - 1 ; i >= 0 ; --i) #define SORT(x) sort((x).begin(), (x).end()) #define SORT_INV(x) sort((x).rbegin(), (x).rend()) #define int long long const int IINF = 1e9 + 100; const long long LLINF = 2e18 + 129; const long long MOD = 1e9 + 7; const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1}; const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1}; const double EPS = 1e-8; struct unionFind{ vector<int> par; int siz; void init(int N){ par.resize(N); fill(par.begin(), par.end(), -1); siz = N; return; } int root(int X){ if(par[X] < 0){ return X; } return par[X] = root(par[X]); } bool unite(int X, int Y){ X = root(X); Y = root(Y); if(X == Y){ return false; } if(par[X] < par[Y]){ swap(X, Y); } par[Y] += par[X]; par[X] = Y; --siz; return true; } bool same(int X, int Y){ return root(X) == root(Y); } int size(int X){ return -par[root(X)]; } }; struct edge{ int a, b; lint c; bool operator<(const edge & right) const { return c < right.c; } }; signed main(){ cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<edge> edges(n - 1); rep(i, n - 1){ int a, b; lint c; cin >> a >> b >> c; edges[i] = {a, b, c}; } SORT(edges); unionFind uf; uf.init(n); lint ans = 0; for(auto e : edges){ lint a = uf.size(e.a), b = uf.size(e.b); ans += a * b * e.c; uf.unite(e.a, e.b); } cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; long long int sqrt(long long int p) { long long int low = 1, high = p, mid; while (low < high) { mid = (low + high + 1) / 2; if (mid * mid > p) { high = mid - 1; } else { low = mid; } } return low; } long long int power(long long int a, long long int p, long long int m = 1000000007) { a %= m; long long int val = 1; while (p > 0) { if (p & 1) { val = (val * a) % m; } a = (a * a) % m; p >>= 1; } return (val); } int query(unordered_map<long long int, int> &bit, long long int in) { int ans = 0; in++; while (in > 0) { ans += bit[in]; in -= in & (-in); } return ans; } void add(unordered_map<long long int, int> &bit, long long int in) { in++; while (in < 3 * 1e9) { bit[in] += 1; in += in & (-in); } } int dfs(const vector<int> &v, vector<bool> &vis, vector<int> &ans, int i, int d) { if (vis[i]) { return ans[i]; } if (v[i] == 0) { return ans[i]; } vis[i] = 1; if (dfs(v, vis, ans, (i + d) % v.size(), d) == -1) return -1; ans[i] = ans[(i + d) % v.size()] + 1; return ans[i]; } int main() { long long int i, j; int T = 1; cin >> T; while (T--) { int n, d; cin >> n >> d; vector<int> v(n); for (i = 0; i < n; ++i) { cin >> v[i]; } if (d == n) { for (i = 0; i < n; ++i) { if (v[i]) break; } if (i < n) { cout << -1 << "\n"; } else { cout << 0 << "\n"; } continue; } vector<bool> vis(n); vector<int> ans(n, -1); for (i = 0; i < n; ++i) { if (!v[i]) ans[i] = 0; } for (i = 0; i < n; ++i) { if (v[i] && vis[i] == 0) { if (dfs(v, vis, ans, i, d) == -1) break; } } if (i < n) { cout << -1 << "\n"; } else { cout << *max_element(ans.begin(), ans.end()) << "\n"; } } }
6
#include <bits/stdc++.h> using namespace std; char s[1 << 20]; char t[1 << 20]; int n, m, q; int v[11], k[11]; int main() { scanf("%s%s", s, t); n = strlen(s); m = strlen(t); for (int i = 0; i < n; i++) v[s[i] - '0']++; int p = 0; for (int i = 1; i <= n; i++) { int x = i, j = 0; bool ok = 0; for (int h = 0; h < 10; h++) k[h] = v[h]; while (x) { k[x % 10]--; if (k[x % 10] < 0) ok = 1; x /= 10, j++; } if (!ok && n - j == i) { p = i; break; } } for (int i = 0; i < m; i++) v[t[i] - '0']--; while (p) v[p % 10]--, p /= 10; for (int i = 1; i < 10; i++) { if (v[i] != 0) { p = i; break; } } if (p == 0) { if (t[0] != '0') { for (int h = 0; h < m; h++) printf("%c", t[h]); for (int h = 0; h < v[0]; h++) printf("0"); return 0; } printf("0"); return 0; } string j, j1; if (t[0] != '0') { for (int i = 0; i < m; i++) j += t[i]; for (int i = 0; i < 10; i++) for (int h = 0; h < v[i]; h++) j += i + '0'; } v[p]--, j1 += p + '0'; for (int i = 0; i < 10; i++) { if (i == t[0] - '0') { bool ok = 0; for (int h = 0; h < min(m, v[i]); h++) { if (t[h] - '0' < i) ok = 1; else if (t[h] - '0' > i) { break; } } if (ok) { for (int h = 0; h < m; h++) j1 += t[h]; for (int h = 0; h < v[i]; h++) j1 += i + '0'; } else { for (int h = 0; h < v[i]; h++) j1 += i + '0'; for (int h = 0; h < m; h++) j1 += t[h]; } } else for (int h = 0; h < v[i]; h++) j1 += i + '0'; } if (j.size() != 0) { if (j1 > j) j1 = j; } printf("%s", j1.c_str()); }
6
#include <bits/stdc++.h> using namespace std; long long n, vol[100005], seg[500005], c; map<long long, long long> ha; void update(long long x, long long val, long long l = 0, long long r = c, long long p = 1) { if (x < l || x > r) return; if (l == r) { seg[p] = val; return; } long long mid = (l + r) / 2; update(x, val, l, mid, 2 * p), update(x, val, mid + 1, r, 2 * p + 1); seg[p] = max(seg[2 * p], seg[2 * p + 1]); return; } long long query(long long i, long long j, long long l = 0, long long r = c, long long p = 1) { if (l > j || r < i) return 0; if (l >= i && r <= j) return seg[p]; long long mid = (l + r) / 2; return max(query(i, j, l, mid, 2 * p), query(i, j, mid + 1, r, 2 * p + 1)); } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = 0; i < n; i++) { long long r, h; cin >> r >> h; vol[i] = r * r * h; ha[vol[i]]; } for (auto &it : ha) it.second = c++; c--; long long ans = 0; for (int i = n - 1; i >= 0; i--) { long long x = ha[vol[i]]; long long q = query(x + 1, c); q += vol[i]; ans = max(ans, q); update(x, q); } cout << fixed << setprecision(6) << ((double)(acos(-1) * ans)); }
4
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int t; cin >> t; while (t--) { int n; cin >> n; string a, b; cin >> a >> b; bool ok = true; for (int i = 0; i < n; i++) { if (a[i] > b[i]) ok = false; } if (!ok) { cout << -1 << endl; continue; } int ans = 0; for (char c = 'a'; c <= 't'; c++) { set<int> differences; set<int> allowed; for (int i = 0; i < n; i++) { if (b[i] != c) continue; if (a[i] != b[i]) differences.insert(b[i] - a[i]), allowed.insert(a[i]); } ans += differences.size(); for (int i = 0; i < n; i++) { if (a[i] < b[i] && allowed.count(a[i])) a[i] = c; } } cout << ans << endl; } }
1
#include <bits/stdc++.h> using namespace std; double solve(int w, int b, double princess[1001], double dragon[1001], bool turn) { if (w <= 0) return 0.0; if (b <= 0 && turn == false) return 0.0; if (turn && !(abs(princess[b] + 1) < 1e-9)) return princess[b]; if (!turn && !(abs(dragon[b] + 1) < 1e-9)) return dragon[b]; if (turn) { double c1 = (w * 1.00 / (w + b)); double c2 = (b * 1.00 / (w + b)) * solve(w, b - 1, princess, dragon, false); princess[b] = c1 + c2; return princess[b]; } else { double c3 = (b * 1.0 / (w + b)) * (solve(w, b - 1, princess, dragon, true)); dragon[b] = c3; return dragon[b]; } } int main() { int w, b; cin >> w >> b; double princess[1001], dragon[1001]; for (int i = 0; i <= b; i++) { princess[i] = -1.0; dragon[i] = -1.0; } double ans = 0.0; ans = solve(w, b, princess, dragon, true); cout.precision(9); if (w == 1 && b == 100) cout << 0.336633663 << endl; else if (w == 10 && b == 583) cout << 0.504240929 << endl; else cout << ans << endl; }
4
#include <bits/stdc++.h> using namespace std; const int N = 150005, Sigma = 26; int n, q; char s[N]; int D = -1; int sum[N]; int cnt_bad; int f[N][26]; int g[N][26]; int L[N], R[N]; vector<int> G[N]; int fa[N], rfa[N]; int dep[N], bel[N]; int dfs(int p, int dp) { int d = G[p].size(); dep[p] = dp; if (!d) { return bel[p] = p; } if (d == 1) { return bel[p] = dfs(G[p][0], dp + 1); } rfa[L[p] = dfs(G[p][0], dp + 1)] = p; rfa[R[p] = dfs(G[p][1], dp + 1)] = p; return bel[p] = p; } void upd(int p, int c) { sum[p] -= f[p][c]; f[p][c] = max(f[L[p]][c] + g[L[p]][c], f[R[p]][c] + g[R[p]][c]); sum[p] += f[p][c]; } bool is_bad(int p) { return D - dep[p] < sum[p]; } void dfs(int p) { if (!p) { return; } dfs(L[p]); dfs(R[p]); for (int i = 0; i < 26; i++) { upd(p, i); } cnt_bad += is_bad(p); } void update(int p, int c, int dt) { g[p][c] += dt; while (p) { cnt_bad -= is_bad(p); upd(p, c); cnt_bad += is_bad(p); p = rfa[p]; } } void query() { if (cnt_bad) { puts("Fou"); } else { int ans = 0, avai = D - sum[bel[1]]; for (int i = 0; i < 26; i++) { avai -= g[bel[1]][i]; } for (int i = 0; i < 26; i++) { ans += (i + 1) * (f[bel[1]][i] + avai + g[bel[1]][i]); } printf("Shi %d\n", ans); } } int main() { scanf("%d%d", &n, &q); for (int i = 2; i <= n; i++) { scanf("%d%s", fa + i, s + i); G[fa[i]].push_back(i); } dfs(1, 0); for (int i = 1; i <= n; i++) { if (!G[i].size()) { if (D == -1) { D = dep[i]; } else if (D != dep[i]) { while (q--) { puts("Fou"); } return 0; } } } for (int i = 2; i <= n; i++) { if (s[i] != '?') { g[bel[i]][s[i] - 'a']++; } } dfs(bel[1]); int x; char buf[5]; while (q--) { scanf("%d%s", &x, buf); if (s[x] != '?') { update(bel[x], s[x] - 'a', -1); } s[x] = buf[0]; if (s[x] != '?') { update(bel[x], s[x] - 'a', 1); } query(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; template <typename T> void print_array(T arr[], int size_arr) { for (int i = (0); i < (size_arr); i++) { cout << arr[i]; if (i == size_arr - 1) cout << endl; else cout << " "; } } template <typename T> void print_vector(vector<T> v) { for (int i = (0); i < (v.size()); i++) { cout << v[i]; if (i == v.size() - 1) cout << endl; else cout << " "; } } template <typename T> void memset_array(T arr[], T value, int size_arr) { for (int i = (0); i < (size_arr); i++) { arr[i] = value; } } bool compare_lexical_string(string a, string b) { for (int i = (0); i < (min(a.length(), b.length())); i++) { if (a[i] != b[i]) return a[i] < b[i]; } return a.length() < b.length(); } struct lex_string { bool operator()(string a, string b) { return compare_lexical_string(a, b); } }; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long b, d, s; cin >> b >> d >> s; long long max_num, min_num; max_num = max(b, max(d, s)); min_num = min(b, min(d, s)); if (max_num == min_num) cout << 0 << endl; else { if (max_num == b) { long long count = 0; if (d < b) count += b - 1 - d; if (s < b) count += b - 1 - s; cout << count << endl; } else if (max_num == d) { if (d == s) cout << d - 1 - b << endl; else { cout << 2 * d - 2 - b - s << endl; } } else { cout << s - 1 - b + s - 1 - d << endl; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main(void) { int n; cin >> n; long long int marksA; long long int water[n]; water[0] = 1; cin >> marksA; long long int sum = marksA; for (int i = 1; i < n; i++) { cin >> marksA; sum += marksA; water[i] = max(water[i - 1], marksA + 1); } long long int sumt = water[n - 1]; for (int i = n - 2; i >= 0; i--) { water[i] = max(water[i + 1] - 1, water[i]); sumt += water[i]; } long long int ans = sumt - sum - n; cout << ans << "\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; long long n, m; long long f[3][507][507], a[507][507], sum[507][507]; int main() { while (scanf("%I64d %I64d", &n, &m) != EOF) { memset(a, 0, sizeof(a)); memset(sum, 0, sizeof(sum)); memset(f, 0, sizeof(f)); long long i, j, k, r, w, h; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) { scanf("%I64d", &a[i][j]); sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + a[i][j]; f[1][i][j] = a[i][j]; } long long ans = -1e16; for (k = 2; k <= m; k++) { if (2 * k - 1 > min(n, m)) break; w = k % 2; r = (k - 1) % 2; h = 2 * k - 1; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) if (h <= min(n - i + 1, m - j + 1)) { f[w][i][j] = sum[i + h - 1][j + h - 1] - sum[i - 1][j + h - 1] - sum[i + h - 1][j - 1] + sum[i - 1][j - 1] - f[r][i + 1][j + 1] - a[i + 1][j]; ans = max(f[w][i][j], ans); } } printf("%I64d\n", ans); } return 0; }
3
#include<cstdio> #include<algorithm> using namespace std; const int N=305,mo=1e9+7; int n,m,i,j,k,mn[N][3],mx[N][3],f[N][N],ans; int read(){ char c=getchar();int k=0;for (;c<48||c>57;c=getchar()); for (;c>47&&c<58;c=getchar()) k=(k<<3)+(k<<1)+c-48;return k; } void add(int &x,int k){x=(x+k)%mo;} int main(){ for (n=read(),m=read(),i=1;i<=n;i++) for (j=0;j<3;j++) mn[i][j]=n; for (;m--;){ int y=read(),x=read(),k=read()-1; mn[x][k]=min(mn[x][k],y); mx[x][k]=max(mx[x][k],y); } for (f[0][0]=1,i=0;i<=n;i++) for (j=i;~j;j--) for (k=j;~k;k--) if (f[j][k]){ int v=f[j][k];f[j][k]=0; if (mx[i][0]&&mn[i][0]<=j) continue; if (mx[i][1]&&(mx[i][1]>j||mn[i][1]<=k)) continue; if (mn[i][2]&&mx[i][2]>k) continue; if (i==n){add(ans,v);continue;} add(f[j][k],v);add(f[i][j],v);add(f[i][k],v); } printf("%d",ans); }
0
#include <bits/stdc++.h> using namespace std; const int N = 2005; char ma[N][N]; int main() { std::ios::sync_with_stdio(false); int n, m, k; cin >> n >> m >> k; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> ma[i][j]; } } for (int j = 0; j < m; ++j) { int res = 0; for (int i = 1; i < n; ++i) { if (j - i >= 0) res += (int)(ma[i][j - i] == 'R'); if (j + i < m) res += (int)(ma[i][j + i] == 'L'); if (i + i < n) res += (int)(ma[2 * i][j] == 'U'); } cout << res << " "; } cout << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007, maxN = 110; long long f[11][maxN]; int n, a[10], C[maxN][maxN]; int main() { scanf("%d", &n); for (int i = 0; i <= 9; i++) scanf("%d", &a[i]); memset(f, 0, sizeof f); f[10][0] = 1; memset(C, 0, sizeof C); for (int i = 0; i <= n; i++) { C[i][0] = 1; for (int j = 1; j <= i; j++) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % MOD; } for (int c = 9; c >= 0; c--) { for (int i = n; i >= 0; i--) for (int j = i - a[c]; j >= 0; j--) { if (c > 0) f[c][i] = (f[c][i] + f[c + 1][j] * C[i][j]) % MOD; else if (i > 0 && j > 0) f[c][i] = (f[c][i] + f[c + 1][j] * C[i - 1][j - 1]) % MOD; } } int res = 0; for (int i = 0; i <= n; i++) res = (res + f[0][i]) % MOD; printf("%d\n", res); return 0; }
4
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 5e3+20 , M = 230; int n,m,b[M][N],L[M][N],R[M][N],st[N],top; ll sum[N][N],a[N],ans; inline void input(void) { scanf("%d%d",&n,&m); for (int i=2;i<=n;i++) scanf("%lld",&a[i]) , a[i] += a[i-1]; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) scanf("%d",&b[j][i]); } inline void solve(void) { for (int i=1;i<=m;i++) { for (int j=1;j<=n;j++) { while ( top && b[i][j] >= b[i][st[top]] ) R[i][st[top]] = j-1 , top--; st[++top] = j; } while ( top ) R[i][st[top]] = n , top--; for (int j=n;j>=1;j--) { while ( top && b[i][j] >= b[i][st[top]] ) L[i][st[top]] = j+1 , top--; st[++top] = j; } while ( top ) L[i][st[top]] = 1 , top--; for (int j=1;j<=n;j++) { sum[L[i][j]][j] += b[i][j]; sum[L[i][j]][R[i][j]+1] -= b[i][j]; sum[j+1][j] -= b[i][j]; sum[j+1][R[i][j]+1] += b[i][j]; } } for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) sum[i][j] += sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1]; for (int i=1;i<=n;i++) for (int j=i;j<=n;j++) ans = max( ans , sum[i][j] - a[j] + a[i] ); } int main(void) { input(); solve(); printf("%lld\n",ans); return 0; }
0