#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<int,int>pii;
typedef pair<int,pii>pi2;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
void solve(){
int n;
cin >> n;
pii arr[2*n];
int counter=0;
int cnt[n+5][2];
memset(cnt,0,sizeof(cnt));
for(int x=0;x<2*n;x++){
cin >> arr[x].first >> arr[x].second;
arr[x].second--;
if(arr[x].first<1){
counter+=1-arr[x].first;
arr[x].first=1;
}
else if(arr[x].first>n){
counter+=arr[x].first-n;
arr[x].first=n;
}
if(arr[x].second<0){
counter+=0-arr[x].second;
arr[x].second=0;
}
else if(arr[x].second>1){
counter+=arr[x].second-1;
arr[x].second=1;
}
cnt[arr[x].first][arr[x].second]++;
}
//for(int x=1;x<=n;x++){
//cout << cnt[x][0] << " ";
//}
//cout << "\n";
//for(int x=1;x<=n;x++){
//cout << cnt[x][1] << " ";
//}
//cout << "\n";
queue<int>storage[2];
vector<pii>v;
int prefix[n+5];
memset(prefix,0,sizeof(prefix));
for(int x=1;x<=n;x++){
while(cnt[x][0]>1){
v.push_back({x,0});
cnt[x][0]--;
prefix[x]++;
}
if(cnt[x][0]==0){
storage[0].push(x);
prefix[x]--;
}
while(cnt[x][1]>1){
v.push_back({x,1});
cnt[x][1]--;
prefix[x]++;
}
if(cnt[x][1]==0){
storage[1].push(x);
prefix[x]--;
}
}
for(int x=1;x<=n;x++) prefix[x]+=prefix[x-1];
vector<int>rgt;
for(int x=1;x<=n;x++){
if(prefix[x]==0){
rgt.push_back(x);
}
}
//show4(rgt,rgt);
for(int x=0;x<(int)v.size();x++){
//cout << v[x].first << " " << v[x].second << "\n";
int index=lower_bound(rgt.begin(),rgt.end(),v[x].first)-rgt.begin();
index=rgt[index];
if(storage[0].empty()||storage[0].front()>index){
counter+=abs(storage[1].front()-v[x].first);
counter+=abs(1-v[x].second);
storage[1].pop();
}
else if(storage[1].empty()||storage[1].front()>index){
counter+=abs(storage[0].front()-v[x].first);
counter+=abs(v[x].second);
storage[0].pop();
}
else{
int mini=min(storage[0].front(),storage[1].front());
int maxi=max(storage[0].front(),storage[1].front());
if(max(v[x].first,mini)<min(maxi,v[x+1].first)){
//special case
if(storage[0].front()<storage[1].front()){
counter+=abs(storage[0].front()-v[x].first);
counter+=abs(v[x].second);
storage[0].pop();
}
else{
counter+=abs(storage[1].front()-v[x].first);
counter+=abs(1-v[x].second);
storage[1].pop();
}
}
else if(v[x].second==0){
counter+=abs(v[x].first-storage[0].front());
storage[0].pop();
}
else{
counter+=abs(v[x].first-storage[1].front());
storage[1].pop();
}
}
}
cout << counter;
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
//freopen("in.txt","r",stdin);
//freopen("in.txt","w",stdout);
int t=1;
//cin >> t;
while(t--){
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |