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 endl '\n'
using namespace std;
const int maxn = 1e6 + 3;
int n;
int a[maxn];
void read()
{
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
}
int dp[maxn];
void solve()
{
dp[n+1] = 0;
for (int i = n; i >= 1; i--)
{
set <int> s;
for (int j = 1; j < i; j++)
s.insert(a[j]);
dp[i] = dp[i+1] + 1;
auto it = s.upper_bound(a[i]);
for (int j = i+1; j <= n; j++)
{
if (a[j] < a[j-1])
break;
if (it != s.end() && a[j] > *it)
break;
dp[i] = min(dp[i], dp[j+1] + 1);
}
}
for (int i = 1; i <= n-1; i++)
assert(dp[i] >= dp[i+1]);
cout << dp[1] << endl;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
read();
solve();
}
/*
6
3 6 4 5 1 2
*/
# | 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... |