#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
int n,k, res;
struct F{
priority_queue<int> L;
priority_queue<int> R;
int opti =0;
int insert(int v){
int ans =0;
if(v>opti){
R.push(-v);
R.push(-v);
ans = (abs(-v - (R.top())));
L.push(-R.top());
R.pop();
}
else{
L.push(v);
L.push(v);
ans = (abs(v - (L.top())));
R.push(-L.top());
L.pop();
}
opti = L.top();
return ans;
}
};
signed main(){
cin>>n>>k;
vector<pii> inters;
for(int i = 0; i<k; i++){
char a, b;
int pa, pb;
cin>>a>>pa>>b>>pb;
if(pa>pb){
swap(pa, pb);
}
////cout<<a<<" "<<b<<endl;
if(a!=b){
inters.push_back(pii(pa, pb));
}
else{
res += abs(pb-pa);
}
}
////cout<<"done "<<inters.size()<<endl;
auto cmp =[&](pii& a, pii& b){
return a.first+a.second<b.first+b.second;
};
sort(inters.begin(), inters.end(), cmp);
int p = inters.size();
vector<pii> scores(p+1, pii(0, 0));
F Lf;
int s= 0;
for(int i = 0; i<p; i++){
s+= Lf.insert(inters[i].first) + Lf.insert(inters[i].second);
scores[i].first = s;
}
////cout<<"right "<<endl;
F Rf;
s= 0LL;
for(int i = p-1; i>=0; i--){
s += Rf.insert(inters[i].first) + Rf.insert(inters[i].second);
scores[i].second = s;
}
int cost_cross= 1e18;
for(int i = 0; i<p; i++){
//cout<<scores[i].first<<" "<<scores[i].second<<endl;
cost_cross = min(cost_cross, scores[i].first + scores[i+1].second);
}
res+=p;
if(p==0){
cout<<res<<endl;
}
else if(n==2){
cout<<res+cost_cross<<endl;
}
else{
cout<<scores[p-1].first +res<<endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
436 KB |
Output is correct |
6 |
Correct |
2 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 |
436 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
444 KB |
Output is correct |
7 |
Correct |
1 ms |
440 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 |
57 ms |
12788 KB |
Output is correct |
13 |
Correct |
129 ms |
13112 KB |
Output is correct |
14 |
Correct |
94 ms |
12408 KB |
Output is correct |
15 |
Correct |
75 ms |
7656 KB |
Output is correct |
16 |
Correct |
77 ms |
13004 KB |
Output is correct |
17 |
Correct |
108 ms |
13020 KB |
Output is correct |
18 |
Correct |
107 ms |
13068 KB |
Output is correct |
19 |
Correct |
116 ms |
13056 KB |
Output is correct |
20 |
Correct |
83 ms |
12552 KB |
Output is correct |
21 |
Correct |
107 ms |
12968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
300 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
300 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
2 ms |
428 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
304 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 |
440 KB |
Output is correct |
21 |
Correct |
1 ms |
440 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
436 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
300 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
304 KB |
Output is correct |
12 |
Correct |
1 ms |
212 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 |
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 |
440 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 |
56 ms |
12732 KB |
Output is correct |
26 |
Correct |
83 ms |
13500 KB |
Output is correct |
27 |
Correct |
119 ms |
14172 KB |
Output is correct |
28 |
Correct |
127 ms |
14188 KB |
Output is correct |
29 |
Correct |
128 ms |
14876 KB |
Output is correct |
30 |
Correct |
65 ms |
7832 KB |
Output is correct |
31 |
Correct |
72 ms |
13672 KB |
Output is correct |
32 |
Correct |
111 ms |
14304 KB |
Output is correct |
33 |
Correct |
100 ms |
13888 KB |
Output is correct |
34 |
Correct |
111 ms |
14276 KB |
Output is correct |
35 |
Correct |
101 ms |
13352 KB |
Output is correct |
36 |
Correct |
109 ms |
14008 KB |
Output is correct |
37 |
Correct |
55 ms |
12928 KB |
Output is correct |