Submission #399374

# Submission time Handle Problem Language Result Execution time Memory
399374 2021-05-05T15:48:54 Z 12tqian Worst Reporter 4 (JOI21_worst_reporter4) C++17
100 / 100
610 ms 122404 KB
#include <bits/stdc++.h>

using namespace std;

#define f1r(i, a, b) for (int i = a; i < b; ++i)
#define f0r(i, a) f1r(i, 0, a)
#define each(t, a) for (auto& t : a)

#define mp make_pair
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define all(x) begin(x), end(x)

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;

template <class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

template <class T> int get_pos(vector<T>& v, T x) {
    return lower_bound(all(v), x) - v.begin();
}

struct DSU {
    vi e;
    
    void init(int n) {
        e = vi(n, -1);
    }

    int get(int x) {
        return e[x] < 0 ? x : e[x] = get(e[x]);
    }

    int size(int x) {
        return -e[get(x)];
    }

    bool unite(int x, int y) {
        x = get(x), y = get(y);
        if (x == y) return false;
        if (e[x] > e[y]) swap(x, y);
        e[x] += e[y]; e[y] = x;
        return true;
    }
};


int main() {
    cin.tie(0)->sync_with_stdio(0);
    int n; cin >> n;
    vi a(n);
    vi h(n);
    vl c(n);
    vector<bool> vis(n);
    vector<bool> cyc(n);
    vi tmp;
    f0r(i, n) {
        cin >> a[i] >> h[i] >> c[i];
        --a[i];
        tmp.pb(h[i]);
    }
    sort(all(tmp));
    tmp.erase(unique(all(tmp)), tmp.end());
    f0r(i, n) h[i] = get_pos(tmp, h[i]);
    f0r(i, n) h[i] = sz(tmp) - 1 - h[i];
    DSU D; D.init(n);
    f0r(i, n) {
        if (vis[i]) continue;
        int cur = i;
        vi path;
        while (!vis[cur]) {
            vis[cur] = true;
            path.pb(cur);
            cur = a[cur];
        }
        int loc = -1;
        f0r(i, sz(path)) {
            if (path[i] == cur) {
                loc = i;
                break;
            }
        }
        if (loc == -1) continue;
        f1r(i, loc, sz(path)) {
            cyc[path[i]] = true;
            D.unite(cur, path[i]);
        }
    }
    vi v;
    f0r(i, n) v.pb(D.get(i));
    sort(all(v));
    v.erase(unique(all(v)), v.end());
    vi id(n);
    f0r(i, n) id[i] = get_pos(v, D.get(i));
    int sz = sz(v);
    vector<map<int, ll>> dp(sz);
    vector<vi> comps(sz);
    f0r(i, n) {
        comps[id[i]].pb(i);
    }
    vi roots;
    each(x, v) {
        if (cyc[comps[get_pos(v, x)][0]]) {
            roots.pb(get_pos(v, x));
        }
    }
    vector<vi> g(sz);
    f0r(i, n) {
        int x = id[i];
        int y = id[a[i]];
        if (x == y) continue;
        g[x].pb(y);
        g[y].pb(x);
    }
    ll ans = 0;
    int cnt = 0;
    each(r, roots) {
        function<void(int, int)> dfs = [&](int u, int p) {
            each(v, g[u]) {
                if (v == p) continue;
                dfs(v, u);
                if (sz(dp[v]) > sz(dp[u])) swap(dp[u], dp[v]);
                each(x, dp[v]) {
                    dp[u][x.f] += x.s;
                }
            } 
            // add the root
            auto& st = dp[u];
            if (u == r) {
                auto it = st.begin();
                while (it != st.end()) {
                    if (it != st.begin()) {
                        it->s += prev(it)->s;
                    }
                    it = next(it);
                }
                map<int, ll> cost;
                each(vert, comps[u]) {
                    cost[h[vert]] += c[vert];
                }
                ll mx = 0;
                if (sz(st)) mx = st.rbegin()->s;
                each(vert, cost) {
                    int d = vert.f;
                    ll v = vert.s;
                    it = st.upper_bound(d);
                    if (it != st.begin()) {
                        ckmax(mx, prev(it)->s + v);
                    }
                    ckmax(mx, v);
                }
                ans += mx;
            } else {
                int vert = comps[u][0];
                int d = h[vert];
                ll v = c[vert];
                st[d] += v;
                auto it = st.upper_bound(d);
                while (it != st.end() && v) {
                    ll rem = min(v, it->s);
                    it->s -= rem;
                    v -= rem;
                    if (it->s == 0) {
                        it = next(it);
                        st.erase(prev(it));
                    }
                }
            }
        };
        dfs(r, -1);
    }
    ll sum = 0;
    f0r(i, n) sum += c[i];
    ans = sum - ans;
    cout << ans << '\n';
    return 0;
}

