#include<bits/stdc++.h> usingnamespace std; #define ll unsigned long long #define endl '\n' #define int ll constint N = 1e6 + 10;
voidsolve() { int n, m; cin >> n >> m; auto find = [&](ll n) { ll num; if (n % 2 != 0) { ll h = (n - 1) / 2; num = h * h; } else { ll h = (n - 2) / 2; num = h * (h + 1); } return num; }; int res = find(n); if (res >= m) { cout << res << endl; } else { ll l = 2, r = 1e13; while (l <= r) { ll mid = (l + r) / 2; if (find(mid) >= m) { r = mid - 1; } else l = mid + 1; } cout << l; } }
#include<bits/stdc++.h> usingnamespace std; #define ll long long #define endl '\n' #define int ll int n, q, k;
constint N = 1e6 + 10; int tr[N]; int idx[N], s[N]; intlowbit(int x){ return x & -x; } voidadd(int x, int c) { for (; x <= n; x += lowbit(x)) tr[x] += c; } ll getsum(int x) { ll ans = 0; for (; x; x -= lowbit(x)) ans += tr[x]; return ans; }
voidsolve() { cin >> n >> k; vector<int> a(n + 1), b(n + 1); vector<pair<int, int>> c(n + 1); int sum = 0; for (int i = 1; i <= n; i++) cin >> a[i], sum += a[i]; for (int i = 1; i <= n; i++) cin >> b[i]; for (int i = 1; i <= n; i++) c[i].first = b[i] - a[i], c[i].second = i; sort(c.begin() + 1, c.begin() + 1 + n, greater<pair<int, int>>());
for (int i = 1; i <= n; i++) { idx[c[i].second] = i; } for (int i = 1; i <= n; i++) { add(i, 1); s[i] = s[i - 1] + c[i].first; }
int ans; cin >> q; while (q--) { int m; cin >> m; vector<int> id(m); for (int &x : id) cin >> x, add(idx[x], -1); int l = 1, r = n; while (l < r) { int mid = l + r >> 1; if (getsum(mid) < k) l = mid + 1; else r = mid; } ll res = sum + s[r]; for (int x : id) { if (idx[x] <= r) res -= c[idx[x]].first; add(idx[x], 1); } cout << res << '\n'; } }
#include<bits/stdc++.h> usingnamespace std; #define ll long long #define endl '\n' constint N = 110; ll n, m, k; char s[N][N]; int dp[N][N], cost[N][N], l[N], fa[N]; structA { int u, v, w; booloperator<(const A &a) const { return w < a.w; } } G[N * N]; intlcs(int u, int v) { for (int i = 0; i <= 100; i++) { for (int j = 0; j <= 100; j++) dp[i][j] = 0; } int len1 = l[u]; int len2 = l[v];
for (int i = 1; i <= len1; i++) { for (int j = 1; j <= len2; j++) { if (s[u][i] == s[v][j]) dp[i][j] = dp[i - 1][j - 1] + 1; else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } }
return len1 + len2 - 2 * dp[len1][len2] + k; } intfind(int x) { if (x == fa[x]) return x; else return fa[x] = find(fa[x]); } voidsolve() { cin >> n >> k; int cnt = 0; for (int i = 1; i <= n; i++) { cin >> l[i]; cin >> s[i] + 1; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { if (i == j) continue; G[++cnt] = {i, j, lcs(i, j)}; } }
for (int i = 1; i <= n; i++) { G[++cnt] = {0, i, l[i]}; }
sort(G + 1, G + cnt + 1);
for (int i = 1; i <= n; i++) { fa[i] = i; } int ans = 0, num = n + 1; for (int i = 1; i <= cnt; i++) { auto [u, v, w] = G[i]; u = find(u), v = find(v); if (u == v) continue; ans += w; if (--num == 0) break; fa[u] = v; } cout << ans << '\n'; }