답안 #391400

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
391400 2021-04-18T16:58:56 Z timmyfeng Coin Collecting (JOI19_ho_t4) C++17
0 / 100
1 ms 328 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 100000;

int coins[N][2];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    long long ans = 0;
    for (int i = 0; i < 2 * n; ++i) {
        int x, y;
        cin >> x >> y;
        --x, --y;

        if (x < 0) {
            ans += -x;
            x = 0;
        } else if (x >= n) {
            ans += x - n + 1;
            x = n - 1;
        }

        if (y <= 0) {
            ans += -y;
            ++coins[x][0];
        } else {
            ans += y - 1;
            ++coins[x][1];
        }
    }

    int k = 0;
    for (int i = 0; i < n; ++i) {
        while (coins[k][0] + coins[k][1] == 0) {
            ++k;
        }

        if (min(coins[k][0], coins[k][1]) > 0) {
            ans += 2 * abs(k - i);
            --coins[k][0], --coins[k][1];
        } else {
            int j = coins[k][0] > 0;
            ans += abs(k - i);
            --coins[k][1 - j];

            while (coins[k][0] + coins[k][1] == 0) {
                ++k;
            }

            if (coins[k][j] > 0) {
                ans += abs(k - i);
                --coins[k][j];
            } else {
                ans += abs(k - i) + 1;
                --coins[k][1 - j];
            }
        }
    }

    cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Incorrect 1 ms 204 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Incorrect 1 ms 204 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 328 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Incorrect 1 ms 204 KB Output isn't correct
7 Halted 0 ms 0 KB -