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>
//#include "railroad.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
struct seg {
int b,m,e;
ii v;
seg *l,*r;
seg(int nl, int nr) {
b=nl,e=nr,m=(nl+nr)/2,v=make_pair(0,0);
if (b!=e) l=new seg(b,m), r=new seg(m+1,e);
}
void upd(int p, int nv) {
if (b==e) {
v=max(v,make_pair(nv,b)); return;
}
if (p<=m) l->upd(p,nv);
else r->upd(p,nv);
v=max(l->v,r->v);
}
ii rmax(int nl, int nr) {
if (nl>nr) return make_pair(-1,0);
if (nl<=b&&e<=nr) return v;
ii val = make_pair(0,0);
if (nl<=m) val=max(val,l->rmax(nl,nr));
if (m<nr) val=max(val,r->rmax(nl,nr));
return val;
}
};
vector<ii> segs;
int n;
ll m[16][65536];
ll dp(int seg, int bm) {
if (bm==(1<<n)-1) return m[seg][bm]=0;
if (seg!=-1&&m[seg][bm]!=-1) return m[seg][bm];
ll l = 1ll<<60;
for (int i=0;i<n;i++) {
if (((1<<i)&~bm)) l=min(l,(seg!=-1&&segs[seg].second>segs[i].first?segs[seg].second-segs[i].first:0)+dp(i,bm|(1<<i)));
}
//printf("%d %d %d %lld\n",segs[seg].second,seg,bm,l);
return (seg!=-1 ? m[seg][bm]=l : l);
}
ll plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
n = (int) s.size();
for (int i=0;i<n;i++) segs.push_back(make_pair(s[i],t[i]));
if (n<=16) {
fill(&m[0][0],&m[16][0],-1);
return dp(-1,0);
} else {
//sort by enter speed? then get lowest exit...
seg s(0,n-1);
sort(segs.begin(),segs.end());
for (int i=0;i<n;i++) s.upd(i,segs[i].second);
int rem=n, speed=0;
ii buf = s.rmax(0,n-1);
s.upd(buf.second,0);
while (rem--) {
buf = s.rmax(lower_bound(segs.begin(),segs.end(),make_pair(speed,0))-segs.begin(),n-1);
//printf("%d %d ",buf.first,buf.second);
speed=buf.first;
if (speed==-1) return 0;
s.upd(buf.second,-1);
//printf("%d %d\n",lower_bound(segs.begin(),segs.end(),make_pair(speed,0))-segs.begin(),speed);
}
return 1;
}
}
Compilation message (stderr)
railroad.cpp: In function 'll plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:40:35: warning: array subscript is below array bounds [-Warray-bounds]
if (bm==(1<<n)-1) return m[seg][bm]=0;
~~~~~^
# | 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... |