#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
bool comp (const pair<int, int> p1, const pair<int, int> p2) {
return p1.first+p1.second < p2.first+p2.second;
}
priority_queue<int> lpq;
priority_queue< int, vector<int>, greater<int> > rpq;
long long lsum, rsum;
void insert (int x) {
if (lpq.size() == 0 || x <= lpq.top()) {
lpq.push(x);
lsum += x;
} else {
rpq.push(x);
rsum += x;
}
if (rpq.size()+1 < lpq.size()) {
int cur = lpq.top();
lpq.pop();
rpq.push(cur);
lsum -= cur;
rsum += cur;
}
if (lpq.size() < rpq.size()) {
int cur = rpq.top();
rpq.pop();
lpq.push(cur);
rsum -= cur;
lsum += cur;
}
}
int main () {
int k, n;
cin >> k >> n;
char ai, ci;
int bi, di;
long long o = 0;
vector< pair<int, int> > pts;
for (int i=0; i<n; i++) {
cin >> ai >> bi >> ci >> di;
if (ai != ci) {
pts.push_back(make_pair(min(bi, di), max(bi, di)));
} else {
o += abs(bi-di);
}
}
n = pts.size();
o += n;
pts.push_back(make_pair(0, 0));
sort(pts.begin(), pts.end(), comp);
long long pre[n+1];
pre[0] = 0;
lsum = rsum = 0;
for (int i=1; i<=n; i++) {
insert(pts[i].first);
insert(pts[i].second);
pre[i] = rsum-lsum;
}
long long ans = pre[n];
if (k == 2) {
while (lpq.size() > 0) lpq.pop();
while (rpq.size() > 0) rpq.pop();
lsum = rsum = 0;
for (int i=n; i>0; i--) {
insert(pts[i].first);
insert(pts[i].second);
ans = min(ans, pre[i-1]+rsum-lsum);
}
}
o += ans;
cout << o << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
3 ms |
384 KB |
Output is correct |
12 |
Correct |
81 ms |
3864 KB |
Output is correct |
13 |
Correct |
170 ms |
5716 KB |
Output is correct |
14 |
Correct |
112 ms |
4588 KB |
Output is correct |
15 |
Correct |
102 ms |
3568 KB |
Output is correct |
16 |
Correct |
123 ms |
4844 KB |
Output is correct |
17 |
Correct |
185 ms |
5480 KB |
Output is correct |
18 |
Correct |
135 ms |
5096 KB |
Output is correct |
19 |
Correct |
168 ms |
5396 KB |
Output is correct |
20 |
Correct |
136 ms |
5356 KB |
Output is correct |
21 |
Correct |
158 ms |
5228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
1 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
1 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
0 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
1 ms |
256 KB |
Output is correct |
12 |
Correct |
1 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
256 KB |
Output is correct |
2 |
Correct |
0 ms |
256 KB |
Output is correct |
3 |
Correct |
0 ms |
256 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
1 ms |
256 KB |
Output is correct |
7 |
Correct |
0 ms |
256 KB |
Output is correct |
8 |
Correct |
1 ms |
256 KB |
Output is correct |
9 |
Correct |
1 ms |
256 KB |
Output is correct |
10 |
Correct |
1 ms |
256 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
256 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
88 ms |
3948 KB |
Output is correct |
26 |
Correct |
122 ms |
4332 KB |
Output is correct |
27 |
Correct |
176 ms |
5356 KB |
Output is correct |
28 |
Correct |
209 ms |
5864 KB |
Output is correct |
29 |
Correct |
203 ms |
6044 KB |
Output is correct |
30 |
Correct |
106 ms |
3184 KB |
Output is correct |
31 |
Correct |
132 ms |
4976 KB |
Output is correct |
32 |
Correct |
206 ms |
5480 KB |
Output is correct |
33 |
Correct |
151 ms |
5100 KB |
Output is correct |
34 |
Correct |
194 ms |
5480 KB |
Output is correct |
35 |
Correct |
148 ms |
5480 KB |
Output is correct |
36 |
Correct |
182 ms |
5228 KB |
Output is correct |
37 |
Correct |
84 ms |
3944 KB |
Output is correct |