# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
332844 | ace_in_the_hole | Coin Collecting (JOI19_ho_t4) | C++14 | 61 ms | 5868 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
typedef long long Int;
const int MAX = 2e5 + 500;
int cnt[MAX][2];
int SIGN(int x) {
if (x) return x / abs(x); return 0;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
Int answer = 0;
int n; cin >> n;
for (int i = 0; i < n + n; i++) {
int x, y;
cin >> x >> y;
if (x < 1) answer += 1 - x, x = 1;
if (x > n) answer += x - n, x = n;
if (y < 1) answer += 1 - y, y = 1;
if (y > 2) answer += y - 2, y = 2;
// cerr << '(' << x << ',' << y << ")\n";
cnt[--x][--y]++;
}
for (int i = 0; i < n; i++) for (int j = 0; j < 2; j++) {
cnt[i][j]--;
// cerr << "(" << i + 1 << ',' << j + 1 << ") gives " << cnt[i][j] << '\n';
}
for (int i = 0; i + 1 < n; i++) {
if (SIGN(cnt[i][0]) == SIGN(cnt[i][1])) {
// cerr << cnt[i][0] << '/' << cnt[i][1] << '\n';
answer += abs(cnt[i][0]) + abs(cnt[i][1]);
// cerr << "ans += " << abs(cnt[i][0]) + abs(cnt[i][1]) << '\n';
cnt[i + 1][0] += cnt[i][0];
cnt[i + 1][1] += cnt[i][1];
} else {
// cerr << "steps to zero(s) : " << min(abs(cnt[i][0]), abs(cnt[i][1])) << '\n';
if (abs(cnt[i][0]) <= abs(cnt[i][1])) {
answer += abs(cnt[i][0]);
cnt[i][1] += cnt[i][0];
answer += abs(cnt[i][1]);
cnt[i + 1][1] += cnt[i][1];
} else {
answer += abs(cnt[i][1]);
cnt[i][0] += cnt[i][1];
answer += abs(cnt[i][0]);
cnt[i + 1][0] += cnt[i][0];
}
}
}
assert(cnt[n - 1][0] + cnt[n - 1][1] == 0);
answer += abs(cnt[n - 1][0]);
cout << answer;
}
/*
5
1000000000 1000000000
-1000000000 1000000000
-1000000000 -1000000000
1000000000 -1000000000
-1 -5
-2 2
2 8
4 7
-2 5
7 3
4
2 1
2 1
2 1
3 1
3 1
3 1
3 1
3 1
*/
컴파일 시 표준 에러 (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... |