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;
typedef long long ll;
typedef long double ld;
const int N = 400100;
vector <int> g[N];
bool used[N];
void dfs(int v){
used[v] = 1;
for (int i = 0; i < g[v].size(); i++){
int to = g[v][i];
if (!used[to])
dfs(to);
}
}
int dp[N], dsu[N], kol;
int dsu_get(int v){
if (v == dsu[v])
return v;
return dsu[v] = dsu_get(dsu[v]);
}
void dsu_unite(int a, int b){
a = dsu_get(a);
b = dsu_get(b);
if (a == b)
return;
dsu[b] = a;
--kol;
}
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
vector <int> zhat;
s.push_back(1e9);
t.push_back(1);
for (int i = 0; i < s.size(); i++){
zhat.push_back(s[i]);
zhat.push_back(t[i]);
}
sort(zhat.begin(), zhat.end());
zhat.erase(unique(zhat.begin(), zhat.end()), zhat.end());
kol = zhat.size();
for (int i = 0; i < zhat.size(); i++)
dsu[i] = i;
memset(dp, 0, sizeof(dp));
for (int i = 0; i < s.size(); i++){
int ss = lower_bound(zhat.begin(), zhat.end(), s[i])-zhat.begin();
int tt = lower_bound(zhat.begin(), zhat.end(), t[i])-zhat.begin();
--dp[ss];
++dp[tt];
dsu_unite(ss, tt);
g[ss].push_back(tt);
}
int cur = 0;
ll ans = 0;
vector <pair<int, int> > edges;
for (int i = 0; i + 1 < zhat.size(); i++){
cur += dp[i];
if (cur > 0){
g[i].push_back(i+1);
dsu_unite(i, i+1);
}
if (cur < 0){
g[i+1].push_back(i);
ans -= cur*1ll*(zhat[i+1]-zhat[i]);
dsu_unite(i+1, i);
}
edges.push_back({zhat[i+1]-zhat[i], i});
}
sort(edges.begin(), edges.end());
for (int i = 0; i < edges.size(); i++){
int len = edges[i].first, id = edges[i].second;
int a = dsu_get(id), b = dsu_get(id+1);
if (a == b)
continue;
dsu_unite(a, b);
ans += len;
if (kol == 1)
break;
}
return ans;
}
Compilation message (stderr)
railroad.cpp: In function 'void dfs(int)':
railroad.cpp:15:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < g[v].size(); i++){
~~^~~~~~~~~~~~~
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:43:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < s.size(); i++){
~~^~~~~~~~~~
railroad.cpp:50:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < zhat.size(); i++)
~~^~~~~~~~~~~~~
railroad.cpp:53:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < s.size(); i++){
~~^~~~~~~~~~
railroad.cpp:64:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i + 1 < zhat.size(); i++){
~~~~~~^~~~~~~~~~~~~
railroad.cpp:78:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < edges.size(); i++){
~~^~~~~~~~~~~~~~
# | 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... |