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 pair<ll,ll> P;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<bool> vb;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,k,n) for(ll i=(ll)(k);i<(ll)(n);i++)
#define all(a) a.begin(),a.end()
#define fi first
#define se second
#define lb(v,k) (lower_bound(all(v),k)-v.begin())
#define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());}
#define pb emplace_back
template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<class T> void out(T a){cout<<a<<endl;}
template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<endl;}
template<class T> void outvv(T v){for(auto x:v)outv(x);}
const ll inf=1001001001001001001;
const ll INF=1001001001;
struct UF{
ll n;
vi par,sz;
UF(ll n_):n(n_),par(n_,-1),sz(n_,1){}
ll root(ll i){
if(par[i]==-1)return i;
return root(par[i]);
}
bool merge(ll a,ll b){
a=root(a);b=root(b);
if(a==b)return false;
if(sz[a]>sz[b])swap(a,b);
par[a]=b;sz[b]+=sz[a];
return true;
}
};
long long plan_roller_coaster1(std::vector<int> s, std::vector<int> t) {
int n = (int) s.size();
vp v(n);rep(i,n)v[i]=P(s[i],t[i]);
vvi dp(1<<n,vi(n,inf));
rep(i,n)dp[1<<i][i]=0;
REP(bit,1,1<<n)rep(i,n)if(dp[bit][i]!=inf){
rep(j,n)if(!(bit>>j&1))chmin(dp[bit|(1<<j)][j],dp[bit][i]+max(0ll,v[i].se-v[j].fi));
}
ll ans=inf;
rep(i,n)chmin(ans,dp.back()[i]);
return ans;
}
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
int n = (int) s.size();
vp v(n);rep(i,n)v[i]=P(s[i],t[i]);
vi srt;srt.pb(0);srt.pb(INF);
rep(i,n){
srt.pb(v[i].fi);srt.pb(v[i].se);
}
dupli(srt);
vi imos(srt.size());
rep(i,n){
v[i].fi=lb(srt,v[i].fi);v[i].se=lb(srt,v[i].se);
}
v.pb(srt.size()-1,0);
for(auto x:v){
imos[x.fi]++;imos[x.se]--;
}
rep(i,imos.size()-1)imos[i+1]+=imos[i];
ll ans=0;
rep(i,imos.size()-1)ans+=max(0ll,imos[i])*(srt[i+1]-srt[i]);
UF uf(srt.size());
rep(i,imos.size()-1)if(imos[i]!=0)uf.merge(i,i+1);
for(auto x:v)uf.merge(x.fi,x.se);
vp edge;
rep(i,srt.size()-1)edge.pb(srt[i+1]-srt[i],i);
sort(all(edge));
for(auto x:edge)if(uf.merge(x.se,x.se+1))ans+=x.fi;
return ans;
}
# | 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... |