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 "railroad.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mx = 4e5 + 5;
ll dp[16][1<<16];
vector<int> X;
vector<int> O[mx];
int deg[mx];
bool vis[mx];
void dfs(int x){
vis[x] = 1;
for(int &u : O[x]){
if(!vis[u]) dfs(u);
}
}
ll plan_roller_coaster(vector<int> s, vector<int> t) {
int n = (int) s.size();
if(n > 16)
{
for(int i = 0; i < n; i++){
X.push_back(s[i]);
X.push_back(t[i]);
}
sort(X.begin(),X.end());
X.erase(unique(X.begin(),X.end()),X.end());
for(int i = 0; i < n; i++){
s[i] = lower_bound(X.begin(),X.end(),s[i]) - X.begin();
t[i] = lower_bound(X.begin(),X.end(),t[i]) - X.begin();
++deg[ s[i] ];
--deg[ t[i] ];
O[ s[i] ].push_back(t[i]);
}
for(int i = 0; i < X.size(); i++){
int cnt = (i == 0) - deg[i];
if(cnt > 0){
deg[i] += cnt;
deg[i+1] -= cnt;
O[i].push_back(i+1);
}else if(cnt < 0) return 42;
}
dfs(0);
for(int i = 0; i < X.size(); i++) if(!vis[i]) return 42;
return 0;
}
for(int i = 0; i < n; i++) fill(dp[i], dp[i] + (1 << n), LLONG_MAX);
for(int i = 0; i < n; i++) dp[i][1<<i] = 0;
for(int b = 1; b < (1 << n); b++){
for(int i = 0; i < n; i++){
if(!(b >> i & 1)) continue;
for(int j = 0; j < n; j++){
if(b >> j & 1) continue;
ll &d = dp[j][b | (1 << j)];
d = min(d, dp[i][b] + max(0, t[i]-s[j]));
}
}
}
ll ans = LLONG_MAX;
for(int i = 0; i < n; i++){
ans = min(ans, dp[i][(1<<n)-1]);
}
return ans;
}
Compilation message (stderr)
railroad.cpp: In function 'll plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:39:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < X.size(); i++){
~~^~~~~~~~~~
railroad.cpp:48:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < X.size(); i++) if(!vis[i]) return 42;
~~^~~~~~~~~~
# | 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... |