solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> using namespace std; float PHI = 1.6180339; const float PI_F = 3.14159265358979f; long long modpow(long long x, long long n, long long m) { if (n == 0) return 1 % m; long long u = modpow(x, n / 2, m); u = (u * u) % m; if (n % 2 == 1) u = (u * x) % m; return u; } long long add(long long x, long long y, long long m) { x += y; if (x >= m) return x - m; return x; } long long sub(long long x, long long y) { x -= y; if (x < 0) return x + 1000000007; return x; } long long inv(long long a, long long m = 998244353) { return modpow(a, m - 2, m); } long long mul(long long a, long long b, long long m) { return (long long)a * b % m; } long long nCrModp(long long n, long long r, long long p) { long long C[r + 1]; memset(C, 0, sizeof(C)); C[0] = 1; for (long long i = 1; i <= n; i++) { for (long long j = min(i, r); j > 0; j--) C[j] = (C[j] + C[j - 1]) % p; } return C[r]; } bool sortinrev(const pair<long long, long long> &a, const pair<long long, long long> &b) { return (a.first > b.first); } vector<long long> z_function(string &s) { long long n = s.size(); vector<long long> z(n); for (long long i = 1, l = 0, r = 0; i < n; i++) { if (i <= r) z[i] = min(r - i + 1, z[i - l]); while (i + z[i] < n && s[z[i]] == s[i + z[i]]) z[i]++; if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1; } return z; } const long long maxn = 1e4; vector<long long> v[maxn + 10]; long long vis[maxn + 10]; vector<long long> ans[maxn + 10]; long long dp[maxn + 10]; void dfs(long long s, long long p) { vis[s] = 1; ans[p].push_back(s); for (long long i = 0; i < v[s].size(); i++) { if (vis[v[s][i]] == 0) { dfs(v[s][i], p); } } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(); cout.tie(); cout << fixed << setprecision(16); long long n, m, w; cin >> n >> m >> w; long long wt[n + 10]; long long bt[n + 10]; for (long long i = 1; i <= n; i++) { cin >> wt[i]; } for (long long i = 1; i <= n; i++) { cin >> bt[i]; } for (long long i = 0; i < m; i++) { long long x, y; cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } long long p = 0; for (long long i = 1; i <= n; i++) { if (vis[i] == 0) { dfs(i, p); ++p; } } vector<pair<long long, long long> > minwt; vector<pair<long long, long long> > maxbt; for (long long i = 0; i < p; i++) { for (long long j = w; j >= 1; j--) { long long fw = 0, fb = 0; for (long long k = 0; k < ans[i].size(); k++) { fw += wt[ans[i][k]]; fb += bt[ans[i][k]]; if (j >= wt[ans[i][k]]) { dp[j] = max(dp[j], dp[j - wt[ans[i][k]]] + bt[ans[i][k]]); } } if (j >= fw) { dp[j] = max(dp[j], dp[j - fw] + fb); } } } cout << dp[w] << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long int MOD = 1000000007; const long double pii = 3.141592653589793238; long long int power(long long int x, long long int y, long long int p) { long long int res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long long int max3(long long int a, long long int b, long long int c) { return max(max(a, b), c); } long long int min3(long long int a, long long int b, long long int c) { return min(min(a, b), c); } long long int nCr(long long int n, long long int r) { long long int x = n - r, y = 0, z = 1; while (y < r) { z = (z * (x + 1)) / (y + 1); x++; y++; } return z; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t, n, i; cin >> t; while (t--) { cin >> n; long long int c = 0, m = n; while (m) { c = c + (m & 1); m = m >> 1; } cout << 2 * n - c << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; template <typename T> T pow(T a, T b, long long int m) { T ans = 1; while (b > 0) { if (b % 2 == 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans % m; } template <typename T> void swap(T *a, T *b) { T temp = *a; *a = *b; *b = temp; return; } const long long int N = 1e5 + 2; long long int x[N], y[N], z[N]; void solve() { long long int temp = N; for (__typeof(temp) i = (0) - ((0) > (temp)); i != (temp) - ((0) > (temp)); i += 1 - 2 * ((0) > (temp))) { x[i] = 0; y[i] = 0; z[i] = 0; } string s; cin >> s; long long int len = s.length(); for (__typeof(len) i = (0) - ((0) > (len)); i != (len) - ((0) > (len)); i += 1 - 2 * ((0) > (len))) { if (s[i] == 'x') x[i + 1]++; else if (s[i] == 'y') y[i + 1]++; else z[i + 1]++; } long long int m; cin >> m; for (__typeof(len + 1) i = (1) - ((1) > (len + 1)); i != (len + 1) - ((1) > (len + 1)); i += 1 - 2 * ((1) > (len + 1))) { x[i] += x[i - 1]; y[i] += y[i - 1]; z[i] += z[i - 1]; } while (m--) { long long int l, r; cin >> l >> r; if (r - l + 1 <= 2) { cout << "YES" << '\n'; continue; } long long int a = x[r] - x[l - 1]; long long int b = y[r] - y[l - 1]; long long int c = z[r] - z[l - 1]; if (max({a, b, c}) - min({a, b, c}) <= 1) { cout << "YES\n"; } else cout << "NO\n"; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); ; solve(); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; vector<pair<string, string> > v; for (int i = 0; i < q; i++) { string a, b; cin >> a >> b; v.push_back(make_pair(a, b)); } vector<pair<string, string> > ans; for (int i = 0; i < (int)(v.size());) { string s = v[i].first; string ss = v[i].second; v[i].first = " "; v[i].second = " "; int j = 0; for (; j < (int)(v.size()); j++) { if (v[j].first == ss) { ss = v[j].second; v[j].first = " "; v[j].second = " "; } } if (s != " ") ans.push_back(make_pair(s, ss)); i++; } for (int i = 0; i < (int)(v.size()); i++) { if (v[i].first != " ") ans.push_back(make_pair(v[i].first, v[i].second)); } cout << (int)(ans.size()) << endl; for (int i = 0; i < (int)(ans.size()); i++) { cout << ans[i].first << " " << ans[i].second << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; inline int read() { register int s = 0, f = 0; register char ch = getchar(); while (!isdigit(ch)) f |= (ch == '-'), ch = getchar(); while (isdigit(ch)) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return f ? -s : s; } int n, m, w; const int MAX = 500015; int sum[MAX], a[MAX]; bool solve(int x) { int res = 0, b = m; for (register int i = 1; i <= n; i = -~i) { if (i >= w) res -= sum[i - w]; sum[i] = max((int)0, x - res - a[i]); res += sum[i]; b -= sum[i]; if (b < 0) return 0; } return 1; } signed main() { n = read(), m = read(), w = read(); int minn = (1 << 30); for (register int i = 1; i <= n; i = -~i) { a[i] = read(); minn = min(a[i], minn); } int maxx = minn + m; int mid = (minn + maxx) / 2 + 1; while (minn < maxx) { if (solve(mid)) minn = mid; else maxx = mid - 1; mid = (minn + maxx) / 2 + 1; } printf("%d", minn); return 0; }
4
#include <bits/stdc++.h> using namespace std; bool comparator(int a, int b) { return a > b; } long long pw(int x, int y) { long long res = 1; while (y) { if (y & 1) res *= x; y = (y >> 1); x *= x; } return res; } int main() { ; ios::sync_with_stdio(false); cin.tie(0); ; int t; cin >> t; while (t--) { long long n, k; cin >> n >> k; if (k < n) { cout << (k * (k + 1)) / 2 << endl; } else { long long ans = n - 1; long long fans = ((ans) * (ans + 1)) / 2; cout << fans + 1 << endl; } } }
2
#include <bits/stdc++.h> using namespace std; int a[1005][105]; int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { scanf("%d", &a[i][j]); } } printf("%d\n", m * (m - 1) / 2); if (k == 0) for (int i = 1; i <= m; i++) { for (int j = i + 1; j <= m; j++) { printf("%d %d\n", i, j); } } else { for (int i = 1; i <= m; i++) { for (int j = i + 1; j <= m; j++) { printf("%d %d\n", j, i); } } } return 0; }
3
#include <bits/stdc++.h> using namespace std; int arr[111]; int main() { int n; scanf("%d", &n); for (int i = (0); i < (n); ++i) scanf("%d", &arr[i]); sort(arr, arr + n); int t = 10; int ans = 0; int qts = 0; for (int i = (0); i < (n); ++i) { t += arr[i]; if (t > 720) break; qts++; if (t > 360) ans += (t - 360); } printf("%d %d\n", qts, ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; struct v { int x, y, s; } t, e; pair<int, int> p; set<pair<int, int> > S; queue<v> q; int d[8][2] = {1, 0, 1, 1, 0, 1, -1, 1, -1, 0, -1, -1, 0, -1, 1, -1}; int main() { int i, j, k, x1, y1, x2, y2, n, a, b, r; scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &n); while (n--) { scanf("%d%d%d", &r, &a, &b); for (i = a; i <= b; i++) S.insert(pair<int, int>(r, i)); } t.x = x1; t.y = y1; t.s = 0; q.push(t); S.erase(pair<int, int>(x1, y1)); while (!q.empty()) { t = q.front(); q.pop(); for (i = 0; i < 8; i++) { e.x = t.x + d[i][0]; e.y = t.y + d[i][1]; e.s = t.s + 1; if (e.x == x2 && e.y == y2) { printf("%d\n", e.s); return 0; } if (S.find(pair<int, int>(e.x, e.y)) != S.end()) { S.erase(pair<int, int>(e.x, e.y)); e.s = t.s + 1; q.push(e); } } } printf("-1\n"); return 0; }
5
#include <bits/stdc++.h> using namespace std; template <class T> struct PBTrie { char pos; T v; shared_ptr<PBTrie> next[2]; PBTrie(long long maxVal) { for (pos = -1; maxVal; ++pos, maxVal >>= 1) ; } PBTrie(const char pos) : pos(pos) {} void updateHelper(const long long i, const T v) { if (pos == -1) this->v = T(v, this->v); else { bool bit = (i >> pos) & 1; if (next[bit]) next[bit] = make_shared<PBTrie>(*next[bit]); else next[bit] = make_shared<PBTrie>((char)(pos - 1)); next[bit]->updateHelper(i, v); this->v = !next[!bit] ? next[bit]->v : T(next[0]->v, next[1]->v); } } shared_ptr<PBTrie> update(const long long i, const T v) const { auto dup = make_shared<PBTrie>(*this); dup->updateHelper(i, v); return dup; } T query(const long long l, const long long r, const long long lo, const long long hi) { if (l <= lo && hi <= r) return v; auto mid = (lo + hi) >> 1; T res; if (r > mid && next[1]) res = T(res, next[1]->query(l, r, mid + 1, hi)); if (l <= mid && next[0]) res = T(res, next[0]->query(l, r, lo, mid)); return res; } T query(const long long l, const long long r) { auto hi = (1ll << (pos + 1)) - 1; return (l <= r && l < hi && r >= 0) ? query(l, r, 0, hi) : T(); } }; struct PBTVal { long long v = 0; PBTVal() {} PBTVal(const long long v) : v(v) {} PBTVal(const PBTVal &v1, const PBTVal &v2) { v = v1 + v2; } operator long long() const { return v; } }; template <class T> struct IDGen { map<T, int> mp; bool hasID(const T x) { return mp.count(x); } int getID(T x) { auto it = mp.find(x); if (it != mp.end()) return it->second; int sz = mp.size(); mp[x] = sz; return sz; } }; struct MyStringTrie { const int MaxQ = 1e5; PBTrie<PBTVal> pbTrie; shared_ptr<IDGen<string>> idGenPtr; MyStringTrie() : pbTrie(MaxQ + 1ll) { idGenPtr = make_shared<IDGen<string>>(); } void updateHelper(const string a, PBTVal v) { pbTrie.updateHelper(idGenPtr->getID(a), v); } shared_ptr<MyStringTrie> update(const string a, PBTVal v) { auto dup = make_shared<MyStringTrie>(*this); dup->updateHelper(a, v); return dup; } int query(string a) { auto id = idGenPtr->getID(a); return pbTrie.query(id, id); } }; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int q; cin >> q; vector<pair<PBTrie<PBTVal>, MyStringTrie>> states = {{{(long long)1e9}, {}}}; states.reserve(q); for (int i = 1; i <= q; ++i) { string op; cin >> op; if (op == "undo") { int x; cin >> x; states.push_back(states[i - x - 1]); continue; } string a; cin >> a; states.push_back(states.back()); auto y = states.back().second.query(a); if (op == "query") { cout << (y ? (long long)states.back().first.query(0, y - 1) : -1ll) << endl; continue; } if (y) { states.back().first.updateHelper(y, -1); states.back().second.updateHelper(a, -y); } if (op == "set") { int x; cin >> x; states.back().first.updateHelper(x, 1); states.back().second.updateHelper(a, x); } } return 0; }
7
#include <bits/stdc++.h> using namespace std; int const MAX = 1e6 + 5; int n, m, cnt, tot, all; int outd[MAX], a[MAX], ans[MAX]; multiset<int> e[MAX]; void DFS(int x) { while (!e[x].empty()) { int i = *(e[x].begin()); e[x].erase(e[x].begin()); e[i].erase(e[i].find(x)); DFS(i); } ans[++all] = x; } int main() { int x, y; scanf("%d %d", &n, &m); while (m--) { scanf("%d %d", &x, &y); e[x].insert(y); e[y].insert(x); outd[x]++; outd[y]++; cnt++; } for (int i = 1; i <= n; i++) if (outd[i] % 2) a[++tot] = i; for (int i = 1; i < tot; i += 2) { e[a[i]].insert(a[i + 1]); e[a[i + 1]].insert(a[i]); cnt++; } if (cnt % 2) cnt++; printf("%d\n", cnt); all = 0; DFS(1); for (int i = 1; i < all; i++) { if (i % 2) printf("%d %d\n", ans[i], ans[i + 1]); else printf("%d %d\n", ans[i + 1], ans[i]); } if (!(all % 2)) printf("1 1\n"); }
9
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { int n, s; cin >> n >> s; s *= 100; int max = -1; while (n--) { int x, y; cin >> x >> y; int amount = x * 100 + y; int temp = s / amount; if (temp) { amount %= 100; amount = 100 - amount; amount %= 100; if (amount > max) max = amount; } } cout << max << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int ans; string a, b; int main() { cin >> a >> b; for (int i = 0; i + b.length() <= a.length(); ++i) { if (a.substr(i, b.length()) == b) { ++ans; a[i + b.length() - 1] = '#'; } } cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, m, q, i, j, ans = 0; ; int b[505]; int a[505][505]; cin >> n >> m >> q; for (i = 0; i < n; i++) { int c = 0, maxr = 0; ; for (j = 0; j < m; j++) { cin >> a[i][j]; if (a[i][j] == 1) c++; else { maxr = max(c, maxr); c = 0; } } maxr = max(c, maxr); b[i] = maxr; ans = max(ans, b[i]); } int l, r; while (q--) { cin >> l >> r; a[l - 1][r - 1] = 1 - a[l - 1][r - 1]; int c = 0, maxr = 0; ; for (i = 0; i < m; i++) { if (a[l - 1][i] == 1) c++; else { maxr = max(c, maxr); c = 0; } } maxr = max(maxr, c); b[l - 1] = maxr; ans = 0; for (i = 0; i < n; i++) ans = max(ans, b[i]); cout << ans << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, m; int a[N], b[N]; vector<int> v[N]; vector<int> g[N]; vector<int> who[N]; int cur; stack<int> s; int start[N]; int iiiii; vector<int> arr; bool mark[N]; int col[N]; int subtree[N]; void dfs1(int node, int parent) { subtree[node] = 1; for (int next : g[node]) { if (next == parent) { continue; } if (next == node) { continue; } if (mark[next]) { continue; } dfs1(next, node); subtree[node] += subtree[next]; } } int find(int node, int parent, int val) { for (int next : g[node]) { if (next == parent) { continue; } if (next == node) { continue; } if (mark[next]) { continue; } if (subtree[next] > val) { return find(next, node, val); } } return node; } void dfs(int node, int level) { dfs1(node, 0); int cent = find(node, 0, subtree[node] >> 1); col[cent] = level; mark[cent] = 1; for (int next : g[cent]) { if (mark[next]) { continue; } dfs(next, level + 1); } } bool cmp(int a, int b) { if (a > iiiii && b > iiiii) { return a < b; } else if (a > iiiii && b < iiiii) { return true; } else if (b > iiiii && a < iiiii) { return false; } else { return a < b; } } bool cmp2(int a, int b) { int x = who[a].size(); int y = who[b].size(); for (int i = 0; i < min(x, y); ++i) { if (who[b][i] > who[a][i]) { return 1; } if (who[a][i] > who[b][i]) { return 0; } } return x < y; } int main() { scanf("%d %d", &n, &m); for (int i = 1; i <= n; ++i) { v[i].clear(); who[i].clear(); g[i].clear(); start[i] = -1; } cur = 0; for (int i = 1; i <= m; ++i) { scanf("%d %d", a + i, b + i); if (a[i] > b[i]) { swap(a[i], b[i]); } v[a[i]].emplace_back(b[i]); v[b[i]].emplace_back(a[i]); } for (int i = 1; i <= n; ++i) { iiiii = i; sort(v[i].begin(), v[i].end(), cmp); reverse(v[i].begin(), v[i].end()); } while (!s.empty()) { s.pop(); } s.push(0); for (int i = 1; i <= n; ++i) { if (!v[i].empty()) { who[s.top()].emplace_back(i); for (int x : v[i]) { if (x == start[s.top()]) { int lol = s.top(); s.pop(); g[lol].emplace_back(s.top()); g[s.top()].emplace_back(lol); who[s.top()].emplace_back(i); } else { ++cur; g[cur].emplace_back(s.top()); g[s.top()].emplace_back(s.top()); s.push(cur); who[cur].emplace_back(i); start[cur] = i; } } } else { who[s.top()].emplace_back(i); } } for (int i = 1; i <= n && s.top() != 0; ++i) { if (!v[i].empty()) { who[s.top()].emplace_back(i); for (int x : v[i]) { if (x == start[s.top()]) { int lol = s.top(); s.pop(); g[lol].emplace_back(s.top()); g[s.top()].emplace_back(lol); who[s.top()].emplace_back(i); } } } else { who[s.top()].emplace_back(i); } } for (int i = 0; i <= cur; ++i) { sort(who[i].begin(), who[i].end()); who[i].resize(unique(who[i].begin(), who[i].end()) - who[i].begin()); reverse(who[i].begin(), who[i].end()); sort(g[i].begin(), g[i].end()); g[i].resize(unique(g[i].begin(), g[i].end()) - g[i].begin()); } arr.clear(); for (int i = 0; i <= cur; ++i) { arr.emplace_back(i); } sort(arr.begin(), arr.end(), cmp2); for (int i = 1; i <= n; ++i) { mark[i] = 0; col[i] = 0; } dfs(0, 1); for (int i = 0; i <= cur; ++i) { printf("%d%c", col[arr[i]], " \n"[i == n]); } }
10
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("sse4") using namespace std; template <typename T> void etch(T V) { for (auto x : V) cout << x << ' '; cout << '\n'; } vector<string> vec_splitter(string s) { s += ','; vector<string> res; while (!s.empty()) { res.push_back(s.substr(0, s.find(','))); s = s.substr(s.find(',') + 1); } return res; } void debug_out(vector<string> __attribute__((unused)) args, __attribute__((unused)) long long int idx, __attribute__((unused)) long long int LINE_NUM) { cerr << endl; } template <typename Head, typename... Tail> void debug_out(vector<string> args, long long int idx, long long int LINE_NUM, Head H, Tail... T) { if (idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, LINE_NUM, T...); } const long long int MOD = 1e9 + 7; const long long int MOD1 = 998244353; const long long int N = 2e5 + 5; const long long int INF = 1000111000111000111LL; const long double EPS = 1e-12; const long double PI = 3.141592653589793116; void impossible() { cout << "IMPOSSIBLE"; exit(0); } int32_t main() { std::ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); long long int n; cin >> n; vector<long long int> L(n), R(n); for (long long int i = 0; i < n; i++) { cin >> L[i] >> R[i]; } string ans = ""; for (long long int i = n - 1; i >= 0; i--) { long long int cntr = 0; vector<long long int> possibilities; possibilities.push_back(1); long long int in_btw = 0; for (auto ch : ans) { in_btw++; if (ch == '(') { cntr++; } else { cntr--; if (!cntr) { possibilities.push_back(in_btw + 1); } } } long long int idx = -1; for (auto p : possibilities) { if (p >= L[i] && p <= R[i]) { idx = p; break; } } if (idx == -1) impossible(); string new_ans = "(" + ans.substr(0, idx - 1) + ")" + ans.substr(idx - 1); ans = new_ans; } cout << ans; return 0; }
7
#include <bits/stdc++.h> using namespace std; inline int read() { int first = 0, f = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) { if (ch == '-') f = -1; } for (; isdigit(ch); ch = getchar()) { first = first * 10 + ch - 48; } return first * f; } const int mxN = 1000; struct Matrix { int a[2][2]; Matrix() { memset(a, 0, sizeof(a)); } Matrix operator*(const Matrix &arg) const { Matrix ret; for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) for (int k = 0; k < 2; k++) { ret.a[i][j] ^= (a[i][k] & arg.a[k][j]); } return ret; } } I, A[4]; Matrix a[mxN + 3]; int n, m; int decode(char first) { return first == 'R' ? 1 : (first == 'Y' ? 2 : (first == 'B' ? 3 : 0)); } namespace Gauss { bitset<mxN * 2 + 3> a[mxN * 2 + 3]; int pos[mxN * 2 + 3], sol[mxN * 2 + 3]; int n, m; bool solve() { for (int i = 1, j = 1; i <= m && j <= n; i++, j++) { while (j <= n) { bool found = false; for (int k = i; k <= m; k++) if (a[k][j]) { swap(a[i], a[k]); found = true; break; } if (found) break; j++; } if (j > n) { break; } pos[i] = j; for (int k = i + 1; k <= m; k++) { if (a[k][j]) { a[k] ^= a[i]; } } } for (int i = m; i >= 1; i--) if (pos[i]) { int tmp = a[i][n + 1]; for (int j = pos[i] + 1; j <= n; j++) { tmp ^= (a[i][j] & sol[j]); } sol[pos[i]] = tmp; } for (int i = 1; i <= m; i++) { int tmp = a[i][n + 1]; for (int j = pos[i]; j <= n; j++) { tmp ^= (a[i][j] & sol[j]); } if (tmp) { return false; } } return true; } } // namespace Gauss int main() { I.a[0][0] = I.a[1][1] = A[3].a[0][1] = A[3].a[1][0] = A[1].a[0][0] = A[1].a[0][1] = A[1].a[1][1] = A[2].a[0][0] = A[2].a[1][0] = A[2].a[1][1] = 1; n = read(), m = read(); for (int i = 1; i <= n; i++) a[i] = I; Gauss::n = n * 2; for (int i = 1; i <= m; i++) { char opt[7]; scanf("%s", opt); if (opt[0] == 'm') { Gauss::m += 2; int cnt = read(); while (cnt--) { int first = read(); Gauss::a[Gauss::m - 1][2 * first - 1] = a[first].a[0][0]; Gauss::a[Gauss::m - 1][2 * first] = a[first].a[0][1]; Gauss::a[Gauss::m][2 * first - 1] = a[first].a[1][0]; Gauss::a[Gauss::m][2 * first] = a[first].a[1][1]; } scanf("%s", opt); int c = decode(opt[0]); Gauss::a[Gauss::m - 1][n + n + 1] = c & 1; Gauss::a[Gauss::m][n + n + 1] = c >> 1; } else { int c = decode(opt[0]) ^ decode(opt[1]), cnt = read(); while (cnt--) { int first = read(); a[first] = A[c] * a[first]; } } } if (!Gauss::solve()) { puts("NO"); } else { puts("YES"); for (int i = 1; i <= n; i++) { int first = (Gauss::sol[2 * i] << 1) | Gauss::sol[2 * i - 1]; printf("%c", first == 0 ? '.' : (first == 1 ? 'R' : (first == 2 ? 'Y' : 'B'))); } puts(""); } return 0; }
12
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1e4 + 7; pair<int, int> p[MAX_N]; set<pair<int, int> > ans; void daq(int l, int r) { if (r - l == 1) { return; } int mid = (r + l) / 2; daq(l, mid); daq(mid, r); int x = p[mid].first; for (int i = l; i < r; ++i) { ans.insert({x, p[i].second}); } } int main() { int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> p[i].first >> p[i].second; ans.insert(p[i]); } sort(p, p + n); daq(0, n); cout << ((int)(ans).size()) << '\n'; for (auto i = ans.begin(); i != ans.end(); ++i) { cout << i->first << ' ' << i->second << '\n'; } return 0; }
7
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x; } inline void write(int num) { if (num == 0) { putchar('0'); return; } int t[8], cnt = 0; while (num) t[++cnt] = num % 10, num /= 10; for (int i = cnt; i >= 1; i--) putchar('0' + t[i]); } inline int max(int a, int b) { return a > b ? a : b; } inline int min(int a, int b) { return a < b ? a : b; } char s[1000005]; bool vis[1000005][2], del[1000005]; int p[1000005][10], cnt = 0, maxn = 0; int num[10], f[1000005][2]; int main() { int n = read(), q = read(), l, r; scanf("%s", s + 1); while (maxn < n) { int pos = maxn + 1, sta = maxn + 1, dir = 0, las = 0, lp = -1; while (pos >= sta && pos <= n) { maxn = max(maxn, pos); if (!vis[pos][dir]) { f[pos][dir] = ++cnt; vis[pos][dir] = 1; memcpy(p[cnt], num, sizeof(num)); } if (del[pos]) { pos += (dir ? -1 : 1); continue; } if (las && (s[pos] == '<' || s[pos] == '>')) { del[lp] = 1; } if (s[pos] == '>') { lp = pos; pos++; dir = 0; las = 1; continue; } if (s[pos] == '<') { lp = pos; pos--; dir = 1; las = 1; continue; } las = 0; num[s[pos] - '0']++; s[pos]--; if (s[pos] < '0') del[pos] = 1; pos += (dir ? -1 : 1); } if (!vis[pos][dir]) { f[pos][dir] = ++cnt; vis[pos][dir] = 1; memcpy(p[cnt], num, sizeof(num)); } } while (q--) { l = read(); r = read(); int st = f[l][0], ed = (r < n ? f[r + 1][0] : cnt); if (vis[l - 1][1]) ed = min(ed, f[l - 1][1]); for (int i = 0; i <= 9; i++) write(p[ed][i] - p[st][i]), putchar(' '); putchar('\n'); } return 0; }
10
#include <bits/stdc++.h> using namespace std; int n, a[(long long)2e5 + 10], l[(long long)2e5 + 10], r[(long long)2e5 + 10], ans[(long long)2e5 + 10]; int main() { cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; stack<int> s; for (int i = 0; i < n; ++i) { while (!s.empty() && a[s.top()] >= a[i]) s.pop(); if (s.empty()) l[i] = -1; else l[i] = s.top(); s.push(i); } while (!s.empty()) s.pop(); for (int i = n - 1; i >= 0; --i) { while (!s.empty() && a[s.top()] >= a[i]) s.pop(); if (s.empty()) r[i] = n; else r[i] = s.top(); s.push(i); } for (int i = 0; i < n; ++i) ans[r[i] - l[i] - 1] = max(ans[r[i] - l[i] - 1], a[i]); for (int i = n - 1; i >= 0; --i) ans[i] = max(ans[i], ans[i + 1]); for (int i = 1; i <= n; ++i) cout << ans[i] << " "; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int time_hrs, time_mins, curr_hrs, curr_mins; scanf("%d:%d", &curr_hrs, &curr_mins); scanf("%d:%d", &time_hrs, &time_mins); curr_hrs -= time_hrs; curr_mins -= time_mins; if (curr_mins < 0) { curr_hrs--; curr_mins = -1 * curr_mins; curr_mins = 60 - curr_mins; } if (curr_hrs < 0) { curr_hrs *= -1; curr_hrs %= 24; curr_hrs = 24 - curr_hrs; } if (curr_hrs == 24) curr_hrs = 0; printf("%02d:%02d", curr_hrs, curr_mins); }
0
#include <bits/stdc++.h> using namespace std; const int mo = 1e9 + 7; const int N = 55; int f[2][2 * N][N]; int g[2][2][2 * N][N][N]; int e[N][N], st[N * N], ed[N * N], n, m; int v1[2 * N][N][N], v2[2 * N][N][N]; vector<int> seq[N * N]; pair<int, int> walk(vector<int> &vec, int x, int flg) { int p = 0; for (; p + 1 <= vec.size();) { int y = vec[p], ed = e[x][y]; if (!ed || vec.size() > 2 * n || ((ed > m) ^ flg)) return pair<int, int>(23333, -1); for (auto j : seq[ed]) vec.push_back(j); x = y; ++p; } return pair<int, int>(vec.size(), x); } void work(bool flg) { memset(v1, 0, sizeof(v1)); memset(v2, 0, sizeof(v2)); for (int i = (int)(1); i <= (int)(2 * m); i++) if ((i <= m) ^ flg) for (int j = (int)(0); j <= (int)(seq[i].size() - 1); j++) if (seq[i][j] == st[i]) { int sz = seq[i].size(); vector<int> t1, t2; for (int k = (int)(j - 1); k >= (int)(0); k--) t1.push_back(seq[i][k]); pair<int, int> res1 = walk(t1, st[i], flg ^ 1); if (res1.first > 2 * n) continue; if (j + 1 == sz) ++v2[res1.first + 1][res1.second][ed[i]]; else if (seq[i][j + 1] == ed[i]) { for (int k = (int)(j + 2); k <= (int)(sz - 1); k++) t2.push_back(seq[i][k]); pair<int, int> res2 = walk(t2, ed[i], flg); if (res2.first + res1.first + 1 <= 2 * n) ++v1[res1.first + res2.first + 1][res1.second][res2.second]; } } } void UPD(int &x, int y) { (x += y) >= mo ? x -= mo : 233; } int main() { scanf("%d%d", &n, &m); for (int i = (int)(1); i <= (int)(m); i++) { int x, y, cnt, v; scanf("%d%d%d", &x, &y, &cnt); e[x][y] = i; e[y][x] = i + m; st[i] = ed[i + m] = x; ed[i] = st[i + m] = y; for (int j = (int)(1); j <= (int)(cnt); j++) { scanf("%d", &v); seq[i].push_back(v); } seq[i + m] = seq[i]; reverse(seq[i + m].begin(), seq[i + m].end()); if (!cnt) ++g[0][0][1][x][y]; } work(0); memcpy(g[1][1], v1, sizeof(v1)); memcpy(g[1][0], v2, sizeof(v2)); work(1); for (int l = (int)(1); l <= (int)(n * 2); l++) for (int x = (int)(1); x <= (int)(n); x++) for (int y = (int)(1); y <= (int)(n); y++) g[0][1][l][x][y] = v2[l][y][x]; for (int l = (int)(1); l <= (int)(n * 2); l++) for (int x = (int)(1); x <= (int)(n); x++) for (int y = (int)(1); y <= (int)(n); y++) for (int fl = (int)(0); fl <= (int)(1); fl++) UPD(f[fl][l][y], g[1][fl][l][x][y]); for (int l = (int)(1); l <= (int)(n * 2); l++) { int ans = 0; for (int i = (int)(1); i <= (int)(n); i++) { UPD(ans, f[1][l][i]); for (int la = (int)(0); la <= (int)(1); la++) for (int no = (int)(0); no <= (int)(1); no++) for (int j = (int)(0); j <= (int)(2 * n - l); j++) for (int k = (int)(1); k <= (int)(n); k++) UPD(f[no][l + j][k], 1ll * f[la][l][i] * g[la ^ 1][no][j][i][k] % mo); } printf("%d\n", ans); } }
11
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s; cin >> s; string t; cin >> t; long long n = s.length(); long long m = t.length(); long long pre[n + 5]; memset(pre, 0, sizeof(pre)); pre[0] = (s[0] == t[0]); long long j = pre[0]; for (long long i = 1; i < n; i++) { if (j < m and s[i] == t[j]) { pre[i] = 1; j++; } pre[i] += pre[i - 1]; } long long suf[n + 5]; memset(suf, 0, sizeof(suf)); suf[n - 1] = (s[n - 1] == t[m - 1]); j = m - 1 - suf[n - 1]; for (long long i = n - 2; i >= 0; i--) { if (j >= 0 and s[i] == t[j]) { suf[i] = 1; j--; } suf[i] += suf[i + 1]; } long long lo = 0; long long ans = 0; long long hi = n; while (lo <= hi) { long long mid = (lo + hi) / 2; bool flg = false; for (long long i = 0; i <= n - mid; i++) { long long j = i + mid - 1; long long Ans = 0; if (i > 0) { Ans += pre[i - 1]; } if (j < n - 1) { Ans += suf[j + 1]; } if (Ans >= m) { flg = true; } } if (flg) { ans = max(ans, mid); lo = mid + 1; } else { hi = mid - 1; } } cout << ans; }
4
#include <bits/stdc++.h> using namespace std; int F[100005], pF[100005]; int Fast(int x, int k) { int res = 1; while (k) { if (k & 1) res = 1ll * res * x % 1000000007; x = 1ll * x * x % 1000000007; k >>= 1; } return res; } int C(long long n, long long m) { if (n < 0 || m < 0) return 0; if (n < m) return 0; if (n == 0 || m == 0) return 1; return 1ll * F[n] * pF[m] % 1000000007 * pF[n - m] % 1000000007; } int f, w, h; int p, q; int main() { scanf("%d %d %d", &f, &w, &h); if ((f == 0 && w > h) || w == 0) { cout << 1 << endl; return 0; } F[0] = pF[0] = 1; for (int i = 1; i < 100005; i++) F[i] = 1ll * F[i - 1] * i % 1000000007; for (int i = 1; i < 100005; i++) pF[i] = Fast(F[i], 1000000007 - 2); for (int i = 1; i <= f; i++) { int bas = C(f - 1, i - 1); for (int j = -1; j <= 1; j++) { int ww = i + j; if (ww < 1 || ww > w) continue; int v = bas; if (j == 0) v = (v + v) % 1000000007; q = (q + 1ll * v * C(w - 1, ww - 1) % 1000000007) % 1000000007; p = (p + 1ll * v * C(w - 1ll * ww * h - 1, ww - 1) % 1000000007) % 1000000007; } } cout << 1ll * p * Fast(q, 1000000007 - 2) % 1000000007 << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; static const int N = 30001; int m[500][N]; int n, d; int v[N]; int h[3] = {-1, 0, 1}; int f(int pos, int dif) { if (m[250 + dif][pos] == -1) { m[250 + dif][pos] = 0; for (int i = 0; i < 3; i++) if ((pos + d + dif + h[i] < N) && (d + dif + h[i] > 0) && m[250 + dif][pos] < f(pos + d + dif + h[i], dif + h[i])) m[250 + dif][pos] = f(pos + d + dif + h[i], dif + h[i]); m[250 + dif][pos] += v[pos]; } return m[250 + dif][pos]; } int main() { int p; scanf("%i", &n); scanf("%i", &d); for (int i = 0; i < n; i++) { scanf("%i", &p); v[p]++; } for (int i = 0; i < 500; i++) for (int j = 0; j < N; j++) m[i][j] = -1; printf("%i\n", v[0] + f(d, 0)); return 0; }
5
#include <bits/stdc++.h> using namespace std; int a[200005], n, k; long long ans; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sort(a + 1, a + n + 1); for (int i = 1; i <= n / 2; i++) if (a[i] > k) ans += a[i] - k; for (int i = n / 2 + 2; i <= n; i++) if (a[i] < k) ans += k - a[i]; printf("%lld\n", ans + abs(k - a[n / 2 + 1])); }
2
#include <bits/stdc++.h> using namespace std; long long MOD = 1000000007; int main() { double l, p, q; cin >> l >> p >> q; cout << p * l / (p + q) << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; double n; int l, d, j; double suma = 0.0; typedef struct niz { int vrednost; double pozicija; } niz; niz a[100001], c[100001]; void merge_count1(int x, int y) { int m; m = ((x + y) / 2); if (x < y) { merge_count1(x, m); merge_count1(m + 1, y); l = x; d = m + 1; j = x; double suma_indeksa = 0.0; double broj_indeksa = 0.0; while (l <= m && d <= y) { if (a[l].vrednost < a[d].vrednost) { c[j].vrednost = a[l].vrednost; c[j].pozicija = a[l].pozicija; suma_indeksa += a[l].pozicija; broj_indeksa = broj_indeksa + 1.0; j++; l++; } else { c[j].vrednost = a[d].vrednost; c[j].pozicija = a[d].pozicija; suma += (broj_indeksa * n * (n + 1.0) / 2.0); suma -= (suma_indeksa * (n - a[d].pozicija + 1.0)); j++; d++; } } while (d <= y) { c[j].vrednost = a[d].vrednost; c[j].pozicija = a[d].pozicija; suma += (broj_indeksa * n * (n + 1.0) / 2.0); suma -= (suma_indeksa * (n - a[d].pozicija + 1.0)); j++; d++; } while (l <= m) { c[j].vrednost = a[l].vrednost; c[j].pozicija = a[l].pozicija; j++; l++; } for (int i = x; i <= y; i++) { a[i].vrednost = c[i].vrednost; a[i].pozicija = c[i].pozicija; } } } int main() { scanf("%lf", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i].vrednost); a[i].vrednost = ((int)n) - a[i].vrednost + 1; a[i].pozicija = (double)i; } for (double i = 1.0; i <= n; i++) suma += (i * (i - 1.0) * (n - i + 1.0) / 4); merge_count1(1, (int)n); suma /= (n * (n + 1.0) / 2.0); printf("%.12lf", suma); }
8
#include <bits/stdc++.h> using namespace std; const int maxn = 111111; vector<int> vec[66]; int n, cnt, prime[66], gen[66], arr[111], brr[111], dp[111][1 << 17]; bool isGood(int num, int mask) { for (int i = 0; i < vec[num].size(); ++i) { if (mask & (1 << prime[vec[num][i]])) return false; } return true; } void foo() { for (int i = 2; i < 60; ++i) { bool isPrime = true; for (int j = 2; j * j <= i; ++j) { if (i % j == 0) isPrime = false; } if (isPrime) prime[i] = cnt++; } for (int i = 2; i < 60; ++i) { for (int j = 2; j < 60; ++j) { if (prime[j] != -1 && i % j == 0) vec[i].push_back(j), gen[i] |= (1 << prime[j]); } } } int solve(int idx, int mask) { if (idx == n) return dp[idx][mask] = 0; if (dp[idx][mask] != -1) return dp[idx][mask]; int ret = INT_MAX; for (int i = 1; i < 60; ++i) { if (isGood(i, mask)) ret = min(ret, abs(arr[idx] - i) + solve(idx + 1, mask | gen[i])); } return dp[idx][mask] = ret; } void go(int mn, int idx, int mask) { for (int i = 1; i < 60; ++i) { if (isGood(i, mask) && dp[idx + 1][mask | gen[i]] == mn - abs(arr[idx] - i)) { brr[idx] = i; return go(mn - abs(arr[idx] - i), idx + 1, mask | gen[i]); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); memset(dp, -1, sizeof(dp)); memset(prime, -1, sizeof(prime)); cin >> n; for (int i = 0; i < n; ++i) cin >> arr[i]; foo(); solve(0, 0); go(dp[0][0], 0, 0); for (int i = 0; i < n; ++i) cout << brr[i] << " "; cout << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; int n, k; int main() { cin >> n >> k; vector<int> g; for (int i = 0; i < n; i++) { g.push_back((6 * i + 1) * k); g.push_back((6 * i + 2) * k); g.push_back((6 * i + 3) * k); g.push_back((6 * i + 5) * k); } cout << g[g.size() - 1] << endl; for (int i = 0; i < g.size(); i += 4) { for (int j = 0; j < 4; j++) printf("%d%c", g[i + j], j == 3 ? '\n' : ' '); } return 0; }
5
#include<bits/stdc++.h> using namespace std; #define ll long long //#define ll int #define pb push_back #define binabreakwalabreak continue #define lb lower_bound #define ub upper_bound #define pu push #define db double #define m_p make_pair ll mod = 1e9 + 7; void clear( std::queue<char> &q ) { std::queue<char> empty; std::swap( q, empty ); } // sort vector pair by second element bool sortbysec(const pair<int,int> &a,const pair<int,int> &b) { return (a.second < b.second); } bool is_prime(ll n) { for(ll i=2;i*i<=n;i++) { if(n%i==0) { // inde=i; return false; } } return true; } ll bpw(ll a , ll b) { ll res = 1; while(b) { if(b & 1) res = 1LL * res * a % mod; a = 1LL * a * a % mod; b >>= 1; } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll t; t=1; cin>>t; ll t1=t; while(t1--) { ///your code goes here///// ll n,m; cin>>n; cin>>m; ll v[n][m]; //string s; char c; for(ll i=0;i<n;i++) { for(ll j=0;j<m;j++) { cin>>c; if(c=='*') v[i][j]=1; else v[i][j]=0; } } ll count=0; /*for(ll i=0;i<n;i++) { //string str=v[i]; for(ll j=0;j<m;j++) { if(v[i][j]=='*') v[i][j]=1; else v[i][j]=0; } }*/ for(ll i=n-2;i>=0;i--) { for(ll j=m-2;j>0;j--) { if(v[i][j]>0) { v[i][j]+=min(v[i+1][j],min(v[i+1][j-1],v[i+1][j+1])); } } } for(ll i=0;i<n;i++) { for(ll j=0;j<m;j++) count+=v[i][j]; } cout<<count<<endl; ///code end here///////////// } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, v, ans = 0, tank = 0; cin >> n >> v; int need = n - 1; if (need <= v) { cout << need; return 0; } ans += v; need -= v; need++; ans += need * (need + 1) / 2; ans--; cout << ans; }
0
#include <bits/stdc++.h> using namespace std; const int N = 100000 + 5, M = 100000 + 5; const bool T_BUS = false, T_PERSON = true; const int EVENT = N + M; const int SIZELS = EVENT * 2; int n, m; struct Event { bool type; int id; int l, r, t; inline bool operator<(const Event &b) const { if (r != b.r) return r > b.r; return type == T_BUS && b.type == T_PERSON; } }; Event event[EVENT]; int ans[M]; int ls[SIZELS], cnt_ls; map<int, int> bit[SIZELS]; inline void b_insert(int p, int t, int id) { for (; p <= cnt_ls; p += (p & -p)) { bit[p][t] = id; } } inline int b_query(int p, int t) { int ret = 0x3f3f3f3f, retid = -1; for (; p; p ^= (p & -p)) { map<int, int>::iterator it = bit[p].lower_bound(t); if (it == bit[p].end()) continue; if (it->first < ret) { ret = it->first; retid = it->second; } } return retid; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) { event[i].type = T_BUS; event[i].id = i; scanf("%d%d%d", &event[i].l, &event[i].r, &event[i].t); } for (int i = n + 1; i <= n + m; ++i) { event[i].type = T_PERSON; event[i].id = i - n; scanf("%d%d%d", &event[i].l, &event[i].r, &event[i].t); } for (int i = 1; i <= n + m; ++i) { ls[++cnt_ls] = event[i].l; ls[++cnt_ls] = event[i].r; } sort(ls + 1, ls + cnt_ls + 1); cnt_ls = unique(ls + 1, ls + cnt_ls + 1) - ls - 1; for (int i = 1; i <= n + m; ++i) { event[i].l = lower_bound(ls + 1, ls + cnt_ls + 1, event[i].l) - ls; event[i].r = lower_bound(ls + 1, ls + cnt_ls + 1, event[i].r) - ls; } sort(event + 1, event + n + m + 1); for (int i = 1; i <= n + m; ++i) { if (event[i].type == T_BUS) { b_insert(event[i].l, event[i].t, event[i].id); } else { ans[event[i].id] = b_query(event[i].l, event[i].t); } } for (int i = 1; i <= m; ++i) printf("%d ", ans[i]); putchar('\n'); return 0; }
8
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") using namespace std; const int MOD = 1000000007; bool ispalindrome(string &s) { for (int i = 0; i < (int)s.size() / 2; i++) { if (s[i] == s[(s.size() - i - 1)]) continue; else return false; } return 1; } int main(int argc, char *argv[]) { string s; cin >> s; if (ispalindrome(s)) { cout << 0 << '\n'; } else { cout << 3 << '\n'; cout << 'R' << " " << s.size() - 1 << '\n'; cout << 'L' << " " << s.size() << '\n'; cout << 'L' << " " << 2 << '\n'; } }
3
#include <bits/stdc++.h> using namespace std; long long a, b, c, d[100009 * 2]; long long x, y; set<long long> s; map<long long, bool> mp; int main() { cin >> a >> b >> c; for (int i = 1; i <= a; i++) cin >> d[i]; long long z = 0; for (int i = 1; i <= c; i++) { cin >> x >> y; if (x == 1) { if (z < b) s.insert(d[y]), mp[d[y]] = 1, z++; else if (s.size() > 0 and d[y] > *s.begin()) mp[*s.begin()] = 0, s.erase(*s.begin()), s.insert(d[y]), mp[d[y]] = 1; } else { if (s.size() > 0 and mp[d[y]]) cout << "YES" << endl; else cout << "NO" << endl; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; vector<vector<int>> officers(2e5); vector<int> order; vector<pair<int, int>> range(2e5); void dfs(int i) { int st = order.size(); order.push_back(i); for (auto x : officers[i]) dfs(x); int en = order.size(); range[i] = make_pair(st, en - st); } int main() { int n, q, temp, temp1; cin >> n >> q; for (int i = 1; i < n; i++) { cin >> temp; officers[temp - 1].push_back(i); } dfs(0); for (int i = 0; i < q; i++) { cin >> temp >> temp1; if (range[temp - 1].second < temp1) cout << -1 << endl; else cout << order[range[temp - 1].first + temp1 - 1] + 1 << endl; } }
4
#include <bits/stdc++.h> using namespace std; long long dx[] = {-1, 1, 0, 0}; long long dy[] = {0, 0, -1, 1}; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n; cin >> n; vector<long long> l(n); vector<long long> r(n); for (auto &k : l) cin >> k; for (auto &k : r) cin >> k; vector<long long> ans(n); for (long long i = 0; i < n; ++i) { ans[i] = n - l[i] - r[i]; } for (long long i = 0; i < n; ++i) { long long p = 0, q = 0; for (long long j = 0; j < i; ++j) { if (ans[j] > ans[i]) p++; } for (long long j = i + 1; j < n; ++j) { if (ans[j] > ans[i]) q++; } if (p != l[i] or q != r[i]) { cout << "NO" << "\n"; return 0; } } cout << "YES" << "\n"; for (auto &k : ans) cout << k << " "; }
3
#include <bits/stdc++.h> using namespace std; const int N = 8; string c[N]; bool blocked[N + 1][N][N]; char stat[N + 1][N][N]; int main() { for (int i = 0; i < N; i++) { cin >> c[i]; } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { blocked[0][i][j] = (c[i][j] == 'S'); } } for (int i = 1; i <= N; i++) { for (int j = 0; j < N; j++) { for (int k = 0; k < N; k++) { if (j == 0) blocked[i][j][k] = false; else blocked[i][j][k] = blocked[i - 1][j - 1][k]; } } } for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { stat[N][i][j] = 'W'; } } for (int i = N - 1; i >= 0; i--) { for (int j = 0; j < N; j++) { for (int k = 0; k < N; k++) { stat[i][j][k] = 'L'; if (blocked[i][j][k]) continue; for (int h = 0; h < 5; h++) { for (int j2 = j - 1; j2 <= j + 1; j2++) { for (int k2 = k - 1; k2 <= k + 1; k2++) { if (j2 < 0 || j2 >= N || k2 < 0 || k2 >= N) continue; if (!blocked[i][j2][k2] && !blocked[i + 1][j2][k2]) { if (stat[i + 1][j2][k2] == 'W') stat[i][j][k] = 'W'; } } } } } } } if (stat[0][N - 1][0] == 'W') { cout << "WIN" << endl; } else { cout << "LOSE" << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int N = 5000; int sum[N][N]; int main() { int n, m, ans = 0; string s; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> s; int r = m; for (int j = m - 1; j >= 0; j--) if (s[j] == '1') sum[j][r - 1] += 1; else r = j; } for (int i = 0; i < m; i++) { int now = 0; for (int j = m - 1; j >= i; j--) { now += sum[i][j]; sum[i][j] = now; } } for (int i = 0; i < m; i++) for (int j = i; j < m; j++) ans = max(ans, (j - i + 1) * sum[i][j]); cout << ans; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MaxN = 2e5; long long t[4 * MaxN]; long long getSum(int v, int tl, int tr, int l, int r) { if (l > r) return 0; if (l == tl && r == tr) { return t[v]; } int mid = (tl + tr) / 2; return getSum(v * 2, tl, mid, l, min(r, mid)) + getSum(v * 2 + 1, mid + 1, tr, max(l, mid + 1), r); } void setVal(int v, int tl, int tr, int pos, long long val) { if (tl == tr) { t[v] = val; return; } int mid = (tl + tr) / 2; if (pos <= mid) setVal(v * 2, tl, mid, pos, val); else setVal(v * 2 + 1, mid + 1, tr, pos, val); t[v] = t[v * 2] + t[v * 2 + 1]; } int a[MaxN]; long long f(long long totalLen, long long ignoredLen) { return ignoredLen * (ignoredLen + 1) / 2 + ignoredLen * (totalLen - ignoredLen); } int main(int argc, const char* argv[]) { int n, q; cin >> n >> q; for (int i = 0; i < n; ++i) cin >> a[i]; set<pair<int, int>> segs; auto addSeg = [&](int l, int r) { segs.insert({l, r}); int len = r - l + 1; setVal(1, 0, n - 1, l, len * 1ll * (len + 1) / 2); }; auto removeSeg = [&](int l, int r) { segs.erase({l, r}); setVal(1, 0, n - 1, l, 0); }; int l = 0; for (int i = 0; i < n; ++i) { if (i > 0 && a[i] < a[i - 1]) { addSeg(l, i - 1); l = i; } } addSeg(l, n - 1); for (int qq = 0; qq < q; ++qq) { int type; cin >> type; if (type == 1) { int pos, val; cin >> pos >> val; pos--; auto it = segs.lower_bound({pos, n}); it--; int l = it->first; int r = it->second; a[pos] = val; if (pos > l && pos < r) { if (a[pos] < a[pos - 1]) { removeSeg(l, r); addSeg(l, pos - 1); addSeg(pos, r); } else if (a[pos] > a[pos + 1]) { removeSeg(l, r); addSeg(l, pos); addSeg(pos + 1, r); } } else { if (l == r) { if (l > 0 && a[pos] >= a[pos - 1]) { it--; int leftL = it->first; int leftR = it->second; removeSeg(leftL, leftR); removeSeg(l, l); addSeg(leftL, pos); } else if (r < n - 1 && a[pos] <= a[pos + 1]) { it++; int rightL = it->first; int rightR = it->second; removeSeg(l, l); removeSeg(rightL, rightR); addSeg(pos, rightR); } } else { if (pos == l) { if (l == 0) { if (a[l] > a[l + 1]) { removeSeg(l, r); addSeg(l + 1, r); addSeg(l, l); } } else { it--; int leftL = it->first; int leftR = it->second; if (a[l] >= a[l - 1] && a[l] <= a[l + 1]) { removeSeg(leftL, leftR); removeSeg(l, r); addSeg(leftL, r); } else if (a[l] >= a[l - 1]) { removeSeg(leftL, leftR); removeSeg(l, r); addSeg(leftL, l); addSeg(pos + 1, r); } else if (a[l] <= a[l + 1]) { } else { removeSeg(l, r); addSeg(l, l); addSeg(l + 1, r); } } } else { if (r == n - 1) { if (a[r] < a[r - 1]) { removeSeg(l, r); addSeg(l, r - 1); addSeg(r, r); } } else { it++; int rightL = it->first; int rightR = it->second; if (a[r] >= a[r - 1] && a[r] <= a[r + 1]) { removeSeg(rightL, rightR); removeSeg(l, r); addSeg(l, rightR); } else if (a[r] <= a[r + 1]) { removeSeg(rightL, rightR); removeSeg(l, r); addSeg(l, r - 1); addSeg(r, rightR); } else if (a[r] >= a[r - 1]) { } else { removeSeg(l, r); addSeg(r, r); addSeg(l, r - 1); } } } } } } else { int l, r; cin >> l >> r; l--; r--; auto it1 = segs.lower_bound({l, n}); it1--; auto it2 = segs.lower_bound({r, n}); it2--; int lLeft = it1->first; int rLeft = it1->second; int lRight = it2->first; int rRight = it2->second; long long totalSum = 0; if (lLeft != lRight) { totalSum = getSum(1, 0, n - 1, lLeft, lRight); totalSum -= f(rLeft - lLeft + 1, l - lLeft); totalSum -= f(rRight - lRight + 1, rRight - r); } else { totalSum = getSum(1, 0, n - 1, lLeft, lRight); totalSum -= f(rLeft - lLeft + 1, rLeft - lLeft + 1 - (r - l + 1)); } cout << totalSum << "\n"; } } return 0; }
7
#include <bits/stdc++.h> int n; long long solve(std::vector<int>& arr) { long long ans = 0; int i, j, cnt; std::vector<int> b; for (i = 0; i <= 24; i++) { b.clear(); for (j = 0; j < n; j++) b.push_back(arr[j] % (1 << (i + 1))); std::sort(b.begin(), b.end()); cnt = 0; for (j = 0; j < n; j++) { cnt += std::lower_bound(b.begin() + j + 1, b.end(), (1 << (i + 1)) - b[j]) - std::lower_bound(b.begin() + j + 1, b.end(), (1 << i) - b[j]); cnt += std::upper_bound(b.begin() + j + 1, b.end(), (1 << (i + 2)) - 2 - b[j]) - std::lower_bound(b.begin() + j + 1, b.end(), (1 << (i + 1)) + (1 << i) - b[j]); } if (cnt % 2) ans += (1ll << i); } return ans; } int main() { std::cin >> n; int i; std::vector<int> a(n); for (i = 0; i < n; i++) std::cin >> a[i]; std::cout << solve(a) << '\n'; }
6
#include <bits/stdc++.h> using namespace std; int n; const int maxn = 105; struct node { int l, r; void read() { cin >> l >> r; } } a[maxn]; int head[maxn], rear[maxn]; int tot; int main() { tot = 0; cin >> n; for (int i = 1; i <= n; i++) { scanf("%d%d", &a[i].l, &a[i].r); } for (int i = 1; i <= n; i++) { if (a[i].l == 0) { int j = i; while (a[j].r != 0) { j = a[j].r; } head[++tot] = i; rear[tot] = j; } } for (int i = 2; i <= tot; i++) { int u = rear[1], v = head[i]; a[u].r = v; a[v].l = u; rear[1] = rear[i]; } for (int i = 1; i <= n; i++) { printf("%d %d\n", a[i].l, a[i].r); } return 0; }
3
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int x[100010], y[100010], xx[100010], yy[100010], pd[2][2]; int n, m, midx, midy, mx, my; long long ans; int main() { n = read(); m = (n - 1) / 2; for (int i = 0; i <= n - 1; i++) { x[i] = read(); y[i] = read(); xx[i] = x[i], yy[i] = y[i]; } sort(x, x + n); mx = x[m + 1] - x[m]; sort(y, y + n); my = y[m + 1] - y[m]; if (n % 2) { mx = min(mx, x[m] - x[m - 1]); my = min(my, y[m] - y[m - 1]); } midx = x[m], midy = y[m]; bool flag = 0; for (int i = 0; i <= n - 1; i++) { ans += abs(x[i] - midx) + abs(y[i] - midy); pd[xx[i] <= midx][yy[i] <= midy] = 1; if (midx == xx[i] && midy == yy[i]) flag = 1; } if ((n % 2 == 0 || flag) && (pd[0][0] == pd[0][1])) ans = max(ans - mx, ans - my); printf("%I64d\n", ans * 2); return 0; }
11
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int n; cin >> n; int ans = 0; for (int i = 0; 1; ++i) { if (n < (i + 1) * (i + 1)) { if (n <= i * (i + 1)) { int left = n - i * i; ans = 4 * i; if (left) ans += 2; } else { int left = n - i * (i + 1); ans = 4 * i + 2; if (left) ans += 2; } break; } } cout << ans << "\n"; return 0; }
1
#include <bits/stdc++.h> using namespace std; bool comp(pair<int, int> a, pair<int, int> b) { return a.first < b.first; } int main() { int n, k, p; cin >> n >> k >> p; vector<int> tabo(0); vector<int> tabe(0); for (int i = int(0); i < int(n); ++i) { int tmp; cin >> tmp; if (tmp % 2 == 1) tabo.push_back(tmp); else tabe.push_back(tmp); } int cptodd = tabo.size(); int cpteven = tabe.size(); int rem = cptodd - (k - p); rem /= 2; if ((cptodd % 2 != (k - p) % 2) || (cptodd < k - p) || ((n - cptodd + rem) < p)) { cout << "NO" << endl; return 0; } cout << "YES" << endl; vector<vector<int> > oddtab(k - p, vector<int>(0)); vector<vector<int> > eventab(p, vector<int>(0)); int inde = 0, indo = 0; for (; indo < k - p; indo++) oddtab[indo].push_back(tabo[indo]); for (int i = int(0); i < int(p); ++i) { if (indo != cptodd) { eventab[i].push_back(tabo[indo++]); assert(indo != cptodd); eventab[i].push_back(tabo[indo++]); } else { assert(inde != cpteven); eventab[i].push_back(tabe[inde++]); } } vector<vector<int> > &tabref = ((p == 0) ? oddtab : eventab); for (; indo != cptodd; indo++) tabref[0].push_back(tabo[indo]); for (; inde != cpteven; inde++) tabref[0].push_back(tabe[inde]); for (int i = int(0); i < int(p); ++i) { cout << eventab[i].size() << " "; for (int j = int(0); j < int(eventab[i].size()); ++j) cout << eventab[i][j] << " "; cout << endl; } for (int i = int(0); i < int(k - p); ++i) { cout << oddtab[i].size() << " "; for (int j = int(0); j < int(oddtab[i].size()); ++j) cout << oddtab[i][j] << " "; cout << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int LIM = 307; int dp[LIM][LIM][LIM], T[3]; void solve() { int n; cin >> n; for (int i = 0; i < (n); ++i) cin >> T[i]; if (n == 3) { if ((T[0] ^ T[1] ^ T[2]) != 0) { cout << "BitLGM\n"; } else { cout << "BitAryo\n"; } return; } for (int a = 0; a < (T[0] + 1); ++a) for (int b = 0; b < (T[1] + 1); ++b) for (int c = 0; c < (T[2] + 1); ++c) { for (int i = 1; i <= a; ++i) if (!dp[a - i][b][c]) { dp[a][b][c] = 1; break; } if (dp[a][b][c]) continue; for (int i = 1; i <= b; ++i) if (!dp[a][b - i][c]) { dp[a][b][c] = 1; break; } if (dp[a][b][c]) continue; for (int i = 1; i <= c; ++i) if (!dp[a][b][c - i]) { dp[a][b][c] = 1; break; } if (dp[a][b][c]) continue; if (n == 2) { int p = min(a, b); for (int i = 1; i <= p; ++i) if (!dp[a - i][b - i][c]) { dp[a][b][c] = 1; break; } } else if (n == 3) { int p = min(min(a, b), c); for (int i = 1; i <= p; ++i) if (!dp[a - i][b - i][c - i]) { dp[a][b][c] = 1; break; } } } cout << (dp[T[0]][T[1]][T[2]] ? "BitLGM" : "BitAryo") << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int _ = 1; while (_--) solve(); }
6
#include <bits/stdc++.h> using namespace std; const int mod = 998244353, G = 3, Gi = 332748118; int n, m, m2, invm2, k; int fact[100010], inv[100010]; int nxt[100010], mi[100010], ss[100010]; int rev[270000], tot, bit; long long A[270000], B[270000]; long long ans; inline int read() { int x = 0, w = 0; char ch = 0; while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while (isdigit(ch)) { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } return w ? -x : x; } long long ksm(long long B, long long K, long long A = 1) { for (; K; K >>= 1, B = B * B % mod) if (K & 1) A = A * B % mod; return A; } void xxs() { nxt[0] = nxt[1] = 1; mi[1] = 1; mi[0] = 0; for (int i = 2; i <= 100000; i++) { if (!nxt[i]) mi[ss[++ss[0]] = nxt[i] = i] = ksm(i, k); for (int j = 1; i * ss[j] <= 100000; j++) { nxt[i * ss[j]] = ss[j]; mi[i * ss[j]] = 1ll * mi[i] * mi[ss[j]] % mod; if (i % ss[j] == 0) break; } } } void prepare() { fact[0] = 1; for (int i = 1; i <= 100000; i++) fact[i] = 1ll * fact[i - 1] * i % mod; inv[100000] = ksm(fact[100000], mod - 2); for (int i = 100000 - 1; i >= 0; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % mod; } long long C(long long N, long long M) { if (N < M || M < 0) return 0; return fact[N] * inv[M] % mod * inv[N - M] % mod; } void solve() { m2 = mod + 1 - m; invm2 = ksm(m2, mod - 2); long long mim = 1, mim2 = ksm(m2, n); for (int i = 0; i <= n; i++) { (ans += C(n, i) * mim % mod * mim2 % mod * mi[i] % mod) %= mod; mim = mim * m % mod; mim2 = mim2 * invm2 % mod; } cout << ans << '\n'; } void NTT(long long C[], int inv) { for (int i = 0; i < tot; i++) if (i < rev[i]) swap(C[i], C[rev[i]]); for (int mid = 1; mid < tot; mid <<= 1) { long long w1 = ksm(inv == 1 ? G : Gi, (mod - 1) / (mid << 1)); for (int i = 0; i < tot; i += (mid << 1)) { long long wk = 1; for (int j = 0; j < mid; j++, wk = wk * w1 % mod) { long long x = C[i + j], y = wk * C[i + j + mid] % mod; C[i + j] = (x + y) % mod; C[i + j + mid] = (x - y + mod) % mod; } } } if (inv == 1) return; inv = ksm(tot, mod - 2); for (int i = 0; i < tot; i++) C[i] = C[i] * inv % mod; } int main() { n = read(); m = ksm(read(), mod - 2); k = read(); if (n == 1) return printf("1\n"), 0; prepare(); xxs(); if (n <= k) return solve(), 0; for (int i = 0; i <= k; i++) A[i] = ksm(i, k) * inv[i] % mod; for (int i = 0; i <= k; i++) if (i & 1) B[i] = -inv[i]; else B[i] = inv[i]; while ((1 << bit) <= k + k) bit++; tot = 1 << bit; for (int i = 1; i < tot; i++) rev[i] = (rev[i >> 1] >> 1 | ((i & 1) << (bit - 1))); NTT(A, 1); NTT(B, 1); for (int i = 0; i < tot; i++) A[i] = A[i] * B[i] % mod; NTT(A, -1); long long p = m, xjm = 1; ans = A[0]; for (int i = 1; i <= k; i++) { (xjm *= n - i + 1) %= mod; (ans += A[i] % mod * p % mod * xjm % mod) %= mod; p = p * m % mod; } cout << (ans + mod) % mod << '\n'; }
9
#include <bits/stdc++.h> int main() { int n, k, a[1000], b[1000]; scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) scanf("%d", &a[i]); for (int i = 0; i < n; i++) scanf("%d", &b[i]); int flag, minn = 99999; while (k >= 0) { for (int i = 0; i < n; i++) { if (minn > b[i] / a[i]) { minn = b[i] / a[i]; flag = i; } } if (k < a[flag] - b[flag] % a[flag]) break; k = k - (a[flag] - b[flag] % a[flag]); b[flag] += a[flag] - b[flag] % a[flag]; minn = b[flag] / a[flag]; } printf("%d\n", minn); return 0; }
3
#include <bits/stdc++.h> using namespace std; int const INF = (int)1e9 + 1e3; long long const INFL = (long long)1e18 + 1e6; mt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count()); uniform_int_distribution<long long> ll_distr; long long rnd(long long a, long long b) { return ll_distr(tw) % (b - a + 1) + a; } void solve() { int a, b; cin >> a >> b; string s; cin >> s; vector<int> segs; int from = 0; for (int i = 0; i <= ((int)s.size()); ++i) { if (i == ((int)s.size()) || s[i] == 'X') { if (from < i) { segs.push_back(i - from); } from = i + 1; } } multiset<int> have; for (int l : segs) { if (l >= b) { have.insert(l); } } for (int l : segs) { if (l >= a) { auto mem = have; have.erase(have.find(l)); for (int i = 0; i + a <= l; ++i) { if (i >= b) { have.insert(i); } if (l - i - a >= b) { have.insert(l - i - a); } if (!((int)have.size())) { cout << "YES\n"; return; } if (*--have.end() >= b * 2) { } else { if (*have.begin() < a) { } else { if (((int)have.size()) % 2 == 0) { cout << "YES\n"; return; } } } if (i >= b) { have.erase(have.find(i)); } if (l - i - a >= b) { have.erase(have.find(l - i - a)); } } have.insert(l); assert(mem == have); } } cout << "NO\n"; } int main() { cout << setprecision(15) << fixed; ios::sync_with_stdio(false); cin.tie(nullptr); int test_count = 1; cin >> test_count; for (int test = 1; test <= test_count; ++test) { solve(); } }
8
#include <bits/stdc++.h> using namespace std; int main() { int t, k; long long n; cin >> t; while (t) { cin >> n >> k; if (n < k) cout << "NO\n"; else if (n % 2 == 1 && k % 2 == 0) cout << "NO\n"; else if (n % 2 == 0) { if (k <= n / 2) { cout << "YES\n"; for (int i = 0; i < k - 1; i++) cout << 2 << " "; cout << n - (k - 1) * 2 << endl; } else { if (k % 2 == 1) cout << "NO\n"; else { cout << "YES\n"; for (int i = 0; i < k - 1; i++) cout << 1 << " "; cout << n - (k - 1) << endl; } } } else { cout << "YES\n"; for (int i = 0; i < k - 1; i++) cout << 1 << " "; cout << n - (k - 1) << endl; } t--; } }
2
#include<bits/stdc++.h> using namespace std; int main() { int a,t; cin>>t; while(t--) { string s; cin>>a>>s; if(s[0]=='2'&&s[1]=='0'&&s[2]=='2'&&s[3]=='0') { cout<<"YES"<<endl; } else if(s[0]=='2'&&s[1]=='0'&&s[2]=='2'&&s[a-1]=='0') { cout<<"YES"<<endl; } else if(s[0]=='2'&&s[1]=='0'&&s[a-2]=='2'&&s[a-1]=='0') { cout<<"YES"<<endl; } else if(s[0]=='2'&&s[a-3]=='0'&&s[a-2]=='2'&&s[a-1]=='0') { cout<<"YES"<<endl; } else if(s[a-4]=='2'&&s[a-3]=='0'&&s[a-2]=='2'&&s[a-1]=='0') { cout<<"YES"<<endl; } else { cout<<"NO"<<endl; } } }
0
#include <bits/stdc++.h> using namespace std; struct fast_ios { fast_ios() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (auto &x : v) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { for (long long i = 0; i < v.size(); i++) { cout << v[i]; if (i != v.size() - 1) cout << endl; }; return os; } template <typename T> void Out(T x) { cout << x << endl; } template <typename T1, typename T2> void Ans(bool first, T1 y, T2 n) { if (first) Out(y); else Out(n); } using vec = vector<long long>; using mat = vector<vec>; using Pii = pair<long long, long long>; using PiP = pair<long long, Pii>; using PPi = pair<Pii, long long>; using bools = vector<bool>; using pairs = vector<Pii>; const long long mod = 1000000007; signed main() { long long t; cin >> t; vec ans(t, 0); for (long long q = (0); q < (t); q++) { long long n, m; cin >> n >> m; mat a(n, vec(m)); cin >> a; long long k = min(n, m); mat a0(n, vec(k)); pairs p(m); for (long long j = (0); j < (m); j++) { long long mm = 0; for (long long i = (0); i < (n); i++) mm = max(mm, a[i][j]); p[j] = Pii(mm, j); } sort(p.begin(), p.end()); reverse(p.begin(), p.end()); for (long long j = (0); j < (k); j++) { long long j0 = p[j].second; for (long long i = (0); i < (n); i++) a0[i][j] = a[i][j0]; } vec I(k, 0); while (I[0] == 0) { long long tmp = 0; for (long long i = (0); i < (n); i++) { long long mm = 0; for (long long j = (0); j < (k); j++) mm = max(mm, a0[(i + I[j]) % n][j]); tmp += mm; } ans[q] = max(ans[q], tmp); I[k - 1]++; for (long long i = (k)-1; i >= (1); i--) { if (I[i] == n) { I[i] = 0; I[i - 1]++; } else break; } } } Out(ans); return 0; }
6
#include <bits/stdc++.h> using namespace std; using ll = long long; using db = long double; // or double, if TL is tight using str = string; // yay python! using pi = pair<int,int>; using pl = pair<ll,ll>; using pd = pair<db,db>; using vi = vector<int>; using vb = vector<bool>; using vl = vector<ll>; using vd = vector<db>; using vs = vector<str>; using vpi = vector<pi>; using vpl = vector<pl>; using vpd = vector<pd>; #define tcT template<class T #define tcTU tcT, class U // ^ lol this makes everything look weird but I'll try it tcT> using V = vector<T>; tcT, size_t SZ> using AR = array<T,SZ>; tcT> using PR = pair<T,T>; // pairs #define mp make_pair #define f first #define s second // vectors // oops size(x), rbegin(x), rend(x) need C++17 #define sz(x) int((x).size()) #define bg(x) begin(x) #define all(x) bg(x), end(x) #define rall(x) x.rbegin(), x.rend() #define sor(x) sort(all(x)) #define rsz resize #define ins insert #define ft front() #define bk back() #define pb push_back #define eb emplace_back #define pf push_front #define rtn return #define lb lower_bound #define ub upper_bound tcT> int lwb(V<T>& a, const T& b) { return int(lb(all(a),b)-bg(a)); } // loops #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define F0R(i,a) FOR(i,0,a) #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) #define R0F(i,a) ROF(i,0,a) #define rep(a) F0R(_,a) #define each(a,x) for (auto& a: x) const int MOD = 998244353; const int MX = 2e5+5; const ll INF = 1e18; // not too close to LLONG_MAX const db PI = acos((db)-1); const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!! mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); template<class T> using pqg = priority_queue<T,vector<T>,greater<T>>; // bitwise ops // also see https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set constexpr int bits(int x) { // assert(x >= 0); // make C++11 compatible until USACO updates ... return x == 0 ? 0 : 31-__builtin_clz(x); } // floor(log2(x)) constexpr int p2(int x) { return 1<<x; } constexpr int msk2(int x) { return p2(x)-1; } ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down tcT> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } // set a = min(a,b) tcT> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } tcTU> T fstTrue(T lo, T hi, U f) { hi ++; assert(lo <= hi); // assuming f is increasing while (lo < hi) { // find first index such that f is true T mid = lo+(hi-lo)/2; f(mid) ? hi = mid : lo = mid+1; } return lo; } tcTU> T lstTrue(T lo, T hi, U f) { lo --; assert(lo <= hi); // assuming f is decreasing while (lo < hi) { // find first index such that f is true T mid = lo+(hi-lo+1)/2; f(mid) ? lo = mid : hi = mid-1; } return lo; } tcT> void remDup(vector<T>& v) { // sort and remove duplicates sort(all(v)); v.erase(unique(all(v)),end(v)); } tcTU> void erase(T& t, const U& u) { // don't erase auto it = t.find(u); assert(it != end(t)); t.erase(it); } // element that doesn't exist from (multi)set // INPUT #define tcTUU tcT, class ...U tcT> void re(complex<T>& c); tcTU> void re(pair<T,U>& p); tcT> void re(V<T>& v); tcT, size_t SZ> void re(AR<T,SZ>& a); tcT> void re(T& x) { cin >> x; } void re(double& d) { str t; re(t); d = stod(t); } void re(long double& d) { str t; re(t); d = stold(t); } tcTUU> void re(T& t, U&... u) { re(t); re(u...); } tcT> void re(complex<T>& c) { T a,b; re(a,b); c = {a,b}; } tcTU> void re(pair<T,U>& p) { re(p.f,p.s); } tcT> void re(V<T>& x) { each(a,x) re(a); } tcT, size_t SZ> void re(AR<T,SZ>& x) { each(a,x) re(a); } tcT> void rv(int n, V<T>& x) { x.rsz(n); re(x); } // TO_STRING #define ts to_string str ts(char c) { return str(1,c); } str ts(const char* s) { return (str)s; } str ts(str s) { return s; } str ts(bool b) { // #ifdef LOCAL // return b ? "true" : "false"; // #else return ts((int)b); // #endif } tcT> str ts(complex<T> c) { stringstream ss; ss << c; return ss.str(); } str ts(V<bool> v) { str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); res += "}"; return res; } template<size_t SZ> str ts(bitset<SZ> b) { str res = ""; F0R(i,SZ) res += char('0'+b[i]); return res; } tcTU> str ts(pair<T,U> p); tcT> str ts(T v) { // containers with begin(), end() #ifdef LOCAL bool fst = 1; str res = "{"; for (const auto& x: v) { if (!fst) res += ", "; fst = 0; res += ts(x); } res += "}"; return res; #else bool fst = 1; str res = ""; for (const auto& x: v) { if (!fst) res += " "; fst = 0; res += ts(x); } return res; #endif } tcTU> str ts(pair<T,U> p) { #ifdef LOCAL return "("+ts(p.f)+", "+ts(p.s)+")"; #else return ts(p.f)+" "+ts(p.s); #endif } // OUTPUT tcT> void pr(T x) { cout << ts(x); } tcTUU> void pr(const T& t, const U&... u) { pr(t); pr(u...); } void ps() { pr("\n"); } // print w/ spaces tcTUU> void ps(const T& t, const U&... u) { pr(t); if (sizeof...(u)) pr(" "); ps(u...); } // DEBUG void DBG() { cerr << "]" << endl; } tcTUU> void DBG(const T& t, const U&... u) { cerr << ts(t); if (sizeof...(u)) cerr << ", "; DBG(u...); } #ifdef LOCAL // compile with -DLOCAL, chk -> fake assert #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); #else #define dbg(...) 0 #define chk(...) 0 #endif void setPrec() { cout << fixed << setprecision(15); } void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } // FILE I/O void setIn(str s) { freopen(s.c_str(),"r",stdin); } void setOut(str s) { freopen(s.c_str(),"w",stdout); } void setIO(str s = "") { unsyncIO(); setPrec(); // cin.exceptions(cin.failbit); // throws exception when do smth illegal // ex. try to read letter into int if (sz(s)) setIn(s+".in"), setOut(s+".out"); // for USACO } #define ints(...) int __VA_ARGS__; re(__VA_ARGS__); #define rvi(x,y) vi x(y); re(x); /** * Description: modular arithmetic operations * Source: * KACTL * https://codeforces.com/blog/entry/63903 * https://codeforces.com/contest/1261/submission/65632855 (tourist) * https://codeforces.com/contest/1264/submission/66344993 (ksun) * also see https://github.com/ecnerwala/cp-book/blob/master/src/modnum.hpp (ecnerwal) * Verification: * https://open.kattis.com/problems/modulararithmetic */ template<int MOD, int RT> struct mint { static const int mod = MOD; static constexpr mint rt() { return RT; } // primitive root for FFT int v; explicit operator int() const { return v; } // explicit -> don't silently convert to int mint() { v = 0; } mint(ll _v) { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD); if (v < 0) v += MOD; } friend bool operator==(const mint& a, const mint& b) { return a.v == b.v; } friend bool operator!=(const mint& a, const mint& b) { return !(a == b); } friend bool operator<(const mint& a, const mint& b) { return a.v < b.v; } friend void re(mint& a) { ll x; re(x); a = mint(x); } friend str ts(mint a) { return ts(a.v); } mint& operator+=(const mint& m) { if ((v += m.v) >= MOD) v -= MOD; return *this; } mint& operator-=(const mint& m) { if ((v -= m.v) < 0) v += MOD; return *this; } mint& operator*=(const mint& m) { v = int((ll)v*m.v%MOD); return *this; } mint& operator/=(const mint& m) { return (*this) *= inv(m); } friend mint pow(mint a, ll p) { mint ans = 1; assert(p >= 0); for (; p; p /= 2, a *= a) if (p&1) ans *= a; return ans; } friend mint inv(const mint& a) { assert(a.v != 0); return pow(a,MOD-2); } mint operator-() const { return mint(-v); } mint& operator++() { return *this += 1; } mint& operator--() { return *this -= 1; } friend mint operator+(mint a, const mint& b) { return a += b; } friend mint operator-(mint a, const mint& b) { return a -= b; } friend mint operator*(mint a, const mint& b) { return a *= b; } friend mint operator/(mint a, const mint& b) { return a /= b; } }; typedef mint<MOD,5> mi; // 5 is primitive root for both common mods typedef vector<mi> vmi; typedef pair<mi,mi> pmi; typedef vector<pmi> vpmi; vector<vmi> scmb; // small combinations void genComb(int SZ) { scmb.assign(SZ,vmi(SZ)); scmb[0][0] = 1; FOR(i,1,SZ) F0R(j,i+1) scmb[i][j] = scmb[i-1][j]+(j?scmb[i-1][j-1]:0); } // g(a)=f(a+1)-f(a)=prod(2n-1,2n-a)/prod(n-1,n-a) // f(a)=n/(n-1)*(prod(2n-1,2n-a)/prod(n,n-a+1)-1) // maroonrk! mi prod(int r, int l) { mi ans = 1; FOR(i,l,r+1) ans *= i; return ans; } int main() { setIO(); ints(M); rvi(A,M); int N = 0; each(t,A) N += t; sor(A); vmi f; int ind = 0; mi num = 1, den = 1, ans = 0; F0R(a,N+1) { if (a) num *= 2*N-a, den *= N-a+1; while (ind < sz(A) && A[ind] == a) { ans += num/den-1; ++ind; } } ans -= num/den-1; ans *= mi(N)/(N-1); ps(-2*ans); // you should actually read the stuff at the bottom } /* stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH */
13
#include <bits/stdc++.h> const double eps = 1e-9; const int int_inf = 2000000000; const long long i64_inf = 1000000000000000000LL; const double pi = acos(-1.0); using namespace std; int c[10000010]; bool isSq[10000010]; long long res = 0; int a, n; int main() { cin >> a >> n; for (int i = a; i <= a + n - 1; i++) c[i] = i; memset(isSq, 0, sizeof(isSq)); for (int i = 2; i < a + n; i++) if (isSq[i]) { for (int j = i; j < a + n; j += i) c[j] = min(c[j], j / i); long long I = i; if (I * I < (long long)(a + n)) isSq[i * i] = 1; } else { long long I = i; if (I * I < (long long)(a + n)) isSq[i * i] = 1; } for (int i = a; i < a + n; i++) res += c[i]; cout << res << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > v; int main() { int r, c, q, x, a, b, _c, i, j; cin >> r >> c >> q; int matrix[r + 1][c + 1]; for (i = 1; i <= r; i++) for (j = 1; j <= c; j++) matrix[i][j] = 0; while (q--) { cin >> x >> a; if (x < 3) { v.push_back(make_pair(x, a)); if (x == 1) { b = matrix[a][1]; for (j = 2; j <= c; j++) matrix[a][j - 1] = matrix[a][j]; matrix[a][c] = b; } else { b = matrix[1][a]; for (j = 2; j <= r; j++) matrix[j - 1][a] = matrix[j][a]; matrix[r][a] = b; } } else { cin >> b >> _c; matrix[a][b] = _c; } } for (i = v.size() - 1; i >= 0; i--) { x = v[i].second; if (v[i].first == 1) { b = matrix[x][c]; for (j = c; j > 1; j--) matrix[x][j] = matrix[x][j - 1]; matrix[x][1] = b; } else { b = matrix[r][x]; for (j = r; j > 1; j--) matrix[j][x] = matrix[j - 1][x]; matrix[1][x] = b; } } for (i = 1; i <= r; i++) { for (j = 1; j <= c; j++) cout << matrix[i][j] << " "; cout << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, m; int data[105000], num[105000], w[105000], cnt[105000]; pair<int, int> mat[105000]; queue<int> que; bool in_que[105000]; vector<int> ans; int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n * m; i++) { scanf("%d", &data[i]); mat[i].first = data[i]; mat[i].second = i % m; } memset(in_que, true, sizeof(in_que)); for (int i = 0; i < n; i++) { sort(mat + i * m, mat + i * m + m); cnt[i] = 0; while (cnt[i] < m && mat[i * m + cnt[i]].first == -1) { if (++num[mat[i * m + cnt[i]].second] == n) ans.push_back(mat[i * m + cnt[i]].second); cnt[i]++; } que.push(i); } while (!que.empty()) { int i = que.front(); que.pop(); int j = cnt[i]; while (j < m && mat[i * m + j].first == mat[i * m + cnt[i]].first) { j++; w[i]++; } j = cnt[i]; while (j < m && mat[i * m + j].first == mat[i * m + cnt[i]].first) { if (++num[mat[i * m + j].second] == n) { ans.push_back(mat[i * m + j].second); for (int k = 0; k < n; k++) if (data[k * m + mat[i * m + j].second] != -1) { if (--w[k] == 0) que.push(k); } } j++; } cnt[i] = j; } if (ans.size() == m) { for (__typeof(ans.begin()) i = ans.begin(); i != ans.end(); i++) printf("%d ", *i + 1); puts(""); } else printf("-1\n"); return 0; }
7
#include <bits/stdc++.h> using namespace std; int n, m, ans1; char ans[1000010], temp[1000010]; void update(int t, int b) { int len = 1, i, k; while (t != b && t >= 0 && b > +0) { if (t > b) { temp[len++] = 'T'; t -= b; } else { temp[len++] = 'B'; b -= t; } } if (len != n || t != 1) return; temp[len] = 'T'; k = 0; for (i = 1; i < len; i++) if (temp[i] == temp[i + 1]) k++; if (k < ans1) { ans1 = k; for (i = 0; i < len; i++) ans[i] = temp[len - i]; ans[len] = '\0'; } return; } int main() { while (~scanf("%d%d", &n, &m)) { int i, j, k; ans1 = 0x3f3f3f3f; for (i = 1; i <= m; i++) { update(i, m); update(m, i); } if (ans1 == 0x3f3f3f3f) printf("IMPOSSIBLE\n"); else printf("%d\n%s\n", ans1, ans); } }
6
#include <bits/stdc++.h> const int MAX = 1e6 + 10, inf = 1e9 + 10; using namespace std; int n, m; long long N; bool isTime = false; int dx[8] = {0, 0, 1, 1, 1, -1, -1, -1}; int dy[8] = {1, -1, 0, -1, 1, 0, -1, 1}; vector<int> dist[MAX], vis[MAX]; vector<pair<int, int> > goodOnes, ans; vector<char> grid[MAX], resp[MAX]; queue<long long> q; bool valid(int i, int j) { return 0 <= i && 0 <= j && i < n && j < m; } void get(long long &i, long long &j) { i = q.front() / N, j = q.front() - i * N; } bool check(int val) { for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { vis[i][j] = inf; if (grid[i][j] == 'X' && dist[i][j] >= val) { q.push(i * N + j); if (isTime) ans.push_back(make_pair(i, j)); vis[i][j] = 0; } } while (!q.empty()) { long long i, j; get(i, j); q.pop(); for (int k = 0; k < 8; k++) { long long ni = i + dx[k], nj = j + dy[k]; if (!valid(ni, nj) || vis[ni][nj] != inf) continue; vis[ni][nj] = vis[i][j] + 1; q.push(N * ni + nj); } } for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if ((grid[i][j] == '.' && vis[i][j] < val) || (grid[i][j] == 'X' && vis[i][j] >= val)) return false; return true; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n + 2; i++) for (int j = 0; j < m + 2; j++) grid[i].push_back('.'); for (int i = 1; i < n + 1; i++) for (int j = 1; j < m + 1; j++) scanf(" %c", &grid[i][j]); n += 2, m += 2; N = 1LL * max(n, m); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { dist[i].push_back(-1); vis[i].push_back(0); if (grid[i][j] == '.') { q.push(i * N + j); dist[i][j] = 0; } else goodOnes.push_back(make_pair(i, j)); } while (!q.empty()) { long long i, j; get(i, j); q.pop(); int ni, nj; for (int k = 0; k < 8; k++) { ni = i + dx[k], nj = j + dy[k]; if (!valid(ni, nj) || dist[ni][nj] != -1) continue; dist[ni][nj] = dist[i][j] + 1; q.push(N * ni + nj); } } int l = 1, r = n * m + 10, mid, best = 1; while (l <= r) { mid = (l + r) >> 1; if (check(mid)) { best = mid; l = mid + 1; } else r = mid - 1; } printf("%d\n", best - 1); isTime = true; check(best); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) resp[i].push_back('.'); for (pair<int, int> p : ans) resp[p.first][p.second] = 'X'; for (int i = 1; i < n - 1; i++) { for (int j = 1; j < m - 1; j++) printf("%c", resp[i][j]); printf("\n"); } }
7
#include <bits/stdc++.h> struct Edge { int u, v; int id; Edge(int u, int v, int id) : u(u), v(v), id(id) {} }; int a[500005], b[500005]; std::vector<Edge> g[2000005]; int in[2000005]; int vis[1000005]; int stack[500005]; int top = -1; int p[2000005]; int size[2000005]; int find(int u) { if (p[u] == u) return u; else return p[u] = find(p[u]); } void merge(int u, int v) { int x = find(u), y = find(v); if (x != y) { p[x] = y; size[y] += size[x]; } } void dfs(int u) { while (g[u].size() != 0) { while (g[u].size() != 0 && vis[g[u][g[u].size() - 1].id]) g[u].pop_back(); if (g[u].size() == 0) break; Edge edge = g[u][g[u].size() - 1]; vis[edge.id] = vis[edge.id ^ 1] = 1; dfs(edge.v); stack[++top] = edge.id; } } int getEuler(int mask, int n) { int root = -1; int total = 0; for (int i = 1; i <= n; i++) { int u = a[i] & mask, v = b[i] & mask; in[u]++, in[v]++; if (p[u] == -1) total++; if (p[v] == -1 && v != u) total++; p[u] = u, p[v] = v; size[u] = size[v] = 1; g[u].push_back(Edge(u, v, 2 * i)); g[v].push_back(Edge(v, u, 2 * i + 1)); vis[2 * i] = vis[2 * i + 1] = 0; root = u; } int ok = 1; for (int i = 1; i <= n; i++) { int u = a[i] & mask, v = b[i] & mask; if (in[u] % 2 || in[v] % 2) { ok = 0; break; } else merge(u, v); } if (size[find(root)] != total || ok == 0) { ok = 0; } else { top = -1; dfs(root); } for (int i = 1; i <= n; i++) { int u = a[i] & mask, v = b[i] & mask; in[u] = in[v] = size[u] = size[v] = 0; p[u] = p[v] = -1; g[u].clear(); g[v].clear(); } return ok; } int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d%d", &a[i], &b[i]); } memset(p, -1, sizeof(p)); for (int i = 20; i >= 0; i--) { int mask = (1 << i) - 1; int res = getEuler(mask, n); if (res) { printf("%d\n", i); for (int i = top; i >= 0; i--) { int u = stack[i]; int p = u / 2; if (u % 2 == 0) printf("%d %d ", 2 * p - 1, 2 * p); else printf("%d %d ", 2 * p, 2 * p - 1); } printf("\n"); break; } } return 0; }
8
#include <bits/stdc++.h> using namespace std; long long int INF = 1e12; long long int n, m, i, a, b, maxx, ff = 0, flag = 0; vector<long long int> adj[1000001]; vector<long long int> fi(1000001, 0); vector<long long int> brr(1000001, 0); unordered_map<string, long long int> hM; vector<pair<long long int, long long int> > res; void removeEdges(long long int i) { hM.clear(); res.clear(); fi = brr; long long int cnt = 0; for (; cnt < n;) { if (fi[i] > 0) { for (auto j = adj[i].begin(); j != adj[i].end() && fi[i] > 0; j++) { if (fi[*j] != 0 && hM[to_string(i) + to_string(*j)] != *j && hM[to_string(*j) + to_string(i)] != i) { fi[i]--; fi[*j]--; hM[to_string(*j) + to_string(i)] = i; hM[to_string(i) + to_string(*j)] = *j; res.push_back({i, *j}); } } } if (fi[i] > 0) { for (auto j = adj[i].begin(); j != adj[i].end() && fi[i] > 0; j++) { if (hM[to_string(i) + to_string(*j)] != *j && hM[to_string(*j) + to_string(i)] != i) { fi[i]--; fi[*j]--; hM[to_string(*j) + to_string(i)] = i; hM[to_string(i) + to_string(*j)] = *j; res.push_back({i, *j}); } } } if (i == 1 && ff == 1) i = n + 1; cnt++; if (flag == 0) i++; else i--; } } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; long long int randm[7] = {1, n / 2, n / 4, n / 8, n * 3 / 2, n * 4 / 2, n}; maxx = ceil((n + m) / 2.0); for (i = 0; i < m; i++) { cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); brr[a]++; fi[a] = ceil(brr[a] / 2.0); brr[b]++; fi[b] = ceil(brr[b] / 2.0); } brr.clear(); brr = fi; for (i = 0; i < 7; i++) { removeEdges(randm[i]); if (maxx < res.size()) { flag = 1; ff = 1; } else break; } cout << res.size() << "\n"; for (auto i = res.begin(); i != res.end(); i++) { cout << i->first << " " << i->second << "\n"; } return 0; }
8
#include <bits/stdc++.h> using namespace std; long long readi() { long long input = 0; char c = ' '; while (c < '-') { c = getchar(); } bool negative = false; if (c == '-') { negative = true; c = getchar(); } while (c >= '0') { input = 10 * input + (c - '0'); c = getchar(); } if (negative) { input = -input; } return input; } string reads() { string input = ""; char c = ' '; while (c <= ' ') { c = getchar(); } while (c > ' ') { input += c; c = getchar(); } return input; } void printi(long long output) { if (output == 0) { putchar('0'); return; } if (output < 0) { putchar('-'); output = -output; } vector<char> vout; while (output) { vout.push_back((output % 10) + '0'); output /= 10; } for (int i = vout.size() - 1; i >= 0; i--) { putchar(vout[i]); } return; } void prints(string output) { for (int i = 0; i < output.length(); i++) { putchar(output[i]); } return; } long long N; int arr[100010]; long long tree[100010]; long double ans; long long p; void update(int idx, long long val) { for (int e = idx + 1; e <= N; e += e & (-e)) { tree[e] += val; } return; } long long query(int idx) { long long res = 0; for (int e = idx + 1; e > 0; e -= e & (-e)) { res += tree[e]; } return res; } int32_t main() { cout << fixed << setprecision(10); ios_base::sync_with_stdio(false); if (fopen("cf749e.in", "r")) { freopen("cf749e.in", "r", stdin); freopen("cf749e.out", "w", stdout); } cin >> N; for (int i = 0; i < N; i++) { cin >> arr[i]; arr[i] = N - arr[i]; } ans = (long double)((N + 2) * (N - 1)) / 24.0; for (int i = 0; i < N; i++) { p += (long double)query(arr[i] - 1); update(arr[i], 1); } ans += p; p = 0; for (int i = 0; i <= N; i++) { tree[i] = 0; } for (int i = 0; i < N; i++) { p += (N - i) * query(arr[i] - 1); update(arr[i], i + 1); } p *= 2; ans -= (((long double)p) / ((long double)(N * (N + 1)))); cout << ans << '\n'; return 0; }
8
#include <bits/stdc++.h> using namespace std; int dp[2][1005][1005]; int v[2][1005][1005]; const int INF = 1000000000; int n; int main() { scanf("%d", &n); bool iszero = false; int zx, zy; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int x; scanf("%d", &x); if (x == 0) { iszero = true; zx = i, zy = j; v[0][i][j] = v[1][i][j] = 1000; } v[0][i][j] = __builtin_ctz(x); while (x && x % 5 == 0) { x /= 5; v[1][i][j]++; } } } for (int k = 0; k < 2; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == 0 && j == 0) { dp[k][i][j] = v[k][i][j]; continue; } int ans = INF; if (i) ans = min(ans, dp[k][i - 1][j]); if (j) ans = min(ans, dp[k][i][j - 1]); dp[k][i][j] = ans + v[k][i][j]; } } } string ans; int ok = min(dp[0][n - 1][n - 1], dp[1][n - 1][n - 1]); int K = (dp[0][n - 1][n - 1] == ok) ? 0 : 1; if (iszero && ok > 1) { printf("1\n"); for (int i = 0; i < zx; i++) printf("D"); for (int i = 0; i < n - 1; i++) printf("R"); for (int i = zx; i < n - 1; i++) printf("D"); return 0; } int curx = n - 1, cury = n - 1; while (curx != 0 || cury != 0) { if (curx == 0) { ans += "R"; cury--; } else if (cury == 0) { ans += "D"; curx--; } else { if (dp[K][curx - 1][cury] < dp[K][curx][cury - 1]) { ans += "D"; curx--; } else { ans += "R"; cury--; } } } reverse(ans.begin(), ans.end()); printf("%d\n", ok); cout << ans; }
6
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 7; const int INF = 1e9 + 7; int d[N]; int n, k, ans; vector<int> g[N]; vector<pair<int, int> > r; pair<int, int> deg[N]; map<pair<int, int>, int> mp; void dfs(int v = 1, int p = 0, int c = 0) { int clr = 1; for (int to : g[v]) { if (to == p) continue; if (clr == c) clr++; if (d[v]) clr = max(1, c); mp[make_pair(min(v, to), max(v, to))] = clr; dfs(to, v, clr); clr++; } } int main() { scanf("%d%d", &n, &k); for (int i = 1; i < n; i++) { int a, b; scanf("%d%d", &a, &b); g[a].push_back(b); g[b].push_back(a); deg[a] = make_pair(deg[a].first + 1, a); deg[b] = make_pair(deg[b].first + 1, b); r.push_back(make_pair(min(a, b), max(a, b))); } sort(deg + 1, deg + n + 1); for (int i = n; i > n - k; i--) { d[deg[i].second] = 1; } ans = deg[n - k].first; dfs(); printf("%d\n", ans); for (pair<int, int> o : r) printf("%d ", mp[o]); }
5
#include <bits/stdc++.h> using namespace std; vector<string> V; vector<long long> B; set<long long> S; double n, a, c, l, r, ans, b, m; string p, s; long long arr[101]; int main() { ios_base::sync_with_stdio(0); cin >> a >> b >> c; double D = 0; D = pow(b, 2) - 4 * a * c; D = sqrt(D); n = -b - D; n /= 2 * a; m = -b + D; m /= 2 * a; cout << setprecision(15) << max(n, m) << endl; cout << setprecision(15) << min(n, m) << endl; }
2
#include <bits/stdc++.h> const int N = 5e5 + 30; int read() { char ch = getchar(); int s = 0; while (ch < '0' || ch > '9') ch = getchar(); while (ch >= '0' && ch <= '9') s = (s << 3) + (s << 1) + ch - '0', ch = getchar(); return s; } int n, m, Son, cnt, x, y; int c[N], siz[N], son[N], ans[N], dep[N], tot[N]; int f[2 * N], nx[2 * N], to[2 * N], F[N][22]; struct question { int dep, id; }; std::vector<question> q[N]; std::string s[N]; std::map<std::string, int> mp[N]; void dfs1(int p, int fa) { siz[p] = 1; F[p][0] = fa; dep[p] = dep[fa] + 1; for (int i = 1; i < 20; ++i) F[p][i] = F[F[p][i - 1]][i - 1]; for (int i = f[p]; i; i = nx[i]) { if (to[i] == fa) continue; dfs1(to[i], p), siz[p] += siz[to[i]]; if (siz[to[i]] > siz[son[p]]) son[p] = to[i]; } } void add(int p, int fa, int val) { mp[dep[p]][s[p]] += val; if (mp[dep[p]][s[p]] == 1 && val == 1) tot[dep[p]]++; if (mp[dep[p]][s[p]] == 0) tot[dep[p]]--; for (int i = f[p]; i; i = nx[i]) if (to[i] != Son && to[i] != fa) add(to[i], p, val); } void dfs2(int p, int fa, int opt) { for (int i = f[p]; i; i = nx[i]) if (to[i] != son[p] && to[i] != fa) dfs2(to[i], p, 0); if (son[p]) dfs2(son[p], p, 1), Son = son[p]; add(p, fa, 1); Son = 0; for (int i = 0; i < q[p].size(); ++i) ans[q[p][i].id] = tot[q[p][i].dep + dep[p]]; if (!opt) add(p, fa, -1); } int main() { n = read() + 1; for (int i = 2; i <= n; ++i) std::cin >> s[i], x = read() + 1, nx[++nx[0]] = f[x], to[nx[0]] = i, f[x] = nx[0]; m = read(); dfs1(1, 0); for (int i = 1; i <= m; ++i) { x = read() + 1, y = read(); q[x].push_back((question){y, i}); } dfs2(1, 0, 0); for (int i = 1; i <= m; ++i) printf("%d\n", ans[i]); return 0; }
8
#include <bits/stdc++.h> using namespace std; int t, n; int MAXN; vector<long long> S, D; vector<pair<long long, pair<long long, long long> > > C[2]; void input(); void solve(); long long dist(long long, long long, int); pair<long long, long long> toFL(long long, int); int main() { input(); solve(); return 0; } void input() { cin >> t; S.push_back(1); S.push_back(2); D.push_back(0); D.push_back(1); while (S[((int)(S.size())) - 1] < (long long)(1e16)) { S.push_back(S[((int)(S.size())) - 1] + S[((int)(S.size())) - 2]); D.push_back(D[((int)(D.size())) - 2] + 1); } S.push_back(S[((int)(S.size())) - 1] + S[((int)(S.size())) - 2]); D.push_back(D[((int)(D.size())) - 2] + 1); MAXN = ((int)(S.size())) - 1; } void solve() { cin >> n; while (t--) { long long a, b; cin >> a >> b; for (int i = 0; i < 2; i++) { C[i].clear(); C[i].resize(MAXN + 1); } cout << dist(a, b, n) << endl; } } long long dist(long long a, long long b, int n) { if (a > b) swap(a, b); n = min(MAXN, n); if (a == b || n == 0) return 0; if (b - a == 1) return 1; if (n == 1 || n == 2) return 1; long long l = S[n - 1]; if (a <= l) { if (b <= l) return (min(dist(a, b, n - 1), min(toFL(a, n - 1).first, toFL(a, n - 1).second) + min(toFL(b, n - 1).first, toFL(b, n - 1).second) + 2)); return (min(toFL(a, n - 1).first, toFL(a, n - 1).second) + toFL(b - l, n - 2).first + 1); } return (dist(a - l, b - l, n - 2)); } pair<long long, long long> toFL(long long v, int n) { for (int i = 0; i < 2; i++) if (C[i][n].first == v) return C[i][n].second; if (n == 0) return make_pair(0, 0); if (n == 1) return make_pair(v == 1 ? 0 : 1, v == 1 ? 1 : 0); int k = 0; if (C[k][n].first) k++; assert(C[k][n].first == 0); if (v > S[n - 1]) { pair<long long, long long> to = toFL(v - S[n - 1], n - 2); C[k][n] = make_pair(v, make_pair(to.first + 1, to.second)); return C[k][n].second; } pair<long long, long long> to = toFL(v, n - 1); C[k][n] = make_pair(v, make_pair(min(to.first, to.second + 2), min(to.first, to.second) + 1 + D[n - 2])); return C[k][n].second; }
9
#include <bits/stdc++.h> using namespace std; const int N = 3005; int n, m, k, t, a[N], b[N], sum; char s[N]; void dfs(int p, int m) { for (; m; p = (p >= n - 1 ? 1 : p + 1)) { if (a[p] == 1 && a[p + 1] == 0) { printf("%d %d\n", 1, p); swap(a[p], a[p + 1]); ++p; --m; } } } int main() { cin >> n >> m; cin >> s + 1; for (int i = 1; i <= n; ++i) for (int j = i + 1; j <= n; ++j) if (s[i] == 'R' && s[j] == 'L') ++sum; if (sum < m) return cout << -1, 0; for (int i = 1; i <= n; ++i) a[i] = (s[i] == 'R' ? 1 : 0); if (m <= 10000) { copy(a + 1, a + 1 + n, b + 1); for (int i = 1; i <= m; ++i) for (int j = 1; j < n; ++j) if (b[j] == 1 && b[j + 1] == 0) { swap(b[j], b[j + 1]); ++j; } for (int i = 1; i < n; ++i) if (b[i] == 1 && b[i + 1] == 0) return cout << -1, 0; } while (m) { if (sum == m) { dfs(1, m); break; } k = 0; for (int i = 1; i < n; ++i) if (a[i] == 1 && a[i + 1] == 0) ++k; k = min(k, sum - m + 1); t = 0; sum -= k; m -= 1; printf("%d", k); for (int i = 1; i < n; ++i) if (a[i] == 1 && a[i + 1] == 0) { printf(" %d", i); swap(a[i], a[i + 1]); ++i; if (++t == k) break; } printf("\n"); } }
6
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; while (n--) { long long c, s; cin >> c >> s; long long k = abs(c - s); long long q = min(c, s); if (q >= k && (q - k) % 3 == 0) cout << "YES" << endl; else cout << "NO" << endl; } }
2
#include <bits/stdc++.h> using namespace std; char s[103]; int a[103], Ans[103], M; vector<int> G[103]; queue<int> Q; int main() { int N; scanf("%d", &N); for (int i = 1; i <= N; i++) { scanf("%s", s + 1); for (int j = 1; j <= N; j++) if (s[j] == '1') G[i].push_back(j); } for (int i = 1; i <= N; i++) scanf("%d", &a[i]); for (int i = 1; i <= N; i++) if (!a[i]) Q.push(i); if (Q.empty()) { puts("0"); return 0; } while (!Q.empty()) { int u = Q.front(); Ans[++M] = u; Q.pop(); for (auto v : G[u]) if (!(--a[v])) Q.push(v); } printf("%d\n", M); for (int i = 1; i <= M; i++) printf("%d%c", Ans[i], " \n"[i == M]); return 0; }
7
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx2") using namespace std; using ll = long long; template <typename T> inline T& ckmin(T& a, T b) { return a = a > b ? b : a; } template <typename T> inline T& ckmax(T& a, T b) { return a = a < b ? b : a; } class lazy_segment_tree { struct node { ll sum, mx, mn, lzy; }; int n; vector<node> a; private: void push(int t, int s, int e) { if (a[t].lzy) { a[t].sum = a[t].lzy * (e - s + 1); a[t].mn = a[t].mx = a[t].lzy; if (s != e) a[t << 1 | 1].lzy = a[t << 1].lzy = a[t].lzy; a[t].lzy = 0; } } void pull(int t, int s, int e) { assert(e != s); assert(a[t].lzy == 0); a[t].sum = a[t << 1].sum + a[t << 1 | 1].sum; a[t].mx = a[t << 1].mx; a[t].mn = a[t << 1 | 1].mn; } void update(int t, int s, int e, int l, int r, ll y) { push(t, s, e); if (r < s || e < l) return; if (l <= s && e <= r) { a[t].lzy = y; push(t, s, e); return; } int m = (s + e) >> 1; update(t << 1, s, m, l, r, y); update(t << 1 | 1, m + 1, e, l, r, y); pull(t, s, e); } ll sum_query(int t, int s, int e, int l, int r) { push(t, s, e); if (r < s || e < l) return 0; if (l <= s && e <= r) return a[t].sum; int m = (s + e) >> 1; return sum_query(t << 1, s, m, l, r) + sum_query(t << 1 | 1, m + 1, e, l, r); } ll max_query(int t, int s, int e, int l, int r) { push(t, s, e); if (r < s || e < l) return 0; if (l <= s && e <= r) return a[t].mx; int m = (s + e) >> 1; return max(max_query(t << 1, s, m, l, r), max_query(t << 1 | 1, m + 1, e, l, r)); } int first_leq(int t, int s, int e, ll y) { push(t, s, e); if (a[t].mn > y) return e + 1; if (s == e) return s; if (a[t << 1].mn <= y) return first_leq(t << 1, s, (s + e) >> 1, y); else return first_leq(t << 1 | 1, (s + e + 2) >> 1, e, y); } int first_sum_gr(int t, int s, int e, ll y) { push(t, s, e); if (s == e) return s; int m = (s + e) >> 1; push(t << 1, s, m); if (a[t << 1].sum <= y) return first_sum_gr(t << 1 | 1, m + 1, e, y - a[t << 1].sum); else return first_sum_gr(t << 1, s, m, y); } public: lazy_segment_tree(int n_) : n{n_}, a(n << 2) {} void update(int l, int r, ll y) { update(1, 0, n - 1, l, r, y); } ll sum_query(int l, int r) { return sum_query(1, 0, n - 1, l, r); } ll max_query(int l, int r) { return max_query(1, 0, n - 1, l, r); } int first_leq(ll y) { return first_leq(1, 0, n - 1, y); } int first_sum_gr(ll y) { if (a[1].sum <= y) return n; return first_sum_gr(1, 0, n - 1, y); } }; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, q; cin >> n >> q; vector<ll> a(n); for (ll& x : a) cin >> x; lazy_segment_tree seg(n); for (int i = (n)-1; i >= 0; --i) seg.update(0, i, a[i]); auto update = [&](int x, ll y) { int z = seg.first_leq(y); 42; if (z > x) return; seg.update(z, x, y); }; function<int(int, ll)> query = [&](int x, ll y) { if (x == n || y == 0) return 0; int z{x - 1}; z = seg.first_sum_gr(y + (x ? seg.sum_query(0, x - 1) : 0)) - 1; assert(seg.sum_query(x, z) <= y); assert(z == n - 1 || seg.sum_query(x, z + 1) > y); if (z >= x) y -= seg.sum_query(x, z); int r{max(z - x + 1, 0)}; int t = seg.first_leq(y); 42; assert((t == n || seg.max_query(t, n - 1) <= y) && (t == 0 || seg.max_query(t - 1, n - 1) > y)); assert(z == n - 1 || t > max(z, x)); if (t > max(z, x)) r += query(t, y); return r; }; for (int _ = 0; _ < (q); ++_) { int t, x; ll y; cin >> t >> x >> y; --x; if (t == 1) update(x, y); else cout << query(x, y) << '\n'; } return 0; }
9
#include <bits/stdc++.h> using namespace std; const long double PI = 3.1415926535897932384626433832795; const long double EPS = 1e-11; int n, t; bool b[1 << 20]; set<int> st, st2; set<int>::iterator it; int main() { cout << setiosflags(ios::fixed) << setprecision(10); scanf("%d", &n); for (int i = 0; i < (n); i++) { scanf("%d", &t); b[t] = 1; for (it = st.begin(); it != st.end(); it++) { st2.insert((*it) | t); b[(*it) | t] = 1; } st.clear(); for (it = st2.begin(); it != st2.end(); it++) st.insert((*it)); st.insert(t); st2.clear(); } int res = 0; for (int i = 0; i < (1 << 20); i++) if (b[i]) res++; cout << res; return 0; }
4
#include <bits/stdc++.h> char a[200001], b[200001]; int main() { int T; scanf("%d", &T); int n; while (T--) { scanf("%d", &n); scanf("%s", &b); scanf("%s", &a); int cont = 0; for (int i = 0; i < n; i++) { if (a[i] == '1') { if (b[i] == '0') cont++; else if (i - 1 >= 0 && b[i - 1] == '1') { b[i - 1] = '2'; cont++; } else if (i + 1 < n && b[i + 1] == '1') { b[i + 1] = '2'; cont++; } } } printf("%d\n", cont); } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long int ans = LLONG_MAX; bool isValid(long long int n) { long long int a4 = 0, a7 = 0; while (n != 0) { if (n % 10 == 4) a4++; if (n % 10 == 7) a7++; n = n / 10; } if (a4 == a7) return true; else return false; } void make(long long int n, long long int sum) { if (sum > 1e12) return; if (sum >= n && isValid(sum)) { ans = min(ans, sum); return; } else { make(n, sum * 10 + 4); make(n, sum * 10 + 7); return; } } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; make(n, 0); cout << ans; }
2
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; const int inf = INT_MAX; int n, m, s, ans, now = 1, w[N], v[N], c[N], nex[N], head[N]; void add(int x, int y, int co, int z) { nex[++now] = head[x], w[now] = z; head[x] = now, v[now] = y, c[now] = co; } int vi[N], dis[N], way[N], bfe[N]; queue<int> que; bool spfa() { for (int i = 0; i < s; i++) dis[i] = inf; que.push(s), dis[s] = 0, way[s] = inf; while (!que.empty()) { int x = que.front(); que.pop(), vi[x] = 0; for (int i = head[x]; i; i = nex[i]) if (w[i] && dis[x] + c[i] < dis[v[i]]) { dis[v[i]] = dis[x] + c[i]; bfe[v[i]] = i ^ 1, way[v[i]] = min(way[x], w[i]); if (!vi[v[i]]) que.push(v[i]), vi[v[i]] = 1; } } return dis[0] != inf; } int flow[N]; int main() { scanf("%d%d", &n, &m), s = n + 1; for (int i = 1, x, y, c, f; i <= m; i++) { scanf("%d%d%d%d", &x, &y, &c, &f); flow[x] -= f, flow[y] += f; if (c < f) { ans += f - c; add(x, y, 2, inf), add(y, x, -2, 0); add(y, x, 0, f - c), add(x, y, 0, 0); add(y, x, 1, c), add(x, y, -1, 0); } else { add(x, y, 2, inf), add(y, x, -2, 0); add(y, x, 1, f), add(x, y, -1, 0); if (c != f) add(x, y, 1, c - f), add(y, x, -1, 0); } } add(n, 1, 0, inf), add(1, n, 0, 0); for (int i = 2; i < n; i++) if (flow[i] < 0) { add(i, 0, 0, -flow[i]), add(0, i, 0, 0); add(s, n, 0, -flow[i]), add(n, s, 0, 0); } else if (flow[i] > 0) { add(s, i, 0, flow[i]), add(i, s, 0, 0); add(1, 0, 0, flow[i]), add(0, 1, 0, 0); } while (spfa()) { int x = 0; while (x != s) { w[bfe[x]] += way[0]; w[bfe[x] ^ 1] -= way[0]; x = v[bfe[x]]; } ans += way[0] * dis[0]; } printf("%d\n", ans); return 0; }
10
#include <bits/stdc++.h> using namespace std; long long n, x1, x2; vector<pair<long long, long long> > v; int32_t main() { scanf("%lld%lld%lld", &n, &x1, &x2); long long l = 1, r = n; v.resize(n); for (long long i = 0; i < n; i++) scanf("%lld", &v[i].first), v[i].second = i; sort(v.begin(), v.end()); long long r1 = -1, l2 = n - 2, r2 = l2; for (long long i = n - 1; i > 0; i--) { long long sz = n - i; if (sz * v[i].first >= x1) { r1 = i; break; } } if (r1 != -1) { l2 = r1 - 1; r2 = -1; for (long long i = r1 - 1; i >= 0; i--) { long long sz = l2 - i + 1; if (sz * v[i].first >= x2) { r2 = i; break; } } if (r2 != -1) { printf("Yes\n"); printf("%lld %lld\n", n - 1 - r1 + 1, l2 - r2 + 1); for (long long i = n - 1; i >= r1; i--) { printf("%lld ", v[i].second + 1); } printf("\n"); for (long long i = l2; i >= r2; i--) { printf("%lld ", v[i].second + 1); } printf("\n"); return 0; } } r1 = -1, l2 = n - 2, r2 = l2; for (long long i = n - 1; i > 0; i--) { long long sz = n - i; if (sz * v[i].first >= x2) { r1 = i; break; } } if (r1 != -1) { l2 = r1 - 1; r2 = -1; for (long long i = r1 - 1; i >= 0; i--) { long long sz = l2 - i + 1; if (sz * v[i].first >= x1) { r2 = i; break; } } if (r2 != -1) { printf("Yes\n"); printf("%lld %lld\n", l2 - r2 + 1, n - 1 - r1 + 1); for (long long i = l2; i >= r2; i--) { printf("%lld ", v[i].second + 1); } printf("\n"); for (long long i = n - 1; i >= r1; i--) { printf("%lld ", v[i].second + 1); } printf("\n"); return 0; } } printf("No"); }
4
#include <bits/stdc++.h> using namespace std; const int MAX_N = 2e5; const long long INF = 1e18; int N; long long C[MAX_N]; vector<int> G[MAX_N]; long long dp[MAX_N][2]; bool used[MAX_N]; vector<int> ans; int nxt[MAX_N]; long long psm[MAX_N + 1]; bool memo[MAX_N][2]; void dfs(int v, int p) { for (auto e : G[v]) { if (e != p) { dfs(e, v); } } psm[0] = 0; int n = 0; for (auto e : G[v]) { if (e != p) { psm[n + 1] = psm[n] + dp[e][0]; n++; } } if (n == 0) { dp[v][0] = C[v]; dp[v][1] = 0; } else { dp[v][1] = INF; { int inx = 0; for (auto e : G[v]) { if (e != p) { if (dp[v][1] > psm[inx] + dp[e][1] + psm[n] - psm[inx + 1]) { dp[v][1] = psm[inx] + dp[e][1] + psm[n] - psm[inx + 1]; } inx++; } } } dp[v][0] = psm[n]; if (dp[v][1] + C[v] < dp[v][0]) { dp[v][0] = dp[v][1] + C[v]; } } } void dfs2(int v, int p) { psm[0] = 0; int n = 0; for (auto e : G[v]) { if (e != p) { psm[n + 1] = psm[n] + dp[e][0]; n++; } } if (memo[v][0]) { if (dp[v][0] == C[v] + dp[v][1]) { memo[v][1] = true; used[v] = true; } if (dp[v][0] == psm[n]) { for (auto e : G[v]) { if (e != p) { memo[e][0] = true; } } } } if (memo[v][1]) { int inx = 0; vector<int> vec; for (auto e : G[v]) { if (e != p) { if (dp[v][1] == psm[inx] + dp[e][1] + psm[n] - psm[inx + 1]) { vec.push_back(e); } inx++; } } if ((int)vec.size() > 1) { for (auto e : G[v]) { if (e != p) { memo[e][0] = true; } } } else if ((int)vec.size() == 1) { for (auto e : G[v]) { if (e != p && e != vec[0]) { memo[e][0] = true; } } } for (auto i : vec) { memo[i][1] = true; } } for (auto e : G[v]) { if (e != p) { dfs2(e, v); } } } int main() { cin >> N; for (int i = (0); i < (N); i++) cin >> C[i]; for (int i = (0); i < (N - 1); i++) { int a, b; cin >> a >> b; a--; b--; G[a].push_back(b); G[b].push_back(a); } dfs(0, -1); cout << dp[0][0] << " "; memo[0][0] = true; dfs2(0, -1); for (int i = (0); i < (N); i++) { if (used[i]) { ans.push_back(i); } } cout << ans.size() << endl; for (int i = (0); i < ((int)ans.size()); i++) { if (i) cout << " "; cout << ans[i] + 1; } cout << endl; }
8
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, table = 0, res = 0; cin >> n; vector<int> cnt(n + 1, 0); for (int i = 0; i < 2 * n; ++i) { int s; cin >> s; if (cnt[s] == 0) { ++cnt[s]; ++table; } else { --cnt[s]; --table; } if (table > res) res = table; } cout << res << endl; return 0; }
0
#include <bits/stdc++.h> struct TNode { bool is_terminal; int number_of_sons; std::map<char, int> sons; TNode() : is_terminal(false), number_of_sons(0), sons() {} }; std::vector<TNode> trie; int ans = 0; int last_pref = 0; std::vector<int> cur_word; int process(int node, char c) { ans += 1; if (c < 'a' or c > 'z') { if (!trie[node].is_terminal) { trie[node].is_terminal = true; trie[node].number_of_sons += 1; for (int prev_node : cur_word) { trie[prev_node].number_of_sons += 1; } } node = 0; cur_word.clear(); last_pref = 0; } else { cur_word.push_back(node); if (trie[node].sons.count(c)) { node = trie[node].sons[c]; if (trie[node].number_of_sons > 1) { last_pref = cur_word.size(); } if (trie[node].sons.size() == 0) { ans -= cur_word.size(); ans += std::min(int(cur_word.size()), last_pref + 2); } } else { trie[node].sons[c] = trie.size(); trie.emplace_back(); node = trie.size() - 1; } } return node; } int main() { trie.emplace_back(); int node = 0; trie[node].is_terminal = true; char c; while (scanf("%c", &c) == 1) { node = process(node, c); } printf("%d\n", ans); }
5
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int N = 1e6 + 6; long long a[N]; void solve() { int n; cin >> n; vector<pair<int, int> > v; for (int i = 1; i <= n; i++) { int x, y; cin >> x >> y; v.push_back({x, 1}); v.push_back({y, -1}); } v.push_back({1e9 + 12, 0}); sort(v.begin(), v.end()); int cnt = 0; int ans1 = 0; int ans2 = 0; for (int i = 0; i < n + n; i++) { if (v[i].second == 1) { cnt++; } if (v[i].second == -1) { cnt--; } if (v[i].first != v[i + 1].first) { if (cnt > ans2) { ans2 = cnt; ans1 = v[i].first; } } } cout << ans1 << " " << ans2 << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; while (t--) { solve(); } return 0; }
2
#include<bits/stdc++.h> #define ll long long #define pb push_back #define ff first #define ss second #define debug cout<<"here\n" #define MOD 1000000007 const int MAXN = 2e5 +15; using namespace std; int A[MAXN], pre[MAXN], mpre[MAXN]; int st[4*MAXN][2]; void update(int f, int v, int tl, int tr, int p, int x){ if(tl == tr){ st[v][f] = x; return; } int tm = (tl + tr)/2; if(p <= tm){ update(f, 2*v, tl , tm, p, x); } else update(f, 2*v + 1, tm+1,tr, p, x); st[v][f] = max(st[2*v][f], st[2*v+1][f]); } int rmax(int f, int v, int tl, int tr, int l, int r){ if(l > r ) return 0; if(tl == l && tr == r) return st[v][f]; int tm = (tl+tr)/2; return max(rmax(f,2*v, tl, tm, l, min(r,tm)), rmax(f,2*v + 1, tm+1, tr, max(tm+1,l),r)); } void ok(int n){ for(int i=0; i<=n; i++){ pre[i] = 0; mpre[i] = 0; } for(int i=0; i<= 4*n; i++){ st[i][0] = st[i][1] = 0; } } void solve(){ int n; cin>>n; ok(n); map<int, int> mp; vector<int> v; for(int i=1; i<=n; i++){ cin>>A[i]; v.pb(A[i]); } sort(v.begin(), v.end() ); mp[v[0]] = 1; for(int i=1; i<n; i++) mp[v[i]] = mp[v[i-1]] + (v[i] != v[i-1]); for(int i=1; i<=n; i++) A[i] = mp[A[i]]; //coordinate compressed stack<int> st; st.push(1); for(int i=2; i<=n; i++){ while(!st.empty()){ int p = st.top(); if(A[p] <= A[i]){ pre[i] = p; break; } else st.pop(); } st.push(i); } while(!st.empty()) st.pop(); st.push(1); for(int i=2; i<=n; i++){ while(!st.empty()){ int p = st.top(); if(A[p] >= A[i]){ mpre[i] = p; break; } else st.pop(); } st.push(i); } //pre and mpre made ll ans = 0; int L = 0; for(int i=1; i<= n; i++){ int x = A[i]; int las = max(rmax(0,1,1,n,1,A[i]), rmax(1,1,1,n,A[i],n)); L = max(L, las); //cout<<A[i]<<" "<<pre[i]<<" "<<mpre[i]<<" : "<<rmax(0,1,1,n,1,A[i])<<" "<<rmax(1,1,1,n,A[i],n)<<"\n"; ans += (i - L); update(0,1,1,n,x,pre[i]); update(1,1,1,n,x,mpre[i]); } cout<<ans; } int main(){ ios_base::sync_with_stdio(false); cout.tie(0); cin.tie(0); cout<<fixed<<setprecision(12); int t =1; cin>>t; for(int i=1; i<= t; i++){ solve(); cout<<"\n"; } return 0; } /* stuff you should look for * int overflow, array bounds * special cases (n=1?) * do smth instead of nothing and stay organized * WRITE STUFF DOWN * DON'T GET STUCK ON ONE APPROACH */
4
#include <bits/stdc++.h> using namespace std; void Emsawy() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); } const double PI = acos(-1.0); const double eps = 1e-10; const long long mod = 1e9 + 7, oo = 1e9; int dx[]{1, -1, 0, 0, 1, -1, 1, -1}; int dy[]{0, 0, 1, -1, 1, -1, -1, 1}; int n, m, k, c; const int N = 100000 + 5; pair<int, int> g[N]; int cnt[N], p[300], lstp[300]; int main() { Emsawy(); while (cin >> n) { for (int i = 0; i < n; i++) { cin >> g[i].first; } for (int i = 0; i < n; i++) { cin >> g[i].second; } sort(g, g + n); for (int i = n - 1; i >= 0; i--) { cnt[i] = g[i].second; if (i + 1 < n) { cnt[i] += cnt[i + 1]; } } int ans = oo, maxlen = 0; for (int i = 0; i < n; i++) { p[g[i].second]++; maxlen++; if (g[i].first != g[i + 1].first) { int cur = 0, rem = (i + 1) - (maxlen * 2 - 1); if (rem > 0) { for (int j = 0; j < 300; j++) { if (rem - lstp[j] >= 0) { rem -= lstp[j]; cur += lstp[j] * j; } else { cur += rem * j; rem = 0; break; } } } ans = min(ans, cnt[i + 1] + cur); for (int j = 0; j < 300; j++) { lstp[j] += p[j]; } memset(p, 0, sizeof(p)); maxlen = 0; } } cout << ans << endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, k, startnode, ansnode; vector<int> sz, mark, lleft, rright; vector<vector<int> > g; int getSize(int node = startnode, int par = -1) { sz[node] = mark[node]; for (int i = 0; i < (int)g[node].size(); i++) { int to = g[node][i]; if (to != par) sz[node] += getSize(to, node); } return sz[node]; } int findCentroid(int node = startnode, int par = -1) { for (int i = 0; i < (int)g[node].size(); i++) { int to = g[node][i]; if (to != par && sz[to] > k) return findCentroid(to, node); } return node; } void findNodes(int node = ansnode, int par = -1) { if (mark[node] && (int)lleft.size() < k && node != ansnode) lleft.push_back(node); else if (mark[node] && node != ansnode) rright.push_back(node); for (int i = 0; i < (int)g[node].size(); i++) { int to = g[node][i]; if (to != par) findNodes(to, node); } } int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); cin >> n >> k; g.resize(n + 1); sz.resize(n + 1); mark.resize(n + 1, 0); for (int i = 0; i < n - 1; i++) { int from, to; cin >> from >> to; g[from].push_back(to); g[to].push_back(from); } for (int i = 0; i < 2 * k; i++) { int node; cin >> node; mark[node] = 1; } for (int i = 1; i <= n; i++) if ((int)g[i].size() == 1) startnode = i; getSize(); ansnode = findCentroid(); findNodes(); if (mark[ansnode]) rright.push_back(ansnode); cout << 1 << '\n' << ansnode << '\n'; for (int i = 0; i < k; i++) cout << lleft[i] << ' ' << rright[i] << ' ' << ansnode << '\n'; return 0; }
7
#include <bits/stdc++.h> #pragma GCC optimize "-O3" using namespace std; const int MAXN = 120000; int n; int a[MAXN]; int go[MAXN]; int pl[MAXN]; int en[MAXN]; int pr[MAXN]; vector<int> gg[MAXN]; void solve() { cin >> n; for (int i = 0; i < n; ++i) cin >> a[i], --a[i], pl[a[i]] = i; memset(en, 0, sizeof(en[0]) * n); int k = 0; while (k * (k + 1) / 2 <= n) ++k; --k; int cur = n; vector<vector<int>> ansv; while (k && cur) { fill(go, go + n + 1, n); go[0] = -1; int ans = 0; int st = -1; for (int i = 0; i < n; ++i) { if (en[i]) continue; int x = lower_bound(go, go + n, a[i]) - go; pr[i] = go[x - 1]; go[x] = a[i]; if (x > ans) ans = x, st = i; } if (ans > k) { cur -= ans; int now = st; vector<int> vv; while (true) { en[now] = 1; vv.push_back(a[now]); if (pr[now] == -1) break; now = pl[pr[now]]; } reverse((vv).begin(), (vv).end()); ansv.push_back(vv); --k; } else { break; } } for (int i = 0; i < n; ++i) gg[i].clear(); set<pair<int, int>> ss; int cc = 0; for (int i = 0; i < n; ++i) { if (en[i]) continue; auto it = ss.lower_bound(make_pair(a[i], 0)); if (it == ss.end()) { gg[cc].push_back(a[i]); ss.emplace(a[i], cc); ++cc; } else { int x = it->second; gg[x].push_back(a[i]); ss.erase(it); ss.emplace(a[i], x); } } for (int i = 0; i < cc; ++i) { ansv.push_back(gg[i]); } cout << ansv.size() << "\n"; for (auto &vv : ansv) { cout << vv.size() << " "; for (int i : vv) cout << i + 1 << " "; cout << "\n"; } } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); int tt; cin >> tt; for (int i = 0; i < tt; ++i) solve(); return 0; }
13
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; int modmult(int x, int y) { return (x * int64_t(y)) % MOD; } template <class T> void chmin(T& x, T y) { if (y < x) x = y; } int64_t solve(const vector<string>& s) { int n = s.size(); vector<array<int, 26>> cnt(n); for (int i = 0; i < n; ++i) { fill(cnt[i].begin(), cnt[i].end(), 0); for (char c : s[i]) { cnt[i][c - 'a']++; } } vector<int> sub_all(1 << n, 0); for (int i = 1; i < (1 << n); ++i) { array<int, 26> c; fill(c.begin(), c.end(), 1e9); for (int j = 0; j < n; ++j) { if ((i >> j) & 1) { for (int k = 0; k < 26; ++k) { chmin(c[k], cnt[j][k]); } } } sub_all[i] = 1; for (int j = 0; j < 26; ++j) { sub_all[i] = modmult(sub_all[i], c[j] + 1); } } vector<int> f = sub_all; for (int i = 0; i < n; ++i) { for (int mask = 0; mask < (1 << n); ++mask) { if ((mask >> i) & 1) { f[mask] -= f[mask ^ (1 << i)]; if (f[mask] < 0) f[mask] += MOD; } } } for (int mask = 0; mask < (1 << n); ++mask) { if (!(__builtin_popcount(mask) & 1)) { if (f[mask] != 0) { f[mask] = MOD - f[mask]; } } } int64_t ans = 0; for (int mask = 1; mask < (1 << n); ++mask) { vector<int> inds; for (int i = 0; i < n; ++i) { if ((mask >> i) & 1) { inds.push_back(i); } } int sub_ans = f[mask]; int64_t sum_inds = 0; for (int i : inds) { sum_inds += (i + 1); } ans ^= (sum_inds * int64_t(inds.size()) * sub_ans); } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; vector<string> s(n); for (int i = 0; i < n; ++i) { cin >> s[i]; } cout << solve(s) << endl; }
8
#include <bits/stdc++.h> using namespace std; int t; long long n, m, k; long long blocks[233333]; long long mmax(long long a, long long b) { return (a > b ? a : b); } int main() { scanf("%d", &t); while (t--) { scanf("%lld%lld%lld", &n, &m, &k); for (int i = 1; i <= n; i++) scanf("%lld", &blocks[i]); for (int i = 1; i < n; i++) { if (blocks[i] > blocks[i + 1] - k) m += blocks[i] - mmax(blocks[i + 1] - k, 0LL); if (blocks[i + 1] - blocks[i] > k) { m -= blocks[i + 1] - blocks[i] - k; if (m < 0) break; } } if (m >= 0) puts("YES"); else puts("NO"); } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MAXN = 2222; char a[MAXN][MAXN]; vector<pair<int, int> > V; set<int> S[MAXN]; int dp[MAXN][MAXN]; int odpow[MAXN]; deque<int> lw, pw; void dodlw(int val) { while (!lw.empty() && lw.back() < val) lw.pop_back(); lw.push_back(val); } void dodpw(int val) { while (!pw.empty() && pw.back() > val) pw.pop_back(); pw.push_back(val); } int kl[MAXN], kp[MAXN]; int obsluz(int y, int n, int m) { int lwk = 1; int wyn = 0; lw.push_front(0), pw.push_front(m + 1); for (int i = 1; i <= n; i++) { auto it = S[i].upper_bound(y); it--; int lw1 = *it; it = S[i].lower_bound(y); int pw1 = *it; dodlw(lw1), dodpw(pw1); kl[i] = lw1, kp[i] = pw1; while (lwk < i && pw.front() - lw.front() - 1 < i - lwk + 1) { if (pw.front() == kp[lwk]) pw.pop_front(); if (lw.front() == kl[lwk]) lw.pop_front(); lwk++; } wyn = max(wyn, min(i - lwk + 1, pw.front() - lw.front() - 1)); } lw.clear(), pw.clear(); return wyn; } int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = 1; i <= n; i++) { scanf("%s", a[i] + 1); S[i].insert(0), S[i].insert(m + 1); for (int j = 1; j <= m; j++) { if (a[i][j] == 'X') S[i].insert(j); } } for (int i = 1; i <= k; i++) { int x, y; scanf("%d%d", &x, &y); V.emplace_back(x, y); a[x][y] = 'X'; S[x].insert(y); } int wyn = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (a[i][j] == 'X') continue; dp[i][j] = 1 + min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])); wyn = max(wyn, dp[i][j]); } } odpow[k] = wyn; for (int i = k - 1; i >= 0; i--) { int x, y; tie(x, y) = V[i]; S[x].erase(y); wyn = max(wyn, obsluz(y, n, m)); odpow[i] = wyn; } for (int i = 1; i <= k; i++) printf("%d\n", odpow[i]); return 0; }
10
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[2 * n]; for (int i = 0; i < 2 * n; i++) { cin >> a[i]; } int co = 0; for (int i = 0; i < 2 * n; i = i + 2) { int now = a[i]; int pos; for (int j = i + 1; j < 2 * n; j++) { if (a[j] == now) { pos = j; break; } } for (int j = pos; j >= i + 2; j--) { swap(a[j], a[j - 1]); co++; } } cout << co << endl; }
3
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> #include <vector> #include <set> #include <map> #include <unordered_set> #include <unordered_map> #include <queue> #include <ctime> #include <cassert> #include <complex> #include <string> #include <cstring> #include <chrono> #include <random> #include <bitset> using namespace std; #ifdef LOCAL #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); #else #define eprintf(...) 42 #endif using ll = long long; using ld = long double; using uint = unsigned int; using ull = unsigned long long; template<typename T> using pair2 = pair<T, T>; using pii = pair<int, int>; using pli = pair<ll, int>; using pll = pair<ll, ll>; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myRand(ll B) { return (ull)rng() % B; } #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second clock_t startTime; double getCurrentTime() { return (double)(clock() - startTime) / CLOCKS_PER_SEC; } const ll INF = (ll)1e17; const int N = 2020; int n, m; ll dpV[N][N][2][2]; ll dpE[N][N][2][2]; ll a[N][2]; pii ord[N]; int st[N]; int stSz; int ed[N][2]; vector<int> g[N]; int sz[N]; int ans[N]; vector<int> lb, rb; set<pii> segms; int finSegm[N][2]; void dfsRestore(int v, int s, int fl, int fr); void mergeSon(int v, int u, int id1, int id2) { for (int x = 0; x < sz[v]; x++) for (int y = 0; y < sz[u]; y++) for (int fl = 0; fl < 2; fl++) for (int fm = 0; fm < 2; fm++) for (int fr = 0; fr < 2; fr++) { ll w = dpE[id1][x][fl][fm] + dpV[u][y][fm][fr]; if (w < -INF / 2) continue; dpE[id2][x + y + fm][fl][fr] = max(dpE[id2][x + y + fm][fl][fr], w); } } void unmergeSon(ll &curW, int &ss, int &ffl, int &ffr, int v, int u, int id1, int id2) { for (int x = 0; x < sz[v]; x++) for (int y = 0; y < sz[u]; y++) for (int fl = 0; fl < 2; fl++) for (int fm = 0; fm < 2; fm++) for (int fr = 0; fr < 2; fr++) { if (fl != ffl || fr != ffr) continue; ll w = dpE[id1][x][fl][fm] + dpV[u][y][fm][fr]; if (w < -INF / 2) continue; if (w != curW) continue; if (x + y + fm != ss) continue; dfsRestore(u, y, fm, fr); curW = dpE[id1][x][fl][fm]; ss = x; ffl = fl; ffr = fm; return; } assert(false); } void dfsDp(int v) { eprintf("dfsDp %d\n", v); sz[v] = 1; if (g[v].empty()) { for (int f = 0; f < 2; f++) for (int h = 0; h < 2; h++) { ll w = a[v][1] - a[v][0]; if (f) w += a[v][0]; if (h) w -= a[v][1]; dpV[v][0][f][h] = w; } return; } for (int id : g[v]) dfsDp(ed[id][1]); int u = ed[g[v][0]][1]; for (int s = 0; s < sz[u]; s++) for (int x = 0; x < 2; x++) for (int y = 0; y < 2; y++) dpE[g[v][0]][s][x][y] = dpV[u][s][x][y]; sz[v] += sz[u]; for (int it = 1; it < (int)g[v].size(); it++) { int id = g[v][it]; u = ed[id][1]; mergeSon(v, u, g[v][it - 1], id); sz[v] += sz[u]; } for (int s = 0; s < sz[v]; s++) for (int fl = 0; fl < 2; fl++) for (int fr = 0; fr < 2; fr++) { ll w = dpE[g[v].back()][s][fl][fr]; if (w < -INF / 2) continue; if (fl) { dpV[v][s][0][fr] = max(dpV[v][s][0][fr], w - a[v][0]); dpV[v][s][1][fr] = max(dpV[v][s][1][fr], w); } if (s > 0) { dpV[v][s - 1][fl][fr] = max(dpV[v][s - 1][fl][fr], w); } if (fr) { dpV[v][s][fl][0] = max(dpV[v][s][fl][0], w + a[v][1]); dpV[v][s][fl][1] = max(dpV[v][s][fl][1], w); } } } void addLeft(int x) { eprintf("addLeft %d\n", x); lb.push_back(x); } void addRight(int x) { eprintf("addRight %d\n", x); rb.push_back(x); } void dfsRestore(int v, int s, int fl, int fr) { eprintf("dfsRestore %d %d %d %d\n", v, s, fl, fr); if (g[v].empty()) { addLeft(a[v][0]); addRight(a[v][1]); ans[v] = 0; if (fl) addRight(a[v][0]); if (fr) addLeft(a[v][1]); return; } ll curW = dpV[v][s][fl][fr]; bool fnd = false; for (int ns = 0; !fnd && ns < sz[v]; ns++) for (int nfl = 0; !fnd && nfl < 2; nfl++) for (int nfr = 0; !fnd && nfr < 2; nfr++) { ll w = dpE[g[v].back()][ns][nfl][nfr]; if (w < -INF / 2) continue; if (nfl && nfr == fr && ns == s) { ll nw = w; if (fl == 0) nw -= a[v][0]; if (nw == curW) { fnd = true; addLeft(a[v][0]); curW = w; if (fl) addRight(a[v][0]); fl = nfl; ans[v] = -1; break; } } if (ns > 0 && nfl == fl && nfr == fr && ns == s + 1) { if (curW == w) { fnd = true; s = ns; ans[v] = 0; break; } } if (nfr && nfl == fl && ns == s) { ll nw = w; if (fr == 0) nw += a[v][1]; if (nw == curW) { fnd = true; addRight(a[v][1]); curW = w; if (fr) addLeft(a[v][1]); fr = nfr; ans[v] = 1; break; } } } assert(fnd); for (int it = (int)g[v].size() - 1; it > 0; it--) { int id = g[v][it]; int u = ed[id][1]; sz[v] -= sz[u]; unmergeSon(curW, s, fl, fr, v, u, g[v][it - 1], id); } dfsRestore(ed[g[v][0]][1], s, fl, fr); eprintf("endRestore %d\n", v); } void dfsSegms(int v) { for (int id : g[v]) dfsSegms(ed[id][1]); if (ans[v] == -1) { auto it = segms.lower_bound(mp(a[v][0], -1)); assert(it != segms.end()); assert(it->first == a[v][0]); finSegm[v][0] = it->first; finSegm[v][1] = it->second; segms.erase(it); } else if (ans[v] == 0) { auto it = segms.lower_bound(mp(a[v][0], -1)); assert(it != segms.end()); assert(it->second <= a[v][1]); finSegm[v][0] = it->first; finSegm[v][1] = it->second; segms.erase(it); } else { auto it = segms.lower_bound(mp(a[v][1], -1)); assert(it != segms.begin()); it--; assert(it->second == a[v][1]); finSegm[v][0] = it->first; finSegm[v][1] = it->second; segms.erase(it); } } int main() { startTime = clock(); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lld%lld", &a[i][0], &a[i][1]); ord[i] = mp((int)a[i][0], i); } sort(ord, ord + n); for (int i = n - 1; i >= 0; i--) { int v = ord[i].second; while(stSz > 0 && a[v][1] > a[st[stSz - 1]][0]) { stSz--; g[v].push_back(m); ed[m][0] = v; ed[m][1] = st[stSz]; eprintf("edge %d %d\n", v, st[stSz]); m++; } st[stSz++] = v; } for (int v = 0; v < n; v++) for (int s = 0; s <= n; s++) for (int x = 0; x < 2; x++) for (int y = 0; y < 2; y++) dpV[v][s][x][y] = dpE[v][s][x][y] = -INF; for (int i = 0; i < stSz; i++) dfsDp(st[i]); ll ansVal = 0; for (int i = 0; i < stSz; i++) { int v = st[i]; ansVal += dpV[v][0][0][0]; } printf("%lld\n", ansVal); for (int i = 0; i < stSz; i++) { int v = st[i]; dfsRestore(v, 0, 0, 0); } assert((int)lb.size() == n); assert((int)rb.size() == n); sort(all(lb)); sort(all(rb)); for (int i = 0; i < n; i++) segms.insert(mp(lb[i], rb[i])); for (int i = 0; i < stSz; i++) { int v = st[i]; dfsSegms(v); } for (int i = 0; i < n; i++) printf("%d %d\n", finSegm[i][0], finSegm[i][1]); return 0; }
12
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) if (a[a[a[i]]] == i) { cout << "YES"; return 0; } cout << "NO"; }
0
#include <bits/stdc++.h> using namespace std; bool checkBit(int mask, int idx) { return mask & (1 << idx); } int main() { int tc; scanf("%d", &tc); while (tc--) { long long d, m; scanf("%lld %lld", &d, &m); long long st[35] = {}; long long en[35] = {}; st[0] = 0; en[0] = 1; long long i = 1; for (; (1LL << (i + 1)) <= d; i++) { st[i] = (en[i - 1] + ((en[i - 1] + 1))) % m; en[i] = (en[i - 1] + ((en[i - 1] + 1) * (1LL << (i))) % m) % m; } --i; d -= (1LL << (i + 1)) - 1; printf("%lld\n", (en[i] + (en[i] + 1LL) * d) % m); } return 0; }
4
#include <bits/stdc++.h> int st[101]; int main() { int to, car = 2, cal = 0, cho = 100, flo = 1, egg = 1; scanf("%d", &car); for (to = 0; car > 0; car--) { scanf("%d", &cho); st[++to] = cho; st[to] *= flo; cho = st[to--]; st[to] += cho; st[++to] = flo; st[to] += egg; flo = st[to--]; } printf("%d\n", st[0]); return 0; }
6
#include <bits/stdc++.h> using namespace std; int n, m; bool a[111][111]; int X[] = {-1, 0, 1, 0}; int Y[] = {0, -1, 0, 1}; void DFS(int x, int y) { a[x][y] = 0; for (int i = 0; i < 4; i++) { if (a[x + X[i]][y + Y[i]]) DFS(x + X[i], y + Y[i]); } } int main() { ios_base::sync_with_stdio(0); cin >> n >> m; char x; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> x; if (x == 'B') a[i][j] = 1; } } int ans = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (a[i][j]) { DFS(i, j); ans++; } } } cout << ans; return 0; }
2
#include <bits/stdc++.h> using namespace std; int x, n, px; int main() { scanf("%d", &x); if (x == 3) { printf("5\n"); return 0; } for (n = 1; 1; n += 2) { px = n * n / 2 + 1; if (px >= x) { printf("%d\n", n); return 0; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long UNDEF = -1; const long long INF = 1e18; template <typename T> inline bool chkmax(T& aa, T bb) { return aa < bb ? aa = bb, true : false; } template <typename T> inline bool chkmin(T& aa, T bb) { return aa > bb ? aa = bb, true : false; } static char stdinBuffer[1024]; static char* stdinDataEnd = stdinBuffer + sizeof(stdinBuffer); static const char* stdinPos = stdinDataEnd; void readAhead(size_t amount) { size_t remaining = stdinDataEnd - stdinPos; if (remaining < amount) { memmove(stdinBuffer, stdinPos, remaining); size_t sz = fread(stdinBuffer + remaining, 1, sizeof(stdinBuffer) - remaining, stdin); stdinPos = stdinBuffer; stdinDataEnd = stdinBuffer + remaining + sz; if (stdinDataEnd != stdinBuffer + sizeof(stdinBuffer)) *stdinDataEnd = 0; } } int readInt() { readAhead(16); int x = 0; bool neg = false; while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos; if (*stdinPos == '-') { ++stdinPos; neg = true; } while (*stdinPos >= '0' && *stdinPos <= '9') { x *= 10; x += *stdinPos - '0'; ++stdinPos; } return neg ? -x : x; } const int maxn = 2004; string a[maxn]; int vdx[] = {1, 0, -1, 0}; int vdy[] = {0, 1, 0, -1}; int n, m; int getcnt(int x, int y) { int cnt = 0; for (int k = 0; k < 4; k++) { int dx = vdx[k], dy = vdy[k]; int nx = x + dx, ny = y + dy; if ((0 <= nx && nx < n) && (0 <= ny && ny < m)) { if (a[nx][ny] == '.') { cnt++; } } } return cnt; } pair<int, int> getneigh(int x, int y) { for (int k = 0; k < 4; k++) { int dx = vdx[k], dy = vdy[k]; int nx = x + dx, ny = y + dy; if ((0 <= nx && nx < n) && (0 <= ny && ny < m)) { if (a[nx][ny] == '.') return make_pair(nx, ny); } } return make_pair(-1, -1); } queue<pair<int, int> > q; void upd(int x, int y) { for (int k = 0; k < 4; k++) { int dx = vdx[k], dy = vdy[k]; int nx = x + dx, ny = y + dy; if ((0 <= nx && nx < n) && (0 <= ny && ny < m)) { if (a[nx][ny] == '.' && getcnt(nx, ny) == 1) { q.push(make_pair(nx, ny)); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int x = 0; x < n; x++) cin >> a[x]; for (int x = 0; x < n; x++) for (int y = 0; y < m; y++) { if (getcnt(x, y) == 1) { q.push(make_pair(x, y)); } } while (!q.empty()) { pair<int, int> top = q.front(); q.pop(); int x = top.first, y = top.second; if (a[x][y] != '.') continue; pair<int, int> p = getneigh(x, y); int px = p.first, py = p.second; if (px == -1) continue; if (a[px][py] != '.') continue; if (x == px) { if (y + 1 == py) a[x][y] = '<', a[x][py] = '>'; else if (py + 1 == y) a[x][py] = '<', a[x][y] = '>'; else assert(0); } else if (y == py) { if (x + 1 == px) a[x][y] = '^', a[px][y] = 'v'; else if (px + 1 == x) a[px][y] = '^', a[x][y] = 'v'; else assert(0); } else assert(0); upd(x, y); upd(px, py); } for (int x = 0; x < n; x++) for (int y = 0; y < m; y++) { if (a[x][y] == '.') { cout << "Not unique" << "\n"; return 0; } } for (int x = 0; x < n; x++) cout << a[x] << "\n"; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b; cin >> n >> a >> b; if (n - a > b + 1) { cout << b + 1; } else { cout << n - a; } return 0; }
1
#include<stdio.h> #include<math.h> #include<string.h> #include<string> #include<stack> #include<map> #include<set> #include<queue> #include<bitset> #include<iostream> #include<algorithm> #include<unordered_map> #include<unordered_set> #define PI acos(-1) #define pb push_back #define all(x) x.begin(),x.end() #define INF 0x3f3f3f3f #define dd(x) cout<<#x<<" = "<<x<<"," #define de(x) cout<<#x<<" = "<<x<<"\n" #define rep(i, a, b) for(int i=(a); i<(b); i++) #define per(i, a, b) for(int i=(b)-1; i>=(a); i--) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int N=1e6+5; ll qpow(ll a,ll n){ ll re=1; while(n>0){ if(n&1){ re*=a; } a*=a; n>>=1; } return re; } ll p[N],cnt[N],sum[N]; int main() { ll n,q; scanf("%lld%lld",&n,&q); for(int i=0;i<n;i++){ p[i]=pow(2,i); } for(int i=0;i<n;i++){ scanf("%lld",&cnt[i]); sum[i]=cnt[i]*p[i]; } while(q--){ ll op; scanf("%lld",&op); if(op==1){ ll pos,val; scanf("%lld%lld",&pos,&val); cnt[pos]=val; sum[pos]=cnt[pos]*p[pos]; } else if(op==2){ ll x,k; scanf("%lld%lld",&x,&k); ll tot=0; for(int i=0;i<n;i++){ tot+=sum[i]; } if(tot<k){ printf("-1\n"); continue; } ll tot1=0,tot2=0; for(int i=0;i<=x;i++){ tot1+=cnt[i],tot2+=sum[i]; } if(tot1>=k){ printf("0\n"); continue; } ll ans=0; for(int i=x+1;i<n;i++){ ll re=min((k-tot1)/(p[i]/p[x]),cnt[i]); tot1+=re*p[i]/p[x]; tot2+=re*p[i]; ans+=re*(p[i]/p[x]-1); if(re<cnt[i]){ for(int j=i-1;j>=x;j--){ if(tot2>=k) break; ans++; if(tot1+p[j]/p[x]<=k){ tot1+=p[j]/p[x]; tot2+=p[j]; ans+=p[j]/p[x]-1; } } break; } } if(tot2>=k){ printf("%lld\n",ans+k-tot1); } } } } /* 6 100 0 0 0 0 1 1 */
10
#include <bits/stdc++.h> using namespace std; const int MAXL = 62; long long dp[MAXL + 10], fp[MAXL + 10]; void make() { for (int i = 1; i <= MAXL; i++) { memset(dp, 0, sizeof dp); for (int j = 1; j <= i; j++) { if (i % j != 0) continue; dp[j] = (1ll << (j - 1)); } for (int j = 1; j < i; j++) for (int k = j + j; k <= i; k += j) dp[k] -= dp[j]; for (int j = 1; j < i; j++) if (i % j == 0) fp[i] += dp[j]; fp[i] += fp[i - 1]; } } int len(long long x) { for (int i = MAXL; i >= 0; i--) if (x & (1ll << i)) return i + 1; } long long mer(long long p, int t) { long long ret = 0, l = len(p); for (int i = 1; i <= t; i++) ret <<= l, ret += p; return ret; } long long f(long long x) { memset(dp, 0, sizeof dp); if (x == 0) return 0; int l = len(x); long long ret = 0; for (int i = 1; i < l; i++) { if (l % i != 0) continue; long long p = x, ans = 0; p >>= (l - i); ans += p - (1ll << (i - 1)); if (mer(p, l / i) <= x) ans++; dp[i] += ans; } for (int i = 1; i < l; i++) for (int j = i + i; j <= l; j += i) dp[j] -= dp[i]; for (int i = 1; i < l; i++) if (l % i == 0) ret += dp[i]; return ret + fp[l - 1]; } int main() { ios::sync_with_stdio(false); make(); long long l, r; cin >> l >> r; long long ans1 = f(r), ans2 = f(l - 1); cout << ans1 - ans2 << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; for (int i = 1; i <= t; i++) { int a, b, c, d, s, e, minx, maxx; cin >> a >> b >> c; s = a + b + c; e = max(a, max(b, c)); minx = (e - (s - e)); maxx = s; cout << (minx + maxx) / 2 << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 300 + 10; const int maxm = 100000 + 10; map<string, int> mp; char s[maxm]; int n, res, sum, i, j, k, a[maxn], len[maxn], nxt[maxn]; int getId(const string& t) { if (!mp[t]) mp[t] = (int)mp.size(); return mp[t]; } int kmp(int a[], int n, int b[], int m) { int i, j, res = 0; for (nxt[0] = j = -1, i = 1; i < n; nxt[i++] = j) { while (~j && a[j + 1] != a[i]) j = nxt[j]; j += (a[j + 1] == a[i]); } for (j = -1, i = 0; i < m; i++) { while (~j && a[j + 1] != b[i]) j = nxt[j]; j += (a[j + 1] == b[i]); if (j == n - 1) res++, j = -1; } return res; } int main() { cin >> n; for (i = 1; i <= n; i++) { cin >> s; a[i] = getId(s); len[i] = strlen(s); } for (sum = n - 1, i = 1; i <= n; i++) sum += len[i]; res = sum; for (i = 1; i <= n; i++) { for (j = i; j <= n; j++) { int cnt = kmp(a + i, j - i + 1, a + 1, n); if (cnt >= 2) { int tot = j - i; for (k = i; k <= j; k++) tot += len[k] - 1; res = min(res, sum - tot * cnt); } } } cout << res << endl; return 0; }
7
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; string home; cin >> home; int cntl = 0, cntr = 0; while (n--) { string t; cin >> t; string l, r; l = t.substr(0, 3); r = t.substr(5, 3); if (l == home) cntl++; if (r == home) cntr++; } if (cntl == cntr) cout << "home" << "\n"; else cout << "contest" << "\n"; } int main() { int t = 1; while (t--) solve(); return 0; }
0