solution
stringlengths
52
181k
difficulty
int64
0
6
#include<bits/stdc++.h> #define For(a,b,c) for(int a=b;a<=c;++a) #define Dor(a,b,c) for(int a=b;a>=c;--a) #define CC(_...) fprintf(stderr,_) using namespace std; typedef long long LL; enum{N=307,E=607,P=998244353}; int n,k,hd[N],nt[E],to[E],F[N][N],G[N][N],tF[N][N],tG[N][N],sF[N][N],sG[N][N],H[N]; void fly(int u,int f=0) { For(i,0,n+1) F[u][i]=G[u][i]=0; G[u][0]=1; F[u][k]=1; For(i,0,n+1) { sF[u][i]=F[u][i],sG[u][i]=G[u][i]; if(i) (sF[u][i]+=sF[u][i-1])%=P,(sG[u][i]+=sG[u][i-1])%=P; } for(int e=hd[u];e;e=nt[e]) { int v=to[e]; if(v==f) continue; fly(v,u); For(i,0,n) { tF[u][i]=(1LL*(i?sF[u][i-1]:0)*F[v][i+1]+1LL*F[u][i]*sF[v][i+1]+1LL*F[u][i]*(i?sG[v][i-1]:0)+1LL*sG[u][i]*F[v][i+1])%P; tG[u][i]=(1LL*G[u][i]*sF[v][i]+1LL*G[u][i]*(i?sG[v][i-1]:0)+1LL*(i?sG[u][i-1]:0)*(i?G[v][i-1]:0)+(i?1LL*sF[u][i-1]*G[v][i-1]:0))%P; } For(i,0,n+1) { F[u][i]=sF[u][i]=tF[u][i],G[u][i]=sG[u][i]=tG[u][i]; tF[u][i]=tG[u][i]=0; if(i) (sF[u][i]+=sF[u][i-1])%=P,(sG[u][i]+=sG[u][i-1])%=P; } } } int main() { scanf("%d",&n); int e=0; For(i,2,n) { int x,y; scanf("%d%d",&x,&y); nt[++e]=hd[x],hd[x]=e,to[e]=y; nt[++e]=hd[y],hd[y]=e,to[e]=x; } For(i,1,n) { k=i; fly(1); H[i]=sF[1][n]-1; } int ans=n-1; For(i,1,n-1) ans=(ans+1LL*(H[i+1]-H[i]+P)*i)%P; For(i,1,n) { if(ans&1) ans+=P; ans>>=1; } printf("%d",ans); return 0; }
6
#include <bits/stdc++.h> const long long MaxN = 205; const long long lim = 1e18 + 5; template <class t> inline void read(t &s) { s = 0; register long long f = 1; register char c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) s = (s << 3) + (s << 1) + (c ^ 48), c = getchar(); s *= f; return; } template <class t, class... A> inline void read(t &x, A &...a) { read(x); read(a...); } template <class t> inline void write(t x) { if (x < 0) putchar('-'), x = -x; long long buf[21], top = 0; while (x) buf[++top] = x % 10, x /= 10; if (!top) buf[++top] = 0; while (top) putchar(buf[top--] ^ '0'); return; } inline void setIn(std::string s) { freopen(s.c_str(), "r", stdin); return; } inline void setOut(std::string s) { freopen(s.c_str(), "w", stdout); return; } inline void setIO(std::string s = "") { setIn(s + ".in"); setOut(s + ".out"); return; } template <class t> inline bool checkmin(t &x, t y) { if (x > y) { x = y; return 1; } return 0; } template <class t> inline bool checkmax(t &x, t y) { if (x < y) { x = y; return 1; } return 0; } inline long long lowbit(long long x) { return x & (-x); } long long a[MaxN][MaxN], p[MaxN], t[MaxN], n, m, K, q; long long b[MaxN], f[MaxN][MaxN]; inline void add(long long &x, long long y) { (x += y) >= lim && (x = lim); } inline long long calc() { std::memset(f, 0, sizeof f); f[0][0] = 1; for (long long i = 1; i <= q; ++i) { if (b[i] >= 0) for (long long j = 1; j <= q; ++j) add(f[i][j], f[i - 1][j - 1]); if (b[i] <= 0) for (long long j = 0; j < q; ++j) add(f[i][j], f[i - 1][j + 1]); } return f[q][0]; } signed main(void) { read(n, m, K); for (long long i = 1; i <= n; ++i) for (long long j = 1; j <= m; ++j) read(a[i][j]); { std::vector<std::pair<long long, long long> > dr; for (long long i = 1; i <= n; ++i) for (long long j = 1; j <= m; ++j) dr.push_back(std::make_pair(a[i][j], i + j - 1)); std::sort((dr).begin(), (dr).end()); register long long cnt = 0; for (auto i : dr) if (!p[i.second]) p[i.second] = ++cnt; for (long long i = 1; i <= cnt; ++i) t[p[i]] = i; for (long long i = 1; i <= cnt; ++i) p[i] = t[i]; } q = n + m - 1; register long long sum = 0; for (long long i = 1; i <= q; ++i) { b[p[i]] = 1; register long long t = calc(); if (sum + t >= K) continue; b[p[i]] = -1; sum += t; } for (long long i = 1; i <= n; ++i, std::puts("")) for (long long j = 1; j <= m; ++j) std::putchar(b[i + j - 1] > 0 ? '(' : ')'); return 0; }
3
#include <iostream> using namespace std; int main(){ int N; cin >>N; cout << (double)((N+1)/2)/N; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t = 0, n, i, k, p, q; { cin >> n >> k; while (n--) { long long int arr[7] = {0}; cin >> p >> q; arr[p]++; arr[q]++; arr[7 - p]++; arr[7 - q]++; arr[k]++; k = 7 - k; arr[k]++; for (i = 0; i < 7; i++) { if (arr[i] > 1) { t = 1; break; } } } if (t == 0) cout << "YES" << endl; else cout << "NO" << endl; } return 0; } long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long int power(long long int x, long long int y, long long int m) { if (y == 0) return 1; long long int p = power(x, y / 2, m) % m; p = (p * p) % m; return (y % 2 == 0) ? p : (x * p) % m; } long long int modInverse(long long int a, long long int m) { return power(a, m - 2, m); }
1
#include <iostream> #include <vector> using namespace std; int N; bool Find; vector<int> res,sign; void dfs(int ind) { if (Find) { return; } if (ind == N-1) { if ((res[0] == res[ind]*sign[ind]*res[ind-1]) && (res[1] == res[0]*sign[0]*res[ind])) { for (int i = 0; i < N; ++i) { if (res[i] > 0) { cout << "S"; } else { cout << "W"; } } cout << endl; Find = true; } return; } if (ind < 2) { res[ind] = 1; if (ind == 1) { res[ind+1] = sign[ind]*res[ind-1]; } dfs(ind+1); res[ind] = -1; if (ind == 1) { res[ind+1] = -sign[ind]*res[ind-1]; } dfs(ind+1); } else { res[ind+1] = res[ind]*sign[ind]*res[ind-1]; dfs(ind+1); } } int main(void) { cin >> N; sign = vector<int>(N); for (int i = 0; i < N; ++i) { char c; cin >> c; if (c == 'o') { sign[i] = 1; } else { sign[i] = -1; } } res = vector<int>(N); Find = false; dfs(0); if (!Find) { cout << -1 << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long n, m, t, a[25][25], b[25], c[1 << 22]; char s[200005]; long long sum[1 << 22]; inline void go(int x1, int y1010101010101, int x2, int v) { sum[x1] += v; sum[y1010101010101] -= v; sum[x2] -= v; sum[y1010101010101 & x2] += v; } bool used[25]; int to = 0; set<int> st[25]; int main() { scanf("%d%d%d%s", &n, &m, &t, s + 1); for (int i = 1; i <= n; i++) used[s[i] - 'A'] = 1; for (int i = 0; i < m; i++) scanf("%lld", &b[i]); for (int i = 0; i < m; i++) if (used[i]) to |= (1 << i); for (int i = 1; i < (1 << m); i++) c[i] = b[(int)(log2(i & (-i)))] + c[i ^ (i & (-i))]; for (int i = 0; i < m; i++) for (int j = 0; j < m; j++) scanf("%lld", &a[i][j]); for (int i = 1; i <= n; i++) st[s[i] - 'A'].insert(i); for (int i = 1; i <= n; i++) { st[s[i] - 'A'].erase(st[s[i] - 'A'].find(i)); vector<pair<int, int> > v; for (int j = 0; j < m; j++) { if (!st[j].empty()) { v.push_back(make_pair(*st[j].begin(), j)); } } sort(v.begin(), v.end()); int msk = (1 << m) - 1; for (int j = 0; j < v.size(); j++) { msk ^= 1 << v[j].second; go(msk | (1 << v[j].second), (msk | (1 << v[j].second)) ^ (1 << (s[i] - 'A')), msk, a[s[i] - 'A'][v[j].second]); if (v[j].second == s[i] - 'A') break; } } for (int i = 0; i < m; i++) for (int j = (1 << m) - 1; ~j; j--) { if (!(j >> i & 1)) sum[j] += sum[j | (1 << i)]; } int ans = 0; for (int i = 0; i < (1 << m); i++) if (sum[((1 << m) - 1) ^ i] + c[i] <= t && (to | i) == to && (to & i) != to) ans++; printf("%d\n", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main(){ int n; scanf("%d", &n); vector<int> a(n); for(int i = 0; i < n; i++) a[i] = i + 1; do { for(int i = 0; i < n - 1; i++) printf("%d ", a[i]); printf("%d\n", a[n-1]); } while(next_permutation(a.begin(), a.end())); }
0
#include <bits/stdc++.h> long long dx[4] = {0, -1, 0, 1}; long long dy[4] = {1, 0, -1, 0}; using namespace std; class pa3 { public: long long x, y, z; pa3(long long x = 0, long long y = 0, long long z = 0) : x(x), y(y), z(z) {} bool operator<(const pa3 &p) const { if (x != p.x) return x < p.x; if (y != p.y) return y < p.y; return z < p.z; } bool operator>(const pa3 &p) const { if (x != p.x) return x > p.x; if (y != p.y) return y > p.y; return z > p.z; } bool operator==(const pa3 &p) const { return x == p.x && y == p.y && z == p.z; } bool operator!=(const pa3 &p) const { return !(x == p.x && y == p.y && z == p.z); } }; class pa4 { public: double x; long long y, z, w; pa4(double x = 0, long long y = 0, long long z = 0, long long w = 0) : x(x), y(y), z(z), w(w) {} bool operator<(const pa4 &p) const { if (x != p.x) return x < p.x; if (y != p.y) return y < p.y; if (z != p.z) return z < p.z; return w < p.w; } bool operator>(const pa4 &p) const { if (x != p.x) return x > p.x; if (y != p.y) return y > p.y; if (z != p.z) return z > p.z; return w > p.w; } bool operator==(const pa4 &p) const { return x == p.x && y == p.y && z == p.z && w == p.w; } }; class pa2 { public: long long x, y; pa2(long long x = 0, long long y = 0) : x(x), y(y) {} pa2 operator+(pa2 p) { return pa2(x + p.x, y + p.y); } pa2 operator-(pa2 p) { return pa2(x - p.x, y - p.y); } bool operator<(const pa2 &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator>(const pa2 &p) const { return x != p.x ? x > p.x : y > p.y; } bool operator==(const pa2 &p) const { return abs(x - p.x) == 0 && abs(y - p.y) == 0; } bool operator!=(const pa2 &p) const { return !(abs(x - p.x) == 0 && abs(y - p.y) == 0); } }; class Point { public: double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator+(Point p) { return Point(x + p.x, y + p.y); } Point operator-(Point p) { return Point(x - p.x, y - p.y); } Point operator*(double a) { return Point(x * a, y * a); } Point operator/(double a) { return Point(x / a, y / a); } double absv() { return sqrt(norm()); } double norm() { return x * x + y * y; } bool operator<(const Point &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator==(const Point &p) const { return fabs(x - p.x) < (1e-7) && fabs(y - p.y) < (1e-7); } }; struct Segment { Point p1, p2; }; double dot(Point a, Point b) { return a.x * b.x + a.y * b.y; } double cross(Point a, Point b) { return a.x * b.y - a.y * b.x; } bool parareru(Point a, Point b, Point c, Point d) { return abs(cross(a - b, d - c)) < (1e-7); } double distance_ls_p(Point a, Point b, Point c) { if (dot(b - a, c - a) < (1e-7)) return (c - a).absv(); if (dot(a - b, c - b) < (1e-7)) return (c - b).absv(); return abs(cross(b - a, c - a)) / (b - a).absv(); } bool is_intersected_ls(Segment a, Segment b) { if (a.p1 == b.p1 || a.p2 == b.p1 || a.p1 == b.p2 || a.p2 == b.p2) return false; if (parareru((a.p2), (a.p1), (a.p1), (b.p2)) && parareru((a.p2), (a.p1), (a.p1), (b.p1))) { if (dot(a.p1 - b.p1, a.p1 - b.p2) < (1e-7)) return true; if (dot(a.p2 - b.p1, a.p2 - b.p2) < (1e-7)) return true; if (dot(a.p1 - b.p1, a.p2 - b.p1) < (1e-7)) return true; if (dot(a.p1 - b.p2, a.p2 - b.p2) < (1e-7)) return true; return false; } else return (cross(a.p2 - a.p1, b.p1 - a.p1) * cross(a.p2 - a.p1, b.p2 - a.p1) < (1e-7)) && (cross(b.p2 - b.p1, a.p1 - b.p1) * cross(b.p2 - b.p1, a.p2 - b.p1) < (1e-7)); } double segment_dis(Segment a, Segment b) { if (is_intersected_ls(a, b)) return 0; double r = distance_ls_p(a.p1, a.p2, b.p1); r = min(r, distance_ls_p(a.p1, a.p2, b.p2)); r = min(r, distance_ls_p(b.p1, b.p2, a.p2)); r = min(r, distance_ls_p(b.p1, b.p2, a.p1)); return r; } Point intersection_ls(Segment a, Segment b) { Point ba = b.p2 - b.p1; double d1 = abs(cross(ba, a.p1 - b.p1)); double d2 = abs(cross(ba, a.p2 - b.p1)); double t = d1 / (d1 + d2); return a.p1 + (a.p2 - a.p1) * t; } string itos(long long i) { ostringstream s; s << i; return s.str(); } long long gcd(long long v, long long b) { if (v > b) return gcd(b, v); if (v == b) return b; if (b % v == 0) return v; return gcd(v, b % v); } double distans(double x1, double y1, double x2, double y2) { double rr = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); return sqrt(rr); } long long beki(long long wa, long long rr, long long warukazu) { if (rr == 0) return 1ll; if (rr == 1) return wa % warukazu; if (rr % 2 == 1) return (beki(wa, rr - 1, warukazu) * wa) % warukazu; long long zx = beki(wa, rr / 2, warukazu); return (zx * zx) % warukazu; } long long a[1010][1010] = {0}; long long ue[1010][1010] = {0}; long long si[1010][1010] = {0}; long long mi[1010][1010] = {0}; long long hi[1010][1010] = {0}; long long ans[1010][1010] = {0}; long long ban[1010][1010] = {0}; long long hikaku[1010][1010] = {0}; vector<pa3> ve; signed main() { cin.tie(0); ios::sync_with_stdio(false); long long n, m; cin >> n >> m; for (long long i = 1; i <= n; i++) { string s; cin >> s; s = "s" + s; for (long long j = 1; j <= m; j++) { if (s[j] == '*') a[i][j] = 1; } } for (long long j = 1; j <= m; j++) { for (long long i = 1; i <= n; i++) { if (!a[i][j]) ue[i][j] = 0; else ue[i][j] = ue[i - 1][j] + 1; } } for (long long j = 1; j <= m; j++) { for (long long i = n; i > 0; i--) { if (!a[i][j]) si[i][j] = 0; else si[i][j] = si[i + 1][j] + 1; } } for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= m; j++) { if (!a[i][j]) hi[i][j] = 0; else hi[i][j] = hi[i][j - 1] + 1; } } for (long long i = 1; i <= n; i++) { for (long long j = m; j > 0; j--) { if (!a[i][j]) mi[i][j] = 0; else mi[i][j] = mi[i][j + 1] + 1; } } for (long long i = 1; i <= n; i++) for (long long j = 1; j <= m; j++) { if (!a[i][j]) ans[i][j] = 0; else { long long d = min({mi[i][j], si[i][j], ue[i][j], hi[i][j]}) - 1; ans[i][j] = d; if (d) { ban[i - d][j]++; ban[i + d + 1][j]--; ban[i - d][j + 1]--; ban[i + d + 1][j + 1]++; ban[i][j - d]++; ban[i][j + d + 1]--; ban[i + 1][j - d]--; ban[i + 1][j + d + 1]++; ve.push_back((pa3){i, j, d}); } } } for (long long i = 1; i <= n; i++) for (long long j = 1; j <= m; j++) { hikaku[i][j] = hikaku[i - 1][j] + hikaku[i][j - 1] - hikaku[i - 1][j - 1] + ban[i][j]; if (hikaku[i][j] == 0 && a[i][j] == 1) { cout << -1 << endl; return 0; } } cout << ve.size() << endl; for (auto v : ve) { cout << v.x << " " << v.y << " " << v.z << "\n"; } return 0; }
5
#include <bits/stdc++.h> const int d4x[4] = {-1, 0, 1, 0}, d4y[4] = {0, 1, 0, -1}; const int d8x[8] = {-1, -1, -1, 0, 1, 1, 1, 0}, d8y[8] = {-1, 0, 1, 1, 1, 0, -1, -1}; using namespace std; int main() { int n; string s{}; cin >> n >> s; vector<int> v{}; for (int i{}; i < n; i++) { int a; cin >> a; v.push_back(a); } int mindist = -1; for (int i{1}; i < s.length(); i++) { if (s[i] == 'L' and s[i - 1] == 'R') { if (mindist == -1 or v[i] - v[i - 1] < mindist) { mindist = v[i] - v[i - 1]; } } } if (mindist == -1) { cout << -1 << '\n'; } else { cout << mindist / 2; } }
1
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; const int MOD = 1e9 + 7; int n; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout << fixed; cin >> n; int dp[] = {1, 0}; for (int i = 1; i < n; i++) dp[i % 2] = (dp[i % 2] + dp[1 - i % 2] + 1) % MOD; cout << (dp[0] + dp[1]) % MOD << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { cout << "YES\nNO\nNO\nYES\nNO\nNO\nNO\nNO\nNO\nYES\nYES\nYES\nYES\nYES\nYES\nY" "ES\nNO\nYES\nNO\nYES\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO\nYES\nYES\nNO" "\nYES\nNO\nYES\nNO\nNO\nYES\nYES\nNO\nYES\nYES\nNO\nYES\nYES\nNO\nYES" "\nYES\nNO\nNO\nYES\nNO\nNO\nYES\nYES\nYES\nYES\nNO\nNO\nNO\nNO\nYES\n" "NO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nNO\nYES\nNO\nNO\nNO\nNO\nNO\nYES\nYES" "\nNO\nNO\nYES\nNO\nNO\nNO\nNO\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nYES\nYES" "\nNO\nNO\nNO\nYES\nYES\nYES\nYES\nYES\nNO\nNO\nYES\nYES\nNO\nYES\nNO" "\nYES\nNO\nNO\nNO\nNO\nYES\nYES\nYES\nYES\nNO\nNO\nNO\nYES\nNO\nYES\n" "YES\nYES\nNO\nYES\nNO\nNO\nNO\nYES\nNO\nNO\nNO\nYES\nNO\nYES\nYES\nYE" "S\nYES\nYES\nYES\nYES\nYES\nYES\nNO\nYES\nNO\nNO\nNO\nNO\nNO\nYES\nNO" "\nNO\nYES\nNO\nYES\nNO\nYES\nNO\nYES\nNO\nNO\nYES\nYES\nNO\nNO\nYES\n" "NO\nNO\nNO\nNO\nYES\nNO\nYES\nNO\nNO\nYES\nNO\nYES\nYES\nNO\nNO\nNO\n" "NO\nYES\nYES\nNO\nYES\nNO\nYES\nYES\nYES\nNO\nYES\nYES\nNO\nNO\nNO\nY" "ES\nYES\nNO\nNO\nYES\nNO\nNO\nYES\nNO\nNO\nNO\nYES\nYES\nNO\nYES\n"; }
5
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; void solve() { int n, m; cin >> n >> m; int p[n]; for (int i = 0; i <= n - 1; i++) cin >> p[i]; set<int> st[n + 1]; for (int i = 1; i <= m; i++) { int a, b; cin >> a >> b; st[a].insert(b); } set<int> s; vector<bool> flag(n + 1, false); int ans = 0; for (int i = n - 2; i >= 0; i--) { if (st[p[i]].find(p[n - 1]) != st[p[i]].end()) { int cnt = 0; for (auto j : st[p[i]]) { if (flag[j]) cnt++; } if (cnt == ((int)(s).size())) ans++; else { flag[p[i]] = true; s.insert(p[i]); } } else { flag[p[i]] = true; s.insert(p[i]); } } cout << ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; solve(); }
4
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; int add(int x, int y) { x += y; if (x >= mod) x -= mod; return x; } int p[2][100001]; int get(int i, int l, int r) { if (r < 0) return 0; l = max(l, 0); return add(p[i][r], mod - (l == 0 ? 0 : p[i][l - 1])); } void solution() { int n, m; cin >> n >> m; vector<int> f(m + 1); vector<int> l(n), r(n); for (int i = 0; i < n; i++) cin >> l[i] >> r[i]; for (int d = 1; d <= m; d++) { vector<int> L(n), R(n); bool fail = false; for (int i = 0; i < n; i++) { L[i] = (l[i] + d - 1) / d; R[i] = r[i] / d; if (R[i] < L[i]) fail = true; } if (fail) continue; int bound = m / d; for (int i = 0; i <= 1; i++) for (int j = 0; j <= bound; j++) p[i][j] = 0; p[0][0] = 1; for (int i = 1; i <= bound; i++) p[0][i] = 1; int cur = 0, nxt = 1; for (int i = 0; i < n; i++) { for (int j = 0; j <= bound; j++) p[nxt][j] = add(p[nxt][j], get(cur, j - R[i], j - L[i])); for (int j = 1; j <= bound; j++) p[nxt][j] = add(p[nxt][j], p[nxt][j - 1]); for (int j = 0; j <= bound; j++) p[cur][j] = 0; swap(cur, nxt); } f[d] = p[cur][bound]; } for (int i = m; i >= 1; i--) { for (int j = i + i; j <= m; j += i) f[i] = add(f[i], mod - f[j]); } cout << f[1]; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int tc = 1; while (tc--) { solution(); cout << "\n"; } }
5
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int INF=1e9; int ans=INF; int n; int a[20][2],p[20],t[20],b[20][2]; void jud(int s){ for(int i=0;i<n;++i){ if(s&(1<<i)) p[i]=1; else p[i]=0; t[i]=a[i][p[i]]; b[i][0]=a[i][0]; b[i][1]=a[i][1]; } sort(t,t+n); bool flag=0; int val=0; for(int i=0;i<n;++i){ int j=i; for(;j<n;++j) if(b[j][p[j]]==t[i]&&((j-i)&1)==p[j]) break; if(j>=n) { flag=1; break; } for(;j>i;--j){ swap(b[j][0],b[j-1][1]); swap(b[j][1],b[j-1][0]); swap(p[j],p[j-1]); p[j]^=1;p[j-1]^=1; ++val; } } if(flag) return; ans=min(ans,val); } int main(){ scanf("%d",&n); for(int i=0;i<n;++i) scanf("%d",&a[i][0]); for(int i=0;i<n;++i) scanf("%d",&a[i][1]); for(int s=0;s<(1<<n);++s) jud(s); printf("%d\n",ans==INF?-1:ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n, n) { vector<int> p(n); for (auto& i : p) cin >> i; sort(p.begin(), p.end()); int ans = 1<<28; for (int i = 0; i < n-1; i++) { ans = min(ans, p[i+1]-p[i]); } cout << ans << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; bool blocks[1000008]; bool ans[1000008]; const int N = 1000001; int n; void readInput() { scanf("%d", &n); fill(blocks, blocks + N + 2, false); for (int _17 = 0; _17 < ((int)n); _17++) { int x; scanf("%d", &x); blocks[x] = true; } } vector<int> misses; int nMisses = 0; void tc() { readInput(); printf("%d\n", n); fill(ans, ans + N + 2, false); for (int i = 1; i < ((int)500001); i++) { if (blocks[i] && !blocks[N - i]) { ans[N - i] = true; } else if (!blocks[i] && blocks[N - i]) { ans[i] = true; } else if (blocks[i] && blocks[N - i]) { nMisses++; } else if (!blocks[i] && !blocks[N - i]) { misses.push_back(i); } else throw 123948; } for (int _41 = 0; _41 < ((int)nMisses); _41++) { int i = misses.back(); ans[i] = true; ans[N - i] = true; misses.pop_back(); } bool first = true; for (int i = 1; i < ((int)N); i++) { if (ans[i]) { if (first) first = false; else printf(" "); printf("%d", i); } } printf("\n"); } int main() { tc(); }
2
// warm heart, wagging tail,and a smile just for you! // // β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’β–’ // β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’ // β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’ // β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ // β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’ // β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’ // β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ // β–’β–ˆβ–ˆβ–ˆβ–ˆβ–’β–’β–’β–’β–’β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’ // β–ˆβ–ˆβ–ˆβ–’β–’β–’β–’β–’β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’β–’β–’ // β–’β–ˆβ–ˆβ–’β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’β–’β–’β–’β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ // β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’β–’β–’β–’β–’β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’ // β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’β–’β–’β–’β–’β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ // β–’β–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–’β–’β–’β–’β–’β–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–’ // β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’ β–ˆβ–ˆβ–ˆβ–ˆ β–’β–’β–’β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’ // β–’β–ˆβ–ˆβ–’β–’β–’β–’β–’ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–’β–’β–’β–ˆβ–ˆβ–’ // β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–’β–’β–’β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆ // β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ // β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’β–’β–’β–’β–’β–’ β–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ // β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–’β–’β–’β–’β–’β–’β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’ // β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’ // β–ˆβ–ˆβ–ˆβ–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’ // β–’β–’β–’β–’β–’β–’β–’β–’β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’β–’β–’β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ // β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’β–’β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’ // β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’β–’β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’ // β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆ // β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’ // β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’β–’β–’β–’β–’β–’β–’β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–’β–’β–’ // #include "bits/stdc++.h" using namespace std; #define MOD 1000000007 //#define MOD 998244353 const double EPS = 1e-9; #define INF (1LL<<60) #define D double #define fs first #define sc second #define int long long #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define RFOR(i,a,b) for(int i = (b-1);i>=(a);--i) #define REP(i,n) FOR(i,0,(n)) #define RREP(i,n) RFOR(i,0,(n)) #define ITR(itr,mp) for(auto itr = (mp).begin(); itr != (mp).end(); ++itr) #define RITR(itr,mp) for(auto itr = (mp).rbegin(); itr != (mp).rend(); ++itr) #define range(i,a,b) ((a)<=(i) && (i)<(b)) #define debug(x) cout << #x << " = " << (x) << endl; #define SP << " " << typedef pair<int,int> P; typedef vector<int> vec; typedef vector<vector<int>> mat; signed main(){ ios::sync_with_stdio(false); cin.tie(0); int n,q; cin >> n >> q; REP(_,q){ int u,v; cin >> u >> v; set<int> st; if(n == 1){ cout << min(u,v) << "\n"; continue; } while(u){ st.insert(u); u = (u+n-2)/n; } while(v){ if(st.find(v) != st.end()){ cout << v << "\n"; break; } v = (v+n-2)/n; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 500; const long long mod = 1e9 + 7; const long long cmod = 998244353; const long long inf = 1LL << 61; const int M = 1e6 + 500; const long long int ths = 1LL << 40; const int NN = 5e3 + 6; void solve() { long long int n, m; cin >> n >> m; long long int a[n], b[m]; map<long long int, long long int> ma; vector<long long int> v; stack<long long int> s; for (int i = 0; i < n; i++) { cin >> a[i]; ma[i] = a[i]; } for (int i = n - 1; i >= 0; i--) { s.push(a[i]); } for (int i = 0; i < m; i++) { cin >> b[i]; } set<long long int> st; long long int ans = 0; for (int i = 0; i < m; i++) { long long int x = b[i]; if (st.find(x) != st.end()) { ans++; st.erase(x); continue; } long long int cnt = (long long int)st.size(); while (!s.empty()) { long long int f = s.top(); s.pop(); if (f == x) { ans += 2 * cnt; ans++; break; } else { cnt++; st.insert(f); } } } cout << ans; cout << '\n'; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; cin >> t; while (t--) { solve(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; struct edge { int y; edge* next; }; edge* new_edge() { static edge a[600005]; static int top = 0; return &a[top++]; } edge* li[300005]; void inserts(int x, int y) { edge* t = new_edge(); t->y = y; t->next = li[x]; li[x] = t; } void insert_edge(int x, int y) { inserts(x, y); inserts(y, x); } const int modo = 998244353; int vis[300005]; int temp[300005]; int mul1[300005]; int mul2[300005]; int dp[300005][3]; void dfs(int x) { vis[x] = 1; edge* t; for (t = li[x]; t != 0; t = t->next) { if (vis[t->y] == 1) { continue; } dfs(t->y); } dp[x][1] = 1; dp[x][2] = 1; for (t = li[x]; t != 0; t = t->next) { if (vis[t->y] == 1) { continue; } dp[x][1] = (long long)dp[x][1] * (dp[t->y][0] * 2 % modo + dp[t->y][2]) % modo; dp[x][2] = (long long)dp[x][2] * (dp[t->y][0] + dp[t->y][2]) % modo; } int cnt = 0; for (t = li[x]; t != 0; t = t->next) { if (vis[t->y] == 1) { continue; } temp[cnt++] = t->y; } int i; mul1[0] = 1; for (i = 0; i < cnt; i++) { mul1[i + 1] = (long long)mul1[i] * (dp[temp[i]][0] * 2 % modo + dp[temp[i]][2]) % modo; } mul2[cnt] = 1; for (i = cnt - 1; i >= 0; i--) { mul2[i] = (long long)mul2[i + 1] * (dp[temp[i]][0] * 2 % modo + dp[temp[i]][2]) % modo; } for (i = 0; i < cnt; i++) { dp[x][0] = (dp[x][0] + (long long)mul1[i] * mul2[i + 1] % modo * dp[temp[i]][1]) % modo; } vis[x] = 2; } int main() { int n; scanf("%d", &n); int i; for (i = 1; i < n; i++) { int x, y; scanf("%d%d", &x, &y); x--; y--; insert_edge(x, y); } dfs(0); printf("%d\n", (dp[0][0] + dp[0][2]) % modo); return 0; }
3
#include "bits/stdc++.h" #include<unordered_map> #include<unordered_set> #pragma warning(disable:4996) using namespace std; using ld = long double; template<class T> using Table = vector<vector<T>>; const ld eps=1e-9; //// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\b.txt" int main() { int Q; cin >> Q; while (Q--) { int N; cin >> N; string st; cin >> st; vector<vector<int>>needs(2, vector<int>(N + 1)); for (int i = 0; i < 4 * N; ++i) { int num(i); int row = num>= 2 * N; num %= (2 * N); int col = (num + 1) / 2; needs[row][col] |= (st[i] == 'Y'); } vector<vector<int>>dp(N + 2, vector<int>(3,100000)); dp[0][0] = 0; for (int i = 0; i <= N; ++i) { if (!needs[0][i] && !needs[1][i]) { for (int f = 0; f < 3; ++f) { for (int t = 0; t < 3; ++t) { dp[i + 1][t] = min(dp[i + 1][t], dp[i][f] + 1+abs(f - t)); } } } else if (!needs[0][i] && needs[1][i]) { int nums[3][3] = { {3,2,2}, {2,1,1}, {2,1,1}, }; for (int f = 0; f < 3; ++f) { for (int t = 0; t < 3; ++t) { dp[i + 1][t] = min(dp[i + 1][t], dp[i][f] + 1 + nums[f][t]); } } } else if (needs[0][i] && !needs[1][i]) { int nums[3][3] = { { 1,1,2 }, { 1,1,2 }, { 2,2,3 }, }; for (int f = 0; f < 3; ++f) { for (int t = 0; t < 3; ++t) { dp[i + 1][t] = min(dp[i + 1][t], dp[i][f] + 1 + nums[f][t]); } } } else { int nums[3][3] = { { 3,2,2 }, { 2,2,2 }, { 2,2,3 }, }; for (int f = 0; f < 3; ++f) { for (int t = 0; t < 3; ++t) { dp[i + 1][t] = min(dp[i + 1][t], dp[i][f] + 1 + nums[f][t]); } } } } cout << -1+dp[N + 1][0] << endl; } return 0; }
0
#include <bits/stdc++.h> typedef struct point { double x; double y; } point; typedef struct segment { point a; point b; } segment; const double eps = 0.000000001; const double inf = 10e25; int eq(double a, double b) { return (fabs(a - b) < eps); } double min(double a, double b) { return (a < b) ? a : b; } double dist(point a, point b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } double point_seg_dist(point a, segment s) { double d1 = dist(a, s.a), d2 = dist(a, s.b), ans = min(d1, d2); if (eq(s.a.x, s.b.x)) { if ((a.y - s.a.y) * (a.y - s.b.y) < 0) { ans = min(ans, fabs(a.x - s.a.x)); } } if (eq(s.a.y, s.b.y)) { if ((a.x - s.a.x) * (a.x - s.b.x) < 0) { ans = min(ans, fabs(a.y - s.a.y)); } } return ans; } double seg_dist(segment p, segment q) { double d1 = min(point_seg_dist(p.a, q), point_seg_dist(p.b, q)), d2 = min(point_seg_dist(q.a, p), point_seg_dist(q.b, p)); return min(d1, d2); } double g[1002][1002], d[1002]; int n, col[1002]; segment s[1002]; point A, B; double a, b; void add(int i, int j, double dist) { if (dist < a || eq(dist, a)) { if (i != (n + 1) && j != (n + 1)) { g[i][j] = g[j][i] = a + b; } else { g[i][j] = g[j][i] = dist; } } else { g[i][j] = g[j][i] = inf; } } void read_n(void) { scanf("%lf%lf", &a, &b); scanf("%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y); scanf("%d", &n); int i, j; for (i = 0; i < n; ++i) { scanf("%lf%lf%lf%lf", &s[i].a.x, &s[i].a.y, &s[i].b.x, &s[i].b.y); add(i, (n), point_seg_dist(A, s[i])); add(i, (n + 1), point_seg_dist(B, s[i])); } for (i = 0; i < n; ++i) { for (j = i + 1; j < n; ++j) { add(i, j, seg_dist(s[i], s[j])); } } add((n), (n + 1), dist(A, B)); } void dijkstra(void) { int i, mink; double mind; for (i = 0; i < n + 2; ++i) { col[i] = 0; d[i] = inf; } d[(n)] = 0.0; while (1) { mind = inf; mink = -1; for (i = 0; i < n + 2; ++i) { if (col[i] == 0 && d[i] < mind) { mind = d[i]; mink = i; } } if (mink == -1) { break; } col[mink] = 1; for (i = 0; i < n + 2; ++i) { if (col[i] == 0 && d[i] > mind + g[mink][i]) { d[i] = mind + g[mink][i]; } } } if (eq(d[(n + 1)], inf)) { printf("-1"); } else { printf("%.10lf", d[(n + 1)]); } } int main(void) { read_n(); dijkstra(); return 0; }
1
#include <bits/stdc++.h> const int N = 200005; using namespace std; int n, a, b, c; int P[N][18], L[N]; int LCA(int x, int y) { if (L[x] < L[y]) swap(x, y); for (int j = 17; j >= 0; j--) if (L[x] - (1 << j) >= L[y]) x = P[x][j]; if (x == y) return x; for (int j = 17; j >= 0; j--) if (P[x][j] != P[y][j]) x = P[x][j], y = P[y][j]; return P[x][0]; } int dis(int x, int y) { int lca = LCA(x, y); return L[x] + L[y] - L[lca] - L[lca]; } int main() { scanf("%d", &n); a = b = 1; for (int v = 2, p; v <= n; v++) { scanf("%d", &p); L[v] = L[p] + 1; P[v][0] = p; for (int j = 1; j < 18; j++) P[v][j] = P[P[v][j - 1]][j - 1]; int x = dis(a, v); int y = dis(b, v); if (x >= y && c < x) b = v, c = x; else if (x < y && c < y) a = v, c = y; printf("%d ", c); } }
3
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 9; int N, M, root; int cyc[105], sz[105], h[105], up[105], cmb[105][105], dp[105][105], dpall[105], dpp[105]; int F, f[105]; vector<int> roots, nodes, edg[105]; int add(int x, int y) { x += y; if (x >= mod) x -= mod; return x; } int mul(int x, int y) { return (1LL * x * y) % mod; } void DFS(int nod, int fth) { if (f[nod] == F) return; f[nod] = F; for (auto nxt : edg[nod]) if (nxt != fth) DFS(nxt, nod); } void solveDFS(int nod, int fth) { cyc[nod] = 2; dp[nod][0] = 1; h[nod] = h[fth] + 1; sz[nod] = 0; for (auto nxt : edg[nod]) { if (nxt == fth || cyc[nxt] == 1) continue; solveDFS(nxt, nod); sz[nod] += sz[nxt]; for (int i = sz[nod]; i >= 1; i--) for (int j = 1; j <= i && j <= sz[nxt]; j++) dp[nod][i] = add(dp[nod][i], mul(cmb[i][j], mul(dp[nod][i - j], dp[nxt][j]))); } sz[nod]++; dp[nod][sz[nod]] = dp[nod][sz[nod] - 1]; } void solveupDFS(int nod, int fth) { for (auto nxt : edg[nod]) if (nxt != fth) { up[nxt] = up[nod]; int szz = sz[root] - sz[nod]; for (auto nxt2 : edg[nod]) if (nxt2 != fth && nxt2 != nxt) { up[nxt] = mul(up[nxt], mul(cmb[szz + sz[nxt2]][szz], dp[nxt2][sz[nxt2]])); szz += sz[nxt2]; } } for (auto nxt : edg[nod]) if (nxt != fth) solveupDFS(nxt, nod); } void sizeDFS(int nod, int fth) { cyc[nod] = 3; sz[nod] = 1; nodes.push_back(nod); for (auto nxt : edg[nod]) if (nxt != fth) { sizeDFS(nxt, nod); sz[nod] += sz[nxt]; } } int main() { scanf("%d%d", &N, &M); for (int i = 1; i <= M; i++) { int x, y; scanf("%d%d", &x, &y); edg[x].push_back(y); edg[y].push_back(x); } for (int i = 1; i <= N; i++) { for (auto nxt : edg[i]) { F++; DFS(nxt, i); for (auto nxt2 : edg[i]) if (nxt != nxt2 && f[nxt2] == F) { cyc[i] = 1; break; } if (cyc[i]) break; } } for (int i = 1; i <= N; i++) if (cyc[i]) for (auto nxt : edg[i]) if (!cyc[nxt]) roots.push_back(nxt); cmb[0][0] = 1; for (int i = 1; i <= N; i++) { cmb[i][0] = 1; for (int j = 1; j <= i; j++) cmb[i][j] = add(cmb[i - 1][j - 1], cmb[i - 1][j]); } dpall[0] = 1; for (auto r : roots) { solveDFS(r, 0); for (int i = N; i >= 1; i--) { for (int j = 1; j <= sz[r] && j <= i; j++) dpall[i] = add(dpall[i], mul(cmb[i][j], mul(dpall[i - j], dp[r][j]))); } } for (int i = 1; i <= N; i++) if (!cyc[i]) { nodes.clear(); sizeDFS(i, 0); root = i; if (sz[i] == 1) { dp[root][0] = dp[root][1] = 1; for (int i = N; i >= 1; i--) dpall[i] = add(dpall[i], mul(cmb[i][1], dpall[i - 1])); continue; } else if (sz[i] == 2) { dp[root][0] = 1, dp[root][1] = 2, dp[root][2] = 2; for (int i = N; i >= 1; i--) for (int j = 1; j <= 2 && j <= i; j++) dpall[i] = add(dpall[i], mul(cmb[i][j], mul(dpall[i - j], dp[root][j]))); continue; } for (auto x : nodes) if ((int)edg[x].size() > 1) root = x; solveDFS(root, 0); up[root] = 1; solveupDFS(root, 0); for (int i = 0; i <= N; i++) dpp[i] = dp[root][i]; for (auto x : nodes) if (h[x] > 1) { for (int i = 0; i < sz[x]; i++) dpp[i + sz[root] - sz[x]] = add(dpp[i + sz[root] - sz[x]], mul(cmb[i + sz[root] - sz[x]][i], mul(up[x], dp[x][i]))); dpp[sz[root]] = add(dpp[sz[root]], mul(cmb[sz[root] - 1][sz[x] - 1], mul(up[x], dp[x][sz[x] - 1]))); } for (int i = N; i >= 1; i--) for (int j = 1; j <= sz[root] && j <= i; j++) dpall[i] = add(dpall[i], mul(cmb[i][j], mul(dpall[i - j], dpp[j]))); } for (int i = 0; i <= N; i++) printf("%d\n", dpall[i]); return 0; }
4
#include <bits/stdc++.h> using namespace std; struct cells { int x, y, turns; }; vector<cells> start[15]; int n, m, p; int a[15]; string s[1005]; int dx[5] = {1, 0, -1, 0}; int dy[5] = {0, -1, 0, 1}; int ans[15] = {0}; queue<cells> q[15]; bool check = true; inline bool isval(int x, int y) { if (x >= 0 and x < n and y >= 0 and y < m and s[x][y] == '.') { return true; } return false; } inline void bfs(int x, int y, int player, int steps) { if (steps == 0) { return; } for (int i = 0; i < 4; i++) { int x1 = x + dx[i]; int y1 = y + dy[i]; if (isval(x1, y1)) { if (!check) { check = true; } s[x1][y1] = char(player + '0'); q[player].push((cells){x1, y1, steps - 1}); } } } int main() { cin >> n >> m >> p; for (int i = 1; i <= p; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> s[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (s[i][j] - '0' >= 1 and s[i][j] - '0' <= p) { start[s[i][j] - '0'].push_back((cells){i, j, a[s[i][j] - '0']}); } } } while (check) { check = false; for (int pl = 1; pl <= p; pl++) { for (int i = 0; i < start[pl].size(); i++) { q[pl].push(start[pl][i]); } start[pl].clear(); while (!q[pl].empty()) { int x = q[pl].front().x; int y = q[pl].front().y; int steps = q[pl].front().turns; if (steps == 0) { start[pl].push_back((cells){x, y, a[pl]}); } q[pl].pop(); bfs(x, y, pl, steps); } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if ((s[i][j] - '0') >= 1 and (s[i][j] - '0') <= p) { ans[s[i][j] - '0']++; } } } for (int i = 1; i <= p; i++) { cout << ans[i] << " "; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { string s1, s2; cin >> s1 >> s2; int len1 = s1.length(), len2 = s2.length(), x = 0, y = 0; for (int i = 0; i < len1; i++) if (s1[i] == '1') x++; for (int i = 0; i < len2; i++) if (s2[i] == '1') y++; cout << ((x >= y || (x + 1 == y && x % 2)) ? "YES" : "NO") << "\n"; return 0; }
3
// F - Intervals on Tree #include <bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; int64_t E = 0, V = 1LL*n*(n+1)*(n+2)/6; for(int i=0; i<n-1; ++i){ int u, v; cin>>u>>v; if(u > v) swap(u, v); E += 1LL*u*(n-v+1); } cout<< V-E <<endl; }
0
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; typedef long long ll; #define mod ((ll)(1e9)+7) vector<ll> v(2*(ll)(1e5+1),0); vector<ll> v_inv(2*(ll)(1e5+1),0); ll pmod(ll n, ll p){ ll m=n%mod; if(p==0) return 1; ll ans=pmod((m*m)%mod, p/2)%mod; if(p%2==1) ans=ans*m%mod; return ans; } void factorial(ll n){ v[0]=1; for(ll i=1; i<=n; i++){ v[i]=v[i-1]*i%mod; } return; } void factorial_inv(ll n){ for(ll i=0; i<=n; i++){ v_inv[i]=pmod(v[i],mod-2); } } ll binom(ll n, ll k){ if(n<k || n<0 || k<0) return 0; ll tmp=v_inv[k]*v_inv[n-k]%mod; return v[n]*tmp%mod; } int main(){ int n; cin >> n; factorial(n+1); factorial_inv(n+1); int c[n+1]; for(int i=1; i<=n; i++) c[i]=-1; int p, q; for(int i=0; i<n+1; i++){ int a; cin >> a; if(c[a]==-1) c[a]=i; else{ p=c[a]; q=i; } } for(int k=1; k<=n+1; k++){ ll ans=(binom(n+1, k)-binom(n-q+p, k-1))%mod; if(ans<0) ans+=mod; cout << ans << endl; } return 0; }
0
#include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; cout<<((n*(n+1))/2)<<endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cout << (long long int)pow(n - 2, 2); return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, m, k, fa[400005], ans; int get(int x) { return fa[x] == x ? x : fa[x] = get(fa[x]); } int main() { scanf("%d%d%d", &n, &m, &k); for (int i = (int)(1); i <= (int)(n + m); i++) fa[i] = i; int ans = n + m - 1; for (int i = (int)(1); i <= (int)(k); i++) { int x, y; scanf("%d%d", &x, &y); x = get(x); y = get(y + n); if (x != y) fa[x] = y, ans--; } printf("%d\n", ans); }
2
#include <bits/stdc++.h> using namespace std; static const double EPS = 1e-5; long long dp[3001][3001]; long long mi[3001]; int main(void) { int n; cin >> n; vector<pair<long long, long long> > input; for (int i = (0); i < (int)(n); ++i) { long long a, b; cin >> a >> b; input.push_back(pair<long long, long long>(a, b)); } sort(input.begin(), input.end()); for (int i = (0); i < (int)(3001); ++i) mi[i] = 100000000000000000; dp[0][0] = (long long)input[0].second; mi[0] = (long long)input[0].second; for (int i = (1); i < (int)(n); ++i) { for (int j = (0); j < (int)(i); ++j) { dp[i][j] = (long long)dp[i - 1][j] + (long long)abs((long long)input[i].first - input[j].first); mi[i] = (long long)min(mi[i], dp[i][j]); } dp[i][i] = (long long)mi[i - 1] + (long long)input[i].second; mi[i] = (long long)min(mi[i], dp[i][i]); } cout << mi[n - 1] << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<ll, ll> P; #define fi first #define se second #define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++) #define rep(i,n) repl(i,0,n) #define all(x) (x).begin(),(x).end() #define dbg(x) cout<<#x"="<<x<<endl #define mmax(x,y) (x>y?x:y) #define mmin(x,y) (x<y?x:y) #define maxch(x,y) x=mmax(x,y) #define minch(x,y) x=mmin(x,y) #define uni(x) x.erase(unique(all(x)),x.end()) #define exist(x,y) (find(all(x),y)!=x.end()) #define bcnt __builtin_popcountll #define INF 1e16 #define mod 1000000007 int main(){ cin.tie(0); ios::sync_with_stdio(false); ll n; cin>>n; vector<ll> a(n); rep(i,n)cin>>a[i]; ll sum=0,mn=INF; rep(i,n){ sum+=a[i]; minch(mn,a[i]); } if(n%2==1){ if(sum%2==0) cout<<"Second"<<endl; else cout<<"First"<<endl; }else{ if(mn%2==0&&sum%2==0)cout<<"Second"<<endl; else cout<<"First"<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int tc; cin >> tc; cout << (tc * 3) / 2; return 0; }
1
#include <bits/stdc++.h> using namespace std; using ll = long long; using pll = pair<ll, ll>; int main() { priority_queue<pll> pq; ll N, K, tmp; cin >> N >> K; vector<ll> times(N + 1); ll cost = 0; for (ll i = 1; i <= K; i++) { cin >> tmp; pll cost_place = make_pair(tmp, i); pq.push(cost_place); } for (ll i = K + 1; i <= N; i++) { cin >> tmp; pll cost_place = make_pair(tmp, i); pq.push(cost_place); pll most_exp = pq.top(); pq.pop(); times[most_exp.second] = i; cost += (i - most_exp.second) * most_exp.first; } for (ll i = N + 1; i <= N + K; i++) { pll most_exp = pq.top(); pq.pop(); times[most_exp.second] = i; cost += (i - most_exp.second) * most_exp.first; } cout << cost << endl; for (ll i = 1; i <= N; i++) { if (i > 1) { cout << " "; } cout << times[i]; } cout << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, k, pre = -1; priority_queue<pair<int, int> > q; const int M = 6666666; int mx[M], ls[M], rs[M], tg[M], sz; void pushup(int k) { mx[k] = max(mx[ls[k]], mx[rs[k]]) + tg[k]; } void upd(int &k, int l, int r, int x, int v) { if (!k) k = ++sz; if (l == r) { tg[k] = mx[k] = v; return; } int mid = (l + r) / 2; if (x <= mid) upd(ls[k], l, mid, x, v); else upd(rs[k], mid + 1, r, x, v); pushup(k); } void add(int &k, int l, int r, int x, int y) { if (!k || y < l || r < x) return; if (x <= l && r <= y) { mx[k]++; tg[k]++; return; } int mid = (l + r) / 2; add(ls[k], l, mid, x, y); add(rs[k], mid + 1, r, x, y); pushup(k); } int ask(int &k, int l, int r, int x, int y) { if (!k || y < l || r < x) return 0; if (x <= l && r <= y) return mx[k]; int mid = (l + r) / 2; int ans = ask(ls[k], l, mid, x, y); ans = max(ans, ask(rs[k], mid + 1, r, x, y)); return ans + tg[k]; } int rt; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) { int l, r; scanf("%d%d", &l, &r); l = max(0, l); q.push(pair<int, int>(-r, -l)); q.push(pair<int, int>(-l, 1)); } while (!q.empty()) { int l = -q.top().second, r = -q.top().first; q.pop(); if (l >= 0) add(rt, 0, 1e9, l, r); else { if (r == pre) continue; int u = ask(rt, 0, 1e9, pre, pre); int v = ask(rt, 0, 1e9, 0, r - k); if (l == -1 || u < v) { pre = r; upd(rt, 0, 1e9, r, v); if (r + k <= 1e9) q.push(pair<int, int>(-r - k, 2)); } } } printf("%d\n", mx[1]); }
2
#include <bits/stdc++.h> using namespace std; using int64 = long long; template< typename Monoid, typename OperatorMonoid = Monoid > struct LazySegmentTree { using F = function< Monoid(Monoid, Monoid) >; using G = function< Monoid(Monoid, OperatorMonoid, int) >; using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >; int sz; vector< Monoid > data; vector< OperatorMonoid > lazy; const F f; const G g; const H h; const Monoid M1; const OperatorMonoid OM0; LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &M1, const OperatorMonoid OM0) : f(f), g(g), h(h), M1(M1), OM0(OM0) { sz = 1; while(sz < n) sz <<= 1; data.assign(2 * sz, M1); lazy.assign(2 * sz, OM0); } void set(int k, const Monoid &x) { data[k + sz] = x; } void build() { for(int k = sz - 1; k > 0; k--) { data[k] = f(data[2 * k + 0], data[2 * k + 1]); } } void propagate(int k, int len) { if(lazy[k] != OM0) { if(k < sz) { lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]); lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]); } data[k] = g(data[k], lazy[k], len); lazy[k] = OM0; } } Monoid update(int a, int b, const OperatorMonoid &x, int k, int l, int r) { propagate(k, r - l); if(r <= a || b <= l) { return data[k]; } else if(a <= l && r <= b) { lazy[k] = h(lazy[k], x); propagate(k, r - l); return data[k]; } else { return data[k] = f(update(a, b, x, 2 * k + 0, l, (l + r) >> 1), update(a, b, x, 2 * k + 1, (l + r) >> 1, r)); } } Monoid update(int a, int b, const OperatorMonoid &x) { return update(a, b, x, 1, 0, sz); } Monoid query(int a, int b, int k, int l, int r) { propagate(k, r - l); if(r <= a || b <= l) { return M1; } else if(a <= l && r <= b) { return data[k]; } else { return f(query(a, b, 2 * k + 0, l, (l + r) >> 1), query(a, b, 2 * k + 1, (l + r) >> 1, r)); } } Monoid query(int a, int b) { return query(a, b, 1, 0, sz); } Monoid operator[](const int &k) { return query(k, k + 1); } }; int main() { int N; int64 A[100000], B[100000]; cin >> N; for(int i = 0; i < N; i++) cin >> A[i]; for(int i = 0; i < N; i++) cin >> B[i]; for(int i = 1; i < N; i++) A[i] += A[i - 1]; for(int i = 1; i < N; i++) B[i] += B[i - 1]; int64 ret = 0; for(int i = 0; i < N; i++) ret += llabs(B[i] - A[i]); int64 sa = A[N - 1] - B[N - 1]; ret -= sa; using Pi = pair< int64, int >; auto f = [](Pi x, Pi y) { return min(x, y); }; auto g = [](Pi x, int64 y, int len) { return Pi(x.first + y, x.second); }; auto h = [](int64 x, int64 y) { return x + y; }; const int64 INF = 1LL << 60; LazySegmentTree< Pi, int64 > rest(N - 1, f, g, h, {INF, -1}, 0); for(int i = 0; i < N - 1; i++) rest.set(i, {A[i] - B[i], i}); rest.build(); LazySegmentTree< Pi, int64 > cost(N - 1, f, g, h, {INF, -1}, 0); int64 sum = 0; for(int i = N - 2; i >= 0; i--) { --sum; cost.set(i, {sum, i}); } cost.build(); while(sa > 0) { Pi it; while((it = rest.query(0, N - 1)).first <= 0) { cost.update(0, it.second + 1, 2); rest.update(it.second, it.second + 1, INF - it.first); } auto best = cost.query(0, N - 1); if(best.first >= 0) break; int64 po = min(sa, rest.query(best.second, N - 1).first); rest.update(best.second, N - 1, -po); sa -= po; ret += best.first * po; } cout << ret << endl; }
0
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (a == 0) { return b; } if (b == 0) { return a; } return gcd(b % a, a); } int main() { long long ra, la, ta, rb, lb, tb; cin >> la >> ra >> ta; cin >> lb >> rb >> tb; long long g = gcd(ta, tb); if (ta > tb) { long long j = la, k = ra, l = ta; la = lb; ra = rb; ta = tb; lb = j; rb = k; tb = l; } lb -= la; rb -= la; long long buff = ((lb + tb) % tb) - lb; lb += buff; rb += buff; ra -= la; la -= la; long long bdif = 0; long long nf = (lb % g) - lb; lb += nf; rb += nf; bdif = max(bdif, 1 + min(rb, ra) - max(lb, la)); long long nl = ((lb + g - 1) % g) - g + 1; long long df = nl - lb; lb += df; rb += df; bdif = max(bdif, 1 + min(rb, ra) - max(lb, la)); cout << bdif << endl; }
3
#include<bits/stdc++.h> using namespace std; int main(){ string s,t; cin>>s>>t; int cnt=0; for(int i=0;i<3;i++){ if(s[i]==t[i]) cnt++; } cout<<cnt; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a; for (int i = 1; i <= n; i++) { int b; cin >> b; a.push_back(b); } sort(a.begin(), a.end()); int ans = 0; for (int i = 0; i < a.size() - 1; i += 2) { int dif = a[i + 1] - a[i]; ans += dif; } cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> char a[4000]; int n, q, k; char b[10] = {' ', 'R', 'G', 'B'}; char c[10] = {' ', 'G', 'B', 'R'}; char d[10] = {' ', 'B', 'R', 'G'}; int main(void) { scanf("%d", &q); while (q--) { scanf("%d%d", &n, &k); scanf("%s", a + 1); int Minsum = 1e8; for (register int i = 1; i + k - 1 <= n; ++i) { int tmp1 = 0, tmp2 = 0, tmp3 = 0; for (register int j = i; j <= i + k - 1; ++j) { if (b[(j - 1) % 3 + 1] != a[j]) ++tmp1; if (c[(j - 1) % 3 + 1] != a[j]) ++tmp2; if (d[(j - 1) % 3 + 1] != a[j]) ++tmp3; } Minsum = std::min(Minsum, std::min(std::min(tmp1, tmp2), tmp3)); } printf("%d\n", Minsum); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 5050, inf = 1 << 29; int n, t[maxn][3], will[3][7], solved[3], points[3], num_hack[3], score[maxn], sol = maxn, dp[2][100][100][100], can[maxn][3]; vector<int> v; inline int calc(int i, int j) { if (j == 0) return 0; return points[i] * (250 - j) / 250; } inline int calc2(int j) { int c = 500; if (n < 2 * j && j <= n) c = 500; else if (n < 4 * j && 2 * j <= n) c = 1000; else if (n < 8 * j && 4 * j <= n) c = 1500; else if (n < 16 * j && 8 * j <= n) c = 2000; else if (n < 32 * j && 16 * j <= n) c = 2500; else c = 3000; return c; } int solve(int first, int a, int b, int c) { memset(dp, 0, sizeof dp); for (int i = 0; i <= a; i++) for (int j = 0; j <= b; j++) for (int k = 0; k <= c; k++) dp[0][a][b][c] = (a >= 0 && b >= 0 && c >= 0) ? 0 : -inf; bool f = 0; for (int second = 1; second <= first; second++) { f = !f; for (int i = 0; i <= a; i++) { for (int j = 0; j <= b; j++) { for (int k = 0; k <= c; k++) { int &ret = dp[f][i][j][k]; ret = -inf; int z = v[second]; for (int ii = 0; ii <= can[z][0]; ii++) { for (int jj = 0; jj <= can[z][1]; jj++) { for (int kk = 0; kk <= can[z][2]; kk++) { if (i - ii < 0 || j - jj < 0 || k - kk < 0) continue; int sc = calc(0, t[z][0]) * (!ii) + calc(1, t[z][1]) * (!jj) + calc(2, t[z][2]) * (!kk); ret = max(ret, dp[!f][i - ii][j - jj][k - kk] + (sc <= score[0])); } } } } } } } return dp[f][a][b][c]; } int main() { memset(will, -1, sizeof will); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d %d %d", &t[i][0], &t[i][1], &t[i][2]); for (int j = 0; j < 3; j++) { if (t[i][j] < 0) { t[i][j] = -t[i][j]; can[i][j] = true; num_hack[j]++; } if (t[i][j]) solved[j]++; } } if (num_hack[0] + num_hack[1] + num_hack[2] >= 90) { printf("1\n"); return 0; } for (int i = 0; i < 3; i++) { int j, c; for (int k = 0; k <= num_hack[i]; k++) { j = solved[i] - k; c = calc2(j); will[i][c / 500 - 1] = k; } } for (int i = 0; i < 6; i++) { if (will[0][i] == -1) continue; for (int j = 0; j < 6; j++) { if (will[1][j] == -1) continue; for (int k = 0; k < 6; k++) { if (will[2][k] == -1) continue; points[0] = 500 * (i + 1); points[1] = 500 * (j + 1); points[2] = 500 * (k + 1); v.clear(); v.push_back(-1); for (int l = 0; l < n; l++) { score[l] = calc(0, t[l][0]) + calc(1, t[l][1]) + calc(2, t[l][2]); if (l == 0) score[l] += 100 * (will[0][i] + will[1][j] + will[2][k]); if (l && score[l] > score[0]) v.push_back(l); } sol = min(sol, (int)v.size() - solve(v.size() - 1, will[0][i], will[1][j], will[2][k])); } } } printf("%d\n", sol); return 0; }
5
#include <bits/stdc++.h> int main() { int x, y, a, b; scanf("%d%d%d%d", &x, &y, &a, &b); int min = 1; int temp; if (x <= y) { temp = x; x = y; y = temp; } for (int i = 2; i <= x;) { if (x % i == 0 && y % i == 0) { x = x / i; y = y / i; min = min * i; continue; } i++; } min = min * x * y; int sum = 0; if (min <= b) { b = b / min; int k; k = a / min; if (a % min == 0) k--; sum = b - k; } printf("%d\n", sum); }
1
#include <bits/stdc++.h> using namespace std; void ASS(bool bb) { if (!bb) { ++*(int*)0; } } #pragma comment(linker, "/STACK:106777216") vector<long long> u; const int N = 1 << 17; struct RSQ { int a[N * 2]; void Add(int pos) { for (int i = pos + N; i; i >>= 1) a[i]++; } int Sum(int L, int R) { int res = 0; for (L += N, R += N; L < R; L >>= 1, R >>= 1) { if (L & 1) res += a[L++]; if (R & 1) res += a[--R]; } return res; } void Clear() { memset(a, 0, sizeof(a)); } }; RSQ rsq; int FindGr(long long val) { int L = -1; int R = (int)u.size(); while (R - L > 1) { int m = (L + R) >> 1; if (val < u[m]) R = m; else L = m; } return R; } long long Cnt(long long val, const vector<long long>& sums, const vector<int>& vind) { rsq.Clear(); rsq.Add(vind[0]); long long res = 0; for (int i = 1; i < (int)sums.size(); i++) { res += rsq.Sum(0, FindGr(sums[i] - val)); rsq.Add(vind[i]); } return res; } int main() { int n; long long k; cin >> n >> k; vector<long long> a(n); for (int i = 0; i < (int)(n); i++) cin >> a[i]; vector<long long> sums(n + 1, 0); for (int i = 0; i < (int)(n); i++) sums[i + 1] = sums[i] + a[i]; u = sums; sort(u.begin(), u.end()); u.resize(unique(u.begin(), u.end()) - u.begin()); map<long long, int> ind; for (int i = 0; i < (int)(u.size()); i++) ind[u[i]] = i; vector<int> vind(n + 1); for (int i = 0; i < (int)(sums.size()); i++) vind[i] = ind[sums[i]]; const long long inf = (long long)(1e16 + 2e9); long long L = -inf; long long R = inf; while (R - L > 1) { long long m = (L + R) / 2; if (Cnt(m, sums, vind) >= k) L = m; else R = m; } cout << L << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; typedef pair<double,int> P; int main() { int n; while(cin >> n && n) { vector<P> a; for(int i=0; i<n; i++) { int x,y,z; cin >> x >> y >> z; a.push_back(P(y,x)); a.push_back(P((double)z-0.1,-x)); } sort(a.begin(),a.end()); int w=0; bool f=0; for(int i=0; i<a.size(); i++) { w+=a[i].second; if(w>150) f=1; } if(f) cout << "NG" << endl; else cout << "OK" << endl; } return 0; }
0
#include <iostream> #include <sstream> #include <string> #include <algorithm> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <cassert> #include <complex> using namespace std; #define FOR(i,k,n) for(int i=(k); i<(int)n; ++i) #define REP(i,n) FOR(i,0,n) #define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i) template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cout<<*i<<" "; cout<<endl; } typedef long long ll; #define X real() #define Y imag() typedef double D; typedef complex<D> P; typedef pair<P, P> L; typedef vector<P> Pol; const D INF = 1e40; const D EPS = 1e-5; const D PI = acos(-1); int sign(D r){ return (r < -EPS) ? -1 : (r > EPS) ? 1 : 0; } bool eq(D a, D b){ return abs(b - a) < EPS; } bool near(P a, P b){ return abs(b - a) < EPS; } D dot(P a, P b){ return a.X * b.X + a.Y * b.Y; } D cross(P a, P b){ return a.X * b.Y - a.Y * b.X; } P vec(L a){ return a.second - a.first; } P pLL(L a, L b){ return a.first + vec(a) * (cross(vec(b), b.first - a.first) / cross(vec(b), vec(a))); } int main(){ int N; while(cin>>N && N){ vector<L> l; REP(i, N){ double a, b, c, d; cin>>a>>b>>c>>d; l.push_back(L(P(a, b), P(c, d))); } int ans = 1; for(int i = 0; i < N; i++){ int cnt = 1; vector<P> crs; for(int j = 0; j < i; j++){ if(!sign(cross(vec(l[i]), vec(l[j])))) continue; P p = pLL(l[i], l[j]); if(!(abs(p.X) < 100 - EPS && abs(p.Y) < 100 - EPS)) continue; bool ok = true; for(int k = 0; k < crs.size(); k++){ if(eq(crs[k].X, p.X) && eq(crs[k].Y, p.Y)){ ok = false; break; } } if(ok){ crs.push_back(p); cnt++; } } ans += cnt; } cout<<ans<<endl; } return 0; }
0
#include <bits/stdc++.h> int main() { int h, m, hh, mm; scanf("%d:%d", &h, &m); scanf("%d:%d", &hh, &mm); int x, y; if (mm > m) { h--, m += 60; } y = m - mm; x = (h + 24 - hh) % 24; printf("%02d:%02d\n", x, y); return 0; }
1
#include <bits/stdc++.h> using namespace std; vector<vector<int>> dept(150007); int parent[150007]; int size[200007]; int nexti[200007]; void make_set(int v) { parent[v] = v; size[v] = 1; nexti[v] = v + 1; } int find_set(int v) { if (v == parent[v]) { return v; } return parent[v] = find_set(parent[v]); } void union_set_wo_size_rank(int a, int b) { parent[find_set(b)] = find_set(a); } void union_set_by_size(int a, int b) { a = find_set(a); b = find_set(b); if (a != b) { if (size[a] < size[b]) { swap(a, b); } parent[b] = a; } } void union_set_skip(int i, int j) { if (i > j) { swap(i, j); } for (int l = i + 1; l <= j;) { union_set_by_size(l - 1, l); int temp = nexti[l]; nexti[l] = max(nexti[l], j); l = temp; } } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); clock_t t1, t2; t1 = clock(); int n; cin >> n; for (int i = 1; i <= n; i++) { parent[i] = i; dept[i].push_back(i); } for (int i = 1; i <= (n - 1); i++) { int x, y; cin >> x >> y; x = find_set(x); y = find_set(y); if (x == y) { continue; } if (dept[x].size() > dept[y].size()) { swap(x, y); } for (auto it : dept[x]) { dept[y].push_back(it); } parent[x] = y; dept[x].clear(); } int k = find_set(n); for (auto it : dept[k]) { cout << it << " "; } cout << "\n"; return 0; t2 = clock(); float diff = (float)t2 - (float)t1; float execution_time = diff / CLOCKS_PER_SEC; }
6
#include<stdio.h> int main(){ int N,Y,b,sum; scanf("%d %d",&N,&Y); for(int i = 0;i<Y;i++){ scanf("%d",&b); } sum = 0; for(;;){ N = N / 2; sum = sum + N; if(N==1) break; } printf("%d\n",sum-Y); }
0
#include <bits/stdc++.h> using namespace std; long long n, m, r, k; struct node { long long x, y, z; bool operator<(const node &rhs) const { return rhs.z > z; } }; priority_queue<node> Q; map<pair<long long, long long>, long long> mp; long long disx[4] = {1, 0, -1, 0}, disy[4] = {0, 1, 0, -1}; signed main() { scanf("%lld%lld%lld%lld", &n, &m, &r, &k); long long i = (n + 1) / 2; long long j = (m + 1) / 2; long long a = (min(i + r - 1, n) - max(i, r) + 1) * (min(j + r - 1, m) - max(j, r) + 1); Q.push((node){i, j, a}); mp[make_pair(i, j)] = 1; long long ans = 0; while (k--) { long long x = Q.top().x, y = Q.top().y; ans += Q.top().z; Q.pop(); for (long long l = 0; l < 4; ++l) { long long nx = x + disx[l], ny = y + disy[l]; if (nx >= 1 && ny >= 1 && nx <= n && ny <= m && !mp[make_pair(nx, ny)]) { mp[make_pair(nx, ny)] = 1; Q.push((node){nx, ny, (min(nx + r - 1, n) - max(nx, r) + 1) * (min(ny + r - 1, m) - max(ny, r) + 1)}); } } } printf("%.10lf", (double)(ans) / (double)((n - r + 1) * (m - r + 1))); return 0; }
4
#include <iostream> #include <vector> using namespace std; const int MAXN = 1e5 + 10, MAXM = 2e5 + 10; vector<pair<int, int> > vc[MAXN]; vector<pair<int, pair<int, int> > > e; long long d[MAXN], n, m; bool se[MAXN]; void dfs(int v, long long dis) { d[v] = dis; se[v] = 1; for (int i = 0; i < vc[v].size(); i++) if (!se[vc[v][i].first]) dfs(vc[v][i].first, dis + vc[v][i].second); } int main() { cin >> n >> m; for (int i = 0; i < m; i++) { int li, ri, di; cin >> li >> ri >> di; vc[li].push_back({ri, di}); vc[ri].push_back({li, -di}); e.push_back({di, {li, ri}}); } for (int i = 1; i <= n; i++) if (!se[i]) dfs(i, 0); for (int i = 0; i < m; i++) if (d[e[i].second.second] - d[e[i].second.first] != e[i].first) return cout << "No", 0; cout << "Yes"; }
0
#include "bits/stdc++.h" using namespace std; #define rep(i,n) for (int i=0;i<(n);i++) #define rep2(i,a,b) for (int i=(a);i<(b);i++) using ll = long long; using Pll = pair<ll, ll>; const ll inf = 1ll << 60; template<class T, class E> struct SegTree{ typedef function<T(T,T)> F; typedef function<T(T,E)> G; typedef function<E(E,E)> H; typedef function<E(E,int)> P; int n; F f; G g; H h; P p; T d1; E d0; vector<T> dat; vector<E> lazy; SegTree(int n_, F f, G g, H h, T d1, E d0, P p=[](E a, int b){ return a; }) : f(f), g(g), h(h), d1(d1), d0(d0), p(p) { n = 1; while (n < n_) n *= 2; dat.resize(2 * n - 1, d1); lazy.resize(2 * n - 1, d0); } void build(const vector<E>& a) { for (int i = 0; i < a.size(); i++) dat[i + n - 1] = Pll(a[i], 1); for (int i = n - 2; i >= 0; i--) dat[i] = f(dat[2 * i + 1], dat[2 * i + 2]); } void eval(int k, int l, int r) { if (lazy[k] == d0) return; dat[k] = g(dat[k], p(lazy[k], r - l)); if (k < n - 1) { lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]); lazy[2 * k + 2] = h(lazy[2 * k + 2], lazy[k]); } lazy[k] = d0; } void update(int a, int b, E x, int k = 0, int l = 0, int r = -1) { if (r == -1) r = n; // cerr << "update: a = " << a << ", b = " << b << ", x = " << x << ", k = " << k << ", l = " << l << ", r = " << r << endl; eval(k, l, r); if (b <= l || r <= a) return; if (a <= l && r <= b) { lazy[k] = h(lazy[k], x); eval(k, l, r); } else { update(a, b, x, 2 * k + 1, l, (l + r) / 2); update(a, b, x, 2 * k + 2, (l + r) / 2, r); dat[k] = f(dat[2 * k + 1], dat[2 * k + 2]); } } // return op[a..b) // k: node id, [l, r): node interval T query(int a, int b, int k = 0, int l = 0, int r = -1) { if (r == -1) r = n; // cerr << "query: a = " << a << ", b = " << b << ", k = " << k << ", l = " << l << ", r = " << r << endl; eval(k, l, r); if (r <= a || b <= l) return d1; if (a <= l && r <= b) return dat[k]; T vl = query(a, b, 2 * k + 1, l, (l + r) / 2); T vr = query(a, b, 2 * k + 2, (l + r) / 2, r); return f(vl, vr); } void print() { int j = 2; for (int i = 0; i < 2 * n - 1; i++) { cerr << "(" << "(" << dat[i].first << ", " << dat[i].second << ")" << ", " << lazy[i] << ")"; if (i == j - 2) { cerr << endl; j *= 2;} else { cerr << " "; } } } }; int main() { std::ios::sync_with_stdio(false); std::cin.tie(0); int h, w, q; cin >> h >> w >> q; vector<ll> a(h); rep(i, h) cin >> a[i]; vector<ll> b(w); rep(i, w) cin >> b[i]; SegTree<Pll, ll> segAm( // Pll = {min, #min} h, [](Pll a, Pll b) { if (a.first < b.first) return a; else if (a.first > b.first) return b; else return Pll(a.first, a.second + b.second); }, [](Pll a, ll x) { return Pll(a.first + x, a.second); }, [](ll x, ll y) { return x + y; }, Pll(inf, 0), ll(0) ); SegTree<Pll, ll> segAM( // Pll = {max, #max} h, [](Pll a, Pll b) { if (a.first > b.first) return a; else if (a.first < b.first) return b; else return Pll(a.first, a.second + b.second); }, [](Pll a, ll x) { return Pll(a.first + x, a.second); }, [](ll x, ll y) { return x + y; }, Pll(-inf, 0), ll(0) ); SegTree<Pll, ll> segBm( w, [](Pll a, Pll b) { if (a.first < b.first) return a; else if (a.first > b.first) return b; else return Pll(a.first, a.second + b.second); }, [](Pll a, ll x) { return Pll(a.first + x, a.second); }, [](ll x, ll y) { return x + y; }, Pll(inf, 0), ll(0) ); SegTree<Pll, ll> segBM( w, [](Pll a, Pll b) { if (a.first > b.first) return a; else if (a.first < b.first) return b; else return Pll(a.first, a.second + b.second); }, [](Pll a, ll x) { return Pll(a.first + x, a.second); }, [](ll x, ll y) { return x + y; }, Pll(-inf, 0), ll(0) ); segAm.build(a); segAM.build(a); segBm.build(b); segBM.build(b); rep(i, q) { int t; cin >> t; if (t == 1 || t == 2) { ll l, r, v; cin >> l >> r >> v; l--; if (t == 1) { segAm.update(l, r, v); segAM.update(l, r, v); } else { segBm.update(l, r, v); segBM.update(l, r, v); } } else { ll la, ra, lb, rb; cin >> la >> ra >> lb >> rb; la--; lb--; ll ma, Ma, mb, Mb, cma, cMa, cmb, cMb; tie(ma, cma) = segAm.query(la, ra); tie(Ma, cMa) = segAM.query(la, ra); tie(mb, cmb) = segBm.query(lb, rb); tie(Mb, cMb) = segBM.query(lb, rb); map<ll, ll> mpA; mpA[ma] = cma; mpA[Ma] = cMa; map<ll, ll> mpB; mpB[mb] = cmb; mpB[Mb] = cMb; map<ll, ll> mp; for (auto pa : mpA) { for (auto pb : mpB) { mp[pa.first * pb.first] += pa.second * pb.second; } } ll ans, cnt; if (t == 3) tie(ans, cnt) = *(mp.begin()); else tie(ans, cnt) = *(mp.rbegin()); if (ans == 0) { ll cza = mpA[0], czb = mpB[0]; // 0 = all - non_zero * non_zero ll len_a = ra - la; ll len_b = rb - lb; cnt = len_a * len_b - (len_a - cza) * (len_b - czb); } cout << ans << " " << cnt << endl; } } }
0
#include <bits/stdc++.h> using namespace std; char a[1005][1005]; bool vis[1005][1005]; int n, m; int check(string s) { int i; for (i = 0; i < s.size(); i++) { if (i + 1 < s.size() && s[i] == '#' && s[i + 1] == '.') { break; } } i++; for (; i < s.size(); i++) { if (s[i] == '#') { cout << -1 << endl; exit(0); } } } void dfs(int i, int j) { if (i < 0 || i >= n || j < 0 || j >= m) return; if (a[i][j] == '.' || vis[i][j]) return; vis[i][j] = true; dfs(i + 1, j); dfs(i - 1, j); dfs(i, j + 1); dfs(i, j - 1); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) { cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cin >> a[i][j]; } int a1 = 0, b1 = 0; for (int i = 0; i < n; i++) { string s = ""; for (int j = 0; j < m; j++) s += a[i][j]; check(s); if (a1 == 0) { for (int j = 0; j < m; j++) { if (s[j] == '.') { a1 = 1; continue; } else { a1 = 0; break; } } } } for (int j = 0; j < m; j++) { string s = ""; for (int i = 0; i < n; i++) s += a[i][j]; check(s); if (b1 == 0) { for (int i = 0; i < n; i++) { if (s[i] == '.') { b1 = 1; continue; } else { b1 = 0; break; } } } } if (a1 != b1) { cout << -1 << endl; exit(0); } int ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (!vis[i][j] && a[i][j] == '#') { ans++; dfs(i, j); } } } cout << ans << endl; } cerr << "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << "ms" << '\n'; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int m, n, *p; cin >> m >> n; p = new int[m]; for (int i = 0; i < m; i++) cin >> p[i]; sort(p, p + m); cout << p[m - n] << endl; }
2
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { int ans = 9999999; string s; cin>>s; for(char c = 'a'; c <= 'z'; c++){ int mx = 0; int a = 0; for(char i : s){ if(c != i){ a++; } else{ a = 0; } mx = max(mx, a); } ans = min(ans, mx); } cout << ans; return 0; }
0
#include <bits/stdc++.h> using namespace std; long long int ans = 0; vector<int> vx[200005]; bool vu[200005]; int n, k; int dfs(int v, int p) { int aa = 0, aa2 = 0; for (int i = 0; i < vx[v].size(); i++) { if (vx[v][i] == p) continue; int pr = dfs(vx[v][i], v); aa += pr; aa2 += min(pr, 2 * k - pr); } ans += aa2; if (vu[v]) aa++; return aa; } int main() { for (int i = 0; i < 200005; i++) vu[i] = 0; cin >> n >> k; int u, v; for (int i = 0; i < k; i++) { cin >> u >> v; vu[u] = vu[v] = 1; } for (int i = 1; i < n; i++) { cin >> u >> v; vx[u].push_back(v); vx[v].push_back(u); } dfs(1, -1); cout << ans; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; cout << n - 1 - n / 2 << endl; } }
1
#include<iostream> using namespace std; int n, m; bool isIn(int w, int* width){ int sum = 0, step = 1; for (int i = 0; i < n; i++) { if(sum + width[i] > w){ sum = 0; step++; } sum += width[i]; } if(step > m) return false; return true; } int bs(int* width){ int begin = 0, end = 1500000; for (int i = 0; i < n; i++) { if(begin < width[i]) begin = width[i]; } int mid = (begin + end) / 2; while(begin < mid){ if(isIn(mid, width)){ end = mid; }else{ begin = mid; } mid = (begin + end) / 2; } if(isIn(mid, width)){ return mid; }else{ return mid + 1; } } int main(int argc, char *argv[]){ while(cin >> m >> n, n){ int width[n]; for (int i = 0; i < n; i++) { cin >> width[i]; } std::cout << bs(width) << std::endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; int v[1024]; int vv[1024]; int N; int try_it(int last) { for (int i = (0); i < (N); i++) vv[i] = v[i]; for (int i = (last); i < (N); i++) vv[i] = max(0, vv[i] - 1); vv[N] = 0; int NN = N; while (NN > 0 && vv[NN - 1] == 0) --NN; int moves = 0, p = -1; while (vv[NN - 1] > 0) { moves += 3; ++p; while (vv[p + 1] >= vv[p]) { moves += 2; ++p; } ++moves; --vv[p]; --p; while (p >= 0 && vv[p] == vv[p + 1] + 1) { --vv[p]; --p; moves++; } moves++; } return moves + N * 3 - 2 * p - last; } void do_it(int last) { for (int i = (0); i < (N); i++) vv[i] = v[i]; for (int i = (last); i < (N); i++) vv[i] = max(0, vv[i] - 1); vv[N] = 0; int NN = N; while (NN > 0 && vv[NN - 1] == 0) --NN; int p = -1; while (vv[NN - 1] > 0) { cout << "ARA"; ++p; while (vv[p + 1] >= vv[p]) { cout << "RA"; ++p; } cout << "L"; --vv[p]; --p; while (p >= 0 && vv[p] == vv[p + 1] + 1) { --vv[p]; --p; cout << "L"; } cout << "A"; } while (p++ < N - 1) cout << "AR"; cout << "A"; while (p-- > last) cout << "L"; cout << "A\n"; } int main() { ios::sync_with_stdio(false); cin >> N; for (int i = (0); i < (N); i++) cin >> v[i]; v[N] = 0; while (N > 0 && v[N - 1] == 0) --N; int best = try_it(0); int last = 0; for (int i = (1); i < (N); i++) { int c = try_it(i); if (c < best) { best = c; last = i; } } do_it(last); return 0; }
5
#include <bits/stdc++.h> using namespace std; template <class T, class U> inline void chkmax(T& x, U y) { if (x < y) x = y; } template <class T, class U> inline void chkmin(T& x, U y) { if (y < x) x = y; } const int N = 1e5 + 5; int st[N], en[N], id[N], n, tp[N]; bool cmp(const int i, const int j) { return (n - st[i] - en[i]) > (n - st[j] - en[j]); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", st + i); if (i) en[i - 1] = st[i]; id[i] = i; } en[n - 1] = st[0]; sort(id, id + n, cmp); for (int i = 0; i < n; i++) tp[id[i]] = i; for (int i = 0; i < n; i++) printf("%d%c", tp[i], " \n"[i == n - 1]); return 0; }
3
#include <bits/stdc++.h> using namespace std; template <class T> T abs(T x) { return x > 0 ? x : -x; } int n; int m; int p[300000], q[300000], use[300000], last[300000]; pair<int, int> g[100000][3]; pair<int, int> w[100000]; int main() { scanf("%d", &n); for (int i = 0; i < 3 * n; i++) { scanf("%d", &p[i]); p[i]--; q[p[i]] = i; } for (int i = 0; i < n; i++) { for (int j = 0; j < 3; j++) { scanf("%d", &g[i][j].second); g[i][j].second--; g[i][j].first = q[g[i][j].second]; } sort(g[i], g[i] + 3); } int f; scanf("%d", &f); f--; for (int i = 0; i < n; i++) { if (g[i][0].second == f) { use[g[i][1].second] = use[g[i][2].second] = 1; for (int k = 0; k < 3 * n && (last[g[i][1].second] != i + 1 || last[g[i][2].second] != i + 1); k++) if (use[k]) { last[k] = i + 1; printf("%d ", k + 1); } for (int k = 0; k < 3 * n; k++) if (last[k] != i + 1 && k != f) { last[k] = i + 1; printf("%d ", k + 1); } printf("\n"); return 0; } use[g[i][0].second] = use[g[i][1].second] = use[g[i][2].second] = 1; } for (int i = 0; i < 3 * n; i++) if (i != f) printf("%d ", i + 1); printf("\n"); return 0; }
4
#include <iostream> using namespace std; int N[40]; int main(){ while(1){ int n; cin >> n; if(n==0) break; N[0]=1; N[1]=1; N[2]=2; for(int i=3;i<n+1;++i){ N[i]=N[i-1]+N[i-2]+N[i-3]; } cout <<1 + ( N[n] / 3650 ) << endl; } }
0
#include <bits/stdc++.h> using namespace std; int N, M; int st, dr, res; string s[1010]; int viz[1010][1010]; int dy[] = {-1, 0, 1}; int isBad(int x, int y) { if (y >= s[1].size()) return 0; if (s[x][y] == '.' || s[x][y] == 's') { return 0; } return 1; } int valid(int x, int xz, int y) { if (x <= 0 || x > 3) { return 0; } if (viz[x][y] == 1) return 0; if (isBad(x, y + 2 * (y - 1)) || isBad(xz, y + 2 * (y - 1))) { return 0; } if (isBad(x, y + 2 * y) || isBad(x, y + 2 * y - 1)) { return 0; } return 1; } void dfs(int x, int y) { if (y >= N) return; viz[x][y] = 1; for (int i = 0; i < 3; ++i) { if (valid(x + dy[i], x, y + 1)) { dfs(x + dy[i], y + 1); } } } int main() { int T; cin >> T; while (T--) { cin >> N >> M; for (int i = 1; i <= 3; ++i) { cin >> s[i]; } for (int i = 0; i <= 4; ++i) { for (int j = 1; j <= s[1].size() + 100; ++j) { viz[i][j] = 0; } } int start = 0; for (int i = 1; i <= 3; ++i) if (s[i][0] == 's') { start = i; } dfs(start, 0); --N; int ret = viz[1][N] || viz[2][N] || viz[0][N]; if (ret == 1) { printf("YES\n"); } else { printf("NO\n"); } } }
4
#include <bits/stdc++.h> using namespace std; bool can[35][75 * 31][75]; int num[75]; int nnum[75]; int w[75][75]; int nx[35][75 * 31][75]; int ncnt[75]; struct th { int id, nnum; bool operator<(const th &a) const { return nnum < a.nnum; } } p[75]; int main() { int m; scanf("%d", &m); for (int i = 1; i <= m; i++) scanf("%d", &num[i]); sort(num + 1, num + 1 + m); bool flag = 0; for (int n = 1; n < 75; n++) { if (flag) break; memset(can, 0, sizeof(can)); can[m][n * (n - 1) / 2][n + 1] = 1; for (int j = n; j >= 1; j--) for (int q = 0; q < j * 31; q++) for (int r = 0; r <= m; r++) { int ns = q + num[r]; if (r && ns >= j * (j - 1) / 2 && can[r][ns][j + 1]) can[r][q][j] = 1, nx[r][q][j] = num[r]; else if (r != m) { int ns2 = q + num[r + 1]; if (ns2 >= j * (j - 1) / 2 && can[r + 1][ns2][j + 1]) can[r][q][j] = 1, nx[r][q][j] = num[r + 1]; } } if (can[0][0][1]) { printf("%d\n", n); flag = 1; int nr = 1, ns = 0, npl = 1; for (int j = 1; j <= n; j++) { nnum[j] = nx[nr][ns][npl], ns += nnum[j]; if (nnum[j] == num[nr + 1]) nr++; npl++; } for (int j = n; j >= 1; j--) p[j].id = j, p[j].nnum = nnum[j]; for (int j = n; j >= 1; j--) { sort(p + 1, p + j + 1); for (int q = j - 1; q >= p[j].nnum + 1; q--) w[p[q].id][p[j].id] = 1, p[q].nnum--; for (int q = p[j].nnum; q; q--) w[p[j].id][p[q].id] = 1; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) printf("%d", w[i][j]); printf("\n"); } } } if (!flag) printf("=\n"); return 0; }
4
#include <bits/stdc++.h> using namespace std; using ll = long long; const int INF = 2e9; vector<string> v; int n, m; int diff(string a, string b) { int i, cc = 0; for (i = 0; i < m; ++i) cc += (a[i] != b[i]); return cc; } string solve(int mi) { string s = v[mi], t; int i, j, cc; char c; bool f; for (i = 0; i < m; ++i) { t = s; for (c = 'a'; c <= 'z'; ++c) { t[i] = c; cc = 0; f = 1; for (j = 0; j < n; ++j) { cc = diff(t, v[j]); if (cc > 1) f = 0; } if (f) return t; } } return "-1"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int tc, ti = 1; cin >> tc; while (tc--) { int i, j, mi, md = INF, cc; cin >> n >> m; v.resize(n); for (i = 0; i < n; ++i) cin >> v[i]; for (i = 0; i < n; ++i) { cc = 0; for (j = 0; j < n; ++j) { if (i == j) continue; cc += diff(v[i], v[j]); } if (cc < md) { mi = i; md = cc; } } cout << solve(0) << "\n"; } return 0; }
6
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; typedef long long ll; const int INF = 1001001001; struct edge{ int t; int w; }; vector<edge> to[100007]; vector<int> visit(100007); vector<int> dist(100007); void dfs(int u, int b=-1){ visit[u] = 1; rep(i, to[u].size()){ int next = to[u][i].t; int nexw = to[u][i].w; if(next == b)continue; if(visit[next] == 0){ dist[next] = dist[u] + nexw; dfs(next, u); } } return; } int main() { int n; cin >> n; rep(i, n-1){ int s, t, w; cin >> s >> t >> w; edge E1 = {t, w}; to[s].push_back(E1); edge E2 = {s, w}; to[t].push_back(E2); } rep(i, n){ dist[i] = INF; } dist[0] = 0; dfs(0); int x = -1; int ma = -1; rep(i, n){ if(dist[i] > ma){ x = i; ma = dist[i]; } } rep(i, n){ dist[i] = INF; visit[i] = 0; } dist[x] = 0; dfs(x); x = -1; ma = -1; rep(i, n){ if(dist[i] > ma){ x = i; ma = dist[i]; } } cout << ma << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, m; int a[2005]; cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int i = n - 1; int t = 0; while (i >= 0) { t += (a[i] - 1) * 2; i -= m; } cout << t; return 0; }
2
#include <bits/stdc++.h> using namespace std; int n; int x[33333]; int C[33333]; int BB[33333]; int seen[33333]; int seena[33333]; int main() { scanf("%d", &n); int cc = 0; for (int i = 0; i < 2 * n; i++) { scanf("%d", &x[i]); C[x[i]]++; BB[x[i]]++; } int ca = 0, cb = 0; vector<int> res(2 * n, 0); for (int i = 0; i < 2 * n; i++) { if (C[x[i]] == 1) { cc++; if (cc % 2) { res[i] = 1; ca++; } else { res[i] = 2; cb++; } } else { if (seena[x[i]] == 0) { res[i] = 1; ca++; } else if (seena[x[i]] == 1) { res[i] = 2; cb++; } seena[x[i]]++; } } for (int i = 0; i < 2 * n; i++) { if (res[i] == 0) { if (ca < n) { res[i] = 1; ca++; } else res[i] = 2; } } int can = 0; vector<int> A, B; for (int i = 0; i < 2 * n; i++) { if (res[i] == 1) A.push_back(x[i]); else B.push_back(x[i]); } for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { int nn = A[i] * 100 + B[j]; if (!seen[nn]) can++; seen[nn] = 1; } printf("%d\n", can); printf("%d", res[0]); for (int i = 1; i < 2 * n; i++) { printf(" %d", res[i]); } printf("\n"); return 0; }
2
#include <iostream> #include <queue> #include <map> #include <string> #define f first #define s second using namespace std; int main(){ string x; int y; int n,q,i,cnt=0; queue<pair <string,int> > a; cin>>n>>q; for(i=0;i<n;i++){ cin>>x>>y; a.push(make_pair(x,y)); } while(!a.empty()){ cnt+=q; a.front().s-=q; if(a.front().s>0){ a.push(a.front()); a.pop(); } else { cnt+=a.front().s; cout<<a.front().f<<" "<<cnt<<endl; a.pop(); } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxN = 2 * 100000 + 10; int h, m, n; int a[maxN]; map<int, int> at; long long res = 0; int main() { ios::sync_with_stdio(false); cin >> h >> m >> n; memset(a, -1, sizeof a); for (int i = 0; i < n; i++) { char ch; cin >> ch; if (ch == '+') { int id, t; cin >> id >> t; for (int j = 0, x = t; j == 0 || x != t; j++, x = (x + m) % h, res++) { if (a[x] == -1) { a[x] = id, at[id] = x; break; } } } else if (ch == '-') { int id; cin >> id; a[at[id]] = -1; } } cout << res << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int NR = 2005; const double eps = 1e-12; void Min(int& x, int y) { x = min(x, y); } void Max(int& x, int y) { x = max(x, y); } int n; long long ans = 0; double x[NR], y[NR]; double p[NR]; bool jd(double a, double b) { return fabs(a - b) <= eps; } int read() { int x = 0, f = 1; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') f = -1; ch = getchar(); } while (ch <= '9' && ch >= '0') { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); } return x * f; } int main() { n = read(); for (int i = 1; i <= n; i++) { int a = read(), b = read(), c = read(); x[i] = 1. * a * c / (1. * a * a + 1. * b * b); y[i] = 1. * b * c / (1. * a * a + 1. * b * b); } for (int i = 1; i <= n - 2; i++) { int same = 0, tot = 0; for (int j = i + 1; j <= n; j++) { if (jd(x[i], x[j]) && jd(y[i], y[j])) same++; else p[++tot] = jd(x[i], x[j]) ? 1e15 : (y[i] - y[j]) / (x[i] - x[j]); } ans += same * (n - i) - same * (same + 1) / 2; sort(p + 1, p + tot + 1); int la = 1; for (int j = 2; j <= tot; j++) if (!jd(p[j], p[la])) ans += (j - la) * (j - la - 1) / 2, la = j; ans += (tot - la) * (tot - la + 1) / 2; } printf("%lld\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; const double pi = 3.14159265358979323846264338; struct hehe { long long x, y; } p[100005]; int n; double maxr, minr, ans; long long cross(hehe p1, hehe p2, hehe p3) { return (p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y); } long long dc(hehe p1, hehe p2, hehe p3) { return (p2.x - p1.x) * (p3.x - p1.x) + (p2.y - p1.y) * (p3.y - p1.y); } double dis(hehe p1, hehe p2) { return sqrt( (double)((p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y))); } void solve(int a, int b) { double da, db, h; long long dc1, dc2; da = dis(p[a], p[0]); db = dis(p[b], p[0]); h = (double)fabs(cross(p[0], p[a], p[b])) / dis(p[a], p[b]); dc1 = dc(p[a], p[0], p[b]); dc2 = dc(p[b], p[0], p[a]); if (da > db) swap(da, db); if (dc1 < 0 || dc2 < 0) { maxr = max(maxr, db); minr = min(minr, da); } else { maxr = max(maxr, db); minr = min(minr, h); } } int main() { int i; cin >> n >> p[0].x >> p[0].y; maxr = 0; minr = 0x7ffffffff; for (i = 1; i <= n; i++) cin >> p[i].x >> p[i].y; for (i = 1; i <= n - 1; i++) solve(i, i + 1); solve(1, n); ans = (maxr * maxr - minr * minr) * pi; printf("%.18f", ans); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 2010; long long ans, nbr; int n, k, a[N][N]; vector<int> v; int main() { ios::sync_with_stdio(0); cin >> n >> k; for (int i = 1; i <= n; ++i) { a[i][i] = -1; for (int j = i + 1; j <= n; ++j) { cin >> a[i][j]; a[j][i] = a[i][j]; } } if (k < 3) { for (int i = 1; i <= n; ++i) { long long sum = 0; for (int j = 1; j <= n; ++j) if (a[i][j] >= 0) { v.push_back(a[i][j]); sum += v.back(); } int mul = int(v.size()); if (k == 2) mul = mul * (int(v.size()) - 1) / 2; ans += 1LL * (k == 2 ? (int(v.size()) - 1) : 1) * sum; nbr += mul; v.clear(); } ans /= nbr; } else { for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) if (a[i][j] > 0) ans += a[i][j]; ans /= n; } cout << ans << '\n'; }
4
#include<bits/stdc++.h> using namespace std; typedef double D; const D EPS= (1e-10); #define X real() #define Y imag() #define SZ size() #define PB(x) push_back(b) #define PREV(x,i) (x[(i+x.SZ-1)%x.SZ]) #define NEXT(x,i) (x[(i+1)%x.SZ]) template<class T> using CR=const T&; using P=complex<D>; using G=vector<P>; int sgn(D a,D b=0){ if(a>b+EPS)return 1; if(a<b-EPS)return -1; return 0; } D dist(P a,P b){ return abs(a-b); } tuple<P,P> normal_direction(P v){ D x=v.X,y=v.Y; return tuple<P,P>(P(y,-x),P(-y,x)); } P normalize(P v){ D x=v.X,y=v.Y; D d=abs(v); return P(x/d,y/d); } tuple<P,P> getC(P a,P b){ auto v=b-a; auto pq=normal_direction(v); P p,q;tie(p,q)=pq; D d=sqrt(1-(dist(a,b)/2)*(dist(a,b)/2)); return tuple<P,P>(a+0.5*v+d*normalize(p),a+0.5*v+d*normalize(q)); } int cnt(G& g,P c){ int n=g.size(),res=0; for(int i=0;i<n;i++) if(sgn(dist(c,g[i]),1.0)<=0){ res++; } return res; } int main(){ for(int n;cin>>n,n;){ G g(n); for(int i=0;i<n;i++){\ D x,y; cin>>x>>y; g[i]=P(x,y); } int res=1; for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) if(sgn(dist(g[i],g[j]),2.0)<=0){ auto pq=getC(g[i],g[j]); P p,q;tie(p,q)=pq; res=max({res,cnt(g,p),cnt(g,q)}); } cout<<res<<endl; } }
0
#include <bits/stdc++.h> using namespace std; const long long INF = 1000000009; #pragma GCC optimize("Ofast") #pragma GCC optimize("no-stack-protector") #pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native") #pragma GCC optimize("fast-math") #pragma GCC optimize "-O3" void solve() { long long a, b; cin >> a >> b; if (a == b) cout << 0; else cout << (abs(b - a) + 9) / 10; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); long long t = 1; cin >> t; while (t--) { solve(); cout << '\n'; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 2050; struct Number { bitset<N> bits; Number() {} void init(vector<int> s) { int c = 0; while (s.size()) { bits[c] = s[0] & 1; c++; s[0] >>= 1; for (int i = 1; i < s.size(); i++) s[i - 1] += (s[i] & 1) * 5, s[i] /= 2; while (s.size() && s.back() == 0) s.pop_back(); } } } a[N]; bitset<N> all[N]; int Gauss[N]; void Add(int id) { bitset<N> st; for (int i = N - 1; ~i; i--) { if (a[id].bits[i]) { if (Gauss[i]) { a[id].bits ^= a[Gauss[i]].bits; st ^= all[i]; } else { Gauss[i] = id; all[i] = st; all[i].set(id); printf("0\n"); return; } } } printf("%i ", st.count()); for (int i = 0; i < N; i++) if (st.test(i)) printf("%i ", i - 1); printf("\n"); } int main() { int n, i, j; string s; vector<int> v; scanf("%i", &n); for (i = 1; i <= n; i++) { cin >> s; v.clear(); for (j = s.size() - 1; ~j; j--) v.push_back((int)(s[j] - '0')); a[i].init(v); Add(i); } return 0; }
4
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; cout<<(1000-n%1000)%1000; return 0; }
0
#include<bits/stdc++.h> #define MAXN 100005 #define INF 1000000009 using namespace std; typedef long long ll; ll n,m,a[MAXN],res; priority_queue<ll, vector<ll>, greater<ll> > pque; vector<ll> G[MAXN]; bool used[MAXN]; void dfs(ll v,ll p) { used[v]=true; if(a[v]<res) { if(res!=INF) pque.push(res); res=a[v]; } else pque.push(a[v]); for(ll i=0;i<G[v].size();i++) { ll to=G[v][i]; if(to==p) continue; if(!used[to]) dfs(to,v); } return; } int main() { scanf("%lld%lld",&n,&m); for(ll i=0;i<n;i++) scanf("%lld",&a[i]); ll num=n-1; for(ll i=0;i<m;i++) { ll x,y; scanf("%lld%lld",&x,&y); G[x].push_back(y); G[y].push_back(x); num--; } num*=2; if(num>n) { printf("Impossible\n"); return 0; } if(num==0) { printf("0\n"); return 0; } ll ans=0; memset(used,false,sizeof(used)); while(pque.size()) pque.pop(); for(ll i=0;i<n;i++) { if(!used[i]) { res=INF; dfs(i,-1); ans+=res; num--; } } for(ll i=0;i<num;i++) { ans+=pque.top(); pque.pop(); } printf("%lld\n",ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n = s.length(); int a[n + 2][3]; for (int i = 0; i < n + 1; i++) for (int j = 0; j < 3; j++) a[i][j] = 0; for (int k = 0; k < s.length(); k++) { if (s[k] == '.') continue; else if (s[k] == 'A') { a[k][1] = 1; a[k + 1][1] = 1; a[k + 2][1] = 1; } else if (s[k] == 'B') { a[k][0] = 1; a[k + 1][0] = 1; a[k + 2][0] = 1; } else { a[k][2] = 1; a[k + 1][2] = 1; a[k + 2][2] = 1; } } for (int l = 1; l <= n; l++) { if (a[l][0] == 1 && a[l][1] == 1 && a[l][2] == 1) { cout << "YES"; return 0; } } cout << "NO"; return 0; }
1
#include<bits/stdc++.h> #define ll long long #define mo 1000000007 using namespace std; int n,k; int main(){ scanf("%d%d",&n,&k); ll ans=0; for (int i=1;i<=n;i++){ int times=n/i; ans+=n; ans-=times*min(i,k); ans-=min(k,n%i+1); ans+=(k!=0); } printf("%lld\n",ans); }
0
#include <bits/stdc++.h> using namespace std; int a[1001], b[1001], ans[1001]; int temp[10]; bool used[1001]; int main() { int n, diff = 0; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; used[i] = false; } for (int i = 1; i <= n; i++) { cin >> b[i]; if (a[i] == b[i]) { ans[i] = a[i]; used[b[i]] = true; } else { ans[i] = 0; temp[diff] = i; diff++; } } if (diff == 1) { for (int i = 1; i <= n; i++) { if (!used[i]) { a[temp[0]] = i; break; } } for (int i = 1; i <= n; i++) { cout << a[i]; if (i < n) cout << " "; } } else { int t = a[temp[0]]; a[temp[0]] = b[temp[0]]; b[temp[0]] = t; if (!used[a[temp[0]]] && !used[a[temp[1]]]) { for (int i = 1; i <= n; i++) { cout << a[i]; if (i < n) cout << " "; } } else { for (int i = 1; i <= n; i++) { cout << b[i]; if (i < n) cout << " "; } } } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { string s; long long sz; cin >> sz; cin >> s; long long ans = sz; for (long long i = 0; i < sz; i++) if (s[i] != '#' && i + 1 < sz && s[i] != s[i + 1]) ans--, s[i + 1] = '#'; cout << ans << endl; return 0; }
1
#include <cstdio> using namespace std; int main(){ while(1){ int n,a=0; scanf("%d",&n); if(!n)break; while(n!=1){ if(n&1)n=n*3+1; else n/=2; a++; } printf("%d\n",a); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, m, second; vector<int> g[200005]; vector<int> r[200005]; vector<int> cyc[200005]; int visit[200005]; int SCC[200005]; void dfs(int u) { visit[u] = 1; for (int v : g[u]) { if (visit[v]) continue; dfs(v); } } bool color[200005], vis[200005]; stack<int> st; vector<int> compo[200005]; int mark; void dfs1(vector<int> g[], int u) { color[u] = 1; for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (!color[v]) dfs1(g, v); } st.push(u); return; } void dfs2(vector<int> r[], int u, int mark) { compo[mark].push_back(u); vis[u] = 1; for (int i = 0; i < r[u].size(); i++) { int v = r[u][i]; if (!vis[v]) dfs2(r, v, mark); } return; } void findSCC(vector<int> g[], vector<int> r[], int node) { for (int i = 1; i <= node; i++) { if (!color[i]) dfs1(g, i); } while (!st.empty()) { int u = st.top(); st.pop(); if (!vis[u]) { dfs2(r, u, mark); mark++; } } return; } bool yes(int id) { for (int i = 0; i < compo[id].size(); i++) { int u = compo[id][i]; if (cyc[u].size()) return 0; if (visit[u]) return 0; } return 1; } int main() { { ios_base::sync_with_stdio(false); cin.tie(NULL); } cin >> n >> m >> second; int x[m + 5], y[m + 5]; for (int i = 0; i < m; i++) { cin >> x[i] >> y[i]; g[x[i]].push_back(y[i]); r[y[i]].push_back(x[i]); } dfs(second); findSCC(g, r, n); for (int i = 0; i < mark; i++) { for (int j = 0; j < compo[i].size(); j++) { SCC[compo[i][j]] = i; } } for (int i = 0; i < m; i++) { if (SCC[x[i]] != SCC[y[i]]) { cyc[y[i]].push_back(x[i]); } } int ans = 0; for (int i = 0; i < mark; i++) { if (yes(i)) ans++; } cout << ans << endl; }
5
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; char A[100100]; int len; int O[13], T1[13], T2[13]; char S1[100100], S2[100100]; int main() { int cur, resd, add, res = -1000000000, resz; cin.getline(A, 100100, '\n'); len = strlen(A); for (int i = 0; i < len; i++) O[(int)A[i] - 48]++; for (int sz = 0; sz <= O[0]; sz++) { for (int d = 0; d <= 9; d++) { for (int i = 0; i <= 9; i++) T1[i] = T2[i] = O[i]; T1[0] = T2[0] = O[0] - sz; cur = sz; if (T1[d] && T2[10 - d]) { cur++; T1[d]--; T2[10 - d]--; for (int i = 0; i <= 9; i++) { add = min(T1[i], T2[9 - i]); cur += add; T1[i] -= add; T2[9 - i] -= add; } } if (cur > res) { res = cur; resd = d; resz = sz; } } } for (int i = 0; i <= 9; i++) T1[i] = T2[i] = O[i]; T1[0] = T2[0] = O[0] - resz; int sidx = len - 1; S1[len] = S2[len] = '\0'; for (int i = 1; i <= resz; i++) { S1[sidx] = S2[sidx] = '0'; sidx--; } if (T1[resd] && T2[10 - resd]) { T1[resd]--; T2[10 - resd]--; S1[sidx] = (char)(resd + 48); S2[sidx--] = (char)(10 - resd + 48); for (int i = 0; i <= 9; i++) { add = min(T1[i], T2[9 - i]); T1[i] -= add; T2[9 - i] -= add; for (int j = 1; j <= add; j++) { S1[sidx] = (char)(i + 48); S2[sidx--] = (char)(9 - i + 48); } } } int i1, i2; i1 = i2 = sidx; for (int i = 0; i <= 9; i++) { if (T1[i]) { for (int j = 1; j <= T1[i]; j++) S1[i1--] = (char)(i + 48); } if (T2[i]) { for (int j = 1; j <= T2[i]; j++) S2[i2--] = (char)(i + 48); } } cout << S1 << endl << S2 << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; bool check(int a, int b) { return a == b; } int main() { int hh, mm; cin >> hh >> mm; int h, d, c, n; cin >> h >> d >> c >> n; if (hh >= 20) { if (h % n == 0) printf("%.4f\n", (h / n) * ((float)4 * c) / 5); else printf("%.4f\n", (h / n + 1) * ((float)4 * c) / 5); } else { int f = 60 - mm + 60 * (20 - hh - 1); int total = h + d * f; float val1, val2; if (total % n == 0) val1 = (total / n) * ((float)4 * c) / 5; else val1 = (total / n + 1) * ((float)4 * c) / 5; if (h % n == 0) val2 = c * (h / n); else val2 = c * (h / n + 1); printf("%.4f\n", min(val1, val2)); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, k, p, i, j, max = 0; cin >> n >> k >> p; long long a[n + 1]; long long b[k + 1]; for (i = 1; i <= n; i++) cin >> a[i]; for (i = 1; i <= k; i++) cin >> b[i]; sort(a + 1, a + n + 1); sort(b + 1, b + k + 1); long long x; long long ans = INT_MAX; for (i = 1; i <= k - n + 1; i++) { max = 0; for (long long l = 1; l <= n; l++) { x = abs(b[i + l - 1] - p) + abs(a[l] - b[i + l - 1]); if (x > max) max = x; } if (max < ans) ans = max; } cout << ans << endl; }
1
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (!a) return b; return gcd(b % a, a); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n, s; cin >> n >> s; long long a[n], ans = 0; for (long long i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (long long i = 0; i <= (n / 2); i++) { if (a[i] > s) ans += a[i] - s; } for (long long i = (n / 2); i < n; i++) { if (a[i] < s) ans += s - a[i]; } cout << ans; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); long long max, min, i, j, a[105], q, k, n; cin >> q; while (q--) { max = 0; min = 0x3f3f3f3f; memset(a, 0, sizeof(a)); cin >> n >> k; for (i = 0; i < n; i++) { cin >> a[i]; if (a[i] <= min) min = a[i]; if (a[i] >= max) max = a[i]; } if (max - min > 2 * k) { cout << -1 << endl; } else { cout << min + k << endl; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, q; int l, r; int v[100005], k[100005]; pair<int, int> p[100005]; vector<int> st; int main() { scanf("%d%d", &n, &q); for (int i = 0; i < n; i++) scanf("%d", &v[i]); while (q--) { scanf("%d%d", &l, &r); int sz = 0; for (int i = l; i < r; i++) k[sz++] = abs(v[i] - v[i - 1]); st.clear(); for (int i = 0; i < sz; i++) { while (st.size() != 0 && k[st.back()] < k[i]) p[st.back()].second = i, st.pop_back(); p[i].second = sz; if (st.size() != 0) p[i].first = st.back() + 1; else p[i].first = 0; st.push_back(i); } long long ans = 0; for (int i = 0; i < sz; i++) ans += 1LL * (p[i].second - i) * (i - p[i].first + 1) * k[i]; printf("%lld\n", ans); } }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++){ int x, y, b, p; scanf("%d %d %d %d", &x, &y, &b, &p); int A = x * b + y * p; int B = (x * max(b, 5) + y * max(p, 2)) * 4 / 5; printf("%d\n", min(A, B)); } }
0
#include<cstdio> #include<cstring> #include<algorithm> #define MN 300005 using namespace std; inline int in(){ int x=0;bool f=0; char c; for (;(c=getchar())<'0'||c>'9';f=c=='-'); for (x=c-'0';(c=getchar())>='0'&&c<='9';x=(x<<3)+(x<<1)+c-'0'); return f?-x:x; } int k,n,a[MN],cur,cnt; int main() { k=in();n=in(); if (!(k&1)){ printf("%d ",(k>>1)); for (int i=2;i<=n;++i) printf("%d ",k); }else { for (int i=1;i<=n;++i) a[i]=(k>>1)+1,++cnt;cur=n; for (int j=1;j<=(cnt>>1);++j){ --a[cur];if (!a[cur]) --cur; else{ for (int i=cur+1;i<=n;++i) a[i]=k;cur=n; } } for (int i=1;i<=cur;++i) printf("%d ",a[i]); }return 0; }
0
#include<bits/stdc++.h> using namespace std; int sum; int main() { for(int i=1; i<=6; i++) { char x; cin>>x; if(x=='1') sum++; } printf("%d\n",sum);return 0; }
0
#include<bits/stdc++.h> using namespace std; const int maxn=1e6+5; const int inf=0x3f3f3f3f; int a[maxn],b[maxn]; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); //freopen(".in","r",stdin); //freopen(".out","w",stdout); int n,k,c; string s; cin>>n>>k>>c>>s; int num=0; for(int i=0;i<n;i++) { if(s[i]=='o') { a[num++]=i; i+=c; } } num=k-1; for(int i=n-1;i>=0;i--) { if(num<0) break; if(s[i]=='o') { b[num--]=i; i-=c; } } for(int i=0;i<k;i++) if(a[i]==b[i]) cout<<a[i]+1<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const long double PI = atan(1.0) * 4.0; const long long int MOD = 1e9 + 7; const long long int INF = 1e8; bool comp(pair<int, vector<int> > p1, pair<int, vector<int> > p2) { return (p1.first > p2.first); } int n, m, x, y, color[300010], vis[300010] = {0}, p[300010]; vector<int> edge[300010]; vector<pair<int, vector<int> > > v; int vis1[300010]; int ans = 0, cnt, ind; void DFS(int u) { vis[u] = 1; map<int, bool> vis1; for (auto v1 : v[u].second) { if (color[v1] != INF) vis1[color[v1]] = 1; } cnt = 0; ind = 0; auto vv = v[u].second; while (ind < v[u].first) { if (vis1[cnt] == 1) { cnt++; continue; } if (color[vv[ind]] != INF) { ind++; continue; } color[vv[ind]] = cnt; cnt++; ind++; } for (auto v1 : edge[u]) { if (!vis[v1]) { DFS(v1); } } } int main() { std::ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 0; i <= n - 1; i++) { cin >> x; vector<int> v1; for (int j = 0; j <= x - 1; j++) { cin >> y; y--; v1.push_back(y); } v.push_back({x, v1}); } for (int i = 0; i <= n - 2; i++) { cin >> x >> y; x--; y--; edge[x].push_back(y); edge[y].push_back(x); } fill(color, color + 300010, INF); for (int i = 0; i <= n - 1; i++) { if (vis[i] == 0) { DFS(i); } } ans = 0; for (int i = 0; i <= m - 1; i++) { if (color[i] == INF) color[i] = 0; } for (int i = 0; i <= m - 1; i++) { ans = max(ans, color[i]); } cout << ans + 1 << endl; for (int i = 0; i <= m - 1; i++) { cout << color[i] + 1 << " "; } cout << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const unsigned long long B = 31; const int maxn = 50005; int n, ban, s[maxn], tag[maxn]; char _s[maxn]; unsigned long long h[maxn], pw[maxn]; unsigned long long calc_hash(int l, int r) { return h[r] - h[l - 1] * pw[r - l + 1]; } void init_hash() { for (int i = 1; i <= n; i++) { h[i] = h[i - 1] * B + s[i]; } } int get_lcs_L(int x, int y) { int l = 1, r = x - ban; for (; l <= r;) { int mid = l + r >> 1; if (calc_hash(x - mid + 1, x) != calc_hash(y - mid + 1, y)) r = mid - 1; else l = mid + 1; } return r; } int get_lcs_R(int x, int y) { int l = 1, r = n - y + 1; for (; l <= r;) { int mid = l + r >> 1; if (calc_hash(x, x + mid - 1) != calc_hash(y, y + mid - 1)) r = mid - 1; else l = mid + 1; } return r; } void del(int x, int y, int z) { for (int i = x; i <= y; i++) tag[i] = z; } void print() { for (int i = 1; i <= n; i++) putchar(s[i] + 'a'); puts(""); } int main() { scanf("%s", _s + 1); n = strlen(_s + 1); pw[0] = 1; for (int i = 1; i <= n; i++) pw[i] = pw[i - 1] * B; for (int i = 1; i <= n; i++) s[i] = _s[i] - 'a'; init_hash(); for (int len = 1; len + len <= n; len++) { int find = 0; ban = 0; for (int i = len; i + len <= n; i += len) { if (s[i] != s[i + len]) continue; int dL = get_lcs_L(i, i + len); int dR = get_lcs_R(i + 1, i + len + 1); if (dL + dR >= len) { del(i - dL + 1, i - dL + len, len); ban = i - dL + len; find = 1; } } if (find) { int t = 0; for (int i = 1; i <= n; i++) if (tag[i] != len) s[++t] = s[i]; n = t; init_hash(); } } print(); return 0; }
4
#include <bits/stdc++.h> using namespace std; deque<vector<int> > v; int main() { int(n), (k); scanf("%d %d", &(n), &(k)); string s; cin >> s; int mn = 0, mx = 0; while (1) { mn++; v.push_back(vector<int>()); for (int i = 1; i < n; i++) if (s[i] == 'L' && s[i - 1] == 'R') v.back().push_back(i), mx++; if (!v.back().size()) { mn--; break; } for (int i : v.back()) s[i] = 'R', s[i - 1] = 'L'; } if (!(mn <= k && k <= mx)) { cout << "-1\n"; return 0; } int rem = k - mn; for (vector<int> vec : v) { while (rem && vec.size()) { cout << "1 " << vec.back() << "\n"; vec.pop_back(); rem--; if (vec.empty()) rem++; } if (vec.size()) { cout << vec.size() << " "; for (int i = 0; i <= vec.size() - 1; i++) cout << vec[i] << (i == vec.size() - 1 ? '\n' : ' '); } } return 0; }
4
#include<iostream> #include<algorithm> using namespace std; int main(void){ int x,y,z; while(cin >> x >> y){ if(x<y) swap(x,y); while((z=x%y)!=0){ x=y; y=z; } cout << y << endl; } }
0
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); ; long long a, b, c; cin >> a >> b >> c; long long x, y, z; x = a + b + c; y = a * b * c; z = (a + b) * c; long long d = a * (b + c); cout << max(max(x, y), max(z, d)); }
1
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10, B = 350; int n, pos[N], Lef[N], Rig[N], a[N]; struct block { int sta[B], top, cnt; } b[B]; void updata(int p) { int l = Lef[p], r = Rig[p]; int top = 0, cnt = 0; for (register int i = (l); i <= (r); ++i) { if (a[i] == 0) continue; if (a[i] == -1) cnt++; if (a[i] == -1 && top > 0) top--; if (a[i] > 0) b[p].sta[++top] = a[i], cnt--; } b[p].top = top; b[p].cnt = cnt; } int query() { int t = 0; for (register int i = (pos[n]); i >= (1); --i) { t += b[i].top; if (t > 0) return b[i].sta[t]; t -= b[i].top; t -= b[i].cnt; } return -1; } signed main() { int x, y, opt; scanf("%d", &n); int blo = sqrt(n); for (register int i = (1); i <= (n); ++i) pos[i] = (i - 1) / blo + 1; for (register int i = (1); i <= (n); ++i) Rig[pos[i]] = i; for (register int i = (n); i >= (1); --i) Lef[pos[i]] = i; for (register int i = (1); i <= (n); ++i) { scanf("%d %d", &x, &opt); if (opt == 1) scanf("%d", &y), a[x] = y; if (opt == 0) a[x] = -1; updata(pos[x]); printf("%d\n", query()); } return 0; }
3
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const long double pi = acos(-1.0); const long double log23 = 1.58496250072115618145373894394781; const long double eps = 1e-8; const long long INF = 1e18 + 239; const long long prost = 239; const int two = 2; const int th = 3; const long long MOD = 1e9 + 7; const long long MOD2 = MOD * MOD; const int BIG = 1e9 + 239; const int alf = 26; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, 1, 0, -1}; const int dxb[4] = {-1, 1, 1, -1}; const int dyb[4] = {-1, 1, -1, 1}; const int dxo[8] = {-1, -1, -1, 0, 1, 1, 1, 0}; const int dyo[8] = {-1, 0, 1, 1, 1, 0, -1, -1}; const int dig = 10; const int day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int digarr[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6}; const long long bt = 30; template <typename T> inline T gcd(T a, T b) { while (a) { b %= a; swap(a, b); } return b; } template <typename T> inline T lcm(T a, T b) { return (a / gcd(a, b)) * b; } inline bool is_down(char x) { return ('a' <= x && x <= 'z'); } inline bool is_upper(char x) { return ('A' <= x && x <= 'Z'); } inline bool is_digit(char x) { return ('0' <= x && x <= '9'); } mt19937_64 rnd(239); const int M = 3e5 + 239; const int N = 2e3 + 239; const int L = 20; const int T = (1 << 19); const int B = trunc(sqrt(M)) + 1; const int X = 110; int n; long long a[M], ans; vector<pair<int, long long> > v[M]; long long dfs(int p, int pr) { vector<long long> to; to.push_back(0); to.push_back(0); long long rt = 0; for (pair<int, long long> t : v[p]) if (pr != t.first) { long long x = dfs(t.first, p) + a[t.first] - t.second; to.push_back(x); sort(to.rbegin(), to.rend()); to.pop_back(); rt = max(rt, x); } ans = max(ans, to[0] + to[1] + a[p]); return rt; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n - 1; i++) { int s, f, c; cin >> s >> f >> c; s--, f--; v[s].push_back(make_pair(f, c)); v[f].push_back(make_pair(s, c)); } ans = 0; ans = max(ans, dfs(0, -1) + a[0]); cout << ans; return 0; }
1