#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct A {
int x,y;
} s[100001];
bool cmp(A a, A b) {
return a.x+a.y<b.x+b.y;
}
int n,k;
ll ans,rsum,lsum,con,g[100001];
priority_queue<int> lsq;
priority_queue<int, vector<int>, greater<int>> rsq;
void add(int x) {
int median = lsq.empty() ? 1000000002 : lsq.top();
if (x<=median) lsq.push(x), lsum+=x;
else rsq.push(x), rsum+=x;
if (lsq.size() < rsq.size()) {
int k=rsq.top();
rsq.pop();
lsq.push(k);
rsum-=k;
lsum+=k;
}
if (lsq.size() > rsq.size()+1) {
int k=lsq.top();
lsq.pop();
rsq.push(k);
rsum+=k;
lsum-=k;
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>k>>n;
int idx=0;
for (int i=0; i<n; ++i) {
char a,b;
int x,y;
cin>>a>>x>>b>>y;
if (a==b) con+=abs(x-y);
else s[idx++]={x,y}, ++con;
}
sort(s,s+idx,cmp);
for (int i=0; i<idx; ++i) add(s[i].x), add(s[i].y), g[i]=rsum-lsum;
ans=rsum-lsum+con;
if (k==1) {
cout<<ans;
return 0;
}
while (!lsq.empty()) lsq.pop();
while (!rsq.empty()) rsq.pop();
lsum=0; rsum=0;
for (int i=idx-1; i>0; --i) {
add(s[i].x); add(s[i].y);
ans=min(ans,rsum-lsum+g[i-1]+con);
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 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 |
340 KB |
Output is correct |
9 |
Correct |
2 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 |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
368 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 |
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 |
12 |
Correct |
27 ms |
2928 KB |
Output is correct |
13 |
Correct |
50 ms |
2956 KB |
Output is correct |
14 |
Correct |
41 ms |
2700 KB |
Output is correct |
15 |
Correct |
32 ms |
2032 KB |
Output is correct |
16 |
Correct |
34 ms |
3040 KB |
Output is correct |
17 |
Correct |
42 ms |
3092 KB |
Output is correct |
18 |
Correct |
37 ms |
2988 KB |
Output is correct |
19 |
Correct |
43 ms |
3100 KB |
Output is correct |
20 |
Correct |
32 ms |
3076 KB |
Output is correct |
21 |
Correct |
44 ms |
3028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
224 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 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 |
212 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 |
328 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
328 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
364 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 |
356 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
212 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 |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 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 |
25 |
Correct |
40 ms |
3768 KB |
Output is correct |
26 |
Correct |
66 ms |
3656 KB |
Output is correct |
27 |
Correct |
79 ms |
4516 KB |
Output is correct |
28 |
Correct |
82 ms |
5028 KB |
Output is correct |
29 |
Correct |
83 ms |
5196 KB |
Output is correct |
30 |
Correct |
44 ms |
2840 KB |
Output is correct |
31 |
Correct |
42 ms |
4412 KB |
Output is correct |
32 |
Correct |
61 ms |
5204 KB |
Output is correct |
33 |
Correct |
65 ms |
4656 KB |
Output is correct |
34 |
Correct |
73 ms |
5192 KB |
Output is correct |
35 |
Correct |
45 ms |
4672 KB |
Output is correct |
36 |
Correct |
65 ms |
4928 KB |
Output is correct |
37 |
Correct |
29 ms |
3600 KB |
Output is correct |