제출 #1361763

#제출 시각아이디문제언어결과실행 시간메모리
1361763gayCoin Collecting (JOI19_ho_t4)C++20
0 / 100
0 ms344 KiB
#include <bits/stdc++.h>

using namespace std;

using ld = long double;
using ll = long long;

const ll INF = 3e18, MOD = 1e9 + 7;

void solve();

signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int q = 1;
    while (q--) {
        solve();
    }
}

void solve() {
    ll n; cin >> n;
    vector<pair<ll, ll>> a(2 * n + 1);
    ll ans = 0;
    for (int i = 1; i <= 2 * n; i++) {
        cin >> a[i].first >> a[i].second;
        if (abs(1 - a[i].second) < abs(2 - a[i].second)) {
            a[i].second = 1;
            ans += abs(1 - a[i].second);
        } else {
            a[i].second = 2;
            ans += abs(2 - a[i].second);
        }
    }
    sort(a.begin() + 1, a.end());
    vector<ll> dp(n + 1, INF);
    dp[0] = 0;
    for (int i = 1; i <= 2 * n; i++) {
        for (int j = min(ll(i), n); j >= 0; j--) {
            dp[j] = dp[j] + abs(2 - a[i].second) + abs(i - j - a[i].first);
            if (j != 0) {
                dp[j] = min(dp[j], dp[j - 1] + abs(1 - a[i].second) + abs(j - a[i].first));
            }
        }
    }
    cout << dp[n];
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…