Submission #1042819

#TimeUsernameProblemLanguageResultExecution timeMemory
1042819ZicrusRoller Coaster Railroad (IOI16_railroad)C++17
0 / 100
2044 ms10308 KiB
#include <bits/stdc++.h>
#include "railroad.h"
using namespace std;

typedef long long ll;

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

    vector<pair<ll, ll>> a(n);
    for (int i = 0; i < n; i++) {
        a[i] = {s[i], t[i]};
    }
    mt19937 mt(time(0));

    ll finalRes = 1ll << 62ll;
    bool done;
    for (int k = 0; k < 1000000; k++) {
        shuffle(a.begin(), a.end(), mt);
        ll res = 0;
        for (int i = 1; i < n; i++) {
            res += max(0ll, a[i-1].second - a[i].first);
        }

        do {
            done = true;
            for (int i = 1; i < n; i++) {
                ll prev = max(0ll, a[i-1].second - a[i].first);
                ll nw = max(0ll, a[i].second - a[i-1].first);
                if (i > 1) {
                    prev += max(0ll, a[i-2].second - a[i-1].first);
                    nw += max(0ll, a[i-2].second - a[i].first);
                }
                if (i < n-1) {
                    prev += max(0ll, a[i].second - a[i+1].first);
                    nw += max(0ll, a[i-1].second - a[i+1].first);
                }
                if (nw < prev) {
                    swap(a[i-1], a[i]);
                    done = false;
                    res += nw - prev;
                }
            }
            for (int i = 0; i < n-2; i++) {
                for (int j = i+2; j < n; j++) {
                    ll prev = max(0ll, a[i].second - a[i+1].first) + max(0ll, a[j-1].second - a[j].first);
                    ll nw = max(0ll, a[j].second - a[i+1].first) + max(0ll, a[j-1].second - a[i].first);
                    if (i > 1) {
                        prev += max(0ll, a[i-1].second - a[i].first);
                        nw += max(0ll, a[i-1].second - a[j].first);
                    }
                    if (j < n-1) {
                        prev += max(0ll, a[j].second - a[j+1].first);
                        nw += max(0ll, a[i].second - a[j+1].first);
                    }
                    if (nw < prev) {
                        swap(a[i], a[j]);
                        done = false;
                        res += nw - prev;
                    }
                }
            }
        } while (!done);

        res = 0;
        for (int i = 1; i < n; i++) {
            res += max(0ll, a[i-1].second - a[i].first);
        }

        finalRes = min(finalRes, res);
    }
    
    return finalRes;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...