# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
887068 | stefanneagu | Coin Collecting (JOI19_ho_t4) | C++17 | 0 ms | 460 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int nmax = 3e5 + 1;
int f[nmax][3];
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, ans = 0;
cin >> n;
for(int i = 1; i <= 2 * n; i ++) {
int a, b;
cin >> a >> b;
// cout << i << " ";
// punem in cel mai apropiat loc
if(a < 1) {
//cout << "b mai mic ";
ans += 1 - a;
a = 1;
} else if(a > n) {
//cout << "b mai mare ";
ans += a - n;
a = n;
}
if(b < 1) {
//cout << "a mai mic ";
ans += 1 - b;
b = 1;
} else if(b > 2) {
//cout << "a mai mare ";
ans += b - 2;
b = 2;
}
//cout << endl;
f[a][b] ++;
}
/*
for(int i = 1; i <= 2; i ++) {
for(int j = 1; j <= n; j ++) {
cout << f[i][j] << " ";
}
cout << endl;
}
*/
for(int i = 1; i <= n; i ++) {
for(int j = 1; j <= 2; j ++) {
if(f[i][j] == 0) {
int minn = 1e9, ni, nj;
for(int x = 1; x <= n; x ++) {
for(int y = 1; y <= 2; y ++) {
if(f[x][y] > 1 && minn > abs(x - i) + abs(y - j)) {
ni = x;
nj = y;
minn = abs(x - i) + abs(y - j);
}
}
}
f[i][j] ++;
f[ni][nj] --;
ans += minn;
}
}
}
cout << ans;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |