This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ve vector
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9 + 10;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, d, t, m, ans;
ve<int> T, P;
ve<pii> A;
ve<ve<int>> DP;
cin >> n >> d >> t;
T = ve<int>(n);
for (int i = 0; i < n; i++) cin >> T[i];
bool b = true;
int e = INF;
int l = -1;
A.push_back({-1, -1});
P.push_back(-1);
for (int i = 0; i < n; i++) {
if (T[i] > t) {
if (!b) {
int r = i - 1 + t - e;
A.push_back({l, r});
P.push_back(i - 1);
e = INF;
b = true;
l = -1;
}
}
else {
if (b) {
l = i;
b = false;
}
e = min(e + 1, T[i]);
}
}
if (!b) {
A.push_back({l, n - 1});
P.push_back(n - 1);
}
m = (int)A.size();
DP = ve<ve<int>>(d + 1, ve<int>(m, INF));
DP[0][0] = 0;
for (int i = 1; i <= d; i++) {
for (int j = 1; j < m; j++) {
ve<pii> S;
pii a = A[j];
a.second = P[j];
int s = a.second - a.first + 1;
S.push_back(a);
for (int k = j - 1; k >= 0; k--) {
DP[i][j] = min(DP[i][j], DP[i - 1][k] + s);
a = A[k];
a.second = min(a.second, P[j]);
while (S.size() > 0 && S.back().second <= a.second) {
s -= S.back().second - S.back().first + 1;
S.pop_back();
}
if (S.size() > 0) {
if (S.back().first <= a.second) {
a.second = S.back().second;
S.pop_back();
}
}
s += a.second - a.first + 1;
S.push_back(a);
}
}
}
int s = 0;
ve<pii> S;
ans = INF;
for (int j = m - 1; j >= 0; j--) {
for (int i = 0; i <= d; i++) {
ans = min(ans, DP[i][j] + s);
}
pii a = A[j];
a.second = min(a.second, n - 1);
while (S.size() > 0 && S.back().second <= a.second) {
s -= S.back().second - S.back().first + 1;
S.pop_back();
}
if (S.size() > 0) {
if (S.back().first <= a.second) {
a.second = S.back().second;
S.pop_back();
}
}
s += a.second - a.first + 1;
S.push_back(a);
}
cout << ans << endl;
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |