-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53d0765
commit ab9e72b
Showing
5 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
typedef long long ll; | ||
|
||
int n, q; | ||
pair<int, int> a[300005], qy[300005]; | ||
int ft[300005]; | ||
|
||
void update(int x, int v) { | ||
while (x <= 300000){ | ||
ft[x] += v; | ||
x += x & -x; | ||
} | ||
} | ||
|
||
int query(int x){ | ||
int res = 0; | ||
while (x){ | ||
res += ft[x]; | ||
x -= x & -x; | ||
} | ||
return res; | ||
} | ||
|
||
int query(int x, int y){ | ||
return query(y) - query(x - 1); | ||
} | ||
|
||
int32_t main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | ||
cin >> n >> q; | ||
for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second; | ||
for (int i = 0; i < n; i++) cin >> qy[i].first >> qy[i].second; | ||
int idx = 0, maxh = 0, sum = 0; | ||
for (int x = 0; x < q; x++) { | ||
int A = qy[x].first, H = qy[x].second; | ||
if (x == 0 || A > qy[x - 1].first) { | ||
sum = 0; | ||
int it = (maxh + A - 1) / A; | ||
for (int i = 0; i < it; i++) | ||
sum += query(i * A + 1, i * A + A) * (i + 1); | ||
} | ||
while (sum < H && idx < n) { | ||
sum += a[idx].first * ((a[idx].second + A - 1) / A); | ||
update(a[idx].second, a[idx].first); | ||
maxh = max(maxh, a[idx].second); | ||
idx++; | ||
} | ||
if (sum < H) cout << -1 << '\n'; | ||
else cout << idx << '\n'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
typedef long long ll; | ||
|
||
int n, q, depth[200005], pre[200005], post[200005], timer = 1; | ||
vector<int> g[200005]; | ||
vector<int> st[200005]; | ||
|
||
void dfs(int v) { | ||
pre[v] = timer++; | ||
for (const int u : g[v]) { | ||
depth[u] = depth[v] + 1; | ||
dfs(u); | ||
} | ||
post[v] = timer - 1; | ||
} | ||
|
||
int32_t main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | ||
cin >> n; | ||
for (int i = 2; i <= n; i++) { | ||
int x; cin >> x; | ||
g[x].push_back(i); | ||
} | ||
|
||
depth[1] = 0; | ||
dfs(1); | ||
|
||
for (int i = 1; i <= n; i++) st[depth[i]].push_back(pre[i]); | ||
for (int i = 1; i <= n; i++) sort(st[i].begin(), st[i].end()); | ||
|
||
cin >> q; | ||
while (q--) { | ||
int c, t; cin >> c >> t; | ||
if (t < depth[c]) { cout << 0 << '\n'; continue; } | ||
cout << upper_bound(st[t].begin(), st[t].end(), post[c]) | ||
- lower_bound(st[t].begin(), st[t].end(), pre[c]) << '\n'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
typedef long long ll; | ||
|
||
int n, m, k; string s; | ||
int pos[200005], pre[200005]; vector<int> v; | ||
int dp[200005][5]; | ||
|
||
bool check(int x) { | ||
for (int i = 0; i <= n; i++) { | ||
for (int j = 0; j <= k; j++) { | ||
if (i == 0) { dp[i][j] = 1e9; continue; } | ||
if (i - x >= 1) dp[i][j] = min(dp[i - 1][j], dp[pos[i - x]][j - 1] + pre[i] - pre[i - x]); | ||
else dp[i][j] = dp[i - 1][j]; | ||
} | ||
} | ||
cout << dp[n][k] << ' '; | ||
return dp[n][k] <= m; | ||
} | ||
|
||
int32_t main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | ||
int t; cin >> t; | ||
while (t--) { | ||
cin >> n >> m >> k >> s; | ||
for (int i = n - 1; i >= 0; i--) { | ||
v.push_back(i + 1); | ||
if (s[i] == '0') { | ||
while (!v.empty()) { | ||
pos[v.back()] = i; | ||
v.pop_back(); | ||
} | ||
} | ||
} | ||
|
||
pre[0] = 0; | ||
for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + (s[i - 1] == '1'); | ||
|
||
int l = 1, r = n, ans = -1; | ||
while (l <= r) { | ||
int m = (l + r) / 2; | ||
if (check(m)) l = m + 1, ans = l; | ||
else r = m - 1; | ||
} | ||
|
||
cout << ans << '\n'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
typedef long long ll; | ||
|
||
void test_case(int test) { | ||
int n; cin >> n; | ||
int a[n + 1]; for (int i = 1; i <= n; i++) cin >> a[i]; | ||
|
||
int dp[n + 1][n + 1][n + 1]; | ||
for (int i = 1; i <= n; i++) for (int j = 0; j <= n; j++) for (int k = 0; k <= n; k++) { | ||
int ai = a[i]; | ||
{ | ||
int nj = j > 0 ? j + 1 : 0; | ||
int nk = max(0, k - 1); | ||
dp[i + 1][nj][nk] = min(dp[i + 1][nj][nk], dp[i][j][k]); | ||
} | ||
{ | ||
int nj = j > 0 ? j + 1 : 0; | ||
if (nj <= ai) nj = 0; | ||
int nk = max(0, k - 1); | ||
dp[i + 1][nj][nk] = min(dp[i + 1][nj][nk], dp[i][j][k] + 1); | ||
} | ||
{ | ||
int nj = j > 0 ? j + 1 : 0; | ||
int nk = max(a[i], k - 1); | ||
dp[i + 1][nj][nk] = min(dp[i + 1][nj][nk], dp[i][j][k] + 1); | ||
} | ||
} | ||
} | ||
|
||
int32_t main() { | ||
ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); | ||
int t; cin >> t; | ||
for (int i = 1; i <= t; i++) test_case(i); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
main:main.cpp | ||
url:https://codeforces.com/contest/1927/problem/G |