#include "bits/stdc++.h"
using namespace std;
#define ar array
#define int long long
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n; cin>>n; n <<= 1;
vector<ar<int, 2>> a(n), b;
int res = 0;
for(int i=0;i<n;i++){
cin>>a[i][0]>>a[i][1];
if(a[i][1] > 2) res += (a[i][1] - 2), a[i][1] = 2;
if(a[i][1] < 1) res += (1 - a[i][1]), a[i][1] = 1;
if(a[i][0] < 1) res += (1 - a[i][0]), a[i][0] = 1;
if(a[i][0] > n/2) res += (a[i][0] - n/2), a[i][0] = n/2;
//~ cout<<a[i][0]<<" "<<a[i][1]<<"\n";
}
vector<ar<int, 2>> used(n / 2);
for(int i=0;i<n;i++){
if(used[a[i][0] - 1][a[i][1] - 1]){
b.push_back(a[i]);
} else {
used[a[i][0] - 1][a[i][1] - 1] = 1;
}
}
a.clear();
swap(a, b);
for(int i=0;i<n/2;i++){
for(int j=0;j<2;j++){
if(used[i][j]) continue;
b.push_back({i + 1, j + 1});
}
}
//~ cout<<"here"<<endl;
sort(a.begin(), a.end());
sort(b.begin(), b.end());
assert(a.size() == b.size());
n = (int)a.size();
deque<int> tmp[2];
for(int i=0;i<n;i++){
res += abs(a[i][0] - b[i][0]);
if(a[i][1] != b[i][1]){
tmp[a[i][1] - 1].push_back(i);
//~ cout<<a[i][0]<<" "<<b[i][0]<<" "<<a[i][1]<<"\n";
} else {
//~ cout<<a[i][0]<<" "<<a[i][1]<<" : "<<b[i][0]<<" "<<b[i][1]<<"\n";
}
}
while(!tmp[0].empty() && !tmp[1].empty()){
int i = tmp[0].front(), j = tmp[1].front();
if(i > j) swap(i, j);
//~ cout<<a[i][0]<<" "<<b[i][0]<<" "<<a[i][1]<<"\n";
//~ cout<<a[j][0]<<" "<<b[j][0]<<" "<<a[j][1]<<"\n\n";
if(abs(a[i][0] - b[j][0]) + abs(a[j][0] - b[i][0]) == abs(a[i][0] - b[i][0]) + abs(a[j][0] - b[j][0])){
tmp[0].pop_front(), tmp[1].pop_front();
} else {
res++;
if(tmp[0].front() == i) tmp[0].pop_front();
else tmp[1].pop_front();
}
}
//~ for(auto i : tmp[0]){
//~ cout<<a[i][0]<<" "<<a[i][1]<<" : "<<b[i][0]<<" "<<b[i][1]<<"\n";
//~ }
//~ for(auto i : tmp[1]){
//~ cout<<a[i][0]<<" "<<a[i][1]<<" : "<<b[i][0]<<" "<<b[i][1]<<"\n";
//~ }
res += (int)tmp[0].size() + (int)tmp[1].size();
cout<<res<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
308 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
308 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
308 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
316 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |