func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; int fun(string s, int i) { int k = 0; while (s[i] == 0 & i >= 0) { k++; i--; } return k; } int main(int argc, char const *argv[]) { string s; int a, k = 0, count = 1; cin >> a; cin >> s; string ans = ; int i = s.size() - 1; if (s[i] == 0 ) { while (s[i] == 0 ) { ans = ans + 0 ; i--; } } while (i >= 0) { k = 0; if (s[i] == 1 ) { while (s[i] == 1 && i >= 0) { k++; i--; } ans = ans + char( 0 + k); } else if (s[i] == 0 ) { int t = fun(s, i); i = i - t; while (t > 1) { ans = ans + 0 ; t = t - 1; } } } reverse(ans.begin(), ans.end()); int myint1 = stoi(ans); cout << myint1; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX_N = 55; int n, m, dp[MAX_N][MAX_N][MAX_N][MAX_N]; char a[MAX_N][MAX_N]; int dfs(int x1, int y1, int x2, int y2) { if (dp[x1][y1][x2][y2] >= 0) { return dp[x1][y1][x2][y2]; } if (x1 == x2 && y1 == y2) { return dp[x1][y1][x2][y2] = a[x1][y1] == # ; } int &ans = dp[x1][y1][x2][y2]; ans = max(x2 - x1 + 1, y2 - y1 + 1); for (int i = x1; i < x2; ++i) { ans = min(ans, dfs(x1, y1, i, y2) + dfs(i + 1, y1, x2, y2)); } for (int j = y1; j < y2; ++j) { ans = min(ans, dfs(x1, y1, x2, j) + dfs(x1, j + 1, x2, y2)); } return ans; } int main() { scanf( %d , &n); memset(dp, -1, sizeof dp); for (int i = 1; i <= n; ++i) { scanf( %s , a[i] + 1); } printf( %d n , dfs(1, 1, n, n)); }
#include <bits/stdc++.h> using namespace std; const long long int delta = 1e9 + 5; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; int x; int d; cin >> x >> d; vector<long long int> ans; long long int cur = 1; for (int i = 31; i >= 0; i--) { long long int cnt = (1LL << i); while (x >= cnt) { x -= cnt; for (int j = 0; j < i; j++) { ans.push_back(cur); } cur += d; ans.push_back(cur); cur += d; } } cout << ans.size() << n ; for (auto a : ans) cout << a << ; cout << endl; ; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); string str; cin >> str; int n = str.length(); map<char, int> mp; for (int i = 0; i < n; i++) { mp[str[i]]++; } if (mp.size() % 2 == 1) cout << IGNORE HIM! ; else cout << CHAT WITH HER! ; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long MOD = 998244353LL, INV2 = (MOD + 1) / 2; const int MAXN = 500; long long DP[MAXN + 1][MAXN + 1]; int main() { ios::sync_with_stdio(0); cin.tie(0); int N, K; cin >> N >> K; for (int k = 1; k <= min(N, K); ++k) { DP[k][0] = 2; for (int n = 1; n <= N; ++n) { for (int m = n - 1; m >= 0 && n - m <= k; --m) DP[k][n] += DP[k][m]; DP[k][n] %= MOD; } } long long ans = 0; for (int k = 1; k <= N && k < K; ++k) { long long aux = (DP[k][N] + MOD - DP[k - 1][N]) * DP[min((K + k - 1) / k - 1, N)][N] % MOD; aux = aux * INV2 % MOD; ans += aux; ans %= MOD; } cout << ans << n ; return 0; }
#include <bits/stdc++.h> using namespace std; long long cubes[100001], x; long long my_pow(long long x) { return x * x * x; } pair<long long, long long> best; void rec(long long m, long long steps, long long subtracted) { if (m == 0) { best = max(best, make_pair(steps, subtracted)); return; } long long x = 1; while (my_pow(x + 1) <= m) ++x; rec(m - my_pow(x), steps + 1, subtracted + my_pow(x)); if (x - 1 >= 0) rec(my_pow(x) - 1 - my_pow(x - 1), steps + 1, subtracted + my_pow(x - 1)); } int main() { long long m; scanf( %I64d , &m); rec(m, 0, 0); printf( %I64d %I64d , best.first, best.second); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) using namespace std; const long long infl = 0x3f3f3f3f3f3f3f3fLL; const long long infi = 0x3f3f3f3f; mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); const long long mod = 1e9 + 7; const long long N = 1e6 + 5; void solve() { long long m; cin >> m; cout << (m / 4 + 1) % mod * ((m / 2 - m / 4 + 1) % mod + mod) % mod << n ; } signed main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << fixed << setprecision(15); long long t = 1; cin >> t; while (t--) solve(); return 0; } long long powm(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; } long long divide(long long a, long long b) { return (a % mod) * powm(b, mod - 2) % mod; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
#include <bits/stdc++.h> using namespace std; int ABS(int a) { if (a < 0) return (-a); return a; } void input() {} int n, table; int a[55]; double dp[55][55][55][55]; int aim; double f(int idx, int sum, int k, int cnt) { if (idx == n) { if (sum != aim || cnt == 0) return 0; return (k * cnt * 1.0) / ((double)(n)-k); } double &res = dp[idx][sum][k][cnt]; if (res > -0.5) return res; double invK = (k + 1.0) / (idx + 1.0); double invNK = (idx - k + 1.0) / (idx + 1.0); res = 0.0; if (sum + a[idx] <= aim) { res += invK * f(idx + 1, sum + a[idx], k + 1, cnt); } if (aim + a[idx] > table) cnt++; res += invNK * f(idx + 1, sum, k, cnt); return res; } int main() { input(); cin >> n; int sum = 0; for (int i = (0); i < (n); ++i) { cin >> a[i]; sum += a[i]; } cin >> table; if (sum <= table) { printf( %d n , n); return 0; } double ans = 0.0; for (int i = (1); i < (table + 1); ++i) { for (int I = (0); I < (55); ++I) for (int j = (0); j < (55); ++j) for (int k = (0); k < (55); ++k) for (int l = (0); l < (55); ++l) dp[I][j][k][l] = -1.0; aim = i; ans += f(0, 0, 0, 0); } printf( %.12lf n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 100010; const int MAXK = 20; int times[MAXN]; int dp[MAXN][MAXK]; int subtree[MAXN]; int marc[MAXN]; int prof[MAXN]; vector<int> graph[MAXN]; vector<pair<int, int>> arcs; void init_dfs(int u) { marc[u] = 1; for (int v : graph[u]) { if (marc[v]) continue; dp[v][0] = u; for (int i = 1; i < MAXK; ++i) dp[v][i] = dp[dp[v][i - 1]][i - 1]; prof[v] = prof[u] + 1; init_dfs(v); } } inline int lca(int a, int b) { if (prof[a] > prof[b]) swap(a, b); int d = prof[b] - prof[a]; for (int i = 0; i < MAXK; ++i) if (d & (1 << i)) b = dp[b][i]; if (a == b) return a; for (int i = MAXK - 1; i >= 0; --i) if (dp[a][i] != dp[b][i]) { a = dp[a][i]; b = dp[b][i]; } return dp[a][0]; } inline void add_path(int u, int v) { ++times[u]; --times[v]; } inline void add_arc(int u, int v) { graph[u].push_back(v); graph[v].push_back(u); arcs.push_back({u, v}); } void final_dfs(int u) { marc[u] = 2; subtree[u] = times[u]; for (int v : graph[u]) { if (marc[v] == 2) continue; final_dfs(v); subtree[u] += subtree[v]; } } int main() { int n; scanf( %d , &n); for (int i = 0; i < n - 1; ++i) { int u, v; scanf( %d%d , &u, &v); add_arc(u, v); } init_dfs(1); int k; scanf( %d , &k); for (int i = 0; i < k; ++i) { int u, v; scanf( %d%d , &u, &v); int w = lca(u, v); add_path(u, w); add_path(v, w); } final_dfs(1); for (auto [u, v] : arcs) { if (prof[u] < prof[v]) swap(u, v); printf( %d , subtree[u]); } printf( n ); }
#include <bits/stdc++.h> using namespace std; vector<pair<double, double>> v[4]; double x[3], y[3], r[3]; void add_point(int p, double x, double y) { for (int t = 0; t < v[p].size(); t++) if (fabs(x - v[p][t].first) < 1e-7 && fabs(y - v[p][t].second) < 1e-7) return; v[p].push_back(make_pair(x, y)); } int main() { int n, ans = 0; cin >> n; for (int i = 0; i < n; i++) cin >> x[i] >> y[i] >> r[i]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { double a = 2 * (x[j] - x[i]), b = 2 * (y[j] - y[i]), c = (a * a + b * b) / 4 + r[i] * r[i] - r[j] * r[j]; double d = a * a + b * b, x0 = a * c / d + x[i], y0 = b * c / d + y[i], md = r[i] * r[i] * d - c * c; if (i == j || md < 0) continue; b *= sqrt(md) / d, a *= sqrt(md) / d; for (int k = 0, p[3] = {3, i, j}; k < 3; k++) add_point(p[k], x0 + b, y0 - a), add_point(p[k], x0 - b, y0 + a); } ans += v[i].size() ? v[i].size() : 1; } cout << ans + 2 - (v[3].size() ? v[3].size() : 1) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long Mod = 1000000007; long long fp(long long a, long long k) { long long res = 1; while (k) { if (k & 1) res = res * a % Mod; a = a * a % Mod; k >>= 1; } return res; } int main() { long long n, m; scanf( %I64d%I64d , &n, &m); long long res = (m == 1 ? n + 1 : ((m * fp(2 * m - 1, n) - fp(m, n)) % Mod + Mod) * fp(m - 1, Mod - 2) % Mod); printf( %I64d , res); return 0; }
#include <bits/stdc++.h> using namespace std; const long long inf = 1e9 + 7; void solve() { long long n, m; cin >> n >> m; if (m == 1) { long long a = n * (n - 1) / 2; cout << a << << a << endl; } else { long long aux = n - (m - 1); long long maxi = aux * (aux - 1) / 2; long long mini = 0; aux = n / m; long long left = n - m * aux; if (left != 0) { mini = (aux + 1) * aux / 2 * left + (m - left) * (aux * (aux - 1)) / 2; } else { mini = (aux * (aux - 1)) / 2 * m; } cout << mini << << maxi << endl; } } int main() { ios::sync_with_stdio(0); cin.tie(0); int t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 25; long long n, ans[N]; pair<int, int> arr[N]; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n; for (int i = 0; i < n; i++) cin >> arr[i].first, arr[i].second = i; sort(arr, arr + n); for (int i = 0; i < n; i++) ans[arr[(i + 1) % n].second] = arr[i].first; for (int i = 0; i < n; i++) cout << ans[i] << ; return 0; }
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { return b == 0 ? a : gcd(b, a % b); } inline long long int lbt(long long int x) { return x & -x; } inline void fail() { cout << -1 << endl; exit(0); } int main() { long long int a, b, c, d, e, f, h, x(0); cin >> a >> b >> c >> d; e = a * b; f = c * d; h = gcd(e, f); e /= h, f /= h; if (e == 1) { while (c % 3 == 0 && f != lbt(f)) ++x, c /= 3, c <<= 1, f /= 3, f <<= 1; while (d % 3 == 0 && f != lbt(f)) ++x, d /= 3, d <<= 1, f /= 3, f <<= 1; ; } else if (f == 1) { while (a % 3 == 0 && e != lbt(e)) ++x, a /= 3, a <<= 1, e /= 3, e <<= 1; while (b % 3 == 0 && e != lbt(e)) ++x, b /= 3, b <<= 1, e /= 3, e <<= 1; ; } else if (e == lbt(e)) { h = f; while (h % 3 == 0) h /= 3; if (h != 1) fail(); ; while (c % 3 == 0 && f != lbt(f)) ++x, c /= 3, c <<= 1, f /= 3, f <<= 1; while (d % 3 == 0 && f != lbt(f)) ++x, d /= 3, d <<= 1, f /= 3, f <<= 1; ; } else if (f == lbt(f)) { h = e; while (h % 3 == 0) h /= 3; if (h != 1) fail(); ; while (a % 3 == 0 && e != lbt(e)) ++x, a /= 3, a <<= 1, e /= 3, e <<= 1; while (b % 3 == 0 && e != lbt(e)) ++x, b /= 3, b <<= 1, e /= 3, e <<= 1; ; } else fail(); while ((~c & 1) && e < f) ++x, c >>= 1, f >>= 1; while ((~d & 1) && e < f) ++x, d >>= 1, f >>= 1; ; while ((~a & 1) && f < e) ++x, a >>= 1, e >>= 1; while ((~b & 1) && f < e) ++x, b >>= 1, e >>= 1; ; FINAL: cout << x << endl << a << << b << endl << c << << d << endl; }
#include <bits/stdc++.h> using namespace std; long long n, l, r; long long gerar(long long n) { if (n == 0) { return 0; } return 2 * gerar(n / 2) + 1; } long long solve(long long esq, long long dir, long long l, long long r, long long valor) { long long mid = (esq + dir) / 2; long long res = 0; if (valor == 1) { return 1; } if (valor == 0) { return 0; } if (mid >= l && mid <= r) { res += valor % 2ll; } if (mid - 1 >= l) { res += solve(esq, mid - 1, l, r, valor / 2ll); } if (mid + 1 <= r) { res += solve(mid + 1, dir, l, r, valor / 2ll); } return res; } int main() { scanf( %lld %lld %lld , &n, &l, &r); long long qtd = gerar(n); printf( %lld n , solve(1, qtd, l, r, n)); }
#include <bits/stdc++.h> using namespace std; long long add(long long a, long long b) { a += b; if (a >= 998244353) { a -= 998244353; } return a; } long long sub(long long a, long long b) { a -= b; if (a < 0) { a += 998244353; } return a; } long long mul(long long a, long long b) { return (a * b) % 998244353; } void add_self(long long& a, long long b) { a = add(a, b); } void sub_self(long long& a, long long b) { a = sub(a, b); } void mul_self(long long& a, long long b) { a = mul(a, b); } const long long MAXN = 200010; long long fpower(long long x, long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } int main() { long long x, y; cin >> x >> y; cout << fpower(2, x + y, 998244353) << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int num[500005]; int maps[500005]; int to[500005]; int main() { ios::sync_with_stdio(0); int n, m; int l, r; scanf( %d%d , &n, &m); for (int i = 1; i <= n; i++) { scanf( %d , &num[i]); to[num[i]] = i; } for (int i = 1; i <= m; i++) { scanf( %d%d , &l, &r); if (to[r] < to[l]) { int tmp = r; r = l; l = tmp; } maps[to[r]] = max(to[l], maps[to[r]]); } l = 1; long long ans = 0; for (int i = 1; i <= n; i++) { if (maps[i] >= l) l = maps[i] + 1; ans += i - l + 1; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long n, k; cin >> n >> k; long long i = 1, cnt = 0; while (i < k && i < n) { cnt++; i = i * 2; } if (i < n) { cnt += (n - i) / k; if ((n - i) % k) cnt++; } cout << cnt << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using UL = unsigned int; using ULL = unsigned long long; using LL = long long; int N; int CB[300000]; int CN[300000]; int Bbuf = 0, Nbuf = 0; int F(int cn, int cb) { int ans = 0; for (int i = 0; i < (N); i++) { ans = max(ans, abs(cn - CN[i])); ans = max(ans, abs(cb - CB[i])); ans = max(ans, abs((cn - CN[i]) - (cb - CB[i]))); } return ans; } int FF(int cb) { int lN, rN; lN = -1; rN = 500010; if (cb == 0) lN = 0; while (lN + 2 < rN) { int m1N = (lN + lN + rN) / 3, m2N = (lN + rN + rN) / 3; if (F(m1N, cb) < F(m2N, cb)) rN = m2N; else lN = m1N; } Nbuf = lN + 1; return F(Nbuf, cb); } int main() { cin >> N; for (int i = 0; i < (N); i++) { CB[i] = CN[i] = 0; string S; cin >> S; for (char c : S) if (c == B ) CB[i]++; else if (c == N ) CN[i]++; } int lB, rB; lB = -1; rB = 500010; while (lB + 2 < rB) { int m1B = (lB + lB + rB) / 3, m2B = (lB + rB + rB) / 3; if (FF(m1B) < FF(m2B)) rB = m2B; else lB = m1B; } Bbuf = lB + 1; FF(Bbuf); printf( %d n , F(Nbuf, Bbuf)); for (int i = 0; i < (Bbuf); i++) printf( B ); for (int i = 0; i < (Nbuf); i++) printf( N ); printf( n ); return 0; }
#include <bits/stdc++.h> using namespace std; set<int> S; int n, a[500005], b[500005], q[500005], r; int main() { scanf( %d , &n); n *= 2; int tot = 0; for (int i = 1; i <= n; i++) { char c = getchar(); while (c != + && c != - ) c = getchar(); if (c == + ) q[++r] = ++tot, b[i] = 0; else { int x; scanf( %d , &x); b[i] = x; if (!r) return puts( NO ), 0; a[q[r]] = x; r--; } } if (r) return puts( NO ), 0; tot = 0; for (int i = 1; i <= n; i++) { if (!b[i]) { ++tot; S.insert(a[tot]); } else { int p = (*S.begin()); S.erase(S.begin()); if (p != b[i]) return puts( NO ), 0; } } puts( YES ); for (int i = 1; i <= n / 2; i++) printf( %d , a[i]); }
#include <bits/stdc++.h> using namespace std; int main() { int t; scanf( %d , &t); while (t--) { int n; scanf( %d , &n); int a[n], b[n]; for (int i = 0; i < n; i++) scanf( %d%d , &a[i], &b[i]); if (a[0] < b[0]) printf( NO n ); else { int f = 1; for (int i = 1; i < n; i++) { if (a[i] < a[i - 1] || b[i] < b[i - 1]) { f = 0; break; } if (a[i] - a[i - 1] < b[i] - b[i - 1]) { f = 0; break; } } if (f) printf( YES n ); else printf( NO n ); } } return 0; }
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const int inf = (int)1e9; struct vert { int to, z, num; }; struct ans { int x, y, z; }; const int N = (int)(1e5) + 5; int n, m; vector<vert> g[N]; vector<int> d(N, inf); vector<int> dp(N, inf); vector<int> pr(N, -1); vector<bool> road(N); void go() { queue<int> q; q.push(0); d[0] = 0; while (!q.empty()) { int v = q.front(); q.pop(); for (int i = 0; i < (int)g[v].size(); ++i) { int to = g[v][i].to; if (d[to] == inf) { d[to] = d[v] + 1; q.push(to); } } } } void getAns() { queue<int> q; q.push(n - 1); dp[n - 1] = 0; while (!q.empty()) { int v = q.front(); q.pop(); for (int i = 0; i < (int)g[v].size(); ++i) { int to = g[v][i].to; int z = 1 - g[v][i].z; if (d[to] + 1 == d[v]) { if (dp[to] > dp[v] + z) { dp[to] = dp[v] + z; pr[to] = v; } q.push(to); } } } } int main() { cin >> n >> m; vert p; int kol = 0; for (int i = 0; i < m; ++i) { int x, y, z; scanf( %d %d %d , &x, &y, &z); --x; --y; p.z = z; p.num = i; if (z == 0) { ++kol; } p.to = y; g[x].push_back(p); p.to = x; g[y].push_back(p); } go(); getAns(); vector<ans> out; ans a; int v = 0; for (int to = pr[0]; to != -1; to = pr[to]) { vert now; for (int k = 0; k < g[v].size(); ++k) { if (g[v][k].to == to) { now = g[v][k]; break; } } road[now.num] = true; if (now.z == 0) { a.x = v + 1; a.y = to + 1; a.z = 1; out.push_back(a); } v = to; } for (int i = 0; i < n; ++i) { for (int j = 0; j < (int)g[i].size(); ++j) { if (road[g[i][j].num]) { continue; } if (g[i][j].z == 1) { a.x = i + 1; a.y = g[i][j].to + 1; a.z = 0; road[g[i][j].num] = true; out.push_back(a); } } } cout << out.size() << n ; for (int i = 0; i < out.size(); ++i) { cout << out[i].x << << out[i].y << << out[i].z << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int mod = (int)1e9 + 7; struct matrix { int A[2][2] = {0}; matrix operator*(matrix B) { matrix C; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) for (int k = 0; k < 2; k++) C[i][j] = (C[i][j] + 1ll * A[i][k] * B[k][j]) % mod; return C; } matrix operator+(matrix B) { matrix C; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) C[i][j] = (A[i][j] + B[i][j]) % mod; return C; } matrix pow(int n) { matrix B, C = *this; for (int i = 0; i < 2; i++) B[i][i] = 1; while (n) { if (n & 1) B = B * C; C = C * C; n >>= 1; } return B; } int *operator[](int i) { return A[i]; } }; matrix fib; struct segment_tree { struct node { node *node_l, *node_r; int l, r; matrix sum, mul; } * root; node *build(vector<int> &a, int l, int r) { node *res = new node; res->l = l; res->r = r; res->mul[0][0] = res->mul[1][1] = 1; if (l == r) { res->sum = fib.pow(a[l]); res->node_l = res->node_r = nullptr; } else { int m = (l + r) >> 1; res->node_l = build(a, l, m); res->node_r = build(a, m + 1, r); res->sum = res->node_l->sum + res->node_r->sum; } return res; } void build(vector<int> &a) { root = build(a, 0, a.size() - 1); } void push(node *root) { if (root->mul[1][0] + root->mul[0][0] + root->mul[0][1] + root->mul[1][1] == 2) return; if (root->node_l) { root->node_l->mul = root->node_l->mul * root->mul; root->node_l->sum = root->node_l->sum * root->mul; } if (root->node_r) { root->node_r->mul = root->node_r->mul * root->mul; root->node_r->sum = root->node_r->sum * root->mul; } root->mul[0][0] = root->mul[1][1] = 1; root->mul[1][0] = root->mul[0][1] = 0; } void update(node *root, int l, int r) { if (root->l > r || root->r < l) return; if (root->l >= l && root->r <= r) { root->sum = root->sum * fib; root->mul = root->mul * fib; } else { push(root); update(root->node_l, l, r); update(root->node_r, l, r); root->sum = root->node_l->sum + root->node_r->sum; } } void update(int l, int r) { update(root, l, r); } matrix query(node *root, int l, int r) { push(root); if (root->l > r || root->r < l) return matrix(); if (root->l >= l && root->r <= r) return root->sum; return query(root->node_l, l, r) + query(root->node_r, l, r); } matrix query(int l, int r) { return query(root, l, r); } }; int main() { fib[0][1] = fib[1][0] = fib[0][0] = 1; int n, m; scanf( %d %d , &n, &m); vector<int> v(n); for (int &p : v) scanf( %d , &p); segment_tree sg; sg.build(v); for (int i = 0; i < m; i++) { int t, l, r; scanf( %d %d %d , &t, &l, &r); l--, r--; if (t == 1) { int x; scanf( %d , &x); matrix tmp = fib; fib = fib.pow(x); sg.update(l, r); fib = tmp; } else { printf( %d n , sg.query(l, r)[0][1]); } } }
#include <bits/stdc++.h> using namespace std; int a[110], b[110]; int main() { int n; cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; sort(a + 1, a + 1 + n); int aa = a[n]; cin >> n; for (int i = 1; i <= n; ++i) cin >> b[i]; sort(b + 1, b + 1 + n); int bb = b[n]; cout << aa << << bb << endl; }
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3f; int v[1010]; bool compare(pair<int, int> a, pair<int, int> b) { if (a.second != b.second) return a.second > b.second; if (v[a.first] != v[b.first]) return v[a.first] < v[b.first]; return a.first < b.first; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) cin >> v[i]; vector<pair<int, int> > ret; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (v[j] < v[i]) ret.push_back({i, j}); } } sort(ret.begin(), ret.end(), compare); cout << ret.size() << n ; for (auto x : ret) { cout << x.first + 1 << << x.second + 1 << n ; } exit(0); }
#include <bits/stdc++.h> using namespace std; void fast() { ios_base::sync_with_stdio(0); cin.tie(NULL), cout.tie(NULL); } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int lcm(int a, int b) { return a * (b / gcd(a, b)); } char maze[1001][1001]; int main() { fast(); int num, one = 0, two = 0, there = 0, six = 0, fou = 0; cin >> num; ; for (int i = 0; i < num; i++) { int x; cin >> x; one += bool(x == 1); two += bool(x == 2); there += bool(x == 3); fou += bool(x == 4); six += bool(x == 6); } if ((fou + six) != num / 3 || (one) != num / 3 || (two + there) != num / 3) { cout << -1 << endl; return 0; } vector<string> vs; for (int i = 0; i < num / 3; i++) { string s = ; s += 1 ; s += ; if (there == six && six && there) { s += 3 , s += , s += 6 ; six--, there--; } else if (two) { s += 2 ; s += ; if (fou) s += 4 , fou--; else if (six) s += 6 , six--; else { cout << -1 << endl; return 0; } two--; } else { cout << -1 << endl; return 0; } vs.push_back(s); } for (int i = 0; i < vs.size(); i++) { cout << vs[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); const int maxN = 2 * 1e5; int answers[maxN]; int n, m, x, neg = 0, pos = 0; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> x; if (x == -1) neg++; else pos++; } int li, ri, range; for (int i = 0; i < m; i++) { cin >> li >> ri; range = ri - li + 1; if (range % 2 != 0) answers[i] = 0; else if (pos >= range / 2 && neg >= range / 2) { answers[i] = 1; } else { answers[i] = 0; } } for (int i = 0; i < m; i++) { cout << answers[i] << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 * 4; inline int get() { int x; cin >> x; return x; } inline long long getlong() { long long x; cin >> x; return x; } inline string getString() { string s; cin >> s; return s; } template <typename T> inline vector<T> getvector(int len) { vector<T> a(len); for (auto &it : a) cin >> it; return a; } inline vector<int> getvector(int len) { vector<int> a(len); for (auto &it : a) cin >> it; return a; } inline vector<pair<int, int>> getvector_andPairCnt(int len) { vector<pair<int, int>> a(len); int i = 1; for (auto &it : a) { it.second = i; i++; cin >> it.first; } return a; } double power_log(double power, double val) { return log(val) / log(power); } template <typename T> T sqr(T x) { return x * x; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto it : v) os << it << ; os << n ; return os; } template <typename T, typename H> inline vector<pair<T, H>> getvector_andBiPair(int len) { vector<pair<T, H>> a(len); for (auto &it : a) { cin >> it.first >> it.second; } return a; } template <typename T> inline set<T> vector_to_set(const vector<T> &vec) { set<T> s; for (auto &it : vec) s.insert(it); return s; } int main() { auto n = getlong(), k = getlong(); int t = get(); if (t <= k) { cout << t; } else { if (t >= n) { cout << (n + k) - t; } else { cout << k; } } }
#include <bits/stdc++.h> using namespace std; void split(string &s, vector<string> &v, string &sep) { int current, previous = 0; current = s.find_first_of(sep); while (current != string::npos) { v.push_back(s.substr(previous, current - previous)); previous = current + 1; current = s.find_first_of(sep, previous); } v.push_back(s.substr(previous, current - previous)); } int main() { int n, flag = 0; cin >> n; for (int i = 0; i < n; i++) { int temp; cin >> temp; if (temp) { flag = 1; } } if (flag) { cout << HARD ; cout << endl; } else { cout << EASY ; cout << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int has[205][205]; int vis[205][205]; vector<pair<int, int> > q; int gcd(int x, int y) { if (x < y) swap(x, y); while (y) { int t = x % y; x = y; y = t; } return x; } int main() { int n; scanf( %d , &n); for (int i = 0; i < n; i++) { int x, y; scanf( %d%d , &x, &y); q.push_back(make_pair(y + 100, x + 100)); has[x + 100][y + 100] = 1; } sort(q.begin(), q.end()); if (n < 3) { printf( 0 n ); return 0; } long long ans1 = 0; int hehe = 0; int L = q.size(); for (int i = 1; i <= 200; i++) { for (int j = 1; j <= 200; j++) { if (gcd(i, j) != 1) continue; memset(vis, 0, sizeof(vis)); for (int k = 0; k < L; k++) { int yy = q[k].first; int xx = q[k].second; if (vis[xx][yy]) continue; int res = 0; while (xx <= 200 && yy <= 200) { vis[xx][yy] = 1; res += has[xx][yy]; xx += i; yy += j; } if (res > 2) ans1 += (long long)res * (res - 1) * (res - 2) / 6; } memset(vis, 0, sizeof(vis)); for (int k = 0; k < L; k++) { int yy = q[k].first; int xx = q[k].second; if (vis[xx][yy]) continue; int res = 0; while (xx >= 0 && yy <= 200) { vis[xx][yy] = 1; res += has[xx][yy]; xx -= i; yy += j; } if (res > 2) ans1 += (long long)res * (res - 1) * (res - 2) / 6; } } } for (int i = 0; i <= 200; i++) { int xx = i; int yy = 0; int res = 0; while (xx <= 200 && yy <= 200) { res += has[xx][yy]; yy += 1; } if (res >= 3) { ans1 += (long long)res * (res - 1) * (res - 2) / 6; } } for (int i = 0; i <= 200; i++) { int xx = 0; int yy = i; int res = 0; while (xx <= 200 && yy <= 200) { res += has[xx][yy]; xx += 1; } if (res >= 3) { ans1 += (long long)res * (res - 1) * (res - 2) / 6; } } long long ans = (long long)n * (n - 1) * (n - 2) / 6 - ans1; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long N; scanf( %I64d , &N); int l = 1, r = 20000000, ans = 0; while (l <= r) { int mid = (l + r) >> 1; if ((long long)mid * (mid + 1) < N * 2) ans = mid, l = mid + 1; else r = mid - 1; } printf( %I64d , N - (long long)ans * (ans + 1) / 2); return 0; }
#include <bits/stdc++.h> #pragma comment(linker, /STACK:96777216 ) using namespace std; const double NOT_FOUND = -1000000000; const double EPS = 0.00000001; const double PI = acos(-1.0); struct point { double x, y; }; point make_point(double a, double b) { point c; c.x = a; c.y = b; return c; } struct line { double A, B, C; }; line make_line(double a, double b, double c) { line p; p.A = a; p.B = b; p.C = c; return p; } struct circle { double R; point center; vector<double> intersects; }; circle make_circle(point c, double r) { circle C; C.R = r; C.center = c; return C; } int main() { int N, K; cin >> N >> K; if (K > N) { cout << -1; return 0; } if (K == 1 && N > 1) { cout << -1; return 0; } if (K == N) { char p = a ; for (int i = 0; i < N; i++) { cout << p; p++; } return 0; } string S; for (char c = a + K - 1; c > b ; c--) { S += c; N--; } if (N % 2) { S += a ; N--; } for (int i = 0; i < N; i += 2) S += ba ; for (int i = (int)S.length() - 1; i >= 0; i--) cout << S[i]; return 0; }
#include <bits/stdc++.h> using namespace std; int prim[10] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; int n; long long ans; void DFS(int a, int b, long long temp) { if (temp < ans && a == n) ans = temp; if (a >= n) return; for (int i = 1; i <= 64; i++) { if (ans / prim[b] < temp) break; temp *= prim[b]; DFS(a * (i + 1), (b + 1), temp); } } int main() { while (scanf( %d , &n) != EOF) { ans = LONG_LONG_MAX; DFS(1, 0, 1); printf( %lld n , ans); } return 0; }
#include <bits/stdc++.h> using namespace std; int ABS(int a) { if (a < 0) return (-a); return a; } void input() {} int a[100005]; int seg[100005]; int nxt[100005]; int hop[100005]; void readInt(int &n) { n = 0; char ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) { n = n * 10 + ch - 48; ch = getchar(); } } int main() { input(); int n, q; readInt(n); readInt(q); int sq = sqrt(n); int c = 0, cnt = 0; for (int i = (0); i < (n); ++i) { readInt(a[i]); seg[i] = cnt; c++; if (c == sq) c = 0, cnt++; } for (int i = (n - 1); i >= (0); --i) { int to = i + a[i]; hop[i] = 1; if (to >= n) nxt[i] = -1; else if (seg[to] != seg[i]) nxt[i] = to; else { nxt[i] = nxt[to]; hop[i] += hop[to]; } } while (q--) { int type; readInt(type); if (type == 0) { int x, y; readInt(x); readInt(y); x--; int i = x; a[x] = y; while (i >= 0) { if (seg[i] != seg[x]) break; int to = i + a[i]; hop[i] = 1; if (to >= n) nxt[i] = -1; else if (seg[to] != seg[i]) nxt[i] = to; else { nxt[i] = nxt[to]; hop[i] += hop[to]; } i--; } } else { int x; readInt(x); x--; int ans = 0; int last = x; while (x != -1) { ans += hop[x]; last = x; x = nxt[x]; } while (last + a[last] < n) last = last + a[last]; printf( %d %d n , last + 1, ans); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int mx = 200100; struct node { int x, y, z; } a[mx]; struct nod { int id, x; } b[mx]; int fa[mx]; long long num[mx]; long long ans[mx]; int n, m; bool cmp(node x, node y) { return x.z < y.z; } bool cmp1(nod x, nod y) { return x.x < y.x; } int find(int x) { if (x != fa[x]) { return fa[x] = find(fa[x]); } return fa[x]; } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) fa[i] = i, num[i] = 1; for (int i = 1; i < n; i++) { cin >> a[i].x >> a[i].y >> a[i].z; } for (int i = 1; i <= m; i++) { cin >> b[i].x; b[i].id = i; } sort(a + 1, a + n, cmp); sort(b + 1, b + m + 1, cmp1); int l = 1; long long sum = 0; for (int i = 1; i < n; i++) { while (l <= m && a[i].z > b[l].x) { ans[b[l].id] = sum; l++; } int x = find(a[i].x); int y = find(a[i].y); sum += num[y] * num[x]; fa[x] = y; num[y] += num[x]; } while (l <= m) { if (a[n - 1].z <= b[l].x) { ans[b[l].id] = sum; } l++; } for (int i = 1; i <= m; i++) cout << ans[i] << n [i == m]; return 0; }
#include <bits/stdc++.h> using namespace std; long long int a[100005], ans[100005]; int main() { long long int n, i, j; cin >> n; for (i = 0; i < n; i++) { cin >> j; a[j] += 1 * j; } ans[1] = a[1]; ans[2] = a[2]; ans[3] = a[1] + a[3]; for (i = 4; i < 100005; i++) { ans[i] = max(ans[i - 2] + a[i], ans[i - 3] + a[i]); } cout << ans[100004]; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int T; cin >> T; while (T--) { int n; cin >> n; long double theta = 3.14159265358979323846 / (2 * n); long double answer = 1 / tan(theta); cout << setprecision(15) << answer << n ; } return 0; }
#include <bits/stdc++.h> const int M = 1 << 20, L = 100020; char s[L]; int n, m, g[M], f[M], i, j, k, x, y, D; void init() { scanf( %d%d%s , &n, &m, s + 1); D = (1 << m) - 1; for (i = 1; i < n; i++) { x = s[i] - 97; y = s[i + 1] - 97; if (x != y) g[1 << x | 1 << y]++; } for (j = 0; j < m; j++) for (i = 0; i <= D; i++) if (i >> j & 1) g[i] += g[i ^ 1 << j]; } int min(int x, int y) { return x < y ? x : y; } void work() { memset(f, 0x3f, sizeof(f)); f[0] = 0; for (i = 0; i <= D; i++) for (j = 0; j < m; j++) if ((i >> j & 1) == 0) f[i | 1 << j] = min(f[i | 1 << j], f[i] + g[D] - g[i] - g[D ^ i]); printf( %d , f[D]); } int main() { init(); work(); return 0; }
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(); cout.tie(); long long t; cin >> t; while (t--) { long long n; cin >> n; string a; cin >> a; string last = a; for (long long i = 0; i <= n; ++i) { string s; s = a; for (long long k = 1; k < n; ++k) { if (a[k - 1] == A ) { s[k] = A ; } } a = s; if (a == last) { cout << i << n ; break; } last = a; } } }
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n = s.length(); vector<int> P(n, 0); int C = 0; int R = 0; for (int i = 1; i < n - 1; i++) { int mirror = 2 * C - i; if (R > i) { P[i] = min(R - i, P[mirror]); } else { P[i] = 0; } while ((i + 1 + P[i] < n) && (i - 1 - P[i] >= 0) && (s[i + 1 + P[i]] == s[i - 1 - P[i]])) { P[i]++; } if (i + P[i] > R) { C = i; R = i + P[i]; } } string rev(s); reverse(rev.begin(), rev.end()); vector<int> T(n, 0); T[0] = -1; int pos = 2; int cnd = 0; while (pos < n) { if (rev[pos - 1] == rev[cnd]) { cnd++; T[pos] = cnd; pos++; } else if (cnd > 0) { cnd = T[cnd]; } else { T[pos] = 0; pos++; } } int m = 0; int revI = 0; vector<int> found(n + 1, -1); found[0] = 0; while (revI < n && m + revI < n) { if (rev[revI] == s[m + revI]) { if (found[revI + 1] == -1) { found[revI + 1] = m; } revI++; } else { m = m + revI - T[revI]; if (T[revI] > -1) { revI = T[revI]; } else { revI = 0; } } } vector<int> fend(n + 1, -1); int fendMax = 0; for (int i = 0; i <= n; i++) { if (found[i] != -1) { fend[i] = found[i] + i; fendMax = i; } } int best = 0; int bestMidSt = 0; int bestMidL = 0; int bestLeftSt = 0; int bestSideL = 0; for (int i = 0; i < n; i++) { int midStart = i - P[i]; int midEnd = i + P[i]; int lLow = 0; int lHigh = min(n - midEnd, fendMax + 1); int lCur = (lLow + lHigh) / 2; while (lHigh - lLow > 1) { if (fend[lCur] <= midStart) { lLow = lCur; } else { lHigh = lCur; } lCur = (lLow + lHigh) / 2; } int curBest = 2 * P[i] + 1 + 2 * lCur; if (curBest > best) { best = curBest; bestMidSt = midStart; bestMidL = 2 * P[i] + 1; bestLeftSt = found[lCur]; bestSideL = lCur; } } if (bestSideL == 0) { cout << 1 << endl; cout << (bestMidSt + 1) << << bestMidL << endl; } else { cout << 3 << endl; cout << (bestLeftSt + 1) << << bestSideL << endl; cout << (bestMidSt + 1) << << bestMidL << endl; cout << (n - bestSideL + 1) << << bestSideL << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; bool ck(int x) { int t[50], l = 0; while (x) t[l++] = x & 1, x >>= 1; if (l & 1) { int lim = l - 1 >> 1; for (int i = 0; i < lim; i++) if (t[i]) return 0; for (int i = lim; i < l; i++) if (!t[i]) return 0; } else return 0; return 1; } int main() { int n; cin >> n; for (int i = n; i >= 1; i--) { if (n % i == 0 && ck(i)) { printf( %d n , i); return 0; } } }
#include <bits/stdc++.h> const int N = 2010; std::set<std::pair<long long, int>> set; long long a[N][N], dis[N]; bool vis[N]; int n; int main() { scanf( %d , &n); memset(dis, 0x7f, sizeof(dis)); long long mine = LLONG_MAX; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { scanf( %I64d , &a[i][j]); mine = std::min(mine, a[j][i] = a[i][j]); } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i == j) { continue; } a[i][j] -= mine; dis[i] = std::min(dis[i], 2 * a[i][j]); } } for (int i = 0; i < n; ++i) { set.insert({dis[i], i}); } for (int i = 0; i < n; ++i) { std::pair<long long, int> p = *(set.begin()); set.erase(set.begin()); vis[p.second] = true; for (int j = 0; j < n; ++j) { if (!vis[j] && p.second != j) { set.erase({dis[j], j}); dis[j] = std::min(dis[p.second] + a[p.second][j], dis[j]); set.insert({dis[j], j}); } } } for (int i = 0; i < n; ++i) { printf( %I64d n , dis[i] + (n - 1) * mine); } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) #pragma GCC target( avx2 ) using namespace std; void solve() { int q; cin >> q; unordered_map<string, string> map; while (q--) { string a, b; cin >> a >> b; if (map.find(a) != map.end()) { string s = map[a]; map.erase(a); map[b] = s; continue; } map[b] = a; } cout << map.size() << n ; for (auto i : map) { cout << i.second << << i.first << n ; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; template <class Key, class Value> struct Heap { Value p[100100]; int pos[100100]; vector<Key> hp; void init() { hp.clear(); } void swapPos(int i, int a) { int temp; temp = pos[hp[a]]; pos[hp[a]] = pos[hp[i]]; pos[hp[i]] = temp; swap(hp[i], hp[a]); } void up(int a) { int i; while (1) { i = a; if (a + a + 1 < hp.size() && p[hp[i]] > p[hp[a + a + 1]]) { i = a + a + 1; } if (a + a + 2 < hp.size() && p[hp[i]] > p[hp[a + a + 2]]) { i = a + a + 2; } if (i == a) break; swapPos(i, a); a = i; } } void down(int a) { int i, temp; while (1) { i = a; if (a && p[hp[i]] < p[hp[(a - 1) / 2]]) { i = (a - 1) / 2; } if (i == a) break; swapPos(i, a); a = i; } } void update(Key k, Value v) { p[k] = v; up(pos[k]); down(pos[k]); } void push_heap(Key k, Value v) { hp.push_back(k); p[k] = v; down(pos[k] = hp.size() - 1); } Value top() { return p[hp[0]]; } void pop(Key& k, Value& v) { k = hp[0]; v = p[hp[0]]; swapPos(0, hp.size() - 1); hp.pop_back(); up(0); } int size() { return hp.size(); } }; Heap<int, long long int> hp; template <class T> struct Graph { static const int gmaxn = 101000; static const int gmaxm = 101000; int ind[gmaxn]; int last[gmaxm * 2], e[gmaxm * 2], gt; T w[gmaxm * 2]; bool bi; int n, m; void init() { gt = 0; memset(ind, 0, sizeof(ind)); } void addE1(int a, int b, T& c) { gt++; last[gt] = ind[a]; ind[a] = gt; e[gt] = b; w[gt] = c; } void addE2(int a, int b, T& c) { addE1(a, b, c); addE1(b, a, c); } void addE(int a, int b, T& c) { if (bi) addE2(a, b, c); else addE1(a, b, c); } Graph(bool bidirectional = true) { bi = bidirectional; } void next(int& a) { a = last[a]; } void dijkstra(vector<int>& S); }; int from[100100]; long long int dis[100100]; template <class T> void Graph<T>::dijkstra(vector<int>& S) { memset(dis, -1, sizeof(dis)); int i, now, j; long long int nowdis; for (i = 0; i < S.size(); i++) { dis[from[S[i]] = S[i]] = 0; hp.push_heap(S[i], 0); } while (hp.size()) { hp.pop(now, nowdis); for (i = ind[now]; i; next(i)) { j = e[i]; if (dis[j] < 0) { dis[j] = nowdis + w[i]; from[j] = from[now]; hp.push_heap(j, dis[j]); } else if (dis[j] > nowdis + w[i]) { dis[j] = nowdis + w[i]; from[j] = from[now]; hp.update(j, dis[j]); } } } } Graph<long long int> g; bool k[100100]; vector<int> s; int fa[100100]; int findfa(int a) { if (a == fa[a]) return a; return fa[a] = findfa(fa[a]); } bool merge(int a, int b) { a = findfa(a); b = findfa(b); if (a == b) return 0; if (rand() & 1) swap(a, b); fa[a] = b; return 1; } vector<pair<long long int, pair<int, int> > > eg; int main() { scanf( %d %d , &g.n, &g.m); int i, j, a, b, cc; long long int c; for (i = 0; i < g.m; i++) { scanf( %d %d %d , &a, &b, &cc); c = cc; g.addE(a, b, c); } scanf( %d , &i); while (i--) { scanf( %d , &j); k[j] = 1; s.push_back(j); } g.dijkstra(s); for (i = 1; i <= g.n; i++) { fa[i] = i; for (j = g.ind[i]; j; g.next(j)) { if (from[i] != from[g.e[j]]) { eg.push_back(make_pair(dis[i] + dis[g.e[j]] + g.w[j], make_pair(from[i], from[g.e[j]]))); } } } sort(eg.begin(), eg.end()); long long int ans = 0; if (!k[1]) ans += dis[1]; for (i = 0; i < eg.size(); i++) { if (merge(eg[i].second.first, eg[i].second.second)) { ans += eg[i].first; } } printf( %I64d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = INT_MAX; const int mod = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; int arr[2][n]; int sum[2][n]; for (int i = 0; i < 2; i++) { for (int j = 0; j < n; j++) { cin >> arr[i][j]; if (j == 0) sum[i][j] = arr[i][j]; else sum[i][j] = sum[i][j - 1] + arr[i][j]; } } int move1 = sum[0][n - 1]; int move2 = sum[1][n - 1]; int ans = min(move1 - arr[0][0], move2 - arr[1][n - 1]); for (int i = 1; i < n; i++) { int temp1 = move1 - sum[0][i]; int temp2 = sum[1][i - 1]; ans = min(ans, max(temp1, temp2)); } cout << ans << endl; } }
#include <bits/stdc++.h> #pragma comment(linker, /STACK:167772160 ) using namespace std; int main() { int n; cin >> n; pair<int, int> x[1111]; for (int i = 0; i < n; ++i) cin >> x[i].first >> x[i].second; long long p = 0, maxx = -1; int j = 0; for (int i = 0; i < x[n - 1].first; i++) { if (p != 0) --p; if (i == x[j].first - 1) { p += x[j].second; ++j; } if (p > maxx) maxx = p; } cout << x[n - 1].first + p << << maxx << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long int n, k, a, b; vector<long long int> arr; long long int rec(long long int l, long long int r) { long long int i = lower_bound(arr.begin(), arr.end(), l) - arr.begin(); long long int j = upper_bound(arr.begin(), arr.end(), r) - arr.begin(); long long int x = j - i; long long int val; if (x == 0) { val = a; } else { val = b * x * (r - l + 1); } if (l == r || x == 0) { return val; } long long int mid = (l + r) / 2; return min(val, rec(l, mid) + rec(mid + 1, r)); } int main() { cin >> n >> k >> a >> b; for (int i = 0; i < k; i++) { int x; cin >> x; arr.push_back(x); } sort(arr.begin(), arr.end()); long long int start = 1; long long int end = pow(2, n); cout << rec(start, end) << endl; }
#include <bits/stdc++.h> using namespace std; char s[10]; int main() { scanf( %s , s); int res = 1; for (int i = 1; i < 7; i++) { if (s[i] == 1 ) res += 10, i++; else res += s[i] - 0 ; } printf( %d n , res); }
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; char s[N]; int main() { vector<int> v1, v2; scanf( %s , s); int len = strlen(s); for (int i = 0; i < len; i++) { if (s[i] == l ) { v1.push_back(i + 1); } else v2.push_back(i + 1); } for (int x : v2) printf( %d n , x); for (int i = (int)v1.size() - 1; i >= 0; i--) printf( %d n , v1[i]); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 100; int n, p[4], bx[N], wx, by[N], wy, sum[N][4]; vector<int> wh[N]; struct node { int x, y, c; } sh[N]; bool cmp(node a, node b) { return a.x < b.x; } inline int read() { int f = 1, x = 0; char s = getchar(); while (s < 0 || s > 9 ) { if (s == - ) f = -1; s = getchar(); } while (s >= 0 && s <= 9 ) { x = x * 10 + s - 0 ; s = getchar(); } return x * f; } struct bit { int sh[N]; void init() { memset(sh, 0, sizeof(sh)); } int lowbit(int x) { return x & (-x); } void change(int x, int v) { while (x <= n) sh[x] += v, x += lowbit(x); } int query(int x) { int ans = 0; while (x) ans += sh[x], x -= lowbit(x); return ans; } int ask(int l, int r) { return query(r) - query(l - 1); } } T[4]; int g(int l, int r, int c) { return sum[r][c] - sum[l - 1][c]; } void init() { memset(sum, 0, sizeof(sum)); wx = wy = 0; for (int i = 1; i <= n; i++) bx[++wx] = sh[i].x; sort(bx + 1, bx + 1 + wx); wx = unique(bx + 1, bx + 1 + wx) - bx - 1; for (int i = 1; i <= n; i++) sh[i].x = lower_bound(bx + 1, bx + 1 + wx, sh[i].x) - bx; for (int i = 1; i <= n; i++) by[++wy] = sh[i].y; sort(by + 1, by + 1 + wy); wy = unique(by + 1, by + 1 + wy) - by - 1; for (int i = 1; i <= n; i++) sh[i].y = lower_bound(by + 1, by + 1 + wy, sh[i].y) - by; sort(sh + 1, sh + 1 + n, cmp); for (int i = 1; i <= n; i++) sum[sh[i].x][sh[i].c]++; for (int i = 1; i <= wx; i++) for (int j = 1; j <= 3; j++) sum[i][j] += sum[i - 1][j]; } int solve() { for (int i = 1; i <= wx; i++) wh[i].clear(); for (int i = 1; i <= n; i++) wh[sh[i].x].push_back(i); for (int i = 1; i <= 3; i++) p[i] = i; int ans = 1; do { for (int i = 2; i <= wx; i++) { int A = sum[i - 1][p[1]], l = i, r = wx - 1; while (l < r) { int mid = l + ((r - l + 1) >> 1); if (g(i, mid, p[2]) <= g(mid + 1, wx, p[3])) l = mid; else r = mid - 1; } ans = max(ans, min({A, g(i, l, p[2]), g(l + 1, wx, p[3])})); l++; ans = max(ans, min({A, g(i, l, p[2]), g(l + 1, wx, p[3])})); } } while (next_permutation(p + 1, p + 4)); for (int i = 1; i <= 3; i++) p[i] = i; auto find = [](int A, int &ans) { int l = 1, r = wy - 1; while (l < r) { int mid = l + ((r - l + 1) >> 1); if (T[p[2]].ask(1, mid) <= T[p[3]].ask(mid + 1, wy)) l = mid; else r = mid - 1; } ans = max(ans, min({A, T[p[2]].ask(1, l), T[p[3]].ask(l + 1, wy)})); l++; ans = max(ans, min({A, T[p[2]].ask(1, l), T[p[3]].ask(l + 1, wy)})); }; do { T[p[2]].init(); T[p[3]].init(); for (int i = wx; i >= 1; i--) { int A = g(1, i, p[1]); find(A, ans); for (int j : wh[i]) if (sh[j].c != p[1]) T[sh[j].c].change(sh[j].y, 1); } } while (next_permutation(p + 1, p + 4)); for (int i = 1; i <= 3; i++) p[i] = i; do { T[p[2]].init(); T[p[3]].init(); for (int i = 1; i <= wx; i++) { int A = g(i, wx, p[1]); find(A, ans); for (int j : wh[i]) if (sh[j].c != p[1]) T[sh[j].c].change(sh[j].y, 1); } } while (next_permutation(p + 1, p + 4)); return ans; } signed main() { n = read(); for (int i = 1; i <= n; i++) sh[i] = (node){read(), read(), read()}; init(); int ans = solve(); for (int i = 1; i <= n; i++) swap(sh[i].x, sh[i].y); init(); ans = max(ans, solve()); printf( %d n , ans * 3); }
#include <bits/stdc++.h> static const int MAXN = 50004; static const int MAXE = 100004 * 2; static const int MAXL = 20; static const int MAXT = 1000005; int n, e; int head[MAXN], dest[MAXE], next[MAXE]; int l[MAXN], t[MAXN][MAXL]; int T = 0; int p[MAXN]; int conf[MAXN], team[MAXN]; int team_conf[MAXT]; inline void add_edge(int u, int v) { static int w = 0; dest[w] = v; next[w] = head[u]; head[u] = w++; } int main() { scanf( %d%d , &n, &e); for (int i = 0; i < n; ++i) head[i] = -1; int u, v; for (int i = 0; i < e; ++i) { scanf( %d%d , &u, &v); --u, --v; add_edge(u, v); add_edge(v, u); } for (int i = 0; i < n; ++i) { scanf( %d , &l[i]); for (int j = 0; j < l[i]; ++j) { scanf( %d , &t[i][j]); --t[i][j]; T = std::max(T, t[i][j]); } } ++T; srand(87665544); for (int i = 0; i < n; ++i) p[i] = i; for (int attempts = 0;; ++attempts) { if (attempts % 16 == 0) { std::random_shuffle(p, p + n); std::fill(conf, conf + n, -1); int ct[2]; for (int i = 0; i < n; ++i) { ct[0] = ct[1] = 0; for (int w = head[p[i]]; w != -1; w = next[w]) { if (conf[dest[w]] != -1) ++ct[conf[dest[w]]]; } conf[p[i]] = (ct[0] > ct[1] ? 1 : 0); } } for (int i = 0; i < T; ++i) team_conf[i] = rand() % 2; bool valid = true; for (int i = 0; i < n; ++i) { team[i] = -1; for (int j = 0; j < l[i]; ++j) if (conf[i] == team_conf[t[i][j]]) { team[i] = t[i][j]; break; } if (team[i] == -1) { valid = false; break; } } if (!valid) continue; for (int i = 0; i < n; ++i) printf( %d%c , team[i] + 1, i == n - 1 ? n : ); for (int i = 0; i < T; ++i) printf( %d%c , team_conf[i] + 1, i == T - 1 ? n : ); break; } return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; int ans = 654; vector<int> a, b, us(10); int main() { cin >> n >> m; a.resize(n); b.resize(m); for (int i = 0; i < n; ++i) { cin >> a[i]; us[a[i]] = 1; } for (int j = 0; j < m; ++j) { cin >> b[j]; if (us[b[j]] == 1) { ans = min(ans, b[j]); } } sort(a.begin(), a.end()); sort(b.begin(), b.end()); cout << min({(a[0] * 10) + b[0], (b[0] * 10) + a[0], ans}); }
#include <bits/stdc++.h> using namespace std; inline void read(int &x) { x = 0; char c = getchar(); int f = 1; while (!isdigit(c)) { if (c == - ) f = -1; c = getchar(); } while (isdigit(c)) { x = x * 10 + c - 0 ; c = getchar(); } x *= f; } const int N = 505000; int n, a[N], ans[N]; int main() { read(n); for (int i = 1; i <= n; i++) read(a[i]); for (int i = 0; i <= n - 2; i++) { if ((n - i) & 1) ans[i] = max(min(a[(1 + n - i) / 2], max(a[(1 + n - i) / 2 + 1], a[(1 + n - i) / 2 - 1])), min(a[(1 + n + i) / 2], max(a[(1 + n + i) / 2 + 1], a[(1 + n + i) / 2 - 1]))); else ans[i] = max(a[(n - i + 1) / 2], a[(n + i + 2) / 2]); if ((n & 1) && i == 1) ans[i] = max(ans[i], a[n / 2 + 1]); if (i - 2 >= 0) ans[i] = max(ans[i], ans[i - 2]); } for (int i = 1; i <= n; i++) ans[n - 1] = max(ans[n - 1], a[i]); for (int i = 0; i <= n - 1; i++) printf( %d , ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; int dp[1001][2010]; int digit[1002], k; void init() { memset(dp, -1, sizeof dp); } int dfs(int pos, int dist, bool doing) { if (pos == -1) return dist == 0; if (!doing && dp[pos][dist] != -1) return dp[pos][dist]; int ans = 0; int end = doing ? digit[pos] : 9; for (int i = 0; i <= end; i++) { int nextdist = dist == 0 ? 0 : dist + 1; if (i == 4 || i == 7) { if (dist <= k) nextdist = 0; else nextdist = 1; } ans = (ans + dfs(pos - 1, nextdist, doing && i == end)) % MOD; } if (!doing) dp[pos][dist] = ans; return ans; } int cal(char x[]) { int pos = 0, l = strlen(x); while (pos < l) { digit[pos] = x[l - pos - 1] - 0 ; pos++; } return dfs(pos - 1, k + 2, 1); } bool check(char x[]) { int dist = k + 1; for (int i = 0; i < strlen(x); i++) { if (!dist) break; if (x[i] == 4 || x[i] == 7 ) { if (dist <= k) dist = 0; else dist = 1; } else dist++; } return dist == 0; } int main() { int T; init(); char a[1002], b[1002]; scanf( %d %d , &T, &k); while (T--) { scanf( %s %s , a, b); int ansa = cal(a); int ansb = cal(b); if (check(a)) ansa--; ansb = ((ansb - ansa) % MOD + MOD) % MOD; printf( %d n , ansb); } return 0; }
#include <bits/stdc++.h> using namespace std; vector<string> vec_splitter(string s) { s += , ; vector<string> res; while (!s.empty()) { res.push_back(s.substr(0, s.find( , ))); s = s.substr(s.find( , ) + 1); } return res; } void debug_out(vector<string> __attribute__((unused)) args, __attribute__((unused)) int idx, __attribute__((unused)) int LINE_NUM) { cerr << n ; } template <typename Head, typename... Tail> void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) { if (idx > 0) cerr << , ; else cerr << Line( << LINE_NUM << ) ; stringstream ss; ss << H; cerr << args[idx] << = << ss.str(); debug_out(args, idx + 1, LINE_NUM, T...); } void run() { ios_base::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); } const int N = 5e4 + 4, MOD = 1e9 + 7, mod2 = 998244353; int dp[N][503]; int n, k; vector<int> adj[N]; long long ans; void dfs(int u, int p) { dp[u][0] = 1; for (auto &v : adj[u]) { if (v == p) continue; dfs(v, u); for (int i = 0; i < k; ++i) { dp[u][i + 1] += dp[v][i]; } } long long sum = 0; for (auto &v : adj[u]) { if (v == p) continue; for (int i = 1; i < k; ++i) { sum += dp[v][i - 1] * (dp[u][k - i] - dp[v][k - i - 1]); } } ans += dp[u][k] + sum / 2; } int main() { run(); cin >> n >> k; for (int i = 0; i < (int)(n - 1); ++i) { int u, v; cin >> u >> v; adj[v].push_back(u); adj[u].push_back(v); } dfs(1, -1); cout << ans; }
#include <bits/stdc++.h> using namespace std; void Genawy() { std::ios_base::sync_with_stdio(0); cin.tie(NULL); } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int n, cost[305]; int length[305]; map<pair<int, int>, int> dp; int solve(int ind, int gc) { if (ind == n) return gc == 1 ? 0 : 1e9; if (dp.find({ind, gc}) != dp.end()) return dp[{ind, gc}]; dp[{ind, gc}] = solve(ind + 1, gc); dp[{ind, gc}] = min(dp[{ind, gc}], solve(ind + 1, gcd(gc, length[ind])) + cost[ind]); return dp[{ind, gc}]; } int main() { Genawy(); cin >> n; for (int i = 0; i < n; i++) cin >> length[i]; for (int i = 0; i < n; i++) cin >> cost[i]; int ans = solve(0, 0); if (ans >= 1e9) ans = -1; cout << ans << endl; return 0; }
#include <bits/stdc++.h> int compare(const void *a, const void *b) { long long la = *(long long *)a; long long lb = *(long long *)b; return la == lb ? 0 : la < lb ? -1 : 1; } long long aa[8], aa_[8]; int bb[8], dp[8][1 << (8 - 1)]; char pp[8]; int n, ans = 0x3f3f3f3f; void solve(int i) { int j; if (i == n) { int cnt, sum; cnt = 0, sum = 0; for (i = 0; i < n; i++) { int k; if (!pp[i]) cnt++; k = dp[i][bb[i]]; sum += k == 1 && bb[i] == 0 ? 1 : k + 1; } if (cnt >= 2) sum++; if (ans > sum) ans = sum; return; } solve(i + 1); pp[i] = 1; for (j = i + 1; j < n; j++) if (aa_[j] % aa[i] == 0) { aa_[j] /= aa[i], bb[j] ^= 1 << i; solve(i + 1); aa_[j] *= aa[i], bb[j] ^= 1 << i; } pp[i] = 0; } int main() { int i, j; scanf( %d , &n); for (i = 0; i < n; i++) scanf( %lld , &aa[i]); qsort(aa, n, sizeof *aa, compare); for (i = 0; i < n; i++) { long long a; int k, p, b; a = aa[i]; k = 0; for (p = 2; p <= a / p; p++) while (a % p == 0) { k++; a /= p; } if (a > 1) k++; dp[i][0] = k; for (b = 1; b < 1 << i; b++) { a = aa[i]; k = dp[i][0]; for (j = 0; j < i; j++) if ((b & 1 << j)) { if (a % aa[j] != 0) { k = -1; break; } a /= aa[j]; k -= dp[j][0]; } dp[i][b] = k; } } for (i = 0; i < n; i++) aa_[i] = aa[i]; solve(0); printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; vector<string> v; int main() { string x, y; int cnt = 0; char temp; int n; cin >> n; n = n + n - 1; for (int i = 0; i < n; i++) { cin >> x; if (x == ** ) continue; cin >> y; x += y; sort(v.begin(), v.end()); if (binary_search(v.begin(), v.end(), x)) continue; cnt++; v.push_back(x); for (int j = 1; j <= 3; j++) { y = ; if (j == 1) { y += x[2]; y += x[0]; y += x[3]; y += x[1]; v.push_back(y); } else if (j == 2) { y += x[3]; y += x[2]; y += x[1]; y += x[0]; v.push_back(y); } else if (j == 3) { y += x[1]; y += x[3]; y += x[0]; y += x[2]; v.push_back(y); } } } cout << cnt; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; const int inf = 1 << 30; int n, qn, tot = 0, head[N], col[N], nowHead[N], deg[N], seq[N]; int dfsc = 0, id[N], siz[N], fa[N], son[N], dep[N], top[N]; bool vis[N]; struct Edge { int to, nxt; } e[N << 1]; inline void add(int from, int to) { e[++tot].to = to; e[tot].nxt = head[from]; head[from] = tot; } struct Seg { int l, r, c; inline Seg(int L = 0, int R = 0, int C = 0) { l = L, r = R, c = C; } friend bool operator<(const Seg x, const Seg y) { return x.l < y.l; } }; set<Seg> seg[N]; template <typename T> inline void read(T &X) { X = 0; char ch = 0; T op = 1; for (; ch > 9 || ch < 0 ; ch = getchar()) if (ch == - ) op = -1; for (; ch >= 0 && ch <= 9 ; ch = getchar()) X = (X << 3) + (X << 1) + ch - 48; X *= op; } namespace Bit { int s[N << 1]; inline void modify(int p, int v) { if (!p) return; for (; p < (N << 1); p += (p & (-p))) s[p] += v; } inline int query(int p) { int res = 0; for (; p > 0; p -= (p & (-p))) res += s[p]; return res; } } // namespace Bit void dfs1(int x, int fat, int depth) { fa[x] = fat, dep[x] = depth, siz[x] = 1; int maxson = -1; for (int i = head[x]; i; i = e[i].nxt) { int y = e[i].to; if (y == fat) continue; dfs1(y, x, depth + 1); siz[x] += siz[y]; if (siz[y] > maxson) { maxson = siz[y]; son[x] = y; } } } void dfs2(int x, int topf) { top[x] = topf, id[x] = ++dfsc; if (!son[x]) return; dfs2(son[x], topf); for (int i = head[x]; i; i = e[i].nxt) { int y = e[i].to; if (y == fa[x] || y == son[x]) continue; dfs2(y, y); } } inline int getLca(int x, int y) { for (; top[x] != top[y];) { if (dep[top[x]] < dep[top[y]]) swap(x, y); x = fa[top[x]]; } return dep[x] < dep[y] ? x : y; } inline int getDis(int x, int y) { return dep[x] + dep[y] - 2 * dep[getLca(x, y)]; } inline set<Seg>::iterator getSeg(set<Seg> &s, int l) { set<Seg>::iterator it = s.lower_bound(Seg(l, inf, 0)); if (it != s.end() && it->l == l) return it; --it; int nowl = it->l, nowr = it->r, nowc = it->c; s.erase(it); if (l - 1 >= nowl) s.insert(Seg(nowl, l - 1, nowc)); s.insert(Seg(l, nowr, nowc)); return s.lower_bound(Seg(l, nowr, nowc)); } inline void ins(set<Seg> &s, int l, int r, int c, int st) { set<Seg>::iterator itl = getSeg(s, l), itr; if (r + 1 <= id[st] + siz[st] - 1) itr = getSeg(s, r + 1); else itr = s.end(); for (set<Seg>::iterator it = itl; it != itr; ++it) { Bit ::modify(it->c, -((it->r) - (it->l) + 1)); } Bit ::modify(c, r - l + 1); s.erase(itl, itr), s.insert(Seg(l, r, c)); } inline void modify(int x, int y, int c) { nowHead[c] = y; for (; top[x] != top[y];) { if (dep[top[x]] < dep[top[y]]) swap(x, y); ins(seg[top[x]], id[top[x]], id[x], c, top[x]); x = fa[top[x]]; } if (dep[x] > dep[y]) swap(x, y); ins(seg[top[x]], id[x], id[y], c, top[x]); } inline int solve(int x) { int c = getSeg(seg[top[x]], id[x])->c; return Bit ::query(c - 1) + getDis(x, nowHead[c]) + 1; } int main() { read(n), read(qn); for (int x, y, i = 1; i < n; i++) { read(x), read(y); ++deg[x], ++deg[y]; add(x, y), add(y, x); } dfs1(1, 0, 1), dfs2(1, 1); int cnt = 0; set<int> q; for (int i = 1; i <= n; i++) if (deg[i] == 1) q.insert(i), vis[i] = 1; for (; !q.empty();) { int now = (*q.begin()); q.erase(*q.begin()); seq[now] = ++cnt; nowHead[cnt] = now; seg[top[now]].insert(Seg(id[now], id[now], cnt)); Bit ::modify(cnt, 1); for (int i = head[now]; i; i = e[i].nxt) { if (vis[e[i].to]) continue; --deg[e[i].to]; if (deg[e[i].to] == 1 && !vis[e[i].to]) q.insert(e[i].to), vis[e[i].to] = 1; } } int lst = n; for (char op[9]; qn--;) { scanf( %s , op); if (op[0] == u ) { int x; read(x); modify(x, lst, ++cnt); lst = x; } if (op[0] == w ) { int x; read(x); printf( %d n , solve(x)); } if (op[0] == c ) { int x, y; read(x), read(y); printf( %d n , solve(x) < solve(y) ? x : y); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9 + 7; int up(int n) { int r = 1; while (r < n) r <<= 1; return r; } class segtree { vector<int> arr; vector<bool> laz; void inv(int l, int r, int cl, int cr, int v) { if (r <= cl || cr <= l) return; push(v); if (l <= cl && cr <= r) { laz[v] = !laz[v]; return; } int ct = (cl + cr) / 2; inv(l, r, cl, ct, 2 * v); inv(l, r, ct, cr, 2 * v + 1); upd(v); } int cnt(int l, int r, int cl, int cr, int v) { if (r <= cl || cr <= l) return 0; push(v); if (l <= cl && cr <= r) return arr[v]; int ct = (cl + cr) / 2; ct = cnt(l, r, cl, ct, 2 * v) + cnt(l, r, ct, cr, 2 * v + 1); upd(v); return ct; } void push(int v) { if (laz[v]) { laz[v] = false; arr[v] = arr.size() / up(v + 1) - arr[v]; if (v < (int)arr.size() / 2) { laz[2 * v] = !laz[2 * v]; laz[2 * v + 1] = !laz[2 * v + 1]; } } } void upd(int v) { if (v < (int)arr.size() / 2) { push(2 * v); push(2 * v + 1); arr[v] = arr[2 * v] + arr[2 * v + 1]; } } public: segtree(int n) : arr(2 * up(n)), laz(2 * up(n)) {} void inv(int l, int r) { inv(l, r, 0, arr.size() / 2, 1); } int cnt(int l, int r) { return cnt(l, r, 0, arr.size() / 2, 1); } }; void dfs(const vector<vector<int>> &gr, int v, vector<int> &in, vector<int> &out, int &cnt) { in[v] = cnt++; for (auto it : gr[v]) dfs(gr, it, in, out, cnt); out[v] = ++cnt; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<vector<int>> gr(n); for (int i = 1; i < n; i++) { int p; cin >> p; p--; gr[p].push_back(i); } vector<int> in(n), out(n); n = 0; dfs(gr, 0, in, out, n); segtree st(n); n /= 2; for (int i = 0; i < n; i++) { int t; cin >> t; if (t == 1) { st.inv(in[i], in[i] + 1); st.inv(out[i] - 1, out[i]); } } int q; cin >> q; for (int i = 0; i < q; i++) { string s; int t; cin >> s >> t; t--; if (s == get ) { cout << st.cnt(in[t], out[t]) / 2 << endl; } else { assert(s == pow ); st.inv(in[t], out[t]); } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int p1 = 0, q1 = 0, r1 = 0, s1 = 0; int ans = 1; int a = 0, b = 0, c = 0, d = 0; vector<int> v; int sum = 0; for (int j = 0; j < s.length(); j++) { if (s[j] == R ) { if ((q1 > 0)) { ans++; v.push_back(j); q1 = 0; r1 = 0; s1 = 0; } p1++; } if (s[j] == L ) { if ((p1 > 0)) { ans++; v.push_back(j); p1 = 0; r1 = 0; s1 = 0; } q1++; } if (s[j] == U ) { if (s1 > 0) { ans++; v.push_back(j); q1 = 0; p1 = 0; s1 = 0; } r1++; } if (s[j] == D ) { if ((r1 > 0)) { ans++; v.push_back(j); q1 = 0; r1 = 0; p1 = 0; } s1++; } } cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; long long int a[n], sum = 0, fg = 0, cnt = 0, ind = 1, ans = 0; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n; i++) { if (a[i] <= ind) { ind++; continue; } ans = ans + a[i] - ind; ind = a[i] + 1; } cout << ans; cout << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; long long s1[2100][2100], sum[2100][2100], cnt[2100][2100], cnt1[2100][2100]; int main() { int n, m, i, j; long long res; scanf( %d%d , &n, &m); for (i = 2; i <= m; i++) { for (j = 1; j <= n; j++) { cnt[i][j] = sum[i][j - 1] + 1; cnt[i][j] %= 1000000007; s1[i][j] = s1[i - 1][j] + cnt[i][j]; s1[i][j] %= 1000000007; sum[i][j] = sum[i - 1][j] + s1[i][j]; sum[i][j] %= 1000000007; cnt1[i][j] = cnt[i][j] - cnt[i][j - 1]; if (cnt1[i][j] < 0) cnt1[i][j] += 1000000007; } } res = 0; for (i = 2; i <= m; i++) for (j = 1; j <= n; j++) { res += ((cnt[i][j] * cnt1[i][n - j + 1]) % 1000000007) * (m - i + 1); res %= 1000000007; } printf( %I64d n , res); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; s = * + s; int add = 0; int n = (s.length()) - 1; for (int i = 1; i + i <= n; i++) { if (s[i] != s[s.size() - i]) { add++; } } if (add <= 1 && (n & 1) || add == 1) cout << YES n ; else cout << NO n ; }
#include <bits/stdc++.h> using namespace std; int main() { int n, a, i, j, arr[300] = {0}, p = 0, k = 0, c, d; cin >> n >> a; for (i = 1; i < n + 1; i++) { cin >> arr[i]; } arr[0] = 1, arr[n + 1] = 1; j = min(a, n - a + 1); for (i = j; i < n + 1; i++) { c = a + k; d = a - k; if (d < 1) d = 0; if (c > n) c = n + 1; if (arr[d] == arr[c] and arr[c]) { if (c != d and c <= n and d > 0) p += 2; else p++; } k++; } cout << p << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct node { int x, y; } arr[104]; vector<int> graph[105]; int visited[105] = {0}; void dfs(int i) { visited[i] = 1; int j = 0; while (j < graph[i].size()) { if (visited[graph[i][j]] == 0) { dfs(graph[i][j]); } j++; } } int main() { int n; scanf( %d , &n); int x, y; for (int i = 0; i < n; i++) { scanf( %d%d , &x, &y); arr[i].x = x; arr[i].y = y; } int hash[102] = {0}; int ct = 0; for (int i = 0; i < n; i++) { int flag = 0; for (int j = 0; j < n; j++) { if (i == j) continue; if (arr[j].x == arr[i].x || arr[j].y == arr[i].y) { graph[i].push_back(j); graph[j].push_back(i); flag = 1; } } if (flag == 0) visited[i] = 1; } for (int i = 0; i < n; i++) { if (visited[i] == 0) { dfs(i); ct++; } } int c = 0; for (int i = 0; i < n; i++) { if (!graph[i].size()) c++; } printf( %d n , c - 1 + ct); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXT = (1 << 18) + 5; int icT[MAXT], dcT[MAXT]; const int MAXN = 100010; int a[MAXN], idxofa[MAXN], b[MAXN]; int n; priority_queue<pair<int, int> > dc, ic; int main() { scanf( %d , &n); for (int i = 1; i <= (int)(n); i++) { scanf( %d , &a[i]); idxofa[a[i]] = i; } for (int i = 0; i < (int)(MAXT); i++) icT[i] = dcT[i] = 1000000000; int NN = (1 << 17) - 1; for (int i = 1; i <= (int)(n); i++) { scanf( %d , &b[i]); if (i > idxofa[b[i]]) { dc.push(make_pair(-(i - idxofa[b[i]]), b[i])); dcT[NN + b[i]] = abs(i - idxofa[b[i]]); for (int node = (NN + b[i]) / 2; node; node >>= 1) dcT[node] = min(dcT[2 * node], dcT[2 * node + 1]); } else { ic.push(make_pair(-(i - 1), b[i])); icT[NN + b[i]] = abs(i - idxofa[b[i]]); for (int node = (NN + b[i]) / 2; node; node >>= 1) icT[node] = min(icT[2 * node], icT[2 * node + 1]); } } for (int t = 0; t < n; t++) { int dcres = dcT[1] - t; int icres = icT[1] + t; printf( %d n , min(dcres, icres)); while (dc.size() > 0 && -dc.top().first == t) { pair<int, int> qt = dc.top(); dc.pop(); ic.push(make_pair(-t - (idxofa[qt.second] - 1), qt.second)); dcT[NN + qt.second] = 1000000000; icT[NN + qt.second] = -t; for (int node = (NN + qt.second) / 2; node; node >>= 1) icT[node] = min(icT[2 * node], icT[2 * node + 1]); for (int node = (NN + qt.second) / 2; node; node >>= 1) dcT[node] = min(dcT[2 * node], dcT[2 * node + 1]); } while (ic.size() > 0 && -ic.top().first == t) { pair<int, int> qt = ic.top(); ic.pop(); dc.push(make_pair(-t - (n - idxofa[qt.second]) - 1, qt.second)); icT[NN + qt.second] = 1000000000; dcT[NN + qt.second] = t + 1 + n - idxofa[qt.second]; for (int node = (NN + qt.second) / 2; node; node >>= 1) icT[node] = min(icT[2 * node], icT[2 * node + 1]); for (int node = (NN + qt.second) / 2; node; node >>= 1) dcT[node] = min(dcT[2 * node], dcT[2 * node + 1]); } } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; const long long inf = 0x3f3f3f3f3f3f3f3f; int n, k, b, c; int a[N], A[N], cost[N]; long long ans = inf; void solve(int x) { for (int i = 1; i <= n; ++i) { A[i] = a[i]; cost[i] = 0; while (A[i] % 5 != x) ++A[i], cost[i] += c; } priority_queue<long long> heap; long long res = 0; for (int i = 1; i <= k; ++i) { long long val = 5 * cost[i] - 1LL * b * A[i]; res += val; heap.push(val); } ans = min(ans, (res + 1LL * k * b * A[k]) / 5); for (int i = k + 1; i <= n; ++i) { long long val = 5 * cost[i] - 1LL * b * A[i]; if (val < heap.top()) { res -= heap.top(); heap.pop(); heap.push(val); res += val; } ans = min(ans, (res + 1LL * k * b * A[i]) / 5); } } int main() { cin >> n >> k >> b >> c; for (int i = 1; i <= n; ++i) { scanf( %d , &a[i]); a[i] += 1e9; } sort(a + 1, a + n + 1); b = min(b, c * 5); for (int i = 0; i < 5; ++i) solve(i); cout << ans << endl; return 0; }
#include <bits/stdc++.h> const int N = 1e5 + 5; const int OO = 1e9 + 7; using namespace std; long long n, m, dishes[N], cost[N]; priority_queue<pair<long long, long long>, vector<pair<long long, long long>>, greater<pair<long long, long long>>> q; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n >> m; for (int i = 0; i < n; i++) cin >> dishes[i]; for (int i = 0; i < n; i++) cin >> cost[i], q.push({cost[i], i}); for (int i = 0; i < m; i++) { long long kind, numofdishes, COST = 0; cin >> kind >> numofdishes; kind--; if (numofdishes <= dishes[kind]) { COST += (numofdishes * cost[kind]); dishes[kind] -= numofdishes; cout << COST << n ; } else { COST += (dishes[kind] * cost[kind]); numofdishes -= dishes[kind]; dishes[kind] = 0; while (!q.empty() && numofdishes > 0) { if (dishes[q.top().second] <= 0) { q.pop(); continue; } if (numofdishes <= dishes[q.top().second]) { COST += (numofdishes * q.top().first); dishes[q.top().second] -= numofdishes; numofdishes = 0; } else { COST += (dishes[q.top().second] * q.top().first); numofdishes -= dishes[q.top().second]; dishes[q.top().second] = 0; } if (dishes[q.top().second] <= 0) { q.pop(); continue; } } if (numofdishes > 0) cout << 0 << n ; else cout << COST << n ; } } }
#include <bits/stdc++.h> using namespace std; void myswap(int& a, int& b) { int temp; temp = a; a = b; b = temp; } void fun(vector<int>& vec, int k, int l, int r) { if (k <= 1 || l >= r - 1) { return; } int mid = (l + r) / 2; if (k == 3) { myswap(vec[mid - 1], vec[mid]); return; } k--; int p = k / 2; if (p % 2 == 1) { fun(vec, p, l, mid); fun(vec, p, mid, r); } else if (mid - l >= r - mid) { fun(vec, p + 1, l, mid); fun(vec, p - 1, mid, r); } else { fun(vec, p - 1, l, mid); fun(vec, p + 1, mid, r); } } int _count(int l, int r) { if (l >= r) { return 0; } if (l == r - 1) { return 1; } int mid = (l + r) / 2; return 1 + _count(l, mid) + _count(mid, r); } int main() { int n, k, i, j; cin >> n >> k; if (k % 2 == 0) { cout << -1; } else if (k == 1) { for (i = 1; i <= n; i++) { cout << i << ; } } else { int mm = _count(0, n); if (k > mm) { cout << -1; } else { vector<int> vec; for (i = 1; i <= n; i++) { vec.push_back(i); } fun(vec, k, 0, n); for (i = 0; i < n; i++) { cout << vec[i] << ; } } } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, k; cin >> n >> k; string s; cin >> s; int freq[26] = {0}; vector<int> v; for (int i = 0; i < s.length(); ++i) { freq[s[i] - a ]++; } for (int i = 0; i < 26; ++i) { v.push_back(freq[i]); } sort(v.begin(), v.end()); int maxAns = 1; for (int i = 1; i <= k; ++i) { if (k % i == 0) { for (int j = i; j <= n; j += i) { int ans = 0; int div = j / i; for (int k = 0; k < v.size(); ++k) { ans += v[k] / div; } if (ans >= i) { maxAns = max(maxAns, j); } } } } cout << maxAns << endl; } }
#include <bits/stdc++.h> int t; long long n, k, d1, d2, td1, td2, x1, x2, x3; bool solve() { if (n % 3) return false; for (int i = -1; i <= 1; ++i) for (int j = -1; j <= 1; ++j) { if (i == 0 || j == 0) continue; d1 = i * td1; d2 = j * td2; if ((d1 * 2 + d2 + k) % 3 || (d2 - d1 + k) % 3 || (k - d1 - 2 * d2) % 3) continue; x1 = d1 * 2 + d2 + k; x2 = d2 - d1 + k; x3 = k - d1 - 2 * d2; if (x1 <= n && x2 <= n && x3 <= n && x1 >= 0 && x2 >= 0 && x3 >= 0) return true; } return false; } int main() { scanf( %d , &t); while (t--) { scanf( %lld%lld%lld%lld , &n, &k, &td1, &td2); puts(solve() ? yes : no ); } return 0; }
#include <bits/stdc++.h> int n; char str[50]; int F[50], sz[50]; inline int find(int x) { return x == F[x] ? x : F[x] = find(F[x]); } inline void merge(int x, int y) { x = find(x); y = find(y); if (x == y) return; F[y] = x; sz[x] += sz[y]; } std::vector<int> G[55]; int id[66]; int e[25]; int f[(1 << 23) + 5], g[(1 << 23) + 5], coe[(1 << 23) + 5]; int main() { scanf( %d , &n); for (int i = 0; i <= n - 1; ++i) F[i] = i, sz[i] = 1; for (int i = 0; i <= n - 1; ++i) { scanf( %s , str); for (int j = i + 1; j <= n - 1; ++j) { if (str[j] == A ) merge(i, j); if (str[j] == X ) G[i].emplace_back(j), G[j].emplace_back(i); } } int ans = n - 1; n = 0; for (int i = 0; i <= ans; ++i) id[i] = find(i); for (int i = 0; i <= ans; ++i) for (auto x : G[i]) if (id[i] == id[x]) { puts( -1 ); exit(0); } for (int i = 0; i <= ans; ++i) { if (sz[id[i]] == 1) { id[i] = -1; continue; } if (id[i] != i) { id[i] = id[id[i]]; continue; } id[i] = n++; } if (n <= 1) { printf( %d n , ans + n); return 0; } assert(n <= 23); for (int i = 0; i <= ans; ++i) for (auto x : G[i]) if (~id[i] && ~id[x]) e[id[i]] |= (1 << id[x]); f[0] = coe[0] = 1; for (int S = 1; S <= (1 << n) - 1; ++S) { int lb = __builtin_ctz(S); f[S] = (!(e[lb] & S)) & f[S ^ (1 << lb)]; coe[S] = -coe[S ^ (1 << lb)]; } for (int i = 0; i <= n - 1; ++i) { for (int S = 0; S <= (1 << n) - 1; ++S) { if ((S >> i) & 1) f[S] += f[S ^ (1 << i)]; } } for (int i = 0; i <= (1 << n) - 1; ++i) g[i] = f[i]; for (int k = 1; k <= n; ++k) { auto chk = [&]() { int ans = 0; for (int S = 0; S <= (1 << n) - 1; ++S) ans += g[S] * coe[S]; return ans != 0; }; if (chk()) { printf( %d n , ans + k); return 0; } for (int S = 0; S <= (1 << n) - 1; ++S) g[S] *= f[S]; } return 0; }
#include <bits/stdc++.h> int main() { int n; while (scanf( %d , &n) != EOF) { if (n % 2 == 1) { printf( -1 n ); continue; } printf( 2 1 ); for (int i = 3; i < n; i = i + 2) printf( %d %d , i + 1, i); printf( n ); } return 0; }
#include <bits/stdc++.h> using namespace std; map<int, int> Map; pair<int, int> a[250001]; vector<int> b[250001], t[250001]; char type; long long ans; int len[250001], n, m, i, q, l, r, d, mid, nom, nomN, zn, sum; bool pole[250001]; int get(int tl, int tr, int nom, int l, int r, int nomT) { if (tl > tr) return 0; if (tl > r || tr < l) return 0; if (l <= tl && tr <= r) return t[nomT][nom]; int mid = (tl + tr) / 2; return get(tl, mid, nom * 2, l, r, nomT) + get(mid + 1, tr, nom * 2 + 1, l, r, nomT); } void update(int tl, int tr, int nom, int nomN, int zn, int nomT) { if (tl > tr) return; if (tl > nomN || tr < nomN) return; t[nomT][nom] += zn; if (tl == tr) return; int mid = (tl + tr) / 2; update(tl, mid, nom * 2, nomN, zn, nomT); update(mid + 1, tr, nom * 2 + 1, nomN, zn, nomT); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> q; for (i = 0; i < n; i++) if (!pole[i]) { nom = i; d++; t[d].push_back(0); b[d].push_back(0); while (!pole[nom]) { pole[nom] = true; len[d]++; a[nom + 1] = make_pair(d, len[d]); b[d].push_back(nom); t[d].push_back(0); t[d].push_back(0); t[d].push_back(0); t[d].push_back(0); nom = (nom + m) % n; } } while (q--) { cin >> type; if (type == + ) { cin >> zn >> nom; nom++; sum = 0; if (get(1, len[a[nom].first], 1, a[nom].second, len[a[nom].first], a[nom].first) != len[a[nom].first] - a[nom].second + 1) { l = a[nom].second; r = len[a[nom].first]; while (l <= r) { mid = (l + r) / 2; if (get(1, len[a[nom].first], 1, a[nom].second, mid, a[nom].first) != mid - a[nom].second + 1) { r = mid - 1; sum = mid - a[nom].second; } else l = mid + 1; } update(1, len[a[nom].first], 1, a[nom].second + sum, 1, a[nom].first); Map[zn] = b[a[nom].first][a[nom].second + sum] + 1; } else { ans += len[a[nom].first] - a[nom].second + 1; l = 1; r = a[nom].second - 1; while (l <= r) { mid = (l + r) / 2; if (get(1, len[a[nom].first], 1, 1, mid, a[nom].first) != mid) { r = mid - 1; sum = mid - 1; } else l = mid + 1; } update(1, len[a[nom].first], 1, sum + 1, 1, a[nom].first); Map[zn] = b[a[nom].first][sum + 1] + 1; } ans += sum; } else { cin >> zn; nom = Map[zn]; update(1, len[a[nom].first], 1, a[nom].second, -1, a[nom].first); } } cout << ans << n ; return 0; }
#include <bits/stdc++.h> using namespace std; const long long int nax = 1e5 + 5; void solve() { long long int n; cin >> n; vector<long long int> a(n); for (auto &x : a) cin >> x; long long int sum = 0, mx = 0; for (long long int i = 0; i < n - 1; i++) { sum += max((long long int)0, a[i] - a[i + 1]); } cout << sum << n ; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t = 1; cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; inline int read() { int n = 0, f = 1, ch = getchar(); while (ch < 0 || ch > 9 ) { if (ch == - ) f = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) { n = n * 10 + ch - 0 ; ch = getchar(); } return n * f; } char a[2000005], b[2000005]; int n; int c[2000005]; int get(int x) { int cur = 0, res = 0; for (int i = 1; i <= n; i++) { cur += x * c[i]; res = max(res, cur); if (cur < 0) cur = 0; } return res; } int main() { int s1 = 0, s2 = 0; n = read(); scanf( %s%s , a + 1, b + 1); for (int i = 1; i <= n; i++) { if (a[i] != b[i] && a[i] == 0 ) c[i] = -1, s1++; else if (a[i] != b[i] && a[i] == 1 ) c[i] = 1, s2++; } if (s1 != s2) { printf( -1 n ); return 0; } printf( %d n , max(get(1), get(-1))); return 0; }
#include <bits/stdc++.h> using namespace std; long double sq(long double a) { return a * a; } long double dist(pair<long double, long double> a, pair<long double, long double> b) { return sqrt((long double)(sq(a.second - b.second) + sq(a.first - b.first))); } long double mandist(pair<long long, long long> a, pair<long long, long long> b) { return abs(a.second - b.second) + abs(a.first - b.first); } long long a, b, c; pair<long long, long long> p1, p2; int main() { scanf( %lld%lld%lld , &a, &b, &c); scanf( %lld%lld%lld%lld , &p1.first, &p1.second, &p2.first, &p2.second); if (a == 0 || b == 0) printf( %.20Lf n , mandist(p1, p2)); else { long double res = mandist(p1, p2); long double interhoriz1 = ((long double)(-c - a * p1.first)) / ((long double)b); long double interhoriz2 = ((long double)(-c - a * p2.first)) / ((long double)b); long double intervert1 = ((long double)(-c - b * p1.second)) / ((long double)a); long double intervert2 = ((long double)(-c - b * p2.second)) / ((long double)a); res = min(res, abs((long double)p1.second - interhoriz1) + abs((long double)p2.second - interhoriz2) + dist(make_pair((long double)p1.first, interhoriz1), make_pair((long double)p2.first, interhoriz2))); res = min(res, abs((long double)p1.second - interhoriz1) + abs((long double)p2.first - intervert2) + dist(make_pair((long double)p1.first, interhoriz1), make_pair(intervert2, (long double)p2.second))); res = min(res, abs((long double)p1.first - intervert1) + abs((long double)p2.first - intervert2) + dist(make_pair(intervert1, (long double)p1.second), make_pair(intervert2, (long double)p2.second))); res = min(res, abs((long double)p1.first - intervert1) + abs((long double)p2.second - interhoriz2) + dist(make_pair(intervert1, (long double)p1.second), make_pair((long double)p2.first, interhoriz2))); printf( %.20Lf n , res); } }
#include <bits/stdc++.h> using namespace std; int a[2005], b[2005]; int main() { int n, i, j, x, y, k; long long s; cin >> n; for (i = 0; i < n; i++) { cin >> x >> y; a[x + y]++; b[1001 + x - y]++; } s = 0; for (i = 0; i < 2003; i++) { k = a[i]; s += (k - 1) * k / 2; k = b[i]; s += (k - 1) * k / 2; } cout << s << endl; return 0; }
#include <bits/stdc++.h> using namespace std; void solve(); signed main() { ios::sync_with_stdio(false); cin.tie(0); cout << fixed; cout.precision(12); solve(); return 0; } template <typename T> void prv(vector<T> v) { for (int __ii = 0; __ii < ((int)v.size()); __ii++) { if (__ii) cout << ; cout << v[__ii]; } cout << n ; } template <typename T> void prm(vector<vector<T>> v) { for (int __ii = 0; __ii < ((int)v.size()); __ii++) { for (int __jj = 0; __jj < v[__ii].size(); __jj++) { if (__jj) cout << ; cout << v[__ii][__jj]; } cout << n ; } } template <typename T> void sc(T& x) { cin >> x; } template <typename Head, typename... Tail> void sc(Head& head, Tail&... tail) { cin >> head; sc(tail...); } template <typename T> void pr(const T& x) { cout << x << n ; } template <typename Head, typename... Tail> void pr(const Head& head, const Tail&... tail) { cout << head << ; pr(tail...); } template <typename... T> void err(const T&... cod) { pr(cod...); exit(0); } void solve() { int n, m, y, x; sc(n, m, y, x); string s; sc(s); int dx = (s[1] == L ? -1 : 1); int dy = (s[0] == U ? -1 : 1); const int MAX_ITER = 1e6 + 10; set<pair<int, int>> st; long long ans = 1; for (int i = 0; i < MAX_ITER; i++) { st.emplace(y, x); if (((int)st.size()) == n + m - 2) err(ans); int u = (dy == -1 ? y - 1 : n - y); int v = (dx == -1 ? x - 1 : m - x); int w = min(u, v); x += dx * w; y += dy * w; ans += w; if (v == w) dx *= -1; if (u == w) dy *= -1; } pr(-1); }
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0); string s; cin >> s; string v = aeiou ; string c = bcdfghjklmnpqrstvwxyz ; map<char, int> cnt; map<char, int> mp; for (auto i : v) mp[i] = 1; for (auto i : c) mp[i] = 0; string temp = ; int vc = 0; int cc = 0; int dc = 0; vector<string> ans; for (int i = 0; i < s.size(); i++) { if (mp[s[i]] == 1) { cc = 0; dc = 0; for (auto j : c) cnt[j] = 0; } else { if (cnt[s[i]] == 0) { cnt[s[i]]++; dc++; cc++; } else { cnt[s[i]]++; cc++; } } if (cc > 2 && dc > 1) { ans.push_back(temp); for (auto j : c) cnt[j] = 0; cnt[s[i]] = 1; cc = 1; dc = 1; temp = s[i]; } else temp += s[i]; } ans.push_back(temp); for (auto i : ans) cout << i << ; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; string s[10005]; int vis[10005]; int main() { ios::sync_with_stdio(0); int n, sum(0); cin >> n; for (long long i = 0; i < n; ++i) cin >> s[i], sum += s[i].size(); char c; cin >> c; for (long long i = 0; i < n; ++i) s[i] += c; sort(s, s + n); sum = sum * 2 / n + 1; for (long long i = 0; i < n; ++i) if (!vis[i]) { cout << s[i]; vis[i]++; for (long long j = i + 1; j < n; ++j) if (!vis[j] && sum - s[i].size() == s[j].size() - 1) { cout << s[j].substr(0, s[j].size() - 1) << n ; vis[j]++; break; } } }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; map<int, int> used; int ans = 0; for (int i = n - 1; i >= 0; --i) { if (used[a[i]] == 0) { used[a[i]] = i; ans = a[i]; } } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; const long double E = 1e-7; const int MOD = 1e9 + 7; const long long MAX = 1e6 + 1; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n, k; cin >> n >> k; int ar[n]; for (int i = 0; i < n; i++) cin >> ar[i]; int ans = INT_MIN; int a[n], b[n]; for (int i = 0; i < n; i++) for (int j = i; j < n; j++) { int A = j - i + 1; int B = n - A; copy(ar + i, ar + j + 1, a); copy(ar, ar + i, b); copy(ar + j + 1, ar + n, b + i); sort(a, a + A); sort(b, b + B); for (int l = 0, r = B - 1, q = k; l < A && r >= 0 && a[l] < b[r] && q; l++, r--, --q) swap(a[l], b[r]); int sum = 0; for (int i = 0; i < A; i++) sum += a[i]; ans = max(ans, sum); } cout << ans; }
#include <bits/stdc++.h> using namespace std; int gcd(int x, int y) { if (x > y) return gcd(x - y, y); else if (x == y) return x; else return gcd(x, y - x); } int main() { int n, m, z; cin >> n >> m >> z; cout << (gcd(n, m) * z) / (n * m) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, cnt, flag = 0; cin >> n; int a, b, c = 0; for (int i = 1; i <= n; i++) { cin >> a >> b; if (b - a >= 2) ++c; } cout << c << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int n, p = 0, ans = 0; string s, dvg, answ, r_dvg; cin >> n >> s; for (int i = 0; i < n - 1; i++) { dvg = s.substr(i, 2); for (int j = i; j < n - 1; j++) { r_dvg = s.substr(j, 2); if (dvg == r_dvg) { p++; } } if (p > ans) { ans = p; answ = dvg; } p = 0; } cout << answ << endl; return 0; }
#include <bits/stdc++.h> using namespace std; struct Point { long long x, y; void read() { scanf( %lld%lld , &x, &y); } }; long long product(const Point &o, const Point &a, const Point &b) { return (a.x - o.x) * (b.y - o.y) - (b.x - o.x) * (a.y - o.y); } bool intersect(const Point &a, const Point &b, const Point &c, const Point &d) { bool insect1 = product(a, c, d) * product(b, c, d) < 0; bool insect2 = product(c, a, b) * product(d, a, b) < 0; return insect1 && insect2; } int main() { ios ::sync_with_stdio(false); cout << fixed << setprecision(16); int R, B; scanf( %d%d , &R, &B); vector<Point> bases(B); vector<Point> ships(R); vector<int> b(R); for (int i = 0; i < R; i++) { ships[i].read(); } for (int i = 0; i < B; i++) { bases[i].read(); } if (B != R) { puts( No ); return 0; } for (int i = 0; i < R; i++) { b[i] = i; } bool yes = false; do { bool ok = true; for (int i = 0; i < B; i++) { for (int j = i + 1; j < B; j++) { if (intersect(ships[b[i]], bases[i], ships[b[j]], bases[j])) { ok = false; } } } if (ok) { yes = true; break; } } while (next_permutation(b.begin(), b.end())); puts(yes ? Yes : No ); return 0; }
#include <bits/stdc++.h> using namespace std; const long long maxn = 305000; long long mod = 1e9 + 7; long long h, w, n; long long dp[maxn], f[maxn], inv[maxn]; struct stu { long long x, y; } a[maxn]; long long cmp(stu a, stu b) { if (a.x != b.x) return a.x < b.x; else return a.y < b.y; } long long ksm(long long base, long long b, long long mod) { long long ans = 1; while (b) { if (b & 1) { ans *= base; ans %= mod; } base *= base; base %= mod; b >>= 1; } return ans; } void init() { f[0] = 1, inv[0] = 1; for (long long i = 1; i <= 200000; i++) { f[i] = f[i - 1] * i % mod; inv[i] = ksm(f[i], mod - 2, mod); } } long long c(long long x, long long y) { if (x < y) return 0; else return f[x] * inv[y] % mod * inv[x - y] % mod; } signed main() { cin >> h >> w >> n; init(); for (long long i = 1; i <= n; i++) cin >> a[i].x >> a[i].y; sort(a + 1, a + n + 1, cmp); a[n + 1].x = h, a[n + 1].y = w; for (long long i = 1; i <= n + 1; i++) { dp[i] = c(a[i].x + a[i].y - 2, a[i].x - 1); for (long long j = 1; j < i; j++) { if (a[j].x > a[i].x || a[j].y > a[i].y) continue; dp[i] = (dp[i] - dp[j] * c(a[i].x + a[i].y - a[j].x - a[j].y, a[i].x - a[j].x)) % mod; } } cout << (dp[n + 1] + mod) % mod << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int v[3005], inv = 0, n; double sol; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> v[i]; for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) if (v[i] > v[j]) inv++; sol = double(inv) - inv / 2 + (inv / 2) * 3.0; cout << fixed; cout << setprecision(6); cout << sol << n ; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = 1000 * 1000 * 1000; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; pair<int, int> minp1(INF, INF), minp2(INF, INF); for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; minp1 = min(minp1, pair<int, int>(x, y)); } for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; x *= -1; y *= -1; minp2 = min(minp2, pair<int, int>(x, y)); } cout << minp1.first - minp2.first << << minp1.second - minp2.second; }
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf( %d , &n); printf( %d n , (n & 1) ? (n - 1) + (n - 1) * ((n - 3) / 2) / 2 : (n - 2) * (n - 2) / 4 + (n - 2)); if (n & 1) { for (int i = 2; i <= n; i += 2) { printf( %d %d %d %d n , 3, 1, i, i + 1); printf( %d %d %d %d n , 3, 1, i, i + 1); for (int j = 2; j < i; j += 2) { printf( %d %d %d %d %d n , 4, i, j, i + 1, j + 1); printf( %d %d %d %d %d n , 4, i, j, i + 1, j + 1); } } } else { puts( 4 1 2 3 4 ), puts( 4 1 3 4 2 ), puts( 4 1 4 2 3 ); for (int i = 5; i <= n; i += 2) { printf( %d %d %d %d n , 3, 1, i, i + 1); printf( %d %d %d %d n , 3, 2, i, i + 1); printf( %d %d %d %d %d n , 4, 1, i, 2, i + 1); for (int j = 3; j < i; j += 2) { printf( %d %d %d %d %d n , 4, i, j, i + 1, j + 1); printf( %d %d %d %d %d n , 4, i, j, i + 1, j + 1); } } } return 0; }
#include <bits/stdc++.h> const int MAXN = 100100; struct Gao { int p[MAXN], q[MAXN]; bool r[MAXN]; void init(int n) { for (int i = 0; i < n; ++i) { q[i] = 1; r[i] = true; } } void next(int i) { if (!r[p[i]] && p[i] != i) { next(p[i]); q[i] += q[p[i]]; p[i] = p[p[i]]; } } void kill(int i) { if (r[i]) { next(i); r[i] = false; } } } gao; int main() { int n, m, x, y, z; static int a[MAXN], b[MAXN]; scanf( %d%d , &n, &m); gao.init(n); for (int i = 0; i < n; ++i) { scanf( %d , &gao.p[i]); --gao.p[i]; } for (int i = 0; i < m; ++i) { scanf( %d , &a[i]); --a[i]; } for (int i = 0; i < m; ++i) { scanf( %d , &b[i]); } z = 0; for (int i = 0; i < m; ++i) { x = (a[i] + z) % n; y = b[i]; z = 0; while (true) { if (!gao.r[x]) { gao.next(x); y -= gao.q[x]; x = gao.p[x]; } if (gao.r[x] && y > 0) { ++z; gao.kill(x); } else { break; } } printf( %d n , z); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 4e4 + 5; int pr[N], ps = 0; bool np[N]; bool check(int n) { if (n == 1 || n == 0) return false; for (int i = 0; pr[i] * pr[i] <= n; ++i) if (n % pr[i] == 0) return false; return true; } int main() { for (int i = 2; i < N; ++i) if (!np[i]) { pr[ps++] = i; for (int j = i + i; j < N; j += i) np[j] = 1; } int n; cin >> n; if (check(n)) { cout << 1 n << n << endl; return 0; } for (int i = 0; i < ps && pr[i] <= n; ++i) { if (check(n - pr[i])) { cout << 2 n << pr[i] << << n - pr[i] << endl; return 0; } if (pr[i] % 2 == 1 && check((n - pr[i]) / 2)) { cout << 3 n << pr[i] << << (n - pr[i]) / 2 << << (n - pr[i]) / 2 << endl; return 0; } } cout << lol nope n ; return 0; }
#include <bits/stdc++.h> using namespace std; const long long mk = 100005; long long n; vector<long long> g[mk]; long long find(long long A) { if (A == 0) return 0; long long tmp = A & 3; long long R = (find(A / 4) << 2); if (tmp == 0) return R; if (tmp == 1) return (R | 2); if (tmp == 2) return (R | 3); if (tmp == 3) return (R | 1); } void init() { long long test; cin >> test; for (long long i = 1; i <= test; i++) { long long n; cin >> n; long long root = 1; while (root * 4 <= n) root *= 4; n -= (root - 1); long long A = (root | ((n - 1) / 3)); long long B = find(A); long long C = A ^ B; if (n % 3 == 1) cout << A; if (n % 3 == 2) cout << B; if (n % 3 == 0) cout << C; cout << n ; } } signed main() { init(); }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e4 + 5; pair<int, int> p[maxn]; set<pair<int, int> > s; void solve(int l, int r) { if (r - l < 2) return; int mid = (l + r) / 2; solve(l, mid), solve(mid, r); for (int i = l; i < r; i++) s.insert(make_pair(p[mid].first, p[i].second)); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 0; i < n; i++) cin >> p[i].first >> p[i].second, s.insert(make_pair(p[i].first, p[i].second)); sort(p, p + n); solve(0, n); cout << s.size() << endl; for (pair<int, int> now : s) cout << now.first << << now.second << endl; return 0; }
#include <bits/stdc++.h> int main() { int n; long long int s; scanf( %d , &n); s = 6 * n * (n - 1) + 1; printf( %I64d , s); return 0; }
#include <bits/stdc++.h> template <class C, class E> inline bool contains(const C& container, const E& element) { return container.find(element) != container.end(); } template <class T> inline void checkmin(T& a, T b) { if (b < a) a = b; } template <class T> inline void checkmax(T& a, T b) { if (b > a) a = b; } using namespace std; class UnionFind { public: UnionFind(int elements) : _subsets(elements), _setCount(elements) { for (int i = 0; i < elements; ++i) { _subsets[i].parent = i; _subsets[i].rank = 0; } } int Find(int i) { if (_subsets[i].parent != i) _subsets[i].parent = Find(_subsets[i].parent); return _subsets[i].parent; } void Union(int x, int y) { int xroot = Find(x); int yroot = Find(y); if (xroot == yroot) return; --_setCount; if (_subsets[xroot].rank < _subsets[yroot].rank) _subsets[xroot].parent = yroot; else if (_subsets[xroot].rank > _subsets[yroot].rank) _subsets[yroot].parent = xroot; else { _subsets[yroot].parent = xroot; _subsets[xroot].rank++; } } struct subset { int parent; int rank; }; void Trace() { for (int i = (0), _b = ((_subsets.size()) - 1); i <= _b; i++) cout << i << : << Find(i) << | ; cout << endl; } vector<subset> _subsets; int _setCount; }; struct SetInfo { set<int> _yc; set<int> _xc; }; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout << std::setprecision(15); cout << std::fixed; long long xs, ys, q; cin >> xs >> ys >> q; UnionFind uf(xs + ys); vector<pair<int, int> > p(q); for (int i = (0), _b = ((q)-1); i <= _b; i++) { int x, y; cin >> p[i].first >> p[i].second; --p[i].first; --p[i].second; uf.Union(p[i].first, xs + p[i].second); } map<int, SetInfo> ss; for (int i = (0), _b = ((q)-1); i <= _b; i++) { int id = uf.Find(p[i].first); SetInfo& setInfo = ss[id]; setInfo._xc.insert(p[i].first); setInfo._yc.insert(p[i].second); } long long got = 0; long long totX = 0, totY = 0; for (auto& p : ss) { totX += (long long)p.second._xc.size(); totY += (long long)p.second._yc.size(); } long long remX = xs - totX; long long remY = ys - totY; long long res = remX + remY - 1 + ss.size(); cout << res; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> T sqr(T x) { return x * x; } template <typename T> T abs(T x) { return x < 0 ? -x : x; } template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } template <typename T> bool chmin(T& x, const T& y) { if (x > y) { x = y; return true; } return false; } template <typename T> bool chmax(T& x, const T& y) { if (x < y) { x = y; return true; } return false; } string to_bin(int x) { string ret; while (x) { ret = ret + (char)((x % 2) + 0 ); x /= 2; } return ret; } int main(int, char**) { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { long long n; cin >> n; long long m = (n - 1) / 3; int k = 0; long long p = 1; while (m >= p) { m -= p; k += 1; p *= 4; } vector<long long> a; for (int i = 0; i < k; ++i) { a.push_back(m % 4); m /= 4; } long long A, B; A = 1LL << (2 * k); B = 1LL << (2 * k + 1); vector<long long> b = {0, 2, 3, 1}; for (int i = 0; i < k; ++i) { A ^= a[i] << (2 * i); B ^= b[a[i]] << (2 * i); } long long C = A ^ B; long long ans; if (n % 3 == 1) { ans = A; } else if (n % 3 == 2) { ans = B; } else { ans = C; } cout << ans << n ; } cerr << Time execute: << clock() / (double)CLOCKS_PER_SEC << sec << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { double n, m, a, b; cin >> n >> m >> a >> b; double s = n / m; long long int s1 = floor(s) * m; long long int s2 = ceil(s) * m; long long int a1 = (n - s1) * b; long long int a2 = (s2 - n) * a; cout << (min(a1, a2)); }