fork download
  1. /***
  2. ** AUTHOR::ASHUTOSH MOUDGIL
  3. ***/
  4.  
  5.  
  6. #include<bits/stdc++.h>
  7. using namespace std;
  8.  
  9. #define pb push_back
  10. #define mp make_pair
  11. #define IOS ios_base::sync_with_stdio(false); cin.tie(NULL);
  12. #define F first
  13. #define S second
  14. #define int long long
  15. //#define ll long long
  16. #define all(x) x.begin(),x.end()
  17. #define endl '\n'
  18. #define vi vector<int>
  19. #define vlli vector<long long>
  20. #define pii pair<int,int>
  21. #define mod 1000000007
  22. #define LMAX 1e18
  23.  
  24. #define M 998244353
  25.  
  26. int tree[1501]={INT_MAX};
  27. vi target(501,INT_MAX);
  28. int dp[501][501];
  29. map<int,int> clr;
  30. void build(int s,int e, int i){
  31. if(s==e){
  32. tree[i] = target[s];
  33. return ;
  34. }
  35. int mid = (s+e)/2;
  36. build(s,mid,2*i);
  37. build(mid+1,e,2*i+1);
  38.  
  39. tree[i] = min(tree[2*i],tree[2*i+1]);
  40. return ;
  41. }
  42.  
  43. int query(int s, int e, int l , int r,int i){
  44. if( l > e || r < s )
  45. return INT_MAX;
  46. if(l<=s && r>=e){
  47. return tree[i];
  48. }
  49. int mid = (s+e)/2;
  50. return min(query(s,mid,l,r,2*i),query(mid+1,e,l,r,2*i+1));
  51.  
  52. }
  53.  
  54. int solve(int l,int r,int n){
  55.  
  56. if(l <0 || r>=n)
  57. return 0;
  58. if(l==r)
  59. return 1;
  60. if(l>r)
  61. return 0;
  62.  
  63. if(dp[l][r]!=-1)
  64. return dp[l][r];
  65.  
  66. int cmin = query(0,n-1,l,r,1);
  67. int ci = clr[cmin];
  68. //cout << "ci,l,r:"<<ci<<" "<<l<<" "<<r<<endl;
  69. int ans = 0,left=0,right=0;
  70.  
  71. for(int a = l;a<=ci;a++){
  72.  
  73. left += dp[l][a-1]*dp[a][ci-1];
  74. }
  75. for(int b = ci;b<=r;b++){
  76.  
  77. right += dp[ci+1][b]*dp[b+1][r];
  78. }
  79.  
  80. dp[l][r]=left*right;
  81.  
  82.  
  83. return dp[l][r];
  84.  
  85. }
  86.  
  87.  
  88.  
  89.  
  90. void mymain(){
  91. int n,m;
  92. cin>>n>>m;
  93.  
  94. //vi target(n);
  95. for(int i=0;i<n;i++){
  96. cin>>target[i];
  97. clr[target[i]]=i;
  98. }
  99. build(0,n-1,1);
  100. for(int i=0;i<501;i++)
  101. for(int j=0;j<501;j++)
  102. dp[i][j]=-1;
  103. cout << solve( 0,n-1,n )<<endl;
  104.  
  105.  
  106. }
  107.  
  108. signed main(){
  109. IOS;
  110. ///freopen("input.txt", "r", stdin);
  111. ///freopen("output.txt", "w", stdout);
  112. int t;
  113. t=1;
  114. //cin>>t;
  115. for(int tt=0;tt<t;tt++){
  116. //cout << "Case #" << tt+1 <<": ";
  117. mymain();
  118. }
  119. return 0;
  120. }
  121.  
Success #stdin #stdout 0s 17208KB
stdin
3 3 
1 2 3
stdout
0