Educational Codeforces Round 83 (Rated for Div. 2)E(区间DP)

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 int a[507];
 5 int dp[507];
 6 int main(){
 7     ios::sync_with_stdio(false);
 8     cin.tie(NULL);
 9     cout.tie(NULL);
10     int n;
11     cin>>n;
12     for(int i=1;i<=n;++i)
13         cin>>a[i];
14     for(int i=1;i<=n;++i){
15         stack<int>s;
16         s.push(a[i]);
17         dp[i]=dp[i-1]+1;
18         for(int j=i-1;j;--j){
19             int num=a[j];
20             while(!s.empty()&&s.top()==num){
21                 s.pop();
22                 ++num;
23             }
24             s.push(num);
25             dp[i]=min(dp[i],dp[j-1]+(int)s.size());
26         }
27     }
28     cout<<dp[n];
29     return 0;
30 }

 

posted @ 2020-03-12 08:11  sewage  阅读(129)  评论(0编辑  收藏  举报