solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; const long long inf = 1e10 + 2; const double eps = 0; const int ms = 0; const int md = 0; const int mxn = 105; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; int temp = n; int hand, total = 0, ans = 0; while (temp--) { cin >> hand; total += hand; } for (int i = 1; i <= 5; i++) { total += 1; if (total % (n + 1) != 1) { ans += 1; } } cout << ans; }
1
#include <bits/stdc++.h> using namespace std; int a[1000], cnt; string s; int main() { getline(cin, s); for (int i = 1; i <= s.length() - 2; i += 3) { a[s[i]] = 1; } for (int i = 0; i <= 256; i++) cnt += a[i]; cout << cnt; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 20; bool v[MAXN][MAXN]; int r, c, n, k; int main() { ios::sync_with_stdio(0); while (scanf("%d %d %d %d", &r, &c, &n, &k) > 0) { memset(v, 0, sizeof(v)); for (int i = (0); i < (n); i++) { int x, y; scanf("%d %d", &x, &y); x--; y--; v[x][y] = true; } int res = 0; for (int x0 = (0); x0 < (r); x0++) for (int y0 = (0); y0 < (c); y0++) for (int xf = (x0); xf < (r); xf++) for (int yf = (y0); yf < (c); yf++) { int cant = 0; for (int xi = (x0); xi < (xf + 1); xi++) for (int yi = (y0); yi < (yf + 1); yi++) cant += v[xi][yi]; if (cant >= k) res++; } printf("%d\n", res); } return 0; }
6
//Largest Square #include <iostream> #include <algorithm> using namespace std; static const int MAX = 1400; int dp[MAX][MAX], G[MAX][MAX]; int H, W; int getLargestSquare() { int maxWidth = 0; for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) { dp[i][j] = (G[i][j] + 1) % 2; maxWidth |= dp[i][j]; } } for(int i = 1; i < H; i++) { for(int j = 1; j < W; j++) { if(dp[i][j] == 0) continue; dp[i][j] += min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]); maxWidth = max(maxWidth, dp[i][j]); } } return maxWidth * maxWidth; } int main() { int c; cin >> H >> W; for(int i = 0; i < H; i++) { for(int j = 0; j < W; j++) cin >> G[i][j]; } cout << getLargestSquare() << endl; }
0
#include<bits/stdc++.h> using namespace std; string s; int main(){ cin>>s; int n=s.length(); for(int i=0;i<n;i++)if(s[i]!=s[n-i-1])return puts("No"),0; for(int i=0;i<(n-1)/2;i++)if(s[i]!=s[(n-1)/2-i-1])return puts("No"),0; for(int i=(n+1)/2;i<n;i++)if(s[i]!=s[n-1-(i-(n+1)/2)])return puts("No"),0; return puts("Yes"),0; }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<ll, P> P3; const ll MOD = ll(1e9+7); const int IINF = INT_MAX; const ll LLINF = LLONG_MAX; const int MAX_N = int(3e5 + 5); const double EPS = 1e-4; const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0}; #define REP(i, n) for (int i = 0; i < n; i++) #define REPR(i, n) for (int i = n; i >= 0; i--) #define SORT(v) sort((v).begin(), (v).end()) #define ALL(v) (v).begin(), (v).end() int n, q, cmax, cnt[MAX_N]; int solve(int m){ int ma = 0; for(int i=0; i<cmax; i+=m){ int bg = i+ma, en = min(i+m-1, cmax); if(bg>cmax || cnt[en]-cnt[bg]<=0)continue; ma = lower_bound(cnt+bg, cnt+en, cnt[en])-(cnt+i); } return ma; } int main() { cin >> n >> q; REP(i,n){ int c; cin >> c; cnt[c]++; cmax = max(cmax, c); } REP(i,cmax){ cnt[i+1] += cnt[i]; } REP(i,q){ int m; cin >> m; cout << solve(m) << endl; } return 0; }
0
#include<iostream> #include<math.h> #include<float.h> #include<cstdio> using namespace std; #define EPS (1e-10) #define equals(a,b) fabs((a)-(b))<DBL_EPSILON*fmax(1,fmax(fabs(a),fabs(b))) //点のクラス class Point{ public: double x,y; Point(double x=0,double y=0):x(x),y(y){} Point operator + (Point p){return Point(x+p.x,y+p.y);} Point operator - (Point p){return Point(x-p.x,y-p.y);} Point operator * (double a){return Point(a*x,a*y);} Point operator / (double a){return Point(x/a,y/a);} double norm(){return x*x+y*y;} double abs(){return sqrt(norm());} bool operator < (const Point &p)const{ return x!=p.x?x<p.x:y<p.y; } bool operator == (const Point &p)const{ return equals(x,p.x)&&equals(y,p.y); } }; typedef Point Vector; //内積 double dot(Vector a,Vector b){ return a.x*b.x+a.y*b.y; } //外戚 double cross(Vector a,Vector b){ return a.x*b.y-a.y*b.x; } //ベクトルa,bの位置関係 int ccw(Point p0,Point p1,Point p2){ Vector a=p1-p0; Vector b=p2-p0; if(cross(a,b)>EPS) return 1;//p0,p1,p2が反時計回り if(cross(a,b)<-EPS) return -1;//p0,p1,p2が時計回り if(dot(a,b)<-EPS) return 2;//p1,p0,p2の順で一直線 if(a.norm()<b.norm()) return -2;//p0,p1,p2の順で一直線 return 0; //p0,p2,p1の順で一直線 } int main(){ int q,flag; Point p1,p2; scanf("%lf %lf %lf %lf",&p1.x,&p1.y,&p2.x,&p2.y); cin>>q; Point p; for(int i=0;i<q;i++){ cin>>p.x>>p.y; flag=ccw(p1,p2,p); switch (flag){ case 1: cout<<"COUNTER_CLOCKWISE"; break; case -1: cout<<"CLOCKWISE"; break; case 2: cout<<"ONLINE_BACK"; break; case -2: cout<<"ONLINE_FRONT"; break; default: cout<<"ON_SEGMENT"; } cout<<"\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int m, n; while (cin >> m >> n, m) { bool live[1010]; int idx = 1, cnt = m; memset(live, true, sizeof live); for (int i = 1; i <= n; ++i) { string s; cin >> s; if (cnt != 1) { string str = to_string(i); if (i % 15 == 0) str = "FizzBuzz"; else if (i % 3 == 0) str = "Fizz"; else if (i % 5 == 0) str = "Buzz"; if (str != s) { live[idx] = false; cnt--; } idx++; if (idx == m + 1) idx = 1; while (!live[idx]) { idx++; if (idx == m + 1) idx = 1; } } } vector<int> ans; for (int i = 1; i <= m; ++i) { if (live[i]) ans.push_back(i); } for (int i = 0; i < ans.size(); ++i) { cout << ans[i] << (i + 1 == ans.size() ? "\n" : " "); } } }
0
#include <bits/stdc++.h> using namespace std; long long int dp[105][2610]; string s; int n; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); for (int i = 1; i <= 26; i++) { dp[1][i] = 1; } for (int i = 2; i <= 100; i++) { for (int j = i; j <= i * 26; j++) { for (int k = 1; k <= 26; k++) { dp[i][j] += dp[i - 1][j - k]; dp[i][j] %= 1000000007; } } } cin >> n; while (n--) { cin >> s; int sum = 0; for (int i = 0; i < s.size(); i++) { sum += (int(s[i] - 'a') + 1); } cout << dp[s.size()][sum] - 1 << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, m; set<pair<int, int> > tt; const int MAXN = 300006; vector<int> g[MAXN]; bool used[MAXN]; int timer, tin[MAXN], fup[MAXN]; int pol[1000000]; void IS_BRIDGE(int v, int to) { tt.insert({v, to}); tt.insert({to, v}); } void dfs(int v, int p = -1) { used[v] = true; tin[v] = fup[v] = timer++; for (size_t i = 0; i < g[v].size(); ++i) { int to = g[v][i]; if (to == p) continue; if (used[to]) fup[v] = min(fup[v], tin[to]); else { dfs(to, v); fup[v] = min(fup[v], fup[to]); if (fup[to] > tin[v]) IS_BRIDGE(v, to); } } } void find_bridges() { timer = 0; for (int i = 0; i < n; ++i) used[i] = false; for (int i = 0; i < n; ++i) if (!used[i]) dfs(i); } bool used2[1000000]; vector<int> g2[MAXN]; int kupol = 1; void dfs4(int v, int cc) { used2[v] = true; pol[v] = cc; for (int i = 0; i < g[v].size(); i++) { int to = g[v][i]; if (!used2[to]) { if (tt.find({v, to}) == tt.end()) { dfs4(to, cc); } } } } void dfs2(int v) { used[v] = true; for (int i = 0; i < g[v].size(); i++) { int to = g[v][i]; if (!used[to]) { if (tt.find({v, to}) == tt.end()) { dfs2(to); } else { if (pol[to] == 0) { kupol++; dfs4(to, kupol); } dfs2(to); g2[pol[v]].push_back(pol[to]); } } } } int dp[1000000]; int tp[1000000]; int ans = 0; void dfs3(int v, int par, int dep) { int mx1 = 0; int mx2 = 0; for (auto to : g2[v]) { if (to != par) { dfs3(to, v, dep + 1); if (tp[to] > mx1) { mx2 = mx1; mx1 = tp[to]; } else if (tp[to] > mx2) { mx2 = tp[to]; } } } tp[v] = mx1 + 1; ans = max(ans, mx1 + mx2 + 1); } int main() { ios_base::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; u--; v--; g[u].push_back(v); g[v].push_back(u); } find_bridges(); for (int i = 0; i < n; i++) { used[i] = false; } dfs4(0, 1); dfs2(0); dfs3(1, 0, 1); cout << ans - 1 << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; long long mod = (long long)1e9 + 7; long long fast_expo(long long base, long long pw) { long long ans = 1; while (pw > 0) { if (pw % 2 == 1) ans = ans * base % mod; base = base * base % mod; pw = pw / 2; } return ans; } int main() { long long a, b, n, x; cin >> a >> b >> n >> x; long long inv1 = fast_expo(a - 1, mod - 2); long long an = fast_expo(a, n); long long bolu = (an - 1) * inv1 % mod; if (a == 1) bolu = n % mod; long long ilk = b * bolu % mod; long long iki = an * x % mod; long long res = ilk + iki; if (n == 1) { res = a * x % mod + b % mod; } cout << res % mod << endl; }
4
#include <iostream> const int INF=1e9; using namespace std; int main(){ int d[100][100],n,p; cin>>n; for(int i=0;i<n;i++) for(int j=0;j<n;j++) d[i][j]=INF; for(int i=0,r,k,t;i<n&&cin>>r>>k;i++) for(int j=0;j<k&&cin>>t;j++) d[r-1][t-1]=1; for(int k=0;k<n;k++) for(int i=0;i<n;i++) for(int j=0;j<n;j++) d[i][j]=min(d[i][j],d[i][k]+d[k][j]); for(int i=0;i<n;i++) for(int j=0;j<n;j++) d[i][j]++; cin>>p; for(int i=0,s,t,v;i<p;i++){ cin>>s>>t>>v; if(d[s-1][t-1]>v) cout<<"NA"<<endl; else cout<<d[s-1][t-1]<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; long long eq(long long start, long long k) { long long num = k - (start - 1); return ((num * (start + k)) / 2) - (num - 1); } void solve() { long long n, k; cin >> n >> k; long long q = eq(2, k); if (n == 1) cout << 0 << '\n'; else if (n <= k) cout << 1 << '\n'; else if (q < n) cout << -1 << '\n'; else { long long en = k; long long st = 2; long long res = -1; while (st < en && st > 1) { long long mid = st + en >> 1; if (eq(mid, k) < n) { en = mid; } else { st = mid + 1; res = mid; } } res--; cout << k - res << '\n'; } } int main() { ios::sync_with_stdio(0), cin.tie(nullptr), cout.tie(nullptr); int t = 1; while (t--) { solve(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; struct matrix { long long t[3][3]; matrix(int k) { if (k == -1) { for (int i = 0; i < (3); ++i) for (int j = 0; j < (3); ++j) t[i][j] = 0; } else { for (int i = 0; i < (3); ++i) for (int j = 0; j < (3); ++j) t[i][j] = 1; t[0][2] = t[2][0] = 0; for (int i = 0; i < (3); ++i) if ((((k) >> (i)) & 1)) for (int j = 0; j < (3); ++j) t[i][j] = 0; } } }; matrix operator*(matrix a, matrix b) { matrix c(-1); for (int i = 0; i < (3); ++i) for (int j = 0; j < (3); ++j) for (int k = 0; k < (3); ++k) { c.t[i][j] = (c.t[i][j] + a.t[i][k] * b.t[k][j] % MOD) % MOD; } return c; } matrix POW(matrix a, long long k) { if (k == 1) return a; matrix c = POW(a, k >> 1ll); c = c * c; if (k % 2) c = c * a; return c; } int cnt[4]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, m; cin >> n >> m; vector<pair<long long, int> > ban; for (int i = (1); i <= (n); ++i) { long long a, l, r; cin >> a >> l >> r; ban.push_back(pair<long long, int>(l, a)); if (r != m) ban.push_back(pair<long long, int>(r + 1, -a)); } sort(ban.begin(), ban.end()); matrix ans(-1); ans.t[1][0] = 1; long long col = 1, status = 0; ban.push_back(pair<long long, int>(m + 1, 4)); for (pair<long long, int> x : ban) { if (x.first != col) { matrix now(status); if (col != 1) ans = POW(now, x.first - col) * ans; else { if (x.first - col > 1) ans = POW(now, x.first - col - 1) * ans; } col = x.first; } if (x.first == 1) { if (x.second == -2) { cout << 0; return 0; } } if (x.second < 0) { cnt[-x.second]--; if (cnt[-x.second] == 0) status ^= (1 << (-x.second - 1)); } else { if (cnt[x.second] == 0) status ^= (1 << (x.second - 1)); cnt[x.second]++; } } cout << ans.t[1][0]; }
6
#include<bits/stdc++.h> using namespace std; int n,a[500001],b[500001]; inline int f(int x) { for(int i=0;i<n;i++)b[i]=(a[i]>=x); bool f=1; for(int i=1;i<n;i++) if(b[i]==b[i-1]){f=0;break;} if(f)return b[0]; int best=n,ans=0,m=n/2; for(int i=0;i<n;) { int j=i; while(j+1<n&&b[j+1]==b[j])j++; if(i==j){i++;continue;} int val=(i<=m&&m<=j?0:(i>m?i-m:m-j)); if(best>val)best=val,ans=b[i]; i=j+1; } return ans; } int main() { scanf("%d",&n);n=2*n-1; for(int i=0;i<n;i++)scanf("%d",a+i); int l=1,r=n,ans=1; while(l<=r) { int m=(l+r)/2; if(f(m))l=m+1,ans=m; else r=m-1; } cout<<ans<<endl; return 0; }
0
#include<bits/stdc++.h> using namespace std; #define int long signed main(){ int n,k,r=0; cin>>n>>k; while(n)n/=k,++r; cout<<r; }
0
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <vector> #include <queue> #include <sstream> using namespace std; int w, h; string field[100]; bool check[100][100]; string search(int y, int x){ if(y < 0 || x < 0 || y >= h || x >= w){ return ""; } if(check[y][x]){ return ""; } if(field[y][x] != '.'){ string ret = " "; ret[0] = field[y][x]; return ret; } check[y][x] = true; return search(y - 1, x) + search(y + 1, x) + search(y, x - 1) + search(y, x + 1) + "1"; } int main(){ while(true){ cin >> w >> h; if(w == 0 && h == 0){ break; } for(int i = 0; i < h; i++){ cin >> field[i]; } for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ check[i][j] = false; } } int ans_b = 0; int ans_w = 0; for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ string ret = search(i, j); bool flag1 = false; bool flag2 = false; int count = 0; for(int k = 0; k < ret.size(); k++){ if(ret[k] == 'B'){ flag1 = true; }else if(ret[k] == 'W'){ flag2 = true; }else{ count++; } } if(flag1 && flag2){ continue; }else if(flag1){ ans_b += count; }else if(flag2){ ans_w += count; } } } cout << ans_b << " " << ans_w << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> groups(n); for (int i = 0; i < n; i++) { cin >> groups[i]; } sort(groups.begin(), groups.end()); cout << groups[n / 2]; return 0; }
1
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int n, m, k; long long tot, ans; struct cla { int a, b; } c[1000010]; bool operator<(cla a, cla b) { return a.b < b.b; } int main() { n = read(); m = read(); k = read(); for (int i = 1; i <= n; i++) { c[i].a = read(); c[i].b = read(); tot += c[i].a; c[i].a = m - c[i].a; } tot = (long long)n * k - tot; if (tot <= 0) { printf("0\n"); return 0; } sort(c + 1, c + n + 1); for (int i = 1; i <= n; i++) { if (!tot) break; if (c[i].a <= tot) { tot -= c[i].a; ans += (long long)c[i].b * c[i].a; } else if (c[i].a > tot) { ans += (long long)tot * c[i].b; tot = 0; } } printf("%lld\n", ans); }
3
#include <bits/stdc++.h> using namespace std; long long m, n, a[100010], ans[100010], sum[100010]; int main() { long long i, t; cin >> n >> m; for (i = 0; i < m; i++) { cin >> a[i]; sum[i + 1] = sum[i] + a[i]; } ans[m - 1] = a[m - 1] + 1; t = n - a[m - 1]; for (i = m - 2; i >= 0; i--) { if (t - a[i] > sum[i] || t <= 0) { cout << -1 << endl; return 0; } ans[i] = max(max((long long)1, a[i] - ans[i + 1] + 1), t - sum[i]) + ans[i + 1]; t = n - ans[i] + 1; } if (t > 0) { cout << -1 << endl; return 0; } for (i = 0; i < m; i++) { cout << ans[i] - a[i] << " "; } cout << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; long long sum1d(vector<int>& v, int st, int& l, int& r) { int n = (int)v.size(); vector<long long> dp(n, 0); vector<int> back(n, -1); long long m = 0; l = n; r = n - 1; for (int i = st; i < n; i++) { if (i > st && dp[i - 1] > 0) { back[i] = back[i - 1]; } else { back[i] = i; } dp[i] = max(i > st ? dp[i - 1] : 0ll, 0ll) + v[i]; if (dp[i] > m) { l = back[i]; r = i; m = dp[i]; } } return m; } int main() { int n; scanf("%d", &n); vector<int> v(n); long long s = 0; for (int i = 0; i < n; i++) { scanf("%d", &v[i]); s += v[i]; } long long ans = 0; long long ps = 0; int a1, a2, a3; for (int i = 0; i < n; i++) { int l = -1; int r = -1; long long x = sum1d(v, i, l, r); long long c = ps + x - (s - ps - x); if (c >= ans) { a1 = i; a2 = l; a3 = r + 1; ans = c; } ps += v[i]; } printf("%d %d %d\n", a1, a2, a3); return 0; }
3
#include<bits/stdc++.h> #define fo(i,a,b) for(int i=a;i<=b;i++) #define fd(i,a,b) for(int i=a;i>=b;i--) using namespace std; typedef long long LL; typedef double db; int get(){ char ch; while(ch=getchar(),(ch<'0'||ch>'9')&&ch!='-'); if (ch=='-'){ int s=0; while(ch=getchar(),ch>='0'&&ch<='9')s=s*10+ch-'0'; return -s; } int s=ch-'0'; while(ch=getchar(),ch>='0'&&ch<='9')s=s*10+ch-'0'; return s; } const int N = 605; const int mo = 1e9+7; int n; int a[N]; bool bz[N]; bool can[N]; LL f[N][305][305]; LL add(LL x,LL y){return x+y>=mo?x+y-mo:x+y;} int main(){ n=get(); fo(i,1,n*2)a[i]=get(); fo(i,1,n*2)if (a[i]!=-1)bz[a[i]]=1; fo(i,1,n*2)can[i]=1; fo(i,1,n) if (a[i*2-1]!=-1&&a[i*2]!=-1)can[a[i*2-1]]=can[a[i*2]]=0; f[n*2+1][0][0]=1; fd(i,n*2,1) if (can[i]){ if (bz[i]){ fo(j,0,n) fo(k,0,n) if (f[i+1][j][k]){ //( if (j)f[i][j-1][k]=add(f[i][j-1][k],f[i+1][j][k]); //) f[i][j][k+1]=add(f[i][j][k+1],f[i+1][j][k]); } } else{ fo(j,0,n) fo(k,0,n) if (f[i+1][j][k]){ //( if (j)f[i][j-1][k]=add(f[i][j-1][k],f[i+1][j][k]); if (k)f[i][j][k-1]=(f[i][j][k-1]+f[i+1][j][k]*k)%mo; //) f[i][j+1][k]=add(f[i][j+1][k],f[i+1][j][k]); } } } else fo(j,0,n)fo(k,0,n)f[i][j][k]=f[i+1][j][k]; int cnt=0; fo(i,1,n) if (a[i*2-1]==-1&&a[i*2]==-1)cnt++; LL ans=f[1][0][0]; fo(i,1,cnt)ans=ans*i%mo; cout<<ans<<endl; return 0; }
0
/* * 2565.cc: Broken Audio Signal */ #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<string> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<deque> #include<algorithm> #include<numeric> #include<utility> #include<complex> #include<functional> using namespace std; /* constant */ const int MAX_N = 1000; const int MAX_A = 1000000000; const int INF = 1 << 30; /* typedef */ typedef vector<int> vi; typedef queue<int> qi; typedef pair<int,int> pii; /* global variables */ bool xs[MAX_N]; int as[MAX_N]; /* subroutines */ /* main */ int main() { for (;;) { int n; cin >> n; if (n == 0) break; for (int i = 0; i < n; i++) { string s; cin >> s; if (s == "x") xs[i] = true, as[i] = 0; else xs[i] = false, as[i] = atoi(s.c_str()); //printf("%d: %d(%d)\n", i, as[i], xs[i]); } int mina = -INF, maxa = INF; bool ok = true; for (int i = 0; i < n; i++) { if (xs[i]) { if (! (i & 1)) { int ai = INF; if (i > 0) { if (xs[i - 1]) { ok = false; break; } if (ai > as[i - 1]) ai = as[i - 1]; } if (i < n - 1) { if (xs[i + 1]) { ok = false; break; } if (ai > as[i + 1]) ai = as[i + 1]; } ai--; if (maxa > ai) maxa = ai; //printf("i=%d, ai=%d, maxa=%d\n", i, ai, maxa); } else { int ai = -INF; if (i > 0) { if (xs[i - 1]) { ok = false; break; } if (ai < as[i - 1]) ai = as[i - 1]; } if (i < n - 1) { if (xs[i + 1]) { ok = false; break; } if (ai < as[i + 1]) ai = as[i + 1]; } ai++; if (mina < ai) mina = ai; //printf("i=%d, ai=%d, mina=%d\n", i, ai, mina); } } else { if (! (i & 1)) { if ((i > 0 && ! xs[i - 1] && as[i - 1] <= as[i]) || (i < n - 1 && ! xs[i + 1] && as[i + 1] <= as[i])) { ok = false; break; } } else { if ((i > 0 && ! xs[i - 1] && as[i - 1] >= as[i]) || (i < n - 1 && ! xs[i + 1] && as[i + 1] >= as[i])) { ok = false; break; } } } } //printf("mina=%d, maxa=%d, ok=%d\n", mina, maxa, ok); if (! ok || mina > maxa) puts("none"); else if (mina < maxa) puts("ambiguous"); else printf("%d\n", mina); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; long long int n; long long int k; long long int sum = 0; while (t--) { cin >> n; cin >> k; if ((k % 2) != (n % 2)) { cout << "NO" << endl; } else { sum = ((2 * k) * ((2 * k) + 1) / 2) - 2 * (k * (k + 1) / 2); if (n >= sum) { cout << "YES" << endl; } else { cout << "NO" << endl; } } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long a[n], s = 0, ans = 0, now = 0; for (int i = 0; i < n; i++) cin >> a[i], s += a[i]; s /= n; for (int i = 0; i < n; i++) { now += a[i] - s; ans += now > 0 ? now : -now; } cout << ans << endl; }
2
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); ll t; cin >> t; while (t--) { ll n, k; bool ok = 1; cin >> n >> k; ll ara[n + 1]; for (int i = 0; i < n; ++i) cin >> ara[i]; ll mn = *min_element(ara, ara + n); mn += k; for (int i = 0; i < n; ++i) { if (abs(ara[i] - mn) > k) ok = 0; } if (ok) cout << mn << endl; else cout << "-1\n"; } }
2
#include <iostream> using namespace std; int main(){ long double a,b; cin>>a>>b; if(a>b){ cout<<"GREATER"; } else if(a<b){ cout<<"LESS"; } else cout<<"EQUAL"; }
0
#include <bits/stdc++.h> using namespace std; bool isVowel(string a) { int n = a.length(); char x = a[n - 2]; if (x == ' ') { int i = 1; while (x == ' ') { x = a[n - 2 - i]; i++; } } x = tolower(x); if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u' || x == 'y') return true; return false; } int main() { string a; getline(cin, a); if (isVowel(a)) cout << "YES"; else cout << "NO"; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int a,b,c; while(cin>>a>>b>>c,a,b,c){ int tool[301]={}; for(int i=1;i<=a+b+c;i++)tool[i]=2; int n; cin>>n; int num[1001][4]; for(int i=0;i<n;i++){ for(int j=0;j<4;j++)cin>>num[i][j]; if(num[i][3]==1)for(int j=0;j<3;j++)tool[num[i][j]]=1; } for(int i=0;i<n;i++){ if(num[i][3]==0){ int x=num[i][0],y=num[i][1],z=num[i][2]; if(tool[x]==1&&tool[y]==1)tool[z]=0; else if(tool[x]==1&&tool[z]==1)tool[y]=0; else if(tool[y]==1&&tool[z]==1)tool[x]=0; } } for(int i=1;i<=a+b+c;i++)cout<<tool[i]<<endl; } }
0
#include <bits/stdc++.h> using namespace std; long long m = 1e9 + 7; long long power(long long a, long long b) { if (b == 0) return 1; if (b == 1) return a % m; if (b % 2 == 0) { return (power(a, b / 2) % m * power(a, b / 2) % m) % m; } long long x = (power(a, b / 2) % m * power(a, b / 2) % m) % m; return (a % m * x % m) % m; } int main() { long long t; cin >> t; vector<long long> v; long long x = 1; while (x < m) { v.push_back(x); x = x * 2; } while (t--) { long long n, k; cin >> n >> k; vector<long long> res; while (k) { int r = lower_bound(v.begin(), v.end(), k) - v.begin(); if (r == v.size()) r--; else if (v[r] != k) r--; res.push_back(r); k -= v[r]; } long long ans = 0; for (long long x : res) { ans = (ans % m + power(n, x) % m) % m; } cout << ans << endl; } }
2
#include <bits/stdc++.h> #define N 210 #define ll long long using namespace std; int n,f[N],a[N]; ll s; int main(){ scanf("%lld",&s); for (n=0;s;) if (!(s&1)) s--,a[++n]=0; else s>>=1,a[++n]=1; int h=1,r=n; for (int i=1;i<=n;i++) if (a[i]) f[r--]=n-i+1; else f[h++]=n-i+1; printf("%d\n",n*2); for (int i=1;i<=n;i++) printf("%d ",f[i]); for (int i=1;i<n;i++) printf("%d ",i); printf("%d\n",n); return 0; }
0
#include<iostream> #include<algorithm> using namespace std; int N, T; pair<int, int> items[3000]; int dp[3000][3000]; int main(){ cin >> N >> T; for(int i = 0; i < N; i++){ int a, b; cin >> a >> b; items[i].first = a; items[i].second = b; } sort(items, items + N); int ans = 0; // dp[i][j] for(int i = 0; i < N; i++){ for(int j = 0; j < T; j++){ dp[i + 1][j] = dp[i][j]; if(j >= items[i].first){ dp[i + 1][j] = max(dp[i][j], dp[i][j - items[i].first] + items[i].second); } } ans = max(ans, dp[i][T - 1] + items[i].second); } cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; long long n, a[100010]; vector<pair<long long, long long> > v; int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { while (a[i] == a[i + 1]) i++; v.push_back(make_pair(a[i], i + 1)); } if (v.size() < 3) return cout << 0 << '\n', 0; for (int i = 0; i < v.size() - 2; i++) { if ((v[i].first > v[i + 1].first && v[i + 1].first < v[i + 2].first) || (v[i].first < v[i + 1].first && v[i + 1].first > v[i + 2].first)) { return cout << 3 << '\n' << v[i].second << ' ' << v[i + 1].second << ' ' << v[i + 2].second << '\n', 0; } } cout << 0 << '\n'; return 0; }
3
#include <bits/stdc++.h> using namespace std; int freq[5005], anabas[5005]; short etneen[5001][5001]; vector<vector<short> > cells(5001); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, q, ans = 0, rem = 1e9; cin >> n >> q; vector<pair<int, int> > v(q); for (int i = 0; i < q; i++) cin >> v[i].first >> v[i].second; for (int i = 0; i < q; i++) { for (int j = v[i].first; j <= v[i].second; j++) freq[j]++, cells[j].push_back(i); } for (int i = 0; i < 5005; i++) { if (freq[i]) ans++; if (freq[i] == 1) anabas[cells[i][0]]++; if (freq[i] == 2) etneen[cells[i][0]][cells[i][1]]++, etneen[cells[i][1]][cells[i][0]]++; } for (int i = 0; i < q; i++) { for (int j = i + 1; j < q; j++) { int cnt = anabas[i] + anabas[j]; cnt += etneen[i][j]; rem = min(rem, cnt); } } cout << ans - rem; return 0; }
3
#include <bits/stdc++.h> using namespace std; using LL = long long; using ULL = unsigned long long; using LD = long double; using pii = pair<int, int>; using pll = pair<LL, LL>; using pld = pair<LD, LD>; inline char get_diff(const char chr1, const char chr2) { if (chr1 != 'a' && chr2 != 'a') { return 'a'; } if (chr1 != 'b' && chr2 != 'b') { return 'b'; } return 'c'; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, t; cin >> n >> t; vector<string> strs(2); cin >> strs[0] >> strs[1]; int nr_diff_poss = 0; string res(n, '?'); vector<int> diff_poss, non_diff_poss; for (int i = 0; i < n; ++i) { if (strs[0][i] != strs[1][i]) { diff_poss.emplace_back(i); ++nr_diff_poss; } else { non_diff_poss.emplace_back(i); res[i] = strs[0][i]; } } if (t <= (nr_diff_poss - 1) / 2) { if (strs[0] == strs[1] && t == 0) { cout << strs[0]; } else { cout << "-1"; } return 0; } bool str = 0; int nr_diff_chrs[2] = {0, 0}; for (const int &pos : diff_poss) { res[pos] = strs[str][pos]; str ^= 1; ++nr_diff_chrs[str]; } if (nr_diff_chrs[0] != nr_diff_chrs[1]) { assert(!diff_poss.empty()); assert(abs(nr_diff_chrs[0] - nr_diff_chrs[1]) <= 1); const int &pos = diff_poss.back(); res[pos] = get_diff(strs[0][pos], strs[1][pos]); diff_poss.pop_back(), --nr_diff_poss; nr_diff_chrs[0] = nr_diff_chrs[1]; } for (int i = 0; i < non_diff_poss.size() && nr_diff_chrs[0] < t; ++i) { const int &pos = non_diff_poss[i]; res[pos] = get_diff(strs[0][pos], strs[0][pos]); ++nr_diff_chrs[0], ++nr_diff_chrs[1]; } cerr << "string so far: '" << res << "'" << endl; for (int i = 0; nr_diff_chrs[0] < t || nr_diff_chrs[1] < t; ++i) { assert(i < nr_diff_poss); const int &pos = diff_poss[i]; if (res[pos] == strs[0][pos]) { ++nr_diff_chrs[0]; } else { ++nr_diff_chrs[1]; } res[pos] = get_diff(strs[0][pos], strs[1][pos]); } cout << res; }
3
#include <bits/stdc++.h> #define int long long #define INF 1e4 #define MAX_V 505 using namespace std; /*?????§?????¢?????´?????????(Ford_Fulerson????????¨)???O(F|E|)*/ //????????¨????§???????(???????????????????????????) struct edge{ int to; vector<int> cap; int rev; }; vector<edge> G[MAX_V]; //??°???????????£??\????????? bool used[MAX_V]; //DFS??§?????§??????????????????????????° //from??????to??????????????????cap???????????°????????????????????? void add_edge(int from,int to,vector<int> &cap){ G[from].push_back((edge){to,cap,(int)G[to].size()}); G[to].push_back((edge){from,vector<int>((int)cap.size(),0),(int)G[from].size()-1}); } bool check(vector<int> &A){ for(int i=0;i<A.size();i++) if(A[i]!=0) return A[i]>0; return false; } bool check2(vector<int> &A){ for(int i=0;i<A.size();i++) if(A[i]==INF) return false; return true; } void Minus(vector<int> &res, vector<int> &v){ for(int i=0;i<res.size();i++) res[i]-=v[i]; } void Plus(vector<int> &res, vector<int> &v){ for(int i=0;i<res.size();i++) res[i]+=v[i]; } vector<int> Min(vector<int> &A, vector<int> &B){ for(int i=0;i<A.size();i++){ if(A[i]<B[i]) return A; if(A[i]>B[i]) return B; } return A; } //?¢?????????????DFS??§??¢??? vector<int> dfs(int v,int t,vector<int> f){ if(v == t)return f; used[v]=true; for(int i=0; i<G[v].size() ;i++){ edge &e = G[v][i]; if(!used[e.to] && check(e.cap) ){ vector<int> d = dfs(e.to ,t , Min(f,e.cap)); if(check(d)&&check2(d)){ Minus(e.cap, d); Plus(G[e.to][e.rev].cap, d); return d; } } } return vector<int>((int)f.size(),0); } bool isall0(vector<int> &v){ for(int i=0;i<v.size();i++) if(v[i]!=0) return false; return true; } bool isallnotINF(vector<int> &v){ for(int i=0;i<v.size();i++) if(v[i]>=INF) return false; return true; } int V=55; //s??????t???????????§???????±??????? vector<int> max_flow(int s,int t){ vector<int> flow = vector<int>(V,0); for(;;){ memset(used,0,sizeof(used)); vector<int> r=vector<int>(V,0); r[0]=INF; vector<int> f = dfs(s, t, r); if(isall0(f)) return flow; /*if(isallnotINF(f)) */Plus(flow, f); } } vector<int> tov(string s){ vector<int> res = vector<int>(V,0); int num1, num2, p=0; if('0'<=s[p]&&s[p]<='9'){ num1=0; while('0'<=s[p]&&s[p]<='9') num1=num1*10+s[p++]-'0'; }else num1=1; if(p==s.size()){ num2=0; }else{ if(p+1==s.size()) num2=1; else{ //assert(s[p+1]=='^'&&('0'<=s[p+2]&&s[p+2]<='9')); p+=2; num2=0; while('0'<=s[p]&&s[p]<='9') num2=num2*10+s[p++]-'0'; } } res[num2]=num1; return res; } vector<int> tocap(string s){ vector<int> res = vector<int>(V,0); s+='+'; string t; for(int i=0;i<s.size();i++){ if(s[i]=='+'){ vector<int> r=tov(t); Plus(res,r); t=""; }else t+=s[i]; } reverse(res.begin(), res.end()); return res; } string itos(int num){ string res; while(num){ res=(char)(num%10+'0')+res; num/=10; } return res; } void output(vector<int> ans){ reverse(ans.begin(), ans.end()); string s; int flag=0; for(int i=ans.size()-1;i>=0;i--){ if(ans[i]==0) continue; if(flag) s+="+"; flag=1; if(i==0||(i!=0&&ans[i]!=1)) s+=itos(ans[i]); if(i==1) s+="x"; else if(i) s+="x^", s+=itos(i); } if(s=="") s="0"; cout<<s<<endl; } signed main(){ int n, m; while(1){ cin>>n>>m; if(!n&&!m) break; int a, b; string s; for(int i=0;i<m;i++){ cin>>a>>b>>s; vector<int> cap=tocap(s); add_edge(a-1,b-1,cap); add_edge(b-1,a-1,cap); } assert(0!=n-1); output(max_flow(0,n-1)); for(int i=0;i<MAX_V;i++) G[i].clear(); } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long gcd(long long x, long long y) { if (y == 0) return x; return gcd(y, x % y); } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } void XsliferX() { ios::sync_with_stdio(0); ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); } long long f_p(long long x, int y) { if (y == 0) return 1; else if (y % 2 == 0) return f_p(x * x, y / 2); else return x * f_p(x * x, y / 2); } long long l_p(long long n) { n = n | (n >> 1); n = n | (n >> 2); n = n | (n >> 4); n = n | (n >> 8); return (n + 1) >> 1; } int dx[9] = {1, -1, 0, 0, 0, 1, -1, 1, -1}; int dy[9] = {0, 0, 1, -1, 0, 1, -1, -1, 1}; int n, m; char arr[101][101], a[101][101], b[101][101]; int main() { XsliferX(); cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> arr[i][j]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = '.'; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) b[i][j] = '.'; int idx; int f; for (char c = 'a'; c <= 'z'; c++) for (int i = 0; i < n; i++) { f = 0; idx = -1; for (int j = 0; j < m; j++) { if (arr[i][j] == c) { f++; idx = j; } } if (f == 1 && idx != -1) { a[i][idx] = c; } } for (char c = 'a'; c <= 'z'; c++) for (int j = 0; j < m; j++) { f = 0; idx = -1; for (int i = 0; i < n; i++) { if (arr[i][j] == c) { f++; idx = i; } } if (f == 1 && idx != -1 && a[idx][j] == c) { b[idx][j] = c; } } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { if (b[i][j] != '.') cout << b[i][j]; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f, mod = 1e9 + 7; const int maxn = 3e5 + 100; const long double PI = acos(-1.0); long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); } long long power(long long a, long long n) { long long p = 1; while (n > 0) { if (n % 2) { p = p * a; } n >>= 1; a *= a; } return p; } long long power(long long a, long long n, long long mod) { long long p = 1; while (n > 0) { if (n % 2) { p = p * a; p %= mod; } n >>= 1; a *= a; a %= mod; } return p % mod; } int n, m, k; int flag[maxn]; set<int> gg; set<int>::iterator it; vector<int> nex[maxn]; set<int> one, aa, bb; int main() { while (~scanf("%d%d%d", &n, &m, &k)) { one.clear(); gg.clear(); aa.clear(); bb.clear(); for (int i = 1; i <= n; i++) nex[i].clear(); for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); if (u == 1) one.insert(v); else if (v == 1) one.insert(u); else { nex[u].push_back(v); nex[v].push_back(u); } } memset(flag, 0, sizeof(flag)); for (int i = 2; i <= n; i++) gg.insert(i); int ans = 1, flv = 0; for (int i = 0; i < nex[2].size(); i++) { flag[nex[2][i]] = 1; gg.erase(nex[2][i]); aa.insert(nex[2][i]); } gg.erase(2); do { if (aa.size() == 0) break; flv = 0; while (!gg.empty()) { it = gg.begin(); int d = *it; flv = 0; bb.clear(); for (int i = 0; i < nex[d].size(); i++) if (flag[nex[d][i]] == ans) { aa.erase(nex[d][i]); bb.insert(nex[d][i]); } for (it = aa.begin(); it != aa.end(); it++) { gg.insert(*it); flag[*it] = ans - 1; } if (bb.size() == 0) { flv = 1; break; } aa.clear(); for (it = bb.begin(); it != bb.end(); it++) aa.insert(*it); gg.erase(d); } if (flv == 1) break; it = aa.begin(); int d = *it; flv = 0; gg.clear(); for (it = aa.begin(); it != aa.end(); it++) gg.insert(*it); aa.clear(); for (int i = 0; i < nex[d].size(); i++) if (flag[nex[d][i]] == ans) { gg.erase(nex[d][i]); flag[nex[d][i]] = ans + 1; aa.insert(nex[d][i]); } gg.erase(d); ans++; } while (1); gg.clear(); for (int i = 2; i <= n; i++) if (one.count(i) == 0) gg.insert(flag[i]); int s = gg.size(); if (s != ans || ans > k || k + one.size() >= n) cout << "impossible" << endl; else cout << "possible" << endl; } return 0; }
5
#include<iostream> #include<queue> #include<map> #include<algorithm> #define fs first #define sc second using namespace std; typedef pair<int,int> P; typedef pair<P,P> P2; int main(){ int w,h; string room[2][50]; int ry,rx,ly,lx; while(cin >> w >> h && (w||h)){ int ay[2],ax[2]; for(int i=0;i<h;i++){ for(int k=0;k<2;k++){ cin >> room[k][i]; for(int j=0;j<w;j++){ if(room[k][i][j] == 'L'){ ly = i; lx = j; room[k][i][j] == '.'; } if(room[k][i][j] == 'R'){ ry = i; rx = j; room[k][i][j] = '.'; } if(room[k][i][j] == '%'){ ay[k] = i; ax[k] = j; } } } } bool visit[50][50][50][50]; for(int i=0;i<h;i++) for(int j=0;j<w;j++) for(int k=0;k<h;k++) for(int l=0;l<w;l++)visit[i][j][k][l] = false; queue<P2> q; q.push(P2(P(ly,lx),P(ry,rx))); while(q.size()){ P2 p = q.front();q.pop(); ly = p.fs.fs; lx = p.fs.sc; ry = p.sc.fs; rx = p.sc.sc; if(visit[ly][lx][ry][rx])continue; visit[ly][lx][ry][rx] = true; if(room[0][ly][lx] == '%' && room[1][ry][rx] == '%')break; int ldy[] = {-1,0,1,0}, ldx[] = {0,-1,0,1}; int rdy[] = {-1,0,1,0}, rdx[] = {0,1,0,-1}; for(int i=0;i<4;i++){ int lsy = ly + ldy[i], lsx = lx + ldx[i]; int rsy = ry + rdy[i], rsx = rx + rdx[i]; if(lsy < 0 || h-1 < lsy)lsy = ly; if(lsx < 0 || w-1 < lsx)lsx = lx; if(room[0][lsy][lsx] == '#'){ lsy = ly; lsx = lx; } if(rsy < 0 || h-1 < rsy)rsy = ry; if(rsx < 0 || w-1 < rsx)rsx = rx; if(room[1][rsy][rsx] == '#'){ rsy = ry; rsx = rx; } if( (room[0][ly][lx] == '%') != (room[1][ry][rx] == '%'))continue; q.push(P2(P(lsy,lsx),P(rsy,rsx))); } } if(visit[ay[0]][ax[0]][ay[1]][ax[1]])cout << "Yes\n"; else cout << "No\n"; } }
0
#include <bits/stdc++.h> using namespace std; const int mxN = 1e5, M = 1e9 + 7; int n, eu[mxN], ev[mxN], ew[mxN], ec[mxN], s[mxN], ctp[mxN]; vector<int> adj[mxN]; long long ans = 1; vector<pair<long long, int> > v1, v2; pair<long long, int> ft[3 * mxN]; pair<long long, int> operator+(const pair<long long, int> &a, const pair<long long, int> &b) { return pair<long long, int>(a.first * b.first % M, a.second + b.second); } void upd(int i, pair<long long, int> x) { for (; i < 3 * n; i += i & -i) ft[i] = ft[i] + x; } pair<long long, int> qry(int i) { pair<long long, int> r(1, 0); for (; i > 0; i -= i & -i) r = r + ft[i]; return r; } long long pm(long long b, long long p) { long long r = 1; for (; p; b = b * b % M, p /= 2) if (p & 1) r = r * b % M; return r; } void dfs1(int u, int pe, bool a = 0) { s[u] = 1; for (int e : adj[u]) { int v = eu[e] ^ ev[e] ^ u; if (e == pe || ~ctp[v]) continue; dfs1(v, e, a); s[u] += s[v]; } if (a) ans = ans * pm(ew[pe], (long long)s[u] * (n - s[u])) % M; } int dfs2(int u, int pe, int n) { for (int e : adj[u]) { int v = eu[e] ^ ev[e] ^ u; if (e ^ pe && ctp[v] == -1 && s[v] > n / 2) return dfs2(v, e, n); } return u; } void dfs3(int u, int pe, long long pw, int pd) { v1.push_back(pair<long long, int>(pw, pd)); for (int e : adj[u]) { int v = eu[e] ^ ev[e] ^ u; if (e ^ pe && ctp[v] == -1) dfs3(v, e, pw * ew[e] % M, pd + (ec[e] ? 2 : -1)); } } void cd(int u = 0, int p = -2) { u = dfs2(u, 0, s[u]); ctp[u] = p; dfs1(u, 0); for (int e : adj[u]) { int v = eu[e] ^ ev[e] ^ u; if (~ctp[v]) continue; dfs3(v, 0, ew[e], n + (ec[e] ? 2 : -1)); for (pair<long long, int> a : v1) { pair<long long, int> b = qry(2 * n - a.second - 1); ans = ans * b.first % M * pm(a.first, b.second) % M; } for (; v1.size(); v2.push_back(v1.back()), v1.pop_back()) upd(v1.back().second, pair<long long, int>(v1.back().first, 1)); } for (; v2.size(); v2.pop_back()) upd(v2.back().second, pair<long long, int>(pm(v2.back().first, M - 2), -1)); for (int e : adj[u]) { int v = eu[e] ^ ev[e] ^ u; if (ctp[v] == -1) cd(v, u); } } void solve() { dfs1(0, 0); cd(); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i < n; ++i) { cin >> eu[i] >> ev[i] >> ew[i] >> ec[i], --eu[i], --ev[i]; adj[eu[i]].push_back(i); adj[ev[i]].push_back(i); } memset(ctp, -1, 4 * n); dfs1(0, 0, 1); for (int i = 1; i < n; ++i) ew[i] = pm(ew[i], M - 2); fill(ft, ft + 3 * n, pair<long long, int>(1, 0)); upd(n, pair<long long, int>(1, 1)); solve(); memset(ctp, -1, 4 * n); for (int i = 1; i < n; ++i) ec[i] ^= 1; solve(); cout << ans; }
4
#include <bits/stdc++.h> using namespace std; vector<long long> adj[1000100]; bool vis[1000100]; long long a[1000100]; long long bfs(long long s) { long long mn = 1e9; queue<long long> q; q.push(s); vis[s] = true; while (!q.empty()) { long long v = q.front(); mn = min(mn, a[v - 1]); q.pop(); for (int i = 0; i < adj[v].size(); i++) { long long to = adj[v][i]; if (!vis[to]) { vis[to] = true; q.push(to); } } } return mn; } int main() { ios::sync_with_stdio(0); long long n, m; cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < m; i++) { long long b, c; cin >> b >> c; adj[b].push_back(c); adj[c].push_back(b); } long long ans = 0; for (int i = 1; i <= n; i++) if (!vis[i]) ans += bfs(i); cout << ans; return 0; }
3
#include <iostream> #include <map> using namespace std; int main() { int n; while(cin >> n){ if(!n){ break; } map<unsigned long long int,unsigned long long int> syain; bool flag = true; unsigned long long int a,b,c; for(int i=0;i<n;i++){ map<unsigned long long int,unsigned long long int> tmp; cin >> a >> b >> c; if(syain.find(a) != syain.end() && syain[a] < 1000000) syain[a] += b*c; else syain[a] = b*c; if(syain[a] >= 1000000){ cout << a << endl; flag = false; } } if(flag) cout << "NA" << endl; } return 0; }
0
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std ; string s; int main(){ cin>>s; if (s=="AKIHABARA"|| s=="KIHABARA" || s=="AKIHBARA" || s=="AKIHABRA" || s=="AKIHABAR" || s=="KIHBARA" || s=="KIHABRA" || s=="KIHABAR" || s=="AKIHBRA" || s=="AKIHBAR" || s=="AKIHABR" || s=="KIHBRA" || s=="KIHBAR" || s=="KIHABR" || s=="AKIHBR" || s=="KIHBR") puts("YES") ; else puts("NO"); return 0 ; }
0
#include <bits/stdc++.h> using namespace std; long long nx2(long long n) { long long p = 1; if (n && !(n & (n - 1))) return n; while (p < n) p <<= 1; return p; } namespace Debug { void dout() { cerr << "\n"; } template <typename Head, typename... Tail> void dout(Head H, Tail... T) { cerr << " " << H; dout(T...); } template <typename T> void douta(const T *a, int n) { cerr << "\n["; for (int i = 0; i < n; i++) { cerr << " " << a[i]; } cerr << " ]\n"; } template <typename T> void doutaa(T **b, int r, int c) { for (int i = 0; i < r; i++) { cerr << "["; for (int j = 0; j < c; j++) { cerr << " " << b[i][j]; } cerr << " ]\n"; } } template <typename T> void dout(const vector<T> v) { cerr << "\n["; for (T i : v) { cerr << " " << i; } cerr << " ]\n"; } template <typename T> void dout(const vector<vector<T>> v) { cerr << "\n"; for (vector<T> u : v) { cerr << "["; for (T i : u) { cerr << " " << i; } cerr << " ]\n"; } } template <typename F, typename S> void dout(const vector<pair<F, S>> u) { cerr << "\n"; for (pair<F, S> v : u) { cerr << "[ " << v.first << " " << v.second << " ]\n"; } } } // namespace Debug using namespace Debug; int n; vector<pair<int, int>> a; vector<int> initial; int binsearch(int x, int r) { int l = 0, m; while (l < r) { m = l + (r - l + 1) / 2; if (a[m].first < x) { l = m; } else { r = m - 1; } } return (a[l].first < x) ? l : -1; } int main() { cin >> n; a.assign(n, make_pair(0, 0)), initial.assign(n, 1); int ans; for (int i = 0; i < n; i++) { cin >> a[i].first >> a[i].second; } sort(a.begin(), a.end()); for (int i = 0; i < n; i++) { if (i == 0) { ans = 1; continue; } int safei = binsearch(a[i].first - a[i].second, i); if (safei != -1) { initial[i] = initial[safei] + 1; } (ans) = max((ans), (initial[i - 1])); ; } (ans) = max((ans), (initial[n - 1])); ; cout << n - ans << '\n'; }
1
#include <bits/stdc++.h> using namespace std; long long pwr(long long a, long long b); long long pwr(long long a, long long b, long long m); int baap[200002]; int find(int i); bool Union(int x, int y); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n; cin >> n; long long x[n + 1], y[n + 1], z[n + 1]; for (int i = 1; i <= n; i++) { cin >> x[i] >> y[i] >> z[i]; } set<long long> rem; for (int i = 1; i <= n; i++) { if (rem.find(i) != rem.end()) continue; long long mx = 1e18, jmx = i; for (int j = i + 1; j <= n; j++) { if (rem.find(j) != rem.end()) continue; long long dis = pwr(abs(x[i] - x[j]), 2) + pwr(abs(y[i] - y[j]), 2) + pwr(abs(z[i] - z[j]), 2); if (dis < mx) { mx = dis; jmx = j; } } rem.insert(i); rem.insert(jmx); cout << i << " " << jmx << endl; } } long long pwr(long long a, long long b) { if (b == 0) return 1; long long c = pwr(a, b / 2); c = c * c; if (b % 2) return c * a; return c; } long long pwr(long long a, long long b, long long m) { if (b == 0) return 1; long long c = pwr(a, b / 2, m) % m; c = (c * c) % m; if (b % 2) return (c * a % m) % m; return c; } int find(int i) { if (baap[i] != i) baap[i] = find(baap[i]); return baap[i]; } bool Union(int x, int y) { x = find(x); y = find(y); if (x == y) return false; baap[x] = y; return true; }
3
#define _USE_MATH_DEFINES #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <clocale> #include <cmath> #include <cstdlib> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <tuple> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; #define IOS ios::sync_with_stdio(false); cin.tie(0); #define FOR(i, s, n) for(int i = (s), i##_len=(n); i < i##_len; ++i) #define FORS(i, s, n) for(int i = (s), i##_len=(n); i <= i##_len; ++i) #define VFOR(i, s, n) for(int i = (s); i < (n); ++i) #define VFORS(i, s, n) for(int i = (s); i <= (n); ++i) #define REP(i, n) FOR(i, 0, n) #define REPS(i, n) FORS(i, 0, n) #define VREP(i, n) VFOR(i, 0, n) #define VREPS(i, n) VFORS(i, 0, n) #define RFOR(i, s, n) for(int i = (s), i##_len=(n); i >= i##_len; --i) #define RFORS(i, s, n) for(int i = (s), i##_len=(n); i > i##_len; --i) #define RREP(i, n) RFOR(i, n, 0) #define RREPS(i, n) RFORS(i, n, 0) #define ALL(v) (v).begin(), (v).end() #define SORT(v) sort(ALL(v)) #define RSORT(v) sort(ALL(v), greater<decltype(v[0])>()) #define SZ(x) ((int)(x).size()) #define PB push_back #define EB emplace_back #define MP make_pair #define MT make_tuple #define BIT(n) (1LL<<(n)) #define UNIQUE(v) v.erase(unique(ALL(v)), v.end()) using ll = long long; using ull = unsigned long long; using Pi_i = pair<int, int>; using VB = vector<bool>; using VC = vector<char>; using VD = vector<double>; using VI = vector<int>; using VLL = vector<ll>; using VS = vector<string>; using VSH = vector<short>; using VULL = vector<ull>; const int MOD = 1000000007; const int INF = 1000000000; const int NIL = -1; template<class T, class S> bool chmax(T &a, const S &b){ if(a < b){ a = b; return true; } return false; } template<class T, class S> bool chmin(T &a, const S &b){ if(b < a){ a = b; return true; } return false; } int main(){ set<int> S; int q; cin >> q; REP(i, q){ int op, x, y; cin >> op >> x; switch(op){ case 0: S.insert(x); cout << S.size() << endl; break; case 1: cout << noboolalpha << (S.find(x) != S.end()) << "\n"; break; case 2: S.erase(x); break; case 3: cin >> y; for(auto itr = S.lower_bound(x); itr != S.end(); ++itr){ if(x <= *itr && *itr <= y) cout << *itr << "\n"; if(y <= *itr) break; } } } return 0; }
0
#include <bits/stdc++.h> using namespace std; #define ll long long #define BLOCK 750 #define mxn 5000001 using namespace std; struct query{ int l; int r; int i; }; query Q[mxn]; int ar[mxn] , ans[mxn]; int fre[mxn]; int cnt = 0; bool comp(query a , query b) { if(a.l / BLOCK != b.l/BLOCK) return a.l/BLOCK < b.l/BLOCK; return a.r < b.r; } void add(int pos) { fre[ar[pos]]++; if(fre[ar[pos]] == 1) cnt++; } void remove(int pos) { fre[ar[pos]]--; if(fre[ar[pos]] == 0) cnt--; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n , q; cin>>n >> q; for(int i=0;i<n;i++) cin>>ar[i]; for(int i=0;i<q;i++){ cin>>Q[i].l>>Q[i].r; Q[i].i = i , Q[i].l-- , Q[i].r--; } sort(Q , Q+q , comp); int ML = 0 , MR = -1; for(int i=0;i<q;i++) { int L = Q[i].l; int R = Q[i].r; while(ML > L) ML-- , add(ML); while(MR < R) MR++ , add(MR); while(ML < L) remove(ML) , ML++; while(MR > R) remove(MR) , MR--; ans[Q[i].i] = cnt; } for(int i=0;i<q;i++) cout<<ans[i]<<'\n'; }
0
#include <bits/stdc++.h> using namespace std; long long query(long long x, long long y, long long k) { long long br = 0; if (k < max(x, y)) { return -1; } if (x == y) { k -= x; br += x; br += (k / 2) * 2; if (k % 2 == 1) { br--; } } else { long long tmp = min(x, y); y = max(x, y); x = tmp; k -= x; br += x; br += ((y - x) / 2) * 2; if ((y - x) % 2 == 1) { k--; } k -= ((y - x) / 2) * 2; br += (k / 2) * 2; if (k % 2 == 1) { if ((y - x) % 2 == 1) { br++; } else { br--; } } } return br; } int main() { int q; cin >> q; for (int j = 0; j < q; j++) { long long x, y, k; cin >> x >> y >> k; cout << query(x, y, k) << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { long long n; cin >> n; long long last = -1; vector<long long> vec(n); for (long long i = 0; i < n; i++) { vec[i] = i + 1; } vector<pair<int, int> > ans; if (n == 2) { cout << 2 << '\n'; cout << 1 << ' ' << 2 << '\n'; continue; } ans.push_back({n, n - 2}); vec.pop_back(); vec.pop_back(); vec.pop_back(); vec.push_back(n - 1); vec.push_back(n - 1); int cnt = 0; while (vec.size() != 1) { ans.push_back({vec.back(), vec[vec.size() - 2]}); long long kek = ((vec[vec.size() - 1] + vec[vec.size() - 2]) / 2); vec.pop_back(); vec.pop_back(); vec.push_back(kek); cnt++; } cout << vec[0] << '\n'; for (auto& i : ans) { cout << i.first << ' ' << i.second << '\n'; } } }
3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, k; cin >> n >> k; int a[n], b[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; } sort(a, a + n); sort(b, b + n); for (int i = 0, j = n - 1, l = 0; i < n && j >= 0 && l < k; i++, j--, l++) { if (a[i] < b[j]) { a[i] = b[j]; } } int count = 0; for (int i = 0; i < n; i++) { count += a[i]; } cout << count << endl; } return 0; }
2
#include <iostream> using namespace std; int main(){ double a; while(cin >> a){ double s = a; for(int i=2;i<11;i++){ if (i % 2) a /= 3; else a *= 2; s += a; } cout.precision(8); cout << fixed << s << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int t, a, b; cin >> t; while (t--) { cin >> a >> b; if (a == b) cout << 0 << "\n"; else if (a > b) { if (a == b + 1) cout << 2 << "\n"; else if ((a - b) % 2 == 0) cout << 1 << "\n"; else cout << 2 << "\n"; } else if (a < b) { if (a + 1 == b) cout << 1 << "\n"; else if ((b - a) % 2 == 0) cout << 2 << "\n"; else cout << 1 << "\n"; } } }
1
#include <bits/stdc++.h> using namespace std; long long read() { int f = 1; long long res = 0; char ch; do { ch = getchar(); if (ch == '-') f = -f; } while (ch < '0' || ch > '9'); do { res = res * 10 + ch - '0'; ch = getchar(); } while (ch >= '0' && ch <= '9'); return f == 1 ? res : -res; } void fast_io() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const int N = 100005; const int M = 3010; const int mod = 1e9 + 7; const long long INF = 1e18; int n, m; int A[N], B[N]; char str[N]; int head[N], to[N * 2], nxt[N * 2], tot; void addEdge(int u, int v) { tot++; nxt[tot] = head[u]; to[tot] = v; head[u] = tot; } template <class T> T mmax(T a, T b) { return a < b ? b : a; } template <class T> T mmin(T a, T b) { return a < b ? a : b; } int countOne(long long set) { int res = 0; while (set) { res++; set &= set - 1; } return res; } bool contain(long long set, int i) { return (set & (1LL << i)) > 0; } long long myPow(long long a, long long p) { if (p == 0) return 1; long long res = myPow(a, p / 2); res *= res; res %= mod; if (p % 2 == 1) { res *= a; res %= mod; } return res % mod; } void addMode(long long &a, long long b) { a = (a + b) % mod; } long long mul(long long a, long long b) { return a * b % mod; } template <class T> void mySwap(T &a, T &b) { T tmp = a; a = b; b = tmp; } long long p[N][2][2]; void no() { cout << "NO\n"; exit(0); } void go(int sz, vector<set<pair<long long, long long>>> &nums) { if (sz == 1) return; long long mx[4] = {-INF, -INF, -INF, -INF}; set<pair<long long, long long>>::iterator its[4]; for (int i = 0; i < 4; i++) { its[i] = nums[i].begin(); } bool okok = false; vector<long long> idxs; for (int i = 1; i < sz; i++) { for (int j = 0; j < 4; j++) { mx[j] = mmax(mx[j], -p[its[j]->second][(j >> 1) ^ 1][(j & 1)]); its[j]++; } for (int j = 0; j < 4; j++) { if (mx[j] <= its[j]->first) { okok = true; for (auto it = nums[j].begin(); it != its[j]; it++) { idxs.push_back(it->second); } } if (okok) break; } if (okok) break; } if (!okok) no(); vector<set<pair<long long, long long>>> nnums(4); for (int i : idxs) { nnums[0].insert(make_pair(p[i][0][0], i)); nnums[1].insert(make_pair(p[i][0][1], i)); nnums[2].insert(make_pair(p[i][1][0], i)); nnums[3].insert(make_pair(p[i][1][1], i)); nums[0].erase(make_pair(p[i][0][0], i)); nums[1].erase(make_pair(p[i][0][1], i)); nums[2].erase(make_pair(p[i][1][0], i)); nums[3].erase(make_pair(p[i][1][1], i)); } go(idxs.size(), nnums); go(sz - idxs.size(), nums); } int main() { fast_io(); cin >> n; for (int i = 0; i < n; i++) { cin >> p[i][0][0] >> p[i][0][1]; cin >> p[i][1][0] >> p[i][1][1]; } vector<set<pair<long long, long long>>> nums(4); for (int i = 0; i < n; i++) { p[i][1][0] *= -1; p[i][1][1] *= -1; nums[0].insert(make_pair(p[i][0][0], i)); nums[1].insert(make_pair(p[i][0][1], i)); nums[2].insert(make_pair(p[i][1][0], i)); nums[3].insert(make_pair(p[i][1][1], i)); } go(n, nums); cout << "YES\n"; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int a, b, r; cin >> a >> b >> r; if (min(a, b) < 2 * r) { cout << "Second" << endl; } else { cout << "First" << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int Max = 1e5 + 10; bool seen[Max]; int H[Max]; int P[Max]; int G[Max]; int C[Max]; vector<int> N[Max]; bool DFS(int v) { seen[v] = true; bool ans = true; C[v] = P[v]; for (auto u : N[v]) { if (!seen[u]) { ans &= DFS(u); C[v] += C[u]; G[v] += G[u]; } } if (N[v].size() == 1 && v || N[v].size() == 0) { G[v] = H[v] + P[v]; if (G[v] % 2) ans = false; G[v] /= 2; if (G[v] > P[v] || G[v] < 0) ans = false; } else { int trgG = H[v] + C[v]; if (trgG % 2) ans = false; trgG /= 2; if (G[v] > trgG || trgG > C[v]) ans = false; G[v] = trgG; } return ans; } int main() { int q; cin >> q; while (q--) { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { G[i] = 0; C[i] = 0; seen[i] = false; N[i].clear(); cin >> P[i]; } for (int i = 0; i < n; i++) cin >> H[i]; for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--, b--; N[a].push_back(b); N[b].push_back(a); } bool res = DFS(0); if (res) cout << "YES\n"; else cout << "NO\n"; } }
3
#include <bits/stdc++.h> using namespace std; const int MAX = 1e6 + 6; vector<int> v; map<int, int> freq; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { v.clear(); freq.clear(); int n; cin >> n; for (int i = 0; i < n; ++i) { int a; cin >> a; ++freq[a]; } for (auto it = freq.begin(); it != freq.end(); ++it) { if ((it->second >= 4)) { v.push_back(it->first); v.push_back(it->first); v.push_back(it->first); v.push_back(it->first); } else if ((it->second) >= 2) { v.push_back(it->first); v.push_back(it->first); } } int x = v[0], y = v[2]; long double ret = (long double)y / (long double)x; for (int i = 3; i < v.size(); ++i) { if (v[i] == v[i - 1] && v[i - 2] == v[i - 3]) { long double can = (long double)v[i] / (long double)v[i - 2]; if (can < ret) { ret = can; x = v[i - 2]; y = v[i]; } } } cout << x << " " << x << " " << y << " " << y << "\n"; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, data[100005], cnt; struct Node { int num, r; } a[100005]; void swap(Node &x, Node &y) { Node temp = x; x = y; y = temp; } void stack_down(int x) { while (x * 2 <= cnt) { int t = x * 2; if (x * 2 < cnt && a[t + 1].num > a[t].num) t++; if (a[t].num <= a[x].num) break; swap(a[t], a[x]); x = t; } } void stack_up(int x) { while (x > 1) { if (a[x / 2].num >= a[x].num) break; swap(a[x / 2], a[x]); x = x / 2; } } void stack_pop() { swap(a[1], a[cnt]); cnt--; stack_down(1); } void stack_insert(Node k) { a[++cnt] = k; stack_up(cnt); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &data[i]); sort(data, data + n); int j = 1; cnt = 0; for (int i = 1; i < n; i++) { if (data[i] == data[i - 1]) j++; else { a[++cnt].num = j; a[cnt].r = data[i - 1]; j = 1; } } a[++cnt].num = j; a[cnt].r = data[n - 1]; for (int i = cnt / 2; i >= 1; i--) stack_down(i); int ans = 0; int s[100005][3]; while (cnt >= 3) { if (a[1].num == 0) break; Node k1 = a[1]; s[ans][0] = a[1].r; stack_pop(); if (a[1].num == 0) break; Node k2 = a[1]; s[ans][1] = a[1].r; stack_pop(); if (a[1].num == 0) break; Node k3 = a[1]; s[ans][2] = a[1].r; stack_pop(); ans++; k1.num--; k2.num--; k3.num--; if (k1.num != 0) stack_insert(k1); if (k2.num != 0) stack_insert(k2); if (k3.num != 0) stack_insert(k3); } printf("%d\n", ans); for (int i = 0; i < ans; i++) { sort(s[i], s[i] + 3); printf("%d %d %d\n", s[i][2], s[i][1], s[i][0]); } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int LG = 21; const int M = 1000; const int N = 500010; const int V = 1000010; int n, q, a[N], store[N], tmp[LG][M], cur[LG], msb[V]; int suff[N][LG], pref[N][LG], suff_sz[N], pref_sz[N]; void gauss() { for (int i = LG - 1; i >= 0; --i) { if (cur[i] == 0) continue; int yo, nxt; for (int j = 1; j < cur[i]; ++j) { if (tmp[i][j] != tmp[i][0]) { yo = tmp[i][j] ^ tmp[i][0], nxt = msb[yo]; tmp[nxt][cur[nxt]++] = yo; } } cur[i] = 1; } } void solve(int l, int r, vector<tuple<int, int, int> > v) { if (v.empty()) return; if (l == r) { for (auto it : v) { store[get<2>(it)] = a[l]; } return; } int mid = l + r >> 1, x, y, z; vector<tuple<int, int, int> > one, two, now; for (auto it : v) { tie(x, y, z) = it; if (y <= mid) one.push_back(it); else if (x > mid) two.push_back(it); else now.push_back(it); } solve(l, mid, one), solve(mid + 1, r, two); for (int i = 0; i < LG; ++i) cur[i] = 0; for (int i = mid; i >= l; --i) { int yo = a[i], nxt = msb[yo]; if (yo) tmp[nxt][cur[nxt]++] = yo; gauss(); suff_sz[i] = 0; for (int j = 0; j < LG; ++j) { if (cur[j]) suff[i][suff_sz[i]++] = tmp[j][0]; } } for (int i = 0; i < LG; ++i) cur[i] = 0; for (int i = mid + 1; i <= r; ++i) { int yo = a[i], nxt = msb[yo]; if (yo) tmp[nxt][cur[nxt]++] = yo; gauss(); pref_sz[i] = 0; for (int j = 0; j < LG; ++j) { if (cur[j]) pref[i][pref_sz[i]++] = tmp[j][0]; } } for (auto it : now) { int x, y, z; tie(x, y, z) = it; for (int i = 0; i < LG; ++i) cur[i] = 0; for (int i = 0; i < suff_sz[x]; ++i) { int yo = suff[x][i], nxt = msb[yo]; if (yo) tmp[nxt][cur[nxt]++] = yo; } for (int i = 0; i < pref_sz[y]; ++i) { int yo = pref[y][i], nxt = msb[yo]; if (yo) tmp[nxt][cur[nxt]++] = yo; } gauss(); int ans = 0; for (int i = LG - 1; i >= 0; --i) { if (cur[i]) ans = max(ans, ans ^ tmp[i][0]); } store[z] = ans; } } int main() { for (int i = 1; i < V; ++i) { for (int j = LG; j >= 0; --j) { if (i & 1 << j) { msb[i] = j; break; } } } cin >> n; for (int i = 1; i <= n; ++i) { scanf("%d", a + i); } cin >> q; vector<tuple<int, int, int> > v; for (int i = 0; i < q; ++i) { int l, r; scanf("%d %d", &l, &r); v.emplace_back(l, r, i); } solve(1, n, v); for (int i = 0; i < q; ++i) { printf("%d\n", store[i]); } return 0; }
6
#include <iostream> #include <cstdio> #define rep(i,n) for(int i=0;i<n;i++) #define ck(a) (0<=(a)&&(a)<9) int di[]={-3, -1, 1, 3}; int main() { using namespace std; int n, s, t, b; char cs, ct, cb; while((cin>>n),n) { cin >> cs >> ct >> cb; s = cs-'A'; t = ct-'A'; b = cb-'A'; // 確率漸化式(添字は場所に対応。漸化式はqを用いることによって更新される) double p[9]={0}; p[s]=1; while(n--) { double q[9]={0}; rep(i,9)rep(d,4) { if(ck(i+di[d])&&!(i%3==0&&di[d]==-1)&&!(i%3==2&&di[d]==1)&&i+di[d]!=b) q[i+di[d]]+=p[i]/4; else q[i]+=p[i]/4; } rep(i,9)p[i]=q[i]; // 最新データに更新 } printf("%.8f\n", p[t]); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base ::sync_with_stdio(false); int n, m; cin >> n >> m; vector<int> a(n + 1); vector<int> yes(n + 1), no(n + 1); int cntn = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] > 0) { yes[a[i]]++; } else { cntn++; no[abs(a[i])]++; } } set<int> pod; for (int i = 1; i <= n; i++) { int tru = yes[i] + cntn - no[i]; if (tru == m) pod.insert(i); } int cnt = pod.size(); for (int i = 1; i <= n; i++) { if (a[i] > 0) { if (pod.find(a[i]) != pod.end()) { if (cnt == 1) cout << "Truth" << endl; else cout << "Not defined" << endl; } else cout << "Lie" << endl; } else { if (pod.find(abs(a[i])) != pod.end()) { if (cnt == 1) cout << "Lie" << endl; else cout << "Not defined" << endl; } else cout << "Truth" << endl; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 111111; const int INF = int(1e9), mod = (int)1e9 + 7; char s[N + N]; stack<char> st; string res; int main() { gets(s); int n = (int)strlen(s); for (int(i) = (0); (i) < (n); ++(i)) st.empty() ? st.push(s[i]) : (st.top() == s[i] ? st.pop() : st.push(s[i])); while (!st.empty()) { res += st.top(); st.pop(); } reverse((res).begin(), (res).end()); puts(res.c_str()); return 0; }
1
#include <bits/stdc++.h> using namespace std; int father[200]; void merge(int, int); int find(int); int main() { int n, m; cin >> n >> m; bool allzero = true; for (int i = 1; i <= n + m; ++i) father[i] = -1; for (int i = 1; i <= n; ++i) { int k; cin >> k; if (k) allzero = false; for (int j = 1; j <= k; ++j) { int ok; cin >> ok; merge(i, ok + n); } } if (allzero) cout << n << endl; else { int alone = 0, connect = 0; for (int i = 1; i <= n + m; ++i) if (father[i] < 0) { if (father[i] == -1) { if (i <= n) ++alone; } else ++connect; } cout << alone + connect - 1 << endl; } return 0; } void merge(int x, int y) { int rx = find(x), ry = find(y); if (rx == ry) return; if (rx > ry) { father[ry] += father[rx]; father[rx] = ry; } else { father[rx] += father[ry]; father[ry] = rx; } } int find(int k) { int s = k; for (; father[s] > 0; s = father[s]) ; while (k != s && father[k] != s) { int t = father[k]; father[k] = s; k = t; } return s; }
3
#include <bits/stdc++.h> using namespace std; int main(void){ string str; cin >> str; cout << str.substr(0, 3) << endl; }
0
#include <bits/stdc++.h> using namespace std; struct node { node* next[26]; node* link; }; int nid; node pool[1100005]; void insert(node* root, string s) { for (char x : s) { if (root->next[x - 'a'] == nullptr) { root = root->next[x - 'a'] = pool + nid; nid++; } else { root = root->next[x - 'a']; } } } vector<int> e[1100005]; int dp[1100005][2]; bool start[1100005]; void dfs(int x) { dp[x][0] = 0; dp[x][1] = 1; for (int y : e[x]) { dfs(y); dp[x][0] += max(dp[y][0], dp[y][1]); dp[x][1] += dp[y][0]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); int t; cin >> t; while (t--) { nid = 1; int n; cin >> n; vector<string> vs; int gomno = 2; while (n--) { string s; cin >> s; vs.push_back(s); gomno += s.size(); } memset(pool, 0, sizeof(node) * gomno); for (string s : vs) { insert(pool, s); } queue<node*> q; pool->link = nullptr; for (int i = 0; i < 26; i++) { if (pool->next[i]) { pool->next[i]->link = pool; q.push(pool->next[i]); } } while (!q.empty()) { node* t = q.front(); q.pop(); if (t == nullptr) { continue; } for (int i = 0; i < 26; i++) { if (t->next[i] && t->link) { t->next[i]->link = t->link->next[i]; q.push(t->next[i]); } } } for (int i = 0; i < nid; i++) { e[i].clear(); } for (int i = 0; i < nid; i++) { start[i] = true; } for (int i = 0; i < nid; i++) { if (pool[i].link) { int u = pool[i].link - pool; int v = i; e[u].push_back(v); start[v] = false; } } int sol = 0; for (int i = 0; i < nid; i++) { if (start[i]) { dfs(i); if (i == 0) { sol += dp[i][0]; } else { sol += max(dp[i][0], dp[i][1]); } } } cout << sol << '\n'; } }
2
#include <bits/stdc++.h> using namespace std; #define N 55 #define S 5005 #define ll long long #define set(a,v) memset(a,v,sizeof(a)) int n,m,s,lim=5e3,c[N],d[N];bool vs[N];ll dp[N][S]; struct Edge {int v,a,b;};vector<Edge> e[N];queue<int> q; void addE(int u,int v,int a,int b) { e[u].push_back((Edge) {v,a,b}); e[v].push_back((Edge) {u,a,b}); } void SPFA() { set(dp,0x3f);dp[1][s]=0;vs[1]=1;q.push(1); while(!q.empty()) { int u=q.front();q.pop();vs[u]=0; for(int i=c[u];i<=lim;++i) dp[u][i]=min(dp[u][i],dp[u][i-c[u]]+d[u]); for(int i=0,v,a,b,fl;i<e[u].size();++i) { v=e[u][i].v;a=e[u][i].a;b=e[u][i].b;fl=0; for(int j=a;j<=lim;++j) if(dp[u][j]+b<dp[v][j-a]) dp[v][j-a]=dp[u][j]+b,fl=1; if(fl && !vs[v]) vs[v]=1,q.push(v); } } } int main() { scanf("%d %d %d",&n,&m,&s);s=min(s,lim); for(int i=1,u,v,a,b;i<=m;++i) scanf("%d %d %d %d",&u,&v,&a,&b),addE(u,v,a,b); for(int i=1;i<=n;++i) scanf("%d %d",&c[i],&d[i]);SPFA(); for(int i=2;i<=n;++i) { ll res=1e18; for(int j=0;j<=lim;++j) res=min(res,dp[i][j]); printf("%lld\n",res); }return 0; }
0
#include <bits/stdc++.h> long work() { char c = getchar(); long x, y, i, j, z = 0; if (c == '0') return 1; if (c == '1') return 8; if (c == '?') return 6; x = work(); c = getchar(); y = work(); for (i = 0; i < 4; i++) if (x & (1 << i)) for (j = 0; j < 4; j++) if (y & (1 << j)) if (c == '&') z |= 1 << (i & j); else if (c == '|') z |= 1 << (i | j); else if (c == '^') z |= 1 << (i ^ j); getchar(); return z; } int main() { scanf("%*d "); puts(work() & 6 ? "YES" : "NO"); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; int count=1; while(count*N%360!=0)count++; cout << count << endl; }
0
#include<iostream> #include<algorithm> #include<vector> #include<queue> #define M 1000000007 using namespace std; long long ans=1; long long n,t[100000],a[100000],u[100000]; int main(){ cin>>n; for(int i=0;i<n;i++)cin>>t[i]; for(int i=0;i<n;i++)cin>>a[i]; u[0]=t[0]; u[n-1]=a[n-1]; for(int i=1;i<n;i++){ if(t[i-1]!=t[i]){ if(u[i]&&u[i]!=t[i])ans=0; u[i]=t[i]; } if(t[i-1]>t[i])ans=0; } for(int i=n-2;i>=0;i--){ if(a[i]!=a[i+1]){ if(u[i]&&a[i]!=u[i])ans=0; u[i]=a[i]; } if(a[i]<a[i+1])ans=0; } for(int i=0;i<n;i++){ if(!u[i]){ ans= ( ans*min(t[i],a[i]) )%(long long)(M); } else if(u[i]>min(t[i],a[i]))ans=0; } if(n==1&&a[0]!=t[0])ans=0; cout<<ans<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int n, i, j; long long a[105], k; vector<vector<long long> > b, id; vector<vector<long long> > mul(vector<vector<long long> > x, vector<vector<long long> > y) { vector<vector<long long> > z; for (int ii = 0; ii < x.size(); ii++) { vector<long long> tmp; for (int jj = 0; jj < x.size(); jj++) tmp.push_back(0); z.push_back(tmp); } for (int ii = 0; ii < x.size(); ii++) { for (int jj = 0; jj < x.size(); jj++) { for (int kk = 0; kk < x.size(); kk++) z[ii][jj] = (z[ii][jj] + x[ii][kk] * y[kk][jj]) % MOD; } } return z; } vector<vector<long long> > pow(vector<vector<long long> > u, long long lv) { if (lv == 0) return id; vector<vector<long long> > now = pow(u, lv / 2); now = mul(now, now); if (lv & 1) now = mul(now, u); return now; } int main() { scanf("%d%lld", &n, &k); for (i = 0; i < n; i++) scanf("%lld", &a[i]); for (i = 0; i < n; i++) { vector<long long> tmp1; for (j = 0; j < n; j++) tmp1.push_back(0); b.push_back(tmp1); } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (__builtin_popcountll(a[i] ^ a[j]) % 3 == 0) b[j][i] = 1; } } for (i = 0; i < n; i++) { vector<long long> tmp1; for (j = 0; j < n; j++) { if (i == j) tmp1.push_back(1); else tmp1.push_back(0); } id.push_back(tmp1); } b = pow(b, k - 1); long long res = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) res = (res + b[i][j]) % MOD; } printf("%lld\n", res); }
5
#include <bits/stdc++.h> using namespace std; int n, Q, T, P, used[210][210], cnt[210]; struct dot { int x, y; } a[210]; double mat[14 + 2][210][210], cur[210][2], tmp[210][2], ans; vector<vector<int> > l; vector<int> curr; dot operator-(dot a, dot b) { a.x -= b.x, a.y -= b.y; return a; } long long operator*(dot a, dot b) { return 1ll * a.x * b.y - 1ll * a.y * b.x; } template <typename T> void chkmax(T &x, T y) { x = max(x, y); } template <typename T> void chkmin(T &x, T y) { x = min(x, y); } template <typename T> void read(T &x) { x = 0; int f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } x *= f; } bool on_line(dot a, dot x, dot y) { return (((a - x) * (a - y)) == 0); } void work() { unsigned si = curr.size(); for (unsigned i = 0; i < si; ++i) for (unsigned j = 0; j < i; ++j) used[curr[i]][curr[j]] = used[curr[j]][curr[i]] = 1; } int main() { read(n); for (int i = 1; i <= n; ++i) read(a[i].x), read(a[i].y); for (int i = 1; i <= n; ++i) used[i][i] = 1; for (int i = 1; i <= n; ++i) for (int j = 1; j < i; ++j) { if (used[i][j]) continue; curr.clear(); curr.push_back(i); curr.push_back(j); for (int t = 1; t <= n; ++t) { if (t == i || t == j) continue; if (on_line(a[t], a[i], a[j])) curr.push_back(t); } work(); l.push_back(curr); for (unsigned t = 0, si = curr.size(); t < si; ++t) ++cnt[curr[t]]; } for (unsigned i = 0; i < l.size(); ++i) { int tot = l[i].size(); for (unsigned j = 0; j < (unsigned)tot; ++j) for (unsigned t = 0; t < (unsigned)tot; ++t) { int u = l[i][j], v = l[i][t]; mat[0][u][v] += 1.0 / (cnt[u] * tot); } } for (int p = 1; p <= 14; ++p) for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) for (int t = 1; t <= n; ++t) mat[p][i][j] += mat[p - 1][i][t] * mat[p - 1][t][j]; read(Q); while (Q--) { ans = 0; read(T), read(P); memset(cur, 0, sizeof(cur)); cur[T][1] = 1; for (int p = 0; p <= 14; ++p) if ((P - 1) & (1 << p)) { memset(tmp, 0, sizeof(tmp)); for (int i = 1; i <= n; ++i) for (int j = 1; j <= 1; ++j) for (int t = 1; t <= n; ++t) tmp[i][j] += mat[p][i][t] * cur[t][j]; for (int i = 1; i <= n; ++i) for (int j = 1; j <= 1; ++j) cur[i][j] = tmp[i][j]; } for (unsigned i = 0; i < l.size(); ++i) { double tot = 0; for (unsigned j = 0; j < l[i].size(); ++j) tot += cur[l[i][j]][1]; chkmax(ans, tot / l[i].size()); } printf("%.10lf\n", ans); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int p[] = {0, 1, 2, 3, 4, 5, 6, 7, 8}; int x[9], y[9]; bool dp(int a, int b, int c) { if ((x[p[a]] - x[p[b]]) * (x[p[c]] - x[p[b]]) + (y[p[a]] - y[p[b]]) * (y[p[c]] - y[p[b]]) == 0) return 1; else return 0; } int dis(int a, int b) { return ((x[p[a]] - x[p[b]]) * (x[p[a]] - x[p[b]])) + ((y[p[a]] - y[p[b]]) * (y[p[a]] - y[p[b]])); } int main() { for (int i = 1; i <= 8; i++) cin >> x[i] >> y[i]; do { if (dp(1, 2, 3) && dp(2, 3, 4) && dp(3, 4, 1) && dis(1, 2) == dis(2, 3) && dis(3, 4) && dp(5, 6, 7) && dp(6, 7, 8) && dp(7, 8, 5)) { cout << "YES\n"; for (int j = 1; j <= 4; j++) cout << p[j] << " "; cout << endl; for (int j = 5; j <= 8; j++) cout << p[j] << " "; cout << endl; return 0; } } while (next_permutation(p + 1, p + 9)); cout << "NO" << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { long long int n, k; cin >> n >> k; long long int i; map<long long int, long long int> mp; map<long long int, long long int>::iterator it; long long int a[200001]; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (i = 0; i < n; i++) { mp[a[i]] = i + 1; } if (k == 0) { if (a[0] > 1) { cout << "1"; return 0; } else { cout << "-1"; return 0; } } for (it = mp.begin(); it != mp.end(); it++) { if (it->second == k) { cout << it->first; return 0; } } cout << -1; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int a,b,c; cin >>a>>b>>c; cout<<min(a+b,min(b+c,a+c))<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll INF = 1e9; class RangeMin { private: vector<ll> seg; int n; int recf(int a, ll v) const { while (a < n) a = 2 * a + (seg[2 * a] > v); return a - n; } public: RangeMin(const vector<ll>& v) : n(v.size()), seg(2 * v.size()) { for (int i = 0; i < n; ++i) seg[i + n] = v[i]; for (int i = n - 1; i > 0; --i) seg[i] = min(seg[2 * i], seg[2 * i + 1]); } int search(int a, ll v) const { int b = 2 * n, r = 2 * n; for (a += n; a < b; a = (a + 1) / 2, b /= 2) { if ((a & 1) && seg[a] <= v) return recf(a, v); if ((b & 1) && seg[b - 1] <= v) r = b - 1; } return recf(r, v); } ll get(int a, int b) const { ll res = 2 * INF; for (a += n, b += n + 1; a < b; a = (a + 1) / 2, b /= 2) { if (a & 1) res = min(res, seg[a]); if (b & 1) res = min(res, seg[b - 1]); } return res; } void set(int i, ll v) { seg[i + n] = v; for (i += n; i > 1; i /= 2) seg[i / 2] = min(seg[i], seg[i ^ 1]); } }; class HLD { private: vector<int> par, siz, cmp, ind, rev; int dfs1(int i, vector<vector<int>>& g) { for (auto& t : g[i]) if (t != par[i]) { par[t] = i; siz[i] += dfs1(t, g); if (siz[t] > siz[g[i][0]] || g[i][0] == par[i]) swap(t, g[i][0]); } return siz[i]; } void dfs2(int i, int& x, const vector<vector<int>>& g) { ind[i] = x; ++x; for (auto t : g[i]) if (t != par[i]) { cmp[t] = (x == ind[i] + 1 ? cmp[i] : t); dfs2(t, x, g); } } public: HLD(vector<vector<int>> g, int r = 0) : par(g.size(), -1), siz(g.size(), 1), cmp(g.size(), r), ind(g.size()), rev(g.size()) { dfs1(r, g); int x = 0; dfs2(r, x, g); for (int i = 0; i < g.size(); ++i) rev[ind[i]] = i; } pair<int, int> subtree(int i) const { return {ind[i], ind[i] + siz[i] - 1}; } int lca(int a, int b) const { for (;; b = par[cmp[b]]) { if (ind[b] < ind[a]) swap(a, b); if (ind[cmp[b]] <= ind[a]) return a; } } int oneDown(int a, int b) const { while (true) { if (b == cmp[b]) { if (par[b] == a) return b; else b = par[b]; } else { if (ind[cmp[b]] <= ind[a]) return rev[ind[a] + 1]; else b = cmp[b]; } } } int revInd(int i) const { return rev[i]; } }; const int N = 1e5; vector<pair<int, ll>> conns[N]; ll dep[N]; void dfs(int i, int p, ll& cur, vector<ll>& times) { for (int ti = (int)conns[i].size() - 1; ti >= 0; --ti) { auto pr = conns[i][ti]; int t = pr.first; if (t == p) continue; dep[t] = dep[i] + pr.second; dfs(t, i, cur, times); } times[i] = cur; ++cur; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<vector<int>> g(n); for (int i = 0; i < n - 1; ++i) { int a, b; ll d; cin >> a >> b >> d; --a; --b; g[a].push_back(b); g[b].push_back(a); conns[a].emplace_back(b, d); conns[b].emplace_back(a, d); } HLD hld(g); ll cur = INF; vector<ll> tmp(n, -1); dfs(0, -1, cur, tmp); vector<ll> times(n); for (int i = 0; i < n; ++i) times[i] = tmp[hld.revInd(i)]; RangeMin rm(times); vector<pair<ll, ll>> evs; for (int j = 0; j < m; ++j) { int tar; ll tim; cin >> tar >> tim; --tar; int r = 0; while (r != tar) { auto st = hld.subtree(r); ll prev_time = -rm.get(st.first + 1, st.second); int prev_ind = rm.search(st.first + 1, -prev_time); int prev_tar = hld.revInd(prev_ind); int next_r = hld.lca(tar, prev_tar); if (next_r == tar) break; else if (next_r != prev_tar) { ll arr_time = prev_time + dep[next_r]; evs.emplace_back(prev_time + dep[next_r] + 1, tim + dep[next_r]); r = hld.oneDown(next_r, tar); } else { r = next_r; } } int i = hld.subtree(tar).first; rm.set(i, -tim); } sort(evs.begin(), evs.end()); int tot = 0; priority_queue<ll> que, hangs; bool fail = 0; int ind = 0; ll t = 1; while (true) { while (ind < evs.size() && evs[ind].first <= t) { que.push(-evs[ind].second); ++ind; } if (que.empty()) { if (ind == evs.size()) { break; } else { t = evs[ind].first; } } else { if (-que.top() >= t) { while (!hangs.empty() && -hangs.top() < t) { hangs.pop(); } hangs.push(que.top()); que.pop(); ++tot; ++t; } else { --t; fail = 1; break; } } } if (fail) cout << t << ' ' << tot - hangs.size() << '\n'; else cout << -1 << ' ' << evs.size() << '\n'; }
5
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int a[3]; cin >> a[0] >> a[1] >> a[2]; sort(a, a + 3); int ans = 0; if (a[2]) { --a[2]; ++ans; } if (a[1]) { --a[1]; ++ans; } if (a[0]) { --a[0]; ++ans; } if (a[2] && a[1]) { --a[2]; --a[1]; ++ans; } if (a[2] && a[0]) { --a[2]; --a[0]; ++ans; } if (a[1] && a[0]) { --a[1]; --a[0]; ++ans; } if (a[2] && a[1] && a[0]) { ++ans; } cout << ans << endl; } }
1
#include <bits/stdc++.h> using namespace std; double eps = 1e-9; long long int n; long double arr[1000000]; long double solve(long double mid) { double maxi = 0.0, sum = 0.0; for (long long int i = 0; i < n; i++) { sum += (arr[i] - mid); if (sum < 0) { sum = 0; } maxi = max(abs(sum), maxi); } sum = 0.0; for (long long int i = 0; i < n; i++) { sum += (arr[i] - mid); if (sum > 0) { sum = 0; } maxi = max(abs(sum), maxi); } return maxi; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (long long int i = 0; i < n; i++) { cin >> arr[i]; } long double lo = -10000.0, hi = 10000.0, mid1, mid2; for (long long int i = 1; i <= 100; i++) { mid1 = lo + (hi - lo) / 3; mid2 = hi - (hi - lo) / 3; if (solve(mid1) >= solve(mid2)) { lo = mid1; } else { hi = mid2; } } cout << fixed << setprecision(10) << solve(lo) << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long int N = 200010; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, m; cin >> n >> m; vector<long long int> adj[n + 1]; for (long long int i = 0; i < m; ++i) { long long int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } for (long long int i = 1; i <= n; ++i) { sort(adj[i].begin(), adj[i].end()); } vector<long long int> ans; set<long long int> left; for (long long int i = 1; i <= n; ++i) { left.insert(i); } while (!left.empty()) { long long int v = *left.begin(); left.erase(left.begin()); long long int count = 0; queue<long long int> buffer; buffer.push(v); while (!buffer.empty()) { ++count; v = buffer.front(); buffer.pop(); set<long long int> newcandidates; for (auto u : adj[v]) { if (left.find(u) != left.end()) { newcandidates.insert(u); left.erase(u); } } for (auto u : left) { buffer.push(u); } left = newcandidates; } ans.push_back(count); } sort(ans.begin(), ans.end()); cout << ans.size() << endl; for (long long int i = 0; i < ans.size(); ++i) { cout << ans[i] << " "; } return (0); }
5
#include <iostream> #include <iomanip> #include <cmath> #include <cstdio> #include <algorithm> #include <vector> #include <memory> #include <cstring> #include <cassert> #include <numeric> #include <sstream> #include <map> #include <set> #include <queue> #include <stack> #include <cctype> #include <unordered_map> #include <unordered_set> using namespace std; #define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++) #define REP(i, n) REP2(i, 0, n) #define ALL(S) (S).begin(), (S).end() typedef long long ll; int main(){ ios::sync_with_stdio(false); int n; while (cin >> n && n){ vector<string> vs(n); REP(i, n) cin >> vs[i]; REP2(i, 1, n){ size_t j = 0; while (!isalpha(vs[i][j])){ vs[i][j++] = ' '; } j--; vs[i][j] = '+'; size_t k = i - 1; while (vs[k][j] == ' '){ vs[k][j] = '|'; k--; } } REP(i, n){ cout << vs[i] << endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; int32_t main() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int a, b; cin >> a >> b; int s = a + b; int sum = 0, k = 1; while (sum + k <= s) { sum += k; k++; } --k; vector<int> A, B; for (int i = k; i >= 1; i--) { if (a >= i) { a -= i; A.push_back(i); } else { b -= i; B.push_back(i); } } cout << A.size() << "\n"; for (int i : A) { cout << i << " "; } cout << "\n" << B.size() << "\n"; for (int i : B) { cout << i << " "; } }
1
#include <bits/stdc++.h> using namespace std; int n; long long a[123456], f[123456], ans, tmp; void init() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%I64d", &tmp); a[tmp]++; } } void solve() { f[1] = a[1]; ans = f[1]; for (int i = 2; i < 123456; i++) { f[i] = max(f[i - 1], f[i - 2] + a[i] * i); ans = max(f[i], ans); } printf("%I64d\n", ans); } int main() { init(); solve(); }
3
#include<stdio.h> int main(){ int x; scanf("%d",&x); int r = 0; while(x > 0){ ++r; x-=r; } printf("%d\n",r); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int a[m]; int eq, diff; eq = n / m; diff = m - (n - (m * eq)); for (int i = 0; i < m; i++) { if (i < diff) { a[i] = eq; } else { a[i] = eq + 1; } } for (int i = 0; i < m; i++) { cout << a[i] << " "; } cout << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const double eps = 1e-10; void use_input() { freopen("input.txt", "r", stdin); } void use_output() { freopen("output.txt", "w", stdout); } void use_files() { use_input(); use_output(); } long long pw(long long n) { long long s = 1; for (long long i = 0; i < n; i++) s *= 10; return s; } long long gs(long long p) { return (long long)(p * (p - 1) * (p - 2)) / 6; } long long gsd(long long a, long long b) { while (a > 0 && b > 0) if (a > b) a %= b; else b %= a; return a + b; } double pol(long long x1, long long y1, long long x2, long long y2) { double dx = (x2 - x1); double dy = (y2 - y1); if (dy < 0) { dx *= -1; dy *= -1; } if (dx == 0) return 90; return (double)dy / dx; } bool isOperator(char c) { return (c == '+' || c == '-' || c == '*' || c == '/' || c == '%' || c == '^'); } long long priority(char c) { if (c < 0) return 8; return (c == '+' || c == '-') ? 1 : ((c == '*' || c == '/' || c == '%' || c == '^') ? 2 : -1); } bool whiteSpace(char c) { return (c == ' ' || c == ' '); } bool isScobe(char c) { return (c == '(' || c == ')'); } bool isOperand(char c) { return (!whiteSpace(c) && !isOperator(c) && !isScobe(c)); } bool isUnary(char c) { return (c == '+' || c == '-'); } bool right_assoc(char c) { return (c == '+' || c == '-' || c == '*' || c == '%' || c == '^'); } void doOperation(vector<long long> &operands, char op) { if (op < 0) { long long l = operands.back(); operands.pop_back(); switch (-op) { case '+': operands.push_back(l); break; case '-': operands.push_back(-l); break; } } else { long long r = operands.back(); operands.pop_back(); long long l = operands.back(); operands.pop_back(); switch (op) { case '+': operands.push_back(l + r); break; case '-': operands.push_back(l - r); break; case '*': operands.push_back(l * r); break; case '/': operands.push_back(l / r); break; case '%': operands.push_back(l - r * l / r); break; case '^': operands.push_back(pow(l, r)); break; } } } long long calc(string &s, map<string, double> ch = {}) { vector<long long> operands; vector<char> operators; bool unary = true, found; for (long long i = 0; i < s.size(); i++) if (!whiteSpace(s[i])) { if (s[i] == '(') { operators.push_back('('); unary = true; } else if (s[i] == ')') { while (operators.back() != '(') doOperation(operands, operators.back()), operators.pop_back(); operators.pop_back(); unary = false; } else if (isOperator(s[i])) { char current = s[i]; if (unary && isUnary(current)) current *= -1; while ( !operators.empty() && (((!right_assoc(current) && priority(operators.back()) >= priority(current) && current > 0) || (right_assoc(current) && priority(operators.back()) > priority(current) && current > 0)) || (current < 0 && priority(operators.back()) > priority(current)))) doOperation(operands, operators.back()), operators.pop_back(); operators.push_back(current); unary = false; } else { string operand; while (i < s.length() && isOperand(s[i])) operand += s[i++]; --i; operands.push_back(atoi(operand.c_str())); unary = false; } } while (!operators.empty()) doOperation(operands, operators.back()), operators.pop_back(); return operands.back(); } int main() { string s, mq, s1, s2, ss, s3; long long mx = 0, t; cin >> s; vector<long long> vc; vc.push_back(-1); for (long long i = 0; i < s.size(); i++) if (s[i] == '*') vc.push_back(i); vc.push_back(s.size()); long long q, w; for (long long i = 0; i < vc.size(); i++) for (long long j = i + 1; j < vc.size(); j++) { q = vc[i]; w = vc[j]; if (q == -1) s1 = ""; else s1 = s.substr(0, q); s2 = s.substr(q + 1, w - q - 1); if (w == s.size()) s3 = ""; else s3 = s.substr(w + 1, s.size() - w); ss = '(' + s2 + ')'; if (s1 != "") ss = s1 + '*' + ss; if (s3 != "") ss = ss + '*' + s3; t = calc(ss); if (t > mx) { mq = ss; mx = t; } } cout << mx; }
5
#include <bits/stdc++.h> long long max(long long a, long long b) { return (a < b) ? b : a; } long long min(long long a, long long b) { return (a > b) ? b : a; } using namespace std; long long x[300], y[300]; long long ab(long long r) { return (r < 0) ? -r : r; } int main() { long long x, ans = LLONG_MAX / 2, bb, aa; cin >> x; map<long long, long long> pr; while (x > 1) { long long br = 1; for (long long i = 2; i * i <= x; i++) { if (x % i == 0) { pr[i]++; x /= i; br = 0; break; } } if (br) { pr[x]++; x = 1; } } for (long long i = 0; i < 1 << pr.size(); i++) { long long a = 1, b = 1; long long te = i; for (auto&& j : pr) { if (te % 2 == 1) { for (long long v = 0; v < j.second; v++) a *= j.first; } else { for (long long v = 0; v < j.second; v++) b *= j.first; } te /= 2; } if (ans > max(a, b)) ans = max(a, b), aa = a, bb = b; } cout << aa << " " << bb << endl; }
3
#include <bits/stdc++.h> using namespace std; long long n, i, x, y, s, j; long long A[100001], M[100001], R[100001]; vector<long long> V[100001]; long long solve(long long i) { long long &res = M[i], j, x, m, &b = R[i]; vector<long long> X, Y; if (res == -1) { res = 0; for (j = m = 0; j < V[i].size(); j++) { x = V[i][j]; if (M[x] == -1) { X.push_back(solve(x)); Y.push_back(R[x]); m++; } } sort(X.begin(), X.end(), greater<long long>()); b = A[i]; if (i != s) b--; for (j = 0; j < m && b; j++, b--) res += X[j] + 2; for (j = 0; j < m && b; j++) { x = min(b, Y[j]); b -= x; res += 2 * x; } } return res; } int main() { scanf("%I64d", &n); for (i = 1; i <= n; i++) scanf("%d", &A[i]); for (i = 0; i + 1 < n; i++) { scanf("%I64d%I64d", &x, &y); V[x].push_back(y); V[y].push_back(x); } scanf("%I64d", &s); memset(M, -1, sizeof M); printf("%I64d", solve(s)); return 0; }
5
#include <bits/stdc++.h> using namespace std; inline long long Max(long long a, long long b) { return a > b ? a : b; } inline long long Min(long long a, long long b) { return a < b ? a : b; } inline long long read() { long long x = 0; int ch = getchar(), f = 1; while (!isdigit(ch) && (ch != '-') && (ch != EOF)) ch = getchar(); if (ch == '-') f = -1, ch = getchar(); while (isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar(); return x * f; } inline void write(long long x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) write(x / 10), putchar(x % 10 + '0'); else putchar(x + '0'); } void scan(int &x) { bool neg = false; register int c; x = 0; c = getchar(); if (c == '-') { neg = true; c = getchar(); } for (; (c > 47 && c < 58); c = getchar()) x = (x << 1) + (x << 3) + c - 48; if (neg) x *= -1; } int mx = ((1 << 22) - 1); bool exist[(1 << 22)]; bool vis[(1 << 22)]; int n, m; void dfs(int i) { vis[i] = true; for (int j = 0; j < n; j++) { if (((i >> j) & 1) == 1 && !vis[i ^ (1 << j)]) { dfs(i ^ (1 << j)); } } if (exist[i] == 1 && !vis[i ^ mx]) { dfs(i ^ mx); } } int main() { int t = 1; while (t--) { cin >> n >> m; int a[m]; mx = (1 << n) - 1; for (int i = 0; i < m; i++) { cin >> a[i]; exist[a[i]] = true; } long long cnt = 0; for (int i = 0; i <= mx; i++) { if (exist[i] && !vis[i]) { dfs(i); cnt++; } } cout << cnt << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; mt19937 Rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); template <typename T> inline void chkmax(T &x, T y) { if (x < y) x = y; } template <typename T> inline void chkmin(T &x, T y) { if (x > y) x = y; } inline int read() { int x = 0; char c = getchar(); while (c < 48) c = getchar(); while (c > 47) x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); return x; } const int maxn = 1e5 + 10; long long dp[21][maxn]; int n, m, a[maxn]; long long nowAns; int nowL, nowR, cnt[maxn]; void add(int x) { nowAns += cnt[x]++; } void del(int x) { nowAns -= --cnt[x]; } long long query(int l, int r) { while (nowL < l) del(a[nowL++]); while (nowL > l) add(a[--nowL]); while (nowR < r) add(a[++nowR]); while (nowR > r) del(a[nowR--]); return nowAns; } long long *Lst, *Ans; void divide(int l, int r, int ql, int qr) { if (l > r) return; int mid = (l + r) >> 1, pos; Ans[mid] = 1e18; for (int i = (ql), iend = (min(qr, mid - 1)); i <= iend; ++i) { long long v = Lst[i] + query(i + 1, mid); if (Ans[mid] > v) { Ans[mid] = v, pos = i; } } divide(l, mid - 1, ql, pos); divide(mid + 1, r, pos, qr); } int main() { n = read(), m = read(); for (int i = (1), iend = (n); i <= iend; ++i) a[i] = read(); for (int i = (1), iend = (n); i <= iend; ++i) { dp[1][i] = dp[1][i - 1] + cnt[a[i]]++; } nowL = 1, nowR = 0; memset(cnt, 0, sizeof cnt); for (int i = (2), iend = (m); i <= iend; ++i) { Lst = dp[i - 1], Ans = dp[i]; divide(i, n, 1, n); } printf("%I64d", dp[m][n]); return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; long long ans1, ans2, a[N], n, b[N], now = 1, p = 1; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) { if (a[i] < 0) p *= -1; b[i] = p; if (p > 0) ans1++; else ans2++; } int z = ans1, f = ans2; for (int i = 1; i < n; i++) { if (now * b[i] > 0) z--; else f--; if (a[i] < 0) swap(z, f), now *= -1; ans1 += z; ans2 += f; } cout << ans2 << " " << ans1 << endl; }
2
#include <bits/stdc++.h> using namespace std; struct pointer { int x; int y; } p[101]; int main() { int n, k; while (cin >> n >> k) { for (int i = 0; i < n; ++i) { cin >> p[i].x >> p[i].y; } double dis = 0; for (int i = 1; i < n; ++i) { dis += sqrt((double)(p[i].x - p[i - 1].x) * (p[i].x - p[i - 1].x) + (p[i].y - p[i - 1].y) * (p[i].y - p[i - 1].y)); } cout << fixed << setprecision(9) << dis * k / 50 << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; while (t--) { int n, k; cin >> n >> k; string s; cin >> s; if (s.substr(0) < s.substr(k)) { int i = k - 1; while (s[i] == '9') { s[i] = '0'; i--; } s[i]++; } cout << n << "\n"; for (int i = 0; i < n; i++) { cout << s[i % k]; } cout << "\n"; } }
1
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++) int main(){ ll D; cin >> D; vector<ll> c(26), t(D), last(26, -1); vector< vector<ll> > s(365, vector<ll> (26)); rep(i, 0, 26) cin >> c[i]; rep(i, 0, D){ rep(j, 0, 26) cin >> s[i][j]; } rep(i, 0, D){ cin >> t[i]; t[i]--; } ll M; cin >> M; vector<ll> d(M), q(M), rec(26, 0); rep(i, 0, M){ cin >> d[i] >> q[i]; d[i]--; q[i]--; } ll res = 0; rep(d, 0, D){ res += s[d][t[d]]; last[t[d]] = d; rep(j, 0, 26) { res -= c[j] * (d - last[j]); rec[j] += c[j] * (d - last[j]); } } // t[d[m]] := 0 - 25 rep(m, 0, M){ res -= s[d[m]][t[d[m]]]; res += s[d[m]][q[m]]; res += rec[t[d[m]]]; res += rec[q[m]]; rec[t[d[m]]] = 0; rec[q[m]] = 0; ll lst = -1; rep(i, 0, D){ if(i != d[m] && t[i] == t[d[m]]){ lst = i; } res -= c[t[d[m]]] * (i - lst); rec[t[d[m]]] += c[t[d[m]]] * (i - lst); } lst = -1; rep(i, 0, D){ if(i == d[m] || t[i] == q[m]){ lst = i; } res -= c[q[m]] * (i - lst); rec[q[m]] += c[q[m]] * (i - lst); } t[d[m]] = q[m]; cout << res << endl; } return 0; }
0
#include <iostream> using namespace std; int main() { int d; while(cin >> d) { int ans = 0; for(int i = 0; i < 600; i += d) { ans += i * i * d; } cout << ans << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; double a, v, l, d, w, ans, L, t1, t2, v_2; int main() { scanf("%lf%lf%lf%lf%lf", &a, &v, &l, &d, &w); L = l - d; double A = w * w; if (v <= w || A >= 2 * a * d) { if (2 * a * l >= v * v) { ans = v / a; ans += (l - v * v / 2 / a) / v; } else ans = sqrt(2 * l / a); } else { double V = sqrt((2 * a * d + w * w) / 2); if (V >= v) { t1 = v / a + (v - w) / a; t1 += (d - (2 * v * v - w * w) / 2 / a) / v; } else { t1 = V / a + (V - w) / a; } if (w * w + 2 * a * L <= v * v) { v_2 = sqrt(w * w + 2 * a * L); t2 = (v_2 - w) / a; } else { t2 = (v - w) / a; t2 += (L - (v * v - w * w) / (2 * a)) / v; } ans = t1 + t2; } printf("%.12lf", ans); return 0; }
4
#include<iostream> using namespace std; int n, k; long long int T[100000]; long long sum; long long l; int check(long long p) { int m = 0; for (int i = 0; i < k; i++) { long long s = 0; while (s + T[m] <= p) { s += T[m]; m++; if (m >= n) { return n; } } } return m; } int main() { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> T[i]; sum += T[i]; } l = sum / k; while (check(l) != n) { l++; } cout << l << endl; return 0; }
0
#include <iostream> #include <iomanip> using namespace std; int main() { double r1, r2; cin >> r1 >> r2; cout << fixed << setprecision(7) << r1 * r2 / (r1 + r2) << endl; }
0
#include <bits/stdc++.h> #define maxn 5010 using namespace std; typedef long long LL; #define G c = getchar() inline LL read() { LL x = 0, f = 1; char G; for (; c > 57 || c < 48; G) if (c == '-') f = -1; for (; c > 47 && c < 58; G) x = x * 10 + c - 48; return x * f; } struct Poi { LL x, y; int i; }; LL dist(Poi a, Poi b) { return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); } int main() { vector<Poi> V; int n = read(); for (int i = 0; i < n; i++) { int u = read(), v = read(); V.push_back({u, v, i + 1}); } auto cur = V[0]; V.erase(V.begin()); printf("%d", cur.i); for (int _ = 1; _ < n; _++) { LL ans = 0, tmp; decltype(V.begin()) best; for (auto it = V.begin(); it != V.end(); it++) { if ((tmp = dist(cur, *it)) > ans) { ans = tmp; best = it; } } cur = *best; V.erase(best); printf(" %d", cur.i); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, e, f; while (cin >> a >> b >> c >> d >> e >> f) { cout << (a + b + c) * (a + b + c) - a * a - c * c - e * e << endl; } }
1
#include <bits/stdc++.h> using namespace std; int leapYear(long long x) { if (x % 400 == 0) return 1; if (x % 4 == 0 && x % 100 != 0) return 1; return 0; } int main() { long long y; cin >> y; long long days = 0; for (long long x = y;; x++) { if (leapYear(x)) days = (days + 366) % 7; else days = (days + 365) % 7; if (x != y && days == 0 && leapYear(x + 1) == leapYear(y)) { cout << x + 1 << endl; break; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int n, m, temp; cin >> n >> m; vector<long long int> vec(m, 0); for (long long int i = 0; i < n; i++) { cin >> temp; vec[--temp]++; } long long int sum = 0; for (long long int i = 0; i < m; i++) { sum += (vec[i] * (n - vec[i])); } cout << sum / 2 << endl; }
2
#include <bits/stdc++.h> #define ALL(v) v.begin(), v.end() #define MOD 1000000007 #define INF 1000000007 #define rep(i, n) for(int i = 0; i < (int)(n); i++) #define req(i, j, n) for(int i = j; i <= (int)(n); i++) using namespace std; typedef long long ll; typedef pair<int, int> P; int main() { int n; cin>>n; int a[n+1]; for(int i=1;i<=n;i++){ int p; cin>>p; a[i]=p; } int ans=0; for(int i=2;i<=n;i++){ if(a[i]>a[i-1]){ ans++; } } cout<<ans<<endl; return 0; }
0