#include <bits/stdc++.h>
using namespace std;
template<typename T>
class twoPitchers {
private:
priority_queue<T> lower;
priority_queue<T, vector<T>, greater<T>> upper;
T lowerSum, upperSum;
public:
twoPitchers() {
lowerSum = 0;
upperSum = 0;
}
void upd(const T& val) {
if (lower.empty()) {
lower.push(val);
lowerSum += val;
return;
}
T med = lower.top();
if (val <= med) {
lower.push(val);
lowerSum += val;
if (lower.size() > upper.size() + 1) {
upper.push(med);
upperSum += med;
lower.pop();
lowerSum -= med;
}
return;
}
upper.push(val);
upperSum += val;
if (upper.size() > lower.size()) {
med = upper.top();
upper.pop();
upperSum -= med;
lower.push(med);
lowerSum += med;
}
}
T qry() {
T med = lower.top();
T upperDist = upperSum - med * upper.size();
T lowerDist = med * lower.size() - lowerSum;
return lowerDist + upperDist;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int k, n;
cin >> k >> n;
if (k == 1) {
long long same = 0;
vector<int> pa;
for (int i = 0; i < n; i++) {
char z, zz;
int p, pp;
cin >> z >> p >> zz >> pp;
if (z == zz) {
same += abs(p - pp);
} else {
pa.push_back(p);
pa.push_back(pp);
}
}
if (!pa.empty()) {
sort(pa.begin(), pa.end());
int loc = pa[pa.size() / 2];
for (int i = 0; i < (int) pa.size(); i++) {
same += abs(pa[i] - loc);
}
same += (pa.size() / 2);
}
cout << same << '\n';
return 0;
}
long long same = 0;
vector<array<int, 3>> pa;
for (int i = 0; i < n; i++) {
char z, zz;
int p, pp;
cin >> z >> p >> zz >> pp;
if (z == zz) {
same += abs(p - pp);
} else {
pa.emplace_back(array<int, 3>{p + pp, p, pp});
}
}
if (!pa.empty()) {
n = pa.size();
sort(pa.begin(), pa.end());
twoPitchers<long long> L, R;
vector<long long> costR(n);
for (int i = n - 1; i >= 0; i--) {
R.upd(pa[i][1]);
R.upd(pa[i][2]);
costR[i] = n - i + R.qry();
}
long long res = costR[0];
for (int i = 0; i + 1 < n; i++) {
L.upd(pa[i][1]);
L.upd(pa[i][2]);
res = min(res, i + 1 + L.qry() + costR[i + 1]);
}
same += res;
}
cout << same << '\n';
return 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 |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
424 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 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 |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
22 ms |
1484 KB |
Output is correct |
13 |
Correct |
45 ms |
1388 KB |
Output is correct |
14 |
Correct |
33 ms |
1484 KB |
Output is correct |
15 |
Correct |
26 ms |
848 KB |
Output is correct |
16 |
Correct |
31 ms |
1492 KB |
Output is correct |
17 |
Correct |
32 ms |
1484 KB |
Output is correct |
18 |
Correct |
39 ms |
1492 KB |
Output is correct |
19 |
Correct |
44 ms |
1412 KB |
Output is correct |
20 |
Correct |
32 ms |
1380 KB |
Output is correct |
21 |
Correct |
43 ms |
1484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
308 KB |
Output is correct |
3 |
Correct |
1 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 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 |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
2 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 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 |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
46 ms |
6732 KB |
Output is correct |
26 |
Correct |
64 ms |
6368 KB |
Output is correct |
27 |
Correct |
78 ms |
7004 KB |
Output is correct |
28 |
Correct |
79 ms |
6348 KB |
Output is correct |
29 |
Correct |
78 ms |
6300 KB |
Output is correct |
30 |
Correct |
41 ms |
3652 KB |
Output is correct |
31 |
Correct |
54 ms |
6676 KB |
Output is correct |
32 |
Correct |
58 ms |
6708 KB |
Output is correct |
33 |
Correct |
75 ms |
6772 KB |
Output is correct |
34 |
Correct |
74 ms |
6708 KB |
Output is correct |
35 |
Correct |
52 ms |
6708 KB |
Output is correct |
36 |
Correct |
71 ms |
6712 KB |
Output is correct |
37 |
Correct |
40 ms |
6680 KB |
Output is correct |