#include <cstdio>
#include <stdio.h>
#include <stdbool.h>
#include <iostream>
#include <map>
#include <vector>
#include <climits>
#include <stack>
#include <string>
#include <queue>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <cctype>
#include <bitset>
#include <iomanip>
#include <cstring>
#include <numeric>
#include <cassert>
using namespace std;
#define int long long
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, ans=0;
cin>>n;
vector<vector<int> > c(n+1, vector<int>(3, 0));
vector<deque<int> > need(3), got(3);
for (int i=0, a, b; i<2*n; ++i){
cin>>a>>b;
if (a<1)ans+=1-a, a=1;
if (a>n)ans+=a-n, a=n;
if (b<1)ans+=1-b, b=1;
if (b>2)ans+=b-2, b=2;
++c[a][b];
}
for (int i=1; i<=n; ++i){
if (!c[i][1])need[1].pb(i);
if (!c[i][2])need[2].pb(i);
for (int j=1; j<c[i][1]; ++j)got[1].pb(i);
for (int j=1; j<c[i][2]; ++j)got[2].pb(i);
while (need[1].size()&&got[1].size())ans+=abs(need[1][0]-got[1][0]), need[1].pop_front(), got[1].pop_front();
while (need[2].size()&&got[2].size())ans+=abs(need[2][0]-got[2][0]), need[2].pop_front(), got[2].pop_front();
while (need[1].size()&&got[2].size())ans+=abs(need[1][0]-got[2][0])+1, need[1].pop_front(), got[2].pop_front();
while (need[2].size()&&got[1].size())ans+=abs(need[2][0]-got[1][0])+1, need[2].pop_front(), got[1].pop_front();
}
cout<<ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |