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;
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
template <class X, class Y>
bool minimize(X &x, Y y) {
if (x > y) return x = y, true;
else return false;
}
int n, k;
const int maxn = (int) 1e5 + 7;
int a[maxn + 2];
namespace subtask1 {
bool check() {
return (n <= 20);
}
const int inf = (int) 1e9 + 7;
const int N = (int) 20 + 7;
int dp[N + 2][N + 2]; // dp[i][j]: tong time khi xet i nguoi dau tien va da bat k lan;
void solve() {
FOR(i, 0, n)
FOR(j, 0, k) dp[i][j] = inf;
dp[0][0] = 0;
FOR(turn, 1, k) {
FOR(i, 1, n) {
FOR(j, 1, i) {
// use: i -> j;
minimize(dp[i][turn], dp[j - 1][turn - 1] + a[i] - a[j] + 1);
}
}
}
cout << dp[n][k];
}
}
namespace subtask2 {
bool check() {
return (n <= 5000);
}
const int inf = (int) 1e9 + 7;
const int N = (int) 5e3 + 7;
int dp[N + 2][N + 2]; // dp[i][j]: tong time khi xet i nguoi dau tien va da bat k lan;
int f[N + 2];
void solve() {
FOR(i, 0, n)
FOR(j, 0, k) dp[i][j] = inf;
dp[0][0] = 0;
f[0] = 0;
FOR(i, 1, n) f[i] = -a[1];
FOR(turn, 1, k) {
FOR(i, 1, n) {
minimize(dp[i][turn], f[i] + a[i] + 1);
}
f[0] = inf;
FOR(i, 1, n) f[i] = min(f[i - 1], dp[i - 1][turn] - a[i]);
}
cout << dp[n][k];
}
}
namespace subtask3 {
void solve() {
vector <int> v;
FOR(i, 1, n - 1) v.push_back(a[i + 1] - 1 - a[i]);
sort(v.begin(), v.end(), greater <int> ());
int ans = a[n] - a[1] + 1;
FOR(i, 1, k - 1) ans -= v[i - 1];
cout << ans;
}
}
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> k;
FOR(i, 1, n) cin >> a[i];
if (subtask1 :: check()) return subtask1 :: solve(), 0;
if (subtask2 :: check()) return subtask2 :: solve(), 0;
subtask3 :: solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |