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>
#include <functional>
#include <vector>
using namespace std;
#define F first
#define S second
typedef long long ll;
const string noyes[2] = {"NO", "YES"};
const ll INFLL = (1ll << 62);
const int INF = (1 << 30);
const int MAXN = 2'000'000;
const ll MOD = 1'000'000'007;
ll n, D, T;
ll a[MAXN];
int dp[MAXN][2];
void subtask_1() {
{
int c = 0;
ll last = INF;
for (int i = 0; i < n; i++) {
last = min(last + 1, a[i]);
c += (last <= T);
dp[i][0] = c;
}
}
for (int i = 0; i < D; i++) {
int cur = i & 1;
for (int t = i; t < n; t++)
dp[t][cur ^ 1] = INF;
for (int t = i; t < n; t++) {
int c = 0;
ll last = INF;
for (int e = t + 1; e < n; e++) {
last = min(last + 1, a[e]);
c += (last <= T);
dp[e][cur ^ 1] = min(dp[e][cur ^ 1], dp[t][cur] + c);
}
}
}
cout << dp[n - 1][D & 1] << "\n";
}
void subtask_2() {
ll pref[n];
{
ll c = 0;
ll last = INF;
for (int i = 0; i < n; i++) {
last = min(last + 1, a[i]);
if (last <= T) {
c++;
}
pref[i] = c;
}
}
ll suf[n];
{
stack<ll> slow;
ll c = 0;
ll last = INF;
for (int i = n - 1; i >= 0; i--) {
last = a[i];
slow.push(i);
while (!slow.empty() && abs(i - slow.top()) + last <= T) {
c++;
slow.pop();
}
suf[i] = c;
}
}
ll ans = n;
for (int i = 0; i + 1 < n; i++)
ans = min(ans, pref[i] + suf[i + 1]);
cout << ans << "\n";
return;
}
void subtask_3_4() {
pair<int, int> f[n];
{
stack<int> s;
for (int i = n - 1; i >= 0; i--) {
f[i] = {-1, n};
int c = 0;
s.push(a[i]);
while (!s.empty() && abs(i - s.top()) + a[i] <= T) {
c++;
f[s.top()] = {i, s.top() - i - c};
s.pop();
}
}
}
{
int c = 0;
ll last = INF;
for (int i = 0; i < n; i++) {
last = min(last + 1, a[i]);
c += (last <= T);
dp[i][0] = c;
}
}
for (int i = 0; i < D; i++) {
int cur = i & 1;
int p = i;
dp[i + 1][cur ^ 1] = dp[i][cur] + (a[i + 1] <= T);
for (int t = i + 2; t < n; t++) {
dp[t][cur ^ 1] = dp[t - 1][cur ^ 1] + (f[t].F > p);
if (f[t].F > p) {
if (dp[t][cur ^ 1] > (dp[f[t].F][cur] + f[t].S)) {
p = f[t].F;
dp[t][cur ^ 1] = (dp[f[t].F][cur] + f[t].S);
}
}
}
}
cout << dp[n - 1][D & 1] << "\n";
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> D >> T;
for (int i = 0; i < n; i++)
cin >> a[i];
if (D == 1)
subtask_2();
else
subtask_3_4();
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... |