#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
vector<vector<int>> v(2, vector<int>(n, 0));
ll ans = 0;
for(int i = 0; i < 2*n; i++){
ll x, y; cin >> x >> y;
if(x > n) ans+=x-n, x = n;
if(x < 1) ans+=-x+1, x = 1;
if(y > 2) ans+=y-2, y = 2;
if(y < 1) ans+=-y+1, y = 1;
x--, y--;
v[y][x]++;
}
//cerr << ans << " d\n";
//for(int x: v[0]) cerr << x << " "; cerr << "d\n";
//for(int x: v[1]) cerr << x << " "; cerr << "d\n";
for(int i = 0; i < n; i++){
for(int k = 0; k < 2; k++){
if(v[k][i] > 0) continue;
cerr << k << " " << i << " ---\n";
pair<int, int> p; int mn = 1e9;
for(int kk = 0; kk < 2; kk++) for(int j = 0; j < n; j++){
if(v[kk][j] <= 1) continue;
if(abs(k-kk)+abs(i-j) < mn) mn = abs(k-kk)+abs(i-j), p = {kk, j};
}
//cerr << p.f << " " << p.sc << " d\n";
v[p.f][p.sc]--;
v[k][i]++;
ans+=abs(k-p.f)+abs(i-p.sc);
}
if(i != n-1){
v[0][i+1]+=v[0][i]-1, ans+=v[0][i]-1, v[0][i] = 1;
v[1][i+1]+=v[1][i]-1, ans+=v[1][i]-1, v[1][i] = 1;
}
}
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
208 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Incorrect |
0 ms |
208 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
208 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Incorrect |
0 ms |
208 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
208 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Incorrect |
0 ms |
208 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |