#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template <class T>
void read(T &x)
{
x = 0;
register int c;
while ((c = getchar()) && (c > '9' || c < '0'))
;
for (; c >= '0' && c <= '9'; c = getchar())
x = x * 10 + c - '0';
}
constexpr bool typetest = 0;
constexpr int N = 2e5 + 5;
constexpr int Inf = 1e9 + 7;
struct dsu
{
int par[N];
dsu()
{
memset(par, -1, sizeof par);
}
int findpar(int v)
{
return par[v] < 0 ? v : par[v] = findpar(par[v]);
}
} f;
struct Fraction
{
ll a, b;
Fraction(ll a = 0, ll b = 1) : a(a), b(b)
{
Normalize();
}
inline void Normalize()
{
ll v = __gcd(a, b);
a /= v;
b /= v;
if (b < 0)
{
a = -a;
b = -b;
}
}
bool operator<(const Fraction &x) const
{
return a * x.b < b * x.a;
}
bool operator==(const Fraction &x) const
{
return a * x.b == b * x.a;
}
};
ll scheduling_cost(vector<int> p, vector<int> u, vector<int> d)
{
int n = p.size();
/*if (count(p.begin(), p.end(), 0) == n - 1)
{
vector<int> id(n);
for (int i = 0; i < n; ++i)
id[i] = i;
sort(id.begin() + 1, id.end(), [&](const int &x, const int &y)
{ return d[x] * u[y] < d[y] * u[x]; });
ll ans(0), time(0);
for (int i = 0; i < n; ++i)
{
time += d[id[i]];
//cout << id[i] << " " << time << "\n";
ans += u[id[i]] * time;
}
return ans;
}*/
ll ans(0), time(0);
vector<Fraction> ud(n);
struct Tque
{
Fraction x;
int v;
Tque(Fraction x = Fraction(0, 1), ll v = 0) : x(x), v(v) {}
bool operator<(const Tque &a) const
{
return x < a.x || (x == a.x && v > a.v);
}
};
priority_queue<Tque> q;
for (int i = 0; i < n; ++i)
{
ud[i] = Fraction(u[i], d[i]);
q.emplace(ud[i], i);
}
while (!q.empty())
{
Tque c = q.top();
q.pop();
if (!(c.x == ud[c.v]))
continue;
if (p[c.v] == -1 || ud[f.findpar(p[c.v])] == Fraction(-1, 1))
{
time += d[c.v];
ans += time * u[c.v];
ud[c.v] = Fraction(-1, 1);
}
else
{
int v = f.findpar(p[c.v]);
ans -= 1ll * u[v] * d[c.v];
u[v] += u[c.v];
d[v] += d[c.v];
ud[v] = Fraction(u[v], d[v]);
f.par[c.v] = v;
q.emplace(ud[v], v);
}
}
return ans;
}
void Read()
{
int n;
cin >> n;
vector<int> p(n), d(n), u(n);
for (int i = 0; i < n; ++i)
cin >> p[i] >> u[i] >> d[i];
cout << scheduling_cost(p, u, d);
}
void Solve()
{
}
Compilation message
job.cpp: In function 'void read(T&)':
job.cpp:12:18: warning: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
12 | register int c;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
984 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
2 ms |
1100 KB |
Output is correct |
5 |
Correct |
51 ms |
5436 KB |
Output is correct |
6 |
Correct |
104 ms |
9656 KB |
Output is correct |
7 |
Correct |
172 ms |
15544 KB |
Output is correct |
8 |
Correct |
245 ms |
17496 KB |
Output is correct |
9 |
Correct |
258 ms |
17464 KB |
Output is correct |
10 |
Correct |
245 ms |
17428 KB |
Output is correct |
11 |
Correct |
1 ms |
972 KB |
Output is correct |
12 |
Correct |
244 ms |
17468 KB |
Output is correct |
13 |
Correct |
216 ms |
17500 KB |
Output is correct |
14 |
Correct |
213 ms |
17484 KB |
Output is correct |
15 |
Correct |
180 ms |
17504 KB |
Output is correct |
16 |
Correct |
244 ms |
17508 KB |
Output is correct |
17 |
Correct |
247 ms |
17548 KB |
Output is correct |
18 |
Correct |
236 ms |
17568 KB |
Output is correct |
19 |
Correct |
167 ms |
17564 KB |
Output is correct |
20 |
Correct |
130 ms |
17560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
132 ms |
15300 KB |
Output is correct |
5 |
Correct |
165 ms |
15288 KB |
Output is correct |
6 |
Correct |
134 ms |
15288 KB |
Output is correct |
7 |
Correct |
137 ms |
15316 KB |
Output is correct |
8 |
Correct |
138 ms |
15332 KB |
Output is correct |
9 |
Correct |
172 ms |
15316 KB |
Output is correct |
10 |
Correct |
135 ms |
15308 KB |
Output is correct |
11 |
Correct |
133 ms |
15336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
7 ms |
2064 KB |
Output is correct |
6 |
Correct |
145 ms |
15436 KB |
Output is correct |
7 |
Correct |
143 ms |
15416 KB |
Output is correct |
8 |
Correct |
146 ms |
15660 KB |
Output is correct |
9 |
Correct |
153 ms |
15428 KB |
Output is correct |
10 |
Correct |
1 ms |
972 KB |
Output is correct |
11 |
Correct |
2 ms |
1228 KB |
Output is correct |
12 |
Correct |
6 ms |
2052 KB |
Output is correct |
13 |
Correct |
7 ms |
2064 KB |
Output is correct |
14 |
Correct |
140 ms |
15368 KB |
Output is correct |
15 |
Correct |
175 ms |
15484 KB |
Output is correct |
16 |
Correct |
143 ms |
15396 KB |
Output is correct |
17 |
Correct |
145 ms |
15392 KB |
Output is correct |
18 |
Correct |
147 ms |
15364 KB |
Output is correct |
19 |
Correct |
153 ms |
15568 KB |
Output is correct |
20 |
Correct |
141 ms |
15448 KB |
Output is correct |
21 |
Correct |
142 ms |
15344 KB |
Output is correct |
22 |
Correct |
149 ms |
15448 KB |
Output is correct |
23 |
Correct |
139 ms |
15416 KB |
Output is correct |
24 |
Correct |
139 ms |
15416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
269 ms |
15408 KB |
Output is correct |
3 |
Correct |
243 ms |
17496 KB |
Output is correct |
4 |
Correct |
242 ms |
17464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
972 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1076 KB |
Output is correct |
11 |
Correct |
1 ms |
972 KB |
Output is correct |
12 |
Correct |
1 ms |
972 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
972 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
1 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
972 KB |
Output is correct |
2 |
Correct |
1 ms |
984 KB |
Output is correct |
3 |
Correct |
1 ms |
972 KB |
Output is correct |
4 |
Correct |
2 ms |
1100 KB |
Output is correct |
5 |
Correct |
51 ms |
5436 KB |
Output is correct |
6 |
Correct |
104 ms |
9656 KB |
Output is correct |
7 |
Correct |
172 ms |
15544 KB |
Output is correct |
8 |
Correct |
245 ms |
17496 KB |
Output is correct |
9 |
Correct |
258 ms |
17464 KB |
Output is correct |
10 |
Correct |
245 ms |
17428 KB |
Output is correct |
11 |
Correct |
1 ms |
972 KB |
Output is correct |
12 |
Correct |
244 ms |
17468 KB |
Output is correct |
13 |
Correct |
216 ms |
17500 KB |
Output is correct |
14 |
Correct |
213 ms |
17484 KB |
Output is correct |
15 |
Correct |
180 ms |
17504 KB |
Output is correct |
16 |
Correct |
244 ms |
17508 KB |
Output is correct |
17 |
Correct |
247 ms |
17548 KB |
Output is correct |
18 |
Correct |
236 ms |
17568 KB |
Output is correct |
19 |
Correct |
167 ms |
17564 KB |
Output is correct |
20 |
Correct |
130 ms |
17560 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
1 ms |
972 KB |
Output is correct |
24 |
Correct |
132 ms |
15300 KB |
Output is correct |
25 |
Correct |
165 ms |
15288 KB |
Output is correct |
26 |
Correct |
134 ms |
15288 KB |
Output is correct |
27 |
Correct |
137 ms |
15316 KB |
Output is correct |
28 |
Correct |
138 ms |
15332 KB |
Output is correct |
29 |
Correct |
172 ms |
15316 KB |
Output is correct |
30 |
Correct |
135 ms |
15308 KB |
Output is correct |
31 |
Correct |
133 ms |
15336 KB |
Output is correct |
32 |
Correct |
1 ms |
972 KB |
Output is correct |
33 |
Correct |
1 ms |
972 KB |
Output is correct |
34 |
Correct |
1 ms |
972 KB |
Output is correct |
35 |
Correct |
1 ms |
1100 KB |
Output is correct |
36 |
Correct |
7 ms |
2064 KB |
Output is correct |
37 |
Correct |
145 ms |
15436 KB |
Output is correct |
38 |
Correct |
143 ms |
15416 KB |
Output is correct |
39 |
Correct |
146 ms |
15660 KB |
Output is correct |
40 |
Correct |
153 ms |
15428 KB |
Output is correct |
41 |
Correct |
1 ms |
972 KB |
Output is correct |
42 |
Correct |
2 ms |
1228 KB |
Output is correct |
43 |
Correct |
6 ms |
2052 KB |
Output is correct |
44 |
Correct |
7 ms |
2064 KB |
Output is correct |
45 |
Correct |
140 ms |
15368 KB |
Output is correct |
46 |
Correct |
175 ms |
15484 KB |
Output is correct |
47 |
Correct |
143 ms |
15396 KB |
Output is correct |
48 |
Correct |
145 ms |
15392 KB |
Output is correct |
49 |
Correct |
147 ms |
15364 KB |
Output is correct |
50 |
Correct |
153 ms |
15568 KB |
Output is correct |
51 |
Correct |
141 ms |
15448 KB |
Output is correct |
52 |
Correct |
142 ms |
15344 KB |
Output is correct |
53 |
Correct |
149 ms |
15448 KB |
Output is correct |
54 |
Correct |
139 ms |
15416 KB |
Output is correct |
55 |
Correct |
139 ms |
15416 KB |
Output is correct |
56 |
Correct |
1 ms |
972 KB |
Output is correct |
57 |
Correct |
269 ms |
15408 KB |
Output is correct |
58 |
Correct |
243 ms |
17496 KB |
Output is correct |
59 |
Correct |
242 ms |
17464 KB |
Output is correct |
60 |
Correct |
1 ms |
972 KB |
Output is correct |
61 |
Correct |
1 ms |
972 KB |
Output is correct |
62 |
Correct |
1 ms |
972 KB |
Output is correct |
63 |
Correct |
1 ms |
972 KB |
Output is correct |
64 |
Correct |
1 ms |
972 KB |
Output is correct |
65 |
Correct |
1 ms |
972 KB |
Output is correct |
66 |
Correct |
1 ms |
972 KB |
Output is correct |
67 |
Correct |
1 ms |
972 KB |
Output is correct |
68 |
Correct |
1 ms |
1100 KB |
Output is correct |
69 |
Correct |
1 ms |
1076 KB |
Output is correct |
70 |
Correct |
1 ms |
972 KB |
Output is correct |
71 |
Correct |
1 ms |
972 KB |
Output is correct |
72 |
Correct |
1 ms |
972 KB |
Output is correct |
73 |
Correct |
1 ms |
972 KB |
Output is correct |
74 |
Correct |
1 ms |
972 KB |
Output is correct |
75 |
Correct |
1 ms |
972 KB |
Output is correct |
76 |
Correct |
1 ms |
972 KB |
Output is correct |
77 |
Correct |
1 ms |
972 KB |
Output is correct |
78 |
Correct |
1 ms |
972 KB |
Output is correct |
79 |
Correct |
1 ms |
972 KB |
Output is correct |
80 |
Correct |
1 ms |
972 KB |
Output is correct |
81 |
Correct |
1 ms |
1072 KB |
Output is correct |
82 |
Correct |
1 ms |
972 KB |
Output is correct |
83 |
Correct |
1 ms |
972 KB |
Output is correct |
84 |
Correct |
1 ms |
972 KB |
Output is correct |
85 |
Correct |
1 ms |
1080 KB |
Output is correct |
86 |
Correct |
1 ms |
1080 KB |
Output is correct |
87 |
Correct |
1 ms |
972 KB |
Output is correct |
88 |
Correct |
5 ms |
1596 KB |
Output is correct |
89 |
Correct |
7 ms |
1808 KB |
Output is correct |
90 |
Correct |
8 ms |
2064 KB |
Output is correct |
91 |
Correct |
41 ms |
5356 KB |
Output is correct |
92 |
Correct |
88 ms |
9600 KB |
Output is correct |
93 |
Correct |
215 ms |
17624 KB |
Output is correct |
94 |
Correct |
223 ms |
17592 KB |
Output is correct |
95 |
Correct |
202 ms |
17552 KB |
Output is correct |
96 |
Correct |
204 ms |
17612 KB |
Output is correct |
97 |
Correct |
214 ms |
17664 KB |
Output is correct |
98 |
Correct |
198 ms |
17540 KB |
Output is correct |
99 |
Correct |
178 ms |
17616 KB |
Output is correct |
100 |
Correct |
220 ms |
17680 KB |
Output is correct |
101 |
Correct |
243 ms |
17620 KB |
Output is correct |
102 |
Correct |
152 ms |
17560 KB |
Output is correct |
103 |
Correct |
218 ms |
17744 KB |
Output is correct |
104 |
Correct |
256 ms |
17552 KB |
Output is correct |
105 |
Correct |
180 ms |
17592 KB |
Output is correct |
106 |
Correct |
149 ms |
17596 KB |
Output is correct |