solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7, siz = 2e5 + 5; vector<int> g[siz], good[110]; int n, m, k, s, a[siz], dist[siz], q[siz]; int ans[siz][110]; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k >> s; for (int i = 1; i <= n; i++) { int x; cin >> x; good[x].push_back(i); } for (int i = 1; i <= m; i++) { int x, y; cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } for (int i = 1; i <= k; i++) { for (int j = 1; j <= n; j++) dist[j] = 1e9; int cnt = 0; for (auto city : good[i]) dist[city] = 0, q[cnt++] = city; for (int j = 0; j < cnt; j++) { int cur = q[j]; for (auto next : g[cur]) { if (dist[next] <= dist[cur]) continue; dist[next] = 1 + dist[cur]; q[cnt++] = next; } } for (int j = 1; j <= n; j++) ans[j][i] = dist[j]; } vector<int> out; for (int i = 1; i <= n; i++) { sort(ans[i] + 1, ans[i] + 1 + k); int sum = 0; for (int j = 1; j <= s; j++) sum += ans[i][j]; out.push_back(sum); } for (auto x : out) cout << x << " "; }
4
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 100, M = 3; int n, q, dex, ans, f[N], good[M], bad[M], fen[M][N]; string s; set<int> st[M]; inline void add(int dex, int type, int val) { for (; dex < N; dex += (dex & -dex)) fen[type][dex] += val; } inline int get(int dex, int type) { return (dex > 0 ? fen[type][dex] + get(dex ^ (dex & -dex), type) : 0); } inline void check(int l1, int r1, int l2, int r2, int type) { int l = max(l1, l2), r = min(r1, r2); if (r >= l) ans += get(r, type) - get(l - 1, type); } inline int solve() { ans = 0; for (int i = 0; i < M; i++) { if (st[i].empty()) continue; else if (st[i].size() == n) { ans = n; break; } if (st[bad[i]].empty()) { ans += st[i].size(); continue; } else if (st[good[i]].empty()) continue; int a, b, A, B; a = (*st[bad[i]].begin()) - 1; auto x = st[good[i]].begin(); b = (st[good[i]].empty() ? n + 1 : max(a + 1, *x)); B = (*st[bad[i]].rbegin()) + 1; x = --st[good[i]].end(); A = (st[good[i]].empty() ? 0 : min(*x, B - 1)); check(0, a, 0, A, i); check(0, a, B, n, i); check(b, n, 0, A, i); check(b, n, B, n, i); } return ans; } inline void add_element(int dex) { add(dex, f[s[dex]], 1); st[f[s[dex]]].insert(dex); } inline void del_element(int dex) { add(dex, f[s[dex]], -1); st[f[s[dex]]].erase(dex); } int main() { ios::sync_with_stdio(false), cin.tie(0); f['R'] = 0, f['P'] = 1, f['S'] = 2; for (int i = 0; i < M; i++) bad[i] = (i + 1) % M, good[i] = (i - 1 + M) % M; cin >> n >> q >> s; s = "#" + s; for (int i = 1; i <= n; i++) add_element(i); cout << solve() << '\n'; while (q--) { cin >> dex; del_element(dex); cin >> s[dex]; add_element(dex); cout << solve() << '\n'; } return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { string a, b; cin >> a >> b; for (int i = 0; i < a.length(); i++) { if (a[i] != b[i]) cout << 1; else cout << 0; } }
0
#include <bits/stdc++.h> using namespace std; long long n; long long v[100050]; long long r[100050]; long long s[100050]; int main() { scanf("%lld", &n); for (long long i = 1; i <= n; i++) { scanf("%lld", &v[i]); } v[0] = -3000; r[0] = 0; s[0] = 0; for (long long i = 1; i <= n; i++) { long long b, c; { long long l = 0, r = i - 1; long long ach = v[i] - 89; while (l < r) { long long mid = (l + r) / 2; if (v[mid] < ach) { l = mid + 1; } else { r = mid; } } b = l - 1; } { long long l = 0, r = i - 1; long long ach = v[i] - 1439; while (l < r) { long long mid = (l + r) / 2; if (v[mid] < ach) { l = mid + 1; } else { r = mid; } } c = l - 1; } long long resp = min(20 + s[i - 1], min(50 + s[b], 120 + s[c])); resp -= s[i - 1]; r[i] = resp; printf("%lld\n", r[i]); s[i] = r[i] + s[i - 1]; } return 0; }
4
#include <bits/stdc++.h> using namespace std; template <class T> T abs(T x) { return x > 0 ? x : (-x); } template <class T> T sqr(T x) { return x * x; } const double eps = 1E-8; double get(double v0, double a, double l) { double D = v0 * v0 + 2. * a * l; D = sqrt(D); double res = D - v0; res /= a; return res; } bool check(double vm, double a, double l, double w) { double left = l; left -= a * sqr(vm / a) * 0.5; double cv = min(vm, w); double tmp = (vm - cv) / a; left -= vm * tmp - a * sqr(tmp) * 0.5; return left >= 0.0; } int main() { double a, v, l, d, w; cin >> a >> v >> l >> d >> w; w = min(w, v); double res = 0.0; double cv = 0.0; { double down = 0.0, up = v; for (int it = 0; it < 1000; it++) { double t = (up + down) * 0.5; if (check(t, a, d, w)) down = t; else up = t; } double vm = (up + down) * 0.5; double left = d; res += vm / a; left -= a * sqr(vm / a) * 0.5; cv = min(vm, w); double tmp = (vm - cv) / a; res += tmp; left -= vm * tmp - a * sqr(tmp) * 0.5; res += left / vm; } { double left = l - d; double t_raz = (v - cv) / a; double t_f = get(cv, a, left); t_raz = min(t_raz, t_f); res += t_raz; left -= cv * t_raz + a * sqr(t_raz) * 0.5; res += left / v; } printf("%.10lf\n", res); return 0; }
6
#include <iostream> #include <cassert> using namespace std; int N, M, K; pair <int, int> v[2005]; int mars1[2005][2005], mars2[2005][2005]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> N >> M >> K; for(int i = 1; i <= M; i++) cin >> v[i].first >> v[i].second; for(int i = 1; i <= M; i++) { int L1 = v[i].first, R1 = v[i].second; for(int p1 = 1; p1 <= N - K + 1; p1++) { int L2 = p1, R2 = p1 + K - 1; int intersection = max(0, min(R1, R2) - max(L1, L2) + 1); if(intersection == 0) continue; int rightMostPoint = L1 + intersection - 2; if(rightMostPoint - K + 1 >= 1) ///> { mars2[p1][1] += intersection; mars2[p1][rightMostPoint - K + 2] -= intersection; } int leftMostPoint = R1 - intersection + 2; if(leftMostPoint + K - 1 <= N) ///> { mars2[p1][leftMostPoint] += intersection; mars2[p1][N - K + 2] -= intersection; } if(intersection == R1 - L1 + 1) ///= { int pseudoLeft = R1 - K + 1; int pseudoRight = L1; pseudoLeft = max(1, pseudoLeft); pseudoRight = min(pseudoRight, N - K + 1); mars1[p1][pseudoLeft] += intersection; mars1[p1][pseudoRight + 1] -= intersection; } else if(intersection == R2 - L2 + 1) ///= { int start = L1; int fin = R1 - K + 1; mars1[p1][start] += intersection; mars1[p1][fin + 1] -= intersection; } else ///= { int pl1 = L1 + intersection - K; int pl2 = R1 - intersection + 1; if(pl1 >= 1) { mars1[p1][pl1] += intersection; mars1[p1][pl1 + 1] -= intersection; } if(pl2 + K - 1 <= N) { mars1[p1][pl2] += intersection; mars1[p1][pl2 + 1] -= intersection; } } } } for(int i = 1; i <= N - K + 1; i++) { for(int j = 1; j <= N - K + 1; j++) mars1[i][j] += mars1[i][j - 1], mars2[i][j] += mars2[i][j - 1]; } int ans = 0; for(int i = 1; i <= N - K + 1; i++) for(int j = 1; j <= N - K + 1; j++) { ans = max(ans, mars2[i][j] + mars2[j][i] + mars1[i][j]); assert(mars1[i][j] == mars1[j][i]); } cout << ans << '\n'; return 0; }
8
#include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T& v) { for (auto& x : v) cin >> x; } template <typename T> T nextIntLn() { string tmp; T val; cin >> val; getline(cin, tmp); return val; } void solve() { string str; cin >> str; map<char, int> q; for (int i = 0; i < str.size(); ++i) { ++q[str[i]]; } int cnt = 0; char find = '\0'; for (int ch = 'a'; ch <= 'z'; ++ch) { if (q[ch] % 2 == 0) continue; for (int j = 'z'; j > ch; --j) { if (q[j] % 2 != 0) { ++q[ch]; --q[j]; break; } } if (q[ch] % 2 != 0) { ++cnt; find = ch; --q[ch]; } } for (int ch = 'a'; ch <= 'z'; ++ch) { if (q[ch]) { cout << string(q[ch] / 2, ch); } } if (cnt) cout << find; for (int ch = 'z'; ch >= 'a'; --ch) { if (q[ch]) { cout << string(q[ch] / 2, ch); } } cout << endl; } int main() { std::ios_base::sync_with_stdio(false); solve(); return 0; }
5
#include <bits/stdc++.h> using namespace std; int co[200006]; int main() { int t, k, i, j, n, m, x, y, q, c = 0; int ma = 0; cin >> n >> m; for (i = 1; i <= n; i++) { cin >> j; co[j]++; if (i == m) { co[j]--; } else ma = max(ma, j); if (i == m && j != 0) { c++; } } int flag = 0; c += co[0]; x = co[0]; j = ma; for (i = 1; i <= ma && j > i; i++) if (co[i] == 0) { flag = 1; if (x) x--; else { c++; co[j]--; while (!co[j]) j--; } } cout << c; return 0; }
5
#include <bits/stdc++.h> using namespace std; long long n, d, A[100001]; int main() { ios::sync_with_stdio(0); cin >> n >> d; for (long long i = 0; i < (long long)n; i++) cin >> A[i]; long long Res = 0; for (int i = 0; i < n - 2; i++) { long long *p = upper_bound(A + i + 2, A + n, d + A[i]); if (p == A + i + 2) continue; p--; long long idx = p - A; long long D = idx - i; Res += (D * (D - 1)) / 2; } cout << Res << endl; }
2
#include <bits/stdc++.h> using namespace std; struct node { node *next[2]; char c; int sum; } * root, *last; char str[5001]; bool dp[5001][5001]; int len, k, sum, lastkey; string res; void insert(int be, int en) { node *p = last, *q; int id; while (be <= en) { id = str[be++] - 'a'; q = p->next[id]; if (!q) { q = new node; q->next[0] = q->next[1] = 0; q->sum = 0; q->c = char('a' + id); p->next[id] = q; } p = q; } p->sum++; last = p; } void dfs(node *p) { if (!p) return; sum += p->sum; if (sum < k) dfs(p->next[0]); if (sum < k) dfs(p->next[1]); if (sum >= k) { if (p->c != '&') res += p->c; } } int main() { while (cin >> str) { len = strlen(str); cin >> k; memset(dp, 0, sizeof(dp)); for (int i = 0; i < len; i++) { for (int j = 0; i + j < len; j++) { if (i > 3) { if (dp[j + 2][i + j - 2] && str[j] == str[i + j]) dp[j][i + j] = 1; } else if (str[j] == str[i + j]) dp[j][i + j] = 1; } } root = new node; root->next[0] = root->next[1] = 0; root->sum = 0; root->c = '&'; for (int i = 0; i < len; i++) { last = root; lastkey = i; for (int j = i; j < len; j++) { if (dp[i][j]) { insert(lastkey, j); lastkey = j + 1; } } } res = ""; sum = 0; dfs(root); reverse(res.begin(), res.end()); cout << res << endl; } return 0; }
7
#include <bits/stdc++.h> using namespace std; const int N = 22; string s[N]; int a[N][N], n, m; pair<int, int> jump[N][N]; inline void minimize(int &a, int b) { a = min(a, b); } int main() { scanf("%d%d\n", &n, &m); for (int i = 0; i < n; i++) { getline(cin, s[i]); } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { scanf("%d", &a[i][j]); } for (int i = 0; i < n; i++) for (int p = 0; p < m; p++) { int total = 0, maxCost = 0, mask = 0; for (int j = 0; j < n; j++) if (s[i][p] == s[j][p]) { mask |= 1 << j; total += a[j][p]; maxCost = max(maxCost, a[j][p]); } jump[i][p] = make_pair(mask, total - maxCost); } vector<int> dp(1 << n, 1e9); dp[0] = 0; for (int mask = 0; mask + 1 < 1 << n; mask++) { int i; for (i = 0; mask & (1 << i); i++) ; for (int p = 0; p < m; p++) { dp[mask | (1 << i)] = min(dp[mask | (1 << i)], dp[mask] + a[i][p]); dp[mask | jump[i][p].first] = min(dp[mask | jump[i][p].first], dp[mask] + jump[i][p].second); } } printf("%d", dp.back()); }
8
#include <bits/stdc++.h> using namespace std; int main() { int n, v1, v2, t1, t2, s, f; cin >> n >> v1 >> v2 >> t1 >> t2; f = 2 * t1 + n * v1; s = 2 * t2 + n * v2; if (f < s) cout << "First"; else if (s < f) cout << "Second"; else cout << "Friendship"; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 200005; template <typename T> inline void read(T &AKNOI) { T x = 0, flag = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') flag = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } AKNOI = flag * x; } int n, xl[MAXN], xr[MAXN], yl[MAXN], yr[MAXN]; int tot, m, sor[MAXN], vis[MAXN], ans; int mn[MAXN << 2], mx[MAXN << 2]; set<int> S[MAXN << 2]; struct Node { int x, l, r, id; Node() {} Node(int _x, int _l, int _r, int _id) : x(_x), l(_l), r(_r), id(_id) {} bool operator<(const Node &rhs) const { return (x == rhs.x) ? (id < rhs.id) : (x < rhs.x); } } p[MAXN]; inline void pushup(int rt, int b, int e) { if (b == e) { mn[rt] = mx[rt] = 0; } else { mn[rt] = min(mn[(rt << 1)], mn[(rt << 1) | 1]); mx[rt] = max(mx[(rt << 1)], mx[(rt << 1) | 1]); } if (S[rt].size()) { int x = *S[rt].rbegin(); if (vis[x]) { mn[rt] = max(mn[rt], x); } else { mx[rt] = max(mx[rt], x); } if (mn[rt] > mx[rt]) { mx[rt] = 0; } } } void Update(int rt, int b, int e, int l, int r, int v) { if (l <= b && e <= r) { if (v > 0) S[rt].insert(v); if (v < 0) S[rt].erase(-v); pushup(rt, b, e); return; } int mid = (b + e) >> 1; if (l <= mid) Update((rt << 1), b, mid, l, r, v); if (r > mid) Update((rt << 1) | 1, mid + 1, e, l, r, v); pushup(rt, b, e); } void init() { read(n); for (int i = 1; i <= n; ++i) { read(xl[i]); read(yl[i]); read(xr[i]); read(yr[i]); sor[++m] = yl[i]; sor[++m] = yr[i]; } sort(sor + 1, sor + m + 1); m = unique(sor + 1, sor + m + 1) - (sor + 1); for (int i = 1; i <= n; ++i) { yl[i] = lower_bound(sor + 1, sor + m + 1, yl[i]) - sor; yr[i] = upper_bound(sor + 1, sor + m + 1, yr[i] - 1) - sor - 1; p[++tot] = Node(xl[i], yl[i], yr[i], i); p[++tot] = Node(xr[i], yl[i], yr[i], -i); } sort(p + 1, p + tot + 1); } void solve() { for (int i = 1, j = 1; i <= tot; i = j) { for (; j <= tot && p[j].x == p[i].x; ++j) { Update(1, 1, m, p[j].l, p[j].r, p[j].id); } while (mx[1]) { int cur = mx[1]; ans += (vis[cur] = 1); Update(1, 1, m, yl[cur], yr[cur], 0); } } printf("%d\n", ans + 1); } int main() { init(); solve(); return 0; }
12
#include <bits/stdc++.h> using namespace std; template <typename input_type> input_type get() { input_type input; cin >> input; return input; } const int alpha = 26; const long long int mod = 1e9 + 7; long long int power(long long int base, int exp) { if (exp == 0) { return 1; } else if (exp % 2) { return (power(base, exp - 1) * base) % mod; } else { long long int tmp = power(base, exp / 2); return (tmp * tmp) % mod; } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int sz = get<int>(), query = get<int>(); long long int last; string str = get<string>(); for (int q = 0; q < query; q++) { int loc = get<int>(); if ((q == 0) or (loc - last >= str.length())) { sz -= str.length(); } else if (str[loc - last] != str[0]) { cout << 0; return 0; } else { sz -= loc - last; } last = loc; } cout << power(alpha, sz); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int n, m; vector<vector<char> > arr; cin >> n >> m; arr.resize(n); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (i % 2 == 0) arr[i].push_back('#'); else arr[i].push_back('.'); } } for (int i = 0; i < n; i++) { int half = i / 2; if (half % 2 == 0) arr[i][m - 1] = '#'; else arr[i][0] = '#'; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) cout << arr[i][j]; cout << "\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long dp[1024][1024]; long long f(int n, int k) { long long an = 0; if (k == 0) return 1; if (dp[n][k] >= 0) return dp[n][k]; int i; for (i = 1; i <= n - 2; i++) { an += f(i, k - 1) * (n - 1 - i); } return dp[n][k] = an % 1000000007; } int main() { int i, j, k, n, m; scanf("%d%d%d", &n, &m, &k); memset(dp, -1, sizeof(dp)); cout << f(n, k) * f(m, k) % 1000000007 << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; long long n, i, j, k, l, low, mid, high, d[200005]; string a, b, c; bool check(long long mid) { c = a; long long flag = 0; for (i = 0; i < mid; i++) c[d[i] - 1] = '0'; for (i = 0; i < a.size(); i++) { if (c[i] == b[flag]) { flag++; } if (flag == b.size()) return true; } return false; } int main() { cin >> a >> b; for (i = 0; i < a.size(); i++) { cin >> d[i]; } c = a; low = 0; high = a.size(); mid = (low + high) / 2; while (high - low > 1) { if (check(mid)) low = mid; else high = mid; mid = (low + high) / 2; } cout << mid; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> data(n); for (int i = 0; i < n; ++i) { cin >> data[i]; } int ans = 100500; for (int i = 1; i < n - 1; ++i) { int hard = 0; for (int j = 1; j < n; ++j) { if (i == j) continue; if (j == i + 1) { hard = max(hard, data[j] - data[i - 1]); continue; } hard = max(hard, data[j] - data[j - 1]); } ans = min(ans, hard); } cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1; const int MX = 1e9 + 10; const long long int INF = 1e18; inline long long int uceil(long long int a, long long int b) { return (a % b ? a / b + 1 : a / b); } template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { for (auto& x : v) os << x << " "; return os << '\n'; } template <typename T> ostream& operator<<(ostream& os, const set<T>& v) { for (auto& x : v) os << x << " "; return os << '\n'; } void solve() { long long int n; cin >> n; long long int m = -1; for (long long int i = 2; i * i <= n; i++) { if (n % i == 0) { m = i; break; } } long long int ans = 1; if (m != -1) ans += ((n - m) / 2); cout << ans << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 3000010; int n, a[N]; int maxr[N]; long long ans, cur; int id, ansid; int tmax, tmin, zero; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); ansid = 0; for (int i = 1; i <= n; i++) { if (a[i] > i) maxr[a[i] - i]++, tmax++; else if (a[i] < i) tmin++; else zero++; ans += abs(i - a[i]); } cur = ans; for (int i = 1; i < n; i++) { if (a[n - i + 1] == n) { cur -= tmax; tmax++; tmin += zero - 1; cur += tmin; zero = maxr[i]; tmax -= maxr[i]; maxr[i] = 0; cur += n - 1; } else if (a[n - i + 1] == 1) { cur -= tmax; tmin--; tmin += zero; cur += tmin; zero = maxr[i] + 1; tmax -= maxr[i]; maxr[i] = 0; cur -= n - 1; } else { cur -= tmax; tmax++; maxr[a[n - i + 1] - 1 + i]++; tmin--; tmin += zero; cur += tmin; zero = maxr[i]; tmax -= maxr[i]; maxr[i] = 0; cur -= n - a[n - i + 1]; cur += a[n - i + 1] - 1; } if (cur < ans) { ans = cur; ansid = i; } } cout << ans << " " << ansid << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; inline int read() { int f = 1, x = 0; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } inline long long readll() { long long f = 1, x = 0; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } const int MAXN = 150004; int n, a[MAXN]; int main() { int T = read(); while (T--) { n = read(); for (int i = 1; i <= n; ++i) a[i] = read(); int mn = 1e9, c = 0; for (int i = n; i >= 1; --i) { if (a[i] > mn) c++; mn = min(mn, a[i]); } printf("%d\n", c); } return 0; }
1
#include <bits/stdc++.h> using namespace std; struct arr { int x, y; } q[2502000]; bool v[52][52], a[52][52]; int n, m, ans, l, r, dx[5], dy[5], d[52][52], ma; char x; void spfa() { while (l <= r) { for (int i = 1; i <= 4; i++) { if (q[l].x + dx[i] < 1 || q[l].x + dx[i] > n || q[l].y + dy[i] < 1 || q[l].y + dy[i] > m) continue; if (d[q[l].x + dx[i]][q[l].y + dy[i]] > d[q[l].x][q[l].y] + (a[q[l].x + dx[i]][q[l].y + dy[i]] != a[q[l].x][q[l].y])) { d[q[l].x + dx[i]][q[l].y + dy[i]] = d[q[l].x][q[l].y] + (a[q[l].x + dx[i]][q[l].y + dy[i]] != a[q[l].x][q[l].y]); if (!v[q[l].x + dx[i]][q[l].y + dy[i]]) { q[++r].x = q[l].x + dx[i]; q[r].y = q[l].y + dy[i]; v[q[r].x][q[r].y] = true; } } } v[q[l].x][q[l].y] = false; l++; } } int max() { int re = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { if (re < d[i][j] + 1 && a[i][j]) re = d[i][j] + 1; if (re < d[i][j] && !a[i][j]) re = d[i][j]; } return re; } int main() { scanf("%d %d\n", &n, &m); dx[1] = 0; dx[2] = -1; dx[3] = 0; dx[4] = 1; dy[1] = -1; dy[2] = 0; dy[3] = 1; dy[4] = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { x = getchar(); if (x == 'B') a[i][j] = 1; ans += a[i][j]; } scanf("%c", &x); } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { memset(d, 10, sizeof d); memset(v, 0, sizeof v); d[i][j] = 0; l = r = 1; q[l].x = i; q[l].y = j; spfa(); ma = max(); if (ma < ans) ans = ma; } printf("%d\n", ans); return 0; }
9
#include <bits/stdc++.h> using namespace std; using i64 = long long; class TaskC { public: void solve(istream& in, ostream& out) { int n; in >> n; vector<pair<i64, int>> ev(2 * n); for (i64 i = 0, l, r; i < n; ++i) { in >> l >> r; ev[2 * i] = {l, 0}; ev[2 * i + 1] = {r + 1, 1}; } vector<i64> res(n + 1, 0); int cnt = 0; sort(begin(ev), end(ev)); for (i64 i = 0, last = 0; i < 2 * n;) { i64 cur = ev[i].first; res[cnt] += max(0LL, cur - last); while (i < 2 * n && ev[i].first == cur && ev[i].second) { --cnt; ++i; } while (i < 2 * n && ev[i].first == cur && !ev[i].second) { ++cnt; ++i; } last = cur; } assert(cnt == 0); for (int i = 1; i <= n; ++i) { out << res[i] << " "; } out << "\n"; } }; int main() { std::ios::sync_with_stdio(false); cin.tie(nullptr); TaskC solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
4
#include<bits/stdc++.h> using namespace std; int main() { /*#ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif*/ int t; cin >> t; while (t--) { int n; cin >> n; vector<int> v; for (int i = 0; i < n; i++) { int num; cin >> num; v.push_back(num); } long long int s = 0; bool flag = true; for (int i = 0; i < n; i++) { if (v[i] >= i) s += (v[i] - i); else if (s >= (i - v[i])) s -= (i - v[i]); else { cout << "NO" << endl; flag = false; break; } } if (flag) cout << "YES" << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int fst[1000001], last[1000001]; int was[1000001]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int a; cin >> a; if (fst[a] == 0) fst[a] = i + 1; last[a] = i + 1; was[a]++; } int ans = 1e9, ansl = 0, ansr = 0; int maxx = *max_element(was, was + 1000001); for (int i = 1; i <= 1000000; i++) { if (last[i] - fst[i] + 1 < ans and was[i] == maxx) { ans = last[i] - fst[i] + 1; ansl = fst[i]; ansr = last[i]; } } cout << ansl << ' ' << ansr << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; char s[4]; long long a, b, c, d; long long func2(long long a, long long b) { if (s[2] == '+') return a + b; return a * b; } long long func3(long long a, long long b, long long c) { long long ans = 1LL << 50; if (s[1] == '+') { ans = min(ans, func2(a + b, c)); ans = min(ans, func2(a + c, b)); ans = min(ans, func2(b + c, a)); } else { ans = min(ans, func2(a * b, c)); ans = min(ans, func2(a * c, b)); ans = min(ans, func2(b * c, a)); } return ans; } long long func4(long long a, long long b, long long c, long long d) { long long ans = 1LL << 50; if (s[0] == '+') { ans = min(ans, func3(a + b, c, d)); ans = min(ans, func3(a + c, b, d)); ans = min(ans, func3(a + d, b, c)); ans = min(ans, func3(b + c, a, d)); ans = min(ans, func3(b + d, a, c)); ans = min(ans, func3(c + d, a, b)); } else { ans = min(ans, func3(a * b, c, d)); ans = min(ans, func3(a * c, b, d)); ans = min(ans, func3(a * d, b, c)); ans = min(ans, func3(b * c, a, d)); ans = min(ans, func3(b * d, a, c)); ans = min(ans, func3(c * d, a, b)); } return ans; } int main(void) { cin >> a >> b >> c >> d; for (int i = 0; i < 3; i++) cin >> s[i]; long long ans = func4(a, b, c, d); cout << ans; return 0; }
4
#include <bits/stdc++.h> int main(void) { int q; scanf("%d", &q); while (q--) { int l, r, d; scanf("%d %d %d", &l, &r, &d); if (d < l) { printf("%d\n", d); continue; } printf("%d\n", ((r / d) + 1) * d); } }
1
#include <bits/stdc++.h> using namespace std; using ll = long long; using ul = unsigned long long; using ld = long double; const int N = 5001, MOD = 1e9 + 7; int a[N], f[N], h[N], tc[N], cc[N]; ll rr; vector<int> lf[N]; int fc(int x) { int c1 = cc[x], c2 = tc[x] - c1; if (c1 > c2) swap(c1, c2); ll p1 = lower_bound((lf[x]).begin(), (lf[x]).end(), c1 + 1) - lf[x].begin(), p2 = lower_bound((lf[x]).begin(), (lf[x]).end(), c2 + 1) - lf[x].begin(); if (p1 > 0 && p2 > 1) { rr = p1 * (p2 - 1); return 2; } if (p2 > 0) { rr = p1 + p2; return 1; } rr = 1; return 0; } ll pw(ll x, ll k) { ll r = 1; while (k) { if (k & 1) r = r * x % MOD; x = x * x % MOD; k >>= 1; } return r; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < (n); i++) { cin >> a[i]; a[i]--; tc[a[i]]++; } for (int i = 0; i < (m); i++) { cin >> f[i] >> h[i]; f[i]--; lf[f[i]].push_back(h[i]); } int ans = 0, ca = 0; for (int i = 0; i < (n); i++) { sort((lf[i]).begin(), (lf[i]).end()); if (!lf[i].empty() && lf[i][0] <= tc[i]) ca++; } for (int i = 0; i < (n); i++) { ans = max(ans, ca); ca -= fc(a[i]); cc[a[i]]++; ca += fc(a[i]); } ans = max(ans, ca); ll res = 0, cr = 1; ca = 0; for (int i = 0; i < (n); i++) { int pos = lower_bound((lf[i]).begin(), (lf[i]).end(), tc[i] + 1) - lf[i].begin(); if (pos) { ca++; cr = cr * pos % MOD; } } for (int i = 0; i < (n); i++) cc[i] = 0; if (ca == ans) res = cr; for (int i = 0; i < (n); i++) { ca -= fc(a[i]); cr = cr * pw(rr, MOD - 2) % MOD; cc[a[i]]++; ca += fc(a[i]); if (ca == ans) { ll p1 = lower_bound((lf[a[i]]).begin(), (lf[a[i]]).end(), cc[a[i]]) - lf[a[i]].begin(), p2 = lower_bound((lf[a[i]]).begin(), (lf[a[i]]).end(), tc[a[i]] - cc[a[i]] + 1) - lf[a[i]].begin(); if (p1 < lf[a[i]].size() && lf[a[i]][p1] == cc[a[i]]) { if (p2 - 1 >= p1) p2--; res = (res + cr * max(p2, 1ll)) % MOD; } } cr = cr * rr % MOD; } cout << ans << ' ' << res; }
8
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, e, i, j, k; int xx[105]; while (~scanf("%d", &a)) { for (i = 0; i < a; i++) { scanf("%d", &xx[i]); } sort(xx, xx + a); int sum = 0; int maxx = 0; for (i = 0; i < a; i++) { sum++; if (i == a - 1) { maxx = max(maxx, sum); break; } if (xx[i] != xx[i + 1]) { maxx = max(maxx, sum); sum = 0; } } int pl = 0; if (a % 2 == 0) { a /= 2; if (maxx > a) { pl = 1; } } else { a /= 2; a++; if (maxx > a) { pl = 1; } } printf(pl == 1 ? "NO\n" : "YES\n"); } return 0; }
1
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma comment(linker, "/stack:200000000") using namespace std; const int mod0 = 1e9 + 7; const long long mod1 = 998244353; const long long mod2 = 1e9 + 9; const long long mod3 = 2147483647; const int sz = 512 * 1024; const long long inf = 2 * 1024 * 1024 * 1023 - 1; const long long INF = inf * inf; const long double eps = 1e-7; const long long K = 1e12; void init() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(16); } void solve() { int n, m; cin >> n >> m; int i1 = 1, i2 = n, j1 = 2, j2 = m - 1; cout << 1 << " " << 1 << '\n'; if (n * m == 1) return; cout << n << " " << m << '\n'; while (i1 <= i2) { if (j2 == 0) { i1++, i2--; j1 = 1, j2 = m; } if (i1 > i2) break; if (i1 == i2 && j1 > j2) break; cout << i1 << " " << j1 << '\n'; if (i1 != i2 || j1 != j2) cout << i2 << " " << j2 << '\n'; j1++, j2--; } } int main() { init(); solve(); return 0; }
5
#include <bits/stdc++.h> using namespace ::std; int n, k; char aa[1000005]; int main() { int i; scanf("%d %d", &n, &k); if (k > n || (k == 1 && n != 1)) printf("-1"); else { if (k == n) { for (i = 1; i <= k; i++) printf("%c", 'a' + i - 1); } else { for (i = 1; i <= n - k + 2; i++) { printf("%c", 'a' + (i - 1) % 2); } for (; i <= n; i++) { printf("%c", 'c' + (i - (n - k + 3))); } } } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { long long int te; cin >> te; long long int cg = 3; long long int ans = 1; while (cg < te) { if (te % cg != 0) { ans = te / cg; ans++; break; } else cg *= 3; } cout << ans << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n % 2 == 0) { cout << n / 2 << endl; for (int i = 0; i < n / 2; i++) cout << "2 "; return 0; } else { cout << (n / 2) << endl; for (int i = 0; i < (n / 2) - 1; i++) cout << "2 "; cout << "3"; return 0; } }
0
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; ++i) scanf("%d", &v[i]); int ans = 0; sort((v).begin(), (v).end()); for (int i = 0, j = 0; i < n; i++) { int l = lower_bound((v).begin(), (v).end(), v[i] - 5) - v.begin(); ans = max(i - l + 1, ans); } printf("%d\n", ans); } int main() { int t; solve(); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n), p(n); for (int i = 0; i < n; i++) { cin >> a[i] >> p[i]; } int min = 179; long long ans = 0; for (int i = 0; i < n; i++) { if (p[i] < min) { min = p[i]; } ans += min * a[i]; } cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; vector<long long int> emptyVector; const long long int MAX = 2e5 + 5; const long long int INF = 1e18; const long long int MOD = 1e9 + 7; const long double pi = 3.141592653589793238462643383279502884197169399375105; long long int binaryExpo(long long int x, long long int n) { if (n == 0) return 1; if (n % 2 == 0) return binaryExpo((x * x) % MOD, n / 2); return (x * binaryExpo((x * x) % MOD, n / 2)) % MOD; } long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long int cost = 0; int a[MAX], b[MAX], c[MAX]; vector<long long int> adj[MAX]; void dfs(int u, int& t1, int& t2, int val, int p = -1) { for (auto it : adj[u]) { if (it != p) { int T1 = 0, T2 = 0; dfs(it, T1, T2, min(val, a[it]), u); t1 += T1; t2 += T2; } } if (b[u] == 0 and c[u] == 1) t1++; else if (b[u] == 1 and c[u] == 0) t2++; int mn = min(t1, t2); t1 -= mn; t2 -= mn; cost += 2ll * mn * val; } void solve(void) { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i] >> b[i] >> c[i]; for (int i = 0; i < n - 1; i++) { int x, y; cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } int t1 = 0, t2 = 0; dfs(1, t1, t2, min(a[1], (int)(1e9 + 1))); if (t1 or t2) { cout << "-1" << "\n"; return; } cout << cost << "\n"; } int main(void) { { solve(); } return 0; }
6
#include<bits/stdc++.h> using namespace std; #define INF INT_MAX typedef long long ll; typedef pair<int,int> pii; const ll mod = 1e9 + 7; int main() { std::ios::sync_with_stdio(false); int t; cin>>t; while(t--){ int n; cin>>n; vector<ll> x,y; for(int i=0;i<n;i++){ ll u,v; cin>>u>>v; x.push_back(u); y.push_back(v); } sort(x.begin(),x.end()); sort(y.begin(),y.end()); ll distX=n%2?1:x[n/2]-x[n/2-1]+1; ll distY=n%2?1:y[n/2]-y[n/2-1]+1; cout<<distX*distY<<endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, p, l, r; cin >> n >> p >> l >> r; if (l > 1 && r < n) { if (p < l) cout << r - p + 2 << endl; else if (p > r) cout << p - l + 2 << endl; else cout << min(r - p, p - l) + r - l + 2 << endl; } else if (l > 1 && r == n) cout << abs(p - l) + 1 << endl; else if (r < n && l == 1) cout << abs(r - p) + 1 << endl; else cout << 0 << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int Maxn = 1e6 + 7; const int Inf = 1e8 + 7; const int tmp = 1e3; vector<pair<int, int> > V[Maxn]; vector<int> ans; int dfs(int u, int par = 0, int bad = 0) { bool t = 0; for (int i = 0; i < V[u].size(); i++) { int v = V[u][i].first, w = V[u][i].second; if (v != par) t += dfs(v, u, !w); } if (!t && bad) ans.push_back(u); return t + bad; } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); int n; cin >> n; for (int i = 1; i < n; i++) { int u, v, t; cin >> u >> v >> t; if (t == 2) t = 0; V[u].push_back(make_pair(v, t)); V[v].push_back(make_pair(u, t)); } dfs(1); cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) cout << ans[i] << " "; }
4
#include <bits/stdc++.h> using namespace std; inline bool cmp(pair<long long, long long> a, pair<long long, long long> b) { if (a.first == b.first) return a.second < b.second; return a.first > b.first; } long long sumofdigit(long long n) { long long sum = 0; while (n) { sum = sum + n % 10; n = n / 10; } return sum; } template <class T> inline T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> inline T lcm(T a, T b) { return a * b / gcd<T>(a, b); } template <class T> inline T power(T b, T p) { long long ans = 1; while (p--) ans *= b; return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long a, b, c; cin >> a >> b >> c; vector<long long> v; for (long long i = 1; i <= 81; i++) { long long x = b * pow(i, a) + c; if (sumofdigit(x) == i && x <= 1000000000 && x >= 0) v.push_back(x); } cout << v.size() << endl; for (long long i = 0; i < v.size(); i++) cout << v[i] << " "; }
3
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using PII = pair<int, int>; using PLL = pair<ll, ll>; template <typename T1, typename T2> ostream& operator<<(ostream& s, const pair<T1, T2>& p) { return s << "(" << p.first << ", " << p.second << ")"; } template <typename T> ostream& operator<<(ostream& s, const vector<T>& v) { s << "["; for (int i = 0; i < v.size(); i++) s << (i == 0 ? "" : ", ") << v[i]; s << "]"; return s; } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { ll a, b; scanf("%lld %lld", &a, &b); ll ok = 0, ng = 1500000LL; while (ng - ok > 1) { ll mid = (ng + ok) / 2; if (mid * mid * mid <= a * b) { ok = mid; } else { ng = mid; } } ll c = ok; if (c * c * c == a * b && a % c == 0 && b % c == 0) { puts("Yes"); } else { puts("No"); } } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, q, t, k, d, sum = 0, i, ppp; int a[105] = {0}; int b[105]; cin >> n >> q; while (q--) { cin >> t >> k >> d; ppp = 0; for (i = 0; i < n; i++) { if (a[i] <= t) { b[ppp++] = i; } } if (ppp >= k) { sum = 0; for (i = 0; i < k; i++) { sum = sum + b[i]; a[b[i]] = t + d; } sum = sum + k; cout << sum << endl; } else { cout << -1 << endl; } } }
2
#include <bits/stdc++.h> using namespace std; bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int tests; cin >> tests; while (tests-- > 0) { int h, m; cin >> h >> m; cout << 1440 - 60 * h - m << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int dx[] = {1, 0, -1, 0, 1, 1, -1, -1}; int dy[] = {0, 1, 0, -1, 1, -1, 1, -1}; pair<pair<int, int>, pair<int, int> > intersect(pair<int, int> f1, pair<int, int> f2, pair<int, int> s1, pair<int, int> s2) { int x5, y5, x6, y6; x5 = max(min(f1.first, f2.first), min(s1.first, s2.first)); x6 = min(max(f1.first, f2.first), max(s1.first, s2.first)); y5 = max(min(f1.second, f2.second), min(s1.second, s2.second)); y6 = min(max(f1.second, f2.second), max(s1.second, s2.second)); if (x5 >= x6 || y5 >= y6) { return {{0, 0}, {0, 0}}; } return {{x5, y5}, {x6, y6}}; } template <class T, class V> int min(T a, V b) { return min((int)a, (int)b); } template <class T, class V> int max(T a, V b) { return max((int)a, (int)b); } bool isPrime(long long n, long long p) { if (n < 2) return 0; for (long long i = 2; i * i <= n; i++) { if (i > p) break; if (n % i == 0) return 0; } return 1; } void faster() { if (0) { freopen( "taskA" ".in", "r", stdin); freopen( "taskA" ".out", "w", stdout); } if (1) { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } } const long long INF = 1e18; const int inf = 1e9; const int MAXN = (int)3e5 + 123; const int MOD = (int)1e9 + 7; const long double PI = acos(-1); const long double EPS = 3E-16; namespace MATH_CAL { const int mod = MOD; inline long long add(long long a, long long b) { return a + b >= mod ? a + b - mod : a + b; } inline long long sub(long long a, long long b) { return a - b < 0 ? a - b + mod : a - b; } inline long long mul(long long a, long long b) { return (long long)a * b % mod; } inline void Add(long long &a, long long b) { (a += b) >= mod ? a -= mod : 0; } inline void Sub(long long &a, long long b) { (a -= b) < 0 ? a += mod : 0; } inline long long qpow(long long first, long long n) { long long r = 1; for (; n; n /= 2, first = mul(first, first)) if (n & 1) r = mul(r, first); return r; } inline long long mod_inv(long long first) { return qpow(first, mod - 2); } inline long long gcd(long long a, long long b) { while (b) { a %= b; swap(a, b); } return a; } } // namespace MATH_CAL using namespace MATH_CAL; int mn[MAXN]; int bal[MAXN]; int id; bool bad(string s) { int cnt = 0; mn[id] = MAXN; for (auto i : s) { if (i == '(') { cnt++; } else { cnt--; } mn[id] = min(mn[id], cnt); } bal[id] = cnt; return mn[id] != cnt; } void solve() { int n; cin >> n; long long ans = 0; map<int, int> cnt; for (int i = 0; i < n; i++) { string s; cin >> s; int cur = 0; id = i; if (bad(s)) continue; for (auto i : s) { if (i == '(') { cur += 1; } else { cur -= 1; } } cnt[cur]++; } for (int i = 0; i < n; i++) { if (mn[i] < 0) continue; ans += cnt[-bal[i]]; } cout << ans << endl; } int main() { faster(); int t = 1; while (t--) { solve(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int n; struct POINT { int x, y, num; POINT(int a, int b, int c) : x(a), y(b), num(c){}; }; vector<POINT> sector[1001][1001]; vector<int> perm; bool cmp(const POINT &lhs, const POINT &rhs) { if (lhs.x + lhs.y != rhs.x + rhs.y) return lhs.x + lhs.y < rhs.x + rhs.y; return lhs.num < rhs.num; } int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; POINT p(x, y, i + 1); sector[x / 1000][y / 1000].push_back(p); } for (int i = 0; i <= 1000; i++) { if (i % 2 == 0) { for (int j = 0; j <= 1000; j++) { sort(sector[i][j].begin(), sector[i][j].end(), cmp); for (auto u : sector[i][j]) perm.push_back(u.num); } } else { for (int j = 1000; j >= 0; j--) { sort(sector[i][j].begin(), sector[i][j].end(), cmp); for (auto u : sector[i][j]) perm.push_back(u.num); } } } for (auto u : perm) cout << u << ' '; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; while (q--) { int n, clock = 0, anti = 0; cin >> n; int arr[n]; for (int i = 0; i < n; ++i) { cin >> arr[i]; if (arr[i] == 1) clock = i; if (arr[i] == n) anti = i; } int c = 0; if (clock == n - 1) { int p = 2; for (int i = 0; i < n - 1; ++i) { if (arr[i] != p) { c = 1; break; } p++; } if (c == 0) cout << "YES" << "\n"; } else if (clock == 0) { int p = 2; for (int i = 1; i < n; ++i) { if (arr[i] != p) { c = 1; break; } p++; } if (c == 0) cout << "YES" << "\n"; } else { int a = n - 1 - clock; int p = 2; for (int i = clock + 1; i < n; ++i) { if (arr[i] != p) { c = 1; break; } p++; } if (c == 0) { for (int i = 0; i < clock; ++i) { if (arr[i] != p) { c = 1; break; } p++; } } if (c == 0) cout << "YES" << "\n"; } if (c == 1) { c = 0; if (anti == n - 1) { int p = n - 1; for (int i = 0; i < n - 1; ++i) { if (arr[i] != p) { c = 1; break; } p--; } if (c == 0) cout << "YES" << "\n"; } else if (anti == 0) { int p = n - 1; for (int i = 1; i < n; ++i) { if (arr[i] != p) { c = 1; break; } p--; } if (c == 0) cout << "YES" << "\n"; } else { int a = n - 1 - anti; int p = n - 1; for (int i = anti + 1; i < n; ++i) { if (arr[i] != p) { c = 1; break; } p--; } if (c == 0) { for (int i = 0; i < anti; ++i) { if (arr[i] != p) { c = 1; break; } p--; } } if (c == 0) cout << "YES" << "\n"; } } if (c == 1) { cout << "NO" << "\n"; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int a, b, t; int main() { cin >> t; for (int i = 0; i < t; i++) { cin >> a >> b; if (a % b == 0) cout << 0 << '\n'; else cout << b - a % b << '\n'; } }
3
#include <bits/stdc++.h> using namespace std; double t[200100], sum[200100]; double dp[55][200100], F[200100]; struct node { double add, mul; } que[200100]; double Func(node a, double x) { return a.add + a.mul * x; } double getx(node a, node b) { return (a.add - b.add) / (b.mul - a.mul); } int main() { int n, k; scanf("%d %d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%lf", t + i); sum[i] = sum[i - 1] + t[i]; F[i] = F[i - 1] + sum[i] / t[i]; } for (int i = 1; i <= k; i++) { int l = 0, r = 0; double asum = 0; que[0] = (node){dp[i - 1][i - 1] - F[i - 1], -sum[i - 1]}; for (int j = 1; j <= n; j++) { if (j < i) continue; asum += 1 / t[j]; while (l < r && Func(que[l], asum) >= Func(que[l + 1], asum)) l++; dp[i][j] = Func(que[l], asum) + F[j]; if (i == 1) continue; node x = (node){dp[i - 1][j] - F[j] + sum[j] * asum, -sum[j]}; while (l < r && getx(que[r - 1], x) <= getx(que[r - 1], que[r])) r--; que[++r] = x; } } printf("%.10f\n", dp[k][n]); return 0; }
8
#include <bits/stdc++.h> using namespace std; const long long losn = 1e6 + 5; const long long maxn = 1e5 + 5; const long long minn = 1e3 + 5; const long long tiny = 1e2 + 5; const long long inf = 1e9; const long long mod = 1e9 + 7; const long long hmod = 4698571; long long n; char c[maxn * 2]; map<pair<long long, long long>, long long> mp; int main() { long long t; cin >> t; while (t--) { cin >> n; long long ansl = 1, ansr = n; scanf(" %s", c + 1); long long anslen = inf; mp.clear(); long long x = 0, y = 0; pair<long long, long long> ps(x, y); mp[ps] = 0; for (long long i = 1; i <= n; i++) { if (c[i] == 'L') { x--; } else if (c[i] == 'R') { x++; } else if (c[i] == 'D') { y--; } else if (c[i] == 'U') { y++; } pair<long long, long long> p(x, y); if (mp.count(p)) { long long len = i - mp[p]; if (len < anslen) { anslen = len; ansl = mp[p] + 1; ansr = i; } } mp[p] = i; } if (anslen != inf) { cout << ansl << " " << ansr << endl; } else { cout << -1 << endl; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; inline int solve(int a, int b, int c, int ac, int bc, int abc) { int res = abc; { res += bc; a = max(a - bc, 0); } { res += ac; int take = min(b, ac); b -= take; ac -= take; a = max(a - ac, 0); } res += max(c, max((b + c + 1) / 2, (a + b + c + 2) / 3)); return res; } int main() { int n, A, B, C; scanf("%d", &n); scanf("%d %d %d", &A, &B, &C); if (A > B) swap(A, B); if (A > C) swap(A, C); if (B > C) swap(B, C); int x[42]; x[0] = A; x[1] = B; x[2] = C; x[3] = A + B; x[4] = A + C; x[5] = B + C; x[6] = A + B + C; sort(x, x + 7); vector<int> z(7, 0); for (int i = 0; i < n; i++) { int y; scanf("%d", &y); if (y > x[6]) { printf("%d\n", -1); return 0; } ++z[lower_bound(x, x + 7, y) - x]; } int ans = n; for (int cnt = 0; cnt <= n; cnt++) { if (C < A + B) { ans = min(ans, cnt + solve(z[0], z[1], z[2], z[3] + z[4], z[5], z[6])); } else { ans = min(ans, cnt + solve(z[0], z[1], z[2] + z[3], z[4], z[5], z[6])); } for (int who = 2; who <= 3; who++) { for (int j = who; j >= 0; j--) { if (z[j] > 0) { z[j]--; break; } } } } printf("%d\n", ans); return 0; }
8
#include <bits/stdc++.h> using namespace std; const long long int M = 1000000007; long long int arr[1000006]; char str[1000006]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t, n, k, m, ans, x, y; t = 1; cin >> t; while (t--) { cin >> n >> k; long long int l1, l2, r1, r2, l_inter, r_inter; cin >> l1 >> r1; cin >> l2 >> r2; l_inter = max(l1, l2); r_inter = min(r1, r2); long long int intersection = r_inter - l_inter; ans = 0; if (intersection > 0) k -= intersection * n; if (k <= 0) cout << 0 << '\n'; else if (intersection >= 0) { long long int max_possible = max(r1, r2) - min(l1, l2); if (k <= n * (max_possible - intersection)) cout << k << '\n'; else { ans = n * (max_possible - intersection); k -= ans; cout << ans + 2 * k << '\n'; } } else { intersection *= -1; long long int max_possible = max(r1, r2) - min(l1, l2); if (max_possible >= k) cout << intersection + k << '\n'; else if (max_possible <= intersection) cout << intersection - max_possible + k + k << '\n'; else if (n * max_possible <= k) cout << n * intersection + 2 * k - n * max_possible << '\n'; else { ans = 0; long long int total = 0; for (int i = 0; i < n; i++) { ans += intersection + max_possible; total += max_possible; if (total >= k) break; } if (total > k) { ans -= intersection + max_possible; total -= max_possible; } if (k - total <= intersection) ans += (k - total) * 2; else ans += (intersection + k - total); cout << ans << '\n'; } } } return 0; }
6
#include <bits/stdc++.h> using namespace std; constexpr int MAX = 1111111; int m[MAX]; int main() { int n, k, r = 0; scanf("%d\n", &n); for (int i = 1; i <= n; i++) { scanf("%d", &k); m[k]++; } for (int i = 0; i < MAX; i++) if (m[i]) { m[i + 1] += m[i] >> 1; m[i] &= 1; if (m[i]) r++; } printf("%d\n", r); return 0; }
3
#include <bits/stdc++.h> using namespace std; inline long long read() { long long sum = 0, nega = 1; char ch = getchar(); while (ch > '9' || ch < '0') { if (ch == '-') nega = -1; ch = getchar(); } while (ch <= '9' && ch >= '0') sum = sum * 10 + ch - '0', ch = getchar(); return sum * nega; } const long long mod = 1e9 + 7; long long n, k, res, k1, k2, ans; char a[1009], b[1009]; inline long long Pow(long long x, long long y) { long long res = 1, base = x; while (y) { if (y & 1) res = res * base % mod; base = base * base % mod; y >>= 1; } return res; } signed main() { scanf("%s", a + 1); scanf("%s", b + 1); n = strlen(a + 1); k = read(); if (k % 2 == 1) k2 = (Pow(n - 1, k) + 1) % mod * Pow(n, mod - 2) % mod, k1 = k2 - 1; else k2 = (Pow(n - 1, k) - 1 + mod) % mod * Pow(n, mod - 2) % mod, k1 = k2 + 1; for (long long i = 1; i <= n; i++) { long long flag = 0; for (long long j = 1; j <= n; j++) { if (a[j] != b[(i + j - 2 + n) % n + 1]) flag = 1; } if (flag == 0 && i == 1) ans = (ans + k1) % mod; else if (flag == 0) ans = (ans + k2) % mod; } cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int x[200005]; int front[200005]; int main() { int t; scanf("%d", &t); while (t--) { int n, k; scanf("%d%d", &n, &k); map<int, int> count; int index = 0; for (int i = 1; i <= n; i++) { int temp; scanf("%d", &temp); count[temp]++; if (count[temp] == 1) x[index++] = temp; } for (int i = 1; i <= n; i++) { int temp; scanf("%d", &temp); } sort(x, x + index); x[index] = 2000000000; int left = 0, right = 0; int sum = 0; vector<pair<int, int>> candidate; while (left < index) { while (x[right] - x[left] <= k) { sum += count[x[right]]; right++; } candidate.push_back(make_pair(left, sum)); sum -= count[x[left]]; left++; } left = 0; for (int i = 1; i < candidate.size(); i++) { front[i] = front[i - 1]; while (x[candidate[left].first] + k < x[candidate[i].first]) { front[i] = max(front[i], candidate[left].second); left++; } } int answer = 0; for (int i = 0; i < candidate.size(); i++) { answer = max(answer, front[i] + candidate[i].second); } printf("%d\n", answer); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int ans[500010] = {}; int n; int a, b, c, d; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d%d%d%d", &a, &b, &c, &d); a = abs(a); b = abs(b); if (a % 2 == 0 && b % 2 == 0) { ans[i] = 1; } if (a % 2 == 0 && b % 2 == 1) { ans[i] = 2; } if (a % 2 == 1 && b % 2 == 0) { ans[i] = 3; } if (a % 2 == 1 && b % 2 == 1) { ans[i] = 4; } } cout << "YES" << endl; for (int i = 1; i <= n; i++) { printf("%d\n", ans[i]); } return 0; }
6
#include <bits/stdc++.h> using namespace std; int n, f, t, k, v = -INFINITY; int main() { scanf("%d%d", &n, &k); while (n--) { scanf("%d %d", &f, &t); v = max(v, f - max(0, t - k)); } printf("%d", v); return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, a[100007]; char op[100007]; int main() { int i, s = 0; cin >> n; for (i = 0; i < n; ++i) { cin >> a[i]; } for (i = n - 1; i >= 0; --i) { if (s <= 0) { s += a[i]; op[i] = '+'; } else { s -= a[i]; op[i] = '-'; } } if (s < 0) { for (i = 0; i < n; ++i) { op[i] = '+' + '-' - op[i]; } } cout << op << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int q; long long n; int a, b; long long ans; int main(void) { cin >> q; for (int i = 1; i <= q; i++) { cin >> n >> a >> b; if (a * 2 <= b) { ans = a * n; } else { ans = (n / 2) * b + (n % 2) * a; } cout << ans << endl; } }
0
#include <bits/stdc++.h> using namespace std; inline long long read() { long long f = 1, ans = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { ans = ans * 10 + c - '0'; c = getchar(); } return f * ans; } const long long MAXN = 10001; const long long Maxn = 5001; struct Node { long long x1, y1, x2, y2, id; } row[MAXN], col[MAXN]; bool cmp(Node r1, Node r2) { return r1.y2 < r2.y2; } bool cmp1(Node r1, Node r2) { return r1.x2 < r2.x2; } long long cr, cl, N1, N2, tmp0[MAXN], tmp1[MAXN], N, ans; inline long long H1(long long w) { return lower_bound(tmp0 + 1, tmp0 + N1 + 1, w) - tmp0; } inline long long H2(long long w) { return lower_bound(tmp1 + 1, tmp1 + N2 + 1, w) - tmp1; } struct BIT { long long Ans[MAXN]; inline void clear() { memset(Ans, 0, sizeof(Ans)); return; } inline long long lowbit(long long x) { return x & -x; } inline void Modify(long long x, long long w) { for (; x <= N; x += lowbit(x)) Ans[x] += w; return; } inline long long Que(long long x) { long long res = 0; for (; x; x -= lowbit(x)) res += Ans[x]; return res; } inline long long Query(long long l, long long r) { return Que(r) - Que(l - 1); } } bit; long long Que[MAXN]; namespace Q1 { inline void Main() { N = N1; for (register long long i = 1; i <= cr; i++) { bit.clear(); Que[0] = 0; for (long long j = 1; j <= cl; j++) { if (row[i].y1 >= col[j].y1 && col[j].y2 >= row[i].y1 && row[i].x1 <= col[j].x1 && col[j].x1 <= row[i].x2) bit.Modify(col[j].x1, 1), Que[++Que[0]] = j; } long long top = 1; for (register long long j = i + 1; j <= cr; j++) { while (top <= Que[0] && col[Que[top]].y2 < row[j].y1) bit.Modify(col[Que[top]].x1, -1), top++; long long L = max(row[i].x1, row[j].x1), R = min(row[i].x2, row[j].x2); if (L > R) continue; long long res = bit.Query(L, R); res = (res * (res - 1)) / 2; ans += res; } } return; } } // namespace Q1 long long n, q; signed main() { n = read(); for (register long long i = 1; i <= n; ++i) { long long x1 = read(), y1 = read(), x2 = read(), y2 = read(); if (x1 > x2) swap(x1, x2); if (y1 > y2) swap(y1, y2); if (y1 == y2) row[++cr].x1 = x1, row[cr].y1 = y1, row[cr].x2 = x2, row[cr].y2 = y2, row[cr].id = cr; else col[++cl].x1 = x1, col[cl].y1 = y1, col[cl].x2 = x2, col[cl].y2 = y2, col[cl].id = cl; tmp0[++tmp0[0]] = x1, tmp0[++tmp0[0]] = x2; tmp1[++tmp1[0]] = y1, tmp1[++tmp1[0]] = y2; } sort(tmp0 + 1, tmp0 + tmp0[0] + 1); sort(tmp1 + 1, tmp1 + tmp1[0] + 1); N1 = unique(tmp0 + 1, tmp0 + tmp0[0] + 1) - tmp0 - 1; N2 = unique(tmp1 + 1, tmp1 + tmp1[0] + 1) - tmp1 - 1; for (register long long i = 1; i <= cr; ++i) row[i].x1 = H1(row[i].x1), row[i].x2 = H1(row[i].x2), row[i].y1 = H2(row[i].y1), row[i].y2 = H2(row[i].y2); for (register long long i = 1; i <= cl; ++i) col[i].x1 = H1(col[i].x1), col[i].x2 = H1(col[i].x2), col[i].y1 = H2(col[i].y1), col[i].y2 = H2(col[i].y2); sort(row + 1, row + cr + 1, cmp); sort(col + 1, col + cl + 1, cmp); Q1::Main(); printf("%lld\n", ans); return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); long long int n, x, y, a[6], i, q; cin >> x >> y >> n; a[0] = x; a[1] = y; for (i = 2; i < 6; i++) { a[i] = a[i - 1] - a[i - 2]; } q = a[(n - 1) % 6]; if (q >= 0) { cout << q % 1000000007; } else { cout << (1000000007 + q % 1000000007) % 1000000007; } return 0; }
2
#include <bits/stdc++.h> using namespace std; string input; int n; set<string> res; set<pair<int, pair<int, pair<int, pair<int, int> > > > > ac; void solve(int k, int G, int H, int R, int S) { pair<int, pair<int, pair<int, pair<int, int> > > > a; a.first = k; a.second.first = G; a.second.second.first = H; a.second.second.second.first = R; a.second.second.second.second = S; if (ac.find(a) != ac.end()) return; ac.insert(a); int m = min(G, min(H, min(R, S))); if (k == n) { if (m == G) { res.insert("Gryffindor"); } if (m == H) { res.insert("Hufflepuff"); } if (m == R) { res.insert("Ravenclaw"); } if (m == S) res.insert("Slytherin"); return; } if (input[k] == '?') { if (m == G) solve(k + 1, G + 1, H, R, S); if (m == H) solve(k + 1, G, H + 1, R, S); if (m == R) solve(k + 1, G, H, R + 1, S); if (m == S) solve(k + 1, G, H, R, S + 1); } if (input[k] == 'G') { solve(k + 1, G + 1, H, R, S); } if (input[k] == 'H') { solve(k + 1, G, H + 1, R, S); } if (input[k] == 'R') { solve(k + 1, G, H, R + 1, S); } if (input[k] == 'S') { solve(k + 1, G, H, R, S + 1); } } int main() { cin >> n >> input; solve(0, 0, 0, 0, 0); set<string>::iterator it = res.begin(); while (it != res.end()) { cout << *it << endl; it++; } return 0; }
7
#include <bits/stdc++.h> long long dp[2007]; struct node { int t, d, p, id; bool operator<(const node &tmp) const { return d == tmp.d ? t < tmp.t : d < tmp.d; } } p[107]; std::vector<int> item[2007]; int main() { int n, cnt = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d%d%d", &p[cnt].t, &p[cnt].d, &p[cnt].p); if (p[cnt].d > p[cnt].t) p[cnt++].id = i; } std::sort(p, p + cnt); memset(dp, 0, sizeof(dp)); for (int i = 0; i < cnt; i++) for (int j = p[i].d; j-- > p[i].t;) { if (dp[j] < dp[j - p[i].t] + p[i].p) { dp[j] = dp[j - p[i].t] + p[i].p; item[j] = item[j - p[i].t]; item[j].push_back(p[i].id); } } int ans = 0, id; for (int i = 0; i <= 2000; i++) { if (ans < dp[i]) { ans = dp[i]; id = i; } } int len = item[id].size(); printf("%d\n%d\n", ans, len); for (int i : item[id]) printf("%d%c", i, --len > 0 ? ' ' : '\n'); return 0; }
6
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const double PI = 3.141592653589; const long long N = 1e6 + 7; bool prime[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, t, i, j, l, p, w, r, x, y; int m, k, d; string s; cin >> n >> s; long long a[n]; d = 0, p = 0; x = 0; x += 1; for (i = 0; i < n - 1; i++) { if (s[i] != s[i + 1]) x++; } cout << min(n, x + 2); }
4
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool cmax(T &x, T y) { return x < y ? x = y, 1 : 0; } template <typename T> inline bool cmin(T &x, T y) { return y < x ? x = y, 1 : 0; } int N, M; string s, a[105], ss; multiset<string> S; int c[105][26], sum[26]; int main() { cin >> N; if (N == 1) { cout << "? 1 1" << endl; cin >> s; cout << "! " << s << endl; return 0; } M = N >> 1; if (M == 1) { cout << "? 1 1" << endl; cin >> ss; } else { cout << "? 1 " << M << endl; for (int i(1), I(M * (M + 1) / 2); i <= I; ++i) { cin >> s; sort(s.begin(), s.end()); S.insert(s); } cout << "? 2 " << M << endl; for (int i(1), I(M * (M - 1) / 2); i <= I; ++i) { cin >> s; sort(s.begin(), s.end()); S.erase(S.lower_bound(s)); } for (int i(1), I(M); i <= I; ++i) a[S.begin()->size()] = *S.begin(), S.erase(S.begin()); for (int i(1), I(M); i <= I; ++i) for (int j(0), I(i - 1); j <= I; ++j) if (i == 1 || a[i][j] != a[i - 1][j]) { ss += a[i][j]; break; } } cout << "? 1 " << N << endl; for (int i(1), I(N * (N + 1) / 2); i <= I; ++i) { cin >> s; for (char j : s) ++c[s.size()][j - 'a']; } cout << "! " + ss; if (N & 1) for (int i(0), I(25); i <= I; ++i) if (c[M + 1][i] - c[M][i] > 0) { putchar(i + 'a'); break; } for (int i(M), I(1); i >= I; --i) for (int j(0), I(25); j <= I; ++j) if (c[i][j] - c[i - 1][j] + c[i][j] - c[i + 1][j] - (ss[i - 1] == j + 'a') > 0) { putchar(j + 'a'); break; } cout << endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; ifstream in("in.txt"); ofstream out("out.txt"); vector<int> lst1, lst2, lst3; int main(void) { ios::sync_with_stdio(false); int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { string str; int x; cin >> str >> x; if (str == "ATK") lst1.push_back(x); else lst2.push_back(x + 1); } lst3.resize(m); for (int &x : lst3) cin >> x; int ans = 0; sort(lst1.begin(), lst1.end()); sort(lst2.begin(), lst2.end()); sort(lst3.begin(), lst3.end()); for (int i = 1; i <= min((int)lst1.size(), m); i++) { bool ok = true; for (int j = 0; j < i && ok == true; j++) { if (lst1[j] > lst3[m - (i - j)]) ok = false; } if (ok == true) { int cst = 0; for (int j = 0; j < i; j++) cst += lst3[m - j - 1] - lst1[j]; ans = max(ans, cst); } } bool ok = true; for (int x : lst2) { if (lst3.size() == 0 || lst3.back() < x) ok = false; else lst3.erase(lower_bound(lst3.begin(), lst3.end(), x)); } ok &= (lst1.size() <= lst3.size()); for (int i = 0; i < lst1.size() && ok == true; i++) { if (lst1[i] > lst3[lst3.size() - (lst1.size() - i)]) ok = false; } if (ok == true) ans = max(ans, accumulate(lst3.begin(), lst3.end(), 0, plus<int>()) - accumulate(lst1.begin(), lst1.end(), 0, plus<int>())); cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, m, x, y, z, d, i, low, high; long long ans; int main() { cin >> n >> x >> m >> y; ans = n + m + 1; d = abs(x - y); for (i = 1; i < n + 1; i++) { z = d - i; if (z >= 0 && z < m) ans++; low = max(z + 1, -z); high = min(m, d + i - 1); ans += 2 * max(0, high - low); } cout << ans << endl; }
7
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; const long long inf = 1e18; long long a[N], ans[N]; struct node { int l, r; long long lazy; long long v; }; node e[N * 5]; void build(int root, int l, int r) { e[root].l = l; e[root].r = r; e[root].lazy = e[root].v = 0; if (l == r) { e[root].v = a[l]; return; } build(root * 2, l, (l + r) / 2); build(root * 2 + 1, (l + r) / 2 + 1, r); e[root].v = min(e[root * 2].v, e[root * 2 + 1].v); } void updown(int root) { if (e[root].lazy) { e[root * 2].lazy += e[root].lazy; e[root * 2 + 1].lazy += e[root].lazy; e[root * 2].v -= e[root].lazy; e[root * 2 + 1].v -= e[root].lazy; e[root].lazy = 0; } } int que(int root, int x) { if (e[root].l == e[root].r) return e[root].l; updown(root); if (e[root * 2 + 1].v == x) return que(root * 2 + 1, x); else return que(root * 2, x); } void update1(int root, int x) { if (e[root].l == e[root].r) { e[root].v = inf; return; } int mid = (e[root].l + e[root].r) / 2; if (x <= mid) update1(root * 2, x); else update1(root * 2 + 1, x); e[root].v = min(e[root * 2].v, e[root * 2 + 1].v); } void update2(int root, int l, int r, int x) { if (e[root].l == l && e[root].r == r) { e[root].v -= x; e[root].lazy += x; return; } updown(root); int mid = (e[root].l + e[root].r) / 2; if (l > mid) update2(root * 2 + 1, l, r, x); else { if (r <= mid) update2(root * 2, l, r, x); else { update2(root * 2, l, mid, x); update2(root * 2 + 1, mid + 1, r, x); } } e[root].v = min(e[root * 2].v, e[root * 2 + 1].v); } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); build(1, 1, n); for (int i = 1; i <= n; i++) { int k = que(1, 0); ans[k] = i; update1(1, k); if (k != n) update2(1, k + 1, n, i); } for (int i = 1; i <= n; i++) printf("%d ", ans[i]); printf("\n"); return 0; }
5
#include <bits/stdc++.h> using namespace std; int dx[] = {-1, 1, 0, 0, 0, 0, 0, 0}; int dy[] = {0, 0, 1, -1, 0, 0, 0, 0}; const int inf = 1000000000; const double eps = 0.000000009; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, x, y, z, p; cin >> n >> m >> x >> y >> z >> p; pair<int, int> c[p]; for (int i = 0; i < p; i++) cin >> c[i].first >> c[i].second; x = x % 4; y = y % 2; z = z % 4; for (int i = 0; i < p; i++) { int nn = n; int mm = m; for (int j = 0; j < x; j++) { int t1 = c[i].first; int t2 = c[i].second; c[i].first = t2; c[i].second = n + 1 - t1; swap(n, m); } n = nn; m = mm; } if (x % 2) swap(n, m); for (int i = 0; i < p; i++) { for (int j = 0; j < y; j++) { int t1 = c[i].first; int t2 = c[i].second; c[i].first = t1; c[i].second = m + 1 - t2; } } for (int i = 0; i < p; i++) { int nn = n; int mm = m; for (int j = 0; j < z; j++) { int t1 = c[i].first; int t2 = c[i].second; c[i].first = m + 1 - t2; c[i].second = t1; swap(n, m); } n = nn; m = mm; } for (int i = 0; i < p; i++) { cout << c[i].first << " " << c[i].second << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e3 + 10; int dp[maxn][maxn]; string s, t; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> s >> t; int n = s.size(), m = t.size(); for (int i = 0; i <= n; i++) { for (int j = 0; j <= m; j++) { if (!i) { dp[i][j] = j; continue; } if (!j) { dp[i][j] = i; continue; } if (s[i - 1] != t[j - 1]) dp[i][j] = 1 + min({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]}); else dp[i][j] = dp[i - 1][j - 1]; } } cout << dp[n][m] << endl; int i = n, j = m, ans = dp[n][m]; while (i or j) { if (i and j and s[i - 1] == t[j - 1]) { i--; j--; continue; } if (i and j and dp[i][j] == dp[i - 1][j - 1] + 1) { cout << "REPLACE " << i << ' ' << t[j - 1] << endl; i--; j--; continue; } if (j and dp[i][j] == dp[i][j - 1] + 1) { cout << "INSERT " << i + 1 << ' ' << t[j - 1] << endl; j--; continue; } if (i and dp[i][j] == dp[i - 1][j] + 1) { cout << "DELETE " << i << endl; i--; continue; } } }
6
#include <bits/stdc++.h> int n; int a[1000000 + 1]; int c[1000000 + 1]; int rk[1000000 + 1]; int idx[1000000 + 1]; int ne; int v[(2 * (1000000 - 1)) + 1]; int next[(2 * (1000000 - 1)) + 1]; int head[(2 * (1000000 - 1)) + 1]; int sz[1000000 + 1]; int fa[1000000 + 1]; long long ans; inline void connect(int x, int y) { ++ne; v[ne] = y; next[ne] = head[x]; head[x] = ne; ++ne; v[ne] = x; next[ne] = head[y]; head[y] = ne; } inline void read() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1; i <= n - 1; ++i) { int x, y; scanf("%d%d", &x, &y); connect(x, y); } } inline void sort() { int maxa = 0; for (int i = 1; i <= n; ++i) { if (a[i] > maxa) maxa = a[i]; ++c[a[i]]; } for (int i = 2; i <= maxa; ++i) c[i] += c[i - 1]; for (int i = n; i; --i) { rk[i] = c[a[i]]--; idx[rk[i]] = i; } } inline int dsu_find(int x) { int r; for (r = x; fa[r] != 0; r = fa[r]) ; for (int p = x, q; p != r; p = q) { q = fa[p]; fa[p] = r; } return r; } inline void dsu_init() { for (int i = 1; i <= n; ++i) { fa[i] = 0; sz[i] = 1; } } inline int dsu_sz(int x) { int r = dsu_find(x); return sz[r]; } inline void dsu_union(int x, int y) { x = dsu_find(x); y = dsu_find(y); if (sz[x] > sz[y]) { fa[y] = x; sz[x] += sz[y]; } else { fa[x] = y; sz[y] += sz[x]; } } void chief() { sort(); dsu_init(); for (int i = 1; i <= n; ++i) { int x = idx[i]; int s = 1; long long ws = s; for (int t = head[x]; t; t = next[t]) { int y = v[t]; if (rk[y] < i) { int t = dsu_sz(y); ws += (long long)s * t; s += t; dsu_union(x, y); } } ans += ws * a[x]; } dsu_init(); for (int i = n; i; --i) { int x = idx[i]; int s = 1; long long ws = s; for (int t = head[x]; t; t = next[t]) { int y = v[t]; if (rk[y] > i) { int t = dsu_sz(y); ws += (long long)s * t; s += t; dsu_union(x, y); } } ans -= ws * a[x]; } } inline void write() { printf("%I64d\n", ans); } int main() { read(); chief(); write(); return 0; }
8
#include <bits/stdc++.h> int N, M; int main() { scanf("%d%d", &N, &M); int t = 0; for (int i = 1; i <= N; i++) for (int j = 1, x; j <= M; j++) { scanf("%d", &x); if (x && (i == 1 || j == 1 || i == N || j == M)) t = 1; } puts(t ? "2" : "4"); }
1
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; #define FOR(i, a, b) for (int (i) = (a); (i) <= (b); (i)++) #define ROF(i, a, b) for (int (i) = (a); (i) >= (b); (i)--) #define REP(i, n) FOR(i, 0, (n)-1) #define sqr(x) ((x) * (x)) #define all(x) (x).begin(), (x).end() #define reset(x, y) memset(x, y, sizeof(x)) #define uni(x) (x).erase(unique(all(x)), (x).end()) #define BUG(x) cerr << #x << " = " << (x) << endl #define pb push_back #define eb emplace_back #define mp make_pair #define _1 first #define _2 second #define chkmin(a, b) a = min(a, b) #define chkmax(a, b) a = max(a, b) const int maxn = 212345; int n, k, f[maxn], ans[maxn], propagate[maxn]; vector<int> G[maxn], query[maxn]; int main() { #ifdef ONLINE_JUDGE ios_base::sync_with_stdio(false); cin.tie(nullptr); #endif cin >> n; REP(i, n - 1) { int u, v; cin >> u >> v; G[u].eb(v); G[v].eb(u); } cin >> k; vector<int> a(k); REP(i, k) cin >> a[i]; reset(f, -1); queue<int> q; for (auto x : a) { f[x] = 0; q.emplace(x); } while (!q.empty()) { auto u = q.front(); q.pop(); for (auto v : G[u]) if (f[v] == -1) { f[v] = f[u] + 1; q.emplace(v); } } FOR(i, 1, n) query[f[i]].eb(i); ROF(i, n - 1, 1) { queue<pii> Q; for (auto u : query[i]) if (propagate[u] < f[u]) { if (!ans[u]) ans[u] = i; propagate[u] = f[u]; Q.emplace(f[u], u); } while (!Q.empty()) { auto [w, u] = Q.front(); Q.pop(); if (!--w) continue; for (auto v : G[u]) { int nw = min(f[v], w); if (propagate[v] < nw) { if (!ans[v]) ans[v] = i; propagate[v] = nw; Q.emplace(nw, v); } } } } FOR(i, 1, n) cout << ans[i] << ' '; }
9
#include <bits/stdc++.h> using namespace std; const long long MAX = 2500000000; const int inf = 123456789; const double EPS = 1e-9; const double PII = 2 * asin(1.0); const long long mod = 1e9 + 7; int double_cmp(double x, double y = 0, double tol = EPS) { return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; } const int N = 1e5; int n; int bit[2 * N]; void buildNormal() { for (int i = n - 1; i > 0; --i) bit[i] = bit[i << 1] + bit[i << 1 | 1]; } int seg[21][2 * N], lazy[21][N]; int h; void calc(int p, int e) { seg[e][p] = seg[e][p << 1] + seg[e][p << 1 | 1]; if (lazy[e][p]) seg[e][p] = bit[p] - seg[e][p]; } void apply(int p, int e) { seg[e][p] = bit[p] - seg[e][p]; if (p < n) lazy[e][p] ^= 1; } void build(int l, int r, int e) { for (l += n, r += n - 1; l > 1;) { l >>= 1, r >>= 1; for (int i = r; i >= l; --i) calc(i, e); } } void push(int l, int r, int e) { int s = h; for (l += n, r += n - 1; s > 0; --s) { for (int i = l >> s; i <= r >> s; ++i) if (lazy[e][i] != 0) { apply(i << 1, e); apply(i << 1 | 1, e); lazy[e][i] = 0; } } } void modify(int l, int r, int e) { push(l, l + 1, e); push(r - 1, r, e); int l0 = l, r0 = r; for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) apply(l++, e); if (r & 1) apply(--r, e); } build(l0, l0 + 1, e); build(r0 - 1, r0, e); } long long query(int l, int r, int e) { push(l, l + 1, e); push(r - 1, r, e); long long res = 0LL; for (l += n, r += n; l < r; l >>= 1, r >>= 1) { if (l & 1) res += seg[e][l++]; if (r & 1) res += seg[e][--r]; } return (1 << e) * res; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) bit[i + n] = 1; buildNormal(); h = sizeof(int) * 8 - __builtin_clz(n); for (int i = 0; i < n; ++i) { int aux; scanf("%d", &aux); for (int j = 0; j < 21; ++j) { if (aux & (1 << j)) seg[j][i + n] = 1; } } for (int i = 0; i < 21; ++i) build(0, n, i); int m; scanf("%d", &m); while (m--) { int aux; scanf("%d", &aux); if (aux == 1) { int a, b; scanf("%d%d", &a, &b); a--; long long saida = 0LL; for (int i = 0; i < 21; ++i) saida += query(a, b, i); cout << saida << endl; } else { int a, b, c; scanf("%d%d%d", &a, &b, &c); a--; for (int i = 0; i < 21; ++i) { if (c & (1 << i)) modify(a, b, i); } } } }
6
#include <bits/stdc++.h> using namespace std; const int N = 1000005; int n; long long a[N]; long long h[N]; int main() { scanf("%d", &n); long long S = 0; for (int i = 1; i <= n; i++) scanf("%lld", &a[i]), S += a[i]; h[1] = (2 * S - 1LL * n * n + n) / (2 * n); for (int i = 2; i <= n; i++) h[i] = h[i - 1] + 1; long long sum = (h[1] + h[1] + n - 1) * n / 2; int p = S - sum; for (int i = 1; i <= p; i++) h[i]++; for (int i = 1; i <= n; i++) printf("%lld ", h[i]); return 0; }
8
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; vector<int> parent; vector<int> rank2; int mod = 1e9 + 7; int find(int p) { if (parent[p] == p) return p; int temp = find(parent[p]); return parent[p] = temp; } void union2(int p1, int p2) { if (rank2[p1] < rank2[p2]) parent[p1] = p2; else if (rank2[p1] > rank2[p2]) parent[p2] = p1; else { parent[p1] = p2; rank2[p2] += 1; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t = 1; int i, j, n, m, k; cin >> n >> m >> k; if (k > n) { long long ans = 1; for (i = 0; i < n; i++) { ans = (ans * m) % mod; } cout << ans << endl; return 0; } parent.clear(); parent.resize(n, 0); rank2.clear(); rank2.resize(n, 0); for (i = 0; i < n; i++) { parent[i] = i; rank2[i] = 1; } for (i = 0; i <= n - k; i++) { int diff = k - 1; for (j = i;; j++) { int l = j, r = j + diff; if (l >= r) break; if (l != r) { int par1 = find(l); int par2 = find(r); if (par1 != par2) union2(par1, par2); } diff -= 2; } } vector<int> num(n + 2, 0); for (i = 0; i < n; i++) { num[find(i)] += 1; } long long ans = 1; for (i = 0; i < n; i++) { if (num[i] != 0) ans = (ans * m) % mod; } cout << ans % mod << endl; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; n /= 1000; int ret = 1; if (n == 6 || n == 11) ret = 3; else if (n == 8 || n == 9) ret = 2; cout << ret << "\n"; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 200010; vector<int> v[maxn]; bool vis[maxn]; priority_queue<int, vector<int>, greater<int> > q1, q2; int d[maxn]; int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int T; cin >> T; while (T--) { int k; int n; cin >> n; for (int i = 1; i <= n; i++) { v[i].clear(); vis[i] = 0; } for (int i = 1; i <= n; i++) { int x; cin >> k; d[i] = k; if (k == 0) { q1.push(i); } while (k--) { cin >> x; v[x].push_back(i); } } int tmp; int cnt = 0; while (!q1.empty() || !q2.empty()) { cnt++; while (!q1.empty()) { tmp = q1.top(); q1.pop(); vis[tmp] = 1; for (int i = 0; i < v[tmp].size(); i++) { d[v[tmp][i]]--; if (d[v[tmp][i]] == 0) { if (v[tmp][i] > tmp) q1.push(v[tmp][i]); else q2.push(v[tmp][i]); } } } while (!q2.empty()) { tmp = q2.top(); q2.pop(); q1.push(tmp); } } int flag = 0; for (int i = 1; i <= n; i++) { flag += vis[i]; } if (flag == n) cout << cnt << '\n'; else cout << "-1\n"; } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, m, k, d; string s, s1, s2; int i, zx, cx; cin >> n >> m >> k >> s1 >> s2 >> s; if (s2[0] == 'h') { d = -1; } else { d = 1; } zx = m; cx = k; for (i = 0; i < (int)s.length(); i++) { if (s[i] == '0') { if (zx < cx) { zx--; if (zx == 0) { zx++; } } else { zx++; if (zx == n + 1) { zx--; } } } if (d == 1) { cx++; if (cx == n + 1) { cx = n - 1; d = -d; } } else { cx--; if (cx == 0) { cx = 2; d = -d; } } if (s[i] == '0' && cx == zx) { cout << "Controller " << i + 1; return 0; } if (s[i] == '1') { if (d == 1) { zx = 1; } else { zx = n; } } } cout << "Stowaway"; }
3
#include <iostream> #include<bits/stdc++.h> #include<cstdlib> #include<vector> using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int t;cin>>t; while(t--){ int n;cin>>n; int a[n]; for(int i=0;i<n;i++){ cin>>a[i]; }int i; for( i=0;i<n;i++){ if(a[i]<0){ cout<<"NO"<<"\n"; break; } } if(i==n){ cout<<"YES"<<"\n"; cout<<"101"<<"\n"; for(int i=0;i<=100;i++) cout<<i<<" "; } cout<<"\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; struct node { int x, t, id; } a[200002]; int n, h, m, k, i, j; int read() { char c = getchar(); int w = 0; while (c < '0' || c > '9') c = getchar(); while (c <= '9' && c >= '0') { w = w * 10 + c - '0'; c = getchar(); } return w; } int cmp1(const node &a, const node &b) { if (a.x == b.x) return a.t < b.t; return a.x < b.x; } int cmp2(const node &a, const node &b) { return a.t < b.t; } int main() { n = read(); h = read(); m = read(); k = read(); for (i = 1; i <= n; i++) { a[i].x = read(); a[i].t = read() % (m / 2); a[i].id = i; } sort(a + 1, a + n + 1, cmp2); for (i = 1; i <= n; i++) a[n + i] = (node){0, a[i].t + m / 2, a[i].id}; int ans = 1 << 30, tim, l, r; for (i = n + 1, j = 1; i <= 2 * n; i++) { while (a[i].t - a[j].t >= k && j <= 2 * n) j++; if (i - j < ans) ans = i - j, l = j, r = i - 1, tim = a[i].t; } printf("%d %d\n", ans, tim % (m / 2)); if (ans != 0) { for (i = l; i <= r; i++) printf("%d ", a[i].id); puts(""); } return 0; }
7
#include <bits/stdc++.h> using namespace std; const int N = 1 << 18; long long n; long long a[N], iii[N], sum[N]; vector<pair<long long, long long> > v[N << 1]; long double intersect(pair<long long, long long> l1, pair<long long, long long> l2) { return (long double)(l2.second - l1.second) / (l1.first - l2.first); } class trick { public: int ptr; vector<pair<long long, long long> > v; void add(pair<long long, long long> l) { while (ptr + 1 < v.size()) { pair<long long, long long> l1 = v[v.size() - 2]; pair<long long, long long> l2 = v[v.size() - 1]; if (l.first == l2.first or intersect(l1, l) <= intersect(l1, l2)) v.pop_back(); else break; } if (ptr < v.size() and v.back().first == l.first) v.pop_back(); v.push_back(l); } long long get(long long x) { while (ptr + 1 < v.size()) { pair<long long, long long> l1 = v[ptr]; pair<long long, long long> l2 = v[ptr + 1]; if (intersect(l1, l2) <= x) ptr++; else break; } return v[ptr].first * x + v[ptr].second; } } t[N << 1]; long long get(long long l, long long r, long long x) { long long ans = -1e18; for (l += N, r += N; l <= r; l = (l + 1) >> 1, r = (r - 1) >> 1) { if (l & 1) ans = max(ans, t[l].get(x)); if (~r & 1) ans = max(ans, t[r].get(x)); } return ans; } int main() { scanf("%lld", &n); for (int i = 1; i <= n; i++) { scanf("%lld", a + i); iii[i] = iii[i - 1] + a[i] * i; sum[i] = sum[i - 1] + a[i]; int x = i + N; while (x >= 1) { v[x].push_back({-sum[i], iii[i]}); x >>= 1; } } for (int i = 1; i < N + N; i++) { sort(v[i].begin(), v[i].end()); for (auto x : v[i]) t[i].add(x); } long long ans = 0; for (int i = 1; i <= n; i++) { ans = max(ans, get(i, n, i - 1) - iii[i - 1] + (i - 1) * sum[i - 1]); } printf("%lld\n", ans); return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { int n, k, i = 1; cin >> n >> k; if (k == 1) cout << n; else { vector<int> ans; while (ans.size() < k) { i = 2; while (n % i != 0 && i <= n + 1) i++; if (i >= n + 1) { cout << -1; return 0; } n /= i; ans.push_back(i); } for (i = 0; i < ans.size() - 1; i++) cout << ans[i] << " "; cout << n * ans[ans.size() - 1]; } }
1
#include <bits/stdc++.h> using namespace std; int n, p, ans; struct node { string a; char b; } s[50]; queue<string> q; map<unsigned long long, bool> vis; inline unsigned long long Hash(string s) { int p = s.size(); unsigned long long res = 0LL; for (int i = 0; i < p; ++i) res = (res * 26LL + (unsigned long long)(s[i] - 'a' + 1)); return res; } inline bool check(string x) { for (int i = 1; i < n; ++i) { for (int j = 0; j < n - 1; ++j) { bool flg = 0; for (int k = 1; k <= p; ++k) { if (x[j] == s[k].a[0] && x[j + 1] == s[k].a[1]) { x.erase(j + 1, 1); x[j] = s[k].b; flg = 1; break; } } if (flg) break; else return 0; } } return x == "a"; } int main() { cin >> n >> p; for (int i = 1; i <= p; ++i) cin >> s[i].a >> s[i].b; vis[Hash("a")] = 1; q.push("a"); while (!q.empty()) { string x = q.front(); q.pop(); int len = x.size(); for (int i = 0; i < len; ++i) { for (int k = 1; k <= p; ++k) { if (s[k].b == x[i]) { string t = x; t.erase(i, 1); t.insert(i, s[k].a); int H = Hash(t); if (!vis[H]) { vis[H] = 1; if ((int)t.size() < n) { q.push(t); } if ((int)t.size() == n && check(t)) { ans++; } } } } } } cout << ans; return 0; }
2
#include <bits/stdc++.h> using namespace std; long long num[10]; int ps[10]; int dp[10][10]; int ans; int tmp; int n; int f[10]; bool cmp(long long x, long long y) { return x < y; } void dfs(int pz, int sum) { if (pz == 0) { int t = 0; for (int i = 1; i <= n; i++) if (f[i] == -1) t++; if (t == 1) sum++; tmp = max(sum, tmp); return; } for (int i = n; i > pz; i--) { if (num[i] % num[pz] == 0) { num[i] /= num[pz]; int t = f[pz]; f[pz] = i; dfs(pz - 1, ps[pz] + sum); f[pz] = t; num[i] *= num[pz]; } } dfs(pz - 1, sum); } int main() { while (scanf("%d", &n) != EOF) { int i; for (i = 1; i <= n; i++) scanf("%I64d", &num[i]); sort(num + 1, num + 1 + n, cmp); memset(ps, 0, sizeof(ps)); memset(f, -1, sizeof(f)); for (i = 1; i <= n; i++) { long long t = num[i]; for (long long j = 2; j * j <= t && j * j > 0; j++) if (t % j == 0) { while (t % j == 0) { ps[i]++; t /= j; } } if (t != 1) ps[i]++; } ans = 1 + n; for (i = 1; i <= n; i++) if (ps[i] > 1) ans += ps[i]; tmp = 0; dfs(n - 1, 0); printf("%d\n", ans - tmp); } return 0; }
7
#include <bits/stdc++.h> using namespace std; const long long maxc = 1000 + 10, inf = 1e9 + 10, T1 = 123, M = 12345, z = 26; long long n, c; long long mat[T1][T1]; pair<char, long long> p[maxc]; long long zarib[T1][maxc], tmp[T1][T1], ans[T1][T1]; long long nxt[T1][z]; bool mark[z]; void mul(long long a[T1][T1], long long b[T1][T1]) { for (long long i = 0; i < T1; i++) { for (long long j = 0; j < T1; j++) { tmp[i][j] = 0; for (long long k = 0; k < T1; k++) { tmp[i][j] = (tmp[i][j] + a[i][k] * b[k][j]) % M; } } } for (long long i = 0; i < T1; i++) { for (long long j = 0; j < T1; j++) { ans[i][j] = tmp[i][j]; } } } void power(long long b) { if (b == 0) return; power(b >> 1); mul(ans, ans); if (b & 1) { mul(ans, mat); } } int32_t main() { scanf("%lld%lld", &n, &c); long long zr = 1; for (long long i = 0; i < c; i++) { scanf(" %c%lld", &p[i].first, &p[i].second); mark[p[i].first - 'A'] = 1; zr *= p[i].second; } for (long long i = 0; i < T1; i++) { long long mask = i; for (long long j = 0; j < c; j++) { zarib[i][j] = mask % p[j].second; mask /= p[j].second; } } for (long long ch = 0; ch < z; ch++) { for (long long i = 0; i < zr; i++) { long long mask = 0; for (long long j = c - 1; j >= 0; j--) { mask *= p[j].second; mask += (zarib[i][j] + (p[j].first == ch + 'A')) % p[j].second; } nxt[i][ch] = mask; } } for (long long i = 0; i < zr; i++) { ans[i][i] = 1; for (long long ch = 0; ch < z; ch++) { if (mark[ch]) { mat[i][nxt[i][ch]] = (mat[i][nxt[i][ch]] + 1) % M; } } } power(n); long long sum = 0; for (long long i = 0; i < zr; i++) { long long mn[z]; fill(mn, mn + z, inf); for (long long j = 0; j < c; j++) { mn[p[j].first - 'A'] = min(mn[p[j].first - 'A'], zarib[i][j]); } bool ok = 1; for (long long ch = 0; ch < z; ch++) { if (mark[ch] == 1 && mn[ch] > 0) { ok = 0; } } if (ok) { sum = (sum + ans[0][i]) % M; } } printf("%lld\n", sum); }
8
#include <bits/stdc++.h> using namespace std; int main() { int n; int pol; int konpol; int sekund; char k1, s1; cin >> s1 >> k1 >> sekund; switch (s1) { case 118: pol = 0; break; case 60: pol = 1; break; case 94: pol = 2; break; case 62: pol = 3; break; } switch (k1) { case 118: konpol = 0; break; case 60: konpol = 1; break; case 94: konpol = 2; break; case 62: konpol = 3; break; } int pol1 = pol; int pol2 = pol; pol1 = (pol1 + sekund) % 4; pol2 = (pol2 - sekund) % 4; if (pol2 == -1) pol2 = 3; if (pol2 == -3) pol2 = 1; pol2 = abs(pol2); if (pol2 == konpol && pol1 == konpol) { cout << "undefined"; } else if (pol1 == konpol) { cout << "cw"; } else if (pol2 == konpol) cout << "ccw"; else cout << "undefined"; return 0; }
0
#include <bits/stdc++.h> using pii = std::pair<int, int>; using pll = std::pair<long long, long long>; int main() { std::ios::sync_with_stdio(false); int t; std::cin >> t; while (t--) { long long a, b, c; std::cin >> a >> b >> c; long long s = a + b + c; long long t = (s + 1) / 2; if (std::max({a, b, c}) > t) { std::cout << "No" << std::endl; } else { std::cout << "Yes" << std::endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long inf = 0x3f3f3f3f3f3f3f3fll; inline long long read() { long long x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + c - '0'; c = getchar(); } return x * f; } int n, x[5005], m, p[5005], c[5005], q[5005], l, r, t[5005]; long long s[5005], dp[5005][5005]; int main() { n = read(), m = read(); for (int i = 1; i <= n; ++i) { x[i] = read(); } sort(x + 1, x + n + 1); for (int i = 1; i <= m; ++i) { p[i] = read(), c[i] = read(); t[i] = i; } sort(t + 1, t + m + 1, [&](int i, int j) { return p[i] < p[j]; }); memset(dp, 0x3f, sizeof(dp)); dp[0][0] = 0; for (int j = 1; j <= m; ++j) { r = 0, l = 1; for (int i = 1; i <= n; ++i) { s[i] = s[i - 1] + abs(x[i] - p[t[j]]); } for (int i = 0; i <= n; ++i) { while (l <= r && q[l] < i - c[t[j]]) ++l; while (l <= r && dp[i][j - 1] - s[i] <= dp[q[r]][j - 1] - s[q[r]]) --r; q[++r] = i; dp[i][j] = dp[q[l]][j - 1] + s[i] - s[q[l]]; } } if (dp[n][m] == inf) { printf("-1\n"); return 0; } printf("%lld\n", dp[n][m]); return 0; }
9
#include <bits/stdc++.h> const int MOD = 1000003; bool A[2][500000][2]; int n, m, i, j, ans = 1; int main() { scanf("%d%d", &n, &m); for (char c; i < n; ++i) for (j = 0, getchar(); j < m; ++j) if ((c = getchar()) != '.') A[1][j][(i & 1) ^ (c == '1' || c == '4')] = true, A[0][i][(j & 1) ^ c <= '2'] = true; for (i = 0; i < n; ++i) ans *= 2 - A[0][i][0] - A[0][i][1], ans %= MOD; for (j = 0; j < m; ++j) ans *= 2 - A[1][j][0] - A[1][j][1], ans %= MOD; printf("%d\n", ans); }
7
#include <bits/stdc++.h> using namespace std; const int inf = 2e+9; char s1[110], s2[110], str[110]; int main() { int n, num; scanf("%d", &n); scanf("%s", s1); for (int i = 0; i < n; ++i) { scanf("%s", str); int j, k; for (j = k = 0;; ++j, ++k) { if (str[j] == '-') break; s2[k] = str[j]; } if (strcmp(s1, s2) == 0) ++num; for (j += 2, k = 0; str[j] != '\0'; ++k, ++j) { s2[k] = str[j]; } s2[k] = '\0'; if (strcmp(s1, s2) == 0) ++num; } puts(num % 2 ? "contest" : "home"); return 0; }
0
#include <bits/stdc++.h> using namespace std; const unsigned int mm = 4294967295; int main() { int n, k; cin >> n >> k; unsigned int arr[100005]; int d1, d2, d3, d4; for (int i = 0; i < n; i++) { scanf("%d.%d.%d.%d", &d1, &d2, &d3, &d4); arr[i] = (unsigned int)(((unsigned int)(d1 << 24)) + ((unsigned int)(d2 << 16)) + ((unsigned int)(d3 << 8)) + ((unsigned int)(d4))); } sort(arr, arr + n); for (int i = 31; i >= 0; i--) { unsigned int hadi = mm << (i); int ans = 1; for (int j = 1; j < n; j++) { if ((arr[j] & hadi) != (arr[j - 1] & hadi)) ans++; } if (ans == k) { d1 = hadi % 256; hadi /= 256; d2 = hadi % 256; hadi /= 256; d3 = hadi % 256; hadi /= 256; d4 = hadi % 256; cout << d4 << "." << d3 << "." << d2 << "." << d1 << endl; return 0; } } cout << -1 << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; long long power(long long x, long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) { res = (res * x) % p; } y = y >> 1; x = (x * x) % p; } return res; } long long modInverse(long long a, long long p) { return power(a, p - 2, p); } long long gcd(long long x, long long y) { if (x == 0 || y == 0) { return max(y, x); } return gcd(y % x, x); } long long gcdExtended(long long a, long long b, long long &x, long long &y) { if (a == 0) { x = 0; y = 1; return b; } long long x1, y1; long long gcd = gcdExtended(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return gcd; }; vector<long long> a[300005]; int main() { long long n, k; scanf("%lld %lld", &(n), &(k)); long long pp = n - 1; long long ans = 0; ans = (pp + k - 1) / k; ans++; ans += (pp / k); if (pp % k > 1) ans++; cout << ans - 1 << endl; for (long long i = 1; i <= k; i++) { a[0].push_back(i); cout << 1 << " " << i + 1 << endl; } long long d = 0; d = k + 1; while (d < n) { a[d - k].push_back(d); cout << d - k + 1 << " " << d + 1 << endl; d++; } }
5
#include <bits/stdc++.h> using namespace std; int n; double g[20][20], arr[1 << 18]; int main() { scanf("%d", &n); int i, j, k, num; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { scanf("%lf", &g[i][j]); } } arr[(1 << n) - 1] = 1; for (k = (1 << n) - 1; k > 0; k--) { num = 0; for (i = 0; i < n; i++) if (k & (1 << i)) num++; for (i = 0; i < n; i++) { if ((k & (1 << i)) == 0) continue; for (j = 0; j < n; j++) { if ((k & (1 << j)) == 0) continue; if (i == j) continue; arr[k - (1 << i)] += arr[k] * g[j][i] / num / (num - 1) * 2; } } } for (i = 0; i < n; i++) printf("%lf ", arr[1 << i]); return 0; }
5
#include <bits/stdc++.h> using namespace std; void read(long long &x) { scanf("%lld", &x); } void read(long long &x, long long &y) { scanf("%lld%lld", &x, &y); } void read(long long &x, long long &y, long long &z) { scanf("%lld%lld%lld", &x, &y, &z); } void print(long long x) { printf("%lld ", x); } void println(long long x) { printf("%lld\n", x); } const long long N = 400000; long long ans, sz, cur, len, calc, pos, mn; string t, s, li[N + 10]; map<string, long long> v; struct item { long long next[26], pos, cnt; } st[333333]; void f() { if (s.size() == 0) return; li[++sz] = s; long long ch = 1 - v[s], ch1 = 0, can; v[s] = 1; cur = 0; len = s.size(); mn = len; for (long long i = 0; i < len; i++) { if (!st[cur].next[s[i] - 97]) st[cur].next[s[i] - 97] = ++calc; cur = st[cur].next[s[i] - 97]; if (st[cur].cnt == 1 && !ch1) { ch1 = 1; pos = st[cur].pos; can = 1; if (li[pos].size() <= s.size()) { for (long long j = i; j < li[pos].size(); j++) if (li[pos][j] != s[j]) can = 0; if (can) mn = min(mn, i + 2 + len - (long long)li[pos].size()); } } st[cur].pos = sz; st[cur].cnt += ch; } ans += mn; } int main() { while (getline(cin, t)) { ans++; for (long long i = 0; i < t.size(); i++) if (t[i] > 'z' || t[i] < 'a') f(), s = "", ans++; else s += t[i]; f(); s = ""; } cout << ans; }
5
#include <bits/stdc++.h> using namespace std; int main() { std::locale loc; string usr; cin >> usr; std::replace(usr.begin(), usr.end(), '0', 'O'); std::replace(usr.begin(), usr.end(), '1', 'i'); std::replace(usr.begin(), usr.end(), 'l', 'i'); std::replace(usr.begin(), usr.end(), 'L', 'i'); std::transform(usr.begin(), usr.end(), usr.begin(), ::tolower); size_t n; cin >> n; vector<string> log(n); for (size_t i = 0; i < n; ++i) { cin >> log[i]; std::replace(log[i].begin(), log[i].end(), '0', 'O'); std::replace(log[i].begin(), log[i].end(), '1', 'i'); std::replace(log[i].begin(), log[i].end(), 'l', 'i'); std::replace(log[i].begin(), log[i].end(), 'L', 'i'); std::transform(log[i].begin(), log[i].end(), log[i].begin(), ::tolower); if (usr == log[i]) { cout << "No" << endl; return 0; } } cout << "Yes" << endl; return 0; }
2
#include <bits/stdc++.h> const int INF = 1 << 29; using namespace std; inline int nextint() { int temporary; scanf("%d", &temporary); return temporary; } inline long long nextll() { long long temporary; scanf("%lld", &temporary); return temporary; } inline double nextdouble() { double temporary; scanf("%lf", &temporary); return temporary; } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } long long leftChild(long long p) { return p << 1; } long long rightChild(long long p) { return (p << 1) + 1; } inline long long mid(long long l, long long r) { return (l + r) / 2; } const long long MOD = 1000000007; const double eps = 1e-7; const double PI = acos(-1.0); inline bool is_palindrome(const string& s) { return std::equal(s.begin(), s.end(), s.rbegin()); } template <typename T> void modulize(T& a, const T& b) { if (a >= b) { a %= b; } } long long mulmod(long long a, long long b, long long m) { long long q = (long long)(((long double)a * (long double)b) / (long double)m); long long r = a * b - q * m; if (r > m) r %= m; if (r < 0) r += m; return r; } template <typename T, typename S> T expo(T e, S n) { T x = 1, p = e; while (n) { if (n & 1) x = x * p; p = p * p; n >>= 1; } return x; } template <typename T> T power(T e, T n, T& m) { T x = 1, p = e; while (n) { if (n & 1) x = mod(x * p, m); p = mod(p * p, m); n >>= 1; } return x; } template <typename T> T powerL(T e, T n, T& m) { T x = 1, p = e; while (n) { if (n & 1) x = mulmod(x, p, m); p = mulmod(p, p, m); n >>= 1; } return x; } template <typename T> T InverseEuler(T a, T& m) { return (a == 1 ? 1 : power(a, m - 2, m)); } inline int two(int n) { return 1 << n; } inline void set_bit(int& n, int b) { n |= two(b); } inline void unset_bit(int& n, int b) { n &= ~two(b); } int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k; cin >> n >> k; int arr[n + 1]; arr[0] = 0; for (long long i = (long long)(1); i < (long long)(n + 1); i++) { cin >> arr[i]; } sort(arr + 1, arr + n + 1); long long presum[n + 1]; memset(presum, 0, sizeof(presum)); for (long long i = (long long)(1); i < (long long)(n + 1); i++) { presum[i] = arr[i]; presum[i] += presum[i - 1]; } long long len = 1, val = arr[1]; for (long long i = (long long)(2); i < (long long)(n + 1); i++) { int lo = 1, hi = i - 1, pos = i; while (lo <= hi) { int mid = (lo + hi) / 2; long long cost = arr[i] * (long long)(i - mid + 1) - (presum[i] - presum[mid - 1]); if (cost <= k) { pos = mid; hi = mid - 1; } else lo = mid + 1; } if (i - pos + 1 > len) len = i - pos + 1, val = arr[i]; } cout << len << " " << val; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; const int MAXN = 2 * 105000; int n; long long p[MAXN]; int main() { scanf("%d", &n); p[0] = 1; for (int i = 1; i <= n; i++) { p[i] = 1ll * p[i - 1] * 10 % mod; } for (int i = 1; i < n; i++) { long long cur = 0; long long add1 = 2ll * 10 * 9; add1 = add1 * p[n - i - 1] % mod; cur = add1; if (i + 2 <= n) { long long add2 = 10ll * (n - i + 1 - 2) % mod; add2 = add2 * 81ll * p[n - i - 2] % mod; cur = (cur + add2) % mod; } printf("%lld ", cur); } printf("10\n"); return 0; }
5
#include <bits/stdc++.h> using namespace std; class dsu { private: vector<int> par; public: dsu(int N) : par(N) { iota(par.begin(), par.end(), 0); } int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); } bool same(int x, int y) { return find(x) == find(y); } void Union(int x, int y) { if (same(x, y)) { return; } x = find(x); y = find(y); par[x] = y; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int m, n; cin >> m >> n; vector<int> a(m); for (int i = 0; i < m; ++i) { cin >> a[i]; } vector<int> b(n); for (int i = 0; i < n; ++i) { cin >> b[i]; } vector<int> s(m); dsu dsu(n + m); vector<tuple<int, int, int> > edge; long long ans = 0; for (int i = 0; i < m; ++i) { cin >> s[i]; for (int j = 0; j < s[i]; ++j) { int x; cin >> x; --x; edge.emplace_back(a[i] + b[x], i, x + m); ans += a[i] + b[x]; } } sort(edge.begin(), edge.end(), greater<tuple<int, int, int> >()); for (auto [w, x, y] : edge) { if (!dsu.same(x, y)) { ans -= w; dsu.Union(x, y); } } cout << ans << '\n'; return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { long long n, i, k, sum = 0; cin >> n; vector<long long> v; for (i = 0; i < n; i++) { cin >> k; v.push_back(k); } sort(v.begin(), v.end()); for (long long j = 0; j < n / 2; j++) { long long temp = v[j] + v[n - j - 1]; sum = sum + (temp * temp); } cout << sum << "\n"; }
0
#include <bits/stdc++.h> using namespace std; long long a, b, c, d[100007], n, m, T[5][100007 << 2][12], R[100007 << 2], type, ans, sm; void combine(long long z, long long v) { long long tr = R[v << 1] % (((z + 2) << 1) - 2); for (long long i = 0; i < ((z + 2) << 1) - 2; i++) T[z][v][i] = T[z][v << 1][i] + T[z][v << 1 | 1][(i - tr + (4 * z + 8 - 4)) % (((z + 2) << 1) - 2)]; } void form_tree(long long l, long long r, long long v, long long z) { if (l == r) { R[v] = 1; T[z][v][0] = d[l]; return; } form_tree(l, ((l + r) >> 1), v << 1, z); form_tree(((l + r) >> 1) + 1, r, v << 1 | 1, z); R[v] = R[v << 1] + R[v << 1 | 1]; combine(z, v); } void upd(long long x, long long y, long long l, long long r, long long v, long long z) { if (l == r) { T[z][v][0] = y; return; } if (x <= ((l + r) >> 1)) upd(x, y, l, ((l + r) >> 1), v << 1, z); else upd(x, y, ((l + r) >> 1) + 1, r, v << 1 | 1, z); combine(z, v); } long long bul(long long x, long long y, long long l, long long r, long long v, long long z, long long w) { if (y < l || r < x) return 0; if (x <= l && r <= y) { sm += r - l + 1; return T[z][v][(w - ((sm - (r - l + 1)) % (((z + 2) << 1) - 2)) + (4 * z + 8 - 4)) % (((z + 2) << 1) - 2)]; } return (bul(x, y, l, ((l + r) >> 1), v << 1, z, w) + bul(x, y, ((l + r) >> 1) + 1, r, v << 1 | 1, z, w)); } int main() { scanf("%lld", &n); for (long long i = 1; i <= n; i++) scanf("%lld", &d[i]); for (long long i = 0; i < 5; i++) form_tree(1, n, 1, i); scanf("%lld", &m); while (m--) { scanf("%lld%lld%lld", &type, &a, &b); if (type == 2) { scanf("%lld", &c); ans = 0; for (long long i = 1; i <= c; i++) { sm = 0; ans += (bul(a, b, 1, n, 1, c - 2, i - 1) * i); } long long cn = 1; for (int i = (c << 1) - 2; i > c; i--) { cn++; sm = 0; ans += (bul(a, b, 1, n, 1, c - 2, i - 1) * cn); } printf("%lld\n", ans); } else { for (long long i = 0; i < 5; i++) upd(a, b, 1, n, 1, i); } } return 0; }
6