solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < (int)(n); ++i) cin >> a[i]; int x = a[n - 1]; sort((a).begin(), (a).end()); cout << (x ^ (a[n - 1])) << endl; return 0; }
10
#include <bits/stdc++.h> using namespace std; void solve() { long long n, x, y; cin >> n >> x >> y; long long a[n]; for (long long i = 0; i < n; i++) cin >> a[i]; if (x > y) cout << n << "\n"; else { long long ans = 0; vector<long long> v; for (long long i = 0; i < n; i++) { if (a[i] <= x) v.push_back(a[i]); } if (v.empty()) cout << 0 << "\n"; else { ans = ceil(v.size() / 2.0); cout << ans << "\n"; } } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t = 1; while (t--) { solve(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long INF = 2e12; const int MAXN = 2.5e5; const int MAXM = 2.5e5; int N, M; int par[MAXN]; long long V[MAXN]; int sz[MAXN]; vector<int> comp[MAXN]; long long cmin[MAXN]; long long cmax[MAXN]; int main() { ios_base::sync_with_stdio(0); cin >> N >> M; for (int i = 0; i < N; i++) { par[i] = i; sz[i] = 1; comp[i].push_back(i); cmin[i] = cmax[i] = V[i] = 0; } cmin[0] = INF; cmax[N - 1] = -INF; long long range = 0; for (int i = 0; i < M; i++) { int f, t, w, b; cin >> f >> t >> w >> b; f--, t--; long long v = w * b; v += V[f]; f = par[f]; v -= V[t]; t = par[t]; if (f == t) { if (v != 0) { cout << "BAD " << (i + 1) << '\n'; return 0; } else { continue; } } if (sz[t] > sz[f]) { swap(f, t); v *= -1; } for (int i : comp[t]) { V[i] += v; par[i] = f; comp[f].push_back(i); sz[f]++; } comp[t].clear(); cmin[f] = min(cmin[t] + v, cmin[f]); cmax[f] = max(cmax[t] + v, cmax[f]); if (cmin[par[0]] <= V[0]) { cerr << "past start\n"; cout << "BAD " << (i + 1) << '\n'; return 0; } if (cmax[par[N - 1]] >= V[N - 1]) { cerr << "past end\n"; cout << "BAD " << (i + 1) << '\n'; return 0; } range = max(range, cmax[f] - cmin[f]); if (par[0] == par[N - 1] && range >= V[N - 1] - V[0]) { cerr << "past range\n"; cout << "BAD " << (i + 1) << '\n'; return 0; } } if (par[0] == par[N - 1]) { cout << V[N - 1] - V[0] << '\n'; } else { cout << "UNKNOWN" << '\n'; } }
22
#include <bits/stdc++.h> using namespace std; const int ROOT = 1; const int LEAF = (1 << 18); const int S = LEAF * 2; const int T = 10; vector<int> suf[S]; int n; vector<int> query_res; vector<int> query_cur; vector<int> query_new; void st_reset() { for (int i = 0; i < S; i++) { suf[i].clear(); if (i >= LEAF) suf[i].push_back(0); } } void meld() { query_new.clear(); int pr = 0; int pc = 0; while (query_res.size() > pr || query_cur.size() > pc) { if (query_cur.size() == pc) { if (query_res[pr] > 0) query_new.push_back(query_res[pr]); pr++; continue; } if (query_res.size() == pr) { if (query_cur[pc] > 0) query_new.push_back(query_cur[pc]); pc++; continue; } if (query_res[pr] < query_cur[pc]) { if (query_res[pr] > 0) query_new.push_back(query_res[pr]); pr++; } else { if (query_cur[pc] > 0) query_new.push_back(query_cur[pc]); pc++; } } query_res.clear(); for (int i = 0; i < min(T, (int)query_new.size()); i++) { query_res.push_back(query_new[i]); } } void st_rquery(int l, int r, int p, int pl, int pr) { if (pr < l) return; if (r < pl) return; if (l <= pl && pr <= r) { query_cur.clear(); for (auto e : suf[p]) query_cur.push_back(e); meld(); return; } if (p >= LEAF) return; int mid = (pl + pr) / 2; st_rquery(l, r, p * 2 + 0, pl, mid); st_rquery(l, r, p * 2 + 1, mid, pr); } int st_query(int l, int r) { query_res.clear(); st_rquery(l, r, ROOT, 0, LEAF); int ans = INT_MAX; for (int i = 0; i < query_res.size(); i++) { for (int j = i + 1; j < query_res.size(); j++) { int vi = query_res[i]; int vj = query_res[j]; int vs = vi + vj; bool con = false; while (vs > 0) { int di = vi % 10; vi /= 10; int dj = vj % 10; vj /= 10; int ds = vs % 10; vs /= 10; if (ds != di && dj != ds) { con = true; break; } } if (con) ans = min(ans, query_res[i] + query_res[j]); } } if (ans == INT_MAX) return -1; return ans; } void st_update(int p) { int lc = p * 2 + 0; int rc = p * 2 + 1; query_res.clear(); query_cur.clear(); for (auto e : suf[lc]) query_res.push_back(e); for (auto e : suf[rc]) query_cur.push_back(e); meld(); suf[p].clear(); for (auto e : query_res) suf[p].push_back(e); } void st_set(int idx, int value) { int p = idx + LEAF; suf[p].clear(); suf[p].push_back(value); for (p = p / 2; p >= ROOT; p /= 2) { st_update(p); } } void solve() { st_reset(); int q; cin >> n >> q; for (int i = 0; i < 0 + n; i++) { int v; cin >> v; st_set(i, v); } while (q--) { int t; cin >> t; if (t == 1) { int i, v; cin >> i >> v; i--; st_set(i, v); } else { int l, r; cin >> l >> r; l--; cout << "" << st_query(l, r) << "\n"; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); }
15
#include <bits/stdc++.h> using namespace std; int main() { int k; cin >> k; if (k & 1) { puts("-1"); return 0; } for (int i = 1; i <= k; ++i) { for (int j = 1; j <= k; ++j) { for (int x = 1; x <= k; ++x) { if (i & 1) { if ((j % 4 == 1 || j % 4 == 2) && (x % 4 == 1 || x % 4 == 2) || (j % 4 == 3 || j % 4 == 0) && (x % 4 == 3 || x % 4 == 0)) printf("b"); else printf("w"); } else { if ((j % 4 == 1 || j % 4 == 2) && (x % 4 == 1 || x % 4 == 2) || (j % 4 == 3 || j % 4 == 0) && (x % 4 == 3 || x % 4 == 0)) printf("w"); else printf("b"); } } puts(""); } puts(""); } return 0; }
8
#include <bits/stdc++.h> using namespace std; int score(int p, int t) { return (1000 - p) * 1000 + t; } int main(int argc, const char* argv[]) { int n, k; cin >> n >> k; --k; map<int, int> ts; for (int i = 0; i < n; ++i) { int p, t; cin >> p >> t; ts[score(p, t)] += 1; } int res = 0; for (auto p : ts) { if (p.second > k) { res = p.second; break; } k -= p.second; } cout << res; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int A[n]; for (int i = 0; i < n; i++) cin >> A[i]; int a = 0; vector<string> str(n + 1, ""); if (A[0] == 0) { char c = 97 + a; str[0] += c; a = (a + 1) % 26; } for (int i = 0; i < A[0]; i++) { char c = 97 + a; str[0] += c; a = (a + 1) % 26; } for (int i = 0; i < n; i++) { if (str[i].length() <= A[i]) { str[i + 1] = str[i]; int temp = A[i] - str[i].length(); while (temp--) { char c = 97 + a; str[i + 1] += c; a = (a + 1) % 26; } str[i] = str[i + 1]; } else { str[i + 1] = str[i].substr(0, A[i]); if (A[i] == 0) { char c = 97 + a; str[i + 1] += c; a = (a + 1) % 26; } } } for (int i = 0; i < n + 1; i++) cout << str[i] << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long n = 0; cin >> n; map<long long, string> mapc; string m; for (int i = 0; i < n; i++) { cin >> m; mapc[i] = m; } long long res = 6; for (auto& mm : mapc) { for (auto& ms : mapc) { if (ms != mm) { long long tres = 0; for (int i = 0; i < 6; i++) if (mm.second[i] != ms.second[i]) tres++; res = min(res, (tres - 1) / 2); } } } cout << res; return 0; }
6
#include <bits/stdc++.h> using namespace std; const int maxn = 5 * 1000 * 100 + 10; long long c[maxn]; int par[maxn]; long long size[maxn]; bool mark[maxn]; vector<int> e[maxn]; int get_par(int v) { return (v == par[v] ? v : par[v] = get_par(par[v])); } void merge(int v, int u) { v = get_par(v); u = get_par(u); if (size[v] > size[u]) swap(v, u); par[v] = u; size[u] += size[v]; size[v] = 0; } int main() { int fi, se, m; long long n; cin >> n >> m; for (int i = 1; i <= n; i++) par[i] = i, size[i] = 1, cin >> c[i]; for (int i = 0; i < m; i++) cin >> fi >> se, e[fi].push_back(se), e[se].push_back(fi); vector<pair<long long, int> > q(0); for (int i = 1; i <= n; i++) q.push_back(make_pair(c[i], i)); sort(q.begin(), q.end()); long double ans = 0; for (int i = q.size() - 1; i >= 0; i--) { int cur = q[i].second; mark[cur] = true; for (int j = 0; j < e[cur].size(); j++) if (mark[e[cur][j]] and get_par(cur) != get_par(e[cur][j])) ans += size[get_par(cur)] * size[get_par(e[cur][j])] * q[i].first, merge(get_par(cur), get_par(e[cur][j])); } cout << fixed << setprecision(6) << 2. * ans / (long double)(n * n - n) << endl; return 0; }
11
#include <bits/stdc++.h> using namespace std; int c[120], p[120]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.precision(16); ; int t; cin >> t; while (t--) { int n, flag = 0; cin >> n; for (int i = 0; i < n; i++) cin >> p[i] >> c[i]; for (int i = 0; i < n; i++) if (c[i] > p[i]) { flag = 1; break; } for (int i = 1; i < n; i++) { if (c[i] - c[i - 1] > p[i] - p[i - 1] || p[i] < p[i - 1] || c[i] < c[i - 1]) { flag = 1; break; } } if (flag) cout << "NO" << endl; else cout << "YES" << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { string s; int a; cin >> s; int count1 = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == 'a') count1++; } if (count1 > s.size() / 2) { cout << s.size(); } else { cout << (count1 * 2 - 1); } }
0
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, sophepthu; cin >> sophepthu; while (sophepthu--) { cin >> n >> m; if (n == 1) cout << 0 << endl; else if (n == 2) cout << m << endl; else cout << m * 2 << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int c[1000007], n, m, ans[300005], maxi; struct node { int l, r, ind; } a[1000007]; int lowbit(int x) { return x & -x; } void modify(int x) { while (x <= maxi) { c[x]++; x += lowbit(x); } } int getsum(int x) { int ans = 0; while (x > 0) { ans += c[x]; x -= lowbit(x); } return ans; } int cmp(node ka, node kb) { if (ka.l == kb.l) { if (ka.r == kb.r) return ka.ind < kb.ind; return ka.r < kb.r; } return ka.l > kb.l; } int main() { int i, j, k, x, pre, cnt; maxi = 1000007 - 5; while (scanf("%d%d", &n, &m) != EOF) { for (i = 1; i <= n; i++) scanf("%d%d", &a[i].l, &a[i].r), a[i].ind = 0; memset(ans, 0, sizeof(ans)); memset(c, 0, sizeof(c)); int tot = n; for (i = 1; i <= m; i++) { scanf("%d", &cnt); scanf("%d", &x); if (x > 1) a[++tot].l = 1, a[tot].r = x - 1, a[tot].ind = i; pre = x; for (j = 1; j < cnt; j++) { scanf("%d", &x); if (x - 1 >= pre + 1) a[++tot].l = pre + 1, a[tot].r = x - 1, a[tot].ind = i; pre = x; } a[++tot].l = pre + 1, a[tot].r = maxi, a[tot].ind = i; } sort(a + 1, a + tot + 1, cmp); for (i = 1; i <= tot; i++) { if (a[i].ind > 0) ans[a[i].ind] += getsum(a[i].r); else modify(a[i].r); } for (i = 1; i <= m; i++) printf("%d\n", n - ans[i]); } return 0; }
14
#include <bits/stdc++.h> int main() { char a[15]; scanf("%s", a); int q, c = 0; int b = strlen(a); for (int i = b - 1; i >= 0; i--) { if (a[i] == '0') { b = b - 1; } else break; } q = b; if (b % 2 == 0) { for (int j = 0; j < b / 2; j++) { if (a[j] != a[q - 1]) { c = 1; break; } else q = q - 1; } } if (b % 2 != 0) { for (int j = 0; j < b / 2; j++) { if (a[j] != a[q - 1]) { c = 1; break; } else q = q - 1; } } if (c == 0) { printf("YES"); } else printf("NO"); }
1
#include <bits/stdc++.h> using namespace std; const int b = 230; const int mod = 998244353; long long pow2(long long a, long long b) { long long ans = 1; while (b) { if (b % 2 == 1) ans = ans * a % mod; a = a * a % mod; b /= 2; } return ans; } vector<int> g[150005]; int par[18][150005]; int depth[150005]; long long sum[150005]; int sz[150005]; int l[150005], r[150005]; int tim = 0; void dfs(int u, int p, int d) { sz[u] = 1; par[0][u] = p; depth[u] = d; l[u] = ++tim; for (int i = 0; i < (int)(g[u]).size(); i++) { int v = g[u][i]; if (v != p) { dfs(v, u, d + 1); sz[u] += sz[v]; } } r[u] = tim; } long long bit[150005]; void add(int idx, long long val) { for (++idx; idx <= 150000; idx += idx & -idx) { bit[idx] = (bit[idx] + val) % mod; if (bit[idx] >= mod) bit[idx] -= mod; } } void range_add(int l, int r, long long val) { add(l, val); add(r + 1, (-val + mod) % mod); } long long point_query(int idx) { long long ret = 0; for (++idx; idx > 0; idx -= idx & -idx) { ret = (ret + bit[idx]) % mod; if (ret >= mod) ret -= mod; } return ret; } int jump(int u, int d) { for (int i = 17; i >= 0; i--) { if ((d >> i) & 1) u = par[i][u]; } return u; } int main() { int n, q; scanf("%d%d", &n, &q); for (int i = 1; i <= n - 1; i++) { int a, b; scanf("%d%d", &a, &b); g[a].emplace_back(b); g[b].emplace_back(a); } vector<int> heavy; for (int i = 1; i <= n; i++) { if ((int)(g[i]).size() > b) heavy.emplace_back(i); } long long inv = pow2(n, mod - 2); dfs(1, 0, 0); for (int j = 1; j < 18; j++) { for (int i = 1; i <= n; i++) { if (par[j - 1][i] != 0) par[j][i] = par[j - 1][par[j - 1][i]]; } } for (int i = 1; i <= q; i++) { int op; scanf("%d", &op); if (op == 1) { int u, d; scanf("%d%d", &u, &d); if ((int)(g[u]).size() <= b) { range_add(l[u], l[u], d); for (int i = 0; i < (int)(g[u]).size(); i++) { int v = g[u][i]; if (sz[u] > sz[v]) { long long sub = n - sz[v]; long long p = sub * 1ll * inv % mod; range_add(l[v], r[v], p * 1ll * d % mod); } else { long long sub = sz[u]; long long p = sub * 1ll * inv % mod; if (1 <= l[u] - 1) range_add(1, l[u] - 1, p * 1ll * d % mod); if (r[u] + 1 <= n) range_add(r[u] + 1, n, p * 1ll * d % mod); } } } else { sum[u] = (sum[u] + d); if (sum[u] >= mod) sum[u] -= mod; } } else { int u; scanf("%d", &u); long long ans = point_query(l[u]); for (int i = 0; i < (int)(heavy).size(); i++) { int v = heavy[i]; if (l[v] <= l[u] && r[u] <= r[v]) { if (u == v) ans = (ans + sum[v]) % mod; else { int u2 = jump(u, depth[u] - depth[v] - 1); long long sub = n - sz[u2]; long long p = sub * 1ll * inv % mod; ans = (ans + p * 1ll * sum[v] % mod); if (ans >= mod) ans -= mod; } } else { long long sub = sz[v]; long long p = sub * 1ll * inv % mod; ans = (ans + p * 1ll * sum[v] % mod); if (ans >= mod) ans -= mod; } } printf("%lld\n", ans); } } }
19
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t != 0) { string s; int n; cin >> n >> s; int no_of_forward = 0; int no_of_reverse = 0; int result = 0; for (int i = 0; i < n; i++) { if (s[i] == '(') { no_of_forward++; } else { no_of_reverse++; } if (no_of_reverse > no_of_forward) result = max(no_of_reverse - no_of_forward, result); } cout << result << endl; t--; } return 0; }
2
#include <bits/stdc++.h> using namespace std; template <class T1> void deb(T1 e1) { cout << e1 << endl; } template <class T1, class T2> void deb(T1 e1, T2 e2) { cout << e1 << " " << e2 << endl; } template <class T1, class T2, class T3> void deb(T1 e1, T2 e2, T3 e3) { cout << e1 << " " << e2 << " " << e3 << endl; } template <class T1, class T2, class T3, class T4> void deb(T1 e1, T2 e2, T3 e3, T4 e4) { cout << e1 << " " << e2 << " " << e3 << " " << e4 << endl; } template <class T1, class T2, class T3, class T4, class T5> void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5) { cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << endl; } template <class T1, class T2, class T3, class T4, class T5, class T6> void deb(T1 e1, T2 e2, T3 e3, T4 e4, T5 e5, T6 e6) { cout << e1 << " " << e2 << " " << e3 << " " << e4 << " " << e5 << " " << e6 << endl; } const long long int mod = 1000000007ll; vector<int> alive[300005]; vector<int> adj[300005]; vector<int> radj[300005]; int col[300005]; int val[300005]; int deg[300005]; bool possible(int n, int k) { queue<int> qu; for (int i = 1; i <= n; i++) { deg[i] = adj[i].size(); if (deg[i] == 0) { qu.push(i); if (col[i] == 0) val[i] = 1; else val[i] = col[i]; } } while (!qu.empty()) { int u = qu.front(); qu.pop(); if (col[u] != 0) { if (val[u] > col[u]) return false; val[u] = col[u]; } for (int i = 0; i < radj[u].size(); i++) { int v = radj[u][i]; deg[v]--; val[v] = max(val[v], val[u] + 1); if (deg[v] == 0) { qu.push(v); } } } for (int i = 1; i <= n; i++) { if (val[i] == 0 || val[i] > k) return false; } return true; } int cnt[300005]; bool solve(int n, int k) { priority_queue<pair<int, int> > qu; for (int i = 1; i <= n; i++) { deg[i] = radj[i].size(); if (deg[i] == 0) { if (col[i] == 0) qu.push(make_pair(val[i], i)); else alive[col[i]].push_back(i); } cnt[val[i]]++; } int make = k; while (make > 0) { while (make > 0 && cnt[make] != 0) { for (int i = 0; i < alive[make].size(); i++) qu.push(make_pair(val[alive[make][i]], alive[make][i])); make--; } if (qu.empty()) return false; int u = qu.top().second; qu.pop(); for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i]; deg[v]--; if (deg[v] == 0) { if (col[v] == 0 || col[v] >= make) qu.push(make_pair(val[v], v)); else { alive[col[v]].push_back(v); } } } if (make == 0) continue; if (val[u] >= make || col[u] != 0) continue; cnt[val[u]]--; val[u] = make; cnt[val[u]]++; } memset(deg, 0, sizeof deg); for (int i = 1; i <= n; i++) deg[val[i]]++; for (int i = 1; i <= k; i++) if (deg[i] == 0) return false; return true; } int main() { int n, m, k, u, v; scanf("%d %d %d", &n, &m, &k); for (int i = 1; i <= n; i++) { scanf("%d", &col[i]); } for (int i = 1; i <= m; i++) { scanf("%d %d", &u, &v); adj[u].push_back(v); radj[v].push_back(u); } if (!possible(n, k)) { puts("-1"); return 0; } if (!solve(n, k)) { puts("-1"); return 0; } for (int i = 1; i <= n; i++) printf("%d ", val[i]); puts(""); return 0; }
18
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 15; bool v[N]; int p[N], tot, s, t, H[N]; vector<int> adj[N]; vector<pair<int, int> > ret; int find(int x) { return x == p[x] ? x : p[x] = find(p[x]); } void merge(int a, int b) { a = find(a), b = find(b); if (a > b) swap(a, b); p[b] = a; } void dfs(int x) { v[x] = 1; ++tot; int i, u; for (i = 0; i < adj[x].size(); ++i) { u = adj[x][i]; if (!v[u] && u != s && u != t) { merge(u, x); ret.push_back(make_pair(u, x)); dfs(u); } } } int main() { int x, y, n, m, i, j, ds, dt, cs = 0, ct = 0, a; scanf("%d%d", &n, &m); for (i = 0; i < m; ++i) { scanf("%d%d", &x, &y); adj[x].push_back(y); adj[y].push_back(x); } dfs(1); if (tot != n) { printf("No\n"); return 0; } ret.clear(); scanf("%d%d%d%d", &s, &t, &ds, &dt); memset(v, 0, sizeof(v)); for (i = 1; i <= n; ++i) p[i] = i; for (i = 1; i <= n; ++i) if (!v[i] && i != s && i != t) dfs(i); bool dd = 0, st = 0; for (i = 0; i < adj[s].size(); ++i) { x = adj[s][i]; y = find(x); if (x == t) { st = 1; continue; } H[y] |= 1; } for (i = 0; i < adj[t].size(); ++i) { x = adj[t][i], y = find(x); if (x == s) continue; H[y] |= 2; if (H[y] == 3) dd = 1; } for (i = 0; i < adj[s].size(); ++i) { x = adj[s][i], y = find(x); if (H[y] == 1) H[y] = 0, ret.push_back(make_pair(s, x)), ++cs; } for (i = 0; i < adj[t].size(); ++i) { x = adj[t][i], y = find(x); if (H[y] == 2) H[y] = 0, ret.push_back(make_pair(t, x)), ++ct; } if (dd) { for (i = 0; i < adj[s].size(); ++i) { x = adj[s][i], y = find(x); if (H[y] == 3) { H[y] = 0, ret.push_back(make_pair(s, x)), ++cs, ++ct, a = y; break; } } for (i = 0; i < adj[s].size() && cs < ds; ++i) { x = adj[s][i], y = find(x); if (H[y] == 3) H[y] = 0, ret.push_back(make_pair(s, x)), ++cs; } for (i = 0; i < adj[t].size(); ++i) { x = adj[t][i], y = find(x); if (H[y] == 3) H[y] = 0, ret.push_back(make_pair(t, x)), ++ct; if (y == a) ret.push_back(make_pair(t, x)); } } else ++cs, ++ct, ret.push_back(make_pair(s, t)); if (cs <= ds && ct <= dt) { printf("Yes\n"); for (i = 0; i < n - 1; ++i) printf("%d %d\n", ret[i].first, ret[i].second); } else printf("No\n"); }
15
#include <bits/stdc++.h> using namespace std; const int M = 1000000007; int main() { long long int n, m, k; long long int te; cin >> n >> m >> k; for (int i = 0; i < 4 * k; ++i) cin >> te; string ans = ""; for (int i = 0; i < n - 1; ++i) ans += 'U'; for (int i = 0; i < m - 1; ++i) ans += 'L'; for (int i = 1; i <= n; ++i) { if (i & 1) { for (int i = 0; i < m - 1; ++i) ans += "R"; } else { for (int i = 0; i < m - 1; ++i) ans += "L"; } if (i != n) ans += "D"; } cout << n + m - 3 + n * m << endl << ans; return 0; }
8
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; int a[N]; int cnt[N]; int last[N]; int main() { int n, k; scanf("%d %d", &n, &k); for (int i = 0; i < n; i++) { scanf("%d", a + i); for (int j = 0; j < i; j++) { cnt[abs(a[i] - a[j])]++; } } int max_pair = (k + 1) * k / 2; for (int d = n - k; d < N; d++) { int sz = 0; for (int i = 0; i < N; i += d) { sz += cnt[i]; if (sz > max_pair) break; } if (sz > max_pair) continue; int t = 0; for (int i = 0; i < n; i++) { if (last[a[i] % d] == d) t++; last[a[i] % d] = d; } if (t <= k) { cout << d << endl; return 0; } } return 0; }
16
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { int n, k; string s; cin >> n >> k >> s; vector<vector<int>> g(k, vector<int>(200, 0)); for (int i = 0; i < n; i++) g[i % k][s[i]]++; for (int i = 0, cnt = 0; i < k; i++) { int sum = 0, mx = 0; for (char c = 'a'; c <= 'z'; c++) { sum += g[i][c] + g[k - i - 1][c]; mx = max(mx, g[i][c] + g[k - i - 1][c]); } cnt += sum - mx; if (i == k - 1) cout << cnt / 2 << '\n'; } } return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; multiset<int> s; int mx = 0; for (int i = 0; i < n; i++) { int val; cin >> val; s.insert(val); mx = max(mx, val); } for (int i = 1; i <= mx; i++) { if (mx % i == 0) s.erase(s.find(i)); } cout << mx << " " << *--s.end(); }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int grid[n + 1][n + 1]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> grid[i][j]; } } bool good = false; if (n == 1) { if (grid[0][0] <= 1) { good = true; } } else { for (int i = 0; i < n; i++) { bool breakAll = false; for (int j = 0; j < n; j++) { if (grid[i][j] > 1) { bool ok = false; for (int x = 0; x < n; x++) { for (int y = 0; y < n; y++) { if (grid[x][j] + grid[i][y] == grid[i][j]) { good = true; ok = true; break; } } } if (ok == 0) { good = false; breakAll = true; break; } } } if (breakAll) { break; } } } if (good) { cout << "Yes" << endl; } else { cout << "No" << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, ans = 0; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 1; i < n; i++) if (a[i - 1] == 1 && a[i + 1] == 1 && a[i] == 0) { ans++; a[i + 1] = 0; } cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; long long ans = 0; for (int i = (0); i < (n); ++i) { long long t, T, x, cost; cin >> t >> T >> x >> cost; if (t >= T) { ans += cost + m * x; continue; } long long aux1 = cost; if (m > (T - t)) aux1 += m * x; long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; aux2 *= cost; ans += min(aux1, aux2); } cout << ans << endl; return 0; }
11
#include <bits/stdc++.h> int main() { int n, k, i, j; long int m; scanf("%d %d", &n, &k); if (k > (n - 1) / 2) { printf("-1"); } else { m = n * k; printf("%ld\n", m); for (i = 0; i < n; i++) { for (j = 1; j <= k; j++) { printf("%d %d\n", i + 1, (i + j) % n + 1); } } } return 0; }
6
#include <bits/stdc++.h> using namespace std; const int MAXN = 110; int n; char s[MAXN]; int main() { scanf("%d", &n); scanf("%s", s + 1); int ans = n; for (int i = 1; i <= n; i++) { if (s[i] != s[i + 1] && i != n) { ans--; i++; } } printf("%d\n", ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; struct BiconnectedComponents { const int NOT_VISITED = -1; int V; const vector<int>* aa; int current_time; vector<int> ot; vector<int> low; vector<bool> articulation; vector<pair<int, int>> bridges; BiconnectedComponents(int V, const vector<int> aa[]) : V(V), aa(aa), current_time(0), ot(V, NOT_VISITED), low(V), articulation(V, false) { for (int v = 0; v < V; v++) if (ot[v] == NOT_VISITED) dfs(v, -1); } void dfs(int v, int father) { ot[v] = current_time++; low[v] = ot[v]; int sons = 0; for (int a : aa[v]) { if (a == father) continue; if (ot[a] == NOT_VISITED) { dfs(a, v); sons++; if (low[a] == ot[a]) bridges.push_back({v, a}); if (father != -1 and low[a] >= ot[v]) articulation[v] = true; } low[v] = min(low[v], low[a]); } if (father == -1 and sons >= 2) articulation[v] = true; } }; using namespace std; struct Matroid { virtual vector<bool> get_antispan(const vector<bool>& S) = 0; bool is_independent(vector<bool> S) { int sz = S.size(); for (int i = 0; i < sz; i++) { if (!S[i]) continue; S[i] = 0; if (!get_antispan(S)[i]) return false; S[i] = 1; } return true; } }; struct MatroidIntersection { int N; Matroid& A; Matroid& B; vector<bool> S; vector<vector<int>> aa; vector<int> dist; vector<int> bfs_father; MatroidIntersection(int N, Matroid& A, Matroid& B) : N(N), A(A), B(B), S(N, false), aa(N), dist(N), bfs_father(N) { while (augment()) ; } bool augment() { vector<bool> antispanA = A.get_antispan(S); vector<bool> antispanB = B.get_antispan(S); for (int x = 0; x < N; x++) if (antispanA[x] and antispanB[x]) { S[x] = 1; return true; } for (int x = 0; x < N; x++) aa[x].clear(); for (int x = 0; x < N; x++) { if (!S[x]) continue; S[x] = 0; vector<bool> new_antispanA = A.get_antispan(S); vector<bool> new_antispanB = B.get_antispan(S); S[x] = 1; for (int y = 0; y < N; y++) { if (S[y]) continue; if (new_antispanA[y]) aa[x].push_back(y); if (new_antispanB[y]) aa[y].push_back(x); } } bfs(antispanA); int min_dist = 1 << 30; for (int x = 0; x < N; x++) { if (antispanB[x] and bfs_father[x] != -1) { min_dist = min(min_dist, dist[x]); } } if (min_dist >= 1 << 30) return false; for (int x = 0; x < N; x++) { if (antispanB[x] and dist[x] == min_dist) { int it = x; while (it != -1) { S[it] = S[it] ^ 1; it = bfs_father[it]; } break; } } return true; } void bfs(const vector<bool>& sources) { for (int x = 0; x < N; x++) dist[x] = 1 << 30, bfs_father[x] = -1; queue<int> q; for (int x = 0; x < N; x++) { if (sources[x]) { dist[x] = 0; q.push(x); } } while (!q.empty()) { int x = q.front(); q.pop(); for (int y : aa[x]) { if (dist[y] > dist[x] + 1) { bfs_father[y] = x; dist[y] = dist[x] + 1; q.push(y); } } } } }; struct PartitionMatroid : Matroid { int N; int labels_num; vector<int> label; vector<int> multiplicity; PartitionMatroid(int N, int labels_num, const vector<int>& label, const vector<int>& multiplicity) : N(N), labels_num(labels_num), label(label), multiplicity(multiplicity) {} vector<bool> get_antispan(const vector<bool>& S) { assert(N == (int)S.size()); vector<int> label_cnt(labels_num, 0); for (int i = 0; i < N; i++) if (S[i]) label_cnt[label[i]]++; vector<bool> antispan(N, false); for (int i = 0; i < N; i++) { antispan[i] = !S[i] and label_cnt[label[i]] < multiplicity[label[i]]; } return antispan; } }; struct GraphicMatroid : Matroid { int V; int E; vector<pair<int, int>> edges; vector<int> anc; GraphicMatroid(int V, const vector<pair<int, int>>& edges) : V(V), E(edges.size()), edges(edges), anc(V) {} int ancestor(int v) { if (anc[anc[v]] != anc[v]) anc[v] = ancestor(anc[v]); return anc[v]; } void join(int u, int v) { u = ancestor(u); v = ancestor(v); anc[u] = v; } vector<bool> get_antispan(const vector<bool>& S) { assert(E == (int)S.size()); for (int v = 0; v < V; v++) anc[v] = v; for (int e = 0; e < E; e++) { if (S[e]) join(edges[e].first, edges[e].second); } vector<bool> antispan(E, false); for (int e = 0; e < E; e++) { if (ancestor(edges[e].first) != ancestor(edges[e].second)) { antispan[e] = true; } } return antispan; } }; struct CoGraphicMatroid : Matroid { int V; int E; vector<pair<int, int>> edges; vector<int> edge2id; vector<int>* aa; CoGraphicMatroid(int V, const vector<pair<int, int>>& edges) : V(V), E(edges.size()), edges(edges), edge2id(V * V) { aa = new vector<int>[V]; for (int e = 0; e < E; e++) edge2id[hash_edge(edges[e])] = e; } int hash_edge(pair<int, int> edge) { return min(edge.first, edge.second) + V * max(edge.first, edge.second); } vector<bool> get_antispan(const vector<bool>& S) { assert(E == (int)S.size()); for (int v = 0; v < V; v++) aa[v].clear(); for (int e = 0; e < E; e++) { if (!S[e]) { int a = edges[e].first; int b = edges[e].second; aa[a].push_back(b); aa[b].push_back(a); } } vector<pair<int, int>> bridges = BiconnectedComponents(V, aa).bridges; for (auto& pp : bridges) { if (pp.first > pp.second) swap(pp.first, pp.second); } vector<bool> antispan(E, true); for (int e = 0; e < E; e++) if (S[e]) antispan[e] = false; for (auto bridge : bridges) antispan[edge2id[hash_edge(bridge)]] = false; return antispan; } }; struct F2LinearMatroid { int D; int N; vector<unsigned long long> vec; F2LinearMatroid(int D, const vector<unsigned long long>& vec) : D(D), N(vec.size()), vec(vec) {} vector<bool> get_antispan(const vector<bool>& S) { vector<unsigned long long> mat; for (int i = 0; i < N; i++) if (S[i]) mat.push_back(vec[i]); int R = mat.size(); int it = 0; vector<int> bits; for (int l = 0; l < D; l++) { for (int i = it; i < R; i++) { if (mat[i] & (1ull << l)) { for (int j = i + 1; j < R; j++) { if (mat[j] & (1ull << l)) mat[j] ^= mat[i]; } if (it < i) swap(mat[it], mat[i]); bits.push_back(l); it++; break; } } } vector<bool> antispan(N); for (int i = 0; i < N; i++) { if (S[i]) continue; unsigned long long x = vec[i]; for (int j = 0; j < (int)bits.size(); j++) { if (x & (1ull << bits[j])) x ^= mat[j]; } if (x) antispan[i] = true; } return antispan; } }; const int MAXN = 30; const int MAXE = 2000; char mat[MAXN][MAXN]; int cell_id[MAXN][MAXN]; char ans[MAXN * 2][MAXN * 2]; int black_adj[MAXE]; int from[MAXE]; int toto[MAXE]; vector<int> edges[MAXE]; int E; int N, M; int edge_id[MAXE][MAXE]; int xx[MAXE], yy[MAXE]; int free_cells; int di[4] = {0, 1, 0, -1}; int dj[4] = {1, 0, -1, 0}; bool visited[MAXN][MAXN]; void easy_visit(int i, int j) { visited[i][j] = true; for (int t = 0; t < 4; t++) { int i1 = i + di[t], j1 = j + dj[t]; if (i1 < 0 or i1 >= N or j1 < 0 or j1 >= M or mat[i1][j1] == 'X') continue; if (!visited[i1][j1]) easy_visit(i1, j1); } } int main() { ios::sync_with_stdio(false); cin.tie(0); int T; cin >> T; for (int t = 0; t < T; t++) { cin >> N >> M; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { cin >> mat[i][j]; } } for (int i = 0; i < N * M; i++) edges[i].clear(); E = 0; free_cells = 0; for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) cell_id[i][j] = -1; for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) { if (mat[i][j] == 'X') continue; if (cell_id[i][j] == -1) { cell_id[i][j] = free_cells; xx[free_cells] = i, yy[free_cells] = j; free_cells++; } int id0 = cell_id[i][j]; for (int t = 0; t < 2; t++) { int i1 = i + di[t], j1 = j + dj[t]; if (i1 < 0 or i1 >= N or j1 < 0 or j1 >= M or mat[i1][j1] == 'X') continue; if (cell_id[i1][j1] == -1) { cell_id[i1][j1] = free_cells; xx[free_cells] = i1, yy[free_cells] = j1; free_cells++; } int id1 = cell_id[i1][j1]; from[E] = id0; toto[E] = id1; edges[id0].push_back(E); edges[id1].push_back(E); if ((i + j) % 2 == 0) black_adj[E] = id0; else black_adj[E] = id1; edge_id[id0][id1] = E; edge_id[id1][id0] = E; E++; } } bool black_leaf = false; for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) { if ((i == 0 and j == 0) or (i + j) % 2 or mat[i][j] == 'X') continue; if (edges[cell_id[i][j]].size() < 2) black_leaf = true; } if (black_leaf) { cout << "NO\n"; continue; } for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) visited[i][j] = false; easy_visit(0, 0); bool disconnected = false; for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) { if (mat[i][j] != 'X' and !visited[i][j]) disconnected = true; } if (disconnected) { cout << "NO\n"; continue; } vector<int> label(E, 0); for (int i = 0; i < E; i++) label[i] = black_adj[i]; vector<int> mult(free_cells, 2); for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) { if (cell_id[i][j] == -1 or (i + j) % 2) continue; int id = cell_id[i][j]; mult[id] = ((int)edges[id].size()) - 2; } mult[0] = 10; PartitionMatroid PM(E, free_cells, label, mult); vector<pair<int, int>> edges_copy(E); for (int i = 0; i < E; i++) edges_copy[i] = {from[i], toto[i]}; CoGraphicMatroid CM(free_cells, edges_copy); vector<bool> S = MatroidIntersection(E, PM, CM).S; int q = 0; for (int i = 0; i < E; i++) q += S[i]; if (E - q != free_cells - 1) { cout << "NO\n"; continue; } cout << "YES\n"; for (int i = 0; i < 2 * N - 1; i++) for (int j = 0; j < 2 * M - 1; j++) ans[i][j] = ' '; for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) { ans[2 * i][2 * j] = mat[i][j]; ans[2 * i + 1][2 * j] = '0'; ans[2 * i][2 * j + 1] = '0'; } for (int i = 0; i < E; i++) { if (!S[i]) continue; int a = from[i], b = toto[i]; int x = xx[a]; int y = yy[a]; if (x == xx[b]) { ans[2 * x][2 * y + 1] = ' '; } else { ans[2 * x + 1][2 * y] = ' '; } } for (int i = 0; i < 2 * N - 1; i++) { for (int j = 0; j < 2 * M - 1; j++) cout << ans[i][j]; cout << "\n"; } } }
25
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; if (n == 3) cout << 7 << endl; else { if (n % 2 == 0) { for (int i = 0; i < n / 2; i++) cout << 1; } else { cout << 7; int x = (n - 3) / 2; for (int i = 0; i < x; i++) cout << 1; } } }
4
#include <bits/stdc++.h> using namespace std; struct Point { double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} }; Point operator+(Point A, Point B) { return Point(A.x + B.x, A.y + B.y); } Point operator-(Point A, Point B) { return Point(A.x - B.x, A.y - B.y); } Point operator*(Point A, double p) { return Point(A.x * p, A.y * p); } Point operator/(Point A, double p) { return Point(A.x / p, A.y / p); } int Cross(Point A, Point B) { if ((A.x * B.y - A.y * B.x) == 0) { return 0; } else { return 1; } } int main() { int n; cin >> n; vector<Point> vec(n); for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; Point temp(x, y); vec[i] = temp; } int ans = 0; if (n < 3) { cout << "0" << endl; return 0; } for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { for (int k = j; k < n; k++) { Point a, b; a = vec[i] - vec[j]; b = vec[i] - vec[k]; if (Cross(a, b)) { ans++; } } } } cout << ans << endl; }
11
#include <bits/stdc++.h> using namespace std; pair<pair<int, int>, int> p[100002]; bool isgood(int i, int j, int k) { double x1 = p[i].first.first, x2 = p[j].first.first, x3 = p[k].first.first, y1 = p[i].first.second, y2 = p[j].first.second, y3 = p[k].first.second; if (x1 == x2 && x2 == x3) return false; if (x1 == x2 || x2 == x3) return true; if (((y2 - y1) / (x2 - x1)) == ((y3 - y2) / (x3 - x2))) return false; return true; } int main() { int n; cin >> n; int i, j, k; for (i = 1; i <= n; ++i) { cin >> p[i].first.first >> p[i].first.second; p[i].second = i; } sort(p + 1, p + n + 1); i = 1, j = 2, k; while (p[j].first.first == p[i].first.first) ++j; cout << p[i].second << " " << p[j].second << " "; if (j == i + 1) k = j + 1; else k = i + 1; while (!isgood(i, j, k) || k == j) ++k; cout << p[k].second; return 0; }
8
#include <bits/stdc++.h> using namespace std; const int S = 200003; char buf[1000000], *p1, *p2; inline char gc() { return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++; } inline int rd() { register int f = 0; register char c = gc(); while (c < 48 || c > 57) c = gc(); while (c > 47 && c < 58) f = f * 10 + (c ^ 48), c = gc(); return f; } int a[S], b[S], c[S]; int main() { register int n = rd(), i, j, d; long long s = 0; for (i = 1; i <= n; ++i) s += (b[i] = rd()); for (i = 1; i <= n; ++i) s += (c[i] = rd()); s /= (n << 1); for (i = 1; i <= n; ++i) a[i] = (b[i] + c[i] - s) / n; for (i = 0; i < 31; ++i) { d = 0; for (j = 1; j <= n; ++j) if (a[j] & (1 << i)) ++d, c[j] -= n * (1 << i); for (j = 1; j <= n; ++j) if (a[j] & (1 << i)) b[j] -= d * (1 << i); else c[j] -= d * (1 << i); } for (i = 1; i <= n; ++i) if (b[i] || c[i]) { printf("-1"); return 0; } for (i = 1; i <= n; ++i) printf("%d ", a[i]); return 0; }
17
#include <bits/stdc++.h> inline int read() { register int x = 0; register int y = 0; register char c = getchar(); while (c < '0' || c > '9') { if (c == '-') y = 1; c = getchar(); } while (c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar(); return y ? -x : x; } int d, n, m; int x[200006], p[200006]; int nex[200006]; struct data { int x, p; } A[200006]; struct node { int x, p; }; std::priority_queue<node> que; inline int operator<(node a, node b) { return a.p == b.p ? a.x > b.x : a.p > b.p; } inline int cmp(data a, data b) { return a.x < b.x; } int main() { d = read(); n = read(); m = read(); for (register int i = 1; i <= m; i++) A[i].x = read(), A[i].p = read(); std::sort(A + 1, A + 1 + m, cmp); for (register int i = 1; i <= m; i++) x[i] = A[i].x, p[i] = A[i].p; x[m + 1] = d; long long ans = 0; que.push((node){0, 0}); for (register int now, dis, i = 0; i <= m; i++) { now = x[i]; dis = x[i + 1] - now; if (dis > n) return puts("-1"), 0; while (dis) { while (!que.empty() && que.top().x + n <= now) que.pop(); int d = std::min(dis, n - (now - que.top().x)); dis -= d; now += d; ans += (long long)d * que.top().p; } que.push((node){x[i + 1], p[i + 1]}); } printf("%lld", ans); return 0; }
14
#include <bits/stdc++.h> int a[105]; int main() { int n, i, ans = 0, pre = 0, flag = 0, s = 0; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d", &a[i]); if (a[i] < 0) ans++; if (ans >= 3) { s++; ans = 1; } } printf("%d\n", s + 1); ans = 0; for (i = 1; i <= n; i++) { if (a[i] < 0) ans++; if (ans >= 3) { printf("%d ", i - pre - 1); pre = i - 1; ans = 0; i--; } else if (ans < 3 && i == n) printf("%d", i - pre); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int dp[2000 + 1][2000 + 1]; int sp[2000 + 1]; char str[2000 + 10]; char *trim_line(char *str) { return strtok(str, "\r\n"); } int addmod(int x, int y, int p) { long long sum = x; sum += y; if (sum >= p) sum -= p; return (int)sum; } int submod(int x, int y, int p) { long long diff = x; diff -= y; if (diff < 0) diff += p; return (int)diff; } int mulmod(int x, int y, int p) { long long prod = x; prod = (prod * y) % p; return (int)prod; } int muladdmod(long long x, int y, int z, int p) { long long prod = (x * y + z) % p; return (int)prod; } int solve_problem() { char *p; int n, k; if (fgets(str, sizeof(str), stdin) == NULL) return 1; p = strtok(str, " "); n = atoi(p); p = strtok(NULL, " "); k = atoi(p); if (fgets(str, sizeof(str), stdin) == NULL) return 1; sp[0] = dp[0][0] = 1; for (int i = 1; i <= n; i++) for (int j = 0; j <= k; j++) { char ch = str[i - 1]; int step = n + 1 - i; long long delta = 0; dp[i][j] = muladdmod(sp[j], ch - 'a', dp[i][j], 1000000007); for (int r = 0, q = j - step; r < i && q >= 0; r++, q -= step) delta += dp[i - 1 - r][q]; dp[i][j] = muladdmod(delta, 'z' - ch, dp[i][j], 1000000007); sp[j] = addmod(sp[j], dp[i][j], 1000000007); } int result = 0; for (int i = 0; i <= n; i++) result = addmod(result, dp[i][k], 1000000007); printf("%d\n", result); return 0; } int main() { solve_problem(); return 0; }
17
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<int, int>; using vi = vector<int>; ii solve(vector<vi> g) { int n = g.size(); vector<int> depth(n, 0); vector<int> leaf(n, true); int mx = n - 1; function<void(int, int)> dfs = [&](int u, int p) { int cnt = 0; for (auto v : g[u]) { if (v == p) continue; depth[v] = depth[u] + 1; dfs(v, u); leaf[u] = false; if (leaf[v]) cnt++; } if (cnt) mx -= (cnt - 1); }; int root = 0; for (int u = 0; u < n; ++u) if (g[u].size() > g[root].size()) root = u; ((void)0); dfs(root, -1); vector<int> parity(2, 0); for (int u = 0; u < n; ++u) if (leaf[u]) parity[depth[u] % 2]++; int mn = 1; if (parity[0] and parity[1]) mn = 3; return {mn, mx}; } int main(void) { ios::sync_with_stdio(false), cin.tie(NULL); int n; cin >> n; vector<vi> g(n); for (int i = 0; i < n - 1; ++i) { int u, v; cin >> u >> v, u--, v--; g[u].push_back(v); g[v].push_back(u); } auto [mn, mx] = solve(g); cout << mn << " " << mx << endl; return 0; }
10
#include <bits/stdc++.h> using namespace std; long long a3, b3, c3, d3; void findIn(long long a1, long long b1, long long c1, long long d1, long long a2, long long b2, long long c2, long long d2) { a3 = max(a1, a2); b3 = max(b1, b2); c3 = min(c1, c2); d3 = min(d1, d2); } bool valid(long long a, long long b, long long c, long long d) { return (a <= c) && (b <= d); } long long top(long long a, long long b) { if (a % b) { return a / b + 1; } return a / b; } int main() { long long white; long long n, m, t; long long a1, b1, c1, d1; long long a2, b2, c2, d2; cin >> t; while (t--) { cin >> n >> m; white = top(n * m, 2); cin >> a1 >> b1 >> c1 >> d1; cin >> a2 >> b2 >> c2 >> d2; findIn(a1, b1, c1, d1, a2, b2, c2, d2); if (a1 % 2 == b1 % 2) { white += ((c1 - a1 + 1) * (d1 - b1 + 1)) / 2; } else { white += top((c1 - a1 + 1) * (d1 - b1 + 1), 2); } if (valid(a3, b3, c3, d3)) { if (a3 % 2 == b3 % 2) { white -= ((c3 - a3 + 1) * (d3 - b3 + 1)) / 2; } else { white -= top((c3 - a3 + 1) * (d3 - b3 + 1), 2); } } if (a2 % 2 == b2 % 2) { white -= top((c2 - a2 + 1) * (d2 - b2 + 1), 2); } else { white -= ((c2 - a2 + 1) * (d2 - b2 + 1)) / 2; } cout << white << " " << m * n - white << endl; } }
7
#include <bits/stdc++.h> using namespace std; using cd = complex<double>; const double PI = acos(-1); void fft(vector<cd>& a, bool invert) { int n = a.size(); for (int i = 1, j = 0; i < n; i++) { int bit = n >> 1; for (; j & bit; bit >>= 1) j ^= bit; j ^= bit; if (i < j) swap(a[i], a[j]); } for (int len = 2; len <= n; len <<= 1) { double ang = 2 * PI / len * (invert ? -1 : 1); cd wlen(cos(ang), sin(ang)); for (int i = 0; i < n; i += len) { cd w(1); for (int j = 0; j < len / 2; j++) { cd u = a[i + j], v = a[i + j + len / 2] * w; a[i + j] = u + v; a[i + j + len / 2] = u - v; w *= wlen; } } } if (invert) { for (cd& x : a) x /= n; } } vector<int> multiply(vector<int> const& a, vector<int> const& b) { vector<cd> fa(a.begin(), a.end()), fb(b.begin(), b.end()); int n = 1; while (n < (int)(a.size() + b.size())) n <<= 1; fa.resize(n); fb.resize(n); fft(fa, false); fft(fb, false); for (int i = 0; i < n; i++) fa[i] *= fb[i]; fft(fa, true); vector<int> result(n); for (int i = 0; i < n; i++) result[i] = round(fa[i].real()); return result; } int32_t main() { ios::sync_with_stdio(0); cout.tie(0); cin.tie(0); int n, x, y; cin >> n >> x >> y; vector<int> a(x + 1, 0), b(x + 1, 0); for (int i = 0; i < n + 1; ++i) { int p; cin >> p; a[p] = 1; b[x - p] = 1; } vector<int> pos = multiply(a, b), l(1e6 + 5, -1); for (int i = x + 1; i <= 2 * x; ++i) { if (!pos[i]) continue; int perm = 2 * (i - x + y); for (int j = perm; j < 1e6 + 5; j += perm) { l[j] = max(l[j], perm); } } int q; cin >> q; while (q--) { int x; cin >> x; cout << l[x] << " "; } cout << '\n'; }
18
#include <bits/stdc++.h> using namespace std; const int k = 300; const int arr = 1e5 + 100; vector<int> vec[arr]; vector<pair<int, int> > steck[arr / k + 10]; int too[arr][arr / k + 10], from[arr]; int get_ans(int l, int r, int x) { for (int i = max(r / k * k, x) + 1; i <= r; i++) { if (from[i] >= l && from[i] <= x) x = i; } return (x); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int n; cin >> n; int m; cin >> m; for (int i = 1; i <= n; i++) from[i] = i; while (m--) { int l, r; cin >> l >> r; vec[l].push_back(r); from[r] = l; } for (int i = 1; i <= n; i++) { vec[i].push_back(i); vec[i].push_back(1e9); sort(vec[i].begin(), vec[i].end()); int last = 0; for (int j = 1; j * k <= n; j++) { while (vec[i][last] <= j * k) last++; if (last == 0) continue; too[i][j] = vec[i][last - 1]; } } for (int i = n; i >= 0; i--) for (int j = 1; j * k <= n; j++) if (i <= k * j) too[i][j] = max(too[i][j], i); for (int j = 1; j * k <= n; j++) steck[j].push_back({n + 1, 0}); for (int i = n; i >= 1; i--) for (int j = 1; j * k <= n; j++) { while (too[i][j] >= steck[j][steck[j].size() - 1].first) { too[i][j] = max(too[i][j], steck[j][steck[j].size() - 1].second); steck[j].pop_back(); } steck[j].push_back({i, too[i][j]}); } int q; cin >> q; while (q--) { int l, r; cin >> l >> r; cout << get_ans(l, r, max(l, too[l][r / k])) << '\n'; } }
22
#include <bits/stdc++.h> using namespace std; int dp1[155][11255], dp2[155][11255], a[155]; void upd1(int &x, int y) { if (x < y) x = y; } void upd2(int &x, int y) { if (x > y || x == -1) x = y; } int main() { int n, k, s, i, j, l, loc, ans = 0, sum = 0; cin >> n >> k >> s; s = min(s, n * (n - 1) / 2); for (i = 1; i <= n; i++) { scanf("%d", &a[i]); if (i <= k) sum += a[i]; } memset(dp1, -1, sizeof(dp1)); memset(dp2, -1, sizeof(dp2)); dp1[0][0] = 0; dp2[0][0] = 0; for (i = 1; i <= k; i++) { loc = k + 1 - i; for (j = i - 1; j >= 0; j--) for (l = s - loc; l >= 0; l--) if (dp1[j][l] != -1) { upd1(dp1[j + 1][l + loc], dp1[j][l] + a[i]); } } for (i = k + 1; i <= n; i++) { loc = i - (k + 1); for (j = i - 1; j >= 0; j--) for (l = s - loc; l >= 0; l--) if (dp2[j][l] != -1) upd2(dp2[j + 1][l + loc], dp2[j][l] + a[i]); } for (i = 0; i <= min(k, n - k); i++) for (j = 1; j <= s; j++) { if (dp1[i][j - 1] != -1) upd1(dp1[i][j], dp1[i][j - 1]); if (dp2[i][j - 1] != -1) upd2(dp2[i][j], dp2[i][j - 1]); } for (i = 0; i <= min(k, n - k); i++) for (j = 0; j <= s; j++) if (dp1[i][j] != -1 && dp2[i][s - j] != -1) ans = min(ans, dp2[i][s - j] - dp1[i][j]); cout << sum + ans << endl; return 0; }
15
#include <bits/stdc++.h> using namespace std; using LL = long long int; 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...); } template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << "["; for (auto vv : V) os << vv << ","; return os << "]"; } template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << "(" << P.st << "," << P.nd << ")"; } const int MAXN = 1e6 + 6; int ans[MAXN]; int main() { int n, s; cin >> n >> s; for (int i = 1; i <= n; ++i) { ans[i] = 1; } ans[1] = s - n + 1; int poz = ans[1] - 2; if (poz < 0 || poz < n - 1) { cout << "NO" << endl; return 0; } cout << "YES" << endl; cout << s - (n - 1) * 2 << " "; for (int i = 2; i <= n; ++i) { cout << 2 << " "; } cout << endl << "1" << endl; }
6
#include <bits/stdc++.h> using namespace std; const long long mo = 1e9 + 7; long long fpow(long long a, long long b, long long c) { a %= c; long long ans = 1; while (b > 0) { if (b & 1) ans = ans * a % c; b >>= 1; a = a * a % c; } return ans; } long long inv[105]; long long dp[50][10005]; long long E(long long p, long long n, long long k) { for (long long i = 0; i <= n; i++) dp[i][0] = i == n; for (long long t = 1; t <= k; t++) { for (long long i = 0; i <= n; i++) { dp[i][t] = 0; for (long long j = i; j <= n; j++) { dp[i][t] = (dp[i][t] + dp[j][t - 1] * inv[j + 1]) % mo; } } } long long ans = 0; for (long long i = 0; i <= n; i++) ans = (ans + fpow(p, i, mo) * dp[i][k]) % mo; return ans; } long long p[105], t[105], tot = 0; int main() { long long N, K; inv[1] = 1; for (int i = 2; i < 100; i++) inv[i] = (mo - mo / i) * inv[mo % i] % mo; cin >> N >> K; long long temp = N; for (long long i = 2; i * i <= N; i++) if (temp % i == 0) { tot++; p[tot] = i; t[tot] = 0; while (temp % i == 0) temp /= i, t[tot]++; } if (temp > 1) p[++tot] = temp, t[tot] = 1; long long ans = 1; for (long long i = 1; i <= tot; i++) ans = ans * E(p[i], t[i], K) % mo; cout << ans << endl; return 0; }
14
#include <bits/stdc++.h> using namespace std; long long Mod(long long x, long long y, int p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; string str; cin >> str; map<int, int> m; m[0] = 0; m[1] = 0; m[2] = 0; for (int i = 0; i < (int)str.length(); i++) { m[str[i] - '0']++; } for (int i = 0; i < (int)str.length(); i++) { if (str[i] != '0' && m[str[i] - '0'] > n / 3 && m[0] < n / 3) { m[0]++; m[str[i] - '0']--; str[i] = '0'; } } for (int i = (int)str.length() - 1; i >= 0; i--) { if (str[i] != '2' && m[str[i] - '0'] > n / 3 && m[2] < n / 3) { m[2]++; m[str[i] - '0']--; str[i] = '2'; } } for (int i = 0; i < (int)str.length(); i++) { if (str[i] != '1' && str[i] == '2' && m[2] > n / 3 && m[1] < n / 3) { m[1]++; m[2]--; str[i] = '1'; } } for (int i = (int)str.length() - 1; i >= 0; i--) { if (str[i] != '1' && str[i] == '0' && m[0] > n / 3 && m[1] < n / 3) { m[1]++; m[0]--; str[i] = '1'; } } cout << str << endl; }
7
#include <bits/stdc++.h> using namespace std; char s[100], zero[100]; int main() { int n; scanf("%d", &n); printf("1 0\n"); fflush(stdout); scanf("%s", zero); int l = 0, r = 1e9; for (int i = 1; i < n; ++i) { int mid = (l + r) >> 1; printf("1 %d\n", mid); fflush(stdout); scanf("%s", s); if (zero[0] == s[0]) { l = mid; } else { r = mid; } } printf("0 %d 2 %d\n", l, r); fflush(stdout); return 0; }
11
#include <bits/stdc++.h> using namespace std; pair<int, int> c[300005]; int n, x1, x2; vector<int> check(int a, int b) { bool flg = false; int tmp, tmp2, l, l2, i = 0; for (; i < n; i++) { tmp = c[i].first; l = (a + tmp - 1) / tmp; if (i + l >= n) continue; tmp2 = c[i + l].first; l2 = (b + tmp2 - 1) / tmp2; if (i + l + l2 > n) continue; flg = true; break; } vector<int> ans; if (flg) { ans.push_back(i); ans.push_back(l); ans.push_back(l2); } return ans; } void show(vector<int>& v, bool rev) { vector<int> v1, v2; for (int i = v[0]; i < v[0] + v[1]; i++) v1.push_back(c[i].second); for (int i = v[0] + v[1]; i < v[0] + v[1] + v[2]; i++) v2.push_back(c[i].second); cout << "Yes" << endl; if (rev) { cout << v[2] << " " << v[1] << endl; for (auto it : v2) cout << it << " "; cout << endl; for (auto it : v1) cout << it << " "; cout << endl; } else { cout << v[1] << " " << v[2] << endl; for (auto it : v1) cout << it << " "; cout << endl; for (auto it : v2) cout << it << " "; cout << endl; } } int main() { while (cin >> n >> x1 >> x2) { for (int i = 0; i < n; i++) { cin >> c[i].first; c[i].second = i + 1; } sort(c, c + n); vector<int> ans = check(x1, x2); if (ans.size()) { show(ans, false); continue; } ans = check(x2, x1); if (ans.size()) { show(ans, true); continue; } cout << "No" << endl; } return 0; }
9
#include <bits/stdc++.h> using namespace std; int gcd(int A, int B) { if (B) return gcd(B, A % B); return A; } double operating(long long x1, long long y1, long long x2, long long y2) { return (x1 * y2) - (x2 * y1); } int main(int argc, const char* argv[]) { double polygons[(int)1e5 + 10]; double p[(int)1e5 + 10]; int x[(int)1e5 + 10]; int y[(int)1e5 + 10]; double s = 0.0; double result; double tmp = 0.0; double mult; int n; int minimum; cin >> n; minimum = min(n, 60); p[0] = 1; for (int i = 0; i < n; ++i) { cin >> x[i] >> y[i]; p[i + 1] = p[i] * 0.5; } for (int i = 3; i < minimum; ++i) { mult = (p[i] - p[n]) / (1 - p[n] * ((long long)n * (n - 1) / 2 + n + 1)); for (int j = 0; j < n; ++j) { polygons[j] += operating(x[(j + i - 2) % n] - x[j], y[(j + i - 2) % n] - y[j], x[(j + i - 1) % n] - x[j], y[(j + i - 1) % n] - y[j]); s -= mult * polygons[j]; } } for (int i = 0; i < n - 2; ++i) { s += operating(x[i + 1] - x[0], y[i + 1] - y[0], x[i + 2] - x[0], y[i + 2] - y[0]); } s /= 2; for (int i = 2; i <= minimum; ++i) { mult = (p[i] - p[n]) / (1 - p[n] * ((long long)n * (n - 1) / 2 + n + 1)); for (int j = 0; j < n; ++j) { tmp += mult * gcd(abs(x[(j + i - 1) % n] - x[j]), abs(y[(j + i - 1) % n] - y[j])); } } result = s - tmp / 2 + 1; printf("%10f", result); cout << endl; return 0; }
20
#include <bits/stdc++.h> using namespace std; const int MAXN = 500000; char str[MAXN + 5]; int n, T; int houses; bool check(int k) { int c, c2; int rightmost; for (rightmost = n - 1; rightmost >= 0; rightmost--) if (str[rightmost] == 'H') break; int shops = 0; for (c = 0; c < n; c++) { shops += str[c] == 'S'; if (shops + k >= houses) { rightmost = max(rightmost, c); break; } } if (c == n) return 0; shops = k; int i = -1; int needed = 0; int ret = 0; for (c = 0; c <= rightmost; c++) { ret++; if (ret > T) return 0; if (str[c] == '.') continue; if (str[c] == 'S') { shops++; if (needed && shops >= needed) { int x = c - i; int y = rightmost - c; if (x + x + y <= y + y + x) { ret += x + x; shops -= needed; needed = 0; } } continue; } if (shops > 0) { if (needed) needed++; else shops--; } else { if (!needed) i = c; needed++; } } if (needed) ret += rightmost - i; return ret <= T; } int main() { int c, c2; scanf("%d%d", &n, &T); scanf("%s", str); houses = 0; for (c = 0; c < n; c++) houses += str[c] == 'H'; int s = 0, e = n; int ret = -1; while (s <= e) { int mid = (s + e) >> 1; if (check(mid)) { ret = mid; e = mid - 1; } else s = mid + 1; } printf("%d\n", ret); return 0; }
15
#include <bits/stdc++.h> using namespace std; int main() { char A[3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cin >> A[i][j]; } } if ((A[0][0] == A[2][2]) && (A[0][1] == A[2][1]) && (A[2][0] == A[0][2]) && (A[1][0] == A[1][2])) cout << "YES"; else cout << "NO"; return 0; }
0
#include <bits/stdc++.h> using namespace std; int ar[200005], col[200005]; int cnt[200005]; int main() { long long t, n, m, i, ans = 0, mx = 0, k; cin >> n >> m >> k; for (i = 0; i < n; i++) { cin >> ar[i]; if (ar[i] <= m) { mx++; } } if (m > k) cout << n << endl; else cout << (mx + 1) / 2 << endl; }
4
#include <bits/stdc++.h> using namespace std; int diam, diamcv, center; vector<int> g[100005]; void dfs(int x, int p, int d) { if (d > diam) { diam = d; diamcv = x; } for (int i = 0; i < g[x].size(); i++) if (g[x][i] != p) dfs(g[x][i], x, d + 1); } int dfs2(int x, int p) { if (x == diamcv) return -diam / 2; for (int i = 0; i < g[x].size(); i++) if (g[x][i] != p) { int res = dfs2(g[x][i], x); if (res == 0) return 0; if (res == -1) { center = x; return 0; } if (res != -1e9 && res < 0) return res + 1; } return -1e9; } bool dfs3(int c, int p, int k) { if (k == 0) return g[c].size() == 1; if (g[c].size() < 4) return false; bool res = true; for (int i = 0; i < g[c].size(); i++) if (g[c][i] != p) res &= dfs3(g[c][i], c, k - 1); return res; } int n, k, u, v; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 0; i < n - 1; i++) { cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } int first; for (int i = 1; i <= n; i++) if (g[i].size() == 1) { first = i; dfs(i, 0, 0); break; } if (diam % 2) { cout << "No"; return 0; } dfs2(first, 0); if (g[center].size() < 3) { cout << "No"; return 0; } bool res = true; for (int i = 0; i < g[center].size(); i++) res &= dfs3(g[center][i], center, k - 1); cout << (res ? "Yes" : "No"); return 0; }
10
#include <bits/stdc++.h> using namespace std; const int maxn = 12 + 100; int n, m; int a[maxn], b[maxn], c[maxn][maxn]; int main() { cin >> n >> m; for (int i = 1; i <= n; ++i) { cin >> a[i]; c[i][a[i]]++; for (int j = 1; j <= m; ++j) { c[i][j] += c[i - 1][j]; } } int all = 0; for (int i = 1; i <= m; ++i) cin >> b[i], all += b[i]; for (int i = all; i <= n; ++i) { int flag = 0; for (int j = 1; j <= m; ++j) { if (c[i][j] - c[i - all][j] != b[j]) { flag = 1; break; } } if (!flag) { cout << "YES" << endl; return 0; } } cout << "NO" << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; const int N=1e6+5; long long n,arr[N]; int solve() { string s; cin>>s; char ss[2]={'(',')'}; for(int a=0;a<2;a++) { for(int b=0;b<2;b++) { for(int c=0;c<2;c++){ stack<char> st; bool ok=1; for(int i=0;i<s.size();i++) { char A,B,C; A=ss[a]; B=ss[b]; C=ss[c]; if(s[i]=='A') { if(A=='(') st.push('('); else if(st.size()) st.pop(); else{ ok=0; break; } } else if(s[i]=='B') { if(B=='(') st.push('('); else if(st.size()) st.pop(); else{ ok=0; break; } } else if(s[i]=='C'){ if(C=='(') st.push('('); else if(st.size()) st.pop(); else{ ok=0; break; } } else{ ok=0; break; } } if(st.size()==0 && ok) { return cout << "YES\n", 0; } } } } return cout<<"NO\n",0; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin>>t; while(t--) solve(); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector<vector<int>> a(5, vector<int>(55)); for (int i = 1; i <= 4; ++i) { for (int j = 1; j <= n; ++j) { cin >> a[i][j]; } } pair<int, int> cur = {2, 1}; int step = 0, cnt = 0; vector<pair<int, pair<int, int>>> ans; while (step <= 20000 && cnt != k) { auto t = cur; if (cur.first == 2) { ++t.second; } else { --t.second; } if (t.second > n) { ++t.first; --t.second; } if (t.second < 1) { --t.first; ++t.second; } auto g = t; g.first = t.first == 2 ? 1 : 4; if (a[t.first][t.second]) { if (a[t.first][t.second] == a[g.first][g.second]) { ans.push_back({a[t.first][t.second], g}); ++cnt; a[t.first][t.second] = 0; } else if (!a[cur.first][cur.second]) { ans.push_back({a[t.first][t.second], cur}); swap(a[cur.first][cur.second], a[t.first][t.second]); } } cur = t; ++step; } if (cnt == k) { cout << (int)ans.size() << '\n'; for (auto x : ans) { cout << x.first << ' ' << x.second.first << ' ' << x.second.second << '\n'; } } else { cout << -1 << '\n'; } return 0; }
13
#include <bits/stdc++.h> using namespace std; long long rain[3000]; pair<long long, long long> umb[3000]; long long dp[3000][3000]; int main() { ifstream fin("input.txt", ios::in); ios_base::sync_with_stdio(false); cout << setprecision(10); for (long long i = 0; i < 3000; i++) { umb[i].first = 2000000000ll; } for (long long i = 0; i < 3000; i++) { for (long long j = 0; j < 3000; j++) { dp[i][j] = 2000000000ll; } } long long a, n, m; cin >> a >> n >> m; while (n--) { long long c, d; cin >> c >> d; for (long long i = c + 1; i <= d; i++) rain[i] = 1; } long long cost[m + 1]; for (long long i = 0; i < m; i++) { long long c, d; cin >> c >> d; cost[i + 1] = d; if (umb[c].first > d) { umb[c].first = d; umb[c].second = i + 1; } } dp[0][0] = 0; if (umb[0].first < 2000000000ll) dp[0][umb[0].second] = 0; for (long long i = 1; i <= a; i++) { for (long long j = 0; j <= m; j++) { if (j > 0) dp[i][j] = min(dp[i - 1][j] + cost[j], dp[i][j]); if (rain[i] == 0) { dp[i][0] = min(dp[i][0], dp[i - 1][j]); } if (umb[i - 1].first < 2000000000ll) { dp[i][umb[i - 1].second] = min(dp[i][umb[i - 1].second], (dp[i - 1][j] + umb[i - 1].first)); } } } long long ans = 2000000000ll; for (long long i = 0; i <= m; i++) { ans = min(ans, dp[a][i]); } if (ans == 2000000000ll) cout << -1; else cout << ans; }
13
#include <bits/stdc++.h> using namespace std; vector<long long> v[100005]; long long root(long long arr[], long long i) { while (i != arr[i]) { i = arr[arr[i]]; } return i; } bool Find(long long a, long long b, long long arr[]) { long long root_a = root(arr, a); long long root_b = root(arr, b); if (root_a == root_b) { return true; } else { return false; } } void Union(long long a, long long b, long long arr[], long long size[]) { long long root_a = root(arr, a); long long root_b = root(arr, b); if (root_a == root_b) { return; } if (size[root_a] < size[root_b]) { arr[root_a] = arr[root_b]; size[root_b] += size[root_a]; } else { arr[root_b] = arr[root_a]; size[root_a] += size[root_b]; } } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n; cin >> n; long long state[n + 5]; long long arr[n + 4], size[n + 5]; for (auto i = 1; i < n + 1; i++) { arr[i] = i; size[i] = 1; } for (auto i = 1; i < n + 1; i++) { cin >> state[i]; } long long fav[n + 2]; for (auto i = 1; i < n + 1; i++) { cin >> fav[i]; } for (auto i = 1; i < n + 1; i++) { if (i + fav[i] <= n) { Union(i, i + fav[i], arr, size); } if (i - fav[i] >= 1) { Union(i, i - fav[i], arr, size); } } for (auto i = 1; i < n + 1; i++) { if (Find(i, state[i], arr) == false) { cout << "NO\n"; return 0; } } cout << "YES" << endl; }
8
#include <bits/stdc++.h> using namespace std; const int MAX_S = 100; int n; int f[MAX_S]; int fNr; int v[MAX_S]; int main() { int mNr; cin >> n >> fNr >> mNr; for (int i = 0; i < fNr; i++) cin >> f[i]; for (int i = 0; i < mNr; i++) { int now; cin >> now; for (int j = 0; j < fNr; j++) if (now % f[j] == 0) v[j]++; } vector<int> best; int minV = n + 1; for (int i = 0; i < fNr; i++) { if (v[i] < minV) { best.clear(); minV = v[i]; } if (v[i] == minV) best.push_back(i + 1); } cout << best.size() << endl; for (int i = 0; i < best.size(); i++) { if (i) cout << ' '; cout << best[i]; } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 500010; int a[maxn], b[maxn], n; int main() { while (cin >> n) { for (int i = 1; i <= n; i++) scanf("%d", a + i); int flag = 0, ans = 0; b[1] = a[1]; b[n] = a[n]; for (int i = 2; i < n; i++) { b[i] = a[i - 1] + a[i] + a[i + 1] > 1; if (b[i] != a[i]) flag = 1; } if (flag) { memset(a, 0, sizeof a); for (int i = 1; i < n; i++) if (b[i] == b[i + 1]) a[i] = a[i + 1] = 1; int k = 2, len, tmp, t1, t2; while (k < n) { len = 0; while (k < n && a[k]) k++; tmp = k; t1 = b[k - 1]; while (k < n && !a[k]) k++, len++; t2 = b[k + 1]; ans = max(ans, (len + 1) / 2 + 1); if (t1 == t2) { for (int i = tmp; i < k; i++) b[i] = t1; } else { for (int i = 0; i < len / 2; i++) b[tmp++] = t1; for (int i = 0; i < len / 2; i++) b[tmp++] = t2; } } } printf("%d\n", ans); for (int i = 1; i <= n; i++) printf("%d%c", b[i], i == n ? '\n' : ' '); } return 0; }
9
#include <bits/stdc++.h> using namespace std; const int N = 305, M = N * N, INF = 0x3f3f3f3f; int n, own[M]; bool vis[M], G[N][N]; int Dfs(int x) { for (int i = 1; i <= n; ++i) if (G[x][i] && !vis[i]) { vis[i] = 1; if (!own[i] || Dfs(own[i])) { own[i] = x; return 1; } } return 0; } struct Edge { int to, res; Edge *next; Edge() {} Edge(int to, int res, Edge *next) : to(to), res(res), next(next) {} } * head[N], pool[M << 1], *pis = pool, *cur[N]; void AddEdge(int u, int v, int c) { head[u] = new (pis++) Edge(v, c, head[u]); head[v] = new (pis++) Edge(u, 0, head[v]); } int s, t, d[N]; int Dfs(int u, int f) { if (u == t || f == 0) return f; int fellow = 0, D; for (Edge *&now = cur[u]; now; now = now->next) if (d[now->to] == d[u] + 1 && (D = Dfs(now->to, min(f, now->res)))) { fellow += D; f -= D; now->res -= D; (pool + ((now - pool) ^ 1))->res += D; if (!f) break; } return fellow; } queue<int> Q; bool Bfs() { Q.push(s); memset(vis, 0, sizeof vis); vis[s] = 1; while (!Q.empty()) { int u = Q.front(); Q.pop(); for (Edge *now = head[u]; now; now = now->next) if (!vis[now->to] && now->res) { vis[now->to] = 1; d[now->to] = d[u] + 1; Q.push(now->to); } } return vis[t]; } int main() { scanf("%d", &n); for (int i = 1, m; i <= n; ++i) { scanf("%d", &m); for (int j = 0, k; j < m; ++j) { scanf("%d", &k); G[i][k] = 1; } } for (int i = 1; i <= n; ++i) memset(vis, 0, sizeof vis), Dfs(i); s = n + 1, t = n + 2; int fellow = 0; for (int i = 1, w; i <= n; ++i) { scanf("%d", &w); w = -w; if (w < 0) AddEdge(i, t, -w); if (w > 0) AddEdge(s, i, w), fellow += w; for (int v = 1; v <= n; ++v) if (G[i][v] && own[v] && own[v] != i) AddEdge(i, own[v], INF); } while (Bfs()) { for (int i = 1; i <= t; ++i) cur[i] = head[i]; fellow -= Dfs(s, INF); } for (Edge *now = head[s]; now; now = now->next) if (now->res < 0) puts("A"); printf("%d\n", -fellow); return 0; }
21
#include <bits/stdc++.h> using namespace std; using ll = long long int; int main() { ios::sync_with_stdio(0); cin.tie(0); mt19937_64 rng( chrono::high_resolution_clock::now().time_since_epoch().count()); int t; cin >> t; while (t--) { int n, m; cin >> n >> m; vector<string> v(n); for (int i = 0; i < n; ++i) cin >> v[i]; vector<int> ans; auto flip = [&](int x, int y) { ans.push_back(x); ans.push_back(y); v[x][y] = '1' + '0' - v[x][y]; }; for (int i = 0; i + 2 < n; ++i) { for (int j = 0; j < m; ++j) { if (v[i][j] == '0') continue; flip(i, j); flip(i + 1, j); flip(i + 1, j == 0 ? j + 1 : j - 1); } } for (int j = 0; j + 2 < m; ++j) { for (int i = n - 2; i < n; ++i) { if (v[i][j] == '0') continue; flip(i, j); flip(i, j + 1); flip(i == n - 2 ? i + 1 : i - 1, j + 1); } } vector<array<int, 2>> good, bad; for (int i = n - 2; i < n; ++i) for (int j = m - 2; j < m; ++j) if (v[i][j] == '0') good.push_back({i, j}); else bad.push_back({i, j}); if (bad.size() == 4) { for (int i = 0; i < 3; ++i) { flip(bad.back()[0], bad.back()[1]); good.push_back(bad.back()); bad.pop_back(); } } if (bad.size() == 1) { flip(bad[0][0], bad[0][1]); flip(good[0][0], good[0][1]); flip(good[1][0], good[1][1]); auto tmp1 = {bad[0], good[2]}, tmp2 = {good[0], good[1]}; good = tmp1, bad = tmp2; } if (bad.size() == 2) { flip(bad[0][0], bad[0][1]); flip(good[0][0], good[0][1]); flip(good[1][0], good[1][1]); auto tmp1 = {bad[0]}, tmp2 = {good[0], good[1], bad[1]}; good = tmp1, bad = tmp2; } if (bad.size() == 3) { for (int i = 0; i < 3; ++i) flip(bad[i][0], bad[i][1]); } cout << ans.size() / 6 << '\n'; for (int i = 0; i < ans.size(); ++i) cout << ans[i] + 1 << " \n"[i % 6 == 5]; } }
7
#include <bits/stdc++.h> using namespace std; int n, nr_b; struct numbers { int nr; int o, e; }; numbers v[200005]; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> v[i].nr; v[i].e = v[i - 1].e; v[i].o = v[i - 1].o; if (i % 2 == 0) v[i].e += v[i].nr; else v[i].o += v[i].nr; } for (int i = 1; i <= n; i++) { int even = v[i].e; int odd = v[i].o; even += (v[n].o - v[i].o); odd += (v[n].e - v[i].e); if (i % 2 == 0) even -= v[i].nr; else odd -= v[i].nr; if (even == odd) ++nr_b; } cout << nr_b << '\n'; return 0; }
4
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:100000000") char s[100500]; int a[50], ind[50]; bool use[50]; bool cmp(int i, int j) { return a[i] < a[j]; } int main() { int i, j; gets(s); int k; int l = strlen(s); for (i = 0; i < l; ++i) a[s[i] - 'a']++; scanf("%d", &k); for (i = 0; i < 26; ++i) ind[i] = i; sort(ind, ind + 26, cmp); int sum = 0; for (i = 0; i < 26 && sum + a[ind[i]] <= k; ++i) sum += a[ind[i]]; int ans = 0; for (j = i; j < 26; ++j) if (a[ind[j]] > 0) { ++ans; use[ind[j]] = true; } printf("%d\n", ans); for (i = 0; i < l; ++i) if (use[s[i] - 'a']) printf("%c", s[i]); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; const int M = 1e9 + 7; const int MOD = 998244353; const double PI = 3.141592653589793238460; long long int power(long long int a, long long int b) { long long int res = 1; if (a == 0) return 0; if (a == 1) return 1; for (; b > 0; b >>= 1) { if (b & 1) res = (res * a); if (res > MOD) res %= MOD; a = (a * a); if (a > MOD) a = a % MOD; } return res; } bool isPrime[10001]; vector<int> prime; void seive() { isPrime[0] = isPrime[1] = 1; for (int i = 2; i * i <= 10000; i++) { if (!isPrime[i]) for (int j = i * i; j <= 10000; j += i) { isPrime[j] = 1; } } for (int i = 2; i <= 10000; i++) if (!isPrime[i]) prime.push_back(i); } int lpd[1010]; void pre() { for (int i = 1; i <= 1000; i++) lpd[i] = i; for (int i = 2; i <= 1000; i++) { if (lpd[i] == i) { for (int j = i; j <= 1000; j += i) { if (lpd[j] == j) lpd[j] = i; } } } } int dp[501][501]; int main() { int x; cin >> x; cout << 2 << ' ' << 3 << "\n"; long long int p = pow(2, 17); cout << p + x << ' ' << p << ' ' << 0 << "\n"; cout << x << ' ' << p + x << ' ' << x << "\n"; }
9
#include <bits/stdc++.h> using namespace std; const int maxn = 1e7 + 5; struct bits { int limits; int c[maxn]; int lowbit(int x) { return x & -x; } bits(int _l = 0) : limits(_l) {} void init(int a) { limits = a; } void add(int x, int d) { for (int i = x; i < limits; i += lowbit(i)) { c[i] += d; } } int sum(int x) { int ret = 0; for (int i = x; i > 0; i -= lowbit(i)) { ret += c[i]; } return ret; } } sol; int n; int lp[maxn]; long long cnt[maxn]; void solve() { cin >> n; sol.init(n); for (int i = 2; i <= n; i++) { if (lp[i] == 0) { for (int j = i; j <= n; j += i) { if (lp[j] == 0) lp[j] = i; } } } for (int i = 1; i <= n; i++) { cnt[i] = n / i; cnt[i] = cnt[i] * (cnt[i] - 1) / 2; } for (int i = n; i >= 1; i--) { for (int j = i + i; j <= n; j += i) { cnt[i] -= cnt[j]; } } long long ans = 0; for (int i = 2; i <= n; i++) { ans -= cnt[i]; } for (int i = 2; i <= n; i++) { sol.add(lp[i], 1); } for (int i = 2; i <= n; i++) { long long cnt2, cnt3; sol.add(lp[i], -1); cnt2 = sol.sum(n / lp[i]); cnt3 = sol.sum(n / 2); cnt3 -= cnt2; ans += 2 * cnt2; if (2 * lp[i] <= n) ans += 3 * cnt3; } cout << ans << endl; } int main() { solve(); return 0; }
19
#include <bits/stdc++.h> using namespace std; const int inf_int = 1e8; const long long inf_ll = 1e16; const double pi = 3.1415926535898; bool debug = 0; const int MAXN = 2e5 + 100; bool A[1001][10001]; void solve() { int n, a, b; cin >> n >> a >> b; if (n == 2 || n == 3) { if (a == 1 && b == 1) { cout << "NO"; return; } } if (a == 1) { cout << "YES\n"; if (b == 1) { for (int i = 2; i <= n - a + 1; ++i) { A[i - 1][i] = A[i][i - 1] = 1; } } for (int i = 1; i <= b - 1; ++i) { for (int e = 1; e <= n; ++e) { if (i != e) { A[i][e] = A[e][i] = 1; } } } } else { if (b == 1) { cout << "YES\n"; for (int i = 2; i <= n - a + 1; ++i) { A[i - 1][i] = A[i][i - 1] = 1; } } else { cout << "NO"; return; } } for (int i = 1; i <= n; ++i) { string s = ""; for (int e = 1; e <= n; ++e) { if (A[i][e]) s.push_back('1'); else s.push_back('0'); } cout << s << "\n"; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; while (t--) solve(); }
9
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); long long n; cin >> n; long long a = 1, b = 1; string k; for (long long i = 0; i < n; i++) { cin >> k; if (k == "UL" || k == "DR") a++; else if (k == "ULDR") { a++; b++; } else b++; } cout << a * b; }
9
#include <bits/stdc++.h> using namespace std; int n, k; int x[100010]; int main() { cin >> n >> k; for (int i = 1; i < n + 1; ++i) { if (k) { cout << 2 * i << " " << 2 * i - 1 << " "; --k; } else cout << 2 * i - 1 << " " << 2 * i << " "; } }
6
#include <bits/stdc++.h> using namespace std; string cc, aa; int n; int main() { cin >> n; cin >> cc >> aa; int c = 0, a = 0, y = 0, m = 0; for (int i = 0; i < n; i++) { if (cc[i] == '0' && aa[i] == '1') a++; else if (cc[i] == '1' && aa[i] == '0') c++; else if (cc[i] == '1' && aa[i] == '1') y++; else m++; } int m1, y1, a1, c1, flag = 0; for (int i = 0; i <= y; i++) { int x = n / 2 + i - a - y; for (int j = 0; j <= a; j++) { int l = n / 2 - x - i - j; if (l >= 0 && l <= c && x <= m && x >= 0) { a1 = j, y1 = i, m1 = x, c1 = l; flag = 1; break; } } if (flag) break; } if (!flag) { cout << -1 << endl; return 0; } int t = 0; for (int i = 0; i < n; i++) { if (a1 && (aa[i] == '1' && cc[i] == '0')) { a1--; if (t == 0) cout << i + 1; else cout << " " << i + 1; t++; } else if (c1 && (aa[i] == '0' && cc[i] == '1')) { c1--; if (t == 0) cout << i + 1; else cout << " " << i + 1; t++; } else if (y1 && (aa[i] == '1' && cc[i] == '1')) { y1--; if (t == 0) cout << i + 1; else cout << " " << i + 1; t++; } else if (m1 && (aa[i] == '0' && cc[i] == '0')) { m1--; if (t == 0) cout << i + 1; else cout << " " << i + 1; t++; } } cout << endl; }
10
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxn = 250005; string s; struct node { int dep; int mx; int mi; int lmx; int rmx; int ans; } t[800005]; void pushup(int x) { t[x].dep = t[x << 1].dep + t[x << 1 | 1].dep; t[x].mx = max(t[x << 1].mx, t[x << 1 | 1].mx + t[x << 1].dep); t[x].mi = min(t[x << 1].mi, t[x << 1 | 1].mi + t[x << 1].dep); t[x].lmx = max(t[x << 1].lmx, t[x << 1 | 1].lmx - t[x << 1].dep); t[x].lmx = max(t[x].lmx, t[x << 1].mx - 2 * (t[x << 1 | 1].mi + t[x << 1].dep)); t[x].rmx = max(t[x << 1].rmx, t[x << 1 | 1].rmx - t[x << 1].dep); t[x].rmx = max(t[x].rmx, (t[x << 1 | 1].mx + t[x << 1].dep) - 2 * t[x << 1].mi); t[x].ans = max(t[x << 1].ans, t[x << 1 | 1].ans); t[x].ans = max(t[x].ans, max(t[x << 1].lmx + t[x << 1 | 1].mx + t[x << 1].dep, t[x << 1 | 1].rmx - t[x << 1].dep + t[x << 1].mx)); } void update(int x, int l, int r, int p) { if (l == r) { int c; if (s[l] == '(') { s[l] = ')'; c = -1; } else { s[l] = '('; c = 1; } t[x] = {c, c, c, -c, -c, 0}; return; } int mid = (l + r) >> 1; if (p <= mid) update(x << 1, l, mid, p); else update(x << 1 | 1, mid + 1, r, p); pushup(x); } void build(int x, int l, int r) { if (l == r) { int c; if (s[l] == '(') { c = 1; } else { c = -1; } t[x] = {c, c, c, -c, -c, 0}; return; } int mid = (l + r) >> 1; build(x << 1, l, mid); build(x << 1 | 1, mid + 1, r); pushup(x); } void go() { int n, m; cin >> n >> m; cin >> s; s = " " + s; n = (n - 1) << 1; build(1, 1, n); cout << t[1].ans << '\n'; while (m--) { int x, y; cin >> x >> y; if (s[x] == s[y]) { } else { update(1, 1, n, x); update(1, 1, n, y); } cout << t[1].ans << '\n'; ; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int c = 0; int t; if (!c) { t = 1; } else { cin >> t; } while (t--) { go(); } }
19
#include <bits/stdc++.h> using namespace std; int n, a[200005], f; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int i = 0; i < n - 1 && !f; i++) { a[i + 1] -= a[i] % 2; if (a[i + 1] < 0) { f = 1; } } f |= a[n - 1] % 2; printf("%s\n", f ? "NO" : "YES"); return 0; }
3
#include <bits/stdc++.h> using namespace std; string name[16]; int nel[1000][2]; int n, m; int num(int i) { int kol = 0; while (i > 0) { if (i % 2 == 1) ++kol; i /= 2; } return kol; } bool check(int ch) { bool arr[16]; for (int i = 0; i < 16; ++i) arr[i] = 0; int i = 0; while (ch > 0) { arr[i++] = ch % 2; ch /= 2; } for (int i = 0; i < m; ++i) if (arr[nel[i][0]] && arr[nel[i][1]]) return false; return true; } int main() { cin >> n >> m; for (int i = 0; i < n; ++i) cin >> name[i]; for (int i = 0; i < m; ++i) { string s; int t1, t2; cin >> s; for (int j = 0; j < n; ++j) if (name[j] == s) t1 = j; cin >> s; for (int j = 0; j < n; ++j) if (name[j] == s) t2 = j; nel[i][0] = t1; nel[i][1] = t2; } int numm = 0, max = -1; for (int i = 0; i < 1 << n; ++i) { if (check(i) && num(i) > max) max = num(i), numm = i; } cout << max << endl; int i = 0; vector<string> otv; while (numm > 0) { if (numm % 2 == 1) otv.push_back(name[i]); numm /= 2; ++i; } sort(otv.begin(), otv.end()); for (int i = 0; i < otv.size(); ++i) cout << otv[i] << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; template <class T> inline void Min(T &a, T b) { if (b < a) a = b; } const int N = (int)1e4 + 5; int in[N], out[N]; long long dp[2][N]; int main() { int n, m; cin >> n >> m; for (int i = (1), _t = (n + 1); i < _t; ++i) scanf("%d", &in[i]); for (int i = (1), _t = (n + 1); i < _t; ++i) scanf("%d", &out[i]); bool cur = 0, pre = 1; for (int i = (1), _t = (n + 1); i < _t; ++i) { for (int j = (0), _t = (i + 1); j < _t; ++j) { dp[cur][j] = LLONG_MAX; if (j < i) Min(dp[cur][j], dp[pre][j] + in[i] + (long long)j * m); if (j) Min(dp[cur][j], dp[pre][j - 1] + out[i]); } cur ^= 1, pre ^= 1; } long long ans = LLONG_MAX; for (int i = (0), _t = (n + 1); i < _t; ++i) Min(ans, dp[pre][i]); cout << ans << endl; return 0; }
21
#include <bits/stdc++.h> using namespace std; long long f[10010]; int A[10010]; long long _Min(long long x, long long y) { return x < y ? x : y; } int main() { int n, c, i, j, x; long long ans = (long long)1 << 60; scanf("%d%d", &n, &c); for (i = 1; i <= n; i++) scanf("%d", &A[i]); for (i = 1; i <= n; i++) { scanf("%d", &x); f[i] = f[i - 1] + x; for (j = i - 1; j >= 1; j--) f[j] = _Min(f[j] + (long long)c * j + A[i], f[j - 1] + x); f[0] += A[i]; } for (i = 0; i <= n; i++) ans = _Min(ans, f[i]); printf("%I64d\n", ans); return 0; }
21
#include<bits/stdc++.h> // #define int long long using namespace std; const int M = 2650; const int N = 1350; int md; inline void add(int& a, int b) { a += b; if (a >= md) a -= md; } inline int mul(int a, int b) { return (a * 1ll * b) % md; } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n >> md; vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(M, vector<int>(3, 0))); dp[0][N][0] = 1; for (int i = 0; i < n; ++i) { for (int gap = 51; gap < M - 51; ++gap) { int sum = 0; for (int ok = 0; ok <= 2; ++ok) { add(dp[i + 1][gap][ok], mul(i + 1, dp[i][gap][ok])); add(sum, dp[i][gap][ok]); } for (int d = -i; d <= i; ++d) { if (!d) continue; add(dp[i + 1][gap + d][(d > 0) + 1], mul(i + 1 - abs(d),sum)); } } } int ans = 0; for (int gap = N + 1; gap < M; ++gap) { add(ans, dp[n][gap][1]); } cout << ans; return 0; }
16
#include <bits/stdc++.h> using namespace std; struct point { long long x, y; }; int n; int A[100009]; vector<point> X; point B[100009]; inline bool cmpx(point a, point b) { if (a.x == b.x) return a.y < b.y; return a.x < b.x; } inline bool cmpy(point a, point b) { if (a.y == b.y) return a.x < b.x; return a.y < b.y; } long long dist(point a, point b) { return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); } long long dvd(int a, int b) { long long ret = INT_MAX; if (b - a + 1 < 4) { for (int i = a; i <= b; i++) for (int j = i + 1; j <= b; j++) ret = (ret < dist(X[i], X[j]) ? ret : dist(X[i], X[j])); return ret; } int mid = (b - a + 1) / 2; long long x = dvd(a, a + mid); long long y = dvd(a + mid + 1, b); ret = (ret < (x < y ? x : y) ? ret : (x < y ? x : y)); long long linex = X[a + mid].x; int sz = 0; for (int i = a; i <= b; i++) if (abs(linex - X[i].x) <= ret) B[sz++] = X[i]; sort(B, B + sz, cmpy); for (int i = 0; i < sz; i++) for (int j = i + 1; j < (sz < i + 8 ? sz : i + 8); j++) ret = (ret < dist(B[i], B[j]) ? ret : dist(B[i], B[j])); return ret; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", A + i); for (int i = 1; i < n; i++) A[i] += A[i - 1]; X.resize(n); for (int i = 0; i < n; i++) X[i].x = i, X[i].y = A[i]; sort((X).begin(), (X).end(), cmpx); printf("%lld\n", dvd(0, n - 1)); }
14
#include <bits/stdc++.h> using namespace std; const int maxint = -1u >> 1; template <class T> bool get_max(T& a, const T& b) { return b > a ? a = b, 1 : 0; } template <class T> bool get_min(T& a, const T& b) { return b < a ? a = b, 1 : 0; } const int maxn = 100000 + 5; int n, m; vector<int> adj[maxn]; int val[maxn]; set<int> ans, zero; void calc() { while (1) { if (zero.empty()) break; int idx = *zero.begin(); if (ans.count(idx)) { printf("-1\n"); return; } ans.insert(idx); zero.erase(idx); val[idx]--; for (int i = 0; i < adj[idx].size(); i++) { int v = adj[idx][i]; if (val[v] == 0) { zero.erase(v); } else if (val[v] == 1) { zero.insert(v); } val[v]--; } } printf("%d\n", ans.size()); bool first = true; for (set<int>::iterator it = ans.begin(); it != ans.end(); it++) { if (first) { printf("%d", *it); first = false; } else { printf(" %d", *it); } } printf("\n"); } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); adj[u].push_back(v); adj[v].push_back(u); } for (int i = 1; i <= n; i++) { scanf("%d", val + i); if (val[i] == 0) { zero.insert(i); } } calc(); return 0; }
13
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int x[m + 1]; for (int i = 0; i < m + 1; i++) x[i] = 0; int f = 0; for (int z = 0; z < n; z++) { int a, b; cin >> a >> b; if (z == 0) { if (a == 0) { for (int i = a; i <= min(m, b); i++) x[i] = 1; } else f = 1; } else { if (x[a] == 1) { for (int i = a; i <= min(m, b); i++) x[i] = 1; } else f = 1; } } if (f == 0) { if (x[m] == 1) cout << "YES"; else cout << "NO"; } else cout << "NO"; return 0; }
3
#include <bits/stdc++.h> using namespace std; vector<long long> adj[100007], cost[100007]; long long vis[100007], n, p, c, arr[100007], mafia[100007], parent[100007]; void dfs(long long node, long long tot = 0) { vis[node] = 1; for (long long i = 0; i < adj[node].size(); ++i) { if (!vis[adj[node][i]]) { parent[adj[node][i]] = node; dfs(adj[node][i], max(tot + cost[node][i], cost[node][i])); } } if (tot > arr[node]) mafia[node] = 1; } void del(long long node, long long x = -1) { vis[node] = x; for (long long i = 0; i < adj[node].size(); ++i) { if (vis[adj[node][i]] != x && adj[node][i] != parent[node]) del(adj[node][i], x); } } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (long long i = 1; i <= n; ++i) cin >> arr[i]; for (long long i = 1; i < n; ++i) { cin >> p >> c; adj[i + 1].push_back(p); adj[p].push_back(i + 1); cost[i + 1].push_back(c); cost[p].push_back(c); } dfs(1); for (long long i = 1; i <= n; ++i) { if (mafia[i] && vis[i] != -1) del(i); } long long ans = 0; for (long long i = 1; i <= n; ++i) ans += (vis[i] == -1); cout << ans; return 0; }
8
#include <bits/stdc++.h> static const int Infinity = 1e9; static void add(std::vector<std::vector<int>> &v, std::vector<std::vector<bool>> &used, std::queue<std::pair<int, int>> &queue, int r, int c, int l, int max) { if (v[r][c] == Infinity || l > max) return; if (v[r][c] == 0 && used[r][c]) return; if (v[r][c] == 0 && !used[r][c]) { used[r][c] = true; queue.push({r, c}); v[r][c] = l; } else if (v[r][c] != 0 && v[r][c] > l) { used[r][c] = true; queue.push({r, c}); v[r][c] = l; } } static void bfs(std::vector<std::vector<int>> &v, std::vector<std::vector<bool>> &used, int n, int m, int r, int c, int x, int y) { std::queue<std::pair<int, int>> queue; queue.push({r, c}); used[r][c] = true; while (queue.size() > 0) { auto p = queue.front(); queue.pop(); add(v, used, queue, p.first + 1, p.second, v[p.first][p.second] + 0, y); add(v, used, queue, p.first - 1, p.second, v[p.first][p.second] + 0, y); add(v, used, queue, p.first, p.second + 1, v[p.first][p.second] + 1, y); add(v, used, queue, p.first, p.second - 1, v[p.first][p.second] + 0, y); } } int main(int argc, const char *argv[]) { int n, m, r, c, x, y; std::cin >> n >> m >> r >> c >> x >> y; std::vector<std::vector<int>> v(n + 2); for (int i = 0; i < n + 2; i++) v[i].resize(m + 2); std::vector<std::vector<bool>> used(n + 2); for (int i = 0; i < n + 2; i++) used[i].resize(m + 2); for (int i = 0; i < n + 2; i++) for (int j = 0; j < m + 2; j++) v[i][j] = Infinity; for (int i = 1; i <= n; i++) { std::string s; std::cin >> s; for (int j = 1; j <= m; j++) if (s[j - 1] == '.') v[i][j] = 0; } bfs(v, used, n, m, r, c, x, y); int result = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { int t = j - c; int right = v[i][j]; int left = right - t; if (left <= x && right <= y && used[i][j]) result++; } } std::cout << result << std::endl; return 0; }
10
#include <bits/stdc++.h> using namespace std; struct point { double x, y; } p[111][3], A, B, C, D; void prin(point A) { cout << A.x << " " << A.y << endl; } double area(point a, point b, point c) { return (((b.x - a.x) * (c.y - a.y)) - ((c.x - a.x) * (b.y - a.y))) / 2; } int ins(int tid, point po) { double ret = 0; int i; double ar; for (i = 0; i < 3; i++) { ret += fabs(area(p[tid][i], po, p[tid][(i + 1) % 3])); } ar = fabs(area(p[tid][0], p[tid][1], p[tid][2])); if (fabs(ar - ret) < 1e-5) return 1; else return 0; } double dist(point a, point b) { double ret; ret = sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); return ret; } int main() { int i, n, j, k, sz, I, J; double ans, s1, s2; while (scanf("%d", &n) == 1) { ans = 0; for (i = 0; i < n; i++) { for (j = 0; j < 3; j++) { scanf("%lf %lf", &p[i][j].x, &p[i][j].y); } } for (i = 0; i < n; i++) { for (j = 0; j < 3; j++) { A = p[i][j]; B = p[i][(j + 1) % 3]; vector<double> v; v.push_back(0); v.push_back(1); for (I = 0; I < n; I++) { if (I == i) continue; for (J = 0; J < 3; J++) { C = p[I][J]; D = p[I][(J + 1) % 3]; s1 = area(C, D, A); s2 = area(C, D, B); point s; double val = s1 / (s1 - s2); if ((s1 < 0 && s2 > 0) || (s1 > 0 && s2 < 0)) { s1 = area(A, B, C); s2 = area(A, B, D); if ((s1 < 0 && s2 > 0) || (s1 > 0 && s2 < 0)) v.push_back(val); } } } sort(v.begin(), v.end()); point s, te; double d = dist(A, B); for (k = 0; k < (v.size() - 1); k++) { s.x = A.x + (B.x - A.x) * ((v[k + 1] + v[k]) / 2.0); s.y = A.y + (B.y - A.y) * ((v[k + 1] + v[k]) / 2.0); int ok = 1; for (I = 0; I < n; I++) { if (I == i) continue; if (ins(I, s)) { ok = 0; break; } } if (ok) { ans += d * (v[k + 1] - v[k]); } } } } printf("%.10lf\n", ans); } return 0; }
15
#include <bits/stdc++.h> using namespace std; int main() { int n, da = 0, df = 0; bool nb, na; cin >> n; if (n % 400 == 0 or (n % 4 == 0 and n % 100 != 0)) { nb = true; df = (da + 1) % 7; } else nb = false; do { ++n; da = (df + 1) % 7; if (n % 400 == 0 or (n % 4 == 0 and n % 100 != 0)) { df = (da + 1) % 7; na = true; } else { df = da; na = false; } } while (da or nb != na); cout << n << endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; const long double pi = 3.1415926535897; long long exp(long long base, long long power, int p) { if (!base) return 0; long long t = exp(base, power / 2, p); if (power & 1) return t * t * base % p; else return t * t % p; } void solve() { string s; cin >> s; int c(0), c0(0), c1(0), n = s.length(); set<string> st; for (int i = 0; i < n; i++) if (s[i] == '1') c1++; else if (s[i] == '0') c0++; else c++; if (c != 0) { if (c1 + c > c0 + 1) st.insert("11"); if (c1 < c0 + c) st.insert("00"); if (abs(c1 - c0) == c) { if (c1 > c0) { if (s[n - 1] == '?' or s[n - 1] == '0') st.insert("10"); else st.insert("01"); } else { if (s[n - 1] == '?' or s[n - 1] == '1') st.insert("01"); else st.insert("10"); } } if (c1 == c0 + c + 1) { if (s[n - 1] == '?' or s[n - 1] == '0') st.insert("10"); else st.insert("01"); } if (c > abs(c1 - c0)) { if (c1 + c == c0 + 1) { if (s[n - 1] == '?' or s[n - 1] == '1') st.insert("01"); else if (s[n - 1] == '0') st.insert("10"); } else { if (s[n - 1] == '?') { st.insert("10"); st.insert("01"); } else if (s[n - 1] == '0') st.insert("10"); else st.insert("01"); } } } else { if (c1 > c0 + 1) st.insert("11"); if (c1 < c0) st.insert("00"); if (c1 == c0 or c1 == c0 + 1) { if (s[n - 1] == '0') st.insert("10"); else st.insert("01"); } } for (auto x : st) cout << x << "\n"; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; while (t--) { solve(); cout << "\n"; } return 0; }
11
#include <bits/stdc++.h> using namespace std; const int MAXN = 500010; inline int read() { int x = 0, f = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); ch = getchar()) x = (x * 10) + (ch ^ 48); return x * f; } int n, m, q, fa[MAXN]; int Hash[MAXN], cnt; int e[MAXN], size[MAXN]; struct Edge { int u, v, w; } E[MAXN]; vector<pair<int, int> > G[MAXN], tmp; bool res[MAXN]; bool cmp(int a, int b) { return E[a].w < E[b].w; } int find(int x) { return fa[x] == x ? x : find(fa[x]); } int main() { int i, j, k; n = read(), m = read(); for (i = 1; i <= m; i++) { E[i].u = read(), E[i].v = read(); E[i].w = read(), e[i] = i; Hash[++cnt] = E[i].w; } sort(e + 1, e + m + 1, cmp); sort(Hash + 1, Hash + cnt + 1); cnt = unique(Hash + 1, Hash + cnt + 1) - Hash - 1; q = read(); for (i = 1; i <= q; i++) { int c = read(); while (c--) { k = read(); int w = lower_bound(Hash + 1, Hash + cnt + 1, E[k].w) - Hash; G[w].push_back(make_pair(k, i)); } res[i] = true; } for (i = 1; i <= n; i++) fa[i] = i, size[i] = 1; int ed = 0; for (i = 1; i <= cnt; i++) { while (ed < m && E[e[ed + 1]].w < Hash[i]) { ed++; int x = find(E[e[ed]].u), y = find(E[e[ed]].v); if (x == y) continue; if (size[x] < size[y]) fa[x] = y, size[y] += size[x]; else fa[y] = x, size[x] += size[y]; } int nxt; for (j = 0; j < (int)G[i].size(); j = nxt + 1) { for (nxt = j; nxt < (int)G[i].size() - 1 && G[i][nxt + 1].second == G[i][nxt].second; nxt++) ; for (k = j; k <= nxt; k++) { int id = G[i][k].first; int x = find(E[id].u), y = find(E[id].v); if (x == y) break; if (size[x] > size[y]) swap(x, y); fa[x] = y, size[y] += size[x], tmp.push_back(make_pair(x, y)); } if (k <= nxt) res[G[i][j].second] = false; for (k = 0; k < (int)tmp.size(); k++) { fa[tmp[k].first] = tmp[k].first; size[tmp[k].second] -= size[tmp[k].first]; } tmp.clear(); } } for (i = 1; i <= q; i++) { if (res[i]) printf("YES\n"); else printf("NO\n"); } return 0; }
15
#include <bits/stdc++.h> template <class X, class Y> X& remin(X& x, Y y) { return x = (y < x ? y : x); } const int NMAX = 2020, INF = (int)1e9; int n, k, arr[NMAX], minK[NMAX][NMAX]; const bool debug = 0; bool check(int x) { std::fill(&minK[0][0], &minK[0][0] + NMAX * NMAX, INF); minK[1][1] = 0; for (int prefix = 2; prefix <= n; prefix++) { std::cerr&& debug&& std::cerr << "prefix = " << prefix << std::endl; for (int last = 0; last <= prefix; last++) { std::cerr&& debug&& std::cerr << "\tlast=" << last << ", minK" << minK[prefix][last] << std::endl; } for (int last = 0; last <= prefix; last++) { remin(minK[prefix][last], minK[prefix - 1][last] + 1); } const int last = prefix; remin(minK[prefix][last], prefix - 1); for (int prev = 1; prev < prefix; prev++) { if (std::abs(arr[last] - arr[prev]) <= (last - prev + 0LL) * x) { remin(minK[prefix][last], minK[prefix - 1][prev]); } } std::cerr&& debug&& std::cerr << "prefix = " << prefix << std::endl; for (int last = 1; last <= prefix; last++) { std::cerr&& debug&& std::cerr << "\tlast=" << last << ", minK" << minK[prefix][last] << std::endl; } } return *std::min_element(minK[n] + 1, minK[n] + n + 1) <= k; } int solve() { int low = -1, high = (int)2e9; while (high - low > 1) { int mid = low + (high - low) / 2; if (check(mid)) { high = mid; } else { low = mid; } } return high; } int main() { while (std::cin >> n >> k) { for (int i = 1; i <= n; i++) std::cin >> arr[i]; std::cout << solve() << std::endl; } return 0; }
12
#include <bits/stdc++.h> using namespace std; const int INF = 2000000000; const double EPS = 1e-9; int mods(int a, int b) { return (b + (a % b)) % b; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, x, y, ans = 0, a[26] = {}; string s; cin >> n; for (int i = 0; i < n; i++) { cin >> s; a[s[0] - 'a']++; } for (int i = 0; i < 26; i++) { x = a[i] / 2; y = (a[i] + 1) / 2; ans += (x * (x - 1) / 2); ans += (y * (y - 1) / 2); } cout << ans << '\n'; return 0; }
1
#include <bits/stdc++.h> using namespace std; bool comp(pair<long long, long long> a, pair<long long, long long> b) { return a.first < b.first; } pair<long long, long long> color(pair<long long, long long> a, long long m) { return make_pair((a.second), (m - 1 - a.first)); } pair<long long, long long> counter(pair<long long, long long> a, long long m) { return make_pair((m - 1 - a.second), (a.first)); } pair<long long, long long> hor(pair<long long, long long> a, long long m) { return make_pair((a.first), (m - 1 - a.second)); } void solution() { long long n, m, x, y, z, p; cin >> n >> m >> x >> y >> z >> p; long long tn = n, tm = m; for (int i = 0; i < int(p); i++) { pair<long long, long long> temp; cin >> temp.first >> temp.second; n = tn, m = tm; temp.first--; temp.second--; x %= 4; for (int i = 0; i < int(x); i++) { temp = color(temp, n); swap(n, m); } y %= 2; for (int i = 0; i < int(y); i++) temp = hor(temp, m); z %= 4; for (int i = 0; i < int(z); i++) { temp = counter(temp, m); swap(n, m); } cout << temp.first + 1 << " " << temp.second + 1 << endl; } } int main() { double beg = clock(); ios::sync_with_stdio(false); solution(); return 0; }
7
#include <bits/stdc++.h> using namespace std; const long long fuvk = 1e6; vector<pair<long long, long long>> adj[fuvk]; void dijkstra(long long s, long long n) { long long distance[n + 1], parent[n + 1]; bool processed[n + 1]; for (long long i = 1; i <= n; ++i) { distance[i] = 1e18; parent[i] = -1; processed[i] = false; } distance[s] = 0; priority_queue<pair<long long, long long>> q; q.push({0, s}); while (!q.empty()) { long long a = q.top().second; q.pop(); if (processed[a]) continue; processed[a] = true; for (auto u : adj[a]) { long long b = u.first, w = u.second; if (distance[b] > distance[a] + w) { distance[b] = distance[a] + w; parent[b] = a; q.push({-distance[b], b}); } } } if (distance[n] == 1e18) { cout << "-1\n"; } else { vector<long long> path; for (long long ver = n; ver != 1; ver = parent[ver]) { path.push_back(ver); } path.push_back(1); reverse(path.begin(), path.end()); for (long long i = 0; i < path.size(); ++i) cout << path[i] << " "; cout << "\n"; } } int main() { ios::sync_with_stdio(0); cin.tie(0); long long n, m; cin >> n >> m; for (long long i = 0; i < m; ++i) { long long a, b, w; cin >> a >> b >> w; adj[a].push_back({b, w}); adj[b].push_back({a, w}); } dijkstra(1, n); return 0; }
11
#include <bits/stdc++.h> using namespace std; long long gcd(long long n, long long m) { if (m == 0) return n; else return gcd(m, n % m); } int longestsub(string x, string y, int n, int m) { int lcs[n + 1][m + 1]; int result = 0; for (int i = 0; i < n + 1; i++) { for (int j = 0; j < m + 1; j++) { if (i == 0 || j == 0) { lcs[i][j] = 0; } else if (x[i - 1] == y[j - 1]) { lcs[i][j] = 1 + lcs[i - 1][j - 1]; result = max(result, lcs[i][j]); } else lcs[i][j] = 0; } } return result; } long long fast_pow(long long a, long long p) { long long res = 1; while (p) { if (p % 2 == 0) { a = a * 1ll * a % 1000000007; p /= 2; } else { res = res * 1ll * a % 1000000007; p--; } } return res; } long long fact(long long n) { long long res = 1; for (int i = 1; i <= n; i++) { res = res * 1ll * i % 1000000007; } return res; } long long C(long long n, long long k) { return fact(n) * 1ll * fast_pow(fact(k), 1000000007 - 2) % 1000000007 * 1ll * fast_pow(fact(n - k), 1000000007 - 2) % 1000000007; } bool mycmp(pair<int, int> p1, pair<int, int> p2) { return p1.second <= p2.second; } bool mycmp1(pair<int, int> p1, pair<int, int> p2) { return p1.second >= p2.second; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); ; int t; cin >> t; while (t--) { int n; cin >> n; int p[n], q[n]; string s; for (int i = 0; i < n; i++) cin >> p[i]; cin >> s; int cnt1 = 0, cnt0 = 0; for (int i = 0; i < n; i++) { if (s[i] == '1') cnt1++; } if (cnt1 == 0 or cnt1 == n) { for (int i = 0; i < n; i++) { q[i] = p[i]; } } else { vector<pair<int, int>> v, t; for (int i = 0; i < n; i++) { if (s[i] == '0') v.push_back({i, p[i]}); else t.push_back({i, p[i]}); } sort(v.begin(), v.end(), mycmp); sort(t.begin(), t.end(), mycmp1); int x = 1, y = n; for (auto i : v) { q[i.first] = x; x++; } for (auto i : t) { q[i.first] = y; y--; } } for (auto i : q) cout << i << " "; cout << '\n'; } }
2
#include <bits/stdc++.h> using namespace std; bool d[505][505]; int main() { int n, m, c; scanf("%d%d", &n, &m); memset(d, 0, sizeof(d)); d[0][0] = 1; for (int i = 1; i <= n; i++) { scanf("%d", &c); for (int j = m; j >= 0; j--) { for (int k = j; k >= 0; k--) { if (j >= c) { d[j][k] |= d[j - c][k]; } if (j >= c && k >= c) d[j][k] |= d[j - c][k - c]; } } } int ans = 0; for (int i = 0; i <= m; i++) { if (d[m][i]) ans++; } printf("%d\n", ans); for (int i = 0; i <= m; i++) { if (d[m][i]) printf("%d ", i); } return 0; }
11
#include <bits/stdc++.h> using namespace std; const int N = 3000; int n; struct side { int y, next; } e[N * 2 + 9]; int lin[N + 9], cs; void Ins(int x, int y) { e[++cs].y = y; e[cs].next = lin[x]; lin[x] = cs; } void Ins2(int x, int y) { Ins(x, y); Ins(y, x); } void into() { scanf("%d", &n); for (int i = 1; i < n; ++i) { int x, y; scanf("%d%d", &x, &y); Ins2(x, y); } } int fa[N + 9][N + 9], siz[N + 9][N + 9]; void Dfs_siz(int k, int fat, int rot) { fa[rot][k] = fat; siz[rot][k] = 1; for (int i = lin[k]; i; i = e[i].next) if (e[i].y ^ fat) { Dfs_siz(e[i].y, k, rot); siz[rot][k] += siz[rot][e[i].y]; } } void Get_siz() { for (int i = 1; i <= n; ++i) Dfs_siz(i, 0, i); } long long dp[N + 9][N + 9]; long long Dfs_dp(int x, int y) { if (x == y) return 0; if (dp[x][y]) return dp[x][y]; return dp[x][y] = (long long)siz[x][y] * siz[y][x] + max(Dfs_dp(x, fa[x][y]), Dfs_dp(fa[y][x], y)); } long long ans; void Get_dp() { for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) ans = max(ans, Dfs_dp(i, j)); } void work() { Get_siz(); Get_dp(); } void outo() { printf("%lld\n", ans); } int main() { into(); work(); outo(); return 0; }
15
#include <bits/stdc++.h> using namespace std; int n, cnt; int a[(100100)]; int f[(100100)][3]; pair<int, int> V[(100100)]; int Getpos(int pos, int k, int c) { if (f[n][c] - f[pos][c] < k) return n + 1; int l = pos + 1, r = n, mid; while (l != r) { mid = l + r >> 1; if (f[mid][c] - f[pos][c] < k) { l = mid + 1; } else { r = mid; } } return l; } int calc(int k) { int win_1(0), win_2(0), pos(0), pos_1, pos_2; while (true) { pos_1 = Getpos(pos, k, 1); pos_2 = Getpos(pos, k, 2); if (pos_1 > n && pos_2 > n) return -1; if (pos_1 < pos_2) { pos = pos_1; win_1++; } else { pos = pos_2; win_2++; } if (pos == n) { if (pos_1 < pos_2 && win_1 <= win_2) { return -1; } if (pos_2 < pos_1 && win_2 <= win_1) { return -1; } return max(win_1, win_2); } } } int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; f[i][a[i]] = 1; } for (int i = 1; i <= n; i++) { f[i][1] += f[i - 1][1]; f[i][2] += f[i - 1][2]; } for (int i = 1; i <= n; i++) { int j = calc(i); if (j != -1) V[++cnt] = make_pair(j, i); } sort(V + 1, V + cnt + 1); cout << cnt << endl; for (int i = 1; i <= cnt; i++) cout << V[i].first << ' ' << V[i].second << endl; return 0; }
11
#include <bits/stdc++.h> using namespace std; long long spf[11]; long long fac[11]; void sieve() { spf[1] = 1; for (long long i = 2; i < 11; i++) spf[i] = i; for (long long i = 4; i < 11; i += 2) spf[i] = 2; for (long long i = 3; i * i < 11; i++) { if (spf[i] == i) { for (long long j = i * i; j < 11; j += i) if (spf[j] == j) spf[j] = i; } } } map<long long, long long> getfactor(long long a) { map<long long, long long> m; while (a > 1) { m[spf[a]]++; a /= spf[a]; } return m; } long long power(long long x, long long y, long long p) { long long res = 1; x = x % p; if (x == 0 || y < 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } long long inverse(long long a, long long p) { return power(a, p - 2, p); } long long ncr(long long n, long long r, long long p) { if (r > n) return 0; if (r == 0 || r == n) return 1; return (fac[n] * inverse(fac[r], p) % p * inverse(fac[n - r], p) % p) % p; } vector<long long> v[200001]; long long check[200001], degree[200001], t = 0; bool vis[200001], cycle = 0; map<long long, long long> ma; void dfs(long long cur) { vis[cur] = 1; check[cur] = 1; for (auto it : v[cur]) { if (vis[it] == 0) dfs(it); else if (check[it] == 1 && vis[it] == 1) { cycle = 1; return; } } check[cur] = 0; ma[cur] = t; t++; } void solve() { long long n, m; cin >> n >> m; for (long long i = 1; i <= n; i++) { v[i].clear(); check[i] = 0, degree[i] = 0, vis[i] = 0, check[i] = 0; } vector<pair<long long, long long> > v1; vector<pair<long long, long long> > v2; t = 0, cycle = 0; for (long long i = 1; i <= m; i++) { long long t, x, y; cin >> t >> x >> y; if (!t) v1.push_back(make_pair(x, y)); else { v[x].push_back(y); v2.push_back(make_pair(x, y)); } } for (long long i = 1; i <= n; i++) { if (vis[i] == 0) dfs(i); } if (cycle) { cout << "NO" << '\n'; return; } else { cout << "YES" << '\n'; for (auto it : v1) { if (ma[it.first] > ma[it.second]) cout << it.first << " " << it.second << '\n'; else cout << it.second << " " << it.first << '\n'; } for (auto it : v2) cout << it.first << " " << it.second << '\n'; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long test = 1; cin >> test; while (test--) { solve(); } }
12
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; const int MAXN = (int)3e5; struct Query { int r; int pos; bool operator<(const Query &other) const { return r < other.r; } }; int arr[MAXN + 1]; vector<Query> qry[MAXN + 1]; int dp[MAXN + 1]; bool sol[MAXN + 1]; void divide(int l, int r) { if (l == r) return; int mid = (l + r) / 2, i, j; for (j = 0; j < 20; j++) { int last = (1 << j); for (i = mid; i >= l; i--) { dp[i] = last; if (dp[i] & arr[i]) { dp[i] |= arr[i]; } last = dp[i]; } last = (1 << j); for (i = mid + 1; i <= r; i++) { dp[i] = last; if (dp[i] & arr[i]) { dp[i] |= arr[i]; } last = dp[i]; } for (i = mid; i >= l; i--) { for (int p = (int)qry[i].size() - 1; p >= 0 && qry[i][p].r > mid; p--) { if ((dp[i] & arr[i]) && (dp[qry[i][p].r] & arr[qry[i][p].r])) { sol[qry[i][p].pos] = 1; } } } } for (i = l; i <= mid; i++) { while (qry[i].size() && qry[i].back().r > mid) { qry[i].pop_back(); } } divide(l, mid); divide(mid + 1, r); } int main() { int i, n, q; ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); cin >> n >> q; for (i = 1; i <= n; i++) { cin >> arr[i]; } for (i = 1; i <= q; i++) { int l, r; cin >> l >> r; qry[l].push_back({r, i}); } for (i = 1; i <= n; i++) { sort(qry[i].begin(), qry[i].end()); } divide(1, n); for (i = 1; i <= q; i++) { cout << (sol[i] ? "Shi\n" : "Fou\n"); } return 0; }
14
#include <bits/stdc++.h> using namespace std; int n, m, k; int arr[100005]; int main() { cin >> m; for (int i = 0, x; i < m; i++) { cin >> x; if (i == 0) k = x; k = min(k, x); } cin >> n; for (int i = 0; i < n; i++) cin >> arr[i]; sort(arr, arr + n); long long sum = 0, cur = 0; for (int i = n - 1; i >= 0; i--) { if (cur == k) { cur = 0; i--; continue; } sum += arr[i]; cur++; } cout << sum << endl; return 0; }
6
#include <bits/stdc++.h> const int maxn = 50010, maxm = 50010, maxq = 50010; int n, m, q, C, f[maxn], p[maxn], c[maxm]; struct edge { int to; edge* next; } E[maxn], *fir[maxn]; int dep[maxn], siz[maxn], son[maxn], top[maxn]; void init(int i) { siz[i] = 1; for (edge* e = fir[i]; e; e = e->next) { dep[e->to] = dep[i] + 1; init(e->to); if (siz[e->to] > siz[son[i]]) son[i] = e->to; siz[i] += siz[e->to]; } } int dfn[maxn], now, len[maxn], tag[maxn]; void hld(int i) { dfn[i] = ++now; len[top[i]]++; if (son[i]) top[son[i]] = top[i], hld(son[i]); for (edge* e = fir[i]; e; e = e->next) if (e->to != son[i]) hld(top[e->to] = e->to); } struct event { int t, x; event* next; } Ev[maxn + maxq << 1], *nev = Ev, *fe[maxm]; long long sum[maxm], T[2][maxn]; double ans[maxq]; void add(long long* T, int i, int l, long long x) { for (; i <= l; i += i & -i) T[i] += x; } long long qry(long long* T, int i) { long long s = 0; for (; i; i -= i & -i) s += T[i]; return s; } void adds(int i, int x) { long long pos; for (int t; i; i = p[t]) { pos = dfn[i] - dfn[t = top[i]] + 1; tag[t] += x; add(T[0] + dfn[t] - 1, pos, len[t], pos * x); add(T[1] + dfn[t] - 1, pos, len[t], x); } } long long qrys(int i) { long long s = 0, pos; for (int t; i; i = p[t]) { pos = dfn[i] - dfn[t = top[i]] + 1; s += pos * tag[t] + qry(T[0] + dfn[t] - 1, pos) - qry(T[1] + dfn[t] - 1, pos) * pos; } return s; } long long solve(event* ev, double c) { if (!ev) return 0; long long s = solve(ev->next, c); if (ev->t) s += qrys(ev->x) * ev->t * 2 + dep[ev->x], adds(ev->x, ev->t); else ans[ev->x] += c * s; return s; } int main() { scanf("%d%d%d%d", &n, &m, &q, &C); for (int i = 1; i <= n; i++) scanf("%d", f + i); for (int i = 2; i <= n; i++) scanf("%d", p + i), E[i] = (edge){i, fir[p[i]]}, fir[p[i]] = E + i; for (int i = 1; i <= m; i++) scanf("%d", c + i); init(dep[1] = 1); hld(top[1] = 1); for (int i = 1; i <= n; i++) { *nev = (event){1, i, fe[f[i]]}, fe[f[i]] = nev++; sum[f[i]] += dep[i]; } int tot = 0; while (q--) { int t; scanf("%d", &t); if (t == 1) { int x, w; scanf("%d%d", &x, &w); sum[f[x]] -= dep[x]; sum[w] += dep[x]; *nev = (event){-1, x, fe[f[x]]}; fe[f[x]] = nev++; *nev = (event){1, x, fe[f[x] = w]}; fe[w] = nev++; } else { int k; scanf("%d", &k); ans[tot] = (C - 2.0 * sum[k] / n * c[k]) * C; *nev = (event){0, tot++, fe[k]}; fe[k] = nev++; } } for (int i = 1; i <= n; i++) *nev = (event){-1, i, fe[f[i]]}, fe[f[i]] = nev++; for (int i = 1; i <= m; i++) solve(fe[i], 1.0 * c[i] * c[i] / n); for (int i = 0; i < tot; i++) printf("%.12lf\n", ans[i]); }
23
#include <bits/stdc++.h> using namespace std; template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return 1; } return 0; } template <class T> ostream &operator<<(ostream &os, const vector<T> &t) { os << "["; for (__typeof((t).begin()) it = (t).begin(); it != (t).end(); it++) { if (it != t.begin()) os << ","; os << *it; } os << "]"; return os; } template <class T> ostream &operator<<(ostream &os, const set<T> &t) { os << "{"; for (__typeof((t).begin()) it = (t).begin(); it != (t).end(); it++) { if (it != t.begin()) os << ","; os << *it; } os << "}"; return os; } template <class S, class T> ostream &operator<<(ostream &os, const pair<S, T> &t) { return os << "(" << t.first << "," << t.second << ")"; } template <class S, class T> pair<S, T> operator+(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first + t.first, s.second + t.second); } template <class S, class T> pair<S, T> operator-(const pair<S, T> &s, const pair<S, T> &t) { return pair<S, T>(s.first - t.first, s.second - t.second); } const int INF = 1 << 28; const double EPS = 1e-8; const int MOD = 1000000007; long long int parse(int &p, int d); string s; long long int num(int &p) { long long int res = 0; if (p >= s.size()) throw "error 0"; if (s[p] == '(') { res = parse(++p, 4); if (p >= s.size() || s[p++] != ')') throw "error 0"; } else { if (!isdigit(s[p]) || s[p] == '0') throw "error 1"; for (; isdigit(s[p]); ++p) res = res * 10 + s[p] - '0'; } return res; } long long int parse(int &p, int d = 1) { if (p == s.size()) return 0; long long int res = d ? parse(p, d - 1) : num(p); while (p < s.size()) { if (d == 0 && s[p] == '*') { res *= num(++p); } else if (d == 1 && s[p] == '+') { res += parse(++p, 0); } else break; } return res; } int n, m; int main() { string ss; cin >> ss; vector<int> d({-1, (int)ss.size()}); for (int i = 0; i < (int)(ss.size()); i++) { if (ss[i] == '*') d.push_back(i); } sort((d).begin(), (d).end()); long long int ans = 0; for (int i = 0; i < (int)(d.size()); i++) for (int j = 0; j < (int)(i); j++) { int l = d[j] + 1, r = d[i]; if (l < 0 || ss.size() < r) continue; s = ss.substr(0, l) + "(" + ss.substr(l, r - l) + ")" + ss.substr(r, ss.size()); int p = 0; ans = max(ans, parse(p)); } cout << ans << endl; return 0; }
13
#include <bits/stdc++.h> using namespace std; void striker() { int x, y; cin >> x >> y; int a = abs(x) + abs(y); if (x > 0 && y > 0) cout << 0 << " " << a << " " << a << " " << 0; else if (x > 0 && y < 0) cout << 0 << " " << -a << " " << a << " " << 0; else if (x < 0 && y > 0) cout << -a << " " << 0 << " " << 0 << " " << a; else cout << -a << " " << 0 << " " << 0 << " " << -a; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t = 1; for (int i = 0; i < t; i++) { striker(); cout << endl; } return 0; }
2
#include <bits/stdc++.h> #pragma GCC target("sse4,avx") struct point { int x, y; point() : x(0), y(0) {} point(int x, int y) : x(x), y(y) {} }; struct dpoint { double x, y; dpoint() : x(0), y(0) {} dpoint(double x, double y) : x(x), y(y) {} }; int area(point p1, point p2, point p3) { return (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x); } double darea(dpoint p1, dpoint p2, dpoint p3) { return (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x); } struct line { int a, b, c; line(int a, int b, int c) : a(a), b(b), c(c) {} }; line middleLine(point p1, point p2) { int a = 2 * (p2.x - p1.x); int b = 2 * (p2.y - p1.y); int c = ((p1.x * a + p1.y * b) + (p2.x * a + p2.y * b)) / 2; return line(a, b, c); } dpoint intersect(line eq1, line eq2) { double det = eq1.a * eq2.b - eq1.b * eq2.a; return dpoint((eq1.c * eq2.b - eq1.b * eq2.c) / det, (eq1.a * eq2.c - eq1.c * eq2.a) / det); } point intersect(line eq1, line eq2, int& det) { det = eq1.a * eq2.b - eq1.b * eq2.a; return point(eq1.c * eq2.b - eq1.b * eq2.c, eq1.a * eq2.c - eq1.c * eq2.a); } bool isGood(point p1, point p2, point p3, dpoint& dp1, dpoint& dp2, dpoint& dp3, dpoint& dp4) { if ((p1.x == p2.x && p1.y == p2.y) || (p1.x == p3.x && p1.y == p3.y) || (p2.x == p3.x && p2.y == p3.y)) { return false; } if (area(p1, p2, p3) == 0) { return false; } line m1 = middleLine(p1, p2); line m2 = middleLine(p2, p3); line m2T = line(-m2.a, -m2.b, m2.c - m2.a * 2 * p2.x - m2.b * 2 * p2.y); dp2 = intersect(m1, m2T); dp1 = dpoint(2 * p1.x - dp2.x, 2 * p1.y - dp2.y); dp3 = dpoint(2 * p2.x - dp2.x, 2 * p2.y - dp2.y); dp4 = dpoint(2 * p3.x - dp3.x, 2 * p3.y - dp3.y); double area1 = darea(dp1, dp2, dp3); double area2 = darea(dp2, dp3, dp4); double area3 = darea(dp3, dp4, dp1); double area4 = darea(dp4, dp1, dp2); if (area1 < 0) { area2 = -area2; area3 = -area3; area4 = -area4; } if (abs(area1) < 1e-6 || area2 < 1e-6 || area3 < 1e-6 || area4 < 1e-6) { return false; } else { return true; } } bool isGood2(point p1, point p2, point p3, dpoint& dp1, dpoint& dp2, dpoint& dp3, dpoint& dp4) { if ((p1.x == p2.x && p1.y == p2.y) || (p1.x == p3.x && p1.y == p3.y) || (p2.x == p3.x && p2.y == p3.y)) { return false; } if (area(p1, p2, p3) == 0) { return false; } line m1 = middleLine(p1, p2); line m2 = middleLine(p2, p3); line m2T = line(-m2.a, -m2.b, m2.c - m2.a * 2 * p2.x - m2.b * 2 * p2.y); int det; point ip2 = intersect(m1, m2T, det); point ip1 = point(2 * p1.x * det - ip2.x, 2 * p1.y * det - ip2.y); point ip3 = point(2 * p2.x * det - ip2.x, 2 * p2.y * det - ip2.y); point ip4 = point(2 * p3.x * det - ip3.x, 2 * p3.y * det - ip3.y); int area1 = area(ip1, ip2, ip3); int area2 = area(ip2, ip3, ip4); int area3 = area(ip3, ip4, ip1); int area4 = area(ip4, ip1, ip2); if (area1 < 0) { area2 = -area2; area3 = -area3; area4 = -area4; } if (area1 == 0 || area2 <= 0 || area3 <= 0 || area4 <= 0) { return false; } else { double c = 1.0 / det; dp1.x = ip1.x * c; dp1.y = ip1.y * c; dp2.x = ip2.x * c; dp2.y = ip2.y * c; dp3.x = ip3.x * c; dp3.y = ip3.y * c; dp4.x = ip4.x * c; dp4.y = ip4.y * c; return true; } } void run(std::istream& in, std::ostream& out) { int t; in >> t; out.precision(12); for (int test = 0; test < t; test++) { point p1, p2, p3; in >> p1.x >> p1.y; in >> p2.x >> p2.y; in >> p3.x >> p3.y; dpoint dp1, dp2, dp3, dp4; if (isGood2(p1, p2, p3, dp1, dp2, dp3, dp4)) { printf("YES\n %.12lf %.12lf %.12lf %.12lf %.12lf %.12lf %.12lf %.12lf\n", dp1.x, dp1.y, dp2.x, dp2.y, dp3.x, dp3.y, dp4.x, dp4.y); continue; } if (isGood2(p1, p3, p2, dp1, dp2, dp3, dp4)) { printf("YES\n %.12lf %.12lf %.12lf %.12lf %.12lf %.12lf %.12lf %.12lf\n", dp1.x, dp1.y, dp2.x, dp2.y, dp3.x, dp3.y, dp4.x, dp4.y); continue; } if (isGood2(p2, p1, p3, dp1, dp2, dp3, dp4)) { printf("YES\n %.12lf %.12lf %.12lf %.12lf %.12lf %.12lf %.12lf %.12lf\n", dp1.x, dp1.y, dp2.x, dp2.y, dp3.x, dp3.y, dp4.x, dp4.y); continue; } printf("NO\n\n"); } } int main() { std::cin.sync_with_stdio(false); std::cin.tie(nullptr); run(std::cin, std::cout); return 0; }
18
#include <bits/stdc++.h> using namespace std; int main() { int n, vertex[22] = {0}, last = 1; cin >> n; vertex[1] = 1; set<int> edges; for (int i = 2; i <= n; i++) { while (++last) { bool norm = true; for (int j = 1; j < i; j++) if (edges.count(vertex[j] + last)) { norm = false; break; } if (norm) { vertex[i] = last; for (int j = 1; j < i; j++) edges.insert(vertex[j] + last); break; } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i == j) cout << "0"; else cout << vertex[i] + vertex[j]; if (j != n) cout << " "; } cout << endl; } return 0; }
15
#include <bits/stdc++.h> using namespace std; class tree { public: int n; vector<vector<int>> g; vector<int> pv; vector<int> depth; vector<int> sz; vector<int> order; vector<int> pos; vector<int> end; int root; int h; vector<vector<int>> anc; tree(int _n) : n(_n) { g.resize(n); root = -1; } void add(int from, int to) { assert(0 <= from && from < n && 0 <= to && to < n); g[from].push_back(to); g[to].push_back(from); } void init() { pv = vector<int>(n, -1); depth = vector<int>(n, -1); sz = vector<int>(n); order.clear(); pos = vector<int>(n, -1); end = vector<int>(n, -1); } void clear() { pv.clear(); depth.clear(); sz.clear(); order.clear(); pos.clear(); end.clear(); root = -1; } void dfs_from(int v) { init(); root = v; pv[v] = -1; depth[v] = 0; dfs(v); } void prec_kth_anc() { assert(!pv.empty()); int max_depth = 0; for (int i = 0; i < n; i++) { max_depth = max(max_depth, depth[i]); } for (h = 1; (1 << h) <= max_depth; h++) ; anc.resize(n); for (int i = 0; i < n; i++) { anc[i].resize(h); anc[i][0] = pv[i]; } for (int j = 1; j < h; j++) { for (int i = 0; i < n; i++) { anc[i][j] = (anc[i][j - 1] == -1 ? -1 : anc[anc[i][j - 1]][j - 1]); } } } int go_up(int v, int up) { assert(!anc.empty()); up = min(up, (1 << h) - 1); for (int j = h - 1; j >= 0; j--) { if (up & (1 << j)) { v = anc[v][j]; if (v == -1) { break; } } } return v; } int lca(int a, int b) { if (depth[a] < depth[b]) { swap(a, b); } a = go_up(a, depth[a] - depth[b]); if (a == b) return a; for (int i = h - 1; i >= 0; i--) { if (anc[a][i] != anc[b][i]) { a = anc[a][i]; b = anc[b][i]; } } return anc[a][0]; } private: void dfs(int v) { pos[v] = order.size(); order.push_back(v); sz[v] = 1; for (int u : g[v]) { if (u == pv[v]) { continue; } pv[u] = v; depth[u] = depth[v] + 1; dfs(u); sz[v] += sz[u]; } end[v] = order.size() - 1; } }; const int AMAX = 10; class segtree { private: struct node { vector<int> s; void add(const vector<int> &v) { s = v; } void merge(const node a, const node b) { int sa = a.s.size(); int sb = b.s.size(); int sz = min(AMAX, sa + sb); s.assign(sz, 0); int pa = 0, pb = 0; for (int i = 0; i < sz; i++) { if (pa == sa) { s[i] = b.s[pb++]; } else if (pb == sb) { s[i] = a.s[pa++]; } else { if (a.s[pa] < b.s[pb]) { s[i] = a.s[pa++]; } else { s[i] = b.s[pb++]; } } } } }; int b; vector<node> tree; public: const int n; segtree(int _n) : n(_n) { assert(_n > 0); for (b = 1; b < n; b <<= 1) ; tree.resize(b << 1); } void update(int ind, const vector<int> &v) { assert(0 <= ind && ind < n); tree[b + ind].add(v); for (int i = (b + ind) / 2; i > 0; i >>= 1) { tree[i].merge(tree[i * 2], tree[i * 2 + 1]); } } node get(int l, int r) const { assert(0 <= l && l <= r && r < n); l += b; r += b; if (l == r) { return tree[l]; } node left = tree[l]; node right = tree[r]; while (l + 1 < r) { if (l % 2 == 0) { left.merge(left, tree[l + 1]); } if (r % 2) { right.merge(tree[r - 1], right); } l /= 2; r /= 2; } node answer; answer.merge(left, right); return answer; } }; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, m, q; cin >> n >> m >> q; tree g(n); for (int i = 0; i < n - 1; i++) { int x, y; cin >> x >> y; x--; y--; g.add(x, y); } g.dfs_from(0); g.prec_kth_anc(); vector<vector<int>> inv(n); for (int i = 0; i < m; i++) { int c; cin >> c; c--; inv[c].push_back(i + 1); } for (int i = 0; i < n; i++) { if ((int)inv[i].size() > AMAX) { inv[i].resize(AMAX); } } struct query { int v, u, a, L; }; vector<query> queries; vector<vector<int>> qfrom(n); vector<vector<int>> ans(q); for (int i = 0; i < q; i++) { int v, u, a; cin >> v >> u >> a; v--; u--; queries.push_back({v, u, a, g.lca(v, u)}); qfrom[v].push_back(i); qfrom[u].push_back(i); } segtree st(n); stack<int> stck; for (int v : g.order) { while (stck.size() && stck.top() != g.pv[v]) { stck.pop(); } int curr = stck.size(); assert(curr == g.depth[v]); stck.push(v); st.update(curr, inv[v]); for (int qid : qfrom[v]) { const query &qr = queries[qid]; int from = g.depth[qr.L]; if (from < curr) { auto nd = st.get(from + 1, curr); for (int res : nd.s) { ans[qid].push_back(res); } } } } for (int i = 0; i < q; i++) { const query &qr = queries[i]; for (int res : inv[qr.L]) { ans[i].push_back(res); } sort(ans[i].begin(), ans[i].end()); int SZ = min(qr.a, (int)ans[i].size()); cout << SZ << " "; for (int j = 0; j < SZ; j++) { cout << ans[i][j] << " "; } cout << endl; } }
14
#include <bits/stdc++.h> using namespace std; int n, m; void solve(void); int main(void) { int t; scanf("%d", &t); while (t--) solve(); return 0; } void solve(void) { scanf("%d%d", &n, &m); if ((n * m) % 2) printf("%d\n", n * m / 2 + 1); else printf("%d\n", n * m / 2); }
0