# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
539681 | Eug1ena | Roller Coaster Railroad (IOI16_railroad) | C++17 | 0 ms | 0 KiB |
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>
using namespace std;
using lint = long long;
#define rep(i, n) for(int i = 0; i < int(n); i++)
#define rep2(i, l, n) for(int i = int(l); i < int(n); i++)
#define repr(i, n) for(int i = int(n) - 1; i >= 0; i--)
#define all(x) (x).begin(), (x).end()
inline int has(lint x, int k){ return (x >> k) & 1; };
int n;
lint a[30], b[30];
lint dp[20][1 << 16];
lint play(int at, int bit){
if(bit == (1 << n) - 1){
return 0;
}
if(dp[at][bit] != -1) return dp[at][bit];
lint ans = 1e18;
rep(to, n){
if(has(bit, to)) continue;
lint res = max(0ll, b[at] - a[to]);
res += play(to, bit | (1 << to));
ans = min(ans, res);
}
return dp[at][bit] = ans;
}
lint plan_roller_coaster(vector<int> s_in, vector<int> t_in){
n = int(s_in.size());
assert(n <= 16);
rep(i, n){
a[i] = s_in[i];
b[i] = t_in[i];
}
b[n] = 1;
memset(dp, -1, sizeof(dp));
return play(n, 0);
}
int main(){
vector<int> s{1, 4, 5, 6};
vector<int> t{7, 3, 8, 6};
cout << plan_roller_coaster(s, t) << endl;
}