#include "job.h"
#include <bits/stdc++.h>
using namespace std;
template<class T, class U>
void ckmin(T &a, U b)
{
if (a > b) a = b;
}
template<class T, class U>
void ckmax(T &a, U b)
{
if (a < b) a = b;
}
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
#define SZ(x) ((int) ((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define MAXN 200013
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
typedef pair<ld, int> pdi;
typedef pair<pll, int> ppi;
int N;
int parent[MAXN], link[MAXN];
bitset<MAXN> taken;
vi edge[MAXN];
pll arr[MAXN];
int dsu[MAXN];
struct cmp
{
bool operator() (const ppi &a, const ppi &b) const
{
if (a.fi.fi * b.fi.se != a.fi.se * b.fi.fi)
return a.fi.fi * b.fi.se < a.fi.se * b.fi.fi;
return a.se < b.se;
}
};
set<ppi, cmp> s;
ll ans = 0, t = 0;
int get(int u)
{
return (u == dsu[u] ? u : dsu[u] = get(dsu[u]));
}
void merge(int u, int v)
{
u = get(u); v = get(v);
if (u == v) return;
if (u < v) swap(u, v);
dsu[u] = v;
return;
}
ll scheduling_cost(vi p, vi u, vi d)
{
N = SZ(p);
// cerr << "HI\n";
FOR(i, 0, N)
{
parent[i] = p[i];
if (i == 0) parent[i] = N;
else edge[parent[i]].PB(i);
arr[i] = {u[i], d[i]};
s.insert({{arr[i].fi, arr[i].se}, i});
link[i] = parent[i];
ans += arr[i].fi * arr[i].se;
dsu[i] = i;
// cerr << "parent " << parent[i] << endl;
}
dsu[N] = N;
taken[N] = true;
FOR(i, 0, N)
{
int u = (prev(s.end())) -> se;
// cerr << "ERASE " << u << endl;
s.erase(prev(s.end()));
int v = get(parent[get(u)]);
if (taken[v])
{
ans += arr[u].fi * t;
t += arr[u].se;
taken[u] = true;
continue;
//cool gg
}
// cerr << "BAD " << u << ' ' << v << endl;
// cerr << "ONE\n";
merge(u, v);
// cerr << "SEARCH " << arr[v].fi << ' ' << arr[v].se << ' ' << v << endl;
s.erase(s.find({{arr[v].fi, arr[v].se}, v}));
// cerr << "TWO\n";
ans += arr[v].se * arr[u].fi;
arr[v].fi += arr[u].fi;
arr[v].se += arr[u].se;
s.insert({{arr[v].fi, arr[v].se}, v});
// cerr << "INSERT " << arr[v].fi << ' ' << arr[v].se << ' ' << v << endl;
}
return ans;
//yo uwant to take stuff with high fi/se first
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
4988 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
6 ms |
4984 KB |
Output is correct |
4 |
Correct |
7 ms |
5240 KB |
Output is correct |
5 |
Correct |
100 ms |
13048 KB |
Output is correct |
6 |
Correct |
264 ms |
21036 KB |
Output is correct |
7 |
Correct |
449 ms |
29176 KB |
Output is correct |
8 |
Correct |
683 ms |
37276 KB |
Output is correct |
9 |
Correct |
661 ms |
37240 KB |
Output is correct |
10 |
Correct |
640 ms |
37380 KB |
Output is correct |
11 |
Correct |
6 ms |
4984 KB |
Output is correct |
12 |
Correct |
645 ms |
37244 KB |
Output is correct |
13 |
Correct |
375 ms |
37112 KB |
Output is correct |
14 |
Correct |
410 ms |
37240 KB |
Output is correct |
15 |
Correct |
240 ms |
37212 KB |
Output is correct |
16 |
Correct |
299 ms |
37180 KB |
Output is correct |
17 |
Correct |
612 ms |
37496 KB |
Output is correct |
18 |
Correct |
681 ms |
37164 KB |
Output is correct |
19 |
Correct |
192 ms |
37240 KB |
Output is correct |
20 |
Correct |
173 ms |
37200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5116 KB |
Output is correct |
4 |
Correct |
252 ms |
30288 KB |
Output is correct |
5 |
Correct |
344 ms |
30316 KB |
Output is correct |
6 |
Correct |
278 ms |
30316 KB |
Output is correct |
7 |
Correct |
246 ms |
30360 KB |
Output is correct |
8 |
Correct |
353 ms |
30288 KB |
Output is correct |
9 |
Correct |
248 ms |
30448 KB |
Output is correct |
10 |
Correct |
255 ms |
30444 KB |
Output is correct |
11 |
Correct |
244 ms |
30316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5084 KB |
Output is correct |
2 |
Correct |
7 ms |
4984 KB |
Output is correct |
3 |
Correct |
6 ms |
5112 KB |
Output is correct |
4 |
Correct |
8 ms |
5240 KB |
Output is correct |
5 |
Correct |
14 ms |
6392 KB |
Output is correct |
6 |
Correct |
260 ms |
30828 KB |
Output is correct |
7 |
Correct |
254 ms |
30828 KB |
Output is correct |
8 |
Correct |
254 ms |
30856 KB |
Output is correct |
9 |
Correct |
279 ms |
30956 KB |
Output is correct |
10 |
Correct |
7 ms |
4984 KB |
Output is correct |
11 |
Correct |
7 ms |
5244 KB |
Output is correct |
12 |
Correct |
13 ms |
6136 KB |
Output is correct |
13 |
Correct |
14 ms |
6392 KB |
Output is correct |
14 |
Correct |
270 ms |
30908 KB |
Output is correct |
15 |
Correct |
269 ms |
30828 KB |
Output is correct |
16 |
Correct |
261 ms |
31012 KB |
Output is correct |
17 |
Correct |
245 ms |
30828 KB |
Output is correct |
18 |
Correct |
255 ms |
30844 KB |
Output is correct |
19 |
Correct |
318 ms |
31140 KB |
Output is correct |
20 |
Correct |
262 ms |
30900 KB |
Output is correct |
21 |
Correct |
268 ms |
30956 KB |
Output is correct |
22 |
Correct |
287 ms |
30836 KB |
Output is correct |
23 |
Correct |
267 ms |
30828 KB |
Output is correct |
24 |
Correct |
263 ms |
30988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
2 |
Correct |
643 ms |
37312 KB |
Output is correct |
3 |
Correct |
691 ms |
37112 KB |
Output is correct |
4 |
Correct |
689 ms |
37240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
4984 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
7 ms |
5112 KB |
Output is correct |
4 |
Correct |
6 ms |
5112 KB |
Output is correct |
5 |
Correct |
6 ms |
5112 KB |
Output is correct |
6 |
Correct |
6 ms |
5112 KB |
Output is correct |
7 |
Correct |
7 ms |
4984 KB |
Output is correct |
8 |
Correct |
6 ms |
5112 KB |
Output is correct |
9 |
Correct |
6 ms |
4984 KB |
Output is correct |
10 |
Correct |
6 ms |
5112 KB |
Output is correct |
11 |
Correct |
7 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
4984 KB |
Output is correct |
13 |
Correct |
6 ms |
5068 KB |
Output is correct |
14 |
Correct |
6 ms |
5112 KB |
Output is correct |
15 |
Correct |
6 ms |
4984 KB |
Output is correct |
16 |
Correct |
6 ms |
4984 KB |
Output is correct |
17 |
Correct |
6 ms |
5140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
4988 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
6 ms |
4984 KB |
Output is correct |
4 |
Correct |
7 ms |
5240 KB |
Output is correct |
5 |
Correct |
100 ms |
13048 KB |
Output is correct |
6 |
Correct |
264 ms |
21036 KB |
Output is correct |
7 |
Correct |
449 ms |
29176 KB |
Output is correct |
8 |
Correct |
683 ms |
37276 KB |
Output is correct |
9 |
Correct |
661 ms |
37240 KB |
Output is correct |
10 |
Correct |
640 ms |
37380 KB |
Output is correct |
11 |
Correct |
6 ms |
4984 KB |
Output is correct |
12 |
Correct |
645 ms |
37244 KB |
Output is correct |
13 |
Correct |
375 ms |
37112 KB |
Output is correct |
14 |
Correct |
410 ms |
37240 KB |
Output is correct |
15 |
Correct |
240 ms |
37212 KB |
Output is correct |
16 |
Correct |
299 ms |
37180 KB |
Output is correct |
17 |
Correct |
612 ms |
37496 KB |
Output is correct |
18 |
Correct |
681 ms |
37164 KB |
Output is correct |
19 |
Correct |
192 ms |
37240 KB |
Output is correct |
20 |
Correct |
173 ms |
37200 KB |
Output is correct |
21 |
Correct |
6 ms |
4984 KB |
Output is correct |
22 |
Correct |
6 ms |
5112 KB |
Output is correct |
23 |
Correct |
6 ms |
5116 KB |
Output is correct |
24 |
Correct |
252 ms |
30288 KB |
Output is correct |
25 |
Correct |
344 ms |
30316 KB |
Output is correct |
26 |
Correct |
278 ms |
30316 KB |
Output is correct |
27 |
Correct |
246 ms |
30360 KB |
Output is correct |
28 |
Correct |
353 ms |
30288 KB |
Output is correct |
29 |
Correct |
248 ms |
30448 KB |
Output is correct |
30 |
Correct |
255 ms |
30444 KB |
Output is correct |
31 |
Correct |
244 ms |
30316 KB |
Output is correct |
32 |
Correct |
7 ms |
5084 KB |
Output is correct |
33 |
Correct |
7 ms |
4984 KB |
Output is correct |
34 |
Correct |
6 ms |
5112 KB |
Output is correct |
35 |
Correct |
8 ms |
5240 KB |
Output is correct |
36 |
Correct |
14 ms |
6392 KB |
Output is correct |
37 |
Correct |
260 ms |
30828 KB |
Output is correct |
38 |
Correct |
254 ms |
30828 KB |
Output is correct |
39 |
Correct |
254 ms |
30856 KB |
Output is correct |
40 |
Correct |
279 ms |
30956 KB |
Output is correct |
41 |
Correct |
7 ms |
4984 KB |
Output is correct |
42 |
Correct |
7 ms |
5244 KB |
Output is correct |
43 |
Correct |
13 ms |
6136 KB |
Output is correct |
44 |
Correct |
14 ms |
6392 KB |
Output is correct |
45 |
Correct |
270 ms |
30908 KB |
Output is correct |
46 |
Correct |
269 ms |
30828 KB |
Output is correct |
47 |
Correct |
261 ms |
31012 KB |
Output is correct |
48 |
Correct |
245 ms |
30828 KB |
Output is correct |
49 |
Correct |
255 ms |
30844 KB |
Output is correct |
50 |
Correct |
318 ms |
31140 KB |
Output is correct |
51 |
Correct |
262 ms |
30900 KB |
Output is correct |
52 |
Correct |
268 ms |
30956 KB |
Output is correct |
53 |
Correct |
287 ms |
30836 KB |
Output is correct |
54 |
Correct |
267 ms |
30828 KB |
Output is correct |
55 |
Correct |
263 ms |
30988 KB |
Output is correct |
56 |
Correct |
6 ms |
4984 KB |
Output is correct |
57 |
Correct |
643 ms |
37312 KB |
Output is correct |
58 |
Correct |
691 ms |
37112 KB |
Output is correct |
59 |
Correct |
689 ms |
37240 KB |
Output is correct |
60 |
Correct |
6 ms |
4984 KB |
Output is correct |
61 |
Correct |
6 ms |
4984 KB |
Output is correct |
62 |
Correct |
7 ms |
5112 KB |
Output is correct |
63 |
Correct |
6 ms |
5112 KB |
Output is correct |
64 |
Correct |
6 ms |
5112 KB |
Output is correct |
65 |
Correct |
6 ms |
5112 KB |
Output is correct |
66 |
Correct |
7 ms |
4984 KB |
Output is correct |
67 |
Correct |
6 ms |
5112 KB |
Output is correct |
68 |
Correct |
6 ms |
4984 KB |
Output is correct |
69 |
Correct |
6 ms |
5112 KB |
Output is correct |
70 |
Correct |
7 ms |
5112 KB |
Output is correct |
71 |
Correct |
6 ms |
4984 KB |
Output is correct |
72 |
Correct |
6 ms |
5068 KB |
Output is correct |
73 |
Correct |
6 ms |
5112 KB |
Output is correct |
74 |
Correct |
6 ms |
4984 KB |
Output is correct |
75 |
Correct |
6 ms |
4984 KB |
Output is correct |
76 |
Correct |
6 ms |
5140 KB |
Output is correct |
77 |
Correct |
6 ms |
4984 KB |
Output is correct |
78 |
Correct |
6 ms |
4984 KB |
Output is correct |
79 |
Correct |
6 ms |
4984 KB |
Output is correct |
80 |
Correct |
6 ms |
4984 KB |
Output is correct |
81 |
Correct |
6 ms |
4984 KB |
Output is correct |
82 |
Correct |
6 ms |
5116 KB |
Output is correct |
83 |
Correct |
6 ms |
4984 KB |
Output is correct |
84 |
Correct |
6 ms |
4984 KB |
Output is correct |
85 |
Correct |
6 ms |
4984 KB |
Output is correct |
86 |
Correct |
6 ms |
4984 KB |
Output is correct |
87 |
Correct |
6 ms |
4984 KB |
Output is correct |
88 |
Correct |
12 ms |
5852 KB |
Output is correct |
89 |
Correct |
17 ms |
6264 KB |
Output is correct |
90 |
Correct |
19 ms |
6520 KB |
Output is correct |
91 |
Correct |
90 ms |
12408 KB |
Output is correct |
92 |
Correct |
218 ms |
19576 KB |
Output is correct |
93 |
Correct |
665 ms |
34184 KB |
Output is correct |
94 |
Correct |
586 ms |
34168 KB |
Output is correct |
95 |
Correct |
600 ms |
34184 KB |
Output is correct |
96 |
Correct |
570 ms |
34168 KB |
Output is correct |
97 |
Correct |
540 ms |
33372 KB |
Output is correct |
98 |
Correct |
478 ms |
34908 KB |
Output is correct |
99 |
Correct |
400 ms |
32796 KB |
Output is correct |
100 |
Correct |
593 ms |
34292 KB |
Output is correct |
101 |
Correct |
589 ms |
34984 KB |
Output is correct |
102 |
Correct |
343 ms |
34168 KB |
Output is correct |
103 |
Correct |
553 ms |
34068 KB |
Output is correct |
104 |
Correct |
550 ms |
34892 KB |
Output is correct |
105 |
Correct |
283 ms |
34976 KB |
Output is correct |
106 |
Correct |
277 ms |
31356 KB |
Output is correct |