Submission #350063

# Submission time Handle Problem Language Result Execution time Memory
350063 2021-01-19T00:35:56 Z thecodingwizard Job Scheduling (IOI19_job) C++14
100 / 100
196 ms 53704 KB
#include "job.h"
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
#define pb push_back
#define all(x) x.begin(), x.end()
#define ii pair<int, int>
#define f first
#define s second
#define mp make_pair

vector<int> adj[200000];
vector<ll> p, u, d;

struct cmp {
    // returns true if i should go after j
    bool operator()(int i, int j) {
        return d[i]*u[j]>d[j]*u[i];
    }
};
struct cmp2 {
    // returns true if i should go after j
    bool operator()(int i, int j) {
        return d[i]*u[j]>=d[j]*u[i];
    }
};
priority_queue<int, vector<int>, cmp> children[200000];

bool merged[200000];

ll ans = 0;
ll curTime = 0;
void dfs(int node) {
    for (int child : adj[node]) {
        dfs(child);
    }
    for (int child : adj[node]) {
        children[node].push(child);
    }
    while (!children[node].empty()) {
        if (cmp2()(node, children[node].top())) {
            int toMerge = children[node].top();
            children[node].pop();
            ans -= d[toMerge]*u[node];
            d[node] += d[toMerge];
            u[node] += u[toMerge];
            if (children[node].size() < children[toMerge].size()) {
                swap(children[node], children[toMerge]);
            }
            while (children[toMerge].size()) {
                children[node].push(children[toMerge].top());
                children[toMerge].pop();
            }
            merged[toMerge] = true;
        } else {
            break;
        }
    }
}

