#include <bits/stdc++.h>
#pragma optimize("g", on)
// #pragma GCC optimize ("inline")
// #pragma GCC optimize ("Ofast")
// #pragma GCC optimize ("unroll-loops")
#pragma GCC optimize ("03")
#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,avx2,mmx,fma,avx,tune=native")
void Freopen () {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
using namespace std;
#define fi first
#define se second
#define int long long
const int N = 250000 + 7, M = 250000;
int n, a[N], dp[N], d[N];
signed main() {
// freopen("duty.in", "r", stdin);
// freopen("duty.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// cout.tie(NULL);
//Freopen();
cin>>n;
for(int i = 1; i <= n; i++) {
cin>>a[i];
d[i] = 1e9;
}
dp[n + 1] = 1, d[n + 1] = 0;
for(int i = n; i >= 1; i--) {
vector<int> vec;
for(int j = i; j <= n; j++) {
int mx = -1, mn = 1e9;
for(int l = j; l <= n; l++) {
if(!dp[l + 1])continue;
for(int k = j + 1; k <= l; k++) {
mx = max(mx, a[k]), mn = min(mn, a[k]);
}
if(mx == -1) {
bool ans = 1;
for(int k = i + 1; k <= j; k++) {
if(a[k] < a[k - 1]) {
ans = 0;
break;
}
}
if(ans)dp[i] = 1, d[i] = min(d[i], d[j + 1] + 1);
}else {
bool ans = 1;
for(int k = i + 1; k <= j; k++) {
if(a[k] < a[k - 1]) {
ans = 0;
break;
}
}
if(ans) {
for(int k = i; k <= j; k++) {
if(a[k] > mn && a[k] < mx) {
ans = 0;
break;
}
}
if(ans)dp[i] = 1, d[i] = min(d[i], d[j + 1] + 1);
}
}
}
}
// cout << i << ' ' << d[i] << '\n';
}
cout << d[1] << '\n';
}
/*
please AC
*/
컴파일 시 표준 에러 (stderr) 메시지
money.cpp: In function 'void Freopen()':
money.cpp:10:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
money.cpp:11:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
11 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |