#include <bits/stdc++.h>
using namespace std;
#define ss second
#define ff first
typedef long long ll;
typedef long double ld;
#define int ll
int mod = 1e9+7;
bool cmp(pair<int, int> a, pair<int, int> b) {
return a.first + a.second < b.first + b.second;
}
priority_queue<int> lpq;
priority_queue<int, vector<int>, greater<int>> rpq;
ll lsum, rsum;
void insert(int x) {
int median = (lpq.size() ? lpq.top() : 1000000001);
if (x <= median) {
lpq.push(x);
lsum += x;
} else {
rpq.push(x);
rsum += x;
}
if (rpq.size() + 1 < lpq.size()) {
int nxt = lpq.top();
lpq.pop();
rpq.push(nxt);
lsum -= nxt;
rsum += nxt;
} else if (lpq.size() < rpq.size()) {
int nxt = rpq.top();
rpq.pop();
lpq.push(nxt);
rsum -= nxt;
lsum += nxt;
}
}
ll pref[100001];
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int k, n;
ll same_side = 0;
vector<pair<int, int>> v = {{0, 0}};
cin >> k >> n;
for(int i = 0; i < n; ++i) {
char a, b;
int x, y;
cin >> a >> x >> b >> y;
if (a == b) same_side += abs(x - y);
else v.push_back({x, y});
}
sort(v.begin(), v.end(), cmp);
n = v.size() - 1;
same_side += n;
lsum = rsum = 0;
for(int i = 1; i <= n; ++i) {
insert(v[i].first);
insert(v[i].second);
pref[i] = rsum - lsum;
}
ll ans = pref[n];
if (k == 2) {
while (lpq.size()) lpq.pop();
while (rpq.size()) rpq.pop();
lsum = rsum = 0;
for (int i = n; i; i--) {
insert(v[i].first);
insert(v[i].second);
ans = min(ans, rsum - lsum + pref[i - 1]);
}
}
cout << same_side + ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
524 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
504 KB |
Output is correct |
7 |
Correct |
1 ms |
504 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
23 ms |
6860 KB |
Output is correct |
13 |
Correct |
44 ms |
8280 KB |
Output is correct |
14 |
Correct |
33 ms |
7700 KB |
Output is correct |
15 |
Correct |
24 ms |
5064 KB |
Output is correct |
16 |
Correct |
24 ms |
7628 KB |
Output is correct |
17 |
Correct |
32 ms |
8388 KB |
Output is correct |
18 |
Correct |
30 ms |
8388 KB |
Output is correct |
19 |
Correct |
37 ms |
8356 KB |
Output is correct |
20 |
Correct |
26 ms |
8152 KB |
Output is correct |
21 |
Correct |
35 ms |
8216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
504 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
508 KB |
Output is correct |
8 |
Correct |
1 ms |
472 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
504 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
508 KB |
Output is correct |
7 |
Correct |
1 ms |
368 KB |
Output is correct |
8 |
Correct |
1 ms |
504 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
352 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
352 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
352 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
504 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
352 KB |
Output is correct |
23 |
Correct |
1 ms |
352 KB |
Output is correct |
24 |
Correct |
1 ms |
352 KB |
Output is correct |
25 |
Correct |
32 ms |
6872 KB |
Output is correct |
26 |
Correct |
57 ms |
7640 KB |
Output is correct |
27 |
Correct |
69 ms |
7840 KB |
Output is correct |
28 |
Correct |
72 ms |
8656 KB |
Output is correct |
29 |
Correct |
71 ms |
7888 KB |
Output is correct |
30 |
Correct |
38 ms |
4308 KB |
Output is correct |
31 |
Correct |
34 ms |
7372 KB |
Output is correct |
32 |
Correct |
58 ms |
8396 KB |
Output is correct |
33 |
Correct |
53 ms |
7628 KB |
Output is correct |
34 |
Correct |
58 ms |
8384 KB |
Output is correct |
35 |
Correct |
41 ms |
8084 KB |
Output is correct |
36 |
Correct |
57 ms |
8144 KB |
Output is correct |
37 |
Correct |
31 ms |
6860 KB |
Output is correct |