solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i; cin >> n >> m; cout << (n + m - 2 + n * m) << '\n'; for (i = 1; i <= m - 1; i++) cout << "L"; for (i = 1; i <= n - 1; i++) cout << "U"; for (i = 1; i <= n; i++) { for (int j = 1; j < m; j++) cout << (i & 1 ? "R" : "L"); cout << "D"; } }
4
#include <bits/stdc++.h> using namespace std; struct Message { int person = -1; vector<bool> v; int cnt = 0; bool add(int x) { if (!v[x]) { return false; } else { cnt--; v[x] = false; return true; } } string msg; }; vector<string> parseMsg(string msg) { msg += '.'; string s; vector<string> v; for (char c : msg) { if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) { s += c; } else { if (!s.empty()) v.push_back(s); s.clear(); } } return v; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t-- > 0) { int n; cin >> n; vector<string> names(n); map<string, int> ma; for (int i = 0; i < n; i++) { cin >> names[i]; ma[names[i]] = i; } int m; cin >> m; string s; getline(cin, s); vector<Message> messages(m); for (int i = 0; i < m; i++) { string s; getline(cin, s); int idx = s.find(':'); string name = s.substr(0, idx); messages[i].msg = s.substr(idx); auto msg = parseMsg(s.substr(idx + 1)); if (name != "?") { messages[i].person = ma[name]; messages[i].cnt = 1; messages[i].v.assign(n, false); messages[i].v[ma[name]] = true; } else { messages[i].v.assign(n, true); messages[i].cnt = n; for (auto w : msg) { if (ma.count(w)) messages[i].add(ma[w]); } } } for (int i = 0; i < m; i++) { if (i && messages[i - 1].person >= 0) { messages[i].add(messages[i - 1].person); } if (i + 1 < m && messages[i + 1].person >= 0) { messages[i].add(messages[i + 1].person); } } queue<int> q; for (int i = 0; i < m; i++) { auto& msg = messages[i]; if (msg.person < 0 && msg.cnt == 1) q.push(i); } while (!q.empty()) { auto x = q.front(); auto& msg = messages[x]; q.pop(); int idx; for (int i = 0; i < n; i++) { if (msg.v[i]) idx = i; } msg.person = idx; if (x && messages[x - 1].add(idx)) { if (messages[x - 1].cnt == 1) q.push(x - 1); } if (x + 1 < m && messages[x + 1].add(idx)) { if (messages[x + 1].cnt == 1) q.push(x + 1); } } bool possible = true; for (int i = 0; i < m; i++) { auto& msg = messages[i]; if (msg.person < 0) { if (msg.cnt == 0) possible = false; else { int idx; for (int i = 0; i < n; i++) { if (msg.v[i]) idx = i; } msg.person = idx; if (i + 1 < m) messages[i + 1].add(idx); } } } if (possible) { for (auto msg : messages) { cout << names[msg.person] << msg.msg << '\n'; } } else { cout << "Impossible" << '\n'; } } }
7
#include <bits/stdc++.h> using namespace std; long int mod = 1000000007; const int MAXN = 5000005; int spf[MAXN], arr[MAXN]; void sieve() { spf[1] = 1; for (int i = 2; i < MAXN; i++) spf[i] = i; for (int i = 4; i < MAXN; i += 2) spf[i] = 2; for (int i = 3; i * i < MAXN; i++) { if (spf[i] == i) { for (int j = i * i; j < MAXN; j += i) if (spf[j] == j) spf[j] = i; } } } int getFactorization(int x) { int count = 0; while (x > 1) { count++; x = x / spf[x]; } return count; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); memset(spf, 0, sizeof spf); sieve(); int sum = 0; for (long long i = 0; i < MAXN; i++) { int temp = getFactorization(i); sum += temp; arr[i] = sum; } int t; cin >> t; while (t--) { int l, r; cin >> l >> r; cout << arr[l] - arr[r] << '\n'; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; char a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int open = 0, close = 0; int res = 0; for (int i = 0; i < n; i++) { if (a[i] == ')') { close++; } else open++; } if (close != open) { cout << "-1\n"; return 0; } open = 0; close = 0; for (int i = 0; i < n; i++) { if (a[i] == ')') { if (open > 0) open--; else { res++; close++; } } else { if (close > 0) { res++; close--; } else { open++; } } } cout << res << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int n; long long A, B; struct node { long long vx, vy, w; } p[400005]; bool cmp(const node p1, const node p2) { if (p1.w == p2.w) return p1.vx < p2.vx; return p1.w < p2.w; } int main() { scanf("%d%I64d%I64d", &n, &A, &B); int i, j; long long gg; for (i = 1; i <= n; i++) scanf("%I64d%I64d%I64d", &gg, &p[i].vx, &p[i].vy); long long ans = 0; for (i = 1; i <= n; i++) { p[i].w = p[i].vy - p[i].vx * A; } sort(p + 1, p + n + 1, cmp); for (i = 1; i <= n; i++) { j = i; while (j + 1 <= n && p[j + 1].w == p[i].w) j = j + 1; long long num = j - i + 1; int k; for (int t = i; t <= j; t++) { k = t; while (k + 1 <= j && p[k + 1].vx == p[t].vx) k = k + 1; long long siz = k - t + 1; ans += siz * (num - siz); t = k; } i = j; } printf("%I64d", ans); return 0; }
6
#include <bits/stdc++.h> using namespace std; long long ma[30002]; long long dp[30002][502], n, d; long long solve(long long cur, long long le) { if (cur > 30000) return 0; long long jj = 250 + le - d; if (dp[cur][jj] != -1) return dp[cur][jj]; long long ans = 0; for (long long i = -1; i < 2; i++) { long long fin = cur + le + i; if (fin <= 30000 and fin > cur) ans = max(ans, solve(fin, le + i)); } return dp[cur][jj] = ans + ma[cur]; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); ; long long i, j; cin >> n >> d; memset(dp, -1, sizeof dp); for (i = 0; i < n; i++) { long long x; cin >> x; ma[x]++; } cout << solve(d, d); }
5
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int a[n + 5], b[n + 5]; for (int i = 1; i <= n; i++) cin >> a[i]; for (int j = 1; j <= n; j++) cin >> b[j]; bool flag = true; if (n % 2 && a[n / 2 + 1] != b[n / 2 + 1]) flag = false; map<pair<int, int>, int> mp; for (int i = 1; i <= n / 2; i++) { mp[{b[i], b[n - i + 1]}] += 1; mp[{b[n - i + 1], b[i]}] += 1; mp[{a[i], a[n - i + 1]}] -= 1; mp[{a[n - i + 1], a[i]}] -= 1; } for (auto it : mp) { if (it.second != 0) { flag = false; break; } } if (flag) cout << "Yes"; else cout << "No"; cout << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; int v[10000]; void swap(char &a, char &b) { char ch; ch = a; a = b; b = ch; } int main() { int n; int i, t, j; string s; while (cin >> n >> t) { cin >> s; for (i = 1; i <= t; i++) { for (j = s.size() - 1; j >= 0; j--) v[j] = 0; for (j = s.size() - 1; j >= 0; j--) { if (s[j] == 'G' && s[j - 1] == 'B' && v[j] == 0) { swap(s[j], s[j - 1]); v[j - 1] = 1; } } } cout << s << endl; } }
0
#include <bits/stdc++.h> using namespace std; struct Node { int key; int color; Node *l, *r, *p; }; struct Tree { Node *root; Node *begin, *end; int size; }; Tree *create_tree() { Tree *T = (Tree *)malloc(sizeof(Tree)); T->root = T->begin = T->end = NULL; T->size = 0; return T; } Node *right_rotate(Tree *T, Node *B) { if (!B->l) return B; Node *A = B->l; B->l = A->r; if (B->l) B->l->p = B; A->r = B; if (!B->p) T->root = A; else if (B->p->l == B) B->p->l = A; else B->p->r = A; A->p = B->p; B->p = A; return A; } Node *left_rotate(Tree *T, Node *A) { if (!A->r) return A; Node *B = A->r; A->r = B->l; if (A->r) A->r->p = A; B->l = A; if (!A->p) T->root = B; else if (A->p->l == A) A->p->l = B; else A->p->r = B; B->p = A->p; A->p = B; return B; } Node *next(Node *u) { if (!u) return NULL; if (u->r) { u = u->r; while (u->l) u = u->l; return u; } else { while (u->p && u->p->r == u) u = u->p; return u->p; } } Node *prev(Node *u) { if (!u) return NULL; if (u->l) { u = u->l; while (u->r) u = u->r; return u; } else { while (u->p && u->p->l == u) u = u->p; return u->p; } } int count(Tree *T, int key) { Node *u = T->root; while (u) { if (key < u->key) u = u->l; else if (key > u->key) u = u->r; else return 1; } return 0; } void insert(Tree *T, int key) { Node *x = T->root, *par = NULL; while (x) { par = x; if (key < x->key) x = x->l; else if (key > x->key) x = x->r; else return; } x = (Node *)malloc(sizeof(Node)); x->l = x->r = NULL; x->p = par; if (!x->p) T->root = T->begin = T->end = x; else if (key < x->p->key) { x->p->l = x; if (T->begin == x->p) T->begin = x; } else { x->p->r = x; if (T->end == x->p) T->end = x; } x->key = key; x->color = 1; T->size++; while (x->p != NULL and x->p->color == 1) { if (x->p == x->p->p->l) { Node *y = x->p->p->r; if (y && y->color == 1) { x->p->color = 0; y->color = 0; x->p->p->color = 1; x = x->p->p; } else { if (x == x->p->r) { x = x->p; left_rotate(T, x); } right_rotate(T, x->p->p); x->p->color = 0; x->p->r->color = 1; } } else { Node *y = x->p->p->l; if (y && y->color == 1) { x->p->color = 0; y->color = 0; x->p->p->color = 1; x = x->p->p; } else { if (x == x->p->l) { x = x->p; right_rotate(T, x); } left_rotate(T, x->p->p); x->p->color = 0; x->p->l->color = 1; } } } T->root->color = 0; } Node *clear(Node *u) { if (!u) return NULL; clear(u->l); clear(u->r); free(u); return NULL; } Tree *delete_tree(Tree *T) { if (T) { clear(T->root); free(T); } return NULL; } void transplant(Tree *T, Node *u, Node *v) { if (!u->p) T->root = v; else if (u == u->p->l) u->p->l = v; else u->p->r = v; if (v && u) v->p = u->p; } void Delete_fix(Tree *T, Node *x) { while (x and x != T->root and x->color == 0) { if (x == x->p->l) { Node *w = x->p->r; if (w->color == 1) { w->color = 0; x->p->color = 1; left_rotate(T, x->p); w = x->p->r; } if (w->l->color == 0 and w->r->color == 0) { w->color = 1; x = x->p; } else { if (w->r->color == 0) { w->l->color = 0; w->color = 1; right_rotate(T, w); w = x->p->r; } w->color = x->p->color; x->p->color = 0; w->r->color = 0; left_rotate(T, x->p); x = T->root; } } else { Node *w = x->p->l; if (w->color == 1) { w->color = 0; x->p->color = 1; left_rotate(T, x->p); w = x->p->l; } if (w->r->color == 0 and w->l->color == 0) { w->color = 1; x = x->p; } else { if (w->l->color == 0) { w->r->color = 0; w->color = 1; left_rotate(T, w); w = x->p->l; } w->color = x->p->color; x->p->color = 0; w->l->color = 0; right_rotate(T, x->p); x = T->root; } } } if (x) x->color = 0; } void Delete(Tree *T, Node *z) { Node *y = z, *x, *original_z = z; int y_original_color = y->color; if (T->begin == z) T->begin = next(T->begin); if (T->end == z) T->end = prev(T->end); if (!z->l) { x = z->r; transplant(T, z, z->r); } else if (!z->r) { x = z->l; transplant(T, z, z->l); } else { y = z->r; while (y->l) y = y->l; y_original_color = y->color; x = y->r; if (y->p == z) { x->p = y; } else { transplant(T, y, y->r); y->r = z->r; if (y->r) y->r->p = y; } transplant(T, z, y); y->l = z->l; y->l->p = y; y->color = z->color; } if (y_original_color == 0) { Delete_fix(T, x); } free(original_z); T->size--; if (T->root) T->root->color = 0; } const int N = 200006; char s[N]; int n; int p; vector<int> v[N]; Node *getmn(Tree *T) { Node *u = T->root; if (!u) return NULL; while (u->l) u = u->l; return u; } int main() { scanf("%s", s), n = (int)strlen(s); Tree *t[2]; t[0] = create_tree(); t[1] = create_tree(); for (int i = 0; i < n; i++) { if (s[i] == '0') { if (!t[0]->size) insert(t[0], p++); int id = t[0]->begin->key; Delete(t[0], t[0]->begin); insert(t[1], id); v[id].push_back(i + 1); } else { if (!t[1]->size) { delete_tree(t[0]); delete_tree(t[1]); return printf("-1\n"), 0; } int id = t[1]->begin->key; Delete(t[1], t[1]->begin); insert(t[0], id); v[id].push_back(i + 1); } } if (t[0]->size) { delete_tree(t[0]); delete_tree(t[1]); return printf("-1\n"), 0; } printf("%d\n", p); for (int i = 0; i < p; i++) { printf("%d", int(v[i].size())); for (int x : v[i]) printf(" %d", x); printf("\n"); } delete_tree(t[0]); delete_tree(t[1]); }
4
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << ": " << arg1 << endl; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << ": " << arg1 << " |"; __f(comma + 1, args...); } const int INF = 1 << 29; const int MOD = 1e9 + 7; const int N = 1e5 + 10; char s[N], t[N]; int main() { int n; scanf("%d", &n); scanf("%s%s", s, t); int ret = 0; for (int i = 0; i < n / 2; ++i) { int cur = min((s[i] != t[i]) + (s[n - 1 - i] != t[n - 1 - i]), (s[n - 1 - i] != t[i]) + (s[i] != t[n - 1 - i])); if (t[i] == t[n - 1 - i]) cur = min(cur, (int)(s[i] != s[n - 1 - i])); ret += cur; } if (n % 2 && s[n / 2] != t[n / 2]) ret += 1; printf("%d\n", ret); return 0; }
4
#include <bits/stdc++.h> using namespace std; vector<long long> a(16), v[2], row, col; int n, k; void g(int h, int in, long long cur) { if (in == (int)v[h].size()) { if (h == 0) row.push_back(cur); else col.push_back(cur); return; } long long temp = 1; for (int i = 0; i < 60; i++) { if (1.0l * temp * cur <= 1e18) g(h, in + 1, cur * temp); else break; if (1.0l * temp * v[h][in] > 1e18) break; temp *= v[h][in]; } } int get(long long mid) { int cur1 = 0, cur2 = 0, ans = 0; while (cur2 < (int)col.size() && 1.0l * row[cur1] * col[cur2] < mid) cur2++; cur2 = min(cur2, (int)col.size() - 1); while (cur1 < (int)row.size()) { while (cur2 >= 0 && 1.0l * row[cur1] * col[cur2] > mid) cur2--; ans += cur2 + 1; cur1++; } return ans; } void solve() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; cin >> k; for (int i = 0; i < n; i += 2) { v[0].push_back(a[i]); if (i + 1 < n) v[1].push_back(a[i + 1]); } g(0, 0, 1); g(1, 0, 1); sort((row).begin(), (row).end()); sort((col).begin(), (col).end()); long long lo = 0, hi = 1e18; while (hi - lo > 1) { long long mid = (lo + hi) / 2; if (get(mid) < k) lo = mid; else hi = mid; } cout << hi; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) solve(); return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { long long n; string s; string a = "0000000000"; cin >> n >> s; for (long long i = 0; i < n; i++) { if (s[i] == 'L') a[a.find('0')] = '1'; if (s[i] == 'R') a[a.rfind('0')] = '1'; else a[s[i] - '0'] = '0'; } cout << a; }
0
#include <bits/stdc++.h> using namespace std; bool debug = 1; class pes { public: int v; int x, dir; }; vector<pes> v; int n, s; vector<int> pl, pr; bool pos(long double t) { pl.clear(); pr.clear(); pl.resize(1e6 + 10, 0); pr.resize(1e6 + 10, 0); for (int i = 0; i < (n); i++) { if (v[i].dir == 2) continue; long double xp = v[i].x; long double vi = v[i].v; if (xp / vi <= t) { pl.clear(); pl.resize(1e6 + 10, 1); break; } if (xp / (vi + s) > t) continue; long double k = t * (vi + s) * vi; long double xe = (-k + xp * (vi + s)) / s; long double xr = s * (xp - xe) / vi + xe; pl[xp]++; pl[min(floor(xr), 1e6L) + 1]--; if (debug) cout << "i" << " = " << (i) << endl; if (debug) cout << "xp" << " = " << (xp) << endl; if (debug) cout << "min(floor(xr), 1e6L)" << " = " << (min(floor(xr), 1e6L)) << endl; if (debug) cout << endl; } if (debug) cout << "------" << endl; ; for (int i = 0; i < (n); i++) { if (v[i].dir == 1) continue; long double xp = 1e6 - v[i].x; if (debug) cout << "xp" << " = " << (xp) << endl; long double vi = v[i].v; if (debug) cout << "vi" << " = " << (vi) << endl; if (xp / vi <= t) { if (debug) cout << "n" << " = " << (n) << endl; pr.clear(); pr.resize(1e6 + 10, 1); break; } if (xp / (vi + s) > t) continue; long double k = t * (vi + s) * vi; if (debug) cout << "k" << " = " << (k) << endl; long double xe = (-k + xp * (vi + s)) / s; if (debug) cout << "xe" << " = " << (xe) << endl; long double xr = s * (xp - xe) / vi + xe; pr[1e6l - xp + 1]--; if (debug) cout << "1e6l - xp" << " = " << (1e6l - xp) << endl; if (debug) cout << "min(1e6L - floor(xr), 1e6L)" << " = " << (min(1e6L - floor(xr), 1e6L)) << endl; pr[max(1e6L - floor(xr), 0.L)]++; if (debug) cout << "i" << " = " << (i) << endl; if (debug) cout << "xp" << " = " << (xp) << endl; if (debug) cout << "max(1e6L - floor(xr), 0.L)" << " = " << (max(1e6L - floor(xr), 0.L)) << endl; if (debug) cout << endl; } if (debug) cout << "n" << " = " << (n) << endl; for (int i = (1); i <= (1e6); i++) { pl[i] += pl[i - 1]; pr[i] += pr[i - 1]; } for (int i = 0; i < (1e6 + 1); i++) { if (pl[i] < 0 or pr[i] < 0) { cout << "ERRO" << endl; exit(0); } if (pl[i] and pr[i]) return 1; } return 0; } int main() { cin >> n >> s; v.resize(n); for (int i = 0; i < (n); i++) { scanf("%d%d%d", &v[i].x, &v[i].v, &v[i].dir); } long double lo = 0, hi = 1e6 + 10; debug = 0; while (hi - lo > 1e-10) { long double mid = (lo + hi) / 2; if (pos(mid)) { hi = mid; } else { lo = mid; } if (debug) cout << "mid" << " = " << (mid) << endl; } printf("%.10Lf\n", lo); return 0; }
8
#include <bits/stdc++.h> using namespace std; int mod = 998244353; long long int gcd(long long int a, long long int b) { if (b == 0) return a; else return gcd(b, a % b); } int min(int a, int b) { if (a > b) return b; return a; } long long int max(long long int a, long long int b) { if (a > b) return a; return b; } int lcm(int a, int b) { return ((a / gcd(a, b)) * b); } int power(int a, int b) { int res = 1; while (b) { if (b % 2) { res = (res * 1LL * a) % mod; } b /= 2; a = (a * 1LL * a) % mod; } return res; } int par[2 * 100000 + 1]; int fin(int x) { if (par[x] == 0) return 0; if (par[x] == x) { return x; } else { int y = fin(par[x]); par[x] = y; return y; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; int w1 = 0; while (w1 < t) { w1++; int n, m; cin >> n >> m; int j = 0; long long int a[n + 1]; par[0] = 0; long long int mi = 1e12 + 1; for (int i = 1; i < n + 1; i++) { par[i] = 0; cin >> a[i]; if (a[i] < mi) { mi = a[i]; j = i; } } std::vector<pair<long long int, pair<int, int>>> v; for (int i = 1; i < n + 1; i++) { if (i != j) v.push_back(make_pair(mi + a[i], make_pair(i, j))); } long long int x, y, z; for (int i = 0; i < m; i++) { cin >> x >> y >> z; v.push_back(make_pair(z, make_pair(x, y))); } sort(v.begin(), v.end()); long long int ans = 0; int l = 0; int b = 0; while (l < n - 1) { z = v[b].first; x = (v[b].second).first; y = (v[b].second).second; int r = fin(x); int w = fin(y); if (r == 0 && w == 0) { ans += z; par[x] = x; par[y] = x; l++; } else if (r == 0) { ans += z; l++; par[x] = w; } else if (w == 0) { l++; ans += z; par[y] = r; } else { if (w != r) { l++; par[w] = r; ans += z; } } b++; } cout << ans; cout << "\n"; } return 0; }
5
#include <bits/stdc++.h> int main(int argc, char *argv[]) { int x = 2; int n, ans = 1; scanf("%d", &n); while ((x * x - x) / 2 <= n) { if (n % ((x * x - x) / 2) == 0) ans = (x * x - x) / 2; x *= 2; } printf("%d", ans); return 0; }
1
#include <bits/stdc++.h> using namespace std; const double eps = 1e-6; const int MOD = 1e9 + 7; const int INF = 2147000000; const long long inf = 1e18; long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } const int maxn = 3e5 + 100; int head[maxn], to[2 * maxn], Next[2 * maxn], w[2 * maxn], id[2 * maxn]; struct Edge { int from, to, w, id; }; vector<Edge> edges; int sz, n, m, k; void init() { sz = 0; memset(head, -1, sizeof(head)); } void add_edge(int a, int b, int c, int d) { ++sz; to[sz] = b; w[sz] = c; id[sz] = d; Next[sz] = head[a]; head[a] = sz; } struct HeapNode { int u; long long d; int from; bool operator<(const HeapNode& rhs) const { return d > rhs.d; } }; int done[maxn], p[maxn]; long long d[maxn]; Edge edge[maxn]; priority_queue<HeapNode> q; void dij() { q.push((HeapNode){1, 0, -1}); for (int i = 0; i <= n; i++) d[i] = inf; while (!q.empty()) { HeapNode x = q.top(); q.pop(); if (done[x.u]) continue; done[x.u] = 1; d[x.u] = x.d; p[x.u] = x.from; for (int i = head[x.u]; i != -1; i = Next[i]) { int v = to[i]; if (d[v] > d[x.u] + w[i]) { q.push((HeapNode){v, d[x.u] + w[i], id[i]}); } } } } struct Node { int u; long long d; int from; bool operator<(const Node& rhs) const { return d < rhs.d; } }; int vis[maxn]; priority_queue<Node> Q; int main() { scanf("%d%d%d", &n, &m, &k); init(); for (int i = 1; i <= m; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); add_edge(a, b, c, i); add_edge(b, a, c, i); edge[i] = (Edge){a, b, c, i}; } dij(); for (int i = 1; i <= n; i++) { if (p[i] != -1 && !vis[p[i]]) { edges.push_back(edge[p[i]]); vis[p[i]] = 1; Q.push((Node){i, d[i], p[i]}); } } if (edges.size() <= k) { printf("%d\n", edges.size()); for (int i = 0; i < edges.size(); i++) { printf("%d ", edges[i].id); } } else { int num = edges.size(); while (num > k && !Q.empty()) { Q.pop(); num--; } printf("%d\n", k); while (!Q.empty()) { printf("%d ", Q.top().from); Q.pop(); } } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 1000005 * 2; int n; char a[N], b[N]; int Next[N], len[N]; unsigned long long Ha[2][N], Hb[2][N], Fac[2][N], T[2] = {233, 65537}; unsigned long long Get_Hash(unsigned long long *Ha, unsigned long long *Fac, int L, int R) { if (L > R) L = n * 2 - L + 1, R = n * 2 - R + 1; return Ha[R] - Ha[L - 1] * Fac[R - L + 1]; } void KMP(char *a, char *b, int n) { Next[1] = 0; for (int i = 2; i <= n; i++) { int k = Next[i - 1]; while (k > 0 && a[k + 1] != a[i]) k = Next[k]; if (a[k + 1] == a[i]) k++; Next[i] = k; } int k = 0; for (int i = 1; i <= n; i++) { while (k > 0 && a[k + 1] != b[i]) k = Next[k]; if (a[k + 1] == b[i]) k++; len[i] = k; } } int Get(int j) { int L = 1, R = j - 1, mid, k1 = 0; while (L <= R) { mid = (L + R) >> 1; if (Get_Hash(Hb[0], Fac[0], 1, mid) == Get_Hash(Ha[0], Fac[0], j - 1, j - mid)) L = mid + 1, k1 = mid; else R = mid - 1; } if (k1 + len[j - 1] < j - 1) return -1; return len[j - 1]; } int main() { gets(a + 1); gets(b + 1); n = strlen(a + 1); if (strlen(b + 1) != n) return puts("-1 -1"), 0; reverse(a + 1, a + n + 1); for (int i = n + 1; i <= n * 2; i++) { a[i] = a[n * 2 - i + 1]; b[i] = b[n * 2 - i + 1]; } KMP(a, b, n); for (int t = 0; t < 1; t++) { for (int i = Fac[t][0] = 1; i <= n * 2; i++) Fac[t][i] = Fac[t][i - 1] * T[t]; for (int i = 1; i <= n * 2; i++) { Ha[t][i] = Ha[t][i - 1] * T[t] + a[i]; Hb[t][i] = Hb[t][i - 1] * T[t] + b[i]; } } int ansi = 10000000, ansj = 10000000; for (int j = n; j > 1; j--) { if (a[j] != b[j]) break; int i = Get(j); if (~i) if (j < ansj || (j == ansj && i > ansi)) ansi = i, ansj = j; } if (ansj == 10000000) ansi = ansj = -1; else ansi = n - ansi + 1 - 1, ansj = n - ansj + 1 - 1; printf("%d %d\n", ansj, ansi); return 0; }
8
#include <bits/stdc++.h> using namespace std; int const M = 2e5 + 10; vector<int> adj[M]; bool seen[2]; int p[M]; bool mark[M]; void dfs(int v, int l, int path) { if (adj[v].size() == 1) seen[path] = 1, p[v] = l; else p[v] = -1; for (int i = 0; i < adj[v].size(); i++) { int u = adj[v][i]; if (u != l) dfs(u, v, 1 - path); } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; int root = 1; for (int i = 1; i <= n - 1; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); if (adj[a].size() > 1) root = a; if (adj[b].size() > 1) root = b; } dfs(root, 0, 0); if (seen[1] && seen[0]) cout << 3 << " "; else cout << 1 << " "; int ans = n - 1; for (int i = 1; i <= n; i++) { if (adj[i].size() == 1) { if (mark[p[i]]) ans--; mark[p[i]] = 1; } } cout << ans; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; for (int i = 300001; i < 300001 + n; i++) { cout << i << " "; } return 0; }
2
#include <bits/stdc++.h> using namespace std; template <class P, class Q> inline P smin(P &a, Q b) { if (b < a) a = b; return a; } template <class P, class Q> inline P smax(P &a, Q b) { if (a < b) a = b; return a; } const long long N = 1e6 + 6; long long read() { long long x; cin >> x; return x; } long long h, t, a[N]; pair<long long, long long> s[N]; long long add = 0; long long when(pair<long long, long long> x, pair<long long, long long> y) { if (x.first > y.first) swap(x, y); return (y.first - x.first + x.second - y.second - 1) / (x.second - y.second); } long long solve(long long n, long long f = 0) { if (f) for (long long i = 0, _n = (long long)(n); i < _n; i++) a[i] *= -1; long long res = 0; h = 0, t = 0, add = 0; for (long long i = n - 1; i >= 0; --i) { add += -a[i + 1]; pair<long long, long long> cur = {-add, i}; bool flag = 1; while (t - h > 0) { long long arz = s[t - 1].first, shib = s[t - 1].second; if (arz >= cur.first && shib >= cur.second) { flag = 0; break; } if ((arz <= cur.first && shib <= cur.second) || (t - h > 1 && when(s[t - 1], s[t - 2]) <= when(s[t - 1], cur))) { t--; continue; } break; } if (flag) s[t++] = cur; long long bl = -2, br = t - 1; while (bl < br - 1) { long long bm = bl + br >> 1; long long x; if (bm < 0) x = 1e18; else if (bm == t - 1) x = -1e18; else x = when(s[bm], s[bm + 1]); if (x <= a[i]) br = bm; else bl = bm; } res = max(res, a[i] * (s[br].second - i) + s[br].first + add); } return res; } int32_t main() { ios_base ::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long n = read(); long long sum = 0; for (long long i = 0, _n = (long long)(n); i < _n; i++) a[i] = read(), sum += a[i] * (i + 1); long long res = solve(n); reverse(a, a + n); res = max(res, solve(n, 1)); cout << res + sum << endl; }
9
#include <bits/stdc++.h> using namespace std; int d[305], ml[305]; int S, T, len = 1, ans, E[305][305], a[305]; int n, head[305], mr[305], vis[305], cnt; struct way { int num, next, cap; } G[305 * 305]; void link(int x, int y, int z) { G[++len] = (way){y, head[x], z}, head[x] = len; G[++len] = (way){x, head[y], 0}, head[y] = len; } bool bfs() { int i, x; queue<int> Q; for (i = 1; i <= T; i++) d[i] = 0; d[S] = 1, Q.push(S); while (!Q.empty()) { x = Q.front(), Q.pop(); for (i = head[x]; i; i = G[i].next) if (G[i].cap && !d[G[i].num]) { d[G[i].num] = d[x] + 1; Q.push(G[i].num); } } return d[T] > 0; } int dfs(int t, int mini) { int i, ans = 0, res; if (t == T || !mini) return mini; for (i = head[t]; i; i = G[i].next) if (G[i].cap && d[G[i].num] == d[t] + 1) { res = dfs(G[i].num, min(mini, G[i].cap)); ans += res, mini -= res, G[i].cap -= res, G[i ^ 1].cap += res; if (!mini) return ans; } d[t] = (1 << 30); return ans; } bool find(int t) { int i, q; for (i = 1; i <= n; i++) { if (!E[t][i] || vis[i] == cnt) continue; q = mr[i], mr[i] = t, vis[i] = cnt; if (!q || find(q)) return 1; mr[i] = q; } return 0; } int main() { int i, j, x; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &len); for (j = 1; j <= len; j++) { scanf("%d", &x); E[i][x] = 1; } } len = 1, S = n + 1, T = S + 1; for (i = 1; i <= n; i++) { scanf("%d", &a[i]), a[i] = -a[i]; if (a[i] < 0) link(i, T, -a[i]); else ans += a[i], link(S, i, a[i]); } for (i = 1; i <= n; i++) ++cnt, find(i); for (i = 1; i <= n; i++) ml[mr[i]] = i; for (i = 1; i <= n; i++) for (j = 1; j <= n; j++) if (E[i][j] && j != ml[i]) link(i, mr[j], (1 << 30)); while (bfs()) ans -= dfs(S, (1 << 30)); cout << -ans; return 0; }
10
#include <bits/stdc++.h> using namespace std; int n, k; int v[5][1005], u[5][1005]; int dp[1005][1005]; bool mark[1005][1005]; int f(int last, int idx) { if (idx == n) return 0; if (mark[idx][last]) return dp[idx][last]; mark[idx][last] = 1; int ret = f(last, idx + 1); int toLook = v[0][idx + 1]; bool deu = true; for (int i = 1; i < k and deu; i++) if (u[i][last] > u[i][toLook]) return dp[idx][last] = ret; return dp[idx][last] = max(ret, f(toLook, idx + 1) + 1); ; } int main() { scanf("%d%d", &n, &k); for (int i = 0; i < k; i++) for (int j = 1; j <= n; j++) { scanf("%d", &v[i][j]); u[i][v[i][j]] = j; } printf("%d\n", f(0, 0)); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; long long ans = 0; for (int i = 0; i < min(n / 2, k); ++i) { ans += 2LL * (n - 1LL - 2LL * i) - 1; } cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> std::string getlast(std::string str, int value) { return str.substr(str.size() - value, value); } int main() { int tcase; scanf("%d", &tcase); std::string str; while (tcase--) { std::cin >> str; int len = str.size(); if (len >= 2) { if (getlast(str, 2) == "po") { std::cout << "FILIPINO" << "\n"; continue; } } if (len >= 4) { if (getlast(str, 4) == "desu" || getlast(str, 4) == "masu") { std::cout << "JAPANESE" << "\n"; continue; } } if (len >= 5) { if (getlast(str, 5) == "mnida") { std::cout << "KOREAN" << "\n"; continue; } } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; int read() { int x = 0, p = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') p = 0; ch = getchar(); } while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); return p ? x : -x; } int n, hd[500010], cnt, siz[500010], son[500010], dep[500010], val[500010]; struct Edge { int nxt, to, w; } e[500010]; inline void add(int u, int v, int c) { e[++cnt] = (Edge){hd[u], v, 1 << c}, hd[u] = cnt; } void dfs(int u) { siz[u] = 1; for (int i = hd[u]; i; i = e[i].nxt) { int v = e[i].to; dep[v] = dep[u] + 1, val[v] = val[u] ^ e[i].w; dfs(v), siz[u] += siz[v]; if (siz[v] > siz[son[u]]) son[u] = v; } } int buk[(1 << 22) + 10], ans[500010], mx, root; void calc(int u) { int sta, ex = dep[root] << 1; if (buk[val[u]]) mx = max(mx, dep[u] + buk[val[u]] - ex); for (int i = 0; i ^ 22; ++i) { if (buk[sta = (1 << i) ^ val[u]]) mx = max(mx, dep[u] + buk[sta] - ex); if ((val[u] ^ val[root]) == (1 << i)) mx = max(mx, dep[u] - dep[root]); } for (int i = hd[u]; i; i = e[i].nxt) calc(e[i].to); } void update(int u, int tag) { if (tag) buk[val[u]] = max(buk[val[u]], dep[u]); else buk[val[u]] = 0; for (int i = hd[u]; i; i = e[i].nxt) update(e[i].to, tag); } void dfs(int u, int keep) { for (int i = hd[u]; i; i = e[i].nxt) if (e[i].to ^ son[u]) dfs(e[i].to, 0); if (son[u]) dfs(son[u], 1); for (int i = hd[u]; i; i = e[i].nxt) mx = max(mx, ans[e[i].to]); int sta; if (buk[val[u]]) mx = max(mx, buk[val[u]] - dep[u]); for (int i = 0; i ^ 22; ++i) if (buk[sta = (1 << i) ^ val[u]]) mx = max(mx, buk[sta] - dep[u]); for (int i = hd[u]; i; i = e[i].nxt) if (e[i].to ^ son[u]) root = u, calc(e[i].to), update(e[i].to, 1); ans[u] = mx; if (!keep) update(u, 0), mx = 0; else buk[val[u]] = max(buk[val[u]], dep[u]); } int main() { n = read(); int p; char ch; for (int i = 2; i <= n; ++i) p = read(), ch = getchar(), add(p, i, ch - 'a'); dfs(1), dfs(1, 1); for (int i = 1; i <= n; ++i) printf("%d ", ans[i]); putchar('\n'); }
10
#include <bits/stdc++.h> using namespace std; int n, lx, ly, l, cnt[2 * 100005], len, aib[2 * 100005]; int v[2 * 100005], ll; unordered_map<int, int> M; struct el { int val, x, y; bool operator<(const el A) const { if (val == A.val) return x < A.x; return val < A.val; } } Ox[100005], Oy[100005]; struct ev { int x, tip, st, dr; bool operator<(const ev &A) const { if (x == A.x) return tip < A.tip; return x < A.x; } } Events[2 * 100005]; inline void Solve(el v[], int &n) { int m, i; if (!n) return; el aux[100005]; sort(v + 1, v + n + 1); m = 1; aux[1] = v[1]; for (i = 2; i <= n; ++i) if (v[i].val > aux[m].val) aux[++m] = v[i]; else if (v[i].x <= aux[m].y) aux[m].y = max(aux[m].y, v[i].y); else aux[++m] = v[i]; n = m; for (i = 1; i <= m; ++i) v[i] = aux[i]; } inline void upd(int p, int val) { for (int i = p; i <= len; i += (i & (-i))) aib[i] += val; } inline int qry(int p) { int sol = 0; for (int i = p; i; i -= (i & (-i))) sol += aib[i]; return sol; } inline void Normalize() { sort(v + 1, v + ll + 1); M[v[1]] = 1; for (int i = 2; i <= ll; ++i) { M[v[i]] = M[v[i - 1]]; if (v[i] > v[i - 1]) ++M[v[i]]; } len = M[v[ll]]; } int main() { int i, j, x1, x2, y1, y2; long long sol = 0; cin.sync_with_stdio(0); cin >> n; for (i = 1; i <= n; ++i) { cin >> x1 >> y1 >> x2 >> y2; if (x1 == x2) { ++lx; Ox[lx].val = x1; Ox[lx].x = min(y1, y2); Ox[lx].y = max(y1, y2); v[++ll] = y1; if (y1 != y2) v[++ll] = y2; } else { ++ly; Oy[ly].val = y1; Oy[ly].x = min(x1, x2); Oy[ly].y = max(x1, x2); v[++ll] = y1; } } Normalize(); Solve(Ox, lx); Solve(Oy, ly); for (i = 1; i <= ly; ++i) { sol += Oy[i].y - Oy[i].x + 1; ++l; Events[l].x = Oy[i].x; Events[l].tip = 1; Events[l].st = Events[l].dr = Oy[i].val; ++l; Events[l].x = Oy[i].y + 1; Events[l].tip = 2; Events[l].st = Events[l].dr = Oy[i].val; } for (i = 1; i <= lx; ++i) { ++l; Events[l].x = Ox[i].val; Events[l].tip = 3; Events[l].st = Ox[i].x; Events[l].dr = Ox[i].y; } sort(Events + 1, Events + l + 1); for (i = 1; i <= l; ++i) if (Events[i].tip == 1) { if (++cnt[M[Events[i].st]] == 1) upd(M[Events[i].st], 1); } else if (Events[i].tip == 2) { if (--cnt[M[Events[i].st]] == 0) upd(M[Events[i].st], -1); } else sol += (Events[i].dr - Events[i].st + 1) - (qry(M[Events[i].dr]) - qry(M[Events[i].st] - 1)); cout << sol << "\n"; return 0; }
7
#include <bits/stdc++.h> using namespace std; double power(double i, long long j) { if (j == 0) return 1.0; if (j == 1) return i; double ass = power(i, j / 2); if (j % 2 == 0) return ass * ass; return i * ass * ass; } int main() { long long n, m; scanf("%I64d%I64d", &n, &m); double x = 0; double y = (double)n; for (long long i = 1; i <= n - 1; i++) { x += power((double)i / n, m); } x = y - x; printf("%.10lf\n", x); }
4
#include <bits/stdc++.h> using namespace std; vector<long long> adj[100005]; bool visited[100005]; int main() { ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); int a[3], b[3]; for (int i = 0; i < 3; i++) cin >> a[i]; long long h = 0; for (int i = 0; i < 3; i++) { cin >> b[i]; if (a[i] != b[i]) h++; } if (h == 3) return cout << "NO", 0; cout << "YES"; return 0; }
1
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define YES cout<<"YES" #define NO cout<<"NO" #define Yes cout<<"Yes" #define No cout<<"No" #define yes cout<<"yes" #define no cout<<"no" #define FOR(S , E) for(ll i=S;i<=E;i++) #define ROF(E , S) for(ll i=E;i>=S;i--) // V.push_back() // V.pop_front()// V.top() //V.pop()//V.front() // V.back() // pop_back() //second // first // insert // continue // break // erase // St.push() // V.size() // S.length() // clear() // S.length() // reverse // greater // push int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); int T; cin>>T; while(T--) { int N; cin>>N; string S1 , S2; cin>>S1>>S2; map<char,char>Map; Map['0'] = '0'; Map['1'] = '1'; int Zeros = 0 , Ones = 0; for(char C : S1) { if(C == '1')Ones++; else Zeros++; } bool Cant = 0; for(int i=N-1;i>=0;i--) { if(Map[S1[i]] == S2[i]) { if(Map[S1[i]] == '1')Ones--; else Zeros--; continue; } if(Map[S1[i]]!=S2[i] && Zeros == Ones) { Map['0'] = Map['0'] == '0'?'1':'0'; Map['1'] = Map['1'] == '1'?'0':'1'; if(Map[S1[i]] == '1')Ones--; else Zeros--; } else if(Map[S1[i]]!=S2[i] && Zeros != Ones) { Cant = 1; break; } } if(Cant)NO; else YES; cout<<endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; long long i, j, k, n, m, t, l, r, mid; long long g[600000], inf, b[600000], c[600000], L[600000], R[600000], mi; inline bool judge() { int i, j; for (i = 1; i <= r - mid + 1; i++) { j = i + mid - 1; if (g[j] - g[i] > mi) continue; if (L[i] + R[j] <= m) return 1; } return 0; } int main() { g[l = r = 0] = 0; inf = mi = (long long)1e18 + 10; for (; l <= r; l++) { long long A = g[l] * 10 + 4; if (A < inf) g[++r] = A; A += 3; if (A < inf) g[++r] = A; } scanf("%I64d%I64d", &n, &m); for (i = 1; i <= n; i++) scanf("%I64d%I64d", &b[i], &c[i]), mi = min(c[i] - b[i], mi); sort(b + 1, b + n + 1); sort(c + 1, c + n + 1); sort(g + 1, g + r + 1); j = n; g[r + 1] = g[r]; for (i = r; i; i--) { long long A = g[i + 1] - g[i]; if (A && (inf - L[i + 1]) / A < (n - j)) L[i] = inf; else L[i] = L[i + 1] + A * (n - j); for (; j && b[j] >= g[i]; j--) L[i] = min(L[i] + b[j] - g[i], inf); } j = 1; g[0] = g[1]; for (i = 1; i <= r; i++) { long long A = g[i] - g[i - 1]; if (A && (inf - R[i - 1]) / A < (j - 1)) R[i] = inf; else R[i] = R[i - 1] + A * (j - 1); for (; j <= n && c[j] <= g[i]; j++) R[i] = min(R[i] + g[i] - c[j], inf); } int ll = 1, rr = r; for (; ll <= rr;) { mid = (ll + rr) / 2; if (judge()) ll = mid + 1; else rr = mid - 1; } printf("%d\n", ll - 1); return 0; }
8
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> ostream& operator<<(ostream& out, const pair<T1, T2> p) { out << '(' << p.first << ',' << p.second << ')'; return out; } template <class T1, class T2> istream& operator>>(istream& in, pair<T1, T2>& p) { in >> p.first >> p.second; return in; } template <class T> istream& operator>>(istream& in, vector<T>& v) { for (T& x : v) in >> x; return in; } template <class T> ostream& operator<<(ostream& out, const vector<vector<T>>& v) { for (const vector<T>& x : v) out << x << '\n'; return out; } template <class T> ostream& operator<<(ostream& out, const vector<T>& v) { for (const T& x : v) out << x << ' '; return out; } long long gcd(long long a, long long b) { if (b > a) swap(a, b); return (b ? gcd(b, a % b) : a); } using ll = long long; using pii = pair<int, int>; using pll = pair<long long, long long>; using tiii = pair<pair<int, int>, int>; using vi = vector<int>; using vl = vector<long long>; using vvi = vector<vector<int>>; using vvl = vector<vector<long long>>; const int h = 1000000007; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.setstate(ios::failbit); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; vvi MM(n); vector<set<int>> M(n); vi deg(n); for (int i = (0); i < (n - 1); i++) { int x, y; cin >> x >> y; x--, y--; MM[x].push_back(y); MM[y].push_back(x); deg[x]++, deg[y]++; } priority_queue<pii> pq; for (int i = (0); i < (n); i++) { if (deg[i] == 1) { int x = *MM[i].begin(); M[x].insert(i); pq.push({M[x].size(), x}); } } int ans = 0; while (!pq.empty()) { int d = pq.top().first; int x = pq.top().second; pq.pop(); if (M[x].size() != d) continue; if (d < k) break; ans += M[x].size() / k; int to_remove = M[x].size() / k * k; while (to_remove) { int z = *M[x].begin(); M[x].erase(z); M[z].erase(x); deg[x]--, deg[z]--; to_remove--; } if (deg[x] == 1) { for (int y : MM[x]) { if (deg[y] == 0) continue; M[y].insert(x); pq.push({M[y].size(), y}); } } } cout << ans << '\n'; } }
7
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long INF = 1e18; const int inf = 1e9; const int MX = 1e5 + 1; long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); } int main() { long long x0, y0, ax, ay, bx, by; cin >> x0 >> y0 >> ax >> ay >> bx >> by; long long xs, ys, t; cin >> xs >> ys >> t; vector<long long> first = {x0}; vector<long long> second = {y0}; while (true) { x0 = x0 * ax + bx; y0 = y0 * ay + by; if (x0 > xs + t || y0 > ys + t) break; first.push_back(x0); second.push_back(y0); } int nm = first.size(); vector<long long> dist(nm); for (int i = 0; i < (nm); ++i) dist[i] = abs(first[0] - first[i]) + abs(second[0] - second[i]); int ans = 0; for (int i = 0; i < (nm); ++i) { long long cDist = abs(xs - first[i]) + abs(ys - second[i]); int l = -1; int r = i + 1; while (r - l > 1) { int m = (l + r) >> 1; if (cDist + dist[i] - dist[m] <= t) r = m; else l = m; } ans = max(ans, i - r + 1); l = i - 1; r = nm; while (r - l > 1) { int m = (l + r) >> 1; if (cDist - dist[i] + dist[m] <= t) l = m; else r = m; } ans = max(ans, l - i + 1); } cout << ans << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long int q, l1, r1, l2, r2; cin >> q; while (q--) { cin >> l1 >> r1 >> l2 >> r2; cout << l1 << ' '; if (l2 == l1) { cout << l2 + 1 << endl; } else { cout << l2 << ' ' << endl; } } }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; ll n; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; vector<ll> a(n + 1, 0); for (ll i = 1; i <= n; ++i) cin >> a[i]; if (n == 1) { cout << 1 << " " << 1 << endl; cout << -a[1] << endl; cout << 1 << " " << 1 << endl; cout << 0 << endl; cout << 1 << " " << 1 << endl; cout << 0 << endl; return 0; } cout << 1 << " " << 1 << endl; cout << -a[1] << endl; a[1] = 0; cout << 2 << " " << n << endl; for (ll i = 2; i <= n; ++i) cout << (n - 1) * a[i] << " "; cout << endl; cout << 1 << " " << n << endl; for (ll i = 1; i <= n; ++i) cout << -a[i] * n << " "; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; sort(v.rbegin(), v.rend()); long long sum = 0; while (n > 0) { for (int i = 0; i < n; i++) sum += v[i]; n /= 4; } cout << sum; }
3
#include <bits/stdc++.h> using namespace std; long long modular_pow(long long base, long long exponent, long long modulus) { long long result = 1; while (exponent > 0) { if (exponent % 2 == 1) result = (result * base) % modulus; exponent = exponent >> 1; base = (base * base) % modulus; } return result; } void computePrefixTable(string pat, vector<int>& v) { int len = 0; v[0] = 0; int i = 1; while (i < pat.size()) { if (pat[i] == pat[len]) { len++; v[i] = len; i++; } else { if (len != 0) { len = v[len - 1]; } else { v[i] = 0; i++; } } } } long long countDistinctSubstrings(string s, int i, int j) { string r = s.substr(i, j - i + 1); vector<int> v(r.size(), 0); computePrefixTable(r, v); long long tot = v.size() * (v.size() + 1) / 2; for (int k = 0; k < v.size(); k++) { tot -= v[k]; } return tot; } long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); } long long numDigits(long long i) { return i > 0 ? (long long)log10((double)i) + 1 : 1; } long long dist(long long ax, long long ay, long long bx, long long by) { return (bx - ax) * (bx - ax) + (by - ay) * (by - ay); } int main() { ios_base::sync_with_stdio(false); long long ax, ay, bx, by, cx, cy; cin >> ax >> ay >> bx >> by >> cx >> cy; if (dist(ax, ay, bx, by) != dist(bx, by, cx, cy)) { cout << "No" << endl; return 0; } if (4 * dist(ax, ay, bx, by) == dist(ax, ay, cx, cy)) { cout << "No" << endl; return 0; } cout << "Yes" << endl; }
3
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; long long ans, pw[N], p, x; string s; int main() { pw[0] = 1; for (int i = 1; i < N; i++) pw[i] = pw[i - 1] * 2; ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> s; for (int i = 1; i < s.size(); i++) { if (((s[i - 1] - '0') * 10 + (s[i] - '0')) % 4 == 0) ans += i; } for (int i = 0; i < s.size(); i++) { if ((s[i] - '0') % 4 == 0) ans++; } cout << ans; return 0; }
2
#include <bits/stdc++.h> using namespace std; int a[102], b[102]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; int k = 1; for (int i = 0; i < n; i++) { if (a[i] == k) { b[i] = 1; k++; } } cout << k - 1 << endl; for (int i = 0; i < n; i++) { if (b[i] == 1) cout << 2001 + i << " "; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, k, i, ans = 0; string s; int main() { cin >> n >> k >> s; sort(s.begin(), s.end()); for (i = 1; i < s.size(); i++) if (s[i] - s[i - 1] <= 1) { s.erase(i, 1); i = i - 1; } if (s.size() < k) { cout << -1; return 0; } for (i = 0; i < k; i++) ans += s[i] - 96; cout << ans; }
0
#include <bits/stdc++.h> using namespace std; long long tm[1000]; long long f(long long k, long long d) { assert(((1LL << 62LL) + (1LL << 61LL)) / d >= (1LL << k) - 1LL + (d - 1LL) / 2LL); return ((1LL << k) - 1LL + (d - 1LL) / 2LL) * d; } int main() { long long nts = 0; long long n; cin >> n; for (long long k = 0; (1LL << k) <= n + 1LL; k++) { long long mx = 1; long long mn = 1; long long md; while (f(k, mx) <= n) mx *= 2LL; while (mx - mn > 1LL) { md = (mx + mn) / 2LL; if (f(k, md) > n) mx = md; else mn = md; } if (f(k, mn) == n && mn % 2LL == 1LL) tm[nts++] = (1LL << k) * mn; } sort(tm, tm + nts); for (long long i = 0; i < nts; i++) { cout << tm[i] << endl; } if (nts == 0) cout << -1 << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 6; const long long inf = N; vector<pair<long long, long long> > va[20], vb[20]; int x[N]; bool cek(long long a, long long b, long long w, long long h) { return (a <= w && b <= h) || (a <= h && b <= w); } int main() { int n, a, b, h, w; scanf("%d %d %d %d %d", &a, &b, &h, &w, &n); for (int i = 0; i < n; i++) scanf("%d", x + i); sort(x, x + n); va[0].clear(); va[0].emplace_back(1, 1); if (cek(a, b, h, w)) { puts("0"); return 0; } for (int i = 1; i <= min(17, n); i++) { va[i].clear(); long long now = x[n - i]; for (auto it : va[i - 1]) { va[i].emplace_back(min(inf, now * it.first), it.second); va[i].emplace_back(it.first, min(inf, now * it.second)); } for (auto it : va[i]) { if (cek(a, b, it.first * h, it.second * w)) { printf("%d\n", i); return 0; } } } if (n > 17) { sort(va[17].begin(), va[17].end()); long long cur = 0; for (int i = (int)va[17].size() - 1; i >= 0; i--) { cur = max(cur, va[17][i].second); va[17][i].second = cur; } vb[0].clear(); vb[0].emplace_back(1, 1); for (int i = 1; i <= min(17, n - 17); i++) { vb[i].clear(); long long now = x[n - 17 - i]; for (auto it : vb[i - 1]) { vb[i].emplace_back(min(inf, now * it.first), it.second); vb[i].emplace_back(it.first, min(inf, now * it.second)); } sort(vb[i].begin(), vb[i].end()); for (int j = 0, k = (int)va[17].size(); j < vb[i].size(); j++) { while (k > 0 && va[17][k - 1].first * vb[i][j].first * w >= a) k--; if (k < va[17].size()) { if (cek(a, b, va[17][k].first * vb[i][j].first * w, va[17][k].second * vb[i][j].second * h)) { printf("%d\n", i + 17); return 0; } } } for (int j = 0, k = (int)va[17].size(); j < vb[i].size(); j++) { while (k > 0 && va[17][k - 1].first * vb[i][j].first * w >= b) k--; if (k < va[17].size()) { if (cek(a, b, va[17][k].first * vb[i][j].first * w, va[17][k].second * vb[i][j].second * h)) { printf("%d\n", i + 17); return 0; } } } } } puts("-1"); return 0; }
6
#include <bits/stdc++.h> using namespace std; template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; } long long f[2001], dp[2001][2001], m = 998244353; long long fun(long long i, long long j) { if (i == 0) return (f[j]); else return (f[j] - f[i - 1]); } long long fpow(long long a, long long p) { if (p == 0) return (1); long long b = fpow(a, p / 2); if (p % 2 == 0) return ((b * b) % m); else return ((((b * b) % m) * a) % m); } int main() { string s; cin >> s; if (s[0] == '?') f[0] = 1; for (long long i = 1; i < s.length(); i++) { if (s[i] == '?') f[i] += 1; f[i] += f[i - 1]; } for (long long i = 2; i <= s.length(); i++) { for (long long j = 0; j <= (s.length() - i); j++) { if (s[j] != '(') dp[j][j + i - 1] = (dp[j][j + i - 1] + dp[j + 1][j + i - 1]) % m; if (s[j + i - 1] != ')') dp[j][j + i - 1] = (dp[j][j + i - 1] + dp[j][j + i - 2]) % m; if ((s[j] != '(') && (s[j + i - 1] != ')')) dp[j][j + i - 1] = (dp[j][j + i - 1] - dp[j + 1][j + i - 2] + m) % m; if ((s[j] != ')') && (s[j + i - 1] != '(')) dp[j][j + i - 1] = (dp[j][j + i - 1] + dp[j + 1][j + i - 2] + (long long)fpow(2, fun(j + 1, j + i - 2))) % m; } } cout << dp[0][s.length() - 1]; }
9
#include <bits/stdc++.h> using namespace std; const int N = 3003; int g, g0, n, m, a, b, x, y, z, grid[N][N]; long long ans; vector<int> v[N]; deque<int> dq; void solve() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { while (!dq.empty() && dq.front() <= j - b) dq.pop_front(); while (!dq.empty() && grid[i][dq.back()] >= grid[i][j]) dq.pop_back(); dq.push_back(j); if (j >= b) v[i].push_back(grid[i][dq.front()]); } dq.clear(); } for (int j = 0; j < v[1].size(); j++) { for (int i = 1; i <= n; i++) { while (!dq.empty() && dq.front() <= i - a) dq.pop_front(); while (!dq.empty() && v[dq.back()][j] >= v[i][j]) dq.pop_back(); dq.push_back(i); if (i >= a) ans += v[dq.front()][j]; } dq.clear(); } } int main() { scanf("%d%d%d%d%d%d%d%d", &n, &m, &a, &b, &g0, &x, &y, &z); g = g0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { grid[i][j] = g; g = (1LL * g0 * x + y) % z; g0 = g; } } solve(); cout << ans; return 0; }
6
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; std::vector<std::pair<int, int> > cur; void get(int n) { if (n <= 1) return; int k = n - 4; get(n - 4); for (int i = 1; i < k + 1; i++) cur.push_back(make_pair(i, k + 1)); cur.push_back(make_pair(k + 1, k + 2)); for (int i = k; i >= 1; i--) cur.push_back(make_pair(i, k + 2)); for (int i = 1; i < k + 1; i++) cur.push_back(make_pair(i, k + 3)); cur.push_back(make_pair(k + 3, k + 4)); for (int i = k; i >= 1; i--) cur.push_back(make_pair(i, k + 4)); cur.push_back(make_pair(k + 1, k + 4)); cur.push_back(make_pair(k + 2, k + 3)); cur.push_back(make_pair(k + 1, k + 3)); cur.push_back(make_pair(k + 2, k + 4)); } int main() { int n; cin >> n; int m = (n * (n - 1)) / 2; if (m % 2 == 1) { cout << "NO"; return 0; } printf("YES\n"); get(n); for (auto x : cur) printf("%d %d\n", x.first, x.second); }
11
#include <bits/stdc++.h> using namespace std; signed main() { ios::sync_with_stdio(false); int Q; cin >> Q; while (Q--) { int64_t N; cin >> N; auto f = [&](int64_t x) { return N - x + min(N - x, x * (x - 1) / 2); }; int64_t lb = 1, ub = N; while (ub - lb > 2) { int64_t x = lb + (ub - lb) / 3; int64_t y = lb + (ub - lb) * 2 / 3; if (f(x) > f(y)) ub = y; else lb = x; } int64_t ans = 0; for (int i = lb; i <= ub; ++i) { ans = max(ans, f(i)); } cout << ans << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; int a[100005]; int last[100005]; int lson[5000005]; int rson[5000005]; int data[5000005]; int roots[100005]; int tot; int build(int l, int r) { int cur = ++tot; int mid = (l + r) >> 1; if (l != r) { lson[cur] = build(l, mid); rson[cur] = build(mid + 1, r); } return cur; } int modify(int root, int l, int r, int pos, int x) { int cur = ++tot; data[cur] = data[root] + x; int mid = (l + r) >> 1; if (l != r) { if (pos <= mid) { rson[cur] = rson[root]; lson[cur] = modify(lson[root], l, mid, pos, x); } else { lson[cur] = lson[root]; rson[cur] = modify(rson[root], mid + 1, r, pos, x); } } return cur; } int query(int root, int l, int r, int k) { if (l == r) return r; int mid = (l + r) >> 1; if (data[lson[root]] <= k) return query(rson[root], mid + 1, r, k - data[lson[root]]); else return query(lson[root], l, mid, k); } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", a + i); roots[n + 1] = build(1, n); for (int i = n; i >= 1; i--) { if (last[a[i]] != 0) { int root = modify(roots[i + 1], 1, n, last[a[i]], -1); roots[i] = modify(root, 1, n, i, 1); } else { roots[i] = modify(roots[i + 1], 1, n, i, 1); } last[a[i]] = i; } for (int i = 1; i <= n; i++) { int ans = 0, pos = 1; do { if (data[roots[pos]] <= i) { pos = n + 1; ans++; } else { pos = query(roots[pos], 1, n, i); ans++; } } while (pos <= n); printf("%d ", ans); } return 0; }
8
#include <bits/stdc++.h> using namespace std; vector<long long> a, b; vector<pair<long long, char> > ans; long long n, k; bool check(int lf, int rt) { bool b = 0; long long se = 0; for (int i = lf; i <= rt; i++) { long long sum = a[i], l = i - 1, r = i + 1, cur = i; vector<pair<long long, char> > v; while (l >= lf || r <= rt) { if (l >= lf && a[l] < sum) { sum += a[l--]; v.push_back({cur, 'L'}); cur--; } else if (r <= rt && a[r] < sum) { sum += a[r++]; v.push_back({cur, 'R'}); } else break; } if (l < lf && r > rt) { for (int j = 0; j < v.size(); j++) ans.push_back(v[j]); se = sum; b = 1; break; } } int len = rt - lf; a[rt] = se; while (len--) { a.erase(a.begin() + lf); } return b; } int main() { cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; a.push_back(x); } cin >> k; for (int i = 0; i < k; i++) { int x; cin >> x; b.push_back(x); } for (int i = 0; i < b.size(); i++) { long long sum = 0, j = i; while (j < a.size() && sum < b[i]) { sum += a[j++]; } if (sum != b[i]) return cout << "NO\n", 0; if (!check(i, j - 1)) return cout << "NO\n", 0; } if (a.size() > b.size()) return cout << "NO\n", 0; cout << "YES\n"; for (int i = 0; i < ans.size(); i++) cout << ans[i].first + 1 << " " << ans[i].second << "\n"; return 0; }
5
#include<bits/stdc++.h> typedef int LL; typedef double dl; #define opt operator #define pb push_back #define pii std::pair<LL,LL> const LL maxn=1e3+9,mod=998244353,inf=0x3f3f3f3f; LL Read(){ LL x(0),f(1); char c=getchar(); while(c<'0' || c>'9'){ if(c=='-') f=-1; c=getchar(); } while(c>='0' && c<='9'){ x=(x<<3ll)+(x<<1ll)+c-'0'; c=getchar(); }return x*f; } void Chkmin(LL &x,LL y){ if(y<x) x=y; } void Chkmax(LL &x,LL y){ if(y>x) x=y; } LL add(LL x,LL y){ return x+=y,x>=mod?x-mod:x; } LL dec(LL x,LL y){ return x-=y,x<0?x+mod:x; } LL mul(LL x,LL y){ return 1ll*x*y%mod; } LL Pow(LL base,LL b){ LL ret(1); while(b){ if(b&1) ret=mul(ret,base); base=mul(base,base); b>>=1; }return ret; } //#define check namespace Check{ LL n,m; LL a[maxn][maxn]; void Init(LL n0,LL m0){ n=n0; m=m0; for(LL i=1;i<=n;++i){ for(LL j=1;j<=m;++j){ // if(i&1) a[i][j]=1; // else a[i][j]=2; // a[i][j]=rand()%2; a[i][j]=Read(); } } } LL Query(LL h,LL w,LL i1,LL j1,LL i2,LL j2){ LL flag(1); assert(i1+h-1<=n); assert(j1+w-1<=m); assert(i2+h-1<=n); assert(j2+w-1<=m); static LL mark[maxn][maxn]; for(LL i=1;i<=n;++i){ for(LL j=1;j<=m;++j) mark[i][j]=0; } for(LL i=1;i<=h;++i){ for(LL j=1;j<=w;++j){ mark[i1+i-1][j1+j-1]=1; } } for(LL i=1;i<=h;++i){ for(LL j=1;j<=w;++j){ assert(!mark[i2+i-1][j2+j-1]); } } for(LL i=1;i<=h;++i){ for(LL j=1;j<=w;++j){ if(a[i1+i-1][j1+j-1]!=a[i2+i-1][j2+j-1]){ return 0; } } } return 1; } } LL n,m,ans1,ans2; LL Query(LL h,LL w,LL i1,LL j1,LL i2,LL j2){ assert(i1+h-1<=n); assert(j1+w-1<=m); assert(i2+h-1<=n); assert(j2+w-1<=m); printf("? %d %d %d %d %d %d\n",h,w,i1,j1,i2,j2); fflush(stdout); #ifdef check return Check::Query(h,w,i1,j1,i2,j2); #endif #ifndef check return Read(); #endif } void Solve1(){ LL r(n); LL tmp; LL R(n); for(LL i=2;i<=R;++i) if(R%i==0){ while(R%i==0) R/=i; while(r%i==0){ // printf("# %d\n",i); if(i==2){ tmp=Query(r/2,m,1,1,r/2+1,1); if(!tmp) break; }else if(i==3){ tmp=Query(r/3,m,1,1,r/3+1,1); if(!tmp) break; tmp=Query(r/3,m,r/3+1,1,2*(r/3)+1,1); if(!tmp) break; }else{ { LL len((i-1)/2); tmp=Query(len*(r/i),m,1,1,len*(r/i)+1,1); if(!tmp) break; tmp=Query(len*(r/i),m,1,1,(len+1)*(r/i)+1,1); if(!tmp) break; } if(!(i&1)){ LL len(i/2); tmp=Query(len*(r/i),m,1,1,len*(r/i)+1,1); if(!tmp) break; } } r/=i; } } for(LL i=1;i*r<=n;++i){ if(n%(i*r)==0){ ++ans1; } } } void Solve2(){ LL c(m); LL tmp; LL C(m); for(LL i=2;i<=C;++i) if(C%i==0){ while(C%i==0) C/=i; while(c%i==0){ // printf("# %d\n",i); if(i==2){ tmp=Query(n,c/2,1,1,1,c/2+1); if(!tmp) break; }else if(i==3){ tmp=Query(n,c/3,1,1,1,c/3+1); if(!tmp) break; tmp=Query(n,c/3,1,1,1,2*(c/3)+1); if(!tmp) break; }else{ { LL len((i-1)/2); tmp=Query(n,len*(c/i),1,1,1,len*(c/i)+1); if(!tmp) break; tmp=Query(n,len*(c/i),1,1,1,(len+1)*(c/i)+1); if(!tmp) break; } if(!(i&1)){ LL len(i/2); tmp=Query(n,len*(c/i),1,1,1,len*(c/i)+1); if(!tmp) break; } } c/=i; } } for(LL i=1;i*c<=m;++i){ if(m%(i*c)==0){ ++ans2; } } } int main(){ // srand(time(NULL)); // freopen("y1.txt","r",stdin); n=Read(); m=Read(); // n=m=512; #ifdef check Check::Init(n,m); #endif Solve1(); Solve2(); printf("! %d\n",ans1*ans2); fflush(stdout); return 0; }
9
#include <bits/stdc++.h> using namespace std; const int N = 200005; long long s[N], e[N]; int T, cur, w, l; long long win(long long x, long long y) { if (y & 1) { if (x & 1) return 0; return 1; } if (x > y / 2) { if (x & 1) return 1; return 0; } if (x > y / 4) return 1; return win(x, y >> 2); } long long lose(long long x, long long y) { if (x > y / 2) return 1; return win(x, y >> 1); } int main() { scanf("%d", &T); for (int i = 1; i <= T; i++) scanf("%lld%lld", &s[i], &e[i]); for (int i = 1; i <= T; i++) { w = win(s[i], e[i]); l = lose(s[i], e[i]); w ^= cur; l ^= cur; if (w == l) break; if (w) cur = 1; else cur = 0; } printf("%d %d\n", w, l); }
9
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 100; const double PI = 3.1415926536; vector<string> v; long long n, m, k, t, sum, r, l, d, b, c, a, p, e; map<int, string> mp; bool f; string s; stack<char> st; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> s; n = s.size(); string x = ""; for (int i = 0; i < n; i++) { if (s[i] == '>') x += "1000"; else if (s[i] == '<') x += "1001"; else if (s[i] == '+') x += "1010"; else if (s[i] == '-') x += "1011"; else if (s[i] == '.') x += "1100"; else if (s[i] == ',') x += "1101"; else if (s[i] == '[') x += "1110"; else x += "1111"; } reverse(x.begin(), x.end()); n = x.size(); for (int i = 0; i < n; i++) { long long left = x[i] - '0'; if (left) { a = 1; for (int j = 0; j < i; j++) { a *= 2; a %= 1000003; } c += a; c %= 1000003; } } cout << c << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int n; const int maxn = 1000000; long long a[10]; long long b[10]; int bel[10]; bool is_fact[10][10]; bool is_prime[10]; int fact[10]; int work(long long val) { int cnt = 0; for (int i = 2; i <= maxn; i++) { while (val % i == 0) { val /= i; cnt++; } } if (val > 1) cnt++; return cnt; } bool dfs() { bel[0]++; for (int i = 0; i < n; i++) { if (bel[i] >= n) { bel[i + 1]++; bel[i] = i; } } if (bel[n] > 0) return 0; return 1; } bool prime(long long n) { for (long long i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return n != 1; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%I64d", &a[i]); sort(a, a + n); n = unique(a, a + n) - a; for (int i = 0; i < n; i++) { if (prime(a[i])) is_prime[i] = 1; } for (int i = 0; i < n; i++) { fact[i] = work(a[i]); } for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) { if (a[j] % a[i] == 0) is_fact[i][j] = 1; } long long tmp = 0x3f3f3f3f; long long mii = tmp * tmp; long long ans = 0; bool ok = 1; for (int i = 0; i < n; i++) bel[i] = i; while (ok) { for (int i = 0; i < n; i++) b[i] = a[i]; bool yes = 1; for (int i = 0; i < n; i++) { if (bel[i] == i) continue; if (b[bel[i]] < a[i]) { yes = 0; break; } if (b[bel[i]] % a[i] != 0) { yes = 0; break; } b[bel[i]] /= a[i]; } if (yes) { long long ans = 0; int cnt = 0; bool psb = 1; for (int i = 0; i < n; i++) { if (bel[i] == i) { if (!is_prime[i]) ans += fact[i]; ans++; cnt++; if (ans >= mii) { psb = 0; break; } } } if (psb) { for (int i = 0; i < n; i++) { if (bel[i] != i) { if (!is_prime[i]) { ans++; if (ans >= mii) { psb = 0; break; } } } } if (psb) { if (cnt > 1) ans++; mii = min(mii, ans); } } } ok = 0; if (dfs()) ok = 1; } printf("%I64d\n", mii); return 0; }
7
#include <bits/stdc++.h> using namespace std; template <typename T> inline T read() { register T sum = 0; register char cc = getchar(); int sym = 1; while (cc != '-' && (cc > '9' || cc < '0')) cc = getchar(); if (cc == '-') sym = -1, cc = getchar(); sum = sum * 10 + cc - '0'; cc = getchar(); while (cc >= '0' && cc <= '9') sum = sum * 10 + cc - '0', cc = getchar(); return sym * sum; } template <typename T> inline T read(T &a) { a = read<T>(); return a; } template <typename T, typename... Others> inline void read(T &a, Others &...b) { a = read(a); read(b...); } struct Edge { int v; Edge *next; Edge(int a = 0, Edge *b = NULL) { v = a; next = b; } } * head[500010 << 2]; int T, n, cnt, deg[500010 << 2], dis[500010 << 2], End[500010 << 2], pos[500010]; void add(int x, int y) { head[x] = new Edge(y, head[x]); deg[y] += 1; } void build(int k, int l, int r) { head[k] = NULL; End[k] = dis[k] = deg[k] = 0; if (l == r) { End[k] = l; pos[l] = k; return; } int mid = (l + r) >> 1; build(k << 1, l, mid); build(k << 1 | 1, mid + 1, r); add(k << 1, k); add(k << 1 | 1, k); } void change(int k, int l, int r, int x, int y, int z) { if (l >= x && r <= y) { add(k, pos[z]); return; } int mid = (l + r) >> 1; if (x <= mid) change(k << 1, l, mid, x, y, z); if (y > mid) change(k << 1 | 1, mid + 1, r, x, y, z); } void topsort() { queue<int> Q; for (int i = 1; i <= n; i++) if (!deg[pos[i]]) Q.push(pos[i]); while (!Q.empty()) { int t = Q.front(); Q.pop(); if (End[t]) dis[End[t]] = ++cnt; for (Edge *i = head[t]; i != NULL; i = i->next) { deg[i->v] -= 1; if (!deg[i->v]) Q.push(i->v); } } } void print() { for (int j = 1; j <= n; j++) if (!dis[j]) { puts("-1"); return; } for (int j = 1; j <= n; j++) printf("%d ", dis[j]); putchar('\n'); } int main() { T = read<int>(); for (int cas = 1; cas <= T; cas++) { cnt = 0; n = read<int>(); build(1, 1, n); for (int i = 1; i <= n; i++) { int r = read<int>(); if (r == -1) continue; if (i + 1 <= r - 1) change(1, 1, n, i + 1, r - 1, i); if (r <= n) change(1, 1, n, i, i, r); } topsort(); print(); } return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n, x, y, m, ans = 0, lastX = -1, lastY = -1, h, d; scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d %d", &x, &y); if (lastX == -1) { lastX = x; lastY = y; ans = x + y - 1; d = x; h = y; continue; } if (abs(lastY - y) >= x - lastX + 1) { cout << "IMPOSSIBLE" << endl; return 0; } while (d < x - 1) { if (h <= y) h++; else if (h - y + 1 < x - d) h++; else h--; ans = max(ans, h); d++; } d = x; h = y; lastX = x; lastY = y; ans = max(ans, h); } ans = max(ans, h + n - d); cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 3010; const int INF = 1e9; const int MOD = 1e9 + 7; int rnk[MAXN], par[MAXN]; int n, m; vector<pair<pair<int, int>, int> > mv, sv; int vis[MAXN]; int getpar(int a) { return par[a] == a ? a : par[a] = getpar(par[a]); } bool merge(int a, int b) { int pa = getpar(a); int pb = getpar(b); if (pa == pb) { return false; } if (rnk[pa] < rnk[pb]) { par[pa] = pb; } else if (rnk[a] > rnk[pb]) { par[pb] = pa; } else { par[pa] = pb; rnk[pb]++; } return true; } void init() { for (int i = 0; i < n; i++) { rnk[i] = 1; par[i] = i; } } int main() { cin >> n >> m; for (int i = 0; i < m; i++) { int x, y; char p; cin >> x >> y >> p; x--; y--; if (p == 'S') { sv.push_back(pair<pair<int, int>, int>(pair<int, int>(x, y), i)); } else if (p == 'M') { mv.push_back(pair<pair<int, int>, int>(pair<int, int>(x, y), i)); } } if (n % 2 == 0) { cout << "-1" << endl; return 0; } init(); int eachm = (n - 1) / 2; for (int i = 0; i < sv.size(); i++) { merge(sv[i].first.first, sv[i].first.second); } vector<pair<pair<int, int>, int> > add[2]; for (int i = 0; i < mv.size(); i++) { add[merge(mv[i].first.first, mv[i].first.second)].push_back(mv[i]); } if (add[1].size() > eachm) { cout << "-1" << endl; return 0; } init(); vector<int> res; for (int i = 0; i < add[1].size(); i++) { merge(add[1][i].first.first, add[1][i].first.second); res.push_back(add[1][i].second); } for (int i = 0; res.size() < eachm && i < add[0].size(); i++) { if (merge(add[0][i].first.first, add[0][i].first.second)) { res.push_back(add[0][i].second); } } if (res.size() != eachm) { cout << "-1" << endl; return 0; } for (int i = 0; res.size() < 2 * eachm && i < sv.size(); i++) { if (merge(sv[i].first.first, sv[i].first.second)) { res.push_back(sv[i].second); } } if (res.size() != 2 * eachm) { cout << "-1" << endl; return 0; } cout << res.size() << endl; for (int i = 0; i < 2 * eachm; i++) { cout << res[i] + 1 << " "; } cout << endl; }
7
#include <bits/stdc++.h> using namespace std; const int maxn = 200010; int Laxt[maxn], Next[maxn], To[maxn], cost[maxn], dis[maxn], fa[maxn], cnt; int a[maxn], vis[maxn], tN, S, T, ans = 1e9 + 7; int Sdis[maxn], Tdis[maxn], Ldis[maxn]; multiset<int> s; void add(int u, int v, int c) { Next[++cnt] = Laxt[u]; Laxt[u] = cnt; To[cnt] = v; cost[cnt] = c; } void dfs(int u, int f) { fa[u] = f; for (int i = Laxt[u]; i; i = Next[i]) { if (To[i] != f) { dis[To[i]] = dis[u] + cost[i]; dfs(To[i], u); } } } void Sdfs(int u, int f) { for (int i = Laxt[u]; i; i = Next[i]) { if (To[i] != f) { Sdis[To[i]] = Sdis[u] + cost[i]; Sdfs(To[i], u); } } } int Ldfs(int u, int f) { int res = Ldis[u]; for (int i = Laxt[u]; i; i = Next[i]) { if (To[i] == f || vis[To[i]]) continue; Ldis[To[i]] = Ldis[u] + cost[i]; res = max(res, Ldfs(To[i], u)); } return res; } void Tdfs(int u, int f) { for (int i = Laxt[u]; i; i = Next[i]) { if (To[i] != f) { Tdis[To[i]] = Tdis[u] + cost[i]; Tdfs(To[i], u); } } } int main() { int N, K, u, v, c, i, j; scanf("%d%d", &N, &K); for (i = 1; i < N; i++) { scanf("%d%d%d", &u, &v, &c); add(u, v, c); add(v, u, c); } dis[1] = 0; dfs(1, 0); for (i = 1; i <= N; i++) if (dis[i] >= dis[S]) S = i; dis[S] = 0; dfs(S, 0); for (i = 1; i <= N; i++) if (dis[i] >= dis[T]) T = i; Sdfs(S, 0); Tdfs(T, 0); while (T) { a[++tN] = T; vis[T] = 1; T = fa[T]; } for (i = 1; i <= tN; i++) Ldis[a[i]] = Ldfs(a[i], 0); int lg = min(tN, K); ans = max(Tdis[a[1]], Sdis[a[lg]]); for (i = 1; i <= lg; i++) s.insert(Ldis[a[i]]); ans = max(ans, *(--s.end())); for (i = lg + 1; i <= tN; i++) { s.erase(s.lower_bound(Ldis[a[i - lg]])); s.insert(Ldis[a[i]]); ans = min(ans, max(max(Tdis[a[i - lg + 1]], Sdis[a[i]]), *(--s.end()))); } printf("%d\n", ans); return 0; }
8
#include <bits/stdc++.h> using namespace std; int cnt = 1, n, d, k; vector<vector<int>> graph; vector<pair<int, int>> edges; void dfs(int u, int maxd); int main() { cin >> n >> d >> k; graph = vector<vector<int>>(n + 1); if (n < d + 1) { cout << "NO" << endl; return 0; } for (int i = 0; i < d; i++) { edges.push_back({cnt, cnt + 1}); graph[cnt].push_back(cnt + 1); graph[cnt + 1].push_back(cnt); if (graph[cnt].size() > k || graph[cnt + 1].size() > k) { cout << "NO" << endl; return 0; } cnt++; } for (int i = 1; i <= d + 1; i++) { dfs(i, min(d + 1 - i, i - 1)); } if (cnt < n) cout << "NO" << endl; else { cout << "YES" << endl; for (int i = 0; i < edges.size(); i++) { cout << edges[i].first << " " << edges[i].second << endl; } } return 0; } void dfs(int u, int maxd) { if (maxd == 0) return; for (int i = graph[u].size(); i < k; i++) { if (cnt == n) return; edges.push_back({u, ++cnt}); graph[u].push_back(cnt); graph[cnt].push_back(u); dfs(cnt, maxd - 1); } }
6
#include <bits/stdc++.h> using namespace std; int cnt(int bit) { int ret = 0; for (int i = 0; i < 26; i++) { if (bit >> i & 1) { ret++; } } return ret; } char op[100000]; string s[100000]; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; int all = 0, sum = 0; for (int i = 0; i < N; i++) { cin >> op[i] >> s[i]; if (i != N - 1 && op[i] != '.') all++; } const int ALL_BIT = (1 << 26) - 1; int rem = ALL_BIT; for (int i = 0; i < N - 1; i++) { if (op[i] == '.') { int b = 0; for (auto c : s[i]) { b |= 1 << (c - 'a'); } rem &= ~b; } if (op[i] == '!') { int b = 0; for (auto c : s[i]) { b |= 1 << (c - 'a'); } rem &= b; sum++; } if (op[i] == '?') { rem &= ~(1 << (s[i][0] - 'a')); sum++; } if (cnt(rem) == 1) break; } cout << all - sum << endl; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, c = 0, i; cin >> n; string a; cin >> a; for (i = 0; i < n - 1; i++) { if (a[i] != a[i + 1]) { cout << "YES" << endl << a[i] << a[i + 1]; c++; break; } } if (c == 0) cout << "NO"; }
1
#include <bits/stdc++.h> using namespace std; int ada[105]; int main() { int n, a, b; cin >> n >> a >> b; memset(ada, -1, sizeof ada); for (int i = 0; i < a; i++) { int x; cin >> x; ada[x] = 1; } for (int i = 0; i < b; i++) { int x; cin >> x; } for (int i = 1; i <= n; i++) { if (ada[i] == 1) { cout << 1 << " "; } else cout << 2 << " "; } cout << endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; string s, m; cin >> s >> m; int ara[300] = {0}; for (int i = 0; i < n; i++) { ara[m[i]]++; } int mn = 0, mx = 0, k = 0; sort(m.begin(), m.end()); sort(s.begin(), s.end()); for (int i = 0; i < n; i++) { bool flag = false; for (k = k; k < n; k++) { if (m[k] >= s[i]) { k++; flag = true; break; } } if (!flag) mn++; } cout << mn << endl; int j = 0; for (int i = 0; i < n; i++) { for (j = j; j < n; j++) { if (m[j] > s[i]) { j++; mx++; break; } } } cout << mx << endl; return 0; }
2
#include <bits/stdc++.h> const int N = 15; const int M = 2005; const int S = 4105; int n, m, col[M], a[N][M], id[M], bin[N], w[N][S], f[N][S], nx[S][N], mx[N][S]; void clear() { memset(col, 0, sizeof(col)); memset(w, 0, sizeof(w)); memset(f, 0, sizeof(f)); memset(nx, 0, sizeof(nx)); memset(mx, 0, sizeof(mx)); } bool cmp(int x, int y) { return col[x] > col[y]; } void init() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf("%d", &a[i][j]), col[j] = std::max(col[j], a[i][j]); for (int i = 1; i <= m; i++) id[i] = i; std::sort(id + 1, id + m + 1, cmp); std::sort(id + 1, id + std::min(n, m) + 1); for (int j = 1; j <= std::min(n, m); j++) for (int i = 1; i <= n; i++) a[i][j] = a[i][id[j]]; m = std::min(n, m); } int get(int x) { return std::lower_bound(bin, bin + n + 1, x) - bin; } void pre() { bin[0] = 1; for (int i = 1; i <= n; i++) bin[i] = bin[i - 1] * 2; for (int i = 0; i < bin[n]; i++) for (int j = 0; j < n; j++) for (int k = 0; k < n; k++) if (i & bin[k]) nx[i][j] += bin[(k + j) % n]; for (int i = 1; i <= m; i++) { for (int j = 1; j < bin[n]; j++) w[i][j] = w[i][j - (j & (-j))] + a[get(j & (-j)) + 1][i]; for (int j = 1; j < bin[n]; j++) for (int k = 0; k < n; k++) mx[i][j] = std::max(mx[i][j], w[i][nx[j][k]]); } } void dp() { for (int i = 1; i <= m; i++) for (int j = 0; j < bin[n]; j++) { f[i][j] = f[i - 1][j]; for (int k = j; k; k = (k - 1) & j) f[i][j] = std::max(f[i][j], f[i - 1][j - k] + mx[i][k]); } printf("%d\n", f[m][bin[n] - 1]); } int main() { int T; scanf("%d", &T); while (T--) { init(); pre(); dp(); clear(); } return 0; }
6
#include <bits/stdc++.h> const int N = 1e5 + 5; const long double pi = 3.141592653; const long long MOD = 1e9 + 7; const long long mod = 998244353; const long long INF = 9223372036854775807LL; using namespace std; int dx[9] = {1, -1, 0, 0, 1, 1, -1, -1, 0}; int dy[9] = {0, 0, 1, -1, 1, -1, 1, -1, 0}; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T = 1; while (T--) { int n, i, ans = 0, j; cin >> n; vector<int> a(n), g[35]; set<int> s; for (i = 0; i < n; i++) { cin >> a[i]; bitset<32> b(a[i]); for (j = 0; j < 32; j++) { if (b[j] == 1) g[j].push_back(i); } } s.insert(a[0] | a[0]); for (i = 1; i < n; i++) { s.insert(a[i] | a[i]); bitset<32> b(a[i]); set<int, greater<int> > p; for (j = 0; j < 32; j++) { if (b[j] == 0) { int lb = 0, ub = (long long)g[j].size() - 1, pos = -1; while (lb <= ub) { int mid = (lb + ub) / 2; if (g[j][mid] < i) { pos = max(pos, g[j][mid]); lb = mid + 1; } else ub = mid - 1; } if (pos != -1) p.insert(pos); } } int x = a[i]; for (auto &o : p) { s.insert(x | a[o]); x = x | a[o]; } } cout << (long long)s.size(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > e; queue<int> q; int h[200010]; int main() { int n, k, i; scanf("%d%d", &n, &k); for (i = 2; i <= k + 1; i++) { e.push_back(pair<int, int>(1, i)); h[i] = 1; q.push(i); } for (i = k + 2; i <= n; i++) { e.push_back(pair<int, int>(q.front(), i)); h[i] = h[q.front()] + 1; q.pop(); q.push(i); } sort(h + 1, h + n + 1); printf("%d\n", h[n] + h[n - 1]); for (i = 0; i < n - 1; i++) printf("%d %d\n", e[i].first, e[i].second); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 200 * 1000; int n; int l[N], r[N], t[N]; set<pair<int, int>> st[2]; int main() { scanf("%d", &n); for (int i = 0; i < int(n); ++i) scanf("%d%d%d", &l[i], &r[i], &t[i]), --t[i]; vector<pair<int, pair<int, int>>> ev; for (int i = 0; i < int(n); ++i) { ev.push_back(make_pair(l[i], make_pair(0, i))); ev.push_back(make_pair(r[i], make_pair(1, i))); } sort((ev).begin(), (ev).end()); int ans = 0; for (auto it : ev) { int i = it.second.second; if (it.second.first) { int j = t[i]; int k = j ^ 1; if (st[j].count(make_pair(r[i], i)) && !st[k].empty()) { ++ans; st[k].erase(st[k].begin()); } if (st[j].count(make_pair(r[i], i))) st[t[i]].erase(make_pair(r[i], i)); } else { st[t[i]].insert(make_pair(r[i], i)); } } printf("%d\n", n - ans); }
9
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 2e5 + 10; struct node { int l, r, id; friend bool operator<(node A, node B) { return A.r == B.r ? A.l < B.l : A.r < B.r; } }; node a[N]; bool add[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n, m; ; cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> a[i].l >> a[i].r; a[i].id = i; } sort(a + 1, a + 1 + n); multiset<int> b; for (int i = 0; i < m; ++i) b.insert(0); for (int i = 1; i <= n; ++i) { auto p = b.lower_bound(a[i].l); if (p != b.begin()) { add[a[i].id] = 1; --p; b.erase(p); b.insert(a[i].r); } } vector<int> ans; for (int i = 1; i <= n; ++i) if (!add[i]) ans.emplace_back(i); cout << ans.size() << endl; for (auto i : ans) cout << i << ' '; cout << endl; }
5
#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 abbs(long long int a, long long int b) { if (a - b < 0) return b - a; return a - b; } void solve() { long long int n; cin >> n; vector<long long int> v(n); vector<long long int> vc; for (long long int i = 0; i < n; i++) { cin >> v[i]; if (v[i] == 1) { vc.push_back(i); } } long long int ans = 0; for (long long int i = 1; i < vc.size(); i++) ans += vc[i] - vc[i - 1] - 1; cout << ans << "\n"; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t; t = 1; cin >> t; while (t--) { solve(); } }
0
#include <bits/stdc++.h> using namespace std; int p, n; string s; bool solve(int a) { if (a == n) return 1; for (char i = 'a'; i < p + 'a'; i++) { if (s[a - 1] == i || s[a - 2] == i) continue; s[a] = i; if (solve(a + 1)) return true; } return false; } int main() { cin >> n >> p; cin >> s; int a = n - 1; while (a >= 0) { for (char c = s[a] + 1; c < 'a' + p; c++) { if ((a && s[a - 1] == c) || (a > 1 && s[a - 2] == c)) continue; s[a] = c; assert(solve(a + 1)); cout << s; return 0; } a--; } cout << "NO"; return 0; }
4
#include <bits/stdc++.h> const int N = 200020; std::set<int> s[N]; int n, m, a, b, ans, p[N], e[N]; int find(int x) { return !e[x] ? x : e[x] = find(e[x]); } void f(int i, int x) { if (find(p[x - 1]) == find(i)) ans--; if (find(p[x + 1]) == find(i)) ans--; s[i].insert(x); } int main() { scanf("%d%d", &n, &m); ans = n - 1; for (int i = 1; i <= n; i++) scanf("%d", &a), f(a, i), p[i] = a; for (int i = 0; i < m; i++) { if (i) { scanf("%d%d", &a, &b); bool flag = s[b].size() > s[a].size(); if (flag) std::swap(a, b); for (int j : s[b]) f(a, j); if (flag) std::swap(s[a], s[b]); a = find(a), b = find(b); if (e[a] > e[b]) std::swap(a, b); e[a] += e[b]; e[b] = a; } printf("%d\n", ans); } }
7
#include <bits/stdc++.h> using namespace std; const int K = 26; const int NMAX = 4e5; struct info_t { int len = 0; long long c = 0; long long endc = -1; info_t() {} }; struct vertex { int next[K]; int next_term = -1; bool leaf; int p; char pch; int link; int go[K]; info_t info; vertex() : link(-1), p(-1), pch(-1), leaf(false) { fill(go, go + K, -1); fill(next, next + K, -1); } }; static vector<vertex> t(NMAX + 1); int sz; void init() { t.assign(NMAX + 1, vertex()); t[0].p = t[0].link = -1; t[0].info.endc = 0; t[0].next_term = 0; sz = 1; } void add_string(const string& s) { int v = 0; for (size_t i = 0; i < s.length(); ++i) { char c = s[i] - 'a'; if (t[v].next[c] == -1) { t[sz].link = -1; t[sz].p = v; t[sz].pch = c; t[v].next[c] = sz++; } v = t[v].next[c]; } t[v].info.len = s.size(); t[v].info.c++; t[v].leaf = true; } int go(int v, char c); int get_link(int v) { if (t[v].link == -1) { if (v == 0 || t[v].p == 0) { t[v].link = 0; } else { t[v].link = go(get_link(t[v].p), t[v].pch); } } return t[v].link; } int go(int v, char c) { if (t[v].go[c] == -1) { if (t[v].next[c] != -1) { t[v].go[c] = t[v].next[c]; } else { t[v].go[c] = v == 0 ? 0 : go(get_link(v), c); } } return t[v].go[c]; } int next_term(int v) { if (t[v].next_term == -1) { int link = get_link(v); if (t[link].leaf) { t[v].next_term = link; } else { t[v].next_term = next_term(link); } } return t[v].next_term; } long long sol2(string& text) { vector<long long> st(text.size()); vector<long long> ed(text.size()); int v = 0; for (int i = 0; i < text.size(); i++) { v = go(v, text[i] - 'a'); int u = t[v].leaf ? v : next_term(v); while (u != 0) { ed[i] += t[u].info.c; st[i - t[u].info.len + 1] += t[u].info.c; u = next_term(u); } } long long res = 0; for (int i = 0; i < text.size() - 1; i++) { res += ed[i] * st[i + 1]; } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); srand(123); string text(10, 'a'); while (cin >> text) { int n = 2; cin >> n; init(); vector<string> s(n); for (int i = 0; i < n; i++) { cin >> s[i]; add_string(s[i]); } cout << sol2(text) << endl; } }
8
#include <bits/stdc++.h> int digits(int i) { int a, b, c, d; a = i % 10; b = (i / 10) % 10; c = (i / 100) % 10; d = (i / 1000) % 10; if (a != b && b != c && c != d && a != c && a != d && b != d) return 1; else return 0; } int main() { int n, res; scanf("%d", &n); int i = n + 1; while (i > n) { res = digits(i); if (res == 1) { printf("%d", i); break; } i++; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 200010, maxm = 32; string s, t; int L[maxn], R[maxn], W[maxn]; bool valid() { memset(W, -1, sizeof(W)); for (int i = 0; i < ((int)(s).size()); ++i) { if (L[i] != -1) W[t[L[i]] - 'a'] = L[i]; if (W[s[i] - 'a'] == -1 || W[s[i] - 'a'] < R[i]) return 0; } return 1; } int main() { while (cin >> s >> t) { int k = 0; for (int i = 0; i < ((int)(s).size()); ++i) { if (k < ((int)(t).size()) && s[i] == t[k]) ++k; L[i] = k - 1; } k = ((int)(t).size()) - 1; for (int i = ((int)(s).size()) - 1; i >= 0; --i) { if (k >= 0 && s[i] == t[k]) --k; R[i] = k + 1; } if (valid()) printf("Yes\n"); else printf("No\n"); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int ecnt; struct Edge { int to, w, id; Edge *next; } * mat[5010], edges[100010 * 2]; void link(int x, int to, int w, int id) { edges[ecnt].to = to; edges[ecnt].id = id; edges[ecnt].w = w; edges[ecnt].next = mat[x]; mat[x] = &edges[ecnt++]; } bool in_tree[5010][5010]; struct Node { int x, y, w, id; bool friend operator<(const Node &a, const Node &b) { return a.w < b.w; } } input[100010]; int n, m, K; int cost[5010], w[5010][2], p[5010]; int Find(int x) { return p[x] = (x == p[x] ? x : Find(p[x])); } void dfs(int x, int father) { for (Edge *it = mat[x]; it; it = it->next) { int to = it->to; if (!in_tree[x][to] || to == father) continue; w[to][0] = it->w > cost[x] ? x : w[x][0]; w[to][1] = it->w > cost[x] ? to : w[x][1]; cost[to] = max(it->w, cost[x]); dfs(to, x); } } void show(int x, int father) { for (Edge *it = mat[x]; it; it = it->next) { int to = it->to; if (!in_tree[to][x] || to == father) continue; printf("%d ", it->id); show(to, x); } } int main() { scanf("%d%d%d", &n, &m, &K); for (int i = 0; i < m; i++) { int x, y, w; scanf("%d%d%d", &x, &y, &w); link(x, y, w, i + 1); link(y, x, w, i + 1); input[i].x = x, input[i].y = y, input[i].w = w; input[i].id = i + 1; } sort(input, input + m); for (int i = 1; i <= n; i++) w[p[i] = i][0] = -1; int cnt = 0; for (int i = 0; i < m; i++) { int x = input[i].x, y = input[i].y; if (x == 1 || y == 1) continue; if (Find(x) == Find(y)) continue; in_tree[x][y] = in_tree[y][x] = true; p[Find(x)] = Find(y); } for (Edge *it = mat[1]; it; it = it->next) { int to = it->to; int k = p[Find(to)]; if (w[k][0] == -1 || cost[k] > it->w) { cost[k] = it->w; w[k][0] = to; } } bool flag = true; for (int i = 2; i <= n && flag; i++) if (Find(i) == i) { if (w[i][0] == -1) { flag = false; break; } in_tree[1][w[i][0]] = in_tree[w[i][0]][1] = true; K--; } flag &= K >= 0; for (Edge *it = mat[1]; it; it = it->next) { int to = it->to; if (!in_tree[1][to]) continue; cost[to] = -1; dfs(to, 1); } while (flag && K > 0) { int ans, id = -1; for (Edge *it = mat[1]; it; it = it->next) { int to = it->to; if (in_tree[1][to]) continue; if (id == -1 || ans > it->w - cost[to]) { ans = it->w - cost[to]; id = to; } } if (id == -1) { flag = false; break; } in_tree[w[id][0]][w[id][1]] = false; in_tree[w[id][1]][w[id][0]] = false; in_tree[1][id] = in_tree[id][1] = true; cost[id] = -1; dfs(id, 1); K--; } if (flag) { printf("%d\n", n - 1); show(1, -1); } else printf("-1\n"); return 0; }
8
#include <bits/stdc++.h> int read() { register int x = 0; register char ch = getchar(), f = 1; for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = !f; for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ '0'); return f ? x : -x; } int n, a[200005]; int main() { n = read(); if (!(n & 1)) return printf("NO\n"), 0; printf("YES\n"); for (register int i = 1; i <= n; ++i) a[i] = 2 * i - 1; for (register int i = 1; i <= n; ++i) a[n + i] = 2 * i; for (register int i = 2; i <= n; i += 2) std ::swap(a[i], a[n + i]); for (register int i = 1; i <= (n << 1); ++i) printf("%d ", a[i]); }
2
#include <bits/stdc++.h> using namespace std; vector<vector<int>> g; bool visitados[105]; int total = 0; void dfs(int v) { if (visitados[v]) { return; } visitados[v] = 1; total++; for (auto u : g[v]) { dfs(u); } } int32_t main() { int n, m, x, y; cin >> n >> m; g.resize(n + 1); for (int i = 0; i < m; i++) { cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } dfs(1); if (total < n or n != m) { cout << "NO\n"; } else { cout << "FHTAGN!\n"; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 51, M = N * 100000; bitset<M> f; int n, d, a[N]; int main() { scanf("%d%d", &n, &d); for (int i = 1; i <= n; ++i) scanf("%d", a + i); sort(a + 1, a + n + 1); f[0] = 1; int ans = 0; for (int i = 1; i <= n; ++i) { if (ans + d < a[i]) break; ans += a[i]; f |= f << a[i]; } int ans2 = 0, pos = 0; while (pos < ans) { pos += d; while (!f[pos]) --pos; ++ans2; } printf("%d %d\n", ans, ans2); return 0; }
7
#include <bits/stdc++.h> using namespace std; long long a[100009], b[100009], pre[100009]; long long top1, top2, n, d, m; bool com(long long q, long long w) { return q > w; } signed main() { cin >> n >> d >> m; for (long long i = 1; i <= n; i++) { long long x; scanf("%lld", &x); if (x <= m) a[++top1] = x; else b[++top2] = x; } sort(a + 1, a + 1 + top1, com); sort(b + 1, b + 1 + top2, com); for (long long i = 1; i <= n; i++) pre[i] = pre[i - 1] + a[i]; long long sumn = 0, ans = pre[n]; for (long long j = 1; j <= top2; j++) { sumn += b[j]; long long yu = (j - 1) * (d + 1) + 1; if (yu > n) break; yu = n - yu; ans = max(ans, sumn + pre[yu]); } cout << ans; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, ans = 0, x; string a, b; cin >> n; cin >> a; cin >> b; while (n--) { x = abs(a[n] - b[n]); ans += min(x, 10 - x); } cout << ans; return 0; }
0
#include <bits/stdc++.h> using namespace std; int min(int a, int b) { return (a < b) ? a : b; } int main() { int n, m; cin >> n >> m; string str; cin >> str; int dp[n]; for (int i = 0; i < n; i++) { dp[i] = 10000; } dp[0] = 0; for (int i = 0; i < n; i++) { if (str[i] == '1') { for (int j = 0; j <= m; j++) { if (i + j < n && str[i + j] == '1') dp[i + j] = min(dp[i + j], dp[i] + 1); } } } if (dp[n - 1] == 10000) cout << "-1"; else cout << dp[n - 1]; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxN = 2e5 + 100; long long int bigL[maxN], bigR[maxN], L[maxN], a[maxN]; long long int n, q; void input() { cin >> n >> q; for (int i = 1; i <= n; i++) cin >> a[i]; } int recBigL(int pos, int x) { if (x <= L[pos]) return pos; if (pos <= 1) return 0; return recBigL(bigL[pos], x); } int recBigR(int pos, int x) { if (x < L[pos]) return pos; if (pos >= n) return n; return recBigR(bigR[pos], x); } void alg(int l, int r) { for (int i = 2; i <= n; i++) bigL[i] = recBigL(i - 1, L[i]); for (int i = n - 1; i >= 0; i--) bigR[i] = recBigR(i + 1, L[i]); } int main() { input(); for (int i = 1; i < n; i++) L[i] = fabs(a[i] - a[i + 1]); alg(1, n); for (int i = 1; i <= q; i++) { int l, r; cin >> l >> r; for (int i = l; i < r; i++) { } long long int ans = 0; for (int i = l; i < r; i++) { ans += L[i] * (i - max(bigL[i], (long long)(l - 1))) * (min(bigR[i], (long long)r) - i); } cout << ans << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; long long f(long long n) { if (n < 1) return 0; return (n * (n + 1)) / 2; } int main() { long long n, i, j, k, s = 0, x, y, z; cin >> n; long long a[n + 1]; a[n] = 0; for (i = 0; i < n; i++) { cin >> a[i]; } for (i = 0; i < n; i++) { if (a[i + 1] != a[i]) { if (a[i + 1] < a[i]) { x = n - a[i + 1]; y = a[i] - a[i + 1] - 1; z = n - a[i]; } else { x = a[i + 1]; y = a[i]; z = a[i + 1] - a[i]; } s = s + (f(x) - (f(y) + f(z))); } } cout << s << "\n"; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; map<long long, long long> a; long long x, i; long long max = 0; for (i = 0; i < n; i++) { cin >> x; a[x]++; if (a[x] > max) { max = a[x]; } } cout << n - max << endl; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t; cin >> t; while (t--) { long long int n, i, ans = 0, mans = 0; cin >> n; long long int a[n]; for (i = 0; i < n; i++) cin >> a[i]; for (i = 1; i < n; i++) { if (a[i] < a[i - 1]) { ans += (a[i - 1] - a[i]); } } cout << ans, cout << "\n"; } }
2
#include <bits/stdc++.h> using namespace std; ifstream fin("AAtest.in.txt"); int q, n, jada['z' + 1], jadb['z' + 1]; string a, b; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cerr.tie(0); cin >> q; while (q--) { cin >> n >> a >> b; memset(jada, 0, sizeof(jada)); memset(jadb, 0, sizeof(jadb)); for (int(i) = (0); ((i)) < ((n)); ((i))++) jada[a[i]]++, jadb[b[i]]++; int arv = 0; bool on = 0; for (char c = 'a'; c <= 'z'; c++) { if (jada[c] != jadb[c]) { cout << "NO" << endl; on = 1; break; } arv = max(arv, jada[c]); } if (on) continue; if (arv > 1) { cout << "YES" << endl; continue; } int aa = 0, bb = 0; for (int(i) = (0); ((i)) < ((n)); ((i))++) { char ca = a[i], cb = b[i]; for (int j = i + 1; j < n; j++) { if (a[j] < ca) aa++; if (b[j] < cb) bb++; } } if (aa % 2 != bb % 2) { cout << "NO" << endl; continue; } else { cout << "YES" << endl; continue; } } }
6
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; string s; cin >> s; for (int i = 0; i <= 100; i++) { for (int j = 0; j <= 100; j++) { if (i * b + j * c == a) { cout << i + j << endl; for (int k = 0; k < i; k++) { cout << s.substr(0, b) << endl; s = s.substr(b); } for (int k = 0; k < j; k++) { cout << s.substr(0, c) << endl; s = s.substr(c); } return 0; } } } cout << -1 << endl; }
2
#include <bits/stdc++.h> using namespace std; int dx[9] = {1, -1, 0, 0, 1, 1, -1, -1, 0}; int dy[9] = {0, 0, 1, -1, 1, -1, 1, -1, 0}; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); vector<string> v; string s; cin >> s; int i; for (i = 0; i < 6; i++) { string s1, s2; s1.push_back(i / 10 + 48); s1.push_back(i % 10 + 48); s2.push_back(i % 10 + 48); s2.push_back(i / 10 + 48); v.push_back(s1 + ':' + s2); } for (i = 10; i < 16; i++) { string s1, s2; s1.push_back(i / 10 + 48); s1.push_back(i % 10 + 48); s2.push_back(i % 10 + 48); s2.push_back(i / 10 + 48); v.push_back(s1 + ':' + s2); } for (i = 20; i < 24; i++) { string s1, s2; s1.push_back(i / 10 + 48); s1.push_back(i % 10 + 48); s2.push_back(i % 10 + 48); s2.push_back(i / 10 + 48); v.push_back(s1 + ':' + s2); } for (i = 0; i < v.size(); i++) { if (s.compare(v[i]) < 0) break; } if (i == v.size()) cout << v[0]; else cout << v[i]; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { string s; int a, b, x; cin >> a >> b >> x; if (a > b) s += '0', a--; else s += '1', b--; while (x > 1) { char c = s.back(); if (c == '0') s += '1', b--; else s += '0', a--; x--; } char c = s.back(); if (c == '0') { while (a) s += '0', a--; while (b) s += '1', b--; } else if (c == '1') { while (b) s += '1', b--; while (a) s += '0', a--; } cout << s << '\n'; return 0; }
2
#include <stdio.h> #include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define modulo 1000000007 #define mod(mod_x) ((((long long)mod_x+modulo))%modulo) #define Inf 1000000000000000000 vector<int> p; vector<vector<int>> depth; void dfs(vector<vector<int>> &E,int cur,int par,int d){ p[cur] = par; depth[d].push_back(cur); rep(i,E[cur].size()){ int to = E[cur][i]; if(to==par)continue; dfs(E,to,cur,d+1); } } template <typename T,typename F> struct SWAG{ F func; T init_value; vector<pair<T,T>> X,Y; SWAG(F f,T iv):func(f){ init_value = iv; } void push_front(T x){ if(X.empty())X.push_back({x,x}); else X.push_back({x,func(x,X.back().second)}); } void push_back(T x){ if(Y.empty())Y.push_back({x,x}); else Y.push_back({x,func(Y.back().second,x)}); } void pop_front(){ if(X.empty()){ int n = Y.size(); vector<T> t; for(int i=0;i<n/2;i++){ t.push_back(Y.back().first); Y.pop_back(); } while(!Y.empty()){ push_front(Y.back().first); Y.pop_back(); } while(!t.empty()){ push_back(t.back()); t.pop_back(); } if(!X.empty())X.pop_back(); } else{ X.pop_back(); } } void pop_back(){ if(Y.empty()){ int n = X.size(); stack<T> t; for(int i=0;i<n/2;i++){ t.push_back(X.back().first); X.pop_back(); } while(!X.empty()){ push_back(X.back().first); X.pop_back(); } while(!t.empty()){ push_front(t.back()); t.pop_back(); } if(!Y.empty())Y.pop_back(); } else{ Y.pop_back(); } } T get(){ T ret = init_value; if(!X.empty())ret = func(ret,X.back().second); if(!Y.empty())ret = func(ret,Y.back().second); return ret; } T front(){ if(!X.empty())return X.back().first; if(!Y.empty())return Y[0].first; return init_value; } T back(){ if(!Y.empty())return Y.back().first; if(!X.empty())return X[0].first; return init_value; } int size(){ return X.size()+Y.size(); } }; int main(){ int _t; cin>>_t; auto f = [](long long a,long long b){ return max(a,b); }; rep(_,_t){ int n; scanf("%d",&n); vector<vector<int>> E(n); rep(i,n-1){ int v; scanf("%d",&v); v--; E[i+1].push_back(v); E[v].push_back(i+1); } vector<long long> a(n,0); rep(i,n-1){ scanf("%lld",&a[i+1]); } p.assign(n,-1); depth.assign(n+1,vector<int>()); dfs(E,0,-1,0); //cout<<'a'<<endl; vector<long long> dp(n,-1); dp[0] = 0; //cout<<'b'<<endl; for(int i=1;i<depth.size();i++){ if(depth[i].size()==0)break; vector<pair<long long,int>> V; rep(j,depth[i].size()){ V.emplace_back(a[depth[i][j]],depth[i][j]); } sort(V.begin(),V.end()); rep(j,V.size()){ int ind = V[j].second; dp[ind] = dp[p[ind]] + max(V.back().first-V[j].first,V[j].first-V[0].first); } SWAG<long long,decltype(f)> S0(f,-Inf),S1(f,-Inf); rep(j,V.size()){ int ind = V[j].second; S1.push_back(a[ind] + dp[p[ind]]); } rep(j,V.size()){ int ind = V[j].second; S1.pop_front(); dp[ind] = max(dp[ind],a[ind] + S0.get()); dp[ind] = max(dp[ind],-a[ind] + S1.get()); S0.push_back(-a[ind] + dp[p[ind]]); } } long long ans = 0LL; rep(i,n)ans = max(ans,dp[i]); printf("%lld\n",ans); } return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { int n, a = 0, b = 0; cin >> n; for (int i = 0, c; i < n; ++i) { cin >> c; if (c % 2) ++a; else ++b; } cout << min(a, b); }
0
#include <bits/stdc++.h> using namespace std; long long n, k, mod; map<long long, long long> mp; long long v(long long a) { long long r = a * a % mod; (r *= r) %= mod; return (r - k * a % mod + mod) % mod; } int main() { cin >> n >> mod >> k; for (int i = 0; i < n; i++) { long long a; cin >> a; mp[v(a)]++; } long long ans = 0; for (auto x : mp) { ans += x.second * (x.second - 1) / 2; } cout << ans << endl; }
7
#include <bits/stdc++.h> using namespace std; inline long long rd() { char c; long long sign = 1; while ((c = getchar()) < '0' || c > '9') if (c == '-') sign = -1; long long res = c - '0'; while ((c = getchar()) >= '0' && c <= '9') res = res * 10 + c - '0'; return res * sign; } const int MAXN = 100010; const int mod = 1e9 + 7; inline int Mod(int x) { return x >= mod ? x - mod : x; } inline void Add(int& x, int y) { x += y, x -= x >= mod ? mod : 0; } int id[MAXN]; int sgn[MAXN]; int deg[MAXN]; int vis[MAXN]; int tot; int to[MAXN << 1]; int ne[MAXN << 1]; int rv[MAXN << 1]; int fi[MAXN]; int dg[MAXN]; inline void Link(int u, int v, int w) { tot++; to[tot] = v; rv[tot] = w; ne[tot] = fi[u]; fi[u] = tot; } inline void AddEdge(int u, int v, int w) { Link(u, v, w), Link(v, u, w), ++dg[u], ++dg[v]; } int rev[MAXN]; int sta[MAXN]; int top; inline void dfs(int x) { sta[++top] = x, vis[x] = 1; for (int i = fi[x]; i; i = ne[i]) { int u = to[i]; if (vis[u]) continue; rev[top] = rv[i]; dfs(u); } } int res[2]; int main() { int m = rd(), n = rd(), c0 = 0, c1 = 0; for (int i = 1; i <= m; i++) { int k = rd(), x, y; if (k == 2) x = rd(), y = rd(); else x = rd(); deg[i] = k; if (k == 2) { if (x == y) { c0++, vis[i] = 1, id[abs(x)] = i; continue; } if (x == -y) { c1++, vis[i] = 1, id[abs(x)] = i; continue; } } if (id[abs(x)]) AddEdge(i, id[abs(x)], (x < 0) ^ sgn[abs(x)]); else id[abs(x)] = i, sgn[abs(x)] = x < 0; if (k == 2) { if (id[abs(y)]) AddEdge(i, id[abs(y)], (y < 0) ^ sgn[abs(y)]); else id[abs(y)] = i, sgn[abs(y)] = y < 0; } } res[0] = 1; while (c0--) res[0] = res[1] = Mod(res[0] + res[1]); while (c1--) res[0] = Mod(res[0] << 1), res[1] = Mod(res[1] << 1), swap(res[0], res[1]); int mul = 1; for (int i = 1; i <= n; i++) if (!id[i]) mul = Mod(mul << 1); for (int i = 1; i <= m; i++) { if (vis[i]) continue; if (!dg[i]) { vis[i] = 1; if (deg[i] == 1) res[0] = res[1] = Mod(res[0] + res[1]); else { int tr0 = (res[0] + 3LL * res[1]) % mod; int tr1 = (3LL * res[0] + res[1]) % mod; res[0] = tr0, res[1] = tr1; } } if (dg[i] == 1) { top = 0, dfs(i); int r0 = 1, r1 = 0, r2 = 0, r3 = 0; if (deg[sta[1]] == 2) r2 = 1; for (int j = 1; j < top; j++) { int tr0, tr1, tr2, tr3; if (rev[j]) { tr0 = Mod(r1 + r3), tr1 = Mod(r0 + r2); tr2 = Mod(r0 + r3), tr3 = Mod(r1 + r2); } else { tr0 = Mod(r0 + r3), tr1 = Mod(r1 + r2); tr2 = Mod(r1 + r3), tr3 = Mod(r0 + r2); } r0 = tr0, r1 = tr1, r2 = tr2, r3 = tr3; } if (deg[sta[top]] == 2) Add(r2, Mod(r0 + r2)), Add(r3, Mod(r3 + r1)); Add(r0, r3), Add(r1, r2); int tr0 = (1LL * r0 * res[0] + 1LL * r1 * res[1]) % mod; int tr1 = (1LL * r1 * res[0] + 1LL * r0 * res[1]) % mod; res[0] = tr0, res[1] = tr1; } } for (int i = 1; i <= m; i++) { if (vis[i]) continue; top = 0, dfs(i); int R0 = 0, R1 = 0, r0, r1, r2, r3, Rv = 0; for (int j = fi[sta[1]]; j; j = ne[j]) if (to[j] == sta[top]) Rv = rv[j]; r0 = 1, r1 = 0, r2 = 0, r3 = 0; for (int j = 1; j < top; j++) { int tr0, tr1, tr2, tr3; if (rev[j]) { tr0 = Mod(r1 + r3), tr1 = Mod(r0 + r2); tr2 = Mod(r0 + r3), tr3 = Mod(r1 + r2); } else { tr0 = Mod(r0 + r3), tr1 = Mod(r1 + r2); tr2 = Mod(r1 + r3), tr3 = Mod(r0 + r2); } r0 = tr0, r1 = tr1, r2 = tr2, r3 = tr3; } if (Rv) R0 = Mod(r1 + r3), R1 = Mod(r0 + r2); else R0 = Mod(r0 + r3), R1 = Mod(r1 + r2); r0 = 0, r1 = 0, r2 = 1, r3 = 0; for (int j = 1; j < top; j++) { int tr0, tr1, tr2, tr3; if (rev[j]) { tr0 = Mod(r1 + r3), tr1 = Mod(r0 + r2); tr2 = Mod(r0 + r3), tr3 = Mod(r1 + r2); } else { tr0 = Mod(r0 + r3), tr1 = Mod(r1 + r2); tr2 = Mod(r1 + r3), tr3 = Mod(r0 + r2); } r0 = tr0, r1 = tr1, r2 = tr2, r3 = tr3; } if (Rv) Add(R0, Mod(r0 + r3)), Add(R1, Mod(r1 + r2)); else Add(R0, Mod(r1 + r3)), Add(R1, Mod(r0 + r2)); int tr0 = (1LL * R0 * res[0] + 1LL * R1 * res[1]) % mod; int tr1 = (1LL * R1 * res[0] + 1LL * R0 * res[1]) % mod; res[0] = tr0, res[1] = tr1; } cout << 1LL * res[1] * mul % mod << endl; return 0; }
10
#include <bits/stdc++.h> using namespace std; #define ll long long int #define fr(i,n) for(int i=0;i<2*n;i++) #define min3(a, b, c) min(c, min(a, b)) #define min4(a, b, c, d) min(d, min(c, min(a, b))) #define rfr(i, n) for(int i=n-1;i>=0;i--) #define PI 3.1415926535897932384626 const int mod=1000000007; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin>>t; while(t--) { ll n; ll even=0,odd=0; cin>>n; ll size=2*n; ll a[size]; for(int i=0;i<size;i++){ cin>>a[i]; } for(int i=0;i<size;i++){ if(a[i]%2==0){ even++; } else{ odd++; } } if(even==odd){ cout<<"Yes"<<"\n"; } else{ cout<<"No"<<"\n"; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long oo = 100000000000ll; struct node { long long nx; long long w; }; vector<node> e[100005]; struct Heap { long long p; long long val; bool operator<(const Heap &t) const { return t.val < val; } } tp; priority_queue<Heap> Q; long long n, m; long long ix, iy, iw; long long pa[100005]; long long dist[100005]; bool vis[100005]; long long ans[100005], head; int main() { scanf("%lld %lld", &n, &m); while (m--) { scanf("%lld %lld %lld", &ix, &iy, &iw); e[ix].push_back((node){iy, iw}); e[iy].push_back((node){ix, iw}); } pa[1] = 0; for (int i = 2; i <= n; i++) dist[i] = oo; Q.push((Heap){1, 0}); while (!Q.empty()) { tp = Q.top(); Q.pop(); if (vis[tp.p]) continue; vis[tp.p] = true; for (int i = 0; i < e[tp.p].size(); i++) { if (dist[e[tp.p][i].nx] > dist[tp.p] + e[tp.p][i].w) { dist[e[tp.p][i].nx] = dist[tp.p] + e[tp.p][i].w; pa[e[tp.p][i].nx] = tp.p; Q.push((Heap){e[tp.p][i].nx, dist[e[tp.p][i].nx]}); } } } head = n; if (dist[n] == oo) puts("-1"); else { long long k = n; while (pa[k] != 0) { ans[head--] = k; k = pa[k]; } ans[head--] = 1; for (long long i = head + 1; i <= n; i++) printf("%lld ", ans[i]); puts(""); } return 0; }
5
#include <bits/stdc++.h> using namespace std; struct gtr { int x; int y; }; int n, i, x, y; gtr a[1000000], b[1000000]; bool cmp(gtr a, gtr b) { return (a.x < b.x || (a.x == b.x && a.y > b.y)); } int main() { scanf("%ld", &n); for (i = 1; i <= n; i++) { scanf("%ld%ld", &a[i].x, &a[i].y); b[i] = a[i]; } sort(a + 1, a + n + 1, cmp); x = a[1].x; y = a[1].y; for (i = 2; i <= n; i++) if (x > a[i].x || y < a[i].y) { cout << -1; return 0; } for (i = 1; i <= n; i++) if (x == b[i].x && y == b[i].y) { cout << i; return 0; } }
1
#include <bits/stdc++.h> using namespace std; map<long long, int> mp; long long a[200010], b[200010], c[200010]; int main() { long long n, q, k, m, i, j, total = 0, op; cin >> n >> q >> a[0]; long long mx = a[0]; for (i = 1; i < n; i++) { cin >> a[i]; b[i] = mx; c[i] = min(a[i], mx); mx = max(mx, a[i]); } while (q--) { cin >> op; if (op < n) cout << b[op] << " " << a[op] << endl; else { cout << mx << " " << c[(op - 1) % (n - 1) + 1] << endl; } } }
3
#include <bits/stdc++.h> using namespace std; void solution(vector<int> ar, int n) { map<int, bool> mp; int ans = -1; for (int i = 0; i < n; i++) { mp[ar[i]] = 1; } for (int i = 1; i <= 1024; i++) { int c = 0; for (int j = 0; j < n; j++) { int p = ar[j] ^ i; if (mp[p] == 1) { c++; } } if (c == n) { ans = i; break; } } cout << ans; } int main() { int t; cin >> t; for (int i = 0; i < t; i++) { int n; cin >> n; vector<int> ar(n); for (int j = 0; j < n; j++) { cin >> ar[j]; } solution(ar, n); cout << "\n"; } }
2
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; char str[10]; bool flag; cout << 0 << " " << 0 << endl; cout.flush(); cin >> str; if (str[0] == 'w') flag = true; else flag = false; if (n == 1) { cout << "1 0 1 1" << endl; return 0; } int l = 0, r = 1 << (n - 1); for (int i = 0; i < n - 2; i++) { int m = (l + r) / 2; cout << m << " " << m << endl; cout.flush(); cin >> str; if (!flag) { if (str[0] == 'w') r = m; else if (str[0] == 'b') l = m; } else if (flag) { if (str[0] == 'b') r = m; else if (str[0] == 'w') l = m; } } cout << 0 << " " << (1 << (n - 1)) << endl; cout.flush(); cin >> str; if ((flag && str[0] == 'w') || (!flag && str[0] == 'b')) cout << (l + r) / 2 << " " << 1 << " " << (l + r) / 2 << " " << 2 << endl; else cout << 1 << " " << (l + r) / 2 << " " << 2 << " " << (l + r) / 2 << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; long long a, b, Ans[10010]; int l[10010], flag; int main() { scanf("%I64d%I64d", &a, &b), flag = 1; while (a > 1 || b > 1) { if (a > b) Ans[++*Ans] = (a - 1) / b, a -= (a - 1) / b * b, l[*Ans] = 0; else if (b > a) Ans[++*Ans] = (b - 1) / a, b -= (b - 1) / a * a, l[*Ans] = 1; else { flag = 0; break; } } if (!flag) puts("Impossible"); else for (int i = 1; i <= *Ans; ++i) { printf("%I64d", Ans[i]); if (l[i]) putchar('B'); else putchar('A'); } }
8
#include <bits/stdc++.h> using namespace std; int i, n, x, C[101]; int main() { cin >> n; for (i = 0; i < n; i++) { cin >> x, C[x]++; } for (i = 0; i < 101; i++) { while (C[i]--) cout << i << " "; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int k, l; cin >> k >> l; int i = 0; while (l % k == 0 && k != l) { l = l / k; i += 1; }; if (k == l) { cout << "YES" << endl << i; } else { cout << "NO"; }; return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, m; int res = 0; int u[1000][1000]; int a[1111][1111]; int dd[9][2] = {0, 0, -2, -1, -2, 1, -1, -2, -1, 2, 1, 2, 1, -2, 2, 1, 2, -1}; void ban(int x, int y, int t) { for (int i = 0; i < 9; i++) { int xx = x + dd[i][0]; int yy = y + dd[i][1]; if (xx >= 0 && yy >= 0) u[xx][yy] += t; } } void rec(int x, int y, int sum) { if (x == n) { res = max(res, sum); return; } if (y == m) rec(x + 1, 0, sum); else { if (!u[x][y]) ban(x, y, 1), a[x][y] = 1, rec(x, y + 1, sum + 1), a[x][y] = 0, ban(x, y, -1); rec(x, y + 1, sum); } } int ans; int main() { cin >> n >> m; if (n > m) swap(n, m); if (n == 1) { ans = m; } else if (n == 2) { if (m % 4 == 1) ans = m + 1; else ans = ((m + 3) / 4) * 4; } else ans = (n * m + 1) / 2; cout << ans; return 0; }
5