# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1037353 | 12345678 | Roller Coaster Railroad (IOI16_railroad) | C++17 | 489 ms | 39744 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "railroad.h"
#include <bits/stdc++.h>
using namespace std;
const int nx=4e5+5;
int n, T, qs[nx], dsu[nx], rv[nx];
long long ans;
map<int, int> mp;
int find(int x)
{
if (dsu[x]==x) return x;
return dsu[x]=find(dsu[x]);
}
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
n=s.size();
for (int i=0; i<n; i++) mp[s[i]]=mp[t[i]]=0;
for (auto &[x, y]:mp) y=++T, rv[y]=x;
for (int i=1; i<=mp.size(); i++) dsu[i]=i;
for (int i=0; i<n; i++) qs[mp[s[i]]]++, qs[mp[t[i]]]--, dsu[find(mp[s[i]])]=find(mp[t[i]]);
for (int i=1; i<mp.size(); i++)
{
qs[i]+=qs[i-1];
if (qs[i]>1) ans+=(long long)(rv[i+1]-rv[i])*(qs[i]-1), dsu[find(i)]=find(i+1);
if (qs[i]<1) dsu[find(i)]=find(i+1);
}
dsu[find(1)]=find(mp.size());
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
for (int i=1; i<mp.size(); i++) pq.push({rv[i+1]-rv[i], i});
while (!pq.empty())
{
auto [w, u]=pq.top();
pq.pop();
if (find(u)==find(u+1)) continue;
ans+=w;
dsu[find(u)]=find(u+1);
}
return ans;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |