#include <bits/stdc++.h>
#define int long long
#define INF 100000000
using namespace std;
int n;
int calcans(vector<int> &a, vector<int> &b){
int cost = 0;
for(int i = n;i>=1;i--){
int costa = INF, costb = INF, costc = INF;
int bestcost = INF;
//both from a
if(a.size() >= 2){
costa = min(costa, abs(i - a[a.size()-1]) + abs(i - a[a.size()-2]) + 1);
bestcost = min(bestcost, costa);
}
//both from b
if(b.size() >= 2){
costb = min(costb, abs(i - b[b.size()-1]) + abs(i - b[b.size()-2]) + 1);
bestcost = min(bestcost, costb);
}
//one from each
if(a.size() && b.size()){
costc = min(costc, abs(i - a[a.size()-1]) + abs(i - b[b.size()-1]));
bestcost = min(bestcost, costc);
}
if(costa==bestcost){
a.pop_back();
a.pop_back();
}
else if(costb==bestcost){
b.pop_back();
b.pop_back();
}
else{
a.pop_back();
b.pop_back();
}
cost+=bestcost;
}
return cost;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
int cost = 0;
vector<int> v[2];
for(int i = 0;i<(2*n);i++){
int x, y;
cin>>x>>y;
if(y<=1){
cost+=(1 - y);
y = 1;
}
else{
cost+=(y - 2);
y = 2;
}
if(x<1){
cost += (1 - x);
x = 1;
}
if(x>n){
cost += (x - n);
x = n;
}
//cout<<i<<" "<<x<<" "<<y<<" "<<cost<<endl;
v[y-1].push_back(x);
}
sort(v[0].begin(), v[0].end());
sort(v[1].begin(), v[1].end());
cost+=calcans(v[0], v[1]);
cout<<cost<<endl;
}
/*
5
1000000000 1000000000
-1000000000 1000000000
-1000000000 -1000000000
1000000000 -1000000000
-1 -5
-2 2
2 8
4 7
-2 5
7 3
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
512 KB |
Output is correct |
6 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |