#include <bits/stdc++.h>
using namespace std;
typedef pair<long long, long long> ii;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
long long ans = 0ll;
int n;
cin >> n;
long long arr[n][2];
for(int i = 0;i < n;i++){
arr[i][0] = 0;
arr[i][1] = 0;
}
for(int i = 0;i < 2 * n;i++){
long long x, y;
cin >> x >> y;
if(x <= 1){
ans += abs(x-1);
x = 0;
}
else if(x >= n){
ans += abs(x-n);
x = n-1;
}
if(y <= 1){
arr[x][0]++;
ans += abs(y-1);
}
else{
arr[x][1]++;
ans += abs(y-2);
}
}
for(int i = 0;i < n - 1;i++){
long long x = arr[i][0];
long long y = arr[i][1];
long long ds[3] = {0ll, x - 1, 1 - y};
ii best = ii(1203123214213,-1);
for(int j = 0;j < 3;j++){
best = min(best, ii(abs(ds[j]) + abs(x-ds[j]-1) + abs(y+ds[j]-1),ds[j]));
}
ans += best.first;
arr[i+1][0] += (arr[i][0] - best.second - 1);
arr[i+1][1] += (arr[i][1] + best.second - 1);
}
ans += abs(arr[n-1][0] - 1);
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |