solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; int a[50][50], row[50], col[50], n, ans; int main() { cin >> n; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> a[j][i]; row[i] += a[j][i]; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { col[i] += a[i][j]; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (col[j] > row[i]) { ans++; } } } cout << ans << endl; return 0; }
1
// Problem: F. Christmas Game // Contest: Codeforces - CodeCraft-21 and Codeforces Round #711 (Div. 2) // URL: https://codeforces.com/contest/1498/problem/F // Memory Limit: 256 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) #pragma GCC optimize("Ofast,unroll-loops") #pragma GCC target("avx,avx2,sse,sse2") #include<bits/stdc++.h> #define all(x) begin(x), end(x) using namespace std; using ll = long long; template<typename F> void multitest(F func) { int t; cin >> t; while(t--) func(); } void report(int ok) { cout << (ok?"YES":"NO") << '\n'; } const int maxn = 1<<17; int k; struct sol { array<int, 41> f; sol() { fill(all(f), 0); } void shift() { int z = f[2*k-1]; for(int i =2*k-1; i>0; i--) f[i] = f[i-1]; f[0] = z; } void upd(int x) { f[0] ^= x; } friend sol operator+(sol a, const sol &b) { for(int i = 0; i < 2*k; i++) a.f[i] ^= b.f[i]; return a; } bool eval() { int res = 0; for(int i = k; i < 2*k; i++) res ^= f[i]; return !!res; } }; int n; vector<int> g[maxn]; int a[maxn]; sol dp[maxn]; void calc(int v, int p) { dp[v].upd(a[v]); for(auto i : g[v]) if(i != p) { calc(i, v); auto t = dp[i]; t.shift(); dp[v] = dp[v]+t; } } int ans[maxn]; void solve(int v, int p, sol &cur) { ans[v] = cur.eval(); for(auto i : g[v]) if(i != p) { auto chi = cur; auto t = dp[i]; t.shift(); chi = chi+t; chi.shift(); chi = dp[i] + chi; solve(i, v, chi); } } int main() { cin.tie(0)->sync_with_stdio(0); //multitest([&](){}); cin >> n >> k; for(int f, t, i = 1; i < n; i++) { cin >> f >> t; g[f].push_back(t); g[t].push_back(f); } for(int i = 1; i <= n; i++) cin >> a[i]; calc(1, -1); solve(1, -1, dp[1]); for(int i = 1; i <= n; i++) { cout << ans[i] << " "; } }
6
#include <bits/stdc++.h> using namespace std; template <class A, class B> ostream &operator<<(ostream &out, const pair<A, B> &a) { return out << "(" << a.first << "," << a.second << ")"; } template <class A> ostream &operator<<(ostream &out, const vector<A> &a) { for (const A &it : a) out << it << " "; return out; } template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) { return in >> a.first >> a.second; } template <class A> istream &operator>>(istream &in, vector<A> &a) { for (A &i : a) in >> i; return in; } long long poww(const long long &a, long long b, const long long &m = 1000000007) { if (b == 0) return 1; long long x = poww(a, b / 2, m); x = x * x % m; if (b & 1) x = x * a % m; return x; } long long ceil(const long long &a, const long long &b) { return (a + b - 1) / b; } long long n; long double v[18][18]; long double dp[18][1 << 18]; long double f(long long lst, long long mask) { if (__builtin_popcount(mask) == n - 1) return lst == 0; long double &ans = dp[lst][mask]; if (ans >= 0) return ans; ans = 0; for (long long j = 0; j < n; j++) { if (mask & (1 << j) || lst == j) continue; ans = max(ans, f(j, mask | (1 << lst)) * v[j][lst] + f(lst, mask | (1 << j)) * v[lst][j]); } return ans; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (long long i = 0; i < n; i++) for (long long j = 0; j < n; j++) cin >> v[i][j]; for (long long i = 0; i < n; i++) for (long long j = 0; j < (1 << n); j++) dp[i][j] = -(1e18); long double ans = 0; for (long long i = 0; i < n; i++) { for (long long j = i + 1; j < n; j++) { ans = max(ans, f(i, (1 << j)) * v[i][j] + f(j, (1 << i)) * v[j][i]); } } if (n == 1) ans = 1; cout << fixed << setprecision(10) << ans; }
5
#include <bits/stdc++.h> using namespace std; const int Max = 3e5 + 10; const int Mod = 1e9 + 7; const int N = 1e6; int D[N + 10]; void Cin(int &num) { num = 0; char ch = getchar(); int flag = 0; while (!((ch >= '0' & ch <= '9') || ch == '-')) { ch = getchar(); } if (ch == '-') { flag = 1; ch = getchar(); } while (ch >= '0' && ch <= '9') { num = (num << 1) + (num << 3) + ch - '0'; ch = getchar(); } if (flag == 1) { num = 0 - num; } } void Cout(long long n) { long long num = n, rev = n, cnt = 0; char ch; if (n == 0) { putchar('0'); return; } while (rev % 10 == 0) { cnt++; rev /= 10; } rev = 0; while (num > 0) { rev = (rev << 3) + (rev << 1) + num % 10; num /= 10; } while (rev > 0) { ch = (rev % 10) + '0'; putchar(ch); rev /= 10; } while (cnt--) putchar('0'); putchar('\n'); } void divisor() { int i, j; for (i = 1; i <= N; i++) { for (j = i; j <= N; j += i) D[j]++; } } long long tree[Max + 10]; int mp[Max + 10]; long long query(int idx) { long long sum = 0; while (idx > 0) { sum += tree[idx]; idx -= idx & (-idx); } return sum; } void update(int idx, int x, int n) { while (idx <= n) { tree[idx] += x; idx += idx & (-idx); } } void input(int ara[], int n) { ara[0] = 0; mp[0] = 0; for (int i = 1; i <= n; i++) { Cin(ara[i]); update(i, ara[i], n); if (ara[i] <= 2) mp[i] = i + 1; else mp[i] = i; } mp[n] = n; mp[n + 1] = n + 1; } int next(int n) { if (mp[n] == n) return n; else return mp[n] = next(mp[n]); } int main() { int i, n, t, l, r, q; divisor(); Cin(n); Cin(q); int ara[n + 2]; input(ara, n); while (q--) { Cin(t); Cin(l); Cin(r); if (t == 1) { while ((l = next(l)) <= r) { update(l, -ara[l] + D[ara[l]], n); ara[l] = D[ara[l]]; if (ara[l] <= 2) { mp[l] = next(l + 1); } l++; } } else { printf("%lld\n", query(r) - query(l - 1)); } } }
6
#include <bits/stdc++.h> using namespace std; int main() { string s; int n, m, k; cin >> n >> m >> k; for (int i = 1; i <= 2 * k; i++) { int sx, sy; cin >> sx >> sy; } for (int i = 1; i <= m - 1; i++) s += 'L'; for (int i = 1; i <= n - 1; i++) s += 'U'; for (int i = 1; i <= n; i++) { if (i % 2) for (int j = 1; j <= m - 1; j++) s += 'R'; else for (int j = 1; j <= m - 1; j++) s += 'L'; if (i != n) s += 'D'; } cout << s.size() << endl; cout << s << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N=234567; int n; int a[N],b[N]; int check(int x){ for(int i=0; i<2*n-1; i++) b[i]=0; for(int i=0; i<2*n-1; i++) if(a[i]>=x) b[i]=1; int r=b[n-1],ri=N; for(int i=n; i<2*n-1; i++){ if(r==b[i]){ ri=i-(n-1)-1; break; } r=b[i]; } int l=b[n-1],li=N; for(int i=n-2; i>=0; i--){ if(l==b[i]){ li=n-1-i-1; break; } l=b[i]; } if(li<=ri) return l; else return r; } int main(){ cin>>n; for(int i=0; i<2*n-1; i++) scanf("%d",&a[i]); int l=1,u=2*n-1; int mid; while(u-l>1){ mid=(l+u)/2; if(check(mid)) l=mid; else u=mid; } cout<<l<<endl; return 0; }
0
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <vector> #define MV 51 #define MX 101 #define MX2 202 #define MOD 1000000007 using namespace std; typedef long long ll; typedef pair<int, int> pii; template <typename T> void read(T& x) { x = 0; char c = getchar(); while(!isdigit(c)) c = getchar(); while(isdigit(c)) x = x*10+c-'0', c = getchar(); } int fst[MV], nxt[MX2], u[MX2], v[MX2], lnum; ll fac[MX2], faci[MX2]; void addeg(int nu, int nv) { nxt[++lnum] = fst[nu]; fst[nu] = lnum; u[lnum] = nu; v[lnum] = nv; } int low[MV], dfn[MV], stk[MX], col[MV], top, scc, dfc; vector<pii> con[MV]; int n, m, k; void tarjan(int x, int f) { dfn[x] = low[x] = ++dfc; for(int i=fst[x]; i; i=nxt[i]) { int y = v[i]; if(!dfn[y]) { stk[++top] = i; tarjan(y, x); low[x] = min(low[x], low[y]); if(low[y] >= dfn[x]) { scc++; while(1) { int now = stk[top--]; con[scc].push_back(make_pair(u[now], v[now])); if(now == i) break; } } } else if(dfn[y]<dfn[x] && y!=f) { low[x] = min(low[x], dfn[y]); stk[++top] = i; } } } ll gcd(ll x, ll y) { return (x==0) ? (y) : gcd(y%x, x); } ll qpow(ll x, ll t) { ll ans = 1; while(t) { if(t & 1) ans = ans*x%MOD; x = x*x%MOD; t >>= 1; } return ans; } ll binom(int x) { return fac[x+k-1] * faci[k-1] % MOD * faci[x] % MOD; } ll polya(int x) { ll ret = 0; for(int i=0; i<x; i++) ret = (ret + qpow(k, gcd(i, x))) % MOD; return ret * qpow(x, MOD-2) % MOD; } void init() { fac[0] = 1; for(int i=1; i<MX2; i++) fac[i] = fac[i-1] * i%MOD; faci[MX2-1] = qpow(fac[MX2-1], MOD-2); for(int i=MX2-1; i>=1; i--) faci[i-1] = faci[i] * i % MOD; lnum = 0; } void input() { int a, b; read(n); read(m); read(k); for(int i=1; i<=m; i++) { read(a); read(b); addeg(a, b); addeg(b, a); } } int siz[MV], ecn[MV], vis[MV]; void work() { for(int i=1; i<=n; i++) if(!dfn[i]) tarjan(i, 0); for(int i=1; i<=scc; i++) { ecn[i] = con[i].size(); for(auto it=con[i].begin(); it!=con[i].end(); it++) { if(!vis[it->first]) vis[it->first] = 1, siz[i]++; if(!vis[it->second]) vis[it->second] = 1, siz[i]++; } for(auto it=con[i].begin(); it!=con[i].end(); it++) { vis[it->first] = 0; vis[it->second] = 0; } } ll ans = 1; for(int i=1; i<=scc; i++) { if(siz[i] == ecn[i]) ans = ans * polya(ecn[i]) % MOD; else if(siz[i] < ecn[i]) ans = ans * binom(ecn[i]) % MOD; else if(siz[i]==2 && ecn[i]==1) ans = ans * k % MOD; } printf("%lld\n", ans); } int main() { init(); input(); work(); return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, k, f[10], mn = 1 << 29; string str, res; string calc(int b) { int rem = k - f[b]; vector<int> F(f, f + 10); int l = b - 1, r = b + 1; int cost = 0; vector<int> change(10, 0); while (rem) { if (r < 10 && !F[r]) { ++r; continue; } if (l >= 0 && !F[l]) { --l; continue; } if (l >= 0 && r < 10) { if (r - b <= b - l) { cost += r - b; ++change[r]; --F[r]; } else { cost += b - l; ++change[l]; --F[l]; } } else if (l >= 0) { --F[l]; ++change[l]; cost += b - l; } else { --F[r]; ++change[r]; cost += r - b; } --rem; } if (cost > mn) return string(n, '9'); if (cost < mn) res = string(n, '9'); mn = min(mn, cost); string ret = str; for (int j = 9; j > b; --j) for (int i = 0; change[j] && i < ret.size(); ++i) if (ret[i] - '0' == j) { ret[i] = b + '0'; --change[j]; } for (int j = 0; j < b; ++j) for (int i = ret.size() - 1; change[j] && i >= 0; --i) if (ret[i] - '0' == j) { ret[i] = b + '0'; --change[j]; } return ret; } int main() { cin >> n >> k >> str; for (int i = 0; i < str.size(); ++i) { ++f[str[i] - '0']; if (f[str[i] - '0'] == k) { cout << 0 << endl << str << endl; return 0; } } res = string(n, '9'); for (int i = 0; i < 10; ++i) res = min(res, calc(i)); cout << mn << endl; cout << res << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { string s; int i, j, cntb = 0, totb = 0, sum = 1e6, cnt; cin >> s; int a[5005]; if (s[0] == 'a') a[0] = 1; else a[0] = 0, totb++; for (i = 1; s[i]; i++) { if (s[i] == 'a') a[i] = a[i - 1] + 1; else a[i] = a[i - 1], totb++; } for (i = 0; s[i]; i++) { if (s[i] == 'b') { int cnt = 0; for (j = i; s[j]; j++) { if (s[j] == 'b') { cnt++; sum = min(sum, a[j] - a[i] + totb - cnt); } } } } if (sum == 1e6) cout << s.size(); else cout << s.size() - sum; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int N, M, tmp; long long Hest = 0; cin >> N >> M; vector<long long> party[M + 1]; for (int i = 0; i < N; i++) { long long r, c; cin >> r >> c; party[r].push_back(c); } for (int i = 2; i <= M; i++) { sort(party[i].begin(), party[i].end()); Hest = max(Hest, (long long)party[i].size()); for (int j = 1; j < party[i].size(); j++) { party[i][j] += party[i][j - 1]; } } long long ans = 10000000000000007; for (int i = 0; i <= Hest; i++) { int mine = party[1].size(); int loc[M + 1]; long long now = 0; priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > PQ; for (int j = 2; j <= M; j++) { if (party[j].empty()) continue; tmp = party[j].size(); if (tmp > i) { mine += tmp - i; tmp -= i + 1; loc[j] = tmp + 1; now += party[j][tmp]; PQ.push(make_pair(party[j][tmp + 1] - party[j][tmp], j)); } else { loc[j] = 0; PQ.push(make_pair(party[j][0], j)); } } while (mine <= i) { mine++; now += PQ.top().first; if (loc[PQ.top().second] + 1 < party[PQ.top().second].size()) { PQ.push(make_pair(party[PQ.top().second][loc[PQ.top().second] + 1] - party[PQ.top().second][loc[PQ.top().second]], PQ.top().second)); loc[PQ.top().second]++; } PQ.pop(); } ans = min(ans, now); } cout << ans << endl; }
1
#include <bits/stdc++.h> using namespace std; long long int findSum(vector<long long int> vect, int x, int y) { return vect[y] - vect[x]; } void compute() { int n, m, x, y; cin >> n >> m >> x >> y; vector<string> str(n + 1); for (int i = 1; i <= n; i++) { cin >> str[i]; } vector<long long int> ones(m + 1, 0), zeroes(m + 1, 0); for (int j = 0; j < m; j++) { for (int i = 1; i <= n; i++) { if (str[i][j] == '.') { ones[j + 1]++; } else { zeroes[j + 1]++; } } } for (int i = 1; i <= m; i++) { ones[i] += ones[i - 1]; zeroes[i] += zeroes[i - 1]; } vector<vector<long long int>> dp(m + 1, vector<long long int>(2)); dp[0][0] = 0; dp[0][1] = 0; for (int i = 1; i <= m; i++) { dp[i][0] = INT_MAX; dp[i][1] = INT_MAX; for (int j = x; j <= y; j++) { if (i - j < 0) { continue; } dp[i][0] = min(dp[i][0], dp[i - j][1] + findSum(ones, i - j, i)); dp[i][1] = min(dp[i][1], dp[i - j][0] + findSum(zeroes, i - j, i)); } } cout << min(dp[m][0], dp[m][1]); } int main() { int t = 1; while (t--) { compute(); } }
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e4 + 5; const int MaxN = 1e8 + 5, d = 5e7; struct XXX { int check, l, r, x; } s[MAXN + 5]; long long Max[MAXN + 5], p[MAXN + 5]; int n, m; int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= m; i++) scanf("%d %d %d %d", &s[i].check, &s[i].l, &s[i].r, &s[i].x); for (int i = 1; i <= n; i++) Max[i] = -MaxN; for (int i = m; i >= 1; i--) { if (s[i].check == 1) { long long x = -s[i].x; for (int j = s[i].l; j <= s[i].r; j++) Max[j] += x; } else { long long x = s[i].x; for (int j = s[i].l; j <= s[i].r; j++) { if (Max[j] <= -d) Max[j] = x; else if (x <= Max[j]) Max[j] = x; } } } for (int i = 1; i <= n; i++) p[i] = Max[i]; int ok = 1; for (int i = 1; i <= m; i++) { if (s[i].check == 1) { long long x = s[i].x; for (int j = s[i].l; j <= s[i].r; j++) p[j] += x; } else { long long MAX = p[s[i].l], x = s[i].x; for (int j = s[i].l; j <= s[i].r; j++) MAX = max(MAX, p[j]); if (MAX != x) { ok = 0; break; } } } if (ok == 0) printf("NO\n"); else { printf("YES\n"); for (int i = 1; i <= n; i++) { if (i == n) printf("%I64d\n", Max[i]); else printf("%I64d ", Max[i]); } } }
1
#include <bits/stdc++.h> using namespace std; string s; int n; int main() { getline(cin, s); n = (int)s.size(); long long ans = 0; for (int i = 0; i < n; i++) { bool found = false; for (int j = i + 2; j < n; j++) { for (int k = 1; j - 2 * k >= i; k++) { if (s[j] == s[j - k] && s[j] == s[j - 2 * k]) { found = true; break; } } if (found) { ans += n - j; break; } } } cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int n; int f[10005], c[10005]; int main() { while (cin >> n) { for (int i = 2; i <= n; i++) cin >> f[i]; for (int i = 1; i <= n; i++) cin >> c[i]; int ans = 1; for (int i = 2; i <= n; i++) if (c[i] != c[f[i]]) ans++; cout << ans << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, p, q, ans, i, j; string str; while (scanf("%d%d%d\n", &n, &p, &q) != EOF) { int a = -1, b = -1; cin >> str; for (i = 0; i <= n; i++) { for (j = 0; j <= n; j++) { ans = i * p + j * q; if (ans == n) { a = i; b = j; break; } } } if (a == -1 || b == -1) cout << "-1" << endl; else { int temp1, temp2, temp3, k; temp1 = a * p; temp2 = b * q; temp3 = a + b; cout << temp3 << endl; int count = 0; for (i = 0; i < temp1; i++) { cout << str[i]; count++; if (count == p) cout << endl, count = 0; } count = 0; for (i = temp1; i < temp1 + temp2; i++) { cout << str[i]; ; count++; if (count == q) cout << endl, count = 0; } } } }
1
#include <bits/stdc++.h> using namespace std; void ECHO(string _s) { cout << endl; (void)_s; } template <typename T, typename... Args> void ECHO(string _s, T x, Args... args) { unsigned _i; string _s2 = ""; for (_i = 0; _i < _s.length(); ++_i) { if (_s[_i] == ',') break; if (_s[_i] != ' ') _s2 += _s[_i]; } if (_i == _s.length()) { --_i; } cout << " (" << _s2 << "):" << x; ECHO(_s.substr(_i + 1, _s.length() - _i - 1), args...); } template <typename T0, typename T1> inline ostream& operator<<(ostream& os, pair<T0, T1>& p) { return os << "(" << p.first << ", " << p.second << ")"; } template <typename T> inline ostream& operator<<(ostream& os, vector<T>& v) { for (unsigned i = 0; i < v.size(); ++i) { cout << v[i] << "_"; } cout << endl; return os; } template <typename T> inline T _min(T x1, T x2, T x3) { return min(x1, min(x2, x3)); } template <typename T> inline T _min(T x1, T x2, T x3, T x4) { return min(min(x1, x2), min(x2, x3)); } inline int _gcd(int a, int b) { while (b) b %= a ^= b ^= a ^= b; return a; } int main() { ios::sync_with_stdio(false); int n; cin >> n; vector<int> dat(n); for (int i = 0; i < n; i++) cin >> dat[i]; vector<vector<pair<int, int> > > tab; tab.assign((int)log2(n) + 1, vector<pair<int, int> >(n)); for (int i = 0; i < n; i++) tab[0][i] = make_pair(dat[i], 0); int j2; for (int i = 1; i <= log2(n); ++i) { for (int z = 0; z < n; ++z) { j2 = z + (1 << (i - 1)); if (j2 >= n) break; tab[i][z].first = (tab[i - 1][z].first + tab[i - 1][j2].first) % 10; tab[i][z].second = (tab[i - 1][z].first + tab[i - 1][j2].first) / 10 + tab[i - 1][z].second + tab[i - 1][j2].second; } } int k; cin >> k; int a, b; for (int i = 0; i < k; i++) { cin >> a >> b; a--; b--; double si = log2(b - a + 1); if (b - a == 1) { cout << (dat[b] + dat[a]) / 10 << "\n"; } else if (b == a) { cout << "0\n"; } else { cout << tab[(int)si][a].second << "\n"; } } return 0; }
3
#include <bits/stdc++.h> inline long long read() { long long x = 0; char c = getchar(), f = 1; for (; c < '0' || '9' < c; c = getchar()) if (c == '-') f = -1; for (; '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0'; return x * f; } inline void write(long long x) { static char buf[20]; int len = 0; if (x < 0) putchar('-'), x = -x; for (; x; x /= 10) buf[len++] = x % 10 + '0'; if (!len) putchar('0'); else while (len) putchar(buf[--len]); } inline void writesp(long long x) { write(x); putchar(' '); } inline void writeln(long long x) { write(x); putchar('\n'); } int fir[200010], fa[200010], dep[200010], h[200010]; std::vector<int> son[200010]; int ans[200010]; int n; int cmp(int x, int y) { return h[x] < h[y]; } int main() { n = read(); dep[0] = 0; fa[0] = -1; for (int i = 1; i < n; i++) { fa[i] = read(); dep[i] = dep[fa[i]] + 1; son[fa[i]].push_back(i); } for (int i = n - 1; i >= 0; i--) { h[i] = 1; for (int j : son[i]) h[i] = std::max(h[i], h[j] + 1); } for (int i = 0; i < n; i++) std::sort(son[i].begin(), son[i].end(), cmp); int now = 0, tot = 0; while (son[now].size()) now = son[now].back(); while (~fa[now]) { if (son[fa[now]].size() == 1) { now = fa[now]; continue; } ans[++tot] = now; son[fa[now]].pop_back(); fa[now] = son[fa[now]].back(); son[fa[now]].push_back(now); } now = 0; for (int i = 1; i < n; i++) { writesp(now); now = son[now][0]; } writeln(now); writeln(tot); for (int i = tot; i; i--) writesp(ans[i]); putchar('\n'); return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, m, xc, yc, zc; int f[305][305], a[305][305]; int main() { scanf("%d %d", &n, &m); memset(f, 127, sizeof(f)); memset(a, 127, sizeof(a)); for (int i = 1; i <= m; i++) { scanf("%d %d %d", &xc, &yc, &zc); f[xc][yc] = zc; f[yc][xc] = zc; a[xc][yc] = zc; a[yc][xc] = zc; } for (int i = 1; i <= n; i++) f[i][i] = 0; for (int k = 1; k <= n; k++) for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if ((f[i][k] != 2139062143) && (f[k][j] != 2139062143)) f[i][j] = min(f[i][j], f[i][k] + f[k][j]); double ans = 1e10, maxc; for (int i = 1; i <= n; i++) { maxc = 0; for (int j = 1; j <= n; j++) maxc = max(maxc, double(f[i][j])); ans = min(ans, maxc); } double ttj; int maxj[10005]; pair<int, int> e[10005]; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (a[i][j] != 2139062143) { for (int k = 1; k <= n; k++) e[k] = make_pair(f[k][i], f[k][j]); sort(e + 1, e + n + 1); maxj[n + 1] = 0; for (int k = n; k >= 1; k--) maxj[k] = max(maxj[k + 1], e[k].second); for (int k = 1; k <= n; k++) { ttj = double(e[k].first + maxj[k + 1] + a[i][j]) / 2; if ((ttj - e[k].first > 0) && (ttj - e[k].first < a[i][j])) ans = min(ans, double(e[k].first + maxj[k + 1] + a[i][j]) / 2); ans = min(ans, double(max(e[k].first, maxj[k + 1])) + a[i][j]); } } printf("%.20lf\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; vector<long long> divs; long long g(long long a, long long b) { if (a > b) { swap(a, b); } if (a == 0) { return b; } return g(b % a, a); } int main() { long long n, m, gcd = 1; cin >> n >> m; for (long long i = 0; i < n; i++) { long long a; cin >> a; a--; if (i == 0) { gcd = a; } else { gcd = g(gcd, a); } } for (long long i = 1; i * i <= gcd; i++) { if (gcd % i) { continue; } divs.push_back(i); if (i * i != gcd) { divs.push_back(gcd / i); } } for (long long i = 0; i < divs.size(); i++) { while (divs[i] % 2 == 0) { divs[i] /= 2; } } sort(divs.begin(), divs.end()); long long ans = 0; for (long long i = 0; i < divs.size(); i++) { if (i && divs[i] == divs[i - 1]) { continue; } for (long long st = 0; (1LL << st) * divs[i] <= m; st++) { long long d = divs[i] * (1LL << st); long long cpos = m - d; long long ds = d, cntd = 0; for (; ds % 2 == 0; cntd++) { ds /= 2; } long long cnt = cntd; ds = d / (1 << cnt); if (gcd % ds == 0) { ans += cpos; } } } cout << ans << "\n"; return 0; }
5
#include <bits/stdc++.h> using namespace std; void sol() { long long int n; cin >> n; string a; cin >> a; int cnt = 0; for (int i = 0; i < n; i++) { if (a[i] == '2') { cnt++; } } if (cnt > 0 && cnt < 3) { cout << "NO" << endl; return; } char ans[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ans[i][j] = '0'; } } cout << "YES" << endl; for (int i = 0; i < n; i++) { int flag = 0; for (int j = 0; j < n; j++) { if (i == j) { ans[i][j] = 'X'; } else if (a[i] == '1') { ans[i][j] = '='; } else if (a[j] == '1') { ans[i][j] = '='; } if (flag) { a[i] = '-'; } else if (ans[i][j] == '0') { if (ans[i][j] == '0') { ans[i][j] = '+'; ans[j][i] = '-'; } flag = 1; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (ans[i][j] == '0') { ans[i][j] = '='; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << ans[i][j]; } cout << endl; } } int main() { long long int t; cin >> t; while (t--) { sol(); } }
2
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF 1LL<<62 #define inf 1000000007 ll f,od; bool ev; int main() { ll n; cin>>n; for(int i=0;i<n;i++){ ll x; cin>>x; if(x%4==0){ f++; } else if(x%2==0){ ev=true; } else{ od++; } } if(ev){ od++; } cout << (od-1<=f?"Yes":"No"); return 0; }
0
#include<iostream> using namespace std; int main(){ int N,M,P; cin>>N>>M>>P; int u[123],v[123]; for(int i=0;i<M;i++){ cin>>u[i]>>v[i]; u[i]--; v[i]--; } double dp[1<<14]={0,1}; for(int i=2;i<1<<N;i++){ if(!(i&1))continue; dp[i]=1; for(int j=1;j<i;j=(j+1+~i)&i){ double d=dp[j]; for(int k=0;k<M;k++){ if(i>>u[k]&1&&i>>v[k]&1&&j>>u[k]&1^j>>v[k]&1){ d*=P/100.; } } dp[i]-=d; } } cout.precision(9); cout<<fixed<<dp[(1<<N)-1]<<endl; }
0
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } string s; cin >> s; vector<long long> b; for (int i = n - 1; i >= 0; i--) { long long v = a[i]; for (auto x : b) { v = min(v, v ^ x); } if (v) { if (s[i] == '1') { cout << 1 << endl; return; } b.push_back(v); } } cout << 0 << endl; } int main(){ int t; cin >> t; while (t--) { solve(); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-10; const long long mod = 1e9 + 7; const long long INF = (long long)1e14 + 5; ; const int maxn = 1e5 + 5; long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int n, m, k; struct node { int s, t, d, w; } a[maxn]; long long dp[maxn][205]; int tp[205]; int cmp(node a, node b) { return a.s < b.s; } struct cmp2 { bool operator()(pair<int, int> a, pair<int, int> b) { if (a.first == b.first) return a.second < b.second; return a.first < b.first; } }; struct cmp3 { bool operator()(node a, node b) { return a.t > b.t; } }; priority_queue<node, vector<node>, cmp3> pq; multiset<pair<int, int>, cmp2> second; int main(void) { scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= k; i++) { scanf("%d%d%d%d", &a[i].s, &a[i].t, &a[i].d, &a[i].w); } sort(a + 1, a + k + 1, cmp); for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { dp[i][j] = INF; } } int tp = 1; dp[0][0] = 0; for (int i = 0; i <= n - 1; i++) { while (tp <= k) { if (a[tp].s <= i + 1) { pq.push(a[tp]); second.insert(pair<int, int>(a[tp].w, a[tp].d)); ++tp; } else break; } while (!pq.empty()) { node tmp = pq.top(); if (tmp.t < i + 1) { pq.pop(); second.erase(second.find(pair<int, int>(tmp.w, tmp.d))); } else break; } for (int j = 0; j <= min(i, m); j++) { dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j]); if (!second.empty()) { pair<int, int> tmp = *second.rbegin(); dp[tmp.second][j] = min(dp[tmp.second][j], dp[i][j] + tmp.first); } else dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]); } } long long ans = INF; for (int i = 0; i <= m; i++) ans = min(ans, dp[n][i]); printf("%lld\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int MAXN = 15; const int MAXM = 10; const int MAXS = (1 << MAXN); const int MAXLEN = 500000; const int INF = 0x3f3f3f3f; template <class T> void Read(T &x) { x = 0; char c = getchar(); bool flag = 0; while (c < '0' || '9' < c) { if (c == '-') flag = 1; c = getchar(); } while ('0' <= c && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } if (flag) x = -x; } map<string, int> mp; int A[MAXLEN + 10]; int nxt[MAXLEN + 10][MAXN + 5]; int pos[MAXM + 5], len[MAXM + 5]; int n, m, ed; int dp[MAXS + 10][MAXN * MAXN + 10]; int cnts[MAXS + 10]; inline int solve(int x) { memset(dp, 0x3f, sizeof(dp)); dp[0][0] = 0; for (int s = 0; s < ed - 1; ++s) { for (int i = 0; i <= cnts[s] * (cnts[s] - 1) / 2; ++i) { if (dp[s][i] == INF) continue; for (int j = 1; j <= n; ++j) if ((s >> (j - 1)) & 1 ^ 1) { int ss = s ^ (1 << (j - 1)); int delta = cnts[s >> j]; int t = (dp[s][i] == len[x] ? 0 : nxt[pos[x] + dp[s][i] + 1][j]); if (t) dp[ss][i + delta] = min(dp[ss][i + delta], t); } } } for (int i = 0; i <= n * (n - 1) / 2; ++i) if (dp[ed - 1][i] < INF) return i; return INF; } int main() { string str; int a; Read(n), ed = (1 << n); for (int i = 1; i < ed; ++i) cnts[i] = cnts[i >> 1] + (i & 1); for (int i = 1; i <= n; ++i) cin >> str, mp[str] = i; Read(m); for (int i = 1; i <= m; ++i) { pos[i] = pos[i - 1] + len[i - 1]; Read(a); while (a--) { cin >> str; A[pos[i] + (++len[i])] = mp[str]; if (!A[pos[i] + len[i]]) --len[i]; } for (int j = len[i]; j >= 1; --j) { for (int k = 1; k <= n; ++k) nxt[pos[i] + j][k] = nxt[pos[i] + j + 1][k]; nxt[pos[i] + j][A[pos[i] + j]] = j; } } int ans = INF; int p = 0; for (int i = 1; i <= m; ++i) { int t = solve(i); if (t < ans) ans = t, p = i; } if (ans == INF) puts("Brand new problem!"); else { ans = n * (n - 1) / 2 - ans + 1; printf("%d\n", p); printf("[:"); for (int i = 1; i <= ans; ++i) putchar('|'); printf(":]\n"); } }
4
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; while (q--) { long long int a[3]; cin >> a[0] >> a[1] >> a[2]; sort(a, a + 3); if (a[0] != a[1] && a[2] != a[1]) { a[0]++; a[2]--; } else if (a[0] == a[1] && a[0] != a[2]) { a[0]++; a[1]++; if (a[0] != a[2]) a[2]--; } else if (a[2] == a[1] && a[0] != a[2]) { a[2]--; a[1]--; if (a[0] != a[2]) a[0]++; } cout << (a[2] - a[1]) + (a[2] - a[0]) + (a[1] - a[0]) << endl; } }
1
#include<bits/stdc++.h> using namespace std; int main() { int i,j,k,l; while(cin>>l){ j=l/100; k=l%100; if(j<=12&&k<=12&&j!=0&&k!=0) cout<<"AMBIGUOUS"<<endl; else if(j<=12&&j!=0) cout<<"MMYY"<<endl; else if(k<=12&&k!=0) cout<<"YYMM"<<endl; else cout<<"NA"<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 1; int h[maxn]; int main() { int n; cin >> n; h[0] = 0; for (int(i) = (1); (i) <= (n); ++(i)) cin >> h[i]; long long sum = 0; long long minSum = 0; for (int(i) = (0); (i) < (n); ++(i)) sum += h[i] - h[i + 1], minSum = min(minSum, sum); cout << max(0LL, -minSum); }
2
#include <bits/stdc++.h> using namespace std; bool compare(string &s1, string &s2) { return s1.size() < s2.size(); } bool sortmahstyle(const pair<int, int> &a, const pair<int, int> &b) { if (a.first < b.first) return true; return false; } void solve() { long long int n, op; cin >> n >> op; string s; cin >> s; for (int i = 0; i < n - 2 && op > 0; i++) { if (s[i] != '4' && s[i + 1] == '4' && s[i + 2] == '7') { if (i % 2) s[i + 2] = '4', op--; else s[i + 1] = '7', op--; } else if (s[i] == '4' && s[i + 1] == '7' && s[i + 2] != '7') { if (i % 2 == 0) s[i + 1] = '4', op--; else s[i] = '7', op--; } else if (s[i] == '4' && s[i + 1] == '7' && s[i + 2] == '7') { if (i % 2 == 0) { if (op % 2) { s[i + 1] = '4'; break; } else { break; } } else s[i] = '7', op--; } else if (s[i] == '4' && s[i + 1] == '4' && s[i + 2] == '7') { if (i % 2 == 0) { if (op % 2) { s[i + 1] = '7'; break; } else { break; } } else s[i + 2] = '4', op--; } } cout << s << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t = 1; while (t-- > 0) solve(); return 0; }
4
#include<bits/stdc++.h> using namespace std; int main(){ int n,k,sum=0,x,y; cin>>n>>k>>x>>y; for(int i=0;i<n;i++){ sum+=(i<k)?x:y; } cout<<sum; }
0
#include <bits/stdc++.h> #define inf 0x3f3f3f3f #define mod 998244353 #define int long long using namespace std; const int N=3*1e5+100; int n,cnt[5],ans,vi[N],id[30]; char s[N]; vector <int> p[N]; signed main() { id['R'-'A']=1;id['B'-'A']=2;id['G'-'A']=3; scanf("%lld",&n); scanf("%s",s+1); for (int i=1;i<=3*n;i++) p[id[s[i]-'A']].push_back(i); sort(p[1].begin(),p[1].end()); sort(p[2].begin(),p[2].end()); sort(p[3].begin(),p[3].end()); for (int i=0;i<n;i++) { int MAX=0,MIN=inf; for (int j=1;j<=3;j++) MAX=max(MAX,p[j][i]),MIN=min(MIN,p[j][i]); vi[MAX]=1; vi[MIN]=3; for (int j=1;j<=3;j++) if (p[j][i]!=MAX && p[j][i]!=MIN) vi[p[j][i]]=2; } ans=1; cnt[3]=n; for (int i=1;i<=3*n;i++) { ans=(ans*cnt[vi[i]])%mod; cnt[vi[i]]--;cnt[vi[i]-1]++; } printf("%lld\n",ans); }
0
#include <bits/stdc++.h> using namespace std; long long n, m, u, v, edges, ans; vector<long long> adj[100001], nodes; bool visit[100001]; void dfs(int node) { nodes.push_back(node); visit[node] = 1; for (long long i = 0; i < adj[node].size(); i++) { if (!visit[adj[node][i]]) dfs(adj[node][i]); } } int main() { cin >> n >> m; for (long long i = 0; i < m; i++) { cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } for (long long i = 1; i <= n; i++) { if (!visit[i]) { nodes.clear(); edges = 0; dfs(i); for (long long i = 0; i < nodes.size(); i++) edges += adj[nodes[i]].size(); if (edges / 2 < nodes.size()) ans++; } } cout << ans; }
5
#include <bits/stdc++.h> using namespace std; const int MAXN = 50 + 2; int N; char chess[MAXN][MAXN]; char ans[MAXN * 2 + 1][MAXN * 2 + 1]; char new_chess[MAXN][MAXN]; char get() { char ch = getchar(); while (ch != 'o' && ch != 'x' && ch != '.') ch = getchar(); return ch; } int main() { scanf("%d", &N); for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) chess[i][j] = get(); for (int i = 1; i <= N * 2 - 1; i++) for (int j = 1; j <= N * 2 - 1; j++) ans[i][j] = 'x'; ans[N][N] = 'o'; for (int x = 1; x <= N; x++) { for (int y = 1; y <= N; y++) { if (chess[x][y] == 'o') { for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { if (chess[i][j] == '.') { ans[N + (i - x)][N + (j - y)] = '.'; } } } } } } for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) new_chess[i][j] = '.'; for (int x = 1; x <= N; x++) { for (int y = 1; y <= N; y++) { if (chess[x][y] == 'o') { new_chess[x][y] = 'o'; for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { if (ans[N + (i - x)][N + (j - y)] == 'x') if (new_chess[i][j] != 'o') new_chess[i][j] = 'x'; } } } } } bool bo = false; for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { if (chess[i][j] != new_chess[i][j]) { bo = true; break; } } } if (bo) { puts("NO"); } else { puts("YES"); for (int i = 1; i <= N * 2 - 1; i++) { for (int j = 1; j <= N * 2 - 1; j++) putchar(ans[i][j]); putchar('\n'); } } return 0; }
4
#include <map> #include <algorithm> #include <set> #include <iostream> using namespace std; int main() { ios::sync_with_stdio(false); map<int, set<int>> b; int h, w, n; cin >> h >> w >> n; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; b[y].insert(x); } int ans = h; int x = 1, y = 1; while (true) { auto it = b[y].upper_bound(x); int bot = (it == b[y].end()? h + 1: *it); ans = min(ans, bot - 1); if (x == h || b[y].count(x + 1)) { break; } x++; if (!b[y + 1].count(x)) y++; } cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; int dp[503][503], n, c[503]; int main() { cin >> n; for (int i = 0; i < n; i++) cin >> c[i]; for (int l = 0; l < n; l++) for (int i = 0; i < n - l; i++) { int j = i + l; if (i == j) { dp[i][j] = 1; continue; } dp[i][j] = dp[i + 1][j] + 1; if (c[i] == c[i + 1]) dp[i][j] = min(dp[i][j], dp[i + 2][j] + 1); if (j - i > 1) for (int k = i + 2; k <= j; k++) if (c[i] == c[k]) dp[i][j] = min(dp[i][j], dp[i + 1][k - 1] + dp[k + 1][j]); } cout << dp[0][n - 1]; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 4e5 + 7; int n, m, head[maxn], depth[maxn], fa[maxn][50], num = 0; int a[maxn]; struct node { int next, to; }; struct node edge[maxn]; void add(int u, int v) { edge[num].next = head[u]; edge[num].to = v; head[u] = num++; } void dfs(int u, int pre, int d) { fa[u][0] = pre, depth[u] = d; for (int i = head[u]; i != -1; i = edge[i].next) { int to = edge[i].to; if (to != pre) dfs(to, u, d + 1); } } void init(int root) { dfs(root, -1, 0); for (int j = 0; (1 << (j + 1)) < n; j++) { for (int i = 1; i <= n; i++) { if (fa[i][j] < 0) fa[i][j + 1] = -1; else fa[i][j + 1] = fa[fa[i][j]][j]; } } } int LCA(int u, int v) { if (depth[u] > depth[v]) swap(u, v); int temp = depth[v] - depth[u]; for (int i = 0; (1 << i) <= temp; i++) { if ((1 << i) & temp) v = fa[v][i]; } if (v == u) return u; for (int i = log(n * 1.0) / log(2.0); i >= 0; i--) { if (fa[u][i] != fa[v][i]) { u = fa[u][i], v = fa[v][i]; } } return fa[u][0]; } int main() { memset(head, -1, sizeof(head)); int u, v, root, k; cin >> n >> k; for (int i = 0; i < n - 1; i++) { scanf("%d%d", &u, &v); add(u, v); add(v, u); } init(1); while (k--) { int m, pos = 1, flag = 1; cin >> m; for (int i = 0; i < m; i++) { scanf("%d", &a[i]); if (depth[a[i]] > depth[pos]) pos = a[i]; } for (int i = 0; i < m; i++) { int l = LCA(pos, a[i]); if (abs(depth[l] - depth[a[i]]) > 1) { flag = 0; break; } } if (flag) printf("YES\n"); else printf("NO\n"); } }
5
#include "bits/stdc++.h" using namespace std; #define rep(i,n) for(int i=0;i<n;++i) typedef long long ll; typedef vector<ll> vll; ll mod = 1e9+7; int main() { ll n;cin >> n; vll a(n), t(n); rep(i,n) cin>>a[i]; rep(i,n) cin>>t[i]; vll x(n,-1); if(a[n-1] != t[0]) { puts("0"); return 0; } x[0]=a[0],x[n-1]=t[n-1]; for(int i=1;i<n;i++){ if(a[i] > a[i-1]){ if(x[i] >= 0 && x[i] != a[i]) { puts("0"); return 0; } x[i] = a[i]; } } for(int i=n-2;i>=0;i--){ if(t[i] > t[i+1]){ if(x[i] >= 0 && x[i] != t[i]) { puts("0"); return 0; } x[i] = t[i]; } } ll ret = 1; rep(i,n){ //cout << x[i]; if(x[i] == -1){ ret *= (ll)min(a[i],t[i]); ret %= mod; } else if(x[i] > min(a[i],t[i])) { puts("0"); return 0; } } cout << ret << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void chkmax(T& x, U y) { if (x < y) x = y; } template <typename T, typename U> inline void chkmin(T& x, U y) { if (y < x) x = y; } const int INF = 1e9; int main() { int n; scanf("%d", &n); int L = -INF, R = INF; for (int i = 0; i < n; i++) { int x, y; scanf("%d%d", &x, &y); if (y == 1) { chkmax(L, 1900); } else { chkmin(R, 1899); } if (L > R) { puts("Impossible"); return 0; } L += x, R += x; } if (R > INF / 2) { puts("Infinity"); return 0; } cout << R << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAX = 500009; int n, m, k, x, y; int a[MAX]; int ji[MAX]; int ji2[MAX]; int flag, max1, sum1, bj, max2, sum2; vector<int> g[MAX]; int main() { memset(ji2, 0, sizeof(ji2)); memset(ji, 0, sizeof(ji)); memset(a, 0, sizeof(a)); scanf("%d", &n); max1 = -2000000000; max2 = -2000000000; sum1 = 1; sum2 = 2; bj = 0; for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); if (a[i] == max1) { sum1++; } if (a[i] > max1) { max2 = max1; max1 = a[i]; sum2 = sum1; sum1 = 1; } else { if (a[i] > max2 && a[i] < max1) { max2 = a[i]; sum2 = 1; } else if (a[i] == max2) sum2++; } } for (int i = 1; i <= n - 1; i++) { scanf("%d%d", &x, &y); if (a[x] == max1) ji[y]++; if (a[y] == max1) ji[x]++; if (a[x] == max1 && a[y] == max2 && max2 == max1 - 1) { ji2[x]++; } if (a[x] == max2 && a[y] == max1 && max2 == max1 - 1) { ji2[y]++; } } if (sum1 == 1) { if (max2 != max1 - 1) flag = 0; else { flag = 1; for (int i = 1; i <= n; i++) { if (a[i] == max1 && ji2[i] == sum2) { flag = 0; break; } } } } else { flag = 2; for (int i = 1; i <= n; i++) { if (a[i] != max1 && ji[i] == sum1) { flag = 1; break; } if (a[i] == max1 && ji[i] + 1 == sum1) { flag = 1; break; } } } printf("%d\n", max1 + flag); return 0; }
3
#include <bits/stdc++.h> using namespace std; template <typename T> bool scan(T &n) { n = 0; bool got = false; bool negative = false; char c = getchar(); if (c == EOF) return false; while (c<'0' | c> '9') { if (c == '-') negative = true; c = getchar(); } while (c >= '0' && c <= '9') { got = true; n = n * 10 + c - 48; c = getchar(); } if (negative) n = ~(n - 1); return got; } template <typename T> inline T GCD(T a, T b) { a = abs(a); b = abs(b); while (b) { a = a % b; swap(a, b); } return a; } template <typename T> inline T LCM(T x, T y) { T tp = GCD(x, y); if ((x / tp) * 1. * y > 9e18) return 9e18; return (x / tp) * y; } template <typename T> inline T BigMod(T A, T B, T M) { T ret = 1; while (B) { if (B & 1) ret = (ret * A) % M; A = (A * A) % M; B = B >> 1; } return ret; } template <typename T> inline T InvMod(T A, T M) { return BigMod(A, M - 2, M); } int gcdr(int a, int b) { if (a == 0) return b; return gcdr(b % a, a); } long long poW(long long x, long long n) { if (n == 0) return 1; else if (n % 2 == 0) return poW(x * x, n / 2); else return x * poW(x * x, ((n - 1) / 2)); } int main() { long long testCase, cs; set<long long> S; for (long long i = 3LL; i <= 64LL; i++) { for (long long j = 2LL; pow(j, i) <= 1e18; j++) { long long t = poW(j, i); if (t > 1e18 || t <= 0LL) break; long long sq = sqrt(t); if (sq * sq == t) continue; S.insert(t); } } vector<long long> reslt; for (auto it = S.begin(); it != S.end(); ++it) { reslt.push_back(*it); } reslt.push_back(2e18); scan(testCase); for (cs = 1; cs <= testCase; cs++) { long long low, high; long long ans; scan(low); scan(high); low = low; long long x = (upper_bound(reslt.begin(), reslt.end(), high) - reslt.begin()); long long y = (lower_bound(reslt.begin(), reslt.end(), low) - reslt.begin()); long long r2 = ((long long)(sqrt(high)) - (long long)(sqrt(low))); long long sq = sqrt(low); if (sq * sq == low) r2++; ans = (x - y) + r2; printf("%lld\n", ans); } }
3
#include "bits/stdc++.h" using namespace std; int main() { int n; int m; int d; while (~scanf("%d%d%d", &n, &m, &d)) { double ans = (d == 0 ? n : (n - d) * 2) / ((double)n * n); ans *= m - 1; printf("%.10f\n", ans); } }
0
#include <bits/stdc++.h> using namespace std; long long S[1200008]; long long A[300002]; int n; void add(int l, int r, int num, int id = 1, int sl = 0, int sr = n + 1) { if (r <= sl or sr <= l) return; if (l <= sl and sr <= r) { S[id] += num; return; } int mid = (sl + sr) / 2; S[2 * id] += S[id]; S[2 * id + 1] += S[id]; S[id] = 0; add(l, r, num, 2 * id, sl, mid); add(l, r, num, 2 * id + 1, mid, sr); } void get(int ind, int id = 1, int l = 0, int r = n + 1) { if (l > ind or ind >= r) return; if (r - l < 2) { A[l] += S[id]; S[id] = 0; return; } S[2 * id] += S[id]; S[2 * id + 1] += S[id]; S[id] = 0; int mid = (l + r) / 2; get(ind, 2 * id, l, mid); get(ind, 2 * id + 1, mid, r); } multiset<int> ma; map<int, pair<int, int> > inters; void zp(int ind) { auto it = inters.insert(make_pair(ind, make_pair(ind + 1, ind + 1))).first; auto nx = next(it); if (nx != inters.end()) { if (it->second.second == nx->first) { it->second = nx->second; ma.erase(ma.find(nx->second.second - nx->first)); inters.erase(nx); } } if (it != inters.begin()) { auto pv = prev(it); if (pv->second.first == it->first) { ma.erase(ma.find(pv->second.second - pv->first)); pv->second = it->second; ma.insert(pv->second.second - pv->first); inters.erase(it); } else ma.insert(it->second.second - it->first); } else ma.insert(it->second.second - it->first); } void zn(int ind) { auto it = inters.insert(make_pair(ind, make_pair(ind, ind + 1))).first; auto nx = next(it); if (nx != inters.end()) { if (it->second.second == nx->second.first) { it->second.second = nx->second.second; ma.erase(ma.find(nx->second.second - nx->first)); inters.erase(nx); } } if (it != inters.begin()) { auto pv = prev(it); if (pv->second.second == it->first) { ma.erase(ma.find(pv->second.second - pv->first)); pv->second.second = it->second.second; ma.insert(pv->second.second - pv->first); inters.erase(it); } else ma.insert(it->second.second - it->first); } else ma.insert(it->second.second - it->first); } void pz(int ind) { auto it = prev(inters.upper_bound(ind)); auto nx = make_pair(ind + 1, it->second); ma.erase(ma.find(it->second.second - it->first)); it->second = pair<int, int>(ind, ind); if (it->second.second - it->first == 0) inters.erase(it); else ma.insert(it->second.second - it->first); if (nx.second.second - nx.first > 0) inters.insert(nx), ma.insert(nx.second.second - nx.first); } void nz(int ind) { auto it = prev(inters.upper_bound(ind)); auto nx = make_pair(ind + 1, it->second); ma.erase(ma.find(it->second.second - it->first)); it->second.second = ind; nx.second.first = ind + 1; if (it->second.second - it->first == 0) inters.erase(it); else ma.insert(it->second.second - it->first); if (nx.second.second - nx.first > 0) inters.insert(nx), ma.insert(nx.second.second - nx.first); } void query(int l, int r, int d) { if (!d) return; long long a, b; get(l - 1); get(r + 1); get(l); if (l != r) get(r); a = A[l] - A[l - 1]; b = A[r + 1] - A[r]; add(l, r + 1, d); A[l] += d; if (l != r) A[r] += d; if (l - 1 >= 1) { if (a < 0) { if (A[l] == A[l - 1]) nz(l - 1); else if (A[l] > A[l - 1]) { nz(l - 1); zp(l - 1); }; } else if (a == 0) zp(l - 1); } if (r < n) { if (b > 0) { if (A[r + 1] == A[r]) pz(r); else if (A[r + 1] < A[r]) { pz(r); zn(r); }; } else if (b == 0) zn(r); } A[l] -= d; if (l != r) A[r] -= d; } int main() { scanf("%d", &n); int cur; ma.insert(0); for (int i = 1; i <= n; ++i) { scanf("%I64d", &A[i]); } for (int i = 1; i < n;) { if (A[i] != A[i + 1]) { int ind = i; for (; i < n and A[i] < A[i + 1]; ++i) ; pair<int, int> res(i, 0); for (; i < n and A[i] > A[i + 1]; ++i) ; res.second = i; inters[ind] = res; ma.insert(i - ind); } else ++i; } int m, l, r, d; scanf("%d", &m); while (m--) { scanf("%d %d %d", &l, &r, &d); query(l, r, d); printf("%d\n", *ma.rbegin() + 1); } return 0; }
5
#include <bits/stdc++.h> constexpr int N = 100, P = 1000000007; int n, k; int dp[N + 1][N * N + 1]; int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cin >> n >> k; dp[n][0] = 1; for (int i = n; i > 0; --i) { for (int j = 0; j <= n * n; ++j) { for (int v = 0; v <= k; ++v) { if (dp[i][j] == 0) continue; int t = v <= i ? (v + j) / i : 0; dp[i - 1][j + t] = (dp[i - 1][j + t] + dp[i][j]) % P; } } } int ans = 0; for (int i = 0; i <= n * n; ++i) ans = (ans + 1ll * i * dp[0][i]) % P; int tot = 1ll * k * n * (P + 1) / 2 % P; for (int i = 0; i < n; ++i) tot = 1ll * tot * (k + 1) % P; ans = (tot - ans + P) % P; std::cout << ans << "\n"; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int N; scanf("%d", &N); for (int i = 0; i < (N); i++) { int R = N - 4 * i; if (R >= 0 && R % 7 == 0) { for (int j = 0; j < (i); j++) putchar('4'); for (int j = 0; j < (R / 7); j++) putchar('7'); putchar('\n'); return 0; } } printf("-1\n"); return 0; }
1
#include <bits/stdc++.h> using namespace std; struct ww { int x, y; } an[(1 << 20)], tt[(1 << 20)]; int i, j, k, n, m, s, u, t, re; int a[30], b[30], c[30]; inline void Do(int n, int x, int y) { int i; for (i = 1; i <= n; i++) an[++re] = (ww){x + 1, y + 1}; } void dfs(int n, int x, int y, int ty) { if (n > s) return; int i; for (i = 0; i <= 2; i++) if (i != x && i != y) break; if (ty && b[n] > 1 && c[n] % 2 == 0) { if (n == s) { Do(b[n] - 1, x, i); Do(1, x, y); Do(b[n] - 1, i, y); return; } dfs(n + 1, x, y, 0); Do(b[n], x, i); dfs(n + 1, y, x, 0); Do(b[n], i, y); dfs(n + 1, x, y, 1); } else { dfs(n + 1, x, i, 0); c[n] ^= 1; Do(b[n], x, y); dfs(n + 1, i, y, ty & 1); } } int main() { scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &k); if (a[s] != k) a[++s] = k, b[s] = 1; else b[s]++; } dfs(1, 0, 2, 1); printf("%d\n", re); for (i = 1; i <= re; i++) printf("%d %d\n", an[i].x, an[i].y); return 0; }
4
#include <bits/stdc++.h> using namespace std; long long a[100005]; int par[100005]; priority_queue<pair<long long, int> > pq; long long tim(long long carrot, int parts) { long long w = carrot / parts; return (w) * (w) * (parts - carrot % parts) + (w + 1) * (w + 1) * (carrot % parts); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; par[i] = 1; pq.push({tim(a[i], 1) - tim(a[i], 2), i}); } for (int i = 0; i < k - n; i++) { pair<long long, int> top = pq.top(); pq.pop(); long long carrot = a[top.second]; int parts = ++par[top.second]; pq.push({tim(carrot, parts) - tim(carrot, parts + 1), top.second}); } long long ans = 0; for (int i = 0; i < n; i++) { ans += tim(a[i], par[i]); } cout << ans << endl; return 0; }
5
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(0); //e int n; cin >> n; vector<int> pos(n); for (int i = 0; i < n; i++) { int a; cin >> a; pos[--a] = i; } long long ans = 0; set<int> st; st.insert(-1); st.insert(n); st.insert(pos[n - 1]); for (int i = n - 2; i >= 0; i--) { auto a = st.upper_bound(pos[i]); a--; auto b = st.upper_bound(pos[i]); if (*b < n) { auto bb = b; bb++; ans += (1LL * (pos[i] - *a) * (*bb - *b)) * (i + 1); } if (*a > -1) { auto aa = a; aa--; ans += (1LL * (*b - pos[i]) * (*a - *aa)) * (i + 1); } st.insert(pos[i]); } cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 500005; string s; int n, a[maxn]; struct node { int x1, y1, x2, y2; } p[5]; bool cov(node a, node b) { if (a.x1 >= b.x1 && a.y1 >= b.y1 && a.x2 <= b.x2 && a.y2 <= b.y2) return true; return false; } int main() { cin >> p[1].x1 >> p[1].y1 >> p[1].x2 >> p[1].y2; cin >> p[2].x1 >> p[2].y1 >> p[2].x2 >> p[2].y2; cin >> p[3].x1 >> p[3].y1 >> p[3].x2 >> p[3].y2; if (cov(p[1], p[2]) || cov(p[1], p[3])) { cout << "NO" << endl; return 0; } if (p[1].x1 >= p[2].x1 && p[1].y1 >= p[2].y1 && p[1].x2 >= p[2].x2 && p[1].y2 <= p[2].y2) { if (p[1].x2 <= p[3].x2 && p[1].y2 <= p[3].y2 && p[1].x1 <= p[3].x1 && p[1].y1 >= p[3].y1 && p[2].x2 >= p[3].x1) { cout << "NO" << endl; return 0; } } if (p[1].x1 >= p[3].x1 && p[1].y1 >= p[3].y1 && p[1].x2 >= p[3].x2 && p[1].y2 <= p[3].y2) { if (p[1].x2 <= p[2].x2 && p[1].y2 <= p[2].y2 && p[1].x1 <= p[2].x1 && p[2].y1 >= p[2].y1 && p[2].x1 <= p[3].x2) { cout << "NO" << endl; return 0; } } if (p[1].x1 >= p[2].x1 && p[1].y1 >= p[2].y1 && p[1].x2 <= p[2].x2 && p[1].y2 >= p[2].y2) { if (p[1].x2 <= p[3].x2 && p[1].y2 <= p[3].y2 && p[1].x1 >= p[3].x1 && p[1].y1 <= p[3].y1 && p[2].y2 >= p[3].y1) { cout << "NO" << endl; return 0; } } if (p[1].x1 >= p[3].x1 && p[1].y1 >= p[3].y1 && p[1].x2 <= p[3].x2 && p[1].y2 >= p[3].y2) { if (p[1].x2 <= p[2].x2 && p[1].y2 <= p[2].y2 && p[1].x1 >= p[2].x1 && p[2].y1 <= p[2].y1 && p[2].y1 <= p[3].y2) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; template <typename A, typename B> int print_value(A title, B val) { cout << title << " = " << val << "\n"; return 1; } int break_point() { char c; while ((c = getchar()) != '\n') ; return 0; } template <typename A> int logg(A v) { cout << v << "\n"; return 0; } void read_intq(int &r) { r = 0; char c; while (1) { c = getchar(); if (c != ' ' && c != '\n') { r = c - '0'; break; } } while (1) { c = getchar(); if (c == ' ' || c == '\n') return; r = r * 10 + (c - '0'); } } void read_llq(long long &r) { r = 0; char c; while (1) { c = getchar(); if (c != ' ' && c != '\n') { r = c - '0'; break; } } while (1) { c = getchar(); if (c == ' ' || c == '\n') return; r = r * 10 + (c - '0'); } } char get_char() { char c; while (1) { c = getchar(); if (c != '\n' && c != ' ') return c; } } void run(); int main() { srand(time(NULL)); do { run(); if (0) { 0 ? printf("-------------------------------\n") : 0; 0 ? printf("-------------------------------\n") : 0; } } while (0); return 0; } struct point { int x, y; int m; int idx; bool operator<(const point &p) const { if (m & 1) return y < p.y; else return y > p.y; } }; int dist(point &p1, point &p2) { return abs(p1.x - p2.x) + abs(p1.y - p2.y); } priority_queue<point> q[1001]; void run() { int n; scanf("%d", &n); point p; for (int i = 0; i < n; i++) { scanf("%d%d", &p.x, &p.y); p.m = p.x / 1000; p.idx = i + 1; q[p.m].push(p); } for (int i = 0; i < 1001; i++) { while (!q[i].empty()) { printf("%d", q[i].top().idx); putchar(' '); q[i].pop(); } } putchar('\n'); }
3
#include <bits/stdc++.h> using namespace std; const int Imx = 2147483647; const long long Lbig = 2e18; const int mod = 1e9 + 7; struct fastio { char s[100000]; int it, len; fastio() { it = len = 0; } inline char get() { if (it < len) return s[it++]; it = 0; len = fread(s, 1, 100000, stdin); if (len == 0) return EOF; else return s[it++]; } bool notend() { char c = get(); while (c == ' ' || c == '\n') c = get(); if (it > 0) it--; return c != EOF; } } _buff; inline long long getnum() { long long r = 0; bool ng = 0; char c; c = _buff.get(); while (c != '-' && (c < '0' || c > '9')) c = _buff.get(); if (c == '-') ng = 1, c = _buff.get(); while (c >= '0' && c <= '9') r = r * 10 + c - '0', c = _buff.get(); return ng ? -r : r; } template <class T> inline void putnum(T x) { if (x < 0) putchar('-'), x = -x; register short a[20] = {}, sz = 0; while (x) a[sz++] = x % 10, x /= 10; if (sz == 0) putchar('0'); for (int i = sz - 1; i >= 0; i--) putchar('0' + a[i]); } inline char getreal() { char c = _buff.get(); while (c <= 32) c = _buff.get(); return c; } long long qpow(long long x, long long k) { return k == 0 ? 1 : 1ll * qpow(1ll * x * x % mod, k >> 1) * (k & 1 ? x : 1) % mod; } int n; vector<int> vy; int getidy(int y) { return upper_bound(vy.begin(), vy.end(), y) - vy.begin(); } bool ANS[100111]; struct node { set<int> st, tst; int mn, mx; int cmxu, hmxu; void add(int x) { st.insert(x); tst.insert(x); mx = *st.rbegin(); } void del(int x) { st.erase(x); tst.erase(x); assert(st.size() > 0); mx = *st.rbegin(); } node() { add(0); cmxu = hmxu = mn = 0; } void query(int x) { while (tst.size() > 0 && *tst.rbegin() >= x) ANS[*tst.rbegin()] = 1, tst.erase(--tst.end()); } } a[800111]; void updateu(int p) { a[p].cmxu = max(a[p >> 1].cmxu, a[p].mx); } void update(int p, bool isl) { a[p].mn = max(a[p].mx, (isl ? 0 : min(a[p << 1].mn, a[p << 1 | 1].mn))); } void deltag(int p, bool isl) { a[p].query(max(a[p].mn, max(a[p].hmxu, a[p].mx))); if (!isl) { int cmxu = max(a[p].hmxu, a[p].mx); a[p << 1].hmxu = min(a[p << 1].hmxu, cmxu), a[p << 1 | 1].hmxu = min(a[p << 1 | 1].hmxu, cmxu); } a[p].hmxu = a[p >> 1].cmxu; } void modify(int x, int y, int op, int v, int l, int r, int p = 1) { deltag(p, l == r); updateu(p); if (x <= l && r <= y) { if (op == 1) a[p].add(v); else a[p].del(v); updateu(p); update(p, l == r); if (l < r) { a[p << 1].hmxu = min(a[p << 1].hmxu, a[p].cmxu); a[p << 1 | 1].hmxu = min(a[p << 1 | 1].hmxu, a[p].cmxu); } a[p].query(max(a[p].mn, a[p].cmxu)); return; } int m = l + r >> 1; if (x <= m) modify(x, y, op, v, l, m, p << 1); if (m < y) modify(x, y, op, v, m + 1, r, p << 1 | 1); update(p, l == r); a[p].query(max(a[p].mn, a[p].cmxu)); } void pushdownall(int l, int r, int p) { deltag(p, l == r); updateu(p); a[p].query(max(a[p].mn, a[p].cmxu)); if (l == r) return; int m = l + r >> 1; pushdownall(l, m, p << 1); pushdownall(m + 1, r, p << 1 | 1); } struct query { int op, x, yl, yr, id; bool operator<(const query &t) const { return make_pair(make_pair(x, op), op == 1 ? -id : id) < make_pair(make_pair(t.x, t.op), t.op == 1 ? -t.id : t.id); } }; vector<query> qr; int main() { n = getnum(); for (int i = 1; i <= n; i++) { int xl, yl, xr, yr; xl = getnum(), yl = getnum(); xr = getnum(), yr = getnum(); yr--; vy.push_back(yl); vy.push_back(yr + 1); query tmp; tmp.yl = yl; tmp.yr = yr; tmp.id = i; tmp.x = xl; tmp.op = 1; qr.push_back(tmp); tmp.x = xr; tmp.op = 2; qr.push_back(tmp); } sort(vy.begin(), vy.end()); vy.erase(unique(vy.begin(), vy.end()), vy.end()); sort(qr.begin(), qr.end()); int sz = vy.size(); for (int i = 0; i < qr.size(); i++) { int yl = getidy(qr[i].yl); int yr = getidy(qr[i].yr); modify(yl, yr, qr[i].op, qr[i].id, 1, sz); } int ans = 0; for (int i = 0; i <= n; i++) ans += ANS[i]; cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> #pragma warning(disable : 6031) #pragma warning(disable : 4101) using namespace std; const int INF = 1e9; const long long LINF = 1e18; const double PI = acos(-1); const int MAXN = 200005; int n, m, l, r, j, best, ans, A[MAXN]; vector<pair<int, int> > B; void print_res(int ind) { if (ind == l || ind == r) { for (int i = 0; i < B[ind].second; i++) { printf("%d ", B[ind].first); } if (ind != r) { print_res(ind + 1); } } else { for (int i = 0; i < B[ind].second - 1; i++) { printf("%d ", B[ind].first); } print_res(ind + 1); printf("%d ", B[ind].first); } } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &A[i]); } sort(A, A + n); B = {{A[0], 1}}; for (int i = 1; i < n; i++) { if (A[i] == A[i - 1]) { B.back().second++; } else { B.push_back({A[i], 1}); } } m = ((int)(B).size()); for (int i = 0; i < m; i++) { ans = B[i].second; j = i + 1; while (j < n && B[j].first == B[j - 1].first + 1 && B[j].second > 1) { ans += B[j].second; j++; } if (j < n && B[j].first == B[j - 1].first + 1) { ans += 1; } if (ans > best) { l = i; r = j - 1; if (j < n && B[j].first == B[j - 1].first + 1) { r += 1; } best = ans; } i = j - 1; } printf("%d\n", best); print_res(l); printf("\n"); return 0; }
6
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") void scan() {} template <typename F, typename... R> void scan(F& f, R&... r) { cin >> f; scan(r...); } int di_; string dnms_, co_ = ","; void debug_() { cout << endl; } template <typename F, typename... R> void debug_(F& f, R&... r) { while (dnms_[di_] != ',') cout << dnms_[di_++]; di_++; cout << ": " << f << ","; debug_(r...); } template <class A, class B> ostream& operator<<(ostream& out, const pair<A, B>& a) { return out << "(" << a.first << "," << a.second << ")"; } template <class A> ostream& operator<<(ostream& out, const vector<A>& a) { out << ""; for (auto it = a.begin(); it != a.end(); it++) { if (it != a.begin()) out << " "; out << *it; } out << ""; return out; } template <class A, class B> istream& operator>>(istream& in, pair<A, B>& a) { in >> a.first >> a.second; return in; } template <class A> istream& operator>>(istream& in, vector<A>& a) { for (A& i : a) in >> i; return in; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, d, m; cin >> d >> n >> m; vector<pair<int, int> > v(m); cin >> v; sort(v.begin(), v.end()); while (v.back().first >= d) v.pop_back(); v.push_back({d, 0}); int currgas = n - v[0].first, maxgas = n, gassneeded = 0; int pos = (currgas >= 0) ? v[0].first : -1; if (pos == -1) { cout << -1 << endl; return 0; } vector<int> nextsmall(m, -1); stack<int> s; s.push(0); for (int i = 1; i < m; i++) { while (!s.empty() && v[s.top()].second > v[i].second) { nextsmall[s.top()] = i; s.pop(); } s.push(i); } int station = 0; long long int cost = 0; while (station < m) { gassneeded = min(maxgas, (nextsmall[station] == -1 ? d - v[station].first : (v[nextsmall[station]].first - v[station].first))); if (currgas < gassneeded) { long long int rem = gassneeded - currgas; currgas += rem; cost += rem * v[station].second; } station++; currgas -= v[station].first - v[station - 1].first; if (currgas < 0) break; pos += v[station].first - v[station - 1].first; } if (pos < d) cost = -1; cout << cost << endl; return 0; }
3
#include<stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<string> #include<string.h> #ifdef LOCAL #define eprintf(...) fprintf(stderr, __VA_ARGS__) #else #define NDEBUG #define eprintf(...) do {} while (0) #endif #include<cassert> using namespace std; typedef long long LL; typedef vector<int> VI; #define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i) #define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i) template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; } template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; } template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) { for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); } putchar('\n'); } struct Seg { bool fav; int height; Seg() { fav = false; height = -1; } Seg inc(bool fav_) const { Seg z = *this; z.fav = z.fav || fav_; z.height++; return z; } }; Seg operator+(const Seg &x, const Seg &y) { Seg z; z.fav = x.fav || y.fav; z.height = max(x.height, y.height); return z; } const int MAXN = 200111; int N; VI G[MAXN]; VI ord; char S[MAXN]; Seg up[MAXN], dw[MAXN]; vector<Seg> L[MAXN], R[MAXN]; LL answer; void treeDP() { ord.clear(); ord.reserve(N); ord.push_back(0); REP (i_, N) { int v = ord[i_]; EACH (e, G[v]) { ord.push_back(*e); G[*e].erase(remove(G[*e].begin(), G[*e].end(), v), G[*e].end()); // remove parent } } REP (i_, N) { int v = ord[N-1-i_]; int m = G[v].size(); L[v].assign(m+1, Seg()); R[v].assign(m+1, Seg()); REP (i, m) L[v][i+1] = L[v][i] + up[G[v][i]]; for (int i=m; i--; ) R[v][i] = up[G[v][i]] + R[v][i+1]; up[v] = R[v][0].inc(S[v] == '1'); // up[v] = L[v].all + v own } const int INF = 1<<29; dw[0] = Seg(); REP (i_, N) { int v = ord[i_]; //ans[v] = (R[v][0] + dw[v]).inc(S[v] == '1'); // when v == 0, dw[v] is an empty tree int lo = INF, hi[3] = { }; auto update = [&](int h) { hi[2] = h; if (hi[1] < hi[2]) swap(hi[1], hi[2]); if (hi[0] < hi[1]) swap(hi[0], hi[1]); }; if (S[v] == '1') { amin(lo, 0); } if (v) { if (dw[v].fav) amin(lo, dw[v].height+1); update(dw[v].height+1); // dw[v] * up[v]; if (dw[v].height <= up[v].height && dw[v].fav) { answer++; } else if (dw[v].height >= up[v].height && up[v].fav) { answer++; } } REP (i, G[v].size()) { int w = G[v][i]; if (up[w].fav) amin(lo, up[w].height+1); update(up[w].height+1); } if (lo <= hi[1]) { answer += hi[1] - lo + 1; } REP (i, G[v].size()) { int w = G[v][i]; dw[w] = (R[v][i+1] + dw[v] + L[v][i]).inc(S[v] == '1'); // add v own } } } void MAIN() { scanf("%d", &N); REP (i, N-1) { int x, y; scanf("%d%d", &x, &y); x--; y--; G[x].push_back(y); G[y].push_back(x); } scanf("%s", S); treeDP(); printf("%lld\n", answer); } int main() { int TC = 1; // scanf("%d", &TC); REP (tc, TC) MAIN(); return 0; }
0
#include <bits/stdc++.h> using namespace std; char op(char a) { return (a == 'U' ? 'R' : 'U'); } int main() { int n; string s; cin >> n >> s; stack<char> t; for (int i = 0; i < n; i++) { if (t.size() == 0) t.push(s[i]); else { if (t.top() == op(s[i])) { t.pop(); t.push('D'); } else { t.push(s[i]); } } } cout << t.size() << endl; }
1
#include <bits/stdc++.h> using namespace std; char s[100]; int toInt(char *p) { if (p[0] == 'A') return 0; if (p[0] == 'C' && p[1] == 'h') return 1; if (p[0] == 'C' && p[1] == 'l') return 2; if (p[0] == 'T') return 3; if (p[0] == 'D') return 4; if (p[0] == 'S') return 5; return 6; } int l[7][7]; int a[7]; int main() { int aa, bb, cc, n; scanf("%d\n", &n); for (int i = 0; i < n; i++) { gets(s); int h1 = toInt(strtok(s, " ")); strtok(NULL, " "); int h2 = toInt(strtok(NULL, " ")); l[h1][h2] = 1; } scanf("%d%d%d\n", &aa, &bb, &cc); int min_diff = 1 << 30; int min_diff_l; for (a[0] = 0; a[0] < 3; a[0]++) for (a[1] = 0; a[1] < 3; a[1]++) for (a[2] = 0; a[2] < 3; a[2]++) for (a[3] = 0; a[3] < 3; a[3]++) for (a[4] = 0; a[4] < 3; a[4]++) for (a[5] = 0; a[5] < 3; a[5]++) for (a[6] = 0; a[6] < 3; a[6]++) { int n0 = 0, n1 = 0, n2 = 0; for (int i = 0; i < 7; i++) if (a[i] == 0) n0++; else if (a[i] == 1) n1++; else n2++; if (n0 == 0 || n1 == 0 || n2 == 0) continue; int e1 = aa / n0; int e2 = bb / n1; int e3 = cc / n2; int mne = min(min(e1, e2), e3); int mxe = max(max(e1, e2), e3); int re = mxe - mne; if (re > min_diff) continue; int liking = 0; for (int i = 0; i < 7; i++) for (int j = 0; j < 7; j++) if (l[i][j] && a[i] == a[j]) liking++; if (re == min_diff && liking > min_diff_l) { min_diff_l = liking; continue; } if (re < min_diff) { min_diff = re; min_diff_l = liking; } } printf("%d %d", min_diff, min_diff_l); return 0; }
1