#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 12, MOD = (int)1e9 + 7;
int n;
int c[N][2],a[N];
int dist(int x,int y,int x1,int y1) {
return abs(x - x1) + abs(y - y1);
}
ll ans = 0;
void test() {
cin >> n;
for(int i = 1;i <= n + n;i++) {
int a,b;
cin >> a >> b;
int x = 1,y = 1;
auto upd = [&](int x1,int y1) {
if(dist(x1,y1,a,b) < dist(x,y,a,b)) {
x = x1;
y = y1;
}
};
upd(1,2);
upd(n,1);
upd(n,2);
if(a >= 1 && a <= n) {
upd(a,1);
upd(a,2);
}
if(b >= 1 && b <= 2) {
upd(1,b);
upd(n,b);
}
c[x][y]++;
ans += dist(x,y,a,b);
}
int x =0,y = 0;
for(int i = 1;i <= n;i++) {
x += c[i][1];
y += c[i][2];
a[i] = c[i][1] + c[i][2];
}
// for(int i = 1;i <= n;i++) {
// cout << c[i][1] << ' ' << c[i][2] << '\n';
// }
ans += abs(x - y) / 2;
vector<int> l,r;
for(int i = 1;i <= n;i++) {
for(int j = 0;j < a[i] - 2;j++) {
l.push_back(i);
}
for(int j = 0;j < 2 - a[i];j++) {
r.push_back(i);
}
}
assert((int)l.size() == (int)r.size());
for(int i = 0;i < (int)l.size();i++) {
ans += abs(l[i] - r[i]);
}
cout << ans;
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--) {
test();
}
}
Compilation message
joi2019_ho_t4.cpp: In function 'void test()':
joi2019_ho_t4.cpp:43:20: warning: array subscript 2 is above array bounds of 'int [2]' [-Warray-bounds]
43 | y += c[i][2];
| ~~~~~~^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |