func_code_string
stringlengths 59
71.4k
|
---|
#include <bits/stdc++.h> using namespace std; int h1, h2, n; int v[111]; char c[111]; int a[111]; int b[111]; const double eps = 1e-9; int main() { cin >> h1 >> h2 >> n; for (int i = (0); i < (n); i++) cin >> v[i] >> c[i] >> a[i] >> b[i]; int best = 0; for (int i = -200; i <= 200; i++) { bool good = 1; int sum = 0; set<int> used; int x1 = 0; int y1 = h1; int x2 = 100000; int y2 = h2 + i * 100; if (i % 2) y2 = 100 - h2 + i * 100; if (i > 0) { for (int j = (0); j < (i); j++) { int y = 100 + 100 * j; double x = double(x2 - x1) / (y2 - y1) * (y - y1); bool found = 0; for (int k = (0); k < (n); k++) if (a[k] <= x + eps && b[k] >= x - eps) { if (j % 2 == 0 && c[k] == T || j % 2 == 1 && c[k] == F ) { if (used.count(k)) good = 0; used.insert(k); sum += v[k]; found = 1; break; } } if (!found) good = 0; } } else if (i < 0) { for (int j = (0); j < (-i); j++) { int y = -100 * j; double x = double(x2 - x1) / double(y2 - y1) * (y - y1); bool found = 0; for (int k = (0); k < (n); k++) if (a[k] <= x + eps && b[k] >= x - eps) { if (j % 2 == 0 && c[k] == F || j % 2 == 1 && c[k] == T ) { if (used.count(k)) good = 0; used.insert(k); sum += v[k]; found = 1; break; } } if (!found) good = 0; } } if (good) best = max(best, sum); } cout << best << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n, i, j, b, c, a; cin >> a >> b >> c; long long int y = a + b + c; y--; cout << y << endl; } } |
#include <bits/stdc++.h> using namespace std; int n, k, s, a[5][205]; struct node { int id, x, y; }; vector<node> ans; inline void park() { for (int i = 0; i <= 2 * n - 1; ++i) if (a[1][i] && a[1][i] == a[0][i]) { ans.push_back(node{a[1][i], i / n ? 4 : 1, i % n + 1}); a[1][i] = 0; k--; } } inline bool judge() { park(); for (int i = 0; i <= 2 * n - 1; ++i) if (!a[1][i]) return 1; return 0; } inline void turn() { vector<node> t1, t2; int t = a[1][2 * n - 1]; for (int i = 2 * n - 1; i >= 1; --i) { a[1][i] = a[1][i - 1]; if (a[1][i]) { if (i > s) t1.push_back(node{a[1][i], i / n ? 3 : 2, i % n + 1}); else t2.push_back(node{a[1][i], i / n ? 3 : 2, i % n + 1}); } } a[1][0] = t; if (t) t2.push_back(node{t, 2, 1}); int sz1 = t1.size(), sz2 = t2.size(); for (int i = 0; i <= sz2 - 1; ++i) ans.push_back(t2[i]); for (int i = 0; i <= sz1 - 1; ++i) ans.push_back(t1[i]); } int main() { scanf( %d%d , &n, &k); for (int i = 0; i <= n - 1; ++i) scanf( %d , &a[0][i]); for (int i = 0; i <= n - 1; ++i) scanf( %d , &a[1][i]); for (int i = 2 * n - 1; i >= n; --i) scanf( %d , &a[1][i]); for (int i = 2 * n - 1; i >= n; --i) scanf( %d , &a[0][i]); if (!judge()) return puts( -1 ), 0; for (int i = 0; i <= 2 * n - 1; ++i) if (!a[1][i]) { s = i; break; } while (k) { turn(); park(); s = (s + 1) % (2 * n); } int sz = ans.size(); printf( %d n , sz); for (int i = 0; i < sz; ++i) { if (ans[i].x == 3 || ans[i].x == 4) ans[i].y = n - ans[i].y + 1; printf( %d %d %d n , ans[i].id, ans[i].x, ans[i].y); } return 0; } |
#include <bits/stdc++.h> #define int long long using namespace std; signed main(){ int t; cin >> t; while(t--){ int k, n, m; cin >> k >> n >> m; int a[n + 1], b[m + 1]; for(int i = 0;i < n;i++){ cin >> a[i]; } for(int i = 0;i < m;i++){ cin >> b[i]; } a[n] = 0x3f3f3f3f, b[m] = 0x3f3f3f3f; vector<int> ans; //答案序列 int l = k, ai = 0, bi = 0; //ai,bi分别表示两个数组分别删到了哪里,l为行数 bool flag = false; while(ans.size() < n + m){ if(a[ai] == 0){ ai++, ans.push_back(0); l++; continue; } else if(b[bi] == 0){ bi++, ans.push_back(0); l++; continue; } int p = min(a[ai], b[bi]); if(p == a[ai]){ ai++; } else{ bi++; } if(p > l){ flag = true; break; } ans.push_back(p); } if(flag){ cout << -1 << endl; } else{ for(int i = 0;i < ans.size();i++){ cout << ans[i] << ; //输出答案序列 } cout << endl; } } return 0; } |
#include <bits/stdc++.h> const int M = 2000; int T, n, x, ans, f0[M + 10], f1[M + 10]; inline int min(int x, int y) { return x < y ? x : y; } inline int max(int x, int y) { return x > y ? x : y; } void work() { for (int i = 0; i <= M; ++i) f0[i] = i; scanf( %d , &n), ans = 114514; while (n--) { scanf( %d , &x), memset(f1, 0x3f, sizeof f1); for (int i = 0; i <= M; ++i) i - x >= 0 && (f1[i] = min(f1[i], max(f0[i - x], i))), i + x <= M && (f1[i] = min(f1[i], f0[i + x])); memcpy(f0, f1, sizeof f0); } for (int i = 0; i <= M; ++i) ans = min(ans, f0[i]); printf( %d n , ans); } int main() { scanf( %d , &T); while (T--) work(); return 0; } |
#include <bits/stdc++.h> using namespace std; const int L = 332748118, M = 998244353, N = 1e6 + 5; int l[N]; int ksm(int a, long long b) { int an = 1; while (b) { if (b & 1) an = 1ll * an * a % M; a = 1ll * a * a % M; b >>= 1; } return an; } int main() { int n, c = 1, a = 0, b = 0, f = 1, s, d = 1, e, g, h; scanf( %d , &n); e = s = ksm(L, n); h = g = ksm(3, 1ll * n * n); l[1] = 1; for (int i = 1; i <= n; i++) { if (i != 1) l[i] = (M - 1ll * M / i * l[M % i]) % M; c = 1ll * (n - i + 1) * l[i] % M * c % M; d = 1ll * d * s % M; e = 3ll * e % M; h = 3ll * h * s % M; a = (a + 1ll * f * c % M * h % M) % M; b = (b + 1ll * f * c * d % M * (ksm(1 - e, n) - 1) % M) % M; f = -f; } printf( %lld , (((a << 1) + 3ll * g * b) % M + M) % M); } |
#include <bits/stdc++.h> using namespace std; int main() { int n, k, d; cin >> n >> k >> d; vector<int> U(k, 0); for (int i = 0; i < k; i++) U[i] = i + 1; vector<int> V(d - 1, 0); for (int i = 0; i < d - 1; i++) V[i] = i + 1; vector<long long> dp2(n + 1, 0); vector<long long> dp1(n + 1, 0); dp2[0] = 1; dp1[0] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < U.size(); j++) { if (i + U[j] <= n) dp2[i + U[j]] = (dp2[i + U[j]] % (long long)1000000007 + dp2[i] % (long long)1000000007) % (long long)1000000007; } } for (int i = 0; i < n; i++) { for (int j = 0; j < V.size(); j++) { if (i + V[j] <= n) dp1[i + V[j]] = (dp1[i + V[j]] % (long long)1000000007 + dp1[i] % (long long)1000000007) % (long long)1000000007; } } cout << (dp2[n] - dp1[n] + (long long)1000000007) % (long long)1000000007 << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; struct triple { int x, y, z; }; int n, x1, y11, x2, y22; string s[9]; int main() { cin >> n; while (n--) { for (__typeof(8) i = 1; i <= 8; i++) cin >> s[i], s[i] = # + s[i]; bool tmp = false; for (__typeof(8) i = 1; i <= 8; i++) { for (__typeof(8) j = 1; j <= 8; j++) if (s[i][j] == K ) { if (tmp == false) { tmp = true; x1 = i; y11 = j; } else { x2 = i; y22 = j; } } } tmp = false; for (__typeof(8) i = 1; i <= 8; i++) for (__typeof(8) j = 1; j <= 8; j++) if (tmp == false && s[i][j] != # ) { int tx1 = abs(i - x1), ty1 = abs(j - y11), tx2 = abs(i - x2), ty2 = abs(j - y22); if (tx1 % 2 == 0 && ty1 % 2 == 0 && tx2 % 2 == 0 && ty2 % 2 == 0 && (tx1 / 2 + ty1 / 2) % 2 == 0 && (tx2 / 2 + ty2 / 2) % 2 == 0 && (max(tx1 / 2, ty1 / 2) + max(tx2 / 2, ty2 / 2)) % 2 == 0) { cout << YES n ; tmp = true; } } if (tmp == false) cout << NO n ; } } |
#include <bits/stdc++.h> using namespace std; int n, b; int t[200005], d[200005]; queue<pair<int, pair<int, int> > > Q; long long ans[200005]; int main() { int i, j; scanf( %d , &n); scanf( %d , &b); for (i = 1; i <= n; i++) { scanf( %d , &t[i]); scanf( %d , &d[i]); } memset(ans, -1, sizeof(ans)); long long curr = 0; i = 1; while (1) { if (i <= n && Q.size() == 0) { Q.push({i, {t[i], d[i]}}); i++; } if (Q.size() == 0) break; pair<int, pair<int, int> > now = Q.front(); Q.pop(); curr = max(curr, (long long)now.second.first); long long en = curr + now.second.second; ans[now.first] = en; while (i <= n && Q.size() < b) { Q.push({i, {t[i], d[i]}}); i++; } while (i <= n && t[i] < en) { i++; } curr = en; } for (i = 1; i <= n; i++) printf( %lld , ans[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; constexpr int SIZE = (int)1e6; int n, k, from, to; vector<int> g[SIZE], h[SIZE]; vector<int> ord; vector<bool> check; string ans; int component[SIZE]; int comp; void dfs1(int v) { check[v] = true; for (int i : g[v]) { if (!check[i]) { dfs1(i); } } ord.push_back(v); } void dfs2(int v) { check[v] = true; component[v] = comp; for (int i : h[v]) { if (!check[i]) { dfs2(i); } } } int main() { cin >> n >> k; for (int j = 0; j < 2; j++) { cin >> from; from--; for (int i = 1; i < n; i++) { cin >> to; to--; g[from].push_back(to); h[to].push_back(from); from = to; } } check.resize(n, false); for (int i = 0; i < n; i++) { if (!check[i]) { dfs1(i); } } for (int i = 0; i < n; i++) { check[i] = false; } for (int i = ord.size() - 1; i >= 0; i--) { if (!check[ord[i]]) { dfs2(ord[i]); comp++; } } if (comp < k) { cout << NO ; return 0; } ans.resize(n); cout << YES << endl; for (int i = 0; i < ans.size(); i++) { if (k - 1 < component[i]) { ans[i] = char( a + k - 1); } else { ans[i] = char( a + component[i]); } } cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; long long p, k, ans, cnt, num[1000005], check[1000005], a, now, fac[100005]; int main() { fac[0] = fac[1] = 1; ans = 1; scanf( %lld%lld , &p, &k); if (k == 0) { for (int i = 1; i < p; i++) { ans *= p; ans %= 1000000007; } printf( %lld , ans); return 0; } if (k == 1) { for (int i = 0; i < p; i++) { ans *= p; ans %= 1000000007; } printf( %lld , ans); return 0; } check[0] = 1; for (int i = 1; i < p; i++) { a = (k * i) % p; num[i] = a; } for (int i = 1; i < p; i++) { if (!check[i]) { now = i; while (!check[now]) { check[now] = 1; now = num[now]; cnt++; } } if (cnt > 1) ans *= p; cnt = 0; ans %= 1000000007; } printf( %lld , ans); } |
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int main() { int t; cin >> t; while (t--) { long long n; cin >> n; long long tmp = sqrt(n); vector<long long> ans; ans.push_back(0); for (long long i = 1; i <= tmp; i++) { ans.push_back(i); if (n / i != i) { ans.push_back(n / i); } } sort(ans.begin(), ans.end()); cout << ans.size() << endl; for (long long v : ans) cout << v << ; cout << endl; } } |
#include <bits/stdc++.h> using namespace std; int is_prime(long long int n) { for (long long int i = 2; i * i <= n; i++) { if (n % i == 0) return 0; } return 1; } int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int a, b, c, d, k; cin >> a >> b >> c >> d >> k; a = (a + c - 1) / c; b = (b + d - 1) / d; if (a + b <= k) cout << a << << b << endl; else cout << -1 << endl; } return 0; } |
#include<iostream> #include<cmath> using namespace std; long long l,r,d; int main() { int t; cin >> t; while(t--) { scanf( %lld %lld %lld ,&l,&r,&d); if(min(l,r) * d >= max(l,r) - min(l,r)) cout << YES << endl; else cout << NO << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; char st[100000 + 123]; bool inm[256]; int main() { scanf( %s , st); memset(inm, 0, sizeof(inm)); inm[ A ] = true; inm[ H ] = true; inm[ I ] = true; inm[ M ] = true; inm[ O ] = true; inm[ T ] = true; inm[ U ] = true; inm[ V ] = true; inm[ W ] = true; inm[ X ] = true; inm[ Y ] = true; int r = strlen(st) - 1; int l = 0; bool flag = true; while (l <= r) { if (st[l] != st[r] || !inm[st[l]]) { flag = false; break; } l++; r--; } if (flag) puts( YES ); else puts( NO ); return 0; } |
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize( Ofast ) #pragma GCC target( avx,avx2,fma ) long long niz[300005]; long long ima[605][605]; long long res[300005]; long long dp[300005]; struct strukt { int a, b, ind; } query[300005]; bool cmp(strukt a, strukt b) { return a.b < b.b; } const int k = 600; int main() { ios_base::sync_with_stdio(false); cout.precision(10); cout << fixed; int n; cin >> n; for (int i = 1; i <= n; i++) cin >> niz[i]; int q; cin >> q; for (int i = 1; i <= q; i++) { cin >> query[i].a >> query[i].b; query[i].ind = i; } sort(query + 1, query + 1 + q, cmp); for (int i = 1; i <= q; i++) { if (query[i].b > k) { long long sum = 0; int a = query[i].a; int b = query[i].b; for (int j = a; j <= n; j += b) { sum += niz[j]; } res[query[i].ind] = sum; } else if (query[i].b == query[i - 1].b) { res[query[i].ind] = dp[query[i].a]; } else { int b = query[i].b; for (int j = n; j >= 1; j--) { if (j + b > n) dp[j] = niz[j]; else dp[j] = niz[j] + dp[j + b]; } res[query[i].ind] = dp[query[i].a]; } } for (int i = 1; i <= q; i++) cout << res[i] << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; int m; cin >> n >> m; int l; int r; int arr[n]; int pos = 0; int neg = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; if (arr[i] == -1) { neg++; } else { pos++; } } int mn = min(neg, pos); for (int i = 0; i < m; i++) { cin >> l >> r; int c = 0; if (l == r) { cout << 0 << endl; c++; } if (c == 0) { if ((abs(l - r) + 1) / 2 <= mn && (abs(l - r) + 1) % 2 == 0) { cout << 1 << endl; } else { cout << 0 << endl; } } } return 0; } |
#include <bits/stdc++.h> using namespace std; const int MX = 1147483646; const long long MX2 = 9223372036854775800; const int MOD = 1000000007; int p[30]; int find(int n) { if (p[n] < 0) return n; return p[n] = find(p[n]); } void uni(int e1, int e2) { e1 = find(e1); e2 = find(e2); if (e1 == e2) return; p[e1] = e2; return; } int main() { int n; scanf( %d , &n); char s1[100011], s2[100011]; vector<pair<int, int> > ans; fill(p, p + 30, -1); scanf( %s %s , s1, s2); int a1, a2; for (int i = 0; i < n; i++) { a1 = find(int(s1[i] - a )); a2 = find(int(s2[i] - a )); if (a1 != a2) { ans.push_back({a1, a2}); uni(a1, a2); } } printf( %d n , ans.size()); for (pair<int, int> x : ans) { printf( %c %c n , x.first + a , x.second + a ); } return 0; } |
#include <bits/stdc++.h> using namespace std; void solve() { int i, j, k, n, m, ans = 0, cnt = 0, sum = 0; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; vector<int> res; res.push_back(a[0]); int last = a[0], flag = 1; for (int i = 1; i < n - 1;) { if (abs(a[i] - last) + abs(a[i] - a[i + 1]) > abs(last - a[i + 1])) { res.push_back(a[i]); last = a[i]; i++; } else i++; } if (flag) res.push_back(a[n - 1]); cout << res.size() << n ; for (auto x : res) cout << x << ; cout << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { solve(); } return 0; } |
#include <bits/stdc++.h> using namespace std; bool valid(int i, int j, int n, int m) { return i >= 0 && i < n && j >= 0 && j < m; } void solve() { int n, m; int dir[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; cin >> n >> m; char a[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cin >> a[i][j]; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] == B ) { for (int p = 0; p < 4; p++) { if (valid(i + dir[p][0], j + dir[p][1], n, m) && a[i + dir[p][0]][j + dir[p][1]] == . ) a[i + dir[p][0]][j + dir[p][1]] = # ; } } } } bool visited[n][m]; memset(visited, false, sizeof(visited)); queue<pair<int, int> > q; if (a[n - 1][m - 1] == . ) { q.push(make_pair(n - 1, m - 1)); visited[n - 1][m - 1] = true; } while (!q.empty()) { pair<int, int> temp = q.front(); q.pop(); for (int p = 0; p < 4; p++) { int x = temp.first + dir[p][0]; int y = temp.second + dir[p][1]; if (valid(x, y, n, m)) { if (!visited[x][y] && a[x][y] != # ) { q.push(make_pair(x, y)); visited[x][y] = true; } } } } bool ans = true; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] == G && visited[i][j] != true) { ans = false; break; } else if (a[i][j] == B && visited[i][j] != false) { ans = false; break; } } } cout << (ans ? Yes : No ); } int main() { int t, c = 0; cin >> t; while (t--) { solve(); cout << endl; } } |
#include <bits/stdc++.h> using namespace std; const int maxn = 2e3 + 5; const int md = 1e9 + 7; string mat[2], pat; long long n, m, dp[2][maxn][2], pr[2][maxn][2], re, is[2][maxn][maxn], ris[2][maxn][maxn]; long long sum(long long a, long long b) { return (a + b) % md; } long long mul(long long a, long long b) { return (a * b) % md; } void solve() { memset(dp, 0, sizeof dp); memset(is, 0, sizeof is); memset(ris, 0, sizeof ris); for (int r = 0; r < 2; ++r) { for (int j = 0; j < m; ++j) { is[r][0][j] = mat[r][0] == pat[j]; } for (int i = 1; i < n; ++i) { is[r][i][0] = mat[r][i] == pat[0]; for (int j = 1; j < m; ++j) { if (mat[r][i] == pat[j]) is[r][i][j] = is[r][i - 1][j - 1] + 1; } } for (int i = n - 1; i >= 0; i--) { ris[r][i][0] = mat[r][i] == pat[0]; for (int j = 1; j < m; ++j) { if (mat[r][i] == pat[j]) ris[r][i][j] = ris[r][i + 1][j - 1] + 1; } } } for (int i = 0; i < n; ++i) { memcpy(pr, dp, sizeof dp); memset(dp, 0, sizeof dp); dp[0][0][0] = (mat[0][i] == pat[0]); dp[1][0][0] = (mat[1][i] == pat[0]); for (int j = 1; j < m; ++j) { for (int r = 0; r < 2; ++r) if (mat[r][i] == pat[j]) { dp[r][j][0] = pr[r][j - 1][0]; dp[r][j][0] = sum(dp[r][j][0], pr[r][j - 1][1]); int x = j + 1; if (x % 2 == 0 && x / 2 > 1 && i >= x / 2 - 1) { x /= 2; dp[r][j][1] = sum(dp[r][j][1], (is[r][i][j] >= x) * (ris[r ^ 1][i - x + 1][j - x] >= x)); } dp[r][j][1] += dp[r ^ 1][j - 1][0]; } } for (int j = 0; j < m; ++j) { if (j == m - 1) { re = sum(re, dp[0][j][0] + dp[0][j][1] + dp[1][j][1] + dp[1][j][0]); continue; } for (int r = 0; r < 2; ++r) { int x = m - 1 - j; if (x % 2) continue; x /= 2; if (x == 1) continue; int y = i + x; if (y >= n) continue; re = sum(re, dp[r][j][0] * (is[r][y][j + x] >= x) * (ris[r ^ 1][i + 1][m - 1] >= x)); re = sum(re, dp[r][j][1] * (is[r][y][j + x] >= x) * (ris[r ^ 1][i + 1][m - 1] >= x)); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); for (int i = 0; i < 2; ++i) { cin >> mat[i]; } n = mat[0].size(); cin >> pat; m = pat.size(); if (m == 1) { for (auto u : mat[0]) { if (u == pat[0]) { re = re + 1; } } for (auto u : mat[1]) { if (u == pat[0]) { re = re + 1; } } cout << re << n ; return 0; } if (m == 2) { for (int r = 0; r < 2; ++r) { for (int i = 0; i < n; ++i) { if (i > 0 && mat[r][i] == pat[1] && mat[r][i - 1] == pat[0]) { re = re + 1; } if (i < n - 1 && mat[r][i] == pat[1] && mat[r][i + 1] == pat[0]) { re = re + 1; } if (mat[r][i] == pat[1] && mat[r ^ 1][i] == pat[0]) { re = re + 1; } } } cout << re << n ; return 0; } solve(); for (int r = 0; r < 2; ++r) { reverse(mat[r].begin(), mat[r].end()); } solve(); cout << re << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10, mod = 1e9 + 7, INF = 0x3f3f3f3f; int main() { int T; cin >> T; while (T--) { int n, k; cin >> n >> k; int mn = INF, mx = -1; for (int i = 1; i <= n; ++i) { int x; scanf( %d , &x); mn = min(mn, x); mx = max(mx, x); } int m = (mx + mn) >> 1; printf( %d n , max(m - mn, mx - m) <= k ? mn + k : -1); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { #ifdef DEBUG freopen( a.in , r , stdin); #endif ios_base::sync_with_stdio(false), cin.tie(0); int T; cin >> T; while (T--) { int a[2]; cin >> a[0] >> a[1]; string s; cin >> s; int n = a[0] + a[1]; vector<pair<int, int>> x; for (int i = 0; i < n / 2; ++i) { if (s[i] == ? && s[n - 1 - i] == ? ) { x.emplace_back(i, n - 1 - i); } else if (s[i] == ? ) { a[s[n - 1 - i] - 0 ] -= 2; s[i] = s[n - 1 - i]; } else if (s[n - 1 - i] == ? ) { a[s[i] - 0 ] -= 2; s[n - 1 - i] = s[i]; } else { if (s[i] != s[n - 1 - i]) { a[0] = -1; } else { a[s[i] - 0 ] -= 2; } } } if (a[0] < 0 || a[1] < 0) { cout << -1 << n ; continue; } for (int i = 0; i < x.size(); ++i) { if (a[0] >= 2) { s[x[i].first] = s[x[i].second] = 0 ; a[0] -= 2; } else if (a[1] >= 2) { s[x[i].first] = s[x[i].second] = 1 ; a[1] -= 2; } else { a[0] = -1; } } if (a[0] < 0 || a[1] < 0) { cout << -1 << n ; continue; } if ((n & 1) && s[n / 2] != ? ) a[s[n / 2] - 0 ]--; if ((n & 1) && s[n / 2] == ? ) s[n / 2] = a[0] ? 0 : 1 ; if (a[0] < 0 || a[1] < 0) cout << -1 << n ; else cout << s << n ; } return 0; } |
#include <bits/stdc++.h> int main() { int n, k, i, j, swap; scanf( %d %d , &n, &k); int ara[n + 1][3]; for (i = 1; i <= n; i++) { scanf( %d , &ara[i][1]); ara[i][2] = i; } for (i = 1; i <= n; i++) { for (j = 1; j <= (n - i); j++) { if (ara[j][1] > ara[j + 1][1]) { swap = ara[j][1]; ara[j][1] = ara[j + 1][1]; ara[j + 1][1] = swap; swap = ara[j][2]; ara[j][2] = ara[j + 1][2]; ara[j + 1][2] = swap; } } } int sum = 0, p = 0; for (i = 1; i <= n; i++) { sum = sum + ara[i][1]; if (sum <= k) p++; else break; } if (p == 0) printf( 0 n ); else { printf( %d n , p); for (i = 1; i <= p; i++) printf( %d , ara[i][2]); } } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int p[n + 1]; for (int i = 1; i <= n; i++) cin >> p[i]; bool done[n + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) done[j] = false; int start = i; while (1) { if (done[start] == true) { cout << start << ; break; } done[start] = true; start = p[start]; } } return 0; } |
#include <bits/stdc++.h> using namespace std; const int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1}; const double EPS = 1e-8; const double PI = acos(-1.0); int popcount(int n) { return __builtin_popcount(n); } int popcount(long long n) { return __builtin_popcountll(n); } template <class T> int SIZE(T a) { return a.size(); } template <class T> string IntToString(T num) { string res; stringstream ss; ss << num; return ss.str(); } template <class T> T StringToInt(string str) { T res = 0; for (int i = 0; i < SIZE(str); i++) res = (res * 10 + str[i] - 0 ); return res; } template <class T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template <class T> void PrintSeq(T &a, int sz) { for (int i = 0; i < sz; i++) { cout << a[i]; if (sz == i + 1) cout << endl; else cout << ; } } bool EQ(double a, double b) { return abs(a - b) < EPS; } void fastStream() { cin.tie(0); std::ios_base::sync_with_stdio(0); } vector<string> split(string str, char del) { vector<string> res; for (int i = 0, s = 0; i < SIZE(str); i++) { if (str[i] == del) { if (i - s != 0) res.push_back(str.substr(s, i - s)); s = i + 1; } else if (i == SIZE(str) - 1) { res.push_back(str.substr(s)); } } return res; } int froms[1000001]; int tos[1000001]; vector<pair<int, int> > G[1000001]; bool used[1000001]; void dfs(int pprv, int prv, int now) { used[now] = true; for (int i = 0; i < (int)G[now].size(); i++) { int to = G[now][i].first; int en = G[now][i].second; if (!used[to]) { dfs(prv, en, to); } } if (prv != -1 && pprv != -1) cout << pprv << << prv << endl; } int main() { fastStream(); int N; cin >> N; for (int i = 0; i < N - 1; i++) { cin >> froms[i] >> tos[i]; G[froms[i]].push_back(make_pair(tos[i], i + 1)); G[tos[i]].push_back(make_pair(froms[i], i + 1)); } cout << N - 1 << endl; for (int i = 0; i < N - 1; i++) cout << 2 << << froms[i] << << tos[i] << endl; for (int i = 0; i < N; i++) { if (G[i + 1].size() == 1) { dfs(-1, -1, i + 1); break; } } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long x, val, i, j, k, n, m, val2; vector<pair<long long, long long> > ans; cin >> x; for (n = 1; n <= (3000000); n++) { val = x; val -= (n - 1) * n * (2 * n - 1) / 6; val += n * n * (n - 1) / 2; val2 = n * n - ((n - 1) * n) / 2; if (val % val2 == 0) { val2 = val / val2; if (val2 >= n) { ans.push_back(make_pair(n, val2)); if (n != val2) ans.push_back(make_pair(val2, n)); } } } printf( %d n , ans.size()); sort(ans.begin(), ans.end()); for (i = 0; i < ans.size(); i++) printf( %lld %lld n , ans[i].first, ans[i].second); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int n, m, f[N], vis[N], cnt; int find(int x) { return x == f[x] ? x : f[x] = find(f[x]); } struct node { int x, y, w; } a[N]; bool cmp(node a, node b) { return a.w < b.w; } int main() { while (cin >> n >> m) { int ans = 0; for (int i = 1; i <= n; i++) f[i] = i; for (int i = 1; i <= m; i++) { int x, y, k; cin >> x >> y >> k; a[i].x = x; a[i].y = y; a[i].w = k; } sort(a + 1, a + 1 + m, cmp); int mx = 0; for (int i = 1; i <= m;) { int j = i; while (a[i].w == a[j].w) j++; cnt = 0; for (int k = i; k < j; k++) if (find(a[k].x) != find(a[k].y)) cnt++; for (int k = i; k < j; k++) { if (find(a[k].x) != find(a[k].y)) { f[find(a[k].x)] = find(a[k].y); cnt--; } } ans += cnt; i = j; } cout << ans << endl; } } |
#include <bits/stdc++.h> using namespace std; long long n, m; long long x[105]; double f[105][105525]; double sum[105525]; inline void read() { cin >> n >> m; for (long long i = 0; i <= n - 1; i++) { cin >> x[i]; } } inline void solve() { long long ms = n * m; if (m == 1) { puts( 1 ); return; } f[0][0] = 1; for (long long i = 0; i <= n - 1; i++) { for (long long s = 0; s <= ms; s++) { if (s) { sum[s] = sum[s - 1] + f[i][s]; } else { sum[s] = f[i][s]; } if (s > m) { f[i + 1][s] = sum[s - 1] - sum[s - m - 1]; } else { f[i + 1][s] = sum[s - 1]; } if (s >= x[i]) { f[i + 1][s] -= f[i][s - x[i]]; } f[i + 1][s] /= (m - 1); } } long long score = 0; for (long long i = 0; i <= n - 1; i++) { score += x[i]; } double prob = 0; for (long long s = 0; s <= score - 1; s++) { prob += f[n][s]; } prob *= (m - 1); prob += 1.0; cout.precision(20); cout << fixed << prob; } int main() { ios_base::sync_with_stdio(false); cin.tie(); read(); solve(); return 0; } |
#include <bits/stdc++.h> using std::cin; using std::cout; using std::fixed; using std::make_pair; using std::max; using std::min; using std::pair; using std::set; using std::sort; const long long base = (long long)(5 * 1e8 + 5); const long long coefficient = (long long)(4 * 1e9); long long convertCoordinate(long long x, long long y) { return (x + base) * coefficient + (y + base); } pair<long long, long long> convertCoordinateBack(long long pos) { long long x = pos / coefficient - base - base; long long y = pos % coefficient - base - base; return make_pair(x, y); } const int MaxN = 200000 + 9; int n, k, l, r, used; long long points[MaxN]; long long x, y, now; set<long long> waitList; set<pair<long long, long long> > ans; set<long long>::iterator it; set<pair<long long, long long> >::iterator pit; int main() { waitList.clear(); ans.clear(); scanf( %d%d , &n, &k); if (k >= n) return cout << -1 n , 0; for (int i = 1; i <= n; i++) { scanf( %I64d%I64d , &x, &y); points[i] = convertCoordinate(x, y); } sort(points + 1, points + n + 1); for (int i = 1; i <= min(k + 2, n); i++) { for (int j = max(n - k - 2, 1); j <= n; j++) { waitList.insert(points[i] + points[j]); } } for (it = waitList.begin(); it != waitList.end(); it++) { l = 1; r = n; used = 0; while (l <= r) { now = points[l] + points[r]; if (now < (*it)) l++, used++; else if (now > (*it)) r--, used++; else l++, r--; } if (used <= k) ans.insert(convertCoordinateBack(*it)); } if (ans.size() == 0) printf( 0 n ); else { printf( %d n , ans.size()); for (pit = ans.begin(); pit != ans.end(); pit++) { printf( %.2lf %.2lf n , (double)(*pit).first * 0.5, (double)(*pit).second * 0.5); } } } |
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t; string s; cin >> s; long long int n = s.length(); long long int dp[s.length() + 5][27]; for (int i = 0; i <= s.length(); i++) { for (int j = 0; j < 27; j++) dp[i][j] = 0; } for (int i = 0; i < s.length(); i++) { if (i != 0) { for (int j = 0; j < 27; j++) { dp[i + 1][j] = dp[i][j]; } } dp[i + 1][s[i] - a ]++; } long long int ans = 1000000000000000000; for (char c = a ; c <= z ; c++) { long long int low = 1, high = n; long long int mid; while (low <= high) { mid = (low + high) / 2; bool f = 1; for (int i = mid; i <= n; i++) { if (dp[i][c - a ] - dp[i - mid][c - a ] <= 0) { f = 0; } } if (f) { high = mid - 1; } else { low = mid + 1; } } ans = min(low, ans); } cout << ans << endl; } |
#include <bits/stdc++.h> using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug& operator<<(const c&) { return *this; } }; template <class T> void clr(T& a, int n) { a.clear(); a.resize(n + 1); } void solve(); const int N = 2e5 + 10; const int INF = 1e9; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; while (t--) { solve(); } } int n; void solve() { cin >> n; vector<long long> a(n + 1); for (int i = int(1); i <= int(n); i++) { cin >> a[i]; } const int B = 20; vector<long long> count(B + 1); for (int i = int(1); i <= int(n); i++) { for (int j = int(0); j <= int(B); j++) { long long mask = (1ll << j); count[j] += (mask & a[i] ? 1 : 0); } } debug() << [ << count : << (count) << ] ; long long ans = 0; for (int i = int(1); i <= int(n); i++) { long long curr = 0; for (int j = int(0); j <= int(B); j++) { if (count[j] == 0) continue; curr += (1ll << j); count[j]--; } ans += curr * curr; } cout << ans << n ; } |
#include <bits/stdc++.h> class state { public: int d[4]; int e; state() : d{0, 0, 0, 0}, e(1) {} bool operator<(const state &x) const { for (int i = 0; i < 4; ++i) { if (d[i] == x.d[i]) continue; return d[i] > x.d[i]; } return e > x.e; } bool operator!=(const state &x) const { for (int i = 0; i < 4; ++i) if (d[i] != x.d[i]) return true; return e != x.e; } }; std::map<state, int> mp; int main() { int n; scanf( %d , &n); mp[state()] = 0; while (n--) { int a, b; scanf( %d%d , &a, &b); std::map<state, int> nmp; for (auto it = mp.begin(); it != mp.end(); ++it) { if (it->first.d[0] != 0) continue; state s = it->first; int t = it->second; t += abs(a - s.e) + 1; s.e = a; s.d[0] = b; std::sort(s.d, s.d + 4); auto it2 = nmp.lower_bound(s); if (it2 == nmp.end() || it2->first != s) it2 = nmp.insert(it2, std::pair<state, int>(s, t)); it2->second = std::min(it2->second, t); } mp.swap(nmp); 0; for (auto it = mp.begin(); it != mp.end(); ++it) { 0; for (int i = 0; i < 4; ++i) { if (!it->first.d[i]) continue; state s = it->first; int t = it->second; t += abs(s.d[i] - s.e) + 1; s.e = s.d[i]; s.d[i] = 0; std::sort(s.d, s.d + 4); auto it2 = mp.lower_bound(s); if (it2 == mp.end() || it2->first != s) it2 = mp.insert(it2, std::pair<state, int>(s, t)); it2->second = std::min(it2->second, t); } } } int ans = ((int)1e9); for (auto it = mp.begin(); it != mp.end(); ++it) { if (it->first.d[0] || it->first.d[1] || it->first.d[2] || it->first.d[3]) continue; ans = std::min(ans, it->second); } printf( %d n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; int arr[15], cnt[7] = {0}, ans = 0; int j, hr = 0, mi = 0; int se[100] = {0}; vector<int> na, ma; void rec(int pos, int a) { int i; if (pos == -1) { ans++; return; } if (pos == mi - 1) { for (i = 0; i <= na[pos]; i++) { if (cnt[i] != 1) { cnt[i] = 1; if (i == na[pos]) se[pos] = 1; rec(pos - 1, i); cnt[i] = 0; } se[pos] = 0; } } else if (se[pos + 1] == 0) { for (i = 0; i < 7; i++) { if (cnt[i] != 1) { cnt[i] = 1; rec(pos - 1, i); cnt[i] = 0; } } } else { for (i = 0; i <= na[pos]; i++) { if (cnt[i] != 1) { cnt[i] = 1; if (i == na[pos]) { se[pos] = 1; } rec(pos - 1, i); cnt[i] = 0; } se[pos] = 0; } } } bool chk(int *a, int n) { int i, flg = 0; for (i = 0; i < n - 1; i++) { if (a[i] <= a[i + 1]) flg = 1; else { flg = 0; break; } } return flg; } int main() { arr[0] = 1; for (int i = 1; i <= 10; i++) { arr[i] = arr[i - 1] * 7; } int n, m, i; cin >> n >> m; n = n - 1; m = m - 1; int x = n, y = m; if (y == 0) { na.push_back(0); mi++; } while (y != 0) { na.push_back(y % 7); mi++; y = y / 7; } if (x == 0) { na.push_back(0); hr++; } while (x != 0) { na.push_back(x % 7); hr++; x = x / 7; } if (hr + mi > 7) ans = 0; else { int pos = na.size() - 1; for (i = 0; i <= na[pos]; i++) { cnt[i] = 1; if (i == na[pos]) se[pos] = 1; rec(pos - 1, i); cnt[i] = 0; se[pos] = 0; } } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int a[55][55]; int main() { int n; scanf( %d , &n); memset(a, 0, sizeof(a)); int p = 1, q = 2, nn = n / 2 + 1; for (int i = 1; i <= n; ++i) { a[nn][i] = p; p += 2; } for (int i = 1; i <= n; ++i) { if (i != nn) { a[i][nn] = p; p += 2; } } int maxn = n * n; for (int i = 1; i < nn; ++i) { for (int j = 1; j < nn; ++j) { if (p <= maxn) { a[i][j] = p; a[n + 1 - i][j] = p + 2; a[n + 1 - i][n + 1 - j] = p + 4; a[i][n + 1 - j] = p + 6; p += 8; } else { a[i][j] = q; a[n + 1 - i][j] = q + 2; a[n + 1 - i][n + 1 - j] = q + 4; a[i][n + 1 - j] = q + 6; q += 8; } } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) printf( %d , a[i][j]); printf( n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; int max(int a, int b) { return (a > b) ? a : b; } struct player { int i; int j; int pos; }; int main() { bool firstwon1 = false; bool secondwon1 = false; bool firstwon2 = false, firstwon3 = false, firstwon4 = false; bool secondwon2 = false, secondwon3 = false, secondwon4 = false; int won = 0; player a[2], b[2]; cin >> a[0].i >> a[0].j; cin >> a[1].i >> a[1].j; cin >> b[0].i >> b[0].j; cin >> b[1].i >> b[1].j; if (a[0].i > b[1].j && a[1].j > b[0].i) firstwon1 = true; if (a[0].i < b[1].j && a[1].j < b[0].i) secondwon1 = true; if (a[0].i > b[0].j && a[1].j > b[1].i) firstwon2 = true; if (a[0].i < b[0].j && a[1].j < b[1].i) secondwon2 = true; if (a[1].i > b[1].j && a[0].j > b[0].i) firstwon3 = true; if (a[1].i < b[1].j && a[0].j < b[0].i) secondwon3 = true; if (a[1].i > b[0].j && a[0].j > b[1].i) firstwon4 = true; if (a[1].i < b[0].j && a[0].j < b[1].i) secondwon4 = true; if ((firstwon1 == true && firstwon2 == true) || (firstwon3 == true && firstwon4 == true)) won = 1; if ((secondwon1 == true || secondwon2 == true) && (secondwon3 == true || secondwon4 == true)) won = 2; switch (won) { case 1: { cout << Team 1 n ; break; } case 2: { cout << Team 2 n ; break; } case 0: { cout << Draw n ; break; } } return 0; } |
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) using namespace std; const double pi = acos(-1.0); void cp(); long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { return (a / gcd(a, b)) * b; } long long fact(long long n) { if (n <= 1) return n; return n * fact(n - 1); } bool prime[100001]; void sieve() { int n = 10000; memset(prime, true, sizeof(prime)); for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } } void solve() { int n, i; cin >> n; string s; cin >> s; for (i = 0; i <= (2 * n) - 1; i++) { if (i % 2 == 0) cout << s[i]; } cout << n ; } int main() { cp(); int t; cin >> t; while (t--) solve(); return 0; } void cp() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } |
#include <bits/stdc++.h> using namespace std; using namespace std; void enumerateSubmasks(int m) { for (int s = m;; s = (s - 1) & m) { if (s == 0) { break; } } } int mpow(int a, int b, int m) { if (b == 0) return 1; int x = mpow(a, b / 2, m); x = (x * x) % m; if (b % 2) { x = (x * a) % m; } return x; } void update(int s, int e, int qs, int qe, vector<int> &seg, vector<int> &lazy, int index, int value) { if (lazy[index] != -1) { seg[index] = max(seg[index], lazy[index]); if (s != e) { if (lazy[2 * index] == -1) lazy[2 * index] = lazy[index]; else lazy[2 * index] = max(lazy[2 * index], lazy[index]); if (lazy[2 * index + 1] == -1) lazy[2 * index + 1] = lazy[index]; else lazy[2 * index + 1] = max(lazy[2 * index + 1], lazy[index]); } lazy[index] = -1; } if (qs > e || qe < s) return; if (s >= qs && e <= qe) { seg[index] = max(seg[index], value); if (s != e) { if (lazy[2 * index] == -1) lazy[2 * index] = value; else lazy[2 * index] = max(lazy[2 * index], value); if (lazy[2 * index + 1] == -1) lazy[2 * index + 1] = value; else lazy[2 * index + 1] = max(lazy[2 * index + 1], value); } return; } int mid = (s + e) / 2; update(s, mid, qs, qe, seg, lazy, 2 * index, value); update(mid + 1, e, qs, qe, seg, lazy, 2 * index + 1, value); } int query(int s, int e, int qs, int qe, vector<int> &seg, vector<int> &lazy, int index) { if (lazy[index] != -1) { seg[index] = max(seg[index], lazy[index]); if (s != e) { if (lazy[2 * index] == -1) lazy[2 * index] = lazy[index]; else lazy[2 * index] = max(lazy[2 * index], lazy[index]); if (lazy[2 * index + 1] == -1) lazy[2 * index + 1] = lazy[index]; else lazy[2 * index + 1] = max(lazy[2 * index + 1], lazy[index]); } lazy[index] = -1; } if (qs > e || qe < s) return LLONG_MIN; if (s >= qs && e <= qe) { return seg[index]; } int mid = (s + e) / 2; int a = query(s, mid, qs, qe, seg, lazy, 2 * index); int b = query(mid + 1, e, qs, qe, seg, lazy, 2 * index + 1); return max(a, b); } void printBinaryString(int n) { vector<int> temp; while (n) { if (n & 1) temp.push_back(1); else temp.push_back(0); n = n >> 1; } reverse(temp.begin(), temp.end()); for (auto node : temp) cout << node << ; cout << endl; } void readVector(vector<int> &a) { int n = a.size(); for (int i = 0; i < n; ++i) cin >> a[i]; } vector<vector<int>> adj(100002); vector<int> par(100002); void dfs(int node, int parent) { par[node] = parent; for (auto child : adj[node]) { if (child == parent) continue; dfs(child, node); } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<pair<int, int>> graph; for (int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); graph.push_back({u, v}); } dfs(1, 1); cout << n - 1 << endl; for (int i = 2; i <= n; i++) { cout << 2 << << i << << par[i] << endl; } int oneCousin = *adj[1].begin(); for (auto node : adj[1]) { if (node == oneCousin) continue; graph.push_back({oneCousin, node}); } for (auto node : graph) if (node.first != 1 && node.second != 1) cout << node.first - 1 << << node.second - 1 << endl; } |
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; const int OO = 1e18; const int md = 1e9 + 7; int X[] = {0, 0, 1, -1}, Y[] = {1, -1, 0, 0}; void fast() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); } int dp[int(1e4 + 4)], t = 0, x, y, z, n, q, g[int(1e4 + 4)], res; pair<P, int> p[int(1e4 + 4)]; int main() { scanf( %d%d , &n, &q); for (int i = 0; i < q; i++) scanf( %d%d%d , &p[i].first.first, &p[i].first.second, &p[i].second); sort(p, p + q); memset(dp, -1, sizeof dp); memset(g, -1, sizeof g); dp[0] = g[0] = n; for (int i = 0; i < q; i++) { for (int j = 0; j <= n; j++) { g[j] = max(g[j], dp[j]); if (dp[j] >= p[i].first.first && j + p[i].second <= n) g[j + p[i].second] = max(dp[j + p[i].second], min(dp[j], p[i].first.second)); } g[p[i].second] = max(g[p[i].second], p[i].first.second); swap(dp, g); } for (int i = 1; i <= n; i++) if (dp[i] != -1) res++; printf( %d n , res); for (int i = 1; i <= n; i++) if (dp[i] != -1) printf( %d , i); printf( n ); return 0; } |
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int Maxn = 10010; int a[Maxn]; int tot = 0; int main() { int n; cin >> n; long long sum = 0; for (int i = 2; i <= n; i++) { for (int j = 2 * i; j <= n; j += i) { sum += 4ll * (j / i); } } cout << sum << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; struct PR { int to; int ne; } Prise[200010]; bool compare(PR a, PR b) { return (a.to - a.ne) < (b.to - b.ne); } int main(int argc, char const *argv[]) { int n, k; cin >> n >> k; int i; for (i = 0; i < n; i++) { cin >> Prise[i].to; } for (i = 0; i < n; i++) { cin >> Prise[i].ne; } sort(Prise, Prise + n, compare); int count = 0; for (i = 0; i < k; i++) { count += Prise[i].to; } for (i = k; i < n; i++) { if (Prise[i].to < Prise[i].ne) { count += Prise[i].to; } else { count += Prise[i].ne; } } cout << count << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; long long far(long long x, long long y) { return x * x + y * y; } vector<long long> val; vector<long long> dsu; vector<long long> rang; long long pred(long long a) { if (a == dsu[a]) { return a; } else { long long to = pred(dsu[a]); if (to != dsu[a]) { val[a] *= val[dsu[a]]; } dsu[a] = to; return to; } } void unite(long long a, long long b) { a = pred(a); b = pred(b); if (a != b) { if (rang[a] < rang[b]) { swap(a, b); } if (val[a] == -1) { val[b] *= -1; } dsu[b] = a; if (rang[a] == rang[b]) { rang[a]++; } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; val.resize(n); dsu.resize(n); rang.resize(n); vector<pair<long long, long long> > x(n); vector<long long> type(n, 1); long long stx = 0, sty = 0; for (long long i = 0; i < n; i++) { cin >> x[i].first >> x[i].second; } long long p = 1e6; p = p * p; vector<long long> ind(n); for (long long i = 0; i < n; i++) { ind[i] = i; dsu[i] = i; rang[i] = 1; val[i] = 1; } for (long long i = 0; i < n - 2; i++) { for (long long j = i + 0; j < min(n, 3 + i); j++) { for (long long d = j + 1; d < min(n, 3 + i); d++) { if (far(x[j].first + x[d].first, x[j].second + x[d].second) <= p) { unite(ind[j], ind[d]); x[d].first += x[j].first; x[d].second += x[j].second; if (j != i) { x[i + 1] = x[i]; ind[i + 1] = ind[i]; } j = 3 + i; break; } else if (far(x[j].first - x[d].first, x[j].second - x[d].second) <= p) { val[pred(ind[d])] *= -1; unite(ind[j], ind[d]); x[d].first = x[j].first - x[d].first; x[d].second = x[j].second - x[d].second; if (j != i) { x[i + 1] = x[i]; ind[i + 1] = ind[i]; } j = 3 + i; break; } } } } long long p1 = 1.5e6; p1 = p1 * p1; if (n > 1) { if (far(x[n - 2].first + x[n - 1].first, x[n - 2].second + x[n - 1].second) <= p1) { unite(ind[n - 2], ind[n - 1]); } else { val[pred(ind[n - 1])] *= -1; unite(ind[n - 2], ind[n - 1]); } } for (long long i = 0; i < n; i++) { long long t = pred(i); if (t != i) { cout << val[t] * val[i] << ; } else { cout << val[i] << ; } } return 0; } |
#include <bits/stdc++.h> using namespace std; const double PI = 3.14159265358979323846; const double EPS = 1e-15; const int INF = 1e9 + 7; const long long LINF = 8e18 + 7; const int N = 2e5 + 7; vector<long long> t[4 * N]; int a[N]; long long s[N]; void build(int v, int L, int R) { if (L + 1 == R) { t[v].push_back(s[L]); return; } int M = L + R >> 1; build(v << 1, L, M); build(v << 1 | 1, M, R); t[v].resize(t[v << 1].size() + t[v << 1 | 1].size()); merge(t[v << 1].begin(), t[v << 1].end(), t[v << 1 | 1].begin(), t[v << 1 | 1].end(), t[v].begin()); } int get(int v, int L, int R, int l, int r, long long val) { if (L == l && R == r) { return t[v].end() - upper_bound(t[v].begin(), t[v].end(), val); } int M = L + R >> 1; int ans = 0; if (l < M) { ans += get(v << 1, L, M, l, min(M, r), val); } if (r > M) { ans += get(v << 1 | 1, M, R, max(l, M), r, val); } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; long long t; cin >> n >> t; for (int i = 0; i < int(n); ++i) { cin >> a[i]; s[i + 1] = a[i] + s[i]; } build(1, 0, n + 1); long long ans = 0; for (int i = 0; i < int(n); ++i) { ans += get(1, 0, n + 1, 0, i + 1, s[i + 1] - t); } cout << ans << endl; } |
#include <bits/stdc++.h> using namespace std; const int MX = 1e5 + 3; int t; long long m, n; pair<long long, long long> v[MX]; long long p[MX]; int main() { scanf( %d , &t); while (t--) { scanf( %lld %lld , &n, &m); long long ans = 0; for (int i = 0; i < m; i++) { scanf( %lld %lld , &v[i].first, &v[i].second); } sort(v, v + m, greater<pair<long long, long long>>()); for (int i = 0; i < m; i++) { p[i] = v[i].first + (i ? p[i - 1] : 0); if (i < n) ans += v[i].first; } for (int i = 0; i < m; i++) { int l = 0; int r = m - 1; int md; while (l != r) { md = (l + r) / 2; if (v[md].first < v[i].second) r = md; else l = md + 1; } int cnt = l; if (v[m - 1].first >= v[i].second) cnt++, l++; long long sum = 0; if (l) sum = p[l - 1]; if (l > i && cnt) { cnt--; sum -= v[i].first; } if (cnt > n - 2) { continue; } long long ret = v[i].first + v[i].second * (n - cnt - 1) + sum; ans = max(ans, ret); } printf( %lld n , ans); } return 0; } |
#include <bits/stdc++.h> using namespace std; string equalString(string a) { int len = a.size(); if (len % 2 == 1) return a; string a1 = a.substr(0, len / 2); string a2 = a.substr(len / 2, len / 2); a1 = equalString(a1); a2 = equalString(a2); return a1.compare(a2) < 0 ? a1 + a2 : a2 + a1; } int main() { string a, b; cin >> a >> b; string nor_a = equalString(a); string nor_b = equalString(b); if (nor_a.compare(nor_b) == 0) cout << YES n ; else cout << NO n ; return 0; } |
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > seq; const int mod = 1e6; const long long A = 42344; const long long B = 333; int seed = 239; int mrand() { static int now = -1; if (now == -1) now = seed; else now = (A * now + B) % mod; return now; } struct line { long long a, b, c; int id; line() {} line(long long A, long long B, long long C, int i) : a(A), b(B), c(C), id(i) {} }; void run(vector<line> a, int k) { int n = (int)a.size(); if (k >= n) { for (int i = 0; i < n; i++) seq.push_back(make_pair(a[i].id, -1)); sort(seq.begin(), seq.end()); seq.resize(unique(seq.begin(), seq.end()) - seq.begin()); puts( YES ); printf( %d n , (int)seq.size()); for (int i = 0; i < (int)seq.size(); i++) printf( %d %d n , seq[i].first, seq[i].second); exit(0); } if (k == 0) return; vector<line> go; for (int iter = 0; iter < k * k; iter++) { int i = mrand() % n, j = mrand() % (n - 1); if (j >= i) j++; if (a[i].a * a[j].b == a[j].a * a[i].b) continue; go.resize(0); int kol = 0; double lim = max(1.0 * kol, n * (1.0 - 1.0 / (2.0 * k))); for (int q = 0; q < n; q++) if (a[i].a * a[j].b * a[q].c + a[i].b * a[j].c * a[q].a + a[i].c * a[j].a * a[q].b != a[i].c * a[j].b * a[q].a + a[i].b * a[j].a * a[q].c + a[i].a * a[j].c * a[q].b) { go.push_back(a[q]); kol++; if (kol > lim) break; } if (kol > lim) continue; seq.push_back(make_pair(a[i].id, a[j].id)); run(go, k - 1); seq.pop_back(); } } int main() { srand(123654); int n, k; scanf( %d%d , &n, &k); vector<line> l(n); for (int i = 0; i < n; i++) { int a, b, c; scanf( %d%d%d , &a, &b, &c); l[i] = line(a, b, c, i + 1); } run(l, k); puts( NO ); return 0; } |
#include <bits/stdc++.h> using namespace std; string express(int layers) { string ans = ; for (int i = 1; i <= layers; i++) { if (i % 2 == 1) { if (i == layers) { ans += I hate it ; } else { ans += I hate that ; } } else { if (i == layers) { ans += I love it ; } else { ans += I love that ; } } } return ans; } int main() { int layers; cin >> layers; cout << express(layers); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> vec; vec.push_back(0LL); for (int i = 0; i < n; i++) { long long k; cin >> k; vec.push_back(k); } int m; cin >> m; vector<long long> v; long long x = 0, y = 0; for (int i = 0; i < m; i++) { long long a, b; cin >> a >> b; if (vec[a] >= y) { v.push_back(vec[a]); y = vec[a] + b; } else { v.push_back(y); y += b; } } for (int i = 0; i < m; i++) cout << v[i] << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int mx1 = 20; const int mx2 = 405; const int mx3 = 2005; const int mx4 = 30005; const int mx5 = 100005; const int mx6 = 1000005; int _toggle(int N, int pos) { return N = N ^ (1 << pos); } int _set(int N, int pos) { return N = N | (1 << pos); } int _reset(int N, int pos) { return N = N & ~(1 << pos); } bool _check(int N, int pos) { return (bool)(N & (1 << pos)); } bool _upper(char a) { return a >= A && a <= Z ; } bool _lower(char a) { return a >= a && a <= z ; } bool _digit(char a) { return a >= 0 && a <= 9 ; } int dx[] = {1, -1, 0, 0, -1, -1, 1, 1}; int dy[] = {0, 0, 1, -1, -1, 1, -1, 1}; const int inf = 1e9 + 7; int n, a[mx5]; int fnd(int x, int y) { int cnt = (x != 0) + (y != 0); int A = a[1] + x, B = a[2] + y, d = B - A, pre = B; for (int i = 3; i <= n; i++) { bool f = 0; for (int j = -1; j < 2; j++) { int now = a[i] + j; if (now - pre == d) { f = 1; pre = now; cnt += (j != 0); break; } } if (!f) { return inf; } } return cnt; } void solve() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } if (n < 3) { cout << 0 << n ; return; } int ans = inf; for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { ans = min(ans, fnd(i, j)); } } if (ans == inf) { cout << -1 << n ; } else { cout << ans << n ; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int tc = 1; while (tc--) { solve(); } } |
#include <bits/stdc++.h> using namespace std; long long l, r, lim[60]; long long f[60][60][2][2]; inline long long read() { long long x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == - ) f = -1; ch = getchar(); } while (isdigit(ch)) { (x *= 10) += ch - 0 ; ch = getchar(); } return x * f; } long long dfs(long long at, long long sum, bool is_lim, bool is_pre) { if (at == 0) return sum <= 3 && !is_pre; if (f[at][sum][is_lim][is_pre] != -1) return f[at][sum][is_lim][is_pre]; long long mx = is_lim ? lim[at] : 9, res = 0; for (long long i = 0; i <= mx; i++) { if (i == 0 && is_pre) res += dfs(at - 1, sum, is_lim && i == mx, is_pre); else res += dfs(at - 1, sum + (i != 0), is_lim && i == mx, 0); } return f[at][sum][is_lim][is_pre] = res; } long long solve(long long x) { memset(f, -1, sizeof(f)); memset(lim, 0, sizeof(lim)); long long mx_pos = 0; while (x) lim[++mx_pos] = x % 10, x /= 10; return dfs(mx_pos, 0, 1, 1); } signed main() { long long T = read(); while (T--) { l = read(), r = read(); printf( %lld n , solve(r) - solve(l - 1)); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 1000100, mod = 998244353; inline int read() { int x = 0, f = 0; char ch = getchar(); while (ch < 0 || ch > 9 ) f |= ch == - , ch = getchar(); while (ch >= 0 && ch <= 9 ) x = x * 10 + ch - 0 , ch = getchar(); return f ? -x : x; } int n, el, head[maxn], to[maxn * 2], nxt[maxn * 2], len[maxn], fa[maxn], son[maxn], ans[maxn], *f[maxn]; inline void add(int u, int v) { to[++el] = v; nxt[el] = head[u]; head[u] = el; } void dfs1(int u, int f) { fa[u] = f; for (int i = head[u]; i; i = nxt[i]) { int v = to[i]; if (v == f) continue; dfs1(v, u); if (len[v] + 1 > len[u]) son[u] = v, len[u] = len[v] + 1; } } void dfs2(int u) { if (son[fa[u]] != u) f[u] = new int[len[u] + 5](); if (son[u]) { f[son[u]] = f[u] + 1; dfs2(son[u]); if (f[u][ans[son[u]] + 1] > 1) ans[u] = ans[son[u]] + 1; } f[u][0]++; for (int i = head[u]; i; i = nxt[i]) { int v = to[i]; if (v == fa[u] || v == son[u]) continue; dfs2(v); for (int j = (0); j <= (len[v]); j++) { f[u][j + 1] += f[v][j]; if (f[u][j + 1] == f[u][ans[u]]) ans[u] = min(ans[u], j + 1); if (f[u][j + 1] > f[u][ans[u]]) ans[u] = j + 1; } } } int main() { n = read(); for (int i = (1); i <= (n - 1); i++) { int u = read(), v = read(); add(u, v); add(v, u); } dfs1(1, 0); dfs2(1); for (int i = (1); i <= (n); i++) if (son[fa[i]] != i) delete[] f[1]; for (int i = (1); i <= (n); i++) printf( %d n , ans[i]); } |
#include <bits/stdc++.h> using namespace std; long long n; long long N[100006]; long long sum[1000006], S[1000006]; long long t[1000006]; long long t1[1000006]; long long lowbit(long long u) { return u & (-u); } void add(long long u, long long w) { while (u <= n) { t[u] += w; u += lowbit(u); } } long long Find(long long u) { long long ans = 0; while (u) { ans += t[u]; u -= lowbit(u); } return ans; } void add1(long long u, long long w) { while (u <= n) { t1[u] += w; u += lowbit(u); } } long long Find1(long long u) { long long ans = 0; while (u) { ans += t1[u]; u -= lowbit(u); } return ans; } double ans; signed main() { scanf( %lld , &n); for (long long i = 1; i <= n; i++) scanf( %lld , &N[i]); for (long long i = 1; i <= n; i++) { sum[i] += Find(n) - Find(N[i]); S[i] += Find1(n) - Find1(N[i]); if (sum[i] != 0) ans += ((double)(n * (n + 1ll) / 2ll) * sum[i] - (double)(n - i + 1ll) * S[i]) / (double)(n * (n + 1ll) / 2ll); add(N[i], 1); add1(N[i], i); } for (long long i = 2; i <= n; i++) ans += (double)((i - 1) * i) / 2.00 / 2.00 * (double)(n - i + 1) / (double)(n * (n + 1ll) / 2ll); printf( %.12lf n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; int n, nq, tr, tg, z, tot, root; int ls[7000010], rs[7000010], s[7000010]; long long mod, ans, x, y; long long b[100010], a[100010]; void push_down(int p) { if (!ls[p]) ls[p] = ++tot; if (!rs[p]) rs[p] = ++tot; if (s[ls[p]] == 0 || s[p] < s[ls[p]]) s[ls[p]] = s[p]; if (s[rs[p]] == 0 || s[p] < s[rs[p]]) s[rs[p]] = s[p]; s[p] = 0; } void change(int &p, long long l, long long r) { if (l > y || r < x) return; if (!p) p = ++tot; if (l >= x && r <= y) { if (s[p] == 0 || z < s[p]) s[p] = z; return; } if (s[p]) push_down(p); change(ls[p], l, (l + r) / 2); change(rs[p], (l + r) / 2 + 1, r); } int query(int &p, long long l, long long r) { if (!p) p = ++tot; if (l == r) return s[p]; if (s[p]) push_down(p); if (x <= (l + r) / 2) return query(ls[p], l, (l + r) / 2); else return query(rs[p], (l + r) / 2 + 1, r); } int main() { int i, j, q; long long tx, ty; scanf( %d%d%d , &n, &tg, &tr); mod = tg + tr; for (i = 1; i <= n + 1; i++) scanf( %I64d , &a[i]); for (i = 1; i <= n + 1; i++) a[i] += a[i - 1]; for (i = n; i >= 1; i--) { x = mod - a[i] % mod; y = query(root, 1, mod); if (!y) b[i] = 0; else b[i] = b[y] + (mod - (a[y] - a[i]) % mod); tx = ((tg - a[i]) % mod + mod - 1) % mod + 1; ty = ((mod - 1 - a[i]) % mod + mod - 1) % mod + 1; z = i; if (tx <= ty) { x = tx; y = ty; change(root, 1, mod); } else { x = 1; y = ty; change(root, 1, mod); x = tx; y = mod; change(root, 1, mod); } } scanf( %d , &nq); for (q = 1; q <= nq; q++) { scanf( %d , &x); ans = x + a[n + 1]; x = (x - 1) % mod + 1; y = query(root, 1, mod); if (y) ans += mod - (a[y] + x) % mod + b[y]; printf( %I64d n , ans); } return 0; } |
#include <bits/stdc++.h> using namespace std; template <class T> istream &operator>>(istream &in, vector<T> &arr) { for (auto &x : arr) in >> x; return in; } long long n, m; const long long N = 1e6 + 100; vector<pair<long long, long long>> g[N]; long long col[N]; bool cyc; long long res[N]; void dfs(long long r) { col[r] = 1; for (auto x : g[r]) { long long to = x.first, ind = x.second; if (col[to] == 0) { res[ind] = 1; dfs(to); } else if (col[to] == 1) { res[ind] = 2; cyc = true; } else { res[ind] = 1; } } col[r] = 2; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (long long i = 0; i < m; ++i) { long long u, v; cin >> u >> v; --u, --v; g[u].push_back({v, i}); } for (long long i = 0; i < n; ++i) { if (col[i] == 0) { dfs(i); } } cout << (cyc ? 2 : 1) << n ; for (long long i = 0; i < m; ++i) { cout << res[i] << ; } } |
#include <bits/stdc++.h> using namespace std; char s1[1010000]; char s2[1010000]; int main() { scanf( %s , s1); scanf( %s , s2); int len1, len2, i; len1 = strlen(s1); len2 = strlen(s2); int ans; if (len1 - len2) ans = 0; else if (len1 == 1) { if (s1[0] == s2[0]) ans = 1; else ans = 0; } else { if (strcmp(s1, s2) == 0) ans = 1; else { int j1, j2; j1 = j2 = 0; for (i = 0; s1[i]; ++i) if (s1[i] == 1 ) j1 = 1; for (i = 0; s2[i]; ++i) if (s2[i] == 1 ) j2 = 1; if (j1 && j2) ans = 1; else ans = 0; } } if (ans) puts( YES ); else puts( NO ); } |
#include <bits/stdc++.h> using namespace std; int N, M, lb[300010], ind[300010]; vector<int> g[300010], g2[300010]; int val[300010]; bool viz[300010]; int dfs(int nod, int v) { val[nod] = v; viz[nod] = 1; for (int nxt : g2[nod]) { if (viz[nxt]) continue; return dfs(nxt, v + 1); } return nod; } int main() { cin >> N >> M; int i, x, y; for (i = 1; i <= M; i++) { cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } for (i = 1; i <= N; i++) { g[i].push_back(i); sort(g[i].begin(), g[i].end()); ind[i] = i; } sort(ind + 1, ind + N + 1, [](int x, int y) { return g[x] < g[y]; }); lb[ind[1]] = 1; for (i = 2; i <= N; i++) if (g[ind[i]] == g[ind[i - 1]]) lb[ind[i]] = lb[ind[i - 1]]; else lb[ind[i]] = 1 + lb[ind[i - 1]]; for (i = 1; i <= N; i++) for (int nxt : g[i]) { if (lb[nxt] == lb[i]) continue; g2[lb[i]].push_back(lb[nxt]); } int cr = dfs(1, 1); memset(val, 0, sizeof(val)); memset(viz, 0, sizeof(viz)); dfs(cr, 1); for (i = 1; i <= lb[ind[N]]; i++) if (val[i] == 0) { cout << NO n ; return 0; } cout << YES n ; for (i = 1; i <= N; i++) cout << val[lb[i]] << ; cout << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long int INF = 9223372036854775807LL; #pragma GCC optimize( Ofast ) #pragma GCC target( avx,avx2,fma ) #pragma GCC optimization( unroint-loops ) long long int power(long long int b, long long int e, long long int m) { if (b == 0) return 0; if (e == 0) return 1; if (e & 1) return b * power(b * b % m, e / 2, m) % m; return power(b * b % m, e / 2, m); } long long int power(long long int b, long long int e) { if (b == 0) return 0; if (e == 0) return 1; if (e & 1) return b * power(b * b, e / 2); return power(b * b, e / 2); } bool isPowerOfTwo(long long int x) { return (x && !(x & (x - 1))); } void solve() { long long int n, m; cin >> n >> m; vector<long long int> v(n + 1); cin >> v[1]; for (long long int i = 2; i <= n; i++) { cin >> v[i]; v[i] += v[i - 1]; } long long int ans = 0; while (m--) { long long int a, b; cin >> a >> b; if (v[b] - v[a - 1] >= 0) ans += v[b] - v[a - 1]; } cout << ans; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int tt = 1; for (long long int i = 1; i <= tt; i++) { solve(); cout << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 55; int x[MAX]; long long poww(long long x, long long y) { if (y == 0) return 1; long long s = poww(x, y / 2); s = (s * s); if (y % 2 == 1) s = (s * x); return s; } int main() { string s = OYGR ; string c = BIV ; int t = 0; int n; cin >> n; string b = ; for (int i = 0; i < n - 3; i++) { if (t == 4) t = 0; b += s[t]; t++; } cout << b + c; return 0; } |
#include <bits/stdc++.h> int a[1511]; int main() { int n, m, i, j, s(0); scanf( %d , &n); for (i = 0; i < n; ++i) scanf( %d , &a[i]); for (i = 0; i < n; ++i) for (j = i + 1; j < n; ++j) if (a[i] > a[j]) s ^= 1; scanf( %d , &m); while (m--) { scanf( %d%d , &i, &j); if ((j - i + 1) * (j - i) / 2 & 1) s ^= 1; printf( %s n , s ? odd : even ); } return 0; } |
#include <bits/stdc++.h> #pragma comment(linker, /STACK:1000000000 ) #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops ) using namespace std; const int maxn = (int)1e5 + 10; struct vt { long double x, y; }; vt operator-(vt a, vt b) { a.x -= b.x; a.y -= b.y; return a; } vt operator+(vt a, vt b) { a.x += b.x; a.y += b.y; return a; } vt operator*(vt a, long double x) { a.x *= x; a.y *= x; return a; } long double dst(vt a) { return hypotl(a.x, a.y); } vt operator/(vt a, long double x) { a.x /= x; a.y /= x; return a; } const long double eps = 1e-18; vector<vt> inter(vt a, long double r) { long double d = dst(a); if (d * d <= r * r) { return {}; } long double x = r * r / d; long double y = r * sqrt(1. - (r / d) * (r / d)); vt b = a * (x / dst(a)); vt n = {-a.y, a.x}; n = n / dst(n); return {b - n * y, b + n * y}; } long double operator*(vt a, vt b) { return a.x * b.y - a.y * b.x; } int tp(vt a) { return a.y > eps || (abs(a.y) < eps && a.x > eps); } vt v[maxn]; bool operator<(vt a, vt b) { int tpa = tp(a); int tpb = tp(b); if (tpa != tpb) { return tpa < tpb; } return a * b > eps; } int tr[4 * maxn]; void change(int pos) { for (; pos < 4 * maxn; pos |= pos + 1) { tr[pos]++; } } int go(int pos) { int ans = 0; for (; pos >= 0; pos = (pos & (pos + 1)) - 1) { ans += tr[pos]; } return ans; } int go(int l, int r) { return go(r) - go(l - 1); } ostream& operator<<(ostream& out, vt a) { return out << a.x << << a.y; } mt19937 rr; int main() { int n; long long k; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> v[i].x >> v[i].y; } k = (long long)n * (n - 1) / 2 - k; long double l = 0; long double r = sqrt(2.) * 1e4; for (int itr = 0; itr < 36; itr++) { long double m = (l + r) / 2; vector<vt> g; vector<pair<vt, vt> > f; for (int i = 0; i < n; i++) { auto a = inter(v[i], m); if (a.empty()) { continue; } f.push_back(make_pair(a[0], a[1])); g.push_back(a[0]); g.push_back(a[1]); } sort((g).begin(), (g).end()); vector<pair<int, int> > a; memset(tr, 0, sizeof tr); for (int i = 0; i < (int)f.size(); i++) { a.push_back(make_pair( lower_bound((g).begin(), (g).end(), f[i].first) - g.begin(), lower_bound((g).begin(), (g).end(), f[i].second) - g.begin())); if (a.back().first > a.back().second) { change(a.back().second); a.back().second += (int)g.size(); } } sort((a).begin(), (a).end()); long long tot = 0; for (int i = 0; i < (int)a.size(); i++) { tot += go(a[i].first, a[i].second); change(a[i].second); } if (tot <= k) { r = m; } else { l = m; } } printf( %.10lf n , (double)r); } |
#include<bits/stdc++.h> using namespace std; #define mp make_pair #define fi first #define se second #define pb push_back #define FOR(i,a,b) for(int i=a;i<=b;i++) #define FORD(i,a,b) for(int i=a;i>=b;i--) #define faster() ios_base::sync_with_stdio(0); cin.tie(NULL);cout.tie(NULL); typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; int main() { faster(); int t; cin>>t; while(t--) { int a[4]; FOR(i,0,3) cin>>a[i]; int max1=max(a[0],a[1]),max2=max(a[2],a[3]); sort(a,a+4); if(max1>max2 && max1==a[3] && max2==a[2]) cout<< YES <<endl; else if(max1<max2 && max2==a[3] && max1==a[2]) cout<< YES <<endl; else cout<< NO <<endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; inline long long read() { long long sum = 0, ff = 1; char ch = getchar(); while (ch < 0 || ch > 9 ) { if (ch == - ) { ff = -1; } ch = getchar(); } while (ch >= 0 && ch <= 9 ) { sum = sum * 10 + ch - 48; ch = getchar(); } return sum * ff; } void write(long long x) { if (x < 0) putchar( - ), x = -x; if (x > 9) write(x / 10); putchar(x % 10 + 0 ); } const int N = 1000007; int n, m; struct edge { int from, to, dis, next; } e[N], a[N]; int he[N], cnt; void add(int u, int v, int w) { e[++cnt].from = u; e[cnt].to = v; e[cnt].dis = w; e[cnt].next = he[u]; he[u] = cnt; } bool cmp(edge x, edge y) { if (x.from == y.from) return x.to < y.to; return x.from < y.from; } int col[N]; queue<int> q; struct node { int pos, dis; }; queue<node> que; bool vis[N]; int bfs() { que.push((node){n, 0}); vis[n] = true; while (!que.empty()) { node now = que.front(); que.pop(); if (now.pos == 1) return now.dis; for (int i = he[now.pos]; i; i = e[i].next) { int v = e[i].to; if (vis[v]) continue; if (col[v] == -1 || col[v] == 1 - e[i].dis) col[v] = 1 - e[i].dis; else { vis[v] = true; que.push((node){v, now.dis + 1}); } } } return -1; } map<pair<int, int>, bool> mp; int main() { n = read(), m = read(); for (int i = 1; i <= m; i++) { a[i].from = read(), a[i].to = read(), a[i].dis = read(); add(a[i].to, a[i].from, a[i].dis); } memset(col, -1, sizeof(col)); write(bfs()), puts( ); for (int i = 1; i <= n; i++) if (col[i] == -1) printf( 0 ); else write(col[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 2010; struct point { point(int xx, int yy) : x(xx), y(yy) {} int x, y; }; int n, m; char a[N][N]; queue<point> q; int dx[4] = {-1, 1, 0, 0}; int dy[4] = {0, 0, -1, 1}; long long pp; bool uniq(int xx, int yy) { if (a[xx][yy] != . ) { return false; } int counter = 0; for (int i = 0; i < 4; ++i) { int x = xx + dx[i]; int y = yy + dy[i]; if (a[x][y] == . ) { ++counter; } } return (counter == 1); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m; for (int i = 0; i <= n + 1; ++i) { for (int j = 0; j <= m + 1; ++j) { if (i == 0 || j == 0 || i == n + 1 || j == m + 1) { a[i][j] = * ; continue; } cin >> a[i][j]; if (a[i][j] == . ) { ++pp; } } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { if (a[i][j] == * ) { continue; } int counter = 0; if (a[i][j + 1] == . ) { ++counter; } if (a[i][j - 1] == . ) { ++counter; } if (a[i - 1][j] == . ) { ++counter; } if (a[i + 1][j] == . ) { ++counter; } if (counter == 0) { cout << Not unique ; return 0; } if (counter == 1) { q.push(point(i, j)); } } } while (true) { if (q.empty()) { break; } point cur = q.front(); q.pop(); if (a[cur.x][cur.y] != . ) { continue; } if (!uniq(cur.x, cur.y)) { cout << Not unique ; return 0; } int xx = cur.x; int yy = cur.y; for (int i = 0; i < 4; ++i) { xx += dx[i]; yy += dy[i]; if (a[xx][yy] == . ) { break; } xx -= dx[i]; yy -= dy[i]; } if (yy == cur.y) { if (xx < cur.x) { a[cur.x][cur.y] = v ; a[xx][yy] = ^ ; } else { a[xx][yy] = v ; a[cur.x][cur.y] = ^ ; } } if (xx == cur.x) { if (yy < cur.y) { a[cur.x][cur.y] = > ; a[xx][yy] = < ; } else { a[cur.x][cur.y] = < ; a[xx][yy] = > ; } } pp -= 2; for (int i = 0; i < 4; ++i) { if (uniq(xx + dx[i], yy + dy[i])) { q.push(point(xx + dx[i], yy + dy[i])); } } } if (pp > 0) { cout << Not unique ; return 0; } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { cout << a[i][j]; } cout << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int n, i, ans; map<long long, long long> m, ind; long long b[150001], a; int main() { scanf( %d , &n); ans = n; for (i = 1; i <= n; i++) { scanf( %I64d , &a); m[a]++; b[i] = a; while (m[a] > 1) { ans--; m[a] = 0; b[ind[a]] = 0; a *= 2; b[i] = a; m[a]++; } ind[a] = i; } printf( %d n , ans); for (i = 1; i <= n; i++) if (b[i] > 0) printf( %I64d , b[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; vector<vector<pair<int, string> > > regions; int main() { int n, m; cin >> n >> m; regions.resize(m); string name; int r, s; for (int i = 0; i < n; ++i) { cin >> name >> r >> s; regions[r - 1].push_back(make_pair(s, name)); } for (int i = 0; i < m; ++i) { sort(regions[i].rbegin(), regions[i].rend()); if (regions[i].size() == 2 || regions[i][1].first > regions[i][2].first) cout << regions[i][0].second << << regions[i][1].second << endl; else cout << ? << endl; } } |
#include <bits/stdc++.h> using namespace std; const long long MAXN = 2e5 + 7; const long long INF = 3e18 + 7; long long n, t; long long a[MAXN], p[MAXN], l[MAXN], r[MAXN]; long long ans[MAXN]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> t; for (long long i = 0; i < n; ++i) cin >> a[i]; for (long long i = 0; i < n; ++i) { cin >> p[i]; --p[i]; } for (long long i = 0; i < n - 1; ++i) { if (p[i + 1] < p[i]) { cout << No n ; exit(0); } } for (long long i = 0; i < n; ++i) { l[i] = 1; r[i] = INF; } for (long long i = 0; i < n; ++i) { l[p[i]] = max(l[p[i]], a[i] + t); } for (long long i = 0; i < n; ++i) { l[i] = max(l[i], a[i] + t); } long long mx = -1; for (long long i = 0; i < n - 1; ++i) { mx = max(mx, p[i]); if (i < mx) { l[i] = max(l[i], a[i + 1] + t); } } for (long long i = 0; i < n - 1; ++i) { if (p[i] < i) { r[p[i]] = min(r[p[i]], a[p[i]] + t - 1); } else { r[p[i]] = min(r[p[i]], a[p[i] + 1] + t - 1); } } for (long long i = 0; i < n; ++i) { if (!i) ans[i] = l[i]; else ans[i] = max(ans[i - 1] + 1, l[i]); } for (long long i = 0; i < n - 1; ++i) { if (ans[i + 1] <= a[i]) { cout << No n ; exit(0); } if (!(l[i] <= ans[i] && ans[i] <= r[i])) { cout << No n ; exit(0); } } cout << Yes n ; for (long long i = 0; i < n; ++i) { cout << ans[i] << ; } cout << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; void JIZZ(string output = ) { cout << output; exit(0); } const long double PI = 3.14159265358979323846264338327950288; const long double eps = 1e-13; const long long mod = 1e9 + 7; struct node { node *l, *r; int mx; node() : l(0), r(0), mx(-(1 << 30)) {} } * root, pool[100000]; int pptr = -1; int a[20005], pre[20005], dp[53][20005]; node *nnode() { ++pptr; pool[pptr].l = pool[pptr].r = 0; pool[pptr].mx = -(1 << 30); return &pool[pptr]; } void build(node *now, int l, int r) { if (l == r) return; build(now->l = nnode(), l, ((l + r) >> 1)); build(now->r = nnode(), ((l + r) >> 1) + 1, r); } void modify(node *now, int l, int r, int x, int v) { if (l == r) { now->mx = max(now->mx, v); return; } if (x <= ((l + r) >> 1)) modify(now->l, l, ((l + r) >> 1), x, v); else modify(now->r, ((l + r) >> 1) + 1, r, x, v); now->mx = max(now->l->mx, now->r->mx); } int query(node *now, int l, int r, int ql, int qr) { ; ; if (qr < l || r < ql) return -(1 << 30); if (ql <= l && r <= qr) return now->mx; return max(query(now->l, l, ((l + r) >> 1), ql, qr), query(now->r, ((l + r) >> 1) + 1, r, ql, qr)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, k, p; cin >> n >> k >> p; for (int i = 1; i <= n; ++i) cin >> a[i], a[i] %= p; for (int i = 1; i <= n; ++i) pre[i] = (pre[i - 1] + a[i]) % p; for (int i = 1; i <= n; ++i) dp[0][i] = -(1 << 30); for (int i = 1; i <= k; ++i) { pptr = -1; build(root = nnode(), 0, p - 1); modify(root, 0, p - 1, 0, 0); for (int j = 1; j <= n; ++j) { dp[i][j] = pre[j] + query(root, 0, p - 1, 0, pre[j]); if (pre[j] != p - 1) dp[i][j] = max(dp[i][j], pre[j] + query(root, 0, p - 1, pre[j] + 1, p - 1) + p); modify(root, 0, p - 1, pre[j], -pre[j] + dp[i - 1][j]); ; ; } } cout << dp[k][n] << endl; } |
#include <bits/stdc++.h> #pragma comment(linker, /STACK:102400000,102400000 ) using namespace std; int n, cnt; int ans[1005]; bool prime(int x) { for (int i = 2; i < x; i++) if (x % i == 0) return 0; return 1; } int main() { scanf( %d , &n); for (int i = 2; i <= n; i++) { if (!prime(i)) continue; int p = i; while (p <= n) { ans[++cnt] = p; p *= i; } } printf( %d n , cnt); for (int i = 1; i < cnt; i++) printf( %d , ans[i]); if (cnt) printf( %d n , ans[cnt]); } |
#include <bits/stdc++.h> using namespace std; int NewMessages(int right, int left, int k, int size) { int right_border = min(right + k, size - 1); int left_border = max(min(left + k, size - 1), max(0, right - k - 1)); return right_border - left_border; } int main() { int n, k; cin >> n >> k; vector<int> messages(n); for (auto& x : messages) { cin >> x; --x; } vector<int> counts(n, 0); counts[0] = min(static_cast<int>(counts.size()) - 1, k) + 1; for (int i = 1; i < counts.size(); ++i) { if (messages[i] == -1) { counts[i] = min(static_cast<int>(counts.size()) - 1, i + k) - max(0, i - k) + 1; } else { counts[i] = counts[messages[i]] + NewMessages(i, messages[i], k, static_cast<int>(counts.size())); } } for (auto x : counts) { cout << x << ; } cout << n ; } |
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const long long INFLL = 0x3f3f3f3f3f3f3f3fll; const int maxn = 2e5 + 5; const int mod = 1e9 + 7; int n, k; char ans[55][3]; int main() { cin >> n >> k; char ch1 = A , ch2 = a ; for (int i = 0; i < n - k + 1; i++) { string s; cin >> s; if (i == 0) { if (s == YES ) { for (int j = i; j < i + k; j++) { ans[j][0] = ch1; ans[j][1] = ch2++; if (ch2 > z ) ch1 = B , ch2 = a ; } } else { ans[i][0] = ans[i + k - 1][0] = ch1; ans[i][1] = ans[i + k - 1][1] = ch2++; for (int j = i + 1; j < i + k - 1; j++) { ans[j][0] = ch1; ans[j][1] = ch2++; if (ch2 > z ) ch1 = B , ch2 = a ; } } } else { if (s == YES ) { ans[i + k - 1][0] = ch1; ans[i + k - 1][1] = ch2++; } else { ans[i + k - 1][0] = ans[i][0]; ans[i + k - 1][1] = ans[i][1]; } if (ch2 > z ) ch1 = B , ch2 = a ; } } for (int i = 0; i < n; i++) { cout << ans[i][0] << ans[i][1] << ; } cout << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m, k, a[100][100], ans, ma[100]; bool mark[10]; int seen[100000]; void BT(int n) { if (n > ::n) { int ans = 0; for (int i = 1; i <= ::n; i++) { if (a[i][ma[i]] == 0 && ma[i] != 0) return; ans += a[i][ma[i]]; } seen[ans]++; return; } for (int i = 0; i <= ::n; i++) { if (i == 0 || !mark[i]) { ma[n] = i; mark[i] = true; BT(n + 1); ma[n] = 0; mark[i] = false; } } return; } int main() { cin >> n >> m >> k; for (int i = 0; i < m; i++) { int A, b, c; cin >> A >> b >> c; a[A][b] = c; } int t = 0, i = 0; BT(1); seen[0] = 1; while (t < k) { if (seen[i]) t += seen[i]; i++; } cout << i - 1 << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 300005; int f[maxn], n; struct struc { long long a[maxn]; int go[19][maxn], nxt[maxn]; void build(int ty = 0) { if (!ty) { for (int i = 1; i <= n; i++) scanf( %lld , &a[i]); for (int i = n; i; i--) a[i] += a[i + 1]; } static map<long long, int> pos; pos.clear(); pos[0] = n + 1; for (int i = n; i; i--) nxt[i] = pos[a[i]], pos[a[i]] = i; } } sav[3]; vector<pair<int, int> > up[maxn], dow[maxn]; int c[maxn]; void push(int i, int j, int v) { c[max(i, j)] = max(c[max(i, j)], v); if (i <= j) up[i].push_back(pair<int, int>(j, v)); if (j <= i) dow[j].push_back(pair<int, int>(i, v)); } void trans(int i, int j, int v) { push(i + 1, j, v), push(i, j + 1, v); if (sav[0].nxt[i]) push(sav[0].nxt[i], j, v + 1); if (sav[1].nxt[j]) push(i, sav[1].nxt[j], v + 1); if (i == j && sav[2].nxt[i]) push(sav[2].nxt[i], sav[2].nxt[i], v + 1); } int main() { scanf( %d , &n); sav[0].build(), sav[1].build(); for (int i = 1; i <= n; i++) sav[2].a[i] = sav[0].a[i] + sav[1].a[i]; sav[2].build(1); for (int i = 1; i <= n; i++) { int go_up = (1 << 30), go_dow = (1 << 30); trans(i, i, c[i]); for (auto p : up[i]) if (p.second == c[i] + 1) go_dow = min(go_dow, p.first); for (auto p : dow[i]) if (p.second == c[i] + 1) go_up = min(go_up, p.first); if (go_up <= n + 1) trans(go_up, i, c[i] + 1); if (go_dow <= n + 1) trans(i, go_dow, c[i] + 1); } printf( %d n , c[n + 1]); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, l; string s; cin >> n; for (int i = 0; i < n; i++) { cin >> l >> s; int first = s[0] - 0 ; int second = s[1] - 0 ; if (s.length() == 2 && first >= second) { cout << NO << endl; continue; } cout << YES << endl; cout << 2 << endl; cout << s[0] << << s.substr(1) << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; template <typename arg> void debugger(const char* varname, arg&& var) { cout << varname << = << var << endl; } template <typename arg, typename... args> void debugger(const char* varnames, arg&& var, args&&... vars) { const char* comma = strchr(varnames + 1, , ); cout.write(varnames, comma - varnames) << = << var << | ; int sz = 1; if (isspace(comma[sz])) sz++; debugger(comma + sz, vars...); } using ll = long long; using ii = pair<ll, ll>; using vi = vector<ll>; extern const int INF = INT_MAX - 5; extern const ll LINF = LLONG_MAX - 5; extern const int MOD = 1e9 + 7; extern const int N = 2e5 + 200; int t, n; string s; vi find_min() { vi ret(n), pos; if (s[0] == < ) pos.emplace_back(0); for (int i = 1; i < (int)s.size(); ++i) { if (s[i - 1] == > and s[i] == < ) { pos.emplace_back(i); } } if (s[(int)s.size() - 1] == > ) pos.emplace_back(n - 1); int c = 1; for (int i = (int)pos.size() - 1; i >= 0; --i) { ret[pos[i]] = c; c++; } for (int i = pos[pos.size() - 1] + 1; i < n; ++i) { ret[i] = c; c++; } for (int i = (int)pos.size() - 1; i >= 1; --i) { int r = pos[i] - 1, l = pos[i - 1]; while (s[r] == > and s[r - 1] == > ) { ret[r] = c; r--, c++; } while (s[l] == < and s[l + 1] == < ) { ret[l + 1] = c; l++, c++; } ret[l + 1] = c; c++; } for (int i = pos[0] - 1; i >= 0; --i) { ret[i] = c; c++; } return ret; } vi find_max() { vi ret(n); vector<ii> rng; int ant = 0; for (int i = 0; i < (int)s.size(); ++i) { if (s[i] == < ) { rng.emplace_back(ant, i); ant = i + 1; } } rng.emplace_back(ant, n - 1); int c = 1; for (auto i : rng) { ret[i.second] = c; c++; } for (auto i : rng) { for (int k = i.second - 1; k >= i.first; --k) { ret[k] = c; c++; } } return ret; } int main() { cin >> t; while (t--) { cin >> n >> s; vi mn = find_min(); vi mx = find_max(); for (int i = 0; i < n; ++i) { if (i != 0) cout << ; cout << mn[i]; } cout << n ; for (int i = 0; i < n; ++i) { if (i != 0) cout << ; cout << mx[i]; } cout << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; const int inf = 2e9 + 9; int n, m, p; pair<int, int> a[maxn], b[maxn]; pair<pair<int, int>, int> mon[maxn]; struct segment_tree { int val[maxn * 4], lazy[maxn * 4]; void build(int x, int l, int r) { if (l == r) { val[x] = -b[l].second; return; } int mid = (l + r) / 2; build(x * 2, l, mid); build(x * 2 + 1, mid + 1, r); val[x] = max(val[x * 2], val[x * 2 + 1]); } void push(int x) { if (lazy[x] == 0) return; int t = lazy[x]; val[x * 2] += t; lazy[x * 2] += t; val[x * 2 + 1] += t; lazy[x * 2 + 1] += t; lazy[x] = 0; } void upd(int x, int l, int r, int L, int R, int k) { if (L > r || l > R || L > R) return; if (l >= L && r <= R) { val[x] += k; lazy[x] += k; return; } push(x); int mid = (l + r) / 2; upd(x * 2, l, mid, L, R, k); upd(x * 2 + 1, mid + 1, r, L, R, k); val[x] = max(val[x * 2], val[x * 2 + 1]); } int get() { return val[1]; } } tree; void read_input() { cin >> n >> m >> p; for (int i = 1; i <= n; ++i) cin >> a[i].first >> a[i].second; for (int i = 1; i <= m; ++i) cin >> b[i].first >> b[i].second; for (int i = 1; i <= p; ++i) { int x, y, z; cin >> x >> y >> z; mon[i] = make_pair(pair<int, int>(x, y), z); } } void init() { sort(a + 1, a + n + 1); sort(b + 1, b + m + 1); sort(mon + 1, mon + p + 1); tree.build(1, 1, m); } void solve() { int cur = 1; int ans = -inf; for (int i = 1; i <= n; ++i) { int ap = a[i].first; while (cur <= p && mon[cur].first.first < ap) { int p = upper_bound(b + 1, b + m + 1, pair<int, int>(mon[cur].first.second, inf)) - b; tree.upd(1, 1, m, p, m, mon[cur].second); ++cur; } ans = max(ans, -a[i].second + tree.get()); } cout << ans << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); read_input(); init(); solve(); } |
#include <bits/stdc++.h> #pragma comment(linker, /STACK:1024000000,1024000000 ) using namespace std; int main() { cin.tie(0); ios::sync_with_stdio(false); int n, w; cin >> w >> n; if (w <= 3) { cout << YES << endl; return 0; } while (n > 0) { if (n % w > 1 && n % w < w - 1) { cout << NO << endl; return 0; } n = (n + 1) / w; } cout << YES << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int Read() { char c = getchar(); while (c < 0 || c > 9 ) c = getchar(); int t = c - 0 ; c = getchar(); while (c >= 0 && c <= 9 ) t = (t << 3) + (t << 1) + c - 0 , c = getchar(); return t; } int n, Q, s; int Head[900001], To[3200001], Next[3200001], t1; int W[3200001]; void add(int a, int b, int c) { t1++; Next[t1] = Head[a]; Head[a] = t1; To[t1] = b; W[t1] = c; } void Build(int bh, int x, int y) { if (x == y) { add(bh, n * 8 + x, 0), add(n * 8 + x, bh, 0); add(n * 4 + bh, n * 8 + x, 0), add(n * 8 + x, n * 4 + bh, 0); return; } add(bh, bh * 2, 0), add(bh, bh * 2 + 1, 0); add(n * 4 + bh * 2, n * 4 + bh, 0), add(n * 4 + bh * 2 + 1, n * 4 + bh, 0); int mid = (x + y) / 2; Build(bh * 2, x, mid); Build(bh * 2 + 1, mid + 1, y); } void add(int bh, int x, int y, int a, int b, int c, bool t, int u) { if (x == a && y == b) { if (!t) add(n * 8 + u, bh, c); else add(n * 4 + bh, n * 8 + u, c); return; } int mid = (x + y) / 2; if (b <= mid) add(bh * 2, x, mid, a, b, c, t, u); else if (a > mid) add(bh * 2 + 1, mid + 1, y, a, b, c, t, u); else add(bh * 2, x, mid, a, mid, c, t, u), add(bh * 2 + 1, mid + 1, y, mid + 1, b, c, t, u); } struct Node { int id; long long d; bool operator<(const Node& a) const { return a.d < d; } } t; priority_queue<Node> q; bool vis[900001]; long long dis[900001]; void Dij() { for (int i = 1; i <= n * 9; i++) dis[i] = 1e18; dis[n * 8 + s] = 0; t.id = n * 8 + s; t.d = 0; q.push(t); while (!q.empty()) { while (!q.empty() && vis[q.top().id]) q.pop(); if (q.empty()) break; int curr = q.top().id; q.pop(); vis[curr] = 1; for (int i = Head[curr]; i; i = Next[i]) { if (!vis[To[i]]) { if (dis[To[i]] > dis[curr] + W[i]) { dis[To[i]] = dis[curr] + W[i]; t.id = To[i]; t.d = dis[To[i]]; q.push(t); } } } } } int main() { n = Read(); Q = Read(); s = Read(); Build(1, 1, n); for (int i = 1, t, u, v, w, l, r; i <= Q; i++) { t = Read(); if (t == 1) { u = Read(); v = Read(); w = Read(); add(n * 8 + u, n * 8 + v, w); } else if (t == 2) { u = Read(); l = Read(); r = Read(); w = Read(); add(1, 1, n, l, r, w, 0, u); } else { u = Read(); l = Read(); r = Read(); w = Read(); add(1, 1, n, l, r, w, 1, u); } } Dij(); for (int i = n * 8 + 1; i <= n * 8 + n; i++) { if (!vis[i]) printf( -1 ); else printf( %lld , dis[i]); } return 0; } |
#include <bits/stdc++.h> const int INF = (int)1e9 + 7; int f(const std::vector<int> &arr, int k, int d) { std::list<std::pair<int, int>> events, se; for (auto &it : arr) { events.push_back({it - d, 0}); se.push_back({it + d, 1}); } events.merge(se); int bal = 0; for (auto it : events) { bal += (it.second ? -1 : +1); if (bal > k) { return it.first; } } return -INF; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); int t; std::cin >> t; while (t--) { int n, k; std::cin >> n >> k; std::vector<int> arr(n); for (auto &it : arr) { std::cin >> it; } std::sort((arr).begin(), (arr).end()); int low = -1, high = (int)1e9; while (high - low > 1) { int mid = (low + high) / 2; if (f(arr, k, mid) > -INF) { high = mid; } else { low = mid; } } std::cout << f(arr, k, high) << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; char a[n][m]; int num = 0; int mn[m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } for (int i = 0; i < m; i++) { num = 0; for (int j = 0; j < n; j++) { num = max(num, a[j][i] - 0 ); } mn[i] = num; } int c = 0, ans = 0; for (int i = 0; i < n; i++) { c = 0; for (int j = 0; j < m; j++) { if (mn[j] == a[i][j] - 0 ) { c = 3; } } if (c == 3) { ans++; } } cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 105; char arr[N], dp[N][N * N], ndp[N][N * N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; int sz = 0; for (int i = 0; i < n; i++) { int x; cin >> x; arr[x]++; if (arr[x] == 1) sz++; } dp[0][0] = 1; for (int i = 1; i < N; i++) { for (int x1 = 0; x1 < N; x1++) for (int x2 = 0; x2 < N * N; x2++) ndp[x1][x2] = 0; for (int f = 0; f <= arr[i]; f++) for (int x1 = 0; x1 < N; x1++) for (int x2 = 0; x2 < N * N; x2++) if (x2 + f * i < N * N && x1 + f < N) ndp[x1 + f][x2 + f * i] = min(100, ndp[x1 + f][x2 + f * i] + dp[x1][x2]); for (int x1 = 0; x1 < N; x1++) for (int x2 = 0; x2 < N * N; x2++) dp[x1][x2] = ndp[x1][x2]; } if (sz <= 2) { cout << n << n ; return 0; } int ans = 0; for (int i = 1; i < N; i++) for (int f = 1; f <= arr[i]; f++) if (dp[f][f * i] == 1) ans = max(ans, f); cout << ans << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; ifstream in( in.txt ); bool needToDelete(char c) { return tolower(c) == a || tolower(c) == o || tolower(c) == y || tolower(c) == e || tolower(c) == u || tolower(c) == i ; } int main(int argc, char** argv) { string s; cin >> s; string ans; for (int i = 0; i < s.size(); i++) { if (!needToDelete(s[i])) { ans += . ; ans += tolower(s[i]); } } cout << ans; return 0; } |
#include <bits/stdc++.h> long long MAX(long long x, long long y) { return (x > y) ? x : y; } long long MIN(long long x, long long y) { return (x < y) ? x : y; } const int MAXN = 123; const int MAXNM = 223; const int MAXK = 7; const int inf = 0x3f3f3f3f; using namespace std; inline int Read() { register int x = 0; bool flag = false; register char ch = getchar(); while (ch < 0 || ch > 9 ) { if (ch == - ) flag = true; ch = getchar(); } if (flag) { while ( 0 <= ch && ch <= 9 ) { x = x * 10 - ch + 0 ; ch = getchar(); } } else { while ( 0 <= ch && ch <= 9 ) { x = x * 10 + ch - 0 ; ch = getchar(); } } return x; } int N, M, K; int cg(int x, int y) { return (x - 1) * M + y; } pair<int, int> split(int z) { int y = (z - 1) % M + 1; return make_pair((z - y) / M + 1, y); } int dirx[4] = {1, 0, -1, 0}; int diry[4] = {0, 1, 0, -1}; int trans(int z, int dir) { pair<int, int> p = split(z); p = make_pair(p.first + dirx[dir], p.second + diry[dir]); if (1 <= p.first && p.first <= N && 1 <= p.second && p.second <= M) return cg(p.first, p.second); else return -1; } int a[MAXNM]; int f[MAXNM][(1 << MAXK) - 1]; vector<pair<int, int> > frm[MAXNM][(1 << MAXK) - 1]; int ele[MAXK]; queue<int> q; bool inq[MAXNM]; void spfa(int s) { while (!q.empty()) { int hd = q.front(); q.pop(); inq[hd] = false; for (int i = 0; i <= 3; i++) { int v = trans(hd, i); if (~v) { if (f[v][s] > f[hd][s] + a[v]) { f[v][s] = f[hd][s] + a[v]; frm[v][s].clear(); frm[v][s].push_back(make_pair(hd, s)); if (!inq[v]) { q.push(v); inq[v] = true; } } } } } } char mp[MAXN][MAXN]; void print(int i, int s) { pair<int, int> p = split(i); mp[p.first][p.second] = X ; for (auto &v : frm[i][s]) print(v.first, v.second); } int main() { memset(f, 0x3f, sizeof(f)); N = Read(); M = Read(); K = Read(); for (int i = 1; i <= N; i++) for (int j = 1; j <= M; j++) mp[i][j] = . ; for (int i = 1; i <= N * M; i++) a[i] = Read(); for (int i = 0; i <= K - 1; i++) { int x = Read(), y = Read(); ele[i] = cg(x, y); f[ele[i]][1 << i] = a[ele[i]]; } for (int s = 1; s <= (1 << K) - 1; s++) { for (int ss = s & (s - 1); ss; ss = s & (ss - 1)) for (int i = 1; i <= N * M; i++) if (f[i][s] > f[i][ss] + f[i][s ^ ss] - a[i]) { f[i][s] = f[i][ss] + f[i][s ^ ss] - a[i]; frm[i][s].clear(); frm[i][s].push_back(make_pair(i, ss)); frm[i][s].push_back(make_pair(i, s ^ ss)); } for (int i = 1; i <= N * M; i++) if (f[i][s] < inf) { q.push(i); inq[i] = true; } spfa(s); } int ans = inf, id; for (int i = 1; i <= N * M; i++) if (f[i][(1 << K) - 1] < ans) ans = f[id = i][(1 << K) - 1]; printf( %d n , ans); print(id, (1 << K) - 1); for (int i = 1; i <= N; i++) printf( %s n , mp[i] + 1); return 0; } |
#include <bits/stdc++.h> using namespace std; vector<int> getPrimes(int n) { vector<bool> sieve(n + 1, true); sieve[0] = sieve[1] = false; for (int i = 2; i * i <= n; i++) if (sieve[i]) for (int j = i * i; j <= n; j += i) sieve[j] = false; vector<int> primes; for (int i = 2; i <= n; i++) if (sieve[i]) primes.push_back(i); return primes; } void storePrimes(int n, unordered_map<int, int> &m, vector<int> &primes) { for (int p : primes) { if (p * p > n) break; while (n % p == 0) { m[p]++; n /= p; } if (n == 1) break; } if (n > 1) m[n]++; } void removePrimes(int n, unordered_map<int, int> &m, vector<int> &primes) { for (int p : primes) { if (p * p > n) break; while (n % p == 0) { m[p]--; n /= p; } if (n == 1) break; } if (n > 1) m[n]--; } bool isAtleast(unordered_map<int, int> &m1, unordered_map<int, int> &m2) { for (auto it = m2.begin(); it != m2.end(); it++) { int num = it->first, freq = it->second; if (m1.find(num) == m1.end() or m1[num] < freq) return false; } return true; } void printMap(unordered_map<int, int> &m) { for (auto it = m.begin(); it != m.end(); it++) cout << it->first << : << it->second << n ; } long long int solve(int arr[], int n, int k) { vector<int> primes = getPrimes(1000003); unordered_map<int, int> mapK, currMap; storePrimes(k, mapK, primes); int i = 0, j = 0; long long int count = 0; while (j < n) { storePrimes(arr[j], currMap, primes); if (isAtleast(currMap, mapK)) { while (i <= j and isAtleast(currMap, mapK)) { count += (n - j); removePrimes(arr[i], currMap, primes); i++; } } j++; } return count; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; int arr[n]; for (int i = 0; i < n; i++) cin >> arr[i]; long long int ans = solve(arr, n, k); cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; int n, q, p, p1, maxi, a[1000000], idx; stack<int> t, t1, id; int main() { scanf( %d , &n); for (int i = 0; i < n; i++) scanf( %d , &a[i]); maxi = -1; id.push(0); t.push(200000); t1.push(-1); for (int i = 1; i < n; i++) { p = 0; q = 5; while (q == 5) { idx = id.top(); p1 = t.top(); p = t1.top() + 1; if (a[idx] > a[i] && p <= p1) { q = 6; t1.top() = p; if (p > maxi) maxi = p; id.push(i); t.push(p); t1.push(-1); } else if (p1 == 200000) { q = 7; id.push(i); t.push(200000); t1.push(-1); } else { id.pop(); t.pop(); t1.pop(); } } } printf( %d , maxi + 1); return 0; } |
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) #pragma GCC optimize( Ofast ) #pragma GCC optimize( unroll-loops ) using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); int i, n, m; cin >> n >> m; vector<int> a(n + m); for (i = 0; i < n + m; i++) { cin >> a[i]; } set<int> tax, per; for (i = 0; i < n + m; i++) { int t; cin >> t; if (t == 0) { per.insert(a[i]); } else { tax.insert(a[i]); } } map<int, int> ans; for (auto cur : per) { auto right = tax.upper_bound(cur); auto left = --tax.upper_bound(cur); if (left == tax.end()) { ans[*right]++; } else if (right == tax.end()) { ans[*left]++; } else { int l = *left, r = *right; if (cur - l <= r - cur) { ans[l]++; } else { ans[r]++; } } } for (auto cur : tax) { cout << ans[cur] << ; } return 0; } |
#include <bits/stdc++.h> using namespace std; auto fio = (ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)); int32_t main() { long long n; cin >> n; vector<long long> g(n); for (long long& x : g) cin >> x; vector<vector<long long>> idx(8192); for (long long i = 0; i < n; i++) idx[g[i]].push_back(i); vector<long long> mn(8192, n); mn[0] = -1; for (long long i = 0; i <= 5000; i++) { if (idx[i].empty()) continue; for (long long j = 0; j < 8192; j++) { if (mn[j] <= mn[i ^ j] || mn[i ^ j] >= idx[i].back()) continue; auto it = upper_bound(idx[i].begin(), idx[i].end(), mn[i ^ j]); if (it != idx[i].end() && *it < mn[j]) mn[j] = *it; } } cout << 8192 - count(mn.begin(), mn.end(), n) << endl; for (long long i = 0; i < 8192; i++) if (mn[i] != n) cout << i << ; cout << endl; } |
#include <bits/stdc++.h> using namespace std; struct _ { _() { ios_base::sync_with_stdio(0); } } _; template <class T> void PV(T a, T b) { while (a != b) cout << *a++, cout << (a != b ? : n ); } template <class T> inline bool chmin(T &a, T b) { return a > b ? a = b, 1 : 0; } template <class T> inline bool chmax(T &a, T b) { return a < b ? a = b, 1 : 0; } const int inf = 0x3f3f3f3f; const int mod = int(1e9) + 7; const int N = 3 * 111111; int n, q; int d[N]; int p[N]; int v[N]; void add(int first, int t) { if (first == 0) return; while (first <= n) { v[first] += t; first += first & (first ^ (first - 1)); } } int sum(int first) { int r = 0; while (first > 0) { r += v[first]; first -= first & (first ^ (first - 1)); } return r; } int main() { cin >> n; p[0] = 0, p[n + 1] = n + 1; for (int i = 1; i <= n; i++) cin >> d[i], p[d[i]] = i; for (int i = 1; i <= n; i++) if (p[d[i] - 1] > i) add(d[i], 1); cin >> q; while (q--) { int op, l, r, t; cin >> op >> l >> r; if (op == 1) { cout << sum(r) - sum(l) + 1 << endl; } else { if (d[l] > d[r]) { t = p[d[r] - 1]; if (l < t && t < r) add(d[r], 1); t = p[d[r] + 1]; if (l < t && t < r) add(d[r] + 1, -1); t = p[d[l] + 1]; if (l < t && t < r) add(d[l] + 1, 1); t = p[d[l] - 1]; if (l < t && t < r) add(d[l], -1); if (d[l] == d[r] + 1) add(d[l], -1); } else if (d[l] < d[r]) { t = p[d[l] - 1]; if (l < t && t < r) add(d[l], -1); t = p[d[l] + 1]; if (l < t && t < r) add(d[l] + 1, 1); t = p[d[r] + 1]; if (l < t && t < r) add(d[r] + 1, -1); t = p[d[r] - 1]; if (l < t && t < r) add(d[r], 1); if (d[l] == d[r] - 1) add(d[r], 1); } swap(d[l], d[r]); p[d[l]] = l, p[d[r]] = r; } } return 0; } |
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) #pragma GCC target( popcnt ) using namespace std; mt19937 bruh(chrono::steady_clock::now().time_since_epoch().count()); const long long N = 2e5 + 100; const long long mod = 1e9 + 7; const long long big = 1e17; long long t, n, h; long long a[N]; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> t; while (t--) { cin >> n >> h; for (int i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + n + 1); long long l = 1, r = 1e18, ans = h; while (l <= r) { long long m = (l + r) >> 1ll; long long sum = 0, rtst = 0; for (int i = 1; i <= n; i++) { long long l = max(a[i] - 1ll, rtst), r = a[i] + m - 1; sum = sum + (r - l); rtst = r; } if (sum >= h) r = m - 1ll, ans = m; else l = m + 1ll; } cout << ans << n ; } } |
#include <bits/stdc++.h> using namespace std; int in(char &c) { return scanf( %c , &c); } int in(char s[]) { return scanf( %s , s); } int in(int &x) { return scanf( %d , &x); } int in(long long &x) { return scanf( %lld , &x); } int in(double &x) { return scanf( %lf , &x); } int in(pair<int, int> &p) { return scanf( %d%d , &p.first, &p.second); } int in(pair<long long, long long> &p) { return scanf( %lld%lld , &p.first, &p.second); } int in(pair<double, double> &p) { return scanf( %lf%lf , &p.first, &p.second); } int in(long double &a) { return scanf( %Lf , &a); } int in(int &x, long long &y) { return scanf( %d%lld , &x, &y); } int in(long long &x, int &y) { return scanf( %lld%d , &x, &y); } int in(int &x, int &y) { return scanf( %d%d , &x, &y); } int in(long long &x, long long &y) { return scanf( %lld%lld , &x, &y); } int in(int &n, char s[]) { return scanf( %d%d , &n, s); } int in(char a[], char b[]) { return scanf( %s%s , a, b); } int in(double &x, double &y) { return scanf( %lf%lf , &x, &y); } int in(int &x, int &y, int &z) { return scanf( %d%d%d , &x, &y, &z); } int in(long long &x, long long &y, long long &z) { return scanf( %lld%lld%lld , &x, &y, &z); } int in(double &x, double &y, double &z) { return scanf( %lf%lf%lf , &x, &y, &z); } int in(int &x, int &y, int &z, int &k) { return scanf( %d%d%d%d , &x, &y, &z, &k); } int in(long long &x, long long &y, long long &z, long long &k) { return scanf( %lld%lld%lld%lld , &x, &y, &z, &k); } int in(double &x, double &y, double &z, double &k) { return scanf( %lf%lf%lf%lf , &x, &y, &z, &k); } const int N = 3e5 + 10, M = 1e3 + 10, mod = 1e9 + 7; const double eps = 1e-6, Pi = acos(-1.0); int a[N], b[N], L[N], R[N], s[N]; vector<int> vc[N]; int check(int x, int y) { return (L[y] > R[x]); } int query(int x, int p) { x = max(0, x); p = max(0, p); int pos = upper_bound(vc[x].begin(), vc[x].end(), p) - vc[x].begin(); return pos; } int main() { int T, t; in(T); t = T; while (T--) { int n; in(n); for (int i = 1; i <= n; i++) { in(a[i]); b[i] = a[i]; } sort(b + 1, b + 1 + n); int m = unique(b + 1, b + 1 + n) - b; for (int i = 1; i <= n; i++) a[i] = lower_bound(b + 1, b + m, a[i]) - b, s[a[i]]++, vc[a[i]].push_back(i); for (int i = n; i >= 1; i--) L[a[i]] = i; for (int i = 1; i <= n; i++) s[i] += s[i - 1], R[a[i]] = i; m--; int l, r, ans = 0; l = r = 1; L[m + 1] = L[m]; R[m + 1] = R[m]; for (int i = 1; i <= m; i++) { while (r <= m && check(r, r + 1)) r++; int t = query(l - 1, L[l]) + (int)vc[r + 1].size() - query(r + 1, R[r]); ans = max(ans, s[r] - s[l - 1] + t); if (l == r) { for (int j = 0; j < vc[l].size(); j++) { ans = max(ans, (int)vc[l].size() + query(l - 1, vc[l][j]) - j); } } ans = max(ans, s[r] - s[l - 1] + query(l - 1, L[l])); if (r == m) break; l = r + 1; r++; } printf( %d n , n - ans); for (int i = 1; i <= n; i++) s[i] = 0, vc[i].clear(); } return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m, i, j, k = -1, c, d; int main() { scanf( %d%d , &n, &m); for (i = 1; i <= m; i++) { scanf( %d %d , &c, &d); if (k == -1 || (d - c + 1) < k) { k = d - c + 1; } } printf( %d n , k); c = 0; for (i = 1; i <= n; i++) { printf( %d , c); c = (c + 1) % k; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long xa, ya, xb, yb, xc, yc; cin >> xa >> ya >> xb >> yb >> xc >> yc; long long s1 = (ya - yb) * (xa - xc); long long s2 = (xa - xb) * (ya - yc); if (s1 == s2) { cout << TOWARDS ; return 0; } if (s1 > s2) cout << RIGHT ; else cout << LEFT ; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; int big = 0, small = 1000000000; int a[1000]; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] > big) { big = a[i]; } if (a[i] < small) { small = a[i]; } } cout << big - small + 1 - n; } |
#include <bits/stdc++.h> using namespace std; int a, ta, b, tb, hh, mm, res1, res2; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> a >> ta >> b >> tb; char tmp; cin >> hh >> tmp >> mm; hh -= 5; mm += hh * 60; if (mm - tb >= 0) res1 = (mm - tb) / b + 1; mm += ta; mm = min(mm, (24 - 5) * 60); mm--; res2 = mm / b + 1; cout << res2 - res1 << endl; } |
#include <bits/stdc++.h> using namespace std; int maxl[300005][20], minl[300005][20]; int vis[2 * 300005]; int a[300005]; int lmax[300005], lmin[300005], rmax[300005], rmin[300005]; int min(int a, int b) { if (a > b) return b; return a; } int max(int a, int b) { if (a > b) return a; return b; } void S_table(int n) { int l = int(log((double)n) / log(2.0)); for (int j = 1; j <= l; j++) { for (int i = 1; i + (1 << (j - 1)) - 1 <= n; ++i) { maxl[i][j] = max(maxl[i][j - 1], maxl[i + (1 << (j - 1))][j - 1]); minl[i][j] = min(minl[i][j - 1], minl[i + (1 << (j - 1))][j - 1]); } } } int w[300005]; int rmq(int l, int r) { int k = w[r - l + 1]; int a1 = max(maxl[l][k], maxl[r - (1 << k) + 1][k]); return a1; } void init(void) { int i; for (i = 1; i <= 300002; i++) { int k = int(log((double)(i)) / log(2.0)); w[i] = k; } } int rmq1(int l, int r) { int k = w[r - l + 1]; int a2 = min(minl[l][k], minl[r - (1 << k) + 1][k]); return a2; } struct pi { int a, b; } pp[300005]; int cmp(pi a, pi b) { return a.a < b.a; } long long dfs(int x, int y) { if (x == y) return 1; if (y - x == 1) { if (abs(pp[y].b - pp[x].b) == 1) return 3; return 2; } long long s = 0; s = dfs(x, (x + y) / 2); s += dfs((x + y) / 2 + 1, y); int mid = (x + y) / 2; int i; lmin[mid + 1] = 1000000000; lmax[mid + 1] = -1000000000; for (i = mid; i >= x; i--) { lmin[i] = min(lmin[i + 1], pp[i].b); lmax[i] = max(lmax[i + 1], pp[i].b); } rmin[mid] = 1000000000; rmax[mid] = -1000000000; for (i = mid + 1; i <= y; i++) { rmin[i] = min(rmin[i - 1], pp[i].b); rmax[i] = max(rmax[i - 1], pp[i].b); } for (i = mid; i >= x; i--) { int a, b; a = lmax[i]; b = lmin[i]; if (a - b + 1 > mid - i + 1) { int p = a - b + 1 - (mid - i + 1); if (p <= y - mid) { if (rmax[mid + p] < a && rmin[mid + p] > b) { s++; } } } } for (i = mid + 1; i <= y; i++) { int a, b; a = rmax[i]; b = rmin[i]; if (a - b + 1 > i - mid) { int p = a - b + 1 - (i - mid); if (p <= mid - x + 1) { if (lmax[mid - p + 1] < a && lmin[mid - p + 1] > b) { s++; } } } } int ss, tt; ss = mid + 1; tt = mid + 1; for (i = mid; i >= x; i--) { int a = lmax[i]; int b = lmin[i]; while (tt <= y && rmin[tt] >= b) { vis[rmax[tt] - tt + 300005]++; tt++; } while (ss <= y && rmax[ss] < a) { vis[rmax[ss] - ss + 300005]--; ss++; } if (tt <= ss) continue; int p = vis[lmin[i] - i + 300005]; if (p > 0) s += p; } ss = mid; tt = mid; for (i = mid + 1; i <= y; i++) { vis[rmax[i] - i + 300005] = 0; } for (i = mid + 1; i <= y; i++) { int a = rmax[i]; int b = rmin[i]; while (tt >= x && lmin[tt] >= b) { vis[lmax[tt] + tt]++; tt--; } while (ss >= x && lmax[ss] < a) { vis[lmax[ss] + ss]--; ss--; } if (tt >= ss) continue; int p = vis[rmin[i] + i]; if (p > 0) s += p; } for (i = x; i <= mid; i++) { vis[lmax[i] + i] = 0; } return s; } int main() { int i, n; init(); cin >> n; for (i = 1; i <= n; i++) { int a, b; scanf( %d%d , &a, &b); pp[a].b = b; } for (i = 1; i <= n; i++) { maxl[i][0] = pp[i].b; minl[i][0] = pp[i].b; } S_table(n); cout << dfs(1, n) << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; string K; cin >> K; if (K.size() == 1 and k != 0) { cout << 0; return 0; } if (k == 0) { cout << K; return 0; } if (K[0] != 1 ) { K[0] = 1 ; k -= 1; for (int i = 1; i < n; ++i) { if (k == 0) { cout << K; return 0; } if (K[i] != 0 ) { K[i] = 0 ; k--; } } } else { for (int i = 1; i < n; ++i) { if (k == 0) { cout << K; return 0; } if (K[i] != 0 ) { K[i] = 0 ; k--; } } } cout << K; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n, p, a[100000], sum = 0; cin >> n >> p; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; sum %= p; } sum = (sum + p - (a[0] % p)) % p; int cur = a[0] % p, ans = cur + sum; for (int i = 1; i < n - 1; ++i) { cur = (cur + a[i]) % p, sum = (sum + p - (a[i] % p)) % p; ans = max(ans, cur + sum); } cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int r, c, i, j, flag = 0; cin >> r >> c; char a1[r][c]; for (i = 0; i < r; i++) { for (j = 0; j < c; j++) { cin >> a1[i][j]; } } for (i = 0; i < r; i++) { for (j = 0; j < c; j++) { if (a1[i][j] == . ) { a1[i][j] = D ; } } } for (i = 0; i < r; i++) { for (j = 0; j < c; j++) { if (a1[i][j] == S ) { if (i > 0) { if (a1[i - 1][j] == W ) { cout << No ; return 0; } } if (i < r - 1) { if (a1[i + 1][j] == W ) { cout << No ; return 0; } } if (j > 0) { if (a1[i][j - 1] == W ) { cout << No ; return 0; } } if (j < c - 1) { if (a1[i][j + 1] == W ) { cout << No ; return 0; } } } } } cout << Yes << endl; for (i = 0; i < r; i++) { for (j = 0; j < c; j++) { cout << a1[i][j]; } cout << endl; } } |
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) using namespace std; long long w, m, k; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); cin >> w >> m >> k; long long aa = 0; int nc = 0; while (aa < m) aa = aa * 10 + 9, ++nc; long long m2 = m; while (w) { if ((aa - m + 1) <= w / k / nc) { w = w - k * (aa - m + 1) * nc; ++nc; m = aa + 1; aa = aa * 10 + 9; } else { m = m + w / k / nc; break; } } cout << m - m2; return 0; } |
#include <bits/stdc++.h> using namespace std; int n, x; vector<int> w; vector<int> v; bool volt[1111]; int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) { cin >> x; v.push_back(x); } reverse(v.begin(), v.end()); for (int i = 0; i < v.size(); i++) { if (volt[v[i]] == false) { w.push_back(v[i]); volt[v[i]] = true; } } reverse(w.begin(), w.end()); cout << w.size() << endl; for (int i = 0; i < w.size(); i++) { cout << w[i] << ; } } |
#include <bits/stdc++.h> using namespace std; const int maxn = 2005; int n, m; int a[maxn]; int cnt[maxn], vis[maxn]; int ans2 = 0; int main() { scanf( %d%d , &n, &m); for (int i = 1; i <= n; i++) scanf( %d , &a[i]); int ans1 = n / m; int p = n % m; for (int i = 1; i <= n; i++) { if (a[i] > m) continue; if (cnt[a[i]] >= ans1) continue; cnt[a[i]]++; vis[i] = 1; } for (int i = 1; i <= n; i++) { if (vis[i]) continue; int Ans1 = 1e9, Ans2 = 1e9; for (int j = 1; j <= m; j++) { if (cnt[j] < Ans2) Ans2 = cnt[j], Ans1 = j; } if (Ans2 == ans1) continue; ans2++; cnt[Ans1]++, a[i] = Ans1; } cout << ans1 << << ans2 << endl; for (int i = 1; i <= n; i++) cout << a[i] << ; cout << endl; } |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.