이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
/*#pragma GCC optimize("03")
#pragma GCC target("avx,avx2")
#pragma GCC optimeze("unroll-loops") */
#define inf 10000000000000000
#define ll long long
#include "railroad.h"
using namespace std;
int n;
ll dp[16][16][1 << 16],cost[16][16];
ll small_case(vector<int> a,vector<int> b) {
n = a.size();
for (int i = 0; i < n; i++) {
for (int j = 0;j < n; j++) {
if (b[i] > a[j] && j != i) cost[i][j] = b[i] - a[j];
fill(dp[i][j],dp[i][j] + (1 << n),inf);
}
dp[i][i][(1 << i)] = 0;
}
for (int mask = 1; mask < (1 << n); mask++)
for (int i = 0; i < n; i++) {
if ((1 << i) & mask) {
for (int j = 0; j < n; j++) {
if ( ((1 << j) & mask) && j != i) {
for (int e = 0; e < n; e++) {
if (e != j && dp[i][j][mask] > dp[i][e][mask^(1<<j)] + cost[e][j])
dp[i][j][mask] = dp[i][e][mask^(1<<j)] + cost[e][j];
}
}
}
}
}
ll ans = inf;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
ans = min(ans,dp[i][j][(1<<n)-1]);
return ans;
}
ll plan_roller_coaster(vector<int> a, vector<int> b) {
n = a.size();
//if (n <= 16) return small_case(a,b);
vector<pair<int,int> > v;
v.push_back({0,0});
for (int i = 0;i < n; i++) v.push_back({a[i],b[i]});
int st = 1;
v.push_back({1000000000,0});
bool flag = 1;
while (flag) {
flag = 0;
for (int i = st; i < n-1; i++) {
int cost[2] = {0,0};
for (int j = i; j <= i+2; j++) {
if (v[j].first < v[j-1].second)
cost[0] += v[j-1].second - v[j].first;
}
swap(v[i],v[i+1]);
for (int j = i; j <= i+2; j++) {
if (v[j].first < v[j-1].second)
cost[1] += v[j-1].second - v[j].first;
}
if (cost[0] <= cost[1]) {
swap(v[i],v[i+1]);
flag = 1;
st = max(i-1,1);
break;
}
}
}
ll ans = 0;
for (int i = 2; i <= n; i++) {
if (v[i].first < v[i-1].second) ans += v[i-1].second - v[i].first;
}
return ans;
}
# | 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... |