solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; int main() { int n, sum = 0, sum1 = 0, prev = 0; int h[1000001]; cin >> n; for (int i = 0; i < n; i++) { cin >> h[i]; } for (int i = 0; i < n; ++i) { { sum1 += abs(prev - h[i]); prev = h[i]; } } sum = sum1 + n + (n - 1); cout << sum << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 5005, MAXM = 500005; struct Edge { int from, to; int val; int id; Edge(int from = 0, int to = 0, int val = 0, int id = 0) : from(from), to(to), val(val), id(id) {} bool operator<(const Edge& b) const { if (val != b.val) return val < b.val; return from == 1; } }; Edge a[MAXM], b[MAXM]; int N, M, K; int father[MAXN]; int Find(int x) { if (father[x] < 0) return x; return father[x] = Find(father[x]); } int check(double shift, bool can, vector<int>& mst) { memset(father, -1, sizeof(father)); mst.clear(); for (int i = 1; i <= M; ++i) { b[i] = a[i]; if (b[i].from == 1) b[i].val -= shift; } sort(b + 1, b + M + 1); int res = 0; for (int i = 1; i <= M; ++i) { int u = Find(b[i].from), v = Find(b[i].to); if (u == v) continue; if (!can && res + (b[i].from == 1) > K) continue; father[u] = v; mst.push_back(b[i].id); res += (b[i].from == 1); } return res; } int main() { scanf("%d%d%d", &N, &M, &K); for (int i = 1; i <= M; ++i) { scanf("%d%d%d", &a[i].from, &a[i].to, &a[i].val); if (a[i].from > a[i].to) swap(a[i].from, a[i].to); a[i].id = i; } int left = -100001, right = 100001, mid = (left + right) / 2; vector<int> mst; if (check(right, 1, mst) < K || check(left, 1, mst) > K || mst.size() < N - 1) { puts("-1"); return 0; } while (left + 1 < right) { if (check(mid, true, mst) < K) left = mid; else right = mid; mid = (left + right) / 2; } check(right, false, mst); printf("%d\n", N - 1); for (int id : mst) printf("%d ", id); puts(""); return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { long long int a, b, c, d, e, f, g, h, i, j, k, l, m, n; long long int maximum, minimum, answer = 0; cin >> a >> b; while (a > 0 && b > 0 && (a + b) > 2) { if (a > b) { a = a - 2; b = b - 1; answer++; } else { b = b - 2; a = a - 1; answer++; } } cout << answer << endl; }
2
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = n - 1; i >= 0; i--) { if (i >= n / 2) { cout << (-1) * a[i] << " "; } else { a[i]; cout << a[i] << " "; } } } }
0
#include <bits/stdc++.h> using namespace std; int main() { int x; cin >> x; while (x) { x++; if (x % 10 != (x / 10) % 10 && x % 10 != (x / 100) % 10 && x % 10 != (x / 100) % 10 && x % 10 != (x / 1000) % 10 && (x / 10) % 10 != (x / 100) % 10 && (x / 10) % 10 != (x / 1000) % 10 && (x / 100) % 10 != (x / 1000) % 10) { break; } } cout << x; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7; int n, q, a[N]; long long f[N], s[N]; bool cmp(const int &x, const int &y) { return x > y; } long long solve(int k) { if (~f[k]) return f[k]; long long ret = 0, t = 0, p = 1; for (int i = 2, j; i <= n; i = j) { ++t, p = p * k; j = min((long long)(n + 1), i + p); ret += t * (s[j - 1] - s[i - 1]); } return f[k] = ret; } int main() { scanf("%d", &n); for (int i = (1); i < (n + 1); ++i) scanf("%d", &a[i]); sort(a + 1, a + 1 + n, cmp); for (int i = (1); i < (n + 1); ++i) s[i] = s[i - 1] + a[i]; memset(f, -1, sizeof(f)); scanf("%d", &q); for (int i = (0); i < (q); ++i) { int k; scanf("%d", &k); if (i) putchar(' '); printf("%lld", solve(k)); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 100 * 1000 + 5, LG = 20; vector<pair<int, int> > chld[N]; vector<pair<pair<int, int>, int> > q3; vector<pair<int, int> > q2; int par[N][LG], dp[N][LG]; int h[N], indeg[N]; void dfs(int v) { for (auto p : chld[v]) { int u = p.first, w = p.second; h[u] = h[v] + 1; par[u][0] = v; dp[u][0] = w; dfs(u); } } int main() { int n, q; cin >> n >> q; for (int i = 1; i <= q; i++) { int t; cin >> t; if (t == 1) { int u, v; cin >> u >> v; chld[v].push_back({u, i}); indeg[u]++; } if (t == 2) { int v; cin >> v; q2.push_back({v, i}); } if (t == 3) { int v, k; cin >> v >> k; k--; q3.push_back({{v, k}, i}); } } for (int i = 1; i <= n; i++) { par[i][0] = i; } for (int i = 1; i <= n; i++) { if (!indeg[i]) { dfs(i); } } for (int j = 1; j < LG; j++) { for (int i = 1; i <= n; i++) { int v = par[i][j - 1]; par[i][j] = par[v][j - 1]; dp[i][j] = max(dp[i][j - 1], dp[v][j - 1]); } } for (auto p : q3) { int u = p.first.first, k = p.first.second, s = p.second; int v = q2[k].first, mx = 0; for (int i = LG - 1; i >= 0; i--) { if (h[v] - (1 << i) >= h[u] && dp[v][i] < q2[k].second) { mx = max(mx, dp[v][i]); v = par[v][i]; } } if (u != v || mx > s) { cout << "NO\n"; } else { cout << "YES\n"; } } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; inline int read() { int w = 1, s = 0; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') w = -1; ch = getchar(); } while (isdigit(ch)) { s = s * 10 + ch - '0'; ch = getchar(); } return w * s; } struct node { int to, next; } e[1010010]; int tot, h[1000100], n, f[1001010][3]; inline long long ksm(long long x, long long y) { long long res = 1; while (y) { if (y & 1) res = res * x % mod; x = x * x % mod; y >>= 1; } return res; } inline void add(int from, int to) { e[++tot].next = h[from]; h[from] = tot; e[tot].to = to; } inline int pls(int a, int b) { a += b; return a >= mod ? a - mod : a; } inline int mns(int a, int b) { a -= b; return a < 0 ? a + mod : a; } inline void DFS(int now, int ffa) { int fl = 0; for (register int i = h[now]; i; i = e[i].next) { int to = e[i].to; if (to == ffa) continue; DFS(to, now); fl = 1; } if (!fl) f[now][2] = f[now][1] = 1; else { f[now][0] = 1; for (register int i = h[now]; i; i = e[i].next) { int to = e[i].to; if (to == ffa) continue; f[now][0] = 1ll * f[now][0] * pls(f[to][0], f[to][2]) % mod; } f[now][1] = 0; for (register int i = h[now]; i; i = e[i].next) { int to = e[i].to; if (to == ffa) continue; f[now][1] = pls(f[now][1], 1ll * f[now][0] * ksm(pls(f[to][0], f[to][2]), mod - 2) % mod * f[to][1] % mod); } f[now][2] = 0; int f1 = 1; for (register int i = h[now]; i; i = e[i].next) { int to = e[i].to; if (to == ffa) continue; f1 = (1ll * f1 * f[to][1] % mod + 1ll * f1 * pls(f[to][0], f[to][2]) % mod) % mod; } f[now][2] = mns(f1, pls(f[now][0], f[now][1])); f[now][1] = pls(f[now][1], f[now][2]); } } int main() { n = read(); for (register int i = 2; i <= n; ++i) { int x = read(); add(x, i); } DFS(1, 0); cout << pls(f[1][0], f[1][2]); return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { long long tc, o; cin >> tc; for (o = 1; o <= tc; o++) { long long n, a, b, c, p, sm = 0, i, j, flg = 0; vector<long long> v; map<long long, long long> mp; scanf("%lld", &n); for (i = 0; i < n; i++) { scanf("%lld", &a); v.push_back(a); } sort(v.begin(), v.end()); for (j = n - 1; j >= 0; j--) { if (sm + v[j] <= 2048) sm = sm + v[j]; } if (sm == 2048) printf("YES\n"); else printf("NO\n"); v.clear(); } return 0; }
1
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } long long powmod(long long a, long long b, long long MOD) { if (a == 0ll) { return 0ll; } a %= MOD; long long ans = 1; while (b) { if (b & 1) { ans = ans * a % MOD; } a = a * a % MOD; b >>= 1; } return ans; } long long poww(long long a, long long b) { if (a == 0ll) { return 0ll; } long long ans = 1; while (b) { if (b & 1) { ans = ans * a; } a = a * a; b >>= 1; } return ans; } void Pv(const vector<int> &V) { int Len = int(V.size()); for (int i = 0; i < Len; ++i) { printf("%d", V[i]); if (i != Len - 1) { printf(" "); } else { printf("\n"); } } } void Pvl(const vector<long long> &V) { int Len = int(V.size()); for (int i = 0; i < Len; ++i) { printf("%lld", V[i]); if (i != Len - 1) { printf(" "); } else { printf("\n"); } } } inline long long readll() { long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') fh = -1; c = getchar(); } while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh; } inline int readint() { int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') fh = -1; c = getchar(); } while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh; } void pvarr_int(int *arr, int n, int strat = 1) { if (strat == 0) { n--; } for (int i = strat; i <= n; i++) { printf("%d%c", arr[i], i == n ? '\n' : ' '); } } void pvarr_LL(long long *arr, int n, int strat = 1) { if (strat == 0) { n--; } for (int i = strat; i <= n; i++) { printf("%lld%c", arr[i], i == n ? '\n' : ' '); } } const int maxn = 1010; const int inf = 0x3f3f3f3f; int n, m; int x, y; int vis[maxn][maxn]; int flag = 1; void solve() { if (flag) { y++; if (y > m) { y = m; x++; flag ^= 1; } } else { y--; if (y < 1) { y = 1; x++; flag ^= 1; } } } int main() { n = readint(); m = readint(); x = readint(); y = readint(); int cnt = 0; printf("%d %d\n", x, y); printf("%d %d\n", 1, y); printf("%d %d\n", 1, 1); vis[x][y] = vis[1][y] = vis[1][1] = 1; cnt = 3; x = y = 1; do { while (vis[x][y] == 1) { solve(); } printf("%d %d\n", x, y); cnt++; solve(); } while (cnt < n * m); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 1; int n, m; vector<int> adj[N]; bool vis[N]; vector<int> all[N]; int dep[N]; int par[N]; int pairs = 0; bool outputted = 0; void dfs(int u) { if (outputted) return; vis[u] = 1; pairs -= all[dep[u]].size() / 2; all[dep[u]].push_back(u); pairs += all[dep[u]].size() / 2; if (dep[u] >= (n + 1) / 2) { outputted = 1; cout << "PATH" << "\n"; int cur = u; cout << dep[u] << "\n"; while (cur != 0) { cout << cur << " "; cur = par[cur]; } cout << "\n"; } for (int v : adj[u]) { if (vis[v]) continue; par[v] = u; dep[v] = dep[u] + 1; dfs(v); } } void solve() { cin >> n >> m; outputted = 0; for (int i = 1; i <= n; i++) { adj[i].clear(); all[i].clear(); vis[i] = 0; } pairs = 0; for (int i = 1; i <= m; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } dep[1] = 1; dfs(1); if (outputted) return; cout << "PAIRING" << "\n"; cout << pairs << "\n"; for (int i = 1; i < (n + 1) / 2; i++) { for (int j = 0; j + 1 < all[i].size(); j += 2) { cout << all[i][j] << " " << all[i][j + 1] << "\n"; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { solve(); } return 0; }
9
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; const int MOD = 1e9 + 7; const double PI = 2 * acos(0.0); int main() { int n; cin >> n; double ans = 0; for (int i = n; i >= 1; i--) { double x = 1.0 / (double)i; ans += x; } cout << fixed << setprecision(10) << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = (int)1e6 + 100; const int mod = (int)1e9 + 7; string second; int n, sum, ans, cnt, K = 200000, t[maxn]; int ok(char ch) { ch = tolower(ch); if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') return 1; return 0; } void upd(int pos, int val) { pos += K; for (; pos < maxn; pos = (pos | (pos + 1))) t[pos] = min(t[pos], val); } int get(int r) { r += K; int res = mod; for (; r >= 0; r = (r & (r + 1)) - 1) res = min(res, t[r]); return res; } int main() { cin >> second; for (int i = (0); i <= (maxn - 1); ++i) t[i] = mod; n = ((int)(second).size()); upd(0, 0); for (int r = (1); r <= (n); ++r) { sum += (ok(second[r - 1]) ? -1 : 2); int l = get(sum); upd(sum, r); if (l == mod) continue; if (ans < r - l) { ans = r - l; cnt = 1; } else if (ans == r - l) { cnt++; } } if (ans <= 0) { cout << "No solution"; return 0; } cout << ans << " " << cnt; }
6
#include <bits/stdc++.h> using namespace std; int b[110][110] = {0}; bool comp(pair<int, int> a, pair<int, int> b) { return a.first < b.first; } int main() { int n, m, u, v; cin >> n >> m; int* arr = new int[n]; for (int i = 0; i < n; i++) cin >> arr[i]; vector<pair<int, int> > vec; for (int i = 0; i < m; i++) { cin >> u >> v; if (u < v) { vec.push_back(make_pair(u, v)); } else { vec.push_back(make_pair(v, u)); } b[u][v] = 1; b[v][u] = 1; } sort(vec.begin(), vec.end(), comp); bool flag = false; int ans = 2000000000; int sum; for (int i = 0; i < m - 1; i++) { for (int j = i + 1; j < m; j++) { if (vec[i].first == vec[j].first) { if (b[vec[i].second][vec[j].second] == 1) { if ((arr[vec[i].first - 1] + arr[vec[i].second - 1] + arr[vec[j].second - 1]) < ans) ans = (arr[vec[i].first - 1] + arr[vec[i].second - 1] + arr[vec[j].second - 1]); flag = true; } } else break; } } if (flag) cout << ans; else cout << -1; }
2
#include <bits/stdc++.h> using namespace std; int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1}; int dy[8] = {1, -1, 0, -1, 1, 1, 0, -1}; int main() { ios::sync_with_stdio(false); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long n, t1, t2, s1, s2; double k; cin >> n >> t1 >> t2 >> k; vector<pair<double, int>> v(n); for (int i = 0; i < n; i++) { cin >> s1 >> s2; v[i].first = max(s1 * t1 * 100 - k * (s1 * t1) + s2 * t2 * 100, s2 * t1 * 100 - k * (s2 * t1) + s1 * t2 * 100); v[i].second = i; } sort(((v).begin()), ((v).end()), [](pair<int, int> a, pair<int, int> b) { return (a.first > b.first || (a.first == b.first && a.second < b.second)); }); for (auto x : v) cout << x.second + 1 << " " << fixed << setprecision(2) << x.first / 100 << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MAXN = 165; const int MAXJZ = 16; int n, t, len; int lim[MAXJZ]; char dic[MAXJZ]; long long C[MAXN][MAXN], dp[MAXN][MAXN]; int Read() { int x = 0, f = 1; char c = getchar(); while (c > '9' || c < '0') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x * 10) + (c ^ 48); c = getchar(); } return x * f; } void Put1(int x) { if (x > 9) Put1(x / 10); putchar(x % 10 ^ 48); } void Put(int x, char c = -1) { if (x < 0) putchar('-'), x = -x; Put1(x); if (c >= 0) putchar(c); } template <typename T> T Max(T x, T y) { return x > y ? x : y; } template <typename T> T Min(T x, T y) { return x < y ? x : y; } template <typename T> T Abs(T x) { return x < 0 ? -x : x; } void pre() { C[0][0] = 1; for (int i = 1; i <= 160; ++i) { C[i][0] = 1; for (int j = 1; j <= i; ++j) C[i][j] = C[i - 1][j] + C[i - 1][j - 1]; } for (int i = 0; i <= 9; ++i) dic[i] = '0' + i; for (int i = 10; i < MAXJZ; ++i) dic[i] = 'a' + i - 10; } void getlen() { for (len = 1;; ++len) { memset(dp, 0, sizeof(dp)); for (int i = 0; i < Min(len, t + 1); ++i) dp[0][i] = C[len - 1][i]; for (int i = 1; i < MAXJZ; ++i) { for (int j = 0; j <= len; ++j) { for (int k = 0; k <= Min(j, t); ++k) dp[i][j] += dp[i - 1][j - k] * C[len - (j - k)][k]; } } if (dp[15][len] >= n) break; n -= dp[15][len]; } } void solve() { long long dz; for (int i = 0; i < MAXJZ; ++i) lim[i] = t; for (int LEN = len; LEN >= 1; --LEN) { int MIN = 0 + (LEN == len); for (int i = MIN; i < MAXJZ; ++i) { if (!lim[i]) continue; memset(dp, 0, sizeof(dp)); lim[i]--; for (int j = 0; j < MAXJZ; ++j) for (int lennow = 0; lennow <= LEN - 1; ++lennow) for (int k = 0; k <= Min(lennow, lim[j]); ++k) { if (!j) { if (k == lennow) dz = 1; else dz = 0; } else dz = dp[j - 1][lennow - k]; dp[j][lennow] += dz * C[LEN - 1 - (lennow - k)][k]; } if (dp[15][LEN - 1] >= n) { putchar(dic[i]); break; } n -= dp[15][LEN - 1]; lim[i]++; } } } int main() { n = Read(); t = Read(); pre(); getlen(); solve(); return 0; }
8
#include <bits/stdc++.h> using namespace std; vector<int> ans; const int maxn = 3 * 1e5 + 10; bool ok[maxn]; int main() { int t, n, m; cin >> t; while (t--) { ans.clear(); scanf("%d", &n), scanf("%d", &m); int x, y; for (int i = 1; i <= 3 * n; i++) ok[i] = false; for (int i = 1; i <= m; i++) { scanf("%d", &x), scanf("%d", &y); if (!ok[x] && !ok[y]) { ans.push_back(i); ok[x] = ok[y] = true; } } if (ans.size() >= n) { cout << "Matching" << '\n'; for (int i = 0; i < n; i++) cout << ans[i] << " "; cout << endl; } else { cout << "IndSet" << '\n'; int k = 0; for (int i = 1; i <= 3 * n; i++) { if (k >= n) break; if (!ok[i]) { cout << i << " "; k++; } } cout << endl; } } return 0; }
6
#include <bits/stdc++.h> using namespace std; char buffer[100000], *p1, *p2; template <class T> void read(T& x) { char ch = ((p1 == p2 && (p2 = (p1 = buffer) + fread(buffer, 1, 100000, stdin), p1 == p2)) ? EOF : *p1++); x = 0; bool f = 1; while (!('0' <= ch && ch <= '9') && ch != '-') ch = ((p1 == p2 && (p2 = (p1 = buffer) + fread(buffer, 1, 100000, stdin), p1 == p2)) ? EOF : *p1++); if (ch == '-') f = 0, ch = ((p1 == p2 && (p2 = (p1 = buffer) + fread(buffer, 1, 100000, stdin), p1 == p2)) ? EOF : *p1++); while ('0' <= ch && ch <= '9') x = (x << 1) + (x << 3) + ch - '0', ch = ((p1 == p2 && (p2 = (p1 = buffer) + fread(buffer, 1, 100000, stdin), p1 == p2)) ? EOF : *p1++); x = (f) ? x : -x; } int n, m, c, c0; struct ele { int t, a, b; ele(int t, int a, int b) : t(t), a(a), b(b) {} ele() {} bool operator<(const ele y) const { return (t == y.t) ? ((b == y.b) ? a > y.a : b < y.b) : t < y.t; } }; ele lis[600019]; multiset<pair<int, int> > s; long long int nowc = 0; inline void recover() { nowc = 0, s.clear(); } int main() { int T, t, a, b; read(T); while (T--) { read(n), read(m), read(c), read(c0); lis[0] = ele(0, c0, 0); for (register int i = 1; i <= n; i++) read(t), read(a), read(b), lis[i] = ele(t, a, b); lis[++n] = ele(m, 0, 0); sort(lis, lis + 1 + n); long long int ans = 0; for (register int i = 0; i <= n; i++) { int nowt = lis[i].t, nextt; while (i <= n && lis[i].t == nowt) { nowc += lis[i].a; s.insert(pair<int, int>(lis[i].b, lis[i].a)); i++; } while (nowc > (long long int)c) { pair<int, int> op = *s.rbegin(); s.erase(s.lower_bound(op)); nowc -= op.second; if (nowc < c) { op.second = c - nowc; s.insert(op); nowc += op.second; break; } } if (i <= n) { nextt = lis[i].t; int cost = nextt - nowt; while (!s.empty() && cost) { pair<int, int> op = *s.begin(); s.erase(s.begin()); if (cost < op.second) { ans += (long long int)cost * op.first; op.second -= cost; s.insert(op); nowc -= cost; cost = 0; } else { ans += (long long int)op.first * op.second; cost -= op.second; nowc -= op.second; } } if (cost) { printf("-1\n"); goto jump; } } i--; } printf("%lld\n", ans); jump: recover(); } return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { long long m, n; map<long long, long long> mpa, mpb; vector<int> v; cin >> n; long long a, x, b, y; for (int i = 0; i < n; i++) { cin >> a >> x; mpa[a] = x; v.push_back(a); } cin >> m; for (int i = 0; i < m; i++) { cin >> b >> y; mpb[b] = y; v.push_back(b); } long long cnt = 0; for (int i = 0; i < v.size(); i++) { cnt = cnt + max(mpa[v[i]], mpb[v[i]]); mpa[v[i]] = 0; mpb[v[i]] = 0; } cout << cnt << "\n"; return 0; }
1
#include <bits/stdc++.h> using namespace std; int a[200005], k[200005], nowk[200005]; int main() { ios::sync_with_stdio(false); int n, m, ans = -1, sum = 0; int l = 1, r = 0, now = 0; cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= m; i++) { cin >> k[i]; if (k[i] == 0) now++; sum += k[i]; } while (l <= n) { while (r + 1 <= n && now < m) { r++; nowk[a[r]]++; if (k[a[r]] && k[a[r]] == nowk[a[r]]) now++; } if (now == m) { if (ans == -1) ans = r - l + 1 - sum; else ans = min(ans, r - l + 1 - sum); } nowk[a[l]]--; if (k[a[l]] && nowk[a[l]] + 1 == k[a[l]]) now--; l++; } cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MOD = 7000007, sd = 37; int n, sq, now, sum, hs[500005]; char tmp[500005]; bitset<500005> f[1001]; bitset<7000010> b; int md(int x) { return x >= MOD ? x - MOD : x; } int main() { scanf("%d%s", &n, tmp + 1); for (int i = 1; i <= n; i++) hs[i] = tmp[i] - 'a' + 1; sq = sqrt(2 * n) + 1, now = 1, f[1].set(); for (int ans = 2; ans <= sq; ans++) { now += ans; for (int i = n - now + 1; i >= 1; i--) { int pos = i + ans; if (f[ans - 1][pos]) b[hs[pos]] = 1; if (b[hs[i]] || b[hs[i + 1]]) f[ans][i] = 1; } if (f[ans].none()) { printf("%d\n", ans - 1); return 0; } b.reset(); for (int i = 1; i <= n - now + 1; i++) hs[i] = md(1ll * hs[i] * sd % MOD + tmp[i + ans - 1] - 'a' + 1); } printf("%d\n", sq); return 0; }
12
#include <bits/stdc++.h> using namespace std; int main() { int n, l, r, Ql, Qr; cin >> n >> l >> r >> Ql >> Qr; vector<int> w(n); for (int i = 0; i < n; ++i) cin >> w[i]; long long answer = 0; for (int i = 0; i < n; ++i) answer += w[i] * l + Ql; answer -= Ql; long long curanswer = answer; for (int i = n - 1; i > n / 2; --i) { curanswer = curanswer - 2 * Ql - w[i] * l + w[i] * r; if (i - 1 <= n / 2 && n % 2 == 0) { curanswer += Ql; } if (answer > curanswer) { answer = curanswer; } } curanswer = 0; for (int i = 0; i < n; ++i) curanswer += w[i] * r + Qr; curanswer -= Qr; if (curanswer < answer) answer = curanswer; for (int i = 0; i < n / 2; ++i) { curanswer = curanswer - 2 * Qr - w[i] * r + w[i] * l; if ((i + 1 >= n / 2) && (n % 2 == 0)) { curanswer += Qr; } if (curanswer < answer) answer = curanswer; } cout << answer << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 100000 + 10, L = 18; int n, m, a[MAXN]; set<int> s[L]; int main() { cin >> n >> m; for (int i = 0; i < L; ++i) s[i].insert(0), s[i].insert(n + 1); for (int i = 1; i <= n; ++i) { cin >> a[i]; for (int j = 0; j < L; ++j) if (!(a[i] & (1 << j))) s[j].insert(i); } long long Tohka = 0; for (int i = 1; i <= n; ++i) for (int j = 0; j < L; ++j) if (a[i] & (1 << j)) { set<int>::iterator it = s[j].lower_bound(i); Tohka += (1ll << j) * (*it - i); } for (int tt = 1; tt <= m; ++tt) { int p, first; cin >> p >> first; for (int j = 0; j < L; ++j) if (!(a[p] & (1 << j)) && (first & (1 << j))) { set<int>::iterator it = s[j].lower_bound(p); ++it; int r = *it; --it, --it; int l = *it; Tohka += (1ll << j) * (p - l) * (r - p); s[j].erase(p); } else if ((a[p] & (1 << j)) && !(first & (1 << j))) { s[j].insert(p); set<int>::iterator it = s[j].lower_bound(p); ++it; int r = *it; --it, --it; int l = *it; Tohka -= (1ll << j) * (p - l) * (r - p); } cout << Tohka << endl; a[p] = first; } fclose(stdin); fclose(stdout); return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; float a[n]; float p[n]; float price = 1000; int tprice = 0; for (int i = 0; i < n; i++) { cin >> a[i]; cin >> p[i]; price = min(price, p[i]); tprice += price * a[i]; } cout << tprice; return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; const long long int inf = 1e18; long long int n, last = 0, ans = 0; vector<pair<long long int, long long int>> arr; map<long double, vector<pair<long long int, long long int>>> brr; vector<vector<pair<long long int, long long int>>> crr( 2, vector<pair<long long int, long long int>>(1e5 + 1, make_pair(0, 0))); int main(void) { cin >> n; arr.reserve(n); for (int i = 0; i < n; i++) { cin >> arr[i].first >> arr[i].second; if (arr[i].first != 0) { long double r = -((long double)arr[i].second / (long double)arr[i].first); auto it = brr.find(r); if (it == brr.end()) { vector<pair<long long int, long long int>> v; if (arr[i].first > 0) { v.push_back(make_pair(i, 1)); } else { v.push_back(make_pair(i, -1)); } brr.insert( pair<long double, vector<pair<long long int, long long int>>>( -((long double)arr[i].second / (long double)arr[i].first), v)); } else { if (arr[i].first > 0) it->second.push_back(make_pair(i, 1)); else { it->second.push_back(make_pair(i, -1)); } } } else { if (arr[i].second < 0) arr[i].second = 0; } if (arr[i].first < 0) { crr[0][last].first += arr[i].first; crr[0][last].second += arr[i].second; } } for (auto it = brr.begin(); it != brr.end(); it++) { crr[1][last].first = crr[0][last].first; crr[1][last].second = crr[0][last].second; for (int i = 0; i < it->second.size(); i++) { if (it->second[i].second == -1) { crr[1][last].first -= arr[it->second[i].first].first; crr[1][last].second -= arr[it->second[i].first].second; } else { crr[1][last].first += arr[it->second[i].first].first; crr[1][last].second += arr[it->second[i].first].second; } } crr[0][last + 1].first = crr[1][last].first; crr[0][last + 1].second = crr[1][last].second; last++; } last = 0; for (int i = 0; i < brr.size(); i++) { if ((crr[0][last].first != crr[1][last].first) || (crr[0][last].second != crr[1][last].second)) { ans++; } last++; } cout << ans << endl; }
5
#include <bits/stdc++.h> using namespace std; struct comp { bool operator()(const pair<char, int>& a, const pair<char, int>& b) { if (a.first != b.first) return a.first < b.first; return a.second > b.second; } }; int counts[26]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); string s; map<char, int> sres; int m; cin >> m; cin >> s; for (auto i : s) counts[i - 'a']++; int n = s.size(); s.append(100500, (char)(255)); if (m > n) { char c = 255; for (auto i : s) c = min(c, i); cout << c; } else { pair<char, int> cRes = {'z' + 1, m}; for (int i = m - 1; i >= 0; i--) { if (cRes.first > s[i]) { cRes = {s[i], i}; } } sres[cRes.first]++; set<pair<char, int>, comp> setOfLetters; int currI = cRes.second + m; int DELTA = m; for (int i = currI - m; i < currI; i++) setOfLetters.insert({s[i], i}); for (int i = currI; i < n; i++) { setOfLetters.insert({s[i], i}); setOfLetters.erase({s[i - DELTA], i - DELTA}); if (i == currI) { sres[setOfLetters.begin()->first]++; currI = setOfLetters.begin()->second + m; } } auto lastIt = sres.end(); lastIt--; char lastChar = lastIt->first; for (char i = 'a'; i < lastChar; i++) { cout << string(counts[i - 'a'], i); } cout << string(lastIt->second, lastIt->first); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}; int dy[] = {1, -1, 0, 0, 1, -1, 1, -1}; bool comp(string a, string b) { if (a.length() != b.length()) return a.length() > b.length(); return a >= b; } int main() { char s[100005]; scanf("%s", &s); string tmp = ""; vector<string> v; for (int i = strlen(s) - 1; i >= 0; i--) { tmp = s[i] + tmp; if (s[i] == '0') continue; v.push_back(tmp); tmp = ""; } int c = 1; int len = v.size() - 1; string beg = v[len]; for (int i = len - 1; i >= 0; i--) { if (comp(beg, v[i])) c++; else c = 1; beg += v[i]; } cout << c << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int a[1001]; int main() { int n; cin >> n; int m = n * n; for (int r = 1, t = m; r <= n; r++, t--) { for (int i = r, j = t, k = 1; k <= (n / 2); i += n, j -= n, k++) { cout << i << " " << j << " "; } cout << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int Maxn = 100005; const int Maxm = 2097152; const int Maxk = 20; const int Inf = 2000000007; int n, k; int b[Maxn][Maxk]; int q; int typ[Maxn], lef[Maxn], rig[Maxn], x[Maxn]; vector<int> un, seq, vals; int st[Maxm], flag[Maxm]; int Get(int l, int r) { int res = Inf; for (int i = Maxk - 1; i >= 0; i--) if (l + (1 << i) <= r + 1) { res = min(res, b[l][i]); l += 1 << i; } return res; } int getBig(int l, int r) { if (r - l + 1 >= n) return Get(0, n - 1); if (l % n <= r % n) return Get(l % n, r % n); return min(Get(l % n, n - 1), Get(0, r % n)); } void downOn(int v, int val) { st[v] = flag[v] = val; } void Down(int v) { if (flag[v] < Inf) { downOn(2 * v, flag[v]); downOn(2 * v + 1, flag[v]); flag[v] = Inf; } } void Union(int v) { st[v] = min(st[2 * v], st[2 * v + 1]); } void Create(int v, int l, int r) { flag[v] = Inf; if (l == r) st[v] = vals[l]; else { int m = l + r >> 1; Create(2 * v, l, m); Create(2 * v + 1, m + 1, r); Union(v); } } void Update(int v, int l, int r, int a, int b, int x) { if (l == a && r == b) downOn(v, x); else { Down(v); int m = l + r >> 1; if (a <= m) Update(2 * v, l, m, a, min(m, b), x); if (m + 1 <= b) Update(2 * v + 1, m + 1, r, max(m + 1, a), b, x); Union(v); } } int Get(int v, int l, int r, int a, int b) { if (l == a && r == b) return st[v]; else { Down(v); int m = l + r >> 1; int res = Inf; if (a <= m) res = min(res, Get(2 * v, l, m, a, min(m, b))); if (m + 1 <= b) res = min(res, Get(2 * v + 1, m + 1, r, max(m + 1, a), b)); return res; } } int main() { scanf("%d %d", &n, &k); for (int i = 0; i < n; i++) scanf("%d", &b[i][0]); for (int j = 1; j < Maxk; j++) for (int i = 0; i + (1 << j) <= n; i++) b[i][j] = min(b[i][j - 1], b[i + (1 << j - 1)][j - 1]); scanf("%d", &q); for (int i = 0; i < q; i++) { scanf("%d %d %d", &typ[i], &lef[i], &rig[i]); lef[i]--; rig[i]--; if (typ[i] == 1) scanf("%d", &x[i]); un.push_back(lef[i]); un.push_back(rig[i]); } sort(un.begin(), un.end()); un.erase(unique(un.begin(), un.end()), un.end()); int lst = -1; for (int i = 0; i < un.size(); i++) { if (lst + 1 < un[i]) { seq.push_back(lst + 1); vals.push_back(getBig(lst + 1, un[i] - 1)); } seq.push_back(un[i]); vals.push_back(b[un[i] % n][0]); lst = un[i]; } if (lst + 1 < k * n) { seq.push_back(lst + 1); vals.push_back(getBig(lst + 1, k * n - 1)); } Create(1, 0, seq.size() - 1); for (int i = 0; i < q; i++) { lef[i] = lower_bound(seq.begin(), seq.end(), lef[i]) - seq.begin(); rig[i] = lower_bound(seq.begin(), seq.end(), rig[i]) - seq.begin(); if (typ[i] == 1) Update(1, 0, seq.size() - 1, lef[i], rig[i], x[i]); else printf("%d\n", Get(1, 0, seq.size() - 1, lef[i], rig[i])); } return 0; }
7
#include <bits/stdc++.h> using namespace std; long long n, m; int t; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> n >> m; int xu[2], yu[2], xd[2], yd[2]; for (int i = 0; i < 2; i++) { cin >> xd[i] >> yd[i] >> xu[i] >> yu[i]; } long long tot = n * m; long long white = (tot + 1) / 2; long long black = tot - white; if ((xd[0] + yd[0]) % 2 == 0) { long long mm = xu[0] - xd[0] + 1; long long nn = yu[0] - yd[0] + 1; long long tt = nn * mm; long long ww = (tt + 1) / 2; long long bb = tt - ww; white += bb; black -= bb; } else { long long mm = xu[0] - xd[0] + 1; long long nn = yu[0] - yd[0] + 1; long long tt = nn * mm; long long bb = (tt + 1) / 2; long long ww = tt - bb; white += bb; black -= bb; } if ((xd[1] + yd[1]) % 2 == 0) { long long mm = xu[1] - xd[1] + 1; long long nn = yu[1] - yd[1] + 1; long long tt = nn * mm; long long ww = (tt + 1) / 2; long long bb = tt - ww; white -= ww; black += ww; } else { long long mm = xu[1] - xd[1] + 1; long long nn = yu[1] - yd[1] + 1; long long tt = nn * mm; long long bb = (tt + 1) / 2; long long ww = tt - bb; white -= ww; black += ww; } long long xdmx = max(xd[0], xd[1]); long long ydmx = max(yd[0], yd[1]); long long xumi = min(xu[0], xu[1]); long long yumi = min(yu[0], yu[1]); if (xumi - xdmx >= 0 and yumi - ydmx >= 0) { if ((xdmx + ydmx) % 2 == 0) { long long mm = xumi - xdmx + 1; long long nn = yumi - ydmx + 1; long long tt = nn * mm; long long ww = (tt + 1) / 2; long long bb = tt - ww; black += bb; white -= bb; } else { long long mm = xumi - xdmx + 1; long long nn = yumi - ydmx + 1; long long tt = nn * mm; long long bb = (tt + 1) / 2; long long ww = tt - bb; black += bb; white -= bb; } } cout << white << ' ' << black << '\n'; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; void solve() { int n, t; double p; cin >> n >> p >> t; double dp[t + 1][n + 1]; for (int i = 0; i <= t; i++) { for (int j = 0; j <= n; j++) dp[i][j] = 0; } dp[0][0] = 1; for (int i = 0; i < t; i++) { for (int j = 0; j <= n; j++) { if (j == n) { dp[i + 1][j] += dp[i][j]; } else { dp[i + 1][j + 1] += dp[i][j] * p; dp[i + 1][j] += dp[i][j] * (1 - p); } } } double ans = 0; for (int i = 0; i <= n; i++) ans += (double)i * (dp[t][i]); printf("%0.9f\n", ans); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int tests = 1; while (tests--) { solve(); } }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 3000 + 10; int n, ans = 0; string s; int a[maxn] = {}; int main() { ios ::sync_with_stdio(false); cin.tie(0); cin >> n >> s; int ii = 0; while (ii < n && s[ii] == '.') ans++, ii++; if (ii < n && s[ii] == 'L') ans = 0; for (int i = 0; i < n; i++) { if (s[i] == '.') continue; if (s[i] == 'L') { for (int j = i + 1; j < n && s[j] != 'R'; j++) ans++; } if (s[i] == 'R') { int ind = i + 1; while (ind < n && s[ind] == '.') ind++; if (ind == n) continue; if (s[ind] == 'R') continue; if ((ind - i) % 2) continue; ans++; } } cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int countSteps(int m, int n, int init) { assert(n >= m); if (n == m) { if (n == 1) return init; else return 2000000; } else { int a = n - m; if (a > m) return countSteps(m, a, init + 1); else return countSteps(a, m, init + 1); } } int main() { int n; cin >> n; int m = n - 1; for (int i = 2 + (n % 2 == 0); i <= n / 2; i += 1 + (n % 2 == 0)) { m = min(countSteps(i, n - i, 1), m); } cout << m << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; inline int getInt() { int x; scanf("%d", &x); return x; } const int maxn = 1e5 + 5; int a[maxn]; int e[maxn][2]; map<int, int> pos; bool good[maxn][2]; int v[2]; bool res; vector<pair<int, int> > vs; bool color[maxn]; int was[maxn]; int main() { int n; scanf("%d%d%d", &n, &v[0], &v[1]); for (int i = (0); i < (n); i++) { a[i] = getInt(); if (a[i] > v[0] && a[i] > v[1]) { printf("NO"); return 0; } pos[a[i]] = i; } for (int i = (0); i < (n); i++) { int x = a[i]; bool isGood = false; for (int k = (0); k < (2); k++) { e[i][k] = -1; int y = v[k] - x; map<int, int>::iterator it = pos.find(y); if (it == pos.end()) continue; isGood = true; e[i][k] = it->second; } if (!isGood) { printf("NO"); return 0; } } cerr << 1; for (int i = (0); i < (n); i++) { if (e[i][1] != -1) { color[i] = color[e[i][1]] = 1; was[i] = was[e[i][1]] = true; } } for (int i = (0); i < (n); i++) { if (!was[i]) { if (e[i][0] == -1) { printf("NO"); return 0; } if (color[e[i][0]] == 0) { was[i] = was[e[i][0]] = true; } else { int u = e[i][0]; while (color[u] == 1) { color[u] = 0; if (u == e[u][1]) break; u = e[u][1]; color[u] = 0; if (u == e[u][0]) break; u = e[u][0]; if (u == -1) { printf("NO"); return 0; } } } } } printf("YES\n"); for (int i = (0); i < (n); i++) { printf("%d ", color[i]); } return 0; }
6
#include <bits/stdc++.h> typedef long long ll; using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while(t--){ int n; cin >> n; int arr[n]; int eq; for(int i=0;i<n;i++){ cin >> arr[i]; } eq = 0; int eq2; for(int i=1;i<n;i++){ if(arr[i] != arr[eq]){ eq2 =i; } } int c1 = 0; int c2 = 0; for(int i=0;i<n;i++){ if(arr[i] ==arr[eq]){ c1++; }else{ c2++; } } if(c1 > c2){ cout << eq2 + 1; }else cout << eq+1; cout << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; template <class T> using vec = vector<T>; template <typename Iter> ostream& _IterOutput_(ostream& o, Iter b, Iter e, const string ss = "", const string se = "") { o << ss; for (auto it = b; it != e; it++) o << (it == b ? "" : ", ") << *it; return o << se; } template <typename T1, typename T2> ostream& operator<<(ostream& o, pair<T1, T2>& pair) { return o << "(" << pair.first << ", " << pair.second << ")"; } template <typename T> ostream& operator<<(ostream& o, const vector<T>& vec) { return _IterOutput_(o, begin(vec), end(vec), "[", "]"); } template <typename T> ostream& operator<<(ostream& o, const set<T>& st) { return _IterOutput_(o, begin(st), end(st), "{", "}"); } template <typename T, size_t N> ostream& operator<<(ostream& o, const array<T, N>& arr) { return _IterOutput_(o, begin(arr), end(arr), "|", "|"); } using vint = vec<int>; const int MX = 402020; int o1[MX], o2[MX]; int fv[MX]; bool kmp(int N, int M) { int t = -1; fv[0] = -1; for (int(i) = (1); (i) != (N); (i) += ((N) > (1) ? 1 : -1)) { while (t != -1 and o1[t + 1] != o1[i]) { t = fv[t]; } if (o1[t + 1] == o1[i]) t++; fv[i] = t; ; } t = -1; for (int(i) = 0; (i) < (M); (i)++) { while (t != -1 and o1[t + 1] != o2[i]) { t = fv[t]; } if (o1[t + 1] == o2[i]) t++; if (t == N - 1) return true; } return false; } int32_t main() { do { ios_base::sync_with_stdio(0); cin.tie(0); } while (0); int N; cin >> N; vint i1(N), i2(N); bool hs0 = false; for (int(i) = 0; (i) < (N); (i)++) { cin >> i1[i]; hs0 |= (!i1[i]); } for (int(i) = 0; (i) < (N); (i)++) { cin >> i2[i]; } if (!hs0) { if (i1 == i2) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; } int id1, id2; id1 = id2 = 0; for (int(i) = 0; (i) < (N); (i)++) { if (i1[i]) o1[id1++] = i1[i]; if (i2[i]) o2[id2++] = i2[i]; } for (int(i) = 0; (i) < (id2); (i)++) o2[i + id2] = o2[i]; if (kmp(id1, id2 * 2)) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; long long gcd(long long n1, long long n2) { if (!n1) return n2; if (!n2) return n1; if (n1 % n2 == 0) return n2; return gcd(n2, n1 % n2); } long long powmod(long long base, long long exponent) { base %= 1000000007; long long ans = 1; while (exponent) { if (exponent & 1) ans = (ans * base) % 1000000007; base = (base * base) % 1000000007; exponent /= 2; } ans %= 1000000007; return ans; } vector<int> adj[1000100 + 1]; int in[1000100 + 1]; int out[1000100 + 1]; int arr[1000100 + 1]; vector<pair<int, int> > pre[1000100 + 1]; int timer = 0; void dfs(int node, int lv) { in[node] = (++timer); pre[lv].push_back(make_pair(timer, arr[node])); for (int i = 0; i < adj[node].size(); i++) dfs(adj[node][i], lv + 1); out[node] = (timer); } int solve(int i, int j, int h) { int ret = 0, l, r, mid; l = 0; r = pre[h].size() - 1; while (r - l > 1) { mid = (l + r) >> 1; if (pre[h][mid].first > i) r = mid; else l = mid; } while (pre[h][r].first > i) r--; ret ^= pre[h][r].second; l = 0; r = pre[h].size() - 1; while (r - l > 1) { mid = (l + r) >> 1; if (pre[h][mid].first > j) r = mid; else l = mid; } while (pre[h][r].first > j) r--; ret ^= pre[h][r].second; return ret; } int main() { int i, j, k, n, q, v, h; scanf("%d", &n); scanf("%d", &q); for (i = 2; i <= n; i++) { scanf("%d", &j); adj[j].push_back(i); } for (i = 1; i <= n; i++) { char c; scanf(" %c", &c); int temp = (c - 'a'); arr[i] = (1 << temp); } for (i = 1; i <= n + 1; i++) { pre[i].push_back(make_pair(0, 0)); } dfs(1, 1); for (i = 1; i <= n + 1; i++) { for (j = 1; j < pre[i].size(); j++) pre[i][j].second ^= pre[i][j - 1].second; } while (q--) { scanf("%d", &v); scanf("%d", &h); int res = (solve(in[v] - 1, out[v], h)); if (res & (res - 1)) printf("No\n"); else printf("Yes\n"); } return 0; }
7
#include <bits/stdc++.h> using namespace std; const int inf = 2000000000; int a[25][25], pow2[25], b[25], dp[(1 << 20) + 10], dp2[(1 << 20) + 10]; char s[25][25]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%s", s[i] + 1); for (int i = 0; i < n; i++) for (int j = 1; j <= m; j++) scanf("%d", &a[i][j]); pow2[0] = 1; for (int i = 1; i <= n; i++) pow2[i] = pow2[i - 1] << 1; dp[0] = 0; for (int i = 1; i < pow2[n]; i++) dp[i] = inf; for (int j = 1; j <= m; j++) for (int k = 97; k < 123; k++) { int cnt = 0, sum = 0, maxi = 0, f = 0; for (int i = 0; i < n; i++) if (s[i][j] == k) { f |= pow2[i]; b[++cnt] = i; sum += a[i][j]; maxi = max(maxi, a[i][j]); } if (cnt == 0) continue; if (cnt == 1) { for (int i = 0; i < pow2[n]; i++) dp[i | f] = min(dp[i | f], dp[i]); continue; } for (int i = 0; i < pow2[n]; i++) dp2[i] = dp[i]; for (int i = 0; i < pow2[n]; i++) dp2[i | f] = min(dp2[i | f], dp[i] + sum - maxi); for (int i = 1; i <= cnt; i++) for (int p = 0; p < pow2[n]; p++) dp2[p | pow2[b[i]]] = min(dp2[p | pow2[b[i]]], dp2[p] + a[b[i]][j]); for (int i = 0; i < pow2[n]; i++) dp[i] = dp2[i]; } printf("%d\n", dp[pow2[n] - 1]); return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; if (n == 1) { cout << "! 1 1" << endl; continue; } vector<int> v1, v2; for (int i = 1; i + 1 <= n; i += 2) { cout << "? " << i << " " << i + 1 << endl; char ans; cin >> ans; if (ans == '<') { v1.push_back(i); v2.push_back(i + 1); } else { v1.push_back(i + 1); v2.push_back(i); } } if (n & 1) { v1.push_back(n); v2.push_back(n); } int mn = v1[0]; for (int i = 1; i < v1.size(); i++) { cout << "? " << v1[i] << " " << mn << endl; char ans; cin >> ans; if (ans == '<') { mn = v1[i]; } } int mx = v2[0]; for (int i = 1; i < v2.size(); i++) { cout << "? " << v2[i] << " " << mx << endl; char ans; cin >> ans; if (ans == '>') { mx = v2[i]; } } cout << "! " << mn << " " << mx << endl; } }
5
#include <bits/stdc++.h> using namespace std; int ans = 0; int n; int cnt = 26; bool cant[26]; int main() { cin >> n; for (int i = 0; i + 1 < n; i++) { char f; cin >> f; string s; cin >> s; if (f == '.') { for (char j : s) { cnt += cant[j - 'a']; cant[j - 'a'] = true; cnt -= cant[j - 'a']; } } else if (f == '?') { if (cnt == 1) ++ans; cnt += cant[s[0] - 'a']; cant[s[0] - 'a'] = true; cnt -= cant[s[0] - 'a']; } else { if (cnt == 1) ++ans; set<char> used; for (char j : s) used.insert(j); for (char j = 'a'; j <= 'z'; ++j) { if (used.count(j) == 0) { cnt += cant[j - 'a']; cant[j - 'a'] = true; cnt -= cant[j - 'a']; } } } } cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, k; vector<vector<pair<int, int> > > adj(100005); bool visited[100005]; deque<int> nodes; long long ans1 = 0; long long ans = 1; int x = 1; long long mod = 1000000007; void dfs(int nd) { visited[nd] = true; for (int i = 0; i < adj[nd].size(); i++) { if (visited[adj[nd][i].first] == false) { if (adj[nd][i].second == 1) { nodes.push_back(adj[nd][i].first); } else { x++; dfs(adj[nd][i].first); } } } } int main() { scanf("%d %d", &n, &k); int a, b, c; for (int i = 0; i < int(n - 1); i++) { scanf("%d %d %d", &a, &b, &c); adj[a].push_back(make_pair(b, c)); adj[b].push_back(make_pair(a, c)); } memset(visited, false, sizeof visited); nodes.push_back(1); for (int i = 0; i < int(k); i++) { ans = (n * ans) % mod; } long long aux = 1; while (nodes.size() > 0) { x = 1; int aux2 = nodes.back(); nodes.pop_back(); dfs(aux2); aux = 1; for (int i = 0; i < int(k); i++) { aux = (x * aux) % mod; } ans1 = (ans1 + aux) % mod; } if (ans1 > ans) { ans += mod; } cout << (ans - ans1) % mod << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; inline void read(void) {} long long int n, a, b; long long int data[100003]; vector<pair<long long int, long long int> > v; inline void solve(void) { cin >> n >> a >> b; for (int i = 0; i < n; i++) scanf("%lld", &data[i]); for (int i = 0; i < n; i++) { printf("%lld ", ((data[i] * a) % b) / a); } } int main() { solve(); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, m; cin >> n >> m; int v[n + 1][m + 1]; int nega = 0; int mini = INT_MAX; int ii, ij; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int x; cin >> x; v[i][j] = x; if (x < 0) nega++; if (abs(x) < mini) { ii = i; ij = j; mini = abs(x); } } } int sum = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (i == ii and j == ij) continue; sum += abs(v[i][j]); } } if (nega % 2 != 0) v[ii][ij] < 0 ? sum += v[ii][ij] : sum += (-v[ii][ij]); else sum += abs(v[ii][ij]); cout << sum << "\n"; } }
1
#include <bits/stdc++.h> using namespace std; int n, m; const int mod = 1e9 + 7; char str[550][550]; int dp[2][550][550]; bool check(int x1, int y1, int x2, int y2) { if (x1 >= 0 && x1 < n && y1 >= 0 && y1 < m && x2 >= 0 && x2 < n && y2 >= 0 && y2 < m && str[x1][y1] == str[x2][y2]) return true; return false; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) scanf("%s", str[i]); int now = 0; int pre; if (str[0][0] == str[n - 1][m - 1]) dp[now][0][m - 1] = 1; else dp[now][0][m - 1] = 0; for (int k = 1; k <= (n + m - 2) / 2; k++) { pre = now; now ^= 1; memset(dp[now], 0, sizeof(dp[now])); for (int i = 0; i < m; i++) { for (int j = i; j < m; j++) { int x1 = k - i; int y1 = i; int y2 = j; int x2 = n + m - 2 - j - k; if (!check(x1, y1, x2, y2)) continue; if (check(x1 - 1, y1, x2, y2 + 1)) dp[now][i][j] = (dp[now][y1][y2] + dp[pre][y1][y2 + 1]) % mod; if (check(x1 - 1, y1, x2 + 1, y2)) dp[now][i][j] = (dp[now][y1][y2] + dp[pre][y1][y2]) % mod; if (check(x1, y1 - 1, x2, y2 + 1)) dp[now][i][j] = (dp[now][y1][y2] + dp[pre][y1 - 1][y2 + 1]) % mod; if (check(x1, y1 - 1, x2 + 1, y2)) dp[now][i][j] = (dp[now][y1][y2] + dp[pre][y1 - 1][y2]) % mod; } } } long long ans = 0; for (int i = 0; i < m; i++) ans = (ans + dp[now][i][i]) % mod; if ((n + m) % 2 == 1) { for (int i = 0; i < m - 1; i++) ans = (ans + dp[now][i][i + 1]) % mod; } cout << ans << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; const int MLG = 31; int f[MLG] = {-1, 1, 2, 1, 4, 3, 2, 1, 5, 6, 2, 1, 8, 7, 5, 9, 8, 7, 3, 4, 7, 4, 2, 1, 10, 9, 3, 6, 11, 12, 12}; int cnt[MLG], n; set<int> s; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 2; 1ll * i * i <= n; i++) { if (s.count(i)) continue; int num = 0; for (long long j = i; j <= n; j *= i) { num++; s.insert(j); } cnt[num]++; } cnt[1] += n - int((s).size()); int ans = 0; for (int i = 1; i < MLG; i++) { if (cnt[i] & 1) ans ^= f[i]; } if (ans) cout << "Vasya\n"; else cout << "Petya\n"; return 0; }
7
#include <bits/stdc++.h> using namespace std; const int Mod = 1000000007; int add(int first, int second) { first += second; if (first >= Mod) return first - Mod; return first; } int mul(int first, int second) { return (int)((long long)first * second % Mod); } int pot(int first, int second) { int r = 1; while (second) { if (second & 1) r = mul(r, first); first = mul(first, first), second >>= 1; } return r; } int f(long long n) { return (int)(n * (n - 1) / 2 % Mod); } int n, m; vector<pair<int, long long> > E[100005]; int gr[100005]; int root; int bio[100005]; vector<long long> all[100005]; vector<long long> comp[100005]; long long xd[100005]; int dep[100005]; void dfs(int id, int p) { comp[root].push_back(xd[id]); gr[id] = root; bio[id] = 1; for (auto e : E[id]) { if (e.first == p) continue; if (bio[e.first] == 0) { dep[e.first] = 1 + dep[id]; xd[e.first] = xd[id] ^ e.second; dfs(e.first, id); } else if (bio[e.first] == 1) { all[root].push_back(xd[id] ^ xd[e.first] ^ e.second); } else if (bio[e.first] == 2) { } } bio[id] = 2; } int main() { ios_base::sync_with_stdio(false); cin >> n >> m; for (int i = (int)(0); i < (int)(m); ++i) { int u, v; long long t; cin >> u >> v >> t; --u, --v; E[u].push_back({v, t}); E[v].push_back({u, t}); } int sol = 0; for (int i = (int)(0); i < (int)(n); ++i) if (!bio[i]) { root = i; dfs(i, i); vector<long long> &cv = all[i]; vector<long long> gen; for (int d = 60; d >= 0; --d) { long long tmp = -1; for (int ii = (int)(0); ii < (int)(cv.size()); ++ii) if ((cv[ii] >> d) & 1) tmp = cv[ii]; if (tmp == -1) continue; gen.push_back(tmp); for (auto &t : cv) if (t > (t ^ tmp)) t ^= tmp; } for (int d = (int)(0); d < (int)(60); ++d) { bool sym = false; for (auto t : gen) if ((t >> d) & 1) sym = true; int cnt[2] = {0, 0}; for (auto t : comp[root]) ++cnt[(t >> d) & 1]; if (sym) { sol = add(sol, mul(pot(2, d), mul(pot(2, (int)gen.size() - 1), f(cnt[0] + cnt[1])))); } else { sol = add(sol, mul(pot(2, d), mul(pot(2, (int)gen.size()), mul(cnt[0], cnt[1])))); } } } cout << sol << endl; return 0; }
9
#include <bits/stdc++.h> using namespace std; const int N = (int)1e5 + 123; const long long INF = (long long)1e18 + 123; const int inf = (int)1e9 + 123; int n; char c[10 * N]; int d[10 * N]; bool u[10 * N]; int s; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> c[i]; } for (int i = 1; i <= n; i++) { cin >> d[i]; } s = 1; while (1) { if (s < 1 || s > n) { cout << "FINITE"; return 0; } u[s] = 1; if (c[s] == '>') { s += d[s]; if (s < 1 || s > n) { cout << "FINITE"; return 0; } if (u[s]) { cout << "INFINITE"; return 0; } } else { s -= d[s]; if (s < 1 || s > n) { cout << "FINITE"; return 0; } if (u[s]) { cout << "INFINITE"; return 0; } } } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; vector<int> v[N]; vector<pair<int, int> > qs[N]; int mul[N], fen[N], ans[N]; void add(int x) { for (; x < N; x += x & -x) fen[x]++; } int get(int x) { int ret = 0; for (; x; x -= x & -x) ret += fen[x]; return ret; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < n; i++) { int l, r; cin >> l >> r; v[l].push_back(r); } for (int i = 0; i < m; i++) { int cnt, las = 0; cin >> cnt; mul[i] = -1; for (int _ = 0; _ < cnt; _++) { int p; cin >> p; qs[las].push_back(make_pair(p, i)); qs[p].push_back(make_pair(p, i)); las = p; } } for (int i = 0; i < N; i++) { for (auto j : v[i]) add(N - j); for (auto j : qs[i]) { ans[j.second] += mul[j.second] * get(N - j.first); mul[j.second] *= -1; } } for (int i = 0; i < m; i++) cout << ans[i] << "\n"; }
7
#include <bits/stdc++.h> using namespace std; struct big_integer { static const int base = 1000; static const int width = 3; vector<int> v; bool is_negative; big_integer(long long num = 0) { *this = num; is_negative = false; } big_integer operator=(long long val) { v.clear(); if (val < 0) { val = -val; is_negative = true; } else is_negative = false; do { v.push_back(val % base); val /= base; } while (val > 0); return *this; } big_integer operator=(const string &str) { v.clear(); string str2 = str; if (str2[0] == '-') { is_negative = true; for (string::iterator i = str2.begin(); i < str2.end() - 1; i++) { *i = *(i + 1); } *(str2.end() - 1) = '\0'; str2.resize(str2.length() - 1); str2.shrink_to_fit(); } int cur, len = (str2.length() - 1) / width + 1; for (int i = 0; i < len; i++) { int end = str2.length() - i * width; int begin = max(0, end - width); sscanf(str2.substr(begin, end - begin).c_str(), "%d", &cur); v.push_back(cur); } return *this; } big_integer operator+(const big_integer &addend) const { big_integer sum; sum.v.clear(); if (is_negative == true) { if (addend.is_negative == true) sum.is_negative = true; else return addend - (-*this); } else if (addend.is_negative == true) { return *this - (-addend); } for (int i = 0, carry = 0; carry != 0 || i < v.size() || i < addend.v.size(); i++) { int cur = carry; if (i < v.size()) cur += v[i]; if (i < addend.v.size()) cur += addend.v[i]; carry = cur / base; cur %= base; sum.v.push_back(cur); } return sum; } big_integer operator-(const big_integer &subtrahend) const { big_integer difference; difference.v.clear(); if (is_negative == false && subtrahend.is_negative == false && *this < subtrahend) return -(subtrahend - *this); if (is_negative == true && subtrahend.is_negative == false) return -(-*this + subtrahend); else if (is_negative == false && subtrahend.is_negative == true) return *this + (-subtrahend); else if (is_negative == true && subtrahend.is_negative == true) return -subtrahend - (-*this); for (int i = 0, carry = 0; carry != 0 || i < v.size(); i++) { int cur = v[i] + carry; if (i < subtrahend.v.size()) cur -= subtrahend.v[i]; if (cur >= 0) carry = 0; else { carry = -1; cur += base; } difference.v.push_back(cur); } for (int i = difference.v.size() - 1; i >= 0; i--) { if (difference.v[i] == 0) difference.v.resize(difference.v.size() - 1); else break; } difference.v.shrink_to_fit(); if (difference.v.size() == 0) difference.v.resize(1, 0); return difference; } big_integer operator-() const { big_integer big_int = *this; big_int.is_negative = !(is_negative); return big_int; } big_integer operator*(const big_integer &multiplier) const { big_integer product; product.v.clear(); if (is_negative != multiplier.is_negative) product.is_negative = true; for (int i = 0; i < v.size(); i++) { for (int j = 0; j < multiplier.v.size(); j++) { if (i + j >= product.v.size()) product.v.resize(i + j + 1, 0); product.v[i + j] += this->v[i] * multiplier.v[j]; if (product.v[i + j] >= base) { if (i + j + 1 >= product.v.size()) product.v.resize(i + j + 1 + 1, 0); product.v[i + j + 1] += product.v[i + j] / base; product.v[i + j] %= base; } } } for (int i = product.v.size() - 1; i >= 0; i--) { if (product.v[i] == 0) product.v.resize(product.v.size() - 1); else break; } product.v.shrink_to_fit(); if (product.v.size() == 0) product.v.resize(1, 0); return product; } bool operator<(const big_integer &big_int) const { if (is_negative == true && big_int.is_negative == false) return true; if (is_negative == false && big_int.is_negative == true) return false; if (is_negative == true && big_int.is_negative == true) { big_integer big_int1, big_int2; big_int1 = *this; big_int1.is_negative = false; big_int2 = big_int; big_int2.is_negative = false; return big_int2 < big_int1; } if (v.size() != big_int.v.size()) return v.size() < big_int.v.size(); for (int i = v.size() - 1; i >= 0; i--) { if (v[i] != big_int.v[i]) return v[i] < big_int.v[i]; } return false; } friend istream &operator>>(istream &in, big_integer &big_int) { string str; if (!(in >> str)) return in; big_int = str; return in; } friend ostream &operator<<(ostream &out, big_integer &big_int) { if (big_int.is_negative == true) out << '-'; out << big_int.v.back(); for (int i = big_int.v.size() - 1 - 1; i >= 0; i--) { char str[base + 5]; sprintf(str, "%0*d", width, big_int.v[i]); out << str; } return out; } }; long long n, m; big_integer sum = 0; long long x, d; big_integer bi; int main() { cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> x >> d; bi = x * n, sum = sum + bi; if (d < 0) { if (n % 2 == 0) bi = d * n * n / 4; else bi = d * (n * n - 1) / 4; } else bi = d * n * (n - 1) / 2; sum = sum + bi; } big_integer quotient = 0; long long remainder = 0; for (int i = sum.v.size() - 1; i >= 0; i--) { remainder = remainder * sum.base + sum.v[i]; quotient = quotient * sum.base + remainder / n; remainder = remainder - remainder / n * n; } for (int i = quotient.v.size() - 1; i >= 0; i--) { if (quotient.v[i] == 0) quotient.v.resize(quotient.v.size() - 1); else break; } quotient.v.shrink_to_fit(); if (quotient.v.size() == 0) quotient.v.resize(1, 0); long double frac = (long double)(1.0 * remainder / n); string fracs = to_string(frac); if (sum.is_negative == true) cout << '-'; cout << quotient; cout << fracs.substr(1, fracs.length() - 1) << '\n'; return 0; }
4
#include <bits/stdc++.h> using namespace std; long long n1, n2, n3, day, month, year, m[105]; bool ok; string s; bool check(long long da, long long mont, long long yea) { if (yea % 4 == 0) m[2] = 29; else m[2] = 28; if (mont < 13 && da <= m[mont] && (year - yea > 18 || year - yea == 18 && mont < month || year - yea == 18 && mont == month && da <= day)) return 1; else return 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); m[1] = 31; m[3] = 31; m[4] = 30; m[5] = 31; m[6] = 30; m[7] = 31; m[8] = 31; m[9] = 30; m[10] = 31; m[11] = 30; m[12] = 31; cin >> s; day = (s[0] - '0') * 10 + s[1] - '0'; month = (s[3] - '0') * 10 + s[4] - '0'; year = (s[6] - '0') * 10 + s[7] - '0'; cin >> s; n1 = (s[0] - '0') * 10 + s[1] - '0'; n2 = (s[3] - '0') * 10 + s[4] - '0'; n3 = (s[6] - '0') * 10 + s[7] - '0'; if (check(n1, n2, n3)) cout << "YES"; else if (check(n1, n3, n2)) cout << "YES"; else if (check(n2, n3, n1)) cout << "YES"; else if (check(n2, n1, n3)) cout << "YES"; else if (check(n3, n2, n1)) cout << "YES"; else if (check(n3, n1, n2)) cout << "YES"; else cout << "NO"; return 0; }
4
#include <bits/stdc++.h> using namespace std; long long modpow(long long pot) { if (pot == 0) return 1; if (pot == 1) return 2; if (pot & 1) return 2 * modpow(pot - 1) % 1000000007; long long k = modpow(pot / 2) % 1000000007; return (k * k) % 1000000007; } int main() { long long n; cin >> n; if (n == 0) { cout << 1; return 0; } long long a = modpow(n - 1); long long b = (a * 2 + 1) % 1000000007; cout << (a * b) % 1000000007; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MAX_BUF_SIZE = 16384; char BUFOR[MAX_BUF_SIZE]; int BUF_SIZE, BUF_POS; char ZZZ; const int MXN = 1000010; const int C = 262144; const int INF = 1000000001; char str[MXN]; int n; void test() { scanf("%s", str + 1); n = strlen(str + 1); int sum1 = 0, sum2 = 0; for (int i = (1); i <= (n); i++) { int iter = i; while (iter <= n && isalpha(str[iter])) iter++; int val1 = 0, val2 = 0; bool kr = 0; while (iter <= n) { int ile = 0; int tmp = 0; while (iter + ile <= n && isdigit(str[iter + ile])) { tmp = tmp * 10 + (str[iter + ile] - '0'); ile++; } if (iter + ile > n || str[iter + ile] != '.') { if (ile == 3 || !kr) val1 = 1000 * val1 + tmp; else val2 = tmp; iter = iter + ile; break; } else { kr = 1; val1 = 1000 * val1 + tmp; iter = iter + ile; } iter++; } sum1 += val1; sum2 += val2; i = iter - 1; } sum1 += sum2 / 100; sum2 %= 100; vector<int> vec; if (sum1 == 0) vec.push_back(0); while (sum1) { vec.push_back(sum1 % 1000); sum1 /= 1000; } printf("%d", vec.back()); vec.pop_back(); for (int i = ((int)(vec).size() - 1); i >= (0); i--) { int X = vec[i]; if (X >= 100) { printf(".%d", X); } else if (X >= 10) { printf(".0%d", X); } else { printf(".00%d", X); } } if (sum2 > 0) printf(".%d%d\n", sum2 / 10, sum2 % 10); } int main() { int te = 1; while (te--) test(); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); int tt; cin >> tt; while (tt--) { int a, b, c, p; cin >> a >> b >> c; cout << max(a, max(b, c)) << endl; } }
0
#include <bits/stdc++.h> using namespace std; const int root = 0; int idxN; struct NODE { int son[2]; int cnt; void init() { memset(son, -1, sizeof son); cnt = 0; } } node[6000005]; int a[33]; void insert(int x, int f) { for (int i = 0; i <= 30; ++i) a[i] = (x >> (30 - i)) & 1; int i = 0, now = root; while (i <= 30) { int id = a[i]; if (node[now].son[id] == -1) { node[now].son[id] = ++idxN; node[idxN].init(); } now = node[now].son[id]; node[now].cnt += f; ++i; } } int query(int x) { for (int i = 0; i <= 30; ++i) a[i] = (x >> (30 - i)) & 1; int i = 0, now = root, res = 0; while (i <= 30) { int id = a[i]; if (node[now].son[1 - id] != -1 && node[node[now].son[1 - id]].cnt > 0) { res += 1 << (30 - i); now = node[now].son[1 - id]; } else now = node[now].son[id]; ++i; } return res; } int main() { node[root].init(), idxN = 0; insert(0, 1); int q; scanf("%d", &q); while (q--) { char s[2]; int x; scanf("%s%d", s, &x); if (s[0] == '+') { insert(x, 1); } else if (s[0] == '-') { insert(x, -1); } else { printf("%d\n", query(x)); } } return 0; }
5
#include <bits/stdc++.h> using namespace std; int n; int Sum(int x) { int res; res = 0; while (x) { res += x % 10; x /= 10; } return res; } int Root(int x) { if (x <= 9) return Sum(x); return Root(Sum(x)); } long long ans; int q[20]; int p[1000010]; int pq; bool pc[1000010]; int main() { int k; int c, g; int i, j; scanf("%d", &n); for (i = 1; i <= n; i++) { q[i % 9]++; } p[1] = 2; pq = 1; for (i = 3; i * i <= n; i += 2) { if (pc[i]) continue; pq++; p[pq] = i; for (j = 2 * i; j <= n; j += i) { pc[j] = 1; } } ans = 0; for (j = 0; j <= 8; j++) { for (i = 0; i <= 8; i++) { ans += (long long)q[i] * q[j] * q[i * j % 9]; } } for (i = 1; i <= n; i++) { g = 1; k = i; for (j = 1; j <= pq; j++) { if (k == 1) break; if (p[j] > i) break; if (k % p[j] == 0) { c = 0; while (k % p[j] == 0) { c++; k /= p[j]; } g = g * (c + 1); } } if (k != 1) { g = g * 2; } ans -= g; } printf("%lld\n", ans); return 0; }
6
#include <bits/stdc++.h> int INF = 1e6; using namespace std; int main() { int n, a, k; cin >> n >> a; if (a % 2 != 0) a = n - a + 1; k = (n - a) / 2 + 1; cout << k; return 0; }
1
#include <bits/stdc++.h> using namespace std; int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } struct vec { int x[6]; int operator*(vec obj) { int ans = 0; for (int i = 1; i <= 5; i++) ans += x[i] * obj.x[i]; return ans; } vec operator+(vec obj) { vec ans; for (int i = 1; i <= 5; i++) ans.x[i] = x[i] + obj.x[i]; return ans; } vec operator-(vec obj) { vec ans; for (int i = 1; i <= 5; i++) ans.x[i] = x[i] - obj.x[i]; return ans; } void vread() { for (int i = 1; i <= 5; i++) x[i] = read(); } void vwrite() { for (int i = 1; i <= 5; i++) cout << x[i] << ' '; puts(""); } } p[1100]; int n; int bad[1100]; int main() { n = read(); for (int i = 1; i <= n; i++) p[i].vread(); for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { for (int k = j + 1; k <= n; k++) { if (bad[i] + bad[j] + bad[k] == 3) continue; vec tmp1 = p[i] - p[j], tmp2 = p[i] - p[k], tmp3 = p[j] - p[k]; if (!bad[j] && tmp1 * tmp3 < 0) bad[j] = 1; if (!bad[k] && tmp2 * tmp3 > 0) bad[k] = 1; if (!bad[i] && tmp1 * tmp2 > 0) { bad[i] = 1; goto nextstep; } } } nextstep:; } int ans = 0; for (int i = 1; i <= n; i++) { ans += bad[i]; } cout << n - ans << endl; for (int i = 1; i <= n; i++) { if (bad[i] == 0) { printf("%d\n", i); } } return 0; }
4
#include <bits/stdc++.h> const int maxn = 2002; const long long inf = 1e18; int n, m, s, t, type; template <typename t1, typename t2> inline std::ostream& operator<<(std::ostream& cout, const std::pair<t1, t2>& pr) { return cout << '(' << pr.first << ',' << pr.second << ')'; } struct T { int to, nxt, v; } way[100100 << 1]; ; int h[maxn], num; inline void adde(int x, int y, int v) { way[++num] = {y, h[x], v}, h[x] = num; way[++num] = {x, h[y], v}, h[y] = num; } int vis[maxn], p[maxn]; long long dis1[maxn], dis2[maxn]; inline void dij(int s, long long* dis) { std::priority_queue<std::pair<long long, int>, std::vector<std::pair<long long, int>>, std::greater<std::pair<long long, int>>> q; for (int i = 1; i <= n; ++i) q.push(std::pair<long long, int>(dis[i] = inf, i)), vis[i] = 0; const auto relax = [&](int x, long long v) { if (v < dis[x]) q.push(std::pair<long long, int>(dis[x] = v, x)); }; for (relax(s, 0); !q.empty();) { std::pair<long long, int> t = q.top(); q.pop(); int x = t.second; if (vis[x]) continue; vis[x] = 1; for (int i = h[x]; i; i = way[i].nxt) relax(way[i].to, dis[x] + way[i].v); } } int cnt[maxn][maxn]; long long sum[maxn][maxn]; long long dp0[maxn][maxn]; long long dp1[maxn][maxn]; inline void up(long long& x, long long y) { if (x < y) x = y; } template <typename T> inline long long getsum(T a, int x1, int x2, int y1, int y2) { return x1 <= x2 && y1 <= y2 ? a[x2][y2] - a[x2][y1 - 1] - a[x1 - 1][y2] + a[x1 - 1][y1 - 1] : 0; } int main() { std::ios::sync_with_stdio(false), std::cin.tie(0); std::cin >> n >> m >> s >> t; for (int i = 1; i <= n; ++i) std::cin >> p[i]; for (int i = 1, x, y, v; i <= m; ++i) std::cin >> x >> y >> v, adde(x, y, v); dij(s, dis1); dij(t, dis2); std::vector<long long> v1, v2; for (int i = 1; i <= n; ++i) v1.push_back(dis1[i]), v2.push_back(dis2[i]); std::sort(v1.begin(), v1.end()), v1.erase(std::unique(v1.begin(), v1.end()), v1.end()); std::sort(v2.begin(), v2.end()), v2.erase(std::unique(v2.begin(), v2.end()), v2.end()); for (int i = 1; i <= n; ++i) { int x = std::lower_bound(v1.begin(), v1.end(), dis1[i]) - v1.begin() + 1; int y = std::lower_bound(v2.begin(), v2.end(), dis2[i]) - v2.begin() + 1; ++cnt[x][y]; sum[x][y] += p[i]; } int o1 = v1.size(), o2 = v2.size(); for (int i = 1; i <= o1; ++i) for (int j = 1; j <= o2; ++j) cnt[i][j] += cnt[i - 1][j] + cnt[i][j - 1] - cnt[i - 1][j - 1], sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1]; for (int i = o1; i >= 0; --i) { for (int j = o2; j >= 0; --j) { if (i == o1 || j == o2) dp0[i][j] = dp1[i][j] = 0; else { dp0[i][j] = dp1[i][j] = -inf; if (dp0[i + 1][j] > -inf) up(dp0[i][j], dp0[i + 1][j] + getsum(sum, i + 1, i + 1, j + 1, o2)); if (dp1[i][j + 1] > -inf) up(dp1[i][j], dp1[i][j + 1] + getsum(sum, i + 1, o1, j + 1, j + 1)); if (dp1[i + 1][j] > -inf && getsum(cnt, i + 1, i + 1, j + 1, o2)) up(dp0[i][j], -dp1[i + 1][j] + getsum(sum, i + 1, i + 1, j + 1, o2)); if (dp0[i][j + 1] > -inf && getsum(cnt, i + 1, o1, j + 1, j + 1)) up(dp1[i][j], -dp0[i][j + 1] + getsum(sum, i + 1, o1, j + 1, j + 1)); } } } if (dp0[0][0] < 0) std::cout << "Cry" << '\n'; if (dp0[0][0] == 0) std::cout << "Flowers" << '\n'; if (dp0[0][0] > 0) std::cout << "Break a heart" << '\n'; }
10
#include <bits/stdc++.h> using namespace std; inline int Read() { char c = getchar(); int num = 0, f = 1; while ('0' > c || c > '9') { if (c == '-') f = -1; c = getchar(); } while ('0' <= c && c <= '9') num = num * 10 + c - '0', c = getchar(); return (num * f); } int n, m, a[200010]; struct Th { int nm, l, r; }; struct Node { pair<int, int> mxl, mxr, mnl, mnr; Th mn, mx; int sum, rv; } t[200010 * 4]; inline Th max(Th a, Th b) { if (a.nm > b.nm) return (a); return (b); } inline Th min(Th a, Th b) { if (a.nm < b.nm) return (a); return (b); } inline Node Meg(Node L, Node R) { Node res; res.rv = 0; res.mxl = max(L.mxl, make_pair(L.sum + R.mxl.first, R.mxl.second)); res.mxr = max(R.mxr, make_pair(R.sum + L.mxr.first, L.mxr.second)); res.mx = max(L.mx, R.mx); res.mx = max(res.mx, (Th){L.mxr.first + R.mxl.first, L.mxr.second, R.mxl.second}); res.mnl = min(L.mnl, make_pair(L.sum + R.mnl.first, R.mnl.second)); res.mnr = min(R.mnr, make_pair(R.sum + L.mnr.first, L.mnr.second)); res.mn = min(L.mn, R.mn); res.mn = min(res.mn, (Th){L.mnr.first + R.mnl.first, L.mnr.second, R.mnl.second}); res.sum = L.sum + R.sum; return (res); } inline void Rev(Node &x) { swap(x.mxl, x.mnl), swap(x.mxr, x.mnr), swap(x.mx, x.mn); x.mxl.first *= -1, x.mxr.first *= -1, x.mx.nm *= -1; x.mnl.first *= -1, x.mnr.first *= -1, x.mn.nm *= -1; x.sum *= -1, x.rv ^= 1; } inline void Down(int x) { if (!t[x].rv) return; Rev(t[x * 2]), Rev(t[x * 2 + 1]), t[x].rv ^= 1; } inline void Build(int x, int l, int r) { int md = (l + r) / 2; if (l == r) { t[x].mxl = t[x].mxr = make_pair(a[l], l); t[x].mnl = t[x].mnr = make_pair(a[l], l); t[x].mn = t[x].mx = (Th){a[l], l, l}; t[x].sum = a[l]; return; } Build(x * 2, l, md); Build(x * 2 + 1, md + 1, r); t[x] = Meg(t[x * 2], t[x * 2 + 1]); } inline void Chg(int x, int l, int r, int a, int b) { int md = (l + r) / 2; if (l == r) { t[x].mxl = t[x].mxr = make_pair(b, a); t[x].mnl = t[x].mnr = make_pair(b, a); t[x].mn = t[x].mx = (Th){b, a, a}; t[x].sum = b; return; } Down(x); if (a <= md) Chg(x * 2, l, md, a, b); if (md < a) Chg(x * 2 + 1, md + 1, r, a, b); t[x] = Meg(t[x * 2], t[x * 2 + 1]); } inline void Rol(int x, int l, int r, int a, int b) { int md = (l + r) / 2; if (l == a && r == b) { Rev(t[x]); return; } Down(x); if (b <= md) Rol(x * 2, l, md, a, b); if (md < a) Rol(x * 2 + 1, md + 1, r, a, b); if (a <= md && md < b) { Rol(x * 2, l, md, a, md); Rol(x * 2 + 1, md + 1, r, md + 1, b); } t[x] = Meg(t[x * 2], t[x * 2 + 1]); } inline Node Ask(int x, int l, int r, int a, int b) { int md = (l + r) / 2; if (l == a && r == b) return (t[x]); if (b <= md) return (Ask(x * 2, l, md, a, b)); if (md < a) return (Ask(x * 2 + 1, md + 1, r, a, b)); if (a <= md && md < b) { Node L = Ask(x * 2, l, md, a, md); Node R = Ask(x * 2 + 1, md + 1, r, md + 1, b); return (Meg(L, R)); } } pair<int, int> s[200010]; int tot, ans; int main() { n = Read(); for (int i = 1; i <= n; i++) a[i] = Read(); Build(1, 1, n); m = Read(); for (int i = 1; i <= m; i++) { int op = Read(), x, y, z; if (!op) x = Read(), y = Read(), Chg(1, 1, n, x, y); else { ans = tot = 0; x = Read(), y = Read(), z = Read(); for (int j = 1; j <= z; j++) { Node res = Ask(1, 1, n, x, y); if (res.mx.nm < 0) break; s[++tot] = make_pair(res.mx.l, res.mx.r); ans += res.mx.nm; Rol(1, 1, n, res.mx.l, res.mx.r); } printf("%d\n", ans); for (int j = 1; j <= tot; j++) Rol(1, 1, n, s[j].first, s[j].second); } } }
10
#include <bits/stdc++.h> using namespace std; int a[(1 << 22)]; int order[(1 << 22)]; int ans[(1 << 22)]; int exam[(1 << 22)]; int exam2[(1 << 22)]; int n; int main() { cin >> n; for (int i = 0; i < n; i++) { int a2; scanf("%d", &a2); a[a2] = 1; order[i] = a2; } for (int i = 0; i < (1 << 22); i++) if (a[i]) exam[(~i) & ((1 << 22) - 1)] = i; else exam[(~i) & ((1 << 22) - 1)] = -1; int j2; for (int i = 1; i <= 22; i++) { for (int j = 0; j < (1 << 22); j++) { if (j & (1 << (i - 1))) exam2[j] = exam[j]; else if (exam[j] != -1) exam2[j] = exam[j]; else exam2[j] = exam[j + (1 << (i - 1))]; } i++; for (int j = 0; j < (1 << 22); j++) { if (j & (1 << (i - 1))) exam[j] = exam2[j]; else if (exam2[j] != -1) exam[j] = exam2[j]; else exam[j] = exam2[j + (1 << (i - 1))]; } } for (int i = 0; i < n; i++) printf("%d ", exam[order[i]]); }
7
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; long long ans = 0; for (int i = (0); i < (n); ++i) { long long t, T, x, cost; cin >> t >> T >> x >> cost; if (t >= T) { ans += cost + m * x; continue; } long long aux1 = cost; if (m > (T - t)) aux1 += m * x; long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; aux2 *= cost; ans += min(aux1, aux2); } cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 1000 + 5; const int mod = 1e9 + 7; long long dp[maxn][maxn]; int k, pa, pb; int fb(int a, int n) { if (n == 0) return 1; int u = fb(a, n / 2); int res = 1LL * u * u % mod; if (n & 1) return 1LL * res * a % mod; else return res; } int inv(int x) { return fb(x, mod - 2); } int invbase, invb; int getdp(int a, int ab) { if (a + ab >= k) return (a + ab + 1LL * pa * invb) % mod; if (ab >= k) return ab; if (dp[a][ab] != -1) return dp[a][ab]; return dp[a][ab] = 1LL * (1LL * getdp(a + 1, ab) * pa % mod + 1LL * getdp(a, ab + a) * pb % mod) * invbase % mod; } int main() { cin >> k >> pa >> pb; invbase = inv(pa + pb); invb = inv(pb); memset(dp, -1, sizeof(dp[0][0]) * maxn * maxn); cout << getdp(1, 0); }
7
#include <bits/stdc++.h> using namespace std; int main() { double m, r; double t = sqrt(2); double ret = 0; int i; cin >> m >> r; for (i = 0; i < m; i++) { ret += 2 * r; if (i + 1 < m) { ret += 2 * r; ret += r * t; } if (i + 2 < m) { ret += r * (m - i - 1) * (m - i - 2); ret += 2 * t * r * (m - i - 2); } if (i - 1 >= 0) { ret += 2 * r; ret += r * t; } if (i - 2 >= 0) { ret += r * i * (i - 1); ret += 2 * t * r * (i - 1); } } printf("%lf\n", (ret) / (m * m)); return 0; }
5
#include <bits/stdc++.h> using namespace std; template <typename T> T in() { char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == '-') { ng = true; ch = getchar(); break; } if (ch >= '0' && ch <= '9') break; } while (1) { n = n * 10 + (ch - '0'); ch = getchar(); if (ch < '0' || ch > '9') break; } return (ng ? -n : n); } template <typename T> inline T Dist(T x1, T y1, T x2, T y2) { return sqrt((x1 - x2 * x1 - x2) + (y1 - y2 * y1 - y2)); } template <typename T> inline T POW(T B, T P) { if (P == 0) return 1; if (P & 1) return B * POW(B, P - 1); else return (POW(B, P / 2) * POW(B, P / 2)); } template <typename T> inline T ABS(T a) { if (a < 0) return -a; else return a; } template <typename T> inline T Gcd(T a, T b) { if (a < 0) return Gcd(-a, b); if (b < 0) return Gcd(a, -b); return (b == 0) ? a : Gcd(b, a % b); } template <typename T> inline T Lcm(T a, T b) { if (a < 0) return Lcm(-a, b); if (b < 0) return Lcm(a, -b); return a * (b / Gcd(a, b)); } long long Bigmod(long long base, long long power, long long MOD) { long long ret = 1; while (power) { if (power & 1) ret = (ret * base) % MOD; base = (base * base) % MOD; power >>= 1; } return ret; } long long ModInverse(long long number, long long MOD) { return Bigmod(number, MOD - 2, MOD); } int Set(int N, int pos) { return N = N | (1 << pos); } int Reset(int N, int pos) { return N = N & ~(1 << pos); } bool Chkbit(int N, int pos) { return (bool)(N & (1 << pos)); } bool isVowel(char ch) { ch = toupper(ch); if (ch == 'A' || ch == 'U' || ch == 'I' || ch == 'O' || ch == 'E') return true; return false; } bool isConst(char ch) { if (isalpha(ch) && !isVowel(ch)) return true; return false; } int toInt(string s) { int sm; stringstream ss(s); ss >> sm; return sm; } long long int toLlint(string s) { long long int sm; stringstream ss(s); ss >> sm; return sm; } int ans; void Solve(vector<int> v, int n) { if (n == 6) { int tt = 0; for (int i = 1; i <= 2; i++) { int tp = 0; for (int j = 1; j <= 6; j += i) { tp += v[j - 1]; } if (tp != 6) { tt++; } } if (tt) ans++; return; } for (int i = 1; i <= 3; i++) { vector<int> t = v; t.push_back(i); Solve(t, n + 1); } } int main() { long long int ans = 1; long long int n; cin >> n; n *= 3; for (int i = 1; i < n + 1; i++) { ans = (3LL * ans) % 1000000007; } long long int tp = 1; n /= 3; for (int i = 1; i < n + 1; i++) { tp = (7LL * tp) % 1000000007; } ans = (ans - tp + 1000000007) % 1000000007; cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; void setIO(string name = "") { ios_base::sync_with_stdio(0); cin.tie(0); if (name.length()) { freopen((name + ".in").c_str(), "r", stdin); freopen((name + ".out").c_str(), "w", stdout); } } int n; int A[200001]; int main() { setIO(""); cin >> n; for (int i = 0; i < n; i++) { cin >> A[i]; } vector<vector<int>> seqs; int m = 1; seqs.push_back({A[0]}); for (int i = 1; i < n; i++) { int l = 0, r = m; while (l < r) { int mid = (l + r) / 2; if (seqs[mid][seqs[mid].size() - 1] < A[i]) { r = mid; } else { l = mid + 1; } } if (r == m) seqs.push_back({A[i]}), m++; else seqs[r].push_back(A[i]); } for (int i = 0; i < m; i++) { for (int j : seqs[i]) { cout << j << ' '; } cout << '\n'; } return 0; }
4
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { for (; b; a %= b, swap(a, b)) ; return a; } int dx[4] = {1, -1, 0, 0}; int dy[4] = {0, 0, 1, -1}; int main(void) { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); long long n, m; cin >> n >> m; vector<long long> a(n); set<pair<long long, long long> > st; for (long long i = 0; i < n; i++) cin >> a[i]; long long last = a[n - 1]; for (long long i = 0; i < m; i++) { long long x, y; cin >> x >> y; st.insert({x, y}); } long long ans = 0; for (long long i = n - 2; i >= 0; i--) { bool flag = false; long long j = i; while (j <= n - 2 && st.count({a[j], a[j + 1]}) && !flag) { if (a[j + 1] == last) flag = true; swap(a[j], a[j + 1]); j++; } if (flag) ans++; } cout << ans; }
5
#include <bits/stdc++.h> using namespace std; vector<array<int, 2>> ans; void solve(vector<int> &a) { if (a.size() <= 1) return; vector<int> id[2]; for (int i = 0; i < a.size(); i++) id[i & 1].push_back(a[i]); solve(id[0]); solve(id[1]); for (int i = 0; i < int(a.size()) / 2; i++) ans.push_back({id[0][i], id[1][i]}); } signed main() { std::ios::sync_with_stdio(0); std::cout.tie(0); std::cin.tie(0); int n; cin >> n; int p = 1; while (2 * p <= n) p *= 2; vector<int> a(p); iota(a.begin(), a.end(), 1); solve(a); for (int i = 0; i < p; i++) a[i] = n - i; solve(a); cout << ans.size() << '\n'; for (auto &[i, j] : ans) cout << i << ' ' << j << '\n'; return 0; }
7
#include <bits/stdc++.h> using namespace std; int flip(int x, int a, int b) { if (a <= x && x <= b) return a + b - x; return x; } int n; int x[31]; double p[31][31]; double nextP[31][31]; int MAIN() { int times; cin >> n >> times; for (int i = 1; i <= n; i++) cin >> x[i]; memset(p, 0, sizeof(p)); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (x[i] < x[j]) p[i][j] = 1; int sum = n * (n + 1) / 2; for (int i = 1; i <= times; i++) { memset(nextP, 0, sizeof(nextP)); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) for (int L = 1; L <= n; L++) for (int R = L; R <= n; R++) { nextP[flip(i, L, R)][flip(j, L, R)] += p[i][j] * 1.0 / sum; } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) p[i][j] = nextP[i][j]; } double ans = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) if (i > j) ans += p[i][j]; cout << ans << endl; return 0; } int main() { int start = clock(); ios ::sync_with_stdio(false); cout << fixed << setprecision(16); int ret = MAIN(); return ret; }
8
#include <bits/stdc++.h> using namespace std; const int U = 80; const int L = 20000; long long n; int m, a[111]; int F[U][L]; int f0(int n, int i) { if (n == 0 || i < 0) return n; int &res = F[i][n]; if (res < 0) { res = n; for (; i >= 0; --i) res -= f0(n / a[i], i - 1); } return res; } long long f(long long n, int i) { if (n == 0) return 0; if (i < U && n < L) return f0(n, i); long long res = n; for (; i >= 0; --i) res -= f(n / a[i], i - 1); return res; } int main() { memset(F, -1, sizeof F); cin >> n >> m; for (int i(0); i < m; ++i) cin >> a[i]; sort(a, a + m); cout << f(n, m - 1) << endl; return 0; }
9
#include <bits/stdc++.h> using namespace std; inline double sqr(double x) { return x * x; } #pragma comment(linker, "/STACK:16777216") int k; string lst(string s, int n) { int i = s.size(), ctr = 0; string r, v = "aeiou"; while (i--) { r = s[i] + r; if (v.find(s[i]) != -1) ++ctr; if (ctr == k) return r; } return "" + char('0' + n); } int main() { char _s[10001]; string s[4]; int v = 7, n; cin >> n >> k; gets(_s); while (n-- && v) { for (int i = int(0); i < int(4); ++i) { gets(_s); s[i] = lst(_s, i); } if (s[0] != s[1] || s[2] != s[3]) v &= ~1; if (s[0] != s[2] || s[1] != s[3]) v &= ~2; if (s[0] != s[3] || s[1] != s[2]) v &= ~4; } if (v == 7) cout << "aaaa"; else if (v == 1) cout << "aabb"; else if (v == 2) cout << "abab"; else if (v == 4) cout << "abba"; else if (v == 0) cout << "NO"; else return 1; return 0; }
4