# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
791695 |
2023-07-24T08:52:56 Z |
GEN 이지후(#10078) |
Nestabilnost (COI23_nestabilnost) |
C++17 |
|
1500 ms |
120076 KB |
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pi = array<lint, 2>;
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
const int MAXN = 300005;
int a[MAXN], f[MAXN], sm[MAXN], par[MAXN];
int root[MAXN];
vector<int> gph[MAXN];
void dfs(int x, int p) {
if (p == -1)
root[x] = 1, par[x] = -1;
for (auto &y : gph[x]) {
if (y != p) {
if (a[y] <= a[x] && a[y] > 0)
root[y] = 1;
if (a[y] > a[x] + 1)
root[y] = 1;
par[y] = x;
dfs(y, x);
}
}
}
struct node {
int val;
int canGeq;
lint cost;
lint eval() { return cost + (canGeq ? sm[val] : f[val]); }
bool operator<(const node &nd) const { return val < nd.val; }
};
vector<node> solve(int x, int p) {
vector<vector<node>> aggr;
for (auto &y : gph[x]) {
if (y != p && !root[y]) {
auto v = solve(y, x);
if (a[x] + 1 == a[y]) {
lint totmin = 1e18;
for (auto &x : v)
totmin = min(totmin, x.eval());
v.push_back({0, 1, totmin});
} else {
lint costEq = 1e18;
lint totmin = 1e18;
for (auto &foo : v) {
if (foo.val == a[x] + 1 || (foo.val <= a[x] + 1 && foo.canGeq)) {
costEq = min(costEq, foo.cost);
}
totmin = min(totmin, foo.eval());
}
v.clear();
v.push_back({0, 1, totmin});
v.push_back({a[x] + 1, 0, costEq});
// k = a[x] + 1 or k >= 0
}
aggr.push_back(v);
}
}
if (sz(aggr) == 0) {
vector<node> ret;
ret.push_back({a[x] + 1, 1, 0});
return ret;
}
vector<node> ret;
// generate geqs
{
vector<lint> costPoses(sz(aggr), 1e18);
vector<array<lint, 3>> upd;
for (int i = 0; i < sz(aggr); i++) {
for (auto &y : aggr[i]) {
if (y.canGeq) {
upd.push_back({max(y.val, a[x] + 1), y.cost, i});
}
}
}
int toolarge = sz(aggr);
sort(all(upd));
lint t1 = 1e17;
lint sum = 0;
for (int i = 0; i < sz(upd);) {
int j = i;
while (j < sz(upd) && upd[j][0] == upd[i][0])
j++;
for (int k = i; k < j; k++) {
if (costPoses[upd[k][2]] > 5e17) {
toolarge--;
sum += upd[k][1];
} else {
if (costPoses[upd[k][2]] > upd[k][1]) {
sum += upd[k][1] - costPoses[upd[k][2]];
}
}
costPoses[upd[k][2]] = min(costPoses[upd[k][2]], upd[k][1]);
}
if (toolarge) {
i = j;
continue;
}
if (t1 > sum) {
t1 = sum;
ret.push_back({(int)upd[i][0], 1, t1});
}
i = j;
}
}
// generate eqs (mutate if u like it)
{
vector<lint> costPoses(sz(aggr), 1e18);
vector<array<lint, 3>> upd;
vector<array<lint, 3>> maybe;
vector<int> coords;
for (int i = 0; i < sz(aggr); i++) {
for (auto &y : aggr[i]) {
if (y.canGeq) {
upd.push_back({max(y.val, a[x] + 1), y.cost, i});
} else {
if (y.val > a[x]) {
coords.push_back(y.val);
maybe.push_back({y.val, y.cost, i});
}
}
}
}
sort(all(upd));
sort(all(maybe));
sort(all(coords));
coords.resize(unique(all(coords)) - coords.begin());
int p1 = 0, p2 = 0, toolarge = sz(aggr);
lint sum = 0;
for (auto &x : coords) {
while (p1 < sz(upd) && upd[p1][0] <= x) {
if (costPoses[upd[p1][2]] > 5e17) {
toolarge--;
sum += upd[p1][1];
} else {
if (costPoses[upd[p1][2]] > upd[p1][1]) {
sum += upd[p1][1] - costPoses[upd[p1][2]];
}
}
costPoses[upd[p1][2]] = min(costPoses[upd[p1][2]], upd[p1][1]);
p1++;
}
while (p2 < sz(maybe) && maybe[p2][0] < x)
p2++;
int newlarge = toolarge;
lint newcost = sum;
while (p2 < sz(maybe) && maybe[p2][0] <= x) {
lint prv = costPoses[maybe[p2][2]];
lint cur = maybe[p2][1];
if (prv > cur) {
if (prv > 5e17)
newlarge--, newcost += cur;
else
newcost += cur - prv;
}
p2++;
}
if (newlarge == 0) {
ret.push_back({x, 0, newcost});
}
}
}
return ret;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++) {
cin >> f[i];
}
sm[n + 1] = 1e9;
for (int i = n; i; i--) {
sm[i] = min(sm[i + 1], f[i]);
}
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
gph[u].push_back(v);
gph[v].push_back(u);
}
dfs(0, -1);
lint ans = 0;
for (int i = 0; i < n; i++) {
if (root[i]) {
auto foo = solve(i, par[i]);
lint tot = 1e18;
for (auto &x : foo)
tot = min(tot, x.eval());
ans += tot;
}
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
9172 KB |
Output is correct |
2 |
Correct |
7 ms |
9148 KB |
Output is correct |
3 |
Correct |
7 ms |
9192 KB |
Output is correct |
4 |
Correct |
7 ms |
9260 KB |
Output is correct |
5 |
Correct |
6 ms |
7964 KB |
Output is correct |
6 |
Correct |
9 ms |
7892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
256 ms |
120076 KB |
Output is correct |
2 |
Correct |
253 ms |
120068 KB |
Output is correct |
3 |
Correct |
335 ms |
120040 KB |
Output is correct |
4 |
Correct |
288 ms |
120072 KB |
Output is correct |
5 |
Correct |
273 ms |
120072 KB |
Output is correct |
6 |
Correct |
225 ms |
42432 KB |
Output is correct |
7 |
Correct |
247 ms |
40588 KB |
Output is correct |
8 |
Correct |
300 ms |
41340 KB |
Output is correct |
9 |
Correct |
218 ms |
41400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7380 KB |
Output is correct |
3 |
Correct |
3 ms |
7380 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7380 KB |
Output is correct |
6 |
Correct |
3 ms |
7380 KB |
Output is correct |
7 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
9172 KB |
Output is correct |
2 |
Correct |
7 ms |
9148 KB |
Output is correct |
3 |
Correct |
7 ms |
9192 KB |
Output is correct |
4 |
Correct |
7 ms |
9260 KB |
Output is correct |
5 |
Correct |
6 ms |
7964 KB |
Output is correct |
6 |
Correct |
9 ms |
7892 KB |
Output is correct |
7 |
Correct |
4 ms |
7424 KB |
Output is correct |
8 |
Correct |
5 ms |
7380 KB |
Output is correct |
9 |
Correct |
3 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7380 KB |
Output is correct |
11 |
Correct |
3 ms |
7380 KB |
Output is correct |
12 |
Correct |
3 ms |
7380 KB |
Output is correct |
13 |
Correct |
4 ms |
7380 KB |
Output is correct |
14 |
Correct |
8 ms |
7636 KB |
Output is correct |
15 |
Correct |
8 ms |
7636 KB |
Output is correct |
16 |
Correct |
7 ms |
7664 KB |
Output is correct |
17 |
Correct |
7 ms |
7696 KB |
Output is correct |
18 |
Correct |
9 ms |
7764 KB |
Output is correct |
19 |
Correct |
8 ms |
7768 KB |
Output is correct |
20 |
Correct |
7 ms |
7632 KB |
Output is correct |
21 |
Correct |
9 ms |
7672 KB |
Output is correct |
22 |
Correct |
7 ms |
7636 KB |
Output is correct |
23 |
Correct |
8 ms |
7660 KB |
Output is correct |
24 |
Correct |
23 ms |
8052 KB |
Output is correct |
25 |
Correct |
28 ms |
8076 KB |
Output is correct |
26 |
Correct |
47 ms |
8132 KB |
Output is correct |
27 |
Correct |
55 ms |
8220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
9172 KB |
Output is correct |
2 |
Correct |
7 ms |
9148 KB |
Output is correct |
3 |
Correct |
7 ms |
9192 KB |
Output is correct |
4 |
Correct |
7 ms |
9260 KB |
Output is correct |
5 |
Correct |
6 ms |
7964 KB |
Output is correct |
6 |
Correct |
9 ms |
7892 KB |
Output is correct |
7 |
Correct |
256 ms |
120076 KB |
Output is correct |
8 |
Correct |
253 ms |
120068 KB |
Output is correct |
9 |
Correct |
335 ms |
120040 KB |
Output is correct |
10 |
Correct |
288 ms |
120072 KB |
Output is correct |
11 |
Correct |
273 ms |
120072 KB |
Output is correct |
12 |
Correct |
225 ms |
42432 KB |
Output is correct |
13 |
Correct |
247 ms |
40588 KB |
Output is correct |
14 |
Correct |
300 ms |
41340 KB |
Output is correct |
15 |
Correct |
218 ms |
41400 KB |
Output is correct |
16 |
Correct |
4 ms |
7424 KB |
Output is correct |
17 |
Correct |
5 ms |
7380 KB |
Output is correct |
18 |
Correct |
3 ms |
7380 KB |
Output is correct |
19 |
Correct |
4 ms |
7380 KB |
Output is correct |
20 |
Correct |
3 ms |
7380 KB |
Output is correct |
21 |
Correct |
3 ms |
7380 KB |
Output is correct |
22 |
Correct |
4 ms |
7380 KB |
Output is correct |
23 |
Correct |
8 ms |
7636 KB |
Output is correct |
24 |
Correct |
8 ms |
7636 KB |
Output is correct |
25 |
Correct |
7 ms |
7664 KB |
Output is correct |
26 |
Correct |
7 ms |
7696 KB |
Output is correct |
27 |
Correct |
9 ms |
7764 KB |
Output is correct |
28 |
Correct |
8 ms |
7768 KB |
Output is correct |
29 |
Correct |
7 ms |
7632 KB |
Output is correct |
30 |
Correct |
9 ms |
7672 KB |
Output is correct |
31 |
Correct |
7 ms |
7636 KB |
Output is correct |
32 |
Correct |
8 ms |
7660 KB |
Output is correct |
33 |
Correct |
23 ms |
8052 KB |
Output is correct |
34 |
Correct |
28 ms |
8076 KB |
Output is correct |
35 |
Correct |
47 ms |
8132 KB |
Output is correct |
36 |
Correct |
55 ms |
8220 KB |
Output is correct |
37 |
Correct |
364 ms |
29492 KB |
Output is correct |
38 |
Correct |
384 ms |
29540 KB |
Output is correct |
39 |
Correct |
376 ms |
30372 KB |
Output is correct |
40 |
Correct |
421 ms |
30736 KB |
Output is correct |
41 |
Correct |
352 ms |
29508 KB |
Output is correct |
42 |
Correct |
310 ms |
29456 KB |
Output is correct |
43 |
Correct |
375 ms |
29864 KB |
Output is correct |
44 |
Correct |
378 ms |
31484 KB |
Output is correct |
45 |
Correct |
387 ms |
35356 KB |
Output is correct |
46 |
Correct |
287 ms |
29940 KB |
Output is correct |
47 |
Correct |
287 ms |
30328 KB |
Output is correct |
48 |
Correct |
277 ms |
30308 KB |
Output is correct |
49 |
Correct |
321 ms |
30280 KB |
Output is correct |
50 |
Execution timed out |
1578 ms |
44396 KB |
Time limit exceeded |
51 |
Halted |
0 ms |
0 KB |
- |