#include "railroad.h"
#include <iostream>
#include <assert.h>
#include <queue>
#include <utility>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> pll;
int N;
ll ans=1e18;
ll S[1000006],T[1000006];
ll DP[1000006][20];
priority_queue<pll> PQS,PQT;
bool V[1000006];
ll F(int b, int i){
assert(b&(1<<i));
if(DP[b][i]!=-1)
return DP[b][i];
b=b^(1<<i);
if(!b)
return 0;
ll res=1e18;
for(int j=0;j<N;j++)
if(b&(1<<j) and j!=i)
res=min(res,F(b,j)+max(0LL,T[j]-S[i]));
DP[b|(1<<i)][i]=res;
return res;
}
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
N=s.size();
for(int i=0;i<N;i++)
S[i]=s[i];
for(int i=0;i<N;i++)
T[i]=t[i];
if(N>16){
for(int i=0;i<N;i++){
PQS.push({S[i],i});
PQT.push({T[i],i});
}
S[N]=1e18;
T[N]=-1;
PQS.push({S[N],N});
PQT.push({T[N],N});
int cont=N+1,x,y;
while(!PQS.empty() and !PQT.empty()){
while(V[PQT.top().second] and !PQT.empty())
PQT.pop();
while(V[PQS.top().second] and !PQS.empty() and PQS.top().second!=PQT.top().second)
PQS.pop();
if(PQS.empty() or PQT.empty())
return 0;
x=PQT.top().second;
y=PQS.top().second;
if(T[x]<=S[y]){
V[x]=true;
V[y]=true;
S[cont]=S[x];
T[cont]=T[y];
PQS.push({S[cont],cont});
PQT.push({T[cont],cont});
cont++;
}
else
return 1;
}
return 0;
}
int b=(1<<N)-1;
for(int i=0;i<N;i++)
for(int j=0;j<=b;j++)
DP[j][i]=-1;
for(int i=0;i<N;i++)
ans=min(ans,F(b,i));
return ans;
}
/*
4
1 3
3 7
10 10
20 2
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n = 2 |
2 |
Correct |
2 ms |
436 KB |
n = 2 |
3 |
Correct |
2 ms |
436 KB |
n = 2 |
4 |
Correct |
2 ms |
620 KB |
n = 2 |
5 |
Correct |
2 ms |
640 KB |
n = 2 |
6 |
Correct |
2 ms |
640 KB |
n = 2 |
7 |
Correct |
2 ms |
696 KB |
n = 3 |
8 |
Correct |
2 ms |
696 KB |
n = 3 |
9 |
Correct |
2 ms |
696 KB |
n = 3 |
10 |
Correct |
2 ms |
696 KB |
n = 8 |
11 |
Correct |
2 ms |
696 KB |
n = 8 |
12 |
Correct |
2 ms |
696 KB |
n = 8 |
13 |
Correct |
2 ms |
696 KB |
n = 8 |
14 |
Correct |
3 ms |
696 KB |
n = 8 |
15 |
Correct |
3 ms |
696 KB |
n = 8 |
16 |
Correct |
9 ms |
696 KB |
n = 8 |
17 |
Correct |
2 ms |
696 KB |
n = 8 |
18 |
Correct |
2 ms |
696 KB |
n = 8 |
19 |
Correct |
2 ms |
696 KB |
n = 3 |
20 |
Correct |
3 ms |
696 KB |
n = 7 |
21 |
Correct |
3 ms |
696 KB |
n = 8 |
22 |
Correct |
2 ms |
696 KB |
n = 8 |
23 |
Correct |
2 ms |
696 KB |
n = 8 |
24 |
Correct |
2 ms |
696 KB |
n = 8 |
25 |
Correct |
3 ms |
696 KB |
n = 8 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n = 2 |
2 |
Correct |
2 ms |
436 KB |
n = 2 |
3 |
Correct |
2 ms |
436 KB |
n = 2 |
4 |
Correct |
2 ms |
620 KB |
n = 2 |
5 |
Correct |
2 ms |
640 KB |
n = 2 |
6 |
Correct |
2 ms |
640 KB |
n = 2 |
7 |
Correct |
2 ms |
696 KB |
n = 3 |
8 |
Correct |
2 ms |
696 KB |
n = 3 |
9 |
Correct |
2 ms |
696 KB |
n = 3 |
10 |
Correct |
2 ms |
696 KB |
n = 8 |
11 |
Correct |
2 ms |
696 KB |
n = 8 |
12 |
Correct |
2 ms |
696 KB |
n = 8 |
13 |
Correct |
2 ms |
696 KB |
n = 8 |
14 |
Correct |
3 ms |
696 KB |
n = 8 |
15 |
Correct |
3 ms |
696 KB |
n = 8 |
16 |
Correct |
9 ms |
696 KB |
n = 8 |
17 |
Correct |
2 ms |
696 KB |
n = 8 |
18 |
Correct |
2 ms |
696 KB |
n = 8 |
19 |
Correct |
2 ms |
696 KB |
n = 3 |
20 |
Correct |
3 ms |
696 KB |
n = 7 |
21 |
Correct |
3 ms |
696 KB |
n = 8 |
22 |
Correct |
2 ms |
696 KB |
n = 8 |
23 |
Correct |
2 ms |
696 KB |
n = 8 |
24 |
Correct |
2 ms |
696 KB |
n = 8 |
25 |
Correct |
3 ms |
696 KB |
n = 8 |
26 |
Correct |
2 ms |
696 KB |
n = 8 |
27 |
Correct |
3 ms |
696 KB |
n = 8 |
28 |
Correct |
3 ms |
696 KB |
n = 8 |
29 |
Correct |
127 ms |
10912 KB |
n = 16 |
30 |
Correct |
131 ms |
10912 KB |
n = 16 |
31 |
Correct |
99 ms |
10912 KB |
n = 16 |
32 |
Correct |
129 ms |
10912 KB |
n = 16 |
33 |
Correct |
125 ms |
10912 KB |
n = 16 |
34 |
Correct |
131 ms |
10916 KB |
n = 16 |
35 |
Correct |
131 ms |
10916 KB |
n = 16 |
36 |
Correct |
44 ms |
10916 KB |
n = 15 |
37 |
Correct |
3 ms |
10916 KB |
n = 8 |
38 |
Correct |
115 ms |
10936 KB |
n = 16 |
39 |
Correct |
100 ms |
10936 KB |
n = 16 |
40 |
Correct |
3 ms |
10936 KB |
n = 9 |
41 |
Correct |
116 ms |
11004 KB |
n = 16 |
42 |
Correct |
114 ms |
11004 KB |
n = 16 |
43 |
Correct |
101 ms |
11004 KB |
n = 16 |
44 |
Correct |
3 ms |
11004 KB |
n = 9 |
45 |
Correct |
47 ms |
11004 KB |
n = 15 |
46 |
Correct |
102 ms |
11012 KB |
n = 16 |
47 |
Correct |
101 ms |
11012 KB |
n = 16 |
48 |
Correct |
100 ms |
11012 KB |
n = 16 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
505 ms |
51800 KB |
answer is not correct: 1 instead of 0 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n = 2 |
2 |
Correct |
2 ms |
436 KB |
n = 2 |
3 |
Correct |
2 ms |
436 KB |
n = 2 |
4 |
Correct |
2 ms |
620 KB |
n = 2 |
5 |
Correct |
2 ms |
640 KB |
n = 2 |
6 |
Correct |
2 ms |
640 KB |
n = 2 |
7 |
Correct |
2 ms |
696 KB |
n = 3 |
8 |
Correct |
2 ms |
696 KB |
n = 3 |
9 |
Correct |
2 ms |
696 KB |
n = 3 |
10 |
Correct |
2 ms |
696 KB |
n = 8 |
11 |
Correct |
2 ms |
696 KB |
n = 8 |
12 |
Correct |
2 ms |
696 KB |
n = 8 |
13 |
Correct |
2 ms |
696 KB |
n = 8 |
14 |
Correct |
3 ms |
696 KB |
n = 8 |
15 |
Correct |
3 ms |
696 KB |
n = 8 |
16 |
Correct |
9 ms |
696 KB |
n = 8 |
17 |
Correct |
2 ms |
696 KB |
n = 8 |
18 |
Correct |
2 ms |
696 KB |
n = 8 |
19 |
Correct |
2 ms |
696 KB |
n = 3 |
20 |
Correct |
3 ms |
696 KB |
n = 7 |
21 |
Correct |
3 ms |
696 KB |
n = 8 |
22 |
Correct |
2 ms |
696 KB |
n = 8 |
23 |
Correct |
2 ms |
696 KB |
n = 8 |
24 |
Correct |
2 ms |
696 KB |
n = 8 |
25 |
Correct |
3 ms |
696 KB |
n = 8 |
26 |
Correct |
2 ms |
696 KB |
n = 8 |
27 |
Correct |
3 ms |
696 KB |
n = 8 |
28 |
Correct |
3 ms |
696 KB |
n = 8 |
29 |
Correct |
127 ms |
10912 KB |
n = 16 |
30 |
Correct |
131 ms |
10912 KB |
n = 16 |
31 |
Correct |
99 ms |
10912 KB |
n = 16 |
32 |
Correct |
129 ms |
10912 KB |
n = 16 |
33 |
Correct |
125 ms |
10912 KB |
n = 16 |
34 |
Correct |
131 ms |
10916 KB |
n = 16 |
35 |
Correct |
131 ms |
10916 KB |
n = 16 |
36 |
Correct |
44 ms |
10916 KB |
n = 15 |
37 |
Correct |
3 ms |
10916 KB |
n = 8 |
38 |
Correct |
115 ms |
10936 KB |
n = 16 |
39 |
Correct |
100 ms |
10936 KB |
n = 16 |
40 |
Correct |
3 ms |
10936 KB |
n = 9 |
41 |
Correct |
116 ms |
11004 KB |
n = 16 |
42 |
Correct |
114 ms |
11004 KB |
n = 16 |
43 |
Correct |
101 ms |
11004 KB |
n = 16 |
44 |
Correct |
3 ms |
11004 KB |
n = 9 |
45 |
Correct |
47 ms |
11004 KB |
n = 15 |
46 |
Correct |
102 ms |
11012 KB |
n = 16 |
47 |
Correct |
101 ms |
11012 KB |
n = 16 |
48 |
Correct |
100 ms |
11012 KB |
n = 16 |
49 |
Incorrect |
505 ms |
51800 KB |
answer is not correct: 1 instead of 0 |
50 |
Halted |
0 ms |
0 KB |
- |