solution
stringlengths
52
181k
difficulty
int64
0
6
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <map> #include <vector> #include <queue> using namespace std; const int N = 1e7 + 7, M = 1e7 + 7, mod = 1e9 + 7, INF = 0x3f3f3f3f; typedef long long ll; typedef int itn; typedef pair<int, int> PII; int n, m, k, t; int dist[M]; typedef struct { itn y, w; }edge; vector<edge>G[M]; int pos(int x, int w) { return x * 51 + w; } void __add(int x, int y, int w) { G[x].push_back((edge){y, w}); } void add(itn x, int y, int w) { __add(pos(x, 0), pos(y, w), 0); for(int i = 1; i <= 50; ++ i) { __add(pos(x, i), pos(y, 0), (i + w) * (i + w)); } } void dijkstra(int S) { memset(dist, 0x3f, sizeof dist); dist[S] = 0; priority_queue<PII, vector<PII>, greater<PII> >q; q.push({0, S}); while(q.size()) { itn x = q.top().second; int d = q.top().first; q.pop(); if(d != dist[x]) continue; for(auto it : G[x]) { itn y = it.y, w = it.w; if(dist[y] > d + w) { dist[y] = d + w; q.push({dist[y], y}); } } } } int main() { //init(); scanf("%d%d", &n, &m); for(int i = 1; i <= m; ++ i) { int x, y, w; scanf("%d%d%d", &x, &y, &w); add(x, y, w); add(y, x, w); } dijkstra(pos(1, 0)); for(int i = 1; i <= n; ++ i) { if(dist[pos(i, 0)] == INF) dist[pos(i, 0)] = -1; printf("%d ", dist[pos(i, 0)]); } puts(""); return 0; }
5
#include<bits/stdc++.h> using namespace std; int main(){ string a,b,c; cin>>a>>b>>c; if(a[a.size()-1]==b[0] and b[b.size()-1]==c[0]) cout<<"YES"<<endl; else cout<<"NO"<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { string x, y; char temp; cin >> x >> y; sort(y.begin(), y.end(), greater<int>()); for (int i = 0, j = 0; i < y.length() && j < x.length(); i++, j++) { if (y[i] > x[j]) { temp = y[i]; y[i] = x[j]; x[j] = temp; } else i--; } cout << x << "\n"; }
2
#include <bits/stdc++.h> using namespace std; int n, a[100000], sum; char out[100000]; int main() { int i; cin >> n; for (i = 0; i < n; i++) cin >> a[i]; out[n - 1] = '-'; sum -= a[n - 1]; for (i = n - 2; i >= 0; i--) { if (sum > 0) { sum -= a[i]; out[i] = '-'; } else { sum += a[i]; out[i] = '+'; } } if (sum >= 0) for (i = 0; i < n; i++) { cout << out[i]; } else for (i = 0; i < n; i++) { if (out[i] == '-') cout << "+"; else cout << "-"; } cout << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; long long n, m, k, maxi, mini; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); ; cin >> n >> m >> k; if (k > n + m - 2) return cout << "-1", 0; k++; if (k > max(n, m)) return cout << min(n, m) / (k - max(n, m) + 1) << endl, 0; cout << max((max(n, m) / k) * min(n, m), (min(n, m) / k) * max(n, m)) << endl; return 0; }
1
#include <bits/stdc++.h> using int16 = short int; const int N = 10005; int16 lcp[N][N]; int dp[N]; bool is_greater(const std::string& s, int x, int y) { if (lcp[x][y] == static_cast<int>(s.size()) - x) { return false; } return s[x + lcp[x][y]] > s[y + lcp[x][y]]; } int get_score(const std::string& s, int x, int y) { if (is_greater(s, x, y)) { return dp[y] + static_cast<int>(s.size()) - x - lcp[x][y]; } return 0; } int main() { int t; std::cin >> t; while (t--) { int n; std::cin >> n; std::string s; std::cin >> s; for (int i = 0; i <= n; ++i) { for (int j = 0; j <= n; ++j) lcp[i][j] = 0; } for (int i = n - 1; ~i; --i) { for (int j = n - 1; ~j; --j) { if (i == j) { lcp[i][j] = n - i; } else if (s[i] != s[j]) { lcp[i][j] = 0; } else { lcp[i][j] = lcp[i + 1][j + 1] + 1; } } } int ans = n; dp[0] = n; for (int i = 1; i < n; ++i) { dp[i] = n - i; for (int j = 0; j < i; ++j) { dp[i] = std::max(dp[i], get_score(s, i, j)); } ans = std::max(ans, dp[i]); } std::cout << ans << '\n'; } return 0; }
5
#include <bits/stdc++.h> using namespace std; vector<vector<int>> a, pref, b; int n, m; void build(vector<vector<int>> &now) { pref.assign(n + 2, vector<int>(m + 2, 0)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { pref[i][j] = pref[i][j - 1] + pref[i - 1][j] - pref[i - 1][j - 1] + now[i][j]; } } } bool ok(int x, int y) { return (x >= 1 && y >= 1 && x <= n && y <= m); } int get(int l1, int r1, int l2, int r2) { l1 = max(l1, 1); l2 = min(l2, n); r1 = max(r1, 1); r2 = min(r2, m); return pref[l2][r2] - pref[l1 - 1][r2] - pref[l2][r1 - 1] + pref[l1 - 1][r1 - 1]; } bool check(int mid) { build(a); b.assign(n + 2, vector<int>(m + 2, 0)); int need = (2 * mid + 1) * (2 * mid + 1); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (get(i - mid, j - mid, i + mid, j + mid) == need) b[i][j] = 1; } } build(b); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { bool fire = (get(i - mid, j - mid, i + mid, j + mid) > 0); if (fire && a[i][j] == 0) return 0; if (!fire && a[i][j] == 1) return 0; } } return 1; } int main() { ios_base ::sync_with_stdio(0); cin.tie(0); cin >> n >> m; a.assign(n + 2, vector<int>(m + 2, 0)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { char x; cin >> x; if (x == 'X') a[i][j] = 1; } } int l = 0, r = n + 10, mid; while (r - l > 1) { mid = (l + r) / 2; if (check(mid)) l = mid; else r = mid; } check(l); cout << l << endl; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (b[i][j] == 1) cout << 'X'; else cout << '.'; } cout << "\n"; } return 0; }
3
#include <bits/stdc++.h> using namespace std; inline void boost() { ios_base::sync_with_stdio(); cin.tie(0); cout.tie(0); } const long long maxn = 1e5 + 123; const long long inf = 1e9 + 123; const long long mod = 1e9 + 7; const double pi = acos(-1); int main() { boost(); long long n; cin >> n; long long cntr = (n * (n + 1) / 2 * 6) + 1; cout << cntr; return 0; }
4
#include <bits/stdc++.h> using namespace std; long long n,k; int main(){ scanf("%lld%lld",&n,&k); printf("%lld\n",((n+2-k)*(k*n-k*k+k)+(n+2-k)*(n+1-k)*(n+1-2*k)/2-(n-k+1)*(n-k+2)*(2*n-2*k+3)/6+(n+2-k))%1000000007); return 0; }
0
#include <bits/stdc++.h> using namespace std; long long n, k; long long a[(long long)(2e5 + 10)], c[(long long)(2e5 + 10)]; long long solve() { cin >> n >> k; for (long long i = 0; i < n; i++) cin >> a[i]; for (long long i = 1; i <= k; i++) cin >> c[i]; sort(a, a + n, greater<long long>()); priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> pq; vector<vector<long long>> store; long long idx = -1; for (long long it = 0; it < n; it++) { long long i = a[it]; if (pq.empty() or pq.top().first >= c[i]) { idx++; store.push_back({i}); pq.push({1, idx}); continue; } pair<long long, long long> loc = pq.top(); pq.pop(); store[loc.second].push_back(i); loc.first++; pq.push(loc); } cout << store.size() << '\n'; for (vector<long long> &v : store) { cout << v.size() << " "; for (long long &i : v) cout << i << " "; cout << '\n'; } return 0; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin.tie(nullptr); long long t_c = 1; while (t_c--) { auto ans = solve(); } }
4
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; const long double EPS = 1e-9; bool ok[10005]; int dp[10005]; int a[10005], b[10005]; int ic[10005], uc[10005]; int main() { int n, h = 0, mi = 0, mu = 0; int l, r; int ans = 0; scanf("%d%d%d", &n, &l, &r); for (int i = 0; i < (int)(n); i++) { scanf("%d", &a[i]); h += a[i]; } l = h - l; r = h - r; swap(l, r); for (int i = 0; i < (int)(n); i++) scanf("%d", &b[i]); for (int i = 0; i < (int)(n); i++) if (b[i]) ic[mi++] = a[i]; else uc[mu++] = a[i]; sort(ic, ic + mi); reverse(ic, ic + mi); for (int i = 0; i <= h; i++) dp[i] = -INF; dp[0] = 0; for (int i = 0; i < (int)(mu); i++) for (int j = h; j >= uc[i]; j--) dp[j] = max(dp[j], dp[j - uc[i]]); for (int i = 0; i < (int)(mi); i++) for (int j = h; j >= ic[i]; j--) dp[j] = max(dp[j], dp[j - ic[i]] + (j >= l && j <= r)); for (int i = 0; i <= h; i++) ans = max(ans, dp[i]); printf("%d\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, h, l, r, a[2020]; int dp[2020][2020]; int DFS(int i, int sum) { if (i >= n) { return 0; } if (dp[i][sum] != -1) return dp[i][sum]; int x = (a[i] + sum) % h; int y = (a[i] - 1 + sum) % h; int c = DFS(i + 1, x) + (x >= l && x <= r); int d = DFS(i + 1, y) + (y >= l && y <= r); return dp[i][sum] = max(c, d); } int main() { cin >> n >> h >> l >> r; for (int i = 0; i < n; i++) cin >> a[i]; memset(dp, -1, sizeof(dp)); cout << DFS(0, 0) << endl; return 0; }
5
#include<bits/stdc++.h> using namespace std; char a[9],b[9]; int ans; int main(){ scanf("%s%s",a,b); for(int i=0;i<3;i++)if(a[i]==b[i])ans++; printf("%d",ans); }
0
#include <iostream> #include <vector> using namespace std; int N, D, Q; vector<int> vD, A, B; int main(void) { cin.tie(0); ios::sync_with_stdio(false); cin >> N >> D; vD.resize(N + 1); vD[0] = 0; A.resize(N + 1); A[0] = D; for (int i = 1; i <= N; ++i) { int d; cin >> d; vD[i] = d; if (d <= A[i - 1]) A[i] = A[i - 1] - d; else { int d2 = d - A[i - 1]; A[i] = (d2 > A[i - 1]) ? A[i - 1] : d2; } } B.resize(N + 2); B[N + 1] = 1; for (int i = N; i > 0; --i) { if (B[i + 1] <= vD[i] / 2) B[i] = B[i + 1]; else B[i] = B[i + 1] + vD[i]; } cin >> Q; for (int i = 0; i < Q; ++i) { int q; cin >> q; if (A[q - 1] == 0 || B[q + 1] > A[q - 1]) cout << "NO\n"; else cout << "YES\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; void wssert(bool b) { if (!b) { cout << "GARBAGE" << '\n'; exit(0); } } using i64 = int64_t; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int T; cin >> T; while (T--) { i64 N; cin >> N; int M; cin >> M; i64 K; cin >> K; vector<i64> P(M + 2); P[0] = 0; for (int i = 1; i <= M; i++) cin >> P[i]; P[M + 1] = N; vector<i64> lens(M + 1); for (int i = 0; i <= M; i++) { lens[i] = P[i + 1] - P[i]; } M++; auto upVal = [&]() { i64 mi = 0; i64 ma = *max_element(lens.begin(), lens.end()); while (ma - mi > 1) { i64 md = (mi + ma) / 2; i64 curK = 0; for (i64 l : lens) { curK += (l - 1) / md; } if (curK <= K) { ma = md; } else { mi = md; } } return ma; }(); wssert(upVal <= N); auto downVal = [&]() { i64 mi = 1; i64 ma = *min_element(lens.begin(), lens.end()) + 1; while (ma - mi > 1) { i64 md = (mi + ma) / 2; i64 curK = 0; for (i64 l : lens) { curK += l / md - 1; } if (curK >= K) { mi = md; } else { ma = md; } } return mi; }(); wssert(0 < downVal); wssert(downVal <= upVal); vector<pair<i64, i64>> options; options.reserve(M); for (i64 l : lens) { i64 upK = (l - 1) / upVal; i64 downK = l / downVal - 1; wssert(upK - downK <= 1); if (upK - downK == 1) { i64 loVal = l / (upK + 1); i64 hiVal = (l + downK) / (downK + 1); wssert(loVal < downVal && upVal < hiVal); options.emplace_back(loVal, hiVal); } } sort(options.begin(), options.end()); i64 ans = N + 1; i64 curUpVal = upVal; for (int i = 0; i < int(options.size()); i++) { ans = min(ans, curUpVal - options[i].first); curUpVal = max(curUpVal, options[i].second); } ans = min(ans, curUpVal - downVal); cout << ans << '\n'; } return 0; }
5
#include<iostream> using namespace std; int main(){ int n,a,f,ans=0; cin>>n; while(n--){ cin>>a; f=0; for(int i=2;i*i<=a;i++) if(!(a%i))f=1; if(!f)ans++; } cout<<ans<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; n = min(n, m); cout << n + 1 << endl; for (int i = 0; i <= n; i++) { cout << i << " " << n - i << endl; } }
3
#include <iostream> #include <queue> using namespace std; int H,W,K; int sx,sy; int dx[4] = {-1,1,0,0},dy[4] = {0,0,-1,1}; char A[810][810]; int B[810][810] = {{0}},visited[810][810] = {0}; bool in(int x, int y){ return 0<=x && x<=H-1 && 0<=y && y<=W-1; } void bfs(void){ visited[sx][sy] = 1; B[sx][sy] = 0; queue<pair<int,int>> Q; Q.push({sx,sy}); while(!Q.empty()){ pair<int,int> p = Q.front(); Q.pop(); int x = p.first,y = p.second; for(int i=0;i<4;i++){ int nx = x+dx[i],ny = y+dy[i]; if(in(nx,ny) && visited[nx][ny]==0 && B[x][y]<K && A[nx][ny]=='.'){ B[nx][ny] = B[x][y]+1; Q.push({nx,ny}); visited[nx][ny] = 1; } } } } int main(){ cin >> H >> W >> K; for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ cin >> A[i][j]; if(A[i][j]=='S'){ sx = i; sy = j; } } } bfs(); B[sx][sy] = 1; int ans = 1e9; for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ if(B[i][j]!=0){ int t = min(min(i,H-1-i),min(j,W-j-1)); ans = min(ans,1+(t+K-1)/K); if(i==0 || i==H-1 || j==0 || j==W-1) ans = 1; } } } cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; const int MAXC = 1005; int R, C; string s[MAXC]; bool ok[MAXC]; int ans; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> R >> C; for (int i = 0; i < R; ++i) cin >> s[i]; for (int j = 0; j < C; ++j) { bool er = false; for (int i = 1; i < R; ++i) if (s[i][j] < s[i - 1][j] && !ok[i]) { er = true; break; } if (er) { ans++; } else { for (int i = 1; i < R; ++i) if (s[i][j] > s[i - 1][j]) ok[i] = true; } } cout << ans << "\n"; return 0; }
1
#include <bits/stdc++.h> using namespace std; int t, n, a, b, x; string str; int main() { cin >> t; while (t--) { a = 0; b = 0; x = 0; cin >> n; cin >> str; for (int i = 0; i < n; i++) { if (str[i] == '(') a++; else b++; if (a < b) { b--; x++; } } cout << x << endl; } return 0; }
3
#include<bits/stdc++.h> using namespace std; int main() { string s; cin >> s; for(int i=0;i< 3;i++){ cout<<s[i]; } }
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int x, n, m; cin >> x >> n >> m; if (x <= m * 10) { cout << "YES" << endl; } else { int i = 0; while (i < n) { x = (x / 2) + 10; i++; } int y; y = x - (10 * m); if (y > 0) cout << "NO" << endl; else cout << "YES" << endl; } } }
2
#include <bits/stdc++.h> using namespace std; vector<int> v; int n, c, d, m, cnt, a[1000], b[1000]; string s, t; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < m; i++) cin >> b[i]; for (int i = 0; i < n; i++) { if (a[i] <= b[d]) { d++; c++; } if (d == m) break; } cout << c << endl; }
1
#include <bits/stdc++.h> using namespace std; const int Maxn = 50005; const int Maxm = 3 * Maxn; const int lim = 50000000; struct event { int typ, x, y, y2; event(int typ = 0, int x = 0, int y = 0, int y2 = 0) : typ(typ), x(x), y(y), y2(y2) {} bool operator<(const event &e) const { if (x != e.x) return x < e.x; if (typ != e.typ) return typ < e.typ; return y < e.y; } }; int n, m; int vx1[Maxn], vy1[Maxn], vx2[Maxn], vy2[Maxn]; int hx1[Maxn], hy1[Maxn], hx2[Maxn], hy2[Maxn]; event E[Maxm]; int elen; int res; bool Ok(int side) { elen = 0; for (int i = 0; i < n; i++) if (2 * side <= vy2[i] - vy1[i]) E[elen++] = event(1, vx1[i], vy1[i] + side, vy2[i] - side); for (int i = 0; i < m; i++) if (2 * side <= hx2[i] - hx1[i]) { E[elen++] = event(0, hx1[i] + side, hy1[i]); E[elen++] = event(2, hx2[i] - side, hy1[i]); } set<int> Y; sort(E, E + elen); for (int i = 0; i < elen; i++) if (E[i].typ == 0) Y.insert(E[i].y); else if (E[i].typ == 2) Y.erase(E[i].y); else { set<int>::iterator it = Y.lower_bound(E[i].y); if (it != Y.end() && *it <= E[i].y2) return true; } return false; } int main() { scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { int l; scanf("%d %d %d", &vx1[i], &vy1[i], &l); vx2[i] = vx1[i]; vy2[i] = vy1[i] + l; } for (int i = 0; i < m; i++) { int l; scanf("%d %d %d", &hx1[i], &hy1[i], &l); hx2[i] = hx1[i] + l; hy2[i] = hy1[i]; } int l = 1, r = lim; while (l <= r) { int m = l + r >> 1; if (Ok(m)) { res = m; l = m + 1; } else r = m - 1; } printf("%d\n", res); return 0; }
4
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; long long sqr(long long a) { return a * a; }; long long gcd(long long a, long long b) { return !a ? b : gcd(b % a, a); } long long binpow(long long a, long long b, long long mod) { return b ? (b % 2 ? (a * (sqr(binpow(a, b / 2, mod)) % mod)) % mod : sqr(binpow(a, b / 2, mod)) % mod) : 1; } int main() { cout.tie(0), cin.tie(0), ios_base::sync_with_stdio(0); long long i, t, x, y, k; cin >> t; for (i = 0; i < t; i++) { cin >> x >> y; if (!x) { cout << "Bob\n"; continue; } if (y % 3 || x < y) cout << (x % 3 ? "Alice" : "Bob") << '\n'; else if (x == y) cout << "Alice\n"; else if (y == 3) cout << (x % 4 ? "Alice" : "Bob") << '\n'; else { if ((x + 1) % (y + 1) == 0) cout << "Alice\n"; else { k = x / (y + 1); k = k * y + k; cout << ((x - k) % 3 == 0 ? "Bob" : "Alice") << '\n'; } } } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; cout << (s[0]==s[1] || s[1]==s[2] || s[2]==s[3] ? "Bad" : "Good"); }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define all(x) (x).begin(),(x).end() const int mod=1000000007,MAX=1<<20,INF=1<<20; const long double eps=1e-13; int main(){ std::ifstream in("text.txt"); std::cin.rdbuf(in.rdbuf()); cin.tie(0); ios::sync_with_stdio(false); int N,K;cin>>N>>K; vector<long double> x(N),y(N),c(N); for(int i=0;i<N;i++) cin>>x[i]>>y[i]>>c[i]; long double left=0.0,right=1e6; if(K==1){ cout<<0<<endl; return 0; } if(K==2){ long double ans=INF; for(int i=0;i<N;i++){ for(int j=i+1;j<N;j++){ ans=min(ans,(hypot(x[i]-x[j],y[i]-y[j])*c[i]*c[j])/(c[i]+c[j])); } } cout<<setprecision(25)<<ans<<endl; return 0; } for(int q=0;q<100;q++){ long double mid=(left+right)/2.0; bool ok=false; for(int i=0;i<N;i++){ for(int j=0;j<N;j++){ if(i==j) continue; int cnt=0; long double ri=mid/c[i],rj=mid/c[j]; if(ri>rj+eps) continue; if(hypot(x[i]-x[j],y[i]-y[j])>ri+rj+eps) continue; long double x1=x[j]-x[i],y1=y[j]-y[i],a=(x1*x1+y1*y1+ri*ri-rj*rj)/2.0; long double xx=(a*x1+y1*sqrt((x1*x1+y1*y1)*ri*ri-a*a))/(x1*x1+y1*y1); long double yy=(a*y1-x1*sqrt((x1*x1+y1*y1)*ri*ri-a*a))/(x1*x1+y1*y1); //cout<<x1<<" "<<y1<<" "<<a<<" "<<ri<<" "<<rj<<" "<<xx<<" "<<yy<<endl; //cout<<ri<<" "<<a<<" "<<(x1*x1+y1*y1)*ri*ri-a*a+eps<<endl; xx+=x[i]; yy+=y[i]; if(hypot(x1,y1)+ri<rj+eps) continue; for(int k=0;k<N;k++){ if(hypot(xx-x[k],yy-y[k])<=mid/c[k]+eps) cnt++; } if(cnt>=K) ok=true; } } for(int i=0;i<N;i++){ int cnt=0; for(int k=0;k<N;k++){ if(hypot(x[i]-x[k],y[i]-y[k])<=mid/c[k]+eps) cnt++; } if(cnt>=K) ok=true; } if(ok) right=mid; else left=mid; } cout<<setprecision(25)<<right<<endl; }
0
#include <bits/stdc++.h> using namespace std; const int MaxN = 20; const int MaxM = 150; int n; char s[MaxM][MaxN + 10]; long long d[(1 << MaxN)]; long double prob[MaxN + 10]; long double totalGuessed[MaxN + 10]; inline bool inMask(long long mask, int pos) { return (mask >> pos) & 1; } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s", s[i]); } int m = strlen(s[0]); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) { continue; } int same = 0; for (int k = 0; k < m; k++) { if (s[i][k] == s[j][k]) { same |= (1 << k); } } d[same] |= (1LL << i); } } for (int mask = (1 << m) - 1; mask; mask--) { for (int i = 0; i < m; i++) { if (inMask(mask, i)) { d[mask ^ (1 << i)] |= d[mask]; } } } long double ans = 0; for (int mask = 0; mask < (1 << m); mask++) { int moves = __builtin_popcount(mask) + 1; for (int i = 0; i < m; i++) { if (!inMask(mask, i)) { long long res = d[mask] ^ d[mask ^ (1 << i)]; if (res == 0) { continue; } int cntGuessed = __builtin_popcountll(res); totalGuessed[moves] += cntGuessed; } } } for (int i = 1; i <= m; i++) { long double val = totalGuessed[i] * i; for (int j = 0; j < i - 1; j++) { val *= (long double)(i - 1 - j) / (long double)(m - j); } ans += val / (long double)(m - i + 1); } ans /= (long double)(n); printf("%.15lf\n", (double)ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; priority_queue<pair<long long, long long>, vector<pair<long long, long long> >, greater<pair<long long, long long> > > pq; priority_queue<long long, vector<long long>, greater<long long> > pq1; long long result(long long a, long long b, long long p) { long long ans = 1; if (b == (-1)) b = p - 2; while (b) { if (b & 1) { ans = (ans * a) % p; } a = (a * a) % p; b >>= 1; } return ans; } long long T = 1, n, m, k; int main() { if (fopen("input.txt", "r")) freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> T; while (T--) { string s, w; cin >> s; w = s; long long x; cin >> x; n = s.size(); for (int i = 0; i < n; i++) w[i] = '1'; for (int i = 0; i < n; i++) { if (s[i] == '0') { if (i - x >= 0) w[i - x] = '0'; if (i + x < n) w[i + x] = '0'; } } string p = ""; for (int i = 0; i < n; i++) { if ((i - x >= 0 && w[i - x] == '1') || (i + x < n && w[i + x] == '1')) { p.push_back('1'); } else { p.push_back('0'); } } if (p == s) { cout << w << "\n"; } else { cout << "-1\n"; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAX = 111111, MAX2 = 575; int n; int a[100000]; int nsd(int a, int b) { if (!a || !b) return a + b; return nsd(b % a, a); } int main() { cin >> n; int b = 0, q = 0; string s = ""; int type = 0; for (int(i) = (0); i < (n); ++(i)) { cin >> a[i]; if (a[i] % 2 == 1) b++, type = i; q = nsd(q, a[i]); } if (n == 1) { cout << a[0] << endl; for (int(i) = (0); i < (a[0]); ++(i)) cout << 'a'; return 0; } if (b >= 2) { cout << "0\n"; for (int(i) = (0); i < (n); ++(i)) for (int(j) = (0); j < (a[i]); ++(j)) cout << char(i + 'a'); return 0; } if (b == 1) { cout << q << "\n"; s = ""; for (int(i) = (0); i < (n); ++(i)) for (int(j) = (0); j < (a[i] / (2 * q)); ++(j)) { s += char(i + 'a'); } string s0 = s; reverse(s0.begin(), s0.end()); s += char(type + 'a') + s0; for (int(i) = (0); i < (q); ++(i)) cout << s; return 0; } cout << q << "\n"; s = ""; for (int(i) = (0); i < (n); ++(i)) for (int(j) = (0); j < (a[i] / q); ++(j)) { s += char(i + 'a'); } string s0 = s; reverse(s0.begin(), s0.end()); s += s0; q /= 2; for (int(i) = (0); i < (q); ++(i)) cout << s; return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, conf[4024], vol[4024], d[4024]; bool out[4024]; int outBy[4024]; vector<int> ans; void run(int s, int cVol) { for (int i = s; i < n; i++) { if (out[i]) { continue; } conf[i] -= cVol; if (conf[i] < 0) { run(i + 1, d[i]); out[i] = 1; } } } bool cOut[4024]; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> vol[i] >> d[i] >> conf[i]; } for (int s = 0; s < n; s++) { if (out[s]) { continue; } ans.push_back(s + 1); int cVol = vol[s]; for (int i = s + 1; cVol > 0 && i < n; i++) { if (out[i]) { continue; } conf[i] -= cVol; cVol--; } for (int i = 0; i < n; i++) { cOut[i] = out[i]; } cVol = vol[s]; for (int i = s + 1; cVol > 0 && i < n; i++) { if (cOut[i]) { continue; } cVol--; if (out[i]) { continue; } if (conf[i] < 0) { run(i + 1, d[i]); out[i] = 1; } } } cout << ans.size() << "\n"; for (int i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } cout << "\n"; return 0; }
1
#include <bits/stdc++.h> using namespace std; #define ll long long int #define pb push_back #define fi first #define se second #define mp make_pair #define mt make_tuple #define all(A) A.begin(), A.end() #define sz(A) (int)A.size() typedef vector <int> vi; typedef vector <ll> vl; typedef pair <int, int> pii; typedef tuple <int, int, int> tii; const ll mod = 1e9 + 7; //const ll mod = 998244353; const int inf = 1e9; //const ll inf = 1e18; const int N = 2e5; void solve () { int n; cin >> n; int a[n], b[n]; for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; } int tm[n]; int t = 0; for (int i = 0; i < n; i++) { cin >> tm[i]; } for (int i = 0; i < n; i++) { if (i == 0) { t += a[i] + tm[i]; } else { t += a[i] - b[i - 1] + tm[i]; } if (i == n - 1) { cout << t << '\n'; return; } int x = ((b[i] - a[i]) % 2 == 0 ? 0 : 1) + (b[i] - a[i]) / 2; t += max(x, b[i] - t); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); #endif int t; cin >> t; while (t--) { solve(); } return 0; }
1
#include <bits/stdc++.h> using namespace std; long long int arr[27]; long long int arrtest[200005]; int main() { long long int test; cin >> test; for (long long int z = 0; z < test; z++) { string reqst; long long int n, m; cin >> n >> m; cin >> reqst; for (long long int i = 0; i < 26; i++) { arr[i] = 0; } for (long long int i = 0; i <= n; i++) { arrtest[i] = 0; } for (long long int i = 0; i < m; i++) { long long int temp; cin >> temp; arrtest[temp] += 1; } arrtest[n] = 1; long long int counter = 0; for (long long int j = n; j > 0; j--) { counter += arrtest[j]; arr[((int)reqst[j - 1] - (int)'a')] += counter; } for (long long int i = 0; i < 26; i++) { cout << arr[i] << " "; } cout << endl; } }
3
#include <bits/stdc++.h> using namespace std; using ll = long long; using db = double; #define fi first #define se second #define pb push_back #define eb emplace_back #define all(v) (v).begin(), (v).end() #define siz(v) (ll)(v).size() #define rep(i, n) for (ll i = 0; i < (ll)(n); i++) #define repi(i, x, n) for (ll i = x; i < (ll)(n); i++) #define lb lower_bound #define ub upper_bound typedef pair<int, int> P; typedef pair<ll, ll> PL; const ll mod = 1000000007; const ll INF = 1000000099; vector<ll> dx = {-1, 1, 0, 0}, dy = {0, 0, -1, 1}; signed main() { int n; cin >> n; ll p=0,k=1; while (n--) { ll q, x; cin >> q >> x; if (q == 1) { k *= x; p*=x; } else if (q == 2) { p += x; } else { p -= x; } } cout<<-p<<" "<<k<<endl; }
0
#include <bits/stdc++.h> long long ago; using namespace std; template <class T = long long int> T nxt() { T x; cin >> x; return x; } long long int pw(long long int a, long long int b, long long int mod) { if (!b) return 1; if (b & 1) return a * pw(a * a % mod, b / 2, mod) % mod; return pw(a * a % mod, b / 2, mod) % mod; } const long long int N = 1000 + 10; const long long int INF = 8e18; const long long int MOD = 1e9 + 7; long long int n, m, mark[N]; double dist[N], S[N], Z[N]; vector<pair<long long int, double>> adj[N]; priority_queue<pair<double, long long int>, vector<pair<double, long long int>>, greater<pair<double, long long int>>> pq; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout << setprecision(15) << fixed; cin >> n; for (long long int i = 1; i <= n; i++) { for (long long int j = 1; j <= n; j++) { long long int p = nxt(); if (i == j || p == 0) continue; adj[j].emplace_back(i, double(p) / 100); } } fill(Z, Z + N, 1.0); fill(dist, dist + N, INF); dist[n] = 0; pq.push({0, n}); while (!pq.empty()) { long long int u = pq.top().second; pq.pop(); if (mark[u]) continue; mark[u] = 1; for (auto [v, p] : adj[u]) { if (mark[v]) continue; S[v] += Z[v] * p * (dist[u] + 1); Z[v] *= (1 - p); dist[v] = (S[v] + Z[v]) / (1 - Z[v]); pq.push({dist[v], v}); } } cout << dist[1] << '\n'; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { long long n, x, y, ans(0), c(0); cin >> n; vector<pair<long long, long long> > v; map<pair<long long, long long>, long long> mp; for (long long i = 0; i < n; i++) { cin >> x >> y; mp[{x, y}] = mp[{x, y}] + 1; if (mp[{x, y}] == 1) v.push_back({x, y}); if (x == y and mp[{x, y}] == 1) c++; } if (v[0].first != 0 or v[0].second != 0) { v.push_back({0, 0}); sort(v.begin(), v.end()); c++; } for (long long i = 0; i + 1 < v.size(); i++) { long long x1(v[i].first), x2(v[i + 1].first), y1(v[i].second), y2(v[i + 1].second); if (x2 < y1 or y2 < x1) { } else if (y1 <= x1 and x2 <= y2) { ans += (x2 - x1 + 1); } else if (x1 <= y1 and y2 <= x2) { ans += (y2 - y1 + 1); } else if ((y1 < x1 and y2 <= x2) or (y1 <= x1 and y2 < x2)) { ans += (y2 - x1 + 1); } else if (x1 < y1 and x2 <= y2 or (x1 <= y1 and x2 < y2)) { ans += (x2 - y1 + 1); } if (v[i].first == v[i].second) ans--; if (v[i + 1].first == v[i + 1].second) ans--; } ans += c; cout << ans; return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, m; struct Q { int id, v, ans; Q() {} Q(int a, int b) { id = a; v = b; } friend bool operator<(const Q &a, const Q &b) { return a.v < b.v; } friend bool operator<(const Q &a, int b) { return a.v < b; } } q[200010]; struct Interval { int l, r, delta, cnt; Interval() {} Interval(int a, int b, int c = 0, int d = 0) { l = a; r = b; delta = c; cnt = d; } }; pair<int, int> p[200010]; vector<Interval> block; void rebuild() { for (int i = 0; i < block.size(); i++) { for (int j = block[i].l; j <= block[i].r; j++) { q[j].ans += block[i].cnt; q[j].v += block[i].delta; } } sort(q + 1, q + m + 1); block.clear(); block.push_back(Interval(1, m)); } bool cmp(const Q &a, const Q &b) { return a.id < b.id; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d%d", &p[i].second, &p[i].first); p[i].first = -p[i].first; } sort(p + 1, p + n + 1); scanf("%d", &m); for (int i = 1; i <= m; i++) { scanf("%d", &q[i].v); q[i].id = i; q[i].ans = 0; } rebuild(); for (int i = 1; i <= n; i++) { int tmp = p[i].second; int sz = block.size(); for (int j = 0; j < sz; j++) { if (q[block[j].l].v + block[j].delta >= tmp) { block[j].delta -= tmp; block[j].cnt++; } else { if (q[block[j].r].v + block[j].delta >= tmp) { int pos = lower_bound(q + block[j].l, q + block[j].r + 1, tmp - block[j].delta) - q; block.push_back(Interval(pos, block[j].r, block[j].delta - tmp, block[j].cnt + 1)); block[j].r = pos - 1; } } } if (block.size() >= 450) rebuild(); } for (int i = 0; i < block.size(); i++) { for (int j = block[i].l; j <= block[i].r; j++) { q[j].ans += block[i].cnt; q[j].v += block[i].delta; } } sort(q + 1, q + m + 1, cmp); for (int i = 1; i <= m; i++) { printf("%d ", q[i].ans); } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; int l[300010]; int r[300010]; multiset<int> a, b; int main(void) { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &l[i]); scanf("%d", &r[i]); a.insert(l[i]); b.insert(r[i]); } int ans = 0; for (int i = 0; i < n; i++) { a.erase(a.find(l[i])); b.erase(b.find(r[i])); ans = max(ans, *b.begin() - *a.rbegin()); a.insert(l[i]); b.insert(r[i]); } printf("%d\n", ans); }
3
#include <iostream> #include <complex> #include <sstream> #include <string> #include <algorithm> #include <deque> #include <list> #include <map> #include <numeric> #include <queue> #include <vector> #include <set> #include <limits> #include <cstdio> #include <cctype> #include <cmath> #include <cstring> #include <cstdlib> #include <ctime> using namespace std; #define REP(i, j) for(int i = 0; i < (int)(j); ++i) #define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i) #define SORT(v) sort((v).begin(), (v).end()) #define REVERSE(v) reverse((v).begin(), (v).end()) typedef complex<double> P; const int MAX_V = 250; int V, match[MAX_V], used[MAX_V]; vector<int> G[MAX_V]; bool dfs(int v){ used[v] = true; REP(i, G[v].size()){ int u = G[v][i], w = match[u]; if(w < 0 || (!used[w] && dfs(w))){ match[v] = u; match[u] = v; return true; } } return false; } int bipartite_matching(){ int res = 0; memset(match, -1, sizeof(match)); REP(v, V) if(match[v] < 0){ memset(used, 0, sizeof(used)); if(dfs(v)) ++res; } return res; } int main() { int X, Y, E; cin >>X >>Y >>E; V = X + Y; REP(i, E){ int u, v; cin >>u >>v; v += X; G[u].push_back(v); G[v].push_back(u); } cout <<bipartite_matching() <<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 100000 + 50; const int INF = 1 << 30; int a, b, r; int main() { scanf("%d%d%d", &a, &b, &r); if (2 * r > a || 2 * r > b) printf("Second\n"); else { printf("First\n"); } return 0; }
1
#include <bits/stdc++.h> using namespace std; mt19937 rng32(chrono::steady_clock::now().time_since_epoch().count()); const int N = (int)1e6 + 5; const int mod = 1000000007; const double EPS = 1e-9, PI = acos(-1); double DEG_to_RAD(double theta) { return ((theta * PI) / 180.0); } class point { public: double x, y; point() { x = 0; y = 0; } point(double _x, double _y) : x(_x), y(_y) {} bool operator<(point other) const { if (fabs(x - other.x) > EPS) return x < other.x; return y < other.y; } bool operator==(point other) const { return ((fabs(x - other.x) < EPS) && (fabs(y - other.y) < EPS)); } }; ostream &operator<<(ostream &os, const point &p) { os << "(" << p.x << "," << p.y << ")"; } class vect { public: double x, y; vect() : x(0), y(0) {} vect(double _x, double _y) : x(_x), y(_y) {} vect(point a, point b) : x(b.x - a.x), y(b.y - a.y) {} }; double cross(vect a, vect b) { return a.x * b.y - a.y * b.x; } double ccw(point p, point q, point r) { return cross(vect(p, q), vect(p, r)); } bool collinear(point p, point q, point r) { return fabs(cross(vect(p, q), vect(p, r))) < EPS; } point pivot(0, 0); bool compareX(const point &a, const point &b) { if (a.x == b.x) return a.y <= b.y; return a.x < b.x; } vector<point> convex_hull(vector<point> pts) { sort(pts.begin(), pts.end(), compareX); vector<point> H; H.clear(); for (int step = 0; step <= 1; step++) { auto start = H.size(); for (point P : pts) { while (H.size() >= start + 2 && ccw(H[H.size() - 2], H[H.size() - 1], P) < 0) H.pop_back(); H.push_back(P); } H.pop_back(); reverse(pts.begin(), pts.end()); } return H; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); vector<point> v; v.clear(); vector<point> v1; v1.clear(); int n; cin >> n; for (int i = 1; i <= n; i++) { double x, y; cin >> x >> y; point P((double)x, (double)y); v1.push_back(P); v.push_back(P); } int m; cin >> m; for (int i = 1; i <= m; i++) { double x, y; cin >> x >> y; point P((double)x, (double)y); v.push_back(P); } vector<point> CH = convex_hull(v); vector<point> CH1 = convex_hull(v1); if (CH == CH1) { cout << "YES\n"; } else cout << "NO\n"; }
2
#include<cstdio> #include<cstring> #include<algorithm> #define MAXN 100 using namespace std; char Map[MAXN+5][MAXN+5]; int n,m,ex,ey; int sumr[MAXN+5][MAXN+5],sumc[MAXN+5][MAXN+5]; int dp[MAXN+5][MAXN+5][MAXN+5]; void Init() { memset(dp,-1,sizeof(dp)); } int main() { // freopen("robot.in","r",stdin); // freopen("robot.out","w",stdout); Init(); scanf("%d %d",&n,&m); for(int i=1;i<=n;i++) { scanf("%s",Map[i]+1); for(int j=1;j<=m;j++) { sumr[i][j]=sumr[i][j-1]; sumc[i][j]=sumc[i-1][j]; if(Map[i][j]=='E') ex=i,ey=j; else if(Map[i][j]=='o') { sumr[i][j]++; sumc[i][j]++; } } } int L,R,U,D,ans=0; L=ey-1; R=m-ey; U=ex-1; D=n-ex; dp[0][0][0]=0; for(int l=0;l<=L;l++) for(int r=0;r<=R;r++) for(int u=0;u<=U;u++) for(int d=0;d<=D;d++) { if(dp[r][u][d]==-1) continue; ans=max(dp[r][u][d],ans); int up=max(ex-u,d+1); int down=min(ex+d,n-u); int left=max(ey-l,r+1); int right=min(ey+r,m-l); if(up>down||left>right) continue; int add; if(u<U-d) { add=sumr[ex-u-1][right]-sumr[ex-u-1][left-1]; dp[r][u+1][d]=max(dp[r][u+1][d],dp[r][u][d]+add); } if(d<D-u) { add=sumr[ex+d+1][right]-sumr[ex+d+1][left-1]; dp[r][u][d+1]=max(dp[r][u][d+1],dp[r][u][d]+add); } if(r<R-l) { add=sumc[down][ey+r+1]-sumc[up-1][ey+r+1]; dp[r+1][u][d]=max(dp[r+1][u][d],dp[r][u][d]+add); } if(l<L-r) { add=sumc[down][ey-l-1]-sumc[up-1][ey-l-1]; dp[r][u][d]=max(dp[r][u][d],dp[r][u][d]+add); } } printf("%d\n",ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n,k,x; cin>>n>>k; int ans=0; for (int i=0;i<n;i++){ cin>>x; ans+=min(abs(x-k),abs(0-x))*2; } cout<<ans; return 0; }
0
#include <bits/stdc++.h> using namespace std; int read() { int x = 0; bool flg = false; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') flg = true; for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48); return flg ? -x : x; } void print(const __int128 &x) { if (x > 9) print(x / 10); putchar((x % 10) ^ 48); } int Q; struct C { int p, w, s; } stk[600010]; int top; int nxt[600010], gnd[600010]; char s[600010]; unsigned long long S; __int128 W; void erase(int x) { int p = lower_bound(stk + 1, stk + top + 1, (C){x, 0, 0}, [&](const C &x, const C &y) { return x.p < y.p; }) - stk; assert(p <= top); S -= stk[p].w; --stk[p].s; } void solve() { Q = read(); const unsigned lim = (1 << 30) - 1; nxt[0] = -1; for (int i(1), _i(Q); i <= _i; i++) { static char str[2]; scanf("%s", str); s[i] = (str[0] - 'a' + W % 26) % 26 + 'a'; const int w = read() ^ (W & lim); if (i > 1) { int j; for (j = nxt[i - 1]; j && s[j + 1] != s[i]; j = nxt[j]) ; if (s[j + 1] == s[i]) { nxt[i] = j + 1; } if (s[nxt[i - 1] + 1] != s[i]) gnd[i] = i; else gnd[i] = gnd[nxt[i]]; } int s = 1; while (top && w < stk[top].w) S -= 1ULL * stk[top].w * stk[top].s, s += stk[top--].s; stk[++top] = (C){i, w, s}; S += 1ULL * stk[top].w * stk[top].s; for (int j = gnd[i]; j; j = gnd[nxt[j]]) { for (int k = nxt[j - 1]; k != nxt[j] - 1; k = nxt[k]) erase(i - k); } W += S; print(W); putchar(10); } } int main() { for (int T = 1; T--;) solve(); return 0; }
5
#include <bits/stdc++.h> using namespace std; template <class T> T abss(T a) { return a > 0 ? a : -a; } const auto N = 2000; deque<int> arr[N + 1]; list<int> tops; void dfs(int num, int& len) { if (arr[num].size() == 0) return; else { ++len; list<int> lens_ch; for (auto a : arr[num]) { int tmp_len = 0; dfs(a, tmp_len); lens_ch.push_back(tmp_len); } len += *max_element(lens_ch.begin(), lens_ch.end()); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int num, cnt = 0; size_t _q_; cin >> _q_; while (_q_--) { ++cnt; cin >> num; if (num == -1) tops.push_back(cnt); else { arr[num].push_back(cnt); } } vector<int32_t> lens(tops.size(), 0); cnt = 0; for (auto top : tops) { dfs(top, lens[cnt++]); } auto max_ = *max_element(lens.begin(), lens.end()) + 1; cout << max_ << '\n'; return 0; }
1
#include <bits/stdc++.h> using namespace std; int p[1005]; int main() { int T, n, k, h, i; scanf("%d", &T); while (T--) { scanf("%d%d%d", &h, &k, &n); for (i = 0; i < n; i++) { scanf("%d", &p[i]); } int e = 0; while (1) { if (k + e <= h) { int q = k + e; for (i = 0; i < n; i++) { if (p[i] == q) break; } if (i == n) { break; } } if (e != 0) { if (k - e >= 1) { int q = k - e; for (i = 0; i < n; i++) { if (p[i] == q) break; } if (i == n) { break; } } } e++; } printf("%d\n", e); } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int limit = 63; int d[1500010][2], f[1500010][2], cnt = 0; void push(const long long &x) { int t = 0; for (int i = limit; i >= 1; i--) { if (d[t][((x >> (i - 1)) & 1)] == 0) d[t][((x >> (i - 1)) & 1)] = ++cnt; f[t][((x >> (i - 1)) & 1)]++; t = d[t][((x >> (i - 1)) & 1)]; } } void del(const long long &x) { int t = 0; for (int i = limit; i >= 1; i--) { f[t][((x >> (i - 1)) & 1)]--; t = d[t][((x >> (i - 1)) & 1)]; } } int cal(const long long &x, const long long &y) { int t = 0, res = 0; for (int i = limit; i >= 1; i--) { int next = ((x >> (i - 1)) & 1); if (((y >> (i - 1)) & 1)) { res += f[t][next]; if (d[t][1 - next] > 0) t = d[t][1 - next]; else break; } else { if (d[t][next] > 0) t = d[t][next]; else break; } } return res; } int main() { if (fopen("a.txt", "r")) { freopen("a.txt", "r", stdin); } else ios_base::sync_with_stdio(false); cin.tie(NULL); int q; cin >> q; while (q--) { long long x, p, l; cin >> x >> p; if (x == 1) push(p); else if (x == 2) del(p); else { cin >> l; cout << cal(p, l) << '\n'; } } }
5
#pragma GCC optimize("Ofast") #include<bits/stdc++.h> using namespace std; #define dd double #define ll long long int #define setpre(n) cout << std::setprecision(n); #define flush fflush(stdin); fflush(stdout); #define print cout<<"Case #"<<test_case<<": "; #define light ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define pb push_back #define mkp make_pair #define pi pair<ll,ll> #define pii pair<ll,pi> #define fi first #define sc second ll INF=1e18+5; ll mod=998244353; vector<ll> fact; void solve(ll test_case){ ll n; cin>>n; string s; cin>>s; vector<ll> fleft(n),fright(n); ll count=0,ans=0; for(ll i=n-1;i>=0;i--){ ans+=count; fright[i] = ans; if(s[i]=='*'){fright[i]-=count; ans = fright[i];count++; } } //for(auto ele:fright) cout<<ele<<" "; ans=0;count=0; ll i; for(i=0;i<n;i++){ ans+=count; fleft[i] = ans; if(s[i]=='*'){fleft[i]-=count; ans = fleft[i]; count++; } } ans=fright[0]; i=0; //while(i<n && s[i]=='.') i++; for(i=0;i<n-1;i++){ //if(s[i]=='.') ans = min(ans,fright[i+1]+fleft[i]); } // if(ans==INF) cout<<"0\n"; // else cout<<ans<<"\n"; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif light; ll t; cin>>t; ll i=1ll; while(t--){ solve(i); i++; } }
5
#include <bits/stdc++.h> int a[7][3] = {{0, 1, 2}, {1, 0, 2}, {1, 2, 0}, {2, 1, 0}, {2, 0, 1}, {0, 2, 1}, {0, 1, 2}}; int main() { int n, x; std::cin >> n >> x; n = n % 6; std::cout << a[n][x] << '\n'; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() {int A, B, C; cin >> A >> B >> C; int m = max({A, B, C}); cout << 9 * m + A + B + C << endl;}
0
#include <bits/stdc++.h> using namespace std; const int modd = 1e9 + 9; const int maxn = 4000 + 15; long long C[maxn][maxn]; int n, w, b; int main() { scanf("%d%d%d", &n, &w, &b); long long jc = 1ll; for (int i = 2; i <= w; i++) jc = jc * i % modd; for (int i = 2; i <= b; i++) jc = jc * i % modd; long long ans = 0; for (int i = 0, j; i <= max(w, b); i++) for (C[i][0] = j = 1; j <= i; j++) C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % modd; for (int i = 2; i < n; i++) if (w >= i && b >= n - i) ans += C[w - 1][i - 1] * C[b - 1][n - i - 1] % modd * (i - 1) % modd; ans %= modd; ans = ans * jc % modd; printf("%d\n", (int)ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; long double pi = acos(-1); map<pair<long long, long long>, bool> p; int an[200009], ap[200009]; vector<pair<int, int>> f; int main() { long long t, Max = -1; cin >> t; for (int i = 0; i < t; i++) { long long x, y; cin >> x >> y; Max = max(Max, x + y); } cout << Max << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, i, sum = 0; scanf("%d", &n); int a[n]; for (i = 0; i < n; i++) { scanf("%d", &a[i]); } sort(a, a + n); for (i = 0; i < n; i++) { sum += a[i]; } printf("%d\n", a[n - 1] * n - sum); return 0; }
1
#include <bits/stdc++.h> int main() { int a, b, yin, e = 0, s = 0, max; scanf("%d%d", &a, &b); if (a > b) max = a; else max = b; yin = 7 - max; if (yin % 2 == 0) yin /= 2, e++; if (yin % 3 == 0) yin /= 3, s++; if (e == 0 && s == 0) printf("%d/%d", yin, 6); else printf("%d/%d", yin, 6 / (e * 2 + s * 3)); return 0; }
1
#include <bits/stdc++.h> using namespace std; long long q, dp[100005], fac[555555], inv[555555], k, l, m, n, o, p; map<long long, long long> mp; vector<long long> adj[555555]; const long long mod = 1e9 + 7; pair<long long, long long> a[55555]; bool cmp(pair<long long, long long> a, pair<long long, long long> b) { return a.first + a.second < b.first + b.second; } long long pow(long long a, long long b) { long long op = 1; while (b) { if (b & 1) { op = (op * a) % mod; } a = (a * a) % mod; b /= 2; } return op; } long long mod_inverse(long long a) { return pow(a, mod - 2); } long long kj(long long a, long long b) { if (a < 0 || b < 0 || b > a) return 0; return (fac[a] * ((inv[b] * inv[a - b]) % mod)) % mod; } long long how(long long a, long long b) { return kj(a + b, b); } void solve() { cin >> o >> p >> n; for (long long i = 1; i <= n; i++) { cin >> a[i].first >> a[i].second; } a[++n] = {o, p}; sort(a + 1, a + n + 1, cmp); fac[0] = inv[0] = 1; for (long long i = 1; i < 2e5 + 5; i++) { fac[i] = (fac[i - 1] * i) % mod; inv[i] = mod_inverse(fac[i]); } for (long long i = 1; i <= n; i++) { long long op = how(a[i].first - 1, a[i].second - 1); for (long long j = 1; j < i; j++) { if (a[j].first > a[i].first || a[j].second > a[i].second) continue; op = (op - (dp[j] * how(a[i].first - a[j].first, a[i].second - a[j].second)) % mod + mod) % mod; } dp[i] = op; } cout << dp[n] << endl; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); q = 1; while (q--) { solve(); } }
3
#include <bits/stdc++.h> using namespace std; const int INF = 1LL << 30; vector<bool> s; int n, r; int main() { ios::sync_with_stdio(false); cin >> n >> r; s.resize(n); int res = INF, x; for (int i = 0; i <= r; i++) { int moveNum = 0, wMove = 0, a = i, b = r; while (a != 0 && wMove < res) { moveNum += b / a; wMove += b / a - ((a == 1 && b != 1) ? 2 : 1); b %= a; swap(a, b); } if (b == 1 && moveNum == n && wMove < res) x = i, res = wMove; } if (res == INF) cout << "IMPOSSIBLE\n"; else { for (int i = n - 1; i >= 0; i--) if (x < r) { s[i] = 1; r -= x; } else x -= r; cout << res << '\n'; if (s[1]) { for (int i = 0; i < n; i++) cout << (s[i] ? 'B' : 'T'); } else { cout << 'T'; for (int i = 1; i < n; i++) cout << (s[i] ? 'T' : 'B'); } cout << '\n'; } return 0; }
2
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &x) { x = 0; int flag = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') flag = -1; c = getchar(); } while (isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); } x = x * flag; } const int maxn = 1e5 + 10; int n, r; string s; long long x; stack<int> q; bool inq[maxn]; long long dfs() { if (r >= n) return 1LL; long long ret = 0; if (ret >= (1LL << 32)) { printf("OVERFLOW!!!\n"); exit(0); } long long a; while (r < n) { r++; cin >> s; if (ret >= (1LL << 32)) { printf("OVERFLOW!!!\n"); exit(0); } if (s == "add") ret++; if (s == "end") { inq[q.top()] = 0; q.pop(); return ret; } if (s == "for") { int now = r; read(a); q.push(r); inq[now] = 1; ret = ret + a * dfs(); while (inq[now]) { ret = ret + a * dfs(); if (ret >= (1LL << 32)) { printf("OVERFLOW!!!\n"); exit(0); } } return ret; } } return ret; } int main() { read(n); while (r < n) { x += dfs(); if (x >= (1LL << 32)) { printf("OVERFLOW!!!\n"); return 0; } } if (x >= 0) printf("%lld\n", x); else printf("OVERFLOW!!!\n"); return 0; }
2
#include <bits/stdc++.h> using namespace std; int a[110]; int main() { int n, i, count = 0; while (~scanf("%d", &n)) { for (i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] == 1) { a[i] = -1; count++; } else a[i] = 1; } int maxn = -110; int sum = 0; for (i = 0; i < n; i++) { sum += a[i]; if (sum > maxn) maxn = sum; if (sum < 0) sum = 0; } printf("%d\n", count + maxn); } }
1
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; int re = 1; for (int i = 1; i <= min(a, b); i++) re *= i; cout << re; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 10100; struct person { int p; long long c; } a[maxn]; int cnt[maxn]; int cpy[maxn]; int vis[maxn]; long long minn = 0x3f3f3f3f3f3f3f3f; inline bool cmp(person x, person y) { if (x.p == 1) { return false; } if (y.p == 1) { return true; } return x.c < y.c; } int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { int p, c; scanf("%d%lld", &a[i].p, &a[i].c); cnt[a[i].p]++; } sort(a + 1, a + n + 1, cmp); for (int lim = 0; lim <= n - 1; lim++) { long long money = 0; memset(vis, 0, sizeof(vis)); for (int j = 1; j <= m; j++) { cpy[j] = cnt[j]; } for (int j = 1; j <= n && a[j].p != 1; j++) { if (cpy[a[j].p] > lim) { cpy[1]++; cpy[a[j].p]--; money += a[j].c; vis[j] = 1; } } if (cpy[1] <= lim) { for (int j = 1; j <= n; j++) { if (cpy[1] > lim) { break; } if (!vis[j]) { money += a[j].c; cpy[1]++; } } } if (cpy[1] >= lim) { minn = min(money, minn); } } printf("%lld", minn); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MAX = 100005; const int MOD = 1000000007; unordered_map<int, bool> sp; vector<int> g[MAX]; struct node { int u, v; int cost; node(int _u, int _v, int _cost) { u = _u; v = _v; cost = _cost; } }; bool mst_sort(node &a, node &b) { return a.cost < b.cost; } bool ans_sort(node &a, node &b) { return a.cost > b.cost; } vector<node> e; int par[MAX]; int findpar(int r) { if (par[r] == r) return r; par[r] = findpar(par[r]); return par[r]; } vector<node> tree; void mst(int n) { for (int i = 0; i < n; i++) par[i] = i; sort(e.begin(), e.end(), mst_sort); int c = 0; for (int i = 0; i < e.size(); i++) { int u = findpar(e[i].u); int v = findpar(e[i].v); if (u != v) { par[u] = v; tree.push_back(e[i]); g[e[i].u].push_back(e[i].v); g[e[i].v].push_back(e[i].u); c++; if (c == n - 1) break; } } return; } int can[MAX]; map<pair<int, int>, bool> vis; bool dfs(int n, int par) { if (sp[n]) return can[n] = true; if (vis[pair<int, int>(n, par)]) return can[n] = false; if (can[n] != -1) return can[n]; bool ret = false; for (int i : g[n]) { if (i != par) ret |= dfs(i, n); if (ret) return can[n] = ret; } return can[n] = ret; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); ; int n, m, k; cin >> n >> m >> k; for (int i = 0; i < k; i++) { int temp; cin >> temp; sp[temp] = true; } while (m--) { int u, v, cost; cin >> u >> v >> cost; e.push_back(node(u, v, cost)); } mst(n); sort(tree.begin(), tree.end(), ans_sort); int ans = 0; memset(can, -1, sizeof can); for (int i = 0; i < tree.size(); i++) { node p = tree[i]; if (sp[p.u] && sp[p.v]) { ans = p.cost; break; } if (sp[p.u]) { can[p.v] = -1; if (dfs(p.v, p.u)) { ans = p.cost; break; } } else if (sp[p.v]) { can[p.u] = -1; if (dfs(p.u, p.v)) { ans = p.cost; break; } } else { can[p.u] = can[p.v] = -1; if (dfs(p.u, p.v) && dfs(p.v, p.u)) { ans = p.cost; break; } } vis[pair<int, int>(p.u, p.v)] = true; } for (int i = 0; i < k; i++) cout << ans << " "; cout << "\n"; }
4
#include <bits/stdc++.h> using namespace std; using ll = long long; const int mod = 1e9 + 7; int gcd(int a, int b) { if (!b) return a; return gcd(b, a % b); } int main() { int n; scanf("%d", &n); vector<int> a(n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); vector<int> b(1, a[0]); for (int i = 1; i < n; i++) { if (gcd(a[i], a[i - 1]) != 1) b.push_back(1); b.push_back(a[i]); } printf("%d\n", b.size() - n); for (int i = 0; i < b.size(); i++) printf("%d ", b[i]); return 0; }
1
#include<deque> #include<queue> #include<vector> #include<algorithm> #include<iostream> #include<set> #include<cmath> #include<tuple> #include<string> #include<chrono> #include<functional> #include<iterator> #include<random> #include<unordered_set> #include<array> #include<map> #include<iomanip> #include<assert.h> #include<list> #include<bitset> #include<stack> #include<memory> #include<numeric> using namespace std; using namespace std::chrono; typedef long long int llint; typedef long double lldo; #define mp make_pair #define mt make_tuple #define pub push_back #define puf push_front #define pob pop_back #define pof pop_front #define fir first #define sec second #define res resize #define ins insert #define era erase /*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/ const int mod=1000000007; const llint big=2.19e15+1; const long double pai=3.141592653589793238462643383279502884197; const long double eps=1e-4; template <class T,class U>bool mineq(T& a,U b){if(a>b){a=b;return true;}return false;} template <class T,class U>bool maxeq(T& a,U b){if(a<b){a=b;return true;}return false;} llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);} llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;} template<class T> void SO(T& ve){sort(ve.begin(),ve.end());} template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());} template<class T>llint LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();} template<class T>llint UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();} int main(void){ cout<<fixed<<setprecision(20); double A,B,P,Q,R;cin>>A>>B>>P>>Q>>R; cout<<B+(B*P-(B-A)*Q)/(Q+R)<<endl; return 0; } //1000 //100 //10 //1
0
#include <bits/stdc++.h> using namespace std; int main() { char str[100005]; unsigned long long binhash[270] = {0}; cin >> str; int j = 0; while (str[j]) { binhash[str[j]]++; j++; } unsigned long long res = 0; for (int i = 0; i < 270; i++) res += binhash[i] * binhash[i]; cout << res << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; template <typename T> ostream &operator<<(ostream &output, const vector<T> &v) { if (v.empty()) return output; for (int i = 0; i < v.size() - 1; i++) output << v[i] << " "; output << v.back(); return output; } template <typename T> istream &operator>>(istream &input, vector<T> &v) { for (auto &i : v) cin >> i; return input; } long long int gcd(const long long int a, const long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long int lcm(const long long int a, const long long int b) { return (a * b) / gcd(a, b); } const int N = 1e5 + 100, mod = 1e9 + 7; vector<int> g[N]; long long int a[N]; long long int ans = 0; void dfs(int node, int par, map<long long int, long long int> &m) { map<long long int, long long int> cur; for (auto &i : m) cur[gcd(i.first, a[node])] += i.second; cur[a[node]]++; for (auto &i : cur) { ans += (long long int)i.first * i.second % mod; if (ans >= mod) ans -= mod; } for (auto &i : g[node]) { if (i != par) dfs(i, node, cur); } return; } void __sol() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i + 1]; for (int i = 0; i < n - 1; i++) { int x, y; cin >> x >> y; g[x].emplace_back(y); g[y].emplace_back(x); } map<long long int, long long int> m; dfs(1, 0, m); cout << ans; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int tc = 1; while (tc--) __sol(); return 0; }
3
#include <iostream> #include <string> using namespace std; string decode(string s) { for (int a=1; a<26; ++a) { for (int b=0; b<26; ++b) { string t = ""; for (int i=0; i<(int)s.size(); ++i) { if (s[i] == ' ') { t += ' '; } else { t += char('a' + (a * (s[i] - 'a' + b)) % 26); } } if (t.find("this") != string::npos || t.find("that") != string::npos) { return t; } } } } int main() { int n; cin >> n; cin.ignore(); string s; while (n--) { getline(cin, s); cout << decode(s) << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, m, k, a, b; bool zly[100010]; int ok, u, v; vector<int> order, graf[100010]; set<pair<double, int> > se; int oks[100010], best_pos; double best_res; int main() { cin >> n >> m >> k; for (int i = 1; i <= k; i++) { cin >> a; zly[a] = true; } for (int i = 1; i <= m; i++) { cin >> a >> b; graf[a].push_back(b); graf[b].push_back(a); } for (int i = 1; i <= n; i++) { if (!zly[i]) { ok = 0; for (int j = 0; j < graf[i].size(); j++) { if (!zly[graf[i][j]]) ok++; } oks[i] = ok; se.insert(make_pair(double(ok) / double(graf[i].size()), i)); } } best_res = -1.; best_pos = 0; while (se.size() > 0) { if (se.begin()->first > best_res) { best_res = se.begin()->first; best_pos = order.size(); } v = se.begin()->second; order.push_back(v); se.erase(se.begin()); for (int i = 0; i < graf[v].size(); i++) { u = graf[v][i]; if (!zly[u]) { se.erase(make_pair(double(oks[u]) / double(graf[u].size()), u)); oks[u]--; se.insert(make_pair(double(oks[u]) / double(graf[u].size()), u)); } } zly[v] = true; } cout << order.size() - best_pos << endl; for (int i = best_pos; i < order.size(); i++) { cout << order[i] << " "; } cout << endl; }
4
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MOD = 1000000007; int main(){ ll n; cin >> n; string s; cin >> s; map<char, ll> key; for(ll i=0; i<n; i++){ key[s[i]] += 1; } ll sum = 1; for(auto x: key){ sum *= (x.second+1); sum %= MOD; } cout << sum-1<< endl; }
0
#include <bits/stdc++.h> using namespace std; bool compare(int a, int b) { return a > b; } int n, k, sum; int temp[5001]; double ans, all; int main() { scanf("%d%d", &n, &k); ans = 0; for (int i = 1; i <= n; i++) scanf("%d", &temp[i]), all += temp[i]; for (int i = 1; i <= n; ++i) { sum = 0; for (int j = i; j <= n; ++j) { sum += temp[j]; if (j - i + 1 >= k) { all = sum * 1.0 / (j - i + 1); if (all > ans) ans = all; } } } printf("%.15f\n", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int N; cin >> N; ll A[N]; for (int i = 0; i < N; i++) { cin >> A[i]; } sort(A,A+N); ll msum = 0; ll asum = 0; for (int i = 0; i < N; i++) { if (i < N / 2){ msum += A[i]; } else { asum += A[i]; } } ll ans = 0; if (N % 2 == 1) { if (A[N/2] - A[N/2 - 1] > A[N/2 + 1] - A[N/2]) { ans = ((asum - msum) * 2) - A[N/2] - A[N/2 + 1]; } else { msum += A[N/2]; asum -= A[N/2]; ans = (asum - msum) * 2 + A[N/2] + A[N/2 - 1]; } } else { ans = (asum * 2) - (msum * 2) - A[N/2] + A[N/2 - 1]; } cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; long long n, k, h, x, curHeight, secs; int main() { cin >> n >> h >> k; for (int i = 0; i < n; i++) { cin >> x; if (curHeight + x > h) { secs += curHeight / k; curHeight -= k * (curHeight / k); } if (curHeight + x > h) { secs++; curHeight = 0; } curHeight += x; } cout << secs + (curHeight + k - 1) / k << endl; }
2
#include<iostream> #include<cstring> #include<algorithm> using namespace std; int d[11], n, m, i, a, b, sum, e; int main() { while (true) { memset(d, 0, sizeof(d)); sum = 0; cin >> n >> m; if (!n) { break; } for (i = 0; i < n; i++) { cin >> a >> b; d[b] += a; } for (i = 10; i >= 0;i--){ e = min(d[i], m); m -= e; sum += i*(d[i] - e); } cout << sum << endl; } }
0
#include <bits/stdc++.h> using namespace std; const int NMax = 1700; const long long INF = 1000000007LL; int N, M, ID[NMax][NMax], ID1[NMax][NMax]; int A[NMax][NMax]; int F[NMax][NMax]; pair<int, int> Max[100], Min[100], Max1[100], Min1[100]; bool check(int first, int second) { return first > 0 && first <= N && second > 0 && second <= M && A[first][second] && !ID[first][second]; } void DFS(int a, int b, int c) { ID[a][b] = c; Max[c].first = max(Max[c].first, a); Max[c].second = max(Max[c].second, b); Min[c].first = min(Min[c].first, a); Min[c].second = min(Min[c].second, b); if (check(a - 1, b)) DFS(a - 1, b, c); if (check(a + 1, b)) DFS(a + 1, b, c); if (check(a, b - 1)) DFS(a, b - 1, c); if (check(a, b + 1)) DFS(a, b + 1, c); } bool check1(int first, int second, int z) { return first > 0 && first <= N && second > 0 && second <= M && ID1[first][second] == z && !F[first][second]; } int size[100]; void DFS(int a, int b, int c, int d) { F[a][b] = c; size[c]++; if (check1(a - 1, b, d)) DFS(a - 1, b, c, d); if (check1(a + 1, b, d)) DFS(a + 1, b, c, d); if (check1(a, b - 1, d)) DFS(a, b - 1, c, d); if (check1(a, b + 1, d)) DFS(a, b + 1, c, d); } bool check(int a, int b, int c) { return a > 0 && a <= N && b > 0 && b <= M && ID[a][b] == c; } int main() { scanf("%d%d", &N, &M); for (int i = 1; i <= N; i++) for (int j = 1; j <= M; j++) scanf("%d", &A[i][j]); int nn = 0; for (int i = 1; i <= N; i++) for (int j = 1; j <= M; j++) if (A[i][j] && !ID[i][j]) { Max[++nn] = make_pair(-1, -1); Min[nn] = make_pair(2000, 2000); DFS(i, j, nn); } for (int i = 1; i <= nn; i++) { Max1[i] = make_pair(-1, -1); Min1[i] = make_pair(2000, 2000); for (int j = Min[i].first; j <= Max[i].first; j++) { for (int k = Min[i].second; k <= Max[i].second; k++) if (ID[j][k] == i) { int t1 = j, t2 = k; while (check(t1 - 1, k, i)) t1--; int start = t1; t1 = j; while (check(t1 + 1, k, i)) t1++; int last = t1; int X = last - start + 1; while (check(j, t2 - 1, i)) t2--; start = t2; t2 = k; while (check(j, t2 + 1, i)) t2++; last = t2; int Y = last - start + 1; if ((X < 7 || Y < 7)) { ID1[j][k] = i; Max1[i].first = max(Max1[i].first, j); Min1[i].first = min(Min1[i].first, j); Max1[i].second = max(Max1[i].second, k); Min1[i].second = min(Min1[i].second, k); } } } } printf("%d\n", nn); int Q[100], long long = 0; for (int i = 1; i <= nn; i++) { int cc = 0; for (int j = Min1[i].first; j <= Max1[i].first; j++) for (int k = Min1[i].second; k <= Max1[i].second; k++) if (!F[j][k] && ID1[j][k] == i) { size[++cc] = 0; DFS(j, k, cc, i); } int ret = cc; for (int c = 1; c <= cc; c++) { if (size[c] < 18) { ret--; continue; } } Q[long long ++] = ret; } sort(Q, Q + long long); for (int i = 0; i < long long; i++) printf("%d ", Q[i]); puts(""); return 0; }
6
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1); const int N = 100005; struct Node { int cnt[11][10]; Node() { memset(cnt, 0, sizeof cnt); } } BIT[4][N]; int n; char s[N]; string g = "ATGC"; int getID[500]; void edit(Node &a, int &v, int &p) { for (int i = 1; i <= 10; i++) a.cnt[i][(p - 1) % i] += v; } void upd(int pos, int val, int ind) { for (int i = pos; i <= n; i += i & (-i)) edit(BIT[ind][i], val, pos); } int getSum(int &ind, int id, int &len, int &mod) { int ret = 0; for (int i = id; i; i -= i & (-i)) ret += BIT[ind][i].cnt[len][mod]; return ret; } int query(int &s, int &e, int &ind, int &len, int &mod) { return getSum(ind, e, len, mod) - getSum(ind, s, len, mod); } int q; char f[12]; int main() { scanf("%s", s); n = strlen(s); for (int i = 0; i < 4; i++) getID[g[i]] = i; for (int i = 0; i < n; i++) upd(i + 1, 1, getID[s[i]]); scanf("%d", &q); while (q--) { int t; scanf("%d", &t); if (t == 1) { int x; char c; scanf("%d %c", &x, &c); upd(x, -1, getID[s[x - 1]]); s[x - 1] = c; upd(x, 1, getID[c]); } else { int l, r; scanf("%d %d %s", &l, &r, f); int ans = 0; int Len = strlen(f); --l; for (int i = 0, m = l % Len; i < Len; i++, m = (m + 1) % Len) ans += query(l, r, getID[f[i]], Len, m); printf("%d\n", ans); } } return 0; }
3
#include <bits/stdc++.h> using namespace std; int N; char S[1000002]; int A_[1000001], B_[1000001]; int A[1000001], B[1000001]; long long FA[1000001], FB[1000001]; int main() { ios_base::sync_with_stdio(false); cin >> N >> (S + 1); for (int i = 1; i <= N; ++i) { A[i] = A[i - 1]; FA[i] = FA[i - 1]; B[i] = B[i - 1]; FB[i] = FB[i - 1]; if (S[i] == 'U') { A_[++A[i]] = i; FA[i] += i; } else { B_[++B[i]] = i; FB[i] += i; } } for (int i = 1; i <= N; ++i) { long long w; if (S[i] == 'U') { int ka = A[i] - A[0]; int kb = B[N] - B[i]; if (ka > kb) w = N - i + 1 + (FB[N] - FB[i] - FA[i - 1] + FA[A_[A[i] - kb - 1]]) * 2; else w = i + (FB[B_[B[i] + ka]] - FB[i] - FA[i] + FA[0]) * 2; } else { int ka = A[i - 1] - A[0]; int kb = B[N] - B[i - 1]; if (kb > ka) w = i + (FB[B_[B[i] + ka]] - FB[i] - FA[i] + FA[0]) * 2; else w = N - i + 1 + (FB[N] - FB[i - 1] - FA[i] + FA[A_[A[i] - kb]]) * 2; } cout << w; if (i < N) cout << ' '; } cout << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; class Point { public: long long x, y; }; int q[500][500] = {0}; bool cmp(Point a, Point b) { return a.x < b.x; } int main() { int n, m; cin >> n >> m; vector<Point> a(n), b(m); for (int i = 0; i < n; ++i) cin >> a[i].x >> a[i].y; sort(a.begin(), a.end(), cmp); for (int i = 0; i < m; ++i) cin >> b[i].x >> b[i].y; for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) { if (a[i].x == a[j].x) continue; for (int k = 0; k < m; ++k) if ((b[k].y - a[i].y) * (a[j].x - a[i].x) < (a[j].y - a[i].y) * (b[k].x - a[i].x)) if (b[k].x > a[i].x && b[k].x < a[j].x) q[i][j] += 2; else if (b[k].x == a[i].x || b[k].x == a[j].x) q[i][j]++; } int ans = 0; for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) for (int k = j + 1; k < n; ++k) { if (q[i][k] == q[i][j] + q[j][k]) ans++; } cout << ans << '\n'; return 0; }
4
#include <bits/stdc++.h> const long double eps = 1e-9; const double pi = acos(-1.0); const long long inf = 1e18; using namespace std; int n, m, a[200200], pls[200200], tin[200200], tout[200200], h[200200], timer = 0; int order[200200], hh = 0, p[200200]; vector<int> graf[200200]; vector<pair<int, int> > q; void dfs(int v, int prev) { tin[v] = ++timer; p[v] = prev; for (int i = 0; i < graf[v].size(); i++) { int next = graf[v][i]; if (next == prev) continue; h[next] = h[v] ^ 1; dfs(next, v); } tout[v] = timer; order[++hh] = v; } void rebuild() { for (int i = 0; i < q.size(); i++) pls[q[i].first] += q[i].second; q.clear(); for (int i = 1; i <= n; i++) { int v = order[i]; pls[v] -= pls[p[v]]; a[v] += pls[v]; } for (int i = 1; i <= n; i++) pls[i] = 0; } int main(int argc, const char* argv[]) { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i < n; i++) { int a, b; scanf("%d%d", &a, &b); graf[a].push_back(b); graf[b].push_back(a); } timer = 0; h[1] = 1; dfs(1, 0); reverse(order + 1, order + n + 1); while (m--) { int _type; scanf("%d", &_type); if (_type == 1) { int x, val; scanf("%d%d", &x, &val); q.push_back(make_pair(x, val)); if (q.size() >= 450) rebuild(); } if (_type == 2) { int v, res; scanf("%d", &v); res = a[v]; for (int i = 0; i < q.size(); i++) { int x = q[i].first; int val = q[i].second; if (tin[x] <= tin[v] && tout[v] <= tout[x]) { if (h[x] == h[v]) res += val; else res -= val; } } printf("%d\n", res); } } return 0; }
3
#include <bits/stdc++.h> using namespace std; template <typename T> inline T abs(T t) { return t < 0 ? -t : t; } const long long modn = 1000000007; inline long long mod(long long x) { return x % modn; } int n; int a[200009]; pair<int, long long> memo[200009][2]; int seen[200009][2]; const int ok = 0, imp = 1, a1 = 2; pair<int, long long> solve(int i, bool on2) { if (i < 0 || i >= n) return pair<int, long long>(ok, 0); if (i == 0 && on2) return pair<int, long long>(imp, 0); if (i == 0) return pair<int, long long>(a1, 0); pair<int, long long> &r = memo[i][on2]; if (seen[i][on2] == 2) return r; if (seen[i][on2] == 1) return r = pair<int, long long>(imp, 0); seen[i][on2] = 1; pair<int, long long> x = solve(i + a[i] * (on2 * 2 - 1), !on2); r = x; if (r.first != imp) r.second += a[i]; seen[i][on2] = 2; return r; } int main() { int i; scanf("%d", &n); for (i = 1; i < n; i++) scanf("%d", &a[i]); for (i = 0; i < n - 1; i++) { pair<int, long long> y = solve(i + 1, false); y.second += i + 1; if (y.first == a1) y.second += i + 1; if (y.first == imp) puts("-1"); else printf("%I64d\n", y.second); } }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 200000 + 10; int n, m, s[maxn], vis[maxn], ans[maxn]; vector<pair<int, int> > G[maxn]; int dfs(int u) { vis[u] = 1; pair<int, int> v; for (int i = 0, d; i < G[u].size(); ++i) { v = G[u][i]; if (!vis[v.first]) { d = dfs(v.first); ans[abs(v.second)] = v.second > 0 ? d : -d; s[u] += d; } } return s[u]; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &s[i]); s[0] += s[i]; } if (s[0]) return puts("Impossible"), 0; scanf("%d", &m); for (int i = 1, u, v; i <= m; ++i) { scanf("%d%d", &u, &v); G[u].push_back(pair<int, int>(v, i)); G[v].push_back(pair<int, int>(u, -i)); } puts("Possible"); dfs(1); for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]); return 0; }
6
#include<cstdio> #include<algorithm> using namespace std; typedef long long ll; const int N=1e5+10; ll n, a[N], b[N], d[N], sum=0, cnt=0, les=0; inline bool cmp( int x, int y ) { return x>y; } int main() { scanf( "%lld", &n ); for ( int i=1; i<=n; i++ ) scanf( "%lld", &a[i] ); for ( int i=1; i<=n; i++ ) scanf( "%lld", &b[i] ); for ( int i=1; i<=n; i++ ) { if ( a[i]<b[i] ) sum+=b[i]-a[i], ++les; if ( a[i]>b[i] ) d[++cnt]=a[i]-b[i]; } sort( d+1, d+cnt+1, cmp ); int p=0; while ( sum>0&&p<cnt ) sum-=d[++p]; if ( sum>0 ) printf( "-1" ); else printf( "%lld", les+p ); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int N = 200100; struct node { int x, y; } a[2100], b[2100]; struct edge { int next, to; } e[4000100]; long long jc[N], inv[N], ans, f[2100][40], g[2100][40]; int n, m, num, s, head[2100], q[2100], tot = 0, du[2100]; void tj() { s = (s >> 1) + (s & 1); } void add(int x, int y) { e[++tot].next = head[x]; head[x] = tot; e[tot].to = y; du[y]++; } long long ksm(long long x, int y) { long long res = 1; while (y) { if (y & 1) res = (res * x) % mod; x = (x * x) % mod; y >>= 1; } return res; } long long c(int x, int y) { return ((jc[x] * inv[y]) % mod * inv[x - y]) % mod; } void ycl() { jc[0] = 1; for (long long i = 1; i <= 200000; i++) jc[i] = (jc[i - 1] * i) % mod; inv[0] = 1; for (int i = 1; i <= 200000; i++) inv[i] = ksm(jc[i], mod - 2); } void topsort() { int i, j, x, y, fp = 1, rp = 0; for (i = 1; i <= num; i++) for (j = 1; j <= num; j++) if ((j != i) && (a[i].x <= a[j].x) && (a[i].y <= a[j].y)) add(i, j); for (i = 1; i <= num; i++) if (!du[i]) q[++rp] = i; while (fp <= rp) { x = q[fp++]; for (i = head[x]; i; i = e[i].next) { y = e[i].to; du[y]--; if (!du[y]) q[++rp] = y; } } } int main() { int i, j, k; scanf("%d%d%d%d", &n, &m, &num, &s); for (i = 1; i <= num; i++) { scanf("%d%d", &a[i].x, &a[i].y); if ((a[i].x == 1) && (a[i].y == 1)) { num--; tj(); --i; } } topsort(); for (i = 1; i <= num; i++) b[i] = a[i]; for (i = 1; i <= num; i++) a[i] = b[q[i]]; if ((a[num].x == n) && (a[num].y == m)) num--, tj(); ycl(); a[++num].x = n; a[num].y = m; for (i = 1; i <= num; i++) for (j = 0; j <= 21; j++) { f[i][j] = c(a[i].x + a[i].y - 2, a[i].x - 1); for (k = 1; k < i; k++) if ((a[k].x <= a[i].x) && (a[k].y <= a[i].y)) f[i][j] = (f[i][j] - (c(a[i].x - a[k].x + a[i].y - a[k].y, a[i].x - a[k].x) * g[k][j]) % mod + mod) % mod; if (j) g[i][j] = (f[i][j] - f[i][j - 1] + mod) % mod; else g[i][j] = f[i][j]; } i = 0; ans = 0; while (s != 1) { ans = (ans + (long long)s * g[num][i]) % mod; tj(); ++i; } if (i) ans = (ans + c(n + m - 2, n - 1) - f[num][i - 1] + mod) % mod; else ans = (ans + c(n + m - 2, n - 1)) % mod; ans = (ans * ksm(c(n + m - 2, n - 1), mod - 2)) % mod; cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main(){ int A,B,C; cin>>A>>B>>C; if(A+B>=C-1)cout<<B+C; else cout<<B+A+B+1; }
0
#include <bits/stdc++.h> using namespace std; int main() { int k,x; cin>>k>>x; for(int i=x-k+1;i<k+x;i++) cout<<i<<" "; }
0
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const long long int inf = 2e18; const long double eps = 1e-9; const int NN = 1e5 + 2; void solve() { cerr << fixed << setprecision(10); int i = 0, j = 0, k = 0, n = 0, m = 0; cin >> n; bool b1 = 0, b2 = 0; vector<long long int> x(n), y(n); for (i = 0; i < (n); i++) { cin >> x[i] >> y[i]; if (y[i] < 0) b1 = 1; if (y[i] > 0) b2 = 1; ; }; if (b1 && b2) { cout << -1 << '\n'; return; } if (b1) { for (i = 0; i < (n); i++) { y[i] *= -1; } } long double lo = eps, hi = inf / 4, r; for (i = 0; i < 200; i++) { r = (hi + lo) / 2; ; long double xc = -inf; bool is = 1; long double xc_l = inf; for (j = 0; j < n; j++) { long double diff = y[j] - 2 * r; if (diff > 0) { is = 0; break; } diff = 2 * y[j] * r - y[j] * y[j]; if (diff < 0) { is = 0; break; } long double d = sqrt(diff) < 0 ? 0 : sqrt(diff); xc = max(xc, x[j] - d); xc_l = min(xc_l, d + x[j]); }; if (!is) { lo = r + eps; continue; } if (xc_l >= xc) { hi = r; } else lo = r + eps; } cout << fixed << setprecision(15) << hi << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(0); int T = 1, tc; for (tc = 1; tc <= T; tc++) { solve(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int a[200005]; vector<pair<int, int> > b; int n, t; int cnt[200005]; bool p[200005]; bool check(int k) { int res = 0; for (int i = 0; i <= (int)b.size() - 1; ++i) { res += b[i].first / k; if (res >= t) return 1; } return 0; } int main() { scanf("%d%d", &n, &t); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1; i <= n; ++i) ++cnt[a[i]]; for (int i = 1; i <= n; ++i) if (!p[a[i]]) b.push_back(make_pair(cnt[a[i]], a[i])), p[a[i]] = 1; sort(b.begin(), b.end(), greater<pair<int, int> >()); int l = 0; int r = (n / t) + 1; while (l + 1 < r) { int m = (l + r) / 2; if (check(m)) l = m; else r = m; } for (int i = 0; i <= b.size() - 1; ++i) { while (b[i].first >= l) { printf("%d ", b[i].second); b[i].first -= l; --t; if (!t) return 0; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; int sgn(double a) { return a < -1e-8 ? -1 : a > 1e-8 ? 1 : 0; } struct node { double x, y, k; node(double x = 0, double y = 0, double k = 0) : x(x), y(y), k(k) {} bool operator<(const node &B) const { return sgn(x - B.x) ? x < B.x : sgn(y - B.y) ? y < B.y : k < B.k; } } ar[500005]; int cnt, n; double x[1005], y[1005]; int main() { scanf("%d", &n); for (int i = 1, a, b, c, d; i <= n; i++) { scanf("%d%d%d%d", &a, &b, &c, &d); x[i] = 1.0 * a / b, y[i] = 1.0 * c / d; double l = (x[i] * x[i] + y[i] * y[i]); x[i] /= l, y[i] /= l; } for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) { ar[cnt] = node(x[i] + x[j], y[i] + y[j], atan2(y[i] - y[j], x[i] - x[j])); if (ar[cnt].k < 0) ar[cnt].k += 3.1415926535897932384626433832795; if (ar[cnt].k >= 3.1415926535897932384626433832795) ar[cnt].k -= 3.1415926535897932384626433832795; cnt++; } sort(ar, ar + cnt); int c1 = 2, c2 = 1, c3 = 0; for (int i = 1; i < cnt; i++) { if (sgn(ar[i].x - ar[i - 1].x) || sgn(ar[i].y - ar[i - 1].y)) c3 = (c3 + 1ll * c2 * c1 - 1) % 1000000007, c2 = 1, c1 = 2; else if (sgn(ar[i].k - ar[i - 1].k) && sgn(ar[i].k - ar[i - 1].k - 3.1415926535897932384626433832795) && sgn(ar[i].k - ar[i - 1].k + 3.1415926535897932384626433832795)) c2 = 1ll * c2 * c1 % 1000000007, c1 = 2; else c1++; } c3 = (c3 + 1ll * c2 * c1 - 1) % 1000000007; c3 = (c3 - cnt) % 1000000007; printf("%d\n", (c3 + 1000000007) % 1000000007); }
5
#include<bits/stdc++.h> using namespace std; bool solve(string a,string b){ int pos=0; for(int i=0;i<b.length();++i){ if(a[pos]==b[i]) ++pos; if(pos==a.length()) return true; } return false; } int main(){ string s,t,s1,s2; cin>>s>>t; for(int i=0;i<s.size();++i){ if(i%2==0) s1+=s[i]; else s2+=s[i]; } cout<<(solve(s1,t)||solve(s2,t)?"Yes":"No")<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 1000000 + 5; char str[N << 1]; int a[N << 1]; bool flag[N << 1]; int len; int offset, pos; pair<int, int> que[N << 1]; int p1, p2; int sa[N << 1], rk[N << 1]; void da(char *s, int n, int m) { static int t1[N << 1], t2[N << 1], c[N << 1]; int *x = t1, *y = t2, i, j, k, p = 1; memset(c, 0, sizeof(c[0]) * m); for (i = 0; i < n; i++) c[x[i] = s[i]]++; for (i = 1; i < m; i++) c[i] += c[i - 1]; for (i = n - 1; i >= 0; i--) sa[--c[x[i]]] = i; for (k = 1; p < n; k <<= 1, m = p) { for (p = 0, i = n - k; i < n; i++) y[p++] = i; for (i = 0; i < n; i++) if (sa[i] >= k) y[p++] = sa[i] - k; memset(c, 0, sizeof(c[0]) * m); for (i = 0; i < n; i++) c[x[i]]++; for (i = 1; i < m; i++) c[i] += c[i - 1]; for (i = n - 1; i >= 0; i--) sa[--c[x[y[i]]]] = y[i]; for (swap(x, y), x[sa[0]] = 0, p = i = 1; i < n; i++) x[sa[i]] = y[sa[i]] == y[sa[i - 1]] && y[sa[i] + k] == y[sa[i - 1] + k] ? p - 1 : p++; } } int main() { scanf("%s", str + 1); len = strlen(str + 1); offset = 0; for (int i = 1; i <= len; ++i) offset += (str[i] == '(' ? 1 : -1); memcpy(str + len + 1, str + 1, len + 1); a[0] = 0; for (int i = 1; i <= len << 1; ++i) a[i] = a[i - 1] + (str[i] == '(' ? 1 : -1); p1 = p2 = 0; for (int i = 1; i <= len << 1; ++i) { while (p1 != p2 && que[p2 - 1].first > a[i]) --p2; que[p2++] = pair<int, int>(a[i], i); while (p1 != p2 && i - que[p1].second >= len) ++p1; if (i - len >= 0) { if (que[p1].first - a[i - len] >= offset || que[p1].first - a[i - len] >= 0) flag[i - len + 1] = true; } } da(str + 1, len << 1 | 1, 128); for (int i = 1; i <= len << 1; ++i) { if (sa[i] < len && flag[sa[i] + 1]) { pos = sa[i] + 1; break; } } while (offset < 0) printf("("), ++offset; for (int i = pos; i < pos + len; ++i) printf("%c", str[i]); while (offset > 0) printf(")"), --offset; puts(""); }
1
#include <bits/stdc++.h> using namespace std; const int N = 100010; struct road { int a, b, c, id; void read(int u) { scanf("%d %d %d", &a, &b, &c); id = u; } }; int n, k; vector<pair<int, int> > res; long long hd(const road &x, const road &y, const road &z) { return 1ll * x.a * y.b * z.c + 1ll * x.c * y.a * z.b + 1ll * x.b * y.c * z.a; } void go(const vector<road> &u, int k) { int n = u.size(); if (n <= k) { for (int i = 0; i < n; i++) { res.push_back(make_pair(u[i].id, -1)); } cout << "YES\n" << res.size() << endl; for (auto x : res) { cout << x.first << " " << x.second << endl; } exit(0); } if (!k) { return; } for (int cnt = 1; cnt <= k * k; cnt++) { int x = rand() % n; int y = rand() % (n - 1); x += (x == y); if (1ll * u[x].a * u[y].b == 1ll * u[x].b * u[y].a) { continue; } vector<road> now; for (int i = 0; i < n; i++) { if (hd(u[x], u[y], u[i]) != hd(u[x], u[i], u[y])) { now.push_back(u[i]); } } if (now.size() > n - n / (k + k)) { continue; } res.push_back(make_pair(u[x].id, u[y].id)); go(now, k - 1); res.pop_back(); } } int main() { srand(time(0)); scanf("%d %d", &n, &k); vector<road> a(n); for (int i = 0; i < n; i++) { a[i].read(i + 1); } go(a, k); puts("NO"); return 0; }
4
#include <bits/stdc++.h> #pragma GCC optimization("O3") #pragma GCC optimization("unroll-loops") using namespace std; template <class T> using vec = std::vector<T>; bool __hack = std::ios::sync_with_stdio(false); auto __hack1 = cin.tie(nullptr); template <class T> inline T mx(T arg) { return arg; } template <class T, class... Ts> inline typename common_type<T, Ts...>::type mx(T arg, Ts... args) { auto arg1 = mx(args...); return arg > arg1 ? arg : arg1; } struct Input { Input(istream &in) : in(&in) {} template <class T> T next() const { T x; *in >> x; return x; } int ni() const { return next<int>(); } template <class T> vec<T> nVec(int n) const { vec<T> v(n); for (int i = 0; i < n; ++i) { v[i] = next<T>(); } return v; } vec<int64_t> nvi64(int n) const { return nVec<int64_t>(n); } istream *in; }; Input in(cin); class Output { private: ostream *out; template <typename T> void printSingle(const T &value) { *out << value; } public: Output(ostream &out) : out(&out) {} inline void print() {} template <typename T, typename... Ts> inline void print(const T &f, const Ts &...args) { printSingle(f); if (sizeof...(args) != 0) { *out << ' '; print(args...); } } template <typename... Ts> inline void println(const Ts &...args) { print(args...); (*out) << '\n'; } template <typename... Ts> inline void operator()(const Ts &...args) { println(args...); } }; Output out(cout); namespace template_util { constexpr int bytecount(uint64_t x) { return x ? 1 + bytecount(x >> 8) : 0; } template <int N> struct bytetype {}; template <uint64_t N> struct minimal_uint : bytetype<bytecount(N)> {}; } // namespace template_util void solve(istream &inStream, ostream &outStream) { in = Input(inStream); out = Output(outStream); auto n = in.ni(); auto k = in.ni(); auto a = in.nvi64(n); auto cst = in.nvi64(n); if (k >= 2) { out(mx(0LL, accumulate(a.begin(), a.end(), int64_t(0)) - *min_element(cst.begin(), cst.end() - 1), a.back() - cst.back())); return; } if (k == 0) { int64_t res = 0; int64_t sum = 0; for (int i = n - 1; i >= 0; --i) { sum += a[i]; res = max(res, sum - cst[i]); } out(res); return; } int64_t res = 0; int64_t sum = 0; for (int i = n - 1; i > 0; --i) { sum += a[i]; res = max(res, sum - cst[i]); } sum = 0; for (int i = n - 2; i >= 0; --i) { sum += a[i]; res = max(res, sum - cst[i]); }; ; for (int i = n - 2; i >= 0; --i) { res = max(res, sum - cst[i]); }; ; sum += a.back(); for (int i = 0; i < (n); ++i) { res = max(res, sum - a[i] - cst[0]); } vec<int64_t> mi(n); mi[0] = cst[0]; for (int i = 1; i < n; ++i) { mi[i] = min(mi[i - 1], cst[i]); }; ; for (int i = (1); i < (n); ++i) { res = max(res, sum - mi[i - 1] - cst[i]); } out(res); } int main() { solve(cin, cout); return 0; }
5
#include <bits/stdc++.h> using namespace std; 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 = 65; const int mo = 1e9 + 7; const int MAXN = 2e6; long long js[100005]; int n, k; int fa[N]; struct node { int x, y, v; } a[N], p[N]; unsigned long long mi[70]; int m; map<int, int> idx, idy; int kx, ky; long long c[N], tmp[N], s[N]; unsigned long long suf[N]; struct zt { unsigned long long u; int cnt, val; } que[MAXN + 5]; map<unsigned long long, int> id[N]; bool vis[N]; int kth[N]; long long td[N]; int getfather(int x) { return fa[x] == x ? x : fa[x] = getfather(fa[x]); } bool cmp(node a, node b) { return kth[a.x] < kth[b.x]; } long long dec(long long x, long long y) { return x < y ? x - y + mo : x + y; } long long add(long long x, long long y) { return x + y >= mo ? x + y - mo : x + y; } int num[N]; int getcnt(unsigned long long v) { int ret = 0; for (; v; v -= v & -v) ret++; return ret; } int main() { n = get(); k = get(); for (int i = 1; i <= k; i++) { a[i].x = get(), a[i].y = get(), a[i].v = get() - 1; } srand(20010419); random_shuffle(a + 1, a + 1 + k); for (int i = 1; i <= k; i++) fa[i] = i; js[0] = 1; for (int i = 1; i <= 100000; i++) js[i] = js[i - 1] * i % mo; for (int i = 1; i <= k; i++) for (int j = i + 1; j <= k; j++) if (a[i].x == a[j].x || a[i].y == a[j].y) { int fx = getfather(i), fy = getfather(j); fa[fy] = fx; } mi[0] = 1; for (int i = 1; i <= k; i++) mi[i] = mi[i - 1] * 2; c[0] = 1; for (int tp = 1; tp <= k; tp++) if (getfather(tp) == tp) { m = 0; idx.clear(); idy.clear(); kx = ky = 0; for (int i = 1; i <= k; i++) if (getfather(i) == tp) { if (!idx[a[i].x]) idx[a[i].x] = ++kx; if (!idy[a[i].y]) idy[a[i].y] = ++ky; p[++m].x = idx[a[i].x]; p[m].y = idy[a[i].y]; p[m].v = a[i].v; } if (kx < ky) { swap(kx, ky); for (int i = 1; i <= m; i++) swap(p[i].x, p[i].y); } for (int i = 1; i <= kx; i++) vis[num[i] = i] = td[i] = 0; for (int i = 1; i <= m; i++) td[p[i].x] |= mi[p[i].y]; unsigned long long now = 0; for (int i = kx; i >= 1; i--) { int key = 0, cnt = 0; unsigned long long tp = 0; for (int j = 1; j <= kx; j++) if (!vis[j]) { if (!key || cnt > getcnt(now | td[j])) { tp = now | td[j]; cnt = getcnt(tp); key = j; } } vis[key] = 1; now = tp; kth[num[i] = key] = i; } sort(p + 1, p + 1 + m, cmp); suf[m + 1] = 0; for (int i = m; i >= 1; i--) suf[i] = suf[i + 1] | mi[p[i].y]; int he = 0, ta = 1; que[1].cnt = 0; que[1].val = 1; que[1].u = 0; int w = 1; for (int i = 1; i <= kx; i++) { for (int j = 0; j <= k; j++) id[j].clear(); int qw = w; while (w <= m && p[w].x == num[i]) w++; int qt = ta; while (he < qt) { he++; unsigned long long nu = que[he].u; long long nv = que[he].val; int cnt = que[he].cnt; int to; if (id[cnt][nu & suf[w]]) to = id[cnt][nu & suf[w]]; else { to = ++ta; que[to].cnt = cnt; que[to].u = nu; que[to].val = 0; id[cnt][nu & suf[w]] = to; } que[to].val = add(que[to].val, nv); for (int x = qw; x <= w - 1; x++) if ((nu & mi[p[x].y]) == 0) { unsigned long long u_ = (nu ^ mi[p[x].y]) & suf[w]; int to; if (id[cnt + 1][u_]) to = id[cnt + 1][u_]; else { to = ++ta; que[to].cnt = cnt + 1; que[to].u = nu ^ mi[p[x].y]; que[to].val = 0; id[cnt + 1][u_] = to; } que[to].val = add(que[to].val, nv * p[x].v % mo); } } } for (int i = 0; i <= k; i++) tmp[i] = s[i] = 0; for (; he < ta;) { he++; s[que[he].cnt] = add(s[que[he].cnt], que[he].val); } for (int i = 0; i <= k; i++) for (int j = 0; j <= k - i; j++) tmp[i + j] = add(tmp[i + j], c[i] * s[j] % mo); for (int i = 0; i <= k; i++) c[i] = tmp[i]; } long long ans = 0; for (int i = 0; i <= k; i++) ans = add(ans, c[i] * js[n - i] % mo); printf("%I64d\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000100; const int MAXM = 140000; const int INF = 0x7fffffff; const int mod = 1000000007; int len, a[500], b[500]; void add1(int x) { int i = 1; while (x) { if (b[i] == 9) ++i; else { ++b[i], --x; } len = max(len, i); } } void print() { for (int i = len; i >= 1; i--) printf("%d", b[i]); printf("\n"); } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; add1(a[1]); print(); for (int i = 2; i <= n; i++) { if (a[i] > a[i - 1]) add1(a[i] - a[i - 1]); else { int t = a[i] - a[i - 1], k = 1; while (true) { if (k > len) len = k; if (b[k] != 9 && t > 0) { b[k]++, t--; add1(t); break; } t += b[k], b[k] = 0; k++; } } print(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; void solve() { long long int n, d, i; cin >> n >> d; vector<pair<long long int, long long int> > a(n); for (i = 0; i < (n); i++) cin >> a[i].first >> a[i].second; sort(a.begin(), a.end()); long long int cnt = 0; long long int ans = 0; long long int left, right; right = 0; for (left = 0; left < n; left++) { while (right < n && a[right].first - a[left].first < d) { cnt += a[right].second; right++; } ans = max(ans, cnt); cnt -= a[left].second; } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
2
#include <bits/stdc++.h> using namespace std; int a[1000][1000]; int dis[40][1000][1000]; bool vis[40][1000][1000]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, k; cin >> n >> m >> k; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> a[i][j]; --a[i][j]; } } int dir1[4] = {-1, 1, 0, 0}; int dir2[4] = {0, 0, 1, -1}; for (int c = 0; c < k; ++c) { queue<pair<int, int>> q; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (a[i][j] == c) { q.push({i, j}); vis[c][i][j] = 1; } } } vector<int> vis_color(k); vis_color[c] = 1; while (!q.empty()) { auto p = q.front(); q.pop(); for (int r = 0; r < 4; ++r) { int x = p.first + dir1[r], y = p.second + dir2[r]; if (x < 0 || y < 0 || x >= n || y >= m || vis[c][x][y]) continue; vis[c][x][y] = 1; dis[c][x][y] = dis[c][p.first][p.second] + 1; q.push({x, y}); } if (!vis_color[a[p.first][p.second]]) { vis_color[a[p.first][p.second]] = 1; int color = a[p.first][p.second]; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (a[i][j] == color && !vis[c][i][j]) { vis[c][i][j] = 1; dis[c][i][j] = dis[c][p.first][p.second] + 1; q.push({i, j}); } } } } } } int q; cin >> q; vector<vector<int>> checked(n, vector<int>(m)); while (q--) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; --x1, --y1, --x2, --y2; if (x1 == x2 && y1 == y2) { cout << 0 << '\n'; continue; } int d = abs(x1 - x2) + abs(y1 - y2); for (int i = 0; i < k; ++i) { d = min(d, dis[i][x1][y1] + dis[i][x2][y2] + 1); } cout << d << '\n'; } }
6
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; vector<string> s(n); for (int i = 0; i < n; i++) cin >> s[i]; string t; cin >> t; int cntl = 0, cntr = 0; for (int i = 0; i < n; i++) { int l1 = min(s[i].length(), t.length()); bool flag = false; for (int j = 0; j < l1; j++) { if (s[i][j] == '?') { if (t[j] == 'a' || t[j] == 'z') continue; flag = true; break; } if (s[i][j] == t[j]) continue; else if (s[i][j] < t[j]) cntl++; else cntr++; flag = true; break; } if (flag) continue; if (s[i].length() == t.length()) continue; else if (t.length() > l1) cntl++; else cntr++; } for (int i = cntl + 1; i <= n + 1 - cntr; i++) { cout << i << (i + cntr == n + 1 ? "\n" : " "); } return 0; }
0
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) using namespace std; template <typename _T> inline void read(_T &f) { f = 0; _T fu = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') fu = -1; c = getchar(); } while (c >= '0' && c <= '9') { f = (f << 3) + (f << 1) + (c & 15); c = getchar(); } f *= fu; } template <typename T> void print(T x) { if (x < 0) putchar('-'), x = -x; if (x < 10) putchar(x + 48); else print(x / 10), putchar(x % 10 + 48); } template <typename T> void print(T x, char t) { print(x); putchar(t); } int f[20][1 << 12], sum[20][1 << 12], pre[1 << 12], v[20][1 << 12]; int a[20][20], b[20][2005], maxn[2005], pos[2005]; int T, n, m, k, ans; bool cmp(int x, int y) { return maxn[x] > maxn[y]; } inline int lowbit(int x) { return x & -x; } int main() { read(T); for (register int i = 1; i <= 12; i++) pre[1 << (i - 1)] = i; while (T--) { read(n); read(m); for (register int i = 1; i <= m; i++) maxn[i] = 0, pos[i] = i; for (register int i = 1; i <= n; i++) { for (register int j = 1; j <= m; j++) { read(b[i][j]); maxn[j] = max(maxn[j], b[i][j]); } } sort(pos + 1, pos + m + 1, cmp); k = min(n, m); for (register int i = 1; i <= n; i++) { for (register int j = 1; j <= k; j++) { a[i][j] = b[i][pos[j]]; } } memset(f, 0, sizeof(f)); memset(v, 0, sizeof(v)); for (register int i = 1; i <= k; i++) { for (register int z = 1; z <= n; z++) { int w = a[1][i]; for (register int j = 1; j < n; j++) a[j][i] = a[j + 1][i]; a[n][i] = w; for (register int j = 1; j < (1 << n); j++) { sum[i][j] = sum[i][j ^ lowbit(j)] + a[pre[lowbit(j)]][i]; v[i][j] = max(v[i][j], sum[i][j]); } } } for (register int i = 1; i <= k; i++) { for (register int j = 0; j < (1 << n); j++) { for (register int t = j; 1; t = (j & (t - 1))) { f[i][j] = max(f[i][j], f[i - 1][t] + v[i][j ^ t]); if (t == 0) break; } } } print(f[k][(1 << n) - 1], '\n'); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, k; char s[2005]; long long f[2005][2005]; long long g[2005][2005]; long long ans; int main() { scanf("%d%d", &n, &k); scanf("%s", s + 1); f[0][0] = g[0][0] = 1; for (int i = 1; i <= n; i++) for (int j = 0; j <= k; j++) { f[i][j] += g[i - 1][j] * (long long)(s[i] - 'a'); f[i][j] %= 1000000007; for (int pre = 1; pre <= i; pre++) { if (j < pre * (n - i + 1)) break; f[i][j] += f[i - pre][j - pre * (n - i + 1)] * (long long)('z' - s[i]); f[i][j] %= 1000000007; } g[i][j] = g[i - 1][j] + f[i][j]; g[i][j] %= 1000000007; } for (int i = 0; i <= n; i++) { ans += f[i][k]; ans %= 1000000007; } printf("%I64d\n", ans); }
5
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int main() { cin.tie(0); cin.sync_with_stdio(0); long long n; cin >> n; long long a[n / 2], ans[n]; for (int i = 0; i < n / 2; i++) cin >> a[i]; long long l = 0, r = a[0]; for (int i = 0; i < n / 2; i++) { if (a[i] >= r) { l = max(l, a[i] - r); r = a[i] - l; } else { r = min(r, a[i] - l); l = a[i] - r; } ans[i] = l; ans[n - i - 1] = r; } for (auto i : ans) cout << i << ' '; return 0; }
3
#include <bits/stdc++.h> using namespace std; template <class T, class U> void umin(T& x, const U& y) { x = min(x, (T)y); } template <class T, class U> void umax(T& x, const U& y) { x = max(x, (T)y); } template <class T, class U> void init(vector<T>& v, U x, size_t n) { v = vector<T>(n, (T)x); } template <class T, class U, typename... W> void init(vector<T>& v, U x, size_t n, W... m) { v = vector<T>(n); for (auto& a : v) init(a, x, m...); } int main(int argc, char** argv) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15); if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin); int T = 1; cin >> T; for (int tt = 1; tt <= T; tt++) { int n, k; cin >> n >> k; string s; cin >> s; bool ok = 1; vector<bool> canch(k, 0); for (int i = 0; i < k; i++) { int nr1 = 0, nr0 = 0; for (int j = i; j < n; j += k) { nr1 += s[j] == '1'; nr0 += s[j] == '0'; } if (nr1 > 0 && nr0 > 0) { ok = 0; break; } if (nr1 == 0 && nr0 == 0) canch[i] = 1; if (nr1) s[i] = '1'; if (nr0) s[i] = '0'; } int nr1 = 0, nr0 = 0; for (int i = 0; i < k; i++) { nr1 += s[i] == '1'; nr0 += s[i] == '0'; } if (nr1 * 2 > k || nr0 * 2 > k) ok = 0; cout << (ok ? "YES" : "NO") << endl; } if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock() * 1.0 / CLOCKS_PER_SEC << " sec\n"; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, k; string s; cin >> n >> k >> s; if (k == 0) { cout << s; return 0; } if (n == 1) { cout << 0; return 0; } if (s[0] != '1') s[0] = '1', k--; for (int i = 1; i < n && k; i++) if (s[i] != '0') s[i] = '0', k--; cout << s; }
2