func_code_string
stringlengths 59
71.4k
|
---|
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; const int LIM = 100005; long long power(long long x, long long n) { long long res = 1; while (n) { if (n & 1) { res = res * x % mod; } x = x * x % mod; n >>= 1; } return (res % mod); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 0; cin >> t; while (t--) { long long n = 0, x = 0, y = 0; cin >> n >> x >> y; vector<long long> v; long long dif = y - x; long long c = n - 1; long long d = 0; for (c = n - 1; c >= 1; c--) { if (dif % c == 0) { d = dif / c; break; } } for (long long i = x; i <= y; i += d) { v.push_back(i); n--; } long long now = x - d; for (; n > 0;) { if (now <= 0) break; else { v.push_back(now); now -= d; n--; } } now = y + d; for (; n > 0;) { if (now <= 0) break; else { v.push_back(now); now += d; n--; } } sort(v.begin(), v.end()); for (long long i = 0; i < v.size(); i++) { cout << v[i] << ; } cout << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long int n, s, x, k, ans = 0, ch = 0; cin >> n >> s; s = 8 * s; vector<long long int> vec(n); vector<pair<long long int, long long int>> dat; for (long long int i = 0; i < n; i++) cin >> vec[i]; sort(vec.begin(), vec.end()); dat.push_back(make_pair(vec[0], 1)); x = vec[0]; for (long long int i = 1; i < n; i++) { if (vec[i] != x) { dat.push_back(make_pair(vec[i], 1)); x = vec[i]; } else { dat[dat.size() - 1].second++; } } while (1) { k = 0; while (pow(2, k) < dat.size() - ch) k++; k = n * k; if (k <= s) break; ch++; } x = 0; for (long long int i = 0; i < ch; i++) x = x + dat[i].second; ans = x; for (long long int i = 0; i < ch; i++) { x = x - dat[ch - i - 1].second + dat[dat.size() - i - 1].second; if (x < ans) ans = x; } cout << ans << endl; } |
#include <bits/stdc++.h> using namespace std; const int maxn = (int)1e6; const int inf = (1ll << 31) - 1; int p[maxn], l[maxn]; int n; pair<pair<int, int>, int> e[maxn]; pair<int, int> st[maxn]; int sum[maxn], answer[maxn]; int main() { scanf( %d , &n); for (int i = 0; i < n; ++i) { scanf( %d%d , &p[i], &l[i]); } int m; scanf( %d , &m); for (int i = 0; i < m; ++i) { int l, r; scanf( %d%d , &l, &r); l--; r--; e[i] = make_pair(make_pair(l, r), i); } sort(e, e + m); reverse(e, e + m); int ei = 0, stn = 0; for (int i = n - 1; i >= 0; --i) { int end = p[i] + l[i]; while (stn > 0 && st[stn - 1].first <= p[i] + l[i]) { end = max(end, st[stn - 1].second); --stn; } st[stn++] = make_pair(p[i], end); sum[stn - 1] = 0; if (stn > 1) sum[stn - 1] += sum[stn - 2] + st[stn - 2].first - end; while (ei < m && e[ei].first.first == i) { int pos = lower_bound(st, st + stn, make_pair(p[e[ei].first.second], inf), greater<pair<int, int> >()) - st; answer[e[ei].second] = sum[stn - 1] - sum[pos]; ei++; } } for (int i = 0; i < m; ++i) printf( %d n , answer[i]); return 0; } |
#include <bits/stdc++.h> using namespace std; inline int cmp(double x, double y = 0, double tol = 1e-9) { return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; } int main() { int n; double a, d, t0, vm, t_vm, def, last = 0; scanf( %d %lf %lf , &n, &a, &d); def = sqrt(2 * d / a); for (int i = 0; i < n; ++i) { scanf( %lf %lf , &t0, &vm); t_vm = vm / a; if (t_vm < def) t_vm += (d - .5 * a * t_vm * t_vm) / vm; else t_vm = def; last = max(t_vm + t0, last); printf( %.10lf n , last); } return 0; } |
#include <bits/stdc++.h> using namespace std; double pa[2020], pb[2020], pab[2020]; int n, a, b; double dp[2020]; int opt[2020]; pair<int, int> solve(double mida, double midb) { dp[0] = 0; opt[0] = 0; for (int i = 1; i <= n; i++) { double& d = dp[i]; int& o = opt[i]; d = dp[i - 1]; o = 0; if (d < dp[i - 1] + pa[i] - mida) { d = dp[i - 1] + pa[i] - mida; o = 1; } if (d < dp[i - 1] + pb[i] - midb) { d = dp[i - 1] + pb[i] - midb; o = 2; } if (d < dp[i - 1] + pab[i] - mida - midb) { d = dp[i - 1] + pab[i] - mida - midb; o = 3; } } pair<int, int> ans = make_pair(0, 0); for (int i = 1; i <= n; i++) { if (opt[i] & 1) { ans.first++; } if (opt[i] > 1) { ans.second++; } } return ans; } int main() { scanf( %d %d %d , &n, &a, &b); for (int i = 1; i <= n; i++) { scanf( %lf , &pa[i]); } for (int i = 1; i <= n; i++) { scanf( %lf , &pb[i]); } for (int i = 1; i <= n; i++) { pab[i] = pa[i] + pb[i] - pa[i] * pb[i]; } double lowa = 0, higha = 1; double lowb = 0, highb = 1; for (int it = 0; it < 100; it++) { double mida = (lowa + higha) / 2; lowb = 0; highb = 1; for (int itb = 0; itb < 100; itb++) { double midb = (lowb + highb) / 2; auto ans = solve(mida, midb); if (ans.second > b) { lowb = midb; } else { highb = midb; } } auto ans = solve(mida, highb); if (ans.first > a) { lowa = mida; } else { higha = mida; } } auto ret = solve(higha, highb); printf( %.10lf n , dp[n] + lowa * a + lowb * b); return 0; } |
#include <bits/stdc++.h> using namespace std; char IO; inline long long rd() { long long res = 0, f = 1; while (IO = getchar(), IO < 48 || IO > 57) if (IO == - ) f = 0; do res = (res << 3) + (res << 1) + (IO ^ 48); while (IO = getchar(), IO >= 48 && IO <= 57); if (f) return res; return -res; } const long long P = 1e9 + 7, M = 1e5 + 5; long long fac[M], pw[M], inv[M]; char str[M]; long long Pow(long long a, long long b) { long long res = 1; for (; b; b >>= 1, a = a * a % P) if (b & 1) res = res * a % P; return res; } void init() { fac[0] = pw[0] = 1; for (long long i = 1, iend = M - 5; i <= iend; ++i) { fac[i] = fac[i - 1] * i % P; pw[i] = pw[i - 1] * 10 % P; } inv[M - 5] = Pow(fac[M - 5], P - 2); for (long long i = M - 5, iend = 1; i >= iend; --i) inv[i - 1] = inv[i] * i % P; } long long C(long long n, long long m) { if (n < 0 || m < 0 || n < m) return 0; return fac[n] * inv[m] % P * inv[n - m] % P; } int main() { init(); long long n = rd(), m = rd(), s = 0, ans = 0; scanf( %s , str + 1); for (long long i = n, iend = 1; i >= iend; --i) { ans = (ans + (s + C(i - 1, m) * pw[n - i]) * (str[i] - 0 ) % P) % P; s = (s + C(i - 2, m - 1) * pw[n - i] % P); } cout << ans << endl; } |
#include <bits/stdc++.h> using namespace std; const int N = 1000005; int n, q[N], top1, top2; struct ky { int x, y; long long w, val; bool operator<(const ky& p) const { return x < p.x; } } a[N]; long long f[N], ans; inline double slope(const int x, const int y) { return a[x].x == a[y].x ? 1e18 : (double)(1.0 * (f[x] - f[y])) / (a[x].x - a[y].x); } inline long long cal(const int x, const int y) { return f[y] - (0ll + a[y].x) * a[x].y + a[x].val; } template <class I> inline void ckMax(I& p, I q) { p = (p > q ? p : q); } int main() { scanf( %d , &n); register int i; for (i = 1; i <= n; ++i) scanf( %d%d%lld , &a[i].x, &a[i].y, &a[i].w), a[i].val = (0ll + a[i].x) * a[i].y - a[i].w; std::sort(a + 1, a + 1 + n), f[1] = a[1].val, q[top1 = top2 = 1] = 0; for (i = 1; i <= n; ++i) { while (top1 < top2 && slope(q[top1], q[top1 + 1]) >= a[i].y) ++top1; f[i] = cal(i, q[top1]), ckMax(ans, f[i]); while (top1 < top2 && slope(q[top2 - 1], q[top2]) < slope(q[top2], i)) --top2; q[++top2] = i; } printf( %lld , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; const int lim = 10; char ch[11]; int i, j, k, n, m, a[21], t; int main() { scanf( %d , &n); memset(a, 0, sizeof(a)); for (i = 1; i <= n; i++) { scanf( %s%d , ch, &k); if (ch[0] == & ) { for (j = 0; j < lim; j++) { t = ((k >> j) & 1); if (!t) a[j] = 2; } } if (ch[0] == | ) { for (j = 0; j < lim; j++) { t = ((k >> j) & 1); if (t) a[j] = 3; } } if (ch[0] == ^ ) { for (j = 0; j < lim; j++) { t = ((k >> j) & 1); if (t) a[j] ^= 1; } } } puts( 3 ); for (k = i = 0; i < lim; i++) k += ((a[i] & 1) << i); printf( ^ %d n , k); for (k = i = 0; i < lim; i++) if (a[i] != 2) k += (1 << i); printf( & %d n , k); for (k = i = 0; i < lim; i++) if (a[i] == 3) k += (1 << i); printf( | %d n , k); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); int test; for (cin >> test; test--;) { int m, n; cin >> m >> n; vector<vector<int>> s(m + 5, vector<int>(n + 5)); for (int i = 1; i <= m; ++i) { for (int j = 1; j <= n; ++j) { char x; cin >> x; s[i][j] = x - 0 ; } } vector<int> ans; auto f = [&](int i, int j) { s[i][j] ^= 1; ans.push_back(i); ans.push_back(j); }; auto fp = [&](pair<int, int> a) { s[a.first][a.second] ^= 1; ans.push_back(a.first); ans.push_back(a.second); }; if (m > 2) { for (int i = 1; i <= m - 2; ++i) { for (int j = 1; j < n; ++j) { if (s[i][j] == 0) continue; if (s[i][j + 1] == 1) { if (s[i + 1][j] == 1) { f(i, j); f(i + 1, j); f(i, j + 1); } else { f(i, j); f(i, j + 1); f(i + 1, j + 1); } } else { f(i, j); f(i + 1, j + 1); f(i + 1, j); } } if (s[i][n] == 1) { f(i, n); f(i + 1, n); f(i + 1, n - 1); } } for (int j = 1, i = m - 1; j < n; ++j) { if (s[i + 1][j] == 0 && s[i][j] == 0 && (!(i == m - 1 && j == n - 1))) continue; int sum = s[i][j] + s[i][j + 1] + s[i + 1][j] + s[i + 1][j + 1]; if (sum == 4) { f(i + 1, j); f(i + 1, j + 1); f(i, j + 1); f(i, j); f(i + 1, j); f(i, j + 1); f(i, j); f(i, j + 1); f(i + 1, j + 1); f(i, j); f(i + 1, j); f(i + 1, j + 1); } vector<pair<int, int>> e = { {i, j}, {i + 1, j}, {i, j + 1}, {i + 1, j + 1}}; sort(e.begin(), e.end(), [&](pair<int, int> x, pair<int, int> y) { return s[x.first][x.second] < s[y.first][y.second]; }); if (sum == 2) { if (sum == 2) { fp(e[0]); fp(e[1]); fp(e[2]); fp(e[0]); fp(e[1]); fp(e[3]); } continue; } if (sum == 3) { if (s[i][j] == 1) f(i, j); if (s[i + 1][j] == 1) f(i + 1, j); if (s[i][j + 1] == 1) f(i, j + 1); if (s[i + 1][j + 1] == 1) f(i + 1, j + 1); continue; } if (sum == 1) { fp(e[1]); fp(e[2]); fp(e[3]); fp(e[0]); fp(e[1]); fp(e[3]); fp(e[0]); fp(e[2]); fp(e[3]); } } } else { for (int j = 1; j <= n - 2; ++j) { for (int i = 1; i < m; ++i) { if (s[i][j] == 0) continue; if (s[i + 1][j] == 1) { if (s[i][j + 1] == 1) { f(i, j); f(i + 1, j); f(i, j + 1); } else { f(i, j); f(i + 1, j); f(i + 1, j + 1); } } else { f(i, j); f(i, j + 1); f(i + 1, j + 1); } } if (s[m][j] == 1) { f(m, j); f(m - 1, j + 1); f(m, j + 1); } } for (int i = 1, j = n - 1; i < m; ++i) { if (s[i][j] == 0 && s[i][j + 1] == 0 && (!(i == m - 1 && j == n - 1))) continue; int sum = s[i][j] + s[i][j + 1] + s[i + 1][j] + s[i + 1][j + 1]; if (sum == 4) { f(i + 1, j); f(i + 1, j + 1); f(i, j + 1); f(i, j); f(i + 1, j); f(i, j + 1); f(i, j); f(i, j + 1); f(i + 1, j + 1); f(i, j); f(i + 1, j); f(i + 1, j + 1); } vector<pair<int, int>> e = { {i, j}, {i + 1, j}, {i, j + 1}, {i + 1, j + 1}}; sort(e.begin(), e.end(), [&](pair<int, int> x, pair<int, int> y) { return s[x.first][x.second] < s[y.first][y.second]; }); if (sum == 2) { if (sum == 2) { fp(e[0]); fp(e[1]); fp(e[2]); fp(e[0]); fp(e[1]); fp(e[3]); } continue; } if (sum == 3) { if (s[i][j] == 1) f(i, j); if (s[i + 1][j] == 1) f(i + 1, j); if (s[i][j + 1] == 1) f(i, j + 1); if (s[i + 1][j + 1] == 1) f(i + 1, j + 1); continue; } if (sum == 1) { fp(e[1]); fp(e[2]); fp(e[3]); fp(e[0]); fp(e[1]); fp(e[3]); fp(e[0]); fp(e[2]); fp(e[3]); } } } cout << ans.size() / 6 << n ; for (int i = 0; i < ans.size(); i += 6) { cout << ans[i] << << ans[i + 1] << << ans[i + 2] << << ans[i + 3] << << ans[i + 4] << << ans[i + 5] << n ; } } } |
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &x) { x = 0; int fl = 1; char ch = getchar(); while (ch < 0 || ch > 9 ) { if (ch == - ) fl = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) { x = (x << 1) + (x << 3) + ch - 0 ; ch = getchar(); } x *= fl; } template <typename T, typename... Args> inline void read(T &t, Args &...args) { read(t); read(args...); } const int N = 200005; int T, n, m; vector<int> edge[N]; int main() { for (read(T); T; T--) { read(n, m); for (int i = 1; i <= n; i++) edge[i].clear(); vector<int> deg(n + 1), level(n + 1, 1); for (int i = 1; i <= m; i++) { int u, v; read(u, v); edge[u].emplace_back(v); deg[v]++; } vector<int> ans; for (int i = 1; i <= n; i++) { if (level[i] == 3) { ans.emplace_back(i); continue; } for (auto &to : edge[i]) level[to] = max(level[to], level[i] + 1); } printf( %d n , (int)ans.size()); for (auto &k : ans) printf( %d , k); puts( ); } return 0; } |
#include <bits/stdc++.h> using namespace std; const long double pi = 3.1415926535897932384626433832795l; template <typename T> inline auto sqr(T x) -> decltype(x * x) { return x * x; } template <typename T1, typename T2> inline bool umx(T1& a, T2 b) { if (a < b) { a = b; return 1; } return 0; } template <typename T1, typename T2> inline bool umn(T1& a, T2 b) { if (b < a) { a = b; return 1; } return 0; } const int N = 30000; const int M = 30000; struct Input { int mod; int n, m; int a[N]; int l[M], r[M]; bool read() { if (!(cin >> n >> mod)) { return 0; } for (int i = int(0); i < int(n); ++i) { scanf( %d , &a[i]); } cin >> m; for (int i = int(0); i < int(m); ++i) { scanf( %d%d , &l[i], &r[i]); --l[i]; } return 1; } void init(const Input& input) { *this = input; } }; struct Data : Input { int ans[M]; void write() { for (int i = int(0); i < int(m); ++i) { cout << ans[i] << endl; } } virtual void solve() {} virtual void clear() { *this = Data(); } }; struct Solution : Data { static const int K = 12; static const int D = N / K + 1; int f[N + 10]; int p[N], rev[N]; int cnt[M]; int hv[M]; int msk[N + 1]; int mb[1 << K], bt[2][1 << K], v[2][1 << K][2]; void solve() { f[0] = 1; f[1] = 0; for (int i = int(0); i < int(N + 8); ++i) { f[i + 2] = (f[i] + f[i + 1]) % mod; } for (int i = int(0); i < int(n); ++i) { p[i] = i; } sort(p, p + n, [&](int i, int j) { return a[i] < a[j]; }); ; for (int i = int(0); i < int(n); ++i) { rev[p[i]] = i; } for (int i = int(0); i < int(1 << K); ++i) { mb[i] = K; while (mb[i] >= 0 && !(i & (1 << mb[i]))) { --mb[i]; } } memset(ans, 0, sizeof ans); memset(cnt, 0, sizeof cnt); memset(hv, -1, sizeof hv); for (int t = int(0); t < int(D); ++t) { if (t * K >= n) { break; }; msk[0] = 0; for (int i = int(0); i < int(n); ++i) { msk[i + 1] = msk[i]; if (rev[i] >= t * K && rev[i] < (t + 1) * K) { msk[i + 1] |= 1 << (rev[i] - t * K); } }; memset(v, 0, sizeof v); memset(bt, 0, sizeof bt); for (int w = int(0); w < int(2); ++w) { for (int i = int(0); i < int(min(K, n - t * K)); ++i) { for (int q = int(0); q < int(1 << i); ++q) { if (i ? (a[p[t * K + i]] == a[p[t * K + i - 1]]) : w) { if (i) { v[w][q | (1 << i)][0] = v[w][q | (1 << (i - 1))][0]; v[w][q | (1 << i)][1] = v[w][q | (1 << (i - 1))][1]; bt[w][q | (1 << i)] = bt[w][q | (1 << (i - 1))]; } else { v[w][q | (1 << i)][0] = v[w][q][0]; v[w][q | (1 << i)][1] = v[w][q][1]; bt[w][q | (1 << i)] = bt[w][q]; } } else { v[w][q | (1 << i)][0] = (v[w][q][0] + f[bt[w][q]] * (a[p[t * K + i]] % mod)) % mod; v[w][q | (1 << i)][1] = (v[w][q][1] + f[bt[w][q] + 1] * (a[p[t * K + i]] % mod)) % mod; bt[w][q | (1 << i)] = bt[w][q] + 1; } } } } for (int i = int(0); i < int(m); ++i) { int ttt = (hv[i] == -1) ? 0 : (a[p[hv[i]]] == a[p[t * K]]); int mmm = msk[r[i]] & ~msk[l[i]]; ans[i] = (ans[i] + v[ttt][mmm][0] * f[cnt[i] + 2] + v[ttt][mmm][1] * f[cnt[i] + 3]) % mod; ; ; ; cnt[i] += bt[ttt][mmm]; if (mb[mmm] != -1) { hv[i] = t * K + mb[mmm]; } } } } void clear() { *this = Solution(); } }; Solution sol; int main() { ; cout.setf(ios::showpoint | ios::fixed); cout.precision(20); sol.read(); sol.solve(); sol.write(); return 0; } |
#include <bits/stdc++.h> using namespace std; long long sum_prefix(vector<long long> &t, int len) { long long res = 0; int i = len; while (i >= 0) { res += t[i]; i &= (i + 1); i--; } return res; } long long sum_interval(vector<long long> &t, int l, int r) { return sum_prefix(t, r) - sum_prefix(t, l - 1); } void plus_value(vector<long long> &t, int num, long long val) { int N = t.size(); int i = num; while (i < N) { t[i] += val; i |= (i + 1); } } void init(vector<int> &a, vector<long long> &t) { int i; int N = a.size(); t.assign(N, 0); for (i = 0; i < N; i++) plus_value(t, i, a[i]); } const int MAXN = 200005; const int MAXQ = 200005; vector<int> count_i(MAXN, 0); vector<int> sum_i(MAXN, 0); vector<long long> coord; int s; struct active { int type; long long a1; long long a2; }; int get_index(long long a) { return lower_bound(coord.begin(), coord.end(), a) - coord.begin(); } int main() { int i; set<long long>::iterator J; int n, q; cin >> n >> q; vector<active> a(q); vector<long long> now_v(n); set<long long> coord_set; for (i = 0; i < n; i++) { cin >> now_v[i]; coord_set.insert(now_v[i]); } for (i = 0; i < q; i++) { cin >> a[i].type; if (a[i].type == 1) { cin >> a[i].a1 >> a[i].a2; a[i].a1--; coord_set.insert(a[i].a2); } else cin >> a[i].a1; } for (J = coord_set.begin(); J != coord_set.end(); ++J) coord.push_back(*J); s = coord.size(); for (i = 0; i < n; i++) { count_i[get_index(now_v[i])]++; sum_i[get_index(now_v[i])] += now_v[i]; } vector<long long> count_i_t, sum_i_t; init(count_i, count_i_t); init(sum_i, sum_i_t); int num; long long val; int index_now, index_val; for (i = 0; i < q; i++) { if (a[i].type == 1) { num = a[i].a1; val = a[i].a2; index_now = get_index(now_v[num]); index_val = get_index(val); plus_value(count_i_t, index_now, -1); count_i[index_now]--; plus_value(sum_i_t, index_now, -now_v[num]); sum_i[index_now] -= now_v[num]; plus_value(count_i_t, index_val, 1); count_i[index_val]++; plus_value(sum_i_t, index_val, val); sum_i[index_val] += val; now_v[num] = val; } else { val = a[i].a1; int left = 0, right = s - 1; double ans = -1; while (right > left) { int mid = (left + right) / 2; int now_count = sum_prefix(count_i_t, mid); if (now_count == 0) { left = mid + 1; continue; } long long now_sum = sum_prefix(sum_i_t, mid); long long rest = val - (now_count * coord[mid] - now_sum); if (rest < now_count * (coord[mid + 1] - coord[mid])) right = mid; else left = mid + 1; } int now_count = sum_prefix(count_i_t, left); long long now_sum = sum_prefix(sum_i_t, left); long long rest = val - (now_count * coord[left] - now_sum); ans = coord[left] + rest * 1.0 / now_count; printf( %.6f n , ans); } } } |
#include <bits/stdc++.h> template <typename T> void scan(T &x) { x = 0; bool _ = 0; T c = getchar(); _ = c == 45; c = _ ? getchar() : c; while (c < 48 || c > 57) c = getchar(); for (; c < 48 || c > 57; c = getchar()) ; for (; c > 47 && c < 58; c = getchar()) x = (x << 3) + (x << 1) + (c & 15); x = _ ? -x : x; } template <typename T> void printn(T n) { bool _ = 0; _ = n < 0; n = _ ? -n : n; char snum[65]; int i = 0; do { snum[i++] = n % 10 + 48; n /= 10; } while (n); --i; if (_) putchar(45); while (i >= 0) putchar(snum[i--]); } template <typename First, typename... Ints> void scan(First &arg, Ints &...rest) { scan(arg); scan(rest...); } template <typename T> void print(T n) { printn(n); putchar(10); } template <typename First, typename... Ints> void print(First arg, Ints... rest) { printn(arg); putchar(32); print(rest...); } using namespace std; using ll = long long; const int MM = 505; int n; ll a[MM], ans; int main() { scan(n); for (int i = 0; i < n; i++) { scan(a[i]); } for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { for (int k = 0; k <= j; k++) { ans = max(ans, a[i] | a[j] | a[k]); } } } print(ans); } |
#include <bits/stdc++.h> using namespace std; template <typename T> void tk_input(vector<T> &arr) { for (T &i : arr) cin >> i; } void solve() { int n, k; cin >> n >> k; string s; cin >> s; priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq; int sc = 0, ls = 0, ls_i = 0, f_i = n, l_i = -1; for (int i = 0; i <= n - 1; i++) { if (s[i] == W ) { if ((ls > 0) && (f_i != n)) pq.push(pair<int, int>(ls, ls_i)); f_i = min(f_i, i); l_i = max(l_i, i); ls_i = i + 1; ls = 0; } else { ls++; } } while ((!pq.empty()) && (k > 0)) { auto tp = pq.top(); pq.pop(); for (int i = tp.second; i <= tp.first + tp.second - 1; i++) { if (k == 0) break; s[i] = W ; k--; } } for (int i = f_i - 1; i >= 0; i--) { if (k == 0) break; s[i] = W ; k--; } if (l_i != -1) { for (int i = l_i + 1; i <= n - 1; i++) { if (k == 0) break; s[i] = W ; k--; } } sc += (s[0] == W ); for (int i = 1; i <= n - 1; i++) { sc += ((s[i] == W ) * (1 + (s[i - 1] == W ))); } cout << sc << n ; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int _t; cin >> _t; while (_t--) solve(); } |
#include <bits/stdc++.h> using namespace std; const int N = 100010; set<pair<int, int> > s, e; long long dp[N]; int l[N], r[N]; struct Point { int x, h, k; bool f; } p[2 * N]; struct temp { int h, k; } q[N]; int m; bool cmp1(const temp &a, const temp &b) { return a.h > b.h; } bool cmp(const Point &a, const Point &b) { if (a.x != b.x) return a.x < b.x; if (a.f != b.f) return a.f; return a.k < b.k; } vector<int> tran[N]; int main() { set<pair<int, int> >::iterator it; int n, t; scanf( %d%d , &n, &t); for (int i = 0; i < n; i++) { int h; scanf( %d%d%d , &h, &l[i + 1], &r[i + 1]); p[m].x = l[i + 1], p[m].h = h, p[m].k = i + 1, p[m++].f = false; p[m].x = r[i + 1], p[m].h = h, p[m].k = i + 1, p[m++].f = true; q[i].h = h, q[i].k = i + 1; } l[0] = -(1 << 30), r[0] = (1 << 30); l[n + 1] = -(1 << 30), r[n + 1] = (1 << 30); q[n].h = t, q[n].k = 0; q[n + 1].h = 0, q[n + 1].k = n + 1; sort(p, p + m, cmp); sort(q, q + n + 2, cmp1); s.insert(make_pair(t, 0)); s.insert(make_pair(0, n + 1)); for (int i = 0; i < m; i++) { if (p[i].f) { it = s.find(make_pair(p[i].h, p[i].k)); if (it != s.end()) s.erase(it); } else { int a, b; it = s.lower_bound(make_pair(p[i].h, 0)); a = it->second; --it; b = it->second; it = e.find(make_pair(a, b)); if (it != e.end()) e.erase(it); e.insert(make_pair(a, p[i].k)); e.insert(make_pair(p[i].k, b)); s.insert(make_pair(p[i].h, p[i].k)); } } for (it = e.begin(); it != e.end(); it++) tran[it->first].push_back(it->second); dp[0] = (1ll << 50); for (int i = 0; i < n + 2; i++) { int now = q[i].k; for (int j = 0; j < tran[now].size(); j++) { int tag = tran[now][j]; dp[tag] = max( dp[tag], min(dp[now], min((long long)r[now], (long long)r[tag]) - max((long long)l[now], (long long)l[tag]))); } } printf( %I64d n , dp[n + 1]); return 0; } |
#include <bits/stdc++.h> using namespace std; template <typename TH> void _dbg(const char* sdbg, TH h) { cerr << sdbg << = << h << n ; } template <typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) { while (*sdbg != , ) { cerr << *sdbg++; } cerr << = << h << , ; _dbg(sdbg + 1, t...); } template <class C> void mini(C& a4, C b4) { a4 = min(a4, b4); } template <class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); } template <class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> pair) { return out << ( << pair.first << , << pair.second << ) ; } template <class A, class B, class C> struct Triple { A first; B second; C third; bool operator<(const Triple& t) const { if (first != t.first) return first < t.first; if (second != t.second) return second < t.second; return third < t.third; } }; template <class T> void ResizeVec(T&, vector<long long>) {} template <class T> void ResizeVec(vector<T>& vec, vector<long long> sz) { vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; } for (T& v : vec) { ResizeVec(v, sz); } } template <class A, class B, class C> ostream& operator<<(ostream& out, Triple<A, B, C> t) { return out << ( << t.first << , << t.second << , << t.third << ) ; } template <class T> ostream& operator<<(ostream& out, vector<T> vec) { out << ( ; for (auto& v : vec) out << v << , ; return out << ) ; } template <class T> ostream& operator<<(ostream& out, set<T> vec) { out << ( ; for (auto& v : vec) out << v << , ; return out << ) ; } template <class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out << ( ; for (auto& v : vec) out << v << , ; return out << ) ; } int32_t main() { ios_base::sync_with_stdio(0); cout << fixed << setprecision(10); if (0) cout << fixed << setprecision(10); cin.tie(0); long long t; cin >> t; for (long long ii = (1); ii <= (t); ++ii) { long long n; cin >> n; vector<long long> v; long long sum = 0; long long ma = 0; for (long long i = (1); i <= (n); ++i) { long long a; cin >> a; v.push_back(a); sum += a; maxi(ma, a); } if (n == 1) { cout << T n ; continue; } if (2 * ma > sum) { cout << T n ; } else { cout << vector<string>{ HL , T }[sum % 2] << endl; } } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long n, i, j, k; cin >> n; long long a[n]; for (i = 0; i < n; i++) { cin >> a[i]; } long long ans = 10000000000; for (i = 1; i < n - 1; i++) { long long b[n]; for (k = 0; k < n; k++) { b[k] = a[k]; } long long ans1 = 0; for (j = 0; j < n - 1; j++) { if (j != i - 1) ans1 = max(ans1, b[j + 1] - b[j]); else ans1 = max(ans1, b[i + 1] - b[i - 1]); } ans = min(ans, ans1); } cout << ans << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { long long t, n, i, m; cin >> t; while (t--) { cin >> n; string s; cin >> s; if (n % 2 == 0) { int f = 0; for (i = 0; i < n; i++) { long long a1 = int(s[i] - 0 ); if ((i + 1) % 2 == 0 && a1 % 2 == 0) { f = 1; } } if (f == 1) { cout << 2 ; } else { cout << 1 ; } } else if (n % 2 != 0) { int f = 0; for (i = 0; i < n; i++) { long long a1 = int(s[i] - 0 ); if ((i + 1) % 2 != 0 && a1 % 2 != 0) { f = 1; } } if (f == 1) { cout << 1 ; } else { cout << 2 ; } } cout << n ; } } |
#include <bits/stdc++.h> using namespace std; int main() { int n, h, value, width = 0; vector<int> v; cin >> n; cin >> h; for (int i = 0; i < n; i++) { cin >> value; v.push_back(value); } for (int j = 0; j < v.size(); j++) { if (v[j] > h) width = width + 2; else width = width + 1; } cout << width; return 0; } |
#include <bits/stdc++.h> using namespace std; long long f[1001][1001], s[1001][1001]; long long a[10001], ans[10001]; int main() { int n, m, k; cin >> n >> k; for (int i = 1; i <= n; i++) { scanf( %lld , &a[i]); } sort(a + 1, a + n + 1); a[0] = -10080124100; long long sum = 0; for (int t = 1; t * (k - 1) <= a[n]; t++) { s[0][0] = f[0][0] = 1; int l = 0; for (int i = 1; i <= n; i++) { while (a[l] <= a[i] - t) l++; for (int j = 0; j <= k; j++) { if (j != 0) f[i][j] = s[l - 1][j - 1] % 998244353; s[i][j] = (s[i - 1][j] + f[i][j]) % 998244353; } sum = (sum + f[i][k]) % 998244353; } } cout << sum << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; void solve() { long long n; cin >> n; if (n == 0 || n == 1) { cout << 0 << endl; return; } long long a[n]; vector<int> pos; long long even = n / 2, odds = n - even; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] != 0) { if (a[i] & 1) odds--; else even--; pos.push_back(i); } } if (pos.size() == 0) { cout << 1 << endl; return; } vector<pair<int, int>> good; int bad = 0; for (int i = 1; i < pos.size(); ++i) { if (pos[i] - pos[i - 1] != 1) { if (a[pos[i]] % 2 == a[pos[i - 1]] % 2) { good.push_back({pos[i], pos[i - 1]}); } } if (a[pos[i]] % 2 != a[pos[i - 1]] % 2) bad++; } sort(good.begin(), good.end(), [&](pair<int, int> a, pair<int, int> b) { return a.first - a.second < b.first - b.second; }); long long ans = bad; for (auto i : good) { int k = i.first - i.second - 1; if (a[i.first] & 1 && odds >= k) odds -= k; else if (!(a[i.first] & 1) && even >= k) even -= k; else ans += 2; } if (a[0] == 0) { int cnt = 0; while (a[cnt] == 0) cnt++; if (a[cnt] & 1 && odds >= cnt) odds -= cnt; else if (!(a[cnt] & 1) && even >= cnt) even -= cnt; else ans++; } if (a[n - 1] == 0) { int cnt = 0, i = n - 1; while (a[i] == 0) cnt++, --i; if (!(a[i] & 1 && odds >= cnt || !(a[i] & 1) && even >= cnt)) ans++; } cout << ans << endl; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); solve(); } |
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; const int maxm = 1e6 + 10; const int mod = 1e9 + 7; queue<string> que; string str; set<string> se; int ans, n, k; void bfs() { if (k - 1 == 0) return; que.push(str); while (que.size()) { string now = que.front(); que.pop(); for (int i = 0; i < now.size(); i++) { string zz = ; for (int j = 0; j < now.size(); j++) if (j != i) zz += now[j]; if (se.find(zz) == se.end()) { se.insert(zz); que.push(zz); ans += (n - zz.size()); if (se.size() == k - 1) return; } } } } int main() { scanf( %d %d , &n, &k); cin >> str; bfs(); if (se.size() == k - 1) printf( %d n , ans); else printf( -1 n ); return 0; } |
#include <bits/stdc++.h> using namespace std; const long long N = 1000005; const long long M = 1000000007; long long n, m, cc, len; long long s[5]; int main() { long long sum = 0; for (long long i = 0; i < 3; i++) scanf( %I64d , &s[i]), sum += s[i]; sort(s, s + 3); long long ans = -1; for (long long i = 0; i < 3; i++) for (long long j = i + 1; j < 3; j++) { long long ano = sum - s[i] - s[j]; if ((s[i] + s[j]) % 2 == 0 && ano >= (abs(s[j] - s[i]) / 2)) { if (ans == -1) ans = s[j]; ans = min(ans, s[j]); } } if (ans == -1) ans = s[2]; printf( %I64d n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long INF = LLONG_MAX; const long long MINF = LLONG_MIN; const int N = 2e5 + 5; template <typename T> inline T abs(T x) { return x < 0 ? -x : x; } inline long long pow_mod(long long x, long long n) { long long r = 1; while (n) { if (n % 2) r = (r * x) % MOD; x = (x * x) % MOD; n /= 2; } return r; } inline long long pow_(long long x, long long n) { long long r = 1; while (n) { if (n % 2) r = (r * x); x = (x * x); n /= 2; } return r; } void solve() { int n, k, d; cin >> n >> k >> d; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; map<int, int> m; for (int i = 0; i < d; i++) m[a[i]]++; int ans = (int)m.size(); for (int i = d; i < n; i++) { m[a[i - d]]--; if (!m[a[i - d]]) m.erase(a[i - d]); m[a[i]]++; ans = min(ans, (int)m.size()); } cout << ans << n ; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1; cin >> t; while (t--) solve(); } |
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9, MOD = 1e9 + 7; const int n_ = 1e5 + 1000; long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); } long long power(long long a, long long n) { long long p = 1; while (n > 0) { if (n % 2) { p = p * a; } n >>= 1; a *= a; } return p; } long long power(long long a, long long n, long long mod) { long long p = 1; while (n > 0) { if (n % 2) { p = p * a; p %= mod; } n >>= 1; a *= a; a %= mod; } return p % mod; } int main() { ios_base::sync_with_stdio(false); long long n, x, y, a; scanf( %lld %lld %lld , &n, &x, &y); a = y - n + 1; if (a > 0 && a * a + n - 1 >= x) { for (int(i) = 0; (i) < (n - 1); (i)++) puts( 1 ); printf( %lld n , a); } else { puts( -1 ); } return 0; } |
#include <bits/stdc++.h> using namespace std; string nsp(string s) { string t = ; for (int i = 0; i < s.size(); i++) if (s[i] != ) t = t + s[i]; return t; } string gfbu(string s, char c, int a) { string t = ; for (int i = a; i < s.size() - 1; i++) { if (s[i] == c) return t; t += s[i]; } return t; } int in(string s) { return (s == double ? 2 : (s == int ? 3 : (s == T ? 1 : 5))); } vector<int> ext(string s) { int cnt = gfbu(s, ( , 0).size() + 1; vector<int> v; while (cnt < s.size()) v.push_back(in(gfbu(s, , , cnt))), cnt += gfbu(s, , , cnt).size() + 1; return v; } int main() { int n, m, k; map<string, int> ints; map<string, vector<vector<int> > > voids; cin >> n; for (int i = 0; i < n; i++) { string trash, s, vo; if (i == 0) cin.ignore(); getline(cin, s), s = nsp(s), s.erase(0, 4); vo = gfbu(s, ( , 0), voids[vo].push_back(ext(s)); } cin >> m; for (int i = 0; i < m; i++) { string s, t; cin >> s >> t, ints[t] = in(s); } cin >> k; for (int i = 0; i < k; i++) { if (i == 0) cin.ignore(); string s, vo; getline(cin, s), s = nsp(s), vo = gfbu(s, ( , 0); int ans = 0, cnt = gfbu(s, ( , 0).size() + 1; vector<int> vi; while (cnt < s.size()) vi.push_back(ints[gfbu(s, , , cnt)]), cnt += gfbu(s, , , cnt).size() + 1; if (voids[vo].size() == 0) { cout << 0 n ; continue; } for (int j = 0; j < voids[vo].size(); j++) { if (voids[vo][j].size() != vi.size()) continue; for (int k = 0; k < vi.size(); k++) if (vi[k] % voids[vo][j][k]) { ans--; break; } ans++; } cout << ans << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n, k, i; cin >> n >> k; string s[3]; cin >> s[0] >> s[1] >> s[2]; bool a[3][105]; while (s[0].size() <= n + 2) s[0] += . ; while (s[1].size() <= n + 2) s[1] += . ; while (s[2].size() <= n + 2) s[2] += . ; memset(a, 0, sizeof(a)); if (s[0][0] == s ) a[0][0] = true; else if (s[1][0] == s ) a[1][0] = true; else a[2][0] = true; for (i = 0; i < n; i++) { if (a[0][i]) { if (s[0][i + 1] == . ) { if (s[0][i + 3] == . && s[0][i + 2] == . ) a[0][i + 3] = true; if (s[1][i + 3] == . && s[1][i + 2] == . && s[1][i + 1] == . ) a[1][i + 3] = true; } } if (a[1][i]) { if (s[1][i + 1] == . ) { if (s[1][i + 3] == . && s[1][i + 2] == . ) a[1][i + 3] = true; if (s[0][i + 3] == . && s[0][i + 1] == . && s[0][i + 2] == . ) a[0][i + 3] = true; if (s[2][i + 3] == . && s[2][i + 1] == . && s[2][i + 2] == . ) a[2][i + 3] = true; } } if (a[2][i]) { if (s[2][i + 1] == . ) { if (s[2][i + 3] == . && s[2][i + 2] == . ) a[2][i + 3] = true; if (s[1][i + 3] == . && s[1][i + 2] == . && s[1][i + 1] == . ) a[1][i + 3] = true; } } } bool flag = false; if (a[0][n] || a[1][n] || a[2][n] || flag) flag = true; if (a[0][n + 1] || a[1][n + 1] || a[2][n + 1] || flag) flag = true; if (a[0][n - 1] || a[1][n - 1] || a[2][n - 1] || flag) cout << YES n ; else cout << NO n ; } } |
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 1000; const long long INF64 = 1e18; const int N = 1001000; const int MOD = 1e9 + 7; const double EPS = 1E-9; long long gcd(long long a, long long b) { return a == 0 ? b : gcd(b % a, a); } int add(int first, int second, int m) { first += second; while (first >= m) first -= m; while (first < 0) first += m; return first; } int sub(int first, int second, int m) { return add(first, -second, m); } int mul(int first, int second, int m) { return (first * 1ll * second) % m; } int binpow(int first, int second, int m) { int z = 1; while (second) { if (second & 1) z = mul(z, first, m); first = mul(first, first, m); second >>= 1; } return z; } int divide(int first, int second, int m) { return mul(first, binpow(second, m - 2, m), m); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int first, t, a, b, da, db; cin >> first >> t >> a >> b >> da >> db; if (first == 0) { cout << YES n ; return 0; } for (int ta = 0; ta < t; ++ta) { for (int tb = 0; tb < t; ++tb) { if (a - da * ta + b - db * tb == first || a - da * ta == first || b - db * tb == first) { cout << YES n ; return 0; } } } cout << NO n ; } |
#include <bits/stdc++.h> const long long int MOD = 1e9 + 7; using namespace std; constexpr int mul(int a, int b) { return 1LL * a * b % MOD; } template <typename... T> constexpr int mul(int a, int b, T... t) { return mul(mul(a, b), t...); } template <class T> inline T Min(const T &a, const T &b) { return a < b ? a : b; } template <class T> inline T Max(const T &a, const T &b) { return a < b ? b : a; } inline void inc(int &x, int &v, int &mod) { x += v; if (x >= mod) x -= mod; } inline void dec(int &x, int &v, int &mod) { x -= v; if (x < 0) x += mod; } inline int read() { char ch = getchar(); int x = 0, f = 1; while (ch < 0 || ch > 9 ) { if (ch == - ) f = -1; ch = getchar(); } while ( 0 <= ch && ch <= 9 ) { x = x * 10 + ch - 0 ; ch = getchar(); } return x * f; } inline long long int readl() { char ch = getchar(); long long int x = 0, f = 1; while (ch < 0 || ch > 9 ) { if (ch == - ) f = -1; ch = getchar(); } while ( 0 <= ch && ch <= 9 ) { x = x * 10 + ch - 0 ; ch = getchar(); } return x * f; } int T[200010 << 2][9], MIN[200010 << 2][9], minsum[200010 << 2][9]; int a[200010]; int res1, res2; void pushup(int now, int k) { T[now][k] = T[now << 1][k] + T[now << 1 | 1][k]; MIN[now][k] = Min(MIN[now << 1][k], MIN[now << 1 | 1][k]); minsum[now][k] = Min(minsum[now << 1][k], minsum[now << 1 | 1][k]); if (T[now << 1][k] != 0 && T[now << 1 | 1][k] != 0) minsum[now][k] = Min(minsum[now][k], MIN[now << 1][k] + MIN[now << 1 | 1][k]); } void build(int l, int r, int now, int k) { if (l == r) { int tmp = a[l]; for (int i = 0; i < k; i++) { tmp /= 10; } minsum[now][k] = MIN[now][k] = 2e9; if (tmp % 10 != 0) { MIN[now][k] = a[l]; T[now][k] = 1; } else T[now][k] = 0; return; } int mid = (l + r) >> 1; build(l, mid, now << 1, k); build(mid + 1, r, now << 1 | 1, k); pushup(now, k); } int query(int L, int R, int l, int r, int now, int k) { if (L <= l && r <= R) { return T[now][k]; } int mid = (l + r) >> 1; int ans = 0; if (L <= mid) ans += query(L, R, l, mid, now << 1, k); if (mid < R) ans += query(L, R, mid + 1, r, now << 1 | 1, k); return ans; } int query2(int L, int R, int l, int r, int now, int k) { if (L <= l && r <= R) { if (MIN[now][k] < res1) { res2 = res1; res1 = MIN[now][k]; } else if (MIN[now][k] < res2) { res2 = MIN[now][k]; } return minsum[now][k]; } int mid = (l + r) >> 1; int ans1 = 2e9, ans2 = 2e9; if (L <= mid) ans1 = query2(L, R, l, mid, now << 1, k); if (mid < R) ans2 = query2(L, R, mid + 1, r, now << 1 | 1, k); return Min(ans1, ans2); } void update(int pos, int C, int l, int r, int now, int k) { if (l == r) { int tmp = C; for (int i = 0; i < k; i++) tmp /= 10; minsum[now][k] = MIN[now][k] = 2e9; if (tmp % 10 != 0) { MIN[now][k] = C; T[now][k] = 1; } else T[now][k] = 0; return; } int mid = (l + r) >> 1; if (pos <= mid) update(pos, C, l, mid, now << 1, k); else update(pos, C, mid + 1, r, now << 1 | 1, k); pushup(now, k); } int main() { int n = read(), q = read(); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 0; i < 9; i++) { build(1, n, 1, i); } while (q--) { int op = read(), a = read(), b = read(); if (op == 1) { for (int i = 0; i < 9; i++) update(a, b, 1, n, 1, i); } else { bool tag = true; int sum = 2e9; for (int i = 0; i < 9; i++) { int cnt = query(a, b, 1, n, 1, i); if (cnt > 1) { res1 = res2 = 1e9; sum = Min(Min((res1 + res2), sum), query2(a, b, 1, n, 1, i)); tag = false; } } if (tag) { printf( -1 n ); } else { printf( %d n , sum); } } } return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m; int score(pair<int, int> a) { int d = abs(a.first - a.second); return min(d, n - d); } const int N = 1e5 + 7; int nxt[N], prv[N]; vector<int> regions[N]; vector<int> adj[N]; int level[N]; int sz[N]; void dfs1(int u, int par) { assert(level[u] == 0); sz[u] = 1; for (int v : adj[u]) { if (v == par || level[v]) continue; dfs1(v, u); sz[u] += sz[v]; } } int dfs2(int u, int par, int s) { for (int v : adj[u]) { if (v == par || level[v]) continue; if (sz[v] > s / 2) return dfs2(v, u, s); } return u; } void decompose(int u, int lev) { assert(level[u] == 0); dfs1(u, -1); int v = dfs2(u, -1, sz[u]); level[v] = lev; for (int w : adj[v]) if (!level[w]) decompose(w, lev + 1); } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; vector<pair<int, int>> diags; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; diags.push_back({a, b}); } auto cmp1 = [](pair<int, int> a, pair<int, int> b) { return score(a) < score(b); }; sort(diags.begin(), diags.end(), cmp1); for (int i = 1; i <= n; i++) { nxt[i] = 1 + i % n; } int id = 0, last = 0; for (auto pr : diags) { ++id; int u = pr.first; int v = pr.second; int d = (v - u + n) % n; if (d >= n - d) swap(u, v); int cur = u; while (cur != v) { regions[id].push_back(cur); cur = nxt[cur]; } regions[id].push_back(v); nxt[u] = v; last = u; } ++id; int cur = last; while (true) { regions[id].push_back(cur); cur = nxt[cur]; if (cur == last) break; } map<pair<int, int>, vector<int>> mp; for (int i = 1; i <= id; i++) { for (int j = 0; j < regions[i].size(); j++) { int u = regions[i][j]; int v = regions[i][(j + 1) % regions[i].size()]; if (u > v) swap(u, v); mp[make_pair(u, v)].push_back(i); } } for (auto pr : mp) { assert(pr.second.size() <= 2); if (pr.second.size() != 2) continue; int u = pr.second[0]; int v = pr.second[1]; adj[u].push_back(v); adj[v].push_back(u); } for (int i = 1; i <= id; i++) { sort(regions[i].rbegin(), regions[i].rend()); } vector<int> order; for (int i = 1; i <= id; i++) { order.push_back(i); } sort(order.begin(), order.end(), [](int a, int b) { return regions[a] < regions[b]; }); decompose(1, 1); for (int x : order) cout << level[x] << ; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, x, y; cin >> n; x = 1; y = n * n; while (x < y) { cout << x << << y << endl; x++; y--; } return 0; } |
#include <bits/stdc++.h> using namespace std; vector<string> split(const string& s, char c) { vector<string> v; stringstream ss(s); string x; while (getline(ss, x, c)) { v.emplace_back(x); } return move(v); } void err(vector<string>::iterator it) {} template <typename T, typename... Args> void err(vector<string>::iterator it, T a, Args... args) { cerr << it->substr((*it)[0] == , it->length()) << = << a << , ; err(++it, args...); } vector<pair<int, int>> vt[101]; int dp[101][101][26][2]; bool rec(int u, int v, int ch, int side) { if (dp[u][v][ch][side] != -1) return dp[u][v][ch][side]; int ans = 0; if (side) { for (auto g : vt[v]) { if (g.second >= ch) ans |= !rec(u, g.first, g.second, side ^ 1); } } else { for (auto g : vt[u]) { if (g.second >= ch) ans |= !rec(g.first, v, g.second, side ^ 1); } } return dp[u][v][ch][side] = ans; } int main() { memset(dp, -1, sizeof dp); int n, m, i, j, u, v; cin >> n >> m; char c; for (i = 0; i < m; ++i) { cin >> u >> v >> c; vt[u].push_back({v, c - a }); } for (i = 1; i <= n; ++i) { for (j = 1; j <= n; ++j) { if (rec(i, j, 0, 0)) printf( A ); else printf( B ); } puts( ); } } |
#include <bits/stdc++.h> using namespace std; double pi = acos(-1); long long power(long long a, long long b) { long long p_res = 1; while (b > 0) { if (b % 2 == 1) { p_res *= a; b--; } a *= a; b /= 2; } return p_res; } int cc[300001]; int ans[300001]; set<pair<int, int> > ss; set<pair<int, int> >::iterator it; vector<int> adj[300001]; int n, q; int dfs(int here) { int res = 1; for (int i = 0; i < adj[here].size(); i++) { res += dfs(adj[here][i]); } cc[here] = res; return res; } void centroid(int here, int prev) { int mx = 0; for (int i = 0; i < adj[here].size(); i++) { int next = adj[here][i]; mx = max(mx, cc[next]); } if (2 * mx <= cc[here]) ans[here] = here; else ss.insert(make_pair(cc[here], here)); it = ss.lower_bound(make_pair(2 * mx, -1)); while (it != ss.end()) { int v = it->first; int i = it->second; if (v > 2 * cc[here]) break; if (2 * mx <= v && 2 * (v - cc[here]) <= v) { ans[i] = here; ss.erase(it++); } else it++; } for (int i = 0; i < adj[here].size(); i++) { centroid(adj[here][i], here); } } int main() { scanf( %d %d , &n, &q); for (int i = 2; i <= n; i++) { int v; scanf( %d , &v); adj[v].push_back(i); } dfs(1); centroid(1, -1); for (int i = 0; i < q; i++) { int v; scanf( %d , &v); printf( %d n , ans[v]); } return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 300100; const int K = 1001000; vector<int> v[K]; int a[N], s[N]; int l[N], r[N]; int q[N], id[N]; int main() { int n, k; scanf( %d%d , &n, &k); for (int i = 0; i < n; i++) { scanf( %d , &a[i]); } s[0] = a[0] % k; v[0].push_back(-1); v[s[0]].push_back(0); for (int i = 1; i < n; i++) { s[i] = (s[i - 1] + a[i]) % k; v[s[i]].push_back(i); } int c = 0; for (int i = 0; i < n; i++) { while (c && a[q[c - 1]] < a[i]) c--; l[i] = 0; if (c) { l[i] = q[c - 1] + 1; } q[c++] = i; } c = 0; for (int i = n - 1; i >= 0; i--) { while (c && a[q[c - 1]] <= a[i]) c--; r[i] = n - 1; if (c) { r[i] = q[c - 1] - 1; } q[c++] = i; } long long ans = 0; for (int i = 0; i < n; i++) { if (i - l[i] >= r[i] - i) { for (int j = i; j <= r[i]; j++) { int x = (s[j] - a[i]) % k; if (x < 0) { x += k; } int ll = l[i] - 1, rr = i - 1; if (j == i) rr = i - 2; ans += upper_bound(v[x].begin(), v[x].end(), rr) - lower_bound(v[x].begin(), v[x].end(), ll); } } else { for (int j = l[i]; j <= i; j++) { int x = (a[i] + s[j - 1]) % k; if (x < 0) { x += k; } int ll = i, rr = r[i]; if (j == i) ll = i + 1; ans += upper_bound(v[x].begin(), v[x].end(), rr) - lower_bound(v[x].begin(), v[x].end(), ll); } } } printf( %lld n , ans); } |
#include <bits/stdc++.h> using namespace std; inline void read(int &x) { char ch = getchar(); x = 0; while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar(); } const int N = 1e5 + 10; int n, a[N], s[N], c[N << 1]; inline void add(int x) { for (; x; x -= (x & (-x))) ++c[x]; } inline void _add(int x) { for (; x; x -= (x & (-x))) --c[x]; } inline int ask(int x) { int res = 0; for (; x <= (n << 1); x += (x & (-x))) res += c[x]; return res; } signed main() { int T; read(T); while (T--) { int k, ok = 0, kk = 0; read(n), read(k); for (int i = 1; i <= n; ++i) read(a[i]); for (int i = 1; i <= n; ++i) if (a[i] == k) { kk = 1; break; } if (n == 1) { a[1] == k ? puts( yes ) : puts( no ); continue; } if (n == 2) { if (a[1] == k && a[2] >= k || a[2] == k && a[1] >= k) puts( yes ); else puts( no ); continue; } for (int i = 3; i <= n; ++i) { int c = 0; for (int j = i - 2; j <= i; ++j) c += (a[j] >= k); if (c >= 2) { ok = 1; break; } } (ok && kk) ? puts( yes ) : puts( no ); } return 0; } |
#include <bits/stdc++.h> using namespace std; const double eps = 1.0e-5; const int maxn = 100000 + 10; const int mod = 1e9 + 7; int a, b[505], n; int main() { memset(b, 0, sizeof(b)); long long ans = 0; int tmp; scanf( %d , &n); for (int i = 1; i <= n - 1; i++) ans = (2 * ans + 1) % mod; for (int i = 1; i <= n; i++) { scanf( %d , &tmp); if (tmp >= 0) { ans -= (long long)b[tmp] + 1; b[tmp] = (2 * b[tmp] + 1) % mod; } } printf( %lld n , (ans % mod + mod) % mod); } |
#include <bits/stdc++.h> using namespace std; const long long maxn = 5e5 + 10; const long long INF = 1e18; long long n, a[maxn], po, maxx = 0, m[maxn]; int main() { std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> m[i]; if (m[i] > maxx) { maxx = m[i]; po = i; } } long long ans, maxx = 0; for (int po = 1; po <= n; po++) { long long sum = 0; a[po] = m[po]; sum += a[po]; for (int i = po - 1; i >= 1; i--) { a[i] = min(m[i], a[i + 1]); sum += a[i]; } for (int i = po + 1; i <= n; i++) { a[i] = min(m[i], a[i - 1]); sum += a[i]; } if (sum > maxx) { maxx = sum; ans = po; } } long long po = ans; a[po] = m[po]; for (int i = po - 1; i >= 1; i--) { a[i] = min(m[i], a[i + 1]); } for (int i = po + 1; i <= n; i++) { a[i] = min(m[i], a[i - 1]); } for (int i = 1; i <= n; i++) cout << a[i] << ; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d; scanf( %d%d%d%d , &a, &b, &c, &d); int a1 = abs(a - c) * 2; int b1 = abs(b - d) * 2; int ans = a1 + b1 + 4; if (a != c && b != d) printf( %d n , ans); else printf( %d n , ans + 2); return 0; } |
#include <bits/stdc++.h> using namespace std; long long n, k, a, b; long long mini = (long long)1e15; long long maxi = 0; long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } void calc(long long l) { if (l <= 0 || l > n * k) return; long long tmp = n * k / gcd(n * k, l); mini = min(tmp, mini); maxi = max(tmp, maxi); } int main() { cin >> n >> k >> a >> b; for (int i = -2; i <= n; ++i) { calc(k * i + b - a); calc(k * i - b - a); calc(k * i + b + a); calc(k * i - b + a); } cout << mini << << maxi << endl; } |
#include <bits/stdc++.h> int main() { int n, m; scanf( %d%d , &n, &m); std::vector<std::vector<int>> graph(n); for (int i = 0; i < m; ++i) { int a, b; scanf( %d%d , &a, &b); a--; b--; graph.at(a).push_back(b); graph.at(b).push_back(a); } std::vector<int> color(n, -1); bool ok = true; for (int s = 0; s < n && ok; ++s) { if (!~color[s]) { std::vector<int> queue; queue.push_back(s); color[s] = 0; for (int h = 0; h < static_cast<int>(queue.size()); ++h) { auto u = queue[h]; for (int v : graph.at(u)) { if (!~color[v]) { color[v] = color[u] ^ 1; queue.push_back(v); } ok &= color[u] != color[v]; } } } } if (ok) { std::vector<std::vector<int>> result(2); for (int i = 0; i < n; ++i) { result[color[i]].push_back(i); } for (int i = 0; i < 2; ++i) { printf( %d n , static_cast<int>(result[i].size())); for (auto&& v : result[i]) { printf( %d , v + 1); } puts( ); } } else { puts( -1 ); } } |
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main() { int n, l[300], c[300]; cin >> n; for (int i = 0; i < n; i++) cin >> l[i]; for (int i = 0; i < n; i++) cin >> c[i]; unordered_map<int, int> um; for (int i = 0; i < n; i++) if (um.find(l[i]) != um.end()) um[l[i]] = min(um[l[i]], c[i]); else um.emplace(l[i], c[i]); for (int i = 0; i < n; i++) { vector<pair<int, int>> ve; for (auto& elem : um) { int g = gcd(elem.first, l[i]); if (um.find(g) != um.end()) um[g] = min(um[g], elem.second + c[i]); else ve.emplace_back(g, elem.second + c[i]); } for (auto& elem : ve) um.insert(elem); } cout << (um[1] ?: -1) << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; struct B { int p, id; }; priority_queue<B> q; bool operator<(const B &a, const B &b) { return a.p < b.p; } int main() { string s; long long a, b, l, len, cost, i; B tmp; cin >> s; l = 0; len = s.size(); cost = 0; for (i = 0; i < len; i++) { if (s[i] == ( ) l++; else if (s[i] == ) ) l--; else { cin >> a >> b; tmp.p = b - a; tmp.id = i; q.push(tmp); s[i] = ) ; l--; cost += b; } if (l < 0) { if (q.empty()) break; tmp = q.top(); q.pop(); cost -= tmp.p; s[tmp.id] = ( ; l += 2; } } if (l != 0) cout << -1 << endl; else { cout << cost << endl; cout << s << endl; } while (!q.empty()) q.pop(); } |
#include <bits/stdc++.h> using namespace std; struct p { int x; int i; } a[200123]; int cmp(p a, p b) { if (a.x != b.x) return a.x < b.x; else return a.i < b.i; } int main() { int n; int i, j; scanf( %d , &n); for (i = 0; i < n; i++) { scanf( %d , &a[i].x); a[i].i = i; } sort(a, a + n, cmp); int mi = 0; long long y = 0; i = 1; while (i < n && a[i].x == a[0].x) { if (a[i].i - a[i - 1].i - 1 > mi) mi = a[i].i - a[i - 1].i - 1; i++; } if (a[0].i + n - a[i - 1].i - 1 > mi) mi = a[0].i + n - a[i - 1].i - 1; y = (long long)a[0].x * n + mi; printf( %lld n , y); return 0; } |
#include <bits/stdc++.h> using namespace std; const int inf = 1000000000; const double pi = 2 * acos(0.0); const int MOD = 1000000007; vector<int> g[5]; int id[222][222], k[222]; bool ok[222]; int wh[222]; int main() { int n; cin >> n; for (int i = 1; i <= n; ++i) { cin >> wh[i]; g[wh[i]].push_back(i); } for (int i = 1; i <= n; ++i) { cin >> k[i]; for (int j = 1; j <= k[i]; ++j) cin >> id[i][j]; } int res = inf; for (int ST = 1; ST <= 3; ++ST) { int st = ST; memset(ok, false, sizeof(ok)); int cnt = 0, ans = 0; for (;;) { if (cnt == n) break; for (;;) { bool doing = false; for (int c = 0; c <= (int)g[st].size() - 1; ++c) { int work = g[st][c]; if (ok[work]) continue; bool is = true; for (int i = 1; i <= k[work]; ++i) if (!ok[id[work][i]]) is = false; if (is) { doing = true; ++ans; ok[work] = true; ++cnt; } } if (!doing) break; } if (cnt == n) break; ++ans; st++; if (st == 4) st = 1; } res = min(res, ans); } cout << res << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { int a; cin >> a; int i = 1; while (true) { int cnt = i / 2 + 1; if (cnt == a) { break; } ++i; } cout << i << << 2 << endl; cout << 1 2 n ; return 0; } |
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vd = vector<double>; using vs = vector<string>; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<double, double>; using vpii = vector<pii>; using vvpii = vector<vpii>; using vpll = vector<pll>; using vvpll = vector<vpll>; using vpdd = vector<pdd>; using vvpdd = vector<vpdd>; template <typename T> void ckmin(T& a, const T& b) { a = min(a, b); } template <typename T> void ckmax(T& a, const T& b) { a = max(a, b); } mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); namespace __input { template <class T1, class T2> void re(pair<T1, T2>& p); template <class T> void re(vector<T>& a); template <class T, size_t SZ> void re(array<T, SZ>& a); template <class T> void re(T& x) { cin >> x; } void re(double& x) { string t; re(t); x = stod(t); } template <class Arg, class... Args> void re(Arg& first, Args&... rest) { re(first); re(rest...); } template <class T1, class T2> void re(pair<T1, T2>& p) { re(p.first, p.second); } template <class T> void re(vector<T>& a) { for (int i = 0; i < (int((a).size())); i++) re(a[i]); } template <class T, size_t SZ> void re(array<T, SZ>& a) { for (int i = 0; i < (SZ); i++) re(a[i]); } } // namespace __input using namespace __input; namespace __output { template <class T1, class T2> void pr(const pair<T1, T2>& x); template <class T, size_t SZ> void pr(const array<T, SZ>& x); template <class T> void pr(const vector<T>& x); template <class T> void pr(const deque<T>& x); template <class T> void pr(const set<T>& x); template <class T1, class T2> void pr(const map<T1, T2>& x); template <class T> void pr(const T& x) { cout << x; } template <class Arg, class... Args> void pr(const Arg& first, const Args&... rest) { pr(first); pr(rest...); } template <class T1, class T2> void pr(const pair<T1, T2>& x) { pr( { , x.first, , , x.second, } ); } template <class T, bool pretty = true> void prContain(const T& x) { if (pretty) pr( { ); bool fst = 1; for (const auto& a : x) pr(!fst ? pretty ? , : : , a), fst = 0; if (pretty) pr( } ); } template <class T> void pc(const T& x) { prContain<T, false>(x); pr( n ); } template <class T, size_t SZ> void pr(const array<T, SZ>& x) { prContain(x); } template <class T> void pr(const vector<T>& x) { prContain(x); } template <class T> void pr(const deque<T>& x) { prContain(x); } template <class T> void pr(const set<T>& x) { prContain(x); } template <class T1, class T2> void pr(const map<T1, T2>& x) { prContain(x); } void ps() { pr( n ); } template <class Arg> void ps(const Arg& first) { pr(first); ps(); } template <class Arg, class... Args> void ps(const Arg& first, const Args&... rest) { pr(first, ); ps(rest...); } } // namespace __output using namespace __output; namespace __algorithm { template <typename T> void dedup(vector<T>& v) { sort((v).begin(), (v).end()); v.erase(unique((v).begin(), (v).end()), v.end()); } template <typename T> typename vector<T>::iterator find(vector<T>& v, const T& x) { auto it = lower_bound((v).begin(), (v).end(), x); return it != v.end() && *it == x ? it : v.end(); } template <typename T> size_t index(vector<T>& v, const T& x) { auto it = find(v, x); assert(it != v.end() && *it == x); return it - v.begin(); } template <typename C, typename T, typename OP> vector<T> prefixes(const C& v, T id, OP op) { vector<T> r(int((v).size()) + 1, id); for (int i = 0; i < (int((v).size())); i++) r[i + 1] = op(r[i], v[i]); return r; } template <typename C, typename T, typename OP> vector<T> suffixes(const C& v, T id, OP op) { vector<T> r(int((v).size()) + 1, id); for (int i = (int((v).size())) - 1; i >= 0; i--) r[i] = op(v[i], r[i + 1]); return r; } } // namespace __algorithm using namespace __algorithm; #pragma GCC diagnostic push #pragma GCC diagnostic ignored -Wunused-parameter struct monostate { friend istream& operator>>(istream& is, const monostate& ms) { return is; } friend ostream& operator<<(ostream& os, const monostate& ms) { return os; } friend monostate operator+(const monostate& a, const monostate& b) { return a; } } ms; #pragma GCC diagnostic pop namespace __io { void setIn(string second) { freopen(second.c_str(), r , stdin); } void setOut(string second) { freopen(second.c_str(), w , stdout); } void setIO(string second = ) { ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(15); if (int((second).size())) { setIn(second + .in ), setOut(second + .out ); } } } // namespace __io using namespace __io; bool taste(int loc) { cout << ? << loc + 1 << endl; char r; re(r); if (r == E ) assert(0); return r != Y ; } int main() { setIO(); int N, K; re(N, K); int SZ = max(K / 2, 1); int M = N / SZ; vi ct(N, 1); for (int j = (1); j < (M); j++) { for (int res = 0; res < (j); res++) if (res + j < M) { cout << R << endl; for (int i = res; i < M; i += j) { for (int k = 0; k < (SZ); k++) ct[i * SZ + k] &= taste(i * SZ + k); } } } cout << ! << accumulate((ct).begin(), (ct).end(), 0) << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; const long long INF = 1e18; const double eps = 1e-10; mt19937 mrand(chrono::high_resolution_clock::now().time_since_epoch().count()); template <class T> inline void upmin(T &x, T y) { x = x > y ? y : x; } template <class T> inline void upmax(T &x, T y) { x = x < y ? y : x; } const int maxn = 1e5 + 233; vector<int> G[maxn]; int n; int col[maxn]; int fa[maxn]; int cnt[maxn]; void dfs(int u, int f) { for (auto v : G[u]) { if (v == f) continue; col[v] = 3 - col[u]; fa[v] = u; dfs(v, u); } } int deg[maxn]; int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cout << fixed << setprecision(10); cin >> n; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; G[u].emplace_back(v); G[v].emplace_back(u); deg[u]++; deg[v]++; } for (int i = 1; i <= n; i++) { if (deg[i] > 1) { col[i] = 1; dfs(i, 0); break; } } int f1 = 0, f2 = 0; for (int i = 1; i <= n; i++) { if (deg[i] == 1) { if (col[i] == 1) f1 = 1; else f2 = 1; cnt[fa[i]]++; } } int mn, mx; if (f1 && f2) mn = 3; else mn = 1; mx = n - 1; for (int i = 1; i <= n; i++) { if (cnt[i]) mx -= cnt[i] - 1; } cout << mn << << mx << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 5; stack<pair<long long, long long> > q; long long n, a[N], go[N]; struct cell { long long max_l, max_r, or_l, or_r; }; bool d[N][35]; cell b[N]; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (long long i = 1; i <= n; i++) cin >> a[i]; for (long long i = 1; i <= n; i++) { long long q = a[i], k = 1; while (q != 0) { d[i][k] = q % 2; q /= 2; k++; } } q.push({0, 2e9}); for (long long i = 1; i <= n; i++) { while (q.top().second <= a[i]) q.pop(); b[i].max_l = q.top().first; q.push({i, a[i]}); } while (!q.empty()) q.pop(); q.push({n + 1, 2e9}); for (long long i = n; i > 0; i--) { while (q.top().second < a[i]) q.pop(); b[i].max_r = q.top().first; q.push({i, a[i]}); } while (!q.empty()) q.pop(); for (long long i = 1; i <= n; i++) { long long k = 0; for (long long j = 1; j < 35; j++) if (d[i][j] == 0) k = max(k, go[j]); else go[j] = i; b[i].or_l = k; } for (long long i = 1; i < 35; i++) go[i] = n + 1; for (long long i = n; i > 0; i--) { long long k = n + 1; for (long long j = 1; j < 35; j++) if (d[i][j] == 0) k = min(k, go[j]); else go[j] = i; b[i].or_r = k; } long long ans = 0; for (long long i = 1; i <= n; i++) { if (b[i].max_l >= b[i].or_l) if (b[i].max_r <= b[i].or_r) continue; else ans += (i - b[i].max_l) * (b[i].max_r - b[i].or_r); else if (b[i].max_r <= b[i].or_r) ans += (b[i].or_l - b[i].max_l) * (b[i].max_r - i); else { ans += (b[i].or_l - b[i].max_l) * (b[i].or_r - i); ans += (b[i].max_r - b[i].or_r) * (i - b[i].or_l); ans += (b[i].or_l - b[i].max_l) * (b[i].max_r - b[i].or_r); } } cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; vector<int> ev[1000099]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<int> p(n), pos(n); for (int(i) = (0); (i) < (int)(n); ++(i)) { cin >> p[i]; p[i]--; pos[p[i]] = i; ev[n - i].push_back(p[i]); int k = (p[i] - i + n) % n; if (k != n - i) ev[k].push_back(p[i]); } long long sm = 0; int x = 0, y = 0; vector<int> state(n); for (int(i) = (0); (i) < (int)(n); ++(i)) { if (i < p[i]) x++, state[p[i]] = -1; else y++, state[p[i]] = 1; sm += abs(i - p[i]); } long long mi = LLONG_MAX; int ans = -1; for (int(i) = (0); (i) < (int)(n); ++(i)) { if (i > 0) { for (auto &(num) : (ev[i])) { int nextPos = (pos[num] + i) % n; int prePos = (pos[num] + i - 1) % n; int nextState = nextPos < num ? -1 : 1; sm += abs(nextPos - num) - abs(prePos - num); if (state[num] == -1 && nextState == 1) { sm--; x--; y++; state[num] = 1; } else if (state[num] == 1 && nextState == -1) { sm++; x++; y--; state[num] = -1; } else if (state[num] == nextState) { if (state[num] == 1) sm--; else sm++; } } sm -= x; sm += y; } if (sm < mi) { ans = i; mi = sm; } } cout << mi << << ans << endl; } |
#include <bits/stdc++.h> int main() { int a, b, c, d, p1, p2, p3, p4; scanf( %d %d %d %d , &a, &b, &c, &d); p1 = ((3 * a / 10)); p2 = (a - (a * c / 250)); if (p1 > p2) { p1 = p1; } else if (p2 > p1) { p1 = p2; } else if (p2 == p1) { p1 = p1; } p3 = ((3 * b / 10)); p4 = (b - (b * d / 250)); if (p3 > p4) { p3 = p3; } else if (p4 > p3) { p3 = p4; } else if (p3 == p4) { p3 = p3; } if (p1 > p3) { printf( Misha n ); } else if (p3 > p1) { printf( Vasya n ); } else if (p1 == p3) { printf( Tie n ); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int numOfComparisons, HomeUniform, GuestUniform, count = 0; cin >> numOfComparisons; int *Home = new int[numOfComparisons]; int *Guest = new int[numOfComparisons]; for (int i = 0; i < numOfComparisons; i++) { cin >> Home[i] >> Guest[i]; } for (int i = 0; i < numOfComparisons; i++) { for (int j = 0; j < numOfComparisons; j++) { if (Home[i] == Guest[j]) count++; } } cout << count; return 0; } |
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) using namespace std; template <class T> using pq = priority_queue<T>; template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>; int scan() { return getchar(); } void scan(int& a) { cin >> a; } void scan(long long& a) { cin >> a; } void scan(char& a) { cin >> a; } void scan(double& a) { cin >> a; } void scan(long double& a) { cin >> a; } void scan(char a[]) { scanf( %s , a); } void scan(string& a) { cin >> a; } template <class T> void scan(vector<T>&); template <class T, size_t size> void scan(array<T, size>&); template <class T, class L> void scan(pair<T, L>&); template <class T, size_t size> void scan(T (&)[size]); template <class T> void scan(vector<T>& a) { for (auto& i : a) scan(i); } template <class T> void scan(deque<T>& a) { for (auto& i : a) scan(i); } template <class T, size_t size> void scan(array<T, size>& a) { for (auto& i : a) scan(i); } template <class T, class L> void scan(pair<T, L>& p) { scan(p.first); scan(p.second); } template <class T, size_t size> void scan(T (&a)[size]) { for (auto& i : a) scan(i); } template <class T> void scan(T& a) { cin >> a; } void IN() {} template <class Head, class... Tail> void IN(Head& head, Tail&... tail) { scan(head); IN(tail...); } string stin() { string s; cin >> s; return s; } template <class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template <class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } vector<int> iota(int n) { vector<int> a(n); iota(begin(a), end(a), 0); return a; } template <class T> void UNIQUE(vector<T>& x) { sort(begin(x), end(x)); x.erase(unique(begin(x), end(x)), x.end()); } int in() { int x; cin >> x; return x; } long long lin() { unsigned long long x; cin >> x; return x; } void print() { putchar( ); } void print(bool a) { cout << a; } void print(int a) { cout << a; } void print(long long a) { cout << a; } void print(char a) { cout << a; } void print(string& a) { cout << a; } void print(double a) { cout << a; } template <class T> void print(const vector<T>&); template <class T, size_t size> void print(const array<T, size>&); template <class T, class L> void print(const pair<T, L>& p); template <class T, size_t size> void print(const T (&)[size]); template <class T> void print(const vector<T>& a) { if (a.empty()) return; print(a[0]); for (auto i = a.begin(); ++i != a.end();) { cout << ; print(*i); } cout << endl; } template <class T> void print(const deque<T>& a) { if (a.empty()) return; print(a[0]); for (auto i = a.begin(); ++i != a.end();) { cout << ; print(*i); } } template <class T, size_t size> void print(const array<T, size>& a) { print(a[0]); for (auto i = a.begin(); ++i != a.end();) { cout << ; print(*i); } } template <class T, class L> void print(const pair<T, L>& p) { cout << ( ; print(p.first); cout << , ; print(p.second); cout << ) ; } template <class T> void print(set<T>& x) { for (auto e : x) print(e), cout << ; cout << endl; } template <class T, size_t size> void print(const T (&a)[size]) { print(a[0]); for (auto i = a; ++i != end(a);) { cout << ; print(*i); } } template <class T> void print(const T& a) { cout << a; } int out() { putchar( n ); return 0; } template <class T> int out(const T& t) { print(t); putchar( n ); return 0; } template <class Head, class... Tail> int out(const Head& head, const Tail&... tail) { print(head); putchar( ); out(tail...); return 0; } long long gcd(long long a, long long b) { while (b) { long long c = b; b = a % b; a = c; } return a; } long long lcm(long long a, long long b) { if (!a || !b) return 0; return a * b / gcd(a, b); } vector<pair<long long, long long>> factor(long long x) { vector<pair<long long, long long>> ans; for (long long i = 2; i * i <= x; i++) if (x % i == 0) { ans.push_back({i, 1}); while ((x /= i) % i == 0) ans.back().second++; } if (x != 1) ans.push_back({x, 1}); return ans; } vector<int> divisor(int x) { vector<int> ans; for (int i = 1; i * i <= x; i++) if (x % i == 0) { ans.push_back(i); if (i * i != x) ans.push_back(x / i); } return ans; } int popcount(long long x) { return __builtin_popcountll(x); } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int rnd(int n) { return uniform_int_distribution<int>(0, n - 1)(rng); } template <class... T> void err(const T&...) {} struct Setup_io { Setup_io() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << fixed << setprecision(15); } } setup_io; double d(long long x, long long y) { return sqrt((long double)x * (long double)x + (long double)y * (long double)y); } double d(pair<int, int> x) { return d(x.first, x.second); } int main() { int n, k; IN(n, k); map<pair<int, int>, vector<pair<int, int>>> mp; for (long long i = 0; i < n; ++i) { long long a, b; IN(a, b); if (!a and !b) continue; if (b < 0 or (b == 0 and a <= 0)) { long long g = gcd(a, b); g = abs(g); mp[{a / g, b / g}].emplace_back(abs(a), abs(b)); } else { long long g = gcd(a, b); g = abs(g); mp[{a / g, b / g}].emplace_back(abs(a), abs(b)); } } vector<long double> ans{0}; bool flag = false; for (auto [e, v] : mp) { sort(begin(v), end(v), greater<pair<int, int>>()); if (v.size() > k / 2) { for (long long i = 0; i < k / 2; ++i) { long double D = d(v[i]); ans.emplace_back(D * (k - 1 - i * 2)); } int c = k / 2; long double dist = 0; for (long long i = v.size() - 1; i >= c; --i) { long double D = d(v[i]); ans.emplace_back(D * (k - 1 - c * 2) - dist); dist += D * 2; } } else for (long long i = 0; i < v.size(); ++i) { long double D = d(v[i]); ans.emplace_back(D * (k - 1 - i * 2)); } } sort(begin(ans), end(ans), greater<long double>()); long double sum = 0; for (long long i = 0; i < k; ++i) sum += ans[i]; cout << sum << n ; } |
// Author : Mc_LaReN #include <bits/stdc++.h> #define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0) using namespace std; const int MOD = 1e9 + 7, MX = 400005; #define DEBUG cerr<< /n>>>I m Here<<</n <<endl; #define what_is(x) cerr << #x << is << x << endl; #define pf push_front #define pb push_back #define endl n #define ll int64_t #define sz(x) (int)x.size() #define max(x, y) (y-x >> 31 & (x^y) ^ y) #define min(x, y) (y-x >> 31 & (x^y) ^ x) template <class X> void swp( X &a, X &b) { X tp = a; a = b; b = tp; } template <class X> void print(vector<X> &a) {for(auto& it : a) cout<<it<< ; cout<<endl;} ll gcd(ll x,ll y){ return (y==0 ? x : gcd(y, x % y) ); } ll LCM(ll x,ll y){ return (x/gcd(x,y))*y; } char ch,ch1,ch2,ch3; string s,s1,s2,s3; vector<long long> v,v1,v2,v3; vector<int> vi; vector<string> vstr,vstr2,vstr3; void run(); int32_t main() { IOS; #ifndef ONLINE_JUDGE freopen( input.txt , r , stdin); freopen( error.txt , w , stderr); freopen( output.txt , w , stdout); #endif int t; cin >> t; while(t--) { run(); cout << n ; } #ifndef ONLINE_JUDGE cerr<< time taken : <<(float)clock()/CLOCKS_PER_SEC<< secs <<endl; #endif return 0; } void run() { ll n; cin>>n; ll a=1, ans = 0; for(ll i = 1; i <= n; ++i){ // ans+=n/a; // // a=LCM(a,i); a=LCM(a,i); if(a>n)break; ans+=n/a; } cout<< (ans+n) % MOD; } |
#include <bits/stdc++.h> using namespace std; long long int a[200]; int main() { long long int n, ans = 0, m, i, j, x, y; cin >> n >> m; for (i = 1; i <= n; i++) cin >> a[i]; for (i = 1; i <= m; i++) { cin >> x >> y; long long int ret = 0; for (j = x; j <= y; j++) { ret += a[j]; } if (ret > 0) ans += ret; } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; using namespace std; int main() { int t; cin >> t; while (t--) { long long a, b, c, d; long long ar[6]; cin >> a >> b >> c; ar[0] = a; ar[1] = b; ar[2] = c; sort(ar, ar + 3); cout << ar[2] + 1 << endl; } } |
#include <bits/stdc++.h> using namespace std; bool mark[72][72][72]; int a[10]; pair<int, pair<int, int> > Par[72][72][72]; pair<int, int> par[72][72][72]; queue<pair<int, pair<int, int> > > q; vector<pair<int, int> > v; char c[100][100]; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; cin >> a[3] >> a[1] >> a[2]; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> c[i][j]; sort(a + 1, a + 3 + 1); if (a[1] == 1 && a[2] == 2 && a[3] == 3) { cout << 0; return 0; } bool ans = false; mark[a[1]][a[2]][a[3]] = true; q.push(make_pair(a[1], make_pair(a[2], a[3]))); while (q.size()) { int A = q.front().first; int B = q.front().second.first; int C = q.front().second.second; if (A == 1 && B == 2 && C == 3) { ans = true; while (par[A][B][C].first != 0) { v.push_back(par[A][B][C]); int AA = A, BB = B, CC = C; A = Par[AA][BB][CC].first; B = Par[AA][BB][CC].second.first; C = Par[AA][BB][CC].second.second; } } for (int i = 1; i <= n; i++) { if (i != A && i != B && i != C) { if (c[A][i] == c[B][C]) { a[1] = i; a[2] = B; a[3] = C; sort(a + 1, a + 3 + 1); if (!mark[a[1]][a[2]][a[3]]) { par[a[1]][a[2]][a[3]] = make_pair(A, i); Par[a[1]][a[2]][a[3]] = make_pair(A, make_pair(B, C)); mark[a[1]][a[2]][a[3]] = true; q.push(make_pair(a[1], make_pair(a[2], a[3]))); } } } } for (int i = 1; i <= n; i++) { if (i != A && i != B && i != C) { if (c[B][i] == c[A][C]) { a[1] = A; a[2] = i; a[3] = C; sort(a + 1, a + 3 + 1); if (!mark[a[1]][a[2]][a[3]]) { par[a[1]][a[2]][a[3]] = make_pair(B, i); Par[a[1]][a[2]][a[3]] = make_pair(A, make_pair(B, C)); mark[a[1]][a[2]][a[3]] = true; q.push(make_pair(a[1], make_pair(a[2], a[3]))); } } } } for (int i = 1; i <= n; i++) { if (i != A && i != B && i != C) { if (c[C][i] == c[B][A]) { a[1] = A; a[2] = B; a[3] = i; sort(a + 1, a + 3 + 1); if (!mark[a[1]][a[2]][a[3]]) { par[a[1]][a[2]][a[3]] = make_pair(C, i); Par[a[1]][a[2]][a[3]] = make_pair(A, make_pair(B, C)); mark[a[1]][a[2]][a[3]] = true; q.push(make_pair(a[1], make_pair(a[2], a[3]))); } } } } q.pop(); } if (!ans) { cout << -1; return 0; } cout << v.size() << endl; for (int i = v.size() - 1; i >= 0; i--) cout << v[i].first << << v[i].second << endl; } |
#include <bits/stdc++.h> using namespace std; int n, p; bool prime(int num) { for (int i = 2; i <= sqrt(num); i++) { if (num % i == 0) return 0; } return 1; } int main() { cin >> n; if (prime(n)) { return cout << 1 << endl << n, 0; } p = (n - 3) / 2; cout << 3 << n << 3 << ; for (int i = 0; i < p; i++) { if (prime(p - i) && prime(p + i)) { cout << p - i << << p + i; return 0; } } return 0; } |
#include <bits/stdc++.h> using namespace std; void my_functions() { return; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; long long int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } string s; cin >> s; long long int b[n], i; b[0] = a[0]; for (i = 1; i < n; i++) { b[i] = a[i] + b[i - 1]; } long long int m1 = 0, m2 = 0; for (i = 0; i < n; i++) { if (s[i] == 1 ) { m1 = a[i] + m1; if (i > 0) { m2 = b[i - 1]; } m1 = max(m1, m2); } } long long int ans = max(m1, m2); cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; void err(istream_iterator<string> it) {} template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << = << a << n ; err(++it, args...); } long long powMod(long long x, long long y) { long long p = 1; while (y) { if (y % 2) { p = (p * (x % ((long long)1e9 + 7))) % ((long long)1e9 + 7); } y /= 2; x = ((x % ((long long)1e9 + 7)) * (x % ((long long)1e9 + 7))) % ((long long)1e9 + 7); } return p; } long long CpowMod(long long x, long long y, long long w) { long long p = 1; while (y) { if (y % 2) { p = (p * (x % w)) % w; } y /= 2; x = ((x % w) * (x % w)) % w; } return p; } long long invMod(long long x) { return powMod(x, ((long long)1e9 + 7) - 2); } long long CinvMod(long long x, long long w) { return CpowMod(x, w - 2, w); } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long totLeaf; vector<set<long long> > edges; long long bud = -1; long long n; void removeEdge(long long x, long long y) { {}; edges[x].erase(y); } void dfs(long long node, long long par) { for (auto &it : edges[node]) { if (it != par) { dfs(it, node); } } long long cnt = 0; for (auto &it : edges[node]) { if (it == par || edges[it].count(node) == 0) { continue; } cnt++; } if (cnt) { totLeaf += cnt; bud++; removeEdge(node, par); } } void solve() { long long T; cin >> T; while (T--) { totLeaf = 0; bud = -1; cin >> n; edges = vector<set<long long> >(n + 1); for (long long i = 0; i <= n - 2; i++) { long long x, y; cin >> x >> y; edges[x].insert(y); edges[y].insert(x); } dfs(1, 0); cout << totLeaf - (bud) << n ; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(10); solve(); return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 5e3 + 5; const int N = 1e4 + 5; const int inf = 0x3f3f3f3f; struct hor { int x1, x2, y; bool operator<(const hor& a) const { return y < a.y; } } h[maxn]; struct ver { int y1, y2, x; bool operator<(const ver& a) const { return y1 < a.y1; } } v[maxn]; int n, cntv, cnth; int c[N]; int lowbit(int x) { return x & (-x); } int sum(int x) { int ans = 0; for (int i = x; i > 0; i -= lowbit(i)) ans += c[i]; return ans; } int get_sum(int l, int r) { return sum(r) - sum(l - 1); } void update(int x, int val) { for (int i = x; i <= N; i += lowbit(i)) c[i] += val; } int main() { scanf( %d , &n); cntv = cnth = 0; for (int i = 1; i <= n; ++i) { int x1, y1, x2, y2; scanf( %d %d %d %d , &x1, &y1, &x2, &y2); x1 += 5001, y1 += 5001, x2 += 5001, y2 += 5001; if (x1 == x2) v[++cntv] = {min(y1, y2), max(y1, y2), x1}; else h[++cnth] = {min(x1, x2), max(x1, x2), y1}; } sort(h + 1, h + cnth + 1); sort(v + 1, v + cntv + 1); long long ans = 0; for (int i = 1; i <= cnth; ++i) { memset(c, 0, sizeof(c)); for (int j = 1, k = 1; k < i;) { if (v[j].y1 <= h[k].y && j <= cntv) { if (v[j].x >= h[i].x1 && v[j].x <= h[i].x2 && v[j].y2 >= h[i].y) update(v[j].x, 1); ++j; } else { int res = get_sum(h[k].x1, h[k].x2); ans += 1ll * res * (res - 1) / 2; ++k; } } } printf( %lld n , ans); return 0; } |
#include <bits/stdc++.h> const int INF = 500000001; const double EPS = 1e-9; const double PI = acos(-1.0); using namespace std; int main() { srand(time(NULL)); char ch[101][105]; int n, m; int bl[105][105]; while (~scanf( %d %d , &n, &m)) { for (int i = 0; i < n; i++) { scanf( %s , ch[i]); } int ans = 0; for (int j = 0; j < m; j++) { int flag = 1; bl[0][j] = 1; for (int i = 1; i < n; i++) { if (ch[i][j] < ch[i - 1][j] && (j == 0 || bl[i][j - 1] <= 1)) { flag = 0; break; } else if (ch[i][j] == ch[i - 1][j] && (j == 0 || bl[i][j - 1] == 1)) { bl[i][j] = 1; } else if (j == 0 || bl[i][j - 1] >= 1) { bl[i][j] = 2; } else { bl[i][j] = 0; } } if (!flag) { ++ans; for (int i = 0; i < n; i++) { ch[i][j] = ch[0][j]; if (j == 0) bl[i][j] = 1; else bl[i][j] = bl[i][j - 1]; } } } printf( %d n , ans); } return 0; } |
#include <bits/stdc++.h> using namespace std; long long vec3[17][1 << 17]; long long mn(int i, int j) { int p = 31 - __builtin_clz(j - i); return min(vec3[p][i], vec3[p][j - (1 << p)]); } void mn_init(int n) { int mp = 31 - __builtin_clz(n); for (int p = 0; p < mp; ++p) for (int x = 0; x < n - (1 << p); ++x) vec3[p + 1][x] = min(vec3[p][x], vec3[p][x + (1 << p)]); } long long vec2[17][1 << 17]; long long mx(int i, int j) { int p = 31 - __builtin_clz(j - i); return max(vec2[p][i], vec2[p][j - (1 << p)]); } void mx_init(int n) { int mp = 31 - __builtin_clz(n); for (int p = 0; p < mp; ++p) for (int x = 0; x < n - (1 << p); ++x) vec2[p + 1][x] = max(vec2[p][x], vec2[p][x + (1 << p)]); } int izq[100010]; struct rmq { int MAX; long long vec[4 * 100000]; long long* init(int n) { MAX = 1 << (32 - __builtin_clz(n)); fill(vec, vec + 2 * MAX, 0); return vec + MAX; } void updall() { for (int i = MAX - 1; i >= 0; --i) vec[i] = min(vec[2 * i], vec[2 * i + 1]); } void pset(int i, long long vl) { vec[i += MAX] = vl; while (i) { i /= 2; vec[i] = min(vec[2 * i], vec[2 * i + 1]); } } long long pget(int i, int j) { return _pget(i + MAX, j + MAX); } long long _pget(int i, int j) { long long res = 100000000; if (j - i <= 0) return res; if (i % 2) res = min(res, vec[i++]); res = min(res, _pget(i / 2, j / 2)); if (j % 2) res = min(res, vec[--j]); return res; } }; rmq DP; int main() { long long n, s, l; cin >> n >> s >> l; for (int i = 0; i < n; i++) { cin >> vec3[0][i]; vec2[0][i] = vec3[0][i]; } mn_init(n); mx_init(n); for (int i = 1; i < n; i++) { if (mn(0, i) < vec3[0][i] - s) { int bot = 0, top = i; while (top - bot > 1) { int mid = (top + bot) / 2; if (mn(mid, i) < vec3[0][i] - s) bot = mid; else top = mid; } izq[i] = max(izq[i], top); } if (mx(0, i) > vec3[0][i] + s) { int bot = 0, top = i; while (top - bot > 1) { int mid = (top + bot) / 2; if (mx(mid, i) > vec3[0][i] + s) bot = mid; else top = mid; } izq[i] = max(izq[i], top); } } for (int i = 1; i < n; i++) izq[i] = max(izq[i], izq[i - 1]); for (int i = 0; i < n; ++i) izq[i]--; long long* v = DP.init(n); for (int i = 0; i < n; ++i) v[i] = 100000000; DP.updall(); for (int i = 0; i < n; ++i) { if (izq[i] == -1 && i >= l - 1) DP.pset(i, 1); else { DP.pset(i, DP.pget(izq[i], i - l + 1) + 1); } } if (DP.pget(n - 1, n) <= n) cout << DP.pget(n - 1, n) << endl; else cout << -1 << endl; } |
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > v; vector<string> veg; bool comp(pair<int, int> p1, pair<int, int> p2) { return (p1.first < p2.first or ((p1.first == p2.first) and (p1.second < p2.second))); } int main() { int n, m, k, t, x, y; veg.push_back( Grapes ); veg.push_back( Carrots ); veg.push_back( Kiwis ); cin >> n >> m >> k >> t; while (k--) { cin >> x >> y; v.push_back(make_pair(x, y)); } sort(v.begin(), v.end(), comp); while (t--) { int f = 0, s = 0; int left = 0, right = v.size() - 1; cin >> f >> s; int mid = (left + right) / 2, flag = 0; while (left < right) { mid = left + (right - left) / 2 + 1; if (v[mid].first == f and v[mid].second == s) { flag = 1; cout << Waste << endl; break; } if (f < v[mid].first or (f == v[mid].first and s < v[mid].second)) right = mid - 1; else left = mid; } if (!flag) { if (v[left].first == f and v[left].second == s) cout << Waste << endl; else { if (v[left].first > f or v[left].first == f and v[left].second > s) left--; cout << veg[((f - 1) * m + s - left - 1) % 3] << endl; } } } return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxlongint = 2147483647; const int inf = 1000000000; char s[10], a[1000][1100]; ; int main() { scanf( %s , s); int n; cin >> n; for (int i = 1; i <= n; i++) scanf( %s , a[i]); int ans = 0; for (int i = 1; i <= n; i++) { if (a[i][0] == s[0] && a[i][1] == s[1]) ans = 1; for (int j = 1; j <= n; j++) if (a[i][1] == s[0] && a[j][0] == s[1]) ans = 1; } if (ans) printf( YES ); else printf( NO ); } |
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 7; vector<int> G[N]; int vis[N] = {0}, p[N]; long long sum[N] = {0}; stack<int> s; void dfs(int u, int fa) { s.push(u); vis[u] = 1; for (int i = 0; i < (int)G[u].size(); i++) { int v = G[u][i]; if (v == fa) continue; if (!vis[v]) dfs(v, u); else if (vis[v] == 1) { int maxx = u, minn = u; while (!s.empty()) { int t = s.top(); s.pop(); maxx = max(t, maxx); minn = min(t, minn); if (t == v) break; } p[minn] = maxx; } } if (!s.empty() && s.top() == u) s.pop(); vis[u] = 2; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; cin >> x >> y; G[x].push_back(y); G[y].push_back(x); } for (int i = 1; i <= n + 1; i++) p[i] = n + 1; for (int i = 1; i <= n; i++) if (!vis[i]) dfs(i, -1); for (int i = n; i >= 1; i--) { p[i] = min(p[i], p[i + 1]); sum[i] = sum[i + 1] + p[i] - i; } int q; cin >> q; while (q--) { int l, r; cin >> l >> r; int L = l, R = r + 1; while (L < R) { int m = (L + R) / 2; if (p[m] > r) R = m; else L = m + 1; } cout << sum[l] - sum[L] + 1ll * (r - L + 1) * (r - L + 2) / 2 << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int t[10], card[105]; int main() { int i, sum, ans; sum = 0; for (i = 1; i <= 5; i++) { cin >> t[i]; sum = sum + t[i]; card[t[i]]++; } ans = sum; for (i = 100; i >= 1; i--) { if (card[i] == 2) { if (sum - 2 * i < ans) ans = sum - 2 * i; } if (card[i] >= 3) { if (sum - 3 * i < ans) ans = sum - 3 * i; } } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; long long mM(long long a, long long b, long long mod) { long long res = 0; a %= mod; while (b) { if (b & 1) res = (res + a) % mod; a = (2 * a) % mod; b >>= 1; } return res; } long long fpow(long long first, long long second, long long p) { first = first % p; long long res = 1; while (second) { if (second & 1) res = mM(res, first, p); res %= p; if (res < 0) res = (res + p) % p; second = second >> 1; first = mM(first, first, p); if (first < 0) first = (first + p) % p; first %= p; } return res; } long long inv(long long n) { return fpow(n, 1000000007 - 2, 1000000007); } long long modmul(long long first, long long second) { return mM(first, second, 1000000007); } long long modadd(long long first, long long second) { long long temp = first % 1000000007 + second % 1000000007; temp %= 1000000007; if (temp < 0) temp = (temp + 1000000007) % 1000000007; return temp; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; t = 1; while (t--) { long long n, m; cin >> n >> m; string s, p; cin >> s >> p; string s1 = , s2 = ; for (int i = 0; i < s.size(); i++) if (s[i] != * ) s1 += s[i]; else break; for (int i = n - 1; i >= 0; i--) if (s[i] != * ) s2 += s[i]; else break; if (s == p) { cout << YES n ; return 0; } string jk = s2; reverse(jk.begin(), jk.end()); if (s1 == s && jk == s && s != p) { cout << NO n ; return 0; } if (s1.size() + s2.size() > p.size()) { cout << NO n ; return 0; } int i; for (i = 0; i < s1.size(); i++) if (s1[i] != p[i]) break; int j, k = m - 1; for (j = 0; j < s2.size(); j++) if (s2[j] != p[k]) break; else k--; if (i != s1.size() || j != s2.size()) cout << NO n ; else cout << YES n ; } } |
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; vector<int> e[N]; set<int> s[N]; queue<int> q; bool vis[N]; int d[N]; int main() { int n; scanf( %d , &n); for (int i = 1; i < n; i++) { int u, v; scanf( %d %d , &u, &v); d[u]++, d[v]++; e[u].push_back(v), e[v].push_back(u); } for (int i = 1; i <= n; i++) if (d[i] == 1) q.push(i), s[i].insert(0); while (q.size()) { int tp = q.front(); q.pop(); vis[tp] = true; for (int i = 0; i < e[tp].size(); i++) { int v = e[tp][i]; if (vis[v]) continue; d[v]--; s[v].insert(*(s[tp].begin()) + 1); if (d[v] == 1 && s[v].size() == 1) q.push(v); } } int cnt = 0, ans = 0; for (int i = 1; i <= n; i++) if (s[i].size() > 2 || s[i].size() == 0) return 0 * puts( -1 ); else if (s[i].size() == 1) ans = max(ans, *s[i].begin()); else cnt++, ans = max(ans, (*s[i].begin()) + (*s[i].rbegin())); if (cnt > 1) puts( -1 ); else { while (ans % 2 == 0) ans /= 2; printf( %d n , ans); } return 0; } |
#include <bits/stdc++.h> using namespace std; long long x, y, m; int main() { cin >> x >> y >> m; if (x >= m || y >= m) cout << 0 << n ; else { if (x <= 0 && y <= 0) cout << -1 << n ; else { long long cc = 0; if (x > y) swap(x, y); if (x < 0) { cc += x >= 0 ? x : -x / y; x += y * (long long)cc; } while (x < m && y < m) { if (x < y) x = x + y; else y = x + y; cc++; } cout << cc << n ; } } } |
#include <bits/stdc++.h> using namespace std; int main() { vector<int> v; int n, m; cin >> n >> m; int maks = 0; for (int i = 0; i < n; i++) v.push_back(i + 1); do { int res = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { int x = 100; for (int k = i; k <= j; k++) x = min(x, v[k]); res += x; } } maks = max(res, maks); } while (next_permutation(v.begin(), v.end())); v.clear(); for (int i = 0; i < n; i++) v.push_back(i + 1); do { int res = 0; for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { int x = 100; for (int k = i; k <= j; k++) x = min(x, v[k]); res += x; } } if (res == maks) { m--; if (m > 0) continue; for (int i = 0; i < n; i++) cout << v[i] << ; cout << n ; break; } } while (next_permutation(v.begin(), v.end())); } |
#include <bits/stdc++.h> using namespace std; int main() { string s1, s2, s3; long long a = 0, b = 0, c = 0; string unia; cin >> s1 >> s2 >> s3; long long *trm1, *trm2; string *uni; uni = &unia; if (s1[0] == A ) trm1 = &a; else if (s1[0] == B ) trm1 = &b; else if (s1[0] == C ) trm1 = &c; if (s1[2] == A ) trm2 = &a; else if (s1[2] == B ) trm2 = &b; else if (s1[2] == C ) trm2 = &c; if (s1[1] == > ) { *uni += s1[0]; *trm1 = *trm2 + 1 + *trm1; } else if (s1[1] == < ) { *uni += s1[2]; *trm2 = *trm1 + 1 + *trm2; } if (s2[0] == A ) trm1 = &a; else if (s2[0] == B ) trm1 = &b; else if (s2[0] == C ) trm1 = &c; if (s2[2] == A ) trm2 = &a; else if (s2[2] == B ) trm2 = &b; else if (s2[2] == C ) trm2 = &c; if (s2[1] == > ) { *uni += s2[0]; *trm1 = *trm2 + 1 + *trm1; } else if (s2[1] == < ) { *uni += s2[2]; *trm2 = *trm1 + 1 + *trm2; } if (s3[0] == A ) trm1 = &a; else if (s3[0] == B ) trm1 = &b; else if (s3[0] == C ) trm1 = &c; if (s3[2] == A ) trm2 = &a; else if (s3[2] == B ) trm2 = &b; else if (s3[2] == C ) trm2 = &c; if (s3[1] == > ) { *uni += s3[0]; *trm1 = *trm2 + 1 + *trm1; } else if (s3[1] == < ) { *uni += s3[2]; *trm2 = *trm1 + 1 + *trm2; } long long unicnt = 0; long long repcnt = 0; for (long long i = 0; i < 3; i++) { if (i == 0) unicnt++; else { long long j; for (j = i - 1; j >= 0; j = j - 1) { if (unia[i] == unia[j]) { repcnt++; break; } } if (j < 0) unicnt++; } } if (repcnt != 1) { cout << Impossible << endl; } else { vector<pair<long long, char>> m; m.push_back(make_pair(a, A )); m.push_back(make_pair(b, B )); m.push_back(make_pair(c, C )); sort(m.begin(), m.end()); for (long long i = 0; i < m.size(); i++) { cout << m[i].second; } } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int N; ios_base::sync_with_stdio(0); cin >> N; string K; cin >> K; int zm = N; for (int i = 1; i < N; i++) { if (K[i] != K[i - 1]) { zm--; if (i < N - 1) K[i] = K[i + 1]; } } cout << zm; } |
#include <bits/stdc++.h> int mod = 1000000007; using namespace std; using namespace std; int n, k; int a[400005]; int pos[400005]; map<int, int> m; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); ; cin >> n >> k; for (int i = 1; i <= n; i++) pos[i] = 1e7; m[a[1]] = 1e7; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { pos[m[a[i]]] = i; m[a[i]] = i; } int ans = 0; set<int> s; set<pair<int, int> > ss; for (int i = 1; i <= n; i++) { if (!s.count(a[i])) { if (s.size() < k) { ans++; s.insert(a[i]); ss.insert(make_pair(pos[i], a[i])); } else { ans++; set<pair<int, int> >::iterator it = ss.end(); it--; int val = (*it).second; ss.erase(it); s.erase((val)); s.insert(a[i]); ss.insert(make_pair(pos[i], a[i])); } } else { ss.erase(ss.begin()); ss.insert(make_pair(pos[i], a[i])); } } cout << ans; } |
#include <bits/stdc++.h> using namespace std; int arr[11][11]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { arr[1][i] = 1; } for (int i = 2; i <= n; i++) { for (int j = 1; j <= n; j++) { arr[i][j] = arr[i - 1][j] + arr[i][j - 1]; } } cout << arr[n][n] << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int i, j, n, m, k; int h; while (scanf( %d%d%d , &n, &m, &h) != EOF) { int sum1, sum2; sum1 = sum2 = 0; for (i = 1; i <= m; i++) { scanf( %d , &k); if (i == h) sum1 = k; else sum2 += k; } k = n - 1; if (sum1 + sum2 < n) puts( -1 ); else if (sum2 < k - 1) puts( 1 ); else { k = n - 1; h = sum2; double ans = 1; for (i = 1; i <= k; i++) { ans *= h; h--; } h = sum2 + sum1 - 1; for (i = 1; i <= k; i++) { ans /= h; h--; } printf( %.7lf n , 1 - ans); } } return 0; } |
#include <bits/stdc++.h> using namespace std; int main(void) { int n, m, c; cin >> n >> m >> c; vector<int> a(n), b(m); for (int i = 0; i < n; i++) cin >> a[i]; for (int j = 0; j < m; j++) cin >> b[j]; vector<int> beg(n); beg.assign(n, 0); vector<int> start(n), end(n); start.assign(n, 0); end.assign(n, 0); int max; if (n >= 2 * m) { for (int i = 0; i < m; ++i) { beg[i] = i + 1; start[i] = 0; end[i] = start[i] + beg[i]; } for (int i = m; i < n - m + 1; ++i) { beg[i] = m; start[i] = 0; end[i] = start[i] + beg[i]; } for (int i = n - m + 1; i < n; ++i) { beg[i] = n - i; start[i] = start[i - 1] + 1; end[i] = start[i] + beg[i]; } } else { for (int i = 0; i < n - m + 1; ++i) { beg[i] = i + 1; start[i] = 0; end[i] = start[i] + beg[i]; } for (int i = n - m + 1; i < m; ++i) { beg[i] = n - m + 1; start[i] = start[i - 1] + 1; end[i] = start[i] + beg[i]; } for (int i = m; i < n; ++i) { beg[i] = n - i; start[i] = start[i - 1] + 1; end[i] = start[i] + beg[i]; } } for (int i = 0; i < n; ++i) { for (int j = start[i]; j < end[i]; ++j) a[i] = (a[i] + b[j]) % c; cout << a[i] << ; } return 0; } |
#include <bits/stdc++.h> int main() { int a[100]; int e, n, i, j; scanf( %d , &n); for (i = 0; i < n; i++) { a[i] = 0; } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { scanf( %d , &e); if (i >= j) { continue; } a[i] |= e; a[j] |= e; } } for (i = 0; i < n; i++) { printf( %d , a[i]); } return 0; } |
#include <bits/stdc++.h> #define fo(a,b,c) for (a=b; a<=c; a++) #define fd(a,b,c) for (a=b; a>=c; a--) #define fu(x) (((x)&1)?-1:1) #define add(a,b) a=((a)+(b))%mod #define min(a,b) (a<b?a:b) #define max(a,b) (a>b?a:b) #define mod 998244353 #define Mod 998244351 #define ll long long //#define file using namespace std; int n,K,i,j,k,l,ii; int a[51]; ll C[2001][2001],jc[2001],Jc[2001]; ll f[51][2001][51]; ll ans,p0,p1,Len,LEN,L; ll qpower(ll a,int b) {ll ans=1; while (b) {if (b&1) ans=ans*a%mod;a=a*a%mod;b>>=1;} return ans;} ll E(ll p,ll a,ll b) {return p*qpower(qpower(1-b,a+1),Mod)%mod;} //p*x^a*e^b int main() { #ifdef file freopen( CF1477F.in , r ,stdin); #endif scanf( %d%d ,&n,&K); fo(i,1,n) scanf( %d ,&a[i]),Len+=a[i];LEN=qpower(Len,Mod); C[0][0]=1; fo(i,1,2000) { C[i][0]=C[i][i]=1; fo(j,1,i-1) C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod; } jc[0]=1; fo(i,1,2000) jc[i]=jc[i-1]*i%mod; Jc[2000]=qpower(jc[2000],Mod); fd(i,2000-1,0) Jc[i]=Jc[i+1]*(i+1)%mod; f[0][0][0]=1; fo(l,1,n) { L=a[l],ii=(L-1)/K; fo(i,0,ii) { p0=qpower((L-i*K)*LEN%mod,i)*fu(i)*Jc[i]%mod; if (i) p1=qpower((L-i*K)*LEN%mod,i-1)*fu(i)*Jc[i-1]%mod; fo(j,0,Len) { fo(k,0,l-1) if (f[l-1][j][k]) { add(f[l][j+i][k],f[l-1][j][k]*p0); if (i) add(f[l][j+i][k+1],f[l-1][j][k]*p1); } } } } fo(j,0,Len) fo(k,0,n) f[n][j][k]=-f[n][j][k]; add(f[n][0][0],1); assert(!f[n][0][0]); fo(j,0,Len) fo(k,0,n) if (f[n][j][k]) add(ans,E(f[n][j][k],j-k,(Len-j*K)*LEN%mod)*jc[j-k]); printf( %lld n ,(ans+mod)%mod); fclose(stdin); fclose(stdout); return 0; } |
#include <bits/stdc++.h> using namespace std; const long long int MOD = 1e9 + 7; const long long int INF = 2e15; const long long int MAXN = 2e5 + 5; long long int fastexpo(long long int b, long long int exp) { if (exp == 0) return 1; if (exp == 1) return b; long long int ans = (fastexpo(b, exp / 2) % MOD); ans *= ans; ans %= MOD; if (exp % 2 == 1) { ans *= b; } ans %= MOD; return ans; } long long int n; vector<long long int> a; long long int dp[MAXN][4]; long long int solve(long long int idx, vector<long long int> nice, long long int which) { if (idx < 0) { return 0; } if (nice.size() == 1) { return 0; } vector<long long int> o, z; for (long long int i = 0; i < nice.size(); i++) { if ((1LL << idx) & nice[i]) { o.push_back(nice[i]); } else { z.push_back(nice[i]); } } long long int lo = o.size(); long long int lz = z.size(); if (min(lo, lz) <= 1) { if (max(lo, lz) <= 1) { return 0; } else { if (lo <= 1) { return solve(idx - 1, z, 0); } else { return solve(idx - 1, o, 1); } } } return dp[idx][which] = min(solve(idx - 1, o, 1) + max((lz - 1), (long long int)0), solve(idx - 1, z, 0) + max((lo - 1), (long long int)0)); } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; memset(dp, -1, sizeof(dp)); ; long long int mx = 0; for (long long int i = 0; i < n; i++) { long long int x; cin >> x; a.push_back(x); mx = max(mx, (long long int)log2(x)); } cout << solve(mx, a, 2) << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; long long tg, ans[22]; int state[22][22], fa_inc[22][22], fa_n4[22][22], fa_n7[22][22]; int t, cs = 0, dg[22]; int main() { scanf( %d , &t); for (cs = 1; cs <= t; ++cs) { scanf( %I64d , &tg); for (int i = 0; i < 19; ++i) dg[i] = tg % 10, tg /= 10; state[0][0] = cs; for (int i = 0; i < 19; ++i) { for (int j = 0; j < 10; ++j) { if (state[i][j] != cs) continue; for (int n4 = 0; n4 <= 6; ++n4) { for (int n7 = 0; n7 + n4 <= 6; ++n7) { int nj = (j + n4 * 4 + n7 * 7); if (nj % 10 == dg[i]) { state[i + 1][nj / 10] = cs; fa_inc[i + 1][nj / 10] = j; fa_n4[i + 1][nj / 10] = n4; fa_n7[i + 1][nj / 10] = n7; } } } } } if (state[19][0] == cs) { for (int i = 0; i < 6; ++i) ans[i] = 0; for (int i = 19, j = 0; i > 0; --i) { int n4 = fa_n4[i][j], n7 = fa_n7[i][j]; for (int pos = 0; pos < 6; ++pos) { ans[pos] *= 10; if (pos < n4) ans[pos] += 4; else if (pos < n4 + n7) ans[pos] += 7; } j = fa_inc[i][j]; } for (int i = 0; i < 5; ++i) printf( %I64d , ans[i]); printf( %I64d n , ans[5]); } else puts( -1 ); } return 0; } |
#include <bits/stdc++.h> using namespace std; long long dp[200005][4]; vector<int> v[200005]; int mod = 998244353; void dfs(int first, int f) { dp[first][0] = 1; int visf = 0; for (auto it : v[first]) { if (it != f) { dfs(it, first); if (visf) { dp[first][1] *= (dp[it][0] + dp[it][2] + dp[it][1]); dp[first][1] %= mod; dp[first][2] = (dp[first][2] * (dp[it][0] + dp[it][1] + dp[it][2]) + dp[first][0] * (dp[it][2] + dp[it][0])) % mod; dp[first][3] *= (dp[it][0] + dp[it][1] + dp[it][2]); dp[first][3] %= mod; } else { dp[first][1] = (dp[first][1] * (dp[it][0] + dp[it][1] + dp[it][2]) + dp[first][0] * (dp[it][2] + dp[it][0])) % mod; } dp[first][0] *= (dp[it][1] + dp[it][3]); dp[first][0] %= mod; } else { visf = 1; dp[first][3] = dp[first][0]; } } } int main() { int n; scanf( %d , &n); for (int i = 1; i < n; i++) { int first, second; scanf( %d %d , &first, &second); v[first].push_back(second); v[second].push_back(first); } dfs(1, 0); printf( %lld n , (dp[1][0] + dp[1][1] + dp[1][2] + dp[1][3]) % mod); } |
#include <bits/stdc++.h> inline int two(int n) { return 1 << n; } inline int test(int n, int b) { return (n >> b) & 1; } inline void set_bit(int& n, int b) { n |= two(b); } inline void unset_bit(int& n, int b) { n &= ~two(b); } inline int last_bit(int n) { return n & (-n); } inline int ones(int n) { int res = 0; while (n && ++res) n -= n & (-n); return res; } long long int gcd(long long int a, long long int b) { return (a ? gcd(b % a, a) : b); } long long int modPow(long long int a, long long int b, long long int MOD) { long long int x = 1, y = a; while (b > 0) { if (b % 2 == 1) { x = (x * y) % MOD; } b /= 2; y = (y * y) % MOD; } return x; } long long int modInverse(long long int a, long long int p) { return modPow(a, p - 2, p); } using namespace std; int A[26]; int marked[26]; bool check(int ex) { int i, ctr; ctr = 0; for (i = 0; i < (26); i++) { if (A[i] == ex) ctr++; } if (ctr == 1) return true; else return false; } int main() { int n, i, j, k; cin >> n; char ch; int inf = -999999; string str; int ans, ex, f; ans = ex = f = 0; for (i = (1); i <= (n); i++) { cin >> ch >> str; int len = str.size(); if (f) { if (i == n) continue; if (ch == ! || ch == ? ) ans++; } if (ch == ! ) { memset(marked, 0, sizeof(marked)); ex++; for (j = 0; j < (len); j++) { if (A[str[j] - a ] != inf && !marked[str[j] - a ]) A[str[j] - a ]++; marked[str[j] - a ]++; } } else if (ch == . ) { for (j = 0; j < (len); j++) A[str[j] - a ] = inf; } else A[str[0] - a ] = inf; if (check(ex) && !f) f++; } cout << ans; } |
#include <bits/stdc++.h> using namespace std; int v1, v2, v3, vm; inline long long readll() { int sign = 1; char c; for (c = getchar(); !isdigit(c); c = getchar()) if (c == - ) sign = -sign; long long res = c - 0 ; for (c = getchar(); isdigit(c); c = getchar()) res = res * 10 + c - 0 ; return sign * res; } void writell(long long x) { if (x < 0) { putchar( - ); x = -x; } if (x > 9) writell(x / 10); putchar(x % 10 + 0 ); } void inp() { cin >> v1 >> v2 >> v3 >> vm; } void out() { if (vm < v2 && vm <= 2 * v3 && v3 <= 2 * vm) { writell(v1 * 2); putchar( n ); writell(v2 * 2); putchar( n ); writell(min(v3 * 2, vm * 2)); } else { putchar( - ); putchar( 1 ); } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); inp(); out(); return 0; } |
#include <bits/stdc++.h> using namespace std; int x[105]; int main() { int n, p, q; cin >> n >> p; for (int i = 0; i < p; ++i) cin >> x[i]; cin >> q; for (int i = p; i < p + q; ++i) cin >> x[i]; sort(x, x + p + q); int k = 0; for (int i = 0; i < p + q; ++i) { if (x[i] == k + 1) ++k; } if (k == n) cout << I become the guy. << endl; else cout << Oh, my keyboard! << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 1505; int n, k; map<tuple<int, int, int>, bool> memo; bool query(int a, int b, int c) { if (b == a || b == c) return 1; if (a > c) swap(a, c); auto s = make_tuple(a, b, c); if (memo.count(s)) return memo[s]; cout << ? << a + 1 << << b + 1 << << c + 1 << endl; cout.flush(); string ans; cin >> ans; return memo[s] = (ans[0] == Y ); } bool alive[N], done; int GOAL; int closer(int u, int v) { for (int w = 0; w < int(n); w++) if (alive[w] && w != u && w != v && query(u, w, v)) v = w; return v; } int iter(int u) { vector<int> nodes; for (int w = 0; w < int(n); w++) if (w != u && alive[w]) nodes.push_back(w); int sz = nodes.size(); auto w = nodes[rand() % sz]; w = closer(u, w); vector<int> u_friends, w_friends; u_friends.push_back(u); w_friends.push_back(w); for (auto v : nodes) if (v != w) { if (query(u, w, v)) w_friends.push_back(v); else u_friends.push_back(u); } int ans; if (u_friends.size() > w_friends.size()) { ans = u; if (u_friends.size() == GOAL) done = 1; for (auto v : w_friends) alive[v] = 0; } else { ans = w; if (w_friends.size() == GOAL) done = 1; for (auto v : u_friends) alive[v] = 0; } return ans; } int main() { srand(time(0)); cin >> n >> k; fill_n(alive, n, 1); GOAL = 1; while (GOAL * k <= n) GOAL *= k; int u = 0; while (!done) u = iter(u); cout << ! << u + 1 << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); long long x, y, m; cin >> x >> y >> m; if (x <= 0 && y <= 0) { if (max(x, y) >= m) cout << 0 ; else cout << -1 ; return 0; } if (x > y) swap(x, y); long long odp = 0; if (x < 0 && y > 0) { if (y < m) { long long ile = abs(x) / y; odp += ile; x += ile * y; } } while (max(x, y) < m) { if (x < y) x += y; else y += x; ++odp; } cout << odp; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a; int sum = 0; for (int i = 0; i < n; i++) { cin >> a; sum += a; } cout << (sum % n == 0 ? n : n - 1) << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, d, h; cin >> n >> d >> h; if (d > 2 * h || d < h) { cout << -1 << endl; return 0; } if (d == 1 && h == 1 && n != 2) { cout << -1 << endl; return 0; } vector<pair<int, int> > p; for (int i = 0; i < h; i++) { p.push_back(pair<int, int>(i + 1, i + 2)); } if (d - h > 0) { p.push_back(pair<int, int>(1, h + 2)); for (int i = 1; i < d - h; i++) { p.push_back(pair<int, int>(h + 1 + i, h + 2 + i)); } } if (d + 1 > n) { cout << -1 << endl; return 0; } if (h != 1) { for (int i = d + 2; i <= n; i++) { p.push_back(pair<int, int>(2, i)); } } else { for (int i = d + 2; i <= n; i++) { p.push_back(pair<int, int>(1, i)); } } for (int i = 0; i < p.size(); i++) { cout << p[i].first << << p[i].second << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int x1, x2, x3; int Y1, y2, y3; int main() { cin >> x1 >> Y1 >> x2 >> y2; x3 = (x1 <= x2 ? x2 + 1 : x2 - 1); y3 = (Y1 <= y2 ? y2 + 1 : y2 - 1); int r = 0; if (x1 == x2 || Y1 == y2) r = 2; cout << r + 2 * abs(x1 - x3) + 2 * abs(Y1 - y3); return 0; } |
#include <bits/stdc++.h> using namespace std; long long n, a[569], sq[250069], zs, inf = 1e18; void cyc(long long x) { swap(a[x + 1], a[x + 2]); swap(a[x], a[x + 1]); zs++; sq[zs] = x; } int main() { long long t, rr, i, j, ii, e; pair<long long, long long> mne; scanf( %lld , &t); for (rr = 0; rr < t; rr++) { scanf( %lld , &n); for (i = 1; i <= n; i++) { scanf( %lld , a + i); } zs = 0; for (i = 1; i <= n - 2; i++) { mne = {inf, -1}; for (j = i; j <= n; j++) { mne = min(mne, {a[j], j}); } e = mne.second; for (j = e - 2; j >= i; j -= 2) { cyc(j); } if (j == i - 1) { for (ii = 0; ii < 2; ii++) { cyc(i); } } } if (a[n - 1] > a[n]) { for (i = n - 2; i; i--) { cyc(i); if (a[i] <= a[i + 1]) { break; } } if (!i) { zs = -1; } } printf( %lld n , zs); for (i = 1; i <= zs; i++) { printf( %lld%c , sq[i], n [i == zs]); } } } |
#include <bits/stdc++.h> using namespace std; const int maxN = 1e5 + 1; struct Node { int t, x, l, c; } pnt[maxN]; long long t, pos[maxN]; int m, n; int solve(long long t) { int l, r, m; while (t > 0) { l = 1, r = n + 1; while (l + 1 < r) { m = (l + r) >> 1; if (pos[m] < t) l = m; else r = m; } if (pnt[l].t == 1) return pnt[l].x; t = ((t - pos[l] - 1) % pnt[l].l) + 1; } } int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %d , &pnt[i].t); if (pnt[i].t == 1) { scanf( %d , &pnt[i].x); pos[i + 1] = pos[i] + 1; } else { scanf( %d%d , &pnt[i].l, &pnt[i].c); pos[i + 1] = pos[i] + pnt[i].l * pnt[i].c; } } scanf( %d , &m); for (int i = 1; i <= m; i++) { scanf( %I64d , &t); printf( %d , solve(t)); if (i < m) printf( ); else printf( n ); } } |
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) #pragma GCC target( avx,avx2,fma ) using namespace std; const long double pi = acos(-1); const long long int mod = 1e9 + 7; bool debug = false; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long int t = 1, n, i, j, k, len, x, y, z, c, f, flag, p, q, mx, mn, l, r, sum, ans, tmp, it, pos, avg, m, cnt; string s; char ch; vector<long long int> v; vector<pair<long long int, long long int>> vec; unordered_map<long long int, long long int> mappu; pair<long long int, long long int> pr; { f = 0; sum = 0; flag = 0; ans = 0; cnt = 0; v.clear(); mappu.clear(); vec.clear(); string s2; cin >> s >> s2; if (s == s2) cout << s; else cout << 1; } return 0; } |
#include <bits/stdc++.h> using namespace std; template <class T> void chmin(T& a, const T& b) { if (a > b) a = b; } template <class T> void chmax(T& a, const T& b) { if (a < b) a = b; } tuple<long long, long long, long long> adj[400001]; long long cnt; long long nm; int main() { long long k, a, x, y, m, n; cin >> n; for (long long i = 1; i <= n; i++) { cin >> k >> a >> x >> y >> m; long long t = 0; for (long long j = 1; j <= k; j++, adj[nm++] = make_tuple(t, a, i), (a > ((a * x + y) % m) && j != k + 1) ? t++ : t, a = (a * x + y) % m) ; cnt = max(cnt, t); } cout << cnt << n ; sort(adj, adj + nm); for (long long i = 0; i < nm; i++) cout << get<1>(adj[i]) << << get<2>(adj[i]) << n ; } |
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 5; const int mod = 1e9 + 7; long long n, k, arr[MAX], ans = 0; int main(int argc, char** argv) { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; if (n == 1) { cout << 0; exit(0); } bool moded = true; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); long long modu = arr[0] % k; for (int i = 0; i < n; i++) if (arr[i] % k != modu) moded = false; if (moded) for (int i = 0; i < n; i++) ans += (arr[i] - arr[0]) / k; else ans = -1; cout << ans; return 0; } |
#include <bits/stdc++.h> using namespace std; const int Maxn = 100005; char str[Maxn]; int slen; int cnt[Maxn]; bool was[Maxn]; int Z[Maxn]; vector<pair<int, int> > res; int main() { scanf( %s , str); slen = strlen(str); cnt[slen]++; was[slen] = true; int L = -1, R = -1; for (int i = 1; i < slen; i++) { if (i <= R) Z[i] = min(R - i + 1, Z[i - L]); while (i + Z[i] < slen && str[Z[i]] == str[i + Z[i]]) Z[i]++; if (i + Z[i] - 1 > R) { L = i; R = i + Z[i] - 1; } if (i + Z[i] == slen) was[Z[i]] = true; cnt[Z[i]]++; } for (int i = slen - 1; i >= 0; i--) cnt[i] += cnt[i + 1]; for (int i = 1; i <= slen; i++) if (was[i]) res.push_back(pair<int, int>(i, cnt[i])); printf( %d n , res.size()); for (int i = 0; i < res.size(); i++) printf( %d %d n , res[i].first, res[i].second); return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m, a[1000000]; long long bank; vector<int> t; int main() { cin >> n >> m; for (int i = 0, k; i < n; i++) { cin >> k; int p = -1; bool q = false; for (int j = 0; j < k; j++) { cin >> a[j]; if (a[j] < m) p = 1; } if (p == true) { t.push_back(i); bank++; } } cout << bank << endl; for (int i = 0; i < t.size(); i++) cout << t[i] + 1 << ; } |
//In rememberance to GOD// //May GOD take care to me,my family and all the creatures// #include<bits/stdc++.h> using namespace std; #define ll long long #define int ll #define mod ((int)1e9+7) #define pii pair<int,int> #define vi vector<int> #define vvi vector<vi> #define vc vector<char> #define vpi vector<pii> #define vpic vector<pair<int,char>> #define vpid vector<pair<int,double>> #define INF (ll)1e15 #define all(x) (x).begin(),(x).end() #define allr(x) (x).rbegin(),(x).rend() #define pb push_back #define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) #define eb emplace_back #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), , , ); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string> it) {} template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << = << a << endl; err(++it, args...); } #define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr) #define time__(d) for ( auto blockTime = make_pair(chrono::high_resolution_clock::now(), true); blockTime.second; debug( %s: %lld ms n , d, chrono::duration_cast<chrono::milliseconds>(chrono::high_resolution_clock::now() - blockTime.first).count()), blockTime.second = false ) //fill memset iota function // use 1ll<<number(left shift) const int N=((int)1e6); // int primes[N]; // // int spf[N]; // vector<int> pr; // void sieve(){ // // iota(spf+2,spf+N,2); // for(ll i=2;i<N;i++){ // if(primes[i]==0){ // pr.eb(i*i); // for(ll j=i*i;j<N;j+=i){ // // if(spf[j]==j) // // spf[j]=i; // primes[j]=1; // } // } // primes[i]^=1; // } // } // nloglogn ll gcd(ll a, ll b) {if (b > a) {return gcd(b, a);} if (b == 0) {return a;} return gcd(b, a % b);} void swap(int &x, int &y) {int temp = x; x = y; y = temp;} ll mod_add(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;} ll mod_mul(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;} ll mod_sub(ll a, ll b, ll m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;} ll power(ll x, ll y,ll m) {ll res = 1;while (y > 0){ if (y & 1) res = (res*x)%m;y = y>>1;x = x*x%m;}return res;} //avoid it in power of 2. //logn implementation //divisors in o(sqrt(n)) and no of divisors less than 2*(n^1/2);//128(max divisors for n<=1e5) //for n>=1e5 no of divisors is less than n^1/3; // use i&1 instead of i%2 //stoll convert string to integer(long long) // if(a>b) { return a/b+fun(b,a%b);} else return b/a+fun(a,b%a); technique of mod and minus similar answer but diffent time complexity. // if(a>b) { return 1+fun(b,a-b);} else return 1+fun(a,b-a); // class cmp { public: // bool operator()(const pair<int,pii> &p1,const pair<int,pii> &p2) const { if(p1.first!=p2.first) return p1.first>p2.first;else // { // if(p1.second.first!=p2.second.first) // { // return p1.second.first>p2.second.first; // } // else // { // return p1.second.second>p2.second.second; // } // } // } // }; // instead of priority queue i use set defined with cmp class strictly using = sign in comparisons(work as multi set) //erase function with values won t work //to modify a set i use cmp class stictly without using = sign in comparions,example above!!!! //erase function with values will work /////////////////////////////////////// CODE //////////////////////////////////////////////// // int dx[] = {1,0,-1,0}; // int dy[] = {0,1,0,-1}; // vi adj[N]; // int visited[N]; // int par[N]; // int d[N]; // int binary_searching(int a,int y) // { // int start=-1; // int end=a; // while(start+1!=end) // { // int mid=(start+end)/2; // if(mid>(a-y)) // { // end=mid; // } // else // { // start=mid; // } // } // return end; // } bool palin(string s) { int n=s.size(); for(int i=0;i<n/2;i++) { if(s[i]!=s[n-i-1]) { return false; } } return true; } void solve() { string s; cin>>s; bool flag=false; for(int i=0;i<s.size();i++) { if(s[i]!= a ) { flag=true; break; } } if(!flag) { cout<< NO << n ; return; } //appling to first; string temp; // temp=s; temp= a +s; if(!palin(temp)) { cout<< YES << n ; cout<<temp<< n ; return; } temp=s+ a ; if(!palin(temp)) { cout<< YES << n ; cout<<temp<< n ; } } int32_t main() { #ifndef ONLINE_JUDGE freopen( input.txt , r , stdin); freopen( output.txt , w , stdout); #endif ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin>>t; // sieve(); // t=1; for(int i=1;i<=t;i++) { solve(); // time__( solve ); } return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m, q; int p[200001]; int a[200001]; vector<int> cur[200001]; int pos[200001]; int par[200001][21]; struct cmp { bool operator()(pair<int, int> a, pair<int, int> b) { return a.second > b.second; } }; priority_queue<pair<int, int>, vector<pair<int, int> >, cmp> h; struct Q { int l, r, id; }; vector<Q> Query; bool cmp(Q a, Q b) { return a.l < b.l; } int ans[200001]; int Process(int pos) { int k = n - 1; for (int i = 20; i >= 0; i--) { if (k & (1 << i)) { if (par[pos][i] == -1) return -1; pos = par[pos][i]; } } return pos; } int main() { cin >> n >> m >> q; for (int i = 1; i <= n; i++) { cin >> p[i]; pos[p[i]] = i; } for (int i = 1; i <= m; i++) { cin >> a[i]; } memset(par, -1, sizeof par); for (int i = 1; i <= m; i++) { int x = pos[a[i]]; int y; if (x == 1) y = p[n]; else y = p[x - 1]; for (auto k : cur[y]) { par[k][0] = i; } cur[y].clear(); cur[a[i]].push_back(i); } for (int j = 1; j <= 20; j++) { for (int i = 1; i <= m; i++) { if (par[i][j - 1] != -1) par[i][j] = par[par[i][j - 1]][j - 1]; } } for (int i = 1; i <= q; i++) { int l, r; cin >> l >> r; Query.push_back({l, r, i}); } sort(Query.begin(), Query.end(), cmp); for (int i = 1; i <= m; i++) { int r = Process(i); if (r != -1) h.push({i, r}); } for (auto x : Query) { int id = x.id; while (!h.empty() && h.top().first < x.l) h.pop(); if (h.empty() || h.top().second > x.r) ans[id] = 0; else ans[id] = 1; } for (int i = 1; i <= q; i++) cout << ans[i]; return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 105; vector<int> adj[N]; int a[N], d[N], din[N]; int main() { int n; scanf( %d , &n); for (int i = 0; i < n; i++) scanf( %d %d , a + i, d + i); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) continue; if (d[i] < 0) { if (make_pair(a[i], -i) > make_pair(a[j], -j) && make_pair(a[j], -j) > make_pair(a[i] + d[i], -i) && make_pair(a[i] + d[i], -i) > make_pair(a[j] + d[j], -j)) adj[i].push_back(j); } else { if (make_pair(a[i], -i) < make_pair(a[j], -j) && make_pair(a[j], -j) < make_pair(a[i] + d[i], -i) && make_pair(a[i] + d[i], -i) < make_pair(a[j] + d[j], -j)) adj[i].push_back(j); } } } memset(din, 0, sizeof din); for (int i = 0; i < n; i++) { for (int u : adj[i]) din[u]++; } stack<int> st; for (int i = 0; i < n; i++) if (din[i] == 0) { st.push(i); } vector<int> order; while (!st.empty()) { int u = st.top(); st.pop(); order.push_back(u); for (int v : adj[u]) { din[v]--; if (din[v] == 0) st.push(v); } } int ans = 0; for (int i = 0; i < n; i++) { int cnt1, cnt2; cnt1 = cnt2 = 0; for (int j = 0; j < n; j++) { if (j == order[i]) continue; cnt1 += (make_pair(a[j], -j) < make_pair(a[order[i]], -order[i])); cnt2 += (make_pair(a[j], -j) < make_pair(a[order[i]] + d[order[i]], -order[i])); } ans += abs(cnt1 - cnt2); a[order[i]] += d[order[i]]; } printf( %d n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; int arr[100005]; long long sum[100005]; map<int, long long> f; long long solve(int n, int k) { if (f.count(k)) return f[k]; long long ret = 0; long long cur = n - 2; long long p = k; while (cur >= 0) { ret += sum[cur]; cur -= p; p *= k; } return f[k] = ret; } int main() { int n; scanf( %d , &n); for (int i = 0; i < n; ++i) scanf( %d , &arr[i]); sort(arr, arr + n); sum[0] = arr[0]; for (int i = 1; i < n; ++i) sum[i] = sum[i - 1] + arr[i]; int q, a; scanf( %d , &q); for (int i = 0; i < q; ++i) { scanf( %d , &a); if (i) printf( ); printf( %I64d , solve(n, a)); } printf( n ); return 0; } |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.