Submission #966367

# Submission time Handle Problem Language Result Execution time Memory
966367 2024-04-19T18:27:10 Z anango Roller Coaster Railroad (IOI16_railroad) C++17
30 / 100
720 ms 105592 KB
#include "railroad.h"
#include <bits/stdc++.h>
using namespace std;
#define int long long
class DSU {
private:
    int n;
    vector<int> parent;
    vector<int> rank;
public:
    DSU(int siz) {
        n=siz;
        parent=vector<int>(n,-1);
        rank=vector<int>(n,0);
        for (int i=0; i<n; i++) {
            parent[i]=i;
        }
    }
    int find(int node) {
        if (node==parent[node]) {
            return node;
        }
        return parent[node] = find(parent[node]);
    }
    void link(int u, int v) {
        u=find(u);
        v=find(v);
        if (u==v) {
            return;
        }
        if (rank[u]<rank[v]) {
            swap(u,v);
        }
        if (rank[u]==rank[v])  {
            rank[u]++;
        }
        parent[v]=u;
        return;
    }
    vector<int> getpar() {
        for (int i=0; i<n; i++) {
            find(i);
        }
        return parent;
    }
};


int plan_roller_coaster(vector<int32_t> s, vector<int32_t> t) {
    int n = (int) s.size();
    int INF=1000000007;
    set<int> crucial;
    for (int i=0; i<n; i++) {
        crucial.insert(s[i]);
        crucial.insert(t[i]);
    }
    crucial.insert(1);
    crucial.insert(INF);
    int cr = crucial.size();
    map<int,int> rev;
    vector<int> cval;
    for (auto i:crucial) {
        cval.push_back(i); //coordinate compression
    }
    // cout << "COMPRESSION" << endl;
    for (int i=0; i<cr; i++) {
        rev[cval[i]] = i;
        // cout << rev[cval[i]] <<" " << cval[i] << endl;
    }
    for (int i=0; i<n; i++) {
        s[i]=rev[s[i]];
        t[i]=rev[t[i]]; //sorry for polluting the arrays given, check this does not cause a bug in the orange juice
        
    }
    // cout << "OR ARRAYS" << endl;
    for (int i=0; i<n; i++) {
        // cout << s[i] <<" " << t[i] << endl;
    }
    vector<int> deltadiffarray(cr+1,0); //sum of blubberisation coefficients up to cr[i], each is cr[i] to cr[i+1] delta
    for (int i=0; i<n; i++) {
        deltadiffarray[s[i]]++;
        deltadiffarray[t[i]]--;
        // cout << "+"<<s[i] <<" -"<< t[i] << endl;
    }
    deltadiffarray[0]--;
    deltadiffarray[cr-1]++; //make the req val 1
    
    vector<int> deltas(cr,0);
    int su=0;
    int operations = 0;
    for (int i=0; i<cr-1; i++) {
        su+=deltadiffarray[i];
        deltas[i] = su;
        if (deltas[i]>0) {
            operations += cval[i+1] - cval[i];
        }
    }
    // cout << "DELTA DIFFS" << endl;
    for (int i=0; i<cr; i++) {
        // cout << i <<" " << deltadiffarray[i] << endl;
    }
    // cout << "DELTAS" << endl;
    for (int i=0; i<cr; i++) {
        // cout << i <<" " << deltas[i] << endl;
    }
    DSU comp(cr);
    for (int i=0; i<n; i++) {
        comp.link(s[i],t[i]);
        // cout << "linking " << s[i] <<" " << t[i] << endl;
    }
    for (int i=0; i<cr-1; i++) {
        if (deltas[i]!=0) {
            comp.link(i,i+1);
        }
    }
    vector<int> components = comp.getpar();
    // cout << "COMPONENTS" << endl;
    for (int i=0; i<cr; i++) {
        // cout << i <<" " << components[i] << endl;
    }
    set<int> comps;
    for (auto i:components) {
        comps.insert(i);
    }
    vector<int> cvec;
    vector<int> revvec(400000, -1);
    for (auto i:comps) {
        cvec.push_back(i);
    }
    int tau = cvec.size();
    for (int i=0; i<tau; i++) {
        revvec[cvec[i]] = i;
    }
    vector<vector<pair<int,int>>> adjlist(tau);
    vector<vector<int>> edges;
    for (int i=0; i<cr-1; i++) {
        if (components[i] == components[i+1]) {
            continue;
        }
        int dist = cval[i+1] - cval[i];
        adjlist[revvec[components[i]]].push_back({revvec[components[i+1]],dist});
        adjlist[revvec[components[i+1]]].push_back({revvec[components[i]],dist});
        edges.push_back({revvec[components[i]],revvec[components[i+1]],dist});
    }
    //use the method of Kruskal, reusing the DSU, to find the component sum of min span tree
    DSU K(tau);
    sort(edges.begin(), edges.end(), [=](const vector<int> &v1, const vector<int> &v2) {
        return v1[2] < v2[2];
    });
    int cost=0;
    for (vector<int> edge:edges) {
        int u,v,w;
        u=edge[0];
        v=edge[1];
        w=edge[2];
        if (K.find(u)==K.find(v)) {
            continue;
        }
        K.link(u,v);
        cost+=w;
    }

    return operations+cost;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3416 KB n = 2
2 Correct 2 ms 3512 KB n = 2
3 Correct 1 ms 3420 KB n = 2
4 Correct 1 ms 3420 KB n = 2
5 Correct 1 ms 3420 KB n = 2
6 Correct 1 ms 3420 KB n = 2
7 Correct 2 ms 3420 KB n = 3
8 Correct 1 ms 3420 KB n = 3
9 Correct 2 ms 3420 KB n = 3
10 Correct 1 ms 3420 KB n = 8
11 Correct 1 ms 3420 KB n = 8
12 Incorrect 2 ms 3420 KB answer is not correct: 867508423 instead of 2726473632
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3416 KB n = 2
2 Correct 2 ms 3512 KB n = 2
3 Correct 1 ms 3420 KB n = 2
4 Correct 1 ms 3420 KB n = 2
5 Correct 1 ms 3420 KB n = 2
6 Correct 1 ms 3420 KB n = 2
7 Correct 2 ms 3420 KB n = 3
8 Correct 1 ms 3420 KB n = 3
9 Correct 2 ms 3420 KB n = 3
10 Correct 1 ms 3420 KB n = 8
11 Correct 1 ms 3420 KB n = 8
12 Incorrect 2 ms 3420 KB answer is not correct: 867508423 instead of 2726473632
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 687 ms 71528 KB n = 199999
2 Correct 717 ms 73860 KB n = 199991
3 Correct 637 ms 73436 KB n = 199993
4 Correct 394 ms 56352 KB n = 152076
5 Correct 166 ms 36036 KB n = 93249
6 Correct 474 ms 61108 KB n = 199910
7 Correct 483 ms 71272 KB n = 199999
8 Correct 401 ms 61756 KB n = 199997
9 Correct 489 ms 63932 KB n = 171294
10 Correct 390 ms 53356 KB n = 140872
11 Correct 432 ms 62708 KB n = 199886
12 Correct 465 ms 71684 KB n = 199996
13 Correct 425 ms 64968 KB n = 200000
14 Correct 504 ms 78500 KB n = 199998
15 Correct 470 ms 77872 KB n = 200000
16 Correct 512 ms 80384 KB n = 199998
17 Correct 621 ms 73920 KB n = 200000
18 Correct 631 ms 69632 KB n = 190000
19 Correct 473 ms 66472 KB n = 177777
20 Correct 165 ms 38336 KB n = 100000
21 Correct 531 ms 73100 KB n = 200000
22 Correct 720 ms 105592 KB n = 200000
23 Correct 601 ms 73624 KB n = 200000
# Verdict Execution time Memory Grader output
1 Correct 1 ms 3416 KB n = 2
2 Correct 2 ms 3512 KB n = 2
3 Correct 1 ms 3420 KB n = 2
4 Correct 1 ms 3420 KB n = 2
5 Correct 1 ms 3420 KB n = 2
6 Correct 1 ms 3420 KB n = 2
7 Correct 2 ms 3420 KB n = 3
8 Correct 1 ms 3420 KB n = 3
9 Correct 2 ms 3420 KB n = 3
10 Correct 1 ms 3420 KB n = 8
11 Correct 1 ms 3420 KB n = 8
12 Incorrect 2 ms 3420 KB answer is not correct: 867508423 instead of 2726473632
13 Halted 0 ms 0 KB -