/**
 * goes to a[i]
 * maximize the cost if decreasing subsequences up the tree
 * let's consider the cycle at the very end
 * then there is a cost associated with each possible value at the top
 * and a cost associated with nothing
 * first consider the case of fixed
 * things decrease from the root
 * this means that in increasing order it's better
 */

Compilation message

worst_reporter2.cpp: In function 'int main()':
worst_reporter2.cpp:125:9: warning: unused variable 'cnt' [-Wunused-variable]
  125 |     int cnt = 0;
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 9 ms 2252 KB Output is correct
6 Correct 7 ms 1740 KB Output is correct
7 Correct 7 ms 1484 KB Output is correct
8 Correct 9 ms 2204 KB Output is correct
9 Correct 7 ms 1740 KB Output is correct
10 Correct 6 ms 1484 KB Output is correct
11 Correct 7 ms 1356 KB Output is correct
12 Correct 7 ms 2124 KB Output is correct
13 Correct 5 ms 2124 KB Output is correct
14 Correct 6 ms 1748 KB Output is correct
15 Correct 8 ms 1740 KB Output is correct
16 Correct 12 ms 2624 KB Output is correct
17 Correct 7 ms 1996 KB Output is correct
18 Correct 5 ms 1400 KB Output is correct
19 Correct 7 ms 1996 KB Output is correct
20 Correct 6 ms 1868 KB Output is correct
21 Correct 5 ms 1868 KB Output is correct
22 Correct 9 ms 1868 KB Output is correct
23 Correct 5 ms 1524 KB Output is correct
24 Correct 8 ms 1996 KB Output is correct
25 Correct 6 ms 1868 KB Output is correct
26 Correct 5 ms 2124 KB Output is correct
27 Correct 9 ms 1996 KB Output is correct
28 Correct 7 ms 2124 KB Output is correct
29 Correct 9 ms 2252 KB Output is correct
30 Correct 7 ms 2124 KB Output is correct
31 Correct 7 ms 2124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 2232 KB Output is correct
2 Correct 606 ms 92600 KB Output is correct
3 Correct 439 ms 72764 KB Output is correct
4 Correct 531 ms 94548 KB Output is correct
5 Correct 433 ms 72664 KB Output is correct
6 Correct 306 ms 53764 KB Output is correct
7 Correct 261 ms 49216 KB Output is correct
8 Correct 325 ms 80700 KB Output is correct
9 Correct 248 ms 80636 KB Output is correct
10 Correct 187 ms 80664 KB Output is correct
11 Correct 265 ms 61980 KB Output is correct
12 Correct 233 ms 61884 KB Output is correct
13 Correct 479 ms 122404 KB Output is correct
14 Correct 376 ms 86804 KB Output is correct
15 Correct 173 ms 49208 KB Output is correct
16 Correct 411 ms 74904 KB Output is correct
17 Correct 237 ms 68096 KB Output is correct
18 Correct 201 ms 67932 KB Output is correct
19 Correct 412 ms 67996 KB Output is correct
20 Correct 198 ms 55572 KB Output is correct
21 Correct 431 ms 75652 KB Output is correct
22 Correct 232 ms 68500 KB Output is correct
23 Correct 213 ms 80768 KB Output is correct
24 Correct 349 ms 76680 KB Output is correct
25 Correct 317 ms 83164 KB Output is correct
26 Correct 309 ms 86572 KB Output is correct
27 Correct 313 ms 79928 KB Output is correct
28 Correct 304 ms 79904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 9 ms 2252 KB Output is correct
6 Correct 7 ms 1740 KB Output is correct
7 Correct 7 ms 1484 KB Output is correct
8 Correct 9 ms 2204 KB Output is correct
9 Correct 7 ms 1740 KB Output is correct
10 Correct 6 ms 1484 KB Output is correct
11 Correct 7 ms 1356 KB Output is correct
12 Correct 7 ms 2124 KB Output is correct
13 Correct 5 ms 2124 KB Output is correct
14 Correct 6 ms 1748 KB Output is correct
15 Correct 8 ms 1740 KB Output is correct
16 Correct 12 ms 2624 KB Output is correct
17 Correct 7 ms 1996 KB Output is correct
18 Correct 5 ms 1400 KB Output is correct
19 Correct 7 ms 1996 KB Output is correct
20 Correct 6 ms 1868 KB Output is correct
21 Correct 5 ms 1868 KB Output is correct
22 Correct 9 ms 1868 KB Output is correct
23 Correct 5 ms 1524 KB Output is correct
24 Correct 8 ms 1996 KB Output is correct
25 Correct 6 ms 1868 KB Output is correct
26 Correct 5 ms 2124 KB Output is correct
27 Correct 9 ms 1996 KB Output is correct
28 Correct 7 ms 2124 KB Output is correct
29 Correct 9 ms 2252 KB Output is correct
30 Correct 7 ms 2124 KB Output is correct
31 Correct 7 ms 2124 KB Output is correct
32 Correct 9 ms 2232 KB Output is correct
33 Correct 606 ms 92600 KB Output is correct
34 Correct 439 ms 72764 KB Output is correct
35 Correct 531 ms 94548 KB Output is correct
36 Correct 433 ms 72664 KB Output is correct
37 Correct 306 ms 53764 KB Output is correct
38 Correct 261 ms 49216 KB Output is correct
39 Correct 325 ms 80700 KB Output is correct
40 Correct 248 ms 80636 KB Output is correct
41 Correct 187 ms 80664 KB Output is correct
42 Correct 265 ms 61980 KB Output is correct
43 Correct 233 ms 61884 KB Output is correct
44 Correct 479 ms 122404 KB Output is correct
45 Correct 376 ms 86804 KB Output is correct
46 Correct 173 ms 49208 KB Output is correct
47 Correct 411 ms 74904 KB Output is correct
48 Correct 237 ms 68096 KB Output is correct
49 Correct 201 ms 67932 KB Output is correct
50 Correct 412 ms 67996 KB Output is correct
51 Correct 198 ms 55572 KB Output is correct
52 Correct 431 ms 75652 KB Output is correct
53 Correct 232 ms 68500 KB Output is correct
54 Correct 213 ms 80768 KB Output is correct
55 Correct 349 ms 76680 KB Output is correct
56 Correct 317 ms 83164 KB Output is correct
57 Correct 309 ms 86572 KB Output is correct
58 Correct 313 ms 79928 KB Output is correct
59 Correct 304 ms 79904 KB Output is correct
60 Correct 1 ms 204 KB Output is correct
61 Correct 1 ms 224 KB Output is correct
62 Correct 1 ms 204 KB Output is correct
63 Correct 1 ms 224 KB Output is correct
64 Correct 610 ms 81812 KB Output is correct
65 Correct 453 ms 64896 KB Output is correct
66 Correct 553 ms 82324 KB Output is correct
67 Correct 435 ms 65016 KB Output is correct
68 Correct 334 ms 51200 KB Output is correct
69 Correct 307 ms 47672 KB Output is correct
70 Correct 257 ms 21404 KB Output is correct
71 Correct 132 ms 12928 KB Output is correct
72 Correct 274 ms 24988 KB Output is correct
73 Correct 140 ms 13756 KB Output is correct
74 Correct 338 ms 47096 KB Output is correct
75 Correct 189 ms 34564 KB Output is correct
76 Correct 151 ms 34484 KB Output is correct
77 Correct 339 ms 47064 KB Output is correct
78 Correct 188 ms 34484 KB Output is correct
79 Correct 403 ms 61276 KB Output is correct
80 Correct 288 ms 44084 KB Output is correct
81 Correct 212 ms 33424 KB Output is correct
82 Correct 290 ms 25260 KB Output is correct
83 Correct 188 ms 37624 KB Output is correct
84 Correct 395 ms 52360 KB Output is correct
85 Correct 400 ms 52332 KB Output is correct
86 Correct 370 ms 49380 KB Output is correct
87 Correct 387 ms 52352 KB Output is correct
88 Correct 416 ms 52368 KB Output is correct