func_code_string
stringlengths 59
71.4k
|
---|
#include <bits/stdc++.h> using namespace std; const int OO = (int)1e9 + 7; const int mod = (int)1e9 + 7; const int MAX = (int)1e5 + 5; int main(void) { ios_base::sync_with_stdio(false); int n; while (cin >> n) { vector<pair<int, int> > h; for (int i = 0; i < n; i++) { int cur; cin >> cur; h.push_back(make_pair(cur, i)); } sort(h.begin(), h.end()); set<pair<int, int> > s; for (int i = 0; i < n; i++) { int l = min(i, h[i].second), r = max(i, h[i].second); auto it = s.lower_bound(make_pair(l, -1)); if (it == s.end()) { s.insert(make_pair(r, l)); } else { int cl = it->second, cr = it->first; if (r > cl) { s.erase(it); s.insert(make_pair(max(cr, r), min(cl, l))); } else { s.insert(make_pair(r, l)); } } } int seg = s.size(), tot = 0; for (auto it = s.begin(); it != s.end(); it++) { tot += it->first - it->second + 1; } cout << seg + (n - tot) << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; const long long inf = 3e18; long long t, a[maxn], b[maxn], x[maxn]; long long l[maxn], r[maxn]; int n; int main() { scanf( %d%lld , &n, &t); for (int i = 1; i <= n; i++) scanf( %lld , a + i); for (int i = 1; i <= n; i++) scanf( %lld , x + i); for (int i = 1; i <= n; i++) { if (i < n && x[i] > x[i + 1]) { puts( No ); return 0; } l[i] = a[i] + t; r[i] = inf; if (x[i] < i) { puts( No ); return 0; } } int now = 1; for (int i = 1; i < n; i++) { for (now = max(now, i); now < x[i]; now++) { l[now] = max(l[now], a[now + 1] + t); } if (now != n) r[now] = min(r[now], a[now + 1] + t - 1); } for (int i = 1; i <= n; i++) { if (l[i] > r[i]) { puts( No ); return 0; } b[i] = min(r[i], max(b[i - 1] + 1, l[i])); if (b[i] <= b[i - 1] || b[i - 1] + 1 > r[i]) { puts( No ); return 0; } } puts( Yes ); for (int i = 1; i <= n; i++) printf( %lld , b[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; int main(int argc, char *argv[]) { long long n, k, a[55]; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + 1 + n); if (k > n) cout << -1 << endl; else { cout << a[n + 1 - k] << << a[n + 1 - k] << endl; } return EXIT_SUCCESS; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 10000; vector<int> nei[maxn]; bool mark[maxn]; vector<int> nei2[maxn]; bool markk[maxn]; int n, m, comp = 0, comp2 = 0; void dfs(int v) { mark[v] = true; for (int i = 0; i < nei[v].size(); i++) { int u = nei[v][i]; if (!mark[u]) dfs(u); } return; } bool dfs_all() { for (int d = 0; d < n * m; d++) { if (!mark[d]) { dfs(d); comp++; } } if (comp == 1) return true; else return false; } void dfs2(int v) { markk[v] = true; for (int p = 0; p < nei2[v].size(); p++) { int u = nei2[v][p]; if (!markk[u]) { dfs2(u); } } return; } bool dfs2_all() { for (int j = 0; j < n * m; j++) { if (!markk[j]) { dfs2(j); comp2++; } } if (comp2 == 1) return true; else return false; } int main() { string hor, ver; cin >> n >> m; cin >> hor >> ver; for (int j = 0; j < n; j++) { if (hor[j] == > ) { for (int p = j * m; p < j * m + m - 1; p++) { nei[p].push_back(p + 1); nei2[p + 1].push_back(p); } } else { for (int p = j * m + m - 1; p > j * m; p--) { nei[p].push_back(p - 1); nei2[p - 1].push_back(p); } } } for (int j = 0; j < m; j++) { if (ver[j] == v ) { for (int p = j; p < (n - 1) * m + j; p += m) { nei[p].push_back(p + m); nei2[p + m].push_back(p); } } else { for (int p = (n - 1) * m + j; p > j; p -= m) { nei[p].push_back(p - m); nei2[p - m].push_back(p); } } } if (dfs_all() && dfs2_all()) cout << YES << endl; else cout << NO << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; long long int binpow(long long int a, long long int b) { long long int res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } void solve() { long long int n, k; cin >> n >> k; vector<long long int> a(n); for (long long int i = 0; i < a.size(); i++) { cin >> a[i]; } sort((a).begin(), (a).end()); long long int ans = a[n - 1]; for (long long int i = n - 2; i >= 0; i--) { if (a[i] == 0) continue; if (k > 0) { ans += a[i]; k--; } else { break; } } cout << ans << n ; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long int t = 1; cin >> t; while (t--) { solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; long long pre[2010000]; int n, i, m, j, vis[2010000], la[2010000], lr[2010000], s[2010000], q[2010000], tr[2010000], a[2010000], tim, jid; inline int gi() { int s = 0; char c = getchar(); while (c < 0 || c > 9 ) c = getchar(); while (c >= 0 && c <= 9 ) s = s * 10 + c - 0 , c = getchar(); return s; } void Work(long long x) { int j = 1; for (i = 1; i <= n + n; i++) { while (j < min(i, n) + n && pre[j] - pre[i - 1] <= x) ++j; la[i] = j - 1; } ++tim; int ans = n + 1; for (i = 1; i <= la[1]; i++) { int h = 0; if (i > n) i -= n, h = 1; int t = 0, sum = 0, ed = i + n - 1; for (j = i; j <= ed; j = la[j] + 1) { if (vis[j] == tim) { sum += s[j]; if (lr[j] < ed) sum++, j = lr[j] + 1; q[++t] = j, tr[t] = sum; break; } else q[++t] = j, tr[t] = ++sum; } for (j = t; j >= 1; j--) vis[q[j]] = tim, lr[q[j]] = la[q[t]], s[q[j]] = tr[t] - tr[j - 1]; ans = min(ans, sum); if (h) i += n; } i = la[1] + 1; if (i <= n) { int ed = i + n - 1, sum = 0; for (j = i; j <= ed; j = la[j] + 1) ++sum; ans = min(ans, sum); } printf( %d n , ans); } int main() { scanf( %d%d , &n, &m); for (i = 1; i <= n; i++) a[i] = a[i + n] = gi(); for (i = 1; i <= n + n; i++) pre[i] = pre[i - 1] + a[i]; while (m--) { long long x; scanf( %I64d , &x); Work(x); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 2005; long long dp[N], a[N]; int n, k, ans; bool check(long long x) { for (int i = 1; i <= n; i++) { dp[i] = i - 1; for (int j = 1; j < i; j++) { if (abs(a[i] - a[j]) <= (i - j) * x) { dp[i] = min(dp[i], dp[j] + (i - j - 1)); } } if (dp[i] + n - i <= k) return 1; } return 0; } int main() { scanf( %d%d , &n, &k); long long L = 0, R = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; if (i != 1) R = max((long long)R, abs(a[i] - a[i - 1])); } while (L <= R) { long long mid = (L + R) >> 1; if (check(mid)) { ans = mid; R = mid - 1; } else L = mid + 1; } cout << ans; } |
#include <bits/stdc++.h> using namespace std; int N, V, M; int Size[310], Final[310], T[310]; vector<int> G[310]; vector<pair<pair<int, int>, int> > ANS; int source, sink, flow; void FindSource(int node) { if (Size[node] > Final[node]) { source = node; return; } for (vector<int>::iterator it = G[node].begin(); it != G[node].end(); ++it) if (T[*it] == 0 && *it != sink) { T[*it] = node; FindSource(*it); } } void Transfer(int source, int sink, int flow) { Size[source] -= flow; Size[sink] += flow; ANS.push_back(make_pair(make_pair(source, sink), flow)); } void Push(int node) { int next = T[node]; if (next == sink) { Transfer(node, sink, flow); return; } int add = 0; if (Size[next] < flow) { add = flow - Size[next]; Transfer(node, next, add); } Push(next); Transfer(node, next, flow - add); } void Fill(int node) { sink = node; while (Size[node] < Final[node]) { source = 0; memset(T, 0, sizeof(T)); FindSource(node); if (source == 0) break; flow = min(Size[source] - Final[source], Final[sink] - Size[sink]); Push(source); } } int main() { cin >> N >> V >> M; for (int i = 1; i <= N; i++) cin >> Size[i]; for (int i = 1; i <= N; i++) cin >> Final[i]; for (int i = 1; i <= M; i++) { int a, b; cin >> a >> b; G[a].push_back(b); G[b].push_back(a); } for (int i = 1; i <= N; i++) if (Size[i] < Final[i]) Fill(i); bool OK = 1; for (int i = 1; i <= N; i++) if (Size[i] != Final[i]) OK = 0; if (OK == 0) cout << NO << n ; else { cout << ANS.size() << n ; for (vector<pair<pair<int, int>, int> >::iterator it = ANS.begin(); it != ANS.end(); ++it) cout << (it->first).first << << (it->first).second << << (it->second) << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int flag = 0, ans = 0; for (int i = 0; i < m; i++) { int k; cin >> k; set<int> check; flag = 1; for (int j = 0; j < k; j++) { int t; cin >> t; if (check.count(-t)) { flag = 0; } check.insert(t); } if (flag) ans = 1; } if (ans) { cout << YES << endl; } else { cout << NO << endl; } } |
#include <bits/stdc++.h> using namespace std; template <class T> inline T Abs(const T& x) { return x < 0 ? -x : x; } template <class T> inline T Sqr(const T& x) { return x * x; } const int maxn = 110; const int maxk = 210; double a[maxn][maxn][maxk]; int n, k; int Get(int l, int r, int pos) { if (pos < l || pos > r) { return pos; } else { return l + r - pos; } } int main() { ios_base::sync_with_stdio(false); cin >> n >> k; vector<int> p(n); for (int i = 0; i < n; ++i) { cin >> p[i]; } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { a[i][j][0] = (i < j ? 1.0 : 0.0); } } double cnt = 0; for (int i = 0; i < n; ++i) { for (int j = i; j < n; ++j) { cnt += 1.0; } } for (int iter = 1; iter <= k; ++iter) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { a[i][j][iter] = 0; for (int l = 0; l < n; ++l) { for (int r = l; r < n; ++r) { int i1 = Get(l, r, i); int j1 = Get(l, r, j); a[i][j][iter] += 1 / cnt * a[i1][j1][iter - 1]; } } } } } double ans = 0.0; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (p[i] < p[j]) { ans += a[j][i][k]; } else { ans += a[i][j][k]; } } } printf( %.12lf n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; int N, a[100010], b[100010], posA[100010]; multiset<int> S; int get(int x) { multiset<int>::iterator it = S.lower_bound(x), jt = it; return min(abs(*it - x), abs(*--jt - x)); } int main() { scanf( %d , &N); for (int i = 1; i <= N; i++) scanf( %d , &a[i]), posA[a[i]] = i; for (int i = 1; i <= N; i++) scanf( %d , &b[i]), S.insert(i - posA[b[i]]); S.insert(-1e9); for (int x = 1; x <= N; x++) { printf( %d n , get(x - 1)); S.erase(S.find(x - posA[b[x]])); S.insert(x - posA[b[x]] + N); } } |
#include <bits/stdc++.h> using namespace std; double safe_sqrt(double x) { return sqrt(max((double)0.0, x)); } long long GI(long long& x) { return scanf( %lld , &x); } const long long MOD = 1e9 + 7; const long long MN = 1000111; long long gt[MN]; vector<long long> h[MN]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout << (fixed) << setprecision(9); const long long MOD = 1e9 + 7; gt[0] = 1; for (long long i = (1), _b = (MN - 1); i <= _b; ++i) gt[i] = gt[i - 1] * i % MOD; long long n, m; while (GI(n) == 1 && GI(m) == 1) { for (long long i = (1), _b = (m); i <= _b; ++i) h[i].clear(); for (long long i = (1), _b = (n); i <= _b; ++i) { long long k; GI(k); while (k--) { long long u; GI(u); h[u].push_back(i); } } sort(h + 1, h + m + 1); long long i = 1; long long res = 1; while (i <= m) { long long j = i; while (j < m && h[j + 1] == h[i]) j++; res = res * gt[j - i + 1] % MOD; i = j + 1; } cout << res << endl; } } |
#include <bits/stdc++.h> using namespace std; struct node { int x; int i; }; node a[300010]; int b[300010]; map<int, int> mp; bool cmp(node p, node q) { return (p.x < q.x); } int main() { int x, y, i, j, n, k; cin >> n; for (i = 0; i < n; i++) { cin >> a[i].x; mp[a[i].x]++; a[i].i = i; } sort(a, a + n, cmp); for (i = 0, j = 1; i < n; i++) { if (i == 0) b[a[i].i] = a[i].x; else if ((a[i].x == a[i - 1].x)) { j = max(j, a[i].x + 1); while (1) { if (j <= a[i].x || mp[j] > 0) { j++; continue; } break; } b[a[i].i] = j; mp[j]++; } else { b[a[i].i] = a[i].x; } } for (i = 0; i < n; i++) cout << b[i] << ; return 0; } |
#include<bits/stdc++.h> using namespace std; #define forg(i,x) for(register int i=fir[x];i;i=nxt[i]) #define uu unsigned #define scanf a14=scanf #define rint register int #define fre(x) freopen(#x .in , r ,stdin),freopen(#x .out , w ,stdout) typedef long long ll; typedef uu long long ull; typedef pair<int,int>pii; int a14; inline int rd(int l,int r){return rand()%(r-l+1)+l;} const int mxn=1e5+3,sq=400,qs=320; int n,b[mxn],lp[mxn],rp[mxn],kn,be[mxn],po[mxn],qn; int ju[mxn/sq+2][mxn],ad[mxn/sq+2][mxn/qs+1]; int TG; struct shu{ int a[mxn],tg[mxn]; void add(int x){for(;x<=n;x+=x&-x){if(tg[x]!=TG)tg[x]=TG,a[x]=0;++a[x];}} int ask(int x){ int su=0,p=0; for(int i=1<<16;i;i>>=1){ if(p+i>=n)continue; p+=i;if(tg[p]!=TG)tg[p]=TG,a[p]=0; if(p+a[p]+su>=x)p-=i;else su+=a[p]; } return p+1; } void gb(int t){for(int i=1;i<=n;++i){if(tg[i]!=TG)a[i]=0;ju[t][i]=ju[t][i-(i&-i)]+a[i];}for(int i=1;i<=n;++i)ju[t][i]+=i;} }ar; void add2(int t,int x,int y){ int k=(x-1)/qs; for(int i=x;i<=(k+1)*qs;++i)ju[t][i]+=y; for(++k;k*qs+1<=n;++k)ad[t][k]+=y; } int main(){ //cout<<(sizeof(ju)>>20)<<endl; scanf( %d ,&n);for(int i=1;i<=n;++i)scanf( %d ,b+i),b[i]=i-b[i];scanf( %d ,&qn); for(int l=1,r;r!=n;l=r+1){r=min(l+sq-1,n);lp[++kn]=l,rp[kn]=r;for(int i=l;i<=r;++i)be[i]=kn;} for(int t=1;t<=kn;++t){ ++TG; for(int i=lp[t];i<=rp[t];++i)po[i]=ar.ask(b[i]),ar.add(po[i]); ar.gb(t); } while(qn--){ int o,x,y;scanf( %d%d ,&o,&x);if(o==1){ scanf( %d ,&y);b[x]=x-y;int k=be[x],ls; ++TG;for(int i=lp[k];i<=rp[k];++i){ if(i<x)ar.add(po[i]);else{ ls=po[i],po[i]=ar.ask(b[i]),ar.add(po[i]); if(ls==po[i])continue; if(ls==po[i]-1){--ju[k][ls];continue;} if(ls==po[i]+1){++ju[k][po[i]];continue;} assert(i==x); add2(k,ls,-1),add2(k,po[i],1); } } }else{ int t=be[x],re=b[x];for(int i=x+1;i<=rp[t];++i)re+=b[i]<=re; for(++t;t<=kn;++t)re=ju[t][re]+ad[t][(re-1)/qs]; printf( %d n ,re); } } return 0; } |
#include <bits/stdc++.h> using namespace std; int ans[105], a[105], sum[105]; int main() { int n, b; cin >> n >> b; for (int i = 0; i < n; i++) scanf( %d , a + i); int t = 0, nn = 0; for (int i = 0; i < n - 1; i++) { if (a[i] % 2) t++; else t--; if (t == 0) { ans[nn] = abs(a[i] - a[i + 1]); nn++; } } sort(ans, ans + nn); sum[0] = 0; for (int i = 0; i < nn; i++) sum[i + 1] = ans[i] + sum[i]; t = nn; for (;; t--) { if (sum[t] <= b) { cout << t << endl; return 0; } } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int t, n, sumaP, sumaI, ultimo, primero; cin >> t; while (t--) { cin >> n; if (n % 4 != 0) cout << NO << endl; else { sumaP = (n / 2) * (n / 2 + 1); sumaI = (n / 2 - 1) * (n / 2 - 1); ultimo = sumaP - sumaI; cout << YES << endl; primero = 2; for (int i = 0; i < n / 2; i++) { cout << primero << ; primero += 2; } primero = 1; for (int i = n / 2; i < n - 1; i++) { cout << primero << ; primero += 2; } cout << ultimo << endl; } } return 0; } |
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << setprecision(20) << fixed; long long t; cin >> t; while (t--) { long long n; cin >> n; long long a[n]; vector<long long> b, c; long long s = 0, z = 0; for (long long i = 0; i < n; i++) { cin >> a[i]; if (a[i] > 0) { b.push_back(a[i]); s += a[i]; } else if (a[i] == 0) z++; else c.push_back(a[i]); } long long f = 0; for (long long i = 0; i < c.size(); i++) { if (s + c[i] != 0) { s += c[i]; b.push_back(c[i]); } else { f = 1; b.push_back(c[i]); s += c[i]; } } if (f == 1) reverse(b.begin(), b.end()); for (long long i = 0; i < z; i++) b.push_back(0); if (s == 0) cout << NO << endl; else { cout << YES << endl; for (long long i = 0; i < n; i++) cout << b[i] << ; cout << endl; } } return 0; } |
#include <bits/stdc++.h> struct Side { int x, y; Side(int x, int y) : x(x), y(y) {} }; struct Part { int id, z; Part() {} Part(int id, int z) : id(id), z(z) {} }; struct Answer { int a, b; Answer(){}; Answer(int a, int b) : a(a), b(b) {} }; bool operator<(const Side &left, const Side &right) { if (left.x != right.x) { return left.x < right.x; } return left.y < right.y; } int min(int x, int y, int z) { return std::min(x, std::min(y, z)); } void check(int id, int x, int y, int z, std::map<Side, Part> &sides, Answer &ans, int &bestV) { if (x > y) { check(id, y, x, z, sides, ans, bestV); return; } auto it = sides.find(Side(x, y)); if (it == sides.end()) { return; } z += (*it).second.z; int minV = min(x, y, z); if (minV > bestV) { bestV = minV; ans.a = (*it).second.id; ans.b = id; } } void add(int id, int x, int y, int z, std::map<Side, Part> &sides) { if (x > y) { add(id, y, x, z, sides); return; } auto it = sides.find(Side(x, y)); if (it != sides.end()) { if ((*it).second.z >= z) { return; } } sides[Side(x, y)] = Part(id, z); } int main() { std::map<Side, Part> sides; int n; scanf( %d , &n); Answer ans; int bestV = 0; for (int i = 1; i <= n; i++) { int x, y, z; scanf( %d %d %d , &x, &y, &z); int minV = min(x, y, z); if (minV > bestV) { bestV = minV; ans = Answer(i, -1); } check(i, x, y, z, sides, ans, bestV); check(i, x, z, y, sides, ans, bestV); check(i, y, z, x, sides, ans, bestV); add(i, x, y, z, sides); add(i, x, z, y, sides); add(i, y, z, x, sides); } if (ans.b == -1) { printf( 1 n%d , ans.a); } else { printf( 2 n%d %d , ans.a, ans.b); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int N, H; cin >> N >> H; vector<int> A(N); for (int i = 0; i < N; i++) cin >> A[i]; vector<long long> b; int ans = 0; for (int i = 0; i < N; i++) { b.push_back(A[i]); sort(b.begin(), b.end()); long long sum = 0; for (int i = b.size() - 1; i >= 0; i -= 2) { sum += b[i]; } if (sum <= H) ans++; else break; } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; bool MySort(const pair<int, int>& A, const pair<int, int>& B) { return A.first > B.first; } int main() { int T; cin >> T; while (T--) { long long X, Y, K; cin >> X >> Y >> K; long long Ans = 0; long long All = Y * K + K; All--; if (All % (X - 1) == 0) { Ans += (All / (X - 1)); } else { Ans += (All / (X - 1)) + 1; } Ans += K; cout << Ans << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; while (k--) { if (n % 10 == 0) n = n / 10; else n--; } cout << n; return 0; } |
#include <bits/stdc++.h> using namespace std; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n; cin >> n; long long arr[2 * n]; for (long long i = 0; i < 2 * n; i++) cin >> arr[i]; long long m = 0; for (long long i = 0; i < 2 * n; i++) { if (arr[i] != arr[i + 1]) { for (long long j = i + 1; j < 2 * n; j++) { if (arr[i] == arr[j]) { for (long long k = j; k > i + 1; k--) { swap(arr[k], arr[k - 1]); m++; } break; } } i++; } else i++; } cout << m; } |
#include <bits/stdc++.h> using ll = long long; using namespace std; const ll N = 1e5 + 1, mod = 1e9 + 7; struct point { ll x, y; } p[N]; ll n; bool line[N]; signed main() { ios_base::sync_with_stdio(0), cin.tie(0); cin >> n; for (ll i = 1; i <= n; ++i) { cin >> p[i].x >> p[i].y; } if (n <= 4) { return cout << YES , 0; } for (ll i = 1; i <= 3; ++i) { for (ll j = i + 1; j <= 3; ++j) { memset(line, 0, sizeof line); ll a = p[i].y - p[j].y, b = p[j].x - p[i].x; ll c = a * p[i].x + b * p[i].y; for (ll k = 1; k <= n; ++k) { if (a * p[k].x + b * p[k].y != c) { line[k] = 1; } } ll cnt0 = 0; point p1 = {INT_MAX, 0}, p2 = {INT_MAX, 0}; for (ll k = 1; k <= n && cnt0 < 2; ++k) { if (line[k]) { if (!cnt0) { p1 = p[k]; } else { p2 = p[k]; } cnt0++; } } if (p1.x == INT_MAX || p2.x == INT_MAX) { return cout << YES , 0; } a = p1.y - p2.y, b = p2.x - p1.x; c = a * p1.x + b * p1.y; bool yes = 1; for (ll k = 1; k <= n; ++k) { if (line[k] && a * p[k].x + b * p[k].y != c) { yes = 0; } } if (yes) { return cout << YES , 0; } } } cout << NO ; } |
#include <bits/stdc++.h> using namespace std; long long gcd(int a, int b) { if (!b) return a; return gcd(b, a % b); } bool isprime(long long n) { if (n == 2) { return true; } if (n % 2 == 0) { return false; } for (long long i = 3; i <= sqrt(n); i += 2) { if (n % i == 0) { return false; } } return true; } long long a[1005][2]; int main() { long long n, m; cin >> n >> m; long long flag[m]; memset(flag, 0, sizeof(flag)); for (int(i) = (0); (i) < (m); (i)++) { long long k; cin >> k; map<int, int> m; for (int(j) = (0); (j) < (k); (j)++) { long long x; cin >> x; m[x]++; if (m.find(-x) != m.end()) flag[i] = 1; } } for (int(i) = (0); (i) < (m); (i)++) { if (flag[i] == 0) { cout << YES ; return 0; } } cout << NO ; } |
#include <bits/stdc++.h> using namespace std; struct Q { int r, c, id; } q[100001]; int m, n; bool cmph(Q a, Q b) { if (a.r != b.r) return a.r < b.r; return a.c < b.c; } bool cmps(Q a, Q b) { if (a.c != b.c) return a.c < b.c; return a.r < b.r; } bool cmpp(Q a, Q b) { if (a.r + a.c != b.r + b.c) return a.r + a.c < b.r + b.c; return a.r < b.r; } bool cmpn(Q a, Q b) { if (a.r - a.c != b.r - b.c) return a.r - a.c < b.r - b.c; return a.r < b.r; } int cnt[100001]; int main(int argc, char *argv[]) { scanf( %d%d , &m, &n); do { memset(cnt, 0, sizeof(cnt)); } while (0); for (int i = 0; i < n; i++) { scanf( %d%d , &(q[i].r), &(q[i].c)); q[i].id = i; } sort(q, q + n, cmph); for (int i = 1; i < n; i++) { if (q[i - 1].r == q[i].r) { cnt[q[i - 1].id]++; cnt[q[i].id]++; } } sort(q, q + n, cmps); for (int i = 1; i < n; i++) { if (q[i - 1].c == q[i].c) { cnt[q[i - 1].id]++; cnt[q[i].id]++; } } sort(q, q + n, cmpp); for (int i = 1; i < n; i++) { if (q[i - 1].r + q[i - 1].c == q[i].r + q[i].c) { cnt[q[i - 1].id]++; cnt[q[i].id]++; } } sort(q, q + n, cmpn); for (int i = 1; i < n; i++) { if (q[i - 1].r - q[i - 1].c == q[i].r - q[i].c) { cnt[q[i - 1].id]++; cnt[q[i].id]++; } } int cc[9]; do { memset(cc, 0, sizeof(cc)); } while (0); for (int i = 0; i < n; i++) cc[cnt[i]]++; for (int i = 0; i < 9; i++) { printf( %d , cc[i]); } return EXIT_SUCCESS; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 1005; int n; vector<int> point[2]; vector<pair<long long, int> > area[2], vec[4]; long long query(int type, int i, int j, int k) { printf( %d %d %d %d n , type, i, j, k); fflush(stdout); long long res; scanf( %lld , &res); return res; } int main() { scanf( %d , &n); for (int i = 3; i <= n; i++) { if (query(2, 1, 2, i) < 0) point[0].emplace_back(i); else point[1].emplace_back(i); } for (int i = 0; i < point[0].size(); i++) area[0].emplace_back(make_pair(query(1, 1, 2, point[0][i]), point[0][i])); for (int i = 0; i < point[1].size(); i++) area[1].emplace_back(make_pair(query(1, 1, 2, point[1][i]), point[1][i])); pair<long long, int> L, R; for (int i = 0; i < area[0].size(); i++) L = max(L, area[0][i]); for (int i = 0; i < area[1].size(); i++) R = max(R, area[1][i]); for (int i = 0; i < point[0].size(); i++) { if (point[0][i] == L.second) continue; if (query(2, 1, L.second, point[0][i]) < 0) vec[0].push_back(area[0][i]); else vec[1].push_back(area[0][i]); } for (int i = 0; i < point[1].size(); i++) { if (point[1][i] == R.second) continue; if (query(2, 1, R.second, point[1][i]) < 0) vec[2].emplace_back(area[1][i]); else vec[3].emplace_back(area[1][i]); } for (int i = 0; i <= 3; i++) sort(vec[i].begin(), vec[i].end()); printf( 0 1 ); for (int i = 0; i < vec[0].size(); i++) printf( %d , vec[0][i].second); if (L.second) printf( %d , L.second); for (int i = vec[1].size() - 1; i >= 0; i--) printf( %d , vec[1][i].second); printf( 2 ); for (int i = 0; i < vec[2].size(); i++) printf( %d , vec[2][i].second); if (R.second) printf( %d , R.second); for (int i = vec[3].size() - 1; i >= 0; i--) printf( %d , vec[3][i].second); printf( n ); fflush(stdout); } |
#include <bits/stdc++.h> int a[200001]; int main() { int n, i, ans = 0; scanf( %d , &n); for (i = 0; i < n; i++) { int x; scanf( %d , &x); if (a[100000 + x] == 0 && x != 0) { ans++; a[100000 + x] = 1; } } printf( %d n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m; int v[1010][1010]; int dpa[1010][1010]; int dpreva[1010][1010]; int dpb[1010][1010]; int dprevb[1010][1010]; int solvea(int currx, int curry) { if (currx == n && curry == m) return dpa[currx][curry] = v[currx][curry]; int& ret = dpa[currx][curry]; if (ret != -1) return ret; if (currx + 1 <= n) { ret = max(ret, solvea(currx + 1, curry) + v[currx][curry]); } if (curry + 1 <= m) { ret = max(ret, solvea(currx, curry + 1) + v[currx][curry]); } return ret; } int solvereva(int currx, int curry) { if (currx == 1 && curry == 1) return dpreva[currx][curry] = v[currx][curry]; int& ret = dpreva[currx][curry]; if (ret != -1) return ret; if (currx - 1 >= 1) { ret = max(ret, solvereva(currx - 1, curry) + v[currx][curry]); } if (curry - 1 >= 1) { ret = max(ret, solvereva(currx, curry - 1) + v[currx][curry]); } return ret; } int solveb(int currx, int curry) { if (currx == 1 && curry == m) return dpb[currx][curry] = v[currx][curry]; int& ret = dpb[currx][curry]; if (ret != -1) return ret; if (currx - 1 >= 1) { ret = max(ret, solveb(currx - 1, curry) + v[currx][curry]); } if (curry + 1 <= m) { ret = max(ret, solveb(currx, curry + 1) + v[currx][curry]); } return ret; } int solverevb(int currx, int curry) { if (currx == n && curry == 1) return dprevb[currx][curry] = v[currx][curry]; int& ret = dprevb[currx][curry]; if (ret != -1) return ret; if (currx + 1 <= n) { ret = max(ret, solverevb(currx + 1, curry) + v[currx][curry]); } if (curry - 1 >= 1) { ret = max(ret, solverevb(currx, curry - 1) + v[currx][curry]); } return ret; } int main(void) { ios ::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { cin >> v[i][j]; } } memset(dpa, -1, sizeof dpa); memset(dpreva, -1, sizeof dpreva); memset(dpb, -1, sizeof dpb); memset(dprevb, -1, sizeof dprevb); solvea(1, 1); solvereva(n, m); solveb(n, 1); solverevb(1, m); int res = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { int dist = 0; int dist2 = 0; if (j - 1 >= 1 && j + 1 <= m && i - 1 >= 1 && i + 1 <= n) { dist = dprevb[i][j - 1] + dpb[i][j + 1] + dpreva[i - 1][j] + dpa[i + 1][j]; dist2 = dprevb[i + 1][j] + dpb[i - 1][j] + dpreva[i][j - 1] + dpa[i][j + 1]; } res = max(res, dist); res = max(res, dist2); } } cout << res << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n, m, k, l; cin >> n >> m >> k >> l; if (((k + l + m - 1) / m) * m > n) { cout << -1; return 0; } cout << (k + l + m - 1) / m; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 200005; const int maxm = 300005; const int maxq = 500005; const int inf = 0x3f3f3f3f; int n, m, Q, a[maxn], fa[maxn], x_[maxm], y_[maxm], u_[maxq], v_[maxq]; bool vis[maxm]; set<int> s[maxn]; set<int>::iterator it, itl, itr; struct oper { int op, u; } q[maxq]; inline int find(int x) { while (x != fa[x]) x = fa[x]; return x; } inline void merge(int i, int x, int y) { int u = find(x), v = find(y); if (u == v) return; if (s[u].size() > s[v].size()) swap(u, v); u_[i] = u, v_[i] = v, fa[u] = v; for (auto x : s[u]) s[v].insert(x); } int main() { scanf( %d%d%d , &n, &m, &Q); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); for (int i = 1; i <= m; i++) scanf( %d%d , &x_[i], &y_[i]); for (int i = 1; i <= Q; i++) { scanf( %d%d , &q[i].op, &q[i].u); if (q[i].op == 2) vis[q[i].u] = 1; } for (int i = 1; i <= n; i++) fa[i] = i, s[i].insert(a[i]); for (int i = 1; i <= m; i++) if (!vis[i]) merge(0, x_[i], y_[i]); for (int i = Q; i; i--) if (q[i].op == 2) merge(i, x_[q[i].u], y_[q[i].u]); for (int i = 1; i <= Q; i++) if (q[i].op == 1) { int u = find(q[i].u); if (s[u].empty()) { puts( 0 ); continue; } printf( %d n , *s[u].rbegin()); s[u].erase(*s[u].rbegin()); } else if (q[i].op == 2) { if (!u_[i]) continue; int u = u_[i], v = v_[i]; fa[u] = u; for (it = s[u].begin(); it != s[u].end();) { itr = s[v].find(*it); if (itr == s[v].end()) itl = it, it++, s[u].erase(itl); else s[v].erase(itr), it++; } } return 0; return 0; } |
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; const int maxn = 2e5 + 10; const int inf = 0x3f3f3f3f; const double eps = 1e-6; mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); int n; int l[maxn], r[maxn], t[maxn]; struct node { int id; bool operator<(const node &s) const { if (r[id] != r[s.id]) return r[id] < r[s.id]; else if (l[id] != l[s.id]) return l[id] < l[s.id]; else return id < s.id; } }; bool vis[maxn]; struct e { int x, op, id; bool operator<(const e &s) const { if (x != s.x) return x < s.x; else if (op != s.op) return op > s.op; else return id < s.id; } }; multiset<node> s[2]; set<e> ss; int main() { std::ios::sync_with_stdio(false), cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> l[i] >> r[i] >> t[i]; t[i]--; ss.insert((e){l[i], 1, i}); ss.insert((e){r[i], -1, i}); } int ans = n; for (auto u : ss) { int op = u.op; int id = u.id; if (op == 1) { s[t[id]].insert((node){id}); continue; } if (vis[id]) continue; vis[id] = 1; s[t[id]].erase((node){id}); auto it = s[1 - t[id]].begin(); if (it == s[1 - t[id]].end()) continue; int l1 = l[id]; int r1 = r[id]; int l2 = l[(*it).id]; int r2 = r[(*it).id]; if (max(l1, l2) <= min(r1, r2)) { vis[(*it).id] = 1; ans--; s[1 - t[id]].erase((node){(*it).id}); } } cout << ans << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const int INF = 1000000007; const double EPS = 1e-10; const double PI = 3.14159265359; int n; int a[200005]; int cnt[200005]; int main() { int t; cin >> t; while (t--) { set<int> st; int mx = 0; memset(cnt, 0, sizeof cnt); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; st.insert(a[i]); cnt[a[i]]++; mx = max(mx, cnt[a[i]]); } if (mx > st.size()) { cout << st.size() << endl; } else if (mx == st.size()) { cout << mx - 1 << endl; } else { cout << mx << endl; } } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n == 1) { cout << yes << endl; cout << 1 1 n ; return 0; } vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; int l = 0, r = 0; while (l + 1 < n && a[l] < a[l + 1]) l++; r = l + 1; while (r < n && a[r] < a[r - 1]) r++; reverse(a.begin() + l, a.begin() + r); for (int i = 1; i < n; i++) { if (a[i] < a[i - 1]) { cout << no << endl; return 0; } } cout << yes << endl; cout << l + 1 << << r << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int n, k, a[2000005]; int val[2000005], sumVal[2000005]; int getSum(int l, int r) { if (l == 0) return sumVal[r]; else return sumVal[r] - sumVal[l - 1]; } int main() { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; val[a[i]]++; } for (int i = 1; i < 2000005; i++) { sumVal[i] = sumVal[i - 1] + val[i]; } sort(a, a + n); if (a[0] <= k + 1) { cout << a[0]; return 0; } long long ans = k; for (int d = k + 1; d <= a[0]; d++) { int res = 0; for (int i = 0; d * i <= a[n - 1]; i++) { res += getSum(d * i, d * i + k); } if (res == n) ans = d; } cout << ans; } |
#include <bits/stdc++.h> using namespace std; char ch[500005]; bool vis[200]; int n; double f[500005], g[500005]; void init() { vis[ I ] = vis[ E ] = vis[ A ] = vis[ O ] = vis[ U ] = vis[ Y ] = true; int i; n = strlen(ch + 1); f[0] = g[0] = 0.0; for (i = 1; i <= n; i++) { f[i] = f[i - 1] + 1.0 / i; g[i] = g[i - 1] + 1.0 * i / (n - i + 1); } } int main() { scanf( %s , ch + 1); init(); double ans = 0.0; int i, t; for (i = 1; i <= n; i++) { if (!vis[ch[i]]) continue; t = min(i, n - i + 1); ans += t; ans += t * (f[n - t + 1] - f[t]); ans += g[n - (n - t + 1)]; } printf( %.10f n , ans); } |
#include <bits/stdc++.h> using namespace std; string s; vector<char> v; int main() { ios_base::sync_with_stdio(false); getline(cin, s); int cnt = 0; bool mark; for (int i = 0; i < s.size(); i++) { if (s[i] == ) cnt++; else { v.push_back(s[i]); if (s[i] != ) mark = true; } if (mark == true && (i == s.size() - 1 || (s[i] == && s[i + 1] != )) && ((cnt != 1 || s[i] == ) && cnt != 2)) { mark = false; string t = ; for (int i = 0; i < v.size(); i++) if (v[i] != ) t += v[i]; v.clear(); cout << < << t << > << endl; } if (cnt == 2) { cnt = 0; string t = ; for (int i = 0; i < v.size(); i++) t += v[i]; v.clear(); mark = false; cout << < << t << > << endl; } if (cnt == 1 && s[i] == ) v.clear(); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); int n; double m, mm; cin >> n >> mm; m = mm; double *a = new double[n]; double *b = new double[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; if (a[i] == 1) { cout << -1; return 0; } } for (int i = 0; i < n; ++i) { cin >> b[i]; if (b[i] == 1) { cout << -1; return 0; } } double x = m / (b[0] - 1); m += x; for (int i = n - 1; i > 0; --i) { x = m / (a[i] - 1); m += x; x = m / (b[i] - 1); m += x; } x = m / (a[0] - 1); m += x; cout << fixed << setprecision(10) << m - mm; return 0; } |
#include <bits/stdc++.h> using namespace std; vector<int> e[3001], u; int v[3001], d[3001]; bool dfs(int x, int from) { if (v[x]++) { for (v[x] = -1; u.back() != x; u.pop_back()) v[u.back()] = -1; return true; } u.push_back(x); for (size_t i = 0; i < e[x].size(); i++) if (e[x][i] != from && dfs(e[x][i], x)) return true; u.pop_back(); return false; } int main() { int n, x, y; scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %d%d , &x, &y); e[x].push_back(y); e[y].push_back(x); } dfs(1, -1); u.clear(); for (int i = 1; i <= n; i++) if (v[i] < 0) u.push_back(i); for (size_t o = 0; o < u.size(); o++) { for (size_t i = 0; i < e[u[o]].size(); i++) { int t = e[u[o]][i]; if (~v[t]) { v[t] = -1; d[t] = d[u[o]] + 1; u.push_back(t); } } } for (int i = 1; i <= n; i++) printf( %d , d[i]); } |
#include <bits/stdc++.h> using namespace std; mt19937 rd(chrono ::steady_clock ::now().time_since_epoch().count()); long long Rand(long long l, long long h) { return l + rd() % (h - l + 1); } template <typename T> inline void read(T &x) { register char c; bool sign = 0; for (c = getchar(); !isdigit(c); c = getchar()) if (c == - ) sign = !sign; x = c - 0 ; for (c = getchar(); isdigit(c); c = getchar()) x = x * 10 + c - 0 ; if (sign) x = -x; } template <typename T> inline bool mini(T &x, T y) { if (x > y) x = y; else return 0; return 1; } template <typename T> inline bool maxi(T &x, T y) { if (x < y) x = y; else return 0; return 1; } template <typename T> inline void _abs(T &x) { if (x < 0) x = -x; return; } const int N = 1003; int a[N]; int d[2]; int main() { ios ::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; d[0] = d[1] = 0; for (int i = 1; i <= n; ++i) d[a[i]]++; bool t; if (d[0] < d[1]) t = 1; else t = 0; vector<int> res; for (int i = 1; i <= n; ++i) if (a[i] == t) res.push_back(t); if (t && (int)(res.size()) % 2) res.pop_back(); cout << res.size() << n ; for (int x : res) cout << x << ; cout << n ; } return 0; } |
#include <bits/stdc++.h> static const int MOD = 1000000007; using ll = long long; using u32 = unsigned; using u64 = unsigned long long; using namespace std; template <class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208; template <u32 M = 1000000007> struct modint { u32 val; modint() : val(0) {} template <typename T> modint(T t) { t %= (T)M; if (t < 0) t += (T)M; val = t; } modint pow(ll k) const { modint res(1), x(val); while (k) { if (k & 1) res *= x; x *= x; k >>= 1; } return res; } template <typename T> modint& operator=(T t) { t %= (T)M; if (t < 0) t += (T)M; val = t; return *this; } modint inv() const { return pow(M - 2); } modint& operator+=(modint a) { val += a.val; if (val >= M) val -= M; return *this; } modint& operator-=(modint a) { if (val < a.val) val += M - a.val; else val -= a.val; return *this; } modint& operator*=(modint a) { val = (u64)val * a.val % M; return *this; } modint& operator/=(modint a) { return (*this) *= a.inv(); } modint operator+(modint a) const { return modint(val) += a; } modint operator-(modint a) const { return modint(val) -= a; } modint operator*(modint a) const { return modint(val) *= a; } modint operator/(modint a) const { return modint(val) /= a; } modint operator-() { return modint(M - val); } bool operator==(const modint a) const { return val == a.val; } bool operator!=(const modint a) const { return val != a.val; } bool operator<(const modint a) const { return val < a.val; } }; using mint = modint<MOD>; int main() { int n, m; cin >> n >> m; vector<vector<int>> v(n, vector<int>(m, 0)); string s; for (int i = 0; i < n; ++i) { cin >> s; for (int j = 0; j < m; ++j) { v[i][j] = s[j] - a ; } } int q; cin >> q; v.resize(n + q, vector<int>(m, 0)); for (int i = 0; i < q; ++i) { cin >> s; for (int j = 0; j < m; ++j) { v[n + i][j] = s[j] - a ; } } vector<int> inv = {0, 1, 3, 2, 4}; int rank = 0; for (int c = 0; c < m; ++c) { int k = -1; for (int i = rank; i < n; ++i) { if (v[i][c]) { k = i; break; } } if (!~k) continue; swap(v[k], v[rank]); int x = inv[v[rank][c]]; for (int j = 0; j < m; ++j) v[rank][j] = (v[rank][j] * x) % 5; for (int i = 0; i < n + q; ++i) { if (i != rank && v[i][c]) { int coeff = v[i][c]; for (int j = 0; j < m; ++j) { v[i][j] -= v[rank][j] * coeff % 5; if (v[i][j] < 0) v[i][j] += 5; } } } rank++; } int ans = mint(5).pow(n - rank).val; for (int i = 0; i < q; ++i) { int ok = 1; for (int j = 0; j < m; ++j) { if (v[n + i][j]) ok = 0; } printf( %d n , ok ? ans : 0); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, x = 0, y = 0, x0 = 0, y0 = 0, k = 1, ans = 1; string s; cin >> n >> s; for (int i = 0; i < n; i++) { if (s[i] == L ) x--; if (s[i] == R ) x++; if (s[i] == D ) y--; if (s[i] == U ) y++; if (abs(x - x0) + abs(y - y0) < k) { ans++; k = 1; x0 = x; if (s[i] == L ) x0++; if (s[i] == R ) x0--; y0 = y; if (s[i] == D ) y0++; if (s[i] == U ) y0--; } else k++; } printf( %d , ans); return 0; } |
#include <bits/stdc++.h> const long long mod = (long long)1e9 + 7; using namespace std; vector<long long> parent, sz; void make_set(long long v) { parent[v] = v; sz[v] = 1; } long long find_set(long long v) { if (v == parent[v]) return v; return parent[v] = find_set(parent[v]); } void union_sets(long long a, long long b) { a = find_set(a); b = find_set(b); if (a != b) { if (a < b) swap(a, b); parent[b] = a; sz[a] += sz[b]; } } long long n; void clearall() { parent.clear(); sz.clear(); parent.resize(n + 1); sz.resize(n + 1); } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { cin >> n; vector<long long> vc(n); for (long long i = 0; i < n; i++) cin >> vc[i]; clearall(); for (long long i = 0; i <= n; i++) { make_set(i); } vector<long long> mp(n + 1, 0); for (long long i = 0; i < n; i++) { mp[vc[i]] = i; } bool foo = true; vector<long long> ocp(n, 0); vector<long long> mxmp(n, 1); multiset<long long, greater<long long>> st; for (long long i = 0; i < n; i++) st.insert(1); for (long long i = 1; i <= n; i++) { long long mx = *(st.begin()); long long ii = mp[i]; if (mxmp[ii] != mx) { foo = false; break; } auto it = st.find(mxmp[ii]); st.erase(it); if (ii > 0 and ocp[ii - 1] == 1) { union_sets(ii, ii - 1); } if (ii < n - 1 and ocp[ii + 1] == 1) { union_sets(ii, ii + 1); } ocp[ii] = 1; long long pr = find_set(ii); if (pr < n - 1 and ocp[pr + 1] == 0) { it = st.find(mxmp[pr + 1]); st.erase(it); mxmp[pr + 1] = sz[pr] + 1; st.insert(sz[pr] + 1); } } if (foo) { cout << Yes n ; } else { cout << No n ; } } return 0; } |
#include <bits/stdc++.h> using namespace std; int static_init = []() { ios_base::sync_with_stdio(false), cin.tie(0), cout << fixed; return 0; }(); constexpr int N = 2e5; int32_t n, a[N]; bool v[N]; int main() { int64_t t, ans = 0; cin >> n >> t; for (int i = 0; i < n; i++) cin >> a[i]; for (;;) { int64_t s = 0; int m = 0; for (int i = 0; i < n; i++) { if (v[i]) continue; if (s + a[i] > t) v[i] = true; else s += a[i], m++; } if (m == 0) break; ans += t / s * m; t %= s; } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool uax(T &x, T y) { return (y > x) ? x = y, true : false; } template <typename T> inline bool uin(T &x, T y) { return (y < x) ? x = y, true : false; } string to_string(char c) { return + string(1, c) + ; } string to_string(string s) { return + s + ; } string to_string(const char *s) { return to_string((string)s); } template <typename A> string to_string(A); template <typename A, typename B> string to_string(pair<A, B> p) { return ( + to_string(p.first) + : + to_string(p.second) + ) ; } template <typename A> string to_string(A v) { bool f = false; string r = { ; for (auto x : v) { if (f) r += , ; r += to_string(x); f = true; } return r += } ; } template <typename A> string to_string(vector<vector<A>> v) { string r; for (auto x : v) r += n + to_string(x); return r; } int Nerr; template <typename A> string to_string(A *p) { return to_string(vector<A>(p, p + Nerr)); } void err(istream_iterator<string>) { cerr << endl; } template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << =: << to_string(a) << ; ; err(++it, args...); } template <typename T> void kek(T ans) { cout << ans << endl; exit(0); } long long const INF = 1e18 + 42; const int MOD = 998244353; inline int add(int a, int b, int mod = MOD) { a += b; return a >= mod ? a - mod : a; } inline int sub(int a, int b, int mod = MOD) { a -= b; return a < 0 ? a + mod : a; } inline int mul(int a, int b, int mod = MOD) { return int((long long)a * b % mod); } inline int mpow(int base, long long ex, int mod = MOD) { int res = 1; for (; ex > 0; ex >>= 1) { if (ex & 1) res = mul(res, base, mod); base = mul(base, base, mod); } return res; } inline int inv(int a, int mod = MOD) { return mpow(a, mod - 2, mod); } inline int mdiv(int a, int b, int mod = MOD) { return mul(a, mpow(b, mod - 2, mod)); } inline void adds(int &a, int b, int mod = MOD) { a += b; if (a >= mod) a -= mod; } inline void subs(int &a, int b, int mod = MOD) { a -= b; if (a < 0) a += mod; } inline void muls(int &a, int b, int mod = MOD) { a = int((long long)a * b % mod); } inline void mdivs(int &a, int b, int mod = MOD) { a = mdiv(a, b, mod); } template <typename T, int K> struct Basis { array<T, K> bas; int n, rank; Basis() : bas(), n(), rank() {} bool add(T x) { ++n; for (int i = K - 1; i >= 0; --i) { if (x >> i & 1) { if (bas[i]) { x ^= bas[i]; } else { bas[i] = x, ++rank; return true; } } } return false; } int ways(T x) { for (int i = K - 1; i >= 0; --i) { if (x >> i & 1) x ^= bas[i]; } return x == 0 ? mpow(2, n - rank) : 0; } Basis operator+(Basis o) const { for (int i = 0; i < K; ++i) if (bas[i]) o.add(bas[i]); return o; } }; int32_t main() { cin.tie(nullptr)->sync_with_stdio(false); const int M = 35, F = 13, S = M - F; int n, m; cin >> n >> m; Basis<long long, M> basis; for (int i = 0; i < n; ++i) { long long x; cin >> x; basis.add(x); } 42; ; set<long long> s, t; for (int nask = 0; nask < (1 << F); ++nask) { long long val = 0; for (int i = 0; i < F; ++i) if (nask >> i & 1) { val ^= basis.bas[i]; } s.insert(val); } 42; ; for (int nask = 0; nask < (1 << S); ++nask) { long long val = 0; for (int i = 0; i < S; ++i) if (nask >> i & 1) { val ^= basis.bas[i + F]; } t.insert(val); } 42; ; array<array<int, (1 << F)>, F + 1> dp{}; for (auto &v : dp) fill(begin(v), end(v), -1); auto calc = [&](int b, int nask) { if (~dp[b][nask]) return dp[b][nask]; for (int i = 0; i <= F; ++i) dp[i][nask] = 0; for (long long x : s) ++dp[__builtin_popcountll(x ^ nask)][nask]; return dp[b][nask]; }; vector<int> ans(m + 1); for (long long x : t) { long long rt = x & ((1 << F) - 1); long long lf = x - rt; int cnt = __builtin_popcountll(lf); for (int i = 0; i <= F; ++i) { adds(ans[cnt + i], calc(i, (int)rt)); } } const int pw = mpow(2, n - basis.rank); for (int x : ans) { cout << mul(x, pw) << ; } cout << n ; } |
#include <bits/stdc++.h> using namespace std; int main() { int t, n; cin >> t; while (t--) { cin >> n; int ar[n], e = 0, o = 0; for (int i = 0; i < n; i++) cin >> ar[i]; for (int i = 0; i < n; i++) { if (ar[i] % 2 == 0) e = e + 1; else o = o + 1; } if ((e == n) || (o == n)) cout << YES << endl; else cout << NO << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int k, n, w; cin >> k >> n >> w; long long t = k * (w) * (w + 1) / 2, h = 0; cout << max(h, t - n) << endl; } |
#include <bits/stdc++.h> using namespace std; const int N = 3005; const int MOD = 998244353; int f[N][N]; char s[N], t[N]; int main() { scanf( %s %s , s + 1, t + 1); int n = strlen(s + 1), m = strlen(t + 1); for (int l = 1; l <= n; ++l) { for (int r = n; r >= l; --r) { int pos = r - l + 1; if (l == 1 and r >= m) f[l][r] = 1; char cur = s[pos + 1]; if (l > 1 and (l - 1 > m or t[l - 1] == cur)) { f[l][r] += f[l - 1][r]; if (f[l][r] >= MOD) f[l][r] -= MOD; } if (r < n and (r + 1 > m or t[r + 1] == cur)) { f[l][r] += f[l][r + 1]; if (f[l][r] >= MOD) f[l][r] -= MOD; } } } int ans = 0; for (int l = 1; l <= n; ++l) { if (l > m or s[1] == t[l]) { ans += f[l][l]; if (ans >= MOD) ans -= MOD; ans += f[l][l]; if (ans >= MOD) ans -= MOD; } } cout << ans << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; vector<string> a, b; int n, m, k; string s; int main() { cin >> n >> m; for (int i = 0; i < n; ++i) { cin >> s; a.push_back(s); } for (int i = 0; i < m; ++i) { cin >> s; b.push_back(s); } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (a[i] == b[j]) ++k; } } if (k % 2 == 0) { if (n > m) cout << YES ; else cout << NO ; } else { if (n >= m) cout << YES ; else cout << NO ; } } |
#include <bits/stdc++.h> using namespace std; int n, p; long double intial[100009], usage[100009]; bool solve(long double x) { long double pp = p * x; for (int i = 0; i < n; i++) { pp -= max(x * usage[i] - intial[i], (long double)0); if (pp < -1e-10) return false; } return true; } int main() { cin >> n >> p; for (int i = 0; i < n; i++) cin >> usage[i] >> intial[i]; long double low = 0, high = 1e19, m; for (int i = 0; i < 200; i++) { m = (low + high) / 2; if (solve(m)) low = m; else high = m; } if (low > 1e18 + 20) return cout << -1, 0; cout << setprecision(6) << fixed << low; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int a[N]; int b[N]; int n; int mp[2 * N]; int ans; unordered_map<int, int> num; int sol_01(int a[], int n) { memset(mp, -1, sizeof mp); int cur = 0; int ans = 0; int offset = 2e5; for (int i = 1; i <= n; i++) { cur += a[i]; if (cur == 0) ans = i; if (mp[cur + offset] != -1) ans = max(ans, i - mp[cur + offset]); else mp[cur + offset] = i; } return ans; } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { scanf( %d , &a[i]); num[a[i]]++; } int maxx = 0; int cc = 0; for (int i = 1; i <= 100; i++) if (num[i] > cc) { maxx = i; cc = num[i]; } for (int i = 1; i <= 100; i++) if (num[i] && i != maxx) { for (int j = 1; j <= n; j++) if (a[j] == maxx) b[j] = 1; else if (a[j] == i) b[j] = -1; else b[j] = 0; ans = max(ans, sol_01(b, n)); } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { string st; while (getline(cin, st)) { int f = 0; int s = st.size() - 2; if (st[s] == ) { while (st[s] == ) { if (st[s - 1] == a || st[s - 1] == e || st[s - 1] == i || st[s - 1] == o || st[s - 1] == u || st[s - 1] == y || st[s - 1] == A || st[s - 1] == E || st[s - 1] == I || st[s - 1] == O || st[s - 1] == U || st[s - 1] == Y ) { f++; break; } s--; } } if (st[s] == a || st[s] == e || st[s] == i || st[s] == o || st[s] == u || st[s] == y || st[s] == A || st[s] == E || st[s] == I || st[s] == O || st[s] == U || st[s] == Y ) { f++; } if (f == 0) cout << NO << endl; else cout << YES << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int wrapper(int n) { while (n) { int a = n % 3; n = n / 3; if (a == 2) return 0; } return 1; } void solve() { int n; cin >> n; while (wrapper(n) == 0) n++; cout << n << n ; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; cin >> t; while (t--) solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; const int MOD = 1e9 + 7; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); string s; cin >> s; sort(s.begin(), s.end()); long long int sz = (int)s.size(); int i = 0, ct = 0; while (s[i] == a ) { ct++; i++; } while (sz && ct * 2 <= sz) { char ch = s.back(); sz--; if (ch == a ) ct--; } cout << sz; return 0; } |
#include <bits/stdc++.h> #include <stdio.h> using namespace std; typedef long long ll; typedef unsigned long long ul; #define len (int)1e5+5 #define mod (ll)(1e9+7) #define pq(a) priority_queue< a > #define pqd(b) priority_queue< b , vector< b > , greater< b >> #define MP make_pair #define v(i) vector<i> #define p(aa,bb) pair< aa,bb> #define pb push_back #define pp pop_back #define clr(a) memset((a),0,sizeof((a))) #define rep(i,a,b) for(int i=(a);i<=(b);i++) #define repd(i,a,b) for(ll i=(a);i>=(b);i--) #define all(a) (a).begin(),(a).end() #define F first #define S second #define ln length() #define INF (1LL<<61) #define co cout<< #define ci cin>> #define sc(a,b) scanf( %(a) ,&(b)) #define nl n #define fio(a,b) freopen( a , r ,stdin); freopen( b , w ,stdout); #define ios ios::sync_with_stdio(0); cin.tie(0); ul modpow(ul x,ul n,ul m){ if(n==0) return 1UL%m; ul u=modpow(x,n/2,m); u = (u*u)%m; if(n%2==1) u = (u*x)%m; return u; } bool prm[len]; v(int) prm_no; void make_prm(){ prm[0]=prm[1]=true; for(int first=2;first<=len;first++){ if(!prm[first]){ prm_no.pb(first); for(int second=2*first;second<=len;second+=first)prm[second]=true; } } } v(int) factor; void make_factor(int n) { for(int i=1;i*i<=n;i++){ if(n%i==0){ factor.pb(i);factor.pb(n/i); if(i*i==n)factor.pp(); } } } ul pascal_tri[2][(int)105]; void make_pascalTri(int n){ bool f=0;clr(pascal_tri); for(int i=0;i<=n;i++){ pascal_tri[f][0]=1; for(int j=1;j<=i;j++){ pascal_tri[f][j]=pascal_tri[!f][j]+pascal_tri[!f][j-1]; } f=!f; } } int prm_div[len]; void make_prm_div(){ for(auto u:prm_no){ for(int i=u;i<=len;i+=u)prm_div[i]++; } } ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); } ll give_floor(ll n,ll a) { if(n%a==0)return n/a; else{ return n/a+1; } } bool is_perfect(ll n) { ll i; for(i=1;i*i<n;i++); return i*i==n; } int n,k; void solve(){ ci n>>k; string s;ci s; int cnt=0; for(int i=1;i<=(n-i);i++){ if(s[i-1]==s[n-i])cnt++; else break; } if(cnt>=k and n>=2*k+1)co YES n ; else co NO n ; } signed main(){ //ios int t;//=1; ci t; while(t--){ solve(); } } |
#include <bits/stdc++.h> using namespace std; long long a, b, c, d, e, f[200009], i, j, zx, xc, s, lf[200009], rg[200009], tim, up[200009]; vector<long long> v[200009], shv[200009]; pair<long long, long long> dp[200009]; bool bo[200009], ka[200009]; bool anc(long long q, long long w) { if (lf[q] <= lf[w] && rg[q] >= rg[w]) return 1; else return 0; } void dfs(long long q, long long w) { bo[q] = 1; tim++; lf[q] = rg[q] = tim; for (vector<long long>::iterator it = v[q].begin(); it != v[q].end(); it++) { if (bo[(*it)] == 1 && (*it) != w) { ka[q] = 1; } if (bo[(*it)] == 1) continue; shv[q].push_back((*it)); dfs((*it), q); if (rg[q] < rg[(*it)]) rg[q] = rg[(*it)]; if (ka[(*it)] == 1) ka[q] = 1; } } void dfs2(long long q, long long w) { up[q] = q; for (vector<long long>::iterator it = v[q].begin(); it != v[q].end(); it++) { if ((*it) == w) continue; if (anc((*it), up[q]) == 1) up[q] = (*it); } for (vector<long long>::iterator it = shv[q].begin(); it != shv[q].end(); it++) { dfs2((*it), w); if (anc(up[(*it)], up[q]) == 1) up[q] = up[(*it)]; } } void dfs3(long long q, long long w) { dp[q].first = 0; dp[q].second = 0; long long pas = 0, mx = 0, mxx = 0, ma = 0; for (vector<long long>::iterator it = shv[q].begin(); it != shv[q].end(); it++) { dfs3((*it), q); pas += dp[(*it)].first; if (mxx < dp[(*it)].second) mxx = dp[(*it)].second; if (ma < dp[(*it)].second - dp[(*it)].first) ma = dp[(*it)].second - dp[(*it)].first; } if (ka[q] == 1) { dp[q].first = pas + f[q]; } dp[q].second = pas + f[q] + ma; } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> a >> b; for (i = 1; i <= a; i++) cin >> f[i]; for (i = 1; i <= b; i++) { cin >> c >> d; v[c].push_back(d); v[d].push_back(c); } cin >> s; dfs(s, 0); dfs2(s, 0); dfs3(s, 0); cout << dp[s].second; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long oo = 0x7FFFFFFF; int n; char bad[105][105]; int len[105]; char st[105]; int leng; char let[5], letter; bool mai[105]; bool alterar[105]; void read() { scanf( %d , &n); char *c; for (int i = 0; i < n; i++) { scanf( %s , bad[i]); c = bad[i]; while (*c) { if ( A <= *c && *c <= Z ) *c = *c - A + a ; c++; } len[i] = c - bad[i]; } scanf( %s%s , st, let); leng = 0; while (st[leng]) { if ( a <= st[leng] && st[leng] <= z ) mai[leng] = false; else mai[leng] = true; alterar[leng] = false; if (mai[leng]) st[leng] = st[leng] - A + a ; leng++; } letter = let[0]; } void comp(int k) { int lasts = leng - len[k] + 1; for (int i = 0; i < lasts; i++) { bool igual = true; for (int j = 0; j < len[k]; j++) if (st[i + j] != bad[k][j]) { igual = false; break; } if (!igual) continue; for (int j = 0; j < len[k]; j++) alterar[i + j] = true; } } void process() { for (int i = 0; i < n; i++) comp(i); char prox = a ; if (letter == a ) prox = b ; for (int i = 0; i < leng; i++) { if (alterar[i]) { if (st[i] == letter) st[i] = prox; else st[i] = letter; } if (mai[i]) st[i] = st[i] + A - a ; } printf( %s n , st); } int main() { read(); process(); return 0; } |
#include <bits/stdc++.h> using namespace std; int n, k; int odd, even; bool win(int k, int odd, int even) { if (odd * 2 <= k || (even * 2 <= k && (k - even) % 2 == odd % 2)) return false; return true; } int main() { scanf( %d%d , &n, &k); for (int i = 1; i <= n; i++) { int x; scanf( %d , &x); if (x & 1) odd++; else even++; } k = n - k; if (k == 0) { if (odd & 1) printf( Stannis n ); else printf( Daenerys n ); } else if (k % 2 == 0) { if (even * 2 <= k && (k - even) % 2 != odd % 2) printf( Stannis n ); else printf( Daenerys n ); } else if (k % 2 == 1) { if ((odd > 0 && win(k - 1, odd - 1, even)) || (even > 0 && win(k - 1, odd, even - 1))) printf( Stannis n ); else printf( Daenerys n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; vector<pair<long long, long long> > v; long long x; long long k; int broj; int main(void) { cin >> x; k = 0; long long i = 1; for (long long i = 1; i <= sqrt(x); ++i) { if (k + i * i > (1000000000000000000)) goto van; k = k + i * i; if (x - k < 0) goto van; if ((2 * (x - k)) % (i * (i + 1)) == 0) { v.push_back( pair<long long, long long>(i, i + (2 * (x - k)) / (i * (i + 1)))); } } van:; for (int i = 0; i < v.size(); ++i) { broj++; } for (int i = v.size() - 1; i >= 0; --i) { if (v[i].second == v[i].first) continue; broj++; } cout << broj << endl; for (int i = 0; i < v.size(); ++i) { broj++; cout << v[i].first << << v[i].second << endl; } for (int i = v.size() - 1; i >= 0; --i) { if (v[i].second == v[i].first) continue; broj++; cout << v[i].second << << v[i].first << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; long long k; scanf( %d , &n); scanf( %I64d , &k); int a; scanf( %d , &a); int ans = a; int count = 0; if (k > n - 2) { for (int i = 1; i < n; ++i) { scanf( %d , &a); if (a > ans) ans = a; } } else { for (int i = 1; i < n; ++i) { scanf( %d , &a); if (count == k) break; if (ans > a) count++; else { count = 1; ans = a; } } } printf( %d n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int t, n; scanf( %d , &t); while (t--) { scanf( %d , &n); for (int i = n * 4, cnt = 1; cnt < n; cnt++, i -= 2) printf( %d , i); printf( %d n , n * 2 + 2); } return 0; } |
#include <bits/stdc++.h> using namespace std; class BigInt { public: string value; bool sign; BigInt(); BigInt(string); BigInt(long long); BigInt operator+(BigInt &); BigInt operator-(BigInt &); bool operator==(BigInt &); bool operator!=(BigInt &); bool operator>(BigInt &); bool operator<(BigInt &); BigInt operator++(int); BigInt operator--(int); friend ostream &operator<<(ostream &, const BigInt &); friend istream &operator>>(istream &, BigInt &); }; string toString(long long n) { stringstream t; t << n; return t.str(); } string add(string a, string b) { if (a.length() > b.length()) swap(a, b); a = string(b.length() - a.length(), 0 ) + a; string res = ; int carry = 0; for (int i = a.length() - 1; i >= 0; i--) { carry += (a[i] - 0 ) + (b[i] - 0 ); res = char(carry % 10 + 0 ) + res; carry /= 10; } res = (carry != 0 ? string(1, char(carry + 0 )) : ) + res; return res; } string sub(string a, string b) { b = string(a.length() - b.length(), 0 ) + b; string res = ; for (int i = a.length() - 1; i >= 0; i--) { if (a[i] >= b[i]) res = char(a[i] - b[i] + 0 ) + res; else { for (int j = i - 1; j >= 0; j--) { if (a[j] != 0 ) { a[j]--; break; } else a[j] = 9 ; } res = char(10 + a[i] - b[i] + 0 ) + res; } } return res.find_first_not_of( 0 ) != -1 ? res.substr(res.find_first_not_of( 0 )) : 0 ; } int compare(string a, string b) { if (a.length() != b.length()) return a.length() > b.length(); for (int i = 0; i < a.length(); i++) if (a[i] != b[i]) return a[i] > b[i]; return 2; } BigInt::BigInt(string s) { value = s[0] == - ? s.substr(1) : s; sign = s[0] == - && s.substr(1) != 0 ; value = s.find_first_not_of( -0 ) != -1 ? s.substr(s.find_first_not_of( -0 )) : 0 ; } BigInt::BigInt(){}; BigInt::BigInt(long long n) : BigInt(toString(n)) {} BigInt BigInt::operator+(BigInt &secondNumber) { if (sign == secondNumber.sign) return BigInt((sign ? - : ) + add(value, secondNumber.value)); else if (compare(value, secondNumber.value) == 1) return BigInt((sign ? - : ) + sub(value, secondNumber.value)); else return BigInt((secondNumber.sign ? - : ) + sub(secondNumber.value, value)); } BigInt BigInt::operator-(BigInt &secondNumber) { BigInt a = *this, b = BigInt((secondNumber.sign ? : - ) + secondNumber.value); return a + b; } bool BigInt::operator==(BigInt &secondNumber) { return value == secondNumber.value && sign == secondNumber.sign; } bool BigInt::operator!=(BigInt &secondNumber) { return !(*this == secondNumber); } bool BigInt::operator>(BigInt &secondNumber) { BigInt res = *this - secondNumber; return !res.sign && res.value != 0 ; } bool BigInt::operator<(BigInt &secondNumber) { return !(*this > secondNumber) && *this != secondNumber; } BigInt BigInt::operator++(int dummy) { BigInt a = *this, b = BigInt( 1 ); *this = a + b; return a; } BigInt BigInt::operator--(int dummy) { BigInt a = *this, b = BigInt( 1 ); *this = a - b; return a; } ostream &operator<<(ostream &out, const BigInt &c) { out << (c.sign ? - : ) << c.value; return out; } istream &operator>>(istream &in, BigInt &c) { string s; in >> s; c = BigInt(s); return in; } int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}; int dy[] = {1, -1, 0, 0, -1, 1, 1, -1}; int main() { long long n; scanf( %lld , &n); map<long long, long long> f; long long x, cnt = 0; long long sum = 0; BigInt ans = 0; for (int i = 0; i < n; i++) { scanf( %lld , &x); long long prev = cnt - f[x] - f[x + 1] - f[x - 1]; long long nwSum = sum - (f[x] * x) - (f[x + 1] * (x + 1)) - (f[x - 1] * (x - 1)); BigInt add = prev * x - nwSum; ans = ans + add; sum += x; cnt++; f[x]++; } cout << ans; } |
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; namespace BF { long long solve(int N, int M, int K, const vector<int>& T, const vector<int>& A, const vector<int>& B) { long long res = INF; for (int mask = 0, _n = (1 << N); mask < _n; ++mask) { if ((__builtin_popcount((mask))) != M) continue; int cntA = 0, cntB = 0; long long cur = 0; for (int i = 0, _n = (N); i < _n; ++i) { if (mask & (1 << i)) { if (A[i]) cntA++; if (B[i]) cntB++; cur += T[i]; } } if (cntA >= K && cntB >= K) res = min(res, cur); } return res < INF ? res : -1; } } // namespace BF struct Item { int id, value; bool operator<(const Item i) const { if (value != i.value) return value < i.value; return id < i.id; } bool operator>(const Item i) const { if (value != i.value) return value > i.value; return id > i.id; } }; ostream& operator<<(ostream& out, Item const& item) { return out << ( << item.id << : << item.value << ) ; } class MBest { long long best_sum; multiset<int, greater<int> > le; multiset<int> gt; void transfer_gt_to_le() { assert(!gt.empty()); auto it = gt.begin(); le.insert(*it); best_sum += *it; gt.erase(it); } void transfer_le_to_gt() { assert(!le.empty()); auto it = le.begin(); gt.insert(*it); best_sum -= *it; le.erase(it); } public: MBest() : best_sum(0) {} int getM() const { return le.size(); } long long query_best_sum() const { return best_sum; } void add(int x) { le.insert(x); best_sum += x; transfer_le_to_gt(); } void remove(int x) { auto it_gt = gt.find(x); if (it_gt != gt.end()) { gt.erase(it_gt); return; } auto it_le = le.find(x); assert(it_le != le.end()); le.erase(it_le); best_sum -= x; transfer_gt_to_le(); } void increaseM() { transfer_gt_to_le(); } }; vector<long long> get_psum(const vector<Item>& V) { vector<long long> res(int((V).size())); for (int i = (1), _b = (int((V).size()) - 1); i <= _b; ++i) res[i] = res[i - 1] + V[i].value; return res; } long long solve(int N, int M, int K, const vector<int>& T, const vector<int>& A, const vector<int>& B) { MBest mb; vector<Item> alice, bob, both; for (int i = 0, _n = (N); i < _n; ++i) { Item item = {i + 1, T[i]}; if (A[i] && B[i]) both.push_back(item); else if (A[i]) alice.push_back(item); else if (B[i]) bob.push_back(item); else mb.add(item.value); } int minj = min(int((alice).size()), int((bob).size())); if (minj + int((both).size()) < K) { cout << -1 << n ; return -1; } sort(alice.begin(), alice.end()); sort(bob.begin(), bob.end()); sort(both.begin(), both.end()); minj = min(minj, K); for (int j = (minj), _b = (int((alice).size()) - 1); j <= _b; ++j) mb.add(alice[j].value); alice.erase(alice.begin() + minj, alice.end()); alice.insert(alice.begin(), {0, 0}); for (int j = (minj), _b = (int((bob).size()) - 1); j <= _b; ++j) mb.add(bob[j].value); bob.erase(bob.begin() + minj, bob.end()); bob.insert(bob.begin(), {0, 0}); both.insert(both.begin(), {0, 0}); for (int k = (K - minj), _b = (int((both).size()) - 1); k <= _b; ++k) mb.add(both[k].value); vector<long long> psum_alice = get_psum(alice); vector<long long> psum_bob = get_psum(bob); vector<long long> psum_both = get_psum(both); long long res = INF; int bestj = -1; for (int j = minj; j >= 0; --j) { int k = K - j; if (k >= int((both).size())) break; mb.remove(both[k].value); int nbooks = K + j; if (nbooks <= M) { int nmissing = M - nbooks; while (mb.getM() < nmissing) mb.increaseM(); long long best_other = mb.query_best_sum(); long long cur = psum_alice[j] + psum_bob[j] + psum_both[k] + best_other; if (res > cur) { bestj = j; res = cur; } } if (j > 0) { mb.add(alice[j].value); mb.add(bob[j].value); } } if (bestj < 0) { cout << -1 n ; return -1; } set<Item> avail; for (int i = 0, _n = (N); i < _n; ++i) avail.insert({i + 1, T[i]}); vector<int> best_books; for (int j = (1), _b = (bestj); j <= _b; ++j) { Item item = alice[j]; avail.erase(item); best_books.push_back(item.id); } for (int j = (1), _b = (bestj); j <= _b; ++j) { Item item = bob[j]; avail.erase(item); best_books.push_back(item.id); } for (int k = (1), _b = (K - bestj); k <= _b; ++k) { Item item = both[k]; avail.erase(item); best_books.push_back(item.id); } assert(int((best_books).size()) <= M); while (int((best_books).size()) < M) { auto it = avail.begin(); best_books.push_back(it->id); avail.erase(it); } cout << res << n ; long long sum = 0; for (int id : best_books) { cout << id << ; sum += T[id - 1]; } cout << n ; return res; } int main(int argc, char* argv[]) { ios_base::sync_with_stdio(false); cin.tie(nullptr); int N, M, K; cin >> N >> M >> K; vector<int> T(N), A(N), B(N); for (int i = 0, _n = (N); i < _n; ++i) cin >> T[i] >> A[i] >> B[i]; solve(N, M, K, T, A, B); return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m, q; char s[111111][12]; const int OFF = 128, BITS = 7, MASK = 127; int dy[111111][12], dx[111111][12]; char qu[5]; int mx; void go(int &y, int &x, int upTo) { for (int z = 0; z < (int)(mx); ++z) { if (s[y][x] == ^ ) { --y; if (y == upTo || y == 0) break; } else if (s[y][x] == < ) { if (--x == 0) break; } else { if (++x > m) break; } } } int main() { scanf( %d%d%d , &n, &m, &q); for (int i = 1; i <= n; ++i) scanf( %s , s[i] + 1), s[i][0] = s[i][m + 1] = f ; for (int i = 0; i < (int)(m + 2); ++i) s[0][i] = s[n + 1][i] = f ; mx = (OFF + 2) * m; for (int i = OFF; i <= n; i += OFF) for (int j = 1; j <= m; ++j) { int y = i, x = j; go(y, x, i - OFF); if (y != i - OFF && y != 0 && x != 0 && x != m + 1) dy[i][j] = -1; else { dy[i][j] = y; dx[i][j] = x; } } for (int query = 0; query < (int)(q); ++query) { scanf( %s , qu); if (qu[0] == C ) { int Y, X; scanf( %d%d%s , &Y, &X, qu); s[Y][X] = qu[0]; if (Y & MASK) { Y &= ~MASK; Y += OFF; } for (int j = 1; j <= m; ++j) { int y = Y, x = j; go(y, x, Y - OFF); if (y != Y - OFF && y != 0 && x != 0 && x != m + 1) dy[Y][j] = -1; else { dy[Y][j] = y; dx[Y][j] = x; } } } else { int y, x; scanf( %d%d , &y, &x); if (y & MASK) { int upTo = y & ~MASK; go(y, x, upTo); if (y != upTo && y != 0 && x != 0 && x != m + 1) { printf( -1 -1 n ); continue; } } bool flag = false; while (y != 0 && x != 0 && x != m + 1) { int newY = dy[y][x]; x = dx[y][x]; y = newY; if (y == -1) { printf( -1 -1 n ); flag = true; break; } } if (flag) continue; printf( %d %d n , y, x); } } return 0; } |
#include <bits/stdc++.h> using namespace std; using LL = long long; constexpr int N = 2e5 + 5; int p[N]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for (int i = 2; i <= n; i++) { int x; cin >> x; p[i] = x; } vector<int> path; int x = n; while (1) { path.push_back(x); x = p[x]; if (x == 0) break; } reverse(begin(path), end(path)); for (int x : path) cout << x << ; } |
#include <bits/stdc++.h> using namespace std; int main() { int i, j, k, res, n, sum, t, w, temp, sz1, sz2; while (scanf( %d , &n) == 1) { vector<int> v[3]; sum = 0; for (i = 0; i < n; i++) { scanf( %d%d , &t, &w); v[t].push_back(w); sum += w; } sort(v[1].begin(), v[1].end()); sort(v[2].begin(), v[2].end()); sz1 = v[1].size(); sz2 = v[2].size(); res = 10000000; for (i = 0; i <= sz1; i++) { for (j = 0; j <= sz2; j++) { temp = sum; for (k = sz1 - 1; k > sz1 - 1 - i; k--) { temp -= v[1][k]; } for (k = sz2 - 1; k > sz2 - 1 - j; k--) { temp -= v[2][k]; } if (temp <= i + j * 2) res = res < i + j * 2 ? res : i + j * 2; } } printf( %d n , res); } return 0; } |
#include <bits/stdc++.h> using namespace std; bool valid(long long num, long long den) { if (den == 0) { return num == 0; } else { return (num % den) == 0; } } long long gcd(long long a, long long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } bool valid(long long a, long long b, long long c) { if (a == 0 && b == 0) { return c == 0; } if (a < 0) { a = -a; } if (b < 0) { b = -b; } return c % gcd(a, b) == 0; } int main() { long long ax, ay, bx, by, cx, cy; cin >> ax >> ay >> bx >> by >> cx >> cy; long long a_x = -ay, a_y = ax; long long c_x = -cy, c_y = cx; for (long long a = -1; a <= 1; ++a) { for (long long a_ = -1; a_ <= 1; ++a_) { if (abs(a) + abs(a_) == 1) { long long X = bx - a * ax - a_ * a_x; long long Y = by - a * ay - a_ * a_y; long long num1 = X * c_y - Y * c_x; long long den1 = cx * c_y - cy * c_x; long long num2 = X * cy - Y * cx; long long den2 = c_x * cy - c_y * cx; bool ok; if (den1 != 0 && den2 != 0) { ok = (num1 % den1) == 0 && (num2 % den2) == 0; } else { if (cx == 0 && c_x == 0) { ok = X == 0 && valid(cy, c_y, Y); } else if (cy == 0 && c_y == 0) { ok = Y == 0 && valid(cx, c_x, X); } else { if (cx == 0) { ok = (X % c_x) == 0 && (Y % c_y) == 0 && X / c_x == Y / c_y; } else if (c_x == 0) { ok = (X % cx) == 0 && (Y % cy) == 0 && X / cx == Y / cy; } else { ok = valid(cx, c_x, X) && X * cy == Y * cx; } } } if (ok) { cout << YES << endl; return 0; } } } } cout << NO << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; double pi = 4 * atan(1.0); double eps = 1e-9; string stringmin = ! ; string stringmax = ~ ; int main() { cout.precision(14); int n; cin >> n; vector<int> v(n), vlen(n); for (auto &i1 : v) cin >> i1; for (int i = 0; i < n; i++) { int f1 = 0; int q = i; for (int j = 0; j < 101; j++) { q = v[q] - 1; if (q == i) { f1 = 1; vlen[i] = j + 1; break; } } if (!f1) { cout << -1; return 0; } } for (auto &i1 : vlen) if ((i1 & 1) == 0) i1 >>= 1; long long ans = 1; for (int i = 2; i < 110; i++) { int maxcnt = 0; for (auto &i1 : vlen) { int cnt = 0; while (i1 % i == 0) { cnt++; i1 /= i; } maxcnt = max(cnt, maxcnt); } for (int j = 0; j < maxcnt; j++) ans *= i; } cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; struct S { int dist, u, v; S(int dd, int uu, int vv) { dist = dd; u = uu; v = vv; } bool operator<(const S& a) const { if (dist != a.dist) return dist < a.dist; return u < a.u; } bool operator>(const S& a) const { if (dist != a.dist) return dist > a.dist; return u > a.u; } }; vector<pair<int, int> > pA; vector<pair<int, int> > pB; vector<int> A; vector<int> B; pair<int, int> res[100007]; int main() { int N, M; scanf( %d %d , &N, &M); ; for (int i = (0); i < (M); i++) { int a, b; scanf( %d %d , &a, &b); ; if (b) A.push_back(a); else B.push_back(a); if (b) pA.push_back(make_pair(a, i)); else pB.push_back(make_pair(a, i)); } sort(A.begin(), A.end()); sort(B.begin(), B.end()); sort(pA.begin(), pA.end()); sort(pB.begin(), pB.end()); priority_queue<S, vector<S>, greater<S> > pq; for (int i = (0); i < ((int)A.size() - 1); i++) pq.push(S(A[i + 1], i, i + 1)); for (int m = (0); m < (B.size()); m++) { S s = pq.top(); pq.pop(); if (B[m] < s.dist) { cout << -1 << endl; return 0; } res[pB[m].second] = make_pair(s.u, s.v); s.v++; if (s.v < A.size()) { s.dist -= A[s.v - 1]; s.dist += A[s.v]; pq.push(s); } } for (int i = (0); i < (A.size()); i++) res[pA[i].second] = make_pair(i, N - 1); for (int i = (0); i < (M); i++) cout << res[i].first + 1 << << res[i].second + 1 << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf( %d , &t); while (t--) { int n, s1 = 0, s2 = 0; scanf( %d , &n); int a[100] = {0}; for (int i = 0; i < n; i++) { scanf( %d , a + i); if (a[i] % 2) s1++; else s2++; } if (s1 && s2) printf( NO n ); else printf( YES n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long l1, r1, l2, r2, k, l, r; cin >> l1 >> r1 >> l2 >> r2 >> k; l = max(l1, l2); r = min(r1, r2); if (r < l) cout << 0; else if (l <= k && k <= r) cout << r - l; else cout << r - l + 1 << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int putere(long long int a, long long int b) { if (b == 0) { return 1; } else { if (b == 1) return a % 1000000007; else { long long int r; r = putere(a, b / 2); r = (r * r) % 1000000007; if (b % 2 == 0) return r; else return (r * a) % 1000000007; } } } int main() { long long int x, k, nr1, nr2; scanf( %lld%lld , &x, &k); if (x == 0) printf( 0 ); else { x = x % 1000000007; nr1 = putere(2, k); nr2 = nr1 * x * 2 % 1000000007; printf( %lld , (nr2 - nr1 + 1 + 1000000007) % 1000000007); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long int k, n, first = -1, sum = 0; cin >> n >> k; long long int a[n]; long long int ans = 0; for (long long int i = 0; i < n; ++i) cin >> a[i]; string s; cin >> s; int flag = 0; for (long long int i = 0; i < n; ++i) { if (s[i] == s[i + 1]) { if (first == -1) first = i; flag = 1; } else if (flag) { sort(a + first, a + i + 1); long long int mini = min(k, i - first + 1); for (long long int j = 0; j < mini; ++j) { sum += a[i - j]; } flag = 0; first = -1; } else { sum += a[i]; } } cout << sum << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; struct DSU { long long int parent[500001], connected[500001]; void intialize(int x) { for (long long int i = 1; i <= x; i++) { parent[i] = i; connected[i] = 1; } } long long int getparent(long long int k) { while (k != parent[k]) { parent[k] = parent[parent[k]]; k = parent[k]; } return k; } long long int getsize(long long int k) { return connected[getparent(k)]; } void unionn(long long int u, long long int v) { long long int parent1 = getparent(u), parent2 = getparent(v); if (parent1 == parent2) return; if (connected[parent1] > connected[parent2]) swap(parent1, parent2); connected[parent2] += connected[parent1]; connected[parent1] = 0; parent[parent1] = parent[parent2]; } }; DSU dsu; int main() { long long int t; cin >> t; while (t--) { long long int n; cin >> n; dsu.intialize(n + 1); for (long long int i = 1; i <= n; i++) { long long int x; cin >> x; dsu.unionn(i, x); } for (long long int i = 1; i <= n; i++) cout << dsu.getsize(i) << ; cout << n ; } } |
#include <bits/stdc++.h> using namespace std; string str; bool judge(char c) { return (c == _ || islower(c) || isdigit(c) || isupper(c)); } int main() { int cnt = 0, count = 0, type = 0, len; cin >> str; type = 0; len = str.length(); for (int i = 0; i < len; i++) { char c = str[i]; if (c == @ ) { if (type == 1) { cout << NO << endl; return 0; } type = 1; if (cnt < 1 || cnt > 16) { cout << NO << endl; return 0; }; cnt = 0; } else if (c == / ) { if (type == 2) { cout << NO << endl; return 0; } type = 2; if (cnt < 1 || cnt > 16) { cout << NO << endl; return 0; } if (count < 1 || count > 32) { cout << NO << endl; return 0; } cnt = 0; } else if (c == . ) { if (type != 1) { cout << NO << endl; return 0; } if (cnt < 1 || cnt > 16) { cout << NO << endl; return 0; } cnt = 0; } else if (c > 127 || c < 33) { cout << NO << endl; return 0; } else if (type == 0) { if (judge(c)) { cnt++; continue; } else { cout << NO << endl; return 0; } } else if (type == 1) { if (judge(c)) { cnt++; count++; continue; } else { cout << NO << endl; return 0; } } else if (type == 2) { if (judge(c)) { cnt++; continue; } else { cout << NO << endl; return 0; } } } if (cnt < 0 || cnt > 16) { cout << NO << endl; return 0; } if (type >= 1 && cnt > 0) { cout << YES << endl; return 0; } else { cout << NO << endl; return 0; } } |
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; struct cc { int x, y, z; bool operator<(const cc &t) const { if (y != t.y) return y > t.y; else return x > t.x; } } a[N]; struct xx { int w, id; } q[N]; long long dp[N]; int main() { int n, m, i, j, k, ans, num, sum; scanf( %d , &n); for (i = 1; i <= n; i++) { scanf( %d%d%d , &a[i].x, &a[i].y, &a[i].z); } sort(a + 1, a + n + 1); long long maxx = dp[1] = a[1].z; q[1] = {a[1].x, 1}; int l = 1, r = 1; for (i = 2; i <= n; i++) { while (l <= r && q[r].w >= a[i].y) r--; dp[i] = dp[q[r].id] + a[i].z; q[++r] = {a[i].x, i}; maxx = max(maxx, dp[i]); } printf( %lld n , maxx); return 0; } |
#include <bits/stdc++.h> using namespace std; int num[300005]; long long dp[300005], ans = -LONG_LONG_MAX; vector<int> G[300005]; int cnt = 0; void dfs(int now, int fa) { for (int k : G[now]) { if (k != fa) { dfs(k, now); dp[now] += max((long long)0, dp[k]); } } ans = max(ans, dp[now]); } void dfs2(int now, int fa) { for (int k : G[now]) { if (k != fa) { dfs2(k, now); dp[now] += max((long long)0, dp[k]); } } if (dp[now] == ans) { dp[now] = 0; cnt++; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> num[i]; dp[i] = num[i]; } for (int i = 1; i < n; i++) { int x, y; cin >> x >> y; G[x].push_back(y); G[y].push_back(x); } dfs(1, -1); for (int i = 1; i <= n; i++) dp[i] = num[i]; dfs2(1, -1); cout << cnt * ans << << cnt; } |
#include <bits/stdc++.h> using namespace std; int kmp(string s) { vector<int> lps(s.size(), 0); for (int i = 1; i < s.size(); ++i) { int index = lps[i - 1]; while (index > 0 && s[i] != s[index]) index = lps[index - 1]; if (s[i] == s[index]) lps[i] = index + 1; else lps[i] = index; } return lps[lps.size() - 1]; } int func(string s) { string t = s + # ; reverse(s.begin(), s.end()); t += s; return kmp(t); } int main() { int t; cin >> t; while (t--) { string s; cin >> s; int l = 0, n = s.length(); for (int i = 0, j = n - 1; i < j; i++, j--) { if (s[i] == s[j]) l++; else break; } string remaining = s.substr(l, n - 2 * l); string ans = s.substr(0, l); if (remaining.size()) { int lp = func(remaining); reverse(remaining.begin(), remaining.end()); int ls = func(remaining); if (lp > ls) { reverse(remaining.begin(), remaining.end()); ans += remaining.substr(0, lp); } else ans += remaining.substr(0, ls); } ans += s.substr(n - l, l); cout << ans << endl; } } |
#include <bits/stdc++.h> using namespace std; long long power(long long x, long long y, long long m) { long long r = 1; for (; y; y >>= 1) { if (y & 1) r = r * x % m; x = x * x % m; } return r; } const long long N = 105, M = 105; long long a[N][M]; int run_test() { long long n, m; cin >> n >> m; for (long long i = 0; i < n; i++) for (long long j = 0; j < m; j++) cin >> a[i][j]; for (long long i = 0; i < n; i++) for (long long j = 0; j < m; j++) { if (a[i][j] % 2 != (i + j) % 2) a[i][j]++; } for (long long i = 0; i < n; i++) { for (long long j = 0; j < m; j++) cout << a[i][j] << ; cout << n ; } return 0; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); srand(time(NULL)); ; long long t; t = 1; cin >> t; while (t--) { run_test(); } } |
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf( %d , &n); if (n < 6) printf( -1 n ); else { printf( 1 2 n2 3 n2 4 n3 5 n3 6 n ); for (int i = 7; i <= n; i++) printf( 3 %d n , i); } for (int i = 2; i <= n; i++) printf( %d %d n , i - 1, i); return 0; } |
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long int LINF = 0x3f3f3f3f3f3f3f3fll; const long double pi = acos(-1); const int MOD = 1e9 + 7; int main() { int n, i; double ans = 0.0; scanf( %d , &n); for (i = 1; i <= n; i++) { ans += 1 / (float)i; } printf( %.6lf n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; constexpr int maxn = 120000; int p[maxn], sz[maxn], top[maxn], hs[maxn], dfn[maxn], ssz[maxn]; namespace segtree { bool marked[maxn << 2]; int sum[maxn << 2], mss[maxn << 2]; void push_down(int v, int tl, int tr) { if (marked[v]) { sum[(v << 1)] = -(((tl + tr) >> 1) - tl + 1); sum[(v << 1 | 1)] = -(tr - ((tl + tr) >> 1)); mss[(v << 1)] = mss[(v << 1 | 1)] = -1; marked[(v << 1)] = marked[(v << 1 | 1)] = true; marked[v] = false; } } void push_up(int v) { sum[v] = sum[(v << 1)] + sum[(v << 1 | 1)]; mss[v] = max(mss[(v << 1 | 1)], mss[(v << 1)] + sum[(v << 1 | 1)]); } void build(int v, int tl, int tr) { if (tl == tr) sum[v] = mss[v] = -1; else { build((v << 1), tl, ((tl + tr) >> 1)); build((v << 1 | 1), ((tl + tr) >> 1) + 1, tr); push_up(v); } } void add(int v, int tl, int tr, int p, int x) { if (tl == tr) sum[v] = mss[v] += x; else { push_down(v, tl, tr); if (p <= ((tl + tr) >> 1)) add((v << 1), tl, ((tl + tr) >> 1), p, x); else add((v << 1 | 1), ((tl + tr) >> 1) + 1, tr, p, x); push_up(v); } } int tmp; int query(int v, int tl, int tr, int l, int r) { static int res; if (v == 1) res = -1; if (tl >= l and tr <= r) { res = max(res, mss[v] + tmp); tmp += sum[v]; } else { push_down(v, tl, tr); if (r > ((tl + tr) >> 1)) query((v << 1 | 1), ((tl + tr) >> 1) + 1, tr, l, r); if (l <= ((tl + tr) >> 1)) query((v << 1), tl, ((tl + tr) >> 1), l, r); } return res; } void set(int v, int tl, int tr, int l, int r) { if (tl >= l and tr <= r) { sum[v] = -(tr - tl + 1); mss[v] = -1; marked[v] = true; } else { push_down(v, tl, tr); if (l <= ((tl + tr) >> 1)) set((v << 1), tl, ((tl + tr) >> 1), l, r); if (r > ((tl + tr) >> 1)) set((v << 1 | 1), ((tl + tr) >> 1) + 1, tr, l, r); push_up(v); } } } // namespace segtree int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, q; cin >> n >> q; for (int i = 2; i <= n; i += 1) cin >> p[i]; for (int i = n; i >= 2; i -= 1) sz[p[i]] += sz[i] += 1; for (int i = 2; i <= n; i += 1) if (sz[i] > sz[hs[p[i]]]) hs[p[i]] = i; for (int i = 1; i <= n; i += 1) if (i == hs[p[i]]) { top[i] = top[p[i]]; dfn[i] = dfn[p[i]] + 1; } else { top[i] = i; dfn[i] = dfn[p[i]] + ssz[p[i]] + sz[hs[p[i]]] + 1; ssz[p[i]] += sz[i]; } segtree::build(1, 1, n); for (int t, v; q; q -= 1) { cin >> t >> v; if (t == 1) segtree::add(1, 1, n, dfn[v], 1); else if (t == 3) { int res = -1; segtree::tmp = 0; for (int x = v; x; x = p[top[x]]) res = max(res, segtree::query(1, 1, n, dfn[top[x]], dfn[x])); cout << (res >= 0 ? black n : white n ); } else { segtree::set(1, 1, n, dfn[v], dfn[v] + sz[v] - 1); int res = -1; segtree::tmp = 0; for (int x = v; x; x = p[top[x]]) res = max(res, segtree::query(1, 1, n, dfn[top[x]], dfn[x])); if (res >= 0) segtree::add(1, 1, n, dfn[v], -res - 1); } } return 0; } |
#include <bits/stdc++.h> using namespace std; int N, M, Q, A[100][100], B[10000][4], i, j; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N >> M >> Q; for (i = 0; i < Q; i++) { cin >> B[i][0] >> B[i][1]; B[i][1]--; if (B[i][0] == 3) { cin >> B[i][2] >> B[i][3]; B[i][2]--; } } for (i = Q - 1; i >= 0; i--) { if (B[i][0] == 1) { for (j = M - 1; j > 0; j--) { swap(A[B[i][1]][j], A[B[i][1]][j - 1]); } } else if (B[i][0] == 2) { for (j = N - 1; j > 0; j--) { swap(A[j][B[i][1]], A[j - 1][B[i][1]]); } } else A[B[i][1]][B[i][2]] = B[i][3]; } for (i = 0; i < N; i++) { for (j = 0; j < M; j++) { cout << A[i][j] << ; } cout << n ; } } |
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; if (n == 0) { cout << 0 << endl; } else if ((n + 1) % 2 == 0) { cout << (n + 1) / 2 << endl; } else { cout << n + 1 << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; map<int, int> mp; int main() { ios::sync_with_stdio(false); int n, m; cin >> n >> m; int a[n + 1]; int ev = 2, od = 1; int tev = 0, tod = 0; int tot = 0; for (int i = 0; i < n; i++) { cin >> a[i]; mp[a[i]]++; if (a[i] % 2 == 0) tev++; else tod++; } for (int i = 0; i < n; i++) { if (mp[a[i]] > 1) { if (a[i] % 2) { if (tod > tev) { while (mp[ev] != 0) ev += 2; if (ev > m) return cout << -1, 0; mp[ev] = 1; mp[a[i]]--; a[i] = ev; tev++; tod--; tot++; } else { while (mp[od] != 0) od += 2; if (od > m) return cout << -1, 0; mp[od] = 1; mp[a[i]]--; a[i] = od; tot++; } } else { if (tod < tev) { while (mp[od] != 0) od += 2; if (od > m) return cout << -1, 0; mp[od] = 1; mp[a[i]]--; a[i] = od; tev--; tod++; tot++; } else { while (mp[ev] != 0) ev += 2; if (ev > m) return cout << -1, 0; mp[ev] = 1; mp[a[i]]--; a[i] = ev; tot++; } } } } for (int i = 0; i < n; i++) { if (a[i] % 2) { if (tev < tod) { tot++; while (mp[ev] != 0) ev += 2; if (ev > m) return cout << -1, 0; mp[ev] = 1; a[i] = ev; tod--; tev++; } } else { if (tev > tod) { tot++; while (mp[od] != 0) od += 2; if (od > m) return cout << -1, 0; mp[od] = 1; a[i] = od; tod++; tev--; } } } cout << tot << endl; for (int i = 0; i < n; i++) cout << a[i] << ; return 0; } |
#include <bits/stdc++.h> using namespace std; using namespace std::chrono; long long power(long long x, unsigned long long y) { long long res = 1; while (y > 0) { if (y & 1) { res = res * x; } y = y >> 1; x = x * x; } return res; } long long powermod(long long x, unsigned long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) { res = (res * x) % p; } y = y >> 1; x = (x * x) % p; } return res; } long long min(long long a, long long b) { return a < b ? a : b; } long long max(long long a, long long b) { return a > b ? a : b; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << << x << ; } void __print(const char *x) { cerr << << x << ; } void __print(const string &x) { cerr << << x << ; } void __print(bool x) { cerr << (x ? true : false ); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << { ; __print(x.first); cerr << , ; __print(x.second); cerr << } ; } template <typename T> void __print(const T &x) { long long f = 0; cerr << { ; for (auto &i : x) cerr << (f++ ? , : ), __print(i); cerr << } ; } void _print() { cerr << ] n ; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << , ; _print(v...); } void snow() { long long a, b, n; cin >> a >> b >> n; while (n--) { long long l, t, m; cin >> l >> t >> m; l--; long long ans = l; long long r = 1e9; long long faci = m * t; if (t < (a + l * (b))) { cout << -1 << n ; continue; } long long start = a + l * (b); long long ll = l; while (l <= r) { long long mid = (l + r) / 2; long long height = a + mid * (b); long long sum = start * (mid - ll + 1) + b * ((mid - ll) * (mid - ll + 1)) / 2; if (height <= t && sum <= m * t) { ans = mid; l = mid + 1; } else { r = mid - 1; } } cout << ans + 1 << n ; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long t; t = 1; while (t--) snow(); return 0; } |
#include <bits/stdc++.h> using namespace std; template <class T, class U> inline T max(T &a, U &b) { return a > b ? a : b; } template <class T, class U> inline T min(T &a, U &b) { return a < b ? a : b; } template <class T> T gcd(T a, T b) { return (b != 0 ? gcd<T>(b, a % b) : a); } template <class T> T lcm(T a, T b) { return (a / gcd<T>(a, b) * b); } class CLASS_NAME { public: int method_NAME() {} }; int main() { int n; string a, b; cin >> n; for (__typeof(0) i = 0; i <= n - 1; i++) a.push_back( 0 ); for (__typeof(0) i = 0; i <= n - 1; i++) b.push_back( 0 ); int k = n / 2; for (__typeof(0) i = 0; i <= k - 1; i++) { a[i] = 1 ; b[i] = 1 ; } vector<int> v, arra, arrb; for (__typeof(0) i = 0; i <= n - 1; i++) { int x, y; cin >> x >> y; arra.push_back(x); arrb.push_back(y); v.push_back(x); v.push_back(y); } sort(v.begin(), v.end()); int tmp = v[n - 1]; for (__typeof(0) i = 0; i <= n - 1; i++) { if (arra[i] <= tmp) a[i] = 1 ; if (arrb[i] <= tmp) b[i] = 1 ; } cout << a << endl << b << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = n; i >= 1; i--) cout << a[i] << ; cout << endl; } } |
#include <bits/stdc++.h> using namespace std; template <typename T> T in() { char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == - ) { ng = true; ch = getchar(); break; } if (ch >= 0 && ch <= 9 ) break; } while (1) { n = n * 10 + (ch - 0 ); ch = getchar(); if (ch < 0 || ch > 9 ) break; } return (ng ? -n : n); } template <typename T> inline T POW(T B, T P) { if (P == 0) return 1; if (P & 1) return B * POW(B, P - 1); else return (POW(B, P / 2) * POW(B, P / 2)); } template <typename T> inline T Gcd(T a, T b) { if (a < 0) return Gcd(-a, b); if (b < 0) return Gcd(a, -b); return (b == 0) ? a : Gcd(b, a % b); } template <typename T> inline T Lcm(T a, T b) { if (a < 0) return Lcm(-a, b); if (b < 0) return Lcm(a, -b); return a * (b / Gcd(a, b)); } long long Bigmod(long long base, long long power, long long MOD) { long long ret = 1; while (power) { if (power & 1) ret = (ret * base) % MOD; base = (base * base) % MOD; power >>= 1; } return ret; } bool isVowel(char ch) { ch = toupper(ch); if (ch == A || ch == U || ch == I || ch == O || ch == E ) return true; return false; } template <typename T> long long int isLeft(T a, T b, T c) { return (a.x - b.x) * (b.y - c.y) - (b.x - c.x) * (a.y - b.y); } long long ModInverse(long long number, long long MOD) { return Bigmod(number, MOD - 2, MOD); } bool isConst(char ch) { if (isalpha(ch) && !isVowel(ch)) return true; return false; } int toInt(string s) { int sm; stringstream second(s); second >> sm; return sm; } char s[5007]; unordered_map<string, int> mp; vector<string> A[5007]; int dp[5007][2001]; int n, m; int koto(string p, int pos, int b) { if (p[0] == 0 || p[0] == 1 ) return (p[pos] - 0 ); if (p[0] == ? ) return b; int id = mp[p]; return dp[id][pos]; } void solve(int pos, int b) { for (int i = 1; i <= n; i++) { if (A[i].size() == 3) { dp[i][pos] = koto(A[i][2], pos, b); } else { int x = koto(A[i][2], pos, b); int y = koto(A[i][4], pos, b); if (A[i][3][0] == A ) x = x & y; if (A[i][3][0] == O ) x = x | y; if (A[i][3][0] == X ) x = x ^ y; dp[i][pos] = x; } } } int main() { cin >> n >> m; getchar(); for (int i = 1; i < n + 1; i++) { gets(s); vector<string> v; string tp; int ln = strlen(s); for (int j = 0; j < ln; j++) { if (s[j] == ) { v.push_back(tp); tp.clear(); } else { tp += s[j]; } } v.push_back(tp); mp[v[0]] = i; for (int j = 0; j < v.size(); j++) A[i].push_back(v[j]); } string x, y; for (int i = 0; i < m; i++) { solve(i, 0); int cnt = 0; for (int j = 1; j < n + 1; j++) { cnt += dp[j][i]; } solve(i, 1); int cnt1 = 0; for (int j = 1; j < n + 1; j++) { cnt1 += dp[j][i]; } if (cnt >= cnt1) { y += 0 ; } else y += 1 ; if (cnt <= cnt1) { x += 0 ; } else x += 1 ; } cout << x << endl; cout << y << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 300005; const int mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1.0); long long qpow(long long a, long long b, long long p) { long long res = 1; while (b) { if (b & 1) res = res * a % p; a = a * a % p; b >>= 1; } return res; } long long a[N]; int main() { std::ios::sync_with_stdio(false); int n, p, k; while (cin >> n >> p >> k) { for (int i = 1; i <= n; i++) cin >> a[i]; map<long long, long long> mp; for (int i = 1; i <= n; i++) mp[(qpow(a[i], 4, p) - k % p * a[i] % p + p) % p]++; map<long long, long long>::iterator it = mp.begin(); long long ans = 0; for (it; it != mp.end(); it++) { ans += (*it).second * ((*it).second - 1) / 2; } cout << ans << endl; } return 0; } |
#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; vector<int> arr(n); vector<pair<int, int>> vpr(n); for (long long i = 0; i <= n - 1; i++) { cin >> arr[i]; vpr[i] = {arr[i], i}; } sort(vpr.begin(), vpr.end()); int j = 0; vector<int> trfs(n); unordered_multiset<int> mst; for (int i = 0; i < n; ++i) { if (i > 0 && vpr[i].first != vpr[i - 1].first) { j++; } trfs[vpr[i].second] = j; mst.insert(j); } vector<int> dp1(n, 1), dp2(n), dp3(n); unordered_map<int, int> pos; for (int i = 0; i < n; ++i) { if (mst.count(trfs[i])) mst.erase(mst.find(trfs[i])); if (pos.count(trfs[i] - 1)) { if (!pos.count(trfs[i])) { dp2[i] = dp1[pos[trfs[i] - 1]] + 1; if (!mst.count(trfs[i] - 1)) { dp2[i] = max(dp2[i], dp2[pos[trfs[i] - 1]] + 1); } } if (!mst.count(trfs[i] - 1)) { dp3[i] = dp2[pos[trfs[i] - 1]] + 1; } dp3[i] = max(dp3[i], dp1[pos[trfs[i] - 1]] + 1); } if (pos.count(trfs[i])) { dp1[i] = dp1[pos[trfs[i]]] + 1; dp3[i] = max(dp3[i], dp3[pos[trfs[i]]] + 1); dp2[i] = max(dp2[i], dp2[pos[trfs[i]]] + 1); } dp2[i] = max(dp2[i], dp1[i]); dp3[i] = max(dp3[i], dp2[i]); pos[trfs[i]] = i; } cout << n - *max_element(dp3.begin(), dp3.end()) << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; long long const mod = 1000000007; long long const md = 998244353; long long mypow(long long a, long long b) { long long res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } ifstream in( txt.in ); ofstream out( txt.out ); int main() { cout << fixed << setprecision(12); cin.tie(0); ios_base::sync_with_stdio(0); cout.tie(0); string s; cin >> s; long long n; cin >> n; while (n--) { long long l, r, k; cin >> l >> r >> k; --l; --r; int u = r - l + 1; k %= u; char c[10005]; int t = r - k + 1; for (int i = 1; i <= k; ++i) { c[i] = s[t]; ++t; } t = l; int i = k + 1; while (t < r - k + 1) { c[i] = s[t]; ++t; ++i; } t = 1; for (int i = l; i <= r; ++i) s[i] = c[t], ++t; } cout << s << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; template <typename T> istream& operator>>(istream& is, vector<T>& v) { for (auto& i : v) is >> i; return is; } template <typename T> ostream& operator<<(ostream& os, vector<T>& v) { for (auto& i : v) os << i << ; return os; } template <typename T, typename U> istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; } template <typename T, typename U> ostream& operator<<(ostream& os, pair<T, U>& p) { os << p.first << << p.second; return os; } int main() { long long b; cin >> b; vector<pair<long long, long long>> v; long long p = 2; while (p * p <= b) { if (b % p == 0) { v.emplace_back(p, 0); while (b % p == 0) { b /= p; v.back().second++; } } ++p; } if (b != 1) v.emplace_back(b, 1); long long c = 1; for (auto p : v) { c *= p.second + 1; } cout << c << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; string S, T; int L = -1, R = -1; void solve() { int i, j, k, l, r, x, y; string s; cin >> S >> T; x = 0; for (i = 0; i < T.size(); i++) { if (T[i] == S[x]) { x++; if (x == S.size()) { L = i; break; } } } x = S.size() - 1; for (i = T.size() - 1; i >= 0; i--) { if (T[i] == S[x]) { x--; if (x == -1) { R = i; break; } } } if (L >= 0 && R >= 0 && L <= R) cout << R - L << endl; else cout << 0 << endl; } int main(int argc, char** argv) { string s; int i; if (argc == 1) ios::sync_with_stdio(false); for (i = 0; i < argc - 1; i++) s += argv[i + 1], s += n ; for (i = 0; i < s.size(); i++) ungetc(s[s.size() - 1 - i], stdin); solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; int N; string A; string B; int main() { ios_base::sync_with_stdio(NULL); cout.tie(NULL); cin.tie(NULL); cin >> N; cin >> A >> B; int answer = 0; for (int i = 0; i < N - 1; ++i) { if (A[i] == B[i]) continue; if (A[i + 1] == B[i] && A[i] == B[i + 1]) swap(A[i], A[i + 1]); else A[i] = B[i]; ++answer; } if (A[N - 1] != B[N - 1]) { ++answer; A[N - 1] = B[N - 1]; } cerr << A << endl; cout << answer << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; vector<int> toOrder; map<int, set<int>> offersByDays; map<int, set<int>> offers; int totSum; int bSearch(int a, int b) { if (a == b) { return a; } int curSum = totSum; int mid = (a + b) / 2; int money = 0; for (int i = 0; i <= mid; i++) { money++; if (offersByDays.count(i) == 0) { continue; } for (auto offer : offersByDays[i]) { if (money == 0) { break; } set<int>::iterator itr = offers[offer].lower_bound(mid); if (itr != offers[offer].end() && *itr == mid) { if (i != mid) { continue; } } else { itr = prev(itr); if (i != *itr) { continue; } } if (money >= toOrder[offer]) { money -= toOrder[offer]; curSum -= toOrder[offer]; } else { curSum -= money; money = 0; } } } if (money >= 2 * curSum) { return bSearch(a, mid); } return bSearch(mid + 1, b); } int main() { int n, m; cin >> n >> m; totSum = 0; for (int i = 0; i < n; i++) { int temp; cin >> temp; toOrder.push_back(temp); totSum += temp; } for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; if (offersByDays.count(a) == 0) { offersByDays.insert({a, {b}}); } else { offersByDays[a].insert(b); } if (offers.count(b) == 0) { offers.insert({b, {a}}); } else { offers[b].insert(a); } } cout << bSearch(0, int(4e5)) + 1 << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { long long int i, n, d, h; cin >> n >> d >> h; if (n >= d + 1 && d >= h + 1 && d <= 2 * h && h + 1 <= n && d >= 2) { for (i = 1; i <= h; i++) { cout << i << << i + 1 << endl; } i++; int f = 0; if (d - h > 0) { f = 1; cout << 1 << i << endl; int temp = d - h - 1; for (int j = 1; j <= temp; j++, i++) cout << i << << i + 1 << endl; } if (f == 1) i++; for (int j = 1; j <= (n - d - 1); j++, i++) { cout << 1 << i << endl; } } else if (n >= d + 1 && d == h && d <= 2 * h && h + 1 <= n && d >= 2) { for (i = 1; i <= h; i++) cout << i << << i + 1 << endl; i++; int k = i; for (int j = 1; j <= (n - k + 1); j++, i++) { cout << 2 << i << endl; } } else if (d == 1 && n == 2 && h == 1) { cout << 1 << 2 << endl; } else cout << -1 << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9 + 7; const long long MOD = 1e6 + 7; long long gcd(long long a, long long b) { if (a % b) return gcd(b, a % b); return b; } long long n, m; long long a[200005], c[6]; string s[200005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; long long k = *max_element(a, a + n); long long s = accumulate(a, a + n, 0LL); if (s % 2 || k > s / 2) cout << NO ; else cout << YES ; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m, k; cin >> n >> m >> k; std::vector<int> power(n, -1), school(n, -1), chosen(k, -1); int ans = 0; for (int i = 0; i < n; i++) { cin >> power[i]; } for (int i = 0; i < n; i++) { cin >> school[i]; } for (int i = 0; i < k; i++) { cin >> chosen[i]; } for (int i = 0; i < k; i++) { for (int j = 0; j < n; j++) { if (power[j] > power[chosen[i] - 1] && school[chosen[i] - 1] == school[j] && chosen[i] - 1 != j) { ans++; break; } } } cout << ans; return 0; } |
#include<bits/stdc++.h> using namespace std; #define int long long int n, k; int mod(int i){return (i%n+n)%n;} int q(int x){ x = mod(x) + 1; cout << ? << x << endl; int ret; cin >> ret; return ret; } int32_t main(){ cin >> n >> k; int i=0, j=0, x; int lo, hi; while(1){ x = q(j); if(x > k){lo = j-i-i, hi = j; break;} if(x < k){lo = j, hi = j+i+i; break;} j += i; ++i; } while(lo <= hi){ int mid = (lo+hi)/2; int x = q(mid); if(x == k){ cout << ! << mod(mid) + 1 << endl; exit(0); } if(x < k) lo = mid+1; else hi = mid-1; } } |
#include <bits/stdc++.h> using namespace std; const int N = 55; char arr[N][N] = {}; bool vis[N][N] = {}; int n, m; inline bool valid(int nx, int ny) { return (nx >= 1 && nx <= n && ny >= 1 && ny <= m && !vis[nx][ny] && arr[nx][ny] == # ); } void dfs(int xx, int yy) { vis[xx][yy] = true; for (int i = -1; i <= (int)1; ++i) { for (int j = -1; j <= (int)1; ++j) { if (j != i && (!j || !i)) { int nx = xx + i; int ny = yy + j; if (valid(nx, ny)) dfs(nx, ny); } } } } int main() { ios::sync_with_stdio(0), ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); ; cin >> n >> m; for (int i = 1; i <= (int)n; ++i) for (int j = 1; j <= (int)m; ++j) cin >> arr[i][j]; int z = 0; for (int x = 1; x <= (int)n; ++x) { for (int y = 1; y <= (int)m; ++y) { if (arr[x][y] == # ) { z++; arr[x][y] = . ; int cmp = 0; memset(vis, 0, sizeof(vis)); ; for (int i = 1; i <= (int)n; ++i) { for (int j = 1; j <= (int)m; ++j) { if (!vis[i][j] && arr[i][j] == # ) { dfs(i, j); ++cmp; } } } if (cmp >= 2) return cout << 1 << n , 0; arr[x][y] = # ; } } } if (z <= 2) return cout << -1 << n , 0; return cout << 2 << n , 0; return 0; } |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.