solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; int q[100010], t, n; bool cmp(int a, int b) { return a < b; } int main() { scanf("%d", &t); for (int i = 1; i <= t; i++) { scanf("%d", &n); for (int j = 1; j <= n; j++) { scanf("%d", &q[j]); } int flag = 1; sort(q + 1, q + n + 1, cmp); for (int k = n - 2; k >= 1; k--) { if (q[n - 1] >= k + 1) { printf("%d\n", k); flag = 0; break; } } if (flag) printf("0\n"); } }
0
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n; char ch[N]; unsigned f[N]; int main() { scanf("%d%s", &n, ch + 1); if (n & 1) return puts("0"), 0; f[0] = 1; int m = n / 2, q = 0; for (int i = 1; i <= n; i++) if (ch[i] == '?') for (int j = i >> 1; n - i + j >= m && j; j--) f[j] += f[j - 1]; else q++; for (int i = 1; i <= m - q; i++) f[m] *= 25; printf("%u\n", f[m]); return 0; }
10
#include <bits/stdc++.h> using namespace std; int mon[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; map<string, int> mp; vector<pair<int, string> > vis; int isCorrect(string s) { int year, month, day; if (s[0] == '-' || s[1] == '-' || s[3] == '-' || s[4] == '-' || s[6] == '-' || s[7] == '-' || s[8] == '-' || s[9] == '-') return 0; if (s[2] != '-' || s[5] != '-') return 0; day = (s[0] - '0') * 10 + s[1] - '0'; month = (s[3] - '0') * 10 + s[4] - '0'; year = 0; for (int i = 0; i < 4; i++) year = year * 10 + s[6 + i] - '0'; if (year >= 2013 && year <= 2015 && month >= 1 && month <= 12 && day >= 1 && day <= 31 && day <= mon[month]) return 1; else return 0; } int main() { string s; cin >> s; for (int i = 0; i < s.length() - 9; i++) { string s1(s, i, 10); if (isCorrect(s1)) mp[s1]++; } for (map<string, int>::iterator it = mp.begin(); it != mp.end(); it++) vis.push_back(make_pair(it->second, it->first)); sort(vis.begin(), vis.end()); cout << vis[vis.size() - 1].second << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long M = 1e9 + 1; bool f = false; map<string, int> mp, mp_2; set<long long> st; string s, p, q; int main() { ios::sync_with_stdio(false); cin.tie(0); long long tt, a, b, d = 0, sum = 0, sum_2; vector<long long> v, v_2; cin >> tt >> b; while (tt--) { cin >> a; sum += a; sum_2 = sum; if (sum <= b) v_2.push_back(0); else { int c = 0; for (int j = v.size() - 1; j >= 0; j--) { sum_2 = sum_2 - v[j]; if (sum_2 <= b) { c++; break; } else c++; } v_2.push_back(c); } v.push_back(a); sort(v.begin(), v.end()); } for (int j = 0; j < v_2.size(); j++) cout << v_2[j] << " "; cout << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 4e5 + 5; int a[maxn]; int dp[maxn]; int dif[maxn]; int n, I, k; int main() { scanf("%d%d", &n, &I); I *= 8; k = I / n; k = 1 << min(k, 20); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); } sort(a + 1, a + n + 1); dp[0] = 0; int num = 0; for (int i = 1; i <= n; i++) { if (a[i] != a[i - 1]) { dp[i] = dp[i - 1] + 1; dif[num++] = i; } else { dp[i] = dp[i - 1]; } } int res = 0; if (k >= num) res = n; for (int i = 0; i + k < num; i++) { res = max(dif[i + k] - dif[i], res); } printf("%d", n - res); }
4
#include <bits/stdc++.h> using namespace std; long long mod = 95542721; struct node { long long sums[48]; int indx, lazy; node() { indx = 0, lazy = 0; } long long val() { return sums[indx]; } }; node st[4 * (int)1e5 + 100]; int n; long long a[(int)1e5 + 100]; void mrg(int p) { int id1 = st[2 * p].indx; int id2 = st[2 * p + 1].indx; st[p].indx = 0; for (int i = 0; i < 48; i++, id1 = (id1 + 1) % 48, id2 = (id2 + 1) % 48) st[p].sums[i] = (st[2 * p].sums[id1] + st[2 * p + 1].sums[id2]) % mod; } void build(int l = 0, int r = n - 1, int p = 1) { if (l == r) { st[p].sums[0] = a[l]; for (long long j = 1; j < 48; j++) st[p].sums[j] = ((st[p].sums[j - 1] * st[p].sums[j - 1]) % mod * st[p].sums[j - 1]) % mod; return; } int mid = (l + r) >> 1; build(l, mid, 2 * p); build(mid + 1, r, 2 * p + 1); mrg(p); } void push(int p, int l, int r) { if (st[p].lazy) { st[p].indx = (st[p].indx + st[p].lazy) % 48; if (l != r) { st[2 * p].lazy += st[p].lazy; st[2 * p + 1].lazy += st[p].lazy; } st[p].lazy = 0; } } void update(int a, int b, int l = 0, int r = n - 1, int p = 1) { if (b < l || r < a) { push(p, l, r); return; } if (a <= l && r <= b) { st[p].lazy++; push(p, l, r); return; } push(p, l, r); int mid = (l + r) >> 1; update(a, b, l, mid, 2 * p); update(a, b, mid + 1, r, 2 * p + 1); mrg(p); } long long query(int a, int b, int l = 0, int r = n - 1, int p = 1) { if (b < l || r < a) return 0; push(p, l, r); if (a <= l && r <= b) return st[p].val(); int mid = (l + r) >> 1; return (query(a, b, l, mid, 2 * p) + query(a, b, mid + 1, r, 2 * p + 1)) % mod; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%I64d", &a[i]); build(); int q; scanf("%d", &q); while (q--) { int t, l, r; scanf("%d%d%d", &t, &l, &r); l--; r--; if (t == 1) printf("%I64d\n", query(l, r)); else update(l, r); } return 0; }
9
#include <bits/stdc++.h> using namespace std; const int kMaxR = 505, kMaxN = 1e5 + 5, kInf = 0x3f3f3f3f; int r, n, X[kMaxN], Y[kMaxN], T[kMaxN]; int f[kMaxN]; int main() { scanf("%d %d", &r, &n); for (int i = 1; i <= n; ++i) { scanf("%d %d %d", T + i, X + i, Y + i); } int res = 0, cur = 0, ans = 0; fill(f, f + kMaxN, -kInf); for (int i = 1; i <= n; ++i) { while (T[i] - T[cur + 1] > 2 * r) { res = max(res, f[cur + 1]); ++cur; } if (abs(X[i] - 1) + abs(Y[i] - 1) <= T[i] - 0) { f[i] = max(f[i], 1); } for (int j = cur + 1; j < i; ++j) { if (abs(X[i] - X[j]) + abs(Y[i] - Y[j]) <= T[i] - T[j]) { f[i] = max(f[i], f[j] + 1); } } if (cur > 0) f[i] = max(f[i], res + 1); ans = max(ans, f[i]); } printf("%d\n", ans); return 0; }
6
#include <bits/stdc++.h> int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); size_t n; std::cin >> n; std::string result = "ROYGBIV"; while (result.size() < n) { result += result[result.size() - 4]; } std::cout << result << '\n'; }
2
#include <bits/stdc++.h> using namespace std; const int N = 205; inline int read() { int f = 1, x = 0; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - 48; ch = getchar(); } return f * x; } int n, k; int cnt[N]; int main() { cin >> n >> k; vector<pair<int, int> > segs(n); vector<int> ans(n); for (int i = 0; i < n; i++) { cin >> segs[i].first >> segs[i].second; cnt[segs[i].first]++, cnt[segs[i].second + 1]--; } for (int i = 1; i < N; i++) cnt[i] += cnt[i - 1]; for (int i = 1; i < N; i++) { while (cnt[i] > k) { int pos = -1; for (int p = 0; p < n; p++) { if (!ans[p] && (segs[p].first <= i && segs[p].second >= i) && (pos == -1 || segs[p].second > segs[pos].second)) { pos = p; } } for (int j = segs[pos].first; j <= segs[pos].second; j++) cnt[j]--; ans[pos] = 1; } } cout << accumulate(ans.begin(), ans.end(), 0) << endl; for (int i = 0; i < n; i++) { if (ans[i]) cout << i + 1 << " "; } cout << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int d[1 << 13]; int j[1 << 13][32]; int jcost[1 << 13][32]; int parentedge[1 << 13]; struct Edge { int u, v, w; Edge(int u, int v, int w) : u(u), v(v), w(w){}; }; vector<Edge> edges; vector<int> adj[1 << 13]; int N; void dfs(int u, int p, int pc, int dist, int pe) { j[u][0] = p; jcost[u][0] = pc; parentedge[u] = pe; d[u] = dist; for (int k = 1; k < 32 && j[u][k - 1] != -1; k++) { int mid = j[u][k - 1]; j[u][k] = j[mid][k - 1]; jcost[u][k] = min(jcost[u][k - 1], jcost[mid][k - 1]); } for (int e : adj[u]) { int v = u ^ edges[e].u ^ edges[e].v; int w = edges[e].w; if (v == p) continue; dfs(v, u, w, dist + 1, e); } } pair<int, int> walk(int u, int k) { int mincost = INT_MAX; for (int i = 31; i >= 0; i--) { if ((k & (1 << i)) != 0) { mincost = min(mincost, jcost[u][i]); u = j[u][i]; } } return make_pair(u, mincost); } pair<int, int> getlca(int u, int v) { int mincost = INT_MAX; if (d[u] < d[v]) { pair<int, int> w = walk(v, d[v] - d[u]); mincost = min(mincost, w.second); v = w.first; } if (d[u] > d[v]) { pair<int, int> w = walk(u, d[u] - d[v]); mincost = min(mincost, w.second); u = w.first; } if (u == v) { return make_pair(u, mincost); } for (int k = 31; k >= 0; k--) { if (j[u][k] != j[v][k]) { mincost = min({mincost, jcost[u][k], jcost[v][k]}); u = j[u][k]; v = j[v][k]; } } mincost = min({mincost, jcost[u][0], jcost[v][0]}); return make_pair(j[u][0], mincost); } int M; int m[1 << 13][3]; void set_edges(int e) { int u = m[e][0]; int v = m[e][1]; int w = m[e][2]; int lca = getlca(u, v).first; for (int n : {u, v}) { while (n != lca) { int pe = parentedge[n]; edges[pe].w = max(edges[pe].w, w); n = n ^ edges[pe].u ^ edges[pe].v; } } } int main() { scanf("%d", &N); for (int i = 0; i < N - 1; i++) { int u, v; scanf("%d %d", &u, &v); u--; v--; adj[u].push_back(edges.size()); adj[v].push_back(edges.size()); edges.emplace_back(u, v, 1); } for (int i = 0; i < N; i++) { fill(j[i], j[i] + 32, -1); } dfs(0, -1, 0, 0, -1); scanf("%d", &M); for (int i = 0; i < M; i++) { scanf("%d %d %d", &m[i][0], &m[i][1], &m[i][2]); m[i][0]--; m[i][1]--; set_edges(i); } dfs(0, -1, 0, 0, 0); for (int i = 0; i < M; i++) { int mincost = getlca(m[i][0], m[i][1]).second; if (mincost != m[i][2]) { printf("-1"); return 0; } } for (Edge& e : edges) { printf("%d ", e.w); } }
6
#include <bits/stdc++.h> using namespace std; int main() { char board[10][10]; int i, j, w = 0, b = 0; for (i = 0; i < 8; i++) for (j = 0; j < 8; j++) { cin >> board[i][j]; if (board[i][j] >= 'A' && board[i][j] <= 'Z') { if (board[i][j] == 'Q') w = w + 9; if (board[i][j] == 'R') w = w + 5; if (board[i][j] == 'B') w = w + 3; if (board[i][j] == 'N') w = w + 3; if (board[i][j] == 'P') w = w + 1; } else if (board[i][j] >= 'a' && board[i][j] <= 'z') { if (board[i][j] == 'q') b = b + 9; if (board[i][j] == 'r') b = b + 5; if (board[i][j] == 'b') b = b + 3; if (board[i][j] == 'n') b = b + 3; if (board[i][j] == 'p') b = b + 1; } } if (w > b) cout << "White"; else if (w < b) cout << "Black"; else cout << "Draw"; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; int a[n][n]; long long l[n][n], r[n][n]; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> a[i][j]; l[i][j] = r[i][j] = a[i][j]; } } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i > 0 && j > 0) r[i][j] += r[i - 1][j - 1]; if (i > 0 && j < n - 1) l[i][j] += l[i - 1][j + 1]; } } for (int i = n - 1; i >= 0; --i) { for (int j = n - 1; j >= 0; --j) { if (i < n - 1 && j < n - 1) r[i][j] = r[i + 1][j + 1]; if (i < n - 1 && j > 0) l[i][j] = l[i + 1][j - 1]; } } long long max1 = -1; int x1, y1; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (l[i][j] + r[i][j] - a[i][j] > max1) { max1 = l[i][j] + r[i][j] - a[i][j]; x1 = i; y1 = j; } } } long long max2 = -1; int x2, y2; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { int d = abs(i - x1) + abs(j - y1); if ((d & 1) && l[i][j] + r[i][j] - a[i][j] > max2) { max2 = l[i][j] + r[i][j] - a[i][j]; x2 = i; y2 = j; } } } cout << max1 + max2 << endl; cout << x1 + 1 << " " << y1 + 1 << " " << x2 + 1 << " " << y2 + 1 << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; bool debug = 1; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; long long ln, lk, lm; struct point { int x, y; }; point b[2105], r[2105]; bool cmpb(point p, point q) { if (p.y == q.y) return p.x < q.x; return p.y < q.y; } bool cmpr(point p, point q) { if (p.x == q.x) return p.y < q.y; return p.x < q.x; } bool vis[2105][2105]; bool ck(int tm) { memset(vis, 0, sizeof vis); for (int(i) = 0; (i) < (int)(n); (i)++) { int left = b[i].x - 2 * tm; int right = b[i].x + 2 * tm; for (int j = i + 1; j < n; j++) { if (b[i].y != b[j].y) break; if (b[j].x - right <= 2 * tm) { right = b[j].x + 2 * tm; i = j; } else break; } vector<int> qs; for (int(j) = 0; (j) < (int)(m); (j)++) if (r[j].x <= right && r[j].x >= left) { int bottom = r[j].y - 2 * tm; int top = r[j].y + 2 * tm; for (int k = j + 1; k < m; k++) { if (r[k].x != r[j].x) break; if (r[k].y - top <= 2 * tm) { top = r[k].y + 2 * tm; j = k; } else break; } if (bottom <= b[i].y && b[i].y <= top) { qs.push_back(j); } } for (int(j) = 0; (j) < (int)(qs.size()); (j)++) for (int k = j + 1; k < qs.size(); k++) { if (vis[qs[j]][qs[k]]) return 1; vis[qs[j]][qs[k]] = 1; } } return 0; } int main() { scanf("%d%d", &n, &m); int x, y; for (int(i) = 0; (i) < (int)(n + m); (i)++) { scanf("%d%d", &x, &y); if (i < n) { b[i].x = x - y; b[i].y = x + y; } else { r[i - n].x = x - y; r[i - n].y = x + y; } } sort(b, b + n, cmpb); sort(r, r + m, cmpr); int l = 1, r = 1e7; while (l + 1 < r) { int mid = (l + r) / 2; if (ck(mid)) r = mid; else l = mid + 1; } if (ck(l)) printf("%d\n", l); else if (ck(r)) printf("%d\n", r); else puts("Poor Sereja!"); return 0; }
9
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; const int maxn = 5e5 + 100; const int mod = 1e9 + 7; mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); vector<int> add[2 * maxn], rem[2 * maxn]; int par[maxn], siz[maxn], edges[maxn]; int root(int v) { return v == par[v] ? v : par[v] = root(par[v]); } void unite(int a, int b) { a = root(a), b = root(b); if (a == b) { edges[a]++; return; } if (siz[a] < siz[b]) swap(a, b); par[b] = a; edges[a] += edges[b] + 1; siz[a] += siz[b]; } vector<pii> v; int who[maxn * 2]; int main() { ios_base::sync_with_stdio(false), cin.tie(0); int n; cin >> n; v.resize(n + 1); for (int i = 1; i <= n; i++) { int l, r; cin >> l >> r; who[l] = i; add[l].push_back(l); rem[r].push_back(l); v[i] = pii(l, r); } sort(v.begin() + 1, v.end()); for (int i = 1; i <= n; i++) { par[i] = i; siz[i] = 1; edges[i] = 0; } set<int> se; for (int i = 1; i <= 2 * n; i++) { for (int lo : rem[i]) { se.erase(lo); auto it = se.upper_bound(lo); while (it != se.end()) { unite(who[lo], who[*it]); if (siz[root(who[lo])] - 1 < edges[root(who[lo])]) return cout << "NO\n", 0; ++it; } } for (int lo : add[i]) { se.insert(lo); } } if (siz[root(1)] != n) cout << "NO\n"; else cout << "YES\n"; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n, k, i = 0; char c = 'c'; cin >> n >> k; if (k > n || (k == 1 && n > 1) || n + k == 2) { n + k == 2 ? cout << 'a' : cout << "-1"; return 0; } k -= 2; n -= k; while (n--) { cout << (i % 2 == 0 ? 'a' : 'b'); i++; } while (k--) cout << c++; }
2
#include <bits/stdc++.h> using namespace std; bool a[1100]; int n, k; int d[3100]; bool v[3100]; vector<int> u; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= k; i++) { int t; scanf("%d", &t); if (a[t] == 0) { u.push_back(t - n); a[t] = 1; } } queue<int> q; q.push(1000); v[1000] = 1; while (!q.empty()) { int t = q.front(); q.pop(); for (auto &i : u) { if (t + i < 0 || t + i > 2000) continue; if (t + i == 1000) { printf("%d\n", d[t] + 1); return 0; } if (v[t + i] == 0) { d[t + i] = d[t] + 1; v[t + i] = 1; q.push(t + i); } } } puts("-1"); return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { int n, i, k, a[3 * 100000 + 5]; long long int res, tt; scanf("%d", &n); for (i = tt = 0; i < n; i++) scanf("%I64d", &a[i]), tt += a[i]; sort(a, a + n); res = tt; for (i = 0; i < n; i++) { res += a[i]; tt -= a[i]; res += tt; } printf("%I64d\n", res - a[n - 1]); return 0; }
2
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9, PI = acos(-1); const int N = 2e5 + 5, oo = 1e6, M = 1e5 + 5; int32_t main() { int n; cin >> n; int last[N]; for (int i = 0; i < N; i++) last[i] = 1e6; for (int i = 1; i <= n; i++) { int a; cin >> a; last[a] = i; } int mn = INT_MAX, ans; for (int i = 0; i < N; i++) { if (last[i] < mn) mn = last[i], ans = i; } cout << ans; return 0; }
1
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:5000000000") const long long mod = 1000000007; long long Inf = (long long)2e9; long long LINF = (long long)1e18 + 1e17; using namespace std; int main() { int n; double m; cin >> n >> m; double ans = Inf; for (int(i) = 0; (i) < n; (i)++) { double a, b; cin >> a >> b; ans = min(ans, m * a / b); } printf("%.10f", ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long mod = 998244353, mod2 = 1e9 + 9, p1 = 5807, p2 = 8573; const long long N = 2e4 + 5, M = 21; const long double pi = acos(-1); long long power(long long x, unsigned 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; } void MOD(long long *x) { if (*x >= mod) { *x -= (*x / mod) * mod; } } const int root = 15311432; const int root_1 = 469870224; const int root_pw = 1 << 23; void fft(vector<int> &a, bool invert) { int n = a.size(); for (int i = 1, j = 0; i < n; i++) { int bit = n >> 1; for (; j & bit; bit >>= 1) j ^= bit; j ^= bit; if (i < j) swap(a[i], a[j]); } for (int len = 2; len <= n; len <<= 1) { int wlen = invert ? root_1 : root; for (int i = len; i < root_pw; i <<= 1) wlen = (int)((1LL * wlen * wlen) % mod); for (int i = 0; i < n; i += len) { int w = 1; for (int j = 0; j < len / 2; j++) { int u = a[i + j], v = (int)((a[i + j + len / 2] * 1LL * w) % mod); a[i + j] = u + v < mod ? u + v : u + v - mod; a[i + j + len / 2] = u - v >= 0 ? u - v : u - v + mod; w = (int)((w * 1LL * wlen) % mod); } } } if (invert) { int n_1 = power(n, mod - 2, mod); for (int &x : a) x = (int)(x * 1LL * n_1 % mod); } } void multiply(vector<int> const &a, vector<int> const &b, vector<int> &res) { vector<int> fa(a.begin(), a.end()), fb(b.begin(), b.end()); int n = 1; while (n < a.size() + b.size()) n <<= 1; fa.resize(n); fb.resize(n); fft(fa, false); fft(fb, false); for (int i = 0; i < n; i++) { fa[i] = (int)((fa[i] * 1LL * fb[i]) % mod); } fft(fa, true); res.resize(a.size() + b.size() - 1); for (int i = 0; i < res.size(); ++i) { res[i] = fa[i]; } } vector<int> power(vector<int> &v, long long n) { vector<int> vec = {1}; while (n) { if (n & 1) { multiply(vec, v, vec); } n >>= 1; multiply(v, v, v); } return vec; } int32_t main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); ; int n, k; cin >> n >> k; vector<int> v(10); for (int i = 0; i < k; i++) { long long x; cin >> x; v[x] = 1; } vector<int> vec = power(v, n / 2); long long ans = 0; for (int i = 0; i < vec.size(); i++) { ans += 1LL * vec[i] * vec[i]; MOD(&ans); } cout << ans; return 0; }
8
#include <bits/stdc++.h> int main() { int n, s = 0; scanf("%d", &n); char a[200]; scanf("%s", &a); for (int i = 0; a[i] != '\0'; i++) { if (a[i] == 120) s++; } if (s == n / 2) { printf("0\n"); puts(a); } else { int c = s; if (s < n / 2) { for (int i = 0; i < n; i++) { if (c == n / 2) break; if (a[i] == 88) { a[i] = 120; c++; } } } else { for (int i = 0; i < n; i++) { if (c == n / 2) break; if (a[i] == 120) { a[i] = 88; c--; } } } int r = s > n / 2 ? s - n / 2 : n / 2 - s; printf("%d\n", r); puts(a); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 505; const int MAXM = MAXN * MAXN * 2; int n, m, que[MAXM], head, tail, pre[MAXN], tim, g[MAXN], tot, match[MAXN], fa[MAXN], type[MAXN], tic[MAXN]; int u[MAXN], v[MAXN]; struct edge { int to, next; } e[MAXM]; int findf(int x) { return fa[x] == x ? fa[x] : fa[x] = findf(fa[x]); } void add_edge(int from, int to) { e[++tot].to = to; e[tot].next = g[from]; g[from] = tot; } void init() { tim = 0; tot = 0; for (int i = 0; i <= n; ++i) { g[i] = 0; match[i] = 0; tic[i] = 0; } } int lca(int x, int y) { ++tim; while (1) { if (x) { x = findf(x); if (tic[x] == tim) { return x; } else { tic[x] = tim; x = pre[match[x]]; } } swap(x, y); } } void shrink(int x, int y, int p) { while (findf(x) != p) { pre[x] = y, y = match[x]; if (type[y] == 2) type[y] = 1, que[++tail] = y; if (findf(x) == x) fa[x] = p; if (findf(y) == y) fa[y] = p; x = pre[y]; } } bool aug(int s) { for (int i = 1; i <= n; ++i) { fa[i] = i; type[i] = 0; pre[i] = 0; } head = 0; tail = 1; que[tail] = s; type[s] = 1; int t = 0; while (head < tail) { int x = que[++head]; for (int i = g[x]; i; i = e[i].next) { if (findf(e[i].to) == findf(x) || type[e[i].to] == 2) continue; if (!type[e[i].to]) { type[e[i].to] = 2, pre[e[i].to] = x; if (!match[e[i].to]) { int last, tmp; for (int now = e[i].to; now; now = last) { tmp = pre[now]; last = match[tmp]; match[now] = tmp; match[tmp] = now; } return true; } type[match[e[i].to]] = 1; que[++tail] = match[e[i].to]; } else if (type[e[i].to] == 1) { int l = lca(x, e[i].to); shrink(x, e[i].to, l); shrink(e[i].to, x, l); } } } return false; } int EMA() { int ans = 0; for (int i = 1; i <= n; ++i) { ans += (!match[i] && aug(i)); } return ans; } long long n1, m1; int hash1(int x, int y) { return (x - 1) * m1 + y; } const int dx[12] = {-3, -2, -1, 0, 1, 2, 3, 2, 1, 0, -1, -2}; const int dy[12] = {0, 1, 2, 3, 2, 1, 0, -1, -2, -3, -2, -1}; bool check1(int x, int y) { if (x > n1 || y > m1 || x < 1 || y < 1) return false; return true; } int main() { while (scanf("%I64d %I64d", &n1, &m1) != EOF) { if (n1 <= 10 && m1 <= 10) { n = n1 * m1; init(); for (int i = 1; i <= n1; ++i) { for (int j = 1; j <= m1; ++j) { for (int k = 0; k < 12; ++k) { if (check1(i + dx[k], j + dy[k])) { add_edge(hash1(i, j), hash1(i + dx[k], j + dy[k])); add_edge(hash1(i + dx[k], j + dy[k]), hash1(i, j)); } } } } cout << EMA() * 2 << endl; } else { if (n1 > m1) swap(n1, m1); if (n1 == 1) { if (m1 % 6 <= 3) { printf("%I64d\n", m1 / 6 * 6); } else if (m1 % 6 == 4) { printf("%I64d\n", m1 / 6 * 6 + 2); } else if (m1 % 6 == 5) { printf("%I64d\n", m1 / 6 * 6 + 4); } continue; } if ((n1 & 1) && (m1 & 1)) { printf("%I64d\n", n1 * m1 - 1); } else { printf("%I64d\n", n1 * m1); } } } return 0; }
7
#include <bits/stdc++.h> using namespace std; int IsPrime(int k) { for (int u = 2; u * u <= k; u++) if (k % u == 0) return false; return (k != 1); } int printnum(int num, int base) { int curr = 0, ans[10]; while (num != 0) { ans[curr++] = num % base; num /= base; } for (int x = curr - 1; x >= 0; x--) cout << ans[x]; cout << " "; return 0; } int i, n, ans[1000000], a[1000000]; int x, y, z, x2, y2, z2, dx, dy, dz; int main() { cin >> n; for (i = 1; i < n; i++) { for (int j = 1; j < n; j++) printnum(i * j, n); cout << endl; } }
2
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 2e2 + 6e1; const int mod = 1e9 + 7; const double eps = 1e-5; const double pi = asin(1.0) * 2; const double e = 2.718281828459; void fre() { freopen("in.txt", "r", stdin); } int n, k; int p[maxn]; int input; int temp; int main() { scanf("%d%d", &n, &k); memset(p, -1, sizeof(p)); for (int i = 0; i < n; ++i) { scanf("%d", &input); if (p[input] == -1) { temp = (input - k + 1) < 0 ? 0 : (input - k + 1); for (int j = temp; j <= input; ++j) { if (p[j] == -1 || p[j] == j) { for (int l = j; l <= input; ++l) { p[l] = j; } break; } } } cout << p[input] << " "; } return 0; }
4
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:20000000") using namespace std; int ri() { int x; scanf("%d", &x); return x; } long long rll() { long long x; scanf("%I64d", &x); return x; } int mas[1000 * 1050]; void solve() { int n = ri(); mas[0] = 0; for (int i = (int)(1); i <= (int)(n); i++) { int x = i; int res = 2e9; vector<int> temp; while (x) temp.push_back(x % 10), x /= 10; for (int j = (int)(0); j <= (int)((int)temp.size() - 1); j++) if (temp[j] && i - temp[j] >= 0) res = min(mas[i - temp[j]], res); res++; mas[i] = res; } cout << mas[n]; } int main() { solve(); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); (n % 2 == 0) ? printf("%d", n / 2 - 1) : printf("%d", n / 2); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxm = (1 << 14) + 5; int pv[maxm], nw[maxm]; int r[maxm]; int num[maxm]; int a[maxm]; int root(int x) { return x == r[x] ? x : r[x] = root(r[x]); } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, ans = 0; cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m / 4; j++) { char c; cin >> c; int x = (c <= '9' ? c - '0' : 10 + c - 'A'); for (int k = 0; k < 4; k++) a[j * 4 + 3 - k] = ((x & (1 << k)) > 0); } for (int j = 0; j < m; j++) ans += a[j]; int cnt = *max_element(pv, pv + m); for (int j = 0; j < m; j++) { if (!a[j]) nw[j] = 0; else if (j && a[j - 1]) { nw[j] = nw[j - 1], ans--; } else nw[j] = ++cnt; } for (int j = 1; j <= cnt; j++) r[j] = j; for (int j = 0; j < m; j++) { if (!pv[j] || !a[j] || root(pv[j]) == root(nw[j])) continue; r[root(pv[j])] = root(nw[j]); ans--; } for (int j = 0; j <= m; j++) num[j] = 0; cnt = 0; for (int j = 0; j < m; j++) { if (!a[j]) { pv[j] = 0; continue; } int x = root(nw[j]); if (!num[x]) num[x] = ++cnt; pv[j] = num[x]; } } cout << ans << "\n"; return 0; }
8
#include <bits/stdc++.h> using namespace std; using lli = long long int; using pii = pair<int, int>; using vi = vector<int>; using vb = vector<bool>; using vvi = vector<vector<int>>; using vlli = vector<long long int>; using vpii = vector<pair<int, int>>; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; int a, b, c; cin >> a >> b >> c; int ans = 0; if (n == 2) { ans += min(a, b); } else if (n == 3) { ans += min(a, b); ans += min(a, min(b, c)); } else if (n > 3) { ans += min(a, b); ans += min(a, min(b, c)); ans += (n - 3) * min(a, min(b, c)); } cout << ans; return 0; }
0
#include <bits/stdc++.h> using ll = long long; using namespace std; const int INF = 1e9 + 7; void fail() { cout << "No\n"; exit(0); } struct DD { ll up, down; DD(int u, int d) : up(u), down(d) {} bool operator<(const DD& other) { return up * other.down < down * other.up; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<int> m(n); for (int& i : m) { cin >> i; } vector<int> noMark; int marks = 0; ll res = 0; for (int i = 0; i < n; ++i) { while (m[i] > marks) { ++marks; res += i - noMark.back(); noMark.pop_back(); } if (m[i] == marks) { ++marks; } else { noMark.push_back(i); } res += marks - m[i] - 1; } cout << res << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; vector<int> G[maxn]; int dis[maxn], n, ans = 0; void dfs(int x, int step) { dis[step]++; for (int i = 0; i < G[x].size(); i++) { int u = G[x][i]; dfs(u, step + 1); } } int main() { int m; scanf("%d", &n); for (int i = 2; i <= n; i++) { scanf("%d", &m); G[m].push_back(i); } dfs(1, 1); for (int i = 1; i <= n; i++) if (dis[i] & 1) ans++; printf("%d\n", ans); }
3
#include <bits/stdc++.h> using namespace std; long long w, m, carry; vector<int> vec; int main() { ios_base::sync_with_stdio(0); cin >> w >> m; while (m) { vec.push_back(m % w); m /= w; } for (auto &e : vec) { long long cur = (e + carry) % w; if (cur == 0 or cur == 1) { carry = (e + carry) / w; continue; } if ((cur + 1) % w == 0) { carry = (e + carry + 1) / w; } else { cout << "NO"; return 0; } } if (carry > 1) { cout << "NO"; return 0; } cout << "YES"; }
5
#include <bits/stdc++.h> using namespace std; #define int long long const int max_n = 200005, mod = 1000000007; int dp[max_n]; signed main(){ for(int i=0; i<9; i++)dp[i] = 2; dp[9] = 3; for(int i=10; i<max_n; i++){ dp[i] = (dp[i-9] + dp[i-10])%mod; } ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin>>t; while(t--){ int n, m; cin>>n>>m; int ans = 0; while(n > 0){ int x = n%10; ans += ((m + x < 10) ? 1 : dp[m + x - 10]); ans %= mod; n/=10; } cout<<ans<<"\n"; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long int n, k; cin >> n >> k; vector<long long int> c; for (long long int i = 1; (long long)i * i <= (n); i++) { if (n % i == 0) { c.push_back(i); if ((n / i) != i) c.push_back(n / i); } } sort(c.begin(), c.end()); if (k > c.size()) cout << -1; else cout << c.at(k - 1); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<string> a(n + 1), b(n + 1); vector<int> c(n + 1); vector<long long> f1(n + 1), f2(n + 1); for (int i = 1; i <= n; ++i) { cin >> c[i]; } for (int i = 1; i <= n; ++i) { cin >> a[i]; b[i] = a[i]; reverse(b[i].begin(), b[i].end()); } for (int i = 1; i <= n; ++i) { f1[i] = f2[i] = LLONG_MAX; if (f1[i - 1] != LLONG_MAX) { if (a[i] >= a[i - 1]) { f1[i] = min(f1[i], f1[i - 1]); } if (b[i] >= a[i - 1]) { f2[i] = min(f2[i], f1[i - 1] + c[i]); } } if (f2[i - 1] != LLONG_MAX) { if (a[i] >= b[i - 1]) { f1[i] = min(f1[i], f2[i - 1]); } if (b[i] >= b[i - 1]) { f2[i] = min(f2[i], f2[i - 1] + c[i]); } } } if (f1[n] == LLONG_MAX && f2[n] == LLONG_MAX) { cout << -1; } else { cout << min(f1[n], f2[n]); } }
4
#include <bits/stdc++.h> int ara[1001]; int main() { int a, b, t; scanf("%d", &t); for (int i = 1; i < t; i++) { scanf("%d%d", &a, &b); ara[a]++; ara[b]++; } int c = 0; for (int i = 1; i <= t; i++) { if (ara[i] == 1) c++; } printf("%d\n", c); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int x, t, a, b, da, db; cin >> x >> t >> a >> b >> da >> db; if (x == 0) { cout << "YES" << endl; return 0; } if ((x <= a && x > a - da * t && (a - x) % da == 0) || (x <= b && x > b - db * t && (b - x) % db == 0)) { cout << "YES" << endl; return 0; } for (int pa = a, pb = b - db * (t - 1); pa > a - da * t && pb <= b;) { if (pa + pb > x) { pa -= da; } else if (pa + pb < x) { pb += db; } else { cout << "YES" << endl; return 0; } } cout << "NO" << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() #define sz(x) (x).size() #define FT(x) get<0>(x) #define ST(x) get<1>(x) #define TT(x) get<2>(x) #define pb push_back #define endl '\n' #define F first #define S second typedef long double ld; typedef long long ll; typedef unsigned long long ull; const double PI = 3.1415926535897; const ll INF_LL = 1e18 + 7; const double EPS = 1e-5; const int INF_int = 1e9 + 7; const int maxn = 1e6 + 10; const int mod2 = 1e9 + 9; const int mod3 = 1e9 + 33; int p; const int K = 1e6; void solve() { int n; cin >> n; string s; cin >> s; int z = 0, o = 0; if (s[0] != '1' or s[n-1] != '1'){ cout << "NO" << endl; return; } string a = "", b = ""; for (int i = 0; i < n; ++i){ if (s[i] == '1') o++; else z++; } if (z % 2 || o % 2){ cout << "NO" << endl; return; } int flag = 0; int curo = 1; for (int i = 0; i < n; ++i){ if (s[i] == '1' && curo > o/2){ a += ')', b += ')'; continue; } if (s[i] == '1'){ curo++; a += '(', b += '('; continue; } if (flag) a += '(', b += ')', flag = 0; else a += ')', b += '(', flag = 1; } cout << "YES" << endl << a << endl << b << endl; } int32_t main() { //freopen("points.in","r", stdin); //freopen("points.out","w", stdout); cout.precision(15); ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); int t = 1; cin >> t; while (t--) { solve(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; long long mod = pow(10, 9) + 7; int gcdExtended(int a, int b, int *x, int *y); int modInverse(int b, int m) { int x, y; int g = gcdExtended(b, m, &x, &y); if (g != 1) return -1; return (x % m + m) % m; } int gcdExtended(int a, int b, int *x, int *y) { if (a == 0) { *x = 0, *y = 1; return b; } int x1, y1; int gcd = gcdExtended(b % a, a, &x1, &y1); *x = y1 - (b / a) * x1; *y = x1; return gcd; } string decimalToBinary(int n) { string s = bitset<64>(n).to_string(); const auto loc1 = s.find('1'); if (loc1 != string::npos) return s.substr(loc1); return "0"; } int binaryToDecimal(int n) { int num = n; int dec_value = 0; int base = 1; int temp = num; while (temp) { int last_digit = temp % 10; temp = temp / 10; dec_value += last_digit * base; base = base * 2; } return dec_value; } long long factorial(long long n); long long factorial(long long n) { long long ans = 1; while (n != 1) { ans = ((ans % mod) * (n % mod)) % mod; n--; } return ans; } unsigned long long sumbincoef(int N, int k) { unsigned long long bincoef = 1, sum = 1; int i; for (i = 1; i < k; ++i) { bincoef = (bincoef * (N - i + 1) / i); sum += bincoef; } return sum; } bool is_prime(long long n) { if (n == 1) { return false; } long long i = 2; while (i * i <= n) { if (n % i == 0) { return false; } i += 1; } return true; } int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } int nCrModp(int n, int r, int p) { if (r > n - r) r = n - r; int C[r + 1]; memset(C, 0, sizeof(C)); C[0] = 1; for (int i = 1; i <= n; i++) { for (int j = min(i, r); j > 0; j--) C[j] = (C[j] + C[j - 1]) % p; } return C[r]; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); mt19937 rng( (unsigned int)chrono::steady_clock::now().time_since_epoch().count()); int t = 1; while (t > 0) { int k; cin >> k; string s; cin >> s; long long ans = 0; int cnt = 0; int index = -1; if (k == 0) { int val = 0; vector<long long> d; for (int i = 0; i < s.size(); i++) { if (s[i] == '0') { val++; } } int count = 1; for (int i = 0; i <= s.size() - 1; i++) { if (s[i] == s[i + 1]) { count++; if (i == s.size() - 1) { d.push_back(count); } } else { d.push_back(count); count = 1; if (i == s.size() - 1) { d.push_back(count); } } } ans = val; if (s[0] == '0') { for (int i = 0; i < d.size(); i++) { if (i % 2 == 0) ans += d[i] * (d[i] - 1) / 2; } } else { for (int i = 1; i < d.size(); i++) { if (i % 2 != 0) ans += d[i] * (d[i] - 1) / 2; } } cout << ans << endl; } else { for (int i = 0; i < s.size() && cnt <= k; i++) { if (s[i] == '1') { cnt++; } if (cnt == k) { index = i; break; } } if (index == -1) { cout << 0 << endl; } else { int index2 = 0; int raj = 101; while (raj != -1) { auto it = find(s.begin() + index2, s.end(), '1'); auto it2 = find(s.begin() + index + 1, s.end(), '1'); long long ele = it - s.begin() - index2 + 1; long long elem = it2 - s.begin() - index; ans += ele * elem; index2 = it - s.begin() + 1; index = it2 - s.begin(); if (it2 == s.end()) { raj = -1; } } cout << ans << endl; } } t--; } }
4
#include <bits/stdc++.h> const int MOD = 1e9 + 7; using namespace std; int main() { long long t, n, m, a; cin >> t; for (int jjj = 0; jjj < t; jjj++) { long long cnt = 0; string s; vector<pair<string, long long> > v; cin >> n >> s; string t = s; for (int i = 0; i < n; i++) { cnt = 0; for (int j = i; j < n; j++) { t[cnt] = s[j]; cnt++; } if (i % 2 != n % 2) { for (int j = i - 1; j >= 0; j--) { t[cnt] = s[j]; cnt++; } } else { for (int j = 0; j < i; j++) { t[cnt] = s[j]; cnt++; } } v.push_back(make_pair(t, i + 1)); } sort(v.begin(), v.end()); cout << v[0].first << "\n" << v[0].second << "\n"; } }
3
#include <bits/stdc++.h> using namespace std; const long long inf = 1e18 + 9; const int mod = 1e9 + 7; string s1[] = {"cool", "not bad", "don't touch me!", "don't think so", "great"}; string s2[] = {"terrible", "worse", "go die in a hole", "are you serious?", "don't even", "no way"}; int main() { for (int i = 0; i < 10; i++) { cout << i << endl; string s; getline(cin, s); for (int j = 0; j < 5; j++) { if (s == s1[j]) { cout << "normal" << endl, exit(0); } } for (int j = 0; j < 6; j++) { if (s == s2[j]) { cout << "grumpy" << endl, exit(0); } } } }
5
#include <bits/stdc++.h> using namespace std; string arr[] = { "111111101010101111100101001111111", "100000100000000001010110001000001", "101110100110110000011010001011101", "101110101011001001111101001011101", "101110101100011000111100101011101", "100000101010101011010000101000001", "111111101010101010101010101111111", "000000001111101111100111100000000", "100010111100100001011110111111001", "110111001111111100100001000101100", "011100111010000101000111010001010", "011110000110001111110101100000011", "111111111111111000111001001011000", "111000010111010011010011010100100", "101010100010110010110101010000010", "101100000101010001111101000000000", "000010100011001101000111101011010", "101001001111101111000101010001110", "101101111111000100100001110001000", "000010011000100110000011010000010", "001101101001101110010010011011000", "011101011010001000111101010100110", "111010100110011101001101000001110", "110001010010101111000101111111000", "001000111011100001010110111110000", "000000001110010110100010100010110", "111111101000101111000110101011010", "100000100111010101111100100011011", "101110101001010000101000111111000", "101110100011010010010111111011010", "101110100100011011110110101110000", "100000100110011001111100111100000", "111111101101000101001101110010001"}; int main(int argc, char *argv[]) { int a1, a2; cin >> a1 >> a2; cout << arr[a1][a2] << endl; return EXIT_SUCCESS; }
3
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 2; long long solve() { long long n; cin >> n; long long a[n]; map<long long, long long> mp; long long ans = 0; for (long long i = 0; i < n; i++) { cin >> a[i]; mp[a[i] - i - 1] += a[i]; ans = max(ans, mp[a[i] - i - 1]); } cout << ans; return 0; } int32_t main() { long long t = 1; while (t--) { solve(); } }
3
#include <bits/stdc++.h> using namespace std; const long long INF64 = 3e18; const int mod = (int)1e9 + 7; const int MAX_N = 100000 + 5; long long binp(long long a, long long b) { if (b == 0) return 1; long long ans = binp(a, b / 2); long long tmp = (ans * ans); if (b % 2) return ((tmp * a)); return ((tmp)); } void display(vector<long long> v) { for (auto x : v) cout << x << " "; cout << "\n"; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } void qr(long long i, long long j) { cout << "? " << i << " " << j << '\n'; cout.flush(); } void here(long long a) { cout << "here " << a << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; for (long long _loop = 0; _loop < t; _loop++) { long long x, y, k; cin >> x >> y >> k; long long a = (k * (y + 1) - 1) / (x - 1) + ((k * (y + 1) - 1) % (x - 1) != 0) + k; cout << a << '\n'; } }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 100200; int main() { char ch[20]; int ans = 0; for (int i = 0; i < 8; i++) { scanf("%s", ch); for (int j = 0; j < 8; j++) { if (ch[j] == 'Q') ans += 9; else if (ch[j] == 'q') ans -= 9; if (ch[j] == 'R') ans += 5; else if (ch[j] == 'r') ans -= 5; if (ch[j] == 'B') ans += 3; else if (ch[j] == 'b') ans -= 3; if (ch[j] == 'N') ans += 3; else if (ch[j] == 'n') ans -= 3; if (ch[j] == 'P') ans += 1; else if (ch[j] == 'p') ans -= 1; } } if (ans > 0) puts("White"); else if (ans == 0) puts("Draw"); else puts("Black"); return 0; }
0
#include <bits/stdc++.h> using namespace std; long long int max(long long int a, long long int b) { if (a > b) return a; return b; } long long int min(long long int a, long long int b) { if (a < b) return a; return b; } long long int msb(long long int n) { long long int ans; for (long long int i = 31; i >= 0; i--) if ((1LL << i) & (n)) { ans = i; break; } return ans; } long long int power(long long int x, long long int y, long long int p) { long long int res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long long int modFact(long long int n, long long int p) { if (n >= p) return 0; long long int result = 1; for (long long int i = 1; i <= n; i++) result = (result * i) % p; return result; } long long int modInverse(long long int n, long long int p) { return power(n, p - 2, p); } long long int nCrModPFermat(long long int n, long long int r, long long int p) { if (r == 0) return 1; long long int fac[n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % p; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } vector<long long int> primes; void sieve(long long int n) { bool prime[n + 1]; memset(prime, true, sizeof(prime)); for (long long int p = 2; p <= n; p++) { if (prime[p] == true) { for (long long int i = p * p; i <= n; i += p) prime[i] = false; } } for (long long int p = 2; p <= n; p++) if (prime[p]) primes.push_back(p); } vector<pair<int, int>> find_exponents(int n) { vector<pair<int, int>> exp; for (int i = 2; i <= n; i++) { if (n % i == 0) { int cnt = 0; while (n % i == 0) { cnt++; n /= i; } exp.push_back({i, cnt}); } } if (n != 1) exp.push_back({n, 1}); return exp; } vector<long long int> adj[200001], rev[100001]; bool vis[200001], recur[100001]; long long int dp[200001], sum[200001], leaf[200001], val[200001]; void init(long long int n) { for (long long int i = 0; i <= n; i++) { adj[i].clear(); sum[i] = 0; vis[i] = false; leaf[i] = 0; dp[i] = 0; } } void dfs(int node) { if (adj[node].size() == 0) leaf[node] = 1; sum[node] = val[node]; dp[node] = 0; vis[node] = 1; for (auto child : adj[node]) { if (!vis[child]) { dfs(child); leaf[node] += leaf[child]; sum[node] += sum[child]; dp[node] = max(dp[node], dp[child]); } } dp[node] = max(dp[node], ceil((float)sum[node] / (float)leaf[node])); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int n; cin >> n; init(n); for (int i = 2; i <= n; i++) { int x; cin >> x; adj[x].push_back(i); } for (int i = 1; i <= n; i++) cin >> val[i]; dfs(1); cout << dp[1] << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; long long sum[1000007]; int cnta; long long Pow(long long x, long long y) { long long res = 1; while (y > 0) { if (y & 1) res = (res * x) % 1000000007; x = (x * x) % 1000000007; y >>= 1; } return res; } void solve() { sum[0] = 1; for (int i = 1; i < 1000007; i++) { sum[i] = (sum[i - 1] + Pow(2, i)) % 1000000007; } } int main() { solve(); string s; cin >> s; long long ans = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == 'a') cnta++; else if (cnta) { ans = (ans + sum[cnta - 1]) % 1000000007; } } cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const int32_t M = 1e9 + 7; const int32_t MM = 998244353; void solve() { int n; cin >> n; vector<pair<long long int, long long int>> v(n); for (int i = 0; i < n; i++) { cin >> v[i].first; v[i].second = i + 1; } sort((v).begin(), (v).end()); if (n == 1) { cout << 1; return; } else if (n == 2 || n == 3) { cout << 1; return; } map<long long int, long long int> mp; for (int i = n - 1; i >= 2; i--) { mp[v[i].first - v[i - 1].first]++; } if (mp.size() == 1) { cout << v[0].second; return; } map<long long int, long long int>::iterator itr, it; mp[v[2].first - v[1].first]--; if (mp[v[2].first - v[1].first] == 0) { mp.erase(v[2].first - v[1].first); } itr = mp.begin(); if (mp.size() == 1 && (v[2].first - v[0].first) == itr->first) { cout << v[1].second; return; } map<long long int, long long int> tmp; tmp[v[1].first - v[0].first]++; for (int i = 2; i < n; i++) { if (i + 1 < n) { mp[v[i + 1].first - v[i].first]--; if (mp[v[i + 1].first - v[i].first] == 0) mp.erase(v[i + 1].first - v[i].first); } itr = tmp.begin(), it = mp.begin(); if (tmp.size() == 1 && mp.size() == 1 && itr->first == it->first && (v[i + 1].first - v[i - 1].first) == itr->first) { cout << v[i].second; return; } else if (tmp.size() == 1 && mp.size() == 0) { itr = tmp.begin(); if (i == n - 2 && (v[i + 1].first - v[i - 1].first) == itr->first) { cout << v[i].second; return; } else if (i == n - 1) { cout << v[i].second; return; } } tmp[v[i].first - v[i - 1].first]++; } cout << -1; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); int t = 1; while (t--) { solve(); } cerr << "Time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; long long int checkyear(char ch1, char ch2, char ch3, char ch4) { if (ch1 == '2' && ch2 == '0' && ch3 == '1' && (ch4 == '3' || ch4 == '4' || ch4 == '5')) return 1; else return 0; } long long int checkmonth(char ch1, char ch2) { if (ch1 == '0' && ch2 == '2') return 1; else if (ch1 == '0' && (ch2 == '1' || ch2 == '3' || ch2 == '5' || ch2 == '7' || ch2 == '8')) return 3; else if ((ch1 == '1') && (ch2 == '0' || ch2 == '2')) return 3; else if ((ch1 == '0') && (ch2 == '4' || ch2 == '6' || ch2 == '9')) return 2; else if (ch1 == '1' && ch2 == '1') return 2; else return 0; } long long int checkday(char ch1, char ch2, long long int month) { if (month == 1) { if ((ch1 == '0') && (ch2 == '1' || ch2 == '2' || ch2 == '3' || ch2 == '4' || ch2 == '5' || ch2 == '6' || ch2 == '7' || ch2 == '8' || ch2 == '9')) return 1; else if ((ch1 == '1') && (ch2 == '0' || ch2 == '1' || ch2 == '2' || ch2 == '3' || ch2 == '4' || ch2 == '5' || ch2 == '6' || ch2 == '7' || ch2 == '8' || ch2 == '9')) return 1; else if ((ch1 == '2') && (ch2 == '0' || ch2 == '1' || ch2 == '2' || ch2 == '3' || ch2 == '4' || ch2 == '5' || ch2 == '6' || ch2 == '7' || ch2 == '8')) return 1; else return 0; } else if (month == 2) { if ((ch1 == '0') && (ch2 == '1' || ch2 == '2' || ch2 == '3' || ch2 == '4' || ch2 == '5' || ch2 == '6' || ch2 == '7' || ch2 == '8' || ch2 == '9')) { return 1; } else if ((ch1 == '1') && (ch2 == '0' || ch2 == '1' || ch2 == '2' || ch2 == '3' || ch2 == '4' || ch2 == '5' || ch2 == '6' || ch2 == '7' || ch2 == '8' || ch2 == '9')) { return 1; } else if ((ch1 == '2') && (ch2 == '0' || ch2 == '1' || ch2 == '2' || ch2 == '3' || ch2 == '4' || ch2 == '5' || ch2 == '6' || ch2 == '7' || ch2 == '8' || ch2 == '9')) { return 1; } else if ((ch1 == '3') && (ch2 == '0')) { return 1; } else return 0; } else if (month == 3) { if ((ch1 == '0') && (ch2 == '1' || ch2 == '2' || ch2 == '3' || ch2 == '4' || ch2 == '5' || ch2 == '6' || ch2 == '7' || ch2 == '8' || ch2 == '9')) { return 1; } else if ((ch1 == '1') && (ch2 == '0' || ch2 == '1' || ch2 == '2' || ch2 == '3' || ch2 == '4' || ch2 == '5' || ch2 == '6' || ch2 == '7' || ch2 == '8' || ch2 == '9')) { return 1; } else if ((ch1 == '2') && (ch2 == '0' || ch2 == '1' || ch2 == '2' || ch2 == '3' || ch2 == '4' || ch2 == '5' || ch2 == '6' || ch2 == '7' || ch2 == '8' || ch2 == '9')) { return 1; } else if ((ch1 == '3') && (ch2 == '0' || ch2 == '1')) { return 1; } else return 0; } } int main() { char a[100003], b[1000]; string s; scanf("%s", a); map<string, long long int> m; map<string, long long int>::iterator it, pos; long long int len, i = 0, c, d, j = 0, year, month, day; len = strlen(a); for (i = 0; i <= (len - 10); i++) { j = 0; year = checkyear(a[i + 6], a[i + 7], a[i + 8], a[i + 9]); if (year) { month = checkmonth(a[i + 3], a[i + 4]); if (month) { day = checkday(a[i], a[i + 1], month); if (day && a[i + 2] == '-' && a[i + 5] == '-') { b[0] = a[i]; b[1] = a[i + 1]; b[2] = a[i + 2]; b[3] = a[i + 3]; b[4] = a[i + 4]; b[5] = a[i + 5]; b[6] = a[i + 6]; b[7] = a[i + 7]; b[8] = a[i + 8]; b[9] = a[i + 9]; b[10] = '\0'; s = b; m[s]++; } } } } long long int max = 0; it = m.begin(); for (; it != m.end(); it++) { if ((it->second) > max) { max = it->second; pos = it; } } cout << pos->first; return 0; }
4
#include <bits/stdc++.h> using namespace std; vector<int> D[100005]; long long dfffs[100006]; const long long p = 1e9 + 7; void dfs_1(int u, int v) { dfffs[u] = 1; int odw = 0; for (int i = 0; i < D[u].size(); i++) { int f = D[u][i]; if (f != v) { odw = 1; dfs_1(f, u); dfffs[u] = (dfffs[u] * dfffs[f]) % p; } } if (odw == 1) { dfffs[u] = (2 * dfffs[u]) % p; } } int main() { long long n, x, y; long long liscie = 0; long long nielisc = 1; cin >> n; for (int i = 0; i < n - 1; i++) { cin >> x; cin >> y; D[x].push_back(y); D[y].push_back(x); } if (D[1].size() != 1) { dfs_1(1, 0); nielisc = dfffs[1]; } else { int u = D[1][0]; dfs_1(u, 0); nielisc = dfffs[u]; } for (int i = 0; i <= n - 1; i++) { if (D[i + 1].size() == 1) liscie++; } long long ans = ((n + liscie) * nielisc) % p; if (n > 2) cout << ans; if (n == 2) cout << 4; if (n == 1) cout << 1; return 0; }
6
#include <bits/stdc++.h> using namespace std; const int maxn = 100100; const int INF = 1E9; char s[10 * maxn]; char st[10 * maxn]; int top = 0; bool open(char c) { if (c == '{') return 1; if (c == '[') return 1; if (c == '(') return 1; if (c == '<') return 1; return 0; } bool eq(char c, char d) { if (c == '{' && d == '}') return 1; if (c == '(' && d == ')') return 1; if (c == '[' && d == ']') return 1; if (c == '<' && d == '>') return 1; return 0; } int solve() { gets(s); int n = strlen(s); int ans = 0; for (int i = 0; i < n; i++) if (open(s[i])) st[top++] = s[i]; else { if (top == 0) { printf("Impossible\n"); return 0; } top--; if (!eq(st[top], s[i])) ans++; } if (top != 0) printf("Impossible\n"); else printf("%d\n", ans); } int main() { ios_base::sync_with_stdio(0); solve(); return 0; }
3
#include <bits/stdc++.h> const int N = 35; bool vis[N]; long long ans = 0; int n, u, r, p[N], K[N], b[N]; long long a[N]; void dfs(int idx) { if ((u - idx) % 2 == 0) { long long get = 0; for (int i = 1; i <= n; ++i) get += (long long)a[i] * K[i]; if (get > ans) ans = get; } if (u == idx) return; long long use[n + 1]; for (int i = 1; i <= n; ++i) use[i] = a[i]; if (idx == 0 || vis[idx - 1] == 0) { vis[idx] = 1; for (int i = 1; i <= n; ++i) a[i] = a[i] ^ b[i]; dfs(idx + 1); vis[idx] = 0; } for (int i = 1; i <= n; ++i) a[i] = use[p[i]] + r; dfs(idx + 1); for (int i = 1; i <= n; ++i) a[i] = use[i]; } int main() { scanf("%d%d%d", &n, &u, &r); for (int i = 1; i <= n; ++i) std::cin >> a[i]; for (int i = 1; i <= n; ++i) scanf("%d", &b[i]); for (int i = 1; i <= n; ++i) scanf("%d", &K[i]); for (int i = 1; i <= n; ++i) scanf("%d", &p[i]); ans = -(1ll << 60); memset(vis, 0, sizeof vis); dfs(0); std::cout << ans << std::endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; const long double pi = 3.14159265358979323846; long long MOD = 998244353; const char nl = '\n'; const long long inf = 1e15; long long mul(long long x, long long y) { return (1ll * (x % MOD) * (y % MOD)); } long long modpow(long long x, long long y) { long long z = 1; while (y > 0) { if (y % 2) z = mul(z, x); x = mul(x, x); y /= 2; } return z; } long long power(long long x, long long y) { long long z = 1; while (y > 0) { if (y % 2) z = z * x; x = x * x; y /= 2; } return z; } long long gcd(long long a, long long b) { if (a < b) return gcd(b, a); if (b == 0) return a; return gcd(b, a % b); } long long min(long long a, long long b) { if (a < b) return a; return b; } long long max(long long a, long long b) { if (a > b) return a; return b; } long long sq(long long a) { long long ans = (1ll * a * a); return ans; } const long long N = 1e5 + 1; long long n, s; long long ps[N]; long long a[N]; long long chk(long long x) { vector<long long> v; for (long long i = 0; i < n; i++) { v.push_back(a[i] + x * (i + 1)); } sort(((v).begin()), ((v).end())); for (long long i = 0; i < n; i++) { v[i] = v[i] + (i ? v[i - 1] : 0); } long long pay = v[x - 1]; return pay; } void solve() { cin >> n >> s; for (long long i = 0; i < n; i++) cin >> a[i]; for (long long i = 0; i < n; i++) { ps[i] = a[i] + (i ? ps[i - 1] : 0); } long long l = 1, r = n; long long ans = -1, tp = -1; while (l <= r) { long long mid = l + (r - l) / 2; if (chk(mid) <= s) l = mid + 1, ans = mid, tp = chk(mid); else r = mid - 1; } if (ans == -1) { cout << 0 << " " << 0 << nl; } else { cout << ans << " " << tp << nl; } } int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); long long t = 1; while (t--) { solve(); } cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n"; }
3
#include <bits/stdc++.h> using namespace std; int main() { long long b; cin >> b; long long cnt = 0; for (long long i = 1; i * i <= b; i++) { if (b % i == 0) { if (i * i == b) { cnt++; } else cnt += 2; } } cout << cnt; }
2
#include <bits/stdc++.h> using namespace std; const double pi = acos(0.0) * 2.0; const double eps = 1e-12; const int step[8][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; template <class T> inline T abs1(T a) { return a < 0 ? -a : a; } template <class T> inline T max1(T a, T b) { return a > b ? a : b; } template <class T> inline T max1(T a, T b, T c) { return max1(max1(a, b), c); } template <class T> inline T max1(T a, T b, T c, T d) { return max1(max1(a, b, c), d); } template <class T> inline T max1(T a, T b, T c, T d, T e) { return max1(max1(a, b, c, d), e); } template <class T> inline T min1(T a, T b) { return a < b ? a : b; } template <class T> inline T min1(T a, T b, T c) { return min1(min1(a, b), c); } template <class T> inline T min1(T a, T b, T c, T d) { return min1(min1(a, b, c), d); } template <class T> inline T min1(T a, T b, T c, T d, T e) { return min1(min1(a, b, c, d), e); } inline int jud(double a, double b) { if (abs(a) < eps && abs(b) < eps) return 0; else if (abs1(a - b) / abs1(a) < eps) return 0; if (a < b) return -1; return 1; } template <typename t> inline int jud(t a, t b) { if (a < b) return -1; if (a == b) return 0; return 1; } template <typename it, typename t1> inline int find(t1 val, it a, int na, bool f_small = 1, bool f_lb = 1) { int be = 0, en = na - 1; if (*a <= *(a + na - 1)) { if (f_lb == 0) while (be < en) { int mid = (be + en + 1) / 2; if (jud(*(a + mid), val) != 1) be = mid; else en = mid - 1; } else while (be < en) { int mid = (be + en) / 2; if (jud(*(a + mid), val) != -1) en = mid; else be = mid + 1; } if (f_small && jud(*(a + be), val) == 1) be--; if (!f_small && jud(*(a + be), val) == -1) be++; } else { if (f_lb) while (be < en) { int mid = (be + en + 1) / 2; if (jud(*(a + mid), val) != -1) be = mid; else en = mid - 1; } else while (be < en) { int mid = (be + en) / 2; if (jud(*(a + mid), val) != 1) en = mid; else be = mid + 1; } if (!f_small && jud(*(a + be), val) == -1) be--; if (f_small && jud(*(a + be), val) == 1) be++; } return be; } template <class t> inline void vout1(t arr) { for (int i = 0; i < (int)arr.size(); i++) cerr << arr[i] << ' '; cerr << endl; } template <class t> inline void vout2(t arr) { for (int i = 0; i < (int)arr.size(); i++) { for (int j = 0; j < (int)arr[0].size(); j++) cerr << arr[i][j] << ' '; cerr << endl; } } template <class T> inline T lowb(T num) { return num & (-num); } inline int bitnum(unsigned int nValue) { return __builtin_popcount(nValue); } inline int bitnum(int nValue) { return __builtin_popcount(nValue); } inline int bitnum(unsigned long long nValue) { return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32); } inline int bitnum(long long nValue) { return __builtin_popcount(nValue) + __builtin_popcount(nValue >> 32); } inline int bitmaxl(unsigned int a) { if (a == 0) return 0; return 32 - __builtin_clz(a); } inline int bitmaxl(int a) { if (a == 0) return 0; return 32 - __builtin_clz(a); } inline int bitmaxl(unsigned long long a) { int temp = a >> 32; if (temp) return 32 - __builtin_clz(temp) + 32; return bitmaxl(int(a)); } inline int bitmaxl(long long a) { int temp = a >> 32; if (temp) return 32 - __builtin_clz(temp) + 32; return bitmaxl(int(a)); } long long pow(long long n, long long m, long long mod = 0) { if (m < 0) return 0; long long ans = 1; long long k = n; while (m) { if (m & 1) { ans *= k; if (mod) ans %= mod; } k *= k; if (mod) k %= mod; m >>= 1; } return ans; } const int maxn = 2010; int mp[maxn][maxn]; int n; int main() { ios_base::sync_with_stdio(0); scanf("%d", &n); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) scanf("%d", mp[i] + j); int ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { ans += mp[i][j] * mp[j][i]; ans &= 1; } } ans &= 1; int nq; scanf("%d", &nq); for (int i = 0; i < nq; i++) { int cate, no; scanf("%d", &cate); if (cate == 3) { printf("%d", ans); } else { scanf("%d", &no); ans ^= 1; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; using Int = long long; void solve() { Int n; cin >> n; vector<Int> a(n); for (Int i = 0; i < n; i++) cin >> a[i]; vector<Int> cnt(n + 1); vector<unordered_map<Int, Int>> nxt(n + 1); Int ans = 0; for (Int i = n - 1; i >= 0; i--) { if (nxt[i + 1].count(a[i])) { Int r = nxt[i + 1][a[i]]; cnt[i] = cnt[r] + 1; ans += cnt[i]; nxt[i] = move(nxt[r]); } nxt[i][a[i]] = i + 1; } cout << ans << endl; } signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); Int T; cin >> T; while (T--) solve(); }
9
#include <bits/stdc++.h> using namespace std; int main() { int k; cin >> k; switch (k) { case 0: cout << "+------------------------+" << endl; cout << "|#.#.#.#.#.#.#.#.#.#.#.|D|)" << endl; cout << "|#.#.#.#.#.#.#.#.#.#.#.|.|" << endl; cout << "|#.......................|" << endl; cout << "|#.#.#.#.#.#.#.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 1: cout << "+------------------------+" << endl; cout << "|O.#.#.#.#.#.#.#.#.#.#.|D|)" << endl; cout << "|#.#.#.#.#.#.#.#.#.#.#.|.|" << endl; cout << "|#.......................|" << endl; cout << "|#.#.#.#.#.#.#.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 2: cout << "+------------------------+" << endl; cout << "|O.#.#.#.#.#.#.#.#.#.#.|D|)" << endl; cout << "|O.#.#.#.#.#.#.#.#.#.#.|.|" << endl; cout << "|#.......................|" << endl; cout << "|#.#.#.#.#.#.#.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 3: cout << "+------------------------+" << endl; cout << "|O.#.#.#.#.#.#.#.#.#.#.|D|)" << endl; cout << "|O.#.#.#.#.#.#.#.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|#.#.#.#.#.#.#.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 4: cout << "+------------------------+" << endl; cout << "|O.#.#.#.#.#.#.#.#.#.#.|D|)" << endl; cout << "|O.#.#.#.#.#.#.#.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.#.#.#.#.#.#.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 5: cout << "+------------------------+" << endl; cout << "|O.O.#.#.#.#.#.#.#.#.#.|D|)" << endl; cout << "|O.#.#.#.#.#.#.#.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.#.#.#.#.#.#.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 6: cout << "+------------------------+" << endl; cout << "|O.O.#.#.#.#.#.#.#.#.#.|D|)" << endl; cout << "|O.O.#.#.#.#.#.#.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.#.#.#.#.#.#.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 7: cout << "+------------------------+" << endl; cout << "|O.O.#.#.#.#.#.#.#.#.#.|D|)" << endl; cout << "|O.O.#.#.#.#.#.#.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.#.#.#.#.#.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 8: cout << "+------------------------+" << endl; cout << "|O.O.O.#.#.#.#.#.#.#.#.|D|)" << endl; cout << "|O.O.#.#.#.#.#.#.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.#.#.#.#.#.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 9: cout << "+------------------------+" << endl; cout << "|O.O.O.#.#.#.#.#.#.#.#.|D|)" << endl; cout << "|O.O.O.#.#.#.#.#.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.#.#.#.#.#.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 10: cout << "+------------------------+" << endl; cout << "|O.O.O.#.#.#.#.#.#.#.#.|D|)" << endl; cout << "|O.O.O.#.#.#.#.#.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.#.#.#.#.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 11: cout << "+------------------------+" << endl; cout << "|O.O.O.O.#.#.#.#.#.#.#.|D|)" << endl; cout << "|O.O.O.#.#.#.#.#.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.#.#.#.#.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 12: cout << "+------------------------+" << endl; cout << "|O.O.O.O.#.#.#.#.#.#.#.|D|)" << endl; cout << "|O.O.O.O.#.#.#.#.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.#.#.#.#.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 13: cout << "+------------------------+" << endl; cout << "|O.O.O.O.#.#.#.#.#.#.#.|D|)" << endl; cout << "|O.O.O.O.#.#.#.#.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.O.#.#.#.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 14: cout << "+------------------------+" << endl; cout << "|O.O.O.O.O.#.#.#.#.#.#.|D|)" << endl; cout << "|O.O.O.O.#.#.#.#.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.O.#.#.#.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 15: cout << "+------------------------+" << endl; cout << "|O.O.O.O.O.#.#.#.#.#.#.|D|)" << endl; cout << "|O.O.O.O.O.#.#.#.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.O.#.#.#.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 16: cout << "+------------------------+" << endl; cout << "|O.O.O.O.O.#.#.#.#.#.#.|D|)" << endl; cout << "|O.O.O.O.O.#.#.#.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.O.O.#.#.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 17: cout << "+------------------------+" << endl; cout << "|O.O.O.O.O.O.#.#.#.#.#.|D|)" << endl; cout << "|O.O.O.O.O.#.#.#.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.O.O.#.#.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 18: cout << "+------------------------+" << endl; cout << "|O.O.O.O.O.O.#.#.#.#.#.|D|)" << endl; cout << "|O.O.O.O.O.O.#.#.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.O.O.#.#.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 19: cout << "+------------------------+" << endl; cout << "|O.O.O.O.O.O.#.#.#.#.#.|D|)" << endl; cout << "|O.O.O.O.O.O.#.#.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.O.O.O.#.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 20: cout << "+------------------------+" << endl; cout << "|O.O.O.O.O.O.O.#.#.#.#.|D|)" << endl; cout << "|O.O.O.O.O.O.#.#.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.O.O.O.#.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 21: cout << "+------------------------+" << endl; cout << "|O.O.O.O.O.O.O.#.#.#.#.|D|)" << endl; cout << "|O.O.O.O.O.O.O.#.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.O.O.O.#.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 22: cout << "+------------------------+" << endl; cout << "|O.O.O.O.O.O.O.#.#.#.#.|D|)" << endl; cout << "|O.O.O.O.O.O.O.#.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.O.O.O.O.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 23: cout << "+------------------------+" << endl; cout << "|O.O.O.O.O.O.O.O.#.#.#.|D|)" << endl; cout << "|O.O.O.O.O.O.O.#.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.O.O.O.O.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 24: cout << "+------------------------+" << endl; cout << "|O.O.O.O.O.O.O.O.#.#.#.|D|)" << endl; cout << "|O.O.O.O.O.O.O.O.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.O.O.O.O.#.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 25: cout << "+------------------------+" << endl; cout << "|O.O.O.O.O.O.O.O.#.#.#.|D|)" << endl; cout << "|O.O.O.O.O.O.O.O.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.O.O.O.O.O.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 26: cout << "+------------------------+" << endl; cout << "|O.O.O.O.O.O.O.O.O.#.#.|D|)" << endl; cout << "|O.O.O.O.O.O.O.O.#.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.O.O.O.O.O.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 27: cout << "+------------------------+" << endl; cout << "|O.O.O.O.O.O.O.O.O.#.#.|D|)" << endl; cout << "|O.O.O.O.O.O.O.O.O.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.O.O.O.O.O.#.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 28: cout << "+------------------------+" << endl; cout << "|O.O.O.O.O.O.O.O.O.#.#.|D|)" << endl; cout << "|O.O.O.O.O.O.O.O.O.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.O.O.O.O.O.O.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 29: cout << "+------------------------+" << endl; cout << "|O.O.O.O.O.O.O.O.O.O.#.|D|)" << endl; cout << "|O.O.O.O.O.O.O.O.O.#.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.O.O.O.O.O.O.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 30: cout << "+------------------------+" << endl; cout << "|O.O.O.O.O.O.O.O.O.O.#.|D|)" << endl; cout << "|O.O.O.O.O.O.O.O.O.O.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.O.O.O.O.O.O.#.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 31: cout << "+------------------------+" << endl; cout << "|O.O.O.O.O.O.O.O.O.O.#.|D|)" << endl; cout << "|O.O.O.O.O.O.O.O.O.O.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.O.O.O.O.O.O.O.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 32: cout << "+------------------------+" << endl; cout << "|O.O.O.O.O.O.O.O.O.O.O.|D|)" << endl; cout << "|O.O.O.O.O.O.O.O.O.O.#.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.O.O.O.O.O.O.O.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 33: cout << "+------------------------+" << endl; cout << "|O.O.O.O.O.O.O.O.O.O.O.|D|)" << endl; cout << "|O.O.O.O.O.O.O.O.O.O.O.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.O.O.O.O.O.O.O.#.|.|)" << endl; cout << "+------------------------+" << endl; break; case 34: cout << "+------------------------+" << endl; cout << "|O.O.O.O.O.O.O.O.O.O.O.|D|)" << endl; cout << "|O.O.O.O.O.O.O.O.O.O.O.|.|" << endl; cout << "|O.......................|" << endl; cout << "|O.O.O.O.O.O.O.O.O.O.O.|.|)" << endl; cout << "+------------------------+" << endl; break; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); string s; cin >> s; long long i; vector<long long> v; long long p = s.length(); long long pp = 0; for (i = 0; i <= p - 1; i++) { if (s[i] != 'a') { v.push_back(i); pp++; } } long long x = 1; vector<long long> v2; map<long long, long long> m; for (i = pp - 2; i >= 0; i--) { if (v[i + 1] - v[i] == 1) { x++; } else { v2.push_back(x); m[x] = v[i + 1]; x = 1; } } v2.push_back(x); if (pp != 0) { m[x] = v[0]; } sort(v2.rbegin(), v2.rend()); if (pp != 0) { for (i = m[x]; i <= m[x] + (x - 1); i++) { s[i] = s[i] - 1; } } else { for (i = p - 1; i <= p - 1; i++) { s[i] = 'z'; } } cout << s << "\n"; }
2
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-8; struct Tpoint { double x, y; Tpoint() {} Tpoint(double _x, double _y) { x = _x; y = _y; } inline void read() { scanf("%lf%lf", &x, &y); } inline void show() { printf("%lf %lf\n", x, y); } inline double norm() { return sqrt(((x) * (x)) + ((y) * (y))); } }; inline Tpoint operator+(const Tpoint &a, const Tpoint &b) { return Tpoint(a.x + b.x, a.y + b.y); } inline Tpoint operator-(const Tpoint &a, const Tpoint &b) { return Tpoint(a.x - b.x, a.y - b.y); } inline Tpoint operator*(const Tpoint &a, const double &b) { return Tpoint(a.x * b, a.y * b); } inline Tpoint operator/(const Tpoint &a, const double &b) { return Tpoint(a.x / b, a.y / b); } inline double det(const Tpoint &a, const Tpoint &b) { return a.x * b.y - a.y * b.x; } inline double dot(const Tpoint &a, const Tpoint &b) { return a.x * b.x + a.y * b.y; } char s[100005]; string word[50005]; inline bool check(string s, string t) { if (s.size() < t.size()) return false; for (int i = 1; i <= t.size(); ++i) if (s[s.size() - i] != t[t.size() - i]) return false; return true; } inline int gender(string word) { if (check(word, "lios")) return 0; if (check(word, "liala")) return 1; if (check(word, "etr")) return 0; if (check(word, "etra")) return 1; if (check(word, "initis")) return 0; if (check(word, "inites")) return 1; return -1; } inline int type(string word) { if (check(word, "lios")) return 0; if (check(word, "liala")) return 0; if (check(word, "etr")) return 1; if (check(word, "etra")) return 1; if (check(word, "initis")) return 2; if (check(word, "inites")) return 2; return -1; } inline bool check(int n) { for (int i = 0; i < n; ++i) if (gender(word[i]) != gender(word[0]) || gender(word[i]) == -1) return false; if (n == 1) return true; int i = 0; while (i < n && type(word[i]) == 0) ++i; if (i == n) return false; if (type(word[i]) == 1) ++i; else return false; while (i < n && type(word[i]) == 2) ++i; return i == n; } int main() { gets(s); stringstream in(s); int n = 0; while (in >> word[n]) ++n; if (check(n)) puts("YES"); else puts("NO"); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 200006; const int MAX = 1000 * 100 + 1; const long long INF = 1e18; const int MOD = 1000 * 1000 * 1000 + 7; const int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0}; vector<long long> v[N], v1, v2; long long a[N], b[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n; cin >> n; long long x, y; string s; for (int i = (0); i <= (n - 1); i++) { cin >> a[i]; } cin >> s; x = 0; y = 0; for (int i = (0); i <= (n - 1); i++) { if (s[i] == '0') { sort(a + x, a + i + 1); x = i + 1; } else if (i == n - 1) { sort(a + x, a + i + 1); x = i + 1; } } for (int i = (1); i <= (n - 1); i++) { if (a[i] < a[i - 1]) { cout << "NO"; return 0; } } cout << "YES"; }
3
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 500100, mod = 998244353; int n, tt, m; void fn() { cin >> n; cout << n - 1 << endl; for (int i = 2; i <= n; i++) { cout << i << " "; } cout << endl; return; } int main() { ios::sync_with_stdio(0); cin.tie(0); ; cin >> tt; while (tt--) fn(); return 0; }
0
#include <bits/stdc++.h> using namespace std; char s[10000]; int main() { int n; cin >> n; getchar(); long long int *a = new long long int[1000]; long long int count = 1; long long int count1 = 1; for (int i = 1; i <= n; i++) { cin.getline(s, sizeof(s)); long long int v1 = (s[1] - '0') * 10 + (s[2] - '0'); long long int v2 = (s[4] - '0') * 10 + (s[5] - '0'); if (v1 == 12) { v1 = 0; } if (s[7] == 'p') { v1 += 12; } a[i] = v1 * 60 + v2; } long long int x = a[1]; for (int i = 2; i <= n; i++) { if (a[i] < x) { count1 = 1; count++; } else if (a[i] == x) { count1++; } else { count1 = 1; } if (count1 > 10) { count++; count1 = 1; } x = a[i]; } cout << count << endl; }
5
#include <bits/stdc++.h> using namespace std; int a[101]; int main() { int n, i, j; cin >> n; for (i = 0; i < n; i++) { cin >> j; a[j]++; } int numbox = 0, piles = 0, flag; while (1) { numbox = 0; flag = 0; for (i = 0; i <= 100; i++) { if (a[i] > 0 && numbox <= i) { flag = 1; a[i]--; numbox++; i--; } } if (flag == 1) piles++; else break; } cout << piles; return 0; }
3
#include <bits/stdc++.h> using namespace std; void run() { long long n, m; cin >> n >> m; long long cl, ce, v; cin >> cl >> ce >> v; vector<long long> stairs(cl); for (int i = 0; i < cl; ++i) { cin >> stairs[i]; } vector<long long> lift(ce); for (int i = 0; i < ce; ++i) { cin >> lift[i]; } long long q; cin >> q; for (int cnt = 0; cnt < q; ++cnt) { long long x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; if (x1 == x2) { cout << abs(y1 - y2) << endl; continue; } swap(x1, y1); swap(x2, y2); long long closestlift = lower_bound(lift.begin(), lift.end(), x1) - lift.begin(); if (closestlift != lift.size()) { closestlift = lift[closestlift]; } else { closestlift = 1000000000; } long long closestlift1 = lower_bound(lift.begin(), lift.end(), x1) - lift.begin() - 1; if (closestlift1 < 0) { closestlift1 = 1000000000; } else { closestlift1 = lift[closestlift1]; } long long dist1 = abs(closestlift - x1) + abs(closestlift - x2) + ceil((double)(abs(y2 - y1)) / (double)v); long long dist2 = abs(closestlift1 - x1) + abs(closestlift1 - x2) + ceil((double)(abs(y2 - y1)) / (double)v); long long closestlift2 = lower_bound(lift.begin(), lift.end(), x2) - lift.begin(); if (closestlift2 != lift.size()) { closestlift2 = lift[closestlift2]; } else { closestlift2 = 1000000000; } long long closestlift3 = lower_bound(lift.begin(), lift.end(), x2) - lift.begin() - 1; if (closestlift3 < 0) { closestlift3 = 1000000000; } else { closestlift3 = lift[closestlift3]; } long long dist11 = abs(closestlift2 - x1) + abs(closestlift2 - x2) + ceil((double)(abs(y2 - y1)) / (double)v); long long dist22 = abs(closestlift3 - x1) + abs(closestlift3 - x2) + ceil((double)(abs(y2 - y1)) / (double)v); long long closeststair = lower_bound(stairs.begin(), stairs.end(), x1) - stairs.begin(); if (closeststair == stairs.size()) { closeststair = 1000000000; } else { closeststair = stairs[closeststair]; } long long closeststair1 = lower_bound(stairs.begin(), stairs.end(), x1) - stairs.begin() - 1; if (closeststair1 < 0) { closeststair1 = 1000000000; } else { closeststair1 = stairs[closeststair1]; } long long dist3 = abs(closeststair - x1) + abs(closeststair - x2) + abs(y2 - y1); long long dist4 = abs(closeststair1 - x1) + abs(closeststair1 - x2) + abs(y2 - y1); long long closeststair2 = lower_bound(stairs.begin(), stairs.end(), x2) - stairs.begin(); if (closeststair2 == stairs.size()) { closeststair2 = 1000000000; } else { closeststair2 = stairs[closeststair2]; } long long closeststair3 = lower_bound(stairs.begin(), stairs.end(), x2) - stairs.begin() - 1; if (closeststair3 < 0) { closeststair3 = 1000000000; } else { closeststair3 = stairs[closeststair3]; } long long dist33 = abs(closeststair2 - x1) + abs(closeststair2 - x2) + abs(y2 - y1); long long dist44 = abs(closeststair3 - x1) + abs(closeststair3 - x2) + abs(y2 - y1); cout << min(min({dist11, dist22, dist33, dist44}), min({dist1, dist2, dist3, dist4})) << endl; } } int main() { run(); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { string s, r; cin >> s >> r; reverse(s.begin(), s.end()); if (s == r) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
0
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;i++) #define ret(x) return cout<<x,0; #define rety return cout<<"YES",0; #define retn return cout<<"NO",0; typedef long long ll; typedef double db; typedef long double ld; #define stt string #define ve vector<ll> #define se set<ll> #define ar array ll mod=1e9+7,mod2=998244353; using namespace std; ll fac[10000000]; ll gcd(ll x, ll y) { if(y==0) return x; return gcd(y,x%y); } ll fexp(ll a,ll b,ll m){ll ans = 1;while(b){if(b&1)ans=ans*a%m; b/=2;a=a*a%m;}return ans;} ll inverse(ll a, ll p){return fexp(a, p-2,p);} ll ncr(ll n, ll r,ll p) { if (r==0) return 1; return (fac[n]*inverse(fac[n-r],p)%p*inverse(fac[r],p)%p)%p; } // ____Z-Algorithm_____ vector<ll> za(string s) { ll n = s.size(); vector<ll> z(n); ll x = 0, y = 0,p=0; for(ll i= 1; i < n; i++) { z[i] = max(p,min(z[i-x],y-i+1)); while(i+z[i] < n && s[z[i]] == s[i+z[i]]) { x = i; y = i+z[i]; z[i]++; } } return z; } void subset(ll a[],ll k) { for (int i = 1; i < pow(2, k); i++) { for (int j = 0; j < k; j++) { if (i & 1 << j) { cout<<a[j]<<" "; } } cout<<endl; } } vector<ll> pr(string s) { ll n = s.length(); vector<ll> pi(n); for (int i = 1; i < n; i++) { int j = pi[i-1]; while (j > 0 && s[i] != s[j]) j = pi[j-1]; if (s[i] == s[j]) j++; pi[i] = j; } return pi; } //---------------------------------------------------------------------/ int main() { ios::sync_with_stdio(0); cin.tie(0); ll t; cin>>t; while(t--) { int n; cin >> n; vector<int> a(n); for(int i = 0; i < n; i++) { cin >> a[i]; } cout << 3*n << endl; for(int i = 1; i <= n; i+=2) { cout << 1 << " " << i << " " << i+1 << endl; cout << 2 << " " << i << " " << i+1 << endl; cout << 2 << " " << i << " " << i+1 << endl; cout << 1 << " " << i << " " << i+1 << endl; cout << 2 << " " << i << " " << i+1 << endl; cout << 2 << " " << i << " " << i+1 << endl; } } }
1
#include <bits/stdc++.h> using namespace std; int main() { int arr[101], arr1[101]; int n, m; cin >> n >> m; int flag = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; } for (int i = 0; i < m; i++) { cin >> arr1[i]; } int max1 = arr[0]; int min1 = arr[0]; for (int i = 0; i < n; i++) { if (max1 < arr[i]) { max1 = arr[i]; } if (min1 > arr[i]) { min1 = arr[i]; } } int val = max(2 * min1, max1); int min = arr1[0]; for (int i = 0; i < m; i++) { if (min > arr1[i]) { min = arr1[i]; } } if (val < min) { cout << val; } else { cout << "-1"; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { long long t, tc; cin >> tc; for (t = 1; t <= tc; t++) { long long i, p, q, n, x, y, prex, prey, a, b, j, flag = 0; string s = ""; cin >> n; vector<pair<long long, long long>> v; for (i = 0; i < n; i++) { cin >> p >> q; v.push_back(make_pair(p, q)); } sort(v.begin(), v.end()); prex = 0, prey = 0; for (i = 0; i < n; i++) { x = v[i].first; y = v[i].second; if (x < prex || y < prey) { cout << "NO" << endl; flag = 1; break; } a = x - prex; for (j = 1; j <= a; j++) s += 'R'; b = y - prey; for (j = 1; j <= b; j++) s += 'U'; prex = x; prey = y; } if (flag == 0) { cout << "YES" << endl; cout << s << endl; } v.clear(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int _inf = 0xc0c0c0c0; const long long INF = 0x3f3f3f3f3f3f3f3f; const long long _INF = 0xc0c0c0c0c0c0c0c0; const long long mod = (int)1e9 + 7; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long ksm(long long a, long long b, long long mod) { int ans = 1; while (b) { if (b & 1) ans = (ans * a) % mod; a = (a * a) % mod; b >>= 1; } return ans; } long long inv2(long long a, long long mod) { return ksm(a, mod - 2, mod); } const int MAX_N = 300025; int n, m; long long minn[MAX_N], sum[MAX_N]; struct node { long long a, b; bool operator<(const node other) const { if (b == other.b) return a < other.a; return b < other.b; } } arr[MAX_N]; priority_queue<pair<long long, long long> > q; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%lld%lld", &arr[i].a, &arr[i].b); sort(arr + 1, arr + 1 + n); long long sum = 0, cnt = 0, ans = 0; for (int i = n; i >= 1; --i) { if (cnt < m) { sum += arr[i].a; q.push(pair<long long, long long>(-arr[i].a, arr[i].b)); cnt++; } else { pair<long long, long long> top = q.top(); if (arr[i].a > (-top.first)) { sum -= (-top.first); sum += arr[i].a; q.pop(); q.push(pair<long long, long long>(-arr[i].a, arr[i].b)); } } ans = max(ans, arr[i].b * sum); } printf("%lld\n", ans); return 0; }
4
#include <bits/stdc++.h> inline int read() { char c; int x; for (c = getchar(); !isdigit(c); c = getchar()) ; for (x = 0; isdigit(c); c = getchar()) { x = x * 10 + c - '0'; } return x; } const int N = 1e6 + 5; int n, k, s, t, rs, rt, len, ans0, a[N], b[N], ft[N], dis[N], dit[N], deg[N]; long long ans1; std::vector<int> e[N], ed; bool dfs0(int u, int fa) { if (u == s) { return true; } for (auto v : e[u]) { if (v == fa) { continue; } if (dfs0(v, u)) { std::swap(a[u], a[v]); ans0++; return true; } } return false; } void dfs1(int u, int fa) { ft[u] = fa; dit[u] = dit[fa] + 1; if (a[u] != b[u]) { deg[fa]++; } for (auto v : e[u]) { if (v == fa) { continue; } dfs1(v, u); } } void dfs2(int u, int fa) { dis[u] = dis[fa] + 1; for (auto v : e[u]) { if (v == fa) { continue; } dfs2(v, u); } } bool check0() { for (int u = 1; u <= n; u++) { if (a[u] != b[u]) { return false; } } return true; } bool check1() { rt = -1; for (int u = 1; u <= n; u++) { if (a[u] == b[u]) { if (deg[u] > 2) { return false; } if (deg[u] != 0) { if (rt != -1) { return false; } rt = u; } } else { if (deg[u] > 1) { return false; } if (deg[u] == 0) { ed.push_back(u); } len++; } } if (ed.size() > 2) { return false; } if (ed.size() < 2) { ed.push_back(rt); } rs = s; for (; rs != t && a[rs] == b[rs]; rs = ft[rs]) ; if (rs == t) { rs = rt; } for (int u = ed[0]; u != rt; u = ft[u]) { if (a[u] == b[ed[0]]) { k = dit[ed[0]] - dit[u]; } } for (int u = ed[1]; u != rt; u = ft[u]) { if (a[u] == b[ed[0]]) { k = len - (dit[ed[1]] - dit[u] + 1); } } for (int u = ed[0]; u != rt; u = ft[u]) { if (u == rs) { k = len - k; } } return true; } int main() { n = read(); for (int u = 1; u <= n; u++) { a[u] = read(); if (a[u] == 0) { s = u; } } for (int u = 1; u <= n; u++) { b[u] = read(); if (b[u] == 0) { t = u; } } for (int i = 1; i < n; i++) { int u = read(), v = read(); e[u].push_back(v); e[v].push_back(u); } dfs0(t, 0); if (check0()) { printf("0 %d\n", ans0); return 0; } dis[0] = dit[0] = -1; dfs1(t, 0); dfs2(s, 0); if (!check1()) { printf("-1\n"); return 0; } if (rs == rt) { ans1 = dis[rs] + dit[rt] + 1ll * std::min(k, len - k) * (len + 1); } else { int mid = dis[rt] - dis[rs]; ans1 = dis[rs] + dit[rt] + std::min(k * (len + 1ll) - mid, (len - k) * (len + 1ll) + mid); } std::sort(ed.begin(), ed.end()); printf("%d %d %lld\n", ed[0], ed[1], ans1); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; vector<vector<int> > v(n, vector<int>(7, 0)); for (int i = 0; i < n; i++) { cin >> s; for (int j = 0; j < 7; j++) v[i][j] = s[j] - '0'; } vector<int> cnt(7, 0); for (int i = 0; i < 7; i++) { for (int j = 0; j < n; j++) cnt[i] += v[j][i]; } int m = 0; for (int i = 0; i < 7; i++) { if (cnt[i] > m) m = cnt[i]; } cout << m; return 0; }
0
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; int debug = 0; const int N = 2e5 + 10, C = 470; int n, k, freq, alone, ans, q, a[N], p[N], was[N * 3], cnt[N], id[N]; vector<array<int, 2>> cnts; vector<int> clrs; int main() { cin >> n; for (int i = 1; i < n + 1; ++i) { cin >> a[i]; cnt[a[i]]++; } for (int i = 1; i < n + 1; ++i) { cnts.push_back({cnt[i], i}); } sort(cnts.begin(), cnts.end()); reverse(cnts.begin(), cnts.end()); if (cnts.size() > 1 && cnts[0][0] == cnts[1][0]) { cout << n << '\n'; return 0; } k = min((int)cnts.size(), C); for (auto [val, c] : cnts) { clrs.push_back(c); } clrs.resize(k); freq = cnts[0][1]; for (int i = 1; i < n + 1; ++i) { p[i] = p[i - 1] + (a[i] == freq); } ans = 0; for (int c : clrs) { if (c == freq) continue; memset(was, -1, sizeof(was)); int mx = 0, x; was[n] = 0; q = -n; for (int r = 1; r < n + 1; ++r) { q += (a[r] == c); x = p[r] - q; if (was[x] != -1) { mx = max(mx, r - was[x]); } else { was[x] = r; } } ans = max(ans, mx); } cout << ans << '\n'; }
9
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int n, i, a[200005], temp; int main() { scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); temp = 1e9 + 7; for (i = 0; i < n / 2; i++) { temp = min(temp, a[i + n / 2] - a[i]); } printf("%d\n", temp); }
7
#include <bits/stdc++.h> using namespace std; int main() { int sum = 0; bool flag = false, flag0 = false; string s; cin >> s; for (int i = 0; i < s.length(); ++i) { if (s[i] == '1') flag0 = true; if (flag0 && s[i] == '0') ++sum; if (sum == 6) { flag = true; break; } } cout << (flag ? "yes" : "no"); return 0; }
1
#include <bits/stdc++.h> using namespace std; mt19937 mt_rand( chrono::high_resolution_clock::now().time_since_epoch().count()); const int N = 2e5; const int HS = 3; int MOD[HS]; struct hash_number { int v[HS]; hash_number() { memset(v, 0, sizeof v); } hash_number(long long x) { for (int i = 0; i < HS; i++) v[i] = (x % MOD[i] + MOD[i]) % MOD[i]; } hash_number operator+(hash_number x) { hash_number ret; ret.v[0] = (v[0] + x.v[0]) % MOD[0]; ret.v[1] = ((long long)v[1] * x.v[1]) % MOD[1]; ret.v[2] = (v[2] + x.v[2]) % MOD[2]; return ret; } bool operator==(const hash_number& x) const { for (int i = 0; i < HS; i++) if (v[i] != x.v[i]) return false; return true; } }; vector<pair<int, int> > adj[N + 5]; hash_number tot; int n; hash_number val[N + 5]; hash_number hass[15][15]; int ans; int k; void dfs(int d, hash_number hsh) { if (d == k + 1) { if (hsh == tot) ans++; return; } for (int i = 1; i <= d; i++) { dfs(d + 1, hsh + hass[d][i]); } } int main() { ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); MOD[0] = 1e9 + 7; MOD[1] = 1e9 + 9; MOD[2] = std::uniform_int_distribution<int>(1e8, 1e9)(mt_rand); int m; cin >> n >> m >> k; while (m--) { int u, v, w; cin >> u >> v >> w; adj[u].push_back({w, v}); } std::uniform_int_distribution<long long> rg(1, 1e18); for (int i = 1; i <= n; i++) { val[i] = hash_number(rg(mt_rand)); tot = tot + val[i]; } for (int i = 1; i <= n; i++) { int deg = adj[i].size(); sort(adj[i].begin(), adj[i].end()); for (int j = 0; j < deg; j++) { hass[deg][j + 1] = hass[deg][j + 1] + val[adj[i][j].second]; } } ans = 0; hash_number h; dfs(1, h); cout << ans << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; vector<long long int> a(n); vector<long long int> b; for (long long int i = 0; i < n; i++) cin >> a[i]; sort(a.begin(), a.end()); long long int diff = LLONG_MAX; long long int c = 1; for (long long int i = 0; i < n - 1; i++) { if (abs(a[i + 1] - a[i]) < diff) { diff = abs(a[i + 1] - a[i]); c = 1; } else if (abs(a[i + 1] - a[i]) == diff) c++; } cout << diff << " " << c; return 0; }
1
#include <bits/stdc++.h> using namespace std; long long SUM = 0; int n, m, a[10001]; int main() { cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i], SUM += a[i]; } sort(a, a + n); if (SUM - a[n - 1] > m) { cout << "NO"; } else cout << "YES"; return 0; }
0
#include <bits/stdc++.h> using namespace std; vector<long long int> v, a1, b1; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, k; cin >> n >> k; pair<long long int, pair<long long int, long long int> > p[n]; for (long long int i = 0; i < n; i++) { long long int t, a, b; cin >> t >> a >> b; p[i] = {t, {a, b}}; } sort(p, p + n); long long int ans = 0; for (long long int i = 0; i < n; i++) { if (p[i].second.first == 1 && p[i].second.second == 1) { ans += p[i].first; v.push_back(p[i].first); k--; } if (k == 0) break; } long long int flaga = 0; long long int temp = 0; for (long long int i = 0; i < n; i++) { if (flaga == k) temp = 1; if (p[i].second.first == 1 && p[i].second.second == 0) { if (temp == 1) a1.push_back(p[i].first); else { ans += p[i].first; flaga++; } } } long long int flagb = 0; temp = 0; for (long long int i = 0; i < n; i++) { if (flagb == k) temp = 1; if (p[i].second.first == 0 && p[i].second.second == 1) { if (temp == 1) b1.push_back(p[i].first); else { ans += p[i].first; flagb++; } } } long long int q = v.size() - 1; if (v.size() == 0) q = -1; for (long long int i = 0; i < a1.size() && i < b1.size() && q >= 0; i++) { if (a1[i] + b1[i] < v[q]) { ans += (a1[i] + b1[i] - v[q--]); } else break; } if (flaga == k && flagb == k) cout << ans << "\n"; else cout << -1 << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; char s[maxn], t[maxn]; int pos[maxn][26]; int main() { int T; scanf("%d", &T); while (T--) { scanf("%s%s", s, t); memset(pos, 0x3f3f3f3f, sizeof(pos)); for (int i = strlen(s) - 1; i >= 0; i--) { for (int j = 0; j < 26; j++) { if (j == s[i] - 'a') { pos[i][j] = i; } else { pos[i][j] = pos[i + 1][j]; } } } int flag = 0, p = 0, ans = 1; for (int i = 0; i < strlen(t); i++) { if (p == strlen(s) || pos[p][t[i] - 'a'] == 0x3f3f3f3f) { p = 0; ans++; } if (p == 0 && pos[p][t[i] - 'a'] == 0x3f3f3f3f) { flag = 1; break; } p = pos[p][t[i] - 'a'] + 1; } if (flag) printf("-1\n"); else printf("%d\n", ans); } return 0; }
4
#include <bits/stdc++.h> using namespace std; void fun() {} long long int __gcda(long long int a, long long int b) { if (b == 0) { return a; } else if (a == 0) { return b; } else if (a == b) { return a; } else if (b > a) { return __gcda(a, b % a); } else if (a > b) { return __gcda(a % b, b); } return 0; } long long int gcdExtended(long long int a, long long int b, long long int *x, long long int *y) { if (a == 0) { *x = 0; *y = 1; return b; } long long int x1, y1; long long int gcd = gcdExtended(b % a, a, &x1, &y1); *x = y1 - (b / a) * x1; *y = x1; return gcd; } long long int md = 998244353; bool valid(long long int start, long long int end, long long int hashe[], long long int dda[][26]) { for (long long int i = 0; i < 26; i++) { if (dda[end][i] - dda[start - 1][i] > 0) { if (hashe[i] < end - start + 1) return false; } } return true; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); fun(); long long int n, k; cin >> n >> k; long long int dp[n + 1][k + 1][4]; if (k == 1) { cout << "2\n"; return 0; } memset(dp, 0, sizeof(dp)); dp[1][1][1] = dp[1][1][0] = 1; dp[1][1][3] = dp[1][1][2] = 0; dp[1][2][0] = dp[1][2][1] = 0; dp[1][2][2] = dp[1][2][3] = 1; for (long long int i = 2; i <= n; i++) { dp[i][1][0] = dp[i][1][1] = 1; dp[i][1][2] = dp[i][1][3] = 0; for (long long int kk = 2; kk <= min(k, 2 * i); kk++) { dp[i][kk][0] = (dp[i - 1][kk - 1][1] + dp[i - 1][kk][2] + dp[i - 1][kk][3] + dp[i - 1][kk][0]) % md; dp[i][kk][1] = (dp[i - 1][kk - 1][0] + dp[i - 1][kk][2] + dp[i - 1][kk][3] + dp[i - 1][kk][1]) % md; dp[i][kk][2] = (dp[i - 1][kk - 1][0] + dp[i - 1][kk - 1][1] + dp[i - 1][kk - 2][3] + dp[i - 1][kk][2]) % md; dp[i][kk][3] = (dp[i - 1][kk - 1][0] + dp[i - 1][kk - 1][1] + dp[i - 1][kk - 2][2] + dp[i - 1][kk][3]) % md; } } cout << (dp[n][k][0] + dp[n][k][1] + dp[n][k][2] + dp[n][k][3]) % md << "\n"; }
4
#include <bits/stdc++.h> using namespace std; const int N = 55; int n; char s[N][N]; int dp[N][N][N][N]; int solve(int a, int b, int x, int y) { if (dp[a][b][x][y] != -1) return dp[a][b][x][y]; if (a == x && b == y) return s[a][b] == '#'; int ans = max(x - a + 1, y - b + 1); for (int i = a; i < x; i++) ans = min(ans, solve(a, b, i, y) + solve(i + 1, b, x, y)); for (int i = b; i < y; i++) ans = min(ans, solve(a, b, x, i) + solve(a, i + 1, x, y)); return dp[a][b][x][y] = ans; } int main() { cin >> n; for (int i = 0; i < n; i++) cin >> s[i]; memset(dp, -1, sizeof(dp)); printf("%d\n", solve(0, 0, n - 1, n - 1)); }
7
#include <bits/stdc++.h> const int N = 200000; const int MOD = (int)1e9 + 7; void update(int& x, int a) { x += a; if (x >= MOD) { x -= MOD; } } int head[N], to[N], next[N], degree[N]; bool visited[N], used[N]; void solve(int n, int* ways, int s) { std::vector<int> edges; int v = s; while (true) { visited[v] = true; int iterator = head[v]; while (~iterator && used[iterator >> 1]) { iterator = next[iterator]; } if (!~iterator) { break; } used[iterator >> 1] = true; edges.push_back(iterator); v = to[iterator] >> 1; } assert(!edges.empty()); int new_ways[2]; memset(new_ways, 0, sizeof(new_ways)); for (int first = 0; first <= (s < n); ++first) { int dp[2][2][2]; memset(dp, 0, sizeof(dp)); dp[0][first][0] = 1; int m = edges.size(); for (int i = 0; i < m; ++i) { memset(dp[i + 1 & 1], 0, sizeof(dp[i + 1 & 1])); int e = edges[i]; for (int x = 0; x < 2; ++x) { for (int s = 0; s < 2; ++s) { for (int y = 0; y <= ((to[e] >> 1) < n); ++y) { int v = (x ^ (to[e ^ 1] & 1)) || (y ^ (to[e] & 1)); update(dp[i + 1 & 1][y][s ^ v], dp[i & 1][x][s]); } } } } for (int last = 0; last < 2; ++last) { if (s == v && first != last) { continue; } for (int s = 0; s < 2; ++s) { for (int ps = 0; ps < 2; ++ps) { update(new_ways[s ^ ps], static_cast<long long>(ways[ps]) * dp[m & 1][last][s] % MOD); } } } } memcpy(ways, new_ways, sizeof(new_ways)); } int main() { int n, m; scanf("%d%d", &m, &n); int v = n; for (int i = 0; i < m; ++i) { int k; scanf("%d", &k); for (int j = 0; j < 2; ++j) { int a; if (j < k) { scanf("%d", &a); } else { a = ++v; } to[i << 1 | j] = std::abs(a) - 1 << 1 | (a < 0); } } memset(head, -1, sizeof(*head) * v); for (int i = 0; i < m << 1; ++i) { int u = to[i ^ 1] >> 1; next[i] = head[u]; head[u] = i; degree[u]++; } int ways[2]; ways[0] = 1; ways[1] = 0; for (int i = 0; i < v; ++i) { if (!degree[i]) { visited[i] = true; update(ways[0], ways[0]); } } for (int i = 0; i < v; ++i) { if (degree[i] < 2 && !visited[i]) { solve(n, ways, i); } } for (int i = 0; i < v; ++i) { if (!visited[i]) { solve(n, ways, i); } } printf("%d\n", ways[1]); }
10
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000000007; const long long MOD1 = 998244353; const long long MIN_LL = -1e18; const long long MAX_LL = 1e18; const int MAX_INT = 1e9; const int MIN_INT = -1e9; const long double pie = 3.1415926535; long long fpow(long long n, long long p) { ; long long m = 1; while (p) { if (p % 2) m *= n; p >>= 1; n *= n; } return m; } long long mfpow(long long n, long long p, long long M) { long long m = 1; n %= M; while (p) { if (p % 2) m = (m * n) % M; n = (n * n) % M; p >>= 1; } return m % M; } long long invmod(long long n, long long m) { return mfpow(n, m - 2, m); } const vector<long long> days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; bool br, br1, br2; char ch, ch1, ch2; long long n, m, k, x, y, z, d, mini, maxi, l, r, sum, t, w, h; pair<long long, long long> p, p1, p2; string s, s1, s2, s3; multiset<long long> mst; set<long long> st, st1, st2, st3; map<long long, long long> mp, mp1, mp2; long long a[(int)1e6 + 100]; void solve() { cin >> n >> m; string s[n]; for (int i = 0; i < n; i++) cin >> s[i]; for (int i = 0; i < m; i++) { mp.clear(); for (int j = 0; j < n; j++) mp[s[j][i]]++; a[i] = 0; for (auto j : mp) if (a[i] < j.second) a[i] = j.second; } for (int i = 0; i < m; i++) { cin >> x; sum += x * a[i]; } cout << sum << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); solve(); return 0; }
0
#include <bits/stdc++.h> using namespace std; int t, n, mint = 99999999, minn, ans; struct bus { int s, d; }; bus x; int bl[10001]; int main() { scanf("%d%d", &n, &t); for (int i = 1; i <= n; i++) { scanf("%d%d", &x.s, &x.d); if (x.s < t) { ans = t - x.s; ans = ans % x.d; if (ans == 0) { printf("%d", i); return 0; } else { ans = x.d - ans; if (ans < mint) { mint = ans; minn = i; } } } else { if (t == x.s) { cout << i; return 0; } else { ans = x.s - t; if (ans == 0) { cout << i; return 0; } if (ans < mint) { mint = ans; minn = i; } } } } cout << minn; return 0; }
1
#include <bits/stdc++.h> using namespace std; template <typename T> class binary_indexed_tree { vector<T> _t; T sum(int r) { T result = 0; while (r > 0) { result += _t[r]; r = (r & (r + 1)) - 1; } return result; } public: binary_indexed_tree(const size_t n) { _t.assign(n + 1, 0); } binary_indexed_tree(const vector<T> &data) : binary_indexed_tree(data.size()) { for (int i = 0; i < data.size(); i++) inc(i, data[i]); } T sum(const int l, const int r) { return sum(r + 1) - sum(l); } void inc(int index, const T delta) { index++; while (index < _t.size()) { _t[index] += delta; index = index | (index + 1); } } }; class problem { int n; int q; binary_indexed_tree<int64_t> *bit; vector<int64_t> a; public: problem() { ios_base::sync_with_stdio(false); } void solve() { int n, q; cin >> n >> q; a.assign(n, 1); bit = new binary_indexed_tree<int64_t>(a); bool flipped = false; int left = 0; int right = n; for (int k = (0); k <= (int)((q)-1); ++k) { int t; cin >> t; if (t == 1) { int p; cin >> p; if (!flipped) { if (2 * p > right - left) { for (int i = (0); i <= (int)((right - left - p) - 1); ++i) { a[left + p - i - 1] += a[left + p + i]; bit->inc(left + p - i - 1, a[left + p + i]); } flipped = true; right = left + p; } else { for (int i = (0); i <= (int)((p)-1); ++i) { a[left + p + i] += a[left + p - i - 1]; bit->inc(left + p + i, a[left + p - i - 1]); } left = left + p; } } else { if (2 * p > right - left) { for (int i = (0); i <= (int)((right - left - p) - 1); ++i) { a[right - p + i] += a[right - p - i - 1]; bit->inc(right - p + i, a[right - p - i - 1]); } flipped = false; left = right - p; } else { for (int i = (0); i <= (int)((p)-1); ++i) { a[right - p - i - 1] += a[right - p + i]; bit->inc(right - p - i - 1, a[right - p + i]); } right = right - p; } } } else if (t == 2) { int l, r; cin >> l >> r; if (flipped) cout << bit->sum(right - r, right - l - 1) << endl; else cout << bit->sum(left + l, left + r - 1) << endl; } } } }; int main() { problem *p = new problem(); p->solve(); return 0; }
7
#include <bits/stdc++.h> using namespace std; int n; char a[100005]; int ans[100005]; int main() { scanf("%s", a); n = strlen(a); int cnt = 0, s_cnt = 0, last = -1; for (int i = 0; i < n; ++i) { if (a[i] == '(') cnt++; else cnt--; if (cnt < 0) { printf("-1"); return 0; } if (a[i] == '#') { ans[i] = 1; last = i; } } if (last != -1) ans[last] += cnt; cnt = 0; for (int i = 0; i < n; ++i) { if (a[i] == '(') cnt++; else if (a[i] == '#') cnt -= ans[i]; else cnt--; if (cnt < 0) { printf("-1"); return 0; } } if (cnt != 0) { printf("-1"); return 0; } for (int i = 0; i < n; ++i) { if (a[i] == '#') printf("%d\n", ans[i]); } }
3
#include <bits/stdc++.h> using namespace std; vector<unsigned long long int> vec; void factors(unsigned long long int n) { if (n % 2LL == 0) { while (n % 2LL == 0) { n /= 2LL; vec.push_back(2LL); } } for (unsigned long long int i = 3; i * i <= n; i += 2) { if (n % i == 0) { while (n % i == 0) { n /= i; vec.push_back(i); } } } if (n > 1) vec.push_back(n); } void solve() { unsigned long long int x; cin >> x; factors(x); if (vec.size() > 1) { if (vec.size() == 2) cout << 2 << '\n'; else { cout << 1 << '\n' << vec[0] * vec[1] << '\n'; } } else cout << 1 << '\n' << 0 << '\n'; } int main() { ios_base::sync_with_stdio(false); cout.tie(NULL); cin.tie(NULL); ; unsigned long long int t = 1; while (t--) { solve(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; struct node { long long x, y; int id; } s[200005]; struct Bridge { long long x; int id; bool operator<(const Bridge &A) const { if (x != A.x) return x < A.x; return id < A.id; } } bg[200005]; set<Bridge> st; set<Bridge>::iterator it; bool cmp(node A, node B) { if (A.y != B.y) return A.y < B.y; return A.x < B.x; } int ans[200005]; int main() { int n, m; int i, j, k; long long l0, r0, l1, r1; scanf("%d %d", &n, &m); if (m < n - 1) { puts("No"); return 0; } cin >> l0 >> r0; for (i = 0; i < n - 1; i++) { cin >> l1 >> r1; s[i].id = i + 1; s[i].x = l1 - r0; s[i].y = r1 - l0; l0 = l1, r0 = r1; } sort(s, s + n - 1, cmp); for (i = 0; i < m; i++) { cin >> bg[i].x; bg[i].id = i + 1; } sort(bg, bg + m); for (i = 0, j = 0; i < n - 1; i++) { while (j < m && bg[j].x <= s[i].y) { st.insert(bg[j]); j++; } Bridge tmp; tmp.x = s[i].x, tmp.id = -1; it = st.lower_bound(tmp); if (it == st.end()) { puts("No"); return 0; } ans[s[i].id] = (*it).id; st.erase(it); } puts("Yes"); for (i = 1; i < n; i++) printf("%d ", ans[i]); puts(""); return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); string s; cin >> s; int n = s.length(); int posH = INT_MAX, posE = INT_MAX, posI = INT_MAX, posD = INT_MAX, posI2 = 0; for (int i = 0; i < n; i++) { if (s[i] == 'h') { posH = i; } if (s[i] == 'e' && i > posH) { posE = i; } if (s[i] == 'i' && i > posE) { posI = i; } if (s[i] == 'd' && i > posI) { posD = i; } if (s[i] == 'i' && i > posD) { posI2 = i; break; } } if (posI2 > 0) { cout << "YES" << "\n"; } else { cout << "NO" << "\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; using namespace std; const long long mod = 1e9 + 7; int arr[601]; int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m, k, i, j, x, y, z; int t, r; memset(arr, 0, sizeof(arr)); cin >> m >> t >> r; vector<long long> a(m); for (i = (0); i < (m); ++i) cin >> a[i]; if (t < r) { cout << -1 << "\n"; return 0; } int ans = 0; for (i = (0); i < (m); ++i) { z = a[i]; z += t; int cnt = 0; for (j = z - 1; j >= z - t; j--) { if (arr[j]) cnt++; } for (j = z - 1; j >= z - t; j--) { if (cnt == r) break; if (arr[j] == 0) { cnt++; ans++; arr[j] = 1; } } if (cnt < r) { cout << -1 << "\n"; return 0; } } cout << ans << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int ch = 0, c = 0, i = 0; string x; cin >> x; while (x[i] == 'a') { i++; } if (i == x.length()) { x[x.length() - 1] = 'z'; } else { for (i; i < x.size(); i++) { if (x[i] == 'a') { break; } else { x[i]--; } } } cout << x << endl; }
2
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; const int INF = 2e9 + 9; const long long INF1 = 1e18 + 9; const long long MAXN = 2e5 + 7; const long long MAXN1 = 1 << 11; const long long MAXN2 = 4e6 + 9; const long long MOD = 998244353; const long long MOD1 = 1e9 + 9; const long long ALPH = 50; const long long PW1 = 239; const long long PW2 = 199; const long long PW3 = 193; const long long PW4 = 117; const long double EPS = 1e-9; const long long BLOCK = 3684; const long long BLOCK1 = 1 << 9; void solve(); signed main() { srand('a' + 'l' + 'e' + 'x' + 'X' + '5' + '1' + '2'); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int q = 1; if (0) cin >> q; while (q--) solve(); } bool correct[26][26][MAXN]; int zf[MAXN * 2], z[2 * MAXN]; void solve() { int n, m; string s, t; cin >> n >> m >> s >> t; int k = n + m + 1; for (int i = 0; i < m; ++i) z[i] = t[i] - 'a'; z[m] = -1; for (int i = 0; i < n; ++i) z[m + i + 1] = s[i] - 'a'; for (int i = 0; i < 26; ++i) { for (int j = 0; j < 26; ++j) { for (int r = 0, l = 0, q = 1; q < k; ++q) { zf[q] = 0; if (q <= r) { zf[q] = min(r - q + 1, zf[q - l]); } while (q + zf[q] < k && zf[q] < m && (z[q + zf[q]] == (q + zf[q] >= m ? i : j) && z[zf[q]] == (zf[q] >= m ? i : j) || z[q + zf[q]] != (q + zf[q] >= m ? i : j) && z[zf[q]] != (zf[q] >= m ? i : j))) { ++zf[q]; } if (q + zf[q] - 1 > r) { r = q + zf[q] - 1; l = q; } } for (int q = 0; q < n; ++q) { correct[i][j][q] = zf[m + q + 1] == m; } } } vector<int> ind(26, -1), ans; vector<set<int>> in(26); for (int i = m - 1; i >= 0; --i) { ind[z[i]] = i; } for (int i = 0; i + m <= n; ++i) { bool b = 1; for (int j = 0; j < 26; ++j) { if (ind[j] != -1) { if (!correct[z[m + 1 + i + ind[j]]][j][i]) b = 0; in[z[m + 1 + i + ind[j]]].emplace(j); in[j].emplace(z[m + 1 + i + ind[j]]); } } for (int j = 0; j < 26; ++j) { if (in[j].size() > 1) b = 0; in[j].clear(); } if (b) ans.emplace_back(i + 1); } cout << ans.size() << "\n"; for (auto &i : ans) cout << i << " "; }
8
#include <bits/stdc++.h> using namespace std; using ll = long long; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long T = 1; cin >> T; while (T--) { long long n, k; cin >> n >> k; vector<long long> mon(n); vector<long long> peaks(n, 0); for (long long i = 0; i < n; i++) { cin >> mon[i]; } for (long long i = 1; i < n - 1; i++) { if (mon[i] > mon[i + 1] && mon[i] > mon[i - 1]) { peaks[i] = 1; } } long long totalPeaks = 0, ansTotal = 0, ansL = 0; long long l = 0, r = k - 1; for (long long i = l; i < r; i++) { totalPeaks += peaks[i]; } l++; ansTotal = totalPeaks; for (long long i = r + 1; i < n; i++) { if (peaks[i - 1] == 1) { totalPeaks++; } if (peaks[l] == 1) { totalPeaks--; } if (totalPeaks > ansTotal) { ansTotal = totalPeaks; ansL = l; } l++; } cout << ansTotal + 1 << " " << ansL + 1 << '\n'; } return 0; }
2
#include <bits/stdc++.h> using namespace std; inline int gcd(int a, int b) { while (1) { a = a % b; if (a == 0) return b; b = b % a; if (b == 0) return a; } } const int maxN = 1000 + 100; const int inf = 1e9; int a[3]; inline int f(int b, int c, int d) { if (b == 1) return c * d; else return ((b + c + d) * 2) - 6 + f(b - 1, c - 1, d - 1); } inline void init() { scanf("%d%d%d", &a[0], &a[1], &a[2]); sort(a, a + 3); } inline void solve() { printf("%d", f(a[0], a[1], a[2])); } int main() { ios_base::sync_with_stdio(0); init(); solve(); return 0; }
2
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #pragma GCC target("avx2") using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long k; cin >> k; long long pref = 0; vector<long long> poww10(17); poww10[0] = 1; for (int i = 1; i < 17; i++) { poww10[i] = poww10[i - 1] * 10; } for (int len = 1; len <= 15; len++) { if (pref + len * (poww10[len] - poww10[len - 1]) >= k) { k -= pref; long long l = 0, r = poww10[len] - poww10[len - 1]; while (l + 1 < r) { long long mid = (l + r) / 2; if (len * mid < k) { l = mid; } else { r = mid; } } k -= len * l; cout << to_string(poww10[len - 1] + l)[k - 1] << '\n'; return 0; } pref += len * (poww10[len] - poww10[len - 1]); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { long long n, k, c = 1; cin >> n >> k; while (true) { while (k - 3 >= 0 && (n - c) * 2 <= k - 3 && c <= n && c++) k -= 3; while (k - 4 >= 0 && (n - c) * 2 <= k - 4 && c <= n && c++) k -= 4; while (k - 5 >= 0 && (n - c) * 2 <= k - 5 && c <= n && c++) k -= 5; break; } cout << n - c + 1; return 0; }
0
#include <bits/stdc++.h> using std::max; using std::min; const int inf = 0x3f3f3f3f, Inf = 0x7fffffff; const long long INF = 0x3f3f3f3f3f3f3f3f; std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count()); template <typename _Tp> _Tp gcd(const _Tp &a, const _Tp &b) { return (!b) ? a : gcd(b, a % b); } template <typename _Tp> inline _Tp abs(const _Tp &a) { return a >= 0 ? a : -a; } template <typename _Tp> inline void chmax(_Tp &a, const _Tp &b) { (a < b) && (a = b); } template <typename _Tp> inline void chmin(_Tp &a, const _Tp &b) { (b < a) && (a = b); } template <typename _Tp> inline void read(_Tp &x) { char ch(getchar()); bool f(false); while (!isdigit(ch)) f |= ch == 45, ch = getchar(); x = ch & 15, ch = getchar(); while (isdigit(ch)) x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar(); f && (x = -x); } template <typename _Tp, typename... Args> inline void read(_Tp &t, Args &...args) { read(t); read(args...); } inline int read_str(char *s) { char ch(getchar()); while (ch == ' ' || ch == '\r' || ch == '\n') ch = getchar(); char *tar = s; *tar = ch, ch = getchar(); while (ch != ' ' && ch != '\r' && ch != '\n' && ch != EOF) *(++tar) = ch, ch = getchar(); return tar - s + 1; } const int N = 55; struct edge { int v, nxt, w, cost; } c[N * N << 2]; int front[N], edge_cnt = -1; inline void addedge(int u, int v, int w, int cost) { c[++edge_cnt] = (edge){v, front[u], w, cost}, front[u] = edge_cnt; c[++edge_cnt] = (edge){u, front[v], 0, -cost}, front[v] = edge_cnt; } int dep[N], cur[N], S, T, _q[N * N], _l, _r, pre[N]; bool inq[N]; bool bfs() { memset(dep, 63, sizeof(dep)); dep[S] = 0, _q[_l = _r = 1] = S; while (_l != _r + 1) { int x = _q[_l++]; inq[x] = false; for (int i = front[x]; ~i; i = c[i].nxt) { int v = c[i].v; if (c[i].w && dep[v] > dep[x] + c[i].cost) { dep[v] = dep[x] + c[i].cost, pre[v] = i; if (!inq[v]) _q[++_r] = v, inq[v] = true; } } } return dep[T] < inf; } int C[N], pos; void MCMF() { int ans = 0; while (bfs()) { int cur = T; while (cur != S) { int id = pre[cur]; --c[id].w, ++c[id ^ 1].w; ans += c[id].cost; cur = c[id ^ 1].v; } C[++pos] = ans; } } int main() { memset(front, 255, sizeof(front)); int n, m; read(n, m); int x, y, z; S = 1, T = n; for (int i = 1; i <= m; ++i) read(x, y, z), addedge(x, y, 1, z); MCMF(); int _; read(_); while (_--) { read(x); double ans = 1e18; for (int i = 1; i <= pos; ++i) chmin(ans, (double)(x + C[i]) / i); printf("%.8lf\n", ans); } return 0; }
11
#include <bits/stdc++.h> using namespace std; int l; char a[200010], b[200010]; int main() { scanf("%s", a); l = 0; for (int i = 0; a[i]; i++) if (l == 0 || a[i] != b[l - 1]) b[l++] = a[i]; else l--; b[l++] = 0; puts(b); return 0; }
3
#include <bits/stdc++.h> using namespace std; vector<int> adj[100000 + 5], ans1, ans2; int color[100000 + 5]; bool ok = 1; void dfs(int u, int col) { color[u] = col; int sz = adj[u].size(); for (int i = 0; i < sz; ++i) { int v = adj[u][i]; if (color[v] == -1) { dfs(v, 1 - col); } else { if (color[v] != 1 - col) ok = 0; } } } int main() { int n, m; int x, y; scanf("%d%d", &n, &m); for (int i = 0; i < m; ++i) { scanf("%d%d", &x, &y); x--, y--; adj[x].push_back(y); adj[y].push_back(x); } for (int i = 0; i < n; ++i) color[i] = -1; for (int i = 0; i < n; ++i) { if (color[i] == -1) { dfs(i, 0); } } if (!ok) puts("-1"); else { for (int i = 0; i < n; ++i) { if (color[i] == 0) ans1.push_back(i); else ans2.push_back(i); } printf("%d\n", ans1.size()); for (int i = 0; i < ans1.size(); ++i) { if (i == 0) printf("%d", ans1[i] + 1); else printf(" %d", ans1[i] + 1); } puts(""); printf("%d\n", ans2.size()); for (int i = 0; i < ans2.size(); ++i) { if (i == 0) printf("%d", ans2[i] + 1); else printf(" %d", ans2[i] + 1); } puts(""); } }
3
#include <bits/stdc++.h> using namespace std; void solve() { int r1, r2, c1, c2, d1, d2; cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2; for (int i = 1; i < 10; i++) { for (int j = 1; j < 10; j++) { if (i + j == r1 && i != j) { for (int k = 1; k < 10; k++) { if (i + k == c1 && k + j == d2 && j != k && i != k) { for (int l = 1; l < 10; l++) { if (l + k == r2 && l + i == d1 && l != k && l != i && j != l) { cout << i << " " << j << '\n'; cout << k << " " << l << '\n'; return; } } } } } } } cout << -1 << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; while (t--) { solve(); } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxN = 1e3 + 13; int k, q, n; double dp[maxN * 30][maxN]; vector<pair<double, int>> vec; int main() { cin >> k >> q; n = k * log2(k) + 13; for (int i = 1; i < n; i++) { for (int j = 1; j <= k; j++) { if (i == 1 && j == 1) dp[i][j] = 1; else dp[i][j] = (dp[i - 1][j] * j + dp[i - 1][j - 1] * (k - j + 1)) / k; } } for (int i = 1; i < n; i++) { vec.push_back({dp[i][k], i}); } while (q--) { double p; cin >> p; p /= 2000; auto ans = lower_bound(vec.begin(), vec.end(), make_pair(p, 0)); cout << (*ans).second << endl; } }
7