Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Includes of headers
- #include <cmath>
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <cassert>
- #include <algorithm>
- #include <deque>
- #include <iomanip>
- #include <iostream>
- #include <iterator>
- #include <map>
- #include <queue>
- #include <set>
- #include <stack>
- #include <string>
- #include <vector>
- #include <unordered_map>
- #include <unordered_set>
- #include <bitset>
- #include <functional>
- #include <sstream>
- #include <numeric>
- #include <random>
- #include <chrono>
- //Macro definations
- #define all(C) (C).begin(), (C).end()
- #define rall(C) (C).rbegin(), (C).rend()
- #define mp make_pair
- #define pb emplace_back
- #define int ll
- #define forn(i, n) for (int i = 0; i < (int)n; ++i)
- using namespace std;
- //C++11-style typedefs
- using ll = long long;
- using ull = unsigned long long;
- using pii = pair <int, int>;
- using pll = pair <ll, ll>;
- using ld = long double;
- using pld = pair <ld, ld>;
- /*
- //Fast allocation
- const int MAX_MEM = 1e8;
- char mem[MAX_MEM];
- int mpos = 0;
- inline void * operator new (size_t n)
- {
- char *res = mem + mpos;
- mpos += n;
- assert(mpos <= MAX_MEM);
- return (void*) res;
- }
- inline void operator delete(void*) {}
- */
- //IO-streams templates
- template <class F, class S> istream &operator>> (istream &in, pair <F, S> &a)
- {
- in >> a.first >> a.second;
- return in;
- }
- template <class F, class S> ostream &operator<< (ostream &out, pair <F, S> &a)
- {
- out << a.first << " " << a.second;
- return out;
- }
- template <class T> istream &operator>> (istream &in, vector <T> &a)
- {
- for (auto &i : a)
- in >> i;
- return in;
- }
- template <class T> ostream &operator<< (ostream &out, vector <T> &a)
- {
- for (auto &i : a)
- out << i << " ";
- return out;
- }
- //Fast IO
- inline void Start()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- //freopen("points.in", "r", stdin);
- //freopen("points.out", "w", stdout);
- }
- const int INF = 1e9;
- signed main()
- {
- Start();
- int n;
- cin >> n;
- vector <int> a(n);
- cin >> a;
- vector <vector <bool>> del(n, vector <bool> (n, 0));
- for (int i = 0; i < n; ++i)
- {
- del[i][i] = true;
- if (i)
- del[i - 1][i] = true;
- }
- for (int len = 1; len < n; ++len)
- {
- for (int l = 0; l + len < n; ++l)
- {
- int r = l + len;
- for (int k = l + 1; k < r; ++k)
- del[l][r] = del[l][r] || (del[l][k - 1] && del[k + 1][r] && (a[l] < a[k] && a[k] > a[r] || a[l] > a[k] && a[k] < a[r]));
- }
- }
- vector <vector <int>> dp(n, vector <int> (n, 0));
- for (int len = 1; len < n; ++len)
- {
- for (int l = 0; l + len < n; ++l)
- {
- int r = l + len;
- dp[l][r] = INF;
- if (a[r] < a[l])
- continue;
- if (l + 1 < n && r - 1 >= 0)
- dp[l][r] = min(dp[l][r], dp[l + 1][r - 1]);
- for (int k = l + 1; k < r; ++k)
- {
- if (a[k - 1] <= a[k + 1] && (a[k - 1] > k && k < a[k + 1] || a[k - 1] < k && k > a[k + 1]))
- dp[l][r] = min(dp[l][r], dp[l][k - 1] + dp[k + 1][r] + 1);
- }
- if (del[l][r])
- dp[l][r] = min(dp[l][r], r - l - 1);
- }
- }
- if (dp[0][n - 1] == INF)
- {
- cout << -1;
- return 0;
- }
- cout << dp[0][n - 1];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement