#include<iostream>
#include<algorithm>
#include<vector>
#include<math.h>
#define int long long
using namespace std;
const int N=2e5+2;
pair<int,int> coor[N];
vector<int> lis,lis1;
bool used[N],wow[N>>1][2];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n,i,j;
long long ans=0;
cin>>n;
for(i=1;i<=2*n;i++){
cin>>coor[i].first>>coor[i].second;
}
sort(coor+1,coor+1+2*n);
j=1;
bool cac,cac1;
for(i=1;i<=n;i++){
while(j<=2*n&&coor[j].first<=i){
if(coor[j].second>=2){
lis1.push_back(j);
}
else{
lis.push_back(j);
}
j++;
}
cac=false;
cac1=false;
if(!lis.size()&&!lis1.size()){
wow[i][0]=true;
wow[i][1]=true;
continue;
}
if(lis.size()){
ans+=abs(coor[lis.back()].first-i)+abs(coor[lis.back()].second-1);
used[lis.back()]=true;
lis.pop_back();
cac=true;
}
if(lis1.size()){
ans+=abs(coor[lis1.back()].first-i)+abs(coor[lis1.back()].second-2);
used[lis1.back()]=true;
lis1.pop_back();
cac1=true;
}
if(cac&&cac1){
continue;
}
if(!cac){
if(lis1.size()){
ans+=abs(coor[lis1.back()].first-i)+abs(coor[lis1.back()].second-1);
used[lis1.back()]=true;
lis1.pop_back();
}
else{
wow[i][0]=true;
}
continue;
}
if(lis.size()){
ans+=abs(coor[lis.back()].first-i)+abs(coor[lis.back()].second-2);
used[lis.back()]=true;
lis.pop_back();
}
else{
wow[i][1]=true;
}
}
//cout<<"ditmemay "<<ans<<endl;
lis.clear();
lis1.clear();
j=2*n;
for(i=n;i>=1;i--){
while(j&&coor[j].first>=i){
if(!used[j]){
if(coor[j].second>=2){
lis1.push_back(j);
}
else{
lis.push_back(j);
}
}
j--;
}
if(!wow[i][0]&&!wow[i][1]){
continue;
}
if(wow[i][0]){
if(lis.size()){
ans+=abs(coor[lis.back()].first-i)+abs(coor[lis.back()].second-1);
lis.pop_back();
wow[i][0]=false;
}
}
if(wow[i][1]){
if(lis1.size()){
ans+=abs(coor[lis1.back()].first-i)+abs(coor[lis1.back()].second-2);
lis1.pop_back();
wow[i][1]=false;
}
}
if(wow[i][0]){
if(lis1.size()){
ans+=abs(coor[lis1.back()].first-i)+abs(coor[lis1.back()].second-1);
lis1.pop_back();
wow[i][0]=false;
}
}
if(wow[i][1]){
if(lis.size()){
ans+=abs(coor[lis.back()].first-i)+abs(coor[lis.back()].second-2);
lis.pop_back();
wow[i][1]=false;
}
}
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |