#include "closing.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
namespace solLinear {
int n;
int pl[3030], pr[3030];
ll qs[3030];
ll qsx[3030], qsy[3030], qsmx[3030];
ll vx[3030], vy[3030];
int max_score(int N, int X, int Y, ll K, vector<int> U, vector<int> V, vector<int> W) {
n = N;
int x = X + 1, y = Y + 1;
for (int i = 1;i < n;i++) {
qs[i] = qs[i - 1] + W[i - 1];
}
for (int i = 1;i <= n;i++) {
vx[i] = qs[max(x, i) - 1] - qs[min(x, i) - 1];
vy[i] = qs[max(y, i) - 1] - qs[min(y, i) - 1];
qsx[i] = qsx[i - 1] + vx[i];
qsy[i] = qsy[i - 1] + vy[i];
qsmx[i] = qsmx[i - 1] + max(vx[i], vy[i]);
}
int mx = 0;
{ // not intercept
vector<bool> vis(n + 1, false);
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
pq.push({ 0ll, x });
pq.push({ 0ll, -y });
ll cost = 0;
while (!pq.empty()) {
auto x = pq.top();
pq.pop();
int v = x.second, vv = abs(x.second);
if (vis[vv]) continue;
vis[vv] = 1;
if (cost + x.first > K) break;
cost += x.first;
mx++;
if (vv != 1 && !vis[vv - 1])
pq.push({ (v < 0) ? vy[vv - 1] : vx[vv - 1], (v < 0) ? v + 1 : v - 1 });
if (vv != n && !vis[vv + 1])
pq.push({ (v < 0) ? vy[vv + 1] : vx[vv + 1], (v < 0) ? v - 1 : v + 1 });
}
}
pl[0] = x - 1, pr[0] = y + 1;
for (int i = 1;i <= n;i++) {
pl[i] = pl[i - 1], pr[i] = pr[i - 1];
if (pl[i] == 0 && pr[i] == n + 1) continue;
if (pr[i] == n + 1) pl[i]--;
else if (pl[i] == 0) pr[i]++;
else {
if (vx[pl[i]] < vy[pr[i]])
pl[i]--;
else
pr[i]++;
}
}
// intercept
for (int l = 1;l <= y;l++) {
for (int r = max(l, x);r <= n;r++) {
int cnt = (r - l + 1) * 2;
ll cost = qsmx[r] - qsmx[l - 1];
if (r < y) cost += qsy[y] - qsy[r], cnt += y - r;
if (x < l) cost += qsx[l - 1] - qsx[x - 1], cnt += l - x;
if (cost > K) continue;
int L = min(x, l) - 1, R = max(y, r) + 1;
int lo = 0, hi = n;
while (lo < hi) {
int mi = (lo + hi + 1) / 2;
ll w = 0;
if (pl[mi] <= L) w += qsx[L] - qsx[pl[mi]];
if (R <= pr[mi]) w += qsy[pr[mi] - 1] - qsy[R - 1];
if (cost + w > K) hi = mi - 1;
else lo = mi;
}
cnt += max(0, L - pl[lo]) + max(0, pr[hi] - R);
mx = max(mx, cnt);
}
}
return mx;
}
};
namespace subtask1 {
int n;
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
bool vis[200200];
vector<pair<int, int>> adj[200200];
ll dx[200200], dy[200200];
void dfs(int v, int p, ll dis[]) {
for (auto& [x, w] : adj[v]) {
if (x == p) continue;
dis[x] = dis[v] + w;
dfs(x, v, dis);
}
}
int max_score(int N, int X, int Y, ll K, vector<int> U, vector<int> V, vector<int> W) {
n = N;
for (int i = 0;i < n - 1;i++) {
adj[U[i]].push_back({ V[i], W[i] });
adj[V[i]].push_back({ U[i], W[i] });
}
dx[X] = dy[Y] = 0;
dfs(X, -1, dx);
dfs(Y, -1, dy);
pq.push({ 0, X }), vis[X] = 1;
pq.push({ 0, Y }), vis[Y] = 1;
int cnt = 0;
while (!pq.empty()) {
auto x = pq.top();
int v = pq.top().second;
ll w = pq.top().first;
pq.pop();
if (K - w < 0) break;
K -= w;
cnt++;
for (auto& [x, w] : adj[v]) {
if (vis[x]) continue;
vis[x] = 1;
pq.push({ min(dx[x], dy[x]), x });
}
}
while (!pq.empty()) pq.pop();
for (int i = 0;i < n;i++) adj[i].clear(), vis[i] = 0;
return cnt;
}
};
int n;
int max_score(int N, int X, int Y, ll K, vector<int> U, vector<int> V, vector<int> W) {
n = N;
{
bool linear = 1;
for (int i = 0;i < n - 1;i++) {
linear &= U[i] == i && V[i] == i + 1;
}
if (linear) return solLinear::max_score(N, X, Y, K, U, V, W);
}
return subtask1::max_score(N, X, Y, K, U, V, W);
}
Compilation message
closing.cpp: In function 'int subtask1::max_score(int, int, int, ll, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:110:18: warning: variable 'x' set but not used [-Wunused-but-set-variable]
110 | auto x = pq.top();
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4952 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
30008 KB |
Output is correct |
2 |
Correct |
80 ms |
35156 KB |
Output is correct |
3 |
Correct |
40 ms |
10068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4952 KB |
Output is correct |
4 |
Correct |
2 ms |
5008 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
3 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4952 KB |
Output is correct |
4 |
Correct |
2 ms |
5008 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
3 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4952 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
3 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
5160 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
5148 KB |
Output is correct |
19 |
Correct |
3 ms |
5208 KB |
Output is correct |
20 |
Correct |
2 ms |
5212 KB |
Output is correct |
21 |
Correct |
4 ms |
5044 KB |
Output is correct |
22 |
Correct |
3 ms |
5160 KB |
Output is correct |
23 |
Correct |
4 ms |
5208 KB |
Output is correct |
24 |
Correct |
3 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4952 KB |
Output is correct |
4 |
Correct |
2 ms |
5008 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
3 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
3 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4952 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
3 ms |
4956 KB |
Output is correct |
15 |
Correct |
2 ms |
5160 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
5148 KB |
Output is correct |
19 |
Correct |
3 ms |
5208 KB |
Output is correct |
20 |
Correct |
2 ms |
5212 KB |
Output is correct |
21 |
Correct |
4 ms |
5044 KB |
Output is correct |
22 |
Correct |
3 ms |
5160 KB |
Output is correct |
23 |
Correct |
4 ms |
5208 KB |
Output is correct |
24 |
Correct |
3 ms |
5212 KB |
Output is correct |
25 |
Correct |
4 ms |
5208 KB |
Output is correct |
26 |
Correct |
54 ms |
5212 KB |
Output is correct |
27 |
Correct |
5 ms |
5208 KB |
Output is correct |
28 |
Correct |
109 ms |
5212 KB |
Output is correct |
29 |
Correct |
105 ms |
5468 KB |
Output is correct |
30 |
Correct |
7 ms |
5212 KB |
Output is correct |
31 |
Correct |
15 ms |
5468 KB |
Output is correct |
32 |
Correct |
15 ms |
5468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4952 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4952 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4952 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4952 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4952 KB |
1st lines differ - on the 1st token, expected: '6', found: '5' |
2 |
Halted |
0 ms |
0 KB |
- |