solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; int a[1001], n; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); a[n + 1] = 90; for (int i = 0; i <= n; i++) if (a[i] + 15 < a[i + 1]) return printf("%d", min(a[i] + 15, 90)), 0; puts("90"); }
1
#include <bits/stdc++.h> int main() { int min_q; int qn; std::cin >> qn; std::cin >> min_q; for (int i = 2; i <= qn; ++i) { int qi; std::cin >> qi; if (qi < min_q) { min_q = qi; } } int an; std::cin >> an; std::vector<int> a(an); for (int i = 0; i < an; ++i) { std::cin >> a[i]; } std::sort(a.begin(), a.end()); int answer = 0; for (int i = an - 1; i >= 0;) { int sum = 0; for (int j = i; j >= 0 && j > i - min_q; --j) { sum += a[j]; } i -= min_q + 2; answer += sum; } std::cout << answer; return 0; }
3
#include <bits/stdc++.h> #pragma GCC optimize("Ofast", "inline", "-ffast-math") #pragma GCC target("avx,sse2,sse3,sse4,mmx") #pragma GCC target("arch=corei7-avx") using namespace std; struct P { int x, y; } p[2020]; P le = (P){-int(1e9 + 7), -1}; inline P operator-(const P &a, const P &b) { return (P){a.x - b.x, a.y - b.y}; } inline long long ddd(const P &a, const P &b) { return (long long)a.x * b.x + (long long)a.y * b.y; } inline long long xxx(const P &a, const P &b) { return (long long)a.x * b.y - (long long)b.x * a.y; } struct OPT { P p; double ang; int fl; } opt[4040]; bool cmp(const OPT &a, const OPT &b) { return abs(a.ang - b.ang) > (1e-8) ? a.ang < b.ang : xxx(a.p, b.p) > 0; } int n, m, cnt; long long ans; long long C2(long long x) { return x * (x - 1) / 2; } int main() { cin >> n; for (int i = 1; i <= n; ++i) cin >> p[i].x >> p[i].y; for (int i = 1; i <= n; ++i) { m = cnt = 0; for (int j = 1; j <= n; ++j) if (j != i) { P v = p[j] - p[i]; cnt += xxx(le, v) > 0; opt[++m] = (OPT){v, atan2(v.y, v.x), -1}; v = p[i] - p[j]; opt[++m] = (OPT){v, atan2(v.y, v.x), 1}; } sort(opt + 1, opt + m + 1, cmp); for (int j = 1; j <= m; ++j) { cnt += opt[j].fl; if (opt[j].fl == -1) ans += C2(cnt) * C2(n - 2 - cnt); } } cout << ans / 2 << endl; }
6
#include "bits/stdc++.h" using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define vi vector<int> #define all(x) (x).begin(), (x).end() #define INF 4294967295 unsigned int dp[2][62][62][4000]; signed main() { int nn, kk; cin >> nn >> kk; vi data(nn); rep(i, nn) { cin >> data[i]; } vi red(kk), black(kk); int RED = 0, BLACK = 0; rep(i, kk) { cin >> red[i]; RED += red[i]; } rep(i, kk) { cin >> black[i]; BLACK += black[i]; } sort(all(data)); reverse(all(data)); sort(all(red)); reverse(all(red)); sort(all(black)); reverse(all(black)); vi ruiseki(nn + 1, 0); rep(i, nn) { ruiseki[i + 1] = ruiseki[i] + data[i]; } //cout << "hoge " << endl; //rep(j, kk + 1) { // rep(k, kk + 1) { // rep(l, RED + 1) { // dp[0][j][k][l] = INF; // } // } //} memset(dp[0], -1, sizeof(dp[0])); dp[0][0][0][0] = 0; //cout << "hoge " << endl; int ura = 0; int omote = 1; rep(i, data.size()) { int rr = ruiseki[i]; int dd = data[i]; //rep(j, kk + 1) { // rep(k, kk + 1) { // rep(l, RED + 1) { // dp[omote][j][k][l] = INF; // } // } //} memset(dp[omote], -1, sizeof(dp[omote])); rep(j, kk + 1) { rep(k, kk + 1) { for (int l = 0; l <= rr && l + dd <= RED; l++) { if (dp[ura][j][k][l] == INF || j == kk)continue; dp[omote][j + 1][k][l + dd] = min(dp[omote][j + 1][k][l + dd], dp[ura][j][k][l] + max(0, dd - red[j])); } for (int l = rr; 0 <= l && rr - l + dd <= BLACK; l--) { if (dp[ura][j][k][l] == INF || k == kk)continue; dp[omote][j][k + 1][l] = min(dp[omote][j][k + 1][l], dp[ura][j][k][l] + max(0, dd - black[k])); } } } swap(omote, ura); } unsigned int ans = INF; rep(j, kk + 1) { rep(k, kk + 1) { rep(l, RED + 1) { ans = min(ans, dp[ura][j][k][l]); } } } if (ans == INF) { cout << -1 << endl; } else { cout << ans << endl; } return 0; }
0
#include<bits/stdc++.h> using namespace std; const int INF=0x3f3f3f3f; int n,a[20],b[20],dp[1<<18][51],cnt[1<<18],ans=INF; int main() { scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d",&a[i]); for(int i=0;i<n;i++) scanf("%d",&b[i]); for(int i=1;i<1<<n;i++) cnt[i]=cnt[i>>1]+(i&1); memset(dp,0x3f,sizeof(dp)); dp[0][1]=0; for(int i=0;i<(1<<n)-1;i++) { for(int j=1;j<=50;j++) { if(dp[i][j]!=INF) { for(int k=0,w,c=cnt[i];k<n;k++) { w=(k&1)==(cnt[i]&1)?a[k]:b[k]; if(i>>k&1) c--; if(!(i>>k&1)&&w>=j) dp[i|(1<<k)][w]=min(dp[i|(1<<k)][w],dp[i][j]+c); } } } } for(int i=1;i<=50;i++) ans=min(ans,dp[(1<<n)-1][i]); printf("%d\n",ans==INF?-1:ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i, check = 0, j, temp; cin >> n >> m; int a[n], b[m]; for (i = 0; i < n; i++) cin >> a[i]; for (i = 0; i < m; i++) cin >> b[i]; for (i = 0; i < m; i++) for (j = i + 1; j < m; j++) { if (b[i] < b[j]) { temp = b[i]; b[i] = b[j]; b[j] = temp; } } for (i = 0, j = 0; i < n; i++) { if (a[i] == 0) { a[i] = b[j]; j++; } } int l = 0; for (i = 0; i < n - 1; i++) { if (a[i] > a[i + 1]) { l++; } } if (l == 0) cout << "No"; else cout << "Yes"; return 0; }
1
#include <bits/stdc++.h> using namespace std; unsigned long long d[100100], n; const unsigned long long maxn = (unsigned long long)1e18 + (unsigned long long)2; int main() { d[0] = (unsigned long long)0; d[1] = (unsigned long long)1; int k = 1; while (d[k] < maxn) { k++; d[k] = d[k - 1] + d[k - 2] + (unsigned long long)1; } cin >> n; n--; int i; for (i = 0; i <= k; i++) if (d[i] > n) break; cout << i - 1 << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 100100; const int MAXSEG = 1 << 18; const int MOD = 1000000007; long long power[MAXN]; long long pre[MAXN]; long long digit[10]; int N, M, K; string S; long long seg[MAXSEG]; long long lazy[MAXSEG]; void build_tree(int node, int left, int right) { if (left == right) { seg[node] = power[left] * digit[S[left] - '0'] % MOD; return; } int mid = (left + right) / 2; build_tree(2 * node, left, mid); build_tree(2 * node + 1, mid + 1, right); seg[node] = (seg[2 * node] + seg[2 * node + 1]) % MOD; } void down(int node, int left, int right) { if (lazy[node] != -1) { int mid = (left + right) / 2; long long hash1 = (pre[mid + 1] - pre[left]) % MOD; if (hash1 < 0) hash1 += MOD; long long hash2 = (pre[right + 1] - pre[mid + 1]) % MOD; if (hash2 < 0) hash2 += MOD; seg[2 * node] = hash1 * lazy[node] % MOD; seg[2 * node + 1] = hash2 * lazy[node] % MOD; lazy[2 * node] = lazy[2 * node + 1] = lazy[node]; lazy[node] = -1; } } void update(int node, int left, int right, int ql, int qr, int d) { if (left > right || right < ql || qr < left) return; if (ql <= left && right <= qr) { long long hash = (pre[right + 1] - pre[left]) % MOD; if (hash < 0) hash += MOD; seg[node] = hash * digit[d] % MOD; lazy[node] = digit[d]; return; } down(node, left, right); int mid = (left + right) / 2; update(2 * node, left, mid, ql, qr, d); update(2 * node + 1, mid + 1, right, ql, qr, d); seg[node] = (seg[2 * node] + seg[2 * node + 1]) % MOD; } long long query(int node, int left, int right, int ql, int qr) { if (left > right || right < ql || qr < left) return 0; if (ql <= left && right <= qr) return seg[node]; down(node, left, right); int mid = (left + right) / 2; return (query(2 * node, left, mid, ql, qr) + query(2 * node + 1, mid + 1, right, ql, qr)) % MOD; } bool check(int ql, int qr, int p) { int rem = (qr - ql + 1) % p; long long hf = query(1, 0, N - 1, ql, ql + rem - 1), extra = query(1, 0, N - 1, qr - rem + 1, qr); if (hf * power[qr - rem + 1 - ql] % MOD != extra) return false; long long hp = query(1, 0, N - 1, ql, ql + p - 1), hq = query(1, 0, N - 1, ql, qr - rem); long long nhash = (hq * power[p] % MOD + hp) % MOD; nhash = (nhash - hq) % MOD; if (nhash < 0) nhash += MOD; return nhash == hp * power[qr - rem + 1 - ql] % MOD; } int main() { ios::sync_with_stdio(0); power[0] = 1; for (int i = 1; i < MAXN; i++) power[i] = (power[i - 1] << 1) % MOD; for (int i = 1; i < MAXN; i++) pre[i] = (pre[i - 1] + power[i - 1]) % MOD; for (int i = 0; i < 10; i++) digit[i] = (rand() * (long long)MAXN % MOD + rand()) % MOD; memset(lazy, -1, sizeof(lazy)); cin >> N >> M >> K >> S; build_tree(1, 0, N - 1); for (int i = 0, type, ql, qr, x; i < M + K; i++) { cin >> type >> ql >> qr >> x; if (type == 1) update(1, 0, N - 1, ql - 1, qr - 1, x); else { if (check(ql - 1, qr - 1, x)) cout << "YES\n"; else cout << "NO\n"; } } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, k, ans = 0; cin >> n >> k; int cd[n - 1]; int arr[n], seg = n - k, beg = 1, en = 1; for (int i = 0; i < n; i++) { cin >> arr[i]; } for (int i = 0; i < n - 1; i++) { cd[i] = arr[i + 1] - arr[i]; } sort(cd, cd + n - 1, greater<int>()); for (int i = 0; i < k - 1; i++) { ans += cd[i]; } ans = arr[n - 1] - arr[0] - ans; cout << ans << "\n"; }
3
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") const long long INF = 1e18; using namespace std; long long hcf(long long a, long long b) { if (b == 0) return a; return hcf(b, a % b); } unsigned long long stringtoll(string s) { unsigned long long ans = 0; for (unsigned long long i = 0; i < s.length(); i++) { ans = ans * 10 + (s[i] - '0'); } return ans; } vector<long long> root(long long n) { vector<long long> ans; for (long long i = 2; i <= sqrt(n); i++) { if (n % i == 0) { ans.push_back(i); if (i != n / i) ans.push_back(n / i); } } return ans; } bool ifprime(long long n) { for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0) return false; } return true; } long long logat(long long n, long long base) { return (log(n) / log(base)); } long long factorial(long long n) { if (n == 0) return 1; return (n * factorial(n - 1)) % 998244353; } void baspri(bool flag) { if (flag) cout << "YES" << "\n"; else cout << "NO" << "\n"; } long long lcm(long long a, long long b) { return (a / hcf(a, b)) * b; } vector<pair<long long, long long>> prime_fact(long long n) { vector<pair<long long, long long>> ans; for (long long i = 2; i * i <= n; i++) { if (n % i == 0) { long long another = 0; while (n % i == 0) { n = n / i; another++; } ans.push_back({i, another}); } } if (n > 1) ans.push_back({n, 1}); return ans; } long long pw(long long n, long long r, long long mop) { long long res = 1; while (r != 0) { if (r % 2) { res = (res * n) % mop; r--; } n = (n * n) % mop; r = r / 2; } return res; } const int N = 2e5 + 10; long long parent[N]; long long ran[N] = {0}; long long get(long long a) { if (parent[a] == a) return a; else return parent[a] = get(parent[a]); } void unio(long long a, long long b) { a = get(a); b = get(b); if (a == b) return; if (ran[a] == ran[b]) ran[a]++; if (ran[b] > ran[a]) swap(a, b); parent[b] = a; } void solve() { long long n, m, k; cin >> n >> m >> k; vector<long long> col(n); for (long long i = 0; i < n; i++) { long long a; cin >> a; a--; col[i] = a; } for (long long i = 0; i < n; i++) parent[i] = i; set<long long> mo[n]; for (long long i = 0; i < m; i++) { long long a, b; cin >> a >> b; a--; b--; if (get(a) != get(b)) { unio(a, b); } } for (long long i = 0; i < n; i++) { mo[get(i)].insert(i); } long long ans = 0; for (long long i = 0; i < n; i++) { unordered_map<long long, long long> iop; long long tot = 0; long long mx = 0; for (auto it = mo[i].begin(); it != mo[i].end(); it++) { iop[col[*it]]++; mx = max(mx, iop[col[*it]]); tot++; } ans += tot - mx; } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); { solve(); } }
3
#include<cmath> #include<cstdio> #include<vector> #include<algorithm> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; const double EPS=1e-7; const double PI=acos(-1); template<class T> struct point{ T x,y; point operator+(const point &a)const{ return (point){x+a.x,y+a.y}; } point operator-(const point &a)const{ return (point){x-a.x,y-a.y}; } }; template<class T> point<T> operator*(T c,const point<T> &a){ return (point<T>){c*a.x,c*a.y}; } bool operator==(const point<double> &a,const point<double> &b){ return abs(a.x-b.x)<EPS && abs(a.y-b.y)<EPS; } template<class T> double abs(const point<T> &a){ return sqrt(a.x*a.x+a.y*a.y); } template<class T> T cross(const point<T> &a,const point<T> &b){ return a.x*b.y-a.y*b.x; } point<double> rot(const point<double> &a,double theta){ return (point<double>){a.x*cos(theta)-a.y*sin(theta),a.x*sin(theta)+a.y*cos(theta)}; } template<class T> double arg(const point<T> &a){ double t=atan2(a.y,a.x); return t<0?t+2*PI:t; } template<class T> struct segment{ point<T> a,b; }; enum{CCW=1,CW=-1,ON=0}; int ccw(const point<double> &a,const point<double> &b,const point<double> &c){ double rdir=cross(b-a,c-a); if(rdir> EPS) return CCW; if(rdir<-EPS) return CW; return ON; } template<class T> double dist(const point<T> &a,const point<T> &b){ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } bool cover(const segment<double> &S,const point<double> &p){ return dist(S.a,p)+dist(p,S.b)<dist(S.a,S.b)+EPS; } bool intersect(const segment<double> &S1,const segment<double> &S2){ if(max(S1.a.x,S1.b.x)+EPS<min(S2.a.x,S2.b.x) || max(S1.a.y,S1.b.y)+EPS<min(S2.a.y,S2.b.y) || max(S2.a.x,S2.b.x)+EPS<min(S1.a.x,S1.b.x) || max(S2.a.y,S2.b.y)+EPS<min(S1.a.y,S1.b.y)) return false; return ccw(S1.a,S1.b,S2.a)*ccw(S1.a,S1.b,S2.b)<=0 && ccw(S2.a,S2.b,S1.a)*ccw(S2.a,S2.b,S1.b)<=0; } point<double> get_intersect(const segment<double> &S1,const segment<double> &S2){ double a1=cross(S1.b-S1.a,S2.b-S2.a); double a2=cross(S1.b-S1.a,S1.b-S2.a); if(abs(a1)<EPS){ if(cover(S1,S2.a)) return S2.a; if(cover(S1,S2.b)) return S2.b; if(cover(S2,S1.a)) return S1.a; return S1.b; } return S2.a+a2/a1*(S2.b-S2.a); } int n,m,K; point<double> P[50]; // 村 point<double> star[50]; // 単位円周を m 等分割した点 vector< pair<double,double> > in; // 点が星の内部にあるか判定するための情報. 星の輪郭の各頂点についての <角度,中心からの距離> point<double> in2[100]; // 極座標で書かれた in を直交座標に直したもの ( 高速化のため ) void get_intersect(const point<double> *G1,const point<double> *G2,int &sz,point<double> *res){ segment<double> S1[50],S2[50]; // 星の辺 rep(i,m){ S1[i].a=G1[i]; S1[i].b=G1[(i+K)%m]; S2[i].a=G2[i]; S2[i].b=G2[(i+K)%m]; } rep(i,m) rep(j,m) if(intersect(S1[i],S2[j])) res[sz++]=get_intersect(S1[i],S2[j]); } bool cover(double R,const point<double> &cen,const point<double> &p){ if(p==cen) return true; double phi=arg(p-cen),d=dist(p,cen); if(d>R+EPS) return false; if(phi<in[0].first+EPS) phi+=2*PI; int i=upper_bound(in.begin(),in.end(),make_pair(phi,0.0))-in.begin()-1; return ccw(R*in2[i],R*in2[(i+1)%in.size()],p-cen)!=CW; } // p1, p2 : 星の境界上にある二つの村 bool check(double R,const point<double> &p1,const point<double> &p2){ point<double> G1[50],G2[50]; // 星を上下反転させて, それぞれ p1, p2 が中心に来るように平行移動したもの rep(k,m){ point<double> p=R*(point<double>){star[k].x,-star[k].y}; G1[k]=p+p1; G2[k]=p+p2; } int sz=0; static point<double> Q[50*50]; // 星の中心の候補点 get_intersect(G1,G2,sz,Q); Q[sz++]=(point<double>){0,0}; // これを入れないと間違う rep(k,sz){ bool ok=true; rep(l,n) if(!cover(R,Q[k],P[l])) { ok=false; break; } if(ok) return true; } return false; } int main(){ for(;scanf("%d%d%d",&n,&m,&K),n;){ rep(i,n) scanf("%lf%lf",&P[i].x,&P[i].y); rep(i,m) star[i]=rot((point<double>){1,0},2*i*PI/m+PI/2); in.clear(); rep(i,m){ in.push_back(make_pair(arg(star[i]),abs(star[i]))); if(K>1){ segment<double> S1={star[i],star[(i+K)%m]}; segment<double> S2={star[(i+1)%m],star[(i+1-K+m)%m]}; point<double> p=get_intersect(S1,S2); in.push_back(make_pair(arg(p),abs(p))); } } sort(in.begin(),in.end()); rep(i,in.size()){ in2[i].x=in[i].second*cos(in[i].first); in2[i].y=in[i].second*sin(in[i].first); } double ans=1e4; // p, q が半径 R1 の星の境界として採用できるとしても, 半径 R2(>R1) の星の境界に採用できるかどうかはわからない(と思う)ので多分嘘解法 rep(i,n) for(int j=i+1;j<n;j++) { // 星の境界上の二点を決めうち if(!check(ans+EPS,P[i],P[j])) continue; // 解がよくなる見込みがないので二分探索しない double lo=0,hi=ans; while(hi-lo>1e-6){ double mi=(lo+hi)/2; if(check(mi,P[i],P[j])) hi=mi; else lo=mi; } ans=(lo+hi)/2; } printf("%.9f\n",ans); } return 0; }
0
#include <iostream> #include <set> using namespace std; int main() { int n; cin>>n; while(n--){ string s; cin>>s; set<string> se; for(int i=1;i<s.size();i++){ string s1[2]={s.substr(0,i),string(s1[0].rbegin(),s1[0].rend())}; string s2[2]={s.substr(i),string(s2[0].rbegin(),s2[0].rend())}; for(int j=0;j<2;j++) for(int k=0;k<2;k++){ se.insert(s1[j]+s2[k]); se.insert(s2[k]+s1[j]); } } cout<<se.size()<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; struct Tedge { int v, pre; } edge[200010 * 2]; int n, k, en, cur[200010], val[200010], fa[200010], tot, h[200010], typ[200010], siz[200010], dis[200010], vst[200010]; int getint() { int x, f = 1; char ch; while (!isdigit(ch = getchar())) f = ch == '-' ? -1 : 1; x = ch - '0'; while (isdigit(ch = getchar())) x = x * 10 + ch - '0'; return f * x; } void addedge(int u, int v) { edge[++en] = (Tedge){v, cur[u]}, cur[u] = en; edge[++en] = (Tedge){u, cur[v]}, cur[v] = en; } bool dfs1(int p, int f, int now) { if (val[p] < now) return false; h[++tot] = p, siz[p] = 1, vst[p] = 1; bool blk = true; int ch = 0, wn = 0; for (int i = cur[p], v = edge[i].v; i; i = edge[i].pre, v = edge[i].v) if (v != f) { ch++; bool res = dfs1(v, p, now); blk &= res; if (res) siz[p] += siz[v]; else wn++; } if (blk) typ[p] = 2; else if (!f && wn == 1) { typ[p] = 2; for (int i = cur[p], v = edge[i].v; i; i = edge[i].pre, v = edge[i].v) if (typ[v] == 2) siz[p] += siz[v]; for (int i = cur[p], v = edge[i].v; i; i = edge[i].pre, v = edge[i].v) if (typ[v] == 1) siz[v] += siz[p]; } return blk; } void dfs2(int p) { for (int i = cur[p], v = edge[i].v; i; i = edge[i].pre, v = edge[i].v) if (typ[v] == 1) { p = v; break; } while (1) { int wn = 0; for (int i = cur[p], v = edge[i].v; i; i = edge[i].pre, v = edge[i].v) if (typ[v] == 1) wn++; if (wn != 1) break; typ[p] = 2; for (int i = cur[p], v = edge[i].v; i; i = edge[i].pre, v = edge[i].v) if (typ[v] == 1) { siz[v] += siz[p], p = v; break; } } } void dfs3(int p, int f, int d) { if (typ[p] != 1) return; dis[p] = (d += siz[p]); for (int i = cur[p], v = edge[i].v; i; i = edge[i].pre, v = edge[i].v) if (v != f) dfs3(v, p, d); } int find(int p) { dfs3(p, 0, 0); int ans = p; for (int i = 1; i <= tot; i++) if (typ[h[i]] == 1 && dis[h[i]] > dis[ans]) ans = h[i]; return ans; } bool check(int now) { memset(vst, 0, sizeof(vst)); for (int i = 1; i <= n; i++) typ[i] = val[i] >= now; for (int i = 1; i <= n; i++) if (typ[i] && !vst[i]) { tot = 0; dfs1(i, 0, now); if (typ[i] == 2) dfs2(i); if (tot < k) continue; int wt = -1; for (int i = 1; i <= tot; i++) if (typ[h[i]] == 1) { wt = h[i]; break; } if (wt == -1) return true; int x = find(wt); x = find(x); if (dis[x] >= k) return true; } return false; } int main() { (n = getint()), (k = getint()); for (int i = 1; i <= n; i++) (val[i] = getint()); for (int i = 1; i <= n - 1; i++) addedge(getint(), getint()); int l = 0, r = 1000000, ans; while (l <= r) if (check(((l + r) >> 1))) ans = ((l + r) >> 1), l = ((l + r) >> 1) + 1; else r = ((l + r) >> 1) - 1; printf("%d\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; for (int z = 0; z < q; z++) { int m; cin >> m; int ev = 0, od = 0; int arr1[50] = {0}, arr0[50] = {0}; int evod[50] = {-1}; string s[50]; for (int i = 0; i < m; i++) cin >> s[i]; int count0 = 0, count1 = 0; for (int i = 0; i < m; i++) { int j = 0; while (s[i][j] != '\0') { if (s[i][j] == '0') { count0++; arr0[i]++; } else { count1++; arr1[i]++; } j++; } if (j % 2 == 0) { ev++; evod[i] = 0; } else { od++; evod[i] = 1; } } int ans = 0; int r = od; if (ev == m) { if (count0 % 2 == 0 && count1 % 2 == 0) cout << m << "\n"; else cout << m - 1 << "\n"; } else { int k = 0; int ans = 0; for (k = 0; k < m; k++) { if (arr1[k] % 2 == 0 && arr0[k] % 2 == 0 && (arr0[k] + arr1[k]) % 2 == 0) ans++; else if ((arr0[k] + arr1[k]) % 2 == 0) { if (od > 0) { ans++; } } } cout << ans + r << "\n"; } } }
2
#include <bits/stdc++.h> using namespace std; int n, T; int s1[10101]; int dp[1010101]; int dp2[1101014]; int dp3[1000110]; int main() { scanf("%d%d", &n, &T); for (register int i = 1; i <= n; i++) scanf("%d", &s1[i]), dp[s1[i]]++; for (register int i = 1; i <= n * n; i++) s1[i] = s1[(i - 1) % n + 1]; int l = n * min(n, T); for (register int i = 1; i <= l; i++) { int flag = 0; for (register int j = 0; j < i; j++) if (s1[j] <= s1[i]) flag = max(flag, dp3[j]); dp3[i] = flag + 1; } for (register int i = l; i >= 1; i--) { int flag = 0; for (register int j = l + 1; j > i; j--) if (s1[j] >= s1[i]) flag = max(flag, dp2[j]); dp2[i] = flag + 1; } int ans = 0; if (T <= n) for (register int i = 1; i <= l; i++) ans = max(ans, dp3[i] + dp2[i] - 1); else for (register int i = 1; i <= l; i++) ans = max(ans, dp3[i] + dp2[i] - 1 + dp[s1[i]] * (T - n)); printf("%d", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; template <class X, class Y> void minimize(X &x, const Y &y) { if (x > y) x = y; } template <class X, class Y> void maximize(X &x, const Y &y) { if (x < y) x = y; } template <class T> T Abs(const T &x) { return (x < 0 ? -x : x); } const string yes = "YES"; const string no = "NO"; const string sizeName[] = {"S", "M", "L", "XL", "XXL", "XXXL"}; const int numSize = 6; vector<int> singleReq[10], doubleReq[10]; string answer[100100]; int have[10], n; void noAnswer(void) { cout << "NO" << endl; exit(0); } void process(void) { for (int i = 0, _n = (numSize); i < _n; i = i + 1) cin >> have[i]; cin >> n; for (int i = (1), _b = (n); i <= _b; i = i + 1) { string req; cin >> req; for (int j = 0, _n = (numSize); j < _n; j = j + 1) if (req == sizeName[j]) singleReq[j].push_back(i); for (int j = 0, _n = (numSize - 1); j < _n; j = j + 1) if (req == sizeName[j] + "," + sizeName[j + 1]) doubleReq[j].push_back(i); } for (int i = 0, _n = (numSize); i < _n; i = i + 1) { if (have[i] < singleReq[i].size()) noAnswer(); have[i] -= singleReq[i].size(); for (__typeof((singleReq[i]).begin()) it = (singleReq[i]).begin(); it != (singleReq[i]).end(); it++) answer[*it] = sizeName[i]; } for (int i = 0, _n = (numSize - 1); i < _n; i = i + 1) { if (have[i] + have[i + 1] < doubleReq[i].size()) noAnswer(); for (int j = (i), _b = (i + 1); j <= _b; j = j + 1) while (!doubleReq[i].empty() && have[j] > 0) { have[j]--; answer[doubleReq[i].back()] = sizeName[j]; doubleReq[i].pop_back(); } } cout << yes << "\n"; for (int i = (1), _b = (n); i <= _b; i = i + 1) cout << answer[i] << "\n"; } int main(void) { ios::sync_with_stdio(false); cin.tie(NULL); process(); return 0; }
4
#include <bits/stdc++.h> const int MAGIC = 100, MN = 1e5 + 10, MM = 2e5 + 10; std::mt19937 rng(time(NULL)); int N, M, T, c[MN], t[MN], b[MN], d[MN], cnt[MN], dif[MN], at[MN], l[MN], jmp[MN], s[MN]; bool u[MN], v[MN], ok[MN]; std::vector<int> a[MN], ans; bool dfst(int n) { v[n] = u[n] = 1; s[n] = 1; for (int x : a[n]) if (u[x]) continue; else if (v[x]) return 0; else { if (!dfst(x)) return 0; s[n] += s[x]; } u[n] = 0; return 1; } bool test(int n) { memset(u + 1, 0, N * sizeof *u); memset(v + 1, 0, N * sizeof *v); return dfst(n) && s[n] == N; } void dfs(int n) { v[n] = 1; at[d[n]] = n, dif[d[n]] = 0; cnt[n] = 0, l[n] = d[n]; for (int x : a[n]) if (v[x]) { ++cnt[n]; ++dif[d[x]]; l[n] = std::min(l[n], d[x]); } else { d[x] = d[n] + 1; dfs(x); l[n] = std::min(l[n], l[x]); cnt[n] += cnt[x]; } cnt[n] -= dif[d[n]]; if (cnt[n] == 1) jmp[n] = at[l[n]]; else jmp[n] = -1; } void dfs2(int n) { v[n] = 1; if (!ok[n] && ~jmp[n]) ok[n] = ok[jmp[n]]; for (int x : a[n]) if (!v[x]) dfs2(x); } void solve(int n) { memset(v + 1, 0, N * sizeof *v); d[n] = 0; dfs(n); memset(ok + 1, 0, N * sizeof *ok); memset(v + 1, 0, N * sizeof *v); ok[n] = 1; dfs2(n); } int main(void) { scanf("%d", &T); while (T--) { scanf("%d%d", &N, &M); for (int i = 0, x, y; i < M; ++i) scanf("%d%d", &x, &y), a[x].push_back(y); std::iota(b, b + N, 1); std::shuffle(b, b + N, rng); for (int i = std::min(MAGIC, N) - 1; i >= 0; --i) if (test(b[i])) { solve(b[i]); for (int i = 1; i <= N; ++i) if (ok[i]) ans.push_back(i); break; } if (ans.size() * 5 < N) printf("-1\n"); else for (int i = 0; i < ans.size(); ++i) printf("%d%c", ans[i], " \n"[i + 1 == ans.size()]); ans.clear(); for (int i = 1; i <= N; ++i) a[i].clear(); } }
5
#include<bits/stdc++.h> using namespace std; int mx[]={-1,0,1,0}; int my[]={0,-1,0,1}; int main(){ int N; while(cin>>N,N){ vector<pair<int,int>>v(N); v[0]=make_pair(0,0); for(int i=1;i<N;++i){ int n,d; cin>>n>>d; v[i]=make_pair(v[n].first+mx[d],v[n].second+my[d]); } int min_x=1e9,max_x=-1e9; int min_y=1e9,max_y=-1e9; for(int i=0;i<N;++i){ max_x=max(v[i].first,max_x); min_x=min(v[i].first,min_x); max_y=max(v[i].second,max_y); min_y=min(v[i].second,min_y); } cout<<max_x-min_x+1<<" "<<max_y-min_y+1<<endl; } }
0
#include<cstdio> using namespace std; const int maxn=1e5+2; int n,l,pos; int a[maxn]; int main(){ scanf("%d%d",&n,&l); for (int i=1;i<=n;i++){ scanf("%d",&a[i]); if (a[i-1]+a[i]>=l) pos=i-1; } if (!pos) printf("Impossible\n"); else{ printf("Possible\n"); for (int i=1;i<pos;i++) printf("%d\n",i); for (int i=n-1;i>=pos;i--) printf("%d\n",i); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 200000; int n; double tmp; int main() { scanf("%d", &n); double now = 0, ans = 0; for (int i = 1; i <= n; i++) { scanf("%lf", &tmp); now = now * tmp; ans = ans + now + now + tmp; now = now + tmp; } printf("%.6lf\n", ans); }
4
#include <bits/stdc++.h> using namespace std; const int MaxN = 10004, NA = -1, MaxC = 0x3F3F3F3F; int m, n, x; int f(int n, int m, int x) { n -= x + x; m -= x + x; if (n <= 0 || m <= 0) return 0; return (n * m + 1) >> 1; } int main(void) { while (scanf(" %d %d %d", &n, &m, &x) != EOF) { printf("%d\n", f(n, m, x - 1) - f(n, m, x)); } return 0; }
2
#include <bits/stdc++.h> template <typename T> class IntegerIterator : public std::iterator<std::input_iterator_tag, T, std::ptrdiff_t, T*, T> { public: explicit IntegerIterator(T value) : value(value) {} IntegerIterator& operator++() { ++value; return *this; } IntegerIterator operator++(int) { IntegerIterator copy = *this; ++value; return copy; } IntegerIterator& operator--() { --value; return *this; } IntegerIterator operator--(int) { IntegerIterator copy = *this; --value; return copy; } T operator*() const { return value; } bool operator==(IntegerIterator rhs) const { return value == rhs.value; } bool operator!=(IntegerIterator rhs) const { return !(*this == rhs); } private: T value; }; template <typename T> class IntegerRange { public: IntegerRange(T begin, T end) : begin_(begin), end_(end) { ; } IntegerIterator<T> begin() const { return IntegerIterator<T>(begin_); } IntegerIterator<T> end() const { return IntegerIterator<T>(end_); } private: T begin_; T end_; }; template <typename T> class ReversedIntegerRange { public: ReversedIntegerRange(T begin, T end) : begin_(begin), end_(end) { ; } std::reverse_iterator<IntegerIterator<T>> begin() const { return std::reverse_iterator<IntegerIterator<T>>( IntegerIterator<T>(begin_)); } std::reverse_iterator<IntegerIterator<T>> end() const { return std::reverse_iterator<IntegerIterator<T>>(IntegerIterator<T>(end_)); } private: T begin_; T end_; }; template <typename T> IntegerRange<T> range(T to) { return IntegerRange<T>(0, to); } template <typename T> IntegerRange<T> range(T from, T to) { return IntegerRange<T>(from, to); } template <typename T> IntegerRange<T> inclusiveRange(T to) { return IntegerRange<T>(0, to + 1); } template <typename T> IntegerRange<T> inclusiveRange(T from, T to) { return IntegerRange<T>(from, to + 1); } template <typename T> ReversedIntegerRange<T> downrange(T from) { return ReversedIntegerRange<T>(from, 0); } template <typename T> ReversedIntegerRange<T> downrange(T from, T to) { return ReversedIntegerRange<T>(from, to); } template <typename T> ReversedIntegerRange<T> inclusiveDownrange(T from) { return ReversedIntegerRange<T>(from + 1, 0); } template <typename T> ReversedIntegerRange<T> inclusiveDownrange(T from, T to) { return ReversedIntegerRange<T>(from + 1, to); } template <typename T> void updateMin(T& oldValue, const T& newValue) { if (newValue < oldValue) { oldValue = newValue; } } template <typename T> void updateMax(T& oldValue, const T& newValue) { if (oldValue < newValue) { oldValue = newValue; } } using namespace std; class TaskC { public: void solve(std::istream& in, std::ostream& out) { int n, l; in >> n >> l; vector<int64_t> c(31, 1000000000000000000); for (int i : range(n)) { in >> c[i]; } for (int i : range(1, 31)) { updateMin(c[i], 2 * c[i - 1]); } int64_t best = 1000000000000000000; { int64_t ans = 0; for (int i : downrange(31)) { if (l & (1LL << i)) { ans += c[i]; } else { updateMin(best, ans + c[i]); } } updateMin(best, ans); } out << best; } }; int main() { std::ios_base::sync_with_stdio(false); TaskC solver; std::istream& in(std::cin); std::ostream& out(std::cout); in.tie(0); out << std::fixed; out.precision(20); solver.solve(in, out); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << ((n * n) + 1) / 2 << endl; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i % 2 == 0) { if (j % 2 == 0) { cout << 'C'; } else { cout << '.'; } } else { if (j % 2 == 1) { cout << 'C'; } else { cout << '.'; } } } cout << endl; } }
1
#include<iostream> #include<cstring> using namespace std; int main(){ int n; double hig; int data[7]; for(int i=0;i<7;i++){ data[i]=0; } cin>>n; for(int i=0;i<n;i++){ cin>>hig; if(hig<165.0){ data[1]++; }if(hig>=165.0&&170.0>hig){ data[2]++; }if(hig>=170.0&&175.0>hig){ data[3]++; }if(hig>=175.0&&180.0>hig){ data[4]++; }if(hig>=180.0&&185.0>hig){ data[5]++; }if(hig>=185.0){ data[6]++; } } for(int i=1;i<7;i++){ cout<<i<<":"; for(int j=0;j<data[i];j++){ cout<<"*"; } cout<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; #define M 600001 int main(){ int N,Q,q,in,c[M]={0}; cin >> N >> Q; for(int i = 0 ; i < N ; i++){ scanf("%d",&in); c[in] = in; } for(int i = 1 ; i < M ; i++){ c[i] = max(c[i],c[i-1]); } while(Q--){ cin >> q; int r = 0; for(int i = q-1 ; i < M ; i += q){ r = max(r,c[i]%q); } printf("%d\n",r); } return 0; }
0
#include <bits/stdc++.h> const int oo = 0x3f3f3f3f; template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, true : false; } template <typename T> inline bool chkmin(T &a, T b) { return a > b ? a = b, true : false; } template <typename T> T read(T &first) { int f = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (first = 0; isdigit(ch); ch = getchar()) first = 10 * first + ch - '0'; return first *= f; } template <typename T> void write(T first) { if (first < 0) { putchar('-'); first = -first; } static char s[20]; int top = 0; for (; first; first /= 10) s[++top] = first % 10 + '0'; if (top == 0) putchar('0'); else while (top) putchar(s[top--]); } const int MAXN = 1e5 + 5, MAXK = 6; struct Line { int a, b, c; int id; bool on(const std::pair<double, double> &p) { static double EPS = 1e-8; return std::fabs(a * p.first + b * p.second + c) < EPS; } }; int N, K; Line A[MAXN]; std::pair<int, int> ans[MAXN]; int anssize; int st[MAXN]; inline bool intersection(const Line &a, const Line &b, std::pair<double, double> &p) { long long d = (long long)a.b * b.a - (long long)b.b * a.a; if (d == 0) return false; p.first = (double)((long long)b.b * a.c - (long long)a.b * b.c) / d; p.second = (double)((long long)b.c * a.a - (long long)a.c * b.a) / d; return true; } bool dfs(int n, int k) { if (n <= k) { for (int i = 0; i < n; ++i) { ans[anssize++] = std::make_pair(A[i].id + 1, -1); } return true; } if (k == 0) return false; for (int i = n - k - 1; i < n; ++i) { for (int j = i + 1; j < n; ++j) { std::pair<double, double> p; if (intersection(A[i], A[j], p) == false) continue; ans[anssize++] = std::make_pair(A[i].id + 1, A[j].id + 1); int m = n, top = 0; for (int k = n - 1; k >= 0; --k) { if (A[k].on(p)) { st[++top] = k; std::swap(A[k], A[--m]); } } if (dfs(m, k - 1) == true) return true; --anssize; while (top > 0) { std::swap(A[st[top--]], A[m++]); } } } return false; } void input() { read(N); read(K); for (int i = 0; i < N; ++i) { read(A[i].a); read(A[i].b); read(A[i].c); A[i].id = i; } } void solve() { srand(time(NULL)); std::random_shuffle(A, A + N); if (N >= 5 * K * K) { int times = 100; while (times-- && N > 0 && K > 0) { int i = rand() % N; int j = rand() % N; std::pair<double, double> p; if (i == j || intersection(A[i], A[j], p) == false) continue; int top = 0; for (int k = N - 1; k >= 0; --k) { if (A[k].on(p)) st[++top] = k; } if (top > K) { ans[anssize++] = std::make_pair(A[i].id + 1, A[j].id + 1); for (int k = 1; k <= top; ++k) { A[st[k]] = A[--N]; } --K; break; } } } bool haveAns = dfs(N, K); if (haveAns) { puts("YES"); write(anssize); putchar('\n'); for (int i = 0; i < anssize; ++i) { write(ans[i].first); putchar(' '); write(ans[i].second); putchar('\n'); } } else puts("NO"); } int main() { input(); solve(); return 0; }
4
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse2") using namespace std; bool isPowerOfTwo(long long int x) { return x && (!(x & (x - 1))); } void solve() { long long int n; cin >> n; string arr[n]; for (long long int i = 0; i < n; i++) cin >> arr[i]; vector<pair<long long int, long long int>> p, q; for (long long int i = 0; i < n; i++) { for (long long int j = 0; j < n; j++) { if (arr[i][j] == '.') { p.push_back({i, j}); break; } } } for (long long int i = 0; i < n; i++) { for (long long int j = 0; j < n; j++) { if (arr[j][i] == '.') { q.push_back({j, i}); break; } } } if (p.size() == n) { for (auto x : p) cout << x.first + 1 << " " << x.second + 1 << "\n"; } else if (q.size() == n) { for (auto x : q) cout << x.first + 1 << " " << x.second + 1 << "\n"; } else cout << -1; } int32_t main() { std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int T = 1; while (T--) solve(); return 0; }
1
#include<bits/stdc++.h> using namespace std; int main() { int n,q; cin >> n >> q; vector<stack<int>> S(n); while (q--) { int code; cin >> code; switch (code) { case 0: { int t,x; cin >> t >> x; S[t].push(x); } break; case 1: { int t; cin >> t; if (S[t].size()) cout << S[t].top() << endl; } break; case 2: { int t; cin >> t; if (S[t].size()) S[t].pop(); } break; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 3001; int a[maxn], b[maxn], c[maxn]; int f[maxn][2][2]; int n, m; void init() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) scanf("%d", &b[i]); for (int i = 1; i <= n; i++) scanf("%d", &c[i]); } void solve() { f[1][0][0] = a[1]; f[1][0][1] = b[1]; for (int i = 2; i <= n; i++) { f[i][0][0] = max(f[i - 1][0][1], f[i - 1][1][1]) + a[i]; f[i][0][1] = max(f[i - 1][0][1], f[i - 1][1][1]) + b[i]; f[i][1][0] = max(f[i - 1][0][0], f[i - 1][1][0]) + b[i]; f[i][1][1] = max(f[i - 1][0][0], f[i - 1][1][0]) + c[i]; } } int main() { init(); solve(); printf("%d\n", max(f[n][1][0], f[n][0][0])); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MAX = 510; long long x[MAX], d[MAX][MAX], ans[MAX]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> d[i][j]; for (int i = 1; i <= n; i++) cin >> x[i]; for (int k = n; k >= 1; k--) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { d[i][j] = min(d[i][j], d[i][x[k]] + d[x[k]][j]); } } for (int i = k; i <= n; i++) { for (int j = k; j <= n; j++) { ans[k] += d[x[i]][x[j]]; } } } for (int i = 1; i <= n; i++) cout << ans[i] << " "; }
4
#include <bits/stdc++.h> using namespace std; const int N = 100001; int a[N], b[N]; int main() { int n; cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; b[i] = a[i]; } sort(b + 1, b + n + 1); for (int i = 1; i <= n; ++i) { int ans = 1; int j = 1; while (b[j] <= a[i] && j <= n) ++j; --j; ans += n - j; cout << ans << " "; } }
1
#include <bits/stdc++.h> using namespace std; int f() { string s; cin >> s; s.erase(0, 4); if (s.size() == 1) { if (s < "9") cout << "199" + s << endl; else cout << "198" + s << endl; } if (s.size() == 2) { if (s < "99") cout << "20" + s << endl; else cout << "1999" << endl; } if (s.size() == 3) { if (s < "099") cout << "3" + s << endl; else cout << "2" + s << endl; } if (s.size() == 4) { if (s < "3099") cout << "1" + s << endl; else cout << s << endl; } if (s.size() == 5) { if (s < "13099") cout << "1" + s << endl; else cout << s << endl; } if (s.size() == 6) { if (s < "113099") cout << "1" + s << endl; else cout << s << endl; } if (s.size() == 7) { if (s < "1113099") cout << "1" + s << endl; else cout << s << endl; } if (s.size() == 8) { if (s < "11113099") cout << "1" + s << endl; else cout << s << endl; } if (s.size() == 9) { if (s < "111113099") cout << "1" + s << endl; else cout << s << endl; } return 0; } int main() { cin.sync_with_stdio(false); int i, n; cin >> n; for (i = 0; i < n; i++) f(); return 0; }
4
#include <bits/stdc++.h> using namespace std; int f[30][30], ans = 0; bool b[30][30]; int main() { int n; scanf("%d", &n); memset(f, 0, sizeof(f)); memset(b, 0, sizeof(f)); while (n--) { char s[40]; scanf("%s", s + 1); int l = strlen(s + 1); for (int i = 0; i < 26; i++) { if (!b[i][s[1] - 'a'] && s[1] - 'a' != i) continue; b[i][s[1] - 'a'] = b[i][s[l] - 'a'] = true; f[i][s[l] - 'a'] = max(f[i][s[l] - 'a'], l + f[i][s[1] - 'a']); } } for (int i = 0; i < 26; i++) ans = max(ans, f[i][i]); printf("%d\n", ans); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int a, b, i = 0; cin >> a >> b; if (a == b) { cout << "1"; } else { while (a <= b) { a = a * 3; b = b * 2; i++; } cout << i; } }
1
#include <bits/stdc++.h> using namespace std; template <class T> void splitstr(const string &s, vector<T> &out) { istringstream in(s); out.clear(); copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out)); } static void redirect(int argc, const char **argv) { ios::sync_with_stdio(false); cin.tie(NULL); if (argc > 1) { static filebuf f; f.open(argv[1], ios::in); cin.rdbuf(&f); if (!cin) { cerr << "Failed to open '" << argv[1] << "'" << endl; exit(1); } } if (argc > 2) { static filebuf f; f.open(argv[2], ios::out | ios::trunc); cout.rdbuf(&f); if (!cout) { cerr << "Failed to open '" << argv[2] << "'" << endl; } } cin.exceptions(ios::failbit); } static void solve() { int N; cin >> N; vector<int> p(N); for (int i = 0; i < N; i++) { int f; cin >> f; f--; p[f] = i; } long long ans = 0; for (int i = 1; i < N; i++) ans += abs(p[i] - p[i - 1]); cout << ans << '\n'; } int main(int argc, const char **argv) { redirect(argc, argv); solve(); return 0; }
2
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main(){ int n; cin>>n; double ans=0; while(n--){ int x; cin>>x; ans+=1.0/x; } cout<<pow(ans, -1)<<endl; }
0
#include <bits/stdc++.h> using namespace std; long long n, k; string s; void solve(long long n, long long k, string s) { vector<long long> v; long long win = 0; for (long long i = 0; i < n; i++) { if (s[i] == 'W') { win++; } } if (win == 0 and k != 0) { cout << (2 * k) - 1 << endl; return; } else if (win == 0 and k == 0) { cout << 0 << endl; return; } win += k; if (win >= n) { cout << (2 * n) - 1 << endl; return; } long long l = 0; long long j = 0; while (j < n) { if (s[j] == 'L') { l++; } else { v.push_back(l); l = 0; } j++; } if (l != 0) { v.push_back(l); } if (s[0] == 'L') { v.erase(v.begin()); } if (s[n - 1] == 'L') { v.erase(v.begin() + v.size() - 1); } sort(v.begin(), v.end()); long long kk = 0; long long i; for (i = 0; i < v.size(); i++) { if (kk + v[i] > k) { break; } kk += v[i]; } long long notchangedstreaks = v.size() - i; cout << 2 * win - notchangedstreaks - 1 << endl; } int main() { long long t; cin >> t; while (t--) { cin >> n >> k; cin >> s; solve(n, k, s); } return 0; }
2
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<ll, ll>; using pci = pair<char, ll>; mt19937 rnd(5512365); const ll inf = 1e9 + 7; template <class T> using vec = vector<T>; const ld pi = atan2(0, -1); struct point { ll x, y; point() { x = 0, y = 0; } point(ll a, ll b) { x = a, y = b; } }; struct Vector { ll x, y; Vector() { x = 0, y = 0; } Vector(point a, point b) { x = b.x - a.x; y = b.y - a.y; } }; ll skal(Vector a, Vector b) { return (a.x * b.x + a.y * b.y); } ll vect(Vector a, Vector b) { return (a.x * b.y - a.y * b.x); } point minp; bool cmp(point& a, point& b) { Vector a1 = Vector(minp, a); Vector b1 = Vector(minp, b); if (vect(a1, b1) == 0) { return skal(a1, a1) < skal(b1, b1); } return vect(a1, b1) > 0; } vector<point> ans; vector<point> a; bool ok(ll i) { point C = a[i]; point B = ans.back(); point A = ans[ans.size() - 2]; Vector AB(A, B), BC(B, C); return vect(AB, BC) <= 0; } void solv() { ll cur = 0; for (ll i = 1; i < a.size(); ++i) { if (a[i].x < a[cur].x || (a[i].x == a[cur].x && a[i].y < a[cur].y)) { cur = i; } } minp = a[cur]; sort(a.begin(), a.end(), cmp); ans.push_back(a[0]); ans.push_back(a[1]); for (ll i = 2; i < a.size(); ++i) { while (ans.size() > 1 && ok(i)) { ans.pop_back(); } ans.push_back(a[i]); } } ll area(point _a, point b, point c) { return abs((b.x - _a.x) * (c.y - _a.y) - (c.x - _a.x) * (b.y - _a.y)); } void solve(ll numtest) { ll n, S; cin >> n >> S; a.resize(n); for (ll i = 0; i < n; ++i) { cin >> a[i].x >> a[i].y; } solv(); n = ans.size(); a = ans; deque<point> cur; for (auto elem : a) { cur.push_back(elem); } ll a = 0, b = 1, c = 2; while (1) { bool g = 1; for (ll i = 0; i < n; ++i) { if (area(cur[i], cur[b], cur[c]) > area(cur[a], cur[b], cur[c])) a = i, g = 0; if (area(cur[a], cur[i], cur[c]) > area(cur[a], cur[b], cur[c])) b = i, g = 0; if (area(cur[a], cur[b], cur[i]) > area(cur[a], cur[b], cur[c])) c = i, g = 0; } if (g) break; } point A0 = cur[a], B0 = cur[b], C0 = cur[c]; point A1, B1, C1; { Vector r1(A0, B0); Vector r2(A0, C0); r1.x += r2.x; r1.y += r2.y; A1.x = A0.x + r1.x; A1.y = A0.y + r1.y; } { Vector r1(B0, C0); Vector r2(B0, A0); r1.x += r2.x; r1.y += r2.y; B1.x = B0.x + r1.x; B1.y = B0.y + r1.y; } { Vector r1(C0, B0); Vector r2(C0, A0); r1.x += r2.x; r1.y += r2.y; C1.x = C0.x + r1.x; C1.y = C0.y + r1.y; } cout << A1.x << ' ' << A1.y << '\n'; cout << B1.x << ' ' << B1.y << '\n'; cout << C1.x << ' ' << C1.y << '\n'; if (S == 15880) { } } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll tt = 1; cout << fixed << setprecision(10); for (ll test = 0; test < tt; ++test) { solve(test + 1); } return 0; }
5
#include <bits/stdc++.h> int main(int argc, const char* argv[]) { int num; int array[100]; int jugde[100] = {0}; int is_zero = 0; scanf("%d", &num); int sum = 0; for (int i = 0; i < num; ++i) { scanf("%d", &array[i]); sum += array[i]; if (array[i] == 0) { jugde[i] = 1; } if (array[i] != 0) { is_zero = 1; } } if (is_zero == 0) { printf("NO\n"); return 0; } if (sum != 0) { printf("YES\n%d\n%d %d\n", 1, 1, num); return 0; } for (int i = 0; i < num; ++i) { if (array[i] == 0) continue; printf("YES\n%d\n%d %d\n%d %d\n", 2, 1, i + 1, i + 2, num); break; } return 0; }
1
#include <bits/stdc++.h> using namespace std; char str[100010]; int vis[100010]; int fi; int b; void match() { int j = 0; int num = 0; for (int i = 0; i < b; ++i) { if (str[i] == '(') num++; else if (str[i] == ')') num--; else num -= vis[++j]; if (num < 0) { cout << "-1" << endl; return; } } for (int i = 1; i < j + 1; i++) { cout << vis[i] << endl; } } int main() { while (cin >> str) { b = (int)strlen(str); memset(vis, -1, sizeof(vis)); int num = 0, j = 0; fi = 0; for (int i = 0; i < b; ++i) { if (str[i] == '(') num++; else if (str[i] == ')') num--; else { vis[++fi] = 1; num--; } } if (num > 0) { vis[fi] += num; } match(); } return 0; }
1
#include <bits/stdc++.h> using namespace std; long long vis[100010], water[1001][1001], mnwt[100010], in[1005], out[1005]; vector<long long> adj[100010]; set<pair<pair<long long, long long>, long long> > ans; void dfs(long long u, long long v, long long st) { vis[u] = 1; if (v != -1) mnwt[u] = min(min(mnwt[u], mnwt[v]), water[v][u]); long long i = 0; for (i = 0; i < adj[u].size(); i++) { long long v = adj[u][i]; if (!vis[v]) dfs(v, u, st); } if (out[u] == 0 && in[u] && adj[u].size() == 0) { ans.insert(make_pair(make_pair(st, u), mnwt[u])); } } int main() { long long i, n, m, a, b, j, flag = 0; scanf("%lld%lld", &n, &m); for (i = 1; i <= n; i++) { mnwt[i] = 100000007; out[i] = 0; in[i] = 0; vis[i] = 0; } for (i = 1; i <= m; i++) { scanf("%lld%lld", &a, &b); scanf("%lld", &water[a][b]); adj[a].push_back(b); out[a]++; in[b]++; } flag = 1; for (i = 1; i <= n; i++) { if (in[i] == 0 && out[i]) { dfs(i, -1, i); flag = 0; } } if (flag) printf("0\n"); else { cout << ans.size() << "\n"; set<pair<pair<long long, long long>, long long> >::iterator it; for (it = ans.begin(); it != ans.end(); it++) { printf("%lld %lld %lld\n", ((it->first).first), (it->first).second, (it->second)); } } return 0; }
1
#include <bits/stdc++.h> int main() { long long int t, a, b, c, d, sum, p, n; scanf("%lld", &t); while (t--) { sum = 0; scanf("%lld", &a); scanf("%lld", &b); scanf("%lld", &c); scanf("%lld", &d); if (a <= b) { printf("%lld\n", b); } else { if (c <= d) { printf("-1\n"); } else if (c > d) { a = a - b; sum = sum + b; p = c - d; if (a % p == 0) { n = a / p; sum = sum + (n * c); } else { n = a / p + 1; sum = sum + (n * c); } printf("%lld\n", sum); } } } return 0; }
1
#include <iostream> #include <cstdio> #include <cmath> #include <cstdio> #include <cstring> #include <string> #include <vector> #include <set> #include <map> #include <algorithm> #define CH(N,A,B) (A<=N&&N<B) #define REP(i,a,b) for(int i=a;i<b;i++) #define RREP(i,a,b) for(int i=(b-1);a<=i;i--) using namespace std; long long dp[2005][2005]; int n, a[2005]; long long dfs(int sta, int end, int left, bool joi){ if(dp[sta][end] != -1) return dp[sta][end]; if(left == 1){ if(!joi) { return dp[sta][end] = 0; }else{ return dp[sta][end] = a[sta]; } } if(!joi){ if(a[sta] < a[end]){ return dp[sta][end] = dfs(sta, (end+1)%n, left-1, true); }else{ return dp[sta][end] = dfs((sta-1+n)%n, end, left-1, true); } }else{ return dp[sta][end] = max(dfs((sta-1+n)%n, end, left-1, false) + a[sta], dfs(sta, (end+1)%n, left-1, false) + a[end]); } } int main() { cin>>n; REP(i,0,n) cin>>a[i]; REP(i,0,n)REP(j,0,n) dp[i][j] = -1; vector<long long> v; REP(i,0,n) v.push_back( dfs(i,(i+1)%n,n,true) ); cout<<*max_element(v.begin(), v.end())<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, k, t = 0, c = 0; cin >> n >> k; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); for (int i = 0; i < k; i++) { while (a[c] - t <= 0 && c < n) c++; if (a[c] - t > 0 && c < n) { cout << a[c] - t << endl; t = a[c]; c++; } else cout << 0 << endl; } return 0; }
2
#include <bits/stdc++.h> void swap(int *a, int *b); int main(void) { int numCount; scanf("%d", &numCount); int num[numCount]; for (int i = 0; i < numCount; i++) { scanf("%d", &num[i]); } for (int i = 0; i < numCount; i++) { for (int j = 0; j < numCount - 1; j++) { if (num[j] > num[j + 1]) { swap(&num[j], &num[j + 1]); } } } int flag = 1; for (int i = 0; i < numCount; i++) { int counter = 0; if (i % 2 == 0 && i > 0) { if (num[numCount - counter - 1] < num[counter]) { flag = 0; } counter++; } else { if (num[counter] > num[numCount - counter - 1]) { flag = 0; } } } if (flag == 1) { int ctr = 0; for (int i = 0; i < numCount; i++) { if (i % 2 == 0) { printf("%d ", num[ctr]); ctr++; } else if (i % 2 == 1) { printf("%d ", num[numCount - ctr]); } } puts(""); } else { printf("Impossible\n"); } return 0; } void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; return; }
2
#include <bits/stdc++.h> using namespace std; const int mn = 2505; int a[mn][mn], n, m, K; char s[mn]; long long f[2][10], ans; inline int num(int x1, int y1, int x2, int y2) { return a[x2][y2] - a[x1][y2] - a[x2][y1] + a[x1][y1]; } void solve(int x1, int y1, int x2, int y2, bool flg) { if (x1 == x2 || y1 == y2) return; if (x1 + 1 == x2 && y1 + 1 == y2) { ans += (num(x1, y1, x2, y2) == K); return; } if (flg) { int mid = (x1 + x2) >> 1; solve(x1, y1, mid, y2, 0), solve(mid, y1, x2, y2, 0); for (int i = y1; i < y2; i++) { f[0][0] = f[1][0] = mid; for (int j = 1; j <= K + 1; j++) f[0][j] = x1, f[1][j] = x2; for (int j = i + 1; j <= y2; j++) { for (int k = 1; k <= K + 1; k++) { while (num(f[0][k], i, mid, j) >= k) ++f[0][k]; while (num(mid, i, f[1][k], j) >= k) --f[1][k]; } for (int k = 0; k <= K; k++) ans += (f[0][k] - f[0][k + 1]) * (f[1][K - k + 1] - f[1][K - k]); } } } else { int mid = (y1 + y2) >> 1; solve(x1, y1, x2, mid, 1), solve(x1, mid, x2, y2, 1); for (int i = x1; i < x2; i++) { f[0][0] = f[1][0] = mid; for (int j = 1; j <= K + 1; j++) f[0][j] = y1, f[1][j] = y2; for (int j = i + 1; j <= x2; j++) { for (int k = 1; k <= K + 1; k++) { while (num(i, f[0][k], j, mid) >= k) ++f[0][k]; while (num(i, mid, j, f[1][k]) >= k) --f[1][k]; } for (int k = 0; k <= K; k++) ans += (f[0][k] - f[0][k + 1]) * (f[1][K - k + 1] - f[1][K - k]); } } } } int main() { scanf("%d%d%d", &n, &m, &K); for (int i = 1; i <= n; i++) { scanf("%s", s + 1); for (int j = 1; j <= m; j++) a[i][j] = s[j] - '0', a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1]; } solve(0, 0, n, m, 0); printf("%I64d\n", ans); }
5
#include <bits/stdc++.h> using namespace std; int main() { long long d, m, mod = 1e9 + 7; cin >> d; while (d--) { cin >> m; if (m == 1) { cout << 1 << endl; continue; } long long res = 1; m = m * 2; for (int i = m; i >= 1; i--) if (i != 2) res = (res * i) % mod; cout << res << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; long long p; long long n, m, k; long long tag = 0; long long cnt[100000]; long long sum[100000]; inline long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } return x * f; } long long ksm(long long x, long long y) { long long ans = 1; while (y) { if (y & 1) ans = (ans * x) % p; x = (x * x) % p; y >>= 1; } return ans; } signed main() { n = read(), m = read(), k = read(); if (n < m) swap(n, m), tag = 1; for (long long i = 1; i <= n; i++) sum[i] = 1; for (long long i = 1; i <= k; i++) { long long x = read(), y = read(), z = read(); if (tag) swap(x, y); cnt[x]++; sum[x] *= z; } p = read(); if ((n + m) & 1) { cout << 0 << endl; return 0; } long long ans = 0; for (long long i = 1; i <= n; i++) { if (cnt[i] == m) { if (sum[i] == 1) { cout << 0 << endl; return 0; } continue; } ans += m - cnt[i] - 1; } ans -= m - 1; cout << ksm(2, ans); return 0; }
5
#include <bits/stdc++.h> const int MAX = 11; const double PI = acos(-1); const double eps = 1e-5; void FFT(std::complex<double> *a, int length, int type) { for (int i = 1, j = 0; i < length - 1; ++i) { for (int s = length; j ^= s >>= 1, ~j & s;) ; if (i < j) { std::swap(a[i], a[j]); } } int len = -1; for (int x = length; x; ++len, x >>= 1) ; for (int i = 1; i <= len; ++i) { std::complex<double> unit(cos(PI / (1 << i - 1)), type * sin(PI / (1 << i - 1))); for (int j = 0; j < length; j += 1 << i) { std::complex<double> w(1, 0); for (int k = j, szk = 1 << i - 1; k < j + szk; ++k) { std::complex<double> s = a[k], t = w * a[k + szk]; a[k] = s + t, a[k + szk] = s - t; w *= unit; } } } if (type == 1) { return; } for (int i = 0; i < length; ++i) { a[i] /= (std::complex<double>)length; } } void mult(std::complex<double> (*compa)[1 << MAX], std::vector<int> lengtha, std::complex<double> (*compb)[1 << MAX], std::vector<int> lengthb) { std::vector<int> length = {1, 1}; std::vector<int> n = {lengtha[1] + lengthb[1] - 1, lengtha[0] + lengthb[0] - 1}; for (int i = 0; i < 2; ++i) { for (; length[i] < n[i]; length[i] <<= 1) ; } for (int i = 0; i < lengtha[0]; ++i) { FFT(compa[i], length[0], 1); } for (int i = 0; i < lengthb[0]; ++i) { FFT(compb[i], length[0], 1); } for (int i = 0, sz = std::max(length[0], lengtha[0]); i < sz; ++i) { for (int j = 0; j < i; ++j) { std::swap(compa[i][j], compa[j][i]); } } for (int i = 0, sz = std::max(length[0], lengthb[0]); i < sz; ++i) { for (int j = 0; j < i; ++j) { std::swap(compb[i][j], compb[j][i]); } } for (int i = 0; i < length[0]; ++i) { FFT(compa[i], length[1], 1); FFT(compb[i], length[1], 1); } for (int i = 0; i < length[0]; ++i) { for (int j = 0; j < length[1]; ++j) { compa[i][j] *= compb[i][j]; } } for (int i = 0; i < length[0]; ++i) { FFT(compa[i], length[1], -1); } for (int i = 0, sz = std::max(length[0], length[1]); i < sz; ++i) { for (int j = 0; j < i; ++j) { std::swap(compa[i][j], compa[j][i]); } } for (int i = 0; i < n[0]; ++i) { FFT(compa[i], length[0], -1); } } std::complex<double> a[1 << MAX][1 << MAX], b[1 << MAX][1 << MAX]; int n, m, r, c; char s[1 << MAX][1 << MAX]; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) { scanf("%s", s[i]); } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { a[i][j] = std::complex<double>{sin(2 * PI * s[i][j] / 26), cos(2 * PI * s[i][j] / 26)}; } } scanf("%d%d", &r, &c); for (int i = 0; i < r; ++i) { scanf("%s", s[i]); } int cnt = 0; for (int i = 0; i < r; ++i) { for (int j = 0; j < c; ++j) { b[r - i - 1][c - j - 1] = s[i][j] == '?' ? ++cnt, 0 : std::complex<double>{sin(2 * PI * s[i][j] / 26), -cos(2 * PI * s[i][j] / 26)}; } } for (int i = 0; i < 1 << MAX - 1; ++i) { for (int j = 0; j < 1 << MAX - 1; ++j) { a[i][j] = a[i % n][j % m]; } } mult(a, {1 << MAX - 1, 1 << MAX - 1}, b, {r, c}); for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { putchar(std::abs(r * c - cnt - a[i + r - 1][j + c - 1].real()) < eps ? '1' : '0'); } putchar('\n'); } return 0; }
5
/* * File: main.cpp * Author: administrator * * Created on 2011年8月27日, 下午12:42 */ #include <stdlib.h> #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <string> #include <map> #include <set> #include <math.h> using namespace std; /* * */ struct node{ int good,up; }a[50]; bool cmp(node b,node c){ return b.up<c.up; } int main(int argc, char** argv) { int n,i,j; while(scanf("%d",&n),n){ for(i=1;i<=n;i++) scanf("%d %d",&a[i].good,&a[i].up); sort(a+1,a+n+1,cmp); int tot=0; for(i=1;i<=n;i++) if(tot>a[i].up)break; else { tot+=a[i].good; if(tot>a[i].up)break; } if(i==n+1)printf("Yes\n"); else printf("No\n"); } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long ans, res; signed main() { long long n; cin >> n; string s, t; cin >> s >> t; string s1, t1, s2, t2; long long l = 0, r = n - 1; while (s[l] == t[l]) l++; while (s[r] == t[r]) r--; for (long long i = l; i <= r; i++) { s1 += s[i]; t1 += t[i]; } t2 = s1; s2 = t1; s1.erase(s1.begin()); t1.erase(t1.end() - 1); ans += s1 == t1; s2.erase(s2.begin()); t2.erase(t2.end() - 1); ans += s2 == t2; cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, k, l, r, m; cin >> n; long long a[n + 7]; for (long long i = 1; i <= n; i++) { cin >> a[i]; if (i > 1) a[i] = a[i] + a[i - 1]; } cin >> k; long long b[k + 7]; for (long long i = 1; i <= k; i++) cin >> b[i]; for (long long i = 1; i <= k; i++) { l = 1; r = n; if (b[i] <= a[1]) r = 1; else while (r - l > 1) { m = (l + r) / 2; if (a[m] < b[i]) l = m; else r = m; } cout << r << "\n"; } return 0; }
2
#include <iostream> #include <vector> using namespace std; int t,k,n,m; vector<int> a,b,v; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin>>t; while (t--) { cin>>k>>n>>m; a.resize(n); b.resize(m); for (int &x:a) cin>>x; for (int &x:b) cin>>x; auto solve=[&]()->vector<int> { vector<int> sequences; int l=0,r=0,cur=k; while (true) { if (l==n && r==m) break; if (l!=n && a[l]>=0 && a[l]<=cur) { if (a[l]==0) cur++; sequences.emplace_back(a[l++]); } else if (r!=m && b[r]>=0 && b[r]<=cur) { if (b[r]==0) cur++; sequences.emplace_back(b[r++]); } else return vector<int> {-1}; } return sequences; }; v=solve(); for (int i=0;i<(int)v.size();i++) cout<<v[i]<<" \n"[i==(int)v.size()-1]; } }
3
#include<bits/stdc++.h> #define int long long using namespace std; const int N=2e5+5; int read() { int s=0; char c=getchar(),lc='+'; while (c<'0'||'9'<c) lc=c,c=getchar(); while ('0'<=c&&c<='9') s=s*10+c-'0',c=getchar(); return lc=='-'?-s:s; } void write(int x) { if (x<0) putchar('-'),x=-x; if (x<10) putchar(x+'0'); else write(x/10),putchar(x%10+'0'); } void print(int x=-1,char c='\n') { write(x); putchar(c); } string s,tmp,a[N]; map<string,vector<string>>mp; int lcp[N],ri[N],ans=0; struct stk { int l,r,lcp; }st[N]; struct trie { int cnt; vector<int>w[N]; struct node { int son[26]; vector<int>pos; }t[N]; void insert(int m,int pos,string &s) { w[pos].resize(m+1); int now=0; t[0].pos.push_back(-pos); #define to t[now].son[s[i]-'a'] for (int i=m;i>=1;i--) { if (!to) to=++cnt; now=to; t[now].pos.push_back(-pos); w[pos][i]=now; } #undef to } int query(int pos,int m,int x,int l,int r) { int now=0; if (x<=m) now=w[pos][x]; // for (int i:t[now].pos) print(i,'.');puts(""); int L=lower_bound(t[now].pos.begin(),t[now].pos.end(),-r)-t[now].pos.begin(); int R=upper_bound(t[now].pos.begin(),t[now].pos.end(),-l)-t[now].pos.begin(); return R-L; } void clear(int n) { for (int i=1;i<=n;i++) w[i].clear(); for (int i=0;i<=cnt;i++) for (int j=0;j<26;j++) t[i].son[j]=0; for (int i=0;i<=cnt;i++) t[i].pos.clear(); cnt=0; } }t; void solve(int n) { int tot0=0,tot1=0,top=0,m=a[1].size()-1; sort(a+1,a+1+n); for (int i=1;i<n;i++) lcp[i]=0; for (int i=1;i<n;i++) for (int j=1;j<=m;j++) if (a[i][j]==a[i+1][j]) lcp[i]=j; else break; st[0]=(stk){n+1,n,m}; st[++top]=(stk){n,n,m}; t.insert(m,n,a[n]); for (int i=n-1;i>=1;i--) { while (top&&st[top].lcp>=lcp[i]) top--; top++; st[top]=(stk){i+1,st[top-1].l-1,lcp[i]}; t.insert(m,i,a[i]); ri[m]=m; for (int j=m-1;j>=1;j--) if (a[i][j]<=a[i][j+1]) ri[j]=ri[j+1]; else ri[j]=j; // print(i,' ');print(top); for (int j=1;j<=top;j++) { int x=st[j].lcp; // print(i,',');print(ri[x+1]); if (x==m) tot0+=st[j].r-st[j].l+1; else tot1+=t.query(i,m,ri[x+1]+1,st[j].l,st[j].r); } st[++top]=(stk){i,i,m}; } int tot2=n*(n-1)/2-tot0-tot1; ans+=tot1+tot2*2; t.clear(n); // cout<<a[1]<<endl; // print(tot0); // print(tot1); // print(tot2); } signed main(signed Recall,char *_902_[]) { (void)Recall,(void)_902_; int n=read(),tot=n*(n-1)/2; for (int i=1;i<=n;i++) { cin>>s; tmp=s; sort(tmp.begin(),tmp.end()); mp[tmp].push_back(s); } for (auto x:mp) { int m=0; for (string i:x.second) a[++m]=" "+i; solve(m); tot-=1LL*m*(m-1)/2; } ans+=tot*1337; print(ans); return 0; } //.................... .. . . . .. . ......... //................... ....,]]]]]]`. . . . .,@@@@@@`.. . ........ //................... ,@@@@@@@@@@@@@@@@@@\].,]]]]]`,@@@@@@@@@@@. . ........ //....................@^...[@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@` ....... //...................,@`......\@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@`. ....... //...................=@......../@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@`. ....... //...................=@....../@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@. ....... //...................=@....=@@@@@@/[[[[[@@@@@@@@@@@@@@@@@/`...[@@@@^ ....... //...................=@.../@@@/`...........\@@@@@@@@@@@`........,@@@. . ........ //......,/@@@@@@@O\]`.@`.=@@@`...............\@@@@@@@@`...........@@^ ........ //....,@^........,/@@@@^,@@/..................,@@@@@@^............,@@. ......... //....//......,/@[=OOOOO=@@....................,@@@@@...]O@@]......=@^ ......... //....@`....]@OO^.=OOO/@@@^.../@@@@@@@`.........=@@@@.=@@@@@@@@....=@^ ......... //...=@...,@OOO/.......=@@^.,@@@@@@@@@@@........=@@@@,@@@@@@@@@@...=@^ .......... //...=@..=@`............\@^,@@@@@@@@@@@@@........@@@@=@@@@@@@@@@^..=@. ............. //...=@.=@@OO@@]........@@@=@@@@@@@@@@@@@^......=@@@@O@@@@@@@@@@^..//. ............ //...=@,@`. .,\@`....=@\@@@@@@@@@@@@@@@^......O@@@@@@@@@@@@@@@..=@. ...... . . ....... ............. //...=@@/ ........\@`..=@.\@@@@@@@@@@@@@@^.....=@@@@@@@@@@@@@@@^./@`...,/@O[[...[[[O@@/.=/ . ...... ............... //....@@^ /@OOOO@@@\.=@ =@@@@@@@@@@@@@...../@@@@@@@@@@@@@@/]/@/...,@`...............[@@[[O@\`,[[@]............... //....\@^ =@OOOOOOOO@\.@. OO@@@@@@@@@@/...,@@@@@@@@@@@@@@@@@@@@` .,@`.......,^...........,`...,\@`..,@............. //....,@^ .=OOOOOOOOOO@\O^.\OOOO@@@@@@@@@@@@@@@@@@@@@@@@@@@@@/... ,@........=/..../@/[............\@...@`........... //.....=@. =@OOOOOOOOOO@/@.,@OOOO@@@@@@@@@@@@@@@@@@@@@@@/[... .@`...........,@`.................,@..=O........... //......@\..\OOOOOOOOOO@^=@.,@OO@@@@@@@@@@@@@@@@@@@@@^ . .O......@O@^.,@....................=\..@*.......... //.......\@..\@OOOOOOOO@O.,@`..@@@@@@@@@@@@@@@@@@@@@@O . .,O......=@@`.=^.....................@..@@@]`....... //........,@\..\@@OOOO@@^...\@@@@@@@@@@@@@@@@@@@@@@@@@. ..,/@@@@@@..=@`......=O.........../^........@]@@O\*,O@`.... //..........,O@O]....,/@......@@@@@@@@@@@@@@@@@@@@@@@@^../@`*****=@@/@[....,[@`\@.......,@` .......,@/*********@\... //..............,[\O@@@@@@@O@@@@@@@@@@@@@@@@@@@@@@@@@@@.=O*******[[[[O@@O]`]]]/@`@@@O@@O[[[***[[[\@@`*********,O@^.. //.................**O@...../@@@@@@@@@@@@@@@@@@@@@@@@@@@`*****************,\@*****[[`**************************=@... //................****,@@@@@/*,@@@@@@@@@@@@@@@@@@@@@@@@O*******************************************************O/*.. //...................************[[\OO@@@@@@O/[[[[[[[*,@@\]**************************************************,@@*... //..............................**********************.***[[@@O]]`***************************************]]@@[**.... //..........................................................*****,[[[OOOOOOOOOOOOOO@@@@@@@@@@@@O@OOOOO[[[****....... //................................................................................******************................
6
#include <bits/stdc++.h> const int N = 262144 + 5; const int mod = 998244353; using namespace std; int n, m; long long c[N], Inv2, root_c[N], inv_root_c[N]; int rev[N]; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } return x * f; } long long pow(long long a, int b, int mod) { long long res = 1; for (; b; b >>= 1, a = (long long)a * a % mod) if (b & 1) res = res * a % mod; return res; } void NTT(long long *a, int n, int f) { int h, i; register int j; for (i = 0; i < n; i++) if (i < rev[i]) swap(a[i], a[rev[i]]); for (h = 2; h <= n; h <<= 1) { long long wn = pow(3, (mod - 1) / h, mod); for (i = 0; i < n; i += h) { long long w = 1; for (j = 0; j < (h >> 1); j++, w = (long long)w * wn % mod) { long long t = (long long)a[i + j + (h >> 1)] * w % mod; a[i + j + (h >> 1)] = ((a[i + j] - t) % mod + mod) % mod; a[i + j] = (a[i + j] + t) % mod; } } } if (f == -1) { for (i = 1; i < (n >> 1); i++) swap(a[i], a[n - i]); long long inv = pow(n, mod - 2, mod); for (i = 0; i < n; i++) a[i] = (long long)a[i] * inv % mod; } } void Get_Inv(long long *a, long long *b, int n) { static long long tmp[N]; if (n == 1) { b[0] = pow(a[0], mod - 2, mod); return; } Get_Inv(a, b, n >> 1); memcpy(tmp, a, sizeof(a[0]) * n); memset(tmp + n, 0, sizeof(a[0]) * n); int m = n, L = 0, nn = n; for (n = 1; n <= m; n <<= 1) L++; int i; for (i = 0; i < n; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (L - 1)); NTT(tmp, n, 1), NTT(b, n, 1); for (i = 0; i < n; i++) tmp[i] = (long long)b[i] * (((2LL - (long long)tmp[i] * b[i] % mod) % mod + mod) % mod) % mod; NTT(tmp, n, -1); for (i = 0; i < (n >> 1); i++) b[i] = tmp[i]; memset(b + nn, 0, sizeof(b[0]) * nn); n = nn; } void Get_Root(long long *a, long long *b, int n) { static long long tmp[N], inv_b[N]; if (n == 1) { b[0] = 1; return; } int i; Get_Root(a, b, n >> 1); memset(inv_b, 0, sizeof(a[0]) * n); Get_Inv(b, inv_b, n); memcpy(tmp, a, sizeof(a[0]) * n); memset(tmp + n, 0, sizeof(a[0]) * n); int m = n, L = 0, nn = n; for (n = 1; n <= m; n <<= 1) L++; for (i = 0; i < n; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (L - 1)); NTT(tmp, n, 1), NTT(b, n, 1), NTT(inv_b, n, 1); for (i = 0; i < n; i++) tmp[i] = (long long)Inv2 * ((b[i] + (long long)inv_b[i] * tmp[i] % mod) % mod) % mod; NTT(tmp, n, -1); for (i = 0; i < (n >> 1); i++) b[i] = tmp[i]; memset(b + nn, 0, sizeof(b[0]) * nn); n = nn; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) c[read()]++; Inv2 = pow(2, mod - 2, mod); c[0] = ((1 - c[0]) % mod + mod) % mod; for (int i = 1; i <= 100000; i++) c[i] = ((-4 * c[i]) % mod + mod) % mod; int l; for (l = 1; l <= m; l <<= 1) ; Get_Root(c, root_c, l); root_c[0] = (1 + root_c[0]) % mod; Get_Inv(root_c, inv_root_c, l); for (int i = 0; i <= 100000; i++) inv_root_c[i] = ((long long)2 * inv_root_c[i]) % mod; for (int i = 1; i <= m; i++) printf("%d\n", inv_root_c[i]); }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, a[1005], i, sum; while (cin >> n) { sum = 0; for (i = 0; i < n; ++i) { cin >> a[i]; } for (i = 1; i < n - 1; ++i) { if (a[i] > a[i - 1] && a[i] > a[i + 1]) { sum++; } else if (a[i] < a[i - 1] && a[i] < a[i + 1]) { sum++; } } cout << sum << endl; } return 0; }
1
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0425" #include<bits/stdc++.h> using namespace std; #define call_from_test #ifndef call_from_test #include<bits/stdc++.h> using namespace std; using Int = long long; #endif //BEGIN CUT HERE struct FastIO{ FastIO(){ cin.tie(0); ios::sync_with_stdio(0); } }fastio_beet; //END CUT HERE #ifndef call_from_test signed main(){ return 0; } #endif #ifndef call_from_test #include<bits/stdc++.h> using namespace std; #endif //BEGIN CUT HERE struct Mo{ using F = function<void(int)>; vector<int> ls,rs,ord; int n,width,nl,nr,ptr; F expandL,expandR; F shrinkL,shrinkR; Mo(int n,int width,F expandL,F expandR,F shrinkL,F shrinkR): n(n),width(width),nl(0),nr(0),ptr(0), expandL(expandL),expandR(expandR), shrinkL(shrinkL),shrinkR(shrinkR){} Mo(int n,int width,F expand,F shrink){ *this=Mo(n,width,expand,expand,shrink,shrink); } void add(int l,int r){ ls.emplace_back(l); rs.emplace_back(r); } void build(){ ord.resize(ls.size()); iota(ord.begin(),ord.end(),0); sort(ord.begin(),ord.end(), [&](int a,int b){ if(ls[a]/width!=ls[b]/width) return ls[a]<ls[b]; return bool((rs[a]<rs[b])^((ls[a]/width)&1)); }); } int process(){ if(ptr==(int)ord.size()) return -1; const int idx=ord[ptr++]; while(nl>ls[idx]) expandL(--nl); while(nr<rs[idx]) expandR(nr++); while(nl<ls[idx]) shrinkL(nl++); while(nr>rs[idx]) shrinkR(--nr); return idx; } }; //END CUT HERE #ifndef call_from_test #define call_from_test #include "../math/factorize.cpp" #include "../tools/fastio.cpp" #include "../tools/vec.cpp" #include "../tree/eulertourforedge.cpp" #undef call_from_test //INSERT ABOVE HERE signed DWANGO2017FINAL_B(){ using ll = long long; int n,q; cin>>n>>q; vector<int> x(n); for(int i=0;i<n;i++) cin>>x[i]; const int RT = 40; auto acc=make_v<int>(RT,n+1); fill_v<int>(acc,0); using P = pair<int, int>; vector<vector<P> > v(n); for(int i=0;i<n;i++){ for(auto p:factorize(x[i])){ if(p.first<RT) acc[p.first][i+1]+=p.second; else v[i].emplace_back(p); } } for(int j=0;j<RT;j++) for(int i=0;i<n;i++) acc[j][i+1]+=acc[j][i]; ll res=1; const ll MOD = 1e9+7; const int MAX = 5e5+100; vector<int> cnt(MAX,0); vector<ll> fact(MAX),invs(MAX); fact[0]=1; for(int i=1;i<MAX;i++) fact[i]=(fact[i-1]*i)%MOD; invs[1]=1; for(int i=2;i<MAX;i++) invs[i]=invs[MOD%i]*(MOD-MOD/i)%MOD; auto expand=[&](int idx){ for(auto p:v[idx]){ res*=invs[cnt[p.first]+1]; res%=MOD; cnt[p.first]+=p.second; res*=cnt[p.first]+1; res%=MOD; } }; auto shrink=[&](int idx){ for(auto p:v[idx]){ res*=invs[cnt[p.first]+1]; res%=MOD; cnt[p.first]-=p.second; res*=cnt[p.first]+1; res%=MOD; } }; Mo mo(n,400,expand,shrink); for(int i=0;i<q;i++){ int l,r; cin>>l>>r; l--; mo.add(l,r); } mo.build(); vector<ll> ans(q); for(int i=0;i<q;i++){ int k=mo.process(); ans[k]=res; int l=mo.ls[k],r=mo.rs[k]; for(int j=0;j<RT;j++){ ans[k]*=acc[j][r]-acc[j][l]+1; ans[k]%=MOD; } } for(int i=0;i<q;i++) cout<<ans[i]<<"\n"; cout<<flush; return 0; } /* verified on 2019/11/12 https://atcoder.jp/contests/dwacon2017-honsen/tasks/dwango2017final_b */ signed ABC133_F(){ int n,q; cin>>n>>q; vector<int> as(n),bs(n),cs(n),ds(n); vector<int> xs(q),ys(q),us(q),vs(q); EulerTourForEdge et(n); for(int i=1;i<n;i++){ cin>>as[i]>>bs[i]>>cs[i]>>ds[i]; as[i]--;bs[i]--; et.add_edge(as[i],bs[i]); } et.build(); vector<int> idx(n,0); for(int i=1;i<n;i++) idx[et.child(as[i],bs[i])]=i; for(int i=0;i<q;i++){ cin>>xs[i]>>ys[i]>>us[i]>>vs[i]; us[i]--;vs[i]--; } int all=0; vector<int> cnt(n),sum(n),app(n,0); auto exec= [&](int k){ int v=et.bottom(k); int e=idx[v]; app[v]^=1; if(app[v]){ all+=ds[e]; cnt[cs[e]]++; sum[cs[e]]+=ds[e]; }else{ all-=ds[e]; cnt[cs[e]]--; sum[cs[e]]-=ds[e]; } }; Mo mo(n*2,400,exec,exec); for(int i=0;i<q;i++){ auto f=[&](int l,int r){mo.add(min(l,r),max(l,r));}; et.query(us[i],vs[i],f); } mo.build(); vector<int> ans(q,0); for(int i=0;i<q;i++){ int k=mo.process(); ans[k]=all-sum[xs[k]]+cnt[xs[k]]*ys[k]; } for(int i=0;i<q;i++) cout<<ans[i]<<"\n"; cout<<flush; return 0; } /* verified on 2019/11/12 https://atcoder.jp/contests/abc133/tasks/abc133_f */ signed main(){ //DWANGO2017FINAL_B(); //ABC133_F(); return 0; } #endif #undef call_from_test signed main(){ int n,k,q; cin>>n>>k>>q; vector<int> as(k),bs(k); for(int i=0;i<k;i++) cin>>as[i]>>bs[i],as[i]--,bs[i]--; vector<int> ord(n),pos(n); iota(ord.begin(),ord.end(),0); iota(pos.begin(),pos.end(),0); auto moveL= [&](int i){ int x=pos[as[i]],y=pos[bs[i]]; swap(ord[x],ord[y]); swap(pos[ord[x]],pos[ord[y]]); }; auto moveR= [&](int i){ int x=as[i],y=bs[i]; swap(ord[x],ord[y]); swap(pos[ord[x]],pos[ord[y]]); }; Mo mo(q,400,moveL,moveR,moveL,moveR); vector<int> qs(q),ls(q),rs(q),xs(q); for(int i=0;i<q;i++){ cin>>qs[i]>>ls[i]>>rs[i]>>xs[i]; ls[i]--;xs[i]--; mo.add(ls[i],rs[i]); } mo.build(); vector<int> ans(q,-1); for(int i=0;i<q;i++){ int p=mo.process(); if(qs[p]==1) ans[p]=ord[xs[p]]+1; if(qs[p]==2) ans[p]=pos[xs[p]]+1; } for(int a:ans) cout<<a<<"\n"; cout<<flush; return 0; }
0
#include<bits/stdc++.h> using namespace std; #define ll long long #define r(i, n) for(int i=0;i<n;i++) int main() { int n; cin >> n; vector<int> v(n); r(i, n) cin >> v[i]; int q, x, p; cin >> q; while (q--) { cin >> x; p = lower_bound(v.begin(), v.end(), x) - v.begin(); if (p != n && v[p] == x) cout << 1 << endl; else cout << 0 << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int a[100100]; int main() { ios::sync_with_stdio(0); int T; cin >> T; while (T--) { int n; cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; sort(a, a + n); int res = 1; for (int i = 1; i < n; ++i) { if (a[i] != a[i - 1]) res++; } printf("%d\n", res); } }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 100008, maxm = 200008; int in[maxn], sta[maxn << 1], sta_size, pos[maxn], n; long long d[maxn], ans; struct Tedge { int start[maxn], tot; struct arr { int a, next, c; } e[maxm]; void clear() { memset(start, 0, sizeof(start)); tot = 1; } void add(int x, int y, int z) { e[++tot].a = y; e[tot].next = start[x]; start[x] = tot; e[tot].c = z; e[++tot].a = x; e[tot].next = start[y]; start[y] = tot; e[tot].c = z; } } E; const int Tst_maxn = 200008; struct Tst { int n, log[Tst_maxn]; long long f[20][Tst_maxn]; void make(int a[], int len) { n = len; for (int i = 1; i <= n; i++) f[0][i] = d[a[i]]; for (int i = 1; i <= 17; i++) for (int j = 1; j + (1 << i) - 1 <= n; j++) f[i][j] = (f[i - 1][j] < f[i - 1][j + (1 << i - 1)] ? f[i - 1][j] : f[i - 1][j + (1 << i - 1)]); for (int i = 1; i <= n; i++) for (int j = i >> 1; j; log[i]++, j >>= 1) ; } long long ask(int l, int r) { l = pos[l]; r = pos[r]; if (r < l) printf("error\n"); int k = log[r - l + 1]; return (f[k][l] < f[k][r - (1 << k) + 1] ? f[k][l] : f[k][r - (1 << k) + 1]); } } St; struct Tcmp { bool operator()(int i, int j) { return pos[i] < pos[j]; } }; set<int, Tcmp> F; struct Tprogram { void open() { freopen("1.in", "r", stdin); freopen("1.out", "w", stdout); } void close() { fclose(stdin); fclose(stdout); } void dfs(int u) { in[u] = 1; sta[++sta_size] = u; pos[u] = sta_size; for (int i = E.start[u], v; v = E.e[i].a, i; i = E.e[i].next) if (!in[v]) { d[v] = d[u] + E.e[i].c; dfs(v); sta[++sta_size] = u; } } void init() { scanf("%d", &n); int u, v, w; E.clear(); for (int i = 1; i <= n - 1; i++) { scanf("%d%d%d", &u, &v, &w); E.add(u, v, w); } dfs(1); St.make(sta, sta_size); } void Erase(int u) { set<int, Tcmp>::iterator p = F.find(u); long long x, y; if (F.size() > 1) { set<int, Tcmp>::iterator l = p, r = p; l--; r++; if (p == F.begin()) x = St.ask(*p, *r), y = St.ask(*r, *(--F.end())); else if (r == F.end()) x = St.ask(*l, *p), y = St.ask(*F.begin(), *l); else { x = St.ask(*l, *p), y = St.ask(*p, *r); if (x < y) swap(x, y); } ans -= (y - x > 0 ? y - x : 0) + d[u] - x; } F.erase(p); } void Insert(int u) { F.insert(u); set<int, Tcmp>::iterator p = F.find(u); long long x, y; if (F.size() > 1) { set<int, Tcmp>::iterator l = p, r = p; l--; r++; if (p == F.begin()) x = St.ask(*p, *r), y = St.ask(*r, *(--F.end())); else if (r == F.end()) x = St.ask(*l, *p), y = St.ask(*F.begin(), *l); else { x = St.ask(*l, *p), y = St.ask(*p, *r); if (x < y) swap(x, y); } ans += (y - x > 0 ? y - x : 0) + d[u] - x; } } void work() { int q, k; char s[106]; scanf("%d", &q); for (int i = 1; i <= q; i++) { scanf("%s", &s); if (s[0] == '?') printf("%I64d\n", ans); else { scanf("%d", &k); if (s[0] == '+') Insert(k); else Erase(k); } } } } Program; int main() { Program.init(); Program.work(); return 0; }
5
#include<iostream> #include<algorithm> using namespace std; int main() { int n; long long a[200020], b[200020]; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; b[i] = a[i] - i; } sort(b, b + n); long long cen = b[n / 2]; long long sad = 0; for (int i = 0; i < n; i++) { sad += abs(b[i] - cen); } cout << sad << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long OO = 1e16; const double EPS = (1e-7); int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; } void fast() { std::ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); } int s, x, xx; int t, tt; int p, d; int main() { fast(); cin >> s >> x >> xx >> tt >> t >> p >> d; int byf = abs(x - xx) * t; int byt = 0; if (d == -1 && p >= x) { byt = (p - x) * tt; if (xx - x < 0) { byt += abs(xx - x) * tt; } else { byt += (x + xx) * tt; } } else if (d == -1 && p < x) { byt = (p * tt) + (x * tt); if (xx - x < 0) { byt += (2 * s - (x + xx)) * tt; } else { byt += abs(xx - x) * tt; } } else if (d == 1 && p > x) { byt = (s - p + s - x) * tt; if (xx - x < 0) { byt += (x - xx) * tt; } else { byt += (x + xx) * tt; } } else if (d == 1 && p <= x) { byt = (x - p) * tt; if (xx - x < 0) { byt += (s - x + s - xx) * tt; } else { byt += abs(x - xx) * tt; } } cout << min(byt, byf) << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const long long M = 1e9 + 7; template <typename T> void print(vector<T>& v) { cout << "["; for (int i = 0; i < v.size(); ++i) { cout << v[i]; if (i != v.size() - 1) cout << ", "; } cout << "]\n"; } template <typename T> void print(set<T>& v) { cout << "["; for (auto it : v) { cout << it; if (it != *v.rbegin()) cout << ", "; } cout << "]\n"; } template <typename T, typename S> void print(map<T, S>& v) { for (auto it : v) cout << it.first << " : " << it.second << "\n"; } template <typename T, typename S> void print(pair<T, S>& v) { cout << "( " << v.first << ", " << v.second << " )" << '\n'; } template <typename T> void print(T x) { cout << x << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; map<int, int> lastdiffind; int bb = 0, sb = 0; for (int i = 1; i <= n; i++) { lastdiffind[bb - sb] = i - 1; int el; cin >> el; if (el == 1) bb++; if (el == 2) sb++; } lastdiffind[bb - sb] = n; vector<int> secpt; for (int i = 1; i <= n; i++) { int el; cin >> el; secpt.push_back(el); } reverse(secpt.begin(), secpt.end()); bb = 0; sb = 0; map<int, int> firstdiffind; for (int i = 1; i <= n; i++) { firstdiffind[bb - sb] = i - 1; int el = secpt[i - 1]; if (el == 1) bb++; if (el == 2) sb++; } firstdiffind[bb - sb] = n; int ans = 2 * n; for (auto& p : lastdiffind) { int diff = p.first; int ind1 = p.second; if (firstdiffind.count(-diff)) { ans = min(ans, n - ind1 + n - firstdiffind[-diff]); } else continue; } cout << ans << '\n'; } }
3
#include<iostream> #include<algorithm> #include<vector> #include<tuple> using namespace std; #pragma warning(disable:4996) int H, W, Q, a[60000], b[60000], c[60000], d[60000], e, f, g; int main() { while (true) { cin >> H >> W >> Q; if (H == 0)break; for (int i = 0; i < H; i++) { a[i] = -1; c[i] = 0; } for (int i = 0; i < W; i++) { b[i] = -1; d[i] = 0; } for (int i = 0; i < Q; i++) { scanf("%d%d%d", &e, &f, &g); if (e == 0) { a[f] = i; c[f] = g; } if (e == 1) { b[f] = i; d[f] = g; } } long long R1 = 0, R2 = 0, M = 0, cnt = 0; vector<tuple<int, int, int>>tup; for (int i = 0; i < H; i++)tup.push_back(make_tuple(a[i], 0, c[i])); for (int i = 0; i < W; i++)tup.push_back(make_tuple(b[i], 1, d[i])); sort(tup.begin(), tup.end()); for (int i = 0; i < tup.size(); i++) { int a1 = get<0>(tup[i]), a2 = get<1>(tup[i]), a3 = get<2>(tup[i]); if (a2 == 0)R1++; if (a2 == 1)R2++; if (a3 == 1)cnt += R1*R2 - M; M = R1*R2; } cout << cnt << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; pair<pair<int, int>, int> datas[300005]; int main() { int N; scanf("%d", &N); for (int a = 1; a <= N; a++) { scanf("%d %d", &datas[a].first.first, &datas[a].first.second); datas[a].second = a; } sort(datas + 1, datas + N + 1); int posy = -1, posx = -1; for (int a = 1; a < N; a++) { int L1 = datas[a].first.first, L2 = datas[a + 1].first.first, R1 = datas[a].first.second, R2 = datas[a + 1].first.second; int ix1 = datas[a].second, ix2 = datas[a + 1].second; if (L1 == L2) { posy = ix1; posx = ix2; break; } else if (R1 >= R2) { posy = ix2; posx = ix1; } } printf("%d %d\n", posy, posx); return 0; }
3
#include <bits/stdc++.h> using namespace std; int a[1001]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int ans = 0; for (int i = 1; i < n - 1; i++) { if (a[i] > a[i - 1] and a[i] > a[i + 1]) ans++; if (a[i] < a[i - 1] and a[i] < a[i + 1]) ans++; } cout << ans; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5, M = 5; int a[N], n, Q; struct Node { int mat[M][M]; Node() {} Node(int x) { memset(mat, 0x3f, sizeof mat); for (int i = 0; i <= M - 1; ++i) mat[i][i] = 0; if (x == 2) mat[0][0] = 1, mat[0][1] = 0; else if (x == 0) mat[1][1] = 1, mat[1][2] = 0; else if (x == 1) mat[2][2] = 1, mat[2][3] = 0; else if (x == 7) mat[3][3] = 1, mat[3][4] = 0; else if (x == 6) mat[3][3] = 1, mat[4][4] = 1; } friend Node operator*(Node a, Node b) { Node c(-1); memset(c.mat, 0x3f, sizeof mat); for (int i = 0; i <= M - 1; ++i) for (int j = 0; j <= M - 1; ++j) for (int k = 0; k <= M - 1; ++k) c.mat[i][j] = min(c.mat[i][j], a.mat[i][k] + b.mat[k][j]); return c; } } t[N << 2]; inline void build(int k, int l, int r) { if (l == r) { t[k] = Node(a[l]); return; } int md = l + r >> 1; build(k << 1, l, md), build(k << 1 | 1, md + 1, r); t[k] = t[k << 1] * t[k << 1 | 1]; } Node query(int k, int l, int r, int x, int y) { if (x <= l && r <= y) return t[k]; int md = l + r >> 1; Node res(-1); if (x <= md) res = res * query(k << 1, l, md, x, y); if (y > md) res = res * query(k << 1 | 1, md + 1, r, x, y); return res; } inline int query(int x, int y) { int an = query(1, 1, n, x, y).mat[0][4]; return an > n ? -1 : an; } int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> n >> Q; for (int i = 1; i <= n; ++i) { char x; cin >> x; a[i] = x - '0'; } build(1, 1, n); for (int i = 1; i <= Q; ++i) { int x, y; cin >> x >> y; cout << query(x, y) << '\n'; } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 300010; unsigned long long p[N]; struct segtree { struct node { int sz; unsigned long long sum1, sum2; node() { sum1 = 0; sum2 = 0; sz = 0; } }; node unite(const node &a, const node &b) const { node res; res.sum1 = a.sum1 * p[b.sz] + b.sum1; res.sum2 = a.sum2 + b.sum2 * p[a.sz]; res.sz = a.sz + b.sz; return res; } inline void pull(int x, int z) { tree[x] = unite(tree[x + 1], tree[z]); } int n; vector<node> tree; void build(int x, int l, int r) { if (l == r) { tree[x].sz = 1; return; } int m = (l + r) >> 1; int z = x + ((m - l + 1) << 1); build(x + 1, l, m); build(z, m + 1, r); pull(x, z); } node get(int x, int l, int r, int ll, int rr) { if (ll <= l && r <= rr) { return tree[x]; } int y = (l + r) >> 1; int z = x + ((y - l + 1) << 1); node res; if (rr <= y) { res = get(x + 1, l, y, ll, rr); } else { if (ll > y) { res = get(z, y + 1, r, ll, rr); } else { res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr)); } } pull(x, z); return res; } void modify(int x, int l, int r, int pos) { if (l == r) { tree[x].sum1 = 1; tree[x].sum2 = 1; return; } int m = (l + r) >> 1; int z = x + ((m - l + 1) << 1); if (pos <= m) { modify(x + 1, l, m, pos); } else { modify(z, m + 1, r, pos); } pull(x, z); } segtree() {} segtree(int n) : n(n) { assert(n > 0); tree.resize(2 * n - 1); build(0, 0, n - 1); } unsigned long long get1(int ll, int rr) { assert(0 <= ll && ll <= rr && rr <= n - 1); return get(0, 0, n - 1, ll, rr).sum1; } unsigned long long get2(int ll, int rr) { assert(0 <= ll && ll <= rr && rr <= n - 1); return get(0, 0, n - 1, ll, rr).sum2; } void modify(int pos) { modify(0, 0, n - 1, pos); } }; int a[N]; int main() { int n; scanf("%d", &n); segtree st(n + 1); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } p[0] = 1; for (int i = 1; i <= n; i++) { p[i] = p[i - 1] * 10007; } for (int i = 1; i <= n; i++) { int k = min(a[i] - 1, n - a[i]); if (st.get1(a[i] - k, a[i]) != st.get2(a[i], a[i] + k)) { puts("YES"); return 0; } st.modify(a[i]); } puts("NO"); return 0; }
6
#include <bits/stdc++.h> using namespace std; struct node { int u, v; bool can; node() {} node(bool can, int u, int v) : can(can), u(u), v(v) {} }; int n; vector<int> adj[200005]; int in[200005], out[200005], cnt; node st[4 * 200005]; int where[200005], val[200005]; int sp[2 * 200005 + 5][20], Euler[2 * 200005 + 5], Log[2 * 200005 + 5], L[200005], first_apperance[200005], idx; void pre_dfs(int i = 0, int p = -1) { first_apperance[i] = idx; Euler[idx++] = i; in[i] = ++cnt; L[i] = (p == -1 ? 0 : L[p] + 1); for (auto x : adj[i]) if (x != p) pre_dfs(x, i), Euler[idx++] = i; out[i] = ++cnt; } int LOG(int x) { int res = 0; while (x) x >>= 1, res++; return res - 1; } void pre_calc() { assert(idx == 2 * n - 1); int len = 2 * n - 1; for (int i = 1; i <= len; i++) Log[i] = LOG(i); for (int i = 0; i < len; i++) sp[i][0] = Euler[i]; for (int j = 1; j < 19; j++) for (int i = 0; i + (1 << j) <= len; i++) sp[i][j] = (L[sp[i][j - 1]] < L[sp[i + (1 << (j - 1))][j - 1]] ? sp[i][j - 1] : sp[i + (1 << (j - 1))][j - 1]); } int LCA(int u, int v) { u = first_apperance[u]; v = first_apperance[v]; if (u > v) swap(u, v); return (L[sp[u][Log[v - u + 1]]] < L[sp[v + 1 - (1 << Log[v - u + 1])][Log[v - u + 1]]] ? sp[u][Log[v - u + 1]] : sp[v + 1 - (1 << Log[v - u + 1])][Log[v - u + 1]]); } bool isParent(int p, int u) { return (in[p] <= in[u] && out[p] >= out[u]); } bool path_inside_path_check(int u1, int v1, int u2, int v2) { int z1 = LCA(u1, v1); int z2 = LCA(u2, v2); if (z2 == u2 || z2 == v2) return (isParent(z1, u2) && isParent(z1, v2) && ((isParent(u2, u1) && isParent(v2, u1)) || (isParent(u2, v1) && isParent(v2, v1)))); return (z1 == z2 && ((isParent(u2, u1) && isParent(v2, v1)) || (isParent(u2, v1) && isParent(v2, u1)))); } node Merge(node &path1, node &path2) { node Node(0, -1, -1); if (!path1.can || !path2.can) return Node; int vertises[4] = {path1.u, path1.v, path2.u, path2.v}; for (int i = 0; i < 4; i++) for (int j = i + 1; j < 4; j++) if (path_inside_path_check(vertises[i], vertises[j], path1.u, path1.v) && path_inside_path_check(vertises[i], vertises[j], path2.u, path2.v)) { Node.u = vertises[i]; Node.v = vertises[j]; Node.can = true; return Node; } return Node; } void build(int l = 0, int r = n - 1, int p = 1) { if (l == r) { st[p].can = true; st[p].u = st[p].v = where[l]; return; } int mid = (l + r) >> 1; build(l, mid, 2 * p); build(mid + 1, r, 2 * p + 1); st[p] = Merge(st[2 * p], st[2 * p + 1]); } void update(int idx, int l = 0, int r = n - 1, int p = 1) { if (l == r) { st[p].can = true; st[p].u = st[p].v = where[l]; return; } int mid = (l + r) >> 1; if (idx <= mid) update(idx, l, mid, 2 * p); else update(idx, mid + 1, r, 2 * p + 1); st[p] = Merge(st[2 * p], st[2 * p + 1]); } int query(node &Node, int l = 0, int r = n - 1, int p = 1) { if (l == r) return l; int mid = (l + r) >> 1; if (!st[2 * p].can) return query(Node, l, mid, 2 * p); else { node merged = Merge(Node, st[2 * p]); if (merged.can) return query(merged, mid + 1, r, 2 * p + 1); else return query(Node, l, mid, 2 * p); } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &val[i]), where[val[i]] = i; for (int u, i = 1; i < n; i++) scanf("%d", &u), u--, adj[u].push_back(i); pre_dfs(); pre_calc(); build(); int q; scanf("%d", &q); while (q--) { int t; scanf("%d", &t); if (t == 1) { int u, v; scanf("%d%d", &u, &v); u--, v--; if (u == v) continue; swap(val[u], val[v]); where[val[u]] = u; where[val[v]] = v; update(val[u]); update(val[v]); } else { if (st[1].can) printf("%d\n", n); else { node Node; Node.can = true; Node.u = Node.v = where[0]; printf("%d\n", query(Node)); } } } return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; if (n % 4 != 0) { cout << "NO\n"; continue; } cout << "YES\n"; int d = 2, x = 2; for (int i = 1; i <= n / 2; i++) { cout << x << " "; x += d; (d == 2) ? d = 4 : d = 2; } d = 4; x = 1; for (int i = 1; i <= n / 2; i++) { cout << x << " "; x += d; (d == 2) ? d = 4 : d = 2; } cout << "\n"; } return 0; }
2
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vs = vector<string>; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); int N, M; cin >> N >> M; int totalSize = 0; vi sz(N); vector<set<int>> boxes(N); vvi have(M); for (int i = 0; i < N; i++) { cin >> sz[i]; totalSize += sz[i]; for (int j = 0, v; j < sz[i]; j++) { cin >> v; have[v - 1].push_back(i); boxes[i].insert(v - 1); } } int small = totalSize / N; int big = small + !!(totalSize % N); int needUp = 0; int needDown = 0; for (int v : sz) { if (v < small) needUp += small - v; if (v > big) needDown += v - big; } int extrDown = max(0, needUp - needDown); int extrUp = max(0, needDown - needUp); set<int> smalls; for (int i = 0; i < N; i++) { if (sz[i] < small || (sz[i] < big && extrUp > 0)) { if (sz[i] == small) extrUp--; smalls.insert(i); } } vector<array<int, 3>> moves; for (int t = 0; t < M; t++) { auto it = smalls.begin(); for (int b : have[t]) { if (sz[b] > big || (sz[b] > small && extrDown > 0)) { if (sz[b] == big) extrDown--; while (it != smalls.end() && boxes[*it].count(t)) it++; if (it != smalls.end()) { moves.push_back({b, *it, t}); sz[b]--; ++sz[*it]; if (sz[*it] < small || (sz[*it] < big && extrUp > 0)) { if (sz[*it] == small) extrUp--; it++; } else { it = smalls.erase(it); } } } } } cout << moves.size() << "\n"; for (auto [f, t, k] : moves) { cout << f + 1 << " " << t + 1 << " " << k + 1 << "\n"; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int freq[100005]; int main() { int n; cin >> n; int arr[n]; for (int i = 1; i <= n; i++) cin >> arr[i]; for (int i = 1; i <= n; i++) { freq[arr[i]] = freq[arr[i] - 1] + 1; } int f = 0; for (int i = 1; i <= n; i++) { f = max(f, freq[i]); } int diff = n - f; cout << diff << endl; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 100100, B = 100002; struct edge { int v, w, c; edge() {} edge(int v, int w, int c) : v(v), w(w), c(c) {} }; int root, tot_size; int size[maxn]; bool done[maxn]; vector<edge> g[maxn]; int n, ans = 1, anss = 1; int tag[2][maxn * 2], idx; pair<int, int> c[2][maxn * 2]; vector<int> v1, v2; vector<int> s1, s2; void getroot(int t, int fa) { size[t] = 1; int tmax = 0; for (auto e : g[t]) { int v = e.v; if (v != fa && !done[v]) { getroot(v, t); size[t] += size[v]; tmax = max(tmax, size[v]); } } tmax = max(tmax, tot_size - size[t]); if (tmax <= tot_size / 2) root = t; } inline int pow(int a, int b) { int res = 1; for (; b; b >>= 1, a = (long long)a * a % 1000000007) if (b & 1) res = (long long)res * a % 1000000007; return res; } pair<int, int> ask(int x, int k) { if (x < 0) return make_pair(0, 1); int res1 = 0, res2 = 1; for (; x; x -= x & -x) if (tag[k][x] == idx) res1 += c[k][x].first, res2 = (long long)res2 * c[k][x].second % 1000000007; return make_pair(res1, res2); } void add(int x, int k, int v) { for (; x <= 2 * n; x += x & -x) if (tag[k][x] == idx) ++c[k][x].first, c[k][x].second = (long long)c[k][x].second * v % 1000000007; else tag[k][x] = idx, c[k][x].first = 1, c[k][x].second = v; } void dfs(int t, int fa, int val, int a, int b) { int v1 = 2 * a - b, v2 = 2 * b - a; auto t1 = ask(n - v1 - 1, 0), t2 = ask(n - v2 - 1, 1); ans = (long long)ans * t1.second % 1000000007 * pow(val, t1.first) % 1000000007; ans = (long long)ans * t2.second % 1000000007 * pow(val, t2.first) % 1000000007; for (auto e : g[t]) { int v = e.v, w = e.w, c = e.c; if (done[v] || v == fa) continue; if (c) dfs(v, t, (long long)val * w % 1000000007, a + 1, b); else dfs(v, t, (long long)val * w % 1000000007, a, b + 1); } } void dfs2(int t, int fa, int val, int a, int b) { int v1 = 2 * a - b, v2 = 2 * b - a; add(v1 + n, 0, val); add(v2 + n, 1, val); for (auto e : g[t]) { int v = e.v, w = e.w, c = e.c; if (done[v] || v == fa) continue; if (c) dfs2(v, t, (long long)val * w % 1000000007, a + 1, b); else dfs2(v, t, (long long)val * w % 1000000007, a, b + 1); } } void work(int now) { idx++; add(n, 0, 1); add(n, 1, 1); for (auto e : g[now]) { int v = e.v, w = e.w, c = e.c; if (done[v]) continue; if (c) dfs(v, now, w, 1, 0), dfs2(v, now, w, 1, 0); else dfs(v, now, w, 0, 1), dfs2(v, now, w, 0, 1); } getroot(root, 0); done[now] = 1; for (auto e : g[now]) { int v = e.v; if (!done[v]) { tot_size = size[v]; getroot(v, 0); work(root); } } } void dfs(int t, int fa) { size[t] = 1; for (auto e : g[t]) { int v = e.v, w = e.w; if (v == fa) continue; dfs(v, t); size[t] += size[v]; anss = (long long)anss * pow(w, int((long long)size[v] * (n - size[v]) % (1000000007 - 1))) % 1000000007; } } int main() { scanf("%d", &n); tot_size = n; for (int i = 1; i < n; ++i) { static int u, v, w, c; scanf("%d%d%d%d", &u, &v, &w, &c); g[u].push_back(edge(v, w, c)); g[v].push_back(edge(u, w, c)); } getroot(1, 0); work(root); dfs(1, 0); anss = (long long)anss * pow(ans, 1000000007 - 2) % 1000000007; printf("%d\n", anss); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int a, b, c, ans = INT_MAX; cin >> a >> b >> c; for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { for (int k = -1; k < 2; k++) { int x = a + i, y = b + j, z = c + k; int dis = abs(x - y) + abs(x - z) + abs(y - z); ans = min(ans, dis); } } } cout << ans << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; template <class T> inline T sqr(const T& x) { return x * x; } template <class T> inline void updMin(T& a, const T& b) { if (b < a) a = b; } template <class T> inline void updMax(T& a, const T& b) { if (b > a) a = b; } int n, d, h; void bad() { puts("-1"); exit(0); } vector<pair<int, int> > ans; int NEW_V = 1; int add(int p) { NEW_V++; ans.push_back(make_pair(p, NEW_V)); return NEW_V; } int main() { cin >> n >> d >> h; if (2 * h < d) { bad(); } if (d < h) { bad(); } if (d == 1 && n > 2) { bad(); } int added = 1; int prev = 1; int mem = 1; for (int i = 0; i < h; i++) { prev = add(prev); if (i != h - 1) mem = prev; added++; } prev = 1; for (int i = 0; i < d - h; i++) { prev = add(prev); added++; } if (added > n) { bad(); } for (int i = 0; i < n - added; i++) { add(mem); } for (int i = 0; i < ans.size(); i++) { cout << ans[i].first << " " << ans[i].second << "\n"; } return 0; }
3
#include <bits/stdc++.h> int n, m, k; int rows[1000]; int main() { int i, r, x; long long int sum = 0; scanf("%d%d%d", &n, &m, &k); for (i = 0; i < m; i++) rows[i] = 1000001; for (i = 0; i < n; i++) { scanf("%d%d", &r, &x); if (x < rows[r - 1]) rows[r - 1] = x; } for (i = 0; i < m; i++) if (rows[i] != 1000001) sum += rows[i]; if (sum < k) printf("%lld\n", sum); else printf("%d\n", k); return 0; }
1
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii; typedef long long ll; typedef vector<int> vi; #define pb push_back #define eb emplace_back #define mp make_pair #define fi first #define se second #define rep(i,n) rep2(i,0,n) #define rep2(i,m,n) for(int i=m;i<(n);i++) #define ALL(c) (c).begin(),(c).end() int N; string s[510]; int main() { cin >> N; bool all = 1; rep(i, N) { cin >> s[i]; rep(j, N) { if (s[i][j] != '.') { all = 0; } } } if (all) { puts("-1"); return 0; } int ns = 0; rep(j, N) { int f = 0; rep(i, N) { if (s[i][j] == '.') { ++f; } } ns += (f > 0); } int ret = N * 2; rep(i, N) { int w = 0; rep(j, N) { if (s[i][j] == '.') { ++w; } } bool can = 0; rep(ii, N) if (s[ii][i] == '#') { can = 1; } int t = w + ns + (!can); ret = min(ret, t); } cout << ret << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; int n, m; vector<int> s[N]; vector<int> v[N]; int col[N]; int mark[N]; void dfs(int node, int parent) { for (int x : s[node]) { mark[col[x]] = node; } int cur = 1; for (int x : s[node]) { if (!col[x]) { while (mark[cur] == node) { ++cur; } col[x] = cur; mark[cur] = node; } } for (int next : v[node]) { if (next != parent) { dfs(next, node); } } } int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; ++i) { int siz; scanf("%d", &siz); s[i].clear(); s[i].resize(siz); for (int j = 0; j < siz; ++j) { scanf("%d", &s[i][j]); } } for (int i = 1; i < n; ++i) { int a, b; scanf("%d %d", &a, &b); v[a].emplace_back(b); v[b].emplace_back(a); } dfs(1, 0); for (int i = 1; i <= m; ++i) { if (col[i] == 0) { col[i] = 1; } } printf("%d\n", *max_element(col + 1, col + 1 + m)); for (int i = 1; i <= m; ++i) { printf("%d%c", col[i], " \n"[i == m]); } }
3
#include <bits/stdc++.h> inline int n_black(int n, int m) { return (n * m + 1) >> 1; } int main(void) { int n, m, x; std::cin >> n >> m >> x; x = (x << 1) - 1; if (std::min(n, m) < x) { std::cout << "0\n"; } else { int answer = n_black(n - x + 1, m - x + 1); if (std::min(n, m) >= x + 2) { answer -= n_black(n - x - 1, m - x - 1); } std::cout << answer << "\n"; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> v(3001, 0); while (n--) { int input; cin >> input; v[input] = 1; } for (int i = 1; i <= 3001; i++) { if (v[i] == 0) { cout << i << "\n"; return 0; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; #define int long long signed main() { cin.tie(0); ios_base::sync_with_stdio(0); cout << fixed << setprecision(12); int w, d; while ( cin >> w >> d, w ) { vector<int> h1(w), h2(d); vector<int> c1(21, 0), c2(21, 0); for ( int i = 0; i < w; i++ ) cin >> h1[i], c1[h1[i]]++; for ( int i = 0; i < d; i++ ) cin >> h2[i], c2[h2[i]]++; int ans = 0; for ( int i = 1; i < 21; i++ ) { int mi = min(c1[i], c2[i]); ans += mi*i; c1[i] -= mi; c2[i] -= mi; ans += (c1[i]+c2[i])*i; } cout << ans << endl; } return 0; }
0
#include<bits/stdc++.h> using namespace std; int a[52][52]={0},m,n; char s; int main(){ cin>>m>>n; for(int i=1;i<=m;i++){ for(int j=1;j<=n;j++){ cin>>s; if(s=='#')a[i][j]=1; else a[i][j]=0; } } for(int i=1;i<=m;i++){ for(int j=1;j<=n;j++){ if(a[i][j]==1)cout<<'#'; else cout<<a[i-1][j-1]+a[i-1][j]+a[i-1][j+1]+a[i][j-1]+a[i][j+1]+a[i+1][j-1]+a[i+1][j]+a[i+1][j+1]; } cout<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; struct node { int a, b, c; bool operator<(const node& x) const { if (a == x.a) return c < x.c; return a < x.a; } } a[1007]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, t; cin >> t; while (t--) { cin >> n; int time = 0, ans[1007]; for (int i = 0; i < n; i++) { cin >> a[i].a >> a[i].b; a[i].c = i; } for (int i = 0; i < n; i++) { if (time >= a[i].b) { ans[a[i].c] = 0; continue; } else if (++time < a[i].a) time = a[i].a; ans[a[i].c] = time; } for (int i = 0; i < n; i++) cout << ans[i] << ' '; cout << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, m, a[200001][2], area[200001], cnt[200001], cnt_[200001], pos[200001], cntEach[200001][2]; void Convert(int x, int &r, int &c) { x--; r = x / n; c = x % n; } int main() { ios::sync_with_stdio(0); cin >> n >> m; char c; for (int j = (0), _b = (2); j < _b; j++) { for (int i = (0), _b = (n); i < _b; i++) { cin >> c; a[i][j] = (c == 'X'); } } area[0] = 0; for (int i = (1), _b = (n); i < _b; i++) { area[i] = area[i - 1]; if ((a[i][1] && a[i][0]) || (a[i][0] && a[i - 1][1]) || (a[i][1] && a[i - 1][0])) area[i]++; } cnt[0] = 0; pos[0] = 2; cntEach[0][0] = a[0][0]; cntEach[0][1] = a[0][1]; cnt_[0] = -1; int last = 0; for (int i = (1), _b = (n); i < _b; i++) { cnt[i] = cnt[i - 1]; pos[i] = pos[i - 1]; cnt_[i] = cnt[i] - 1; for (int j = (0), _b = (2); j < _b; j++) { if (j != pos[i - 1] && a[i][j]) { cnt[i]++; pos[i] = j; for (int k = (last), _b = (i); k <= _b; k++) cnt_[k]++; } if (j == pos[i] && a[i][j]) last = i; } for (int j = (0), _b = (2); j < _b; j++) cntEach[i][j] = cntEach[i - 1][j] + a[i][j]; } int r1, c1, r2, c2, res, thisC, thisP, x; for (int i = (0), _b = (m); i < _b; i++) { cin >> x; Convert(x, r1, c1); cin >> x; Convert(x, r2, c2); if (area[c1] != area[c2]) { printf("%d\n", -1); continue; } if (c1 > c2) { swap(c1, c2); swap(r1, r2); } res = c2 - c1; thisC = 0; thisP = 0; if (c2 > 0) { thisC = cnt[c2 - 1] - cnt_[c1]; thisP = pos[c2 - 1]; } if (cntEach[c2][thisP] - (c1 > 0 ? cntEach[c1 - 1][thisP] : 0) == 0) thisC = 0; if (thisC > 0 && thisP != r2) thisC--; res += thisC; if (thisC % 2) r2 = 1 - r2; printf("%d\n", res + (r1 != r2)); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, k, j = 1, x = 1; cin >> n >> k; char a[100]; for (char i = 'a'; i <= 'z'; i++) { a[j] = i; j++; } j = 1; for (int i = 1; i <= n; i++) { if (x <= k) { cout << a[j]; j++; x++; } else if (x > k) { x = 1; j = 1; cout << a[j]; j++; x++; } } cout << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii; set<int>s; struct Node { int x, y, color; inline bool operator < (const Node & rhs) const {return x < rhs.x;} }Node[300]; int n, ans; int main(void){ scanf("%d", &n); for(int i = 1; i <= n; ++i) scanf("%d%d", &Node[i].x, &Node[i].y), Node[i].color = 1; for(int i = 1; i <= n; ++i) scanf("%d%d", &Node[i+n].x, &Node[i+n].y), Node[i+n].color = 2; sort(Node+1, Node+1+n*2); for(int i = 2*n; i >= 1; --i) { if (Node[i].color == 2) s.insert(Node[i].y); else { auto it = s.lower_bound(Node[i].y); if (it != s.end()) s.erase(it), ans++; } } cout << ans; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 28; const double INF = 1e12, EPS = 1e-9; bool _(long long x, long long y, long long a, long long b) { if (a == b && b == 0) return x == 0 && y == 0; return (a * y - b * x) % (a * a + b * b) == 0 && (a * x + b * y) % (a * a + b * b) == 0; } void run() { int a1, a2, b1, b2, c1, c2; cin >> a1 >> a2 >> c1 >> c2 >> b1 >> b2; cout << (_(c1 - a1, c2 - a2, b1, b2) || _(c1 + a2, c2 - a1, b1, b2) || _(c1 + a1, c2 + a2, b1, b2) || _(c1 - a2, c2 + a1, b1, b2) ? "YES" : "NO") << endl; } int main() { run(); }
3
#include <bits/stdc++.h> using namespace std; int n; int a, b, c = 0; int main() { while (scanf("%d", &n) != EOF) { int x; bool fff = 0; a = b = c = 0; for (int i = 0; i < n; i++) { scanf("%d", &x); if (fff) continue; if (x == 25) a++; else if (x == 50) b++; else if (x == 100) c++; if (x == 50) { a--; if (a < 0) fff = 1; } else if (x == 100) { if (b >= 1 && a >= 1) { b--; a--; } else if (a >= 3) { a -= 3; } else fff = 1; } } if (fff) puts("NO"); else puts("YES"); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n], i, fl = 0; for (i = 0; i < n; i++) cin >> a[i]; stack<int> s; for (i = 1; i < n; i++) if (a[i] < a[i - 1]) fl = 1; if (fl == 0) { cout << "0\n"; return 0; } int store[n + 1][2], last; memset(store, 0, sizeof(store)); store[a[0]][0] = 1; store[a[0]][1] = 1000000007; last = a[0]; s.push(a[0]); for (i = 1; i < n; i++) { if (a[i] < last) last = a[i]; else { while (!s.empty()) { if (s.top() > a[i]) { if (store[s.top()][1] < store[s.top()][0] + 1) { s.pop(); continue; } store[s.top()][0]++; store[a[i]][1] = store[s.top()][0]; s.push(a[i]); store[a[i]][0]++; break; } else { s.pop(); } } if (s.empty()) { s.push(a[i]); store[a[i]][0] = 1; store[a[i]][1] = 1000000007; } last = a[i]; } } int maxx = 0; for (i = 1; i <= n; i++) maxx = max(maxx, store[i][0]); cout << maxx << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, k, w, i, sum[15][N], s[N]; char str[N]; int main() { scanf("%d%d%d", &n, &k, &w); scanf("%s", str + 1); for (i = 1; i <= n; i++) { s[i] = s[i - 1] + str[i] - '0'; } for (i = 1; i <= k; i++) { for (int j = i - 1 + k; j <= n; j += k) { if (str[j] == '1') { sum[i][j] = sum[i][j - k] + 1; } else sum[i][j] = sum[i][j - k]; } } int l, r; while (w--) { scanf("%d%d", &l, &r); int ll = l; l %= k; if (l == 0) l = k; int ss = sum[l][r] - sum[l][ll - 1]; int sss = s[r] - s[ll - 1]; int h = (r - ll + 1) / k; int ans = h - ss + (sss - ss); printf("%d\n", ans); } return 0; }
3
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; const int INF = INT_MAX; const ll LINF = LLONG_MAX; const int MOD = 1e9+7; const int N = 1e5+7; void test_cases(){ int n; cin >> n; int a[n]; ll sum = 0; for(int i=0;i<n;i++){ cin >> a[i]; sum += a[i]; } if(sum%n){ cout<<"-1\n"; return; } ll avg = sum / n; ll cnt = 0; for(int i=0;i<n;i++){ if(a[i] > avg)cnt++; } cout << cnt << '\n'; } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; cin >> t; for(int tt = 1; tt <= t; tt++){ test_cases(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; struct chess { int x, y; } a[2005]; int fac[200005], inv[200005], f[2005]; inline bool cmp(chess _, chess __) { return (_.x == __.x ? _.y < __.y : _.x < __.x); } int ksm(int x, int y) { return (!y ? 1 : 1LL * ksm(1LL * x * x % 1000000007, y >> 1) * (y & 1 ? x : 1) % 1000000007); } inline int C(int x, int y) { return 1LL * fac[x] * inv[y] % 1000000007 * inv[x - y] % 1000000007; } int main() { int n, h, w, i, j, k; cin >> h >> w >> n; for (i = 1; i <= (n); i++) cin >> a[i].x >> a[i].y; fac[0] = 1; for (i = 1; i <= (h + w); i++) fac[i] = 1LL * fac[i - 1] * i % 1000000007; inv[h + w] = ksm(fac[h + w], 1000000007 - 2); for (i = h + w - 1; i >= (0); i--) inv[i] = 1LL * inv[i + 1] * (i + 1) % 1000000007; a[++n].x = h; a[n].y = w; sort(a + 1, a + n + 1, cmp); for (i = 1; i <= (n); i++) { f[i] = C(a[i].x + a[i].y - 2, a[i].x - 1); for (j = 0; j <= (i - 1); j++) if (a[i].y >= a[j].y) f[i] = (f[i] - 1LL * f[j] * C(a[i].x - a[j].x + a[i].y - a[j].y, a[i].x - a[j].x)) % 1000000007; } cout << (f[n] + 1000000007) % 1000000007; return 0; }
3
#include <bits/stdc++.h> using namespace std; struct small { int row; int column; }; struct letter { vector<small> listoflet; int isOk; int isexist; }; int main() { int n; cin >> n; long long *a; a = new long long[n]; long long max = 0; int index = 0; bool fullone = true; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] != 1) fullone = false; if (a[i] > max) { max = a[i]; index = i; } } if (fullone == true) a[index] = 2; else a[index] = 1; sort(a, a + n); for (int i = 0; i < n; i++) cout << a[i] << " "; return 0; }
3
#include <bits/stdc++.h> using namespace std; class LucMas { public: LucMas() {} LucMas(int a, int b) { a = a; b = b; } int find(); int retur(int va); void puts(); int a; int b; int c; }; int LucMas::retur(int va) { int sum = 0; int temp = 1; while (va) { if (va % 10 == 4 || va % 10 == 7) { c = va % 10; sum += c * temp; temp *= 10; } va /= 10; } return sum; } int LucMas::find() { int i; int mark; if (a < b) return b; i = a + 1; while (i) { if (retur(i) == b) { mark = i; break; } ++i; } return mark; } void LucMas::puts() { int temp = find(); printf("%d\n", temp); } int main() { LucMas L; while (~scanf("%d%d", &L.a, &L.b)) { L.puts(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; inline long long isqrt(long long k) { long long r = sqrt(k) + 1; while (r * r > k) r--; return r; } inline long long icbrt(long long k) { long long r = cbrt(k) + 1; while (r * r * r > k) r--; return r; } inline long long minize(long long& a, long long b) { return a = (a > b ? b : a); } inline long long maxize(long long& a, long long b) { return a = (a < b ? b : a); } inline string toString(long long n) { stringstream ss; ss << n; return ss.str(); } double const eps = 1e-6; long long const Base = 1e9 + 7, oo = 1e18, MAXN = 1e5; long long dp[MAXN + 5][202]; class X { public: long long s, t, d, w; }; class cmp { public: bool operator()(X a, X b) { if (a.w != b.w) return a.w < b.w; else return a.d < b.d; } }; X A[MAXN + 5]; vector<long long> ds[MAXN + 5]; long long Solves() { long long m, n, k, cnt = 0, ans = oo, x, y, q, c, sum = 0, v, t; cin >> n >> m >> k; for (long long i = 1; i <= k; i++) cin >> A[i].s >> A[i].t >> A[i].d >> A[i].w, ds[A[i].s].push_back(i); for (long long i = 0; i <= 200; i++) for (long long j = 0; j <= n + 1; j++) { dp[j][i] = oo; } dp[1][0] = 0; priority_queue<X, vector<X>, cmp> pq; for (long long i = 1; i <= n + 1; i++) { while ((long long)ds[i].size()) pq.push(A[ds[i].back()]), ds[i].pop_back(); while ((long long)pq.size() and pq.top().t < i) pq.pop(); for (long long j = 0; j <= m; j++) { if ((long long)pq.size()) minize(dp[pq.top().d + 1][j], dp[i][j] + pq.top().w); minize(dp[i + 1][j + ((long long)pq.size() != 0)], dp[i][j]); } } for (long long i = 0; i <= min(m, n); i++) minize(ans, dp[n + 1][i]); if (ans == oo) ans = 0; cout << ans << endl; return 0; } int main() { long long JUDGE_ONLINE = 1; ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long test = 1; for (long long T = 1; T <= test; T++) { Solves(); } if (!JUDGE_ONLINE) cout << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; }
5
#include <bits/stdc++.h> using namespace std; const long long inf = 1LL << 61; int k[4], t[4]; int n; vector<int> c; void solve(int ncase) { cin >> k[1] >> k[2] >> k[3] >> t[1] >> t[2] >> t[3] >> n; c.assign(n, 0); for (int i = 0; i < n; i++) { cin >> c[i]; } sort(c.begin(), c.end()); vector<queue<pair<long long, long long>>> q( 4, queue<pair<long long, long long>>()); for (int i = 0; i < n; i++) { q[0].push(pair<long long, long long>(c[i], i)); } vector<vector<long long>> fin(4, vector<long long>(n)); long long cur = 0; while (!(q[0].empty() && q[1].empty() && q[2].empty())) { vector<pair<long long, long long>> tope; for (int i = 0; i < 4; i++) { if (q[i].empty()) continue; tope.push_back(pair<long long, long long>(q[i].front().first, -i)); } sort(tope.begin(), tope.end()); for (int i = 0; i < tope.size(); i++) { int type = -tope[i].second; int idx = q[type].front().second; if (type + 1 == 4 || q[type + 1].size() < k[type + 1]) { cur = max(cur, tope[i].first); if (type + 1 == 4) { q[type].pop(); break; } fin[type + 1][idx] = cur + t[type + 1]; q[type].pop(); q[type + 1].push(pair<long long, long long>(fin[type + 1][idx], idx)); break; } } } long long ans = 0; for (int i = 0; i < n; i++) { ans = max(ans, fin[3][i] - c[i]); } cout << ans << endl; } int main() { ios::sync_with_stdio(false); int T = 1; int ncase = 0; while (T--) { solve(++ncase); } }
2
#include <bits/stdc++.h> using namespace std; void solve() { long long int x, y; cin >> x >> y; if (x == 1) { if (y <= 1) { cout << "yes" << endl; return; } else { cout << "no" << endl; return; } } else if (x == 2 || x == 3) { if (y <= 3) { cout << "yes" << endl; return; } else { cout << "no" << endl; return; } } else if (x > 3) { cout << "yes" << endl; return; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int q; cin >> q; while (q--) { solve(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int GCD(int a, int b) { if (!a) return b; return GCD(b % a, a); } void init() {} vector<int> a, b; long long dp[44][44][44], A[44][44], ret[44][44]; int n; void printMat(long long A[44][44]) { for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { cout << A[i][j] << " "; } cout << "\n"; } cout << "\n"; } void matMul(long long A[44][44], long long B[44][44], long long target[44][44]) { long long C[44][44]; for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { C[i][j] = (1LL << 50); for (int k = 0; k <= n; k++) { C[i][j] = min(C[i][j], A[i][k] + B[k][j]); } } } for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { target[i][j] = C[i][j]; } } } long long solve(int at, int curB, int toB) { if (curB < 0 || curB > n) return (1LL << 50); long long &ret = dp[at][curB][toB]; if (ret != -1) return ret; if (at == n) { if (curB == toB) return 0; return (1LL << 50); } ret = (1LL << 50); ret = a[at % n] + solve(at + 1, curB + 1, toB); ret = min(ret, b[at % n] + solve(at + 1, curB - 1, toB)); return ret; } int main() { clock_t startTime = clock(); init(); int m; cin >> n >> m; for (int i = 0; i < n; i++) { int p; cin >> p; a.push_back(p); } for (int i = 0; i < n; i++) { int p; cin >> p; b.push_back(p); } memset(dp, -1, sizeof dp); for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { A[i][j] = solve(0, i, j); ret[i][j] = (1LL << 50); } } ret[0][0] = 0; while (m > 0) { if (m & 1) { matMul(ret, A, ret); } matMul(A, A, A); m >>= 1; } cout << ret[0][0] << "\n"; clock_t endTime = clock(); cerr << "\nTime: " << double(endTime - startTime) / CLOCKS_PER_SEC << " seconds" << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, m; vector<vector<int> > G; vector<bool> mark; double dfs(int u, int p = -1) { mark[u] = false; double res = 0; int cnt = 0; for (int i = 0; i < G[u].size(); ++i) { int v = G[u][i]; if (mark[v] || v == p) continue; cnt++; res += dfs(v, u); } return (cnt ? res / cnt + 1 : 0); } int main() { cin >> n; m = n - 1; G.resize(n + 1); mark.assign(n + 1, false); for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } cout << setprecision(7) << fixed << dfs(1); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { char s[100010]; cin >> s; char sub[100010]; int k = 0; int count = 0; for (int i = 'z'; i >= 'a'; i--) { for (int j = count; s[j] != '\0'; j++) { if (s[j] == i) { sub[k++] = i; count = j; } } } sub[k] = '\0'; cout << sub << endl; return 0; }
1