Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- //#define int long long
- const long long INF = 1e9 + 7;
- const int MAXN = 1e2 + 10;
- const int N = 1e5 + 10;
- int dp[MAXN][MAXN], can[MAXN][MAXN];
- void solve() {
- int n;
- cin >> n;
- vector<int> a(n + 1);
- for (int i = 1; i <= n; ++i) {
- cin >> a[i];
- }
- for (int i = 0; i <= n; ++i) {
- for (int j = 0; j <= n; ++j) {
- dp[i][j] = INF;
- if (i == j) {
- dp[i][j] = 0;
- can[i][j] = 1;
- }
- }
- }
- for (int len = 2; len <= n + 1; ++len) {
- for (int l = 1; l + len - 1 < n + 1; ++l) {
- int r = l + len - 1;
- for (int m = l; m < r; ++m) {
- if (can[l][m] > 0 && can[m][r] > 0 && (a[m] > a[l] && a[m] > a[r] || a[m] < a[l] && a[m] < a[r])) {
- can[l][r] = m;
- }
- }
- if (a[l] > a[r]) {
- dp[l][r] = INF;
- }
- if (can[l][r]) {
- dp[l][r] = len;
- }
- for (int m = l; m < r; ++m) {
- if (dp[l][m] != INF && dp[m][r] != INF) {
- dp[l][r] = min(dp[l][m], dp[m][r]);
- }
- }
- }
- }
- cout << dp[0][n - 1] << '\n';
- }
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int tt = 1;
- // cin >> tt;
- while (tt--) {
- solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment