답안 #634585

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
634585 2022-08-24T15:15:16 Z slime Coin Collecting (JOI19_ho_t4) C++14
0 / 100
1 ms 212 KB
#include "bits/stdc++.h"
#define int long long
using namespace std;
 
struct coin {
    int ox, oy; // original x, y
    int nx, ny; // new x, y
    int cost = 0;
};
void solve(int tc) {
    int n;
    cin >> n;
    coin c[2*n + 1];
    for(int i=1; i<=2*n; i++) {
        cin >> c[i].ox >> c[i].oy;
        if(c[i].ox > n) {
            if(c[i].oy >= 2) { // Choose (n, 2)
                c[i].nx = n;
                c[i].ny = 2;
            }
            else { // Choose (n, 1)
                c[i].nx = n;
                c[i].ny = 1;
            }
        }
        else if(c[i].ox < 1) {
            if(c[i].oy >= 2) {
                c[i].nx = 1;
                c[i].ny = 2;
            }
            else {
                c[i].nx = 1;
                c[i].ny = 1;
            }
        }
        else { 
            if(c[i].oy >= 2) {
                c[i].nx = c[i].ox;
                c[i].ny = 2;
            }
            else {
                c[i].nx = c[i].ox;
                c[i].ny = 1;
            }
        }
 
        c[i].cost = abs(c[i].ox - c[i].nx) + abs(c[i].oy - c[i].ny);
    }
    int base = 0;
    for(int i=1; i<=2*n; i++) base += c[i].cost;
    int cnt[n+1][3];
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=2; j++) {
            cnt[i][j] = 0;
        }
    }
 
    for(int i=1; i<=2*n; i++) cnt[c[i].nx][c[i].ny]++;
 
    queue<pair<int, int> > zero, pos;
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=2; j++) {
            if(cnt[i][j] > 1) {
                for(int k=0; k<cnt[i][j]-1; k++) pos.push({i, j});
            }
            else if(cnt[i][j] == 0) {
                zero.push({i, j});
            }

            while(zero.size() && pos.size()) {
                base += abs(zero.front().first - pos.front().first);
                base += abs(zero.front().second - pos.front().second);
                zero.pop();
                pos.pop();
            }
        }
    }

    cout << base << '\n';
 
}
int32_t main() {
    int t = 1;
    // cin >> t;
    for(int i=1; i<=t; i++) solve(i);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Incorrect 0 ms 212 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Incorrect 0 ms 212 KB Output isn't correct
7 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Incorrect 0 ms 212 KB Output isn't correct
7 Halted 0 ms 0 KB -