solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long INF = 0x7ffffffffffffffll; const int mod = 1e9 + 7; const int maxn = 1e5 + 5; const int maxv = 1e3 + 5; const double pi = acos(-1.0); const double eps = 1e-9; int t, n, res[105]; char f[105][2][1005]; char in[1005], did[1005]; map<string, int> mmm; void getname(int k) { gets(in); int len = strlen(in); int flag = 0, cnt = 0, f1 = 0, f2 = 0; for (int i = 0; i < len; i++) { if (in[i] == '#') flag = 1; if (flag == 1 && in[i] == ' ') flag = 2; if (flag == 2 && in[i] != ' ') flag = 3; if (flag == 3 && in[i] == ' ') f2 = 1; if (flag == 1 && in[i] != ' ' && in[i] != '#') flag = 3; if (flag == 3 && in[i] != ' ' && f2) { f[k][0][cnt++] = in[i]; f1 = 1; } if (flag == 3 && in[i] == ' ' && f1) flag = 4; if (flag == 4 && in[i] != ' ' && f1) flag = 5, cnt = 0; if (flag == 5 && in[i] != ' ' && f1) f[k][1][cnt++] = in[i]; } } int solve(char s[], int l, int r) { int flag = 0, last = 0; for (int i = r; i >= l; i--) { if (s[i] == '(') flag--; else if (s[i] == ')') flag++; else if (!flag) { if ((s[i] == '/' || s[i] == '*') && !last) last = i; else if (s[i] == '+') { int x = solve(s, l, i - 1); int y = solve(s, i + 1, r); if (x == 2 || y == 2) return 2; else return 3; } else if (s[i] == '-') { int x = solve(s, l, i - 1); int y = solve(s, i + 1, r); if (x == 2 || y == 2 || y == 3) return 2; else return 3; } } } if (last && s[last] == '*') { int x = solve(s, l, last - 1); int y = solve(s, last + 1, r); if (x == 2 || y == 2 || x == 3 || y == 3) return 2; else return 4; } else if (last && s[last] == '/') { int x = solve(s, l, last - 1); int y = solve(s, last + 1, r); if (y == 4 || x == 2 || y == 2 || x == 3 || y == 3) return 2; else return 4; } else if (s[l] == '(' && s[r] == ')') { if (solve(s, l + 1, r - 1) == 2) return 2; else return 1; } else { string tmp = ""; for (int i = l; i <= r; i++) tmp += s[i]; if (mmm.find(tmp) == mmm.end()) return 1; else { int id = mmm[tmp]; if (!res[id]) res[id] = solve(f[id][1], 0, strlen(f[id][1]) - 1); return res[id]; } } } int main() { mmm.clear(); scanf("%d", &n); getchar(); for (int i = 1; i <= n; i++) { getname(i); string name = f[i][0]; mmm[name] = i; } gets(in); int cnt = 0; for (int i = 0; i < strlen(in); i++) if (in[i] != ' ') did[cnt++] = in[i]; int res = solve(did, 0, cnt - 1); if (res == 2) printf("Suspicious"); else printf("OK"); return 0; }
9
#include <bits/stdc++.h> using namespace std; int n, X[100013], Y[100013]; long double t[90]; int gcd(int a, int b) { return a == 0 ? b : gcd(b % a, a); } int main() { t[0] = 1; for (int i = 1; i < 90; i++) t[i] = t[i - 1] * 2; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d%d", &X[i], &Y[i]); long double A = 0; for (int l = 1; l <= min(35, n - 2); l++) { long double prob = n > 88 ? 1 / t[l + 2] : (t[n - l - 1] - 1) / (t[min(n, 89)] - 1 - 1LL * n * (n + 1) / 2) / 2; for (int i = 0; i < n; i++) { int j = (i + l) % n; A += prob * (1LL * Y[j] * X[i] - 1LL * X[j] * Y[i] - abs(gcd(Y[j] - Y[i], X[j] - X[i]))); } } printf("%.9f\n", (double)A + 1); }
10
#include <bits/stdc++.h> using namespace std; int main() { double x; scanf("%lf", &x); double diff = 12345; int da = -1, db = -1; for (int a = 1; a <= 10; a++) for (int b = 1; b <= 10; b++) { double u = (a * b) / sqrt(4 * a * a + b * b); u = fabs(u - x); if (u < diff) { diff = u; da = b; db = a; } } printf("%d %d\n", da, db); return 0; }
5
#include <bits/stdc++.h> using namespace std; const long long inf = 1e18; const int N = 1e6 + 10; const long long mod = 1e9 + 7; const int base = 131; const double pi = acos(-1); const double eps = 1e-8; long long vis[N], num[N], n, m, k, x, y, z; long long a[N], b[N]; long long ex, ey, cnt, ans, sum, flag, t, ff; map<pair<int, int>, int> mp; set<int> sa, sb; vector<pair<int, int> > p; void f(int x, int y) { if (x > y) swap(x, y); if (x > n / 2 && y > n / 2) { p.push_back(pair<int, int>(1, x)); p.push_back(pair<int, int>(1, y)); p.push_back(pair<int, int>(1, x)); } else if (x <= n / 2 && y <= n / 2) { p.push_back(pair<int, int>(n, x)); p.push_back(pair<int, int>(n, y)); p.push_back(pair<int, int>(n, x)); } else { p.push_back(pair<int, int>(1, y)); p.push_back(pair<int, int>(x, n)); p.push_back(pair<int, int>(1, n)); p.push_back(pair<int, int>(1, y)); p.push_back(pair<int, int>(x, n)); } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); num[a[i]] = i; } for (int i = 1; i <= n; i++) { if (num[i] == i) continue; if (abs(num[i] - i) >= n / 2) { p.push_back(pair<int, int>(i, num[i])); } else { f(i, num[i]); } t = a[i]; swap(num[i], num[a[i]]); a[num[i]] = i; a[num[t]] = t; } printf("%d\n", p.size()); for (auto x : p) { printf("%d %d\n", x.first, x.second); } }
4
#include <bits/stdc++.h> using namespace std; long long i, j, k, N, M, K; long long t[100000], a[100000], b[100000], c[100000], ans[100000], ansc[100000]; int check() { for (i = 1; i <= M; i++) { if (t[i] == 2) { long long m = -1 * 1000000000; for (j = a[i]; j <= b[i]; j++) { m = max(m, ans[j]); } if (m != c[i]) return false; } else { for (j = a[i]; j <= b[i]; j++) { ans[j] += c[i]; } } } return true; } int main() { scanf("%I64d%I64d", &N, &M); for (i = 1; i <= M; i++) { scanf("%I64d%I64d%I64d%I64d", &t[i], &a[i], &b[i], &c[i]); } for (i = 1; i <= 5005; i++) ans[i] = 1000000000; for (i = M; i >= 1; i--) { if (t[i] == 2) for (j = a[i]; j <= b[i]; j++) { if (ans[j] > c[i]) ans[j] = c[i]; } else { for (j = a[i]; j <= b[i]; j++) { ans[j] -= c[i]; } } } for (i = 1; i <= N; i++) ans[i] = min(1000000000LL, ans[i]); for (i = 1; i <= N; i++) ansc[i] = ans[i]; if (check()) { printf("YES\n"); for (i = 1; i <= N; i++) { printf("%I64d%c", ansc[i], i == N ? '\n' : ' '); } } else { printf("NO\n"); } }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int n, k, l[maxn], r[maxn]; char c[maxn]; vector<int> v; void dfs(int u) { if (!u) return; dfs(l[u]); v.push_back(u); dfs(r[u]); } int f[maxn], g[maxn]; void calc(int u, int cst) { if (!u || cst > k) return; calc(l[u], cst + 1); if (g[l[u]]) g[u] = 1; else if (f[u]) g[u] = 1, k -= cst; if (g[u]) calc(r[u], 1); } int main() { scanf("%d%d%s", &n, &k, c + 1); for (int i = 1; i <= n; ++i) scanf("%d%d", &l[i], &r[i]); dfs(1); for (int i = n - 2; i >= 0; --i) { if (c[v[i]] < c[v[i + 1]]) f[v[i]] = 1; else if (c[v[i]] == c[v[i + 1]]) f[v[i]] = f[v[i + 1]]; } calc(1, 1); for (int i = 0; i < n; ++i) { putchar(c[v[i]]); if (g[v[i]]) putchar(c[v[i]]); } return 0; }
8
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const int inf = 1e7; const int MAXM = 3e5 + 5; const int MAXN = 1e6 + 4; int main() { long long n, i, j, k; long long a, b; long long x = 0, y = 0; cin >> a >> b; for (i = 1; i * i <= a; i++) { x++; } for (i = 1; i * (i + 1) <= b; i++) { y++; } if (x <= y) { cout << "Vladik\n"; } else { cout << "Valera\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int tests; cin >> tests; while (tests--) { int item; cin >> item; vector<int> v; set<int> s; int temp; for (int i = 0; i < item; i++) { cin >> temp; v.push_back(temp); s.insert(temp); } int distinct = 0; cout << s.size() << '\n'; } return 0; }
0
#include <bits/stdc++.h> char s[200010]; int tree[26][200010 << 2]; bool vis[200010]; inline void PushUp(int t, int rt) { tree[t][rt] = tree[t][rt << 1] + tree[t][rt << 1 | 1]; } void Update(int t, int x, int L, int R, int rt) { if (L == R) tree[t][rt] = 1; else { int mid = (L + R) >> 1; if (mid >= x) Update(t, x, L, mid, rt << 1); else Update(t, x, mid + 1, R, rt << 1 | 1); PushUp(t, rt); } } int Find(int t, int x, int L, int R, int rt) { if (L == R) { tree[t][rt] = 0; return L; } int ans, mid = (L + R) >> 1; if (tree[t][rt << 1] >= x) ans = Find(t, x, L, mid, rt << 1); else { x -= tree[t][rt << 1]; ans = Find(t, x, mid + 1, R, rt << 1 | 1); } PushUp(t, rt); return ans; } int main() { char ch; int k, i, j, q, len, n; while (~scanf("%d %s%d", &k, s + 1, &q)) { memset(vis, false, sizeof(vis)); memset(tree, 0, sizeof(tree)); len = strlen(s + 1); n = len * k; for (j = len + 1; --k;) { for (i = 1; i <= len; i++) s[j++] = s[i]; } s[j] = 0; for (i = 1; s[i]; i++) Update(s[i] - 'a', i, 1, n, 1); while (q--) { scanf("%d %c", &i, &ch); k = Find(ch - 'a', i, 1, n, 1); vis[k] = true; } for (i = 1; s[i]; i++) { if (!vis[i]) putchar(s[i]); } putchar('\n'); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { long n, a; cin >> n; for (long i = 9; i >= 1; i--) { if (n % i == 0) { a = i; break; } } int t = n / a; cout << t << endl; for (long i = 1; i <= t; i++) cout << a << " "; }
0
#include <bits/stdc++.h> #pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline") #pragma GCC option("arch=native", "tune=native", "no-zero-upper") #pragma GCC target("avx2") #pragma GCC optimize("03") #pragma GCC target("sse4") using namespace std; vector<int> psn[(long long)(2e5 + 5)]; vector<pair<long long, long long> > psnp[(long long)(2e5 + 5)]; vector<int> psm[(long long)(2e5 + 5)]; vector<int> ms[(long long)(2e5 + 5)]; int abi[(long long)(2e5 + 5)]; void update(int p, int v) { while (p < (long long)(2e5 + 5)) { abi[p] += v; p += (p & -p); } } int query(int p) { int res = 0; while (p) { res += abi[p]; p -= (p & -p); } return res; } int nexx(int l, int r) { int v = query(l - 1); int second = query(r) - v; if (!second) return -1; int res = 0; int ac = 0; for (int po = (1 << 17); po; po >>= 1) if (res + po < (long long)(2e5 + 5) && ac + abi[res + po] <= v) { res += po; ac += abi[res]; } return res + 1; } void doo(int i, int v) { for (auto y : psm[i]) update(y, v); } int32_t main() { long long n, m, l, r; cin >> n >> m; cin >> l >> r; for (int i = 1; i <= n; i++) for (int j = i; j <= n; j += i) { ms[i].push_back(j); psn[j].push_back(i); } for (int i = 1; i <= n; i++) { for (auto y : psn[i]) { int l = (i / y) + 1; int r = ms[y].size(); if (l > r) continue; psnp[i].push_back(pair<long long, long long>(l, r)); } } for (int i = 1; i <= n; i++) ms[i].clear(); for (int i = 1; i <= m; i++) for (int j = i; j <= m; j += i) { ms[i].push_back(j); psm[j].push_back(ms[i].size()); } long long pl = m + 1; long long pr = m; for (int i = 1; i <= n; i++) { while ((pl - 1) * i >= l) { doo(pl - 1, 1); pl--; } while (pr * i > r) { doo(pr, -1); pr--; } if (pl > pr) { cout << (-1) << '\n'; continue; } bool ok = 0; for (auto y : psnp[i]) { int xl = y.first; int xr = y.second; int v = nexx(xl, xr); if (v == -1) continue; else { long long d = i / (xl - 1); int x1 = i; int x2 = d * v; int y1 = pl - (pl % v); if (y1 != pl) y1 += v; int y2 = (y1 / v) * (xl - 1); cout << x1 << ' ' << y1 << ' ' << x2 << ' ' << y2 << '\n'; ok = 1; break; } } if (!ok) cout << (-1) << '\n'; } return 0; }
11
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> ostream& operator<<(ostream& cout, const pair<T1, T2>& a) { return cout << "(" << a.first << "; " << a.second << ")"; } template <typename T> ostream& operator<<(ostream& cout, const vector<T>& a) { if (a.size() == 0) { return cout << "()"; } cout << "(" << a[0]; for (int i = 1; i < (int)a.size(); i++) { cout << "; " << a[i]; } return cout << ")"; } template <typename T> ostream& operator<<(ostream& cout, const set<T>& a) { if (a.size() == 0) { return cout << "{}"; } auto it = a.begin(); cout << "{" << *it; ++it; for (; it != a.end(); ++it) { cout << "; " << *it; } return cout << "}"; } long long a1, a2, a3; int main() { cin >> a1; cin >> a2; cin >> a3; cout << min({(a1 + a2) * 2, a1 + a3 + a2, (a1 + a3) * 2, (a2 + a3) * 2}); }
0
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s; cin >> s; int i, j; int ans = 0; int m1; int n1; int p1; for (i = 0; i < s.length(); i++) { m1 = 0; n1 = 0; p1 = 0; for (j = i; j < s.length(); j++) { if (s[j] == '(') m1++; if (s[j] == ')') m1--; if (s[j] == '?') p1++; if (p1 > m1) p1--, m1++; if (p1 + m1 < 0) break; if (p1 >= m1) ans++; } } cout << ans; return 0; }
5
#include <bits/stdc++.h> using namespace std; long long a[1509][1509], maxp[1509][1509], maxs[1509][1509], s[1509][1509], dp[1509][1509]; long long i, j, k, l, n, m, res = -10000000000; int main() { scanf("%I64d %I64d", &n, &m); for (i = 1; i <= n; i++) { maxp[i][0] = -10000000000; maxs[i][m + 1] = -10000000000; for (j = 1; j <= m; j++) { scanf("%I64d", &a[i][j]); s[i][j] = s[i][j - 1] + a[i][j]; if (i % 2 == 0) { dp[i][j] = s[i][j] + maxs[i - 1][j + 1]; } else dp[i][j] = s[i][j] + maxp[i - 1][j - 1]; if (i % 2 == 0) maxp[i][j] = max(maxp[i][j - 1], dp[i][j]); if (i == n) res = max(res, dp[i][j]); } if (i % 2 == 1) { for (j = m; j > 0; j--) maxs[i][j] = max(maxs[i][j + 1], dp[i][j]); } } printf("%I64d\n", res); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; if (n == 1) cout << m; else if (n - m > m - 1 && n - m != 0) cout << m + 1; else cout << m - 1; return 0; }
2
#include <bits/stdc++.h> using namespace std; using namespace std; const int inf = -1u / 2; const int N = 5010; bool dp1[N][N], dp2[N][N]; int dp3[N][N]; int mx1[N], mx2[N]; vector<int> idx1, idx2; string s1, s2; int lcp(string &s, int id1, int id2) { int ans = 0; while (id1 < s.length() and id2 < s.length() and s[id1] == s[id2]) { ans++; id1++; id2++; } return ans; } int main() { ios::sync_with_stdio(false); cin >> s1 >> s2; int n = s1.length(), m = s2.length(); for (int i = n - 1; i >= 0; i--) { for (int j = n - 1; j >= i; j--) { if (i == j) dp1[i][j] = 0; else if (s1[i] != s1[j]) dp1[i][j] = s1[i] < s1[j]; else if (j == n - 1) dp1[i][j] = 0; else dp1[i][j] = dp1[i + 1][j + 1]; } } for (int j = 0; j < n; j++) { for (int i = j + 1; i < n; i++) { dp1[i][j] = !dp1[j][i]; } } for (int i = 0; i < n; i++) idx1.push_back(i); sort(idx1.begin(), idx1.end(), [&](int id1, int id2) { return dp1[id1][id2]; }); for (int i = m - 1; i >= 0; i--) { for (int j = m - 1; j >= i; j--) { if (i == j) dp2[i][j] = 0; else if (s2[i] != s2[j]) dp2[i][j] = s2[i] < s2[j]; else if (j == m - 1) dp2[i][j] = 0; else dp2[i][j] = dp2[i + 1][j + 1]; } } for (int j = 0; j < m; j++) { for (int i = j + 1; i < m; i++) { dp2[i][j] = !dp2[j][i]; } } for (int i = 0; i < m; i++) idx2.push_back(i); sort(idx2.begin(), idx2.end(), [&](int id1, int id2) { return dp2[id1][id2]; }); for (int i = 0; i < n; i++) { mx1[idx1[i]] = max((i > 0 ? lcp(s1, idx1[i], idx1[i - 1]) : 0), (i + 1 < n ? lcp(s1, idx1[i], idx1[i + 1]) : 0)) + 1; } for (int i = 0; i < m; i++) { mx2[idx2[i]] = max((i > 0 ? lcp(s2, idx2[i], idx2[i - 1]) : 0), (i + 1 < m ? lcp(s2, idx2[i], idx2[i + 1]) : 0)) + 1; } int ans = inf; for (int i = n - 1; i >= 0; i--) { for (int j = m - 1; j >= 0; j--) { if (s1[i] == s2[j]) { dp3[i][j] = 1 + dp3[i + 1][j + 1]; } else { dp3[i][j] = 0; } int l = dp3[i][j]; int r = max(mx1[i], mx2[j]); if (r <= l) { ans = min(ans, r); } } } if (ans == inf) ans = -1; cout << ans << "\n"; return 0; }
7
#include <bits/stdc++.h> template <class T> inline T read(const T sample) { T x = 0; int f = 1; char s; while ((s = getchar()) > '9' || s < '0') if (s == '-') f = -1; while (s >= '0' && s <= '9') x = (x << 1) + (x << 3) + (s ^ 48), s = getchar(); return x * f; } template <class T> inline void write(const T x) { if (x < 0) return (void)(putchar('-'), write(-x)); if (x > 9) write(x / 10); putchar(x % 10 ^ 48); } template <class T> inline T Max(const T x, const T y) { if (x > y) return x; return y; } template <class T> inline T Min(const T x, const T y) { if (x < y) return x; return y; } template <class T> inline T fab(const T x) { return x > 0 ? x : -x; } template <class T> inline T gcd(const T x, const T y) { return y ? gcd(y, x % y) : x; } template <class T> inline T lcm(const T x, const T y) { return x / gcd(x, y) * y; } template <class T> inline T Swap(T &x, T &y) { x ^= y ^= x ^= y; } const int mod = 1e9 + 7; int n; long long s, f[22]; int qkpow(int x, int y) { int r = 1; while (y) { if (y & 1) r = 1ll * r * x % mod; x = 1ll * x * x % mod; y >>= 1; } return r; } int C(int n, int m) { if (n < m) return 0; if (m > n - m) m = n - m; int up = 1, down = 1; for (register signed i = (0), _end = (m - 1); i <= _end; ++i) { up = 1ll * up * (n - i) % mod; down = 1ll * down * (i + 1) % mod; } return 1ll * up * qkpow(down, mod - 2) % mod; } int Lucas(long long n, long long m) { if (m == 0) return 1; return 1ll * Lucas(n / mod, m / mod) * C(n % mod, m % mod) % mod; } int work() { long long tot, ans = 0, sign; for (register signed i = (0), _end = ((1 << n) - 1); i <= _end; ++i) { tot = s; sign = 1; for (register signed j = (1), _end = (n); j <= _end; ++j) if ((1 << j - 1) & i) { tot -= f[j] + 1; sign = -sign; } if (tot < 0) continue; ans = ((ans + sign * Lucas(tot + n - 1, n - 1)) % mod + mod) % mod; } return ans; } int main() { n = read(9), s = read(9ll); for (register signed i = (1), _end = (n); i <= _end; ++i) f[i] = read(9ll); write(work()), putchar('\n'); return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { long long t; long long a1, b1; long long a2, b2; long long lin, sin; long long lre, sre; cin >> t; for (long long i = 0; i < t; i++) { cin >> a1 >> b1; cin >> a2 >> b2; if (a1 < b1) { sin = a1; lin = b1; } else { sin = b1; lin = a1; } if (a2 < b2) { sre = a2; lre = b2; } else { sre = b2; lre = a2; } if (sin + sre == lin && lin == lre) { cout << "Yes" << endl; } else { cout << "No" << endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; void zoz() { cin.tie(0); std::ios::sync_with_stdio(false); } int main() { zoz(); long long x, y, t, index = 0, maxx = -1e7, arr[1000]; cin >> x >> y; for (int i = 1; i <= x; i++) { cin >> arr[i]; } for (int i = x; i >= 0; i--) { if ((ceil((double)arr[i] / y)) > maxx) { maxx = ceil((double)arr[i] / y); index = i; } } cout << index << endl; }
1
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7, n, m, a[500005]; vector<long long> v1, vis; int main() { scanf("%lld %lld", &n, &m); string s1[n + 1]; for (long long i = 0; i < n; i++) cin >> s1[i]; long long flag = 1; for (long long i = 0; i < n; i++) for (long long j = 0; j < m; j++) if (s1[i][j] == '#') for (long long k = 0; k < n; k++) if (s1[k][j] == '#' && s1[i] != s1[k]) flag = 0; if (flag) cout << "Yes"; else cout << "No"; }
2
#include <bits/stdc++.h> using namespace std; vector<int> box[100100]; int f[100100], b[100100], a[100100]; int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> f[i]; box[f[i]].push_back(i); } for (int i = 1; i <= m; i++) cin >> b[i]; int mark = 0; for (int i = 1; i <= m; i++) { if (box[b[i]].size() == 0) { mark = 2; break; } else if (box[b[i]].size() > 1) { mark = 1; } else { a[i] = box[b[i]][0]; } } if (!mark) { cout << "Possible" << endl; for (int i = 1; i <= m; i++) cout << a[i] << " "; } else if (mark == 1) { cout << "Ambiguity" << endl; } else cout << "Impossible" << endl; }
3
#include <bits/stdc++.h> char str[100]; int m, n, a[1000000], f[22][1 << 21]; int common(int x, int y) { if (x == 0 || y == n + 1) return 0; for (int i = m; i >= 0; i--) { int s1 = a[x] & ((1 << i) - 1); int s2 = a[y] >> (m - i); if (s1 == s2) return i; } return 0; } int main() { int i, spent, save, j, s, raw = 0; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%s", str); m = strlen(str); for (j = 0; j < m; j++) a[i] = (a[i] << 1) + str[j] - '0'; raw += m - common(i - 1, i); } for (j = save = 0; j <= m; j++) for (s = 0; s < (1 << m); s++) f[j][s] = -999999999; f[0][0] = 0; for (i = 2; i <= n; i++) { spent = m - common(i - 1, i); for (j = 0, save = -999999999; j <= m; j++) { int s = a[i] >> j; if (f[m - j][s] - j + spent > save) save = f[m - j][s] - j + spent; } for (j = 0; j <= m; j++) { int s = (1 << (j)) - 1; s &= a[i - 1]; if (save > f[j][s]) f[j][s] = save; } } for (j = save = 0; j <= m; j++) for (s = 0; s < (1 << m); s++) if (f[j][s] > save) save = f[j][s]; printf("%d\n", raw - save); }
10
#include <bits/stdc++.h> template <class T1, class T2> inline bool cmin(T1 &a, const T2 &b) { return b < a ? (a = b, true) : false; } template <class T1, class T2> inline bool cmax(T1 &a, const T2 &b) { return a < b ? (a = b, true) : false; } template <class Type> Type read() { Type a; bool b; unsigned char c; while (c = getchar() - 48, (c > 9) & (c != 253)) ; for (a = (b = c == 253) ? 0 : c; (c = getchar() - 48) <= 9; a = a * 10 + c) ; return b ? -a : a; } auto rd = read<int>; const int N = 2000; int n, deg[N], d[N], cnt, min = 1e9, mincnt; char s[N + 1], e[N][N]; bool strong() { memcpy(d, deg, n * sizeof(int)); std::sort(d, d + n); for (int k = 1, sum = 0; k < n; ++k) { sum += d[k - 1]; if (sum == k * (k - 1) / 2) return false; } return true; } void rev(int u) { deg[u] = n - 1 - deg[u]; for (int v = 0; v < n; ++v) if (u != v) { deg[v] -= e[v][u]; e[u][v] ^= 1, e[v][u] ^= 1; deg[v] += e[v][u]; } } void search(int i, int r) { if (i == n) { if (strong()) if (r < min) min = r, mincnt = 1; else if (r == min) ++mincnt; } else { search(i + 1, r); rev(i); search(i + 1, r + 1); rev(i); } } int main() { assert(scanf("%d", &n) == 1); for (int i = 0; i < n; ++i) { assert(scanf("%s", s) == 1); for (int j = 0; j < n; ++j) deg[i] += e[i][j] = s[j] - 48; } if (strong()) { puts("0 1"); return 0; } for (int i = 0; i < n; ++i) { rev(i); mincnt += strong(); rev(i); } if (mincnt) { printf("1 %d\n", mincnt); return 0; } search(0, 0); if (mincnt == 0) puts("-1"); else { for (int i = 1; i <= min; ++i) mincnt = (long long)mincnt * i % 998244353; printf("%d %d\n", min, mincnt); } return 0; }
12
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; const int mod = 1e9 + 7; int n, m, q, tin[N], tout[N], timer, a[N], p[N], used[N]; vector<int> g[N]; bitset<1001> pr, cur; struct node { bitset<1001> b; int add; node() { add = 0; b.reset(); } } T[N * 4]; void dfs(int v = 1, int pr = 0) { tin[v] = ++timer; p[timer] = v; for (auto to : g[v]) if (to != pr) dfs(to, v); tout[v] = timer; } void build(int v = 1, int tl = 1, int tr = timer) { if (tl == tr) { T[v].b[a[p[tl]]] = 1; return; } int tm = (tl + tr) / 2; build(v * 2, tl, tm); build(v * 2 + 1, tm + 1, tr); T[v].b = (T[v * 2].b | T[v * 2 + 1].b); } void push(int v, int tl, int tr) { if (tl > tr || T[v].add == 0) return; if (tl != tr) { T[v * 2].add += T[v].add; T[v * 2].add %= m; T[v * 2 + 1].add += T[v].add; T[v * 2 + 1].add %= m; } T[v].b = ((T[v].b << T[v].add) | (T[v].b >> (m - T[v].add))); T[v].add = 0; } void upd(int l, int r, int x, int v = 1, int tl = 1, int tr = timer) { push(v, tl, tr); if (tl > r || tr < l) return; if (l <= tl && tr <= r) { T[v].add += x; T[v].add %= m; push(v, tl, tr); return; } int tm = (tl + tr) / 2; upd(l, r, x, v * 2, tl, tm); upd(l, r, x, v * 2 + 1, tm + 1, tr); T[v].b = (T[v * 2].b | T[v * 2 + 1].b); } void get(int l, int r, int v = 1, int tl = 1, int tr = timer) { push(v, tl, tr); if (tl > r || tr < l) return; if (l <= tl && tr <= r) { cur |= T[v].b; return; } int tm = (tl + tr) / 2; get(l, r, v * 2, tl, tm); get(l, r, v * 2 + 1, tm + 1, tr); } int main() { ios_base::sync_with_stdio(0); cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i]; a[i] %= m; } for (int i = 1, a, b; i < n; i++) { cin >> a >> b; g[a].push_back(b); g[b].push_back(a); } dfs(); build(); for (int i = 2; i < m; i++) { if (!used[i]) { pr[i] = 1; for (int j = i; j < m; j += i) used[j] = 1; } } cin >> q; for (int i = 1, t, a, b; i <= q; i++) { cin >> t >> a; if (t == 1) { cin >> b; b %= m; upd(tin[a], tout[a], b); } else { cur.reset(); get(tin[a], tout[a]); cur = (cur & pr); cout << cur.count() << endl; } } return 0; }
10
#include <bits/stdc++.h> using namespace std; const int N = 100012; char S[N]; int main() { long long c = 1LL; int w = 1; cin >> S; S[strlen(S)] = 200; for (int i = (1); i < (strlen(S) + 1); ++i) { if (S[i] - '0' + S[i - 1] - '0' == 9) { ++w; } else { if (w > 1) { if (w % 2 == 1) c *= (w / 2 + 1); w = 1; } } } cout << c << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long max_n = 300011, max_a = 1000011; int n, k, used[max_n], m, cnt[max_n]; long long mx; void get_cnt() { for (int i = 1; i < max_n; ++i) { for (int q = i * 2; q < max_n; q += i) { ++cnt[q]; } } } int main() { get_cnt(); cin >> n >> k; for (int i = 1; i <= n; ++i) { mx += cnt[i]; used[i] = 1; if (mx >= k) { n = i; break; } } mx -= k; m = n; vector<pair<int, int> > v; for (int i = n / 2 + 1; i <= n; ++i) { v.push_back(make_pair(cnt[i], i)); } sort(v.begin(), v.end()); for (int i = v.size() - 1; i >= 0; --i) { if (mx >= v[i].first) { mx -= v[i].first; used[v[i].second] = 0; --m; } } if (mx == 0) { cout << "Yes\n"; cout << m << endl; for (int i = 0; i <= n; ++i) { if (used[i]) printf("%d ", i); } cout << endl; } else { cout << "No\n"; } return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { int n; int ans = 1; cin >> n; int k = 1; ans = n / 2 + 1; cout << ans; return 0; }
0
#include <bits/stdc++.h> using namespace std; int t; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; for (int i = 0; i < t; i++) { long long n, x, kolOne = 0, kolZero = 0; long long diff = 0, ans = 0; string s; cin >> n >> x >> s; for (int j = 0; j < s.size(); j++) { if (s[j] == '1') kolOne++; else kolZero++; if (kolZero - kolOne == x) ans++; } diff = kolZero - kolOne; if (diff == 0 && ans) cout << "-1\n"; else if (diff == 0 && !ans) cout << "0\n"; else { ans = 0; kolOne = 0; kolZero = 0; for (int j = 0; j < s.size(); j++) { if (s[j] == '1') kolOne++; else kolZero++; long long ff = (x - (kolZero - kolOne)) / diff; if (ff >= 0 && (x - (kolZero - kolOne) == diff * ff)) ans++; } if (x == 0) ans++; cout << ans << '\n'; } } }
4
#include <bits/stdc++.h> using namespace std; long long a[200007]; long long x[200007]; int main() { int n, q, i, j, k; long long sum = 0; cin >> n >> q; for (i = 0; i < n; i++) cin >> a[i], sum += a[i]; for (i = 0; i < q; i++) cin >> x[i]; for (i = 1; i < n; i++) a[i] += a[i - 1]; long long cum = 0; for (int u = 0; u < q; u++) { cum += x[u]; int k = upper_bound(a, a + n, cum) - a; int pp = max(n - k, 0); if (pp == 0) cout << n << endl; else cout << pp << endl; if (cum >= sum) cum = 0; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { string a; cin >> a; int t = a.size(); for (int i = 0; i < a.size(); i++) { if (a[i] == '0') { a.erase(a.begin() + i); break; } } if (t == a.size()) { for (int i = a.size() - 1; i >= 0; i--) { if (a[i] == '1') { a.erase(a.begin() + i); break; } } } cout << a << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; fstream fout; const double pi = 3.141592653589793; array<int, 2> pLusminus = {-1, 1}; int main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); if (0) { fout.open("o.txt", ios::out); if (!fout) cout << "noo!\nfilee!\n\n"; } int t = 1; while (t--) { int n; cin >> n; vector<int> v(n); for (auto& i : v) { cin >> i; } int l = 0; while (l < n - 1 && v[l + 1] > v[l]) { l++; } int r = n - 1; while (r > 0 && v[r - 1] < v[r]) { r--; } reverse(v.begin() + l, v.begin() + r + 1); bool f = 1; for (int i = 0; i < n - 1; i++) { if (v[i + 1] < v[i]) { f = 0; break; } } if (!f) { cout << "no\n"; } else { cout << "yes\n"; if (r > l) cout << l + 1 << ' ' << r + 1 << '\n'; else cout << 1 << ' ' << 1 << '\n'; } } oo: if (0) { fout.close(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int a, b; cin >> a >> b; if ((a == b) && a > 0 || (a == b + 1 || b == a + 1)) cout << "YES"; else cout << "NO"; return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, p[105], d; bool g[105][105]; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> p[i]; p[i]--; g[i][i] = true; } for (int i = 0; i < n; i++) { cin >> d; if (i + d < n) { g[i][i + d] = g[i + d][i] = true; } if (i - d >= 0) { g[i][i - d] = g[i - d][i] = true; } } for (int k = 0; k < n; k++) for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (g[i][k] && g[k][j]) g[i][j] = true; bool f = true; for (int i = 0; i < n; i++) if (!g[i][p[i]] && !g[p[i]][i]) { f = false; } if (f) cout << "YES"; else cout << "NO"; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MAX = 2005; int n, k; string c, d; int main() { int T; cin >> T; while (T--) { cin >> n >> k >> c; c = "#" + c; d = c; for (int i = 1; i <= n; i++) if ((i + 1) / 2 < k) if (i % 2) d[i] = '('; else d[i] = ')'; else if (i <= n / 2 + k - 1) d[i] = '('; else d[i] = ')'; cout << n << '\n'; for (int i = 1; i <= n; i++) for (int j = i; j <= n; j++) if (d[i] == c[j]) { for (int k = i; k + k < i + j; k++) swap(c[k], c[i + j - k]); cout << i << " " << j << '\n'; break; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long int n, k, i, j, ans; cin >> n >> k; string s; cin >> s; long long int a[26] = {0}; for (i = 0; i < n; i++) { j = s[i] - 'A'; a[j]++; } j = INT_MAX; for (i = 0; i < k; i++) { if (a[i] < j) j = a[i]; } ans = k * j; cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL; template <typename T, typename U> static void amin(T &x, U y) { if (y < x) x = y; } template <typename T, typename U> static void amax(T &x, U y) { if (x < y) x = y; } template <int MOD> struct ModInt { static const int Mod = MOD; unsigned x; ModInt() : x(0) {} ModInt(signed sig) { int sigt = sig % MOD; if (sigt < 0) sigt += MOD; x = sigt; } ModInt(signed long long sig) { int sigt = sig % MOD; if (sigt < 0) sigt += MOD; x = sigt; } int get() const { return (int)x; } ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; } ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; } ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; } ModInt &operator/=(ModInt that) { return *this *= that.inverse(); } ModInt operator+(ModInt that) const { return ModInt(*this) += that; } ModInt operator-(ModInt that) const { return ModInt(*this) -= that; } ModInt operator*(ModInt that) const { return ModInt(*this) *= that; } ModInt operator/(ModInt that) const { return ModInt(*this) /= that; } ModInt inverse() const { signed a = x, b = MOD, u = 1, v = 0; while (b) { signed t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); } if (u < 0) u += Mod; ModInt res; res.x = (unsigned)u; return res; } }; vector<int> solve(vector<vector<ModInt<5> > > A, vector<vector<ModInt<5> > > bs) { int n = A.size(), m = A[0].size(); int rank = 0; vector<char> used(n, 0); for (int(j) = 0; (j) < (int)(m); ++(j)) { int i = 0; while (i < n && (used[i] || A[i][j].x == 0)) i++; if (i == n) continue; rank++; used[i] = true; for (int(k) = 0; (k) < (int)(n); ++(k)) if (!used[k]) { ModInt<5> coef = ModInt<5>() - A[k][j] / A[i][j]; for (int(l) = 0; (l) < (int)(m); ++(l)) A[k][l] += A[i][l] * coef; for (auto &b : bs) b[k] += b[i] * coef; } } vector<int> res; for (auto &b : bs) { bool ok = true; for (int(i) = 0; (i) < (int)(n); ++(i)) ok &= used[i] || b[i].x == 0; if (!ok) res.push_back(-1); else res.push_back(rank); } return res; } int main() { int n; int m; while (~scanf("%d%d", &n, &m)) { vector<vector<ModInt<5> > > A(m, vector<ModInt<5> >(n)); for (int(i) = 0; (i) < (int)(n); ++(i)) { char buf[501]; scanf("%s", buf); for (int(j) = 0; (j) < (int)(m); ++(j)) A[j][i] = buf[j] - 'a'; } int q; scanf("%d", &q); vector<vector<ModInt<5> > > bs(q, vector<ModInt<5> >(m)); for (int(i) = 0; (i) < (int)(q); ++(i)) { char buf[501]; scanf("%s", buf); for (int(j) = 0; (j) < (int)(m); ++(j)) bs[i][j] = buf[j] - 'a'; } auto ranks = solve(A, bs); for (int rank : ranks) { using minta = ModInt<1000000007>; minta ans; if (rank != -1) { ans = 1; for (int(k) = 0; (k) < (int)(n - rank); ++(k)) ans *= 5; } printf("%d\n", ans.get()); } } return 0; }
9
#include <bits/stdc++.h> using namespace std; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } const int MAXN = 100; const int MINVAL = 1; const int MAXVAL = 1000; int n; int a[MAXN]; int solve() { int ret = 0, cur = 0; for (int i = (0); i < (n); ++i) { if (i - 1 >= 0 && a[i - 1] < a[i] - 1 || i == 0 && a[i] - 1 >= MINVAL) { cur = 0; continue; } if (i + 1 < n && a[i + 1] > a[i] + 1 || i + 1 == n && a[i] + 1 <= MAXVAL) { cur = 0; continue; } ++cur; ret = max(ret, cur); } return ret; } void run() { scanf("%d", &n); for (int i = (0); i < (n); ++i) scanf("%d", &a[i]); printf("%d\n", solve()); } int main() { run(); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; long long int total = 0, coin = 0, val, hand = 0; for (int i = 1; i <= n; i++) { cin >> val; if (val < 0) { val *= -1; total -= val; if (total < 0) { coin += abs(total); total = 0; } } else total += val; } cout << total << "\n"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; const int inf = 0x3f3f3f3f; long long gcd(long long a, long long b) { return (a % b) ? (gcd(b, a % b)) : b; } const int maxn = 1e6 + 5; int pa(int x) { int ans = 0; while (x) { ans += x % 10; x /= 10; } return ans; } vector<int> G[maxn]; int k, ans; int dfs(int x, int p) { vector<int> a; for (auto u : G[x]) { if (u == p) continue; a.push_back(dfs(u, x)); } int mx = 0, i = 0; sort(a.begin(), a.end(), greater<int>()); while (i + 1 < a.size() && a[i] + a[i + 1] > k) { ans++; i++; } mx = G[x].size() == 1 ? 0 : a[i]; return mx + 1; } int main(int argc, const char **argv) { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, i, j; cin >> n >> k; for (i = 1; i < n; i++) { int x, y; cin >> x >> y; G[x].push_back(y); G[y].push_back(x); } for (i = 1; i <= n; i++) if (G[i].size() > 1) { dfs(i, 0); break; } cout << ans + 1 << "\n"; return 0; }
8
#include <bits/stdc++.h> using namespace std; int main() { int n, p; string s, number; cin >> n; string name[n]; int taxi[n], pizza[n], cg[n], tax, pizz, cgc, taxmax = -1, pizzmax = -1, cgcmax = -1; int taxifinal[n], pizzafinal[n], cgcfinal[n], ct = 0, cp = 0, cc = 0; for (int i = 0; i < n; i++) { cin >> p >> name[i]; tax = 0; pizz = 0; cgc = 0; for (int j = 0; j < p; j++) { cin >> number; if (((number[0] == number[3]) && (number[3] == number[6])) && ((number[0] == number[1]) && (number[3] == number[4]) && (number[6] == number[7]))) { tax++; } else if ((int(number[0]) > int(number[1])) && (int(number[1]) > int(number[3])) && (int(number[3]) > int(number[4])) && (int(number[4]) > int(number[6])) && (int(number[6]) > int(number[7]))) { pizz++; } else { cgc++; } } taxi[i] = tax; pizza[i] = pizz; cg[i] = cgc; } for (int i = 0; i < n; i++) { taxmax = max(taxmax, taxi[i]); pizzmax = max(pizzmax, pizza[i]); cgcmax = max(cgcmax, cg[i]); } for (int i = 0; i < n; i++) { if (taxmax == taxi[i]) { taxifinal[ct] = i; ct++; } if (pizzmax == pizza[i]) { pizzafinal[cp] = i; cp++; } if (cgcmax == cg[i]) { cgcfinal[cc] = i; cc++; } } cout << "If you want to call a taxi, you should call:"; if (ct > 1) { for (int j = 0; j < ct - 1; j++) { cout << " " << name[taxifinal[j]] << ","; } cout << " " << name[taxifinal[ct - 1]] << "." << endl; } else { cout << " " << name[taxifinal[ct - 1]] << "." << endl; } cout << "If you want to order a pizza, you should call:"; if (cp > 1) { for (int j = 0; j < cp - 1; j++) { cout << " " << name[pizzafinal[j]] << ","; } cout << " " << name[pizzafinal[cp - 1]] << "." << endl; } else { cout << " " << name[pizzafinal[cp - 1]] << "." << endl; } cout << "If you want to go to a cafe with a wonderful girl, you should call:"; if (cc > 1) { for (int j = 0; j < cc - 1; j++) { cout << " " << name[cgcfinal[j]] << ","; } cout << " " << name[cgcfinal[cc - 1]] << "." << endl; } else { cout << " " << name[cgcfinal[cc - 1]] << "." << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; void think() { long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < n; i++) { cin >> a[i]; } long long ans = 0; unordered_map<long long, long long> mp; mp[a[0]]++; for (long long i = 1; i < n; i++) { long long x = a[i]; a[i] += a[i - 1]; if (mp.find(a[i]) != mp.end() || a[i] == 0) { ans++; mp.clear(); a[i] = x; mp[a[i]]++; } else { mp[a[i]]++; } } cout << ans << "\n"; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; long long testcase = 1; while (testcase--) { think(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long N = 105; long long a[N], b[N]; int main() { long long n, k, x, i, j, p, j1, ans = 0; cin >> n >> k >> x; for (i = 1; i <= n; i++) cin >> a[i]; for (i = 0; i <= n; i++) { long long l = i; long long r = i + 1; long long cur = x; long long cnt = 1; while (true) { long long prel = l; long long prer = r; while (l >= 0 && a[l] == cur) { l--; cnt++; } while (r <= n && a[r] == cur) { r++; cnt++; } if (cnt < 3) { l = prel; r = prer; break; } cur = a[l]; cnt = 0; } ans = max(ans, r - l - 1); } cout << ans << "\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; long long int powmod(long long int a, int b, int n) { long long int rm = 1; while (b) { if (b % 2) { rm = (rm * a) % n; } a = (a * a) % n; b /= 2; } return rm; } const int N = 100000 + 200; vector<int> edge[N]; int L[N] = {0}, maxdp = 0; long long int sL[N] = {0}; long long int isum[N], sum[N]; int n, k, p; void dfs(int vertex, int parent, int depth) { maxdp = max(maxdp, depth); L[depth]++; for (int i = 0; i < edge[vertex].size(); ++i) { if (edge[vertex][i] != parent) { dfs(edge[vertex][i], vertex, depth + 1); } } } pair<long long int, int> get(int i, int j) { if (i == j) return make_pair(0, L[j]); long long int cost = 0; if (j - 1 >= i + 1) { cost += sum[j - 1] - sum[i] - (sL[j - 1] - sL[i]) * (long long int)(maxdp - j + 1); } cost = p - cost; if (cost < 0) return make_pair(-1, -1); cost /= j - i; long long int ppl = sL[j] - sL[i]; ppl += min(cost, (long long int)L[i]); return make_pair(0, ppl); } int main() { int u, v; sum[0] = isum[0] = 0; cin >> n >> k >> p; for (int i = 1; i < n; ++i) { cin >> u >> v; edge[u].push_back(v); edge[v].push_back(u); } dfs(1, 0, 1); for (int i = 1; i <= maxdp; ++i) sL[i] = sL[i - 1] + L[i]; for (int i = 1; i <= maxdp; ++i) { isum[i] = L[i]; isum[i] *= (maxdp - i + 1); sum[i] = isum[i] + sum[i - 1]; } int answer = 0; for (int ed = 2; ed <= maxdp; ++ed) { int it = 2, f = ed, m; while (it != f) { m = (it + f) / 2; pair<long long int, int> tmp = get(m, ed); if (tmp.first >= 0) f = m; else it = m + 1; } pair<long long int, int> tmp = get(f, ed); tmp.second = min(tmp.second, k); answer = max(answer, tmp.second); } cout << answer << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; int n, X, Y, x, y, m, l, r, u, d, i; int main() { cin >> n >> X >> Y; for (; i < n; i++) { cin >> x >> y; if (x < X) l++; if (x > X) r++; if (y < Y) d++; if (y > Y) u++; } m = max(max(l, r), max(d, u)); if (m == l) X--; else if (m == r) X++; else if (m == d) Y--; else Y++; cout << m << '\n' << X << ' ' << Y; }
2
#include <bits/stdc++.h> #pragma GCC optimize("-O3") using namespace std; long long MOD = 1e9 + 7; long long mpow(long long a, long long b) { if (b >= (MOD - 1)) b %= (MOD - 1); if (b == 0) return 1; long long t1 = mpow(a, b / 2); t1 *= t1; t1 %= MOD; if (b % 2) t1 *= a; t1 %= MOD; return t1; } long long mpow(long long a, long long b, long long p) { if (b == 0) return 1; long long t1 = mpow(a, b / 2, p); t1 *= t1; t1 %= p; if (b % 2) t1 *= a; t1 %= p; return t1; } long long modinverse(long long a, long long m) { long long m0 = m; long long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { long long q = a / m; long long t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } long long range(long long l, long long r) { return l + rand() % (r - l + 1); } long long n; long long m, r, c; vector<pair<long long, long long> > vec; vector<long long> pr; long long check(long long v) { long long l1 = 0, r1 = ((long long)(vec).size()); while (l1 != r1) { long long mid = (l1 + r1) / 2; if (vec[mid].first < v) l1 = mid + 1; else r1 = mid; } long long am = (((long long)(vec).size()) - l1) * v + (l1 ? pr[l1 - 1] : 0); return am; } void solve() { cin >> n; map<long long, long long> m1; for (long long i = 0; i < n; i++) { long long t; cin >> t; m1[t]++; } for (auto it : m1) { vec.push_back(make_pair(it.second, it.first)); } sort((vec).begin(), (vec).end()); long long pre = 0; for (long long i = 0; i < ((long long)(vec).size()); i++) { pre += vec[i].first; pr.push_back(pre); } for (int i = 1; i < n + 1; i++) { long long l = 0, r1 = i; while (l != r1) { long long mid = (l + r1 + 1) / 2; if (check(mid) >= i * mid) l = mid; else r1 = mid - 1; } if (i * l > m) { m = i * l; r = i; c = l; } } for (long long i = 0; i < ((long long)(vec).size()); i++) vec[i] = make_pair(min(c, vec[i].first), vec[i].second); cout << m << "\n"; cout << r << " " << c << "\n"; long long a[r][c]; long long cr = 0, cc = 0; long long ci = ((long long)(vec).size()) - 1; while (1) { a[cr][cc] = vec[ci].second; vec[ci].first--; if (vec[ci].first == 0) ci--; if (cc == c - 1) { cr = (cr - cc + 1 + r) % r; cc = 0; } else { cr++; cc++; cr %= r; } if (cc == 0 && cr == 0) break; } for (long long i = 0; i < r; i++) { for (long long j = 0; j < c; j++) cout << a[i][j] << " "; cout << "\n"; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t = 1; for (long long i = 0; i < t; i++) { solve(); } return 0; }
7
#include <bits/stdc++.h> std::pair<int, int> e[1000005]; int d[305]; int main() { int n, t = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &d[i]); for (int i = 1, x = 1; i <= n; ++i) { for (int j = x; j < x + d[i]; ++j) for (int k = j + 1; k <= x + d[n]; ++k) e[++t] = std::make_pair(j, k); for (int j = i + 1; j <= n; ++j) d[j] -= d[i]; x += d[i]; --n; } printf("%d\n", t); for (int i = 1; i <= t; ++i) printf("%d %d\n", e[i].first, e[i].second); return 0; }
8
#include <bits/stdc++.h> using namespace std; long long sum; int main() { int m, a, b; int maxi = 0; int y; cin >> m >> a >> b; int x; x = 0; sum += m + 1; do { while (x - b < 0 && x + a <= m) { x = x + a; maxi = max(maxi, x); sum += m + 1 - maxi; } y = x; while (y + a <= m) { y = y + a; sum += m + 1 - max(maxi, y); } if (x - b >= 0) { x = x - b; if (x > 0) { maxi = max(maxi, x); sum += m + 1 - maxi; } } } while (x != 0 && (x - b >= 0 || x + a <= m)); cout << sum; return 0; }
6
#include <bits/stdc++.h> using namespace std; const int N = 300005; char s1[N], s2[N], s[N]; int main() { int len, i, set = 0; scanf("%s", s1); scanf("%s", s2); len = strlen(s1); sort(s1, s1 + len); sort(s2, s2 + len); int len2 = len / 2; int len1 = len - len2; for (i = 0; i < len2; i++) s2[i] = s2[len - 1 - i]; int p1 = 0, p2 = 0; s1[len1] = '\0'; s2[len2] = '\0'; for (i = 0; i < len; i++) { if (s1[p1] >= s2[p2]) break; if (i % 2 == 0) { s[i] = s1[p1++]; } else { s[i] = s2[p2++]; } } p1 = len1 - 1; p2 = len2 - 1; set = len - 1; for (; i < len; i++) { if (i % 2 == 0) { s[set--] = s1[p1--]; } else { s[set--] = s2[p2--]; } } printf("%s", s); return 0; }
5
#include <bits/stdc++.h> using namespace std; long long n, m = 0, k = 0; bool test = false, test1 = false; long long const N = 1e5 + 7; long long const oo = 1000000007; long long nChoosek(long long n, long long k) { if (k > n) return 0; if (k * 2 > n) k = n - k; if (k == 0) return 1; long long result = n; for (long long i = 2; i <= k; ++i) { result *= (n - i + 1); result /= i; } return result; } vector<long long> v, v1; vector<pair<long long, long long> > vv; map<pair<long long, long long>, long long> my1; map<long long, long long> my; priority_queue<long long> q; long long t[N]; long long dx[4] = {0, 1, 0, -1}; long long dy[4] = {1, 0, -1, 0}; vector<long long> v2[N]; long long z[N]; void dfs(long long s) { if (z[s]) return; z[s] = 1; for (long long u = 0; u < v2[s].size(); u++) { dfs(v2[s][u]); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long i = 0, j = 0, a = 0, b = 0, c = 0, d = -1e18, x = 0, y = 0, s = 0, r, l; string s1, s2, s3, s4, s5; cin >> a >> b; c = abs(a - b); if (c == 0) { cout << 0; return 0; } k = c / 2; if (c % 2) d = k + 1; else d = k; cout << (d * (d + 1)) / 2 + (k * (k + 1)) / 2; return 0; }
0
#include <bits/stdc++.h> using namespace std; mt19937 mt(1904262252); int get_difference(int m, vector<int> v) { if (v.size() * 2 > m) { vector<int> nxtv; for (int i = 0; i < m; ++i) { if (!binary_search(v.begin(), v.end(), i)) { nxtv.push_back(i); } } v = nxtv; } if (v.size() <= 1) return 1; int inv2 = (m + 1) / 2; uniform_int_distribution<int> p(0, v.size() - 1); int rep = 3000000; int opt = -1; map<int, int> sols; for (int i = 0; i < rep; ++i) { int a = v[p(mt)], b = v[p(mt)]; if (a == b) continue; int cnt = 0; while (true) { int z = 1LL * (a + b) * inv2 % m; if (!binary_search(v.begin(), v.end(), z)) break; ++cnt; b = z; } if (opt < cnt) { opt = cnt; sols.clear(); } if (opt == cnt) ++sols[(b - a + m) % m]; } int ans = -1, freq = -1; for (pair<int, int> i : sols) { if (freq < i.second) { freq = i.second; ans = i.first; } } return ans; } int main() { int M, N; cin >> M >> N; vector<int> A(N); for (int i = 0; i < N; ++i) { cin >> A[i]; } sort(A.begin(), A.end()); if (A.front() == A.back()) { cout << A.front() << ' ' << 0 << endl; } else { int pre = 0; vector<int> v; for (int i = 1; i <= N; ++i) { if (i == N || A[i] != A[i - 1]) { if ((i - pre) == N / M + 1) { v.push_back(A[i - 1]); } } } int d = get_difference(M, A); multiset<int> st; for (int i = 0; i < N; ++i) { st.insert(A[i]); } int cur = A[0]; while (true) { st.erase(st.lower_bound(cur)); cur = (cur - d + M) % M; if (st.find(cur) == st.end()) { cur = (cur + d) % M; break; } } vector<int> ra; for (int i = 0; i < N; ++i) { ra.push_back((cur + 1LL * i * d) % M); } sort(ra.begin(), ra.end()); if (ra == A) { cout << cur << ' ' << d << endl; } else { cout << -1 << endl; } } return 0; }
9
#include <bits/stdc++.h> using namespace std; template <class TH> void _dbg(const char *sdbg, TH h) { cerr << sdbg << " = " << h << "\n"; } template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while (*sdbg != ',') cerr << *sdbg++; cerr << " = " << h << ", "; _dbg(sdbg + 1, a...); } const int maxn = 2e5 + 7; const int inf = 1e9 + 7; const long long LLinf = ((long long)1e18) + 7LL; const long double eps = 1e-9; int n; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n; if (n == 1) cout << 1 << " 2" << "\n" << "1 2"; else cout << n + (n - 2) << " 2" << "\n" << "1 2"; }
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; vector<pair<int, int>> arr(m); pair<int, int> p; for (int i = 0; i < m; i++) { cin >> p.second >> p.first; arr[i] = p; } sort(arr.begin(), arr.end(), [](pair<int, int> a, pair<int, int> b) { if (a.first < b.first) return 0; else { if (a.first == b.first) if (a.second < b.second) return 1; else return 0; else return 1; } }); int sum = 0; for (int i = 0; i < m; i++) { if (n < arr[i].second) { sum += arr[i].first * n; break; } else { n -= arr[i].second; sum += arr[i].first * arr[i].second; } } cout << sum; return 0; }
0
#include <bits/stdc++.h> using namespace std; struct P { int x, y, z; bool operator<(const P &a) const { return x > a.x; } }; int a, b, c, i, k, o[3][800002], n, m, d, e, dy[15] = {0, 0, -1, 1, -1, 1, -1, 1}, dx[15] = {-1, 1, 0, 0, 1, 1, -1, -1}; int l[100100]; long long x, y, z, v1, v2; P u[200600]; int j[60]; queue<int> q, q1; stack<int> s; map<int, int> p; string r, r1, r2; bool as(int a, int b) { return a > b; } int f(int h, int t, int w) { int u = 0; for (; t < w;) { if (t % 2) u = max(u, o[h][t]), t++; else if (w % 2 == 0) u = max(u, o[h][w]), w--; else t /= 2, w /= 2; } return max(u, o[h][t]); } int main() { scanf("%d %d %d", &a, &l[0], &l[1]); for (i = 1; i < 100000; i <<= 1) ; for (int t = 1; t <= a; t++) { scanf("%d %d %c", &n, &m, &d); if (o[d - 67][m + i - 1]) { if (m * 2 <= l[d - 67]) k = max(k, o[d - 67][m + i - 1] + n); o[d - 67][m + i - 1] = max(o[d - 67][m + i - 1], n); } else o[d - 67][m + i - 1] = n; if (m <= l[d - 67]) l[d - 65] = max(l[d - 65], n); } if (l[2] && l[3]) k = max(k, l[2] + l[3]); for (int h = 0; h < 2; h++) for (int t = i - 1; t > 0; t--) o[h][t] = max(o[h][t * 2], o[h][t * 2 + 1]); for (int h = 0; h < 2; h++) { for (int t = l[h] - 1; t > 1; t--) { if (o[h][t + i - 1] && f(h, i, min(t + i - 2, i + l[h] - t - 1))) k = max(k, o[h][t + i - 1] + f(h, i, min(t + i - 2, i + l[h] - t - 1))); } } printf("%d", k); }
5
#include <bits/stdc++.h> using namespace std; char s[1005]; int main() { int ret = 0; int p = 0; while (gets(s)) { if (s[0] == '+') p++; else if (s[0] == '-') p--; else { int cnt = 0; while (s[cnt] != ':') cnt++; ret += (strlen(s) - cnt - 1) * p; } } printf("%d\n", ret); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxN = 405; int dp[maxN][maxN]; int fact[maxN]; int f[maxN]; int cnk[maxN][maxN]; int tst; int mod; int n; int sum(int a, int b) { int s = a + b; if (s >= mod) s -= mod; return s; } int sub(int a, int b) { int s = a - b; if (s < 0) s += mod; return s; } int mult(int a, int b) { return (1LL * a * b) % mod; } void solve() { cin >> n; } int main() { cin >> tst >> mod; cnk[0][0] = 1; fact[0] = 1; int lim = 400; for (int i = 1; i <= lim; i++) { fact[i] = mult(fact[i - 1], i); cnk[i][0] = 1; for (int j = 1; j <= i; j++) { cnk[i][j] = sum(cnk[i - 1][j], cnk[i - 1][j - 1]); } } dp[0][0] = 1; for (int cnt = 0; cnt <= lim; cnt++) { for (int s = 0; s <= lim; s++) { for (int add = 1; add <= lim; add++) { if (s + add <= lim) { dp[cnt + 1][s + add] = sum(dp[cnt + 1][s + add], mult(dp[cnt][s], fact[add])); } } } } for (int i = 3; i <= lim; i++) { f[i] = fact[i]; for (int j = 3; j < i; j++) { f[i] = sub(f[i], mult(f[j], dp[j][i])); } int q = 0; for (int k = 1; k <= i + 1; k++) { if (k & 1) { q = sum(q, dp[k + 1][i]); } else { q = sub(q, dp[k + 1][i]); } } q = mult(q, 2); f[i] = sub(f[i], q); } for (int i = 1; i <= tst; i++) { int n; cin >> n; if (n <= 2) cout << n << '\n'; else cout << f[n] << '\n'; } return 0; }
9
#include <bits/stdc++.h> using namespace std; const int Size = 1 << 18; inline char getch() { static char buf[Size], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, Size, stdin), p1 == p2) ? EOF : *p1++; } int read() { int s = 0, f = 1; char c = getch(); while (c > '9' || c < '0') { if (c == '-') f = -1; c = getch(); } while (c <= '9' && c >= '0') { s = (s << 1) + (s << 3) + c - '0'; c = getch(); } return s * f; } const double eps = 1e-6; struct node { double x, y, r; } a[3]; double dist(double x, double y, node p) { return sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y)); } double cost(double x, double y) { double ange[3], dis[3], ans = 0; for (int i = (0); i <= (2); ++i) ange[i] = dist(x, y, a[i]) / a[i].r; for (int i = (0); i <= (2); ++i) dis[i] = ange[i] - ange[(i + 1) % 3]; for (int i = (0); i <= (2); ++i) ans += dis[i] * dis[i]; return ans; } int main(int argc, char const *argv[]) { while (~scanf("%lf%lf%lf", &a[0].x, &a[0].y, &a[0].r)) { for (int i = (1); i <= (2); ++i) { scanf("%lf%lf%lf", &a[i].x, &a[i].y, &a[i].r); } double x = 0, y = 0; for (int i = (0); i <= (2); ++i) x += a[i].x / 3, y += a[i].y / 3; double t = 1.0; while (t > eps) { int kase = 0; double tp = cost(x, y); if (cost(x + t, y) < tp) x += t, kase = 1; else if (cost(x - t, y) < tp) x -= t, kase = 1; else if (cost(x, y + t) < tp) y += t, kase = 1; else if (cost(x, y - t) < tp) y -= t, kase = 1; if (!kase) t /= 2; } if (fabs(cost(x, y) < eps)) printf("%lf %lf\n", x, y); } return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long res = 0; int n, t, rf = INT_MAX, rl = -INT_MAX, rmx = 0, bf = INT_MAX, bl = -INT_MAX, bmx = 0, pl = 0; cin >> n; char c; bool p = false; while (n--) { cin >> t >> c; if (c == 'R') { if (p == false) { rf = min(rf, t); rl = max(rl, t); } else { rmx = max(t - rl, rmx); rf = min(rf, t); rl = max(rl, t); } } else if (c == 'P') { if (p == false) { p = true; if (rf != INT_MAX) res += t - rf; if (bf != INT_MAX) res += t - bf; rl = bl = pl = t; } else { if (bl > pl) bmx = max(bmx, t - bl); if (rl > pl) rmx = max(rmx, t - rl); if (bl == pl && rl == pl) res += t - pl; else if (bl == pl) res += 2 * (t - pl) - rmx; else if (rl == pl) res += 2 * (t - pl) - bmx; else res += min(2 * (t - pl), 3 * (t - pl) - rmx - bmx); rmx = bmx = 0; rl = bl = pl = t; } } else { if (p == false) { bf = min(bf, t); bl = max(bl, t); } else { bmx = max(t - bl, bmx); bf = min(bf, t); bl = max(bl, t); } } } if (!p) { if (rl != -INT_MAX) res += rl - rf; if (bl != -INT_MAX) res += bl - bf; } if (p) { res += bl + rl - pl - pl; } cout << res; return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, c, d; cin >> a >> b >> c >> d; if (a + b == c + d || a + c == d + b || a + d == c + b || a == c + b + d || c == a + b + d || b == c + d + a || d == a + b + c) cout << "YES\n"; else cout << "NO\n"; }
0
#include <bits/stdc++.h> using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int tc; cin >> tc; for (int i = 1; i <= tc; i++) { int n; cin >> n; vector<int> a(n); int coins = 1e9 + 7; for (int i = 0; i < n; i++) { cin >> a[i]; } auto check = [&](int one, int two, int val) -> int { vector<int> combination; for (int i = 0; i <= one; i++) { for (int j = 0; j <= two; j++) { combination.emplace_back(i + 2 * j); } } int cur_coins = 1e9 + 7; for (int i : combination) { if ((val < i) or (val - i) % 3 != 0) continue; cur_coins = min(cur_coins, (val - i) / 3); } return cur_coins + one + two; }; for (int one = 0; one < 3; one++) { for (int two = 0; two < 3; two++) { int cur = 0; for (int i : a) { cur = max(cur, check(one, two, i)); } coins = min(coins, cur); } } cout << coins << '\n'; } return 0; }
6
#include <bits/stdc++.h> using namespace std; using LL = long long; const int mod = 998244353; const int MK = 77, MX = 4002; int N1, N2, K, res[2][MK], ans, fa[MX]; int s[MX][MK], f[MX][MK]; vector<int> G[MX]; void dfs1(int u, int f) { fa[u] = f; int *cur = s[u]; memset(cur, 0, sizeof(int) * (K + 1)); cur[0] = 1; for (int i : G[u]) if (i != f) dfs1(i, u); for (int j = 2; j <= K; j += 2) for (int i : G[u]) if (i != f) for (int k = 0; k <= j - 2; k += 2) cur[j] = (cur[j] + (LL)cur[j - k - 2] * s[i][k]) % mod; } void dfs2(int u, int _f) { for (int i : G[u]) if (i != _f) memset(f[i], 0, sizeof(int) * (K + 1)), f[i][0] = 1; for (int k = 2; k <= K; k += 2) { static int tmp[MK]; memset(tmp, 0, sizeof(int) * (K + 1)); for (int i = 0; i <= k - 2; i += 2) tmp[i] = (tmp[i] + f[u][i]) % mod; for (int i : G[u]) if (i != _f) for (int j = 0; j <= k - 2; j += 2) tmp[j] = (tmp[j] + s[i][j]) % mod; for (auto i : G[u]) if (i != _f) { int *cur = s[i]; for (int j = 0; j <= k - 2; j += 2) tmp[j] = (tmp[j] + mod - cur[j]) % mod; for (int j = 0; j <= k - 2; j += 2) f[i][k] = (f[i][k] + (LL)f[i][k - j - 2] * tmp[j]) % mod; for (int j = 0; j <= k - 2; j += 2) tmp[j] = (tmp[j] + cur[j]) % mod; } } for (int i : G[u]) if (i != _f) dfs2(i, u); } void solve(int N, int *ans) { for (int i = 1; i <= N; i++) G[i].clear(); for (int i = 1, u, v; i < N; i++) { cin >> u >> v; G[u].push_back(v); G[v].push_back(u); } dfs1(1, 0), dfs2(1, 0); for (int i = 1; i <= N; i++) { static int dp[MK]; memset(dp, 0, sizeof(int) * (K + 1)); dp[0] = 1; for (int j = 2; j <= K; j += 2) { for (int k = 0; k <= j - 2; k += 2) dp[j] = (dp[j] + (LL)f[i][k] * dp[j - k - 2]) % mod; for (int u : G[i]) if (u != fa[i]) for (int k = 0; k <= j - 2; k += 2) dp[j] = (dp[j] + (LL)s[u][k] * dp[j - k - 2]) % mod; } for (int j = 0; j <= K; j += 2) ans[j] = (ans[j] + dp[j]) % mod; } } int main() { static int C[MK][MK]; for (int i = 0; i <= 75; i++) C[i][0] = 1; for (int i = 1; i <= 75; i++) for (int j = 1; j <= i; j++) C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % mod; ios::sync_with_stdio(0); cin >> N1 >> N2 >> K; solve(N1, res[0]), solve(N2, res[1]); for (int i = 0; i <= K; i++) ans = (ans + (LL)C[K][i] * res[0][i] % mod * res[1][K - i]) % mod; cout << ans << "\n"; return 0; }
10
#include <bits/stdc++.h> using namespace std; int main() { double d = 16.180339887498949; double x[] = {0, 13.090169943749475, 8.090169943749475, 3.090169943749475}; double y[] = {0, -9.510565162951535, 5.877852522924732, -9.510565162951535}; int n; scanf("%d", &n); printf("%d\n", 4 * n + 1); for (int i = 0; i < 4 * n + 1; i++) printf("%.15lf %.15lf\n", x[i % 4], y[i % 4]), x[i % 4] += d; for (int i = 0; i < n; i++) printf("%d %d %d %d %d\n", 1 + 4 * i, 3 + 4 * i, 5 + 4 * i, 2 + 4 * i, 4 + 4 * i); for (int i = 0; i < n * 4; i++) printf("%d ", i + 1); for (int i = 0; i < n; i++) printf("%d ", 1 + (n - i) * 4); puts("1"); return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, i, j, k, l, m, x, z, y, t, x1, y1, z1, f, q; cin >> n; set<pair<long long, long long> > s; set<pair<long long, long long> >::iterator it; map<long long, long long> mp1, mp2; for (i = 1; i <= n; i++) { mp1[i] = -1; mp2[i] = 1000000000000; } vector<long long> value[n + 1]; vector<long long>::iterator ite; for (i = 0; i < n; i++) { cin >> x >> y; value[x].push_back(y); if (y >= mp1[x]) { s.erase(make_pair(mp1[x], x)); s.insert(make_pair(y, x)); mp1[x] = y; } mp2[x] = min(mp2[x], y); } cin >> q; while (q--) { cin >> m; vector<long long> v; for (i = 0; i < m; i++) { cin >> x; if (mp1[x] != -1) { v.push_back(x); s.erase(make_pair(mp1[x], x)); } } if (s.size() == 0) { cout << "0 0" << "\n"; } else { if (s.size() > 1) { it = s.end(); it--; y = it->second; it--; x = it->first; ite = upper_bound(value[y].begin(), value[y].end(), x); long long in = ite - value[y].begin(); z = value[y][in]; cout << y << " " << z << "\n"; } else { it = s.begin(); cout << it->second << " " << mp2[it->second] << "\n"; } } for (i = 0; i < v.size(); i++) { s.insert(make_pair(mp1[v[i]], v[i])); } v.clear(); } return 0; }
6
#include <bits/stdc++.h> using namespace std; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int tests = 1; cin >> tests; while (tests--) { long long int n; cin >> n; long long int a[n]; for (long long int i = 0; i < n; i++) cin >> a[i]; map<long long int, vector<long long int>> v; set<long long int> second; vector<pair<long long int, long long int>> v1; for (long long int i = 0; i < n; i++) v[a[i]].push_back(i); for (auto it : v) { vector<long long int>& vals = it.second; long long int x = it.first; long long int max1 = max(vals[0], n - 1 - vals.back()); for (long long int i = 1; i < vals.size(); i++) max1 = max(max1, vals[i] - vals[i - 1] - 1); v1.push_back({max1, x}); } sort(v1.begin(), v1.end()); long long int siz = v1.size(); long long int cur = 1; long long int ptr = 0; while (cur <= n) { while (ptr < siz && v1[ptr].first < cur) { second.insert(v1[ptr].second); ptr++; } if (second.empty()) cout << -1 << " "; else { long long int x = *second.begin(); cout << x << " "; } cur++; } cout << endl; } return 0; }
3
#include <bits/stdc++.h> void C() { long long n, p, w, d; std::cin >> n >> p >> w >> d; for (long long y = 0; y < w; y++) { long long x = p - y * d; if (x >= 0 && x % w == 0 && x / w + y <= n) { std::cout << x / w << ' ' << y << ' ' << n - x / w - y << '\n'; return; } } std::cout << -1 << '\n'; return; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); long long t; t = 1; while (t--) { C(); } std::cout.flush(); return 0; }
6
#include <bits/stdc++.h> int min(int a, int b) { if (a < b) return a; else return b; } int main() { int i, n, a, b, c, d, x, y, f = 0, idx, ans; scanf("%d", &n); for (i = 1; i <= 4; i++) { scanf("%d %d %d %d", &a, &b, &c, &d); x = min(a, b); y = min(c, d); if ((x + y) <= n && f == 0) { f = 1; idx = i; ans = x; } } if (f == 0) printf("-1\n"); else printf("%d %d %d\n", idx, ans, n - ans); return 0; }
1
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; const int maxn = 1e6 + 10; const int dx[] = {1, 0, -1, 0}; const int dy[] = {0, 1, 0, -1}; int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } int main(int argc, char const *argv[]) { int n, k; while (cin >> n >> k) { if (n == k) { cout << "0" << " " << "0" << endl; continue; } int t = n / 3; if (k == 0) { cout << "0" << " " << "0" << endl; continue; } if (n <= 3 * k) { cout << 1 << " "; cout << n - k << endl; continue; } else if (n <= 2 * k) { cout << 1 << " "; cout << n - k << endl; continue; } else { cout << 1 << " "; cout << 2 * k << endl; continue; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; vector<vector<int> > graph(30001); int visited[30001] = {0}; bool dfs(int pos, int end) { if (pos == end) { return true; } else { for (int x = 0; x < graph[pos].size(); x++) { if (visited[graph[pos][x]] == 0) { visited[graph[pos][x]] = 1; if (dfs(graph[pos][x], end)) { return true; } } } } return false; } int main() { int n, t, current; cin >> n >> t; for (int x = 1; x <= n; x++) { cin >> current; graph[x].push_back(x + current); } if (dfs(1, t)) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; }
1
#include <iostream> #include <vector> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int x; bool neg = 0; for (int i = 0; i < n; i++) { cin >> x; neg |= (x < 0); } if (neg) cout << "NO\n"; else { cout << "YES\n"; cout << 101 << '\n'; for (int i = 0; i <= 100; i++) cout << i << ' '; cout << '\n'; } } }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int p = 0, q = 0, r = 0; int z; for (int i = 0; i < n; i++) { cin >> z; p = p + z; cin >> z; q = q + z; cin >> z; r = r + z; } if (p == 0 && q == 0 && r == 0) { cout << "YES"; } else { cout << "NO"; } }
1
#include <bits/stdc++.h> using namespace std; int n; vector<string> v; int main() { cin >> n; for (int i = 0; i < n; i++) { string command; cin >> command; if (command == "cd") { string dir; cin >> dir; if (dir[0] == '/') { v.clear(); string s; dir += '/'; for (int j = 0; j < dir.size(); j++) { if (dir[j] != '/') s += dir[j]; else { if (s == "..") { v.pop_back(); s = ""; } else { if (s != "") v.push_back(s); s = ""; } } } } else { string s; dir += '/'; for (int j = 0; j < dir.size(); j++) { if (dir[j] != '/') s += dir[j]; else { if (s == "..") { v.pop_back(); s = ""; } else { if (s != "") v.push_back(s); s = ""; } } } } } else { cout << '/'; for (int i = 0; i < v.size(); i++) { cout << v[i] << '/'; } cout << '\n'; } } }
3
#include <bits/stdc++.h> using namespace std; const int oo = 0x3f3f3f3f; const long long ooo = 9223372036854775807ll; const int _cnt = 1000 * 1000 + 7; const int _p = 1000 * 1000 * 1000 + 7; const int N = 100005; const double PI = acos(-1.0); const double eps = 1e-9; int o(int x) { return x % _p; } int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int lcm(int a, int b) { return a / gcd(a, b) * b; } void file_put() { freopen("filename.in", "r", stdin); freopen("filename.out", "w", stdout); } int n, k, T, g, ans; int Pow(int a, int k) { if (k == 0) return 1; if (k == 1) return a; int t = Pow(a, k >> 1); t = (long long)t * t % _p; if (k & 1) return (long long)t * a % _p; else return t; } inline int inv(int x) { if (x <= 0) x += _p; return Pow(x, _p - 2); } int sum1(int q, int n) { if (n <= 0) return 0; if (q == 1) return n; return (long long)inv(q - 1) * (Pow(q, n) - 1) % _p; } int sum2(int q, int n) { if (n <= 0) return 0; if (q == 1) return (long long)n * (n + 1) / 2 % _p; int t = inv(q - 1); return (-n + (long long)q * (Pow(q, n) - 1) % _p * t % _p) % _p * t % _p; } int solve(int n, int k, int a1, int b1, int a2, int b2, int c1, int c2, int c0) { if (n == k) { int x = (long long)b2 * inv(1 - a2) % _p; return ((long long)c1 * x % _p + c0) % _p; } int q = (n - 1) / k, r = (n - 1) % k + 1, a, b, A1, B1, A2, B2, C1, C2, C0; a = (long long)a2 * Pow(a1, q - 1) % _p, b = (long long)a2 * b1 % _p * sum1(a1, q - 1) % _p + b2, b %= _p; A1 = inv(a), B1 = (long long)-b * A1 % _p; a = (long long)a2 * Pow(a1, q) % _p, b = (long long)a2 * b1 % _p * sum1(a1, q) % _p + b2, b %= _p; A2 = inv(a), B2 = (long long)-b * A2 % _p; C1 = c1 + (long long)c2 * a1 % _p * sum1(a1, q) % _p, C1 %= _p; C2 = c1 + (long long)c2 * a1 % _p * sum1(a1, q - 1) % _p, C2 %= _p; C0 = c0 + (long long)c2 * b1 % _p * ((long long)r * sum1(a1, q) % _p + (long long)k * sum2(a1, q - 1) % _p) % _p, C0 %= _p; return solve(k, r, A1, B1, A2, B2, C1, C2, C0); } int main() { scanf("%d", &T); while (T--) { scanf("%d%d", &n, &k); g = gcd(n, k); n /= g, k /= g; ans = solve(n, k, 1, 1, (_p + 1) / 2, 1, 1, 1, 0); ans = (long long)ans * inv(n) % _p; ans = ((long long)ans + _p) % _p; printf("%d\n", ans); } return 0; }
12
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, i, j, m, a, b; cin >> n; long long total = 0; map<int, int> x; for (i = 0; i < n; i++) { cin >> a >> b; x[a] = b; } cin >> m; map<int, int> y; for (i = 0; i < m; i++) { cin >> a >> b; y[a] = b; } for (auto const& it : x) { if (y[it.first]) { total += (long long)max(it.second, y[it.first]); } else { total += (long long)it.second; } } for (auto const& it : y) { if (x[it.first]) { } else { total += (long long)it.second; } } cout << total << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, k, m; string s[100005]; int is_vol(char c) { string t = "aeiou"; for (int i = 0; i < t.size(); i++) { if (c == t[i]) return 1; } return 0; } int ok(vector<string> v) { if (v.size() == 0) return 1; set<string> v2; for (int i = 0; i < v.size(); i++) { string temp; int cnt = 0; int ok = 0; for (int j = 0; j < v[i].size(); j++) { if (is_vol(v[i][j])) { cnt++; } if (!ok) temp += v[i][j]; if (cnt == k) { ok = 1; } } if (!ok) return 0; v2.insert(temp); } if (v2.size() == 1) return 1; return 0; } int solve(string t) { vector<int> A, B; for (int i = 0; i < 4; i++) { if (t[i] == 'a') { A.push_back(i); } else { B.push_back(i); } } for (int i = 0; i < m; i += 4) { string c[4]; c[0] = s[i]; c[1] = s[i + 1]; c[2] = s[i + 2]; c[3] = s[i + 3]; vector<string> v1, v2; for (int j = 0; j < A.size(); j++) { v1.push_back(c[A[j]]); } for (int j = 0; j < B.size(); j++) { v2.push_back(c[B[j]]); } if (ok(v1) && ok(v2)) { } else return 0; } return 1; } int main() { cin >> n >> k; m = n * 4; for (int i = 0; i < m; i++) { cin >> s[i]; reverse(s[i].begin(), s[i].end()); } if (solve("aaaa")) { cout << "aaaa"; } else if (solve("aabb")) { cout << "aabb"; } else if (solve("abab")) { cout << "abab"; } else if (solve("abba")) { cout << "abba"; } else { cout << "NO"; } }
4
#include <bits/stdc++.h> using namespace std; void solve() { long long int n; cin >> n; int c_2 = 0, c_3 = 0, c_5 = 0; while (!(n % 2)) { ++c_2; n /= 2; } while (!(n % 3)) { ++c_3; n /= 3; } while (!(n % 5)) { ++c_5; n /= 5; } if (n != 1) { cout << "-1" << endl; return; } cout << (c_2 + 2 * c_3 + 3 * c_5) << endl; } int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int tcases; cin >> tcases; while (tcases--) solve(); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long n, a, b; cin >> n >> a >> b; n *= 6; bool flag = false; if (a > b) { swap(a, b); flag = true; } long long ans = (long long)9e18; long long ax = -1, ay = -1; for (long long xx = 1; xx <= n; xx++) { long long x = xx; long long y = (n + x - 1) / x; if (y < x) { break; } if (x < a) x = a; if (y < b) y = b; if (x * y <= ans) { ans = x * y; ax = x; ay = y; } } if (flag) { swap(ax, ay); } cout << ans << endl; cout << ax << " " << ay << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; long long a, n, res; int main() { scanf("%lld%lld", &a, &n); n += a; for (long long l = 1; l <= n; l++) { long long c = n / l, r = n / c; long long x = a % c, y = a / c - x; if (y >= 0 && (n - x) / c >= (n + y + c) / (c + 1)) res += max( min((n - x) / c, r) - max((n + y + c) / (c + 1) - y / (c + 1), l) + 1, 0ll); l = r; } cout << res << endl; return 0; }
9
#include <bits/stdc++.h> using namespace std; long long int modpow(long long int a, long long int n, long long int temp) { long long int res = 1, y = a; while (n > 0) { if (n & 1) res = (res * y) % temp; y = (y * y) % temp; n /= 2; } return res % temp; } int main() { int p = 1, n = 0, x, y, N = 0, flag = 0, P = 1, k, mx = 0, my = 0; string bus; getline(cin, bus); int l = (int)(bus.size()); for (int i = 0; i < l; ++i) { if (bus[i] == '=') k = i; } k++; for (int i = l - 1; i > k; --i) { N += P * (bus[i] - '0'); P *= 10; } for (int i = 1; i < l; i++) { if (bus[i] == '+') p++; else if (bus[i] == '-') n++; } for (y = n; y <= N * n; ++y) { x = N + y; if (x > N * p) break; else if (x >= p) { flag = 1; break; } } if (!flag) { printf("Impossible\n"); return 0; } else { if (p > 0) mx = x % p; if (n > 0) my = y % n; int tm = 0; printf("Possible\n"); if (mx) { tm = 1; mx--; } printf("%d ", x / p + tm); for (int i = 2, f = 0; i < l && bus[i] != '='; i += 4) { if (bus[i] == '+') { if (mx > 0) { tm = 1; mx--; } else tm = 0; printf("+ %d ", x / p + tm); } else { if (my > 0) { tm = 1; my--; } else tm = 0; printf("- %d ", y / n + tm); } } printf("= %d\n", N); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, m, ans, a[10001000], vis[10001000]; bool hav[10001000]; void dfs(int x) { if (vis[x]) return; vis[x] = 1; for (int i = 0; i < n; i++) if (x & (1 << i)) dfs(x ^ (1 << i)); if (hav[x]) dfs(x ^ ((1 << n) - 1)); return; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) scanf("%d", &a[i]), hav[a[i]] = 1; for (int i = 1; i <= m; i++) if (!vis[a[i]]) dfs(a[i]), ans++; printf("%d", ans); return 0; }
8
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16000000") using namespace std; const int Maxn = 200005; const int Maxm = 21; const int Maxmy = 500005; int n; vector<int> neigh[Maxn]; int L[Maxn], P[Maxn][Maxm]; int distA[Maxn], distB[Maxn]; int trunk[Maxn]; vector<int> my[Maxmy]; long long res; vector<pair<pair<int, int>, int> > ans; void Traverse(int v) { for (int i = 0; i < neigh[v].size(); i++) { int u = neigh[v][i]; if (u == P[v][0]) continue; P[u][0] = v; L[u] = L[v] + 1; Traverse(u); } } int getLCA(int a, int b) { if (L[a] < L[b]) swap(a, b); for (int i = Maxm - 1; i >= 0; i--) if (L[a] - (1 << i) >= L[b]) a = P[a][i]; if (a == b) return a; for (int i = Maxm - 1; i >= 0; i--) if (P[a][i] != P[b][i]) a = P[a][i], b = P[b][i]; return P[a][0]; } int getDist(int a, int b) { return L[a] + L[b] - 2 * L[getLCA(a, b)]; } int main() { scanf("%d", &n); for (int i = 0; i < n - 1; i++) { int a, b; scanf("%d %d", &a, &b); neigh[a].push_back(b); neigh[b].push_back(a); } Traverse(1); for (int j = 1; j < Maxm; j++) for (int i = 1; i <= n; i++) P[i][j] = P[P[i][j - 1]][j - 1]; int best = -1, a = -1, b = -1; for (int i = 1; i <= n; i++) { int cand = getDist(1, i); if (cand > best) { best = cand; a = i; } } best = -1; for (int i = 1; i <= n; i++) { distA[i] = getDist(a, i); if (distA[i] > best) { best = distA[i]; b = i; } } for (int i = 1; i <= n; i++) distB[i] = getDist(b, i); for (int i = 1; i <= n; i++) if (distA[i] + distB[i] == distA[b]) trunk[distA[i]] = i; else my[distA[i] + distB[i]].push_back(i); for (int i = Maxmy - 1; i >= 0; i--) for (int j = 0; j < my[i].size(); j++) { int v = my[i][j]; if (distA[v] >= distB[v]) { res += distA[v]; ans.push_back(pair<pair<int, int>, int>(pair<int, int>(a, v), v)); } else { res += distB[v]; ans.push_back(pair<pair<int, int>, int>(pair<int, int>(b, v), v)); } } for (int i = distA[b]; i > 0; i--) { res += i; ans.push_back( pair<pair<int, int>, int>(pair<int, int>(a, trunk[i]), trunk[i])); } printf("%I64d\n", res); for (int i = 0; i < ans.size(); i++) printf("%d %d %d\n", ans[i].first.first, ans[i].first.second, ans[i].second); return 0; }
8
#include <bits/stdc++.h> using namespace std; const int maxn = 700 + 7; const int MOD = 1e9 + 7; const int INF = 1e9 + 7; int a[maxn]; int g[maxn][maxn]; int n; struct as { int l, r, v; bool operator<(const as& b) const { if (l < b.l) return true; if (l > b.l) return false; if (r < b.r) return true; if (r > b.r) return false; return v < b.v; } }; map<as, bool> mp; int gcd(int x, int y) { return x == 0 ? y : gcd(y % x, x); } void get() { int i, j; for (i = 0; i <= n; i++) { for (j = 0; j <= n; j++) { if (i != j && gcd(a[i], a[j]) != 1) { g[i][j] = 1; } } } return; } bool solve(int l, int r, int pos) { if (l > r) return true; if (mp.count(as{l, r, pos})) { return mp[as{l, r, pos}]; } int i, j; for (i = l; i <= r; i++) { if (g[pos][i]) { if (solve(l, i - 1, i) && solve(i + 1, r, i)) { mp[as{l, r, pos}] = true; return true; } } } mp[as{l, r, pos}] = false; return false; } int main() { int i, j, m, t, z; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &a[i]); } get(); if (solve(1, n, 0)) { puts("Yes"); } else puts("No"); return 0; }
6
#include <bits/stdc++.h> using namespace std; const int max_n = 111, inf = 1000111222; const int max_v = 100011; namespace flow { struct edge { int v; long long res_cap; edge() {} edge(int v, long long res_cap) : v(v), res_cap(res_cap) {} }; vector<edge> all; vector<int> g[max_v]; void add_edge(int u, int v, long long cap) { g[u].push_back(all.size()); all.push_back({v, cap}); g[v].push_back(all.size()); all.push_back({u, 0}); } int h[max_v], num[max_v]; bool bfs(int s, int t) { for (int i = 0; i <= t; ++i) { h[i] = -1; } h[s] = 0; queue<int> q; q.push(s); while (!q.empty()) { int v = q.front(); q.pop(); for (int id : g[v]) { if (all[id].res_cap && h[all[id].v] == -1) { h[all[id].v] = h[v] + 1; q.push(all[id].v); } } } return h[t] != -1; } int dfs(int v, int t, int f) { if (v == t) { return f; } for (int &i = num[v]; i < g[v].size(); ++i) { int id = g[v][i]; const edge &e = all[id]; if (h[v] + 1 == h[e.v] && e.res_cap) { int x = dfs(e.v, t, min(1LL * f, e.res_cap)); if (x) { all[id].res_cap -= x; all[id ^ 1].res_cap += x; return x; } } } return 0; } long long dinic(int s, int t) { long long res = 0; while (bfs(s, t)) { memset(num, 0, sizeof(num)); while (int x = dfs(s, t, 2 * inf)) { res += x; } } return res; } } // namespace flow int n, m, dist[max_n][max_n]; int c1, c2, k; int v1[max_v], attack[max_v], fluel[max_v], cost1[max_v], a[max_v]; int v2[max_v], defend[max_v], cost2[max_v], U[max_v], V[max_v], bad[max_v]; vector<pair<int, int>> all[max_n]; vector<int> rg[max_v]; void dfs(int v) { bad[v] = 1; for (int to : rg[v]) { if (!bad[to]) { dfs(to); } } } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { dist[i][j] = inf; } dist[i][i] = 0; } while (m--) { int u, v; scanf("%d%d", &u, &v); --u; --v; dist[u][v] = 1; dist[v][u] = 1; } for (int k = 0; k < n; ++k) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } scanf("%d%d%d", &c1, &c2, &k); for (int i = 0; i < c1; ++i) { scanf("%d%d%d%d", &v1[i], &attack[i], &fluel[i], &cost1[i]); --v1[i]; } for (int i = 0; i < c2; ++i) { scanf("%d%d%d", &v2[i], &defend[i], &cost2[i]); --v2[i]; all[v2[i]].push_back({defend[i], cost2[i]}); } for (int i = 0; i < n; ++i) { sort(all[i].begin(), all[i].end()); for (int j = 1; j < all[i].size(); ++j) { all[i][j].second = max(all[i][j].second, all[i][j - 1].second); } } for (int i = 0; i < c1; ++i) { a[i] = -inf; for (int v = 0; v < n; ++v) { if (dist[v1[i]][v] > fluel[i]) { continue; } auto it = upper_bound(all[v].begin(), all[v].end(), make_pair(attack[i], inf)); if (it != all[v].begin()) { --it; a[i] = max(a[i], it->second); } } if (a[i] != -inf) { a[i] -= cost1[i]; } else { bad[i] = 1; } } int s = c1, t = s + 1; long long big_inf = 10001112LL * inf; for (int i = 0; i < k; ++i) { scanf("%d%d", &U[i], &V[i]); --U[i]; --V[i]; rg[V[i]].push_back(U[i]); } for (int i = 0; i < c1; ++i) { if (bad[i]) { dfs(i); } } long long ans = 0; for (int i = 0; i < c1; ++i) { if (!bad[i]) { flow::add_edge(s, i, inf + a[i]); flow::add_edge(i, t, inf); ans -= a[i] + inf; } } for (int i = 0; i < k; ++i) { if (!bad[U[i]] && !bad[V[i]]) { flow::add_edge(U[i], V[i], big_inf); } } ans += flow::dinic(s, t); printf("%I64d\n", -ans); return 0; }
9
#include <bits/stdc++.h> using namespace std; int a[21021020]; char aa[] = "RGB"; char bb[] = "GBR"; char cc[] = "BRG"; int sa[2102020]; int sb[2020220]; int sc[2020200]; int main() { int T; cin >> T; while (T--) { int n, m; cin >> n >> m; getchar(); char c; char last; a[1] = 1; int ju = 1; int ans = -111, num = 1; int ll, rr; int u = 0; for (int i = 1; i <= n; i++) { scanf("%c", &c); if (c == aa[u]) { sa[i] = sa[i - 1] + 1; } else sa[i] = sa[i - 1]; if (c == bb[u]) { sb[i] = sb[i - 1] + 1; } else sb[i] = sb[i - 1]; if (c == cc[u]) { sc[i] = sc[i - 1] + 1; } else sc[i] = sc[i - 1]; u++; if (u > 2) u = 0; } for (int i = m; i <= n; i++) { u = max(sa[i] - sa[i - m], sb[i] - sb[i - m]); ans = max(ans, u); ans = max(ans, sc[i] - sc[i - m]); } cout << m - ans << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; const long long inf = 1e9 + 7; long long mod = 1e9 + 7; struct node { int l = 0, r = 0, fa = 0, v = 0; char op; bool tmp; } a[maxn]; int dfs1(int u) { int x, y; if (a[u].l != 0) { x = dfs1(a[u].l); } if (a[u].r != 0) { y = dfs1(a[u].r); } if (a[u].op == 'I') return a[u].v; if (a[u].op == 'A') return a[u].v = (x & y); else if (a[u].op == 'X') return a[u].v = (x ^ y); else if (a[u].op == 'O') return a[u].v = (x | y); else if (a[u].op == 'N') return a[u].v = (!x); } void dfs2(int u, bool t1, bool t2) { if (a[u].op == 'I') { if (a[u].v == 1) a[u].tmp = t1; else a[u].tmp = t2; } else if (a[u].op == 'N') { dfs2(a[u].l, t2, t1); } else { if (a[u].op == 'A') { if (a[a[u].l].v == 1) dfs2(a[u].r, t1, t2); else dfs2(a[u].r, t1, t1); if (a[a[u].r].v == 1) dfs2(a[u].l, t1, t2); else dfs2(a[u].l, t1, t1); } else if (a[u].op == 'O') { if (a[a[u].l].v == 1) dfs2(a[u].r, t2, t2); else dfs2(a[u].r, t1, t2); if (a[a[u].r].v == 1) dfs2(a[u].l, t2, t2); else dfs2(a[u].l, t1, t2); } else { if (a[a[u].l].v == 1) dfs2(a[u].r, t2, t1); else dfs2(a[u].r, t1, t2); if (a[a[u].r].v == 1) dfs2(a[u].l, t2, t1); else dfs2(a[u].l, t1, t2); } } } int main(void) { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { char s[10]; cin >> s; if (strcmp(s, "IN") == 0 || strcmp(s, "NOT") == 0) { if (strcmp(s, "IN") == 0) { scanf("%d", &a[i].v); a[i].op = 'I'; } else { scanf("%d", &a[i].l); a[i].op = 'N'; } } else { scanf("%d%d", &a[i].l, &a[i].r); if (strcmp(s, "AND") == 0) a[i].op = 'A'; else if (strcmp(s, "OR") == 0) a[i].op = 'O'; else if (strcmp(s, "XOR") == 0) a[i].op = 'X'; } } dfs1(1); dfs2(1, false, true); for (int i = 1; i <= n; i++) { if (a[i].op == 'I') { if (a[i].tmp) printf("1"); else printf("0"); } } printf("\n"); return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { long long n, t, i, c, cnt = 0, e = 0, j = 0; cin >> n >> t >> c; long long a[n + 5]; for (i = 1; i <= n; i++) { cin >> a[i]; ++j; if (j == c) { if (a[i] <= t) ++e; if (e == c) ++cnt; if (a[i - c + 1] <= t) --e; --j; } else { if (a[i] <= t) ++e; } } cout << cnt << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long int power(long long int x, long long int n) { if (n == 0) return 1; if (n % 2) return x * power(x * x, (n - 1) / 2); return power(x * x, n / 2); } long long int powerm(long long int x, long long int n) { if (n == 0) return 1; if (n % 2) return (x * powerm((x * x) % 1000000007, (n - 1) / 2)) % 1000000007; return powerm((x * x) % 1000000007, n / 2); } int32_t main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int N; cin >> N; int A[N]; for (int i = 0; i < N; i++) { cin >> A[i]; } sort(A, A + N); vector<int> ans; int c = 0; map<int, int> D; int d; int dist = 0; vector<int> ds; if (N == 1) { return cout << -1, 0; } if (N == 2) { d = A[1] - A[0]; if (d != 0) { ans.push_back(A[0] - d); ans.push_back(A[1] + d); if (d % 2 == 0) { ans.push_back(A[0] + (d / 2)); } } else { ans.push_back(A[0]); } cout << ans.size() << "\n"; sort(ans.begin(), ans.end()); for (int i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } return 0; } for (int i = 1; i < N; i++) { int x = A[i] - A[i - 1]; if (D[x] == 0) { dist++; ds.push_back(x); } D[x]++; if (dist > 2) { return cout << 0, 0; } } if (dist == 1) { if (ds[0] != 0) { ans.push_back(A[0] - ds[0]); ans.push_back(A[N - 1] + ds[0]); } else { ans.push_back(A[0]); } } else { if (D[ds[0]] == N - 2) { d = ds[0]; for (int i = 1; i < N; i++) { if (A[i] - A[i - 1] != d) { if (A[i] - d == A[i - 1] + d) { ans.push_back(A[i] - d); } } } } if (D[ds[1]] == N - 2) { d = ds[1]; for (int i = 1; i < N; i++) { if (A[i] - A[i - 1] != d) { if (A[i] - d == A[i - 1] + d) { ans.push_back(A[i] - d); } } } } else { } } cout << ans.size() << "\n"; sort(ans.begin(), ans.end()); for (int i = 0; i < ans.size(); i++) { cout << ans[i] << " "; } return 0; }
4
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (!(ch >= '0' && ch <= '9')) { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + (ch - '0'); ch = getchar(); } return x * f; } const int P[16] = {1, 0, 0, 0, 1, 0, 1, 0, 2, 1, 1, 2, 0, 1, 0, 0}; int main() { int n = read(); int ans = 0; do { ans += P[n % 16]; n /= 16; } while (n); printf("%d\n", ans); return 0; }
6
#include <bits/stdc++.h> using namespace std; using ull = uint64_t; using ll = int64_t; using PII = pair<int, int>; using VI = vector<int>; string to_string(string s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto& x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } int main() { ios::sync_with_stdio(false), cin.tie(0); int n; cin >> n; map<int, int> cnts; for (int _n = n * n, i = 0; i < _n; ++i) { int x; cin >> x; cnts[x]++; } vector<VI> A(n, VI(n)); vector<PII> vec; for (auto& p : cnts) { vec.push_back(p); } int idx = 0; if (n % 2) { bool center = false; for (int r = (0), _b = (n / 2 - 1); r <= _b; ++r) { for (int c = (0), _b = (n / 2 - 1); c <= _b; ++c) { while (idx < ((int)(vec).size()) && vec[idx].second < 4) idx++; if (idx >= ((int)(vec).size())) goto no; vec[idx].second -= 4; int k = vec[idx].first; A[r][c] = k; A[n - r - 1][c] = k; A[r][n - c - 1] = k; A[n - r - 1][n - c - 1] = k; } } for (auto& p : vec) { if (!center && p.second % 2 == 1) { A[n / 2][n / 2] = p.first; p.second--; center = true; } else if (center && p.second % 2 == 1) { goto no; } } idx = 0; for (int c = (0), _b = (n / 2 - 1); c <= _b; ++c) { while (idx < ((int)(vec).size()) && vec[idx].second < 2) idx++; if (idx >= ((int)(vec).size())) goto no; A[n / 2][c] = vec[idx].first; A[n / 2][n - c - 1] = vec[idx].first; vec[idx].second -= 2; } for (int r = (0), _b = (n / 2 - 1); r <= _b; ++r) { while (idx < ((int)(vec).size()) && vec[idx].second < 2) idx++; if (idx >= ((int)(vec).size())) goto no; A[r][n / 2] = vec[idx].first; A[n - r - 1][n / 2] = vec[idx].first; vec[idx].second -= 2; } } else { for (int r = (0), _b = (n / 2 - 1); r <= _b; ++r) { for (int c = (0), _b = (n / 2 - 1); c <= _b; ++c) { while (idx < ((int)(vec).size()) && vec[idx].second < 1) idx++; if (idx >= ((int)(vec).size())) goto no; if (vec[idx].second % 4 != 0) goto no; vec[idx].second -= 4; int k = vec[idx].first; A[r][c] = k; A[n - r - 1][c] = k; A[r][n - c - 1] = k; A[n - r - 1][n - c - 1] = k; } } } cout << "yes\n"; for (int _n = n, i = 0; i < _n; ++i) { for (int _n = n, j = 0; j < _n; ++j) { cout << A[i][j]; if (j < n - 1) cout << " "; } cout << "\n"; } return 0; no: cout << "no\n"; }
4
#include <bits/stdc++.h> using namespace std; long long n, m, s, p[100005]; char a[100005], b[100005], c[100005]; signed main() { scanf("%lld%lld%s%s", &n, &m, a + 1, b + 1); for (long long i = 1; i <= n; i++) if (a[i] != b[i]) p[++s] = i; if (m < (s + 1) / 2) return puts("-1"), 0; if (m >= s) { for (long long i = 1; i <= s; i++) for (char j = 'a'; j <= 'z'; j++) if (j != a[p[i]] && j != b[p[i]]) c[p[i]] = j; m -= s, s = 0; for (long long i = 1; i <= n; i++) { if (c[i] > 0) continue; s++; if (s <= m) { for (char j = 'a'; j <= 'z'; j++) if (j != a[i] && j != b[i]) c[i] = j; } else c[i] = a[i]; } } else { long long cnt = s - m; for (long long i = 1; i <= cnt * 2; i++) c[p[i]] = (i & 1 ? a[p[i]] : b[p[i]]); for (long long i = cnt * 2 + 1; i <= s; i++) for (char j = 'a'; j <= 'z'; j++) if (j != a[p[i]] && j != b[p[i]]) c[p[i]] = j; for (long long i = 1; i <= n; i++) if (!c[i]) c[i] = a[i]; } for (long long i = 1; i <= n; i++) cout << c[i]; return puts(""), 0; }
4
#include <bits/stdc++.h> using namespace std; int solve() { int n, x, count = 2; string s; cin >> s; cout << s[0]; for (int i = 1; i < s.size(); i++) { if (s[i] != s[i - 1] || count > 1) { cout << s[i]; count = 1; } else count++; } return (0); } int main() { ios::sync_with_stdio(0); cin.tie(0); ; int t = 1; cin >> t; while (t--) { int c = solve(); cout << endl; } }
0
#include <bits/stdc++.h> using namespace std; long long powmod(long long a, long long b, long long mod) { if (b == 0 || a == 1) { if (mod == 1) return 0; else return 1; } if (b % 2 == 0) { long long k = powmod(a, b / 2, mod); return (k * k) % mod; } else { long long k = powmod(a, b / 2, mod); return ((k * k) % mod * a) % mod; } } long long gcd(long long a, long long b) { if (a == 0) return b; if (b == 0) return a; if (a > b) return gcd(a % b, b); else return gcd(b % a, a); } int prime(int p) { for (int i = 2; i * i <= p; i++) { if (p % i == 0 && i < p) return i; } return 1; } long long sqr(long long i) { return i * i; } void r(long long &a) { cin >> a; } void r(long long &a, long long &b) { cin >> a >> b; } void r(long long &a, long long &b, long long &c) { cin >> a >> b >> c; } void r(long long &a, long long &b, long long &c, long long &d) { cin >> a >> b >> c >> d; } void r(long long &a, long long &b, long long &c, long long &d, long long &e) { cin >> a >> b >> c >> d >> e; } void r(long long &a, long long &b, long long &c, long long &d, long long &e, long long &f) { cin >> a >> b >> c >> d >> e >> f; } void r(vector<long long> &a) { for (long long i = 0; i < a.size(); i++) r(a[i]); } void r(vector<vector<long long>> &a) { for (long long i = 0; i < a.size(); i++) r(a[i]); } void w(long long a) { cout << a << "\n"; } void w(char a) { cout << a; } void w(long long a, long long b) { cout << a << " " << b << "\n"; } void w(long long a, long long b, long long c) { cout << a << " " << b << " " << c << "\n"; } void w(long long a, long long b, long long c, long long d) { cout << a << " " << b << " " << c << " " << d << "\n"; } void w(vector<long long> a) { for (long long i = 0; i < a.size(); i++) cout << a[i] << " "; cout << "\n"; } void w(vector<vector<long long>> a) { for (long long i = 0; i < a.size(); i++) w(a[i]); cout << "\n"; } void r(pair<long long, long long> &a) { cin >> a.first >> a.second; } void w(pair<long long, long long> a) { cout << a.first << " " << a.second << "\n"; } void r(vector<pair<long long, long long>> &a) { for (long long i = 0; i < a.size(); i++) r(a); } void w(vector<pair<long long, long long>> a) { for (long long i = 0; i < a.size(); i++) w(a[i]); cout << "\n"; } void r(string &a) { cin >> a; } void r(char &a) { cin >> a; } void w(string a) { cout << a << "\n"; } void sort(vector<long long> &a) { sort(a.begin(), a.end()); } void sort(vector<pair<long long, long long>> &a) { sort(a.begin(), a.end()); } void rev(vector<long long> &a) { reverse(a.begin(), a.end()); } void rev(vector<pair<long long, long long>> &a) { reverse(a.begin(), a.end()); } void rev(string &a) { reverse(a.begin(), a.end()); } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } void solve(int ppppppppp = 1) { long long a, b; r(a, b); cout << (a - b >= 2 ? "YES\n" : "NO\n"); return; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tututu; tututu = 1; cin >> tututu; for (long long qwerty = 0; qwerty < tututu; qwerty++) solve(); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 34; int n, a[N], mn = 1e9, sum, less0, x; int main() { cin >> n; for (int i = 0; i < n * 2 - 1; i++) { cin >> x; if (abs(x) < mn) mn = abs(x); sum += abs(x); if (x < 0) less0++; } if (less0 % 2 && n % 2 == 0) sum -= mn * 2; cout << sum; return 0; }
5
#include <bits/stdc++.h> using namespace std; vector<int> phnode[1000010]; int ph = 0; int node[1000010]; int num[1000010]; int low[1000010]; bool chk[1000010]; int cnt = 0; long long sccost[1000010]; vector<long long> cost[1000010]; vector<int> edge[1000010]; vector<long long> phcost[1000010]; vector<int> phedge[1000010]; vector<int> trace; void dfs(int u) { chk[u] = true; low[u] = ++cnt; num[u] = cnt; bool isroot = true; trace.push_back(u); for (int i = 0; i < edge[u].size(); i++) { int v = edge[u][i]; if (!chk[v]) dfs(v); if (low[u] > low[v]) { low[u] = low[v]; isroot = false; } } if (isroot) { ph++; while (true) { int x = trace.back(); trace.pop_back(); node[x] = ph; phnode[ph].push_back(x); low[x] = 0x7fffffff; if (x == u) break; } } } void scc(int n) { for (int i = 1; i <= n; i++) { if (chk[i]) continue; dfs(i); } } int degree[1000010]; int phvisit[1000010]; long long dp[1000010]; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < m; i++) { int x, y, z; scanf("%d %d %d", &x, &y, &z); edge[x].push_back(y); cost[x].push_back(z); } scc(n); for (int i = 1; i <= ph; i++) { long long sum = 0; for (auto &u : phnode[i]) { for (int j = 0; j < cost[u].size(); j++) { auto &gap = cost[u][j]; auto &nxt = edge[u][j]; if (node[nxt] != i) { phedge[i].push_back(node[nxt]); phcost[i].push_back(gap); continue; } double tmp = (sqrt((double)gap * 8 + 1) - 1) / 2; long long f = (long long)round(tmp); if (gap - (f + 1) * (f + 2) / 2 >= 0) f = f + 1; else if (gap - f * (f + 1) / 2 < 0) f = f - 1; sum += gap + gap * f - f * (f + 1) * (f + 2) / 6; } } sccost[i] = sum; } int start; scanf("%d", &start); queue<int> q; q.push(node[start]); phvisit[node[start]] = 1; while (q.size()) { int here = q.front(); q.pop(); for (int i = 0; i < phedge[here].size(); i++) { int nxt = phedge[here][i]; if (phvisit[nxt]) continue; phvisit[nxt] = 1; q.push(nxt); } } for (int i = 1; i <= ph; i++) { if (!phvisit[i]) continue; for (auto &v : phedge[i]) if (phvisit[v]) degree[v]++; } for (int i = 1; i <= ph; i++) { if (!phvisit[i]) continue; if (degree[i] == 0) q.push(i); } long long ans = 0; while (q.size()) { int here = q.front(); q.pop(); dp[here] += sccost[here]; if (ans < dp[here]) ans = dp[here]; for (int i = 0; i < phedge[here].size(); i++) { int nxt = phedge[here][i]; if (!phvisit[nxt]) continue; if (degree[nxt]-- == 1) q.push(nxt); if (dp[nxt] < dp[here] + phcost[here][i]) dp[nxt] = dp[here] + phcost[here][i]; } } printf("%lld\n", ans); return 0; }
6
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using PII = pair<int, int>; using VPII = vector<PII>; using PLL = pair<ll, ll>; using VPLL = vector<PII>; using VI = vector<int>; using VVI = vector<VI>; using Vll = vector<ll>; using VVll = vector<Vll>; using VC = vector<char>; using VS = vector<string>; using VB = vector<bool>; const int INF = 1e9; const ll MOD = 1e9 + 7; pair<PLL, PLL> intersect(pair<PLL, PLL> a, pair<PLL, PLL> b) { ll xl1 = a.first.first, yl1 = a.first.second; ll xr1 = a.second.first, yr1 = a.second.second; ll xl2 = b.first.first, yl2 = b.first.second; ll xr2 = b.second.first, yr2 = b.second.second; ll xl = max(xl1, xl2); ll yl = max(yl1, yl2); ll xr = min(xr1, xr2); ll yr = min(yr1, yr2); if (xl <= xr && yl <= yr) return {{xl, yl}, {xr, yr}}; else return {{0LL, 0LL}, {0LL, 0LL}}; } ll square(pair<PLL, PLL> c) { PLL a = c.first, b = c.second; return (b.first - a.first) * (b.second - a.second); } int main() { ll x1, x2, x3, x4, x5, x6; ll y1, y2, y3, y4, y5, y6; cin >> x1 >> y1 >> x2 >> y2; cin >> x3 >> y3 >> x4 >> y4; cin >> x5 >> y5 >> x6 >> y6; ll sqw = square({{x1, y1}, {x2, y2}}); pair<PLL, PLL> a, b; a = intersect({{x1, y1}, {x2, y2}}, {{x3, y3}, {x4, y4}}); b = intersect({{x1, y1}, {x2, y2}}, {{x5, y5}, {x6, y6}}); ll sqwb1 = square(a); ll sqwb2 = square(b); ll inter = square(intersect(a, b)); if (sqw > sqwb1 + sqwb2 - inter) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 10000 + 5; int main() { int n, m; int ant = 0; set<int> s; scanf("%d %d", &n, &m); int c, a; for (int i = 0; i < m; i++) { s.clear(); int key = 0; scanf("%d", &c); for (int j = 1; j <= c; j++) { scanf("%d", &a); if (key == 0 && ant == 0) { if (s.count(-1 * a) == 1) key = 1; s.insert(a); } } if (key == 0) ant = 1; } if (ant == 1) cout << "YES" << endl; else cout << "NO" << endl; }
2
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); struct pt { double x, y; pt(){}; pt(double _x, double _y) : x(_x), y(_y){}; pt operator+(pt const& p) { return pt(x + p.x, y + p.y); } pt operator-(pt const& p) { return pt(x - p.x, y - p.y); } pt operator*(pt const& p) { return pt(x * p.x, y * p.y); } pt operator/(pt const& p) { return pt(x / p.x, y / p.y); } pt operator~() { return pt(y, x); } friend pt operator*(double val, pt const& p) { return pt(val * p.x, val * p.y); } double dot(pt const& p) { return x * p.x + y * p.y; } }; void findCircle(pt p1, pt p2, pt p3, pt& center, double& radius) { pt vt12 = p1 - p2; pt vt13 = p1 - p3; double d31 = p3.dot(p3) - p1.dot(p1); double d12 = p1.dot(p1) - p2.dot(p2); center = 0.5 * ~(d31 * vt12 + d12 * vt13) / (~vt13 * vt12 - ~vt12 * vt13); radius = hypot(center.x - p1.x, center.y - p1.y); } double alpha(pt vt1, pt vt2) { double norm1 = hypot(vt1.x, vt1.y); double norm2 = hypot(vt2.x, vt2.y); return acos(vt1.dot(vt2) / (norm1 * norm2)); } bool isEqual(double n1, double n2) { return abs(n1 - n2) / n2 < 1e-6; } bool isDivisible(double ang1, double ang2, double div) { double ang3 = 2 * PI - ang1 - ang2; double aux = ang1 / div; if (abs(round(aux) - aux) / aux > 1e-6) return false; aux = ang2 / div; if (abs(round(aux) - aux) / aux > 1e-6) return false; aux = ang3 / div; if (abs(round(aux) - aux) / aux > 1e-6) return false; return true; } int main() { pt p1, p2, p3, cent; int i, sides; double ang1, ang2, ang3, alp, area, radius; cin >> p1.x >> p1.y; cin >> p2.x >> p2.y; cin >> p3.x >> p3.y; findCircle(p1, p2, p3, cent, radius); ang1 = alpha(p1 - cent, p2 - cent); ang2 = alpha(p3 - cent, p1 - cent); ang3 = alpha(p2 - cent, p3 - cent); if (ang1 > ang2) swap(ang1, ang2); if (ang2 > ang3) swap(ang2, ang3); for (i = 3; i <= 100; i++) { alp = PI - (i - 2) * PI / i; if (isDivisible(ang1, ang2, alp)) { sides = i; break; } } alp = (sides - 2) * PI / sides; area = radius * radius * sin(alp) * sides / 2; cout << setprecision(8) << fixed << area << endl; }
6
#include <bits/stdc++.h> using namespace std; int main() { char arr[4][4]; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { cin >> arr[i][j]; } } bool found = false; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { int w = 0, b = 0; for (int k = i; k < i + 2; k++) { for (int h = j; h < j + 2; h++) { if (arr[k][h] == '.') { w++; } else if (arr[k][h] == '#') { b++; } } } if (w >= 3 || b >= 3) { cout << "YES" << endl; found = true; break; } } if (found) { break; } } if (!found) { cout << "NO" << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int s[20]; bool r[20]; vector<pair<int, int> > ans; void Hanoi(int a, int b, int n, bool last) { --n; if (n < 0) return; int c = 6 - a - b; if (last && s[n] > 1 && !r[n]) { if (n == 0) { Hanoi(a, b, n, false); for (int i = 1; i < s[n]; ++i) ans.push_back(make_pair(a, c)); Hanoi(b, c, n, false); ans.push_back(make_pair(a, b)); Hanoi(c, a, n, false); for (int i = 1; i < s[n]; ++i) ans.push_back(make_pair(c, b)); Hanoi(a, b, n, last); } else { Hanoi(a, b, n, false); for (int i = 0; i < s[n]; ++i) ans.push_back(make_pair(a, c)); r[n] ^= 1; Hanoi(b, a, n, false); for (int i = 0; i < s[n]; ++i) ans.push_back(make_pair(c, b)); r[n] ^= 1; Hanoi(a, b, n, last); } } else { Hanoi(a, c, n, false); for (int i = 0; i < s[n]; ++i) ans.push_back(make_pair(a, b)); r[n] ^= 1; Hanoi(c, b, n, last); } } int main() { int n, t; map<int, int> mp; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &t), ++mp[t]; n = 0; for (map<int, int>::const_iterator it = mp.begin(); it != mp.end(); ++it) s[n] = it->second, r[n] = false, ++n; Hanoi(1, 3, n, true); printf("%d\n", (int)ans.size()); for (int i = 0; i < ans.size(); ++i) printf("%d %d\n", ans[i].first, ans[i].second); return 0; }
8
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7, INF = 1e9 + 7; int n, m, ans[N], mx[N << 2]; map<int, int> R; vector<int> V; struct Node { int l, r, t, id; void in() { scanf("%d%d%d", &l, &r, &t); } } a[N], b[N]; bool cmp(Node a, Node b) { return a.l < b.l || a.l == b.l && a.id < b.id; } void upd(int t, int l, int r, int p, int v) { if (l == r) { mx[t] = v; return; } int mid = (l + r) >> 1; if (p <= mid) upd(((t) << 1), l, mid, p, v); else upd(((t) << 1 | 1), mid + 1, r, p, v); mx[t] = max(mx[((t) << 1)], mx[((t) << 1 | 1)]); } int qry(int t, int l, int r, int L, int R) { if (L <= l && r <= R) return mx[t]; int mid = (l + r) >> 1; if (R <= mid) return qry(((t) << 1), l, mid, L, R); else if (L > mid) return qry(((t) << 1 | 1), mid + 1, r, L, R); return max(qry(((t) << 1), l, mid, L, R), qry(((t) << 1 | 1), mid + 1, r, L, R)); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) a[i].in(), a[i].id = i, V.push_back(a[i].t), R[a[i].t] = i; for (int i = 0; i < m; ++i) b[i].in(), b[i].id = i; sort(a, a + n, cmp); sort(b, b + m, cmp); sort(V.begin(), V.end()); V.erase(unique(V.begin(), V.end()), V.end()); int i = 0; for (int j = 0; j < m; ++j) { while (i < n && a[i].l <= b[j].l) { int p = lower_bound(V.begin(), V.end(), a[i].t) - V.begin() + 1; upd(1, 1, V.size(), p, a[i].r); ++i; } int p = lower_bound(V.begin(), V.end(), b[j].t) - V.begin() + 1; if (p > V.size()) { ans[b[j].id] = -1; continue; } int l = p, r = V.size(); while (l + 1 < r) { int mid = (l + r) >> 1; qry(1, 1, V.size(), p, mid) >= b[j].r ? r = mid : l = mid; } if (qry(1, 1, V.size(), p, l) >= b[j].r) ans[b[j].id] = R[V[l - 1]] + 1; else if (qry(1, 1, V.size(), p, r) >= b[j].r) ans[b[j].id] = R[V[r - 1]] + 1; else ans[b[j].id] = -1; } for (int i = 0; i < m; ++i) printf("%d ", ans[i]); return 0; }
8
#include <bits/stdc++.h> using namespace std; vector<int> vis(2e5 + 10, 0), dis(2e5 + 10, 0), par(2e5 + 10, -1); int dp[5001][5001]; int dp2[5001][5001]; class graph { private: int v; list<pair<int, int> >* adj; list<pair<int, int> >* radj; public: graph(int x) { v = x + 1; adj = new list<pair<int, int> >[v]; radj = new list<pair<int, int> >[v]; } void adedge(int a, int b, int c) { adj[a].push_back(make_pair(b, c)); radj[b].push_back(make_pair(a, c)); } void dfs(int s); void bfs(int s); }; vector<int> temp; void graph::dfs(int s) { vis[s] = 1; list<pair<int, int> >::iterator it; int i, j; for (it = radj[s].begin(); it != radj[s].end(); it++) { int cst = (*it).second; int v = (*it).first; if (!vis[v]) { vis[v] = 1; dfs(v); } } for (j = 0; j <= 5000; j++) { dp[s][j] = INT_MAX; } if (s == 1) { dp2[s][1] = 1; dp[s][1] = 0; } for (it = radj[s].begin(); it != radj[s].end(); it++) { int v = (*it).first; int cst = (*it).second; for (j = 1; j <= 5000 && vis[v] == 1; j++) { if (j + 1 <= 5000) { if (dp2[v][j] != 0 && (dp[v][j] + cst) < dp[s][j + 1] && (dp[v][j] + cst) <= 1e9) { dp2[s][j + 1] = v; dp[s][j + 1] = min(dp[s][j + 1], dp[v][j] + cst); } } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); memset(dp, 0, sizeof dp); memset(dp2, 0, sizeof dp2); int n, i, j, k, m, t; cin >> n >> m >> t; graph g(n); while (m--) { int a, b, c; cin >> a >> b >> c; g.adedge(a, b, c); } for (i = 1; i <= n; i++) { if (!vis[i]) { vis[i] = 1; g.dfs(i); } } int ans = 0; for (j = 1; j <= 5000; j++) { if (dp[n][j] <= t) ans = j; } int fl = n; int ori = ans; while (fl != 1 && fl != 0) { temp.push_back(fl); fl = dp2[fl][ans]; ans--; } if (ori != 0) temp.push_back(1); reverse(temp.begin(), temp.end()); cout << (int)temp.size() << "\n"; for (i = 0; i < temp.size(); i++) cout << temp[i] << " "; cout << "\n"; return 0; }
5
#include <bits/stdc++.h> using namespace std; int k, n, a[222222], old[222222]; int main() { scanf("%d%d", &n, &k); for (int i = 0; i <= n; ++i) { int first; scanf("%d", &a[i]); old[i] = a[i]; } for (int i = 0; i < n; ++i) { int add = abs(a[i]) / 2; if (a[i] < 0) add *= -1; a[i + 1] += add; a[i] -= 2 * add; } int minA = 0; while (a[minA] == 0) ++minA; int maxA = n; while (a[maxA] == 0) --maxA; int ans = 0; long long need = 0; for (int i = maxA; i >= 0 && i >= minA - 50; --i) { need = need * 2 + a[i]; if (abs(need) > 1LL * 1000000000 * 1000000) { break; } long long newval = old[i] - need; if (newval == 0 && i == n) continue; if (i <= minA && newval >= -k && newval <= k) { ++ans; } } cout << ans << endl; }
7