#include "railroad.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define INF (1LL<<60)
#define INF2 1145141919
#define all(x) x.begin(), x.end()
#define uniq(x) x.erase(unique(all(x)), x.end())
#define index(x, y) (int)(lower_bound(all(x), y) - x.begin())
#define pb push_back
#define _1 first
#define _2 second
#define MAX_N (1<<18)
struct SegTree {
P seg[MAX_N*2-1];
SegTree() {
rep(i, MAX_N*2-1) seg[i] = P(INF2, -1);
}
void update(int k, P v) {
k += MAX_N-1;
while (k > 0) {
k = (k-1)/2;
seg[k] = min(seg[k*2+1], seg[k*2+2]);
}
}
P query(int a, int b, int k=0, int l=0, int r=MAX_N) {
if (b <= l || r <= a) return P(INF2, -1);
if (a <= l && r <= b) return seg[k];
return min(query(a, b, k*2+1, l, (l+r)/2), query(a, b, k*2+2, (l+r)/2, r));
}
} rmq;
int N;
int S[200000], T[200000];
long long dp[1<<16][16];
vector<int> G[400000];
bool f() {
vector<int> xs;
rep(i, N) xs.pb(S[i]), xs.pb(T[i]);
sort(all(xs)); uniq(xs);
rep(i, N) G[index(xs, S[i])].pb(T[i]);
rep(i, xs.size()) sort(all(G[i]), greater<int>());
rep(i, xs.size()) if (!G[i].empty()) rmq.update(i, P(G[i].back(), i));
int s = 0;
rep(_, N) {
int p = rmq.query(s, MAX_N)._2;
if (p == -1) return false;
s = index(xs, G[p].back());
G[p].pop_back();
rmq.update(p, P(INF2, -1));
if (!G[p].empty()) rmq.update(p, P(G[p].back(), p));
}
return true;
}
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
N = s.size();
rep(i, N) S[i] = s[i], T[i] = t[i];
//if( false){
if (N <= 16) {
rep(i, 1<<N) rep(j, N) dp[i][j] = INF;
rep(i, N) dp[1<<i][i] = 0;
rep(b, 1<<N) {
rep(last, N) {
if (dp[b][last] == INF) continue;
rep(t, N) if (((b>>t)&1) == 0) {
dp[b|(1<<t)][t] = min(dp[b|(1<<t)][t], dp[b][last] + max(T[last]-S[t], 0));
}
}
}
long long m = INF;
rep(i, N) m = min(m, dp[(1<<N)-1][i]);
return m;
}
else {
return !f();
}
}
Compilation message
railroad.cpp: In function 'bool f()':
railroad.cpp:8:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define rep(i, n) for (int i=0; i<(n); i++)
^
railroad.cpp:47:3: note: in expansion of macro 'rep'
rep(i, xs.size()) sort(all(G[i]), greater<int>());
^
railroad.cpp:8:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
#define rep(i, n) for (int i=0; i<(n); i++)
^
railroad.cpp:48:3: note: in expansion of macro 'rep'
rep(i, xs.size()) if (!G[i].empty()) rmq.update(i, P(G[i].back(), i));
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
13816 KB |
n = 2 |
2 |
Correct |
12 ms |
13920 KB |
n = 2 |
3 |
Correct |
12 ms |
13920 KB |
n = 2 |
4 |
Correct |
12 ms |
13920 KB |
n = 2 |
5 |
Correct |
12 ms |
14064 KB |
n = 2 |
6 |
Correct |
12 ms |
14064 KB |
n = 2 |
7 |
Correct |
12 ms |
14064 KB |
n = 3 |
8 |
Correct |
15 ms |
14064 KB |
n = 3 |
9 |
Correct |
15 ms |
14064 KB |
n = 3 |
10 |
Correct |
12 ms |
14124 KB |
n = 8 |
11 |
Correct |
12 ms |
14124 KB |
n = 8 |
12 |
Correct |
12 ms |
14124 KB |
n = 8 |
13 |
Correct |
15 ms |
14124 KB |
n = 8 |
14 |
Correct |
12 ms |
14124 KB |
n = 8 |
15 |
Correct |
12 ms |
14124 KB |
n = 8 |
16 |
Correct |
15 ms |
14124 KB |
n = 8 |
17 |
Correct |
14 ms |
14124 KB |
n = 8 |
18 |
Correct |
12 ms |
14124 KB |
n = 8 |
19 |
Correct |
12 ms |
14124 KB |
n = 3 |
20 |
Correct |
13 ms |
14124 KB |
n = 7 |
21 |
Correct |
12 ms |
14124 KB |
n = 8 |
22 |
Correct |
12 ms |
14124 KB |
n = 8 |
23 |
Correct |
12 ms |
14124 KB |
n = 8 |
24 |
Correct |
12 ms |
14124 KB |
n = 8 |
25 |
Correct |
12 ms |
14124 KB |
n = 8 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
13816 KB |
n = 2 |
2 |
Correct |
12 ms |
13920 KB |
n = 2 |
3 |
Correct |
12 ms |
13920 KB |
n = 2 |
4 |
Correct |
12 ms |
13920 KB |
n = 2 |
5 |
Correct |
12 ms |
14064 KB |
n = 2 |
6 |
Correct |
12 ms |
14064 KB |
n = 2 |
7 |
Correct |
12 ms |
14064 KB |
n = 3 |
8 |
Correct |
15 ms |
14064 KB |
n = 3 |
9 |
Correct |
15 ms |
14064 KB |
n = 3 |
10 |
Correct |
12 ms |
14124 KB |
n = 8 |
11 |
Correct |
12 ms |
14124 KB |
n = 8 |
12 |
Correct |
12 ms |
14124 KB |
n = 8 |
13 |
Correct |
15 ms |
14124 KB |
n = 8 |
14 |
Correct |
12 ms |
14124 KB |
n = 8 |
15 |
Correct |
12 ms |
14124 KB |
n = 8 |
16 |
Correct |
15 ms |
14124 KB |
n = 8 |
17 |
Correct |
14 ms |
14124 KB |
n = 8 |
18 |
Correct |
12 ms |
14124 KB |
n = 8 |
19 |
Correct |
12 ms |
14124 KB |
n = 3 |
20 |
Correct |
13 ms |
14124 KB |
n = 7 |
21 |
Correct |
12 ms |
14124 KB |
n = 8 |
22 |
Correct |
12 ms |
14124 KB |
n = 8 |
23 |
Correct |
12 ms |
14124 KB |
n = 8 |
24 |
Correct |
12 ms |
14124 KB |
n = 8 |
25 |
Correct |
12 ms |
14124 KB |
n = 8 |
26 |
Correct |
12 ms |
14124 KB |
n = 8 |
27 |
Correct |
12 ms |
14124 KB |
n = 8 |
28 |
Correct |
12 ms |
14124 KB |
n = 8 |
29 |
Correct |
57 ms |
22508 KB |
n = 16 |
30 |
Correct |
60 ms |
22508 KB |
n = 16 |
31 |
Correct |
60 ms |
22508 KB |
n = 16 |
32 |
Correct |
57 ms |
22508 KB |
n = 16 |
33 |
Correct |
59 ms |
22508 KB |
n = 16 |
34 |
Correct |
56 ms |
22508 KB |
n = 16 |
35 |
Correct |
57 ms |
22508 KB |
n = 16 |
36 |
Correct |
44 ms |
22508 KB |
n = 15 |
37 |
Correct |
12 ms |
22508 KB |
n = 8 |
38 |
Correct |
58 ms |
22508 KB |
n = 16 |
39 |
Correct |
57 ms |
22508 KB |
n = 16 |
40 |
Correct |
12 ms |
22508 KB |
n = 9 |
41 |
Correct |
58 ms |
22508 KB |
n = 16 |
42 |
Correct |
57 ms |
22508 KB |
n = 16 |
43 |
Correct |
75 ms |
22508 KB |
n = 16 |
44 |
Correct |
12 ms |
22508 KB |
n = 9 |
45 |
Correct |
46 ms |
22508 KB |
n = 15 |
46 |
Correct |
84 ms |
22508 KB |
n = 16 |
47 |
Correct |
57 ms |
22508 KB |
n = 16 |
48 |
Correct |
57 ms |
22508 KB |
n = 16 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
225 ms |
52828 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
13816 KB |
n = 2 |
2 |
Correct |
12 ms |
13920 KB |
n = 2 |
3 |
Correct |
12 ms |
13920 KB |
n = 2 |
4 |
Correct |
12 ms |
13920 KB |
n = 2 |
5 |
Correct |
12 ms |
14064 KB |
n = 2 |
6 |
Correct |
12 ms |
14064 KB |
n = 2 |
7 |
Correct |
12 ms |
14064 KB |
n = 3 |
8 |
Correct |
15 ms |
14064 KB |
n = 3 |
9 |
Correct |
15 ms |
14064 KB |
n = 3 |
10 |
Correct |
12 ms |
14124 KB |
n = 8 |
11 |
Correct |
12 ms |
14124 KB |
n = 8 |
12 |
Correct |
12 ms |
14124 KB |
n = 8 |
13 |
Correct |
15 ms |
14124 KB |
n = 8 |
14 |
Correct |
12 ms |
14124 KB |
n = 8 |
15 |
Correct |
12 ms |
14124 KB |
n = 8 |
16 |
Correct |
15 ms |
14124 KB |
n = 8 |
17 |
Correct |
14 ms |
14124 KB |
n = 8 |
18 |
Correct |
12 ms |
14124 KB |
n = 8 |
19 |
Correct |
12 ms |
14124 KB |
n = 3 |
20 |
Correct |
13 ms |
14124 KB |
n = 7 |
21 |
Correct |
12 ms |
14124 KB |
n = 8 |
22 |
Correct |
12 ms |
14124 KB |
n = 8 |
23 |
Correct |
12 ms |
14124 KB |
n = 8 |
24 |
Correct |
12 ms |
14124 KB |
n = 8 |
25 |
Correct |
12 ms |
14124 KB |
n = 8 |
26 |
Correct |
12 ms |
14124 KB |
n = 8 |
27 |
Correct |
12 ms |
14124 KB |
n = 8 |
28 |
Correct |
12 ms |
14124 KB |
n = 8 |
29 |
Correct |
57 ms |
22508 KB |
n = 16 |
30 |
Correct |
60 ms |
22508 KB |
n = 16 |
31 |
Correct |
60 ms |
22508 KB |
n = 16 |
32 |
Correct |
57 ms |
22508 KB |
n = 16 |
33 |
Correct |
59 ms |
22508 KB |
n = 16 |
34 |
Correct |
56 ms |
22508 KB |
n = 16 |
35 |
Correct |
57 ms |
22508 KB |
n = 16 |
36 |
Correct |
44 ms |
22508 KB |
n = 15 |
37 |
Correct |
12 ms |
22508 KB |
n = 8 |
38 |
Correct |
58 ms |
22508 KB |
n = 16 |
39 |
Correct |
57 ms |
22508 KB |
n = 16 |
40 |
Correct |
12 ms |
22508 KB |
n = 9 |
41 |
Correct |
58 ms |
22508 KB |
n = 16 |
42 |
Correct |
57 ms |
22508 KB |
n = 16 |
43 |
Correct |
75 ms |
22508 KB |
n = 16 |
44 |
Correct |
12 ms |
22508 KB |
n = 9 |
45 |
Correct |
46 ms |
22508 KB |
n = 15 |
46 |
Correct |
84 ms |
22508 KB |
n = 16 |
47 |
Correct |
57 ms |
22508 KB |
n = 16 |
48 |
Correct |
57 ms |
22508 KB |
n = 16 |
49 |
Runtime error |
225 ms |
52828 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
50 |
Halted |
0 ms |
0 KB |
- |