Submission #98963

#TimeUsernameProblemLanguageResultExecution timeMemory
98963eriksuenderhaufRoller Coaster Railroad (IOI16_railroad)C++11
100 / 100
270 ms16488 KiB
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
//#include "railroad.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%I64d", &(n))
#define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i])
#define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%I64d\n", (n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
const double pi = acos(-1);
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const int MAXN = 1e6 + 5;
const double eps = 1e-9;
int par[MAXN], deg[MAXN];

int qry(int x) {
    if (par[x] == x) return x;
    return par[x] = qry(par[x]);
}

void join(int x, int y) {
    x = qry(x), y = qry(y);
    par[x] = y;
}

ll plan_roller_coaster(vi s, vi t) {
    int n = s.size();
    vi tmp;
    for (int i = 0; i < 2 * n; i++) {
        par[i] = i;
        if (i < n)
            tmp.pb(s[i]), tmp.pb(t[i]);
    }
    sort(tmp.begin(), tmp.end());
    tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end());
    for (int i = 0; i < n; i++) {
        s[i] = lower_bound(tmp.begin(), tmp.end(), s[i]) - tmp.begin();
        t[i] = lower_bound(tmp.begin(), tmp.end(), t[i]) - tmp.begin();
        deg[s[i]]++;
        deg[t[i]]--;
        join(s[i], t[i]);
    }
    ll ans = 0;
    int cur = 1;
    vector<pair<ll,pii>> edg;
    for (int i = tmp.size() - 1; i > 0; i--) {
        cur += deg[i];
        if (cur < 0) { // need to accelerate, go |cur| times this way
            ans += 1ll * cur * (tmp[i - 1] - tmp[i]);
            deg[i - 1] += cur;
            join(i, i - 1);
            cur = 0;
        } else if (cur > 0)
            join(i, i - 1); // slowed down/accelerated in coaster
        else if (cur == 0)
            edg.pb({tmp[i] - tmp[i - 1], {i, i - 1}});
    }
    sort(edg.begin(), edg.end());
    for (pair<ll,pii> nx: edg) {
        int u, v;
        tie(u, v) = nx.se;
        if (qry(u) == qry(v))
            continue;
        join(u, v);
        ans += nx.fi;
    }
    return ans;
}

/*
add edges from s to t + dummy edge from inf to 1 (-> start at 1 and at 1)
cur = edges to the left - edges to the right (calc with pref. sums over degrees)
cur = 0 (-> comps may be separated, add edge to the next unprocessed edge)
cur > 0 do nothing ("acceleration" is for free, you just have to be slower!)
        -> edges from left to right can be added (go over node i) -> join left with cur
        comp is "complete", you can enter at the beginning, visit all nodes and come back (dummy node)
        solve for left + go for free from left to cur <-> join left and cur
cur < 0 you go more often to the right than you slow down/you are faster than allowed
        add edges from the right to the left (slow down) cur has to be zero -> add |cur|
        edges with length X(i)-X(i-1), cur and left are now connected
connect all components
*/

/*int main()
{
    cerr << plan_roller_coaster({1},{2}) << "\n";
    return 0;
}*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...