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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int inf = 1e9 + 100;
const ll INF = 1e18 + 100;
const int maxn = 4e5 + 100;
int n;
ll dp[1<<16][16];
int a[maxn];
int b[maxn];
vector<int> s;
int pref[maxn];
int p[maxn];
int get(int v){
if(p[v] == v) return v;
return p[v] = get(p[v]);
}
void join(int a, int b){
p[get(a)] = get(b);
}
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t){
n = s.size();
for(int i = 0; i < n; i++){
a[i] = s[i];
b[i] = t[i];
}
for(int i = 0; i < n; i++){
s.push_back(a[i]);
s.push_back(b[i]);
}
sort(s.begin(), s.end());
s.resize(unique(s.begin(), s.end()) - s.begin());
for(int i = 1; i <= s.size(); i++){
p[i] = i;
}
for(int i = 0; i < n; i++){
a[i] = lower_bound(s.begin(), s.end(), a[i]) - s.begin() + 1;
b[i] = lower_bound(s.begin(), s.end(), b[i]) - s.begin() + 1;
join(a[i], b[i]);
}
a[n] = s.size(); b[n] = 1;
join(s.size(), 1);
for(int i = 0; i <= n; i++){
pref[a[i]]++;
pref[b[i]]--;
}
ll ans = 0;
vector<tuple<int, int, int>> v;
for(int i = 1; i < s.size(); i++){
pref[i] += pref[i-1];
if(pref[i] < 0){
join(i, i + 1);
} else if(pref[i] > 0){
ans += 1ll * (s[i] - s[i-1]) * pref[i];
join(i, i + 1);
} else{
v.push_back({s[i] - s[i-1], i, i + 1});
}
}
sort(v.begin(), v.end());
for(auto [x, a, b]: v){
if(get(a) != get(b)){
ans += x;
join(a, b);
}
}
return ans;
}
Compilation message (stderr)
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:39:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for(int i = 1; i <= s.size(); i++){
| ~~^~~~~~~~~~~
railroad.cpp:55:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int i = 1; i < s.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... |