답안 #883226

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
883226 2023-12-04T20:02:03 Z SalihSahin Coin Collecting (JOI19_ho_t4) C++14
0 / 100
0 ms 348 KB
#include<bits/stdc++.h>
#define pb push_back
#define int long long
#define mp make_pair
 
using namespace std;
const int inf = 2e18;
const int mod = 1e9 + 7;
const int N = 1e5 + 5;

int hizalay(int &y){
    if(y >= 2){
        int k = y - 2;
        y = 2;
        return k;
    }
    else{
        int k = 1 - y;
        y = 1;
        return k;
    }
}

int hizalax(int &x, int n){
    if(x >= n){
        int k = x - n;
        x = n;
        return k;
    }
    else if(x <= 1){
        int k = 1 - x;
        x = 1;
        return k;
    }
    else return 0;
}

int32_t main(){
    ios_base::sync_with_stdio(false), cin.tie();
    int n;
    cin>>n;
    vector<pair<int, int> > a(n*2);
    vector<vector<int> > grid(n+1, vector<int>(2));
    int ans = 0, diff = 0, sz;

    for(int i = 0; i < 2*n; i++){
        cin>>a[i].first>>a[i].second;
        ans += hizalay(a[i].second);
        ans += hizalax(a[i].first, n);
        a[i].first--, a[i].second--;
        grid[a[i].first][a[i].second]++;
    }

    vector<int> rem[2];
    for(int i = 0; i < n; i++){
        int cnt = min((int)(rem[1].size()), grid[i][1]);
        sz = rem[1].size();
        for(int j = sz-1; j >= sz - cnt; j--){
            ans += (i - rem[1][j]);
            rem[1].pop_back();
        }
        grid[i][1] -= cnt;

        cnt = min((int)(rem[0].size()), grid[i][0]);
        sz = rem[0].size();
        for(int j = sz-1; j >= sz - cnt; j--){
            ans += (i - rem[0][j]);
            rem[0].pop_back();
        }
        grid[i][0] -= cnt;

        if(rem[0].size() && grid[i][1]){
            cnt = min((int)(rem[0].size()), grid[i][1]);
            sz = rem[0].size();
            for(int j = sz-1; j >= sz - cnt; j--){
                ans += (i - rem[0][j]);
                rem[0].pop_back();
            }
            grid[i][1] -= cnt;
        }
        else if(rem[1].size() && grid[i][0]){
            cnt = min((int)(rem[1].size()), grid[i][0]);
            sz = rem[1].size();
            for(int j = sz-1; j >= sz - cnt; j--){
                ans += (i - rem[1][j]);
                rem[1].pop_back();
            }
            grid[i][0] -= cnt;
        }
        if(grid[i][1] > 1){
            int ver = grid[i][1] - 1;
            grid[i+1][1] += ver;
            grid[i][1] -= ver;
            ans += ver;
        }
        if(grid[i][0] > 1){
            int ver = grid[i][0] - 1;
            grid[i+1][0] += ver;
            grid[i][0] -= ver;
            ans += ver;
        }

        if(!grid[i][1]) rem[1].pb(i);
        if(!grid[i][0]) rem[0].pb(i);
    }
    if(!grid[n-1][0] || !grid[n-1][1]) ans++; // sonda aşağı yukarı hamle yapmadık

    cout<<ans<<endl;
    return 0;
}

Compilation message

joi2019_ho_t4.cpp: In function 'int32_t main()':
joi2019_ho_t4.cpp:44:18: warning: unused variable 'diff' [-Wunused-variable]
   44 |     int ans = 0, diff = 0, sz;
      |                  ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Incorrect 0 ms 348 KB Output isn't correct
3 Halted 0 ms 0 KB -