Advertisement
Guest User

oaksssskoa

a guest
Nov 16th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.40 KB | None | 0 0
  1. //Includes of headers
  2. #include <cmath>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <cassert>
  7. #include <algorithm>
  8. #include <deque>
  9. #include <iomanip>
  10. #include <iostream>
  11. #include <iterator>
  12. #include <map>
  13. #include <queue>
  14. #include <set>
  15. #include <stack>
  16. #include <string>
  17. #include <vector>
  18. #include <unordered_map>
  19. #include <unordered_set>
  20. #include <bitset>
  21. #include <functional>
  22. #include <sstream>
  23. #include <numeric>
  24. #include <random>
  25. #include <chrono>
  26.  
  27. //Macro definations
  28. #define all(C) (C).begin(), (C).end()
  29. #define rall(C) (C).rbegin(), (C).rend()
  30. #define mp make_pair
  31. #define pb emplace_back
  32. #define int ll
  33. #define forn(i, n) for (int i = 0; i < (int)n; ++i)
  34.  
  35. using namespace std;
  36.  
  37. //C++11-style typedefs
  38. using ll = long long;
  39. using ull = unsigned long long;
  40. using pii = pair <int, int>;
  41. using pll = pair <ll, ll>;
  42. using ld = long double;
  43. using pld = pair <ld, ld>;
  44. /*
  45.  //Fast allocation
  46.  const int MAX_MEM = 1e8;
  47.  char mem[MAX_MEM];
  48.  int mpos = 0;
  49.  inline void * operator new (size_t n)
  50.  {
  51.  char *res = mem + mpos;
  52.  mpos += n;
  53.  assert(mpos <= MAX_MEM);
  54.  return (void*) res;
  55.  }
  56.  inline void operator delete(void*) {}
  57. */
  58. //IO-streams templates
  59. template <class F, class S> istream &operator>> (istream &in, pair <F, S> &a)
  60. {
  61.     in >> a.first >> a.second;
  62.     return in;
  63. }
  64. template <class F, class S> ostream &operator<< (ostream &out, pair <F, S> &a)
  65. {
  66.     out << a.first << " " << a.second;
  67.     return out;
  68. }
  69. template <class T> istream &operator>> (istream &in, vector <T> &a)
  70. {
  71.     for (auto &i : a)
  72.         in >> i;
  73.     return in;
  74. }
  75. template <class T> ostream &operator<< (ostream &out, vector <T> &a)
  76. {
  77.     for (auto &i : a)
  78.         out << i << " ";
  79.     return out;
  80. }
  81.  
  82. //Fast IO
  83. inline void Start()
  84. {
  85.     ios_base::sync_with_stdio(false);
  86.     cin.tie(0);
  87.     cout.tie(0);
  88.     //freopen("points.in", "r", stdin);
  89.     //freopen("points.out", "w", stdout);
  90. }
  91.  
  92. const int INF = 1e9;
  93.  
  94. signed main()
  95. {
  96.     Start();
  97.     int n;
  98.     cin >> n;
  99.     vector <int> a(n);
  100.     cin >> a;
  101.     vector <vector <bool>> del(n, vector <bool> (n, 0));
  102.     for (int i = 0; i < n; ++i)
  103.     {
  104.         del[i][i] = true;
  105.         if (i)
  106.             del[i - 1][i] = true;
  107.     }
  108.     for (int len = 1; len < n; ++len)
  109.     {
  110.         for (int l = 0; l + len < n; ++l)
  111.         {
  112.             int r = l + len;
  113.             for (int k = l + 1; k < r; ++k)
  114.                 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]));
  115.         }
  116.     }
  117.     vector <vector <int>> dp(n, vector <int> (n, 0));
  118.     for (int len = 1; len < n; ++len)
  119.     {
  120.         for (int l = 0; l + len < n; ++l)
  121.         {
  122.             int r = l + len;
  123.             dp[l][r] = INF;
  124.             if (a[r] < a[l])
  125.                 continue;
  126.             if (l + 1 < n && r - 1 >= 0)
  127.                 dp[l][r] = min(dp[l][r], dp[l + 1][r - 1]);
  128.             for (int k = l + 1; k < r; ++k)
  129.             {
  130.                 if (a[k - 1] <= a[k + 1] && (a[k - 1] > k && k < a[k + 1] || a[k - 1] < k && k > a[k + 1]))
  131.                     dp[l][r] = min(dp[l][r], dp[l][k - 1] + dp[k + 1][r] + 1);
  132.             }
  133.             if (del[l][r])
  134.                 dp[l][r] = min(dp[l][r], r - l - 1);
  135.         }
  136.     }
  137.     if (dp[0][n - 1] == INF)
  138.     {
  139.         cout << -1;
  140.         return 0;
  141.     }
  142.     cout << dp[0][n - 1];
  143.     return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement