solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; using ll = long long; using datas = pair<ll, ll>; using ddatas = pair<long double, long double>; using tdata = pair<ll, datas>; using vec = vector<ll>; using mat = vector<vec>; using pvec = vector<datas>; using pmat = vector<pvec>; const ll mod = 1000000007; const ll inf = 1LL << 60; const long double PI = acos(-1); const long double eps = 1e-9; template <class T> inline bool chmax(T& a, T b) { bool x = a < b; if (x) a = b; return x; } template <class T> inline bool chmin(T& a, T b) { bool x = a > b; if (x) a = b; return x; } void startupcpp() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } int main() { startupcpp(); ll i, j = 0, N, ok, ng, mid; cin >> N; string s; cin >> s; vec ans; mat g; for (i = 0; i < (ll)N; ++i) { ok = j; ng = -1; while (ok - ng > 1) { mid = (ok + ng) >> 1; if (g[mid].back() <= s[i]) { ok = mid; } else { ng = mid; } } ans.emplace_back(ok + 1); if (ok == j) { ++j; g.emplace_back(1, s[i]); } else { g[ok].emplace_back(s[i]); } } cout << g.size() << "\n"; do { bool f = 0; for (auto outi : ans) { cout << (f ? " " : "") << outi; f = 1; } cout << "\n"; } while (0); }
6
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 7, M = 1e9 + 7; long long h[N], g[N]; long long sg[N], sig[N], siig[N]; int lp[N + 1]; vector<int> pr; void sieve() { for (int i = 2; i <= N; ++i) { if (lp[i] == 0) { lp[i] = i; pr.push_back(i); } for (int j = 0; j < (int)pr.size() && pr[j] <= lp[i] && i * pr[j] <= N; ++j) lp[i * pr[j]] = pr[j]; } h[1] = 1; for (int i = 2; i <= N; i++) { int x = i, y = 1, e = 0; while (x % lp[i] == 0) { x /= lp[i]; y *= lp[i]; e++; } if (x > 1) h[i] = h[x] * h[y]; else h[i] = (e + 1) * i - e * i / lp[i]; } } int main() { sieve(); for (long long i = 1; i < N; i++) { g[i] = i * (2 * i * i - 6 * i + 1) / 3 + 2 * h[i]; g[i] %= M; sg[i] = (sg[i - 1] + g[i]) % M; sig[i] = (sig[i - 1] + i * g[i]) % M; long long ii = (i * i) % M; siig[i] = (siig[i - 1] + ii * g[i]) % M; } int t; cin >> t; while (t--) { long long m, n; cin >> m >> n; if (n > m) swap(m, n); long long x = ((n + 1) * (m + 1)) % M; long long ans = (x * sg[n] - (m + n + 2) * sig[n] + siig[n]) % M; if (ans < 0) ans += M; cout << ans << endl; } }
10
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 7; const long double eps = 1e-7; const string alpha = "abcdefghijklmnopqrstuvwxyz"; int dx[8] = {+1, +1, +1, 0, 0, -1, -1, -1}; int dy[8] = {+1, 0, -1, +1, -1, +1, 0, -1}; long long power(long long x, long long y) { long long res = 1; x = x; while (y > 0) { if (y & 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } template <typename T> T gcd(T a, T mp) { if (a == 0) return mp; return gcd(mp % a, a); } template <typename T> T powm(T a, T mp, long long m) { T cnt = 1; while (mp > 0) { if (mp % 2 == 1) cnt = (cnt * a) % m; mp /= 2; a = (a * a) % m; } return cnt % m; } long long ncr(long long n, long long r) { long long res = 1; if (r > n) return 0; if (r > n - r) r = n - r; for (long long i = 0; i < r; i++) { res *= (n - i); res /= (i + 1); } return res; } long long sqr(long long x) { return x * x; } void prec() {} struct Hashs { vector<int> hashs; vector<int> pows; int P; int MOD; Hashs() {} Hashs(string &s, int P, int MOD) : P(P), MOD(MOD) { int n = s.size(); pows.resize(n + 1, 0); hashs.resize(n + 1, 0); pows[0] = 1; for (int i = n - 1; i >= 0; i--) { hashs[i] = (1LL * hashs[i + 1] * P + s[i] - 'a' + 1) % MOD; pows[n - i] = (1LL * pows[n - i - 1] * P) % MOD; } pows[n] = (1LL * pows[n - 1] * P) % MOD; } int get_hash(int l, int r) { int ans = hashs[l] + MOD - (1LL * hashs[r + 1] * pows[r - l + 1]) % MOD; ans %= MOD; return ans; } }; Hashs h1, h2; string s, t; void run() { cin >> s; int n = (long long)s.size(); t = s; reverse((t).begin(), (t).end()); h1 = Hashs(s, 31, 1000000009); h2 = Hashs(t, 31, 1000000009); long long dp[n + 5]; dp[0] = 1; long long ans = 1; for (long long i = (1); i <= (n - 1); ++i) { dp[i] = 0; if (h1.get_hash(0, i) == h2.get_hash(n - i - 1, n - 1) and (i + 1) & 1) dp[i] = dp[i / 2 - 1] + 1; else if (h1.get_hash(0, i) == h2.get_hash(n - i - 1, n - 1)) dp[i] = dp[i / 2] + 1; ans += dp[i]; } cout << ans; } int main() { cin.sync_with_stdio(0); cin.tie(0); cin.exceptions(cin.failbit); int tc = 1; prec(); for (long long i = 0; i < tc; ++i) run(); return 0; }
7
#include <bits/stdc++.h> using namespace std; int a[100000], b, k; void input() { cin >> b >> k; for (int i = 0; i < k; i++) { cin >> a[i]; a[i] = a[i] % 10; } } bool ans() { int s = 0, p = 1; for (int i = k - 1; i >= 0; i--) { s += a[i] * p; p *= b; } return (s % 2 == 0); } void output() { if (ans()) cout << "even"; else cout << "odd"; } int main() { input(); output(); }
0
#include <bits/stdc++.h> using namespace std; const long double pi = acos(-1); const long double eps = 1e-8; long long read() { char ch = getchar(); long long x = 0; int op = 1; for (; !isdigit(ch); ch = getchar()) if (ch == '-') op = -1; for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0'; return x * op; } void write(long long a) { if (a < 0) putchar('-'), a = -a; if (a >= 10) write(a / 10); putchar(a % 10 + '0'); } long long ksm(long long x, long long p) { long long ret = 1; for (; p; p >>= 1, x = x * x) if (p & 1) ret = ret * x; return ret; } int n, d[1005], ans; int main() { n = read(); for (int i = (1); i <= (n - 1); i++) { int x = read(), y = read(); d[x]++; d[y]++; } for (int i = (1); i <= (n); i++) if (d[i] == 1) ans++; cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; priority_queue<long long, vector<long long> > ex; priority_queue<long long, vector<long long>, greater<long long> > in; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; long long ans[2 * n]; map<long long, long long> m; long long x; for (int i = 0; i < n; i++) cin >> x, m[x] = i + 1, in.push(x); string str; cin >> str; for (int i = 0; i < 2 * n; i++) { if (str[i] == '1') { long long temp = ex.top(); ex.pop(); ans[i] = m[temp]; } else { long long temp = in.top(); in.pop(); ans[i] = m[temp]; ex.push(temp); } } for (int i = 0; i < 2 * n; i++) cout << ans[i] << " "; return 0; } long long choose(long long n, long long k) { if (k == 0) return 1; return (n * choose(n - 1, k - 1)) / k; } bool isprime(long long n) { for (long long i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } int power(int x, int y, int p) { int res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; }
2
#include <bits/stdc++.h> using namespace std; long long dp[5005]; long long a[5005]; int l[100005]; int r[100005]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long i, j, n, x = 0, y = 0; cin >> n; for (i = 1; i <= n; i++) { cin >> a[i]; if (l[a[i]] == 0) l[a[i]] = i; r[a[i]] = i; } for (i = 1; i <= n; i++) { x = 0; y = n + 1; for (j = i; j >= 1; j--) { if (r[a[j]] > i) break; y = min(y, (long long)l[a[j]]); if (r[a[j]] == j) x = x ^ a[j]; if (y >= j) { dp[i] = max(dp[i], x + dp[j - 1]); } } dp[i] = max(dp[i], dp[i - 1]); } cout << dp[n] << "\n"; return 0; }
5
#include <bits/stdc++.h> using namespace std; using gg = long long; constexpr gg MAX = 405; constexpr gg mod = 1e9 + 7; constexpr gg INF = 4e16; constexpr double eps = 1e-12; gg ti, ni, mi, ki, di, pi, xi, yi; gg up(gg n, gg m) { return n >= 0 ? (n + m - 1) / m : n / m; } gg down(gg n, gg m) { return n >= 0 ? n / m : (n - m + 1) / m; } gg a[MAX][MAX], preSum[MAX][MAX]; gg pv[MAX]; void getPrefixSum(gg m, gg n) { for (gg i = 1; i <= m; ++i) { for (gg j = 1; j <= n; ++j) { preSum[i][j] = preSum[i - 1][j] + preSum[i][j - 1] - preSum[i - 1][j - 1] + a[i][j]; } } } gg getSum(gg r1, gg c1, gg r2, gg c2) { return preSum[r2][c2] - preSum[r1 - 1][c2] - preSum[r2][c1 - 1] + preSum[r1 - 1][c1 - 1]; } int main() { ios::sync_with_stdio(false); cin.tie(0); ti = 1; cin >> ti; while (ti--) { cin >> mi >> ni; string s; for (gg i = (1); i <= (mi); i += (1)) { cin >> s; for (gg j = (1); j <= (ni); j += (1)) { a[i][j] = s[j - 1] - '0'; } } getPrefixSum(mi, ni); gg ans = INF; for (gg x1 = (1); x1 <= (mi); x1 += (1)) { for (gg x2 = (x1 + 4); x2 <= (mi); x2 += (1)) { for (gg y = (1); y <= (ni); y += (1)) { pv[y] = getSum(x1 + 1, 1, x2 - 1, y) + 2 * y - getSum(x1, 1, x1, y) - getSum(x2, 1, x2, y); } gg m = INF, x = x2 - x1 - 1; for (gg y = (ni); y >= (4); y -= (1)) { m = min(pv[y - 1] + x - getSum(x1 + 1, y, x2 - 1, y), m); gg ty = y - 3; ans = min(ans, m - pv[ty] + x - getSum(x1 + 1, ty, x2 - 1, ty)); } } } cout << ans << "\n"; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, vet[1010], val[1010], aux[1010], ptr[1010], qt[1010], cnt0, ans, tt, apaga[1010]; int get(int v) { return lower_bound(aux, aux + n, v) - aux; } int main() { scanf("%d", &n); for (int(i) = (0); (i) < (n); (i)++) scanf("%d", vet + i), aux[i] = vet[i], cnt0 += !vet[i]; ans = cnt0; sort(aux, aux + n); for (int(i) = (0); (i) < (n); (i)++) vet[i] = get(vet[i]), qt[vet[i]]++; for (int(i) = (0); (i) < (n); (i)++) for (int(j) = (0); (j) < (n); (j)++) if (i != j && (aux[vet[i]] || aux[vet[j]])) { int cur = 2; int a = vet[i], b = vet[j]; apaga[tt++] = a; apaga[tt++] = b; ptr[a]++; ptr[b]++; while (1) { int c = get(aux[a] + aux[b]); if (c == n || aux[c] != aux[a] + aux[b] || ptr[c] == qt[c]) break; ptr[c]++; a = b; b = c; cur++; apaga[tt++] = c; } for (int(k) = (0); (k) < (tt); (k)++) ptr[apaga[k]] = 0; tt = 0; ans = max(ans, cur); } printf("%d\n", ans); }
6
#include <bits/stdc++.h> using namespace std; long long max(long long a, long long b) { if (a < b) return b; return a; } long long min(long long a, long long b) { if (a < b) return a; return b; } vector<long long> v; void make_number(long long four, long long seven, long long number) { if (number > 1e10) return; if (number) v.push_back(number); long long n = number * 10 + 4; make_number(four + 1, seven, n); n = number * 10 + 7; make_number(four, seven + 1, n); } long long getAnswer(long long n) { if (n == 0) return 0; if (n < 4) { return n * 4; } long long sum = 0; for (int i = 1; i < v.size(); i++) { long long start = v[i - 1]; long long end = min(v[i], n); long long value = v[i]; if (start > end) break; if (start == 1) { sum += (end)*value; } else sum = sum + (end - start) * value; } return sum; } void solve() { v.push_back(1); make_number(0, 0, 0); sort(v.begin(), v.end()); long long l, r; cin >> l >> r; long long a = getAnswer(r), b = getAnswer(l - 1); cout << a - b; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int Inf = 1e9 + 7; const int N = 5e3 + 7; const int M = 1e3 + 7; int n, m; struct Node { int x, y, op; Node() : x(0), y(0), op(0) {} Node(int x_, int y_, int op_) : x(x_), y(y_), op(op_) {} }; Node expr[N][M]; map<string, int> idx; pair<int, int> eval[N][M]; int get(int r, int c, int w) { return r == -1 ? w : (w == 0 ? eval[r][c].first : eval[r][c].second); } void solve() { cin >> n >> m; for (int i = 0; i < n; i++) { string a, b, c, d, e; cin >> a >> b >> c; if (c[0] != '1' && c[0] != '0') { cin >> d >> e; expr[i][0] = Node((c == "?" ? -1 : idx[c]), (e == "?" ? -1 : idx[e]), (d == "AND" ? 1 : (d == "OR" ? 2 : 3))); for (int j = 1; j < m; j++) expr[i][j] = expr[i][j - 1]; } else { for (int j = 0; j < m; j++) expr[i][j] = Node(c[j] - '0', 0, 0); } idx[a] = i; } vector<int> res1, res2; for (int j = 0; j < m; j++) { for (int i = 0; i < n; i++) { int op = expr[i][j].op; if (op == 0) { eval[i][j].first = expr[i][j].x; eval[i][j].second = expr[i][j].x; } else { vector<int> pv; for (int w = 0; w <= 1; w++) { int vx = get(expr[i][j].x, j, w); int vy = get(expr[i][j].y, j, w); int vz = 0; if (op == 1) vz = vx & vy; else if (op == 2) vz = vx | vy; else if (op == 3) vz = vx ^ vy; pv.push_back(vz); } eval[i][j].first = pv[0]; eval[i][j].second = pv[1]; } } int s1 = 0, s2 = 0; for (int i = 0; i < n; i++) { s1 += eval[i][j].first; s2 += eval[i][j].second; } if (s1 < s2) { res1.push_back(0); res2.push_back(1); } else if (s1 > s2) { res1.push_back(1); res2.push_back(0); } else { res1.push_back(0); res2.push_back(0); } } for (auto e : res1) cout << e; cout << '\n'; for (auto e : res2) cout << e; cout << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout.precision(9); cout << fixed; solve(); return 0; }
5
#include <bits/stdc++.h> using namespace std; vector<vector<int> > adj(100001); int a[100001][3], degree[100001]; map<pair<int, int>, int> M; set<pair<int, int> > S; vector<int> P, Q; void Reset(int n) { for (int i = 1; i <= n; ++i) { adj[i].clear(); degree[i] = 0; } M.clear(); S.clear(); P.clear(); Q.clear(); } void AddPolygonEdges(int x) { for (int i = 0; i < 3; ++i) { for (int j = i + 1; j < 3; ++j) { pair<int, int> p = make_pair(a[x][i], a[x][j]); if (S.count(p)) { S.erase(p); } else { S.insert(p); } } } } void PolygonVertexOrderDFS(int i, int p) { P.push_back(i); for (int j : adj[i]) { if (j == p) { continue; } PolygonVertexOrderDFS(j, i); } } int FindRoot(int n) { int root = 1; for (; adj[root].size() != 1 && root <= n; ++root) ; return root > n ? 1 : root; } void FillCutOrder(int n) { for (int i = 1; i <= n; ++i) { if (degree[i] == 1) { Q.push_back(i); } } if (Q.empty()) { Q.push_back(1); } for (int x = 0; x < Q.size(); ++x) { int i = Q[x]; AddPolygonEdges(i); for (int j : adj[i]) { if (--degree[j] == 1) { Q.push_back(j); } } } } void FillPolygonVertexOrder(int n) { for (int i = 1; i <= n; ++i) { adj[i].clear(); } S.erase(S.begin()); for (auto p : S) { int i, j; tie(i, j) = p; adj[i].push_back(j); adj[j].push_back(i); } int root = FindRoot(n); PolygonVertexOrderDFS(root, 0); } void Print(vector<int> &v) { for (int x : v) { printf("%d ", x); } printf("\n"); } int main() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); for (int x = 1; x <= n - 2; ++x) { for (int i = 0; i < 3; ++i) { scanf("%d", &a[x][i]); } for (int i = 0; i < 3; ++i) { for (int j = i + 1; j < 3; ++j) { if (a[x][j] < a[x][i]) { swap(a[x][j], a[x][i]); } } } for (int i = 0; i < 3; ++i) { for (int j = i + 1; j < 3; ++j) { pair<int, int> p = make_pair(a[x][i], a[x][j]); auto it = M.find(p); if (it != M.end()) { int y = it->second; adj[x].push_back(y); adj[y].push_back(x); ++degree[x], ++degree[y]; } else { M[p] = x; } } } } FillCutOrder(n); FillPolygonVertexOrder(n); Print(P); Print(Q); Reset(n); } return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { int a; cin >> a; if (a % 5 != 0) { cout << a / 5 + 1 << endl; } else { cout << a / 5 << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; int arr[n][4]; multiset<int> s[4]; for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { cin >> arr[i][j]; s[j].insert(arr[i][j]); } } for (int i = 0; i < n; i++) { for (int j = 0; j < 4; j++) { auto temp = s[j].find(arr[i][j]); s[j].erase(temp); } if (*s[0].rbegin() <= *s[2].begin() && *s[1].rbegin() <= *s[3].begin()) { cout << *s[0].rbegin() << " " << *s[1].rbegin() << endl; break; } for (int j = 0; j < 4; j++) s[j].insert(arr[i][j]); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 233; const int T = 998244353; long long f[maxn][101], sum[maxn][101]; int a[maxn], cnt[maxn], id[maxn]; int n, k, len; inline int cal(int x, int v) { if (x < len) return 0; bool ok = 0; if (cnt[x] == 0) ok = 1; if (cnt[x] == 1 && id[x] == v) ok = 1; return ok ? sum[x - len][v] : 0; } int main() { cin >> n >> k >> len; if (len == 1) { puts("0"); return 0; } int last = 1, v = -1; for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int j = 1; j <= k; j++) { int last = -(1 << 30); for (int i = 1; i <= n; i++) { if (a[i] == j) last = i; if (i - last + 1 <= len) { cnt[i]++; id[i] = j; } } } memset(f, 0, sizeof(f)); for (int i = 1; i <= k; i++) sum[0][i] = 1; if (a[1] != -1) { f[1][a[1]] = 1; } else { for (int i = 1; i <= k; i++) f[1][i] = 1; } long long tmp = 0; for (int i = 1; i <= k; i++) tmp += f[1][i]; for (int i = 1; i <= k; i++) sum[1][i] = tmp - f[1][i]; for (int t = 2; t <= n; t++) { long long tot = 0; for (int i = 1; i <= k; i++) tot += f[t - 1][i]; if (a[t] != -1) f[t][a[t]] = (tot - cal(t, a[t]) + T) % T; else { for (int i = 1; i <= k; i++) f[t][i] = (tot - cal(t, i) + T) % T; } tot = 0; for (int i = 1; i <= k; i++) tot += f[t][i]; for (int i = 1; i <= k; i++) sum[t][i] = (tot - f[t][i]) % T; } long long ans = 0; for (int i = 1; i <= k; i++) ans = (ans + f[n][i]) % T; cout << ans << endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; vector<int> moves; int main() { int n; string a, b; cin >> n; cin >> a >> b; for (int i = 0; i < n; i++) { int m = -1; for (int j = i; j < n; j++) { if (a[j] == b[i]) { m = j; break; } } for (int j = m; j > i; j--) { moves.push_back(j); swap(a[j], a[j - 1]); } if (m == -1) { cout << -1 << endl; return 0; } } int msz = moves.size(); cout << msz << endl; for (int k = 0; k < msz; k++) { cout << moves[k] << " "; } cout << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; long long powmod(long long a, long long b) { long long res = 1; a %= mod; for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } int n; vector<int> V, K, X; const int INF = 1e9 + 9; const int N = 77; int dp[N][N][N][2]; void mini(int& a, int b) { a = min(a, b); } int count_remaining(const vector<int>& list, int from, int limit) { int cnt = 0; for (int i = from; i < (int)list.size() && list[i] < limit; i++) { ++cnt; } return cnt; } int main() { ios ::sync_with_stdio(false); cin.tie(0); cout.precision(10); cout << fixed; int n; cin >> n; string s; cin >> s; for (int i = 0; i < n; i++) { if (s[i] == 'V') { V.push_back(i); } else if (s[i] == 'K') { K.push_back(i); } else { X.push_back(i); } } for (int a = 0; a <= n; a++) { for (int b = 0; b <= n; b++) { for (int c = 0; c <= n; c++) { for (int d = 0; d < 2; d++) { dp[a][b][c][d] = INF; } } } } dp[0][0][0][0] = 0; for (int v = 0; v <= (int)V.size(); v++) { for (int k = 0; k <= (int)K.size(); k++) { for (int x = 0; x <= (int)X.size(); x++) { for (int type = 0; type < 2; type++) { auto moving_cost = [&](int where) { return count_remaining(V, v, where) + count_remaining(K, k, where) + count_remaining(X, x, where); }; int already = dp[v][k][x][type]; if (v < (int)V.size()) { mini(dp[v + 1][k][x][1], already + moving_cost(V[v])); } if (k < (int)K.size() && type == 0) { mini(dp[v][k + 1][x][0], already + moving_cost(K[k])); } if (x < (int)X.size()) { mini(dp[v][k][x + 1][0], already + moving_cost(X[x])); } } } } } int ans = min(dp[V.size()][K.size()][X.size()][0], dp[V.size()][K.size()][X.size()][1]); cout << ans << "\n"; }
8
#include <bits/stdc++.h> using namespace std; double r; struct point { double x, y; }; double dis(point a, point b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } bool isValid(point a, point b, point c) { double A = dis(a, b), B = dis(b, c), C = dis(c, a), s = (A + B + C) / 2, area = sqrt(s * (s - A) * (s - B) * (s - C)); if (2 * area / A < r) return false; else return true; } int main() { double y1, y2, yw, xb, yb, xw, y, a, b, c, d; point A, B, C; while (cin >> y1 >> y2 >> yw >> xb >> yb >> r) { y = y1 + r; yw -= r; d = (xb * (yw - yb)) / (2 * yw - y - yb); xw = xb - d; A.x = xw; A.y = yw; B.x = 0; B.y = y; C.x = 0; C.y = y2; if (isValid(A, B, C)) printf("%.9lf\n", xw); else cout << -1 << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; long long dp[1005]; int main() { long long i, n; cin >> n; dp[0] = 1; dp[1] = 1; for (i = 2; i < 1005; i++) { dp[i] = dp[i - 1] + dp[i - 2]; } long long p = 1; for (i = 1; i <= n; i++) { if (dp[p] == i) { cout << "O"; p++; } else { cout << "o"; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, m, pos[2000001]; struct comp { bool operator()(const pair<int, int> &a, const pair<int, int> &b) const { int d1, d2; if (a.first == 1 || a.second == n) d1 = a.second - a.first; else d1 = (a.second - a.first) / 2; if (b.first == 1 || b.second == n) d2 = b.second - b.first; else d2 = (b.second - b.first) / 2; if (d1 != d2) return d1 > d2; else return a.first < b.first; } }; set<pair<int, int>, comp> st1; set<pair<int, int> > st2; int add() { pair<int, int> temp = *st1.begin(); st1.erase(temp); st2.erase(temp); int ans; if (temp.first == 1) ans = 1; else if (temp.second == n) ans = n; else ans = (temp.first + temp.second) / 2; if (temp.first != ans) { st1.insert({temp.first, ans - 1}); st2.insert({temp.first, ans - 1}); } if (temp.second != ans) { st1.insert({ans + 1, temp.second}); st2.insert({ans + 1, temp.second}); } return ans; } void rem(int pos) { set<pair<int, int> >::iterator lef, rig, t2; lef = st2.lower_bound({pos, pos}); t2 = lef; if (lef != st2.begin()) lef--; rig = st2.upper_bound({pos, pos}); vector<pair<int, int> > erase2; int ll, rr; if (t2 == st2.begin() || (*lef).second != pos - 1) { ll = pos; } else { st1.erase(*lef); erase2.push_back(*lef); ll = (*lef).first; } if (rig == st2.end() || (*rig).first != pos + 1) { rr = pos; } else { st1.erase(*rig); erase2.push_back(*rig); rr = (*rig).second; } st1.insert({ll, rr}); st2.insert({ll, rr}); for (int i = 0; i < erase2.size(); ++i) { st2.erase(erase2[i]); } } int main() { scanf("%d %d", &n, &m); st1.insert({1, n}); st2.insert({1, n}); while (m--) { int x, t; scanf("%d %d", &t, &x); if (t == 1) { pos[x] = add(); printf("%d\n", pos[x]); } else rem(pos[x]); } }
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n, i, m, j, k, h = 0; cin >> n; h = 2 * (pow(2, n) - 1); cout << h; return 0; }
1
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; a %= b; return gcd(b, a); } long long power(long long x, long long y, long long md = 1000000007) { long long res = 1; x %= md; while (y > 0) { if (y & 1) res = (res * x) % md; x = (x * x) % md; y = y >> 1; } return res % md; } bool sortbysec(const pair<long long, long long> &a, const pair<long long, long long> &b) { return (a.second < b.second); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n; cin >> n; long long arr[n]; for (long long i = 0; i < n; i++) cin >> arr[i]; long long en = 0, od = 0; for (long long i = 0; i < n; i++) { if (arr[i] % 2 == 0) en++; else od++; } long long ans = (en < od ? en : od); cout << ans << '\n'; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 100100; const int s = 30; int n, d, x; int a[maxn], b[maxn], q[maxn], to[maxn]; int getNextX() { x = (x * 37LL + 10007) % 1000000007; return x; } void initAB() { int i; for (i = 0; i < n; i = i + 1) { a[i] = i + 1; } for (i = 0; i < n; i = i + 1) { swap(a[i], a[getNextX() % (i + 1)]); } for (i = 0; i < n; i = i + 1) { if (i < d) b[i] = 1; else b[i] = 0; } for (i = 0; i < n; i = i + 1) { swap(b[i], b[getNextX() % (i + 1)]); } } int main() { cin >> n >> d >> x; initAB(); for (int i = 0; i < n; i++) to[a[i]] = i; for (int i = 0; i < n; i++) if (b[i]) q[++q[0]] = i; for (int i = 0; i < n; i++) { int j; for (j = n; j >= n - s; j--) { if (j > 0 && i >= to[j] && b[i - to[j]]) { printf("%d\n", j); break; } } if (j < n - s) { int Max = 0; for (j = 1; j <= q[0] && q[j] <= i; j++) { Max = max(Max, a[i - q[j]]); } printf("%d\n", Max); } } return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { int n, m, sum = 0; cin >> n >> m; char s[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> s[i][j]; for (int i = 0; i < n - 1; i++) for (int j = 0; j < m - 1; j++) if (s[i][j] == 'f' || s[i + 1][j] == 'f' || s[i][j + 1] == 'f' || s[i + 1][j + 1] == 'f') if (s[i][j] == 'a' || s[i + 1][j] == 'a' || s[i][j + 1] == 'a' || s[i + 1][j + 1] == 'a') if (s[i][j] == 'c' || s[i + 1][j] == 'c' || s[i][j + 1] == 'c' || s[i + 1][j + 1] == 'c') if (s[i][j] == 'e' || s[i + 1][j] == 'e' || s[i][j + 1] == 'e' || s[i + 1][j + 1] == 'e') sum++; cout << sum; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); char c[6][8]; int a[6][8] = {{3, 3, 0, 4, 4, 0, 3, 3}, {3, 3, 0, 4, 4, 0, 3, 3}, {2, 2, 0, 3, 3, 0, 2, 2}, {2, 2, 0, 3, 3, 0, 2, 2}, {1, 1, 0, 2, 2, 0, 1, 1}, {1, 1, 0, 2, 2, 0, 1, 1}}; int mx_u = -1; int mx_x, mx_y; for (int i = 0; i < 6; i++) for (int j = 0; j < 8; j++) { cin >> c[i][j]; if (c[i][j] == '.' && a[i][j] > mx_u) { mx_u = a[i][j]; mx_x = i; mx_y = j; } } for (int i = 0; i < 6; i++) { for (int j = 0; j < 8; j++) { if (i == mx_x && j == mx_y) cout << 'P'; else cout << c[i][j]; } cout << '\n'; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 1001; const int p = 998244353; int n, m; struct data { int x, y, c; } a[N * N]; int x, y; long long inv[N * N]; long long f[N * N]; long long s, sf, sx, sx2, sy, sy2; void read(int &x) { char ch = getchar(); x = 0; for (; ch < '0' || ch > '9'; ch = getchar()) ; for (; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 3) + (x << 1) + ch - '0'; } int cmp(const data &q, const data &w) { return q.c < w.c; } void Add(long long &x, long long y) { x += y; while (x < 0) x += p; while (x >= p) x -= p; } long long sqr(long long x) { return x * x; } int main() { read(n); read(m); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { read(x); a[(i - 1) * m + j] = (data){i, j, x}; } read(x); read(y); n *= m; sort(a + 1, a + 1 + n, cmp); inv[1] = 1; for (int i = 2; i <= n; i++) inv[i] = (-p / i * inv[p % i]) % p; a[n + 1].c = -1; for (int i = 1; i <= n; i++) { if (s) { Add(f[i], (s * sqr(a[i].x) % p - 2 * sx * a[i].x % p + sx2) % p); Add(f[i], (s * sqr(a[i].y) % p - 2 * sy * a[i].y % p + sy2) % p); Add(f[i], sf); f[i] = f[i] * inv[s] % p; } else f[i] = 0; if (a[i].x == x && a[i].y == y) { cout << (f[i] + p) % p << endl; exit(0); } if (a[i].c != a[i + 1].c) for (int j = i; j; j--) { if (a[j].c != a[i].c) break; Add(sf, f[j]); Add(sx, a[j].x); Add(sx2, sqr(a[j].x)); Add(sy, a[j].y); Add(sy2, sqr(a[j].y)); s++; } } return 0; }
7
#include <bits/stdc++.h> using namespace std; vector<long long int> g1[200001], g[200001]; long long int mini[200001], maxi[200001]; pair<pair<long long int, long long int>, pair<long long int, long long int> > ed[200001]; pair<long long int, long long int> ans[200001]; bool vis[200001]; bool flag = 1; void dfs(long long int x) { vis[x] = 1; mini[x] = 0; maxi[x] = 0; for (auto v : g[x]) { dfs(v); mini[x] += mini[v] + max((ed[v].second.first - (ed[v].second.second - mini[v])), 1ll); maxi[x] += maxi[v] + (ed[v].second.first); } maxi[x] = min(maxi[x], ed[x].second.second); if (mini[x] > maxi[x] || mini[x] > ed[x].second.second) flag = 0; } void dfs1(long long int x, long long int we) { long long int tmp = we; for (auto v : g[x]) { long long int diff = min(ed[v].second.first - 1, ed[v].second.second - mini[v]); ans[v] = {ed[v].second.first - diff, ed[v].second.second - diff}; tmp -= ans[v].first; tmp -= mini[v]; } for (auto v : g[x]) { long long int l = mini[v], r = maxi[v] + 1; while (r - l > 1) { long long int mid = (l + r) / 2; long long int diff = min(ed[v].second.first - 1, ed[v].second.second - mid); long long int k1 = ed[v].second.first - diff, k2 = ed[v].second.second - diff; long long int subs = (mid - mini[v]) + (k1 - ans[v].first); if (subs > tmp) r = mid; else l = mid; } long long int mid = l; long long int diff = min(ed[v].second.first - 1, ed[v].second.second - mid); long long int k1 = ed[v].second.first - diff, k2 = ed[v].second.second - diff; long long int subs = (mid - mini[v]) + (k1 - ans[v].first); tmp -= subs; ans[v].first = k1; ans[v].second = k2; dfs1(v, l); } if (tmp > 0) { for (auto v : g[x]) { if (ed[v].second.second > ans[v].second) { long long int x = min(tmp, ed[v].second.second - ans[v].second); ans[v].second += x; ans[v].first += x; tmp -= x; if (tmp == 0) break; } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n; cin >> n; for (int i = 0; i < n - 1; i++) { long long int u, v, w, p; cin >> u >> v >> w >> p; u--; v--; g1[u].push_back(i); ed[i] = {{u, v}, {w, p}}; } for (int i = 0; i < n - 1; i++) { for (auto x : g1[ed[i].first.second]) { g[i].push_back(x); } } for (int i = 0; i < n - 1; i++) { if (!vis[i]) { dfs(i); dfs1(i, maxi[i]); ans[i] = {ed[i].second.first, ed[i].second.second}; } } if (!flag) { cout << -1; exit(0); } cout << n << endl; for (int i = 0; i < n - 1; i++) { cout << ed[i].first.first + 1 << " " << ed[i].first.second + 1 << " " << ans[i].first << " " << ans[i].second << endl; } }
9
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; using ll = long long; const char E = '\n'; const int N = 10005; const ll mod = 1e9 + 7; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); string s; bool dp[N][2] = {0}; set<string> u; int n; cin >> s; n = s.size(); s = '#' + s + '#' + '#' + '#'; dp[n + 1][0] = dp[n + 1][1] = 1; for (int i = n; i >= 7; i--) { string x, y, x1, y1; x = s[i - 1]; x += s[i]; y = s[i - 2]; y += s[i - 1]; y += s[i]; x1 = s[i + 1]; x1 += s[i + 2]; y1 = s[i + 1]; y1 += s[i + 2]; y1 += s[i + 3]; if ((x != x1 && (dp[i + 1][0] || dp[i + 1][1])) || (x == x1 && dp[i + 1][1])) { dp[i - 1][0] = 1; u.insert(x); } if (i > 7) if ((y != y1 && (dp[i + 1][0] || dp[i + 1][1])) || (y == y1 && dp[i + 1][0])) { dp[i - 2][1] = 1; u.insert(y); } } cout << u.size() << E; for (auto i : u) { cout << i << E; } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 29; const double EPS = 1e-6; const int MAX_N = 305; char s[MAX_N][MAX_N]; bool vist[MAX_N][MAX_N]; int n; bool Solve() { char x = s[0][0], o = s[0][1]; if (x == o) { return false; } int l = 0, r = n - 1; for (int i = 0; i < n; i++) { if (s[i][l] != x || s[i][r] != x) { return false; } vist[i][l] = true; vist[i][r] = true; l++, r--; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (!vist[i][j] && s[i][j] != o) { return false; } } } return true; } int main() { while (cin >> n) { memset(vist, 0, sizeof(vist)); for (int i = 0; i < n; i++) { cin >> s[i]; } if (Solve()) { cout << "YES" << endl; } else { cout << "NO" << endl; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; template <class T> inline void read(T &x) { x = 0; char c = getchar(); while (!isdigit(c)) c = getchar(); while (isdigit(c)) x = x * 10 + (c & 15), c = getchar(); } int n, m, pos1[400005], ch1[400005][26], tot1, ch2[400005][26], tot2, tr[400005][26], nxt[400005], pos2[400005], q[400005], l = 1, r, id[400005], head[400005], cnt, cnt1, t[400005], ans[400005], sz[400005]; vector<int> que[400005]; struct Edge { int a, b; } e[400005]; inline void add(int a, int b) { e[++cnt].a = head[a], e[cnt].b = b, head[a] = cnt; } char s[400005], c; inline void dfs1(int x) { id[x] = ++cnt1, sz[x] = 1; for (int i = head[x]; i; i = e[i].a) dfs1(e[i].b), sz[x] += sz[e[i].b]; } inline void add1(int x, int y) { for (int i = x; i <= cnt1; i += i & -i) t[i] += y; } inline int ask(int x) { int ans = 0; for (int i = x; i; i -= i & -i) ans += t[i]; return ans; } inline void dfs2(int x, int y) { add1(id[y], 1); for (auto &i : que[x]) ans[i] = ask(id[pos2[i]] + sz[pos2[i]] - 1) - ask(id[pos2[i]] - 1); for (int i = 0; i < 26; i++) if (ch1[x][i]) dfs2(ch1[x][i], tr[y][i]); add1(id[y], -1); } signed main() { read(n); for (int i = 1; i <= n; i++) { int x; read(x); if (x == 1) scanf(" %c", &c), pos1[i] = (ch1[0][c - 'a'] ? ch1[0][c - 'a'] : ch1[0][c - 'a'] = ++tot1); else { int y; read(y), scanf(" %c", &c), pos1[i] = (ch1[pos1[y]][c - 'a'] ? ch1[pos1[y]][c - 'a'] : ch1[pos1[y]][c - 'a'] = ++tot1); } } read(m); for (int i = 1, len, no; i <= m; i++) { read(no), que[pos1[no]].push_back(i), scanf("%s", s + 1), len = strlen(s + 1), no = 0; for (int j = 1; j <= len; j++) no = (ch2[no][s[j] - 'a'] ? ch2[no][s[j] - 'a'] : ch2[no][s[j] - 'a'] = ++tot2); pos2[i] = no; } memcpy(tr, ch2, sizeof(tr)); for (int i = 0; i < 26; i++) if (tr[0][i]) q[++r] = tr[0][i]; while (l <= r) { int x = q[l++]; for (int i = 0; i < 26; i++) if (tr[x][i]) q[++r] = tr[x][i], nxt[tr[x][i]] = tr[nxt[x]][i]; else tr[x][i] = tr[nxt[x]][i]; } for (int i = 1; i <= tot2; i++) add(nxt[i], i); dfs1(0), dfs2(0, 0); for (int i = 1; i <= m; i++) printf("%d\n", ans[i]); return 0; }
9
#include <bits/stdc++.h> using namespace std; const long long Long_max = numeric_limits<long long>::max(); const int Int_max = numeric_limits<int>::max(); void File_input(string pathIn) { freopen(pathIn.c_str(), "r", stdin); } void File_output(string pathOut) { freopen(pathOut.c_str(), "w", stdout); } void FastInputOutput() { ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int n; vector<pair<int, pair<int, int> > > vec; bool cmp(pair<int, pair<int, int> > s, pair<int, pair<int, int> > f) { if (f.second.second != s.second.second) return f.second.second < s.second.second; return s.second.first < f.second.first; } int main() { cin >> n; vec.resize(n); for (int i = 0; i < n; i++) { cin >> vec[i].second.first >> vec[i].second.second; vec[i].first = i; } sort(vec.begin(), vec.end(), cmp); int k; cin >> k; vector<int> a(k); for (int i = 0; i < k; i++) cin >> a[i]; vector<bool> in(k); vector<pair<int, int> > ans; int sum = 0; for (auto j : vec) { int mn = Int_max; int idx = -1; for (int i = 0; i < k; i++) { if (!in[i] && a[i] >= j.second.first && mn > a[i]) { mn = min(a[i], mn); idx = i; } } if (idx != -1) { in[idx] = 1; sum += j.second.second; ans.push_back({idx + 1, j.first + 1}); } } cout << (int)ans.size() << " " << sum << "\n"; for (auto i : ans) { cout << i.second << " " << i.first << "\n"; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); int arr[n]; vector<int> v; map<int, int> mp; for (int i = 0; i < n; i++) scanf("%d", &arr[i]); for (int i = n - 1; i >= 0; i--) { if (mp[arr[i]] == 0) v.push_back(arr[i]); mp[arr[i]]++; } int len = v.size(); printf("%d\n", len); for (int i = len - 1; i >= 0; i--) printf("%d ", v[i]); }
0
#include <bits/stdc++.h> using namespace std; int min(int a, int b) { return (a < b) ? a : b; } int max(int a, int b) { return (a > b) ? a : b; } int n, m; string ss; int t; int atk[1000], a = 0; int def[1000], b = 0; int sec[1000]; int del[1000] = {0}; int sol1 = 0, sol2 = 0; void solve1() { for (int i = 0; i < min(a, m); i++) { if (sec[m - 1 - i] < atk[i]) break; sol1 += sec[m - 1 - i] - atk[i]; } } void solve2() { int k = 0, l = 0; while (k < b && l < m) { if (def[k] < sec[l]) { k++; del[l] = 1; l++; } else { l++; } } if (k != b) { sol2 = 0; return; } k = 0; l = 0; while (k < a && l < m) { if (del[l] == 1) { l++; continue; } if (atk[k] <= sec[l]) { del[l] = 1; sol2 += sec[l] - atk[k]; k++; l++; } else { l++; } } if (k != a) { return; } for (int i = 0; i < m; i++) { if (del[i] == 1) continue; sol2 += sec[i]; } } int main() { cin >> n >> m; for (int i = 0; i < n; i++) { cin >> ss >> t; if (ss == "ATK") { atk[a++] = t; } else { def[b++] = t; } } for (int i = 0; i < m; i++) { cin >> sec[i]; } sort(sec, sec + m); sort(atk, atk + a); sort(def, def + b); solve1(); solve2(); cout << max(sol1, sol2) << endl; }
5
#include <bits/stdc++.h> using namespace std; int main() { int tt; cin >> tt; while (tt--) { int n; string s; cin >> n >> s; int i = 0; while (true) { int ok = 0; vector<int> c; for (int i = 0; i < s.length() - 1; i++) if (s[i] == 'A' && s[i + 1] != 'A') { c.push_back(i + 1); ok = 1; } for (auto& it : c) s[it] = 'A'; if (!ok) break; i++; } cout << i << endl; } return 0; }
0
#include <bits/stdc++.h> int Check_Result(int A[], int ALenght, int count, int res) { int *Schet = (int *)malloc(count * sizeof(int)); for (int i = 0; i <= ALenght - res; ++i) { int flag = 1; for (int k = 0; k < count; ++k) { Schet[k] = 0; } for (int j = 0; j < ALenght; ++j) { if (j == i) { j = i + res; } if (j < ALenght) { Schet[A[j]]++; if (Schet[A[j]] >= 2) { flag = 0; break; } } } if (flag == 1) { return 1; } } return 0; } void Q_Sort(int A[], int first, int last) { if (first < last) { int middle = A[(first + last) / 2]; int left = first; int right = last; while (left <= right) { while (A[left] < middle) { left++; } while (A[right] > middle) { right--; } if (left <= right) { int sm = A[left]; A[left] = A[right]; A[right] = sm; left++; right--; } } Q_Sort(A, first, right); Q_Sort(A, left, last); } } int LeftBinSearch(int A[], int n, int x) { int l = -1; int r = n; while (l < r - 1) { int m = (l + r) / 2; if (A[m] < x) { l = m; } else { r = m; } } return r; } int main() { int n = 0; scanf("%d", &n); int *B = (int *)malloc(n * sizeof(int)); int *A = (int *)malloc(n * sizeof(int)); for (int i = 0; i < n; ++i) { scanf("%d", &A[i]); B[i] = A[i]; } Q_Sort(B, 0, n - 1); int count = 1; for (int j = 1; j < n; ++j) { if (B[j - 1] != B[j]) { count++; } } int *C = (int *)malloc(count * sizeof(int)); C[0] = B[0]; int pos = 0; for (int k = 1; k < n; ++k) { if (B[k] != B[k - 1]) { C[pos + 1] = B[k]; pos++; } } for (int l = 0; l < n; ++l) { B[l] = LeftBinSearch(C, count, A[l]); } int left = 0; int right = n - 1; while (left + 1 != right && left != right) { int m = (left + right) / 2; if (Check_Result(B, n, count, m) == 1) { right = m; } else { left = m; } } if (left == 0 && Check_Result(B, n, count, 0) == 1) { printf("%d\n", 0); } else { printf("%d\n", right); } }
3
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 5; int n, a[55]; long long dp[55], k; long long coba(int x) { if (x == (n - 1)) return 1LL; if (x == n) return 1LL; if (x > n) return 0LL; long long &ret = dp[x]; if (ret != -1) return ret; ret = coba(x + 1) + coba(x + 2); return ret; } int main() { scanf("%d %lld", &n, &k); memset(dp, -1, sizeof(dp)); int aw = 0; while (aw < n) { long long tes = coba(aw + 1); if (tes >= k) { a[aw + 1] = aw + 1; aw++; } else { k -= tes; a[aw + 1] = aw + 2; a[aw + 2] = aw + 1; aw += 2; } } bool first = 1; for (int i = 1; i <= n; i++) { if (first) first = 0; else printf(" "); printf("%d", a[i]); } cout << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; vector<long long> M, B; bool bad(int l1, int l2, int l3, bool lowerPart = 1) { if (lowerPart == 1) return (B[l3] - B[l1]) * (M[l1] - M[l2]) <= (B[l2] - B[l1]) * (M[l1] - M[l3]); else return (B[l3] - B[l1]) * (M[l1] - M[l2]) >= (B[l2] - B[l1]) * (M[l1] - M[l3]); } void addLineToCHT(long long m, long long b, bool lowerPart = 1) { M.push_back(m); B.push_back(b); while (M.size() >= 3 && bad(M.size() - 3, M.size() - 2, M.size() - 1, lowerPart)) { M.erase(M.end() - 2); B.erase(B.end() - 2); } } long long qptr = 0; long long queryOffline(long long x, bool findMin = 1) { if (qptr > M.size()) qptr = M.size() - 1; if (findMin) { while (qptr + 1 < M.size() && M[qptr + 1] * x + B[qptr + 1] < M[qptr] * x + B[qptr]) qptr++; } else { while (qptr + 1 < M.size() && M[qptr + 1] * x + B[qptr + 1] > M[qptr] * x + B[qptr]) qptr++; } return M[qptr] * x + B[qptr]; } long long queryTernary(long long x, bool findMin = 1) { int lo = 0, hi = M.size() - 1; long long ans = 3e18; if (findMin) ans = -3e18; while (lo <= hi) { int diff = (hi - lo) / 3; int mid1 = lo + diff; int mid2 = hi - diff; long long y1 = M[mid1] * x + B[mid1], y2 = M[mid2] * x + B[mid2]; if (y1 <= y2) { ans = y1; if (findMin) hi = mid2 - 1; else lo = mid1 + 1; } else { ans = y2; if (findMin) lo = mid1 + 1; else hi = mid2 - 1; } } return ans; } long long a[200010]; int main() { long long cs, ts, i, j, k, x, y, z, q, m, n; cin >> n; for (i = 1; i <= n; i++) scanf("%lld", &a[i]); x = 0; addLineToCHT(0, 0, 0); y = 0, z = 0; for (i = 1; i <= n; i++) { y += a[i]; z += i * a[i]; x = max(x, queryTernary(y, 0) + z); addLineToCHT(-i, i * y - z, 0); } cout << x << endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; const int Maxn = 3; const int Maxm = 301; const int Inf = 1000000000; int n; int a[Maxn]; bool dp[Maxm][Maxm]; int rc[Maxm][Maxm]; bool diag[Maxm][Maxm]; bool win; bool Win1() { return a[0]; } bool Win2() { for (int i = 0; i <= a[0]; i++) for (int j = 0; j <= a[1]; j++) { int mn = min(i, j); for (int l = 1; l <= mn && !dp[i][j]; l++) dp[i][j] = !dp[i - l][j - l]; for (int l = 1; l <= i && !dp[i][j]; l++) dp[i][j] = !dp[i - l][j]; for (int l = 1; l <= j && !dp[i][j]; l++) dp[i][j] = !dp[i][j - l]; } return dp[a[0]][a[1]]; } bool Win3() { fill((int*)rc, (int*)rc + Maxm * Maxm, Inf); bool win; for (int i = 0; i <= a[0]; i++) for (int j = 0; j <= a[1]; j++) for (int k = 0; k <= a[2]; k++) { int i2 = i, j2 = j, k2 = k; if (i2 > j2) swap(i2, j2); if (j2 > k2) swap(j2, k2); if (i2 > j2) swap(i2, j2); win = rc[i2][j2] < k2 || rc[i2][k2] < j2 || rc[j2][k2] < i2 || diag[j2 - i2][k2 - j2]; if (!win) { rc[i2][j2] = min(rc[i2][j2], k2); rc[i2][k2] = min(rc[i2][k2], j2); rc[j2][k2] = min(rc[j2][k2], i2); diag[j2 - i2][k2 - j2] = true; } } return win; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); if (n == 1) win = Win1(); else if (n == 2) win = Win2(); else win = Win3(); printf("%s\n", win ? "BitLGM" : "BitAryo"); return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n, k; int i; char a[105005]; while (cin >> n >> k) { cin >> a; queue<char> q; int len = 0; int res = 0; for (i = 0; i < n; i++) { if (a[i] == 'a') { q.push(a[i]); } else if (len != k) { len++; q.push(a[i]); } else { if (res < q.size()) res = q.size(); while (!q.empty() && q.front() == 'a') q.pop(); if (!q.empty()) { q.pop(); q.push(a[i]); } } } if (res < q.size()) res = q.size(); while (!q.empty()) q.pop(); len = 0; for (i = 0; i < n; i++) { if (a[i] == 'b') { q.push(a[i]); } else if (len != k) { len++; q.push(a[i]); } else { if (res < q.size()) res = q.size(); while (!q.empty() && q.front() == 'b') q.pop(); if (!q.empty()) { q.pop(); q.push(a[i]); } } } if (res < q.size()) res = q.size(); cout << res << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; double a[1001]; bool fucked[1001]; int main() { int n, i, j, x, y; double k, b, k1, b1; cin >> n; for (i = 1; i <= n; i++) scanf("%lf", &a[i]); x = 1; y = 2; for (i = 1; i <= 3; i++) { if (a[x] == a[y]) { k = 0; b = a[x]; } else { k = (a[y] - a[x]) / (y - x); b = (a[x] * y - a[y] * x) / (y - x); } for (j = 1; j <= n; j++) if (k * j + b == a[j]) fucked[j] = 1; for (j = 1; j <= n; j++) if (fucked[j] == 0) break; if (j > n) { cout << "No"; return 0; } b1 = a[j] - k * j; for (j = 1; j <= n; j++) if (k * j + b1 == a[j]) fucked[j] = 1; for (j = 1; j <= n; j++) if (fucked[j] == 0) break; if (j > n) { cout << "Yes"; return 0; } for (j = 1; j <= n; j++) fucked[j] = 0; if (i == 1) y++; else if (i == 2) x++; } cout << "No"; }
4
#include <bits/stdc++.h> using namespace std; bool comp(pair<char, long long> a, pair<char, long long> b) { return a.second > b.second; } void solve() { long long n, k; cin >> n >> k; string s; cin >> s; long long l = s.length(); map<char, long long> m; for (long long i = 0; i < l; i++) { m[s[i]]++; } vector<pair<char, long long>> v; for (auto it : m) { v.push_back(it); } sort(v.begin(), v.end(), comp); long long ans = 0; for (auto it : v) { if (k >= it.second) { ans = ans + it.second * it.second; k = k - it.second; } else { ans = ans + k * k; break; } } cout << ans; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long t = 1; while (t--) { solve(); } return 0; }
2
#include <bits/stdc++.h> namespace { template <typename T> struct SparseTable { std::vector<std::vector<T>> u; std::vector<int> lg; void build(const std::vector<T>& a) { int n = a.size(), L = 1; while ((1 << L) <= n) ++L; std::vector<std::vector<T>>(n, std::vector<T>(L)).swap(u); lg.resize(n + 1); lg[0] = -1; for (int i = 1; i < lg.size(); ++i) lg[i] = lg[i >> 1] + 1; for (int i = 0; i < n; ++i) u[i][0] = a[i]; for (int j = 1; (1 << j) <= n; ++j) { for (int i = 0; i + (1 << j) <= n; ++i) { u[i][j] = std::min(u[i][j - 1], u[i + (1 << (j - 1))][j - 1]); } } } T ask(int a, int b) const { if (a > b || a < 0 || b >= u.size()) throw; int k = lg[b - a + 1]; return std::min(u[a][k], u[b - (1 << k) + 1][k]); } }; using String = std::vector<int>; struct SuffixArray { int n; String str; std::vector<int> sa, rank; std::vector<int> lcp; SparseTable<int> st; SuffixArray(const String& _s) : n(_s.size()), str(_s), sa(n + 1), rank(n + 1) { for (int i = 0; i <= n; ++i) { sa[i] = i; rank[i] = i < n ? str[i] : -1; } std::vector<int> tmp(n + 1); for (int k = 1; k <= n; k <<= 1) { auto compare_sa = [=](int i, int j) { if (rank[i] != rank[j]) return rank[i] < rank[j]; else { int ri = i + k <= n ? rank[i + k] : -1; int rj = j + k <= n ? rank[j + k] : -1; return ri < rj; } }; std::sort(sa.begin(), sa.end(), compare_sa); tmp[sa[0]] = 0; for (int i = 1; i <= n; i++) { tmp[sa[i]] = tmp[sa[i - 1]] + (compare_sa(sa[i - 1], sa[i]) ? 1 : 0); } rank.swap(tmp); } for (int i = 0; i <= n; ++i) rank[sa[i]] = i; lcp.resize(n + 1, 0); int h = 0; for (int i = 0; i < n; ++i) { int j = sa[rank[i] - 1]; if (h > 0) --h; for (; j + h < n && i + h < n; ++h) { if (str[j + h] != str[i + h]) break; } lcp[rank[i] - 1] = h; } } int compare(const String& lhs, int pos, int len, const String& rhs) const { for (int i = pos; i < pos + len; ++i) { if (i >= lhs.size()) { return i - pos >= rhs.size() ? 0 : -1; } if (i - pos >= rhs.size()) { return 1; } if (lhs[i] != rhs[i - pos]) { return lhs[i] - rhs[i - pos]; } } return len < rhs.size() ? -1 : 0; } template <typename Compare> int binary_search(const String& t) const { int lb = -1, ub = n + 1; while (ub - lb > 1) { int mid = (ub + lb) / 2; if (Compare()(compare(str, sa[mid], t.size(), t), 0)) lb = mid; else ub = mid; } return ub; } bool contains(const String& t) const { int lb = 0, ub = n; while (ub - lb > 1) { int mid = (lb + ub) / 2; if (compare(str, sa[mid], t.size(), t) < 0) lb = mid; else ub = mid; } return compare(str, sa[ub], t.size(), t) == 0; } void prepare_lcp_rmq() { st.build(lcp); } int get_lcp(int x, int y) const { if (x == y) return n - x; if (rank[x] > rank[y]) std::swap(x, y); return st.ask(rank[x], rank[y] - 1); } int lower_bound(const String& t) const { return binary_search<std::less<int>>(t); } int upper_bound(const String& t) const { return binary_search<std::less_equal<int>>(t); } }; int example(const String& s) { SuffixArray sa(s); return s.size() - *std::max_element(sa.lcp.begin(), sa.lcp.end()); } template <int MOD> struct Integral { int v_ = 0; template <typename T> Integral(T v) : v_(norm(v)) { static_assert(std::is_integral<T>::value, "input should be an integral."); } Integral() = default; ~Integral() = default; template <typename T> T norm(T v) const { if constexpr (std::is_same<long long, T>::value) { v %= MOD; if (v < 0) v += MOD; } else { if (v >= MOD) v -= MOD; if (v < 0) v += MOD; if (v >= MOD || v < 0) { v %= MOD; if (v < 0) v += MOD; } } return v; } int val() const { return v_; } Integral& operator+=(const Integral& rhs) { v_ += rhs.val(); if (v_ >= MOD) v_ -= MOD; return *this; } Integral& operator-=(const Integral& rhs) { v_ += MOD - rhs.val(); if (v_ >= MOD) v_ -= MOD; return *this; } Integral& operator*=(const Integral& rhs) { v_ = v_ * 1LL * rhs.val() % MOD; return *this; } Integral& operator/=(const Integral& rhs) { v_ = v_ * 1LL * rhs.inv().val() % MOD; return *this; } Integral operator+(const Integral& rhs) const { Integral ret = *this; return ret += rhs; } Integral operator-(const Integral& rhs) const { Integral ret = *this; return ret -= rhs; } Integral operator*(const Integral& rhs) const { Integral ret = *this; return ret *= rhs; } Integral operator/(const Integral& rhs) const { Integral ret = *this; return ret /= rhs; } bool operator==(const Integral& rhs) const { return val() == rhs.val(); } bool operator!=(const Integral& rhs) const { return !(*this == rhs); } const Integral operator-() const { return Integral(-val()); } const Integral& operator++() { v_ += 1; if (v_ >= MOD) v_ -= MOD; return *this; } const Integral operator++(int) { Integral ret = *this; ++(*this); return ret; } const Integral& operator--() { v_ += MOD - 1; if (v_ >= MOD) v_ -= MOD; return *this; } const Integral operator--(int) { Integral ret = *this; --(*this); return ret; } Integral power(long long b) const { long long ret = 1 % MOD, a = v_; for (; b; b >>= 1, a = a * a % MOD) if (b & 1) ret = ret * a % MOD; return ret; } Integral inv() const { return power(MOD - 2); } }; template <int MOD> std::string to_string(const Integral<MOD>& v) { return std::string("{") + std::to_string(v.val()) + "}"; } template <int MOD, bool kAllowBruteForce = false> struct Binomial { std::vector<Integral<MOD>> factor, inv_factor; explicit Binomial(int n = 0) : factor(n + 1), inv_factor(n + 1) { factor[0] = 1; for (int i = 1; i <= n; ++i) factor[i] = factor[i - 1] * i; inv_factor[n] = factor[n].inv(); for (int i = n; i >= 1; --i) inv_factor[i - 1] = inv_factor[i] * i; } ~Binomial() = default; template <typename T> Integral<MOD> operator()(T a, T b) const { if (a < b || b < 0) return 0; if (a < factor.size()) return factor[a] * inv_factor[b] * inv_factor[a - b]; if constexpr (!kAllowBruteForce) { throw std::out_of_range("Binomial"); } else { b = std::min(b, a - b); Integral<MOD> ret = 1; for (T i = 1; i <= b; ++i) ret = ret * (a + 1 - i) / i; return ret; } } }; template <int MOD> struct PowerTable : public std::vector<Integral<MOD>> { PowerTable(int n, const Integral<MOD>& g) { static_assert(sizeof(PowerTable) == sizeof(std::vector<Integral<MOD>>), ""); this->resize(n + 1); this->at(0) = 1; this->at(1) = g; for (int i = 2; i < this->size(); ++i) this->at(i) = this->at(i - 1) * this->at(1); } }; const int MOD = 998244353; using Mint = Integral<MOD>; using Binom = Binomial<MOD>; Binom binom(300000); template <int MOD = 998244353, int kPrimRoot = 3> void ntt(Integral<MOD> A[], int n, int inv) { Integral<MOD> w = 1, d = Integral<MOD>(kPrimRoot).power((MOD - 1) / n), t; int i, j, c, s; if (inv == -1) { for (i = 1, j = n - 1; i < j; ++i, --j) std::swap(A[i], A[j]); for (t = Integral<MOD>(n).inv(), i = 0; i < n; ++i) A[i] = A[i] * t; } for (s = n >> 1; s; s >>= 1, w = 1, d = d * d) { for (c = 0; c < s; ++c, w = w * d) { for (i = c; i < n; i += s << 1) { A[i | s] = (A[i] - (t = A[i | s])) * w; A[i] += t; } } } for (i = 1; i < n; ++i) { for (j = 0, s = i, c = n >> 1; c; c >>= 1, s >>= 1) j = j << 1 | (s & 1); if (i < j) std::swap(A[i], A[j]); } } template <typename T> using Polynomial = std::vector<T>; template <typename T> void ntt(Polynomial<T>& poly, int n, int inv) { if (poly.size() < n) { poly.resize(n, 0); } return ntt(&poly[0], n, inv); } template <typename T> void norm(Polynomial<T>& p) { while (p.size() > 1 && p.back() == T(0)) p.pop_back(); } template <typename T> Polynomial<T> operator+(const Polynomial<T>& lhs, const Polynomial<T>& rhs) { Polynomial<T> ret = lhs; ret.resize(std::max(lhs.size(), rhs.size()), T(0)); for (int i = 0; i < rhs.size(); ++i) ret[i] += rhs[i]; norm(ret); return ret; } template <typename T> Polynomial<T> operator-(const Polynomial<T>& lhs, const Polynomial<T>& rhs) { Polynomial<T> ret = lhs; ret.resize(std::max(lhs.size(), rhs.size()), T(0)); for (int i = 0; i < rhs.size(); ++i) ret[i] -= rhs[i]; norm(ret); return ret; } template <typename T> Polynomial<T> mod_len(Polynomial<T> poly, int len) { if (poly.size() > len) { poly.resize(len); } norm(poly); return poly; } template <typename T> Polynomial<T> enforce_len(Polynomial<T> poly, int len) { poly.resize(len, 0); return poly; } int binary_upper_bound(int w) { if (w <= 0) return 1; const int highbit = 31 - __builtin_clz(w); return (1 << highbit) << ((1 << highbit) != w); } template <typename T> int get_ntt_len(const Polynomial<T>& lhs, const Polynomial<T>& rhs) { return binary_upper_bound((int)lhs.size() + (int)rhs.size() - 1); } template <typename T> Polynomial<T> operator*(Polynomial<T> lhs, Polynomial<T> rhs) { (lhs.size() + rhs.size() > 0); const int L = get_ntt_len(lhs, rhs); lhs.resize(L, 0); rhs.resize(L, 0); ntt(lhs, L, 1); ntt(rhs, L, 1); for (int i = 0; i < L; ++i) { lhs[i] *= rhs[i]; } ntt(lhs, L, -1); norm(lhs); return lhs; } template <typename T> Polynomial<T> operator*(T a, Polynomial<T> p) { for (int i = 0; i < p.size(); ++i) p[i] *= a; norm(p); return p; } template <typename T> Polynomial<T> mod_inv(Polynomial<T> poly, int len = 0) { (poly.size() >= 1 && poly[0] != 0); if (len == 0) len = poly.size(); if (len > poly.size()) poly.resize(len); const int L = binary_upper_bound(len); poly.resize(L, 0); Polynomial<T> w(L << 1), r(L << 1), saved(L << 1); w[0] = poly[0].inv(); for (int len = 2; len <= L; len <<= 1) { std::copy(w.begin(), w.begin() + (len >> 1), r.begin()); std::copy(poly.begin(), poly.begin() + len, saved.begin()); ntt(saved, len, 1); ntt(r, len, 1); for (int i = 0; i < len; ++i) r[i] *= saved[i]; ntt(r, len, -1); for (int i = 0; i < (len >> 1); ++i) r[i] = 0; std::copy(w.begin(), w.begin() + len, saved.begin()); ntt(saved, len, 1); ntt(r, len, 1); for (int i = 0; i < len; ++i) r[i] *= saved[i]; ntt(r, len, -1); for (int i = (len >> 1); i < len; ++i) w[i] = w[i] + w[i] - r[i]; } return mod_len(w, len); } template <typename T> Polynomial<T> derivate(Polynomial<T> poly) { if (poly.size() <= 1) { return Polynomial<T>(1, 0); } for (int i = 1; i < poly.size(); ++i) { poly[i - 1] = poly[i] * i; } poly.pop_back(); norm(poly); return poly; } template <typename T> Polynomial<T> integrate(Polynomial<T> poly) { if (poly.size() == 0) { return poly; } poly.emplace_back(0); for (int i = (int)poly.size() - 1; i > 0; --i) { poly[i] = poly[i - 1] / i; } poly[0] = 0; norm(poly); return poly; } template <typename T> Polynomial<T> mod_sqrt(Polynomial<T> poly, int len = 0) { (poly.size() >= 1 && poly[0] == 1); if (len > poly.size()) poly.resize(len); if (poly.size() == 1) { return poly; } if (len == 0) len = poly.size(); const int L = binary_upper_bound(len); poly.resize(L, 0); Polynomial<T> buffer(1, 1), buffer1; for (int len = 2; len <= L; len <<= 1) { buffer1 = mod_inv(T(2) * buffer, len); ntt(buffer, len, 1); for (int i = 0; i < len; ++i) buffer[i] *= buffer[i]; ntt(buffer, len, -1); for (int i = 0; i < len; ++i) buffer[i] += poly[i]; buffer = enforce_len(buffer * buffer1, len); } return mod_len(buffer, len); } template <typename T> Polynomial<T> mod_ln(Polynomial<T> poly, int len = 0) { if (len == 0) len = poly.size(); if (len > poly.size()) poly.resize(len); return mod_len(integrate(derivate(poly) * mod_inv(poly, len)), len); } template <typename T> Polynomial<T> mod_exp(Polynomial<T> poly, int len = 0) { if (len == 0) len = poly.size(); if (len > poly.size()) poly.resize(len); (poly.size() == 0 || poly[0] == 0); const int L = binary_upper_bound(len); poly.resize(L, 0); Polynomial<T> p(1, 1); for (int len = 2; len <= L; len <<= 1) { Polynomial<T> q = p; p = enforce_len(mod_ln(p, len), len); for (int i = 0; i < len; ++i) p[i] = poly[i] - p[i]; ++p[0]; p = mod_len(p * q, len); } return mod_len(p, len); } template <typename T> Polynomial<T> conditioned_mod_power(Polynomial<T> poly, T k, int len = 0) { (poly.size() >= 1 && poly[0] != T(0)); if (len == 0) len = poly.size(); T constant = poly[0]; if (constant != T(1)) { T inv_constant = T(1) / constant; for (int i = 0; i < poly.size(); ++i) { poly[i] *= inv_constant; } } Polynomial<T> ret = mod_exp(k * mod_ln(poly, len), len); if (constant != T(1)) { constant = constant.power(k.val()); for (int i = 0; i < ret.size(); ++i) { ret[i] *= constant; } } return ret; } template <typename T> T lagrange_inversion(Polynomial<T> poly, int n) { if (poly.empty()) return T(0); (poly[0] == T(0)); (poly.size() > 1); (poly[1] != T(0)); (n > 0); poly.erase(poly.begin()); return enforce_len(conditioned_mod_power(poly, n, n), n)[n - 1] / T(n); } using Poly = Polynomial<Integral<MOD>>; struct Solver { int n, m, k; std::vector<std::vector<int>> A; Mint solve() { using EdgeSet = std::set<int>; std::vector<EdgeSet> prev(k), next(k); for (int i = 0; i < n; ++i) { for (int j = 0; j + 1 < A[i].size(); ++j) { int x = A[i][j]; int y = A[i][j + 1]; prev[y].emplace(x); next[x].emplace(y); } } std::vector<bool> bad(k); std::queue<int> que; for (int i = 0; i < k; ++i) { if (prev[i].size() > 1 || next[i].size() > 1) { bad[i] = true; que.emplace(i); } } while (!que.empty()) { int u = que.front(); que.pop(); for (int v : prev[u]) { if (!bad[v]) { bad[v] = true; que.emplace(v); } } for (int v : next[u]) { if (!bad[v]) { bad[v] = true; que.emplace(v); } } } Poly poly(m + 1); for (int i = 0; i < k; ++i) if (!bad[i] && prev[i].empty()) { int len = 1; for (int x = i; !next[x].empty(); x = *next[x].begin()) { ++len; } poly[len]++; } return mod_inv(Poly(1, 1) - poly, m + 1)[m]; } void solve(int ca, std::istream& reader) { reader >> n >> m >> k; A.resize(n); for (int i = 0; i < n; ++i) { int len; reader >> len; A[i].resize(len); for (int j = 0; j < len; ++j) { reader >> A[i][j]; --A[i][j]; } } printf("%d\n", solve().val()); } }; } // namespace int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::istream& reader = std::cin; int cas = 1; for (int ca = 1; ca <= cas; ++ca) { Solver().solve(ca, reader); } }
9
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair<int, int>; template <class T> inline void read(T &x) { x = 0; char c = getchar(); bool flag = 0; while (!isdigit(c)) { if (c == '-') flag = 1; c = getchar(); } while (isdigit(c)) { x = (x << 3) + (x << 1) + c - '0'; c = getchar(); } x = flag ? -x : x; } template <class T> inline void write(T x) { if (x < 0) { putchar('-'); x = -x; } if (x >= 10) write(x / 10); putchar(x % 10 + '0'); } const int maxn = 3e3 + 10; const int mod = 1e9 + 7; const double eps = 1e-7; int a[maxn]; int cnt[maxn][maxn]; ll c[maxn]; ll d[maxn]; vector<int> v[maxn]; int main() { int t; read(t); while (t--) { int n; read(n); for (int i = 0; i <= n; ++i) { v[i].clear(); for (int j = 0; j <= n; ++j) { cnt[i][j] = 0; } } for (int i = 1; i <= n; ++i) { read(a[i]); cnt[a[i]][i] = 1; v[a[i]].push_back(i); } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { cnt[i][j] += cnt[i][j - 1]; } } ll ans = 0; for (int k = 1; k <= n; ++k) { if (v[k].size() < 2) continue; for (int i = 1; i <= n; ++i) { d[i] = 0; } int p = v[k][0]; for (int i = 1; i < v[k].size(); ++i) { for (int j = 1; j <= n; ++j) { if (j != k) { d[j] += (ll)(cnt[j][v[k][i] - 1] - cnt[j][p - 1]) * i; ans += d[j] * (cnt[j][n] - cnt[j][v[k][i] - 1]); } else { d[j] += (ll)(cnt[j][v[k][i] - 1] - cnt[j][p - 1]) * (i - 1); ans += d[j] * (cnt[j][n] - cnt[j][v[k][i]]); } } p = v[k][i]; } } write(ans); putchar('\n'); } }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<char> empty(m); vector<vector<char>> a(n, empty); int x, y; for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { cin >> a[i][j]; if (a[i][j] == 'S') { x = i; y = j; } } } string check; cin >> check; vector<char> p(4); p[0] = 'u'; p[1] = 'r'; p[2] = 'd'; p[3] = 'l'; sort(p.begin(), p.end()); int cnt = 0; do { int f = x, s = y; for (int i = 0; i < check.length(); ++i) { if (p[(int)(check[i] - '0')] == 'u') { if (f > 0) { f--; } else { break; } } if (p[(int)(check[i] - '0')] == 'r') { if (s < m - 1) { s++; } else { break; } } if (p[(int)(check[i] - '0')] == 'd') { if (f < n - 1) { f++; } else { break; } } if (p[(int)(check[i] - '0')] == 'l') { if (s > 0) { s--; } else { break; } } if (a[f][s] == 'E') { cnt++; break; } if (a[f][s] == '#') { break; } } } while (next_permutation(p.begin(), p.end())); cout << cnt; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 100 * 1000 + 85 - 69; int sa[maxn], pos[maxn], tmp[maxn], lcp[maxn], last[maxn], nexT[maxn], next2[maxn], gap; long long int ans; string s; stack<int> stc; bool sufCmp(int i, int j) { if (pos[i] != pos[j]) return pos[i] < pos[j]; i += gap, j += gap; return (i < ((int)(s).size()) and j < ((int)(s).size())) ? pos[i] < pos[j] : i > j; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> s; for (int i = 0; i < ((int)(s).size()); i++) sa[i] = i, pos[i] = s[i]; for (gap = 1;; gap <<= 1) { sort(sa, sa + ((int)(s).size()), sufCmp); for (int i = 0; i + 1 < ((int)(s).size()); i++) tmp[i + 1] = tmp[i] + sufCmp(sa[i], sa[i + 1]); for (int i = 0; i < ((int)(s).size()); i++) pos[sa[i]] = tmp[i]; if (tmp[((int)(s).size()) - 1] == ((int)(s).size()) - 1) break; } int match = 0; for (int i = 0; i < ((int)(s).size()); i++) { if (match) match--; if (!pos[i]) continue; int cur = i, pre = sa[pos[i] - 1]; while (max(pre, cur) + match < ((int)(s).size()) and s[pre + match] == s[cur + match]) match++; lcp[pos[i]] = match; } for (int i = 0; i < ((int)(s).size()); i++) { while (((int)(stc).size()) and lcp[stc.top()] >= lcp[i]) stc.pop(); last[i] = (((int)(stc).size()) ? stc.top() : 0); stc.push(i); } while (((int)(stc).size())) stc.pop(); for (int i = ((int)(s).size()) - 1; i >= 0; i--) { while (((int)(stc).size()) and lcp[stc.top()] >= lcp[i]) stc.pop(); nexT[i] = (((int)(stc).size()) ? stc.top() : ((int)(s).size())); stc.push(i); } while (((int)(stc).size())) stc.pop(); for (int i = ((int)(s).size()) - 1; i >= 0; i--) { while (((int)(stc).size()) and lcp[stc.top()] > lcp[i]) stc.pop(); next2[i] = (((int)(stc).size()) ? stc.top() : ((int)(s).size())); stc.push(i); } for (int i = 0; i < ((int)(s).size()); i++) if (lcp[i] and lcp[next2[i]] != lcp[i]) { long long int parh = max(lcp[last[i]], lcp[nexT[i]]); long long int dcnt = nexT[i] - last[i]; ans += dcnt * (dcnt + 1) / 2 * (lcp[i] - parh); } for (int i = 0; i < ((int)(s).size()); i++) { int an = max(lcp[i], lcp[i + 1]); if (an < ((int)(s).size()) - sa[i]) ans += ((int)(s).size()) - sa[i] - an; } cout << ans << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; int x = 0, y = 0; int var = 1; int temp1, temp2 = 0; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int i = 0, j = n - 1; x += a[i++]; temp1 = x; int mv = 1; while (i <= j) { if (var == 1) { temp2 = 0; while ((temp2 <= temp1) && (i <= j)) { temp2 += a[j]; j--; } y += temp2; } else { temp1 = 0; while ((temp1 <= temp2) && (i <= j)) { temp1 += a[i]; i++; } x += temp1; } var = 1 - var; mv++; } cout << mv << " " << x << " " << y << endl; } }
2
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; const ll llinf = (1ll << 61) - 1; const double eps = 1e-6, ldeps = 1e-9; struct Dbg { static constexpr auto &os = cout; template <class C> static auto dud(C *x) -> decltype(os << *x, 0); template <class C> static char dud(...); template <class C> typename enable_if<sizeof dud<C>(0) != 1, Dbg &>::type operator<<( const C &x) { os << x; return *this; } template <class C> Dbg &dump(C b, C e) { *this << "\n["; int cur = 0; for (C i = b; i != e and cur < 26; i++, cur++) *this << ", " + 2 * (i == b) << *i; return *this << ']'; } template <class C> typename enable_if<sizeof dud<C>(0) == 1, Dbg &>::type operator<<( const C &x) { return dump(begin(x), end(x)); } template <class C, size_t X> typename enable_if<!is_same<C, const char>::value, Dbg &>::type operator<<( C (&x)[X]) { return dump(begin(x), end(x)); } template <class B, class C> Dbg &operator<<(const pair<B, C> &x) { return *this << '{' << x.first << ", " << x.second << '}'; } template <class C, size_t Y> struct TP { void operator()(Dbg &os, const C &t) { TP<C, Y - 1>()(os, t); os << ", " << get<Y - 1>(t); } }; template <class C> struct TP<C, 1> { void operator()(Dbg &os, const C &t) { os << get<0>(t); } }; template <class... C> Dbg &operator<<(const tuple<C...> &t) { os << '{', TP<decltype(t), sizeof...(C)>()(*this, t); return *this << '}'; } Dbg &operator<<(ostream &(*x)(std::ostream &)) { os << x; return *this; } } dbg; void err(istringstream *iss) { delete iss; } template <class C, class... Args> void err(istringstream *iss, const C &val, const Args &...args) { string name; *iss >> name; if (name.back() == ',') name.pop_back(); dbg << name << " = " << val << "; ", err(iss, args...); } int T, Q, cn; const int inf = 1000000007, mxn = 405; char s[mxn], t[mxn]; int n, m, dp[mxn][mxn]; signed main() { ios::sync_with_stdio(0); cin.tie(NULL); cout.precision(11); cin >> T; while (cn++ != T) { cin >> s >> t; n = int(strlen(s)), m = int(strlen(t)); for (int i = 1; i <= m; i++) { for (int j = 0; j <= n; j++) { for (int k = 0; k <= i; k++) { dp[j][k] = 0; } } dp[0][0] = i; for (int j = 0; j < n; j++) { for (int k = 0; k <= i; k++) { if (dp[j][k] < i) break; if (s[j] == t[k]) dp[j + 1][k + 1] = max(dp[j + 1][k + 1], dp[j][k]); dp[j + 1][k] = max(dp[j + 1][k], (s[j] == t[dp[j][k]]) + dp[j][k]); } } if (dp[n][i] == m) { cout << "YES\n"; goto eot; } } cout << "NO\n"; eot:; } }
7
#include <bits/stdc++.h> using namespace std; const int MAXN = 160000; int val[MAXN * 4], b[MAXN], h, a[MAXN], n, m, lazy[MAXN * 4]; void build(int me, int l, int r) { if (l == r) { val[me] = -l; return; } int mid = (l + r) >> 1; build(me * 2, l, mid); build(me * 2 + 1, mid + 1, r); val[me] = min(val[me * 2], val[me * 2 + 1]); } inline void down(int me) { if (lazy[me] == 0) return; for (int i = (0); i <= (int)1; i++) { lazy[me * 2 + i] += lazy[me]; val[me * 2 + i] += lazy[me]; } lazy[me] = 0; } void add(int me, int l, int r, int x, int y, int v) { if (l ^ r) down(me); if (x <= l && r <= y) { lazy[me] += v; val[me] += v; return; } int mid = (l + r) >> 1; if (x <= mid) add(me * 2, l, mid, x, y, v); if (y > mid) add(me * 2 + 1, mid + 1, r, x, y, v); val[me] = min(val[me * 2], val[me * 2 + 1]); } inline int go(int x) { int l = 1; int r = m; int res = m + 1; while (l < r) { int mid = (l + r) >> 1; if (b[mid] + x >= h) res = mid, r = mid; else l = mid + 1; } if (b[l] + x >= h) res = l; return res; } int main() { scanf("%d%d%d", &n, &m, &h); for (int i = (1); i <= (int)m; i++) scanf("%d", &b[i]); build(1, 1, m); sort(b + 1, b + 1 + m); for (int i = (1); i <= (int)n; i++) scanf("%d", &a[i]); for (int i = (1); i <= (int)m; i++) { int lu = go(a[i]); if (lu > m) continue; add(1, 1, m, lu, m, 1); } int res = (val[1] >= 0); for (int i = (m + 1); i <= (int)n; i++) { int lu = go(a[i - m]); if (lu <= m) add(1, 1, m, lu, m, -1); lu = go(a[i]); if (lu <= m) add(1, 1, m, lu, m, 1); res += (val[1] >= 0); } printf("%d\n", res); return 0; }
9
/*" اللهم صلي و سلم علي سيدنا محمد و علي اله وصحبه اجمعين "*/ #define _CRT_SECURE_NO_WARNINGS #include <bits/stdc++.h> #include<iostream> //#include<unordered_map> using namespace std; #define ll long long #define ull unsigned long long #define sz(s) (int)(s.size()) #define endl "\n" #define BI acos(-1) #define all(a) a.begin(), a.end() #define mem(dp,n) memset(dp, n, sizeof dp); void Open() { #ifndef ONLINE_JUDGE //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); #endif !ONLINEJUDGE ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int dx[]{ 1, -1, 0, 0, 1, 1, -1, -1 }; int dy[]{ 0, 0, 1, -1, 1, -1, 1, -1 }; const int OO = 0x3f3f3f3f; int n; int t = 1; vector<int>prime(50001); void test_case() { cin >> n; n *= 2; vector<int>a(n); priority_queue<int>q1; map<int, int>mm; for (int i = 0; i < n; i++) { cin >> a[i]; q1.push(a[i]); mm[a[i]]++; } sort(all(a)); bool pp = 0; int w = 0; vector<pair<int, int>>p1; for (int i = 0; i < n ; i++) { priority_queue<int>q2; vector<pair<int, int>>p2; q2 = q1; int s = a.back() + a[i]; w = s; map<int, int>mp,nn; bool p = 1; while (!q2.empty()) { if ((q2.top()!=s-q2.top()&&mm[s - q2.top()]-mp[s - q2.top()]>0)|| (q2.top() == s - q2.top() && mm[s - q2.top()]-1 - mp[s - q2.top()] > 0)) { p2.push_back({ q2.top(),s - q2.top() }); mp[s - q2.top()]++; mp[q2.top()]++; nn[s - q2.top()]++; nn[q2.top()]++; s = max(q2.top(), s - q2.top()); } else { p = 0; break; } while (!q2.empty() && nn[q2.top()]) { nn[q2.top()]--; q2.pop(); } } if (p) { pp = 1; p1 = p2; break; } } if (pp) { cout << "YES" << endl << w<<endl; for (auto it : p1)cout << it.first << ' ' << it.second << endl; } else cout << "NO" << endl; } int main() { Open(); cin >> t; while (t--) { test_case(); } }
4
#include <bits/stdc++.h> using namespace std; int n; vector<int> rec(set<int> a, int it = 0) { if (it >= 20) return vector<int>(20, 0); if (a.empty()) return vector<int>(); int changed = 0; int best = 1; set<int> blah; for (auto i : a) { if (i % 2) { changed++; if ((i - 1) / 2) blah.insert((i - 1) / 2); } else if (i / 2) blah.insert(i / 2); } auto ans1 = rec(blah, it + 1); blah.clear(); for (auto i : a) { if (i % 2) { changed++; if ((i + 1) / 2) blah.insert((i + 1) / 2); } else if (i / 2) blah.insert(i / 2); } auto ans2 = rec(blah, it + 1); if (ans2.size() < ans1.size()) { ans1.swap(ans2); best = -1; } for (auto &i : ans1) i *= 2; if (changed) ans1.push_back(best); return ans1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; set<int> a; int b; for (int i = 0; i < n; i++) cin >> b, a.insert(b); auto ans = rec(a); cout << ans.size() << endl; for (int i : ans) cout << i << " "; cout << endl; }
9
#include <bits/stdc++.h> using namespace std; long long sqr(long long x) { return x * x; } const int N = 2e5 + 100; vector<pair<int, int> > a; int n, kt[N]; int main() { scanf("%d", &n); a.clear(); for (int i = 0; i < n; i++) { for (int j = i + 1; j <= n; j++) { a.push_back(pair<int, int>(i, j)); } } int res = 0; int top = 0; do { int v = 0; res++; for (int i = 0; i < a.size(); i++) { if (kt[i] == 0 && a[i].first >= v) { v = a[i].second; kt[i] = 1; top++; } } } while (top != a.size()); cout << res; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int k = n / 2 + n % 2; int a[n + 1]; int dp[n + 1][n + 1][2]; a[0] = 100002; memset(dp, 63, sizeof(dp)); dp[0][0][0] = dp[0][0][1] = dp[1][0][0] = dp[1][1][1] = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 2; i <= n; i++) { dp[i][0][0] = 0; for (int j = 1; j <= k; j++) { dp[i][j][0] = min(dp[i - 1][j][0], dp[i - 1][j][1] + max(a[i] - a[i - 1] + 1, 0)); dp[i][j][1] = min(dp[i - 2][j - 1][1] + max(a[i - 1] - min(a[i - 2], a[i]) + 1, 0), dp[i - 2][j - 1][0] + max(0, a[i - 1] - a[i] + 1)); } } for (int i = 1; i <= k; i++) { cout << min(dp[n][i][0], dp[n][i][1]) << " "; } return 0; }
5
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); if (n % 2 == 0) printf("%d\n", n / 2); else printf("%d\n", n / 2 + 1); return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long int MOD = 1000000007; long long int modpow(long long int a, long long int b, long long int mod) { long long int res = 1; for (; b; b >>= 1) { if (b & 1) res = res * a; res %= mod; a = a * a; a %= mod; } return res; } long long int p(long long int a, long long int b) { long long int res = 1; for (; b; b >>= 1) { if (b & 1) res = res * a; a = a * a; } return res; } long long int gcd(long long int a, long long int b) { return b ? gcd(b, a % b) : a; } void testcases(long long int test) { long long int n, k; cin >> n >> k; vector<long long int> l(n), t(n); for (long long int i = (long long int)0; i < (long long int)n; ++i) cin >> l[i]; for (long long int i = (long long int)0; i < (long long int)n; ++i) cin >> t[i]; long long int excess = 0; vector<pair<long long int, long long int>> ans; long long int ind = 0; long long int flag = 1; long long int timer = 0; while (ind < n) { l[ind] -= excess; t[ind] -= excess; while (ind < n && t[ind] >= 2 * l[ind]) { timer += 2 * l[ind]; ind++; } if (ind == n) break; if (t[ind] < l[ind]) { flag = 0; break; } long long int numtimes = (2 * l[ind] - t[ind] + k - 1) / k; ans.push_back({timer + 2 * (t[ind] - l[ind]), numtimes}); timer += t[ind]; long long int acc = k * numtimes - (2 * l[ind] - t[ind]); ind++; while (ind < n) { if (acc - l[ind] < 0) { timer += acc; break; } if (t[ind] < l[ind]) { flag = 0; break; } acc -= l[ind]; timer += l[ind]; ind++; } if (!flag || ind == n) break; excess = acc; } if (!flag) { cout << -1 << "\n"; return; } long long int tot = 0; for (pair<long long int, long long int> i : ans) tot += i.second; cout << tot << "\n"; if (tot <= 1e5) { for (pair<long long int, long long int> i : ans) { long long int cur = i.first; for (long long int j = (long long int)0; j < (long long int)i.second; ++j) { cout << cur << " "; cur += k; } } cout << "\n"; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t; t = 1; for (long long int i = (long long int)0; i < (long long int)t; ++i) testcases(i); }
7
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; for (int i = 0; i < s.length(); i++) { if (s[i] == '-' && s[i + 1] == '-') { cout << "2"; i++; } else if (s[i] == '-' && s[i + 1] == '.') { cout << "1"; i++; } else { cout << "0"; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:100000000") int matrix[900][900]; int tmp[900][900]; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; vector<pair<int, int> > all; void go(int x, int y) { queue<pair<int, int> > q; tmp[x][y] = 1; q.push(make_pair(x, y)); all.push_back(make_pair(x, y)); while (!q.empty()) { x = q.front().first; y = q.front().second; q.pop(); for (int i = 0; i < 4; i++) { if (x + dx[i] >= 0 && x + dx[i] <= 799 && y + dy[i] >= 0 && y + dy[i] <= 799) { if (matrix[x + dx[i]][y + dy[i]] != 255 && tmp[x + dx[i]][y + dy[i]] == 0) { tmp[x + dx[i]][y + dy[i]] = 1; q.push(make_pair(x + dx[i], y + dy[i])); all.push_back(make_pair(x + dx[i], y + dy[i])); } } } } } int path[10000]; int cnt = 0; void bfs(int x1, int y1, int x2, int y2) { for (int i = 0, maxi = (int)(all).size(); i < maxi; i++) { int x = all[i].first; int y = all[i].second; tmp[x][y] = 0; } all.clear(); queue<pair<int, int> > q; q.push(make_pair(x1, y1)); tmp[x1][y1] = 111; all.push_back(make_pair(x1, y1)); while (!q.empty()) { for (int i = 0, maxi = (int)(q).size(); i < maxi; i++) { int x = q.front().first; int y = q.front().second; q.pop(); if (x == x2 && y == y2) { cnt = 0; while (x != x1 || y != y1) { int j = tmp[x][y] - 1; path[cnt++] = j; x = x - dx[j]; y = y - dy[j]; } return; } for (int j = 0; j < 4; j++) { if (x + dx[j] >= 0 && x + dx[j] <= 799 && y + dy[j] >= 0 && y + dy[j] <= 799) { if (matrix[x + dx[j]][y + dy[j]] != 255 && tmp[x + dx[j]][y + dy[j]] == 0) { tmp[x + dx[j]][y + dy[j]] = j + 1; q.push(make_pair(x + dx[j], y + dy[j])); all.push_back(make_pair(x + dx[j], y + dy[j])); } } } } } } string ans = ""; int main() { int x1, y1, x2, y2; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); x1 += 400; y1 += 400; x2 += 400; y2 += 400; int m; scanf("%d", &m); int max_x = 0, min_x = 800, max_y = 0, min_y = 800; for (int i = 0; i < m; i++) { int x, y; scanf("%d %d", &x, &y); x += 400; y += 400; min_x = min(min_x, x); max_x = max(max_x, x); min_y = min(min_y, y); max_y = max(max_y, y); matrix[x][y] = 255; } go(x1, y1); if (m == 0 || tmp[x1][y1] != tmp[x2][y2]) { printf("-1"); return 0; } while (true) { if (x1 == x2 && y1 == y2) { cout << ans; return 0; } bool flag = false; if (min(y1, y2) > max_y) { while (max(x1, x2) >= min_x) { x1--; x2--; ans += "L"; } while (max(y1, y2) >= min_y) { y1--; y2--; ans += "D"; } flag = true; } else if (max(y1, y2) < min_y) { while (max(x1, x2) >= min_x) { x1--; x2--; ans += "L"; } while (max(y1, y2) >= min_y) { y1--; y2--; ans += "D"; } flag = true; } else if (min(x1, x2) > max_x) { while (max(y1, y2) >= min_y) { y1--; y2--; ans += "D"; } while (max(x1, x2) >= min_x) { x1--; x2--; ans += "L"; } flag = true; } else if (max(x1, x2) < min_x) { while (max(y1, y2) >= min_y) { y1--; y2--; ans += "D"; } while (max(x1, x2) >= min_x) { x1--; x2--; ans += "L"; } flag = true; } if (flag) { if (y2 > y1) { int y = min_y; int x; for (int i = 290; i < 510; i++) { if (matrix[i][y] == 255) { x = i; break; } } while (x2 < x) { x1++; x2++; ans += "R"; } while (y2 != y1) { y1++; if (matrix[x2][y2 + 1] != 255) y2++; ans += "U"; } } else { while (min(y1, y2) <= max_y) { y1++; y2++; ans += "U"; } int y = max_y; int x; for (int i = 290; i < 510; i++) { if (matrix[i][y] == 255) { x = i; break; } } while (x2 < x) { x1++; x2++; ans += "R"; } while (y2 != y1) { y1--; if (matrix[x2][y2 - 1] != 255) y2--; ans += "D"; } } if (x2 > x1) { while (max(x1, x2) >= min_x) { x2--; x1--; ans += "L"; } while (max(y1, y2) >= min_y) { y2--; y1--; ans += "D"; } int x = min_x; int y; for (int i = 290; i < 510; i++) { if (matrix[x][i] == 255) { y = i; break; } } while (y1 < y) { y1++; y2++; ans += "U"; } while (x2 != x1) { x1++; if (matrix[x2 + 1][y2] != 255) x2++; ans += "R"; } cout << ans; return 0; } else { while (min(x1, x2) <= max_x) { x1++; x2++; ans += "R"; } while (max(y1, y2) >= min_y) { y2--; y1--; ans += "D"; } int x = max_x; int y; for (int i = 290; i < 510; i++) { if (matrix[x][i] == 255) { y = i; break; } } while (y1 < y) { y1++; y2++; ans += "U"; } while (x2 != x1) { x1--; if (matrix[x2 - 1][y2] != 255) x2--; ans += "L"; } cout << ans; return 0; } } bfs(x1, y1, x2, y2); for (int i = cnt - 1; i >= 0; i--) { if (path[i] == 0) ans += "R"; else if (path[i] == 1) ans += "U"; else if (path[i] == 2) ans += "L"; else if (path[i] == 3) ans += "D"; int j = path[i]; x1 += dx[j]; y1 += dy[j]; if (matrix[x2 + dx[j]][y2 + dy[j]] != 255) { x2 += dx[j]; y2 += dy[j]; } } } return 0; }
11
#include <bits/stdc++.h> using namespace ::std; map<long long int, long long int> endmap; long long int a[1000000]; vector<pair<long long int, long long int> > v; int main() { long long int i, j, k, t, n, m, x, y, siz, l; cin >> t; while (t--) { cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; } v.clear(); for (i = 0; i < n;) { long long int val = a[i]; j = i + 1; while (j < n && a[j] == a[i]) { j++; } v.push_back(make_pair(val, j - i)); i = j; } long long int maxMedals = n / 2; long long int goldMedals = v[0].second; long long int silverMedals = 0, bronzeMedals = 0; j = 1; siz = v.size(); while (j < siz) { silverMedals += v[j].second; if (silverMedals > goldMedals) break; j++; } k = j + 1; while (k < siz) { bronzeMedals += v[k].second; if (bronzeMedals > goldMedals) break; k++; } long long int totalMedals = goldMedals + silverMedals + bronzeMedals; l = k + 1; while (l < siz) { if (totalMedals + v[l].second > n / 2) { break; } bronzeMedals += v[l].second; totalMedals += v[l].second; l++; } if (totalMedals <= n / 2 && goldMedals != 0 && silverMedals != 0 && bronzeMedals != 0) cout << goldMedals << " " << silverMedals << " " << bronzeMedals << endl; else cout << "0 0 0\n"; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, rt, a[1000050], sz[1000050]; long long dp[1000050], ans; vector<int> g[1000050]; map<int, long long> vis; void dfs(int first, int f) { sz[first] = 1, dp[first] = 1e18; if (f) g[first].erase(find(g[first].begin(), g[first].end(), f)); for (int second : g[first]) dfs(second, first), sz[first] += sz[second]; for (int second : g[first]) dp[first] = min(dp[first], (long long)(sz[first] - sz[second]) * (sz[first] - sz[second]) + dp[second]); if (dp[first] == 1e18) dp[first] = (long long)sz[first] * sz[first]; vis.clear(); for (int second : g[first]) { if (!vis.count(sz[second])) vis[sz[second]] = dp[second]; else { long long t = vis[sz[second]]; ans = min(ans, t + dp[second] + (long long)(n - 2 * sz[second]) * (n - 2 * sz[second])); vis[sz[second]] = min(t, dp[second]); } } for (auto i = vis.begin(); i != vis.end(); ++i) { for (auto j = i; j != vis.end(); ++j) if (j != i) { long long w = n - i->first - j->first; ans = min(ans, i->second + j->second + w * w); } } } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int u, v; scanf("%d%d", &u, &v); g[u].push_back(v), g[v].push_back(u); } if (n == 2) return puts("2"), 0; for (int i = 0; i < n; i++) if (g[i].size() > 1) rt = i; ans = 1e18; dfs(rt, 0); ans = ((long long)n * n - ans) / 2 + (long long)n * (n - 1) / 2; printf("%lld\n", ans); }
9
#include <bits/stdc++.h> using namespace std; string s; int n, dp[10][30][20]; int func(int pos, int rem, int x) { if (pos == n) { if (rem != 0) return 0; return 1; } if (dp[pos][rem][x] != -1) return dp[pos][rem][x]; int i, tot, tmp; if (pos) { if (s[pos] >= '0' && s[pos] <= '9') { int hi = rem; rem *= 10; rem += (s[pos] - '0'); rem %= 25; return dp[pos][hi][x] = func(pos + 1, rem, x); } if (s[pos] == '_') { tot = 0; for (i = 0; i < 10; i++) { tmp = rem * 10 + i; tmp %= 25; tot += func(pos + 1, tmp, x); } return dp[pos][rem][x] = tot; } if (x > 10) { tot = 0; for (i = 0; i < 10; i++) { tmp = rem * 10 + i; tmp %= 25; tot += func(pos + 1, tmp, i); } return dp[pos][rem][x] = tot; } tmp = rem * 10 + x; tmp %= 25; return dp[pos][rem][x] = func(pos + 1, tmp, x); } if (s[pos] == '0') return dp[pos][rem][x] = 0; if (s[pos] > '0' && s[pos] <= '9') { int hi = rem; rem *= 10; rem += (s[pos] - '0'); rem %= 25; return dp[pos][hi][x] = func(pos + 1, rem, x); } if (s[pos] == '_') { tot = 0; for (i = 1; i < 10; i++) { tmp = rem * 10 + i; tmp %= 25; tot += func(pos + 1, tmp, x); } return dp[pos][rem][x] = tot; } tot = 0; for (i = 1; i < 10; i++) { tmp = rem * 10 + i; tmp %= 25; tot += func(pos + 1, tmp, i); } return dp[pos][rem][x] = tot; } void solve() { memset(dp, -1, sizeof dp); cin >> s; n = s.size(); if (s == "0" || s == "_" || s == "X") { cout << 1 << endl; return; } cout << func(0, 0, 15) << endl; } int main() { int t = 1; while (t--) solve(); }
5
#include <bits/stdc++.h> using namespace std; long long int m, n, s1; long long int v[200100]; bool f(int mid) { long long int c = 0, sum = 0; for (int i = 0; i < n; ++i) { if (i && i % mid == 0) c++; sum += max((long long int)0, v[i] - c); } if (sum >= m) return true; else return false; } int main() { cin >> n >> m; for (int i = 0; i < n; ++i) { scanf("%lld", &v[i]); } sort(v, v + n); reverse(v, v + n); int l = 1; int r = n; int mid = (l + r) / 2; long long int ans = -1; while (l <= r) { mid = (l + r) / 2; if (f(mid)) { ans = mid; r = mid - 1; } else { l = mid + 1; } } printf("%lld\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n = 0; int c = 0; cin >> n; while (n > 0) { if (n % 100 == 0) { n = n - 100; c++; } else { if (n % 20 == 0) { n = n - 20; c++; } else { if (n % 10 == 0) { n = n - 10; c++; } else { if (n % 5 == 0) { n = n - 5; c++; } else { if (n % 1 == 0) { n = n - 1; c++; } } } } } } cout << c; }
0
/* Author: Zcus Nlog: https://cnblogs.com/zcus */ #include <bits/stdc++.h> using namespace std; template<class T> inline T read() { T s = 0, w = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') w = -1; c = getchar();} while (c >= '0' && c <= '9') { s = s * 10 + (c ^ 48); c = getchar();} return s * w; } #define fi first #define se second #define pb push_back #define mp make_pair #define ri read<int> #define pii pair<int, int> #define rl read<long long> #define debug(x) cout << #x << " = " << x << endl #define For(i, x, y) for (int ed = (y), i = (x); i <= ed; i++) #define Rep(i, x, y) for (int ed = (y), i = (x); i >= ed; i--) #define ddbug(x, y) cout << #x << " = " << x << " " << #y << " = " << y << "\n" #define file(FILE_NAME) freopen(FILE_NAME".in", "r", stdin), freopen(FILE_NAME".out", "w", stdout) #define filein(FILE_NAME) freopen(FILE_NAME".in", "r", stdin); #define fileout(FILE_NAME) freopen(FILE_NAME".out", "w", stdout); template<class T> inline bool Chkmax(T& x, const T& y) { return x < y ? x = y, true : false; } template<class T> inline bool Chkmin(T& x, const T& y) { return x > y ? x = y, true : false; } const int N = 251 * 501; const int maxn = 510; int n, Mod, dp[2][maxn * maxn]; int A[maxn]; int res[maxn * maxn]; int main() { n = ri(), Mod = ri(); A[0] = 1; for (int i = 1; i <= n; i++) A[i] = 1ll * A[i - 1] * (n - i + 1) % Mod; int ans = 0; dp[0][N] = 1; for (int i = 1; i < n; i++) { memset(dp[i & 1], 0, sizeof dp[i & 1]); int now = i & 1; int C = (i + 1) * (i + 1) / 2; for (int j = N - C; j <= N + C; j++) { int w = dp[now ^ 1][j]; (dp[now][j + i + 1] += w) %= Mod; if (j - i + 1 >= 0) (dp[now][j - i + 1] += w) %= Mod; (dp[now][j + 1] += 1ll * (Mod - 2) * w % Mod) %= Mod; } for (int zzm = 1; zzm <= 2; zzm++) { for (int j = N + C; j >= N - C; j--) { (dp[now][j] += dp[now][j + 1]) %= Mod; } for (int j = N - C; j <= N + C; j++) dp[now][j] = dp[now][j + 1]; } res[C + 1] = 0; for (int j = C; j >= 0; j--) res[j] = (res[j + 1] + dp[now][N + j]) % Mod; for (int a = 0; a <= i; a++) { for (int b = a + 1; b <= i; b++) { (ans += 1ll * A[n - i - 1] * res[b - a + 1] % Mod) %= Mod; } } } cout << ans << endl; return 0; }
9
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 1; const long long INF = 1LL << 62; int n, m; vector<int> G[maxn]; int a[maxn]; int b[maxn]; long long s[maxn]; int main() { scanf("%d%d", &n, &m); long long tp = 0; for (int i = 0; i < (m); i++) { scanf("%d", &a[i]); if (i) { G[a[i - 1]].push_back(a[i]); G[a[i]].push_back(a[i - 1]); tp += abs(a[i] - a[i - 1]); } } long long ans = tp; for (int i = 1; i <= n; i++) { long long tm = tp; int tot = 0; for (int j = 0; j < (G[i].size()); j++) if (G[i][j] != i) { b[tot++] = G[i][j]; tm -= abs(i - G[i][j]); } if (tot == 0) continue; sort(b, b + tot); int mid = tot / 2; for (int i = 0; i < (tot); i++) tm += abs(b[mid] - b[i]); ans = min(ans, tm); } cout << ans << endl; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int maxx = 1e9 + 10; long long a[55][55], b[55][55]; memset(a, maxx, sizeof(a)); memset(b, maxx, sizeof(b)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> a[i][j]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> b[i][j]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] > b[i][j]) { long long temp = a[i][j]; a[i][j] = b[i][j]; b[i][j] = temp; } } } bool flag = 1; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (a[i][j] >= a[i + 1][j] || a[i][j] >= a[i][j + 1] || b[i][j] >= b[i + 1][j] || b[i][j] >= b[i][j + 1]) { flag = 0; } } } if (flag) cout << "Possible" << endl; else cout << "Impossible" << endl; }
3
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int rmax = 60; bool bit(long long mask, int pos) { return (mask >> pos) & 1; } void mul(long long& a, long long b) { a *= b; if (a >= mod) { a %= mod; } } void add(long long& a, long long b) { a += b; if (a >= mod) { a -= mod; } } void sub(long long& a, long long b) { a -= b; if (a < 0) { a += mod; } } long long binpow(long long a, long long n) { long long ans = 1; while (n) { if (n & 1) { mul(ans, a); } n >>= 1; mul(a, a); } return ans; } struct data { long long a, b; data() : a(0), b(0) {} data(long long a, long long b) : a(a), b(b) {} data operator+(const data& other) const { long long new_a = a; add(new_a, other.a); long long new_b = b; add(new_b, other.b); return {new_a, new_b}; } data operator*(const data& other) const { long long val1 = a; mul(val1, other.b); long long val2 = b; mul(val2, other.a); add(val1, val2); long long new_b = val1; val1 = a; mul(val1, other.a); val2 = b; mul(val2, other.b); mul(val2, 5); add(val1, val2); return {val1, new_b}; } data operator-(const data& other) const { long long new_a = a; sub(new_a, other.a); long long new_b = b; sub(new_b, other.b); return {new_a, new_b}; } }; vector<vector<data> > dp; vector<vector<long long> > bin; long long r5 = binpow(5, mod - 2); data count_magic(long long n, int k) { if (n == 0) { return {0, 0}; } data res; for (int j = 0; j <= k; ++j) { data val = dp[k][j]; vector<data> sum(rmax); vector<data> degs(rmax); degs[0] = val; sum[0] = {1, 0}; for (int i = 1; i < rmax; ++i) { sum[i] = sum[i - 1] + degs[i - 1] * sum[i - 1]; degs[i] = degs[i - 1] * degs[i - 1]; } data ans; data curr = {1, 0}; for (int r = rmax - 1; r >= 0; --r) { if (bit(n + 1, r)) { ans = ans + sum[r] * curr; curr = curr * degs[r]; } } mul(ans.a, bin[k][j]); mul(ans.b, bin[k][j]); if ((k - j) & 1) { ans.a = mod - ans.a; ans.b = mod - ans.b; } long long r = binpow(r5, k / 2); mul(ans.a, r); mul(ans.b, r); if (k & 1) { long long new_a = ans.b; long long new_b = ans.a; mul(new_b, r5); ans = {new_a, new_b}; } res = res + ans; } return res; } vector<long long> mul(const vector<long long>& a, const vector<long long>& b) { int n = (int)(a).size() - 1, m = (int)(b).size() - 1; vector<long long> c(n + m + 1); for (int i = 0; i <= n; ++i) { for (int j = 0; j <= m; ++j) { long long val = a[i]; mul(val, b[j]); add(c[i + j], val); } } return c; } int main() { int k; long long l, r; cin >> k >> l >> r; l += 2, r += 2; dp = vector<vector<data> >(k + 1, vector<data>(k + 1)); vector<data> degs_plus(k + 1), degs_minus(k + 1); degs_plus[0] = degs_minus[0] = {1, 0}; long long r2 = binpow(2, mod - 2); for (int i = 1; i <= k; ++i) { data tmp = {r2, r2}; degs_plus[i] = degs_plus[i - 1] * tmp; tmp = {r2, mod - r2}; degs_minus[i] = degs_minus[i - 1] * tmp; } for (int i = 0; i <= k; ++i) { for (int j = 0; j <= i; ++j) { dp[i][j] = degs_plus[j] * degs_minus[i - j]; } } bin = vector<vector<long long> >(k + 1, vector<long long>(k + 1)); bin[0][0] = 1; for (int i = 1; i <= k; ++i) { bin[i][0] = 1; for (int j = 1; j <= k; ++j) { bin[i][j] = bin[i - 1][j]; add(bin[i][j], bin[i - 1][j - 1]); } } if (l > r) { cout << "0\n"; return 0; } vector<long long> p = {0, 1}; for (int i = 2; i <= k; ++i) { long long t = binpow(i, mod - 2); vector<long long> q(2); q[1] = t; q[0] = mod - t; mul(q[0], i - 1); vector<long long> r = mul(p, q); p = r; } data ans; for (int i = 0; i <= k; ++i) { data val = count_magic(r, i); mul(val.a, p[i]); mul(val.b, p[i]); ans = ans + val; val = count_magic(l - 1, i); mul(val.a, p[i]); mul(val.b, p[i]); ans = ans - val; } cout << ans.a << "\n"; }
10
#include <bits/stdc++.h> 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; } constexpr long long MAX = 5100000; constexpr long long INF = 1LL << 60; constexpr int inf = 1 << 28; constexpr long long mod = 998244353LL; using namespace std; struct mint { long long x; mint(long long x = 0) : x((x % mod + mod) % mod) {} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod - a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; } mint operator+(const mint a) const { mint res(*this); return res += a; } mint operator-(const mint a) const { mint res(*this); return res -= a; } mint operator*(const mint a) const { mint res(*this); return res *= a; } mint pow(long long t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } mint inv() const { return pow(mod - 2); } mint& operator/=(const mint a) { return (*this) *= a.inv(); } mint operator/(const mint a) const { mint res(*this); return res /= a; } }; mint solve(vector<long long>& v, vector<vector<mint>>& dp) { mint res = 1; int left = -1; for (int i = 0; i < v.size(); i++) { if (v[i] != -1) { if (left == -1) { res *= dp[2][i]; } else if (v[left] == v[i]) { res *= dp[1][i - left - 1]; } else { res *= dp[0][i - left - 1]; } left = i; } } if (left == -1) res *= dp[3][v.size()]; else res *= dp[2][(int)v.size() - left - 1]; return res; } int main() { long long n, k; scanf("%lld %lld", &n, &k); vector<vector<mint>> dp(4, vector<mint>(n + 1)); dp[0][0] = dp[2][0] = dp[3][0] = 1; for (int len = 1; len <= n; len++) { dp[0][len] = (dp[1][len - 1] + dp[0][len - 1] * (k - 2)); dp[1][len] = dp[0][len - 1] * (k - 1); dp[2][len] = dp[2][len - 1] * (k - 1); dp[3][len] = dp[2][len - 1] * k; } vector<long long> even, odd; for (int i = 0; i < n; i++) { long long x; scanf("%lld", &x); if (i % 2 == 0) even.push_back(x); else odd.push_back(x); } mint res = solve(odd, dp) * solve(even, dp); cout << res.x << "\n"; return 0; }
7
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; long long quick_mod(long long a, long long b, long long m) { long long ans = 1; while (b) { if (b & 1) { ans = (ans * a) % m; b--; } b /= 2; a = a * a % m; } return ans; } int main() { char s[111111]; int n, k; long long ans, q; while (cin >> s >> k) { ans = 0; n = strlen(s); q = quick_mod(2, n, MOD); for (int i = 0; i < n; i++) { if (s[i] == '0' || s[i] == '5') { ans += quick_mod(2, i, MOD) % MOD * (quick_mod(q, k, MOD) - 1 + MOD) % MOD * quick_mod(q - 1, MOD - 2, MOD) % MOD; ans %= MOD; } } cout << ans << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int aaa, bbb; int main() { cin >> aaa >> bbb; cout << aaa + bbb; return 0; }
3
#include <bits/stdc++.h> using namespace std; template <class T> inline T gcd(T a, T b) { return (b) == 0 ? (a) : gcd((b), ((a) % (b))); } template <class T> inline T lcm(T a, T b) { return ((a) / gcd((a), (b)) * (b)); } template <class T> inline T BigMod(T Base, T power, T M = 1000000007) { if (power == 0) return 1; if (power & 1) return ((Base % M) * (BigMod(Base, power - 1, M) % M)) % M; else { T y = BigMod(Base, power / 2, M) % M; return (y * y) % M; } } template <class T> inline T ModInv(T A, T M = 1000000007) { return BigMod(A, M - 2, M); } int fx[] = {-1, +0, +1, +0, +1, +1, -1, -1, +0}; int fy[] = {+0, -1, +0, +1, +1, -1, +1, -1, +0}; int day[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; vector<long long> arr(500005 + 1); int n; bool ok(int k) { for (int i = 1; i <= n - k + 1; i++) { if (arr[i + k - 1] - arr[i - 1] <= 0) return false; } return true; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<long long> a(n); for (int i = 0; i < (n + 1) / 2; i++) { cin >> a[i]; } long long x; cin >> x; for (int i = (n + 1) / 2; i < n; i++) { a[i] = x; } vector<long long> pre(n + 1); for (int i = 1; i <= n; i++) { pre[i] = pre[i - 1] + a[i - 1]; } if (pre[n] > 0) { cout << n << endl; return 0; } else if (a[n - 1] >= 0) { cout << -1 << endl; return 0; } else { long long mx = -1e18; for (int i = 0; i < (n + 1) / 2; i++) { mx = max(mx, pre[i] + x * (n - i) - pre[n]); if (mx < x * (n - i)) { cout << n - i << endl; return 0; } } } cout << -1 << endl; return 0; }
8
#include <bits/stdc++.h> int A[100000 + 200]; int B[100000 + 200]; char s[100000 + 200]; void input() { int n; scanf("%s", &s); n = strlen(s); for (int i = 0; i < n; i++) A[n - 1 - i] = (int)s[i] - 48; scanf("%s", &s); n = strlen(s); for (int i = 0; i < n; i++) B[n - 1 - i] = (int)s[i] - 48; } int min(int a, int b) { if (a < b) return a; return b; } void solve() { for (int i = 100000 - 1; i >= 0; i--) if (A[i] == B[i]) { A[i] = 0; B[i] = 0; } for (int i = 100000 - 1; i >= 0; i--) { int res = min(A[i], B[i]); A[i] -= res; B[i] -= res; if (i <= 1 && A[i] == 1) { printf(">"); return; } if (i <= 1 && B[i] == 1) { printf("<"); return; } if (A[i] >= 2) { printf(">"); return; } if (B[i] >= 2) { printf("<"); return; } if (A[i] >= 1 && i - 2 >= 0) { A[i - 1]++; A[i - 2]++; } if (B[i] >= 1 && i - 2 >= 0) { B[i - 1]++; B[i - 2]++; } } printf("="); } int main() { input(); solve(); }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, sum1 = 0, sum2 = 0; string s; cin >> n >> s; for (int i = 0; i < n / 2; i++) { if (s[i] == '4' || s[i] == '7') { sum1 += s[i] - '0'; continue; } cout << "NO" << endl; return 0; } for (int i = n / 2; i < n; i++) { if (s[i] == '4' || s[i] == '7') { sum2 += s[i] - '0'; continue; } cout << "NO" << endl; return 0; } if (sum1 != sum2) cout << "NO" << endl; else cout << "YES" << endl; return 0; }
0
#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 = 3005; int T, n, a[N], b[N]; int main() { for (read(T); T; T--) { read(n); for (int i = 1; i <= n; i++) read(a[i]), b[i] = a[i]; sort(b + 1, b + n + 1); int len = unique(b + 1, b + n + 1) - (b + 1); for (int i = 1; i <= n; i++) a[i] = lower_bound(b + 1, b + n + 1, a[i]) - b; int now = 1, mx = 0; while (now <= n) { int len = 0; for (int i = 1; i <= n; i++) if (a[i] == now) len++, now++; mx = max(mx, len); } printf("%d\n", n - mx); } return 0; }
6
#include <bits/stdc++.h> using namespace std; int main(void) { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(14); int t; cin >> t; while (t--) { int n; long long W; cin >> n >> W; vector<tuple<long long, int>> a(n); for (int i = 0; i < n; i++) { long long x; cin >> x; a[i] = {x, i + 1}; } sort(a.begin(), a.end(), greater<tuple<long long, int>>()); long long curr_sum = 0; vector<int> sol; for (int i = 0; i < n; i++) { if (curr_sum + get<0>(a[i]) <= W) { sol.push_back(get<1>(a[i])); curr_sum += get<0>(a[i]); } } if (curr_sum < (W + 1) / 2) { cout << "-1\n"; } else { cout << sol.size() << "\n"; for (auto el : sol) cout << el << " "; cout << "\n"; } } }
2
#include <bits/stdc++.h> using namespace std; int n; int s[500001], e[500001]; bool sp[1000001], ep[1000001]; int petaa[1000001]; int par[1000001]; vector<pair<int, int> > pr; int findRepresentative(int n) { if (par[n] == n) { return n; } else { par[n] = findRepresentative(par[par[n]]); return par[n]; } } bool cmp(pair<int, int> a, pair<int, int> b) { return (a.first < b.first); } void input() { scanf("%d", &n); int s, e; pr.push_back(make_pair(0, 0)); for (int i = 1; i <= n; i++) { scanf("%d %d", &s, &e); pr.push_back(make_pair(s, e)); } sort(pr.begin(), pr.end(), cmp); for (int i = 1; i <= n; i++) { par[i] = i; petaa[pr[i].first] = i; petaa[pr[i].second] = i; } } void join(int a, int b) { int repA = findRepresentative(a); int repB = findRepresentative(b); par[repB] = repA; } void solve() { set<int> st; int now; int edges = 0; for (int i = 1; i <= n; i++) { pair<int, int> now = pr[i]; int left = now.first; int right = now.second; for (auto it = st.lower_bound(left); it != st.end(); ++it) { if (*it > right) { break; } if (*it < right) { join(petaa[*it], i); edges++; } if (edges >= n) { printf("NO\n"); return; } } st.insert(right); } if (edges >= n || edges < (n - 1)) { printf("NO\n"); return; } int d = 0; for (int i = 1; i <= n; i++) { if (par[i] == i) { d++; } } cout << ((d == 1) ? "YES" : "NO") << endl; } int main() { input(); solve(); }
6
#include <bits/stdc++.h> using namespace std; int main() { int n, tmp; int t; cin >> t; bool cek[51]; while (t--) { cin >> n; memset(cek, false, sizeof(cek)); for (int i = 0; i < 2 * n; i++) { cin >> tmp; if (cek[tmp] == false) { cout << tmp << " "; cek[tmp] = true; } else { continue; } } cout << endl; } }
0
#include <bits/stdc++.h> using namespace std; int ROW[] = {+0, +0, -1, +1}; int COL[] = {+1, -1, +0, +0}; int X[] = {+0, +0, +1, -1, -1, +1, -1, +1}; int Y[] = {-1, +1, +0, +0, +1, +1, -1, -1}; int KX[] = {-2, -2, -1, -1, 1, 1, 2, 2}; int KY[] = {-1, 1, -2, 2, -2, 2, -1, 1}; int main() { int n; scanf("%d", &n); vector<int> v(n); for (int i = 0; i <= n - 1; i++) scanf("%d", &v[i]); for (int i = 0; i <= n - 1; i++) for (int j = 0; j <= n - 1; j++) for (int k = 0; k <= n - 1; k++) if (v[i] + v[j] == v[k] && i != j && j != k && i != k) { cout << k + 1 << ' ' << j + 1 << ' ' << i + 1 << endl; return 0; } cout << -1 << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int digit[105]; int n, m, k; string board[105]; string score() { scanf("%d %d %d", &n, &m, &k); if (m == 1) return "Second"; for (int i = 0; i < n; i++) cin >> board[i]; int na = 0, nb = 0, ea = 0, eb = 0; for (int i = 0; i < n; i++) { int adda = 0, addb = 0; int ga = -1, gb = -1; for (int j = 0; j < m; j++) if (board[i][j] == 'G') adda++, ga = j; else if (board[i][j] == 'R') addb++, gb = j; if (!adda && !addb) continue; if (!addb) { if (adda < m) ea++, na += adda; } else if (!adda) { if (addb < m) eb++, nb += addb; } else { na++; nb++; if (ga > gb) swap(ga, gb); int dist = gb - ga - 1; for (int j = 0; dist > 0; j++) { digit[j] += dist % 2; dist /= 2; } } } if (!na) return "Second"; if (!nb) return "First"; if (ea && eb) return "Draw"; if (ea) return "First"; if (eb) return "Second"; for (int i = 0; i < 100; i++) if (digit[i] % (k + 1)) return "First"; return "Second"; } int main() { cout << score() << endl; }
9
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 10; bool used[MAXN]; int cnt[MAXN]; int to[MAXN]; const int MOD = 1e9 + 7; int main() { int p, k; cin >> p >> k; for (int i = 0; i < p; ++i) { if (!used[i]) { int len = 0; for (long long j = i; !used[j]; j = (k * j) % p) { ++len; assert(len <= p); used[j] = true; } ++cnt[len]; } } for (int i = 1; i <= p; ++i) { for (int j = i; j <= p; j += i) { assert(cnt[i] * i <= p); to[j] += i * cnt[i]; assert(to[j] <= p); } } long long ans = 1; if (k == 0) { for (int i = 1; i < p; ++i) { ans = (ans * p) % MOD; } } else { for (int i = 1; i <= p; ++i) { for (int j = 0; j < cnt[i]; ++j) { ans = (ans * to[i]) % MOD; } } } cout << ans << '\n'; }
5
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC target( \ "fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") #pragma GCC optimize("unroll-loops") template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; } template <typename T, size_t size> ostream &operator<<(ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; } template <typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); } const int MAX_N = 2e6 + 5; int dp[MAX_N]; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, k, mn = 1e9; cin >> n >> k; for (int i = 0; i < n; i++) { int foo; cin >> foo; mn = min(mn, foo); dp[foo] = 1; } int where = -1; for (int i = 0; i < MAX_N; i++) { if (dp[i]) { where = i; } dp[i] = where; } int best_way = 1; for (int i = 1; i <= mn; i++) { for (int j = 1;; j++) { const int lo = i * (j - 1); const int hi = i * j; if (hi >= MAX_N) break; int s = dp[hi - 1]; if (s == -1 || s <= lo) continue; int lim = s - k; if (lim > lo) { 47; goto invalid; } } best_way = i; invalid:; } cout << best_way << '\n'; return 0; }
6
#include <bits/stdc++.h> using namespace std; long long maxint = 1e9 + 7; long long n; string st; void openFile() { freopen("inp.txt", "r", stdin); } void solve() { cin >> n; cin.ignore(1); cin >> st; long long px = -1; long long res = maxint, x; for (int i = 0; i < n; i++) { cin >> x; if (st[i] == 'R') { px = x; } else { if (px >= 0) res = min(res, (x - px) / 2); } } res == maxint ? cout << -1 : cout << res; } int main() { ios::sync_with_stdio(false); int ntest; solve(); return 0; }
1
#include <bits/stdc++.h> char c[200]; int i, n, s; int main() { gets(c + 1); n = strlen(c + 1); for (i = 1; i <= n; i++) { if ((c[i] == 'D') && (c[i + 1] == 'a') && (c[i + 2] == 'n') && (c[i + 3] == 'i') && (c[i + 4] == 'l')) s++; if ((c[i] == 'O') && (c[i + 1] == 'l') && (c[i + 2] == 'y') && (c[i + 3] == 'a')) s++; if ((c[i] == 'S') && (c[i + 1] == 'l') && (c[i + 2] == 'a') && (c[i + 3] == 'v') && (c[i + 4] == 'a')) s++; if ((c[i] == 'A') && (c[i + 1] == 'n') && (c[i + 2] == 'n')) s++; if ((c[i] == 'N') && (c[i + 1] == 'i') && (c[i + 2] == 'k') && (c[i + 3] == 'i') && (c[i + 4] == 't') && (c[i + 5] == 'a')) s++; } if (s == 1) puts("YES"); else puts("NO"); return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; char s[100005]; int n; long long total_mod = 1, to_add; int main() { bool ok = false; cin >> (s + 1); for (int i = 1; s[i]; ++i) if (s[i] == 'b' && ok || s[i] == 'a') ok = true, s[++n] = s[i]; s[n + 1] = 0; for (int i = 1; s[i]; ++i) { if (s[i] == 'a') ++to_add; else total_mod = (total_mod * (to_add + 1)) % MOD, to_add = 0; } total_mod = (total_mod * (to_add + 1) + MOD - 1) % MOD; cout << total_mod; return 0; }
3
#include <bits/stdc++.h> using namespace std; long long int viz[1234567]; long long int best = 0; int main() { long long int n, a; cin >> n >> a; for (int i = 0; i < n; i++) { long long int x; cin >> x; if (x == a) { viz[a]++; if (viz[a] > viz[best]) { return cout << -1, 0; } } else if (viz[x] >= viz[a]) { viz[x]++; if (viz[x] >= viz[best]) best = x; } } cout << best << endl; }
4
#include <bits/stdc++.h> using namespace std; template <class T1> void debug(T1 e) { cout << e << endl; } template <class T1, class T2> void debug(T1 e1, T2 e2) { cout << e1 << "\t" << e2 << endl; } template <class T1, class T2, class T3> void debug(T1 e1, T2 e2, T3 e3) { cout << e1 << "\t" << e2 << "\t" << e3 << endl; } template <class T1, class T2, class T3, class T4> void debug(T1 e1, T2 e2, T3 e3, T4 e4) { cout << e1 << "\t" << e2 << "\t" << e3 << "\t" << e4 << endl; } template <class T1, class T2, class T3, class T4, class T5> void debug(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) { cout << e1 << "\t" << e2 << "\t" << e3 << "\t" << e4 << "\t" << e5 << endl; } template <class T1, class T2, class T3, class T4, class T5, class T6> void debug(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) { cout << e1 << "\t" << e2 << "\t" << e3 << "\t" << e4 << "\t" << e5 << "\t" << e6 << endl; } template <class T> void debug(vector<vector<T> > e, int row, int col) { int i, j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) cout << e[i][j] << " "; cout << endl; } cout << endl; } template <class T> void debug(vector<basic_string<T> > e, int row, int col) { int i, j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) cout << e[i][j]; cout << endl; } cout << endl; } template <class T> void debug(T e[110][110], int row, int col) { int i, j; for (i = 0; i < row; i++) { for (j = 0; j < col; j++) cout << e[i][j] << " "; cout << endl; } } template <class T> void setmax(T &a, T b) { if (a < b) a = b; } template <class T> void setmin(T &a, T b) { if (b < a) a = b; } template <class T> T Abs(T x) { return x > 0 ? x : -x; } template <class T> inline T sqr(T x) { return x * x; } template <class T> inline T Mod(T n, T m) { return (n % m + m) % m; } template <class T> string toString(T n) { ostringstream oss; oss << n; oss.flush(); return oss.str(); } int toInt(string s) { int r = 0; istringstream sin(s); sin >> r; return r; } bool isVowel(char ch) { ch = tolower(ch); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') return true; return false; } bool isUpper(char c) { return c >= 'A' && c <= 'Z'; } bool isLower(char c) { return c >= 'a' && c <= 'z'; } long long Pow(long long B, long long P) { long long R = 1; while (P > 0) { if (P % 2 == 1) R = (R * B); P /= 2; B = (B * B); } return R; } int BigMod(long long B, long long P, long long M) { long long R = 1; while (P > 0) { if (P % 2 == 1) { R = (R * B) % M; } P /= 2; B = (B * B) % M; } return (int)R; } void binprint(long long mask, long long n) { long long i; string s = ""; do { s += (mask % 2 + '0'); mask /= 2; } while (mask); reverse(s.begin(), s.end()); s = string(max(n - (int)s.size(), 0LL), '0') + s; for (i = (int)s.size() - n; i < (int)s.size(); i++) printf("%c", s[i]); printf("\n"); } void ASCII_Chart() { int i, j, k; printf("ASCII Chart:(30-129)\n"); for (i = 30; i < 50; i++) { for (j = 0; j < 5; j++) { k = i + j * 20; printf("%3d---> '%c' ", k, k); } printf("\n"); } } int dp[110][220][2][55]; string s; int rec(int cur, int dist, int dir, int rem) { if (cur == (int)s.size()) { if (rem == 0) return abs(110 - dist); else return -(1 << 28); } int &ret = dp[cur][dist][dir][rem]; if (ret != -1) return ret; ret = 0; if (s[cur] == 'T') ret = max(ret, rec(cur + 1, dist, -1 * dir, rem)); if (s[cur] == 'F') ret = max(ret, rec(cur + 1, dist + dir, dir, rem)); if (rem) { for (int i = 1; i <= rem; i++) { if (i % 2 == 1) { if (s[cur] == 'T') ret = max(ret, rec(cur + 1, dist + dir, dir, rem - i)); else ret = max(ret, rec(cur + 1, dist, -1 * dir, rem - i)); } else { if (s[cur] == 'T') ret = max(ret, rec(cur + 1, dist, -1 * dir, rem - i)); else ret = max(ret, rec(cur + 1, dist + dir, dir, rem - i)); } } } return ret; } int main() { int test, Case = 1, i, j, ans, N; char str[220]; while (scanf("%s", str) == 1) { s = str; scanf("%d", &N); memset(dp, -1, sizeof(dp)); ans = rec(0, 110, 1, N); printf("%d\n", ans); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int mxn = 2048; int n, mn, ans, w[mxn][mxn], a[mxn], b[mxn]; char s[mxn]; void rev(int u) { for (int i = 1; i <= n; ++i) { w[u][i] ^= 1, w[i][u] ^= 1; if (w[u][i]) ++a[u], --a[i]; else --a[u], ++a[i]; } } bool cmp(const int &x, const int &y) { return a[x] < a[y]; } bool check() { int sum = 0; sort(b + 1, b + n + 1, cmp); for (int i = 1; i < n; ++i) { sum += a[b[i]]; if (sum == i * (i - 1) / 2) return 0; } return 1; } void dfs(int x, int cnt) { if (cnt > mn) return; if (x > n) { if (check()) if (cnt < mn) mn = cnt, ans = 1; else ++ans; else ; return; } rev(x), dfs(x + 1, cnt + 1), rev(x); dfs(x + 1, cnt); } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%s", s); for (int j = 1; j <= n; ++j) w[i][j] = s[j - 1] - '0', a[i] += w[i][j]; b[i] = i; } if (check()) return puts("0 1"), 0; if (n <= 6) { mn = n; dfs(1, 0); for (int i = 1; i <= mn; ++i) ans *= i; if (!ans) puts("-1"); else printf("%d %d\n", mn, ans); return 0; } for (int i = 1; i <= n; ++i) rev(i), ans += check(), rev(i); if (!ans) puts("-1"); else printf("1 %d\n", ans); return 0; }
12
#include <bits/stdc++.h> using namespace std; const long long int zero = 0LL; const int N = 3e5 + 10; const long long int oo = 1e5; const double pi = acos(-1); const int md = 1e9 + 7; int t, n; multiset<int> mst; int main() { scanf("%d", &t); while (t--) { scanf("%d", &n); if (n == 2) { puts("1\n0"); continue; } long long int sum = 0; for (int i = 0; i < 32; i++) { sum += 1 << i; if (sum > n) { sum -= 1 << i; break; } mst.insert(1 << i); } if (n - sum) mst.insert(n - sum); printf("%d\n", mst.size() - 1); int pre = 1; bool shift = 0; for (multiset<int>::iterator it = mst.begin(); it != mst.end(); it++) { if (*it == 1 && !shift) { shift = 1; continue; } printf("%d ", *it - pre); pre = *it; } puts(""); mst.clear(); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int sqbf[3][3]; void Check(int n, vector<int> order) { int tot = 0; for (int(i) = 0; (i) < (n * n); ++(i)) tot += order[i]; int piece = tot / n; for (int(i) = 0; (i) < (n); ++(i)) for (int(j) = 0; (j) < (n); ++(j)) sqbf[i][j] = order[i * n + j]; for (int(i) = 0; (i) < (n); ++(i)) { int sum1 = 0; int sum2 = 0; for (int(j) = 0; (j) < (n); ++(j)) { sum1 += sqbf[i][j]; sum2 += sqbf[j][i]; } if (sum1 != piece || sum2 != piece) return; } int sum1 = 0; int sum2 = 0; for (int(i) = 0; (i) < (n); ++(i)) { sum1 += sqbf[i][i]; sum2 += sqbf[i][n - i - 1]; } if (sum1 != piece || sum2 != piece) return; cout << piece << endl; for (int(i) = 0; (i) < (n); ++(i)) { for (int(j) = 0; (j) < (n); ++(j)) { if (j) cout << " "; cout << sqbf[i][j]; } cout << endl; } exit(0); } char yes[205000000]; int vals[18]; int jml; int sq[18]; int wan; void DfsDone() { cout << (wan - 400000000) << endl; for (int(i) = 0; (i) < (4); ++(i)) { for (int(j) = 0; (j) < (4); ++(j)) { if (j) cout << " "; cout << (sq[i * 4 + j] - 100000000); } cout << endl; } exit(0); } void Dfs12() { if (sq[12] + sq[9] + sq[6] + sq[3] != wan) return; if (sq[4] + sq[5] + sq[6] + sq[7] != wan) return; DfsDone(); } void Dfs11() { int mau = wan - sq[0] - sq[8] - sq[12]; if (mau < 0 || mau > 200000000 || !yes[mau]) return; yes[mau]--; sq[4] = mau; Dfs12(); yes[mau]++; } void Dfs10() { int mau = wan - sq[13] - sq[14] - sq[15]; if (mau < 0 || mau > 200000000 || !yes[mau]) return; yes[mau]--; sq[12] = mau; Dfs11(); yes[mau]++; } void Dfs9() { int mau = wan - sq[2] - sq[6] - sq[10]; if (mau < 0 || mau > 200000000 || !yes[mau]) return; yes[mau]--; sq[14] = mau; Dfs10(); yes[mau]++; } void Dfs8() { int mau = wan - sq[0] - sq[1] - sq[3]; if (mau < 0 || mau > 200000000 || !yes[mau]) return; yes[mau]--; sq[2] = mau; for (int i = 0; i < jml; ++i) { if (yes[vals[i]]) { sq[6] = vals[i]; yes[vals[i]]--; Dfs9(); yes[vals[i]]++; } } yes[mau]++; } void Dfs7b() { int mau = wan - sq[7] - sq[11] - sq[15]; if (mau < 0 || mau > 200000000 || !yes[mau]) return; yes[mau]--; sq[3] = mau; Dfs8(); yes[mau]++; } void Dfs7() { int mau = wan - sq[8] - sq[9] - sq[10]; if (mau < 0 || mau > 200000000 || !yes[mau]) return; yes[mau]--; sq[11] = mau; for (int i = 0; i < jml; ++i) { if (yes[vals[i]]) { sq[7] = vals[i]; yes[vals[i]]--; Dfs7b(); yes[vals[i]]++; } } yes[mau]++; } void Dfs6() { int mau = wan - sq[5] - sq[9] - sq[13]; if (mau < 0 || mau > 200000000 || !yes[mau]) return; yes[mau]--; sq[1] = mau; for (int i = 0; i < jml; ++i) { if (yes[vals[i]]) { sq[8] = vals[i]; yes[vals[i]]--; Dfs7(); yes[vals[i]]++; } } yes[mau]++; } void Dfs5() { for (int i = 0; i < jml; ++i) { if (yes[vals[i]]) { sq[13] = vals[i]; yes[vals[i]]--; Dfs6(); yes[vals[i]]++; } } } void Dfs4() { int mau = wan - sq[5] - sq[10] - sq[15]; if (mau < 0 || mau > 200000000 || !yes[mau]) return; yes[mau]--; sq[0] = mau; for (int i = 0; i < jml; ++i) { if (yes[vals[i]]) { sq[9] = vals[i]; yes[vals[i]]--; Dfs5(); yes[vals[i]]++; } } yes[mau]++; } void Dfs3() { for (int i = 0; i < jml; ++i) { if (yes[vals[i]]) { sq[15] = vals[i]; yes[vals[i]]--; Dfs4(); yes[vals[i]]++; } } } void Dfs2() { for (int i = 0; i < jml; ++i) { if (yes[vals[i]] && vals[i] >= sq[5]) { sq[10] = vals[i]; yes[vals[i]]--; Dfs3(); yes[vals[i]]++; } } } void Dfs1() { for (int i = 0; i < jml; ++i) { sq[5] = vals[i]; yes[vals[i]]--; Dfs2(); yes[vals[i]]++; } } int main() { int n; cin >> n; if (n <= 3) { vector<int> z; for (int(i) = 0; (i) < (n * n); ++(i)) { int a; cin >> a; z.push_back(a); } sort(z.begin(), z.end()); do { Check(n, z); } while (next_permutation(z.begin(), z.end())); exit(0); } long long sub = 0LL; for (int(i) = 0; (i) < (n * n); ++(i)) { int z; cin >> z; z += 100000000; yes[z]++; vals[i] = z; sub += (long long)z; } wan = (int)((long long)sub / (long long)n); for (int(i) = 0; (i) < (16); ++(i)) sq[i] = 100000000; sort(vals, vals + n * n); jml = unique(vals, vals + n * n) - vals; srand(1234567); random_shuffle(vals, vals + jml); Dfs1(); }
3
#include <bits/stdc++.h> using namespace std; const int nn = 100005; int n, l, s; int a[nn], b[nn], c[nn], d[nn]; long long s1, s2; int r1[nn], r2[nn]; bool cmp(int x, int y) { return d[x] < d[y]; } long long work(int *r) { long long u = 0, v = 0, w = 0; for (int i = 1; i < n; ++i) d[i] = a[i + 1] - a[i]; memset(b, 0, sizeof(b)); memset(c, 0, sizeof(c)); if (l >= s - 1) { for (int i = s + 1; i < n; ++i) b[i - s] = i; sort(b + 1, b + n - s, cmp); int k = l - s + 1, j = k; for (int i = 1; i <= k; ++i) c[b[i]] = 1, u += 2 * d[b[i]]; v = u; w = 0; for (int i = 1; i <= k; ++i) { if (c[n - i] > 0) { u -= d[n - i]; c[n - i] = -i; } else { for (; c[b[j]] <= 0 && j; --j) ; u += d[n - i] - 2 * d[b[j]]; c[b[j--]] = -i; } if (u < v) v = u, w = i; } for (int i = 1; i <= s; ++i) r[i] = s - i + 1; j = s; for (int i = s + 1; i < n - w; ++i) if (c[i] < -w) { int ti = i; for (++i; c[i] < -w; ++i) ; r[++j] = i; for (int tj = i - 1; tj >= ti; --tj) r[++j] = tj; } else r[++j] = i; for (int i = 0; i <= w; ++i) r[++j] = n - i; } else { for (int i = 1; i <= l; ++i) r[i] = s - i + 1; for (int i = l + 1; i <= s; ++i) r[i] = i - l; for (int i = s + 1; i <= n; ++i) r[i] = i; } return v + a[n] + a[s]; } int main() { scanf("%d%d%d", &n, &l, &s); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); if (l == 0) { if (s == 1) { printf("%d\n", a[n]); for (int i = 2; i < n; ++i) printf("%d ", i); printf("%d\n", n); } else puts("-1"); return 0; } if (l == n - 1) { if (s == n) { printf("%d\n", a[n]); for (int i = n - 1; i > 1; --i) printf("%d ", i); puts("1"); } else puts("-1"); return 0; } s1 = work(r1); l = n - 1 - l; s = n + 1 - s; reverse(a + 1, a + n + 1); for (int i = n; i; --i) a[i] = a[1] - a[i]; s2 = work(r2); if (s1 < s2) { printf("%I64d\n", s1); for (int i = 2; i < n; ++i) printf("%d ", r1[i]); printf("%d\n", r1[n]); } else { printf("%I64d\n", s2); for (int i = 2; i < n; ++i) printf("%d ", n - r2[i] + 1); printf("%d\n", n - r2[n] + 1); } return 0; }
10
#include <bits/stdc++.h> using namespace std; int b[100001] = {}, c[100001] = {}; int main() { int n; while (cin >> n) { int a[n], x = 0; for (int i = 0; i < n; i++) { cin >> a[i]; b[a[i]]++; } for (int i = 0; i < n; i++) { c[a[i]]++; if (c[a[i]] > 1 || a[i] > n) { while (1) { x++; if (b[x] == 0) { a[i] = x; break; } } } } for (int i = 0; i < n; i++) { cout << a[i] << " "; } cout << endl; } }
2
#include <bits/stdc++.h> using namespace std; using namespace std; const int MaxN = 309, NA = -1, MaxC = 0x3F3F3F3F; int p[MaxN][MaxN]; int q[MaxN][MaxN]; int r[MaxN]; int n; int main(void) { int i, j, k, u; while (scanf(" %d", &n) != EOF) { memset(q, MaxC, sizeof(q)); for (i = 0; i <= n; i++) { for (j = 0; j < n; j++) { scanf(" %d", &p[i][j]); p[i][j]--; } for (j = 0; j < n; j++) q[i][p[i][j]] = j; } for (i = 0; i < n; i++) r[i] = n; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { u = p[i][j]; if (u == i) continue; for (k = 0; k < n; k++) if (p[n][k] != i && p[n][k] <= u) break; assert(k < n); if (p[n][k] == u) { r[i] = u; break; } } assert(j < n); } for (i = 0; i < n; i++) printf("%d%c", r[i] + 1, i + 1 < n ? ' ' : '\n'); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, m, tot; void madd(int &a, int b) { a += b; if (a >= 1000000007) a -= 1000000007; } void mdec(int &a, int b) { a -= b; if (a < 0) a += 1000000007; } struct edge { int u, v, w; }; vector<edge> E; vector<pair<int, int> > G[100005]; int mark[100005], val[100005], fa[100005]; map<long long, int> mp; vector<int> adj; vector<edge> cross; int pre[605][605]; void dfs(int v, int p) { fa[v] = p; for (auto e : G[v]) { if (e.first == p || mark[e.first]) continue; val[e.first] = val[v] ^ e.second; mark[e.first] = mark[v]; dfs(e.first, v); } } long long save[100005], bas[100005]; bool used[100005]; int dp[605], ndp[605]; int get_id(long long x) { if (x & 1) return -1; if (mp.find(x) == mp.end()) { mp[x] = ++tot; save[tot] = x; } return mp[x]; } int go(int x, int y) { if (x == -1 || y == -1) return -1; long long sx = save[x], sy = save[y], ret = sx | sy; if (pre[x][y]) return pre[x][y]; for (int i = 0; i < 32; i++) { if ((sx >> i) & 1) { for (int j = 0; j < 32; j++) { if ((sy >> j) & 1) { ret |= (1LL << (i ^ j)); } } } } return pre[x][y] = get_id(ret); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); G[u].push_back(pair<int, int>(v, w)); G[v].push_back(pair<int, int>(u, w)); E.push_back((edge){u, v, w}); } for (auto e : G[1]) { adj.push_back(e.first); mark[e.first] = e.first; val[e.first] = e.second; } for (auto e : G[1]) dfs(e.first, 1); memset(pre, 0, sizeof(pre)); mp[0] = 1; save[1] = 0; dp[1] = 1; tot = 1; for (int i = 1; i <= n; i++) bas[i] = 1; for (auto e : E) { if (e.u == fa[e.v] || e.v == fa[e.u]) continue; if (mark[e.u] == mark[e.v]) { int weight = val[e.u] ^ val[e.v] ^ e.w; bas[mark[e.u]] = go(bas[mark[e.u]], get_id(1LL << weight)); } else { cross.push_back(e); } } memset(used, false, sizeof(used)); for (auto p : cross) used[p.u] = used[p.v] = true; for (auto p : cross) { int curtot = tot; for (int i = 1; i <= tot; i++) ndp[i] = 0; for (int i = 1; i <= curtot; i++) { int id = i; madd(ndp[id], dp[i]); id = go(id, bas[p.u]); id = go(id, bas[p.v]); if (id != -1) madd(ndp[id], 2LL * dp[i] % 1000000007); id = go(id, get_id(1LL << (val[p.u] ^ val[p.v] ^ p.w))); if (id != -1) madd(ndp[id], dp[i]); } swap(dp, ndp); } for (auto x : adj) { if (used[x]) continue; int curtot = tot; for (int i = 1; i <= tot; i++) ndp[i] = 0; for (int i = 1; i <= curtot; i++) { int id = i; madd(ndp[id], dp[i]); id = go(id, bas[x]); if (id != -1) madd(ndp[id], dp[i]); } swap(dp, ndp); } int ans = 0; for (int i = 1; i <= tot; i++) madd(ans, dp[i]); printf("%d\n", ans); return 0; }
11
#include <bits/stdc++.h> const int N = 1010; const int M = 2010; const long long MOD = 998244353; using namespace std; int n, k, a[N]; long long f[N][N], sum[N], ans = 0; void READ() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sort(a + 1, a + n + 1); } int GET(int x) { for (int i = 1; i <= n; i++) f[i][1] = 1; for (int j = 2; j <= k; j++) { for (int i = 1; i <= n; i++) f[i][j] = 0, sum[i] = (sum[i - 1] + f[i][j - 1]) % MOD; int r = 0; for (int i = 1; i <= n; i++) { while (r < i && a[i] - a[r + 1] >= x) r++; f[i][j] += sum[r]; f[i][j] %= MOD; } } int res = 0; for (int i = 1; i <= n; i++) res = (res + f[i][k]) % MOD; return res; } int main() { READ(); for (int i = (a[n] - a[1]) / (k - 1); i >= 1; i--) { ans += GET(i); ans %= MOD; } printf("%lld\n", ans); return 0; }
8
#include <bits/stdc++.h> using namespace std; const long long int infinity = 9e18; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n; cin >> n; long long int ans = ((n * (n - 1) * (n - 2) * (n - 3) * (n - 4)) / 120) * (n * (n - 1) * (n - 2) * (n - 3) * (n - 4)); cout << ans; return 0; }
3
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > g[100001]; vector<int> tin(100001, -1), low(100001, -1), comp(100001, 0), child[100001], G[100001], P(100001), deph(100001); vector<bool> IsBridge(100001, false), used(100001, false); int cnt = 0; int A[100001][22]; void DFS(int v, int e = -1) { P[v] = e; for (int u : G[v]) if (u != e) { deph[u] = deph[v] + 1; DFS(u, v); } } void preper(int n, int root) { deph[root] = 0; DFS(root); for (int i = 1; i <= n; i++) for (int j = 0; j <= 20; j++) if (j == 0) A[i][j] = P[i]; else A[i][j] = -1; for (int j = 1; j <= 20; j++) for (int i = 1; i <= n; i++) if (A[i][j - 1] != -1) A[i][j] = A[A[i][j - 1]][j - 1]; } int LCA(int v, int u) { if (deph[u] < deph[v]) swap(u, v); int k = deph[u] - deph[v]; while (k > 0) { int t = log2(k); u = A[u][t]; k -= (1 << t); } if (u == v) return u; for (int j = 20; j >= 0; j--) { if (A[v][j] != A[u][j]) { v = A[v][j]; u = A[u][j]; } } return P[v]; } void Tarjan(int v, int e = -1) { used[v] = true; tin[v] = low[v] = cnt; cnt++; for (pair<int, int> x : g[v]) { int u = x.first; if (e == u) continue; if (used[u]) { low[v] = min(low[v], tin[u]); } else { Tarjan(u, v); low[v] = min(low[v], low[u]); if (low[u] > tin[v]) { IsBridge[x.second] = true; } } } } void dfs(int v) { comp[v] = cnt; child[cnt].push_back(v); for (pair<int, int> x : g[v]) { if (IsBridge[x.second]) continue; int u = x.first; if (comp[u] == 0) dfs(u); } } map<pair<int, int>, int> mp; void CondesGraph(int n, int m) { Tarjan(1); cnt = 0; for (int i = 1; i <= n; i++) { if (comp[i] == 0) { cnt++; dfs(i); } } for (int v = 1; v <= n; v++) for (pair<int, int> second : g[v]) { int u = second.first; int x = comp[u], y = comp[v]; if (x > y) swap(x, y); if (x != y && mp.count({x, y}) == 0) { G[x].push_back(y); G[y].push_back(x); mp[{x, y}] = 1; } } } int main() { int n, m; cin >> n >> m; for (int i = 1; i <= m; i++) { int v, u; cin >> v >> u; g[v].push_back({u, i}); g[u].push_back({v, i}); } CondesGraph(n, m); preper(n, 1); int Q; cin >> Q; while (Q--) { int v, u; cin >> v >> u; v = comp[v]; u = comp[u]; if (v == u) { cout << "0" << "\n"; continue; } int lca = LCA(v, u); cout << deph[v] + deph[u] - 2 * deph[lca] << "\n"; } }
4
#include <bits/stdc++.h> using namespace std; const int N = 6e5 + 1; const int LG = 32 - __builtin_clz(N); struct SparseTable { int st[N][LG]; int f(int x) { return x; } int f(int x, int y) { return min(x, y); } void Build(vector<int>& a, int n) { for (int i = 0; i < n; i++) st[i][0] = f(a[i]); for (int j = 1; j < LG; j++) for (int i = 0; i + (1 << j) <= n; i++) st[i][j] = f(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); } int Query(int l, int r) { int range = r - l + 1; int lg = 31 - __builtin_clz(range); return f(st[l][lg], st[r - (1 << lg) + 1][lg]); } } st; int main(void) { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<pair<int, int>> pairs(n); set<int> uni; for (int i = 0; i < n; i++) { cin >> pairs[i].first >> pairs[i].second; uni.insert(pairs[i].first - 1); uni.insert(pairs[i].first); uni.insert(pairs[i].second); } int sz = 0; map<int, int> h; for (int pucp : uni) h[pucp] = sz++; for (int i = 0; i < n; i++) { pairs[i].first = h[pairs[i].first]; pairs[i].second = h[pairs[i].second]; } sz++; vector<int> acc(sz, 0); for (int i = 0; i < n; i++) { acc[pairs[i].first]++; acc[pairs[i].second + 1]--; } for (int i = 1; i < sz; i++) { acc[i] += acc[i - 1]; } st.Build(acc, sz); for (int i = 0; i < n; i++) { int mn = st.Query(pairs[i].first, pairs[i].second); if (mn == 1) continue; cout << i + 1 << '\n'; return 0; } cout << -1 << '\n'; return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 10; const int M = 100000; int n, k, minA, res[N], tmp[N]; string s[N]; bool used[N]; void cmp() { for (int i = 1; i <= n; i++) tmp[i] = res[i]; sort(tmp + 1, tmp + 1 + n); if (tmp[n] - tmp[1] < minA) minA = tmp[n] - tmp[1]; return; } void dfs(int step, int base) { if (step > k) { cmp(); return; } for (int i = 0; i < k; i++) { if (used[i]) continue; used[i] = true; for (int j = 1; j <= n; j++) res[j] += (s[j][i] - '0') * base; dfs(step + 1, base * 10); for (int j = 1; j <= n; j++) res[j] -= (s[j][i] - '0') * base; used[i] = false; } } int main() { minA = 100000000; cin >> n >> k; for (int i = 1; i <= n; i++) cin >> s[i]; for (int i = 0; i < k; i++) { memset(res, 0, sizeof(res)); memset(used, false, sizeof(used)); used[i] = true; for (int j = 1; j <= n; j++) res[j] = s[j][i] - '0'; dfs(2, 10); for (int j = 1; j <= n; j++) res[j] -= s[j][i] - '0'; used[i] = false; } cout << minA << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { string s1, s2; cin >> s1 >> s2; vector<int> arr(26, -1); int res = 0; for (int i = 0; i < s1.length(); i++) { int a1 = s1[i] - 97; int a2 = s2[i] - 97; if (arr[a1] == -1) { arr[a1] = a2; } if (arr[a2] == -1) { arr[a2] = a1; } if (arr[a1] != a2) { cout << "-1"; return 0; } if (arr[a2] != a1) { cout << "-1"; return 0; } } int ans = 0; for (int i = 0; i < 26; i++) { if (arr[i] != -1 && arr[i] != i) ans++; } cout << ans / 2 << endl; for (int i = 0; i < s1.length(); i++) { int index = (int)(s1[i] - 97); if (arr[index] != -1 && arr[index] != index) { char ch = arr[index] + 97; arr[arr[index]] = -1; arr[index] = -1; cout << s1[i] << " " << ch << endl; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxl = 3 * 1000 * 100 + 5; vector<int> v[maxl]; int n, m; bool mark[maxl]; int d[maxl]; struct cmp { bool operator()(const int &i, const int &j) { if (d[i] == d[j]) return i < j; return d[i] < d[j]; } }; set<int, cmp> s; int main() { cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; a--; b--; v[a].push_back(b); d[a]++; v[b].push_back(a); d[b]++; } for (int i = 0; i < n; i++) s.insert(i); set<int>::iterator a = s.end(); a--; while (d[*a] > 1) { int index = *a; s.erase(index); d[index] = v[index].size() - d[index]; mark[index] = !mark[index]; for (int i = 0; i < v[index].size(); i++) { int node = v[index][i]; s.erase(node); if (mark[node] == mark[index]) d[node]++; else d[node]--; s.insert(node); } s.insert(index); a = s.end(); a--; } for (int i = 0; i < n; i++) cout << mark[i]; cout << endl; }
7
#include <bits/stdc++.h> using namespace std; int func(int n, int b); int maxp(int n) { int sum = 2; if (n < 2) return 0; while (sum < n) { sum *= 2; } return (sum > n) ? (sum / 2) : sum; } int main() { int n, b, p, i, j, x; while (cin >> n >> b >> p) { cout << func(n, b) << " " << p * n << endl; } return 0; } int func(int n, int b) { int tmp, i, j, x, y; int rem = 0; int k; int pk; int sum = 0; while (1) { if (n <= 1) break; int k = maxp(n); rem = n - k; n = k / 2 + rem; sum += (k * b + k / 2); } return sum; }
1
#include <bits/stdc++.h> using namespace std; map<long long, long long> m[100005]; long long v[100005]; vector<long long> adj[100005]; long long sol = 0; const int mod = 1000000007; long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r; } return a; } void dfs(int node, int dad) { for (auto x : m[dad]) { long long p = gcd(x.first, v[node]); m[node][p] += x.second; } m[node][v[node]]++; for (auto x : m[node]) { sol = (sol + 1LL * x.first * x.second) % mod; } for (auto x : adj[node]) { if (x != dad) { dfs(x, node); } } } int main() { cin.tie(0)->sync_with_stdio(0); cin.tie(NULL); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> v[i]; } for (int i = 1; i < n; i++) { int x, y; cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } dfs(1, 0); cout << sol; }
6