이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "railroad.h"
#include<bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define ppb pop_back
#define umax(x,y) x=max(x,y)
#define umin(x,y) x=min(x,y)
#define mp(x,y) make_pair(x,y)
#define ll long long
#define ii pair<int,int>
#define iii pair<int,ii>
#define sz(x) ((int) x.size())
#define orta ((bas+son)>>1)
#define all(x) x.begin(),x.end()
#define dbgs(x) cerr<<(#x)<<" --> "<<(x)<<" "
#define dbg(x) cerr<<(#x)<<" --> "<<(x)<<endl;getchar()
#define pw(x) (1<<(x))
#define inf 200000000000000000
#define inff 2000000000
#define MOD 1000000007
#define N 200005
#define MAX 10000006
#define LOG 30
#define KOK 200
using namespace std;
ll full;
bool vis[16][pw(16)];
int n;
int pos[N];
ll dp[16][pw(16)];
vector<ii> v[16];
vector<int> rails,s,t;
ii S[N*4];
ii merge(ii x,ii y) {
if(y.st<x.st) return y;
return x;
}
void up(int node,int bas,int son,int x) {
if(bas>x || son<x) return ;
if(bas==x && son==x) {
S[node]={inff,-1};
return ;
}
up(node<<1,bas,orta,x);
up(node<<1|1,orta+1,son,x);
S[node]=merge(S[node*2],S[node*2+1]);
}
ii get(int node,int bas,int son,int now) {
if(bas>son || s[rails[son]]<now) return mp(inff,-1);
if(s[rails[bas]]>=now) return S[node];
return merge(get(node<<1,bas,orta,now),get(node<<1|1,orta+1,son,now));
}
void build(int node,int bas,int son) {
if(bas==son) {
S[node]={t[rails[bas]],bas};
return ;
}
build(node<<1,bas,orta);
build(node<<1|1,orta+1,son);
S[node]=merge(S[node<<1],S[node<<1|1]);
}
ll S3(vector<int>&ss, vector<int>& tt) {
n=sz(ss);
for(int i=0;i<n;i++) s.pb(ss[i]),t.pb(tt[i]);
for(int i=0;i<n;i++) rails.pb(i);
sort(all(rails),[](int x,int y) {
if(s[x]<s[y]) return true;
if(s[x]>s[y]) return false;
if(t[x]<t[y]) return true;
return false;
});
build(1,0,n-1);
int cur_pos=1;
int total=0;
while(total<n) {
ii best=get(1,0,n-1,cur_pos);
if(best.nd==-1) return 1;
//cerr<<s[rails[best.nd]]<<' '<<t[rails[best.nd]]<<endl;
up(1,0,n-1,best.nd);
cur_pos=best.st;
total++;
}
return 0;
}
ll solve(int now,int mask) {
if(vis[now][mask]) return dp[now][mask];
if(mask==full) return 0;
vis[now][mask]=1;
ll& r=dp[now][mask];
r=inf;
for(int i=0;i<sz(v[now]);i++) {
if(mask&pw(v[now][i].nd)) continue ;
umin(r,solve(v[now][i].nd,mask|pw(v[now][i].nd))+v[now][i].st);
}
return r;
}
ll S12(vector<int>&s, vector<int>& t) {
for(int i=0;i<sz(s);i++) {
for(int j=0;j<sz(s);j++) {
if(i==j) continue ;
v[i].pb({max(0,t[i]-s[j]),j});
}
}
full=pw(sz(s))-1;
ll ans=inf;
for(int i=0;i<sz(s);i++) {
umin(ans,solve(i,pw(i)));
}
return ans;
}
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
if(sz(s)<=16) return S12(s,t);
else return S3(s,t);
}
# | 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... |