fork download
  1. #include <bits/stdc++.h>
  2. // iostream is too mainstream
  3. #include <cstdio>
  4. // bitch please
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <cstdlib>
  8. #include <vector>
  9. #include <set>
  10. #include <map>
  11. #include <queue>
  12. #include <stack>
  13. #include <list>
  14. #include <cmath>
  15. #include <iomanip>
  16. #include <time.h>
  17. #define dibs reserve
  18. #define OVER9000 1234567890
  19. #define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
  20. #define tisic 47
  21. #define soclose 1e-8
  22. #define chocolate win
  23. // so much chocolate
  24. #define patkan 9
  25. #define ff first
  26. #define ss second
  27. #define abs(x) ((x < 0)?-(x):x)
  28. #define uint unsigned int
  29. #define dbl long double
  30. #define pi 3.14159265358979323846
  31. using namespace std;
  32. // mylittledoge
  33.  
  34. #ifdef DONLINE_JUDGE
  35. // palindromic tree is better than splay tree!
  36. #define lld I64d
  37. #endif
  38.  
  39. int ans[51][51][50][50];
  40. int N;
  41. vector<int> A;
  42.  
  43. void solve(int z, int k, int l, int r) {
  44. if(ans[z][k][l][r] >= 0) return;
  45. if(z >= k) {
  46. ans[z][k][l][r] =0;
  47. return;}
  48. solve(z+1,k,l,r);
  49. solve(z,k-1,l,r);
  50. ans[z][k][l][r] =max(ans[z+1][k][l][r],ans[z][k-1][l][r]);
  51. if(A[z] >= l && A[z] <= r) {
  52. solve(z+1,k,A[z],r);
  53. ans[z][k][l][r] =max(ans[z][k][l][r],ans[z+1][k][A[z]][r]+1);}
  54. if(A[k-1] >= l && A[k-1] <= r) {
  55. solve(z,k-1,l,A[k-1]);
  56. ans[z][k][l][r] =max(ans[z][k][l][r],ans[z][k-1][l][A[k-1]]+1);}
  57. if(z < k-1 && l <= A[k-1] && A[k-1] <= A[z] && A[z] <= r) {
  58. solve(z+1,k-1,A[k-1],A[z]);
  59. ans[z][k][l][r] =max(ans[z][k][l][r],ans[z+1][k-1][A[k-1]][A[z]]+2);}
  60. if(z < k-1 && l <= A[k-1] && A[k-1] <= r) {
  61. solve(z+1,k-1,A[k-1],r);
  62. ans[z][k][l][r] =max(ans[z][k][l][r],ans[z+1][k-1][A[k-1]][r]+1);}
  63. if(z < k-1 && l <= A[z] && A[z] <= r) {
  64. solve(z+1,k-1,l,A[z]);
  65. ans[z][k][l][r] =max(ans[z][k][l][r],ans[z+1][k-1][l][A[z]]+1);}
  66. }
  67.  
  68. int main() {
  69. cin.sync_with_stdio(0);
  70. cin.tie(0);
  71. cout << fixed << setprecision(10);
  72. #ifndef LOCAL
  73. freopen("subrev.in","r",stdin);
  74. freopen("subrev.out","w",stdout);
  75. #endif
  76. memset(ans,-10,sizeof(ans));
  77. cin >> N;
  78. A.resize(N);
  79. for(int i =0; i < N; i++) {
  80. cin >> A[i];
  81. A[i]--;}
  82.  
  83. solve(0,N,0,49);
  84. cout << ans[0][N][0][49] << "\n";
  85. return 0;}
  86.  
  87. // look at my code
  88. // my code is amazing
  89.  
Runtime error #stdin #stdout #stderr 0.01s 28872KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::ios_base::failure'
  what():  basic_filebuf::underflow error reading the file