# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
563232 | ngpin04 | Road Closures (APIO21_roads) | C++14 | 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>
#define fi first
#define se second
#define mp make_pair
#define TASK ""
#define bit(x) (1LL << (x))
#define getbit(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) {
return l + rd() % (r - l + 1);
}
const int N = 1e5 + 5;
const int oo = 1e9;
const long long ooo = 1e18;
const int mod = 1e9 + 7; // 998244353;
const long double pi = acos(-1);
#include "roads.h"
long long dp[N][2];
int fr[N];
int to[N];
int w[N];
int n;
vector <long long> sub1() {
sort(w, w + (n - 1), greater <int>());
long long tot = accumulate(w, w + (n - 1), 0LL);
vector <long long> res;
res.push_back(tot);
for (int i = 0; i < n - 1; i++)
tot -= w[i], res.push_back(tot);
return res;
}
vector <long long> sub2() {
vector <long long> res;
res.push_back(accumulate(w, w + (n - 1), 0LL));
for (int i = 0; i < n; i++)
for (int j = 0; j < 2; j++)
dp[i][j] = ooo;
dp[0][0] = 0;
for (int i = 1; i < n; i++) {
dp[i][0] = min(dp[i - 1][0], dp[i - 1][1]) + w[i - 1];
dp[i][1] = dp[i - 1][0];
}
res.push_back(min(dp[n - 1][0], dp[n - 1][1]));
for (int i = 2; i < n; i++)
res.push_back(0);
return res;
}
vector<long long> minimum_closure_costs(int N, vector<int> U,
vector<int> V,
vector<int> W) {
bool cond2 = true;
n = N;
for (int i = 0; i < n - 1; i++) {
fr[i] = U[i];
to[i] = V[i];
w[i] = W[i];
cond2 &= fr[i] == i && to[i] == i + 1;
}
if (*max_element(ALL(U)) == 0)
return sub1();
if (cond2)
return sub2();
return vector<long long>(N, 0);
}
#include "grader.cpp"