long long scheduling_cost(std::vector<int> _p, std::vector<int> _u, std::vector<int> _d) {
    int n = _p.size();
    for (int i = 0; i < n; i++) {
        p.pb(_p[i]);
        u.pb(_u[i]);
        d.pb(_d[i]);
    }

    for (int i = 1; i < n; i++) {
        adj[p[i]].pb(i);
    }

    dfs(0);

    vector<int> indices;
    for (int i = 0; i < n; i++) {
        if (!merged[i]) {
            indices.pb(i);
        }
    }
    sort(all(indices), cmp());
    reverse(all(indices));

    ll curTime = 0;

    for (auto x : indices) {
        curTime += d[x];
        ans += curTime*u[x];
    }

    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11244 KB Output is correct
2 Correct 8 ms 11244 KB Output is correct
3 Correct 8 ms 11244 KB Output is correct
4 Correct 8 ms 11500 KB Output is correct
5 Correct 36 ms 21520 KB Output is correct
6 Correct 66 ms 31828 KB Output is correct
7 Correct 95 ms 41928 KB Output is correct
8 Correct 121 ms 52168 KB Output is correct
9 Correct 125 ms 52168 KB Output is correct
10 Correct 122 ms 52168 KB Output is correct
11 Correct 8 ms 11244 KB Output is correct
12 Correct 122 ms 52168 KB Output is correct
13 Correct 121 ms 52168 KB Output is correct
14 Correct 131 ms 52168 KB Output is correct
15 Correct 117 ms 52168 KB Output is correct
16 Correct 120 ms 52168 KB Output is correct
17 Correct 127 ms 52168 KB Output is correct
18 Correct 121 ms 52168 KB Output is correct
19 Correct 125 ms 52168 KB Output is correct
20 Correct 124 ms 53704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11244 KB Output is correct
2 Correct 8 ms 11244 KB Output is correct
3 Correct 8 ms 11244 KB Output is correct
4 Correct 110 ms 24240 KB Output is correct
5 Correct 114 ms 24264 KB Output is correct
6 Correct 112 ms 24392 KB Output is correct
7 Correct 111 ms 24264 KB Output is correct
8 Correct 108 ms 24136 KB Output is correct
9 Correct 109 ms 24440 KB Output is correct
10 Correct 111 ms 24264 KB Output is correct
11 Correct 108 ms 24136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11244 KB Output is correct
2 Correct 8 ms 11244 KB Output is correct
3 Correct 8 ms 11244 KB Output is correct
4 Correct 8 ms 11372 KB Output is correct
5 Correct 14 ms 12008 KB Output is correct
6 Correct 116 ms 24264 KB Output is correct
7 Correct 120 ms 24392 KB Output is correct
8 Correct 115 ms 24264 KB Output is correct
9 Correct 120 ms 24264 KB Output is correct
10 Correct 8 ms 11244 KB Output is correct
11 Correct 8 ms 11372 KB Output is correct
12 Correct 12 ms 12012 KB Output is correct
13 Correct 13 ms 12008 KB Output is correct
14 Correct 115 ms 24264 KB Output is correct
15 Correct 117 ms 24264 KB Output is correct
16 Correct 117 ms 24264 KB Output is correct
17 Correct 116 ms 24264 KB Output is correct
18 Correct 118 ms 24136 KB Output is correct
19 Correct 116 ms 24392 KB Output is correct
20 Correct 116 ms 24264 KB Output is correct
21 Correct 118 ms 24136 KB Output is correct
22 Correct 116 ms 24264 KB Output is correct
23 Correct 116 ms 24392 KB Output is correct
24 Correct 117 ms 24300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11244 KB Output is correct
2 Correct 117 ms 42824 KB Output is correct
3 Correct 120 ms 42824 KB Output is correct
4 Correct 117 ms 42836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11244 KB Output is correct
2 Correct 8 ms 11244 KB Output is correct
3 Correct 8 ms 11244 KB Output is correct
4 Correct 8 ms 11244 KB Output is correct
5 Correct 8 ms 11244 KB Output is correct
6 Correct 8 ms 11244 KB Output is correct
7 Correct 8 ms 11244 KB Output is correct
8 Correct 8 ms 11244 KB Output is correct
9 Correct 8 ms 11244 KB Output is correct
10 Correct 8 ms 11244 KB Output is correct
11 Correct 8 ms 11244 KB Output is correct
12 Correct 8 ms 11244 KB Output is correct
13 Correct 8 ms 11244 KB Output is correct
14 Correct 9 ms 11244 KB Output is correct
15 Correct 8 ms 11372 KB Output is correct
16 Correct 8 ms 11244 KB Output is correct
17 Correct 8 ms 11244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11244 KB Output is correct
2 Correct 8 ms 11244 KB Output is correct
3 Correct 8 ms 11244 KB Output is correct
4 Correct 8 ms 11500 KB Output is correct
5 Correct 36 ms 21520 KB Output is correct
6 Correct 66 ms 31828 KB Output is correct
7 Correct 95 ms 41928 KB Output is correct
8 Correct 121 ms 52168 KB Output is correct
9 Correct 125 ms 52168 KB Output is correct
10 Correct 122 ms 52168 KB Output is correct
11 Correct 8 ms 11244 KB Output is correct
12 Correct 122 ms 52168 KB Output is correct
13 Correct 121 ms 52168 KB Output is correct
14 Correct 131 ms 52168 KB Output is correct
15 Correct 117 ms 52168 KB Output is correct
16 Correct 120 ms 52168 KB Output is correct
17 Correct 127 ms 52168 KB Output is correct
18 Correct 121 ms 52168 KB Output is correct
19 Correct 125 ms 52168 KB Output is correct
20 Correct 124 ms 53704 KB Output is correct
21 Correct 8 ms 11244 KB Output is correct
22 Correct 8 ms 11244 KB Output is correct
23 Correct 8 ms 11244 KB Output is correct
24 Correct 110 ms 24240 KB Output is correct
25 Correct 114 ms 24264 KB Output is correct
26 Correct 112 ms 24392 KB Output is correct
27 Correct 111 ms 24264 KB Output is correct
28 Correct 108 ms 24136 KB Output is correct
29 Correct 109 ms 24440 KB Output is correct
30 Correct 111 ms 24264 KB Output is correct
31 Correct 108 ms 24136 KB Output is correct
32 Correct 8 ms 11244 KB Output is correct
33 Correct 8 ms 11244 KB Output is correct
34 Correct 8 ms 11244 KB Output is correct
35 Correct 8 ms 11372 KB Output is correct
36 Correct 14 ms 12008 KB Output is correct
37 Correct 116 ms 24264 KB Output is correct
38 Correct 120 ms 24392 KB Output is correct
39 Correct 115 ms 24264 KB Output is correct
40 Correct 120 ms 24264 KB Output is correct
41 Correct 8 ms 11244 KB Output is correct
42 Correct 8 ms 11372 KB Output is correct
43 Correct 12 ms 12012 KB Output is correct
44 Correct 13 ms 12008 KB Output is correct
45 Correct 115 ms 24264 KB Output is correct
46 Correct 117 ms 24264 KB Output is correct
47 Correct 117 ms 24264 KB Output is correct
48 Correct 116 ms 24264 KB Output is correct
49 Correct 118 ms 24136 KB Output is correct
50 Correct 116 ms 24392 KB Output is correct
51 Correct 116 ms 24264 KB Output is correct
52 Correct 118 ms 24136 KB Output is correct
53 Correct 116 ms 24264 KB Output is correct
54 Correct 116 ms 24392 KB Output is correct
55 Correct 117 ms 24300 KB Output is correct
56 Correct 8 ms 11244 KB Output is correct
57 Correct 117 ms 42824 KB Output is correct
58 Correct 120 ms 42824 KB Output is correct
59 Correct 117 ms 42836 KB Output is correct
60 Correct 8 ms 11244 KB Output is correct
61 Correct 8 ms 11244 KB Output is correct
62 Correct 8 ms 11244 KB Output is correct
63 Correct 8 ms 11244 KB Output is correct
64 Correct 8 ms 11244 KB Output is correct
65 Correct 8 ms 11244 KB Output is correct
66 Correct 8 ms 11244 KB Output is correct
67 Correct 8 ms 11244 KB Output is correct
68 Correct 8 ms 11244 KB Output is correct
69 Correct 8 ms 11244 KB Output is correct
70 Correct 8 ms 11244 KB Output is correct
71 Correct 8 ms 11244 KB Output is correct
72 Correct 8 ms 11244 KB Output is correct
73 Correct 9 ms 11244 KB Output is correct
74 Correct 8 ms 11372 KB Output is correct
75 Correct 8 ms 11244 KB Output is correct
76 Correct 8 ms 11244 KB Output is correct
77 Correct 8 ms 11244 KB Output is correct
78 Correct 8 ms 11244 KB Output is correct
79 Correct 8 ms 11244 KB Output is correct
80 Correct 8 ms 11244 KB Output is correct
81 Correct 9 ms 11244 KB Output is correct
82 Correct 8 ms 11244 KB Output is correct
83 Correct 8 ms 11244 KB Output is correct
84 Correct 8 ms 11244 KB Output is correct
85 Correct 8 ms 11244 KB Output is correct
86 Correct 8 ms 11244 KB Output is correct
87 Correct 8 ms 11244 KB Output is correct
88 Correct 11 ms 11756 KB Output is correct
89 Correct 13 ms 12012 KB Output is correct
90 Correct 17 ms 12392 KB Output is correct
91 Correct 44 ms 16320 KB Output is correct
92 Correct 92 ms 21460 KB Output is correct
93 Correct 189 ms 31560 KB Output is correct
94 Correct 196 ms 31560 KB Output is correct
95 Correct 190 ms 31688 KB Output is correct
96 Correct 195 ms 31560 KB Output is correct
97 Correct 171 ms 30024 KB Output is correct
98 Correct 194 ms 32968 KB Output is correct
99 Correct 167 ms 29768 KB Output is correct
100 Correct 191 ms 31560 KB Output is correct
101 Correct 180 ms 33224 KB Output is correct
102 Correct 193 ms 32176 KB Output is correct
103 Correct 180 ms 31344 KB Output is correct
104 Correct 182 ms 32456 KB Output is correct
105 Correct 143 ms 33096 KB Output is correct
106 Correct 136 ms 27592 KB Output is correct