#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,v;
seg *l,*r;
seg(int nl, int nr) {
b=nl,e=nr,m=(nl+nr)/2,v=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=nv; 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 (b==e) return make_pair(v,b);
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
railroad.cpp: In function 'll plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:39: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 |
1 |
Correct |
12 ms |
8576 KB |
n = 2 |
2 |
Correct |
9 ms |
8576 KB |
n = 2 |
3 |
Correct |
9 ms |
8576 KB |
n = 2 |
4 |
Correct |
9 ms |
8576 KB |
n = 2 |
5 |
Correct |
9 ms |
8576 KB |
n = 2 |
6 |
Correct |
10 ms |
8576 KB |
n = 2 |
7 |
Correct |
10 ms |
8576 KB |
n = 3 |
8 |
Correct |
9 ms |
8576 KB |
n = 3 |
9 |
Correct |
11 ms |
8576 KB |
n = 3 |
10 |
Correct |
10 ms |
8576 KB |
n = 8 |
11 |
Correct |
8 ms |
8448 KB |
n = 8 |
12 |
Correct |
9 ms |
8576 KB |
n = 8 |
13 |
Correct |
9 ms |
8576 KB |
n = 8 |
14 |
Correct |
10 ms |
8576 KB |
n = 8 |
15 |
Correct |
10 ms |
8576 KB |
n = 8 |
16 |
Correct |
8 ms |
8576 KB |
n = 8 |
17 |
Correct |
10 ms |
8448 KB |
n = 8 |
18 |
Correct |
10 ms |
8576 KB |
n = 8 |
19 |
Correct |
8 ms |
8576 KB |
n = 3 |
20 |
Correct |
9 ms |
8576 KB |
n = 7 |
21 |
Correct |
9 ms |
8576 KB |
n = 8 |
22 |
Correct |
8 ms |
8576 KB |
n = 8 |
23 |
Correct |
9 ms |
8576 KB |
n = 8 |
24 |
Correct |
10 ms |
8576 KB |
n = 8 |
25 |
Correct |
9 ms |
8576 KB |
n = 8 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
8576 KB |
n = 2 |
2 |
Correct |
9 ms |
8576 KB |
n = 2 |
3 |
Correct |
9 ms |
8576 KB |
n = 2 |
4 |
Correct |
9 ms |
8576 KB |
n = 2 |
5 |
Correct |
9 ms |
8576 KB |
n = 2 |
6 |
Correct |
10 ms |
8576 KB |
n = 2 |
7 |
Correct |
10 ms |
8576 KB |
n = 3 |
8 |
Correct |
9 ms |
8576 KB |
n = 3 |
9 |
Correct |
11 ms |
8576 KB |
n = 3 |
10 |
Correct |
10 ms |
8576 KB |
n = 8 |
11 |
Correct |
8 ms |
8448 KB |
n = 8 |
12 |
Correct |
9 ms |
8576 KB |
n = 8 |
13 |
Correct |
9 ms |
8576 KB |
n = 8 |
14 |
Correct |
10 ms |
8576 KB |
n = 8 |
15 |
Correct |
10 ms |
8576 KB |
n = 8 |
16 |
Correct |
8 ms |
8576 KB |
n = 8 |
17 |
Correct |
10 ms |
8448 KB |
n = 8 |
18 |
Correct |
10 ms |
8576 KB |
n = 8 |
19 |
Correct |
8 ms |
8576 KB |
n = 3 |
20 |
Correct |
9 ms |
8576 KB |
n = 7 |
21 |
Correct |
9 ms |
8576 KB |
n = 8 |
22 |
Correct |
8 ms |
8576 KB |
n = 8 |
23 |
Correct |
9 ms |
8576 KB |
n = 8 |
24 |
Correct |
10 ms |
8576 KB |
n = 8 |
25 |
Correct |
9 ms |
8576 KB |
n = 8 |
26 |
Correct |
9 ms |
8576 KB |
n = 8 |
27 |
Correct |
11 ms |
8576 KB |
n = 8 |
28 |
Correct |
9 ms |
8576 KB |
n = 8 |
29 |
Correct |
144 ms |
8560 KB |
n = 16 |
30 |
Correct |
116 ms |
8560 KB |
n = 16 |
31 |
Correct |
149 ms |
8568 KB |
n = 16 |
32 |
Correct |
117 ms |
8564 KB |
n = 16 |
33 |
Correct |
107 ms |
8568 KB |
n = 16 |
34 |
Correct |
115 ms |
8564 KB |
n = 16 |
35 |
Correct |
96 ms |
8564 KB |
n = 16 |
36 |
Correct |
36 ms |
8576 KB |
n = 15 |
37 |
Correct |
10 ms |
8576 KB |
n = 8 |
38 |
Correct |
126 ms |
8700 KB |
n = 16 |
39 |
Correct |
119 ms |
8560 KB |
n = 16 |
40 |
Correct |
9 ms |
8576 KB |
n = 9 |
41 |
Correct |
131 ms |
8600 KB |
n = 16 |
42 |
Correct |
124 ms |
8568 KB |
n = 16 |
43 |
Correct |
117 ms |
8564 KB |
n = 16 |
44 |
Correct |
9 ms |
8448 KB |
n = 9 |
45 |
Correct |
36 ms |
8576 KB |
n = 15 |
46 |
Correct |
110 ms |
8560 KB |
n = 16 |
47 |
Correct |
93 ms |
8568 KB |
n = 16 |
48 |
Correct |
105 ms |
8568 KB |
n = 16 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
2089 ms |
23884 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
8576 KB |
n = 2 |
2 |
Correct |
9 ms |
8576 KB |
n = 2 |
3 |
Correct |
9 ms |
8576 KB |
n = 2 |
4 |
Correct |
9 ms |
8576 KB |
n = 2 |
5 |
Correct |
9 ms |
8576 KB |
n = 2 |
6 |
Correct |
10 ms |
8576 KB |
n = 2 |
7 |
Correct |
10 ms |
8576 KB |
n = 3 |
8 |
Correct |
9 ms |
8576 KB |
n = 3 |
9 |
Correct |
11 ms |
8576 KB |
n = 3 |
10 |
Correct |
10 ms |
8576 KB |
n = 8 |
11 |
Correct |
8 ms |
8448 KB |
n = 8 |
12 |
Correct |
9 ms |
8576 KB |
n = 8 |
13 |
Correct |
9 ms |
8576 KB |
n = 8 |
14 |
Correct |
10 ms |
8576 KB |
n = 8 |
15 |
Correct |
10 ms |
8576 KB |
n = 8 |
16 |
Correct |
8 ms |
8576 KB |
n = 8 |
17 |
Correct |
10 ms |
8448 KB |
n = 8 |
18 |
Correct |
10 ms |
8576 KB |
n = 8 |
19 |
Correct |
8 ms |
8576 KB |
n = 3 |
20 |
Correct |
9 ms |
8576 KB |
n = 7 |
21 |
Correct |
9 ms |
8576 KB |
n = 8 |
22 |
Correct |
8 ms |
8576 KB |
n = 8 |
23 |
Correct |
9 ms |
8576 KB |
n = 8 |
24 |
Correct |
10 ms |
8576 KB |
n = 8 |
25 |
Correct |
9 ms |
8576 KB |
n = 8 |
26 |
Correct |
9 ms |
8576 KB |
n = 8 |
27 |
Correct |
11 ms |
8576 KB |
n = 8 |
28 |
Correct |
9 ms |
8576 KB |
n = 8 |
29 |
Correct |
144 ms |
8560 KB |
n = 16 |
30 |
Correct |
116 ms |
8560 KB |
n = 16 |
31 |
Correct |
149 ms |
8568 KB |
n = 16 |
32 |
Correct |
117 ms |
8564 KB |
n = 16 |
33 |
Correct |
107 ms |
8568 KB |
n = 16 |
34 |
Correct |
115 ms |
8564 KB |
n = 16 |
35 |
Correct |
96 ms |
8564 KB |
n = 16 |
36 |
Correct |
36 ms |
8576 KB |
n = 15 |
37 |
Correct |
10 ms |
8576 KB |
n = 8 |
38 |
Correct |
126 ms |
8700 KB |
n = 16 |
39 |
Correct |
119 ms |
8560 KB |
n = 16 |
40 |
Correct |
9 ms |
8576 KB |
n = 9 |
41 |
Correct |
131 ms |
8600 KB |
n = 16 |
42 |
Correct |
124 ms |
8568 KB |
n = 16 |
43 |
Correct |
117 ms |
8564 KB |
n = 16 |
44 |
Correct |
9 ms |
8448 KB |
n = 9 |
45 |
Correct |
36 ms |
8576 KB |
n = 15 |
46 |
Correct |
110 ms |
8560 KB |
n = 16 |
47 |
Correct |
93 ms |
8568 KB |
n = 16 |
48 |
Correct |
105 ms |
8568 KB |
n = 16 |
49 |
Execution timed out |
2089 ms |
23884 KB |
Time limit exceeded |
50 |
Halted |
0 ms |
0 KB |
- |