solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; int arr[105] = {0}; int main() { int n, d, res; cin >> n >> d; for (int i = 0; i < n; i++) cin >> arr[i]; res = 2; for (int i = 1; i < n; i++) { int dd = arr[i] - arr[i - 1]; if (dd > 2 * d) res += 2; else if (dd == 2 * d) res++; } cout << res; return 0; }
1
#include <bits/stdc++.h> using namespace std; char in[40][40]; int n, m, cnt[40][40], pl[40][40], all[40][4][2], aa[15], now[15], used[7], ans, domu[70], dom[70], mk[300]; int inv(int a) { return ((a & 7) << 3) + (a >> 3); } void dfs(int np) { int i, j; if (np == 14) { ans++; for (i = 0; i < 14; i++) { aa[i] = now[i]; } return; } for (i = 0; i <= 6; i++) { if (used[i] == 2) continue; for (j = 0; j < 4; j++) { int x = all[np][j][0], y = all[np][j][1]; int c = mk[in[x][y]]; if (dom[c] == 0) dom[c] += i + 1; else { int k = dom[c] + ((i + 1) << 3); int r = inv(k); if (domu[r] || domu[k]) { j--; break; } dom[c] = k; domu[r] = domu[k] = 1; } } if (j == 4) { used[i]++; now[np] = i; dfs(np + 1); used[i]--; } if (j == 4) j--; for (; j >= 0; j--) { int x = all[np][j][0], y = all[np][j][1]; int c = mk[in[x][y]]; if (dom[c] < 8) dom[c] -= i + 1; else { int r = inv(dom[c]); domu[r] = domu[dom[c]] = 0; dom[c] -= ((i + 1) << 3); } } } } int main() { scanf("%d%d", &n, &m); int i, j; for (i = 0; i < n; i++) scanf("%s", in[i]); int cc = 1, k, l; for (i = 0; i < n; i++) for (j = 0; j < m; j++) if (in[i][j] != '.' && cnt[i][j] == 0) { int q = 0; for (k = 0; k < 2; k++) for (l = 0; l < 2; l++) { cnt[i + k][j + l] = cc; all[cc - 1][q][0] = i + k; all[cc - 1][q][1] = j + l; q++; } cc++; } int z = 0; for (i = 0; i < n; i++) for (j = 0; j < m; j++) if (in[i][j] != '.' && !mk[in[i][j]]) { mk[in[i][j]] = ++z; } ans = 0; dfs(0); printf("%d\n", ans); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (in[i][j] == '.') printf("."); else printf("%d", aa[cnt[i][j] - 1]); } puts(""); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long int n, m, i, j, a[505] = {0}, b[1005] = {0}, ans = 0; scanf("%I64d%I64d", &n, &m); for (i = 1; i <= n; i++) { scanf("%I64d", &a[i]); } for (i = 1; i <= m; i++) { scanf("%I64d", &b[i]); long long int s = 0; bool visited[505] = {false}; for (j = i - 1; j > 0; j--) { if (b[j] == b[i]) break; if (visited[b[j]]) continue; s += (a[b[j]]); visited[b[j]] = true; } ans += s; } printf("%I64d\n", ans); return 0; }
3
#include <cstring> #include <cstdio> char str[200005], ans[200005]; int nxt[200005][35], dp[200005], ch[200005]; int main() { // freopen("ARC081-E.in", "r", stdin); scanf("%s", str); int n = strlen(str); for (int i = 0; i < 26; i++) nxt[n + 1][i] = n + i + 1; for (int i = n; i >= 0; i--) { memcpy(nxt[i], nxt[i + 1], sizeof(nxt[i + 1])); if (i) nxt[i][str[i - 1] - 'a'] = i; } memset(dp, 0x3f, sizeof(dp)); for (int i = n + 1; i <= n + 26; i++) dp[i] = 0; for (int i = n; i >= 0; i--) { for (int j = 0; j < 26; j++) { if (dp[nxt[i + 1][j]] + 1 < dp[i]) { dp[i] = dp[nxt[i + 1][j]] + 1; ch[i] = j; } } } int x = 0, len = 0; while (x < n) { ans[len++] = ch[x] + 'a'; x = nxt[x + 1][ch[x]]; } puts(ans); return 0; }
0
#include<bits/stdc++.h> #define N 200010 #define mod 1000000007 using namespace std; int n,x,y,ans=1,tot,min1,min2,c[N],w[N],minc[N],numc[N]; int fac[N],inv[N]; void getmin() { for(int i=1;i<=n;i++) { if(minc[i]<minc[min1]) { min2=min1; min1=i; } else if(minc[i]<minc[min2]) min2=i; } } int poww(int a,int b) { int ans=1; while(b) { if(b&1) ans=(1ll*ans*a)%mod; a=(1ll*a*a)%mod; b>>=1; } return ans; } void preC() { fac[0]=1; for(int i=1;i<=n;i++) fac[i]=1ll*i*fac[i-1]%mod; inv[n]=poww(fac[n],mod-2); for(int i=n-1;i>=0;i--) inv[i]=1ll*inv[i+1]*(i+1)%mod; } int C(int n,int m) { return ((1ll*fac[m]*inv[m-n])%mod*inv[n])%mod; } int main() { memset(minc,127,sizeof(minc)); scanf("%d%d%d",&n,&x,&y); for(int i=1;i<=n;i++) { scanf("%d%d",&c[i],&w[i]); minc[c[i]]=min(minc[c[i]],w[i]); } getmin(); for(int i=1;i<=n;i++) { if(w[i]+minc[c[i]]<=x) w[i]=minc[c[i]]; if((c[i]==min1&&minc[min2]+w[i]<=y)||(c[i]!=min1&&minc[min1]+w[i]<=y)) { numc[c[i]]++; tot++; } } preC(); for(int i=1;i<=n;i++) { if(numc[i]) { ans=(1ll*ans*C(numc[i],tot))%mod; tot-=numc[i]; } } printf("%d\n",ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t; cin >> t; while (t--) { long long int n; cin >> n; long long int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; vector<long long int> v; sort(a, a + n); int i, u; long long int flag = 0; for (i = 1; i <= 1024; i++) { for (int j = 0; j < n; j++) { v.push_back(a[j] ^ i); } for (int i = 0; i < v.size(); i++) { } flag = 0; sort(v.begin(), v.end()); for (int k = 0; k < n; k++) { if (a[k] != v[k]) { flag++; break; } } if (flag == 0) { u = i; break; } v.clear(); } if (flag == 0) cout << u << endl; else cout << "-1" << endl; } }
2
#include<bits/stdc++.h> using namespace std; int main(){ int a,b,n; cin >> n >> a; int rslt = 1; for(int i=1;i<n;i++) { cin >> b; if(a<=b){rslt++;a=b;} } cout << rslt; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, k; scanf("%d %d", &n, &k); set<pair<long long, int> > t; for (int i = 0; i < k; i++) t.insert(make_pair(0, i)); for (int i = 0; i < n; i++) { long long s, m; scanf("%lld %lld", &s, &m); pair<long long, int> p = *t.begin(); t.erase(t.begin()); p.first = max(p.first, s) + m; t.insert(p); printf("%lld\n", p.first); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int a[110]; int d[110]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d%d", &a[i], &d[i]); } int ans = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (a[i] >= a[j]) { if (a[i] < a[j] + d[j] || a[i] + d[i] < a[j]) { ans++; if (a[i] + d[i] >= a[j] + d[j]) ans++; } else if (a[i] + d[i] < a[j] + d[j]) ans++; } else { if (a[i] + d[i] >= a[j] || a[i] >= a[j] + d[j]) { ans++; if (a[i] + d[i] < a[j] + d[j]) ans++; } else if (a[i] + d[i] >= a[j] + d[j]) ans++; } } } printf("%d\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; int a[200001]; int main() { int n, idx1 = 0, idx2 = 0; long long res1 = 0, res2 = 0; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) { if (a[i] == 1) { idx1++; } else { res1 += idx1; } } for (int i = n - 1; i >= 0; i--) { if (a[i] == 0) idx2++; else res2 += idx2; } cout << min(res1, res2) << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; template <class T> inline void read(T &ret) { char c; int sgn; if (c = getchar(), c == EOF) return; while (c != '-' && (c < '0' || c > '9')) c = getchar(); sgn = (c == '-') ? -1 : 1; ret = (c == '-') ? 0 : (c - '0'); while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0'); ret *= sgn; return; } inline void out(int x) { if (x > 9) out(x / 10); putchar(x % 10 + '0'); } const int maxn = 1e6 + 10; const int inf = 0x3f3f3f3f; vector<int> edge[maxn]; int book[maxn], pre[maxn]; int ans = inf; void bfs(int u) { memset(book, inf, sizeof(book)); book[u] = 0; queue<int> q; q.push(u); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = 0; i < edge[u].size(); i++) { int v = edge[u][i]; if (v != pre[u]) { if (book[v] != inf) ans = min(ans, (book[u] + book[v] + 1)); else pre[v] = u, book[v] = book[u] + 1, q.push(v); ; } } } } int main() { int n, x; read(n); for (int i = 0; i < n; i++) { read(x); for (int i = 2; i <= sqrt(x); i++) { while (x % (i * i) == 0) x /= i * i; } if (x == 1) { out(1); return 0; } int u = x, v = 1; edge[u].push_back(v); edge[v].push_back(u); for (int i = 2; i <= sqrt(x); i++) { if (x % i == 0) { u = x / i, v = i; edge[u].push_back(v); edge[v].push_back(u); } } } for (int i = 1; i <= sqrt(maxn); i++) bfs(i); if (ans != inf) out(ans); else printf("-1"); return 0; }
5
#include<bits/stdc++.h> using namespace std; int main(){ int r,g,b,n; cin>>r>>g>>b>>n; int cnt=0; for(int i=0;i<=n;i++){ for(int j=0;j<=n;j++){ int z=n-(i*r+j*g); if(0<=z && z%b==0)cnt++; } } cout<<cnt<<endl; }
0
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; int main(){ int n, k; cin >> n >> k; vector<int> a(n); rep(i,n) cin >> a[i]; vector<bool> ans(k+1,false); rep(i,k) if(!ans[i]) rep(j,n) if(i+a[j] <= k) ans[i+a[j]] = true; cout << (ans[k] ? "First" : "Second") << endl; return 0; }
0
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int n,L,T; struct evol{long long x,w;}a[101010]; bool cmp(evol u,evol v){return u.x<v.x;} int main() { scanf("%d%d%d",&n,&L,&T); for(int i=1,x,y;i<=n;i++) scanf("%lld%lld",&a[i].x,&a[i].w); long long rk=0; for(int i=1;i<=n;i++) { if(a[i].w==1) a[i].x+=T; else a[i].x-=T; rk+=a[i].x/L; if(a[i].x%L<0) rk--; a[i].x=(a[i].x%L+L)%L; } sort(a+1,a+n+1,cmp); rk=(rk%n+n)%n; for(int i=rk+1;i<=n;i++) printf("%lld\n",a[i].x); for(int i=1;i<=rk;i++) printf("%lld\n",a[i].x); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAX = (int)100 + 5; int pr[MAX]; int main() { cin.tie(NULL); ios_base::sync_with_stdio(false); int n, m; cin >> n >> m; for (int i = 0; i < (int)(n); ++i) cin >> pr[i]; sort(pr, pr + n); map<string, int> ma; multiset<int, greater<int>> li; string st; for (int i = 0; i < (int)(m); ++i) { cin >> st; ma[st]++; } for (auto x : ma) li.insert(x.second); int mi = 0, mx = 0, cur = 0; for (auto x : li) mi += x * pr[cur++]; reverse(pr, pr + n); cur = 0; for (auto x : li) mx += x * pr[cur++]; cout << mi << " " << mx; }
3
#include <bits/stdc++.h> using namespace std; #define fn(i,n) for(i = 0; i < n; i++) #define fg(i,a ,n) for(i = a; i < n; i+=2) #define ft(i,a ,n) for(i = a; i < n; i++) #define fp(i,a, n) for(i = a; i >= n; i-- ) #define ld double #define ll long long int #define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define pb push_back #define pf push_front #define mp make_pair #define S second #define F first #define vi vector <ll> #define INF 10000000000 #define all(x) x.begin(), x.end() typedef vector< pair <ll , ll>> vl; typedef pair<string , string> pairs; ll i , j, n, k, t, p = 0,q = 9,a,b, c = 0, ct = 0, m = 0, s, sum = 0, sum1 = 0, sum2 = 0; int32_t main(){ cin >> t; fn(i , t){ cin >> n; deque <ll> d; fn(j , n){ cin >> a;d.pb(a); } fn(j , n/2){ cout << d.front() << " " << d.back() << " "; d.pop_front();d.pop_back(); } if(d.size() > 0) cout << d.front() << " "; cout << endl; } }
1
#include <bits/stdc++.h> using namespace std; template <class T> int getbit(T s, int i) { return (s >> i) & 1; } template <class T> T onbit(T s, int i) { return s | (T(1) << i); } template <class T> T offbit(T s, int i) { return s & (~(T(1) << i)); } template <class T> int cntbit(T s) { return __builtin_popcnt(s); } unsigned long long fac(int k) { unsigned long long ans = 1; for (int i = 2; i <= k; i++) { ans *= i; } return ans; } unsigned long long nCk(int n, int k) { unsigned long long res = 1; if (k > n - k) { k = n - k; } for (int i = 0; i < k; i++) { res *= (n - i); res /= (i + 1); } return res; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; unsigned long long ans = 1; unsigned long long sum_0 = 1; unsigned long long sum_1 = 0; for (int i = 2; i <= k; i++) { unsigned long long t = (i - 1) * (sum_0 + sum_1); sum_0 = sum_1; sum_1 = t; ans += t * nCk(n, i); } cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> char str[2000000]; int l; int calc(int a, int b, char c) { if (c == '|') return (a | b); if (c == '&') return (a & b); if (c == '^') return (a ^ b); } int dfs() { if (str[l] == '0') { l++; return 1 << 0; } if (str[l] == '1') { l++; return 1 << 1; } if (str[l] == '?') { l++; return 1 << 2; } ++l; int a = dfs(); char op = str[l]; ++l; int b = dfs(); ++l; int re = 0; for (int i = 0; i <= 2; i++) { if ((a & (1 << i)) == 0) continue; for (int j = 0; j <= 2; j++) { if ((b & (1 << j)) == 0) continue; if (i < 2 && j < 2) re |= 1 << (calc(i, j, op)); if (i == 2 && j < 2) { if (calc(1, j, op) != calc(0, j, op)) re |= 1 << 2; else re |= 1 << (calc(0, j, op)); } if (i < 2 && j == 2) { if (calc(i, 1, op) != calc(i, 0, op)) re |= 1 << 2; else re |= 1 << (calc(i, 0, op)); } if (i == 2 && j == 2) { if (calc(0, 0, op) == calc(1, 1, op)) re |= 1 << (calc(0, 0, op)); else re |= 1 << 2; re |= 1 << (calc(0, 1, op)); } } } return re; } int main() { int n; char c; scanf("%d", &n); scanf("%s", str); l = 0; if (dfs() & (1 << 2)) printf("YES\n"); else printf("NO\n"); }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, k, a[101], total_score = 0; for (int i = 0; i <= 101; i++) a[i] = 0; int x; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> x; a[x]++; } for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++) { int ch; if (a[10 * j - i] * i > k) { ch = (k / i); } else { ch = a[10 * j - i]; } k -= ch * i; a[10 * j] += ch; a[10 * j - i] -= ch; } } for (int i = 1; i <= 100; i++) { total_score += a[i] * (i / 10); } cout << total_score; }
3
#include <bits/stdc++.h> using namespace std; long long n, m, k; long long ans; long long a[200010]; bool cmp(int a, int b) { return a > b; } int main(void) { ios::sync_with_stdio(false); cin >> n >> m >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + n + 1, cmp); ans += (m / (k + 1)) * (k * a[1] + a[2]); ans += (m % (k + 1)) * a[1]; cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; vector<int> e[303], ans; bool dfs(int x, int y, int last) { if (x == y) return 1; for (int i = 0; i < e[x].size(); i++) if (e[x][i] != last) if (dfs(e[x][i], y, x)) { ans.push_back(x); return 1; } return 0; } int n, x, y; int main() { cin >> n; for (int i = 0; i < n - 1; i++) { cin >> x >> y; e[x].push_back(y); e[y].push_back(x); } ans.push_back(1); int y = 1; while (cin >> x) { dfs(x, y, 0); y = x; } dfs(1, y, 0); if (ans.size() != n * 2 - 1) cout << -1; else for (int i = 0; i < ans.size(); i++) cout << ans[i] << ' '; }
4
#include <bits/stdc++.h> using namespace std; double as[100005]; int main() { int n, a, d; while (scanf("%d%d%d", &n, &a, &d) > 0) { for (int i = 1; i <= n; i++) { int x, y; scanf("%d%d", &x, &y); double s = (1.0 * y * y) / (2.0 * a); double t = 0.0; if (s <= d) t = (y * 1.0) / (a * 1.0) + (1.0 * (d - s)) / (1.0 * y); else t = sqrt(2.0 * d / (1.0 * a)); if (i >= 2 && t + (double)x < as[i - 1]) as[i] = as[i - 1]; else as[i] = t + (double)x; } for (int i = 1; i <= n; i++) printf("%.8f\n", as[i]); } }
1
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ int n, w, d, p, s; while(cin >> n >> w >> d){ if(!w) break; vector < pair<int, int> > cake; cake.push_back(make_pair(w, d)); for(int i=0;i<n;i++){ cin >> p >> s; int nw = cake[p-1].first, nd = cake[p-1].second; for(int j=p-1;j<i;j++) cake[j] = cake[j+1]; s %= (nw+nd); if(s < nw){ cake[i] = make_pair(min(s, nw-s), nd); cake.push_back(make_pair(max(s, nw-s), nd)); } else { s -= nw; cake[i] = make_pair(nw, min(s, nd-s)); cake.push_back(make_pair(nw, max(s, nd-s))); } } vector <int> S; for(int i=0;i<n+1;i++) S.push_back(cake[i].first*cake[i].second); sort(S.begin(), S.end()); for(int i=0;i<n;i++) cout << S[i] << " "; cout << S[n] << endl; } }
0
#include <bits/stdc++.h> using namespace std; vector<pair<int, char> > grp[112345]; bool mark[112345]; vector<int> ans[2], p[3]; map<int, int> vp; int n, e; bool dfs(int part, char cl, int vx) { p[part].push_back(vx); mark[vx] = true; vp[vx] = part; for (int i = 0; i < grp[vx].size(); i++) { int v = grp[vx][i].first; char c = grp[vx][i].second; int x; if (c == cl) x = part; else x = part % 2 + 1; if (mark[v]) { if (vp[v] != x) return false; } else { if (!dfs(x, cl, v)) return false; } } return true; } bool solve(char clr, int as) { for (int i = 0; i <= n; i++) { mark[i] = false; } vp.clear(); for (int i = 1; i <= n; i++) { p[0].clear(); p[1].clear(); p[2].clear(); if (mark[i] == false) { if (dfs(1, clr, i) == true) { if (p[1].size() < p[2].size()) { for (int j = 0; j < p[1].size(); j++) ans[as].push_back(p[1][j]); } else { for (int j = 0; j < p[2].size(); j++) ans[as].push_back(p[2][j]); } } else { return false; } } } return true; } int main() { scanf("%d%d", &n, &e); for (int i = 0; i < e; i++) { int x, y; char ch; cin >> x >> y >> ch; grp[x].push_back(make_pair(y, ch)); grp[y].push_back(make_pair(x, ch)); } bool sr = solve('R', 1), sb = solve('B', 0); int x = 1; if (!sr && !sb) { cout << -1 << endl; return 0; } else if (sr && sb) { if (ans[0].size() < ans[1].size()) x = 0; } else if (sr) { x = 1; } else x = 0; cout << ans[x].size() << endl; ; for (int i = 0; i < ans[x].size(); i++) cout << ans[x][i] << " "; cout << endl; }
2
#include <bits/stdc++.h> using namespace std; int main() { int t[5], i, j, b[101], sum = 0, temp, t1, t2, ans; for (i = 0; i < 101; i++) b[i] = 0; for (i = 0; i < 5; i++) { cin >> t[i]; sum += t[i]; b[t[i]]++; } ans = sum; for (i = 1; i < 101; i++) { if (b[i] >= 2) { if (b[i] == 2) { ans = min(ans, sum - i * 2); } else { ans = min(ans, sum - i * 3); } } } cout << ans << "\n"; return 0; }
1
#include <bits/stdc++.h> typedef long long LL; #define SORT(c) sort((c).begin(),(c).end()) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) using namespace std; // N,E,S,W = 0,1,2,3; int dx[4]={0,1,0,-1}; int dy[4]={-1,0,1,0}; bool inrange(int x,int y,int w,int h){return 0<=x&&x<w && 0<=y&&y<h;} int main(void) { for(;;){ int w,h; cin >> w >> h; if(!w&&!h) return 0; int ope[32][32]; REP(j,h) REP(i,w) cin >> ope[i][j]; int c[4]; REP(i,4) cin >> c[i]; set<tuple<int,int,int> > already; priority_queue<pair<int,tuple<int,int,int> > > dijk; // negc,(x,y,dir) dijk.push(make_pair(0,make_tuple(0,0,1))); while(!dijk.empty()){ pair<int,tuple<int,int,int> > tmp; tmp=dijk.top(); int negc,x,y,dir; negc=tmp.first; tie(x,y,dir)=tmp.second; dijk.pop(); if(!already.insert(make_tuple(x,y,dir)).second) continue; if(x==w-1&&y==h-1){ cout << -negc << endl; break; } REP(i,4){ int cost=c[i]; if(ope[x][y]==i) cost=0; int ndir=(dir+i)%4; if(inrange(x+dx[ndir],y+dy[ndir],w,h)) dijk.push(make_pair(negc-cost,make_tuple(x+dx[ndir],y+dy[ndir],ndir))); } } } }
0
#include <bits/stdc++.h> using namespace std; int n; vector<int> tree[3010], treeT[3010]; bool vis[3010]; int cost[3010], delta[3010][3010], lineL[3010][3010], dp[3010][3010]; int dfs(int base, int root, int sum, int deep, int dpv, int state) { if (base == root) dp[base][root] = 0; else dp[base][root] = min(dpv + 1 - state, sum - state); delta[base][root] = sum; lineL[base][root] = deep; vis[root] = 1; int ret = 0; for (int i = 0; i < tree[root].size(); i++) if (vis[tree[root][i]] == 0) ret += dfs(base, tree[root][i], sum, deep + 1, dp[base][root], 0); for (int i = 0; i < treeT[root].size(); i++) if (vis[treeT[root][i]] == 0) ret += dfs(base, treeT[root][i], sum + 1, deep + 1, dp[base][root], 1) + 1; return ret; } int main() { memset(delta, 0, sizeof delta); memset(lineL, 0, sizeof lineL); memset(cost, 0, sizeof cost); memset(dp, 0, sizeof dp); scanf("%d", &n); for (int i = 0; i < n - 1; i++) { int a, b; scanf("%d%d", &a, &b); tree[a].push_back(b); treeT[b].push_back(a); } for (int i = 1; i <= n; i++) { memset(vis, 0, sizeof vis); cost[i] = dfs(i, i, 0, 0, 0, 0); } int ans = 4000; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (i != j) { int tmp = cost[i] - delta[i][j] + dp[i][j]; if (ans > tmp) ans = tmp; } if (n == 1) printf("%d\n", 0); else printf("%d\n", ans); }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 110005; int n, qq, fa[maxn], f[maxn], sz[maxn], up[maxn], edge[maxn], dep[maxn], son[maxn], q[maxn]; int tot = 0, V[maxn << 1], N[maxn << 1], F[maxn], ans[maxn]; struct event { int c, k, a, b; } events[maxn << 1]; struct info { int ls, rs, len, val; info() { ls = 0, rs = 0, len = 0, val = 0; } } tree[maxn << 2]; bool cmp(event x, event y) { if (x.c != y.c) return x.c > y.c; return x.k < y.k; } void add(int a, int b) { ++tot, V[tot] = b, N[tot] = F[a], F[a] = tot; } void decompose() { int hd = 0, tl = 1; memset(son, 0, sizeof son); memset(fa, 0, sizeof fa); memset(dep, 0, sizeof dep); memset(sz, 0, sizeof sz); q[1] = 1, dep[1] = 0, fa[1] = 0; while (hd != tl) { int x = q[++hd]; for (int p = F[x]; p > 0; p = N[p]) if (V[p] != fa[x]) { q[++tl] = V[p]; dep[V[p]] = dep[x] + 1; fa[V[p]] = x; } } for (int i = n, x; i > 0; i--) { sz[x = q[i]]++; if (fa[x]) { sz[fa[x]] += sz[x]; if (!son[fa[x]] || sz[x] > sz[son[fa[x]]]) son[fa[x]] = x; } } int num = 0; for (int i = n, x, y; i > 0; i--) { x = q[i]; if (son[x]) continue; while (fa[x] && son[fa[x]] == x) x = fa[x]; y = x; while (x > 0) up[x] = y, edge[x] = ++num, x = son[x]; } } info rev(info a) { swap(a.ls, a.rs); return a; } info operator+(const info &a, const info &b) { info ret; ret.len = a.len + b.len; ret.ls = a.ls; if (a.ls == a.len) ret.ls = a.len + b.ls; ret.rs = b.rs; if (b.rs == b.len) ret.rs = b.len + a.rs; ret.val = a.val + b.val - f[a.rs] - f[b.ls] + f[a.rs + b.ls]; return ret; } void build(int cur, int l, int r) { tree[cur].len = r - l + 1; tree[cur].val = tree[cur].ls = tree[cur].rs = 0; if (l != r) build((cur << 1), l, ((l + r) >> 1)), build((cur << 1 | 1), ((l + r) >> 1) + 1, r); } void modify(int cur, int l, int r, int pos) { if (l == r) { tree[cur].val = f[1]; tree[cur].ls = tree[cur].rs = 1; return; } if (pos <= ((l + r) >> 1)) modify((cur << 1), l, ((l + r) >> 1), pos); else modify((cur << 1 | 1), ((l + r) >> 1) + 1, r, pos); tree[cur] = tree[(cur << 1)] + tree[(cur << 1 | 1)]; } info query(int cur, int l, int r, int ql, int qr) { if (ql <= l && r <= qr) return tree[cur]; info ret; if (ql <= ((l + r) >> 1)) ret = ret + query((cur << 1), l, ((l + r) >> 1), ql, qr); if (qr > ((l + r) >> 1)) ret = ret + query((cur << 1 | 1), ((l + r) >> 1) + 1, r, ql, qr); return ret; } void upd(int a, int b) { if (dep[a] < dep[b]) swap(a, b); modify(1, 1, n, edge[a]); } int ask(int x, int y) { info vx, vy; while (up[x] != up[y]) { if (dep[up[x]] < dep[up[y]]) swap(x, y), swap(vx, vy); vx = query(1, 1, n, edge[up[x]], edge[x]) + vx; x = fa[up[x]]; } if (dep[x] < dep[y]) swap(x, y), swap(vx, vy); info ans = rev(vy) + query(1, 1, n, edge[y] + 1, edge[x]) + vx; return ans.val; } int main() { scanf("%d%d", &n, &qq); f[0] = f[n] = 0; for (int i = 1; i < n; i++) scanf("%d", &f[i]); int all = 0; for (int i = 1; i < n; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); add(u, v), add(v, u); events[++all] = (event){w, 0, u, v}; } for (int i = 1; i <= qq; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); events[++all] = (event){w, i, u, v}; } sort(events + 1, events + 1 + all, cmp); decompose(); build(1, 1, n); for (int i = 1; i <= all; i++) if (events[i].k == 0) { upd(events[i].a, events[i].b); } else { ans[events[i].k] = ask(events[i].a, events[i].b); } for (int i = 1; i <= qq; i++) printf("%d\n", ans[i]); return 0; }
5
#include <bits/stdc++.h> using namespace std; long long int MOD = 998244353; long long int mod = 1e9 + 7; long long int INF = 1e18; long long int dx[] = {1, -1, 0, 0}; long long int dy[] = {0, 0, 1, -1}; long long int a[100009] = {0}; long long int sum[100009]; long long int par[100009]; bool dfs(vector<long long int> adj[], long long int x, long long int h, long long int s) { if (h % 2 == 1) { a[x] = sum[x] - s; s = sum[x]; } if (a[x] < 0) return false; long long int m = INF; for (auto j : adj[x]) { if (j != par[x]) { par[j] = x; bool flag = dfs(adj, j, h + 1, s); if (!flag) return false; m = min(a[j], m); } } if (m != INF && h % 2 == 0) { a[x] += m; for (auto j : adj[x]) { if (j != par[x]) a[j] -= m; } } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n; cin >> n; vector<long long int> adj[n + 1]; for (int i = 2; i <= n; i++) { long long int x; cin >> x; adj[x].push_back(i); adj[i].push_back(x); } for (int i = 1; i <= n; i++) cin >> sum[i]; bool flag = dfs(adj, 1, 1, 0); if (!flag) cout << -1; else { long long int ans = 0; for (int i = 1; i <= n; i++) ans += a[i]; cout << ans; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n; int cnt=0; cin >> n; for(int i=0;i<n;i++){ int a; cin >> a; int c=0; while(a%2==0){ a/=2; c++; } cnt+=c; } cout << cnt; }
0
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } bool isperfect(long long n) { long long y = sqrt(n); if (n % y == 0 && y * y == n) return true; return false; } bool comp(pair<int, int> a, pair<int, int> b) { if (a.first == b.first) return a.second > b.second; return a.first < b.first; } long long powi(long long a, long long b) { long long ans = 1; while (b > 0) { if (b & 1) ans = (ans * a); b = b >> 1; a = (a * a); } return ans; } bool isprime(long long n) { for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } bool prime[10000000]; void seive() { prime[1] = true; for (int i = 2; i * i < 10000000; i++) { if (prime[i]) continue; for (int j = i * i; j < 10000000; j += i) { prime[j] = true; } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { long long n, m; cin >> n >> m; long long a[n][m]; int f = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; if ((i == 0 && j == 0) || (i == n - 1 && j == m - 1) || (i == n - 1 && j == 0) || (i == 0 && j == m - 1)) { if (a[i][j] > 2) f = 1; continue; } if (i == 0 || j == 0 || i == n - 1 || j == m - 1) { if (a[i][j] > 3) { f = 1; } } else { if (a[i][j] > 4) { f = 1; } } } } if (f) { cout << "NO" << endl; continue; } cout << "YES" << endl; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if ((i == 0 && j == 0) || (i == n - 1 && j == m - 1) || (i == n - 1 && j == 0) || (i == 0 && j == m - 1)) { cout << 2 << " "; continue; } if (i == 0 || j == 0 || i == n - 1 || j == m - 1) { cout << 3 << " "; } else { cout << 4 << " "; } } cout << endl; } } }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 30; int n; string s, ans = ""; int mp[maxn], ct[maxn], in[maxn], out[maxn], used[maxn]; void span(int s) { ans = ans + char('a' + s); ct[s]--; if (mp[s] != -1) span(mp[s]); } int main() { cin >> n; memset(mp, -1, sizeof(mp)); memset(ct, 0, sizeof(ct)); for (int i = 0; i < n; ++i) { cin >> s; int len = s.size(); if (len > 26) return 0 * printf("NO"); for (int j = 0; j < len - 1; ++j) { ct[s[j] - 'a'] = 1; if (s[j] == s[j + 1]) return 0 * printf("NO"); if (mp[s[j] - 'a'] == -1) mp[s[j] - 'a'] = s[j + 1] - 'a', in[s[j + 1] - 'a']++, out[s[j] - 'a']++; else if (mp[s[j] - 'a'] != s[j + 1] - 'a') return 0 * printf("NO"); } ct[s[len - 1] - 'a'] = 1; } for (int i = 0; i < 26; ++i) if (in[i] > 1 || out[i] > 1) return 0 * printf("NO"); for (int s = 0; s < 26; ++s) if (ct[s] && !in[s]) span(s); int f = 0; for (int i = 0; i < 26; ++i) if (ct[i] != 0) f = 1; if (f) return 0 * printf("NO"); cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; string s; int main() { int i, j, k; cin >> s; int n = (int)(s.size()); if (n & 1) { cout << -1 << endl; return 0; } int x = 0, y = 0; for (i = 0; s[i]; i++) { if (s[i] == 'U') y++; if (s[i] == 'D') y--; if (s[i] == 'R') x++; if (s[i] == 'L') x--; } cout << (abs(x) + abs(y)) / 2 << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; struct treeNode { int num[21]; int lazy; treeNode() { for (int i = 0; i < 21; i++) num[i] = 0; lazy = 0; } }; treeNode st[100000 << 2]; int inp[100000]; void Build(int node, int l, int r) { if (l == r) { int x = inp[l], i = 0; while (x) { st[node].num[i] = (x & 1); x >>= 1; i++; } st[node].lazy = 0; } else { int lc = (node << 1), rc = ((node << 1) + 1), mid = ((l + r) >> 1); Build(lc, l, mid); Build(rc, mid + 1, r); for (int i = 0; i < 21; i++) st[node].num[i] = st[lc].num[i] + st[rc].num[i]; st[node].lazy = 0; } } void Update(int node, int l, int r, int rl, int rr, int a) { if (st[node].lazy) { int x = st[node].lazy, i = 0; while (x) { if (x & 1) st[node].num[i] = (r - l + 1 - st[node].num[i]); x >>= 1; i++; } if (l < r) { st[(node << 1)].lazy ^= st[node].lazy; st[((node << 1) + 1)].lazy ^= st[node].lazy; } st[node].lazy = 0; } if (l > rr || r < rl) { return; } else if (l >= rl && r <= rr) { int x = a, i = 0; while (x) { if (x & 1) st[node].num[i] = (r - l + 1 - st[node].num[i]); x >>= 1; i++; } if (l < r) { st[(node << 1)].lazy ^= a; st[((node << 1) + 1)].lazy ^= a; } } else { int lc = (node << 1), rc = ((node << 1) + 1), mid = ((l + r) >> 1); Update(lc, l, mid, rl, rr, a); Update(rc, mid + 1, r, rl, rr, a); for (int i = 0; i < 21; i++) st[node].num[i] = st[lc].num[i] + st[rc].num[i]; } } treeNode Query(int node, int l, int r, int rl, int rr) { if (st[node].lazy) { int x = st[node].lazy, i = 0; while (x) { if (x & 1) st[node].num[i] = (r - l + 1 - st[node].num[i]); x >>= 1; i++; } if (l < r) { st[(node << 1)].lazy ^= st[node].lazy; st[((node << 1) + 1)].lazy ^= st[node].lazy; } st[node].lazy = 0; } treeNode tmpL, tmpR; if (l > rr || r < rl) { return tmpL; } else if (l >= rl && r <= rr) { return st[node]; } else { int mid = ((l + r) >> 1); tmpL = Query((node << 1), l, mid, rl, rr); tmpR = Query(((node << 1) + 1), mid + 1, r, rl, rr); for (int i = 0; i < 21; i++) tmpL.num[i] += tmpR.num[i]; return tmpL; } } int main() { int n, m; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &inp[i]); Build(1, 0, n - 1); int t, l, r, x; treeNode tmp; long long ans; scanf("%d", &m); while (m--) { scanf("%d", &t); if (t == 1) { scanf("%d%d", &l, &r); l--; r--; tmp = Query(1, 0, n - 1, l, r); ans = 0; for (int i = 0; i < 21; i++) { ans += ((1ll << i) * tmp.num[i]); } printf("%lld\n", ans); } else { scanf("%d%d%d", &l, &r, &x); l--; r--; Update(1, 0, n - 1, l, r, x); } } }
5
#include <bits/stdc++.h> using namespace std; int a[3], b[3]; bool large() { if (b[2] + 18 != a[2]) return b[2] + 18 < a[2]; if (b[1] != a[1]) return b[1] < a[1]; if (b[0] != a[0]) return b[0] < a[0]; return true; } bool legal() { int leap = (b[2] % 4 == 0); if (b[1] == 2) return b[0] <= 28 + leap; if (b[1] <= 7) return b[0] <= 30 + b[1] % 2; if (b[1] <= 12) return b[0] <= 31 - b[1] % 2; return false; } bool solve() { sort(b, b + 3); do { if (legal() && large()) return true; } while (next_permutation(b, b + 3)); return false; } int main(void) { for (int i = 0; i < 3; i++) scanf("%d%*[^0-9]", &a[i]); for (int i = 0; i < 3; i++) scanf("%d%*[^0-9]", &b[i]); puts(solve() ? "YES" : "NO"); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 100 * 1000 + 5; bool Mark[maxn]; vector<pair<int, int> > adj[maxn]; int decompose(int v) { queue<int> q; while (q.size()) q.pop(); for (int i = 0; i < adj[v].size(); i++) { int e = adj[v][i].second; if (!Mark[e]) { q.push(adj[v][i].first); Mark[e] = true; } } int t = q.size(); for (int i = 0; i < t; i++) { int nei = q.front(); int dc = decompose(nei); if (dc == -1) q.push(nei); else cout << v << " " << nei << " " << dc << endl; q.pop(); } t = q.size(); for (int i = 0; i < t - 1; i += 2) { cout << q.front() << " " << v << " "; q.pop(); cout << q.front() << endl; q.pop(); } if (t % 2 == 0) return -1; return q.back(); } int main() { int n, m; cin >> n >> m; for (int i = 0, u, v; i < m; i++) { cin >> u >> v; adj[u].push_back(make_pair(v, i)); adj[v].push_back(make_pair(u, i)); } if (m & 1) { cout << "No solution" << endl; return 0; } decompose(1); return 0; }
5
#include <bits/stdc++.h> using namespace std; char s[100]; long long n, len = 0; int main() { scanf("%s", s); long long ans = 0; for (int i = strlen(s) - 1; i >= 0; i--) { if (s[i] == '9') { ans += 9; s[i] = '\0'; } else { break; } } for (int i = 0; s[i]; i++) ans = ans + s[i] - '0'; len = strlen(s); if (len >= 1) ans = ans + (len - 1) * 9; cout << ans << endl; }
2
#include<bits/stdc++.h> using namespace std; int main(){ int n, ans = 0; string s; cin >> n >> s; for(int i = 0;i<n-2;i++){ if("ABC" == s.substr(i,3)) ans++; } cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 10; vector<int> v[maxn]; string s; int n, m, dp[maxn], vis[maxn], flag; void dfs1(int x) { if (vis[x] == 1) { flag = 1; return; } if (vis[x]) return; vis[x] = 1; for (auto i : v[x]) { dfs1(i); } vis[x] = 2; } void dfs2(int x, int y) { if (vis[x]) return; vis[x] = 1; int ans = 0; for (auto i : v[x]) { dfs2(i, y); ans = max(ans, dp[i]); } dp[x] = ans; if (s[x - 1] - 'a' == y) dp[x]++; } int main() { cin >> n >> m; cin >> s; for (int i = 0; i < m; i++) { int x, y; scanf("%d%d", &x, &y); v[x].push_back(y); } for (int i = 1; i <= n; i++) { if (!vis[i]) dfs1(i); if (flag) { cout << -1; return 0; } } int ans = 0; for (int j = 0; j < 26; j++) { memset(dp, 0, sizeof(dp)); memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++) { if (!vis[i]) dfs2(i, j); } ans = max(ans, *max_element(dp + 1, dp + n + 1)); } cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; long long n, m, s, X, Y, x, y, cnt, ans; int main() { scanf("%I64d%I64d%I64d", &n, &m, &s); for (X = 1; X <= n; X += 2) for (Y = 1; Y <= m; Y += 2) { if (X * Y == s) ans += (n - X + 1) * (m - Y + 1) * ((X + 1) / 2 * (Y + 1) - 1); else if (X * Y > s && (X * Y - s) % 4 == 0) { for (x = 2; x < X; x += 2) { y = (X * Y - s) / x; if (y * x != (X * Y - s) || y & 1 || y >= Y) continue; ans += (n - X + 1) * (m - Y + 1) * 2; } } } printf("%I64d\n", ans); }
3
#include <bits/stdc++.h> using namespace std; const int maxno = 5e4 + 5; string s[maxno]; bool cmp(string a, string b) { string ab = a + b; string ba = b + a; return ab < ba; } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) cin >> s[i]; sort(s, s + n, cmp); for (int i = 0; i < n; i++) cout << s[i]; cout << endl; }
3
#include <bits/stdc++.h> using namespace std; long long n, m, i, j, k, t; long long num[5]; int main() { while (cin >> n >> num[1] >> num[2] >> num[3]) { long long min = 1; long long ans = n % 4; if (ans == 0) { ans = 0; } else { ans = 4 - ans; switch (ans) { case 1: { ans = num[1]; if (ans > num[2] + num[3]) ans = num[2] + num[3]; if (ans > num[3] * 3) ans = num[3] * 3; break; } case 2: { if (2 * num[1] < num[2]) { ans = 2 * num[1]; } else { ans = num[2]; } if (ans > 2 * num[3]) ans = 2 * num[3]; break; } case 3: { if (num[1] + num[2] < num[3]) { ans = num[1] + num[2]; } else { ans = num[3]; } if (ans > 3 * num[1]) { ans = 3 * num[1]; } break; } } } cout << ans << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; bool vis[31622777]; int prime[2000000], tot; long long exgcd(long long a, long long b, __int128 &x, __int128 &y) { if (!b) { x = 1, y = 0; return a; } long long g = exgcd(b, a % b, y, x); y -= a / b * x; return g; } void get_prime(int n) { for (int i = 2; i <= n; i++) { if (!vis[i]) prime[tot++] = i; for (int j = 0; j < tot && 1ll * i * prime[j] <= n; j++) { vis[i * prime[j]] = 1; if (i % prime[j] == 0) break; } } } int main () { get_prime(31622776); long long n; cin >> n; n *= 2; long long tmp = n; vector <long long> all; for (int i = 0; i < tot; i++) { long long pw = prime[i]; while (n % pw == 0) pw *= prime[i]; pw /= prime[i]; if (pw != 1) all.push_back(pw), tmp /= pw; } if (tmp != 1) all.push_back(tmp); int m = all.size(); __int128 ans = 900000000000000000; for (int mask = 0; mask < (1 << m); mask++) { long long x = 1, y = 1; __int128 k1, k2; for (int i = 0; i < m; i++) { if (mask >> i & 1) x *= all[i]; else y *= all[i]; } exgcd(x, y, k1, k2); assert(x * k1 + y * k2 == 1); k2 = -k2; __int128 tmp = max((max((__int128)0, 1 - k1) + y - 1) / y, (max((__int128)0, 1 - k2) + x - 1) / x); k1 += tmp * y, k2 += tmp * x; if (ans / k2 >= y) ans = min(ans, k2 * y); k1 = -k1, k2 = -k2; tmp = max((max((__int128)0, 1 - k1) + y - 1) / y, (max((__int128)0, 1 - k2) + x - 1) / x); k1 += tmp * y, k2 += tmp * x; if (ans / k1 >= x) ans = min(ans, k1 * x); } cout << (long long)ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int binarySearch(vector<long long>& v, long long l, long long r, long long x) { int mid = l + (r - l) / 2; if (l <= r) { if (v[mid] == x) return mid; else if (v[mid] > x) return binarySearch(v, l, mid - 1, x); else return binarySearch(v, mid + 1, r, x); } return -1; } int main() { long long x, y, c = 0; cin >> x >> y; vector<long long> v(x), v1(x); map<long long, int> m; for (int i = 0; i < x; i++) { cin >> v[i]; m[v[i]]++; if (m[v[i]] == 2) c++; } sort(v.begin(), v.end()); if (c != 0) { cout << 0; exit(0); } if (c == 0) { for (int i = 0; i < x; i++) { v1[i] = v[i] & y; int j = binarySearch(v, 0, x - 1, v1[i]); if (j != -1 && j != i) { cout << 1; exit(0); } } sort(v1.begin(), v1.end()); for (int i = 0; i < x; i++) { int j = binarySearch(v1, 0, x - 1, v1[i]); if (j != i && j != -1) { cout << 2; exit(0); } } } cout << -1; }
2
#include<iostream> #include<vector> #include<algorithm> using namespace std; int main(){ int h,w; while(1){ cin >> h >> w; if(!h && !w)break; vector<string> grid; for(int i=0;i<h;i++){ string tmp; cin >> tmp; grid.push_back(tmp); } int ans = 0; for(int i=0;i<h;i++){ for(int j=0;j<w;j++){ if(grid[i][j] == '.'){ int dx = 1,dy = 1; while(i+dy<h && grid[i+dy][j] == '.')dy++; while(j+dx<w && grid[i][j+dx] == '.')dx++; int tmp = 0; int x = dx; for(int k=0;k<dy;k++){ int l; for(l=0;l<x;l++){ if(grid[i+k][j+l]=='*'){ x = min(x,l); break; } } tmp = max(tmp,x*(k+1)); } //if(ans<tmp)cout << "[" << i << " " << j << "]:" << tmp << endl; ans = max(ans,tmp); } } } cout << ans << endl; } }
0
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, i; cin >> a >> b; c = a; while ((a / b) != 0) { c += (a / b); i = a % b; a /= b; a += i; } cout << c; }
1
#include "bits/stdc++.h" using namespace std; // コンテスト中のみ //#define int long long #define ll long long #define rep(i,n) for(int i = 0; i < (n); i++) #define P pair<ll,ll> #define ld long double ll INF = (1LL << 60); int MOD = 1000000007; struct UnionFind { vector<ll>p; UnionFind(ll n) { p.resize(n, -1); } ll find(ll x) { if (p[x] == -1)return x; else return p[x] = find(p[x]); } void unite(ll x, ll y) { x = find(x), y = find(y); if (x == y)return; p[x] = y; } }; struct edge{ ll u, v, cost; }; bool comp(const edge& e1, const edge& e2) { return e1.cost < e2.cost; } ll V, E; edge G[200010]; int kruskal() { sort(G, G + E, comp); UnionFind uf(V); ll res = 0; rep(i, E) { edge e = G[i]; if (uf.find(e.u) != uf.find(e.v)) { uf.unite(e.u, e.v); res += e.cost; } } return res; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cin >> V >> E; rep(i, E) { ll s, t, w; cin >> s >> t >> w; G[i] = { s,t,w }; } cout << kruskal() << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int a[1111111]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } int mx = a[n - 1], ans = 1; for (int i = n - 2; i >= 0; i--) { if (mx > 0) { mx--; } else { ans++; } mx = max(mx, a[i]); } printf("%d\n", ans); }
2
#include <bits/stdc++.h> using namespace std; int main(int argc, char* argv[]) { int a[4]; cin >> a[0] >> a[1] >> a[2] >> a[3]; while ((a[0] != 1) || (a[1] != 1) || (a[2] != 1) || (a[3] != 1)) { bool step = false; for (int i = 0; i < 4; i++) { int j = (i + 1) % 4; if ((a[i] % 2 == 0) && (a[j] % 2 == 0)) { cout << '/' << (i + 1) << endl; a[i] /= 2; a[j] /= 2; step = true; break; } } if (!step) { int i = rand() % 4; int j = (i + 1) % 4; cout << '+' << (i + 1) << endl; a[i]++; a[j]++; } } return 0; }
3
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define ri register int const long double PI=3.141592653589793238462643383279; const int N=500005,M=998244353; int n,i,ans,p,e[N],vis[N]; struct str{ int a,x; }a[N],b[N]; bool cmp(str a,str b) { return a.a<b.a; } int main() { scanf("%d",&n); for(i=1;i<=n;++i) { scanf("%d",&a[i].a); a[i].x=i; } for(i=1;i<=n;++i) { scanf("%d",&b[i].a); b[i].x=i; } sort(a+1,a+1+n,cmp); sort(b+1,b+1+n,cmp); for(i=2;i<=n;++i) if(a[i].a<=b[i-1].a) ans=-1; for(i=1;i<=n;++i) if(a[i].a>b[i].a) ans=n; for(i=1;i<=n;++i) e[a[i].x]=b[i].x; for(p=1;!vis[p];p=e[p]) { vis[p]=1; ++ans; } if(ans<n) puts("Yes"); else puts("No"); }
0
#include <bits/stdc++.h> using namespace std; int n, m, d, a[100005], b[100005], l, r; int main() { scanf("%d%d%d", &n, &m, &d); for (int i = 1; i <= n; i++) scanf("%d", &a[i]), a[i] = -a[i]; for (int i = 1; i <= m; i++) scanf("%d", &b[i]); sort(a + 1, a + 1 + n); sort(b + 1, b + 1 + m); l = 0, r = min(n, m); while (l < r) { int mid = l + r + 1 >> 1; long long ret = 0; for (int i = 1; i <= mid; i++) ret += max(a[i] + b[mid - i + 1], 0); if (ret > d) r = mid - 1; else l = mid; } printf("%d ", l); for (int i = 1; i <= l; i++) d -= b[i]; printf("%d\n", max(-d, 0)); }
4
#include<cstdio> const int N=1e5+5; int n,s,x[N]; long long p[N]; int main(){ scanf("%d%d",&n,&s); for(int i=1;i<=n;++i){ scanf("%d%d",&x[i],&p[i]); } int l=1,r=n,dir=-1; long long ans=0; while(1){ if(s<=x[l]){ ans+=x[r]-s; break; } if(s>=x[r]){ ans+=s-x[l]; break; } if(p[l]>=p[r]){ if(dir!=0) ans+=x[r]-x[l]; p[l]+=p[r]; dir=0; --r; } else{ if(dir!=1) ans+=x[r]-x[l]; p[r]+=p[l]; dir=1; ++l; } } printf("%lld\n",ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int tc; cin >> tc; while (tc--) { int len; cin >> len; string str; cin >> str; int maxP = 0; int consecP; bool initiate = false; for (int i = 0; i < len; ++i) { if (str[i] == 'A') { consecP = 0; initiate = true; } else if (initiate) { consecP++; if (consecP > maxP) maxP = consecP; } } cout << maxP << "\n"; } return 0; }
1
#include <bits/stdc++.h> #pragma GCC optimize(3) using namespace std; const int MAXN = 300005, MAXV = 1000000, B = 1000; int a[MAXN], b[MAXN], v[MAXV], sum[1005], mn[1005]; void update(int d, int x) { int b = d / 1000; v[d] += x; sum[b] = 0; mn[b] = INT_MAX; for (int i = b * 1000; i < (b + 1) * 1000; i++) { sum[b] += v[i]; mn[b] = min(mn[b], sum[b]); } } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); v[MAXV - a[i]]--; } for (int i = 1; i <= m; i++) { scanf("%d", &b[i]); v[MAXV - b[i]]++; } for (int i = 0; i < 1000; i++) { mn[i] = INT_MAX; for (int j = i * 1000; j < (i + 1) * 1000; j++) { sum[i] += v[j]; mn[i] = min(mn[i], sum[i]); } } int q; scanf("%d", &q); while (q--) { int op, p, x; scanf("%d%d%d", &op, &p, &x); if (op == 1) { update(MAXV - a[p], 1); a[p] = x; update(MAXV - x, -1); } else { update(MAXV - b[p], -1); b[p] = x; update(MAXV - x, 1); } bool flag = false; for (int i = 0, s = 0; i < 1000 && !flag; i++) { if (mn[i] + s < 0) { int t = 0; for (int j = i * 1000; !flag; j++) { t += v[j]; if (t + s < 0) { flag = true; printf("%d\n", MAXV - j); } } } s += sum[i]; } if (!flag) puts("-1"); } return 0; }
3
#include <bits/stdc++.h> using namespace std; bool isPrime(long long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (long long i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } long long seve[1000000] = {0}; set<long long> check; void seive() { long long i, j; seve[0] = 1; seve[1] = 1; for (i = 2; i <= 1000000; i++) { if (seve[i] == 0) { check.insert(i); for (j = i * i; j <= 1000000; j = j + i) seve[j] = 1; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long ans = 0, n, m, i, j = 0, f = 0, k = 0, y, x; cin >> n; long long a[n]; string s; for (i = 0; i < n; i++) { cin >> a[i]; k += a[i]; } sort(a, a + n); ans += 2 * k; for (i = 0; i < n; i++) { k -= a[i]; ans += k; } cout << ans - a[n - 1]; }
1
#include <bits/stdc++.h> using namespace std; const int N=2002; const int B=4000000+1; int n,a[N],sum=0; bitset<B> f; int main(){ scanf("%d",&n); f[0]=1; for(int i=1;i<=n;i++){ scanf("%d",&a[i]); f=f|(f<<a[i]); sum+=a[i]; } for(int i=(sum+1)/2;;i++)if(f[i]){ return !printf("%d\n", i); } } /* 1 3 0 1 3 4 */
0
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int t[105] = {}; for (int i = 0; i < n; i++) cin >> t[i]; vector<int> x, y; int r = 0; for (int I = 0; I < k; I++) { pair<int, int> h, g; h.first = 0, h.second = t[0]; g.first = 0, g.second = t[0]; for (int i = 0; i < n; i++) { if (t[i] > h.second) { h.first = i; h.second = t[i]; } if (t[i] < g.second) { g.first = i; g.second = t[i]; } } r = h.second - g.second; if (h.second - g.second > 1) { x.push_back(h.first + 1); y.push_back(g.first + 1); t[h.first]--; t[g.first]++; } else { break; } } int p = 9999999, w = 0; for (int i = 0; i < n; i++) { p = min(p, t[i]); w = max(w, t[i]); } r = w - p; cout << r << " " << x.size() << endl; for (int i = 0; i < x.size(); i++) { cout << x[i] << " " << y[i] << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; template <class T> void _Print(T &&x) { cerr << x << ")" << "\n"; } template <class T, class... S> void _Print(T &&x, S &&...y) { cerr << x << ", "; _Print(y...); } template <class T> class compare { public: bool operator()(pair<T, T> &x, pair<T, T> &y) { if (x.first == y.first) { return x.second > y.second; } return x.first > y.first; } }; template <class T> ostream &operator<<(ostream &os, const pair<T, T> &a) { return os << a.first << " " << a.second; } long long power(long long a, int b) { long long po = 1; while (b) { if (b & 1) po *= a; a *= a; b >>= 1; } return po; } template <class T> pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) { return {a.first + b.first, a.second + b.second}; } template <class T> pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) { return {a.first - b.first, a.second - b.second}; } template <class T> pair<T, T> operator*(const pair<T, T> &a, const pair<T, T> &b) { return {a.first * b.first, a.second * b.second}; } template <class T> pair<T, T> operator%(const pair<T, T> &a, const pair<T, T> &b) { return {a.first % b.first, a.second % b.second}; } template <class T, class U> pair<T, T> operator+(const pair<T, T> &a, const U &b) { return {a.first + b, a.second + b}; } template <class T, class U> pair<T, T> operator*(const pair<T, T> &a, const U &b) { return {a.first * b, a.second * b}; } int Set(int N, int pos) { return N = N | (1 << pos); } int reset(int N, int pos) { return N = N & ~(1 << pos); } bool check(int N, int pos) { return (bool)(N & (1 << pos)); } vector<vector<pair<int, int> > > g; int make, ans, now; vector<bool> vis, changed; vector<int> temp, final, ro, aux; bool flag, ok; void dfs(int u, int pre, int p) { temp.push_back(u); vis[u] = true; if (pre != make) { now++; aux.emplace_back(u); changed[u] = true; } else changed[u] = false; for (auto &v : g[u]) { if (pre == make) { if (!vis[v.first]) dfs(v.first, v.second, u); else if (v.first != p) { if (changed[v.first]) { if (v.second == make) { ok = false; break; } } else { if (v.second != make) { ok = false; break; } } } } else { if (!vis[v.first]) dfs(v.first, v.second ^ 1, u); else if (v.first != p) { if (changed[v.first]) { if (v.second != make) { ok = false; break; } } else { if (v.second == make) { ok = false; break; } } } } } } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, m; cin >> n >> m; g.resize(n + 1); while (m--) { int u, v; char c; cin >> u >> v >> c; g[u].emplace_back(v, (c == 'R' ? 0 : 1)); g[v].emplace_back(u, (c == 'R' ? 0 : 1)); } ans = INT_MAX; for (int i = 0; i < 2; i++) { make = i; vis.assign(n + 1, false); changed.assign(n + 1, false); flag = true; int ans1 = 0; vector<int> etto; for (int j = 1; j <= n; j++) { if (!vis[j]) { temp.clear(); aux.clear(); int mini = INT_MAX; ok = true; now = 0; changed[j] = false; for (auto &v : g[j]) { vis[j] = true; if (!vis[v.first]) dfs(v.first, v.second, j); else { if (changed[v.first]) { if (v.second == make) { ok = false; break; } } else { if (v.second != make) { ok = false; break; } } } } if (ok) { mini = min(mini, now); etto = aux; } for (auto &v : temp) vis[v] = false; ok = true; now = 1; temp.clear(); aux.clear(); aux.emplace_back(j); changed[j] = true; for (auto &v : g[j]) { vis[j] = true; if (!vis[v.first]) dfs(v.first, v.second ^ 1, j); else { if (changed[v.first]) { if (v.second != make) { ok = false; break; } } else { if (v.second == make) { ok = false; break; } } } } if (ok) { if (mini > now) etto = aux; mini = min(mini, now); } if (mini != INT_MAX) { ans1 += mini; for (auto &v : etto) final.emplace_back(v); } else { ans1 = INT_MAX; break; } } } if (ans1 < ans) ro = final; final.clear(); ans = min(ans1, ans); } if (ans == INT_MAX) cout << -1 << "\n"; else { cout << ans << "\n"; for (auto &v : ro) cout << v << " "; } return 0; }
2
#include <bits/stdc++.h> using namespace std; bool debug_disabled = true; int dp[51][51]; int board[51][51]; void solve() { memset(dp, 0, sizeof dp); memset(board, 0, sizeof board); int n; cin >> n; for (int i = 0; i < n; ++i) { string s; cin >> s; for (int j = 0; j < n; ++j) if (s[j] == '1') board[i][j] = 1; } for (int i = 0; i < n; ++i) { if (board[i][n - 1] == 1) dp[i][n - 1] = 1; if (board[n - 1][i] == 1) dp[n - 1][i] = 1; } for (int i = n - 2; i >= 0; --i) { for (int j = i; j >= 0; --j) { if (board[i][j] == 1) { if (dp[i + 1][j] == 1 || dp[i][j + 1] == 1) dp[i][j] = 1; } if (board[j][i] == 1) { if (dp[j + 1][i] == 1 || dp[j][i + 1] == 1) dp[j][i] = 1; } } } for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) if (board[i][j] == 1 && dp[i][j] == 0) { if (debug_disabled) { } else cerr << "i = " << i << ", j = " << j << endl; cout << "NO" << endl; return; } cout << "YES" << endl; } int main() { cout.setf(ios::fixed, ios::floatfield); cout.setf(ios::showpoint); cout.precision(12); cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); int T; cin >> T; for (int i = 1; i <= T; ++i) { solve(); } return 0; }
5
#include <bits/stdc++.h> using namespace std; long long bigMod(long long x, long long y) { long long res = 1; while (y) { if (y & 1) { res = (res * x) % 1000000007; y--; } else { x = (x * x) % 1000000007; y >>= 1; } } return res; } struct query { int l, r, id; } Q[200005]; const int k = 450; int ara[200005], freq[1000006]; bool cmp(query &a, query &b) { int b1 = a.l / k, b2 = b.l / k; if (b1 == b2) return a.r < b.r; return b1 < b2; } int l = 0, r = -1, ans[200005]; long long sum, res; vector<int> fac[200005]; void Add(int x) { sum = (sum * ara[x]) % 1000000007; for (auto &y : fac[x]) { if (!freq[y]) { sum = (sum * (y - 1)) % 1000000007; res = (res * y) % 1000000007; } freq[y]++; } } void Del(int x) { res = (res * ara[x]) % 1000000007; for (auto &y : fac[x]) { freq[y]--; if (!freq[y]) { res = (res * (y - 1)) % 1000000007; sum = (sum * y) % 1000000007; } } } vector<int> prime; bitset<1000006> a; void sieve() { a[0] = a[1] = 1; for (int i = 4; i < 200005; i += 2) a[i] = 1; for (int i = 3; i * i < 200005; i++) { if (!a[i]) { for (int j = i * i; j < 200005; j += 2 * i) a[j] = 1; } } prime.push_back(2); for (int i = 3; i < 200005; i += 2) if (!a[i]) prime.push_back(i); } void primefact(int n) { int x = n; n = ara[n]; for (int i = 0; prime[i] * prime[i] <= n && i < int(prime.size()); i++) { if (n % prime[i] == 0) { while (n % prime[i] == 0) { n /= prime[i]; } fac[x].push_back(prime[i]); } } if (n > 1) { fac[x].push_back(n); } } int main() { sieve(); int q, n; scanf("%d", &n); for (int i = int(0); i < int(n); i++) { scanf("%d", &ara[i]); primefact(i); } scanf("%d", &q); for (int i = int(0); i < int(q); i++) { int x, y; scanf("%d %d", &x, &y); x--, y--; Q[i].l = x, Q[i].r = y; Q[i].id = i; } sum = 1; sort(Q, Q + q, cmp); for (int i = int(0); i < int(q); i++) { res = 1; while (l > Q[i].l) Add(--l); while (r < Q[i].r) Add(++r); while (l < Q[i].l) Del(l++); while (r > Q[i].r) Del(r--); sum = (sum * bigMod(res, 1000000007 - 2)) % 1000000007; ans[Q[i].id] = sum; } for (int i = int(0); i < int(q); i++) { printf("%d\n", ans[i]); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { double A,B;cin>>A>>B; cout<<fixed<<A*B/(A+B)<<endl; }
0
#include <bits/stdc++.h> using namespace std; bool sortbysec(const int a, const int b) { return abs(a) > abs(b); } int main() { int test; cin >> test; while (test--) { long long int n, lastn, lastp = 0, prod = 1, flag = 0; cin >> n; vector<int> arr; for (int p = 0; p < n; p++) { int x; cin >> x; arr.push_back(x); if (x > 0) flag = 1; } sort(arr.begin(), arr.end(), sortbysec); int i; for (i = 0; i < 5; i++) { prod *= arr[i]; if (arr[i] < 0) lastn = arr[i]; else lastp = arr[i]; } if (prod >= 0 || n == 5) cout << prod << endl; else if (flag == 0) { prod = 1; for (int i = n - 1; i >= n - 5; i--) prod *= arr[i]; cout << prod << endl; } else { long long int x = prod, y = prod; int j = i; while (arr[j] < 0 && j < n) j++; if (j < n) { x = x / lastn; x = x * arr[j]; } while (arr[i] >= 0 && i < n) i++; if (i < n && lastp != 0) { y = y / lastp; y = y * arr[i]; } cout << max(x, y) << endl; } } return 0; }
2
#include<iostream> using namespace std; int main() { char a,b,c,d; cin>>a>>b>>c>>d; cout<<((a==b&&b==c)||(b==c&&c==d)?"Yes":"No")<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, e, f; while (scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &e, &f) == 6) { if (b * d * f > a * c * e) puts("Ron"); else if (b * d * f == 0 && a * c * e == 0) { if (d != 0) { if (c == 0) puts("Ron"); else { if (b != 0) { if (a == 0) puts("Ron"); else puts("Hermione"); } else puts("Hermione"); } } else puts("Hermione"); } else puts("Hermione"); } return 0; }
1
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; vector<long long> a(n); set<long long> r; for (long long i = 0; i < n; i++) { cin >> a[i]; r.insert(a[i]); } sort(a.begin(), a.end()); if (r.size() == 1 || r.size() == 2) { cout << "YES"; return 0; } if (r.size() > 3) { cout << "NO"; return 0; } vector<long long> r0; for (auto c : r) r0.push_back(c); r.clear(); long long mmin = r0[0]; long long mmax = r0[r0.size() - 1]; long long sr = r0[r0.size() / 2]; if (mmin + mmax == 2 * sr) cout << "YES"; else cout << "NO"; }
2
#include <bits/stdc++.h> using namespace std; const int MAXN = 2009; int n; bool a[MAXN][MAXN]; bool u[MAXN][MAXN]; bool b[MAXN][MAXN]; int maX = -1, may = -1, mix = -1, miy = -1; int q[MAXN * MAXN][2], top = 0; int l = 1, r = 0; void bfs(int x, int y) { u[x][y] = true; l = 1; r = 0; top = 0; r++; q[r][0] = x; q[r][1] = y; while (l <= r) { x = q[l][0], y = q[l][1]; l++; top++; if (maX == -1 || x > maX) maX = x; if (may == -1 || y > may) may = y; if (mix == -1 || x < mix) mix = x; if (miy == -1 || y < miy) miy = y; if (b[x - 1][y] && !u[x - 1][y]) { u[x - 1][y] = true; r++; q[r][0] = x - 1; q[r][1] = y; } if (b[x + 1][y] && !u[x + 1][y]) { u[x + 1][y] = true; r++; q[r][0] = x + 1; q[r][1] = y; } if (b[x][y - 1] && !u[x][y - 1]) { u[x][y - 1] = true; r++; q[r][0] = x; q[r][1] = y - 1; } if (b[x][y + 1] && !u[x][y + 1]) { u[x][y + 1] = true; r++; q[r][0] = x; q[r][1] = y + 1; } } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { int t; scanf("%d", &t); a[i][j] = t; } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { int s = 0; for (int k = i; k <= i + 7; k++) for (int l = j; l <= j + 7; l++) if (a[k][l]) s++; if (s >= 37) b[i][j] = true; } int ans1 = 0, ans2 = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (!u[i][j] && b[i][j]) { maX = -1, may = -1, mix = -1, miy = -1; bfs(i, j); if (top <= 80) continue; double x = (maX + mix) / 2; double y = (may + miy) / 2; double R = 0; for (int k = 1; k <= top; k++) { if ((q[k][0] - x) * (q[k][0] - x) + (q[k][1] - y) * (q[k][1] - y) > R) R = (q[k][0] - x) * (q[k][0] - x) + (q[k][1] - y) * (q[k][1] - y); } double rat = top / R; if (rat > 2.6) ans1++; else ans2++; } printf("%d %d\n", ans1, ans2); return 0; }
5
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; int main() { vector<priority_queue<long long, vector<long long>, greater<long long> > > w( 4); long long k1, k2, k3, t1, t2, t3, n, nn, me = -1; cin >> k1 >> k2 >> k3 >> t1 >> t2 >> t3 >> n; nn = n; while (n--) { long long h; cin >> h; me = max(me, h); w[0].push(h); } vector<pair<pair<int, long long>, int> > choice(4); long long curt = 0; while (nn > 0) { choice[0] = make_pair( make_pair(w[1].size() >= k1, (w[0].empty() ? 10000000000000000 : max(w[0].top(), curt))), 0); choice[1] = make_pair( make_pair(w[2].size() >= k2, (w[1].empty() ? 10000000000000000 : max(w[1].top(), curt))), 1); choice[2] = make_pair( make_pair(w[3].size() >= k3, (w[2].empty() ? 10000000000000000 : max(w[2].top(), curt))), 2); choice[3] = make_pair(make_pair(0, (w[3].empty() ? 10000000000000000 : max(w[3].top(), curt))), 3); sort((choice).begin(), (choice).end()); switch (choice[0].second) { case 0: { curt = choice[0].first.second; w[1].push(curt + t1); w[0].pop(); } break; case 1: { curt = choice[0].first.second; w[2].push(curt + t2); w[1].pop(); } break; case 2: { curt = choice[0].first.second; w[3].push(curt + t3); w[2].pop(); } break; case 3: { curt = choice[0].first.second; w[3].pop(); --nn; } break; } } cout << curt - me << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; void solve() { long long int n, x; cin >> n; vector<long long int> set_bit(20, 0); for (long long int i = 0; i < n; i++) { cin >> x; long long int curr_bit = 0; while (x) { if (x & 1) { set_bit[curr_bit]++; } x /= 2; curr_bit++; } } long long int ans = 0; for (long long int a = 0; a < n; a++) { long long int curr = 0; for (long long int i = 0; i < 20; i++) { if (set_bit[i]) { curr += (1 << i); set_bit[i]--; } } ans += curr * curr; } cout << ans << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long int t; t = 1; for (long long int i = 0; i < t; i++) { solve(); } }
4
#include <bits/stdc++.h> const int MAXN = 100000 + 10; int n, a[MAXN], b[MAXN]; std::map<int, int> M; template <typename T> inline void read(T &x) { T data = 0, w = 1; char ch = 0; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar(); if (ch == '-') w = -1, ch = getchar(); while (ch >= '0' && ch <= '9') data = ((T)data << 3) + ((T)data << 1) + (ch ^ '0'), ch = getchar(); x = data * w; } template <typename T> inline void write(T x, char ch = '\0') { if (x < 0) putchar('-'), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + '0'); if (ch != '\0') putchar(ch); } template <typename T> inline bool chkmin(T &x, T y) { return y < x ? (x = y, true) : false; } template <typename T> inline bool chkmax(T &x, T y) { return y > x ? (x = y, true) : false; } template <typename T> inline T min(T x, T y) { return x < y ? x : y; } template <typename T> inline T max(T x, T y) { return x > y ? x : y; } int main() { read(n); for (register int i = (1), iend = (n); i <= iend; ++i) read(a[i]); for (register int i = (1), iend = (n); i <= iend; ++i) read(b[i]); if (a[1] != b[1] || a[n] != b[n]) { puts("No"); return 0; } for (register int i = (2), iend = (n); i <= iend; ++i) M[a[i] - a[i - 1]]++; for (register int i = (2), iend = (n); i <= iend; ++i) if (!M[b[i] - b[i - 1]]) { puts("No"); return 0; } else M[b[i] - b[i - 1]]--; puts("Yes"); return 0; }
5
#include<iostream> using namespace std; int main(){ int a,b,w; cin>>w>>a>>b; cout<<max(0,max(b-a-w,a-b-w))<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const long long MOD = 1e9 + 7; const int inf = 1e9; const int N = 1e3 + 2; bool flag; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } void solve() { long long k; cin >> k; long long x = 0, y = 0, tmp = 1, sm = 3, i = 1; while (tmp < k) { tmp += sm; sm += 2; i++; } if (k > tmp - i) { y = i; sm = tmp - i + 1; x = i - (k - sm); } else { x = i; sm = tmp - i + 1; y = i - (sm - k); } cout << y << ' ' << x; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tt = 1; cin >> tt; while (tt--) { flag = 0; solve(); cout << '\n'; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, u, r; int a[50], b[50], p[50], k[50]; long long sol = 0; bool first = true; int rec(int t, bool ch) { if ((u - t) % 2 == 0) { long long tt = 0; for (int i = 1; i <= n; i++) { tt = tt + (((long long)a[i]) * ((long long)k[i])); } if (first) { sol = tt; first = false; } else sol = max(sol, tt); } if (t < u) { int tmp[50]; for (int i = 1; i <= n; i++) tmp[i] = a[i]; if (ch) { for (int i = 1; i <= n; i++) a[i] = tmp[i] ^ b[i]; rec(t + 1, false); } for (int i = 1; i <= n; i++) a[i] = tmp[p[i]] + r; rec(t + 1, true); for (int i = 1; i <= n; i++) a[i] = tmp[i]; } return 0; } int main() { scanf("%d %d %d", &n, &u, &r); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) scanf("%d", &b[i]); for (int i = 1; i <= n; i++) scanf("%d", &k[i]); for (int i = 1; i <= n; i++) scanf("%d", &p[i]); rec(0, true); printf("%I64d\n", sol); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int maxl = 2e5 + 7; const int inf = 0x3f3f3f3f; int dp[5007][5007]; char s1[5007], s2[5007]; int main() { ios::sync_with_stdio(false); int n, m; cin >> n >> m; cin >> s1 + 1 >> s2 + 1; int ans = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (s1[i] == s2[j]) dp[i][j] = dp[i - 1][j - 1] + 2; else { dp[i][j] = max(dp[i][j], dp[i - 1][j] - 1); dp[i][j] = max(dp[i][j], dp[i][j - 1] - 1); } ans = max(ans, dp[i][j]); } } cout << ans << endl; }
4
#include<bits/stdc++.h> using namespace std; const int N=3e5+5; int n,a[N]; struct each{ int a,b; }b[N]; bool com(each x,each y){ return x.a<y.a; } int main(){ scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&b[i].a); b[i].b=i,a[b[i].a]=i; } int now=1; sort(b+1,b+n+1,com); for(int i=1;i<=n;i++){ for(int j=1;j<b[i].b;j++){ while(now<=n*n&&a[now]) ++now; if(now>b[i].a){ puts("No"); return 0; } a[now++]=b[i].b; } } now=n*n; for(int i=n;i;i--){ for(int j=1;j<=n-b[i].b;j++){ while(now<=n*n&&a[now]) --now; if(now<b[i].a){ puts("No"); return 0; } a[now--]=b[i].b; } } puts("Yes"); for(int i=1;i<=n*n;i++) printf("%d ",a[i]); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; vector<set<int>> g; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; vector<int> d(n, 0); g = vector<set<int>>(n); for (int i = 1; i < n; i++) { int x, y; cin >> x >> y; x--, y--; d[x]++; d[y]++; g[x].insert(y); g[y].insert(x); } vector<int> leaves(n, 0); for (int i = 0; i < n; i++) if (d[i] == 1) leaves[*g[i].begin()]++; queue<int> q; for (int i = 0; i < n; i++) if (leaves[i] >= k) q.push(i); int res = 0; while (!q.empty()) { int x = q.front(); q.pop(); vector<int> todelete; for (auto v : g[x]) { if (((int)todelete.size()) < k && d[v] == 1) todelete.push_back(v); if (((int)todelete.size()) == k) break; } if (((int)todelete.size()) < k) continue; res++; for (auto v : todelete) { g[x].erase(v); g[v].erase(x); d[x]--; d[v]--; assert(d[v] == 0); leaves[x]--; } if (leaves[x] >= k) q.push(x); else if (d[x] == 1) { assert(((int)g[x].size()) == 1); int v = *g[x].begin(); if (++leaves[v] >= k) q.push(v); } } cout << res << '\n'; } return 0; }
6
#include<bits/stdc++.h> using namespace std; const int M=155; const int INF=2e9; int n,cost[M][M],from[M],lx[M],ly[M],slack[M],vx[M],vy[M]; int path(int v){ int t; vx[v]=1; for(int i=1;i<=n;i++) { if(vy[i])continue; t=lx[v]+ly[i]-cost[v][i]; if(t==0) { vy[i]=1; if(from[i]==0||path(from[i])) { from[i]=v; return 1; } } else if(t<slack[i]) slack[i]=t; } return 0; } int hung() { int d,ans=0; for(int i=1;i<=n;i++) while(true) { memset(vx,0,sizeof(vx)); memset(vy,0,sizeof(vy)); for(int j=1;j<=n;j++) slack[j]=INF; if(path(i)) break; d=INF; for(int j=1;j<=n;j++) if(!vy[j] && slack[j]<d) d=slack[j]; for(int j=1;j<=n;j++) { if(vx[j])lx[j]-=d; if(vy[j])ly[j]+=d; } } for(int i=1;i<=n;i++) ans+=cost[from[i]][i];//the from meaning... return ans; } int main() { int y,e; cin>>n>>y>>e; while(e--) { int u,v;cin>>u>>v;u++;v++; cost[u][v]=1; if(cost[u][v]>lx[u]) lx[u]=cost[u][v]; } n=max(n,y); cout<<hung()<<endl; }
0
#include <bits/stdc++.h> using namespace std; const int INF = INT_MAX / 2, NINF = INT_MIN / 2; const long double PI = acos(-1.0); void prepareIO() { if (strlen("") != 0) { freopen( "" ".in", "r", stdin); freopen( "" ".out", "w", stdout); } } void debout() {} template <typename Head, typename... Tail> void debout(Head H, Tail... T) {} void stressout() {} template <typename Head, typename... Tail> void stressout(Head H, Tail... T) {} class Timer { public: double begin; Timer() : begin(clock()) {} ~Timer() { fprintf(stderr, "%.6lf\n", (clock() - begin + .0) / CLOCKS_PER_SEC); } } timer_; void solve1(); int main() { prepareIO(); solve1(); return 0; } void solve1() { string str; cin >> str; map<string, int> m; for (int i = 0; i < str.size(); i++) { string ex; for (int j = i; j < str.size(); j++) { ex += str[j]; m[ex]++; } } int ans = 0; for (auto& elem : m) { if (elem.second >= 2) { ans = max(ans, (int)elem.first.size()); } } cout << ans << "\n"; }
1
#include <bits/stdc++.h> using namespace std; int const INF = 2 * 10e5 + 123; int simple(int n) { int i, sq; if (n % 2 == 0) { return 0; } sq = (int)sqrt(n); for (i = 3; i <= sq; i++) { if ((n % i) == 0) { return 0; } } return 1; } long long gcd(long long a, long long b) { if (a > b) swap(a, b); if (!a) return b; return gcd(b % a, a); } bool used[1000]; bool good[1000]; pair<int, int> a[1324]; void solve() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i].first; a[i].second = i; } sort(a, a + n); long long s = 0; for (int i = 0; i < n / 2; i++) { swap(a[i], a[n - i - 1]); } for (int i = 0; i < n; i++) { s += a[i].first * i + 1; } cout << s << endl; for (int i = 0; i < n; i++) { cout << a[i].second + 1 << ' '; } } int main() { int T = 1; while (T--) { solve(); } }
2
#include<bits/stdc++.h> #define MAXN 5005 #define INF 1000000000 #define MOD 1000000007 #define F first #define S second using namespace std; typedef long long ll; typedef pair<int,int> P; int n,a,b; int dp[MAXN][MAXN][2]; int s[MAXN][MAXN],diag[MAXN][MAXN]; void add(int &a,int b) {a+=b; if(a>=MOD) a-=MOD;} void dec(int &a,int b) {a-=b; if(a<0) a+=MOD;} int get_sum(int l1,int r1,int l2,int r2) { int x=s[r1][r2]; dec(x,s[l1-1][r2]); dec(x,s[r1][l2-1]); add(x,s[l1-1][l2-1]); return x; } int get_diag(int x,int y) { return diag[x][y]; } int main() { scanf("%d%d%d",&n,&a,&b); if(a>b) swap(a,b); dp[0][0][1]=1; diag[0][0]=1; for(int j=0;j<=n;j++) s[0][j]=1; for(int i=1;i<=n;i++) { dp[i][0][0]=get_sum(max(0,i-a+1),i-1,0,b-1); for(int j=1;j<=n;j++) { dp[i][j][1]=dp[i-1][j-1][1]; add(dp[i][j][1],dp[i-1][j-1][0]); if(i>=a&&j>=a) add(dp[i][j][0],get_diag(i-a,j-a)); if(j>=b) add(dp[i][j][0],get_sum(max(0,i-a+1),i-1,j,j)); } for(int j=0;j<=n;j++) { s[i][j]=s[i-1][j]; add(s[i][j],dp[i][j][1]); if(j) { add(s[i][j],s[i][j-1]); dec(s[i][j],s[i-1][j-1]); } diag[i][j]=dp[i][j][1]; if(j) add(diag[i][j],diag[i-1][j-1]); } } int ans=0; for(int i=b;i<=n;i++) { add(ans,dp[n][i][0]); add(ans,dp[n][i][1]); } printf("%d\n",ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; char ar[1000], er[1000]; void oku() { int i, t, a; scanf("%s", ar); scanf("%d", &t); a = strlen(ar); for (int i = 0; i < a; i++) if (ar[i] <= 'Z') ar[i] += 32; for (int i = 0; i < a; i++) { if (ar[i] < 97 + t) ar[i] = ar[i] - 32; } cout << ar << endl; } int main() { oku(); return 0; }
4
#include<iostream> using namespace std; int main(){ int a[100]; int ans[100]; int n; cin>>n; for(int i=0;i<n;i++){ cin>>a[i]; } for(int i=n-1;i>=0;i--){ int add=-1; for(int j=0;j<=i;j++){ if(a[j]==j+1){add=j;}} if(add==-1){cout<<-1<<endl;return 0;} else{ ans[i]=add+1; for(int k=add;k<=i;k++){ a[k]=a[k+1]; } } } for(int i=0;i<n;i++){ cout<<ans[i]<<endl; } }
0
#include <bits/stdc++.h> #define forn(i,s,t) for(register int i=(s);i<=(t);++i) using namespace std; const int N = 5e2 + 10; int t, n, m; char mp[N][N]; int main() { scanf("%d", &t); while(t--) { scanf("%d%d", &n, &m); forn(i,0,n + 1) forn(j,0,m + 1) mp[i][j] = 0; forn(i,1,n) scanf("%s", mp[i] + 1); int lst = 0; for(register int i=1;i <= n;i+=3) { forn(j,1,m) mp[i][j] = 'X'; lst = i; } for(register int i=1;i <= n-2;i+=3) { int tmp = 0; forn(j,1,m) { int k1 = (j == 1 || (mp[i + 1][j - 1] == '.' && mp[i + 2][j - 1] == '.')); int k2 = (j == m || (mp[i + 1][j + 1] == '.' && mp[i + 2][j + 1] == '.')); if(k1 && k2) { mp[i + 1][j] = mp[i + 2][j] = 'X'; break ; } } } if(n % 3 == 0) { forn(i,1,m) if(mp[lst + 1][i] == 'X' || mp[lst + 2][i] == 'X') mp[lst + 1][i] = mp[lst + 2][i] = 'X'; } forn(i,1,n) { forn(j,1,m) printf("%c", mp[i][j]); putchar('\n'); } } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, s; cin >> n >> s; map<int, int> buy; map<int, int> sell; for (int i = 0; i < n; i++) { char d; int p, q; scanf(" %c %d %d", &d, &p, &q); if (d == 'B') buy[p] += q; else sell[p] += q; } map<int, int>::iterator itr; vector<pair<int, int> > arr; for (itr = sell.begin(); itr != sell.end(); itr++) { arr.push_back(make_pair(itr->first, itr->second)); } sort(arr.begin(), arr.end(), greater<pair<int, int> >()); for (int i = 0; i < arr.size(); i++) { if (arr.size() - i <= s) { printf("S %d %d\n", arr[i].first, arr[i].second); } } arr.clear(); for (itr = buy.begin(); itr != buy.end(); itr++) { arr.push_back(make_pair(itr->first, itr->second)); } sort(arr.begin(), arr.end(), greater<pair<int, int> >()); for (int i = 0; i < arr.size(); i++) { if (i < s) { printf("B %d %d\n", arr[i].first, arr[i].second); } } }
2
#include <bits/stdc++.h> using namespace std; template <class T> void read(vector<T> &mas) { for (auto &it : mas) cin >> it; } template <class T> void print(vector<T> &mas) { for (auto it : mas) cout << it << " "; cout << endl; } int main() { int n, m; cin >> n >> m; vector<int> days(n); vector<int> preps(m); vector<bool> passed(m, false); vector<int> last(m, -10); for (int i = 0; i < n; i++) { cin >> days[i]; days[i]--; last[days[i]] = i; } read(preps); for (auto it : last) if (it == -10) { cout << -1; exit(0); } int prep = 0; int tm = m; bool can = false; for (int i = 0; i < n; i++) { if (days[i] == -1 || i != last[days[i]]) { prep++; continue; } if (prep < preps[days[i]]) { cout << "-1"; exit(0); } prep -= preps[days[i]]; tm--; if (tm == 0) { can = true; break; } } if (!can) { cout << "-1"; exit(0); } int l = 1, r = n; int mid = 0; while (r - l > 1) { mid = (l + r) / 2; int prep = 0; vector<int> nlast(m, -10); for (int i = 0; i < mid; i++) { if (days[i] != -1) nlast[days[i]] = i; } for (auto it : nlast) if (it == -10) { goto end; } for (int i = 0; i < mid; i++) { if (days[i] == -1 || i != nlast[days[i]]) { prep++; continue; } if (prep < preps[days[i]]) { goto end; } prep -= preps[days[i]]; } r = mid; continue; end: l = mid; } cout << r; }
4
#include <bits/stdc++.h> using namespace std; int main(){ int N; cin >> N; map<string,int> m; int ma=0; for(int i = 0;i < N;i++){ string s; cin >> s; m[s]++; ma=max(ma,m[s]); } for(auto b : m){ if(b.second != ma){ continue; } cout << b.first << endl; } }
0
#include<cstdio> #include<queue> #include<map> #include<cstring> #include<string> #include<algorithm> using namespace std; map<string,int> table; int nex[4]={-4,4,-1,1}; void bfs(string str) { queue<string> que; table[str]=1; que.push(str); while(!que.empty()){ str=que.front(); que.pop(); int num=str.find('0'); for(int i=0;i<4;i++){ int tx=num+nex[i]; if(tx<0||tx>7||(num==3&&nex[i]==1)||(num==4&&nex[i]==-1)) continue; string ss=str; swap(ss[num],ss[tx]); if(!table[ss]){ table[ss]=table[str]+1; que.push(ss); } } } } int main() { int t; string st("01234567"); bfs(st); while(scanf("%d",&t)!=EOF){ st[0]='0'+t; for(int i=1;i<8;i++){ scanf("%d",&t); st[i]='0'+t; } printf("%d\n",table[st]-1); } return 0; }
0
#include<bits/stdc++.h> #define N 200010 #define ll long long using namespace std; ll a[20]; ll solve(int l,int r,int T1,int T2){ if(l>r)return 0; ll SS=1e18; for(int i=l;i<=r;i++){ SS=min(SS,solve(l,i-1,T1,T1+T2)+solve(i+1,r,T1+T2,T2)+(T1+T2)*a[i]); } return SS; } int main(){ int n; scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%lld",&a[i]); printf("%lld\n",solve(2,n-1,1,1)+a[1]+a[n]); return 0; }
0
#include <bits/stdc++.h> using namespace std; template <class T> int getbit(T s, int i) { return (s >> i) & 1; } template <class T> T onbit(T s, int i) { return s | (T(1) << i); } template <class T> T offbit(T s, int i) { return s & (~(T(1) << i)); } template <class T> int cntbit(T s) { return __builtin_popcnt(s); } template <class T> T gcd(T a, T b) { T r; while (b != 0) { r = a % b; a = b; b = r; } return a; } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } string s, temp; int test; double a[5005]; double sqr(double x) { return x * x; } void solve() { double sum = 0; int n = 250; for (int i = (1); i <= (n); ++i) { cin >> a[i]; } sort(a + 1, a + n + 1); for (int i = (1); i <= (240); ++i) a[i] = a[i + 5]; for (int i = (1); i <= (240); ++i) { sum += a[i]; } n = 240; double ave = sum / n; double RR = 0; for (int i = (1); i <= (n); ++i) { RR += sqr(a[i] - ave); } RR /= n; RR = sqrt(RR); if (RR > ave / 3) { cout << "uniform" << endl; } else { cout << "poisson" << endl; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> test; for (int itest = 0; itest < (test); ++itest) { solve(); } return 0; }
4
#include <algorithm> // #include <cstdlib> #include <cstring> #include <cstdio> #include <string> #include <queue> #include <cmath> #include <set> #include <map> #include <bitset> using namespace std; typedef long long LL; typedef double db; const int oo=2139063143; const int N=12010; const int P=1000000007; const db eps=1e-7; #define pritnf printf //char buf[1<<22],*p1=buf,*p2=buf,obuf[1<<22],*O=obuf; //#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) template <typename T> inline void sc (T &x) { x=0; static int p; p=1; static char c; c=getchar(); while (!isdigit(c)) { if (c=='-') p=-1; c=getchar(); } while ( isdigit(c)) { x=(x<<1)+(x<<3)+(c-48); c=getchar(); } x*=p; } template <typename T> inline void print (T x) { if (x< 0) putchar('-'),x=-x; if (x>=10) print(x/10); putchar(x%10+'0'); } template <typename T> inline void pr (T x) { print(x),putchar('\n'); } template <typename T1,typename T2> void chkmax (T1 &A,T2 B) { A=A>=B?A:B; } template <typename T1,typename T2> void chkmin (T1 &A,T2 B) { A=A<=B?A:B; } int n; struct EDGE { int v,w,c,nx; }lb[N<<3]; int tot=1,top[N]; void add (int u,int v,int w,int c) { lb[++tot]=(EDGE){v,w, c,top[u]},top[u]=tot; lb[++tot]=(EDGE){u,0,-c,top[v]},top[v]=tot; } int s,t; LL dis[N]; int flow[N],pre[N],las[N]; bool in[N]; bool spfa () { for (int i=1; i<=t; i++) dis[i]=-1e18,flow[i]=pre[i]=las[i]=0,in[i]=0; queue <int> q; flow[s]=oo; dis[s]=0,q.push (s),in[s]=1; while (q.size ()) { int u=q.front (); q.pop (),in[u]=0; for (int kb=top[u]; kb; kb=lb[kb].nx) { int v=lb[kb].v,w=lb[kb].w,c=lb[kb].c; if (w&&dis[v]< dis[u]+c) { dis[v]=dis[u]+c; flow[v]=min (flow[u],w); pre[v]=u,las[v]=kb; if (!in[v]) q.push (v),in[v]=1; } } } return pre[t]; } int maflow; LL macost; void mcmf () { while (spfa ()) { int u=t,fl=flow[t]; maflow+=fl,macost+=dis[t]*fl; while (u!=s) lb[las[u]].w-=fl,lb[las[u]^1].w+=fl,u=pre[u]; } } struct LY { int x,y,w,id; }a[N],b[N]; const int dx[4]={1,-1,1,-1},dy[4]={1,1,-1,-1}; int id[4]; int main () { // freopen (".in","r",stdin); // freopen (".out","w",stdout); sc(n); for (int i=1; i<=n; i++) sc(a[i].x),sc(a[i].y),sc(a[i].w); for (int i=1; i<=n; i++) sc(b[i].x),sc(b[i].y),sc(b[i].w); s=n*2+4+1,t=s+1; for (int i=1; i<=n; i++) add (s,i,a[i].w,0),add (i+n,t,b[i].w,0); for (int i=1; i<=4; i++) id[i-1]=n*2+i; for (int i=1; i<=n; i++) for (int j=0; j<=3; j++) add (i,id[j],oo,dx[j]*a[i].x+dy[j]*a[i].y),add (id[j],i+n,oo,-(dx[j]*b[i].x+dy[j]*b[i].y)); mcmf (); pr(macost); return 0; }
0
#include<iostream> #include<string> #include<map> #include<algorithm> using namespace std; #define rep0(i,n) for(int i=0;i<(int) n;i++) typedef long long ll; int n; string s; ll m; map<string,ll> mp; string ret,res; int main (){ cin>>n>>s; m=(1<<n); rep0(i,m){ ret={}; res={}; rep0(j,n){ if(i&(1<<j)) ret+=s[j]; else res+=s[j]; } if(res.size()) reverse(res.begin(),res.end()); mp[ret+'+'+res]++; } ll ans=0; rep0(i,m){ ret={}; res={}; rep0(j,n){ if(i&(1<<j)) ret+=s[j+n]; else res+=s[j+n]; } if(ret.size()) reverse(ret.begin(),ret.end()); ans+=mp[ret+'+'+res]; } cout<<ans<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const int MAXN = 1e6 + 5, inf = 1e9; const ll INF = 1e18; const ld PI = 3.1415926535897932384626433832795; int cnt[MAXN]; ll dp[MAXN], tab[MAXN]; bool vis[MAXN], used[MAXN]; vector<int> G[MAXN]; vector<ll> V; set<ll> S; map<ll, int> M; stack<ll> St; queue<ll> Q; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout << setprecision(13) << fixed; int n, m, l; cin >> n >> m >> l; bool ok = 0; int res = 0; for (int i = 1; i <= n; i++) { cin >> tab[i]; if (tab[i] > l) { if (ok) continue; ok = 1; res++; } else ok = 0; } for (int i = 0; i < m; i++) { int co; cin >> co; if (co == 0) { cout << res << "\n"; } else { int p, d; cin >> p >> d; if (tab[p] > l) continue; tab[p] += d; if (tab[p] <= l) continue; if (tab[p - 1] > l and tab[p + 1] > l) res--; else if (tab[p - 1] <= l and tab[p + 1] <= l) res++; } } }
2
#include <bits/stdc++.h> using namespace std; const long long inf = (long long)1e17; bool can1[100100], can2[100100]; int a[100100], b[100100], lst[100100]; long long sum[100100], s[100100], val[100100]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } for (int i = 1; i <= n; i++) { scanf("%d", &b[i]); } for (int i = 1; i <= n; i++) { sum[i] = sum[i - 1] + b[i]; } for (int i = 1; i <= n; i++) { s[i] = s[i - 1] + a[i]; } for (int i = 1; i <= n; i++) { can1[i] = true; } val[n + 1] = -inf; for (int i = n; i > 0; i--) { val[i] = max(val[i + 1], sum[i - 1] - s[i - 1]); } for (int i = 1; i <= n; i++) { if (val[i + 1] > sum[i - 1] - s[i - 1]) { can1[i] = false; } } val[0] = -inf; for (int i = 1; i <= n; i++) { val[i] = max(val[i - 1], sum[i - 1] - s[i - 1]); } for (int i = 1; i <= n; i++) { if (val[i] > sum[i - 1] - s[i - 1] - sum[n] + s[n]) { can1[i] = false; } } for (int i = 1; i <= n; i++) { can2[i] = true; } val[0] = inf; for (int i = 1; i <= n; i++) { val[i] = min(val[i - 1], sum[i - 1] - s[i]); } for (int i = 1; i <= n; i++) { if (sum[i - 1] - s[i] > val[i - 1]) { can2[i] = false; } } val[n + 1] = inf; for (int i = n; i > 0; i--) { val[i] = min(val[i + 1], sum[i - 1] - s[i]); } for (int i = 1; i <= n; i++) { if (sum[i - 1] - s[i] + sum[n] - s[n] > val[i]) { can2[i] = false; } } int sz = 0; for (int i = 1; i <= n; i++) { if (can1[i] || can2[i]) { lst[sz++] = i; } } printf("%d\n", sz); if (sz > 0) { for (int i = 0; i < sz; i++) { if (i > 0) { printf(" "); } printf("%d", lst[i]); } printf("\n"); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; int pow3[600005]; int fact[900005]; int invf[900005]; int Power(int x, int y) { int re = 1; while(y) { if(y & 1) { re = 1ll * re * x % MOD; } x = 1ll * x * x % MOD; y >>= 1; } return re; } int C(int n, int m) { if(m < 0) { return 0; } if(n < m) { return 0; } return 1ll * fact[n] * invf[m] % MOD * invf[n - m] % MOD; } int n, m, k; int main() { fact[0] = 1; for(int i = 1; i <= 900000; i++) { fact[i] = 1ll * fact[i - 1] * i % MOD; } invf[900000] = Power(fact[900000], MOD - 2); for(int i = 900000; i; i--) { invf[i - 1] = 1ll * invf[i] * i % MOD; } pow3[0] = 1; for(int i = 1; i <= 600000; i++) { pow3[i] = 3ll * pow3[i - 1] % MOD; } scanf("%d%d%d", &n, &m, &k); int ans = 0; int t = 1; for(int i = 0; i <= m + k; i++) { int tmp = 1ll * C(n - 1 + i, n - 1) * t % MOD; tmp = 1ll * tmp * pow3[m + k - i] % MOD; ans = (ans + tmp) % MOD; t = ((2ll * t + MOD - C(i, m)) % MOD + MOD - C(i, i - k)) % MOD; } printf("%d\n", ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 110000; const unsigned long long P = 1000000007; unsigned long long p[N], h[N]; int n, a[N], nxt[N]; char s[N]; map<int, int> was; vector<pair<int, int> > ans; int main() { cin >> n; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); map<int, int>::iterator it = was.find(a[i]); if (it != was.end()) { nxt[it->second] = i; it->second = i; } else was.insert(make_pair(a[i], i)); } p[0] = 1; for (int i = 1; i < n; i++) p[i] = p[i - 1] * P; h[0] = a[0]; for (int i = 1; i < n; i++) h[i] = h[i - 1] + a[i] * p[i]; for (int i = 0; i < n; i++) { int cur = i; while (nxt[cur]) { int i2 = nxt[cur]; if (p[i2 - i] * (h[i2 - 1] - (i > 0 ? h[i - 1] : 0)) == h[i2 + i2 - i - 1] - h[i2 - 1]) ans.push_back(make_pair(i2 - i, i)); cur = nxt[cur]; } } int l = 0; sort(ans.begin(), ans.end()); for (int i = 0; i < ans.size(); i++) if (ans[i].second >= l) l = ans[i].second + ans[i].first; cout << n - l << endl; for (int i = l; i < n; i++) printf("%d ", a[i]); }
3
#include <bits/stdc++.h> using namespace std; int n, d, a[10001], ans; int main() { cin >> n >> d; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 2; i <= n; i++) { int j = i - 1; if (a[i] <= a[j]) { ans += (a[j] - a[i]) / d + 1; a[i] += ((a[j] - a[i]) / d + 1) * d; } } cout << ans; return 0; }
1
#include <bits/stdc++.h> #define ll long long #define coutp(a) cout << "(" << a.first << ", " << a.second << ")" using namespace std; const int mx = 2e5; int parent[mx + 10], Rank[mx + 10]; int get(int a) { if(parent[a] != a) parent[a] = get(parent[a]); return parent[a]; } void Union(int a, int b) { a = get(a); b = get(b); if(Rank[a] == Rank[b]) Rank[a]++; if(Rank[a] > Rank[b]) { parent[b] = a; } else { parent[a] = b; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int tt; // cin >> tt; tt = 1; for(int pp = 0 ; pp < tt ; pp++) { int n; cin >> n; vector <pair<ll,ll>> p; ll m = 0; for (int i = 0; i < n; i++) { ll a, b; cin >> a >> b; p.push_back({b, a}); m += a; } sort(p.begin(), p.end()); ll l = 0, r = n - 1, total = 0, ans = 0; while(total < m) { if(total < p[l].first) { if(p[r].second <= p[l].first - total) { ans += p[r].second*2; total += p[r].second; r--; } else { ans += (p[l].first - total)*2; p[r].second -= (p[l].first - total); total = p[l].first; } } if(total != m && total >= p[l].first) { ans += p[l].second; total += p[l].second; l++; } } cout << ans << endl; } }
4
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; for (int i = 0, _n = (c + 1); i < _n; i++) { if (i % a == 0 && (c - i) % b == 0) { puts("Yes"); return 0; } } puts("No"); return 0; }
1
#include<bits/stdc++.h> using namespace std; void serch(int n,int now) { if(n==1)cout<<now<<endl; else if(n%2==0)serch(n/2,now+1); else serch(n*3+1,now+1); } int main() { int n; while(cin>>n,n)serch(n,0); }
0
#include <bits/stdc++.h> using namespace std; int main() { int w, h, k; cin >> w >> h >> k; int count = 0; while (k) { count += 2 * (w + (h - 2)); w -= 4; h -= 4; k--; } cout << count; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); long long n, l; cin >> n >> l; long long a[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; } sort(a, a + n); double r, d; r = max((double)a[0], (double)(l - a[n - 1])); for (int i = 1; i < n; ++i) { d = (a[i] - a[i - 1]) / 2.0; if (d > r) r = d; } printf("%12lf\n", r); return 0; }
2