Submission #292495

#TimeUsernameProblemLanguageResultExecution timeMemory
292495Aldas25Roller Coaster Railroad (IOI16_railroad)C++14
34 / 100
94 ms11256 KiB
#include "railroad.h"
#include <bits/stdc++.h>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(n) FOR(O, 1, (n))
#define f first
#define s second
#define pb push_back
typedef vector<int> vi;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<pii> vii;

const ll INF = 1e16;

int bitCnt (int x, int n) {
    int ret = 0;
    FOR(i, 0, n-1)
        if (x & (1<<i))
            ret ++;
    return ret;
}

ll dp[1 << 17][20];
ll solveDP (vi s, vi t, int n) {

    //FOR(i, 0, n-1) {
    //    cout << " i = " << i << "  s = " << s[i] << " t = " << t[i] << endl;
    //}

    vii srt;
    FOR(mask, 0, (1<<n)-1) {
        if (mask != 0) srt.pb({bitCnt(mask, n), mask});
        FOR(i, 0, n-1) dp[mask][i] = INF;
    }
    sort(srt.begin(), srt.end());

    //FOR(i, 0, n-1) dp[0][i] = 0;

    for (auto p : srt) {
        int mask = p.s;
        int bcnt = p.f;
        if (bcnt == 1) {
            int b = 0;
            FOR(i, 0, n-1)
                if (mask & (1<<i))
                    b = i;
            dp[mask][b] = 0ll;
        } else {
            FOR(i, 0, n-1) FOR(j, 0, n-1) {
                if (!(mask & (1<<i))) continue;
                if (!(mask & (1<<j))) continue;
                ll newVal = dp[mask ^ (1<<i)][j];
                newVal += max(0ll, (ll)t[j] - s[i]);
                dp[mask][i] = min(dp[mask][i], newVal);
            }
        }
    }

    ll ret = dp[(1<<n)-1][0];
    FOR(i, 0, n-1) ret = min (ret, dp[(1<<n)-1][i]);

    return ret;
}

long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
    int n = (int) s.size();

    if (n <= 16) return solveDP(s, t, n);

    vii srt;
    FOR(i, 0, n-1) {
        srt.pb({t[i],s[i]});
    }
    sort(srt.begin(), srt.end());
    ll ret = 0ll;
    ll curS = 1ll;
    for (auto p : srt) {
        ret += max(0ll, curS - p.s);
        curS = p.f;
    }
    return ret;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...