This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//UNSTOPPABLE
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define int long long
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define pii pair<int,int>
#define tpii pair <pair <int,int> , int>
#define bruh cout << "NO\n"
using namespace std;
using namespace __gnu_pbds;
const int N = 3e5 + 5;
int mod = 1e9 + 7;
const int INF = 1e18;
int n,a[N],b[N],p[N][3];
int dist(int a , int b , int c , int d){
return abs(a - c) + abs(b - d);
}
void Goldik(){
cin >> n;
int ans = 0;
for(int i = 1 ; i <= n * 2 ; i++){
cin >> a[i] >> b[i];
if(a[i] <= n && b[i] <= 2 && a[i] >= 1 && b[i] >= 1){
p[a[i]][b[i]]++;
continue;
}
if(a[i] >= 1 && a[i] <= n){
if(b[i] < 1){
p[a[i]][1]++;
ans += abs(1 - b[i]);
continue;
}
p[a[i]][2]++;
ans += abs(b[i] - 2);
continue;
}
if(b[i] >= 1 && b[i] <= 2){
if(a[i] < 1){
p[1][b[i]]++;
ans += abs(1 - a[i]);
}
else{
p[n][b[i]]++;
ans += abs(a[i] - n);
}
continue;
}
int x = dist(a[i] , b[i] , 1 , 1) , y = dist(a[i] , b[i] , n , 1) , z = dist(a[i] , b[i] , 1 , 2) , w = dist(a[i] , b[i] , n , 2);
int mn = min({x , y , z , w});
if(mn == x){
ans += x;
p[1][1]++;
continue;
}
if(mn == y){
ans += y;
p[n][1]++;
continue;
}
if(mn == z){
ans += z;
p[1][2]++;
continue;
}
if(mn == w){
ans += w;
p[n][2]++;
continue;
}
}
// for(int i = 1 ; i <= 2 ; i++){
// for(int j = 1 ; j <= n ; j++){
// cout << p[j][i] << ' ';
// }
// cout << '\n';
// }
for(int i = 1 ; i <= n ; i++){
// cout << "i = " << i << '\n';
if(p[i][1] <= 0 && p[i][2] <= 0){
p[i + 1][1] += (p[i][1] - 1);
p[i + 1][2] += (p[i][2] - 1);
ans += abs(p[i][1] - 1) + abs(p[i][2] - 1);
// cout << abs(p[i][1] - 1) + abs(p[i][2] - 1) << '\n';
p[i][1] = 1;
p[i][2] = 1;
continue;
}
if(p[i][1] > 0 && p[i][2] > 0){
p[i + 1][1] += (p[i][1] - 1);
p[i + 1][2] += (p[i][2] - 1);
ans += abs(p[i][1] - 1) + abs(p[i][2] - 1);
// cout << abs(p[i][1] - 1) + abs(p[i][2] - 1) << '\n';
p[i][1] = 1;
p[i][2] = 1;
continue;
}
if(p[i][1] <= 0 && p[i][2] > 0){
int y = 1 - p[i][1];
if(p[i][2] - 1 >= y){
p[i][2] -= y;
p[i][1] += y;
ans += y;
// cout << y << '\n';
}
else{
int z = p[i][2] - 1;
p[i][2] -= z;
p[i][1] += z;
ans += z;
// cout << z << '\n';
}
p[i + 1][1] += (p[i][1] - 1);
p[i + 1][2] += (p[i][2] - 1);
ans += abs(p[i][1] - 1) + abs(p[i][2] - 1);
// cout << abs(p[i][1] - 1) + abs(p[i][2] - 1) << '\n';
p[i][1] = 1;
p[i][2] = 1;
continue;
}
// cout << "P = " << p[i][1] << ' ' << p[i][2] << '\n';
int y = 1 - p[i][2];
// cout << "y = " << y << '\n';
if(p[i][1] - 1 >= y){
p[i][1] -= y;
p[i][2] += y;
ans += y;
// cout << y << '\n';
}
else{
int z = p[i][1] - 1;
// cout << "z = " << z << '\n';
p[i][1] -= z;
p[i][2] += z;
ans += z;
// cout << z << '\n';
}
p[i + 1][1] += (p[i][1] - 1);
p[i + 1][2] += (p[i][2] - 1);
ans += abs(p[i][1] - 1) + abs(p[i][2] - 1);
// cout << abs(p[i][1] - 1) + abs(p[i][2] - 1) << '\n';
p[i][1] = 1;
p[i][2] = 1;
continue;
}
cout << ans << '\n';
}
//rewai mnogo zadach vozmozhno odna iz nih gde to popadetsya
//returning winter prime?
//chem prowe tem luchshe
signed main(/*AZ AZDAN UZDIKSIZ*/){
//freopen("txt.in","r",stdin);
//freopen("txt.out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
srand(time(0));
int TT = 1;
// cin >> TT;
for(int i = 1 ; i <= TT ; i++){
//cout << "Case " << i << ": ";
Goldik();